2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 /****************************************************************************
14 * Module Title : boolhuff.h
16 * Description : Bool Coder header file.
18 ****************************************************************************/
19 #ifndef __INC_BOOLHUFF_H
20 #define __INC_BOOLHUFF_H
22 #include "vpx_ports/mem.h"
23 #include "vpx/internal/vpx_codec_internal.h"
27 unsigned int lowvalue;
31 unsigned char *buffer;
32 unsigned char *buffer_end;
33 struct vpx_internal_error_info *error;
35 /* Variables used to track bit costs without outputing to the bitstream */
36 unsigned int measure_cost;
37 unsigned long bit_counter;
40 extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer, unsigned char *buffer_end);
42 extern void vp8_encode_value(BOOL_CODER *br, int data, int bits);
43 extern void vp8_stop_encode(BOOL_CODER *bc);
44 extern const unsigned int vp8_prob_cost[256];
47 DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
49 static int validate_buffer(const unsigned char *start,
51 const unsigned char *end,
52 struct vpx_internal_error_info *error)
54 if (start + len > start && start + len < end)
57 vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
58 "Truncated packet or corrupt partition ");
62 static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability)
65 int count = br->count;
66 unsigned int range = br->range;
67 unsigned int lowvalue = br->lowvalue;
68 register unsigned int shift;
70 #ifdef VP8_ENTROPY_STATS
71 #if defined(SECTIONBITS_OUTPUT)
74 Sectionbits[active_section] += vp8_prob_cost[255-probability];
76 Sectionbits[active_section] += vp8_prob_cost[probability];
81 split = 1 + (((range - 1) * probability) >> 8);
88 range = br->range - split;
91 shift = vp8_norm[range];
98 int offset = shift - count;
100 if ((lowvalue << (offset - 1)) & 0x80000000)
104 while (x >= 0 && br->buffer[x] == 0xff)
106 br->buffer[x] = (unsigned char)0;
113 validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
114 br->buffer[br->pos++] = (lowvalue >> (24 - offset));
118 lowvalue &= 0xffffff;
124 br->lowvalue = lowvalue;