|
808
|
1 /*
|
|
|
2 * Common bit i/o utils
|
|
|
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
|
|
|
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
|
|
|
5 *
|
|
|
6 * This file is part of FFmpeg.
|
|
|
7 *
|
|
|
8 * FFmpeg is free software; you can redistribute it and/or
|
|
|
9 * modify it under the terms of the GNU Lesser General Public
|
|
|
10 * License as published by the Free Software Foundation; either
|
|
|
11 * version 2.1 of the License, or (at your option) any later version.
|
|
|
12 *
|
|
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
16 * Lesser General Public License for more details.
|
|
|
17 *
|
|
|
18 * You should have received a copy of the GNU Lesser General Public
|
|
|
19 * License along with FFmpeg; if not, write to the Free Software
|
|
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
21 *
|
|
|
22 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
|
|
|
23 */
|
|
|
24
|
|
|
25 /**
|
|
|
26 * @file bitstream.c
|
|
|
27 * bitstream api.
|
|
|
28 */
|
|
|
29
|
|
|
30 #include "avcodec.h"
|
|
|
31 #include "bitstream.h"
|
|
|
32
|
|
|
33 void align_put_bits(PutBitContext *s)
|
|
|
34 {
|
|
|
35 #ifdef ALT_BITSTREAM_WRITER
|
|
|
36 put_bits(s,( - s->index) & 7,0);
|
|
|
37 #else
|
|
|
38 put_bits(s,s->bit_left & 7,0);
|
|
|
39 #endif
|
|
|
40 }
|
|
|
41
|
|
|
42 void ff_put_string(PutBitContext * pbc, char *s, int put_zero)
|
|
|
43 {
|
|
|
44 while(*s){
|
|
|
45 put_bits(pbc, 8, *s);
|
|
|
46 s++;
|
|
|
47 }
|
|
|
48 if(put_zero)
|
|
|
49 put_bits(pbc, 8, 0);
|
|
|
50 }
|
|
|
51
|
|
|
52 /* VLC decoding */
|
|
|
53
|
|
|
54 //#define DEBUG_VLC
|
|
|
55
|
|
|
56 #define GET_DATA(v, table, i, wrap, size) \
|
|
|
57 {\
|
|
|
58 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
|
|
|
59 switch(size) {\
|
|
|
60 case 1:\
|
|
|
61 v = *(const uint8_t *)ptr;\
|
|
|
62 break;\
|
|
|
63 case 2:\
|
|
|
64 v = *(const uint16_t *)ptr;\
|
|
|
65 break;\
|
|
|
66 default:\
|
|
|
67 v = *(const uint32_t *)ptr;\
|
|
|
68 break;\
|
|
|
69 }\
|
|
|
70 }
|
|
|
71
|
|
|
72
|
|
|
73 static int alloc_table(VLC *vlc, int size, int use_static)
|
|
|
74 {
|
|
|
75 int index;
|
|
|
76 index = vlc->table_size;
|
|
|
77 vlc->table_size += size;
|
|
|
78 if (vlc->table_size > vlc->table_allocated) {
|
|
|
79 vlc->table_allocated += (1 << vlc->bits);
|
|
830
|
80 vlc->table = av_realloc(vlc->table,
|
|
|
81 sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
|
|
808
|
82 if (!vlc->table)
|
|
|
83 return -1;
|
|
|
84 }
|
|
|
85 return index;
|
|
|
86 }
|
|
|
87
|
|
|
88 static int build_table(VLC *vlc, int table_nb_bits,
|
|
|
89 int nb_codes,
|
|
|
90 const void *bits, int bits_wrap, int bits_size,
|
|
|
91 const void *codes, int codes_wrap, int codes_size,
|
|
|
92 uint32_t code_prefix, int n_prefix, int flags)
|
|
|
93 {
|
|
|
94 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
|
|
|
95 uint32_t code;
|
|
|
96 VLC_TYPE (*table)[2];
|
|
|
97
|
|
|
98 table_size = 1 << table_nb_bits;
|
|
|
99 table_index = alloc_table(vlc, table_size, flags & INIT_VLC_USE_STATIC);
|
|
|
100 #ifdef DEBUG_VLC
|
|
|
101 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
|
|
|
102 table_index, table_size, code_prefix, n_prefix);
|
|
|
103 #endif
|
|
|
104 if (table_index < 0)
|
|
|
105 return -1;
|
|
|
106 table = &vlc->table[table_index];
|
|
|
107
|
|
|
108 for(i=0;i<table_size;i++) {
|
|
|
109 table[i][1] = 0; //bits
|
|
|
110 table[i][0] = -1; //codes
|
|
|
111 }
|
|
|
112
|
|
|
113 /* first pass: map codes and compute auxillary table sizes */
|
|
|
114 for(i=0;i<nb_codes;i++) {
|
|
|
115 GET_DATA(n, bits, i, bits_wrap, bits_size);
|
|
|
116 GET_DATA(code, codes, i, codes_wrap, codes_size);
|
|
|
117 /* we accept tables with holes */
|
|
|
118 if (n <= 0)
|
|
|
119 continue;
|
|
|
120 #if defined(DEBUG_VLC) && 0
|
|
|
121 printf("i=%d n=%d code=0x%x\n", i, n, code);
|
|
|
122 #endif
|
|
|
123 /* if code matches the prefix, it is in the table */
|
|
|
124 n -= n_prefix;
|
|
|
125 if(flags & INIT_VLC_LE)
|
|
|
126 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1);
|
|
|
127 else
|
|
|
128 code_prefix2= code >> n;
|
|
|
129 if (n > 0 && code_prefix2 == code_prefix) {
|
|
|
130 if (n <= table_nb_bits) {
|
|
|
131 /* no need to add another table */
|
|
|
132 j = (code << (table_nb_bits - n)) & (table_size - 1);
|
|
|
133 nb = 1 << (table_nb_bits - n);
|
|
|
134 for(k=0;k<nb;k++) {
|
|
|
135 if(flags & INIT_VLC_LE)
|
|
|
136 j = (code >> n_prefix) + (k<<n);
|
|
|
137 #ifdef DEBUG_VLC
|
|
|
138 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n",
|
|
|
139 j, i, n);
|
|
|
140 #endif
|
|
|
141 if (table[j][1] /*bits*/ != 0) {
|
|
|
142 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n");
|
|
|
143 return -1;
|
|
|
144 }
|
|
|
145 table[j][1] = n; //bits
|
|
|
146 table[j][0] = i; //code
|
|
|
147 j++;
|
|
|
148 }
|
|
|
149 } else {
|
|
|
150 n -= table_nb_bits;
|
|
|
151 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
|
|
|
152 #ifdef DEBUG_VLC
|
|
|
153 printf("%4x: n=%d (subtable)\n",
|
|
|
154 j, n);
|
|
|
155 #endif
|
|
|
156 /* compute table size */
|
|
|
157 n1 = -table[j][1]; //bits
|
|
|
158 if (n > n1)
|
|
|
159 n1 = n;
|
|
|
160 table[j][1] = -n1; //bits
|
|
|
161 }
|
|
|
162 }
|
|
|
163 }
|
|
|
164
|
|
|
165 /* second pass : fill auxillary tables recursively */
|
|
|
166 for(i=0;i<table_size;i++) {
|
|
|
167 n = table[i][1]; //bits
|
|
|
168 if (n < 0) {
|
|
|
169 n = -n;
|
|
|
170 if (n > table_nb_bits) {
|
|
|
171 n = table_nb_bits;
|
|
|
172 table[i][1] = -n; //bits
|
|
|
173 }
|
|
|
174 index = build_table(vlc, n, nb_codes,
|
|
|
175 bits, bits_wrap, bits_size,
|
|
|
176 codes, codes_wrap, codes_size,
|
|
|
177 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
|
|
|
178 n_prefix + table_nb_bits, flags);
|
|
|
179 if (index < 0)
|
|
|
180 return -1;
|
|
|
181 /* note: realloc has been done, so reload tables */
|
|
|
182 table = &vlc->table[table_index];
|
|
|
183 table[i][0] = index; //code
|
|
|
184 }
|
|
|
185 }
|
|
|
186 return table_index;
|
|
|
187 }
|
|
|
188
|
|
|
189
|
|
|
190 /* Build VLC decoding tables suitable for use with get_vlc().
|
|
|
191
|
|
|
192 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
|
|
|
193 bigger it is, the faster is the decoding. But it should not be too
|
|
|
194 big to save memory and L1 cache. '9' is a good compromise.
|
|
|
195
|
|
|
196 'nb_codes' : number of vlcs codes
|
|
|
197
|
|
|
198 'bits' : table which gives the size (in bits) of each vlc code.
|
|
|
199
|
|
|
200 'codes' : table which gives the bit pattern of of each vlc code.
|
|
|
201
|
|
|
202 'xxx_wrap' : give the number of bytes between each entry of the
|
|
|
203 'bits' or 'codes' tables.
|
|
|
204
|
|
|
205 'xxx_size' : gives the number of bytes of each entry of the 'bits'
|
|
|
206 or 'codes' tables.
|
|
|
207
|
|
|
208 'wrap' and 'size' allows to use any memory configuration and types
|
|
|
209 (byte/word/long) to store the 'bits' and 'codes' tables.
|
|
|
210
|
|
|
211 'use_static' should be set to 1 for tables, which should be freed
|
|
|
212 with av_free_static(), 0 if free_vlc() will be used.
|
|
|
213 */
|
|
|
214 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
|
|
|
215 const void *bits, int bits_wrap, int bits_size,
|
|
|
216 const void *codes, int codes_wrap, int codes_size,
|
|
|
217 int use_static)
|
|
|
218 {
|
|
|
219 vlc->bits = nb_bits;
|
|
|
220 if(!use_static) {
|
|
|
221 vlc->table = NULL;
|
|
|
222 vlc->table_allocated = 0;
|
|
|
223 vlc->table_size = 0;
|
|
|
224 } else {
|
|
|
225 /* Static tables are initially always NULL, return
|
|
|
226 if vlc->table != NULL to avoid double allocation */
|
|
|
227 if(vlc->table)
|
|
|
228 return 0;
|
|
|
229 }
|
|
|
230
|
|
|
231 #ifdef DEBUG_VLC
|
|
|
232 printf("build table nb_codes=%d\n", nb_codes);
|
|
|
233 #endif
|
|
|
234
|
|
|
235 if (build_table(vlc, nb_bits, nb_codes,
|
|
|
236 bits, bits_wrap, bits_size,
|
|
|
237 codes, codes_wrap, codes_size,
|
|
|
238 0, 0, use_static) < 0) {
|
|
|
239 av_free(vlc->table);
|
|
|
240 return -1;
|
|
|
241 }
|
|
|
242 return 0;
|
|
|
243 }
|
|
|
244
|
|
|
245
|
|
|
246 void free_vlc(VLC *vlc)
|
|
|
247 {
|
|
|
248 av_free(vlc->table);
|
|
|
249 }
|
|
|
250
|