dboolhuff.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 VP8_DECODER_DBOOLHUFF_H_
  11. #define VP8_DECODER_DBOOLHUFF_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. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. typedef size_t VP8_BD_VALUE;
  22. #define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)
  23. /*This is meant to be a large, positive constant that can still be efficiently
  24. loaded as an immediate (on platforms like ARM, for example).
  25. Even relatively modest values like 100 would work fine.*/
  26. #define VP8_LOTS_OF_BITS (0x40000000)
  27. typedef struct
  28. {
  29. const unsigned char *user_buffer_end;
  30. const unsigned char *user_buffer;
  31. VP8_BD_VALUE value;
  32. int count;
  33. unsigned int range;
  34. vpx_decrypt_cb decrypt_cb;
  35. void *decrypt_state;
  36. } BOOL_DECODER;
  37. DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
  38. int vp8dx_start_decode(BOOL_DECODER *br,
  39. const unsigned char *source,
  40. unsigned int source_sz,
  41. vpx_decrypt_cb decrypt_cb,
  42. void *decrypt_state);
  43. void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
  44. static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
  45. unsigned int bit = 0;
  46. VP8_BD_VALUE value;
  47. unsigned int split;
  48. VP8_BD_VALUE bigsplit;
  49. int count;
  50. unsigned int range;
  51. split = 1 + (((br->range - 1) * probability) >> 8);
  52. if(br->count < 0)
  53. vp8dx_bool_decoder_fill(br);
  54. value = br->value;
  55. count = br->count;
  56. bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
  57. range = split;
  58. if (value >= bigsplit)
  59. {
  60. range = br->range - split;
  61. value = value - bigsplit;
  62. bit = 1;
  63. }
  64. {
  65. register int shift = vp8_norm[range];
  66. range <<= shift;
  67. value <<= shift;
  68. count -= shift;
  69. }
  70. br->value = value;
  71. br->count = count;
  72. br->range = range;
  73. return bit;
  74. }
  75. static INLINE int vp8_decode_value(BOOL_DECODER *br, int bits)
  76. {
  77. int z = 0;
  78. int bit;
  79. for (bit = bits - 1; bit >= 0; bit--)
  80. {
  81. z |= (vp8dx_decode_bool(br, 0x80) << bit);
  82. }
  83. return z;
  84. }
  85. static INLINE int vp8dx_bool_error(BOOL_DECODER *br)
  86. {
  87. /* Check if we have reached the end of the buffer.
  88. *
  89. * Variable 'count' stores the number of bits in the 'value' buffer, minus
  90. * 8. The top byte is part of the algorithm, and the remainder is buffered
  91. * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
  92. * occupied, 8 for the algorithm and 8 in the buffer.
  93. *
  94. * When reading a byte from the user's buffer, count is filled with 8 and
  95. * one byte is filled into the value buffer. When we reach the end of the
  96. * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
  97. * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
  98. */
  99. if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS))
  100. {
  101. /* We have tried to decode bits after the end of
  102. * stream was encountered.
  103. */
  104. return 1;
  105. }
  106. /* No error. */
  107. return 0;
  108. }
  109. #ifdef __cplusplus
  110. } // extern "C"
  111. #endif
  112. #endif // VP8_DECODER_DBOOLHUFF_H_