2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #include "./vpx_config.h"
14 #include "vpx_dsp/bitreader.h"
15 #include "vpx_dsp/prob.h"
16 #include "vpx_dsp/vpx_dsp_common.h"
17 #include "vpx_ports/mem.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "vpx_util/endian_inl.h"
21 int vpx_reader_init(vpx_reader
*r
,
22 const uint8_t *buffer
,
24 vpx_decrypt_cb decrypt_cb
,
25 void *decrypt_state
) {
26 if (size
&& !buffer
) {
29 r
->buffer_end
= buffer
+ size
;
34 r
->decrypt_cb
= decrypt_cb
;
35 r
->decrypt_state
= decrypt_state
;
37 return vpx_read_bit(r
) != 0; // marker bit
41 void vpx_reader_fill(vpx_reader
*r
) {
42 const uint8_t *const buffer_end
= r
->buffer_end
;
43 const uint8_t *buffer
= r
->buffer
;
44 const uint8_t *buffer_start
= buffer
;
45 BD_VALUE value
= r
->value
;
47 const size_t bytes_left
= buffer_end
- buffer
;
48 const size_t bits_left
= bytes_left
* CHAR_BIT
;
49 int shift
= BD_VALUE_SIZE
- CHAR_BIT
- (count
+ CHAR_BIT
);
52 size_t n
= VPXMIN(sizeof(r
->clear_buffer
), bytes_left
);
53 r
->decrypt_cb(r
->decrypt_state
, buffer
, r
->clear_buffer
, (int)n
);
54 buffer
= r
->clear_buffer
;
55 buffer_start
= r
->clear_buffer
;
57 if (bits_left
> BD_VALUE_SIZE
) {
58 const int bits
= (shift
& 0xfffffff8) + CHAR_BIT
;
60 BD_VALUE big_endian_values
;
61 memcpy(&big_endian_values
, buffer
, sizeof(BD_VALUE
));
62 #if SIZE_MAX == 0xffffffffffffffffULL
63 big_endian_values
= HToBE64(big_endian_values
);
65 big_endian_values
= HToBE32(big_endian_values
);
67 nv
= big_endian_values
>> (BD_VALUE_SIZE
- bits
);
69 buffer
+= (bits
>> 3);
70 value
= r
->value
| (nv
<< (shift
& 0x7));
72 const int bits_over
= (int)(shift
+ CHAR_BIT
- bits_left
);
75 count
+= LOTS_OF_BITS
;
79 if (bits_over
< 0 || bits_left
) {
80 while (shift
>= loop_end
) {
82 value
|= (BD_VALUE
)*buffer
++ << shift
;
88 // NOTE: Variable 'buffer' may not relate to 'r->buffer' after decryption,
89 // so we increase 'r->buffer' by the amount that 'buffer' moved, rather than
90 // assign 'buffer' to 'r->buffer'.
91 r
->buffer
+= buffer
- buffer_start
;
96 const uint8_t *vpx_reader_find_end(vpx_reader
*r
) {
97 // Find the end of the coded buffer
98 while (r
->count
> CHAR_BIT
&& r
->count
< BD_VALUE_SIZE
) {