bitreader.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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,
  40. const uint8_t *buffer,
  41. size_t size,
  42. vpx_decrypt_cb decrypt_cb,
  43. void *decrypt_state);
  44. void vpx_reader_fill(vpx_reader *r);
  45. const uint8_t *vpx_reader_find_end(vpx_reader *r);
  46. static INLINE int vpx_reader_has_error(vpx_reader *r) {
  47. // Check if we have reached the end of the buffer.
  48. //
  49. // Variable 'count' stores the number of bits in the 'value' buffer, minus
  50. // 8. The top byte is part of the algorithm, and the remainder is buffered
  51. // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
  52. // occupied, 8 for the algorithm and 8 in the buffer.
  53. //
  54. // When reading a byte from the user's buffer, count is filled with 8 and
  55. // one byte is filled into the value buffer. When we reach the end of the
  56. // data, count is additionally filled with LOTS_OF_BITS. So when
  57. // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
  58. //
  59. // 1 if we have tried to decode bits after the end of stream was encountered.
  60. // 0 No error.
  61. return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
  62. }
  63. static INLINE int vpx_read(vpx_reader *r, int prob) {
  64. unsigned int bit = 0;
  65. BD_VALUE value;
  66. BD_VALUE bigsplit;
  67. int count;
  68. unsigned int range;
  69. unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
  70. if (r->count < 0)
  71. vpx_reader_fill(r);
  72. value = r->value;
  73. count = r->count;
  74. bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
  75. range = split;
  76. if (value >= bigsplit) {
  77. range = r->range - split;
  78. value = value - bigsplit;
  79. bit = 1;
  80. }
  81. {
  82. register int shift = vpx_norm[range];
  83. range <<= shift;
  84. value <<= shift;
  85. count -= shift;
  86. }
  87. r->value = value;
  88. r->count = count;
  89. r->range = range;
  90. return bit;
  91. }
  92. static INLINE int vpx_read_bit(vpx_reader *r) {
  93. return vpx_read(r, 128); // vpx_prob_half
  94. }
  95. static INLINE int vpx_read_literal(vpx_reader *r, int bits) {
  96. int literal = 0, bit;
  97. for (bit = bits - 1; bit >= 0; bit--)
  98. literal |= vpx_read_bit(r) << bit;
  99. return literal;
  100. }
  101. static INLINE int vpx_read_tree(vpx_reader *r, const vpx_tree_index *tree,
  102. const vpx_prob *probs) {
  103. vpx_tree_index i = 0;
  104. while ((i = tree[i + vpx_read(r, probs[i >> 1])]) > 0)
  105. continue;
  106. return -i;
  107. }
  108. #ifdef __cplusplus
  109. } // extern "C"
  110. #endif
  111. #endif // VPX_DSP_BITREADER_H_