vp9_read_bit_buffer.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "vp9/decoder/vp9_read_bit_buffer.h"
  11. size_t vp9_rb_bytes_read(struct vp9_read_bit_buffer *rb) {
  12. return (rb->bit_offset + 7) >> 3;
  13. }
  14. int vp9_rb_read_bit(struct vp9_read_bit_buffer *rb) {
  15. const size_t off = rb->bit_offset;
  16. const size_t p = off >> 3;
  17. const int q = 7 - (int)(off & 0x7);
  18. if (rb->bit_buffer + p < rb->bit_buffer_end) {
  19. const int bit = (rb->bit_buffer[p] >> q) & 1;
  20. rb->bit_offset = off + 1;
  21. return bit;
  22. } else {
  23. rb->error_handler(rb->error_handler_data);
  24. return 0;
  25. }
  26. }
  27. int vp9_rb_read_literal(struct vp9_read_bit_buffer *rb, int bits) {
  28. int value = 0, bit;
  29. for (bit = bits - 1; bit >= 0; bit--)
  30. value |= vp9_rb_read_bit(rb) << bit;
  31. return value;
  32. }
  33. int vp9_rb_read_signed_literal(struct vp9_read_bit_buffer *rb,
  34. int bits) {
  35. const int value = vp9_rb_read_literal(rb, bits);
  36. return vp9_rb_read_bit(rb) ? -value : value;
  37. }