blob: 60c546d25f5fa409c11f06ea6dbb14f17b355121 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
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 <stdlib.h>
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070013
14#include "aom_dsp/bitreader.h"
15#include "aom_dsp/prob.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "aom_dsp/aom_dsp_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017#include "aom_ports/mem.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070019#include "aom_util/endian_inl.h"
20
Yaowu Xuf883b422016-08-30 14:01:10 -070021int aom_reader_init(aom_reader *r, const uint8_t *buffer, size_t size,
22 aom_decrypt_cb decrypt_cb, void *decrypt_state) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070023 if (size && !buffer) {
24 return 1;
25 } else {
26 r->buffer_end = buffer + size;
27 r->buffer = buffer;
28 r->value = 0;
29 r->count = -8;
30 r->range = 255;
31 r->decrypt_cb = decrypt_cb;
32 r->decrypt_state = decrypt_state;
Yaowu Xuf883b422016-08-30 14:01:10 -070033 aom_reader_fill(r);
34 return aom_read_bit(r) != 0; // marker bit
Yaowu Xuc27fc142016-08-22 16:08:15 -070035 }
36}
37
Yaowu Xuf883b422016-08-30 14:01:10 -070038void aom_reader_fill(aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070039 const uint8_t *const buffer_end = r->buffer_end;
40 const uint8_t *buffer = r->buffer;
41 const uint8_t *buffer_start = buffer;
42 BD_VALUE value = r->value;
43 int count = r->count;
44 const size_t bytes_left = buffer_end - buffer;
45 const size_t bits_left = bytes_left * CHAR_BIT;
46 int shift = BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
47
48 if (r->decrypt_cb) {
Yaowu Xuf883b422016-08-30 14:01:10 -070049 size_t n = AOMMIN(sizeof(r->clear_buffer), bytes_left);
Yaowu Xuc27fc142016-08-22 16:08:15 -070050 r->decrypt_cb(r->decrypt_state, buffer, r->clear_buffer, (int)n);
51 buffer = r->clear_buffer;
52 buffer_start = r->clear_buffer;
53 }
54 if (bits_left > BD_VALUE_SIZE) {
55 const int bits = (shift & 0xfffffff8) + CHAR_BIT;
56 BD_VALUE nv;
57 BD_VALUE big_endian_values;
58 memcpy(&big_endian_values, buffer, sizeof(BD_VALUE));
59#if SIZE_MAX == 0xffffffffffffffffULL
60 big_endian_values = HToBE64(big_endian_values);
61#else
62 big_endian_values = HToBE32(big_endian_values);
63#endif
64 nv = big_endian_values >> (BD_VALUE_SIZE - bits);
65 count += bits;
66 buffer += (bits >> 3);
67 value = r->value | (nv << (shift & 0x7));
68 } else {
69 const int bits_over = (int)(shift + CHAR_BIT - (int)bits_left);
70 int loop_end = 0;
71 if (bits_over >= 0) {
72 count += LOTS_OF_BITS;
73 loop_end = bits_over;
74 }
75
76 if (bits_over < 0 || bits_left) {
77 while (shift >= loop_end) {
78 count += CHAR_BIT;
79 value |= (BD_VALUE)*buffer++ << shift;
80 shift -= CHAR_BIT;
81 }
82 }
83 }
84
85 // NOTE: Variable 'buffer' may not relate to 'r->buffer' after decryption,
86 // so we increase 'r->buffer' by the amount that 'buffer' moved, rather than
87 // assign 'buffer' to 'r->buffer'.
88 r->buffer += buffer - buffer_start;
89 r->value = value;
90 r->count = count;
91}
92
Yaowu Xuf883b422016-08-30 14:01:10 -070093const uint8_t *aom_reader_find_end(aom_reader *r) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070094 // Find the end of the coded buffer
95 while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
96 r->count -= CHAR_BIT;
97 r->buffer--;
98 }
99 return r->buffer;
100}