Old RC: "rate" is a float nowadays (fix #4088)
[vlc/asuraparaju-public.git] / modules / codec / wmafixed / bitstream.c
blob57e57ef02b0d5fa105663041b97db385e1478286
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 * This file is part of FFmpeg.
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.
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.
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
22 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at>
25 /**
26 * @file bitstream.c
27 * bitstream api.
30 #include "bitstream.h"
32 #include <stdio.h>
34 #define DEBUGF printf
36 /**
37 * Same as av_mallocz_static(), but does a realloc.
39 * @param[in] ptr The block of memory to reallocate.
40 * @param[in] size The requested size.
41 * @return Block of memory of requested size.
42 * @deprecated. Code which uses ff_realloc_static is broken/missdesigned
43 * and should correctly use static arrays
45 attribute_deprecated void *ff_realloc_static(void *ptr, unsigned int size);
47 const uint8_t ff_sqrt_tab[128]={
48 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
49 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
50 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
51 9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
54 const uint8_t ff_log2_tab[256]={
55 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
56 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
57 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
58 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
59 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
60 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
61 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
62 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
65 void align_put_bits(PutBitContext *s)
67 #ifdef ALT_BITSTREAM_WRITER
68 put_bits(s,( - s->index) & 7,0);
69 #else
70 put_bits(s,s->bit_left & 7,0);
71 #endif
74 void ff_put_string(PutBitContext * pbc, char *s, int put_zero)
76 while(*s){
77 put_bits(pbc, 8, *s);
78 s++;
80 if(put_zero)
81 put_bits(pbc, 8, 0);
84 /* VLC decoding */
85 #define GET_DATA(v, table, i, wrap, size) \
87 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\
88 switch(size) {\
89 case 1:\
90 v = *(const uint8_t *)ptr;\
91 break;\
92 case 2:\
93 v = *(const uint16_t *)ptr;\
94 break;\
95 default:\
96 v = *(const uint32_t *)ptr;\
97 break;\
101 static int alloc_table(VLC *vlc, int size)
103 int index;
104 index = vlc->table_size;
105 vlc->table_size += size;
106 if (vlc->table_size > vlc->table_allocated) {
107 DEBUGF("Tried to allocate past the end of a Huffman table: %d/%d\n",
108 vlc->table_allocated, vlc->table_allocated+(1 << vlc->bits));
109 vlc->table_allocated += (1 << vlc->bits);
110 if (!vlc->table)
111 return -1;
113 return index;
116 static int build_table(VLC *vlc, int table_nb_bits,
117 int nb_codes,
118 const void *bits, int bits_wrap, int bits_size,
119 const void *codes, int codes_wrap, int codes_size,
120 uint32_t code_prefix, int n_prefix)
122 int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2;
123 uint32_t code;
124 int flags = 0;
125 VLC_TYPE (*table)[2];
127 table_size = 1 << table_nb_bits;
128 table_index = alloc_table(vlc, table_size);
129 #ifdef DEBUG_VLC
130 printf("new table index=%d size=%d code_prefix=%x n=%d\n",
131 table_index, table_size, code_prefix, n_prefix);
132 #endif
133 if (table_index < 0)
134 return -1;
135 table = &vlc->table[table_index];
137 for(i=0;i<table_size;i++) {
138 table[i][1] = 0; //bits
139 table[i][0] = -1; //codes
142 /* first pass: map codes and compute auxillary table sizes */
143 for(i=0;i<nb_codes;i++) {
144 GET_DATA(n, bits, i, bits_wrap, bits_size);
145 GET_DATA(code, codes, i, codes_wrap, codes_size);
146 /* we accept tables with holes */
147 if (n <= 0)
148 continue;
149 #if defined(DEBUG_VLC) && 0
150 printf("i=%d n=%d code=0x%x\n", i, n, code);
151 #endif
152 /* if code matches the prefix, it is in the table */
153 n -= n_prefix;
154 if(flags & INIT_VLC_LE)
155 code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (uint32_t)(1 << n_prefix)-1);
156 else
157 code_prefix2= code >> n;
158 if (n > 0 && (int)code_prefix2 == (int)code_prefix) {
159 if (n <= table_nb_bits) {
160 /* no need to add another table */
161 j = (code << (table_nb_bits - n)) & (table_size - 1);
162 nb = 1 << (table_nb_bits - n);
163 for(k=0;k<nb;k++) {
164 if(flags & INIT_VLC_LE)
165 j = (code >> n_prefix) + (k<<n);
166 #ifdef DEBUG_VLC
167 av_log(NULL, 0, "%4x: code=%d n=%d\n",
168 j, i, n);
169 #endif
170 if (table[j][1] /*bits*/ != 0) {
171 return -1;
173 table[j][1] = n; //bits
174 table[j][0] = i; //code
175 j++;
177 } else {
178 n -= table_nb_bits;
179 j = (code >> ((flags & INIT_VLC_LE) ? n_prefix : n)) & ((1 << table_nb_bits) - 1);
180 #ifdef DEBUG_VLC
181 av_log(NULL, 0,"%4x: n=%d (subtable)\n",
182 j, n);
183 #endif
184 /* compute table size */
185 n1 = -table[j][1]; //bits
186 if (n > n1)
187 n1 = n;
188 table[j][1] = -n1; //bits
193 /* second pass : fill auxillary tables recursively */
194 for(i=0;i<table_size;i++) {
195 n = table[i][1]; //bits
196 if (n < 0) {
197 n = -n;
198 if (n > table_nb_bits) {
199 n = table_nb_bits;
200 table[i][1] = -n; //bits
202 index = build_table(vlc, n, nb_codes,
203 bits, bits_wrap, bits_size,
204 codes, codes_wrap, codes_size,
205 (flags & INIT_VLC_LE) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i),
206 n_prefix + table_nb_bits);
207 if (index < 0)
208 return -1;
209 /* note: realloc has been done, so reload tables */
210 table = &vlc->table[table_index];
211 table[i][0] = index; //code
214 return table_index;
217 /* Build VLC decoding tables suitable for use with get_vlc().
219 'nb_bits' set thee decoding table size (2^nb_bits) entries. The
220 bigger it is, the faster is the decoding. But it should not be too
221 big to save memory and L1 cache. '9' is a good compromise.
223 'nb_codes' : number of vlcs codes
225 'bits' : table which gives the size (in bits) of each vlc code.
227 'codes' : table which gives the bit pattern of of each vlc code.
229 'xxx_wrap' : give the number of bytes between each entry of the
230 'bits' or 'codes' tables.
232 'xxx_size' : gives the number of bytes of each entry of the 'bits'
233 or 'codes' tables.
235 'wrap' and 'size' allows to use any memory configuration and types
236 (byte/word/long) to store the 'bits' and 'codes' tables.
238 'use_static' should be set to 1 for tables, which should be freed
239 with av_free_static(), 0 if free_vlc() will be used.
241 int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
242 const void *bits, int bits_wrap, int bits_size,
243 const void *codes, int codes_wrap, int codes_size,
244 int flags)
247 vlc->bits = nb_bits;
248 vlc->table_size = 0;
250 #ifdef DEBUG_VLC
251 printf("build table nb_codes=%d\n", nb_codes);
252 #endif
254 if (build_table(vlc, nb_bits, nb_codes,
255 bits, bits_wrap, bits_size,
256 codes, codes_wrap, codes_size,
257 0, 0) < 0) {
258 //av_free(vlc->table);
259 return -1;
261 /* return flags to block gcc warning while allowing us to keep
262 * consistent with ffmpeg's function parameters
264 return flags;