Mercurial > libavcodec.hg
annotate cook.c @ 4639:33fc84eae7c3 libavcodec
Simplify gain block handling.
Patch by Ian Braithwaite <ian at braithwaite dot dk>
author | banan |
---|---|
date | Thu, 08 Mar 2007 20:43:51 +0000 |
parents | 9f74306d4ac7 |
children | 31bf54d9353d |
rev | line source |
---|---|
2956 | 1 /* |
2 * COOK compatible decoder | |
3 * Copyright (c) 2003 Sascha Sommer | |
4 * Copyright (c) 2005 Benjamin Larsson | |
5 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3619
diff
changeset
|
6 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3619
diff
changeset
|
7 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3619
diff
changeset
|
8 * FFmpeg is free software; you can redistribute it and/or |
2956 | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3619
diff
changeset
|
11 * version 2.1 of the License, or (at your option) any later version. |
2956 | 12 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3619
diff
changeset
|
13 * FFmpeg is distributed in the hope that it will be useful, |
2956 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3619
diff
changeset
|
19 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
3019
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
2956 | 21 * |
22 */ | |
23 | |
24 /** | |
25 * @file cook.c | |
26 * Cook compatible decoder. | |
27 * This decoder handles RealNetworks, RealAudio G2 data. | |
28 * Cook is identified by the codec name cook in RM files. | |
29 * | |
30 * To use this decoder, a calling application must supply the extradata | |
31 * bytes provided from the RM container; 8+ bytes for mono streams and | |
32 * 16+ for stereo streams (maybe more). | |
33 * | |
34 * Codec technicalities (all this assume a buffer length of 1024): | |
35 * Cook works with several different techniques to achieve its compression. | |
36 * In the timedomain the buffer is divided into 8 pieces and quantized. If | |
37 * two neighboring pieces have different quantization index a smooth | |
38 * quantization curve is used to get a smooth overlap between the different | |
39 * pieces. | |
40 * To get to the transformdomain Cook uses a modulated lapped transform. | |
41 * The transform domain has 50 subbands with 20 elements each. This | |
42 * means only a maximum of 50*20=1000 coefficients are used out of the 1024 | |
43 * available. | |
44 */ | |
45 | |
46 #include <math.h> | |
47 #include <stddef.h> | |
48 #include <stdio.h> | |
49 | |
50 #include "avcodec.h" | |
51 #include "bitstream.h" | |
52 #include "dsputil.h" | |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
53 #include "common.h" |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
54 #include "bytestream.h" |
2956 | 55 |
56 #include "cookdata.h" | |
57 | |
58 /* the different Cook versions */ | |
4425 | 59 #define MONO 0x1000001 |
60 #define STEREO 0x1000002 | |
2956 | 61 #define JOINT_STEREO 0x1000003 |
62 #define MC_COOK 0x2000000 //multichannel Cook, not supported | |
63 | |
64 #define SUBBAND_SIZE 20 | |
65 //#define COOKDEBUG | |
66 | |
67 typedef struct { | |
4639 | 68 int *now; |
69 int *previous; | |
70 } cook_gains; | |
2956 | 71 |
72 typedef struct { | |
73 GetBitContext gb; | |
74 /* stream data */ | |
75 int nb_channels; | |
76 int joint_stereo; | |
77 int bit_rate; | |
78 int sample_rate; | |
79 int samples_per_channel; | |
80 int samples_per_frame; | |
81 int subbands; | |
3090 | 82 int log2_numvector_size; |
83 int numvector_size; //1 << log2_numvector_size; | |
2956 | 84 int js_subband_start; |
85 int total_subbands; | |
86 int num_vectors; | |
87 int bits_per_subpacket; | |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
88 int cookversion; |
2956 | 89 /* states */ |
90 int random_state; | |
91 | |
92 /* transform data */ | |
93 FFTContext fft_ctx; | |
4542 | 94 DECLARE_ALIGNED_16(FFTSample, mlt_tmp[1024]); /* temporary storage for imlt */ |
2956 | 95 float* mlt_window; |
96 float* mlt_precos; | |
97 float* mlt_presin; | |
98 float* mlt_postcos; | |
99 int fft_size; | |
100 int fft_order; | |
101 int mlt_size; //modulated lapped transform size | |
102 | |
103 /* gain buffers */ | |
4639 | 104 cook_gains gains1; |
105 cook_gains gains2; | |
106 int gain_1[9]; | |
107 int gain_2[9]; | |
108 int gain_3[9]; | |
109 int gain_4[9]; | |
2956 | 110 |
111 /* VLC data */ | |
112 int js_vlc_bits; | |
113 VLC envelope_quant_index[13]; | |
114 VLC sqvh[7]; //scalar quantization | |
115 VLC ccpl; //channel coupling | |
116 | |
117 /* generatable tables and related variables */ | |
118 int gain_size_factor; | |
119 float gain_table[23]; | |
120 float pow2tab[127]; | |
121 float rootpow2tab[127]; | |
122 | |
123 /* data buffers */ | |
124 | |
125 uint8_t* decoded_bytes_buffer; | |
4542 | 126 DECLARE_ALIGNED_16(float,mono_mdct_output[2048]); |
2956 | 127 float mono_previous_buffer1[1024]; |
128 float mono_previous_buffer2[1024]; | |
129 float decode_buffer_1[1024]; | |
130 float decode_buffer_2[1024]; | |
131 } COOKContext; | |
132 | |
133 /* debug functions */ | |
134 | |
135 #ifdef COOKDEBUG | |
136 static void dump_float_table(float* table, int size, int delimiter) { | |
137 int i=0; | |
138 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i); | |
139 for (i=0 ; i<size ; i++) { | |
140 av_log(NULL, AV_LOG_ERROR, "%5.1f, ", table[i]); | |
141 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1); | |
142 } | |
143 } | |
144 | |
145 static void dump_int_table(int* table, int size, int delimiter) { | |
146 int i=0; | |
147 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i); | |
148 for (i=0 ; i<size ; i++) { | |
149 av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]); | |
150 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1); | |
151 } | |
152 } | |
153 | |
154 static void dump_short_table(short* table, int size, int delimiter) { | |
155 int i=0; | |
156 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i); | |
157 for (i=0 ; i<size ; i++) { | |
158 av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]); | |
159 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1); | |
160 } | |
161 } | |
162 | |
163 #endif | |
164 | |
165 /*************** init functions ***************/ | |
166 | |
167 /* table generator */ | |
168 static void init_pow2table(COOKContext *q){ | |
169 int i; | |
170 q->pow2tab[63] = 1.0; | |
171 for (i=1 ; i<64 ; i++){ | |
3106
9cbd63cca826
Don't use pow/powf functions where we just need integer arithmetic.
al
parents:
3091
diff
changeset
|
172 q->pow2tab[63+i]=(float)((uint64_t)1<<i); |
9cbd63cca826
Don't use pow/powf functions where we just need integer arithmetic.
al
parents:
3091
diff
changeset
|
173 q->pow2tab[63-i]=1.0/(float)((uint64_t)1<<i); |
2956 | 174 } |
175 } | |
176 | |
177 /* table generator */ | |
178 static void init_rootpow2table(COOKContext *q){ | |
179 int i; | |
180 q->rootpow2tab[63] = 1.0; | |
181 for (i=1 ; i<64 ; i++){ | |
3106
9cbd63cca826
Don't use pow/powf functions where we just need integer arithmetic.
al
parents:
3091
diff
changeset
|
182 q->rootpow2tab[63+i]=sqrt((float)((uint64_t)1<<i)); |
9cbd63cca826
Don't use pow/powf functions where we just need integer arithmetic.
al
parents:
3091
diff
changeset
|
183 q->rootpow2tab[63-i]=sqrt(1.0/(float)((uint64_t)1<<i)); |
2956 | 184 } |
185 } | |
186 | |
187 /* table generator */ | |
188 static void init_gain_table(COOKContext *q) { | |
189 int i; | |
190 q->gain_size_factor = q->samples_per_channel/8; | |
191 for (i=0 ; i<23 ; i++) { | |
192 q->gain_table[i] = pow((double)q->pow2tab[i+52] , | |
193 (1.0/(double)q->gain_size_factor)); | |
194 } | |
195 } | |
196 | |
197 | |
198 static int init_cook_vlc_tables(COOKContext *q) { | |
199 int i, result; | |
200 | |
201 result = 0; | |
202 for (i=0 ; i<13 ; i++) { | |
203 result &= init_vlc (&q->envelope_quant_index[i], 9, 24, | |
204 envelope_quant_index_huffbits[i], 1, 1, | |
205 envelope_quant_index_huffcodes[i], 2, 2, 0); | |
206 } | |
207 av_log(NULL,AV_LOG_DEBUG,"sqvh VLC init\n"); | |
208 for (i=0 ; i<7 ; i++) { | |
209 result &= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i], | |
210 cvh_huffbits[i], 1, 1, | |
211 cvh_huffcodes[i], 2, 2, 0); | |
212 } | |
213 | |
214 if (q->nb_channels==2 && q->joint_stereo==1){ | |
215 result &= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1, | |
216 ccpl_huffbits[q->js_vlc_bits-2], 1, 1, | |
217 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0); | |
218 av_log(NULL,AV_LOG_DEBUG,"Joint-stereo VLC used.\n"); | |
219 } | |
220 | |
221 av_log(NULL,AV_LOG_DEBUG,"VLC tables initialized.\n"); | |
222 return result; | |
223 } | |
224 | |
225 static int init_cook_mlt(COOKContext *q) { | |
226 int j; | |
227 float alpha; | |
228 | |
229 /* Allocate the buffers, could be replaced with a static [512] | |
230 array if needed. */ | |
231 q->mlt_size = q->samples_per_channel; | |
232 q->mlt_window = av_malloc(sizeof(float)*q->mlt_size); | |
233 q->mlt_precos = av_malloc(sizeof(float)*q->mlt_size/2); | |
234 q->mlt_presin = av_malloc(sizeof(float)*q->mlt_size/2); | |
235 q->mlt_postcos = av_malloc(sizeof(float)*q->mlt_size/2); | |
236 | |
237 /* Initialize the MLT window: simple sine window. */ | |
238 alpha = M_PI / (2.0 * (float)q->mlt_size); | |
239 for(j=0 ; j<q->mlt_size ; j++) { | |
240 q->mlt_window[j] = sin((j + 512.0/(float)q->mlt_size) * alpha); | |
241 } | |
242 | |
243 /* pre/post twiddle factors */ | |
244 for (j=0 ; j<q->mlt_size/2 ; j++){ | |
245 q->mlt_precos[j] = cos( ((j+0.25)*M_PI)/q->mlt_size); | |
246 q->mlt_presin[j] = sin( ((j+0.25)*M_PI)/q->mlt_size); | |
247 q->mlt_postcos[j] = (float)sqrt(2.0/(float)q->mlt_size)*cos( ((float)j*M_PI) /q->mlt_size); //sqrt(2/MLT_size) = scalefactor | |
248 } | |
249 | |
250 /* Initialize the FFT. */ | |
251 ff_fft_init(&q->fft_ctx, av_log2(q->mlt_size)-1, 0); | |
252 av_log(NULL,AV_LOG_DEBUG,"FFT initialized, order = %d.\n", | |
253 av_log2(q->samples_per_channel)-1); | |
254 | |
255 return (int)(q->mlt_window && q->mlt_precos && q->mlt_presin && q->mlt_postcos); | |
256 } | |
257 | |
258 /*************** init functions end ***********/ | |
259 | |
260 /** | |
261 * Cook indata decoding, every 32 bits are XORed with 0x37c511f2. | |
262 * Why? No idea, some checksum/error detection method maybe. | |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
263 * |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
264 * Out buffer size: extra bytes are needed to cope with |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
265 * padding/missalignment. |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
266 * Subpackets passed to the decoder can contain two, consecutive |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
267 * half-subpackets, of identical but arbitrary size. |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
268 * 1234 1234 1234 1234 extraA extraB |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
269 * Case 1: AAAA BBBB 0 0 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
270 * Case 2: AAAA ABBB BB-- 3 3 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
271 * Case 3: AAAA AABB BBBB 2 2 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
272 * Case 4: AAAA AAAB BBBB BB-- 1 5 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
273 * |
2956 | 274 * Nice way to waste CPU cycles. |
275 * | |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
276 * @param inbuffer pointer to byte array of indata |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
277 * @param out pointer to byte array of outdata |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
278 * @param bytes number of bytes |
2956 | 279 */ |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
280 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4) |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
281 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes))) |
2956 | 282 |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
283 static inline int decode_bytes(uint8_t* inbuffer, uint8_t* out, int bytes){ |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
284 int i, off; |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
285 uint32_t c; |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
286 uint32_t* buf; |
2956 | 287 uint32_t* obuf = (uint32_t*) out; |
288 /* FIXME: 64 bit platforms would be able to do 64 bits at a time. | |
289 * I'm too lazy though, should be something like | |
290 * for(i=0 ; i<bitamount/64 ; i++) | |
291 * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]); | |
292 * Buffer alignment needs to be checked. */ | |
293 | |
4429 | 294 off = (int)((long)inbuffer & 3); |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
295 buf = (uint32_t*) (inbuffer - off); |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
296 c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8)))); |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
297 bytes += 3 + off; |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
298 for (i = 0; i < bytes/4; i++) |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
299 obuf[i] = c ^ buf[i]; |
2956 | 300 |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
301 return off; |
2956 | 302 } |
303 | |
304 /** | |
305 * Cook uninit | |
306 */ | |
307 | |
308 static int cook_decode_close(AVCodecContext *avctx) | |
309 { | |
310 int i; | |
311 COOKContext *q = avctx->priv_data; | |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
312 av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n"); |
2956 | 313 |
314 /* Free allocated memory buffers. */ | |
315 av_free(q->mlt_window); | |
316 av_free(q->mlt_precos); | |
317 av_free(q->mlt_presin); | |
318 av_free(q->mlt_postcos); | |
319 av_free(q->decoded_bytes_buffer); | |
320 | |
321 /* Free the transform. */ | |
322 ff_fft_end(&q->fft_ctx); | |
323 | |
324 /* Free the VLC tables. */ | |
325 for (i=0 ; i<13 ; i++) { | |
326 free_vlc(&q->envelope_quant_index[i]); | |
327 } | |
328 for (i=0 ; i<7 ; i++) { | |
329 free_vlc(&q->sqvh[i]); | |
330 } | |
331 if(q->nb_channels==2 && q->joint_stereo==1 ){ | |
332 free_vlc(&q->ccpl); | |
333 } | |
334 | |
335 av_log(NULL,AV_LOG_DEBUG,"Memory deallocated.\n"); | |
336 | |
337 return 0; | |
338 } | |
339 | |
340 /** | |
4639 | 341 * Fill the gain array for the timedomain quantization. |
2956 | 342 * |
343 * @param q pointer to the COOKContext | |
4639 | 344 * @param gaininfo[9] array of gain indices |
2956 | 345 */ |
346 | |
4639 | 347 static void decode_gain_info(GetBitContext *gb, int *gaininfo) |
348 { | |
349 int i, n; | |
2956 | 350 |
351 while (get_bits1(gb)) {} | |
4639 | 352 n = get_bits_count(gb) - 1; //amount of elements*2 to update |
2956 | 353 |
4639 | 354 i = 0; |
355 while (n--) { | |
356 int index = get_bits(gb, 3); | |
357 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1; | |
358 | |
359 while (i <= index) gaininfo[i++] = gain; | |
2956 | 360 } |
4639 | 361 while (i <= 8) gaininfo[i++] = 0; |
2956 | 362 } |
363 | |
364 /** | |
365 * Create the quant index table needed for the envelope. | |
366 * | |
367 * @param q pointer to the COOKContext | |
368 * @param quant_index_table pointer to the array | |
369 */ | |
370 | |
371 static void decode_envelope(COOKContext *q, int* quant_index_table) { | |
372 int i,j, vlc_index; | |
373 int bitbias; | |
374 | |
375 bitbias = get_bits_count(&q->gb); | |
376 quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize | |
377 | |
378 for (i=1 ; i < q->total_subbands ; i++){ | |
379 vlc_index=i; | |
380 if (i >= q->js_subband_start * 2) { | |
381 vlc_index-=q->js_subband_start; | |
382 } else { | |
383 vlc_index/=2; | |
384 if(vlc_index < 1) vlc_index = 1; | |
385 } | |
386 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13 | |
387 | |
388 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table, | |
389 q->envelope_quant_index[vlc_index-1].bits,2); | |
390 quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding | |
391 } | |
392 } | |
393 | |
394 /** | |
395 * Create the quant value table. | |
396 * | |
397 * @param q pointer to the COOKContext | |
398 * @param quant_value_table pointer to the array | |
399 */ | |
400 | |
401 static void inline dequant_envelope(COOKContext *q, int* quant_index_table, | |
402 float* quant_value_table){ | |
403 | |
404 int i; | |
405 for(i=0 ; i < q->total_subbands ; i++){ | |
406 quant_value_table[i] = q->rootpow2tab[quant_index_table[i]+63]; | |
407 } | |
408 } | |
409 | |
410 /** | |
411 * Calculate the category and category_index vector. | |
412 * | |
413 * @param q pointer to the COOKContext | |
414 * @param quant_index_table pointer to the array | |
415 * @param category pointer to the category array | |
416 * @param category_index pointer to the category_index array | |
417 */ | |
418 | |
419 static void categorize(COOKContext *q, int* quant_index_table, | |
420 int* category, int* category_index){ | |
421 int exp_idx, bias, tmpbias, bits_left, num_bits, index, v, i, j; | |
422 int exp_index2[102]; | |
423 int exp_index1[102]; | |
424 | |
425 int tmp_categorize_array1[128]; | |
426 int tmp_categorize_array1_idx=0; | |
427 int tmp_categorize_array2[128]; | |
428 int tmp_categorize_array2_idx=0; | |
429 int category_index_size=0; | |
430 | |
431 bits_left = q->bits_per_subpacket - get_bits_count(&q->gb); | |
432 | |
433 if(bits_left > q->samples_per_channel) { | |
434 bits_left = q->samples_per_channel + | |
435 ((bits_left - q->samples_per_channel)*5)/8; | |
436 //av_log(NULL, AV_LOG_ERROR, "bits_left = %d\n",bits_left); | |
437 } | |
438 | |
439 memset(&exp_index1,0,102*sizeof(int)); | |
440 memset(&exp_index2,0,102*sizeof(int)); | |
441 memset(&tmp_categorize_array1,0,128*sizeof(int)); | |
442 memset(&tmp_categorize_array2,0,128*sizeof(int)); | |
443 | |
444 bias=-32; | |
445 | |
446 /* Estimate bias. */ | |
447 for (i=32 ; i>0 ; i=i/2){ | |
448 num_bits = 0; | |
449 index = 0; | |
450 for (j=q->total_subbands ; j>0 ; j--){ | |
451 exp_idx = (i - quant_index_table[index] + bias) / 2; | |
452 if (exp_idx<0){ | |
453 exp_idx=0; | |
454 } else if(exp_idx >7) { | |
455 exp_idx=7; | |
456 } | |
457 index++; | |
458 num_bits+=expbits_tab[exp_idx]; | |
459 } | |
460 if(num_bits >= bits_left - 32){ | |
461 bias+=i; | |
462 } | |
463 } | |
464 | |
465 /* Calculate total number of bits. */ | |
466 num_bits=0; | |
467 for (i=0 ; i<q->total_subbands ; i++) { | |
468 exp_idx = (bias - quant_index_table[i]) / 2; | |
469 if (exp_idx<0) { | |
470 exp_idx=0; | |
471 } else if(exp_idx >7) { | |
472 exp_idx=7; | |
473 } | |
474 num_bits += expbits_tab[exp_idx]; | |
475 exp_index1[i] = exp_idx; | |
476 exp_index2[i] = exp_idx; | |
477 } | |
478 tmpbias = bias = num_bits; | |
479 | |
480 for (j = 1 ; j < q->numvector_size ; j++) { | |
481 if (tmpbias + bias > 2*bits_left) { /* ---> */ | |
482 int max = -999999; | |
483 index=-1; | |
484 for (i=0 ; i<q->total_subbands ; i++){ | |
485 if (exp_index1[i] < 7) { | |
486 v = (-2*exp_index1[i]) - quant_index_table[i] - 32; | |
487 if ( v >= max) { | |
488 max = v; | |
489 index = i; | |
490 } | |
491 } | |
492 } | |
493 if(index==-1)break; | |
494 tmp_categorize_array1[tmp_categorize_array1_idx++] = index; | |
495 tmpbias -= expbits_tab[exp_index1[index]] - | |
496 expbits_tab[exp_index1[index]+1]; | |
497 ++exp_index1[index]; | |
498 } else { /* <--- */ | |
499 int min = 999999; | |
500 index=-1; | |
501 for (i=0 ; i<q->total_subbands ; i++){ | |
502 if(exp_index2[i] > 0){ | |
503 v = (-2*exp_index2[i])-quant_index_table[i]; | |
504 if ( v < min) { | |
505 min = v; | |
506 index = i; | |
507 } | |
508 } | |
509 } | |
510 if(index == -1)break; | |
511 tmp_categorize_array2[tmp_categorize_array2_idx++] = index; | |
512 tmpbias -= expbits_tab[exp_index2[index]] - | |
513 expbits_tab[exp_index2[index]-1]; | |
514 --exp_index2[index]; | |
515 } | |
516 } | |
517 | |
518 for(i=0 ; i<q->total_subbands ; i++) | |
519 category[i] = exp_index2[i]; | |
520 | |
521 /* Concatenate the two arrays. */ | |
522 for(i=tmp_categorize_array2_idx-1 ; i >= 0; i--) | |
523 category_index[category_index_size++] = tmp_categorize_array2[i]; | |
524 | |
525 for(i=0;i<tmp_categorize_array1_idx;i++) | |
526 category_index[category_index_size++ ] = tmp_categorize_array1[i]; | |
527 | |
528 /* FIXME: mc_sich_ra8_20.rm triggers this, not sure with what we | |
529 should fill the remaining bytes. */ | |
530 for(i=category_index_size;i<q->numvector_size;i++) | |
531 category_index[i]=0; | |
532 | |
533 } | |
534 | |
535 | |
536 /** | |
537 * Expand the category vector. | |
538 * | |
539 * @param q pointer to the COOKContext | |
540 * @param category pointer to the category array | |
541 * @param category_index pointer to the category_index array | |
542 */ | |
543 | |
544 static void inline expand_category(COOKContext *q, int* category, | |
545 int* category_index){ | |
546 int i; | |
547 for(i=0 ; i<q->num_vectors ; i++){ | |
548 ++category[category_index[i]]; | |
549 } | |
550 } | |
551 | |
552 /** | |
553 * The real requantization of the mltcoefs | |
554 * | |
555 * @param q pointer to the COOKContext | |
556 * @param index index | |
557 * @param band current subband | |
558 * @param quant_value_table pointer to the array | |
559 * @param subband_coef_index array of indexes to quant_centroid_tab | |
560 * @param subband_coef_noise use random noise instead of predetermined value | |
561 * @param mlt_buffer pointer to the mlt buffer | |
562 */ | |
563 | |
564 | |
565 static void scalar_dequant(COOKContext *q, int index, int band, | |
566 float* quant_value_table, int* subband_coef_index, | |
567 int* subband_coef_noise, float* mlt_buffer){ | |
568 int i; | |
569 float f1; | |
570 | |
571 for(i=0 ; i<SUBBAND_SIZE ; i++) { | |
572 if (subband_coef_index[i]) { | |
573 if (subband_coef_noise[i]) { | |
574 f1 = -quant_centroid_tab[index][subband_coef_index[i]]; | |
575 } else { | |
576 f1 = quant_centroid_tab[index][subband_coef_index[i]]; | |
577 } | |
578 } else { | |
579 /* noise coding if subband_coef_noise[i] == 0 */ | |
580 q->random_state = q->random_state * 214013 + 2531011; //typical RNG numbers | |
581 f1 = randsign[(q->random_state/0x1000000)&1] * dither_tab[index]; //>>31 | |
582 } | |
583 mlt_buffer[band*20+ i] = f1 * quant_value_table[band]; | |
584 } | |
585 } | |
586 /** | |
587 * Unpack the subband_coef_index and subband_coef_noise vectors. | |
588 * | |
589 * @param q pointer to the COOKContext | |
590 * @param category pointer to the category array | |
591 * @param subband_coef_index array of indexes to quant_centroid_tab | |
592 * @param subband_coef_noise use random noise instead of predetermined value | |
593 */ | |
594 | |
595 static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index, | |
596 int* subband_coef_noise) { | |
597 int i,j; | |
598 int vlc, vd ,tmp, result; | |
599 int ub; | |
600 int cb; | |
601 | |
602 vd = vd_tab[category]; | |
603 result = 0; | |
604 for(i=0 ; i<vpr_tab[category] ; i++){ | |
605 ub = get_bits_count(&q->gb); | |
606 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3); | |
607 cb = get_bits_count(&q->gb); | |
608 if (q->bits_per_subpacket < get_bits_count(&q->gb)){ | |
609 vlc = 0; | |
610 result = 1; | |
611 } | |
612 for(j=vd-1 ; j>=0 ; j--){ | |
613 tmp = (vlc * invradix_tab[category])/0x100000; | |
614 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1); | |
615 vlc = tmp; | |
616 } | |
617 for(j=0 ; j<vd ; j++){ | |
618 if (subband_coef_index[i*vd + j]) { | |
619 if(get_bits_count(&q->gb) < q->bits_per_subpacket){ | |
620 subband_coef_noise[i*vd+j] = get_bits1(&q->gb); | |
621 } else { | |
622 result=1; | |
623 subband_coef_noise[i*vd+j]=0; | |
624 } | |
625 } else { | |
626 subband_coef_noise[i*vd+j]=0; | |
627 } | |
628 } | |
629 } | |
630 return result; | |
631 } | |
632 | |
633 | |
634 /** | |
635 * Fill the mlt_buffer with mlt coefficients. | |
636 * | |
637 * @param q pointer to the COOKContext | |
638 * @param category pointer to the category array | |
639 * @param quant_value_table pointer to the array | |
640 * @param mlt_buffer pointer to mlt coefficients | |
641 */ | |
642 | |
643 | |
644 static void decode_vectors(COOKContext* q, int* category, | |
645 float* quant_value_table, float* mlt_buffer){ | |
646 /* A zero in this table means that the subband coefficient is | |
647 random noise coded. */ | |
648 int subband_coef_noise[SUBBAND_SIZE]; | |
649 /* A zero in this table means that the subband coefficient is a | |
650 positive multiplicator. */ | |
651 int subband_coef_index[SUBBAND_SIZE]; | |
652 int band, j; | |
653 int index=0; | |
654 | |
655 for(band=0 ; band<q->total_subbands ; band++){ | |
656 index = category[band]; | |
657 if(category[band] < 7){ | |
658 if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_noise)){ | |
659 index=7; | |
660 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7; | |
661 } | |
662 } | |
663 if(index==7) { | |
664 memset(subband_coef_index, 0, sizeof(subband_coef_index)); | |
665 memset(subband_coef_noise, 0, sizeof(subband_coef_noise)); | |
666 } | |
667 scalar_dequant(q, index, band, quant_value_table, subband_coef_index, | |
668 subband_coef_noise, mlt_buffer); | |
669 } | |
670 | |
671 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){ | |
672 return; | |
673 } | |
674 } | |
675 | |
676 | |
677 /** | |
678 * function for decoding mono data | |
679 * | |
680 * @param q pointer to the COOKContext | |
681 * @param mlt_buffer1 pointer to left channel mlt coefficients | |
682 * @param mlt_buffer2 pointer to right channel mlt coefficients | |
683 */ | |
684 | |
685 static void mono_decode(COOKContext *q, float* mlt_buffer) { | |
686 | |
687 int category_index[128]; | |
688 float quant_value_table[102]; | |
689 int quant_index_table[102]; | |
690 int category[128]; | |
691 | |
692 memset(&category, 0, 128*sizeof(int)); | |
693 memset(&quant_value_table, 0, 102*sizeof(int)); | |
694 memset(&category_index, 0, 128*sizeof(int)); | |
695 | |
696 decode_envelope(q, quant_index_table); | |
3090 | 697 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size); |
2956 | 698 dequant_envelope(q, quant_index_table, quant_value_table); |
699 categorize(q, quant_index_table, category, category_index); | |
700 expand_category(q, category, category_index); | |
701 decode_vectors(q, category, quant_value_table, mlt_buffer); | |
702 } | |
703 | |
704 | |
705 /** | |
706 * The modulated lapped transform, this takes transform coefficients | |
707 * and transforms them into timedomain samples. This is done through | |
708 * an FFT-based algorithm with pre- and postrotation steps. | |
709 * A window and reorder step is also included. | |
710 * | |
711 * @param q pointer to the COOKContext | |
712 * @param inbuffer pointer to the mltcoefficients | |
713 * @param outbuffer pointer to the timedomain buffer | |
714 * @param mlt_tmp pointer to temporary storage space | |
715 */ | |
716 | |
717 static void cook_imlt(COOKContext *q, float* inbuffer, float* outbuffer, | |
718 float* mlt_tmp){ | |
719 int i; | |
720 | |
721 /* prerotation */ | |
722 for(i=0 ; i<q->mlt_size ; i+=2){ | |
723 outbuffer[i] = (q->mlt_presin[i/2] * inbuffer[q->mlt_size-1-i]) + | |
724 (q->mlt_precos[i/2] * inbuffer[i]); | |
725 outbuffer[i+1] = (q->mlt_precos[i/2] * inbuffer[q->mlt_size-1-i]) - | |
726 (q->mlt_presin[i/2] * inbuffer[i]); | |
727 } | |
728 | |
729 /* FFT */ | |
730 ff_fft_permute(&q->fft_ctx, (FFTComplex *) outbuffer); | |
731 ff_fft_calc (&q->fft_ctx, (FFTComplex *) outbuffer); | |
732 | |
733 /* postrotation */ | |
734 for(i=0 ; i<q->mlt_size ; i+=2){ | |
735 mlt_tmp[i] = (q->mlt_postcos[(q->mlt_size-1-i)/2] * outbuffer[i+1]) + | |
736 (q->mlt_postcos[i/2] * outbuffer[i]); | |
737 mlt_tmp[q->mlt_size-1-i] = (q->mlt_postcos[(q->mlt_size-1-i)/2] * outbuffer[i]) - | |
738 (q->mlt_postcos[i/2] * outbuffer[i+1]); | |
739 } | |
740 | |
741 /* window and reorder */ | |
742 for(i=0 ; i<q->mlt_size/2 ; i++){ | |
743 outbuffer[i] = mlt_tmp[q->mlt_size/2-1-i] * q->mlt_window[i]; | |
744 outbuffer[q->mlt_size-1-i]= mlt_tmp[q->mlt_size/2-1-i] * | |
745 q->mlt_window[q->mlt_size-1-i]; | |
746 outbuffer[q->mlt_size+i]= mlt_tmp[q->mlt_size/2+i] * | |
747 q->mlt_window[q->mlt_size-1-i]; | |
748 outbuffer[2*q->mlt_size-1-i]= -(mlt_tmp[q->mlt_size/2+i] * | |
749 q->mlt_window[i]); | |
750 } | |
751 } | |
752 | |
753 | |
754 /** | |
755 * the actual requantization of the timedomain samples | |
756 * | |
757 * @param q pointer to the COOKContext | |
758 * @param buffer pointer to the timedomain buffer | |
759 * @param gain_index index for the block multiplier | |
760 * @param gain_index_next index for the next block multiplier | |
761 */ | |
762 | |
763 static void interpolate(COOKContext *q, float* buffer, | |
764 int gain_index, int gain_index_next){ | |
765 int i; | |
766 float fc1, fc2; | |
767 fc1 = q->pow2tab[gain_index+63]; | |
768 | |
769 if(gain_index == gain_index_next){ //static gain | |
770 for(i=0 ; i<q->gain_size_factor ; i++){ | |
771 buffer[i]*=fc1; | |
772 } | |
773 return; | |
774 } else { //smooth gain | |
775 fc2 = q->gain_table[11 + (gain_index_next-gain_index)]; | |
776 for(i=0 ; i<q->gain_size_factor ; i++){ | |
777 buffer[i]*=fc1; | |
778 fc1*=fc2; | |
779 } | |
780 return; | |
781 } | |
782 } | |
783 | |
784 | |
785 /** | |
786 * mlt overlapping and buffer management | |
787 * | |
788 * @param q pointer to the COOKContext | |
4639 | 789 * @param gains_ptr current and previous gains |
2956 | 790 * @param previous_buffer pointer to the previous buffer to be used for overlapping |
791 */ | |
792 | |
4639 | 793 static void gain_compensate(COOKContext *q, cook_gains *gains_ptr, |
794 float* previous_buffer) | |
795 { | |
796 const float fc = q->pow2tab[gains_ptr->previous[0] + 63]; | |
797 float *buffer = q->mono_mdct_output; | |
2956 | 798 int i; |
4639 | 799 |
800 /* Overlap with the previous block. */ | |
801 for(i=0 ; i<q->samples_per_channel ; i++) { | |
802 buffer[i] *= fc; | |
803 buffer[i] += previous_buffer[i]; | |
2956 | 804 } |
805 | |
4639 | 806 /* Apply gain profile */ |
807 for (i = 0; i < 8; i++) { | |
808 if (gains_ptr->now[i] || gains_ptr->now[i + 1]) | |
809 interpolate(q, &buffer[q->gain_size_factor * i], | |
810 gains_ptr->now[i], gains_ptr->now[i + 1]); | |
811 } | |
2956 | 812 |
813 /* Save away the current to be previous block. */ | |
814 memcpy(previous_buffer, buffer+q->samples_per_channel, | |
815 sizeof(float)*q->samples_per_channel); | |
816 } | |
817 | |
818 | |
819 /** | |
820 * function for getting the jointstereo coupling information | |
821 * | |
822 * @param q pointer to the COOKContext | |
823 * @param decouple_tab decoupling array | |
824 * | |
825 */ | |
826 | |
827 static void decouple_info(COOKContext *q, int* decouple_tab){ | |
828 int length, i; | |
829 | |
830 if(get_bits1(&q->gb)) { | |
831 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; | |
832 | |
833 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; | |
834 for (i=0 ; i<length ; i++) { | |
835 decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2); | |
836 } | |
837 return; | |
838 } | |
839 | |
840 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; | |
841 | |
842 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; | |
843 for (i=0 ; i<length ; i++) { | |
844 decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits); | |
845 } | |
846 return; | |
847 } | |
848 | |
849 | |
850 /** | |
851 * function for decoding joint stereo data | |
852 * | |
853 * @param q pointer to the COOKContext | |
854 * @param mlt_buffer1 pointer to left channel mlt coefficients | |
855 * @param mlt_buffer2 pointer to right channel mlt coefficients | |
856 */ | |
857 | |
858 static void joint_decode(COOKContext *q, float* mlt_buffer1, | |
859 float* mlt_buffer2) { | |
860 int i,j; | |
861 int decouple_tab[SUBBAND_SIZE]; | |
3009
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
862 float decode_buffer[1060]; |
2956 | 863 int idx, cpl_tmp,tmp_idx; |
864 float f1,f2; | |
865 float* cplscale; | |
866 | |
867 memset(decouple_tab, 0, sizeof(decouple_tab)); | |
868 memset(decode_buffer, 0, sizeof(decode_buffer)); | |
869 | |
870 /* Make sure the buffers are zeroed out. */ | |
871 memset(mlt_buffer1,0, 1024*sizeof(float)); | |
872 memset(mlt_buffer2,0, 1024*sizeof(float)); | |
873 decouple_info(q, decouple_tab); | |
874 mono_decode(q, decode_buffer); | |
875 | |
876 /* The two channels are stored interleaved in decode_buffer. */ | |
877 for (i=0 ; i<q->js_subband_start ; i++) { | |
878 for (j=0 ; j<SUBBAND_SIZE ; j++) { | |
879 mlt_buffer1[i*20+j] = decode_buffer[i*40+j]; | |
880 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j]; | |
881 } | |
882 } | |
883 | |
884 /* When we reach js_subband_start (the higher frequencies) | |
885 the coefficients are stored in a coupling scheme. */ | |
886 idx = (1 << q->js_vlc_bits) - 1; | |
3009
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
887 for (i=q->js_subband_start ; i<q->subbands ; i++) { |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
888 cpl_tmp = cplband[i]; |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
889 idx -=decouple_tab[cpl_tmp]; |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
890 cplscale = (float*)cplscales[q->js_vlc_bits-2]; //choose decoupler table |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
891 f1 = cplscale[decouple_tab[cpl_tmp]]; |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
892 f2 = cplscale[idx-1]; |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
893 for (j=0 ; j<SUBBAND_SIZE ; j++) { |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
894 tmp_idx = ((q->js_subband_start + i)*20)+j; |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
895 mlt_buffer1[20*i + j] = f1 * decode_buffer[tmp_idx]; |
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
896 mlt_buffer2[20*i + j] = f2 * decode_buffer[tmp_idx]; |
2956 | 897 } |
3009
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
898 idx = (1 << q->js_vlc_bits) - 1; |
2956 | 899 } |
900 } | |
901 | |
902 /** | |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
903 * First part of subpacket decoding: |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
904 * decode raw stream bytes and read gain info. |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
905 * |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
906 * @param q pointer to the COOKContext |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
907 * @param inbuffer pointer to raw stream data |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
908 * @param gain_ptr array of current/prev gain pointers |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
909 */ |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
910 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
911 static inline void |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
912 decode_bytes_and_gain(COOKContext *q, uint8_t *inbuffer, |
4639 | 913 cook_gains *gains_ptr) |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
914 { |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
915 int offset; |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
916 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
917 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer, |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
918 q->bits_per_subpacket/8); |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
919 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset, |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
920 q->bits_per_subpacket); |
4639 | 921 decode_gain_info(&q->gb, gains_ptr->now); |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
922 |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
923 /* Swap current and previous gains */ |
4639 | 924 FFSWAP(int *, gains_ptr->now, gains_ptr->previous); |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
925 } |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
926 |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
927 /** |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
928 * Final part of subpacket decoding: |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
929 * Apply modulated lapped transform, gain compensation, |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
930 * clip and convert to integer. |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
931 * |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
932 * @param q pointer to the COOKContext |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
933 * @param decode_buffer pointer to the mlt coefficients |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
934 * @param gain_ptr array of current/prev gain pointers |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
935 * @param previous_buffer pointer to the previous buffer to be used for overlapping |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
936 * @param out pointer to the output buffer |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
937 * @param chan 0: left or single channel, 1: right channel |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
938 */ |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
939 |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
940 static inline void |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
941 mlt_compensate_output(COOKContext *q, float *decode_buffer, |
4639 | 942 cook_gains *gains, float *previous_buffer, |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
943 int16_t *out, int chan) |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
944 { |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
945 int j; |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
946 |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
947 cook_imlt(q, decode_buffer, q->mono_mdct_output, q->mlt_tmp); |
4639 | 948 gain_compensate(q, gains, previous_buffer); |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
949 |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
950 /* Clip and convert floats to 16 bits. |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
951 */ |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
952 for (j = 0; j < q->samples_per_channel; j++) { |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
953 out[chan + q->nb_channels * j] = |
4594 | 954 av_clip(lrintf(q->mono_mdct_output[j]), -32768, 32767); |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
955 } |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
956 } |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
957 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
958 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
959 /** |
2956 | 960 * Cook subpacket decoding. This function returns one decoded subpacket, |
961 * usually 1024 samples per channel. | |
962 * | |
963 * @param q pointer to the COOKContext | |
964 * @param inbuffer pointer to the inbuffer | |
965 * @param sub_packet_size subpacket size | |
966 * @param outbuffer pointer to the outbuffer | |
967 */ | |
968 | |
969 | |
970 static int decode_subpacket(COOKContext *q, uint8_t *inbuffer, | |
971 int sub_packet_size, int16_t *outbuffer) { | |
972 /* packet dump */ | |
973 // for (i=0 ; i<sub_packet_size ; i++) { | |
974 // av_log(NULL, AV_LOG_ERROR, "%02x", inbuffer[i]); | |
975 // } | |
976 // av_log(NULL, AV_LOG_ERROR, "\n"); | |
977 | |
4639 | 978 decode_bytes_and_gain(q, inbuffer, &q->gains1); |
2956 | 979 |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
980 if (q->joint_stereo) { |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
981 joint_decode(q, q->decode_buffer_1, q->decode_buffer_2); |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
982 } else { |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
983 mono_decode(q, q->decode_buffer_1); |
2956 | 984 |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
985 if (q->nb_channels == 2) { |
4639 | 986 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2); |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
987 mono_decode(q, q->decode_buffer_2); |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
988 } |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
989 } |
3014
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
990 |
4639 | 991 mlt_compensate_output(q, q->decode_buffer_1, &q->gains1, |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
992 q->mono_previous_buffer1, outbuffer, 0); |
2956 | 993 |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
994 if (q->nb_channels == 2) { |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
995 if (q->joint_stereo) { |
4639 | 996 mlt_compensate_output(q, q->decode_buffer_2, &q->gains1, |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
997 q->mono_previous_buffer2, outbuffer, 1); |
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
998 } else { |
4639 | 999 mlt_compensate_output(q, q->decode_buffer_2, &q->gains2, |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
1000 q->mono_previous_buffer2, outbuffer, 1); |
2956 | 1001 } |
1002 } | |
2959
24805f4d1b84
This patch adds some support for non-joint stereo streams. It also
rtognimp
parents:
2956
diff
changeset
|
1003 return q->samples_per_frame * sizeof(int16_t); |
2956 | 1004 } |
1005 | |
1006 | |
1007 /** | |
1008 * Cook frame decoding | |
1009 * | |
1010 * @param avctx pointer to the AVCodecContext | |
1011 */ | |
1012 | |
1013 static int cook_decode_frame(AVCodecContext *avctx, | |
1014 void *data, int *data_size, | |
1015 uint8_t *buf, int buf_size) { | |
1016 COOKContext *q = avctx->priv_data; | |
1017 | |
1018 if (buf_size < avctx->block_align) | |
1019 return buf_size; | |
1020 | |
1021 *data_size = decode_subpacket(q, buf, avctx->block_align, data); | |
1022 | |
4638
9f74306d4ac7
Don't output the first two frames, since they don't contain valid audio.
banan
parents:
4594
diff
changeset
|
1023 /* Discard the first two frames: no valid audio. */ |
9f74306d4ac7
Don't output the first two frames, since they don't contain valid audio.
banan
parents:
4594
diff
changeset
|
1024 if (avctx->frame_number < 2) *data_size = 0; |
9f74306d4ac7
Don't output the first two frames, since they don't contain valid audio.
banan
parents:
4594
diff
changeset
|
1025 |
2956 | 1026 return avctx->block_align; |
1027 } | |
3090 | 1028 |
2956 | 1029 #ifdef COOKDEBUG |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1030 static void dump_cook_context(COOKContext *q) |
2956 | 1031 { |
1032 //int i=0; | |
1033 #define PRINT(a,b) av_log(NULL,AV_LOG_ERROR," %s = %d\n", a, b); | |
1034 av_log(NULL,AV_LOG_ERROR,"COOKextradata\n"); | |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1035 av_log(NULL,AV_LOG_ERROR,"cookversion=%x\n",q->cookversion); |
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1036 if (q->cookversion > STEREO) { |
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1037 PRINT("js_subband_start",q->js_subband_start); |
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1038 PRINT("js_vlc_bits",q->js_vlc_bits); |
2956 | 1039 } |
1040 av_log(NULL,AV_LOG_ERROR,"COOKContext\n"); | |
1041 PRINT("nb_channels",q->nb_channels); | |
1042 PRINT("bit_rate",q->bit_rate); | |
1043 PRINT("sample_rate",q->sample_rate); | |
1044 PRINT("samples_per_channel",q->samples_per_channel); | |
1045 PRINT("samples_per_frame",q->samples_per_frame); | |
1046 PRINT("subbands",q->subbands); | |
1047 PRINT("random_state",q->random_state); | |
1048 PRINT("mlt_size",q->mlt_size); | |
1049 PRINT("js_subband_start",q->js_subband_start); | |
3090 | 1050 PRINT("log2_numvector_size",q->log2_numvector_size); |
2956 | 1051 PRINT("numvector_size",q->numvector_size); |
1052 PRINT("total_subbands",q->total_subbands); | |
1053 } | |
1054 #endif | |
3090 | 1055 |
2956 | 1056 /** |
1057 * Cook initialization | |
1058 * | |
1059 * @param avctx pointer to the AVCodecContext | |
1060 */ | |
1061 | |
1062 static int cook_decode_init(AVCodecContext *avctx) | |
1063 { | |
1064 COOKContext *q = avctx->priv_data; | |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1065 uint8_t *edata_ptr = avctx->extradata; |
2956 | 1066 |
1067 /* Take care of the codec specific extradata. */ | |
1068 if (avctx->extradata_size <= 0) { | |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1069 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n"); |
2956 | 1070 return -1; |
1071 } else { | |
1072 /* 8 for mono, 16 for stereo, ? for multichannel | |
1073 Swap to right endianness so we don't need to care later on. */ | |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1074 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size); |
2956 | 1075 if (avctx->extradata_size >= 8){ |
4542 | 1076 q->cookversion = bytestream_get_be32(&edata_ptr); |
1077 q->samples_per_frame = bytestream_get_be16(&edata_ptr); | |
1078 q->subbands = bytestream_get_be16(&edata_ptr); | |
2956 | 1079 } |
1080 if (avctx->extradata_size >= 16){ | |
4542 | 1081 bytestream_get_be32(&edata_ptr); //Unknown unused |
1082 q->js_subband_start = bytestream_get_be16(&edata_ptr); | |
1083 q->js_vlc_bits = bytestream_get_be16(&edata_ptr); | |
2956 | 1084 } |
1085 } | |
1086 | |
1087 /* Take data from the AVCodecContext (RM container). */ | |
1088 q->sample_rate = avctx->sample_rate; | |
1089 q->nb_channels = avctx->channels; | |
1090 q->bit_rate = avctx->bit_rate; | |
1091 | |
1092 /* Initialize state. */ | |
1093 q->random_state = 1; | |
1094 | |
1095 /* Initialize extradata related variables. */ | |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1096 q->samples_per_channel = q->samples_per_frame / q->nb_channels; |
2956 | 1097 q->bits_per_subpacket = avctx->block_align * 8; |
1098 | |
1099 /* Initialize default data states. */ | |
3090 | 1100 q->log2_numvector_size = 5; |
2956 | 1101 q->total_subbands = q->subbands; |
1102 | |
1103 /* Initialize version-dependent variables */ | |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1104 av_log(NULL,AV_LOG_DEBUG,"q->cookversion=%x\n",q->cookversion); |
4428
eeb16216b454
decode_subpacket cleanup by Ian Braithwaite ian braithwaite dot dk.
banan
parents:
4425
diff
changeset
|
1105 q->joint_stereo = 0; |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1106 switch (q->cookversion) { |
4425 | 1107 case MONO: |
2956 | 1108 if (q->nb_channels != 1) { |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1109 av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n"); |
2956 | 1110 return -1; |
1111 } | |
4425 | 1112 av_log(avctx,AV_LOG_DEBUG,"MONO\n"); |
2956 | 1113 break; |
4425 | 1114 case STEREO: |
2956 | 1115 if (q->nb_channels != 1) { |
2959
24805f4d1b84
This patch adds some support for non-joint stereo streams. It also
rtognimp
parents:
2956
diff
changeset
|
1116 q->bits_per_subpacket = q->bits_per_subpacket/2; |
2956 | 1117 } |
4425 | 1118 av_log(avctx,AV_LOG_DEBUG,"STEREO\n"); |
2956 | 1119 break; |
1120 case JOINT_STEREO: | |
1121 if (q->nb_channels != 2) { | |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1122 av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n"); |
2956 | 1123 return -1; |
1124 } | |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1125 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n"); |
2956 | 1126 if (avctx->extradata_size >= 16){ |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1127 q->total_subbands = q->subbands + q->js_subband_start; |
2956 | 1128 q->joint_stereo = 1; |
1129 } | |
1130 if (q->samples_per_channel > 256) { | |
3091
0284d5b34916
Fix broken cosmetics commit and add a check for valid headers.
banan
parents:
3090
diff
changeset
|
1131 q->log2_numvector_size = 6; |
2956 | 1132 } |
1133 if (q->samples_per_channel > 512) { | |
3091
0284d5b34916
Fix broken cosmetics commit and add a check for valid headers.
banan
parents:
3090
diff
changeset
|
1134 q->log2_numvector_size = 7; |
2956 | 1135 } |
1136 break; | |
1137 case MC_COOK: | |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1138 av_log(avctx,AV_LOG_ERROR,"MC_COOK not supported!\n"); |
2956 | 1139 return -1; |
1140 break; | |
1141 default: | |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1142 av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n"); |
2956 | 1143 return -1; |
1144 break; | |
1145 } | |
1146 | |
1147 /* Initialize variable relations */ | |
1148 q->mlt_size = q->samples_per_channel; | |
3090 | 1149 q->numvector_size = (1 << q->log2_numvector_size); |
2956 | 1150 |
1151 /* Generate tables */ | |
1152 init_rootpow2table(q); | |
1153 init_pow2table(q); | |
1154 init_gain_table(q); | |
1155 | |
1156 if (init_cook_vlc_tables(q) != 0) | |
1157 return -1; | |
1158 | |
3303
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3106
diff
changeset
|
1159 |
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3106
diff
changeset
|
1160 if(avctx->block_align >= UINT_MAX/2) |
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3106
diff
changeset
|
1161 return -1; |
68721b62a528
sanity checks, some might have been exploitable ...
michael
parents:
3106
diff
changeset
|
1162 |
4424
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1163 /* Pad the databuffer with: |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1164 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(), |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1165 FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */ |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1166 if (q->nb_channels==2 && q->joint_stereo==0) { |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1167 q->decoded_bytes_buffer = |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1168 av_mallocz(avctx->block_align/2 |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1169 + DECODE_BYTES_PAD2(avctx->block_align/2) |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1170 + FF_INPUT_BUFFER_PADDING_SIZE); |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1171 } else { |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1172 q->decoded_bytes_buffer = |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1173 av_mallocz(avctx->block_align |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1174 + DECODE_BYTES_PAD1(avctx->block_align) |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1175 + FF_INPUT_BUFFER_PADDING_SIZE); |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1176 } |
8c830bde8006
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
banan
parents:
4423
diff
changeset
|
1177 if (q->decoded_bytes_buffer == NULL) |
2956 | 1178 return -1; |
1179 | |
4639 | 1180 q->gains1.now = q->gain_1; |
1181 q->gains1.previous = q->gain_2; | |
1182 q->gains2.now = q->gain_3; | |
1183 q->gains2.previous = q->gain_4; | |
2956 | 1184 |
1185 /* Initialize transform. */ | |
1186 if ( init_cook_mlt(q) == 0 ) | |
1187 return -1; | |
3014
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1188 |
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1189 /* Try to catch some obviously faulty streams, othervise it might be exploitable */ |
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1190 if (q->total_subbands > 53) { |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1191 av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n"); |
3014
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1192 return -1; |
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1193 } |
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1194 if (q->subbands > 50) { |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1195 av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n"); |
3014
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1196 return -1; |
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1197 } |
3091
0284d5b34916
Fix broken cosmetics commit and add a check for valid headers.
banan
parents:
3090
diff
changeset
|
1198 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) { |
0284d5b34916
Fix broken cosmetics commit and add a check for valid headers.
banan
parents:
3090
diff
changeset
|
1199 } else { |
4302
a0f83004d485
av_log(NULL,... -> av_log(avctx,.. where appropriate.
banan
parents:
3947
diff
changeset
|
1200 av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel); |
3091
0284d5b34916
Fix broken cosmetics commit and add a check for valid headers.
banan
parents:
3090
diff
changeset
|
1201 return -1; |
0284d5b34916
Fix broken cosmetics commit and add a check for valid headers.
banan
parents:
3090
diff
changeset
|
1202 } |
4431
85ac154efd99
Check that js_vlc_bits from the extradata is in a valid range.
banan
parents:
4430
diff
changeset
|
1203 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) { |
85ac154efd99
Check that js_vlc_bits from the extradata is in a valid range.
banan
parents:
4430
diff
changeset
|
1204 av_log(avctx,AV_LOG_ERROR,"q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits); |
85ac154efd99
Check that js_vlc_bits from the extradata is in a valid range.
banan
parents:
4430
diff
changeset
|
1205 return -1; |
85ac154efd99
Check that js_vlc_bits from the extradata is in a valid range.
banan
parents:
4430
diff
changeset
|
1206 } |
3014
959b8ad880dc
Dual mono stereo strems sound ok now, added sanity checks and removed
rtognimp
parents:
3009
diff
changeset
|
1207 |
3009
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
1208 #ifdef COOKDEBUG |
4430
407e7fd9b4f4
Get rid of the COOKextradata struct. And use valid C to parse the extradata.
banan
parents:
4429
diff
changeset
|
1209 dump_cook_context(q); |
3009
f5898b9b8a8a
Fix an out of array access and some minor cleanup of the code.
diego
parents:
2959
diff
changeset
|
1210 #endif |
2956 | 1211 return 0; |
1212 } | |
1213 | |
1214 | |
1215 AVCodec cook_decoder = | |
1216 { | |
1217 .name = "cook", | |
1218 .type = CODEC_TYPE_AUDIO, | |
1219 .id = CODEC_ID_COOK, | |
1220 .priv_data_size = sizeof(COOKContext), | |
1221 .init = cook_decode_init, | |
1222 .close = cook_decode_close, | |
1223 .decode = cook_decode_frame, | |
1224 }; |