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 VP8_ENCODER_BOOLHUFF_H_
20 #define VP8_ENCODER_BOOLHUFF_H_
22 #include "vpx_ports/mem.h"
23 #include "vpx/internal/vpx_codec_internal.h"
31 unsigned int lowvalue;
35 unsigned char *buffer;
36 unsigned char *buffer_end;
37 struct vpx_internal_error_info *error;
39 /* Variables used to track bit costs without outputing to the bitstream */
40 unsigned int measure_cost;
41 unsigned long bit_counter;
44 extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer, unsigned char *buffer_end);
46 extern void vp8_encode_value(BOOL_CODER *br, int data, int bits);
47 extern void vp8_stop_encode(BOOL_CODER *bc);
48 extern const unsigned int vp8_prob_cost[256];
51 DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
53 static int validate_buffer(const unsigned char *start,
55 const unsigned char *end,
56 struct vpx_internal_error_info *error)
58 if (start + len > start && start + len < end)
61 vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
62 "Truncated packet or corrupt partition ");
66 static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability)
69 int count = br->count;
70 unsigned int range = br->range;
71 unsigned int lowvalue = br->lowvalue;
72 register unsigned int shift;
74 #ifdef VP8_ENTROPY_STATS
75 #if defined(SECTIONBITS_OUTPUT)
78 Sectionbits[active_section] += vp8_prob_cost[255-probability];
80 Sectionbits[active_section] += vp8_prob_cost[probability];
85 split = 1 + (((range - 1) * probability) >> 8);
92 range = br->range - split;
95 shift = vp8_norm[range];
102 int offset = shift - count;
104 if ((lowvalue << (offset - 1)) & 0x80000000)
108 while (x >= 0 && br->buffer[x] == 0xff)
110 br->buffer[x] = (unsigned char)0;
117 validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
118 br->buffer[br->pos++] = (lowvalue >> (24 - offset));
122 lowvalue &= 0xffffff;
128 br->lowvalue = lowvalue;
136 #endif // VP8_ENCODER_BOOLHUFF_H_