Home | History | Annotate | Download | only in decoder
      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_dboolhuff.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 
     21 
     22 int vp9_reader_init(vp9_reader *r, const uint8_t *buffer, size_t size) {
     23   int marker_bit;
     24 
     25   r->buffer_end = buffer + size;
     26   r->buffer = buffer;
     27   r->value = 0;
     28   r->count = -8;
     29   r->range = 255;
     30 
     31   if (size && !buffer)
     32     return 1;
     33 
     34   vp9_reader_fill(r);
     35   marker_bit = vp9_read_bit(r);
     36   return marker_bit != 0;
     37 }
     38 
     39 void vp9_reader_fill(vp9_reader *r) {
     40   const uint8_t *const buffer_end = r->buffer_end;
     41   const uint8_t *buffer = r->buffer;
     42   VP9_BD_VALUE value = r->value;
     43   int count = r->count;
     44   int shift = BD_VALUE_SIZE - 8 - (count + 8);
     45   int loop_end = 0;
     46   const int bits_left = (int)((buffer_end - buffer)*CHAR_BIT);
     47   const int x = shift + CHAR_BIT - bits_left;
     48 
     49   if (x >= 0) {
     50     count += LOTS_OF_BITS;
     51     loop_end = x;
     52   }
     53 
     54   if (x < 0 || bits_left) {
     55     while (shift >= loop_end) {
     56       count += CHAR_BIT;
     57       value |= (VP9_BD_VALUE)*buffer++ << shift;
     58       shift -= CHAR_BIT;
     59     }
     60   }
     61 
     62   r->buffer = buffer;
     63   r->value = value;
     64   r->count = count;
     65 }
     66 
     67 const uint8_t *vp9_reader_find_end(vp9_reader *r) {
     68   // Find the end of the coded buffer
     69   while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) {
     70     r->count -= CHAR_BIT;
     71     r->buffer--;
     72   }
     73   return r->buffer;
     74 }
     75 
     76 int vp9_reader_has_error(vp9_reader *r) {
     77   // Check if we have reached the end of the buffer.
     78   //
     79   // Variable 'count' stores the number of bits in the 'value' buffer, minus
     80   // 8. The top byte is part of the algorithm, and the remainder is buffered
     81   // to be shifted into it. So if count == 8, the top 16 bits of 'value' are
     82   // occupied, 8 for the algorithm and 8 in the buffer.
     83   //
     84   // When reading a byte from the user's buffer, count is filled with 8 and
     85   // one byte is filled into the value buffer. When we reach the end of the
     86   // data, count is additionally filled with LOTS_OF_BITS. So when
     87   // count == LOTS_OF_BITS - 1, the user's data has been exhausted.
     88   //
     89   // 1 if we have tried to decode bits after the end of stream was encountered.
     90   // 0 No error.
     91   return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS;
     92 }
     93