2
0

bitreader.h 4.4 KB

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