Line data Source code
1 : /*
2 : * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 : *
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.
9 : */
10 :
11 : /****************************************************************************
12 : *
13 : * Module Title : boolhuff.h
14 : *
15 : * Description : Bool Coder header file.
16 : *
17 : ****************************************************************************/
18 : #ifndef VP8_ENCODER_BOOLHUFF_H_
19 : #define VP8_ENCODER_BOOLHUFF_H_
20 :
21 : #include "vpx_ports/mem.h"
22 : #include "vpx/internal/vpx_codec_internal.h"
23 :
24 : #ifdef __cplusplus
25 : extern "C" {
26 : #endif
27 :
28 : typedef struct {
29 : unsigned int lowvalue;
30 : unsigned int range;
31 : int count;
32 : unsigned int pos;
33 : unsigned char *buffer;
34 : unsigned char *buffer_end;
35 : struct vpx_internal_error_info *error;
36 : } BOOL_CODER;
37 :
38 : extern void vp8_start_encode(BOOL_CODER *bc, unsigned char *buffer,
39 : unsigned char *buffer_end);
40 :
41 : extern void vp8_encode_value(BOOL_CODER *br, int data, int bits);
42 : extern void vp8_stop_encode(BOOL_CODER *bc);
43 : extern const unsigned int vp8_prob_cost[256];
44 :
45 : DECLARE_ALIGNED(16, extern const unsigned char, vp8_norm[256]);
46 :
47 0 : static int validate_buffer(const unsigned char *start, size_t len,
48 : const unsigned char *end,
49 : struct vpx_internal_error_info *error) {
50 0 : if (start + len > start && start + len < end) {
51 0 : return 1;
52 : } else {
53 0 : vpx_internal_error(error, VPX_CODEC_CORRUPT_FRAME,
54 : "Truncated packet or corrupt partition ");
55 : }
56 :
57 0 : return 0;
58 : }
59 0 : static void vp8_encode_bool(BOOL_CODER *br, int bit, int probability) {
60 : unsigned int split;
61 0 : int count = br->count;
62 0 : unsigned int range = br->range;
63 0 : unsigned int lowvalue = br->lowvalue;
64 : register int shift;
65 :
66 : #ifdef VP8_ENTROPY_STATS
67 : #if defined(SECTIONBITS_OUTPUT)
68 :
69 : if (bit)
70 : Sectionbits[active_section] += vp8_prob_cost[255 - probability];
71 : else
72 : Sectionbits[active_section] += vp8_prob_cost[probability];
73 :
74 : #endif
75 : #endif
76 :
77 0 : split = 1 + (((range - 1) * probability) >> 8);
78 :
79 0 : range = split;
80 :
81 0 : if (bit) {
82 0 : lowvalue += split;
83 0 : range = br->range - split;
84 : }
85 :
86 0 : shift = vp8_norm[range];
87 :
88 0 : range <<= shift;
89 0 : count += shift;
90 :
91 0 : if (count >= 0) {
92 0 : int offset = shift - count;
93 :
94 0 : if ((lowvalue << (offset - 1)) & 0x80000000) {
95 0 : int x = br->pos - 1;
96 :
97 0 : while (x >= 0 && br->buffer[x] == 0xff) {
98 0 : br->buffer[x] = (unsigned char)0;
99 0 : x--;
100 : }
101 :
102 0 : br->buffer[x] += 1;
103 : }
104 :
105 0 : validate_buffer(br->buffer + br->pos, 1, br->buffer_end, br->error);
106 0 : br->buffer[br->pos++] = (lowvalue >> (24 - offset));
107 :
108 0 : lowvalue <<= offset;
109 0 : shift = count;
110 0 : lowvalue &= 0xffffff;
111 0 : count -= 8;
112 : }
113 :
114 0 : lowvalue <<= shift;
115 0 : br->count = count;
116 0 : br->lowvalue = lowvalue;
117 0 : br->range = range;
118 0 : }
119 :
120 : #ifdef __cplusplus
121 : } // extern "C"
122 : #endif
123 :
124 : #endif // VP8_ENCODER_BOOLHUFF_H_
|