michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: #include "vpx_ports/mem.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: michael@0: #include "vp9/decoder/vp9_dboolhuff.h" michael@0: michael@0: // This is meant to be a large, positive constant that can still be efficiently michael@0: // loaded as an immediate (on platforms like ARM, for example). michael@0: // Even relatively modest values like 100 would work fine. michael@0: #define LOTS_OF_BITS 0x40000000 michael@0: michael@0: michael@0: int vp9_reader_init(vp9_reader *r, const uint8_t *buffer, size_t size) { michael@0: int marker_bit; michael@0: michael@0: r->buffer_end = buffer + size; michael@0: r->buffer = buffer; michael@0: r->value = 0; michael@0: r->count = -8; michael@0: r->range = 255; michael@0: michael@0: if (size && !buffer) michael@0: return 1; michael@0: michael@0: vp9_reader_fill(r); michael@0: marker_bit = vp9_read_bit(r); michael@0: return marker_bit != 0; michael@0: } michael@0: michael@0: void vp9_reader_fill(vp9_reader *r) { michael@0: const uint8_t *const buffer_end = r->buffer_end; michael@0: const uint8_t *buffer = r->buffer; michael@0: VP9_BD_VALUE value = r->value; michael@0: int count = r->count; michael@0: int shift = BD_VALUE_SIZE - 8 - (count + 8); michael@0: int loop_end = 0; michael@0: const int bits_left = (int)((buffer_end - buffer)*CHAR_BIT); michael@0: const int x = shift + CHAR_BIT - bits_left; michael@0: michael@0: if (x >= 0) { michael@0: count += LOTS_OF_BITS; michael@0: loop_end = x; michael@0: } michael@0: michael@0: if (x < 0 || bits_left) { michael@0: while (shift >= loop_end) { michael@0: count += CHAR_BIT; michael@0: value |= (VP9_BD_VALUE)*buffer++ << shift; michael@0: shift -= CHAR_BIT; michael@0: } michael@0: } michael@0: michael@0: r->buffer = buffer; michael@0: r->value = value; michael@0: r->count = count; michael@0: } michael@0: michael@0: const uint8_t *vp9_reader_find_end(vp9_reader *r) { michael@0: // Find the end of the coded buffer michael@0: while (r->count > CHAR_BIT && r->count < BD_VALUE_SIZE) { michael@0: r->count -= CHAR_BIT; michael@0: r->buffer--; michael@0: } michael@0: return r->buffer; michael@0: } michael@0: michael@0: int vp9_reader_has_error(vp9_reader *r) { michael@0: // Check if we have reached the end of the buffer. michael@0: // michael@0: // Variable 'count' stores the number of bits in the 'value' buffer, minus michael@0: // 8. The top byte is part of the algorithm, and the remainder is buffered michael@0: // to be shifted into it. So if count == 8, the top 16 bits of 'value' are michael@0: // occupied, 8 for the algorithm and 8 in the buffer. michael@0: // michael@0: // When reading a byte from the user's buffer, count is filled with 8 and michael@0: // one byte is filled into the value buffer. When we reach the end of the michael@0: // data, count is additionally filled with LOTS_OF_BITS. So when michael@0: // count == LOTS_OF_BITS - 1, the user's data has been exhausted. michael@0: // michael@0: // 1 if we have tried to decode bits after the end of stream was encountered. michael@0: // 0 No error. michael@0: return r->count > BD_VALUE_SIZE && r->count < LOTS_OF_BITS; michael@0: }