vp9_reader.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "vpx_ports/mem.h"
  11. #include "vpx_mem/vpx_mem.h"
  12. #include "vp9/decoder/vp9_reader.h"
  13. // This is meant to be a large, positive constant that can still be efficiently
  14. // loaded as an immediate (on platforms like ARM, for example).
  15. // Even relatively modest values like 100 would work fine.
  16. #define LOTS_OF_BITS 0x40000000
  17. int vp9_reader_init(vp9_reader *r,
  18. const uint8_t *buffer,
  19. size_t size,
  20. vpx_decrypt_cb decrypt_cb,
  21. void *decrypt_state) {
  22. if (size && !buffer) {
  23. return 1;
  24. } else {
  25. r->buffer_end = buffer + size;
  26. r->buffer = buffer;
  27. r->value = 0;
  28. r->count = -8;
  29. r->range = 255;
  30. r->decrypt_cb = decrypt_cb;
  31. r->decrypt_state = decrypt_state;
  32. vp9_reader_fill(r);
  33. return vp9_read_bit(r) != 0; // marker bit
  34. }
  35. }
  36. void vp9_reader_fill(vp9_reader *r) {
  37. const uint8_t *const buffer_end = r->buffer_end;
  38. const uint8_t *buffer = r->buffer;
  39. const uint8_t *buffer_start = buffer;
  40. BD_VALUE value = r->value;
  41. int count = r->count;
  42. int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
  43. int loop_end = 0;
  44. const size_t bytes_left = buffer_end - buffer;
  45. const size_t bits_left = bytes_left * CHAR_BIT;
  46. const int x = (int)(shift + CHAR_BIT - bits_left);
  47. if (r->decrypt_cb) {
  48. size_t n = MIN(sizeof(r->clear_buffer), bytes_left);
  49. r->decrypt_cb(r->decrypt_state, buffer, r->clear_buffer, (int)n);
  50. buffer = r->clear_buffer;
  51. buffer_start = r->clear_buffer;
  52. }
  53. if (x >= 0) {
  54. count += LOTS_OF_BITS;
  55. loop_end = x;
  56. }
  57. if (x < 0 || bits_left) {
  58. while (shift >= loop_end) {
  59. count += CHAR_BIT;
  60. value |= (BD_VALUE)*buffer++ << shift;
  61. shift -= CHAR_BIT;
  62. }
  63. }
  64. // NOTE: Variable 'buffer' may not relate to 'r->buffer' after decryption,
  65. // so we increase 'r->buffer' by the amount that 'buffer' moved, rather than
  66. // assign 'buffer' to 'r->buffer'.
  67. r->buffer += buffer - buffer_start;
  68. r->value = value;
  69. r->count = count;
  70. }
  71. const uint8_t *vp9_reader_find_end(vp9_reader *r) {
  72. // Find the end of the coded buffer
  73. while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
  74. r->count -= CHAR_BIT;
  75. r->buffer--;
  76. }
  77. return r->buffer;
  78. }
  79. int vp9_reader_has_error(vp9_reader *r) {
  80. // Check if we have reached the end of the buffer.
  81. //
  82. // Variable 'count' stores the number of bits in the 'value' buffer, minus
  83. // 8. The top byte is part of the algorithm, and the remainder is buffered
  84. // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
  85. // occupied, 8 for the algorithm and 8 in the buffer.
  86. //
  87. // When reading a byte from the user's buffer, count is filled with 8 and
  88. // one byte is filled into the value buffer. When we reach the end of the
  89. // data, count is additionally filled with LOTS_OF_BITS. So when
  90. // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
  91. //
  92. // 1 if we have tried to decode bits after the end of stream was encountered.
  93. // 0 No error.
  94. return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
  95. }