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: michael@0: #include "dboolhuff.h" michael@0: michael@0: int vp8dx_start_decode(BOOL_DECODER *br, michael@0: const unsigned char *source, michael@0: unsigned int source_sz, michael@0: vp8_decrypt_cb *decrypt_cb, michael@0: void *decrypt_state) michael@0: { michael@0: br->user_buffer_end = source+source_sz; michael@0: br->user_buffer = source; michael@0: br->value = 0; michael@0: br->count = -8; michael@0: br->range = 255; michael@0: br->decrypt_cb = decrypt_cb; michael@0: br->decrypt_state = decrypt_state; michael@0: michael@0: if (source_sz && !source) michael@0: return 1; michael@0: michael@0: /* Populate the buffer */ michael@0: vp8dx_bool_decoder_fill(br); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: void vp8dx_bool_decoder_fill(BOOL_DECODER *br) michael@0: { michael@0: const unsigned char *bufptr = br->user_buffer; michael@0: VP8_BD_VALUE value = br->value; michael@0: int count = br->count; michael@0: int shift = VP8_BD_VALUE_SIZE - 8 - (count + 8); michael@0: size_t bytes_left = br->user_buffer_end - bufptr; michael@0: size_t bits_left = bytes_left * CHAR_BIT; michael@0: int x = (int)(shift + CHAR_BIT - bits_left); michael@0: int loop_end = 0; michael@0: unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1]; michael@0: michael@0: if (br->decrypt_cb) { michael@0: size_t n = bytes_left > sizeof(decrypted) ? sizeof(decrypted) : bytes_left; michael@0: br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n); michael@0: bufptr = decrypted; michael@0: } michael@0: michael@0: if(x >= 0) michael@0: { michael@0: count += VP8_LOTS_OF_BITS; michael@0: loop_end = x; michael@0: } michael@0: michael@0: if (x < 0 || bits_left) michael@0: { michael@0: while(shift >= loop_end) michael@0: { michael@0: count += CHAR_BIT; michael@0: value |= (VP8_BD_VALUE)*bufptr << shift; michael@0: ++bufptr; michael@0: ++br->user_buffer; michael@0: shift -= CHAR_BIT; michael@0: } michael@0: } michael@0: michael@0: br->value = value; michael@0: br->count = count; michael@0: }