vp9_reader.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 VP9_DECODER_VP9_READER_H_
  11. #define VP9_DECODER_VP9_READER_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 "vp9/common/vp9_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. typedef struct {
  25. // Be careful when reordering this struct, it may impact the cache negatively.
  26. BD_VALUE value;
  27. unsigned int range;
  28. int count;
  29. const uint8_t *buffer_end;
  30. const uint8_t *buffer;
  31. vpx_decrypt_cb decrypt_cb;
  32. void *decrypt_state;
  33. uint8_t clear_buffer[sizeof(BD_VALUE) + 1];
  34. } vp9_reader;
  35. int vp9_reader_init(vp9_reader *r,
  36. const uint8_t *buffer,
  37. size_t size,
  38. vpx_decrypt_cb decrypt_cb,
  39. void *decrypt_state);
  40. void vp9_reader_fill(vp9_reader *r);
  41. int vp9_reader_has_error(vp9_reader *r);
  42. const uint8_t *vp9_reader_find_end(vp9_reader *r);
  43. static INLINE int vp9_read(vp9_reader *r, int prob) {
  44. unsigned int bit = 0;
  45. BD_VALUE value;
  46. BD_VALUE bigsplit;
  47. int count;
  48. unsigned int range;
  49. unsigned int split = (r->range * prob + (256 - prob)) >> CHAR_BIT;
  50. if (r->count < 0)
  51. vp9_reader_fill(r);
  52. value = r->value;
  53. count = r->count;
  54. bigsplit = (BD_VALUE)split << (BD_VALUE_SIZE - CHAR_BIT);
  55. range = split;
  56. if (value >= bigsplit) {
  57. range = r->range - split;
  58. value = value - bigsplit;
  59. bit = 1;
  60. }
  61. {
  62. register unsigned int shift = vp9_norm[range];
  63. range <<= shift;
  64. value <<= shift;
  65. count -= shift;
  66. }
  67. r->value = value;
  68. r->count = count;
  69. r->range = range;
  70. return bit;
  71. }
  72. static INLINE int vp9_read_bit(vp9_reader *r) {
  73. return vp9_read(r, 128); // vp9_prob_half
  74. }
  75. static INLINE int vp9_read_literal(vp9_reader *r, int bits) {
  76. int literal = 0, bit;
  77. for (bit = bits - 1; bit >= 0; bit--)
  78. literal |= vp9_read_bit(r) << bit;
  79. return literal;
  80. }
  81. static INLINE int vp9_read_tree(vp9_reader *r, const vp9_tree_index *tree,
  82. const vp9_prob *probs) {
  83. vp9_tree_index i = 0;
  84. while ((i = tree[i + vp9_read(r, probs[i >> 1])]) > 0)
  85. continue;
  86. return -i;
  87. }
  88. #ifdef __cplusplus
  89. } // extern "C"
  90. #endif
  91. #endif // VP9_DECODER_VP9_READER_H_