Mercurial > libavcodec.hg
annotate common.c @ 1893:779bdf5063ce libavcodec
1000000l (using uninitalized variable for initalizing bits per MV table)
author | michael |
---|---|
date | Tue, 16 Mar 2004 16:57:27 +0000 |
parents | 45a1592dadca |
children | 7b345b735ac7 |
rev | line source |
---|---|
0 | 1 /* |
2 * Common bit i/o utils | |
429 | 3 * Copyright (c) 2000, 2001 Fabrice Bellard. |
1739
07a484280a82
copyright year update of the files i touched and remembered, things look annoyingly unmaintained otherwise
michael
parents:
1660
diff
changeset
|
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> |
0 | 5 * |
429 | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Lesser General Public | |
8 * License as published by the Free Software Foundation; either | |
9 * version 2 of the License, or (at your option) any later version. | |
0 | 10 * |
429 | 11 * This library is distributed in the hope that it will be useful, |
0 | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 * Lesser General Public License for more details. | |
0 | 15 * |
429 | 16 * You should have received a copy of the GNU Lesser General Public |
17 * License along with this library; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
19 * |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
20 * alternative bitstream reader & writer by Michael Niedermayer <michaelni@gmx.at> |
0 | 21 */ |
1106 | 22 |
23 /** | |
24 * @file common.c | |
25 * common internal api. | |
26 */ | |
27 | |
524 | 28 #include "avcodec.h" |
8 | 29 |
1064 | 30 const uint8_t ff_sqrt_tab[128]={ |
609 | 31 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, |
32 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, | |
33 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, | |
34 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 | |
35 }; | |
36 | |
1037 | 37 const uint8_t ff_log2_tab[256]={ |
38 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, | |
39 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, | |
40 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, | |
41 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, | |
42 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, | |
43 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, | |
44 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, | |
45 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 | |
46 }; | |
47 | |
0 | 48 void align_put_bits(PutBitContext *s) |
49 { | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
50 #ifdef ALT_BITSTREAM_WRITER |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
51 put_bits(s,( - s->index) & 7,0); |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
52 #else |
238
99a9f903f0e3
optimized the normal bitstream writer, its faster than the alternative one on p3 now ... lets hope its at least not slower on p4 & k7
michaelni
parents:
234
diff
changeset
|
53 put_bits(s,s->bit_left & 7,0); |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
219
diff
changeset
|
54 #endif |
0 | 55 } |
56 | |
1795 | 57 void put_string(PutBitContext * pbc, char *s, int put_zero) |
358 | 58 { |
59 while(*s){ | |
60 put_bits(pbc, 8, *s); | |
61 s++; | |
62 } | |
1795 | 63 if(put_zero) |
64 put_bits(pbc, 8, 0); | |
358 | 65 } |
66 | |
0 | 67 /* bit input functions */ |
68 | |
1257 | 69 /** |
70 * reads 0-32 bits. | |
71 */ | |
72 unsigned int get_bits_long(GetBitContext *s, int n){ | |
73 if(n<=17) return get_bits(s, n); | |
74 else{ | |
75 int ret= get_bits(s, 16) << (n-16); | |
76 return ret | get_bits(s, n-16); | |
77 } | |
78 } | |
79 | |
80 /** | |
81 * shows 0-32 bits. | |
82 */ | |
83 unsigned int show_bits_long(GetBitContext *s, int n){ | |
84 if(n<=17) return show_bits(s, n); | |
85 else{ | |
86 GetBitContext gb= *s; | |
87 int ret= get_bits_long(s, n); | |
88 *s= gb; | |
89 return ret; | |
90 } | |
91 } | |
92 | |
0 | 93 void align_get_bits(GetBitContext *s) |
94 { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
95 int n= (-get_bits_count(s)) & 7; |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
96 if(n) skip_bits(s, n); |
0 | 97 } |
192
1e5f64be86fc
another bitstream reader code (faster on intel cpus) - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
144
diff
changeset
|
98 |
862 | 99 int check_marker(GetBitContext *s, const char *msg) |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
100 { |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
101 int bit= get_bits1(s); |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1548
diff
changeset
|
102 if(!bit) |
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1548
diff
changeset
|
103 av_log(NULL, AV_LOG_INFO, "Marker bit missing %s\n", msg); |
264
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
104 |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
105 return bit; |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
106 } |
28c5c62b1c4c
support decoding (with mplayer) of 3 .mp4 files from mphq
michaelni
parents:
238
diff
changeset
|
107 |
0 | 108 /* VLC decoding */ |
109 | |
110 //#define DEBUG_VLC | |
111 | |
112 #define GET_DATA(v, table, i, wrap, size) \ | |
113 {\ | |
1064 | 114 const uint8_t *ptr = (const uint8_t *)table + i * wrap;\ |
0 | 115 switch(size) {\ |
116 case 1:\ | |
1064 | 117 v = *(const uint8_t *)ptr;\ |
0 | 118 break;\ |
119 case 2:\ | |
1064 | 120 v = *(const uint16_t *)ptr;\ |
0 | 121 break;\ |
122 default:\ | |
1064 | 123 v = *(const uint32_t *)ptr;\ |
0 | 124 break;\ |
125 }\ | |
126 } | |
127 | |
128 | |
129 static int alloc_table(VLC *vlc, int size) | |
130 { | |
131 int index; | |
132 index = vlc->table_size; | |
133 vlc->table_size += size; | |
134 if (vlc->table_size > vlc->table_allocated) { | |
135 vlc->table_allocated += (1 << vlc->bits); | |
1031
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
1025
diff
changeset
|
136 vlc->table = av_realloc(vlc->table, |
19de1445beb2
use av_malloc() functions - added av_strdup and av_realloc()
bellard
parents:
1025
diff
changeset
|
137 sizeof(VLC_TYPE) * 2 * vlc->table_allocated); |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
138 if (!vlc->table) |
0 | 139 return -1; |
140 } | |
141 return index; | |
142 } | |
143 | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
144 static int build_table(VLC *vlc, int table_nb_bits, |
0 | 145 int nb_codes, |
146 const void *bits, int bits_wrap, int bits_size, | |
147 const void *codes, int codes_wrap, int codes_size, | |
1064 | 148 uint32_t code_prefix, int n_prefix) |
0 | 149 { |
150 int i, j, k, n, table_size, table_index, nb, n1, index; | |
1064 | 151 uint32_t code; |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
152 VLC_TYPE (*table)[2]; |
0 | 153 |
154 table_size = 1 << table_nb_bits; | |
155 table_index = alloc_table(vlc, table_size); | |
156 #ifdef DEBUG_VLC | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
157 printf("new table index=%d size=%d code_prefix=%x n=%d\n", |
0 | 158 table_index, table_size, code_prefix, n_prefix); |
159 #endif | |
160 if (table_index < 0) | |
161 return -1; | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
162 table = &vlc->table[table_index]; |
0 | 163 |
164 for(i=0;i<table_size;i++) { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
165 table[i][1] = 0; //bits |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
166 table[i][0] = -1; //codes |
0 | 167 } |
168 | |
169 /* first pass: map codes and compute auxillary table sizes */ | |
170 for(i=0;i<nb_codes;i++) { | |
171 GET_DATA(n, bits, i, bits_wrap, bits_size); | |
172 GET_DATA(code, codes, i, codes_wrap, codes_size); | |
173 /* we accept tables with holes */ | |
174 if (n <= 0) | |
175 continue; | |
176 #if defined(DEBUG_VLC) && 0 | |
177 printf("i=%d n=%d code=0x%x\n", i, n, code); | |
178 #endif | |
179 /* if code matches the prefix, it is in the table */ | |
180 n -= n_prefix; | |
181 if (n > 0 && (code >> n) == code_prefix) { | |
182 if (n <= table_nb_bits) { | |
183 /* no need to add another table */ | |
184 j = (code << (table_nb_bits - n)) & (table_size - 1); | |
185 nb = 1 << (table_nb_bits - n); | |
186 for(k=0;k<nb;k++) { | |
187 #ifdef DEBUG_VLC | |
1602
fdb8244da1e5
av_log patch(2 of ?) by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1598
diff
changeset
|
188 av_log(NULL, AV_LOG_DEBUG, "%4x: code=%d n=%d\n", |
0 | 189 j, i, n); |
190 #endif | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
191 if (table[j][1] /*bits*/ != 0) { |
1598
932d306bf1dc
av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michael
parents:
1548
diff
changeset
|
192 av_log(NULL, AV_LOG_ERROR, "incorrect codes\n"); |
1387 | 193 av_abort(); |
0 | 194 } |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
195 table[j][1] = n; //bits |
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
196 table[j][0] = i; //code |
0 | 197 j++; |
198 } | |
199 } else { | |
200 n -= table_nb_bits; | |
201 j = (code >> n) & ((1 << table_nb_bits) - 1); | |
202 #ifdef DEBUG_VLC | |
203 printf("%4x: n=%d (subtable)\n", | |
204 j, n); | |
205 #endif | |
206 /* compute table size */ | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
207 n1 = -table[j][1]; //bits |
0 | 208 if (n > n1) |
209 n1 = n; | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
210 table[j][1] = -n1; //bits |
0 | 211 } |
212 } | |
213 } | |
214 | |
215 /* second pass : fill auxillary tables recursively */ | |
216 for(i=0;i<table_size;i++) { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
217 n = table[i][1]; //bits |
0 | 218 if (n < 0) { |
219 n = -n; | |
220 if (n > table_nb_bits) { | |
221 n = table_nb_bits; | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
222 table[i][1] = -n; //bits |
0 | 223 } |
224 index = build_table(vlc, n, nb_codes, | |
225 bits, bits_wrap, bits_size, | |
226 codes, codes_wrap, codes_size, | |
227 (code_prefix << table_nb_bits) | i, | |
228 n_prefix + table_nb_bits); | |
229 if (index < 0) | |
230 return -1; | |
231 /* note: realloc has been done, so reload tables */ | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
232 table = &vlc->table[table_index]; |
535 | 233 table[i][0] = index; //code |
0 | 234 } |
235 } | |
236 return table_index; | |
237 } | |
238 | |
239 | |
24 | 240 /* Build VLC decoding tables suitable for use with get_vlc(). |
241 | |
242 'nb_bits' set thee decoding table size (2^nb_bits) entries. The | |
243 bigger it is, the faster is the decoding. But it should not be too | |
244 big to save memory and L1 cache. '9' is a good compromise. | |
245 | |
246 'nb_codes' : number of vlcs codes | |
247 | |
248 'bits' : table which gives the size (in bits) of each vlc code. | |
249 | |
250 'codes' : table which gives the bit pattern of of each vlc code. | |
251 | |
252 'xxx_wrap' : give the number of bytes between each entry of the | |
253 'bits' or 'codes' tables. | |
254 | |
255 'xxx_size' : gives the number of bytes of each entry of the 'bits' | |
256 or 'codes' tables. | |
257 | |
258 'wrap' and 'size' allows to use any memory configuration and types | |
259 (byte/word/long) to store the 'bits' and 'codes' tables. | |
260 */ | |
0 | 261 int init_vlc(VLC *vlc, int nb_bits, int nb_codes, |
262 const void *bits, int bits_wrap, int bits_size, | |
263 const void *codes, int codes_wrap, int codes_size) | |
264 { | |
265 vlc->bits = nb_bits; | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
266 vlc->table = NULL; |
0 | 267 vlc->table_allocated = 0; |
268 vlc->table_size = 0; | |
269 #ifdef DEBUG_VLC | |
270 printf("build table nb_codes=%d\n", nb_codes); | |
271 #endif | |
272 | |
273 if (build_table(vlc, nb_bits, nb_codes, | |
274 bits, bits_wrap, bits_size, | |
275 codes, codes_wrap, codes_size, | |
276 0, 0) < 0) { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
277 av_free(vlc->table); |
0 | 278 return -1; |
279 } | |
280 return 0; | |
281 } | |
282 | |
283 | |
284 void free_vlc(VLC *vlc) | |
285 { | |
520
19a5e2a81e1a
new bitstream reader API (old get_bits() based one is emulated and will still be supported in the future cuz its simpler)
michaelni
parents:
497
diff
changeset
|
286 av_free(vlc->table); |
0 | 287 } |
288 | |
1126
77ccf7fe3bd0
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
1106
diff
changeset
|
289 int64_t ff_gcd(int64_t a, int64_t b){ |
324 | 290 if(b) return ff_gcd(b, a%b); |
291 else return a; | |
292 } |