2
0

dboolhuff.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_VP8_DECODER_DBOOLHUFF_H_
  11. #define VPX_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. const unsigned char *user_buffer_end;
  29. const unsigned char *user_buffer;
  30. VP8_BD_VALUE value;
  31. int count;
  32. unsigned int range;
  33. vpx_decrypt_cb decrypt_cb;
  34. void *decrypt_state;
  35. } BOOL_DECODER;
  36. DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
  37. int vp8dx_start_decode(BOOL_DECODER *br, const unsigned char *source,
  38. unsigned int source_sz, vpx_decrypt_cb decrypt_cb,
  39. void *decrypt_state);
  40. void vp8dx_bool_decoder_fill(BOOL_DECODER *br);
  41. static int vp8dx_decode_bool(BOOL_DECODER *br, int probability) {
  42. unsigned int bit = 0;
  43. VP8_BD_VALUE value;
  44. unsigned int split;
  45. VP8_BD_VALUE bigsplit;
  46. int count;
  47. unsigned int range;
  48. split = 1 + (((br->range - 1) * probability) >> 8);
  49. if (br->count < 0) vp8dx_bool_decoder_fill(br);
  50. value = br->value;
  51. count = br->count;
  52. bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
  53. range = split;
  54. if (value >= bigsplit) {
  55. range = br->range - split;
  56. value = value - bigsplit;
  57. bit = 1;
  58. }
  59. {
  60. const unsigned char shift = vp8_norm[(unsigned char)range];
  61. range <<= shift;
  62. value <<= shift;
  63. count -= shift;
  64. }
  65. br->value = value;
  66. br->count = count;
  67. br->range = range;
  68. return bit;
  69. }
  70. static INLINE int vp8_decode_value(BOOL_DECODER *br, int bits) {
  71. int z = 0;
  72. int bit;
  73. for (bit = bits - 1; bit >= 0; bit--) {
  74. z |= (vp8dx_decode_bool(br, 0x80) << bit);
  75. }
  76. return z;
  77. }
  78. static INLINE int vp8dx_bool_error(BOOL_DECODER *br) {
  79. /* Check if we have reached the end of the buffer.
  80. *
  81. * Variable 'count' stores the number of bits in the 'value' buffer, minus
  82. * 8. The top byte is part of the algorithm, and the remainder is buffered
  83. * to be shifted into it. So if count == 8, the top 16 bits of 'value' are
  84. * occupied, 8 for the algorithm and 8 in the buffer.
  85. *
  86. * When reading a byte from the user's buffer, count is filled with 8 and
  87. * one byte is filled into the value buffer. When we reach the end of the
  88. * data, count is additionally filled with VP8_LOTS_OF_BITS. So when
  89. * count == VP8_LOTS_OF_BITS - 1, the user's data has been exhausted.
  90. */
  91. if ((br->count > VP8_BD_VALUE_SIZE) && (br->count < VP8_LOTS_OF_BITS)) {
  92. /* We have tried to decode bits after the end of
  93. * stream was encountered.
  94. */
  95. return 1;
  96. }
  97. /* No error. */
  98. return 0;
  99. }
  100. #ifdef __cplusplus
  101. } // extern "C"
  102. #endif
  103. #endif // VPX_VP8_DECODER_DBOOLHUFF_H_