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: /**************************************************************************** michael@0: * michael@0: * Module Title : boolhuff.h michael@0: * michael@0: * Description : Bool Coder header file. michael@0: * michael@0: ****************************************************************************/ michael@0: #ifndef __INC_BOOLHUFF_H michael@0: #define __INC_BOOLHUFF_H michael@0: michael@0: #include "vpx_ports/mem.h" michael@0: #include "vpx/internal/vpx_codec_internal.h" michael@0: michael@0: typedef struct michael@0: { michael@0: unsigned int lowvalue; michael@0: unsigned int range; michael@0: int count; michael@0: unsigned int pos; michael@0: unsigned char *buffer; michael@0: unsigned char *buffer_end; michael@0: struct vpx_internal_error_info *error; michael@0: michael@0: /* Variables used to track bit costs without outputing to the bitstream */ michael@0: unsigned int measure_cost; michael@0: unsigned long bit_counter; michael@0: } BOOL_CODER; michael@0: michael@0: extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer, unsigned char *buffer_end); michael@0: michael@0: extern void vp8_encode_value(BOOL_CODER *br, int data, int bits); michael@0: extern void vp8_stop_encode(BOOL_CODER *bc); michael@0: extern const unsigned int vp8_prob_cost[256]; michael@0: michael@0: michael@0: DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]); michael@0: michael@0: static int validate_buffer(const unsigned char *start, michael@0: size_t len, michael@0: const unsigned char *end, michael@0: struct vpx_internal_error_info *error) michael@0: { michael@0: if (start + len > start && start + len < end) michael@0: return 1; michael@0: else michael@0: vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME, michael@0: "Truncated packet or corrupt partition "); michael@0: michael@0: return 0; michael@0: } michael@0: static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability) michael@0: { michael@0: unsigned int split; michael@0: int count = br->count; michael@0: unsigned int range = br->range; michael@0: unsigned int lowvalue = br->lowvalue; michael@0: register unsigned int shift; michael@0: michael@0: #ifdef VP8_ENTROPY_STATS michael@0: #if defined(SECTIONBITS_OUTPUT) michael@0: michael@0: if (bit) michael@0: Sectionbits[active_section] += vp8_prob_cost[255-probability]; michael@0: else michael@0: Sectionbits[active_section] += vp8_prob_cost[probability]; michael@0: michael@0: #endif michael@0: #endif michael@0: michael@0: split = 1 + (((range - 1) * probability) >> 8); michael@0: michael@0: range = split; michael@0: michael@0: if (bit) michael@0: { michael@0: lowvalue += split; michael@0: range = br->range - split; michael@0: } michael@0: michael@0: shift = vp8_norm[range]; michael@0: michael@0: range <<= shift; michael@0: count += shift; michael@0: michael@0: if (count >= 0) michael@0: { michael@0: int offset = shift - count; michael@0: michael@0: if ((lowvalue << (offset - 1)) & 0x80000000) michael@0: { michael@0: int x = br->pos - 1; michael@0: michael@0: while (x >= 0 && br->buffer[x] == 0xff) michael@0: { michael@0: br->buffer[x] = (unsigned char)0; michael@0: x--; michael@0: } michael@0: michael@0: br->buffer[x] += 1; michael@0: } michael@0: michael@0: validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error); michael@0: br->buffer[br->pos++] = (lowvalue >> (24 - offset)); michael@0: michael@0: lowvalue <<= offset; michael@0: shift = count; michael@0: lowvalue &= 0xffffff; michael@0: count -= 8 ; michael@0: } michael@0: michael@0: lowvalue <<= shift; michael@0: br->count = count; michael@0: br->lowvalue = lowvalue; michael@0: br->range = range; michael@0: } michael@0: michael@0: #endif