bitreader.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2010 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 <stdlib.h>
  11. #include "./vpx_config.h"
  12. #include "vpx_dsp/bitreader.h"
  13. #include "vpx_dsp/prob.h"
  14. #include "vpx_dsp/vpx_dsp_common.h"
  15. #include "vpx_ports/mem.h"
  16. #include "vpx_mem/vpx_mem.h"
  17. #include "vpx_util/endian_inl.h"
  18. int vpx_reader_init(vpx_reader *r, const uint8_t *buffer, size_t size,
  19. vpx_decrypt_cb decrypt_cb, void *decrypt_state) {
  20. if (size && !buffer) {
  21. return 1;
  22. } else {
  23. r->buffer_end = buffer + size;
  24. r->buffer = buffer;
  25. r->value = 0;
  26. r->count = -8;
  27. r->range = 255;
  28. r->decrypt_cb = decrypt_cb;
  29. r->decrypt_state = decrypt_state;
  30. vpx_reader_fill(r);
  31. return vpx_read_bit(r) != 0; // marker bit
  32. }
  33. }
  34. void vpx_reader_fill(vpx_reader *r) {
  35. const uint8_t *const buffer_end = r->buffer_end;
  36. const uint8_t *buffer = r->buffer;
  37. const uint8_t *buffer_start = buffer;
  38. BD_VALUE value = r->value;
  39. int count = r->count;
  40. const size_t bytes_left = buffer_end - buffer;
  41. const size_t bits_left = bytes_left * CHAR_BIT;
  42. int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
  43. if (r->decrypt_cb) {
  44. size_t n = VPXMIN(sizeof(r->clear_buffer), bytes_left);
  45. r->decrypt_cb(r->decrypt_state, buffer, r->clear_buffer, (int)n);
  46. buffer = r->clear_buffer;
  47. buffer_start = r->clear_buffer;
  48. }
  49. if (bits_left > BD_VALUE_SIZE) {
  50. const int bits = (shift & 0xfffffff8) + CHAR_BIT;
  51. BD_VALUE nv;
  52. BD_VALUE big_endian_values;
  53. memcpy(&big_endian_values, buffer, sizeof(BD_VALUE));
  54. #if SIZE_MAX == 0xffffffffffffffffULL
  55. big_endian_values = HToBE64(big_endian_values);
  56. #else
  57. big_endian_values = HToBE32(big_endian_values);
  58. #endif
  59. nv = big_endian_values >> (BD_VALUE_SIZE - bits);
  60. count += bits;
  61. buffer += (bits >> 3);
  62. value = r->value | (nv << (shift & 0x7));
  63. } else {
  64. const int bits_over = (int)(shift + CHAR_BIT - (int)bits_left);
  65. int loop_end = 0;
  66. if (bits_over >= 0) {
  67. count += LOTS_OF_BITS;
  68. loop_end = bits_over;
  69. }
  70. if (bits_over < 0 || bits_left) {
  71. while (shift >= loop_end) {
  72. count += CHAR_BIT;
  73. value |= (BD_VALUE)*buffer++ << shift;
  74. shift -= CHAR_BIT;
  75. }
  76. }
  77. }
  78. // NOTE: Variable 'buffer' may not relate to 'r->buffer' after decryption,
  79. // so we increase 'r->buffer' by the amount that 'buffer' moved, rather than
  80. // assign 'buffer' to 'r->buffer'.
  81. r->buffer += buffer - buffer_start;
  82. r->value = value;
  83. r->count = count;
  84. }
  85. const uint8_t *vpx_reader_find_end(vpx_reader *r) {
  86. // Find the end of the coded buffer
  87. while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
  88. r->count -= CHAR_BIT;
  89. r->buffer--;
  90. }
  91. return r->buffer;
  92. }