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 : vp9_boolhuff.h michael@0: * michael@0: * Description : Bool Coder header file. michael@0: * michael@0: ****************************************************************************/ michael@0: #ifndef VP9_ENCODER_VP9_BOOLHUFF_H_ michael@0: #define VP9_ENCODER_VP9_BOOLHUFF_H_ michael@0: michael@0: #include "vpx_ports/mem.h" michael@0: michael@0: typedef struct { michael@0: unsigned int lowvalue; michael@0: unsigned int range; michael@0: unsigned int value; michael@0: int count; michael@0: unsigned int pos; michael@0: uint8_t *buffer; 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: } vp9_writer; michael@0: michael@0: extern const unsigned int vp9_prob_cost[256]; michael@0: michael@0: void vp9_start_encode(vp9_writer *bc, uint8_t *buffer); michael@0: void vp9_stop_encode(vp9_writer *bc); michael@0: michael@0: DECLARE_ALIGNED(16, extern const unsigned char, vp9_norm[256]); michael@0: michael@0: static void vp9_write(vp9_writer *br, int bit, int probability) { 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 ENTROPY_STATS michael@0: #if defined(SECTIONBITS_OUTPUT) michael@0: michael@0: if (bit) michael@0: Sectionbits[active_section] += vp9_prob_cost[255 - probability]; michael@0: else michael@0: Sectionbits[active_section] += vp9_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: lowvalue += split; michael@0: range = br->range - split; michael@0: } michael@0: michael@0: shift = vp9_norm[range]; michael@0: michael@0: range <<= shift; michael@0: count += shift; michael@0: michael@0: if (count >= 0) { michael@0: int offset = shift - count; michael@0: michael@0: if ((lowvalue << (offset - 1)) & 0x80000000) { michael@0: int x = br->pos - 1; michael@0: michael@0: while (x >= 0 && br->buffer[x] == 0xff) { michael@0: br->buffer[x] = 0; michael@0: x--; michael@0: } michael@0: michael@0: br->buffer[x] += 1; michael@0: } michael@0: michael@0: br->buffer[br->pos++] = (lowvalue >> (24 - offset)); 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: static void vp9_write_bit(vp9_writer *w, int bit) { michael@0: vp9_write(w, bit, 128); // vp9_prob_half michael@0: } michael@0: michael@0: static void vp9_write_literal(vp9_writer *w, int data, int bits) { michael@0: int bit; michael@0: michael@0: for (bit = bits - 1; bit >= 0; bit--) michael@0: vp9_write_bit(w, 1 & (data >> bit)); michael@0: } michael@0: michael@0: michael@0: #endif // VP9_ENCODER_VP9_BOOLHUFF_H_