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