bitreader.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef VPX_DSP_BITREADER_H_
  11. #define VPX_DSP_BITREADER_H_
  12. #include <stddef.h>
  13. #include <limits.h>
  14. #include "./vpx_config.h"
  15. #include "vpx_ports/mem.h"
  16. #include "vpx/vp8dx.h"
  17. #include "vpx/vpx_integer.h"
  18. #include "vpx_dsp/prob.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef size_t BD_VALUE;
  23. #define BD_VALUE_SIZE ((int)sizeof(BD_VALUE) * CHAR_BIT)
  24. // This is meant to be a large, positive constant that can still be efficiently
  25. // loaded as an immediate (on platforms like ARM, for example).
  26. // Even relatively modest values like 100 would work fine.
  27. #define LOTS_OF_BITS 0x40000000
  28. typedef struct {
  29. // Be careful when reordering this struct, it may impact the cache negatively.
  30. BD_VALUE value;
  31. unsigned int range;
  32. int count;
  33. const uint8_t *buffer_end;
  34. const uint8_t *buffer;
  35. vpx_decrypt_cb decrypt_cb;
  36. void *decrypt_state;
  37. uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
  38. } vpx_reader;
  39. int vpx_reader_init(vpx_reader *r, const uint8_t *buffer, size_t size,
  40. vpx_decrypt_cb decrypt_cb, void *decrypt_state);
  41. void vpx_reader_fill(vpx_reader *r);
  42. const uint8_t *vpx_reader_find_end(vpx_reader *r);
  43. static INLINE int vpx_reader_has_error(vpx_reader *r) {
  44. // Check if we have reached the end of the buffer.
  45. //
  46. // Variable 'count' stores the number of bits in the 'value' buffer, minus
  47. // 8. The top byte is part of the algorithm, and the remainder is buffered
  48. // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
  49. // occupied, 8 for the algorithm and 8 in the buffer.
  50. //
  51. // When reading a byte from the user's buffer, count is filled with 8 and
  52. // one byte is filled into the value buffer. When we reach the end of the
  53. // data, count is additionally filled with LOTS_OF_BITS. So when
  54. // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
  55. //
  56. // 1 if we have tried to decode bits after the end of stream was encountered.
  57. // 0 No error.
  58. return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
  59. }
  60. static INLINE int vpx_read(vpx_reader *r, int prob) {
  61. unsigned int bit = 0;
  62. BD_VALUE value;
  63. BD_VALUE bigsplit;
  64. int count;
  65. unsigned int range;
  66. unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
  67. if (r->count < 0) vpx_reader_fill(r);
  68. value = r->value;
  69. count = r->count;
  70. bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
  71. range = split;
  72. if (value >= bigsplit) {
  73. range = r->range - split;
  74. value = value - bigsplit;
  75. bit = 1;
  76. }
  77. {
  78. register int shift = vpx_norm[range];
  79. range <<= shift;
  80. value <<= shift;
  81. count -= shift;
  82. }
  83. r->value = value;
  84. r->count = count;
  85. r->range = range;
  86. return bit;
  87. }
  88. static INLINE int vpx_read_bit(vpx_reader *r) {
  89. return vpx_read(r, 128); // vpx_prob_half
  90. }
  91. static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
  92. int literal = 0, bit;
  93. for (bit = bits - 1; bit >= 0; bit--) literal |= vpx_read_bit(r) << bit;
  94. return literal;
  95. }
  96. static INLINE int vpx_read_tree(vpx_reader *r, const vpx_tree_index *tree,
  97. const vpx_prob *probs) {
  98. vpx_tree_index i = 0;
  99. while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0) continue;
  100. return -i;
  101. }
  102. #ifdef __cplusplus
  103. } // extern "C"
  104. #endif
  105. #endif // VPX_DSP_BITREADER_H_