lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / lib / ffmpeg_bitstream.c
blob88e3cbfe3a55c534b94d35fcc3d6c80240563583
1 /*
2 * Common bit i/o utils
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "ffmpeg_bitstream.h"
27 #ifdef ROCKBOX
28 #undef DEBUGF
29 #define DEBUGF(...)
30 #endif
32 const uint8_t ff_log2_run[32]={
33 0, 0, 0, 0, 1, 1, 1, 1,
34 2, 2, 2, 2, 3, 3, 3, 3,
35 4, 4, 5, 5, 6, 6, 7, 7,
36 8, 9,10,11,12,13,14,15
39 /**
40 * Same as av_mallocz_static(), but does a realloc.
42 * @param[in] ptr The block of memory to reallocate.
43 * @param[in] size The requested size.
44 * @return Block of memory of requested size.
45 * @deprecated. Code which uses ff_realloc_static is broken/misdesigned
46 * and should correctly use static arrays
50 void ff_put_string(PutBitContext * pbc, const char *s, int put_zero)
52 while(*s){
53 put_bits(pbc, 8, *s);
54 s++;
56 if(put_zero)
57 put_bits(pbc, 8, 0);
60 /* VLC decoding */
62 //#define DEBUG_VLC
64 #define GET_DATA(v, table, i, wrap, size) \
66 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
67 switch(size) {\
68 case 1:\
69 v = *(const uint8_t *)ptr;\
70 break;\
71 case 2:\
72 v = *(const uint16_t *)ptr;\
73 break;\
74 default:\
75 v = *(const uint32_t *)ptr;\
76 break;\
81 static int alloc_table(VLC *vlc, int size, int use_static)
83 int index;
84 index = vlc->table_size;
85 vlc->table_size += size;
86 if (vlc->table_size > vlc->table_allocated) {
87 if(use_static>1){
88 DEBUGF("init_vlc() used with too little memory : table_size > allocated_memory\n");
91 if (!vlc->table)
92 return -1;
94 return index;
97 static int build_table(VLC *vlc, int table_nb_bits,
98 int nb_codes,
99 const void *bits, int bits_wrap, int bits_size,
100 const void *codes, int codes_wrap, int codes_size,
101 const void *symbols, int symbols_wrap, int symbols_size,
102 uint32_t code_prefix, int n_prefix, int flags)
104 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol;
105 uint32_t code;
106 VLC_TYPE (*table)[2];
108 table_size = 1 << table_nb_bits;
109 table_index = alloc_table(vlc, table_size, flags & (INIT_VLC_USE_STATIC|INIT_VLC_USE_NEW_STATIC));
110 #ifdef DEBUG_VLC
111 DEBUGF("new table index=%d size=%d code_prefix=%x n=%d\n",
112 table_index, table_size, code_prefix, n_prefix);
113 #endif
114 if (table_index < 0)
115 return -1;
116 table = &vlc->table[table_index];
118 for(i=0;i<table_size;i++) {
119 table[i][1] = 0; //bits
120 table[i][0] = -1; //codes
123 /* first pass: map codes and compute auxillary table sizes */
124 for(i=0;i<nb_codes;i++) {
125 GET_DATA(n, bits, i, bits_wrap, bits_size);
126 GET_DATA(code, codes, i, codes_wrap, codes_size);
127 /* we accept tables with holes */
128 if (n <= 0)
129 continue;
130 if (!symbols)
131 symbol = i;
132 else
133 GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size);
134 #if defined(DEBUG_VLC) && 0
135 DEBUGF("i=%d n=%d code=0x%x\n", i, n, code);
136 #endif
137 /* if code matches the prefix, it is in the table */
138 n -= n_prefix;
139 if(flags & INIT_VLC_LE)
140 code_prefix2= code & (n_prefix>=32 ? (int)0xffffffff : (1 << n_prefix)-1);
141 else
142 code_prefix2= code >> n;
143 if (n > 0 && code_prefix2 == (int)code_prefix) {
144 if (n <= table_nb_bits) {
145 /* no need to add another table */
146 j = (code << (table_nb_bits - n)) & (table_size - 1);
147 nb = 1 << (table_nb_bits - n);
148 for(k=0;k<nb;k++) {
149 if(flags & INIT_VLC_LE)
150 j = (code >> n_prefix) + (k<<n);
151 #ifdef DEBUG_VLC
152 DEBUGF("%4x: code=%d n=%d\n",
153 j, i, n);
154 #endif
155 if (table[j][1] /*bits*/ != 0) {
156 DEBUGF("incorrect codes\n");
157 return -1;
159 table[j][1] = n; //bits
160 table[j][0] = symbol;
161 j++;
163 } else {
164 n -= table_nb_bits;
165 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
166 #ifdef DEBUG_VLC
167 DEBUGF("%4x: n=%d (subtable)\n",
168 j, n);
169 #endif
170 /* compute table size */
171 n1 = -table[j][1]; //bits
172 if (n > n1)
173 n1 = n;
174 table[j][1] = -n1; //bits
179 /* second pass : fill auxillary tables recursively */
180 for(i=0;i<table_size;i++) {
181 n = table[i][1]; //bits
182 if (n < 0) {
183 n = -n;
184 if (n > table_nb_bits) {
185 n = table_nb_bits;
186 table[i][1] = -n; //bits
188 index = build_table(vlc, n, nb_codes,
189 bits, bits_wrap, bits_size,
190 codes, codes_wrap, codes_size,
191 symbols, symbols_wrap, symbols_size,
192 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
193 n_prefix + table_nb_bits, flags);
194 if (index < 0)
195 return -1;
196 /* note: realloc has been done, so reload tables */
197 table = &vlc->table[table_index];
198 table[i][0] = index; //code
201 return table_index;
205 /* Build VLC decoding tables suitable for use with get_vlc().
207 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
208 bigger it is, the faster is the decoding. But it should not be too
209 big to save memory and L1 cache. '9' is a good compromise.
211 'nb_codes' : number of vlcs codes
213 'bits' : table which gives the size (in bits) of each vlc code.
215 'codes' : table which gives the bit pattern of of each vlc code.
217 'symbols' : table which gives the values to be returned from get_vlc().
219 'xxx_wrap' : give the number of bytes between each entry of the
220 'bits' or 'codes' tables.
222 'xxx_size' : gives the number of bytes of each entry of the 'bits'
223 or 'codes' tables.
225 'wrap' and 'size' allows to use any memory configuration and types
226 (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables.
228 'use_static' should be set to 1 for tables, which should be freed
229 with av_free_static(), 0 if free_vlc() will be used.
231 int init_vlc_sparse(VLC *vlc, int nb_bits, int nb_codes,
232 const void *bits, int bits_wrap, int bits_size,
233 const void *codes, int codes_wrap, int codes_size,
234 const void *symbols, int symbols_wrap, int symbols_size,
235 int flags)
237 vlc->bits = nb_bits;
238 if(flags & INIT_VLC_USE_NEW_STATIC){
239 if(vlc->table_size && vlc->table_size == vlc->table_allocated){
240 return 0;
241 }else if(vlc->table_size){
242 return -1; // fatal error, we are called on a partially initialized table
244 }else if(!(flags & INIT_VLC_USE_STATIC)) {
245 vlc->table = NULL;
246 vlc->table_allocated = 0;
247 vlc->table_size = 0;
248 } else {
249 /* Static tables are initially always NULL, return
250 if vlc->table != NULL to avoid double allocation */
251 if(vlc->table)
252 return 0;
255 #ifdef DEBUG_VLC
256 DEBUGF("build table nb_codes=%d\n", nb_codes);
257 #endif
259 if (build_table(vlc, nb_bits, nb_codes,
260 bits, bits_wrap, bits_size,
261 codes, codes_wrap, codes_size,
262 symbols, symbols_wrap, symbols_size,
263 0, 0, flags) < 0) {
264 //free(&vlc->table);
265 return -1;
267 /* Changed the following condition to be true if table_size > table_allocated. *
268 * This would be more sensible for static tables since we want warnings for *
269 * memory shortages only. */
270 #ifdef TEST
271 if((flags & INIT_VLC_USE_NEW_STATIC) && vlc->table_size > vlc->table_allocated)
272 DEBUGF("needed %d had %d\n", vlc->table_size, vlc->table_allocated);
273 #endif
274 return 0;