Change liba52 and libmad to the original 8-spaced tabbing.
[kugel-rb.git] / apps / codecs / libcook / cook.c
blob814250ea32fdfcbb3d74b43e369c5dab3193ca9c
1 /*
2 * COOK compatible decoder
3 * Copyright (c) 2003 Sascha Sommer
4 * Copyright (c) 2005 Benjamin Larsson
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
23 /**
24 * @file cook.c
25 * Cook compatible decoder. Bastardization of the G.722.1 standard.
26 * This decoder handles RealNetworks, RealAudio G2 data.
27 * Cook is identified by the codec name cook in RM files.
29 * To use this decoder, a calling application must supply the extradata
30 * bytes provided from the RM container; 8+ bytes for mono streams and
31 * 16+ for stereo streams (maybe more).
33 * Codec technicalities (all this assume a buffer length of 1024):
34 * Cook works with several different techniques to achieve its compression.
35 * In the timedomain the buffer is divided into 8 pieces and quantized. If
36 * two neighboring pieces have different quantization index a smooth
37 * quantization curve is used to get a smooth overlap between the different
38 * pieces.
39 * To get to the transformdomain Cook uses a modulated lapped transform.
40 * The transform domain has 50 subbands with 20 elements each. This
41 * means only a maximum of 50*20=1000 coefficients are used out of the 1024
42 * available.
45 #include <math.h>
46 #include <stddef.h>
47 #include <stdio.h>
48 #include <limits.h>
49 #include <string.h>
50 #include "codeclib.h"
52 #include "cook.h"
53 #include "cookdata.h"
55 /* the different Cook versions */
56 #define MONO 0x1000001
57 #define STEREO 0x1000002
58 #define JOINT_STEREO 0x1000003
59 #define MC_COOK 0x2000000 //multichannel Cook, not supported
61 #define SUBBAND_SIZE 20
62 #define MAX_SUBPACKETS 5
63 //#define COOKDEBUG
64 #ifndef COOKDEBUG
65 #undef DEBUGF
66 #define DEBUGF(...)
67 #endif
69 /**
70 * Random bit stream generator.
72 static inline int cook_random(COOKContext *q)
74 q->random_state =
75 q->random_state * 214013 + 2531011; /* typical RNG numbers */
77 return (q->random_state/0x1000000)&1; /*>>31*/
79 #include "cook_fixpoint.h"
81 /* debug functions */
83 #ifdef COOKDEBUG
84 static void dump_int_table(int* table, int size, int delimiter) {
85 int i=0;
86 DEBUGF("\n[%d]: ",i);
87 for (i=0 ; i<size ; i++) {
88 DEBUGF("%d, ", table[i]);
89 if ((i+1)%delimiter == 0) DEBUGF("\n[%d]: ",i+1);
93 static void dump_short_table(short* table, int size, int delimiter) {
94 int i=0;
95 DEBUGF("\n[%d]: ",i);
96 for (i=0 ; i<size ; i++) {
97 DEBUGF("%d, ", table[i]);
98 if ((i+1)%delimiter == 0) DEBUGF("\n[%d]: ",i+1);
102 #endif
104 /*************** init functions ***************/
105 #define VLCBUFSIZE 1500
106 VLC_TYPE vlcbuf[21][VLCBUFSIZE][2];
108 static int init_cook_vlc_tables(COOKContext *q) {
109 int i, result;
111 result = 0;
112 for (i=0 ; i<13 ; i++) {
113 q->envelope_quant_index[i].table = vlcbuf[i];
114 q->envelope_quant_index[i].table_allocated = VLCBUFSIZE;
115 result |= init_vlc (&q->envelope_quant_index[i], 9, 24,
116 envelope_quant_index_huffbits[i], 1, 1,
117 envelope_quant_index_huffcodes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
119 DEBUGF("sqvh VLC init\n");
120 for (i=0 ; i<7 ; i++) {
121 q->sqvh[i].table = vlcbuf[i+13];
122 q->sqvh[i].table_allocated = VLCBUFSIZE;
123 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
124 cvh_huffbits[i], 1, 1,
125 cvh_huffcodes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
128 if (q->nb_channels==2 && q->joint_stereo==1){
129 q->ccpl.table = vlcbuf[20];
130 q->ccpl.table_allocated = VLCBUFSIZE;
131 result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1,
132 ccpl_huffbits[q->js_vlc_bits-2], 1, 1,
133 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, INIT_VLC_USE_NEW_STATIC);
134 DEBUGF("Joint-stereo VLC used.\n");
137 DEBUGF("VLC tables initialized. Result = %d\n",result);
138 return result;
140 /*************** init functions end ***********/
143 * Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
144 * Why? No idea, some checksum/error detection method maybe.
146 * Out buffer size: extra bytes are needed to cope with
147 * padding/misalignment.
148 * Subpackets passed to the decoder can contain two, consecutive
149 * half-subpackets, of identical but arbitrary size.
150 * 1234 1234 1234 1234 extraA extraB
151 * Case 1: AAAA BBBB 0 0
152 * Case 2: AAAA ABBB BB-- 3 3
153 * Case 3: AAAA AABB BBBB 2 2
154 * Case 4: AAAA AAAB BBBB BB-- 1 5
156 * Nice way to waste CPU cycles.
158 * @param inbuffer pointer to byte array of indata
159 * @param out pointer to byte array of outdata
160 * @param bytes number of bytes
162 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)
163 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
165 static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
166 int i, off;
167 uint32_t c;
168 const uint32_t* buf;
169 uint32_t* obuf = (uint32_t*) out;
170 /* FIXME: 64 bit platforms would be able to do 64 bits at a time.
171 * I'm too lazy though, should be something like
172 * for(i=0 ; i<bitamount/64 ; i++)
173 * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]);
174 * Buffer alignment needs to be checked. */
176 off = (intptr_t)inbuffer & 3;
177 buf = (const uint32_t*) (inbuffer - off);
178 c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));
179 bytes += 3 + off;
180 for (i = 0; i < bytes/4; i++)
181 obuf[i] = c ^ buf[i];
183 return off;
187 * Fill the gain array for the timedomain quantization.
189 * @param q pointer to the COOKContext
190 * @param gaininfo[9] array of gain indexes
193 static void decode_gain_info(GetBitContext *gb, int *gaininfo)
195 int i, n;
197 while (get_bits1(gb)) {}
198 n = get_bits_count(gb) - 1; //amount of elements*2 to update
200 i = 0;
201 while (n--) {
202 int index = get_bits(gb, 3);
203 int gain = get_bits1(gb) ? (int)get_bits(gb, 4) - 7 : -1;
205 while (i <= index) gaininfo[i++] = gain;
207 while (i <= 8) gaininfo[i++] = 0;
211 * Create the quant index table needed for the envelope.
213 * @param q pointer to the COOKContext
214 * @param quant_index_table pointer to the array
217 static void decode_envelope(COOKContext *q, int* quant_index_table) {
218 int i,j, vlc_index;
220 quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize
222 for (i=1 ; i < q->total_subbands ; i++){
223 vlc_index=i;
224 if (i >= q->js_subband_start * 2) {
225 vlc_index-=q->js_subband_start;
226 } else {
227 vlc_index/=2;
228 if(vlc_index < 1) vlc_index = 1;
230 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13
232 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,
233 q->envelope_quant_index[vlc_index-1].bits,2);
234 quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding
239 * Calculate the category and category_index vector.
241 * @param q pointer to the COOKContext
242 * @param quant_index_table pointer to the array
243 * @param category pointer to the category array
244 * @param category_index pointer to the category_index array
247 static void categorize(COOKContext *q, int* quant_index_table,
248 int* category, int* category_index){
249 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
250 int exp_index2[102];
251 int exp_index1[102];
253 int tmp_categorize_array[128*2];
254 int tmp_categorize_array1_idx=q->numvector_size;
255 int tmp_categorize_array2_idx=q->numvector_size;
257 bits_left = q->bits_per_subpacket - get_bits_count(&q->gb);
259 if(bits_left > q->samples_per_channel) {
260 bits_left = q->samples_per_channel +
261 ((bits_left - q->samples_per_channel)*5)/8;
262 //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left);
265 memset(&exp_index1,0,102*sizeof(int));
266 memset(&exp_index2,0,102*sizeof(int));
267 memset(&tmp_categorize_array,0,128*2*sizeof(int));
269 bias=-32;
271 /* Estimate bias. */
272 for (i=32 ; i>0 ; i=i/2){
273 num_bits = 0;
274 index = 0;
275 for (j=q->total_subbands ; j>0 ; j--){
276 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
277 index++;
278 num_bits+=expbits_tab[exp_idx];
280 if(num_bits >= bits_left - 32){
281 bias+=i;
285 /* Calculate total number of bits. */
286 num_bits=0;
287 for (i=0 ; i<q->total_subbands ; i++) {
288 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
289 num_bits += expbits_tab[exp_idx];
290 exp_index1[i] = exp_idx;
291 exp_index2[i] = exp_idx;
293 tmpbias1 = tmpbias2 = num_bits;
295 for (j = 1 ; j < q->numvector_size ; j++) {
296 if (tmpbias1 + tmpbias2 > 2*bits_left) { /* ---> */
297 int max = -999999;
298 index=-1;
299 for (i=0 ; i<q->total_subbands ; i++){
300 if (exp_index1[i] < 7) {
301 v = (-2*exp_index1[i]) - quant_index_table[i] + bias;
302 if ( v >= max) {
303 max = v;
304 index = i;
308 if(index==-1)break;
309 tmp_categorize_array[tmp_categorize_array1_idx++] = index;
310 tmpbias1 -= expbits_tab[exp_index1[index]] -
311 expbits_tab[exp_index1[index]+1];
312 ++exp_index1[index];
313 } else { /* <--- */
314 int min = 999999;
315 index=-1;
316 for (i=0 ; i<q->total_subbands ; i++){
317 if(exp_index2[i] > 0){
318 v = (-2*exp_index2[i])-quant_index_table[i]+bias;
319 if ( v < min) {
320 min = v;
321 index = i;
325 if(index == -1)break;
326 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
327 tmpbias2 -= expbits_tab[exp_index2[index]] -
328 expbits_tab[exp_index2[index]-1];
329 --exp_index2[index];
332 memcpy(category, exp_index2, sizeof(int) * q->total_subbands );
333 memcpy(category_index, tmp_categorize_array+tmp_categorize_array2_idx, sizeof(int) * (q->numvector_size-1) );
338 * Expand the category vector.
340 * @param q pointer to the COOKContext
341 * @param category pointer to the category array
342 * @param category_index pointer to the category_index array
345 static inline void expand_category(COOKContext *q, int* category,
346 int* category_index){
347 int i;
348 for(i=0 ; i<q->num_vectors ; i++){
349 ++category[category_index[i]];
354 * Unpack the subband_coef_index and subband_coef_sign vectors.
356 * @param q pointer to the COOKContext
357 * @param category pointer to the category array
358 * @param subband_coef_index array of indexes to quant_centroid_tab
359 * @param subband_coef_sign signs of coefficients
362 static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index,
363 int* subband_coef_sign) {
364 int i,j;
365 int vlc, vd ,tmp, result;
367 vd = vd_tab[category];
368 result = 0;
369 for(i=0 ; i<vpr_tab[category] ; i++)
371 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
372 if (q->bits_per_subpacket < get_bits_count(&q->gb))
374 vlc = 0;
375 result = 1;
376 memset(subband_coef_index, 0, sizeof(int)*vd);
377 memset(subband_coef_sign, 0, sizeof(int)*vd);
378 subband_coef_index+=vd;
379 subband_coef_sign+=vd;
381 else
383 for(j=vd-1 ; j>=0 ; j--){
384 tmp = (vlc * invradix_tab[category])/0x100000;
385 subband_coef_index[j] = vlc - tmp * (kmax_tab[category]+1);
386 vlc = tmp;
389 for(j=0 ; j<vd ; j++)
391 if (*subband_coef_index++) {
392 if(get_bits_count(&q->gb) < q->bits_per_subpacket) {
393 *subband_coef_sign++ = get_bits1(&q->gb);
394 } else {
395 result=1;
396 *subband_coef_sign++=0;
398 } else {
399 *subband_coef_sign++=0;
404 return result;
409 * Fill the mlt_buffer with mlt coefficients.
411 * @param q pointer to the COOKContext
412 * @param category pointer to the category array
413 * @param quant_index_table pointer to the array
414 * @param mlt_buffer pointer to mlt coefficients
418 static void decode_vectors(COOKContext* q, int* category,
419 int *quant_index_table, REAL_T* mlt_buffer){
420 /* A zero in this table means that the subband coefficient is
421 random noise coded. */
422 int subband_coef_index[SUBBAND_SIZE];
423 /* A zero in this table means that the subband coefficient is a
424 positive multiplicator. */
425 int subband_coef_sign[SUBBAND_SIZE];
426 int band, j;
427 int index=0;
429 for(band=0 ; band<q->total_subbands ; band++){
430 index = category[band];
431 if(category[band] < 7){
432 if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){
433 index=7;
434 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
437 if(index>=7) {
438 memset(subband_coef_index, 0, sizeof(subband_coef_index));
439 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
441 q->scalar_dequant(q, index, quant_index_table[band],
442 subband_coef_index, subband_coef_sign,
443 &mlt_buffer[band * SUBBAND_SIZE]);
446 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
447 return;
448 } /* FIXME: should this be removed, or moved into loop above? */
453 * function for decoding mono data
455 * @param q pointer to the COOKContext
456 * @param mlt_buffer pointer to mlt coefficients
459 static void mono_decode(COOKContext *q, REAL_T* mlt_buffer) {
461 int category_index[128];
462 int quant_index_table[102];
463 int category[128];
465 memset(&category, 0, 128*sizeof(int));
466 memset(&category_index, 0, 128*sizeof(int));
468 decode_envelope(q, quant_index_table);
469 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
470 categorize(q, quant_index_table, category, category_index);
471 expand_category(q, category, category_index);
472 decode_vectors(q, category, quant_index_table, mlt_buffer);
476 * function for getting the jointstereo coupling information
478 * @param q pointer to the COOKContext
479 * @param decouple_tab decoupling array
483 static void decouple_info(COOKContext *q, int* decouple_tab){
484 int length, i;
486 if(get_bits1(&q->gb)) {
487 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
489 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
490 for (i=0 ; i<length ; i++) {
491 decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2);
493 return;
496 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
498 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
499 for (i=0 ; i<length ; i++) {
500 decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits);
502 return;
506 * function for decoding joint stereo data
508 * @param q pointer to the COOKContext
509 * @param mlt_buffer1 pointer to left channel mlt coefficients
510 * @param mlt_buffer2 pointer to right channel mlt coefficients
513 static void joint_decode(COOKContext *q, REAL_T* mlt_buffer1,
514 REAL_T* mlt_buffer2) {
515 int i;
516 int decouple_tab[SUBBAND_SIZE];
517 REAL_T *decode_buffer = q->decode_buffer_0;
518 int idx;
520 memset(decouple_tab, 0, sizeof(decouple_tab));
521 memset(decode_buffer, 0, sizeof(decode_buffer));
523 /* Make sure the buffers are zeroed out. */
524 memset(mlt_buffer1,0, 1024*sizeof(REAL_T));
525 memset(mlt_buffer2,0, 1024*sizeof(REAL_T));
526 decouple_info(q, decouple_tab);
527 mono_decode(q, decode_buffer);
529 /* The two channels are stored interleaved in decode_buffer. */
530 REAL_T * mlt_buffer1_end = mlt_buffer1 + (q->js_subband_start*SUBBAND_SIZE);
531 while(mlt_buffer1 < mlt_buffer1_end)
533 memcpy(mlt_buffer1,decode_buffer,sizeof(REAL_T)*SUBBAND_SIZE);
534 memcpy(mlt_buffer2,decode_buffer+20,sizeof(REAL_T)*SUBBAND_SIZE);
535 mlt_buffer1 += 20;
536 mlt_buffer2 += 20;
537 decode_buffer += 40;
540 /* When we reach js_subband_start (the higher frequencies)
541 the coefficients are stored in a coupling scheme. */
542 idx = (1 << q->js_vlc_bits) - 1;
543 for (i=q->js_subband_start ; i<q->subbands ; i++) {
544 int i1 = decouple_tab[cplband[i]];
545 int i2 = idx - i1 - 1;
546 mlt_buffer1_end = mlt_buffer1 + SUBBAND_SIZE;
547 while(mlt_buffer1 < mlt_buffer1_end)
549 *mlt_buffer1++ = cplscale_math(*decode_buffer, q->js_vlc_bits, i1);
550 *mlt_buffer2++ = cplscale_math(*decode_buffer++, q->js_vlc_bits, i2);
552 mlt_buffer1 += (20-SUBBAND_SIZE);
553 mlt_buffer2 += (20-SUBBAND_SIZE);
554 decode_buffer += (20-SUBBAND_SIZE);
559 * First part of subpacket decoding:
560 * decode raw stream bytes and read gain info.
562 * @param q pointer to the COOKContext
563 * @param inbuffer pointer to raw stream data
564 * @param gain_ptr array of current/prev gain pointers
567 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
569 static inline void
570 decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer,
571 cook_gains *gains_ptr)
573 int offset;
575 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
576 q->bits_per_subpacket/8);
577 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
578 q->bits_per_subpacket);
579 decode_gain_info(&q->gb, gains_ptr->now);
581 /* Swap current and previous gains */
582 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
586 * Final part of subpacket decoding:
587 * Apply modulated lapped transform, gain compensation,
588 * clip and convert to integer.
590 * @param q pointer to the COOKContext
591 * @param decode_buffer pointer to the mlt coefficients
592 * @param gain_ptr array of current/prev gain pointers
593 * @param previous_buffer pointer to the previous buffer to be used for overlapping
594 * @param out pointer to the output buffer
595 * @param chan 0: left or single channel, 1: right channel
598 static void
599 mlt_compensate_output(COOKContext *q, REAL_T *decode_buffer,
600 cook_gains *gains, REAL_T *previous_buffer,
601 int32_t *out, int chan)
603 REAL_T *buffer = q->mono_mdct_output;
604 int i;
605 imlt_math(q, decode_buffer);
607 /* Overlap with the previous block. */
608 overlap_math(q, gains->previous[0], previous_buffer);
610 /* Apply gain profile */
611 for (i = 0; i < 8; i++) {
612 if (gains->now[i] || gains->now[i + 1])
613 interpolate_math(q, &buffer[q->samples_per_channel/8 * i],
614 gains->now[i], gains->now[i + 1]);
617 /* Save away the current to be previous block. */
618 memcpy(previous_buffer, buffer+q->samples_per_channel,
619 sizeof(REAL_T)*q->samples_per_channel);
621 /* Copy output to non-interleaved sample buffer */
622 memcpy(out + (chan * q->samples_per_channel), buffer,
623 sizeof(REAL_T)*q->samples_per_channel);
628 * Cook subpacket decoding. This function returns one decoded subpacket,
629 * usually 1024 samples per channel.
631 * @param q pointer to the COOKContext
632 * @param inbuffer pointer to the inbuffer
633 * @param sub_packet_size subpacket size
634 * @param outbuffer pointer to the outbuffer
638 static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
639 int sub_packet_size, int32_t *outbuffer) {
640 /* packet dump */
641 // for (i=0 ; i<sub_packet_size ; i++) {
642 // DEBUGF("%02x", inbuffer[i]);
643 // }
644 // DEBUGF("\n");
646 decode_bytes_and_gain(q, inbuffer, &q->gains1);
648 if (q->joint_stereo) {
649 joint_decode(q, q->decode_buffer_1, q->decode_buffer_2);
650 } else {
651 mono_decode(q, q->decode_buffer_1);
653 if (q->nb_channels == 2) {
654 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2);
655 mono_decode(q, q->decode_buffer_2);
659 mlt_compensate_output(q, q->decode_buffer_1, &q->gains1,
660 q->mono_previous_buffer1, outbuffer, 0);
662 if (q->nb_channels == 2) {
663 if (q->joint_stereo) {
664 mlt_compensate_output(q, q->decode_buffer_2, &q->gains1,
665 q->mono_previous_buffer2, outbuffer, 1);
666 } else {
667 mlt_compensate_output(q, q->decode_buffer_2, &q->gains2,
668 q->mono_previous_buffer2, outbuffer, 1);
671 return q->samples_per_frame * sizeof(int32_t);
676 * Cook frame decoding
678 * @param rmctx pointer to the RMContext
681 int cook_decode_frame(RMContext *rmctx,COOKContext *q,
682 int32_t *outbuffer, int *data_size,
683 const uint8_t *inbuffer, int buf_size) {
684 //COOKContext *q = avctx->priv_data;
685 //COOKContext *q;
687 if (buf_size < rmctx->block_align)
688 return buf_size;
690 *data_size = decode_subpacket(q, inbuffer, rmctx->block_align, outbuffer);
692 /* Discard the first two frames: no valid audio. */
693 if (rmctx->frame_number < 2) *data_size = 0;
695 return rmctx->block_align;
698 #ifdef COOKDEBUG
699 static void dump_cook_context(COOKContext *q)
701 //int i=0;
702 #define PRINT(a,b) DEBUGF(" %s = %d\n", a, b);
703 DEBUGF("COOKextradata\n");
704 DEBUGF("cookversion=%x\n",q->cookversion);
705 if (q->cookversion > STEREO) {
706 PRINT("js_subband_start",q->js_subband_start);
707 PRINT("js_vlc_bits",q->js_vlc_bits);
709 PRINT("nb_channels",q->nb_channels);
710 PRINT("bit_rate",q->bit_rate);
711 PRINT("sample_rate",q->sample_rate);
712 PRINT("samples_per_channel",q->samples_per_channel);
713 PRINT("samples_per_frame",q->samples_per_frame);
714 PRINT("subbands",q->subbands);
715 PRINT("random_state",q->random_state);
716 PRINT("js_subband_start",q->js_subband_start);
717 PRINT("log2_numvector_size",q->log2_numvector_size);
718 PRINT("numvector_size",q->numvector_size);
719 PRINT("total_subbands",q->total_subbands);
721 #endif
724 * Cook initialization
727 int cook_decode_init(RMContext *rmctx, COOKContext *q)
730 /* cook extradata */
731 q->cookversion = rm_get_uint32be(rmctx->codec_extradata);
732 q->samples_per_frame = rm_get_uint16be(&rmctx->codec_extradata[4]);
733 q->subbands = rm_get_uint16be(&rmctx->codec_extradata[6]);
734 q->extradata_size = rmctx->extradata_size;
735 if (q->extradata_size >= 16){
736 q->js_subband_start = rm_get_uint16be(&rmctx->codec_extradata[12]);
737 q->js_vlc_bits = rm_get_uint16be(&rmctx->codec_extradata[14]);
740 /* Take data from the RMContext (RM container). */
741 q->sample_rate = rmctx->sample_rate;
742 q->nb_channels = rmctx->nb_channels;
743 q->bit_rate = rmctx->bit_rate;
745 /* Initialize RNG. */
746 q->random_state = 0;
748 /* Initialize extradata related variables. */
749 q->samples_per_channel = q->samples_per_frame >> (q->nb_channels-1);
750 q->bits_per_subpacket = rmctx->block_align * 8;
752 /* Initialize default data states. */
753 q->log2_numvector_size = 5;
754 q->total_subbands = q->subbands;
756 /* Initialize version-dependent variables */
757 DEBUGF("q->cookversion=%x\n",q->cookversion);
758 q->joint_stereo = 0;
759 switch (q->cookversion) {
760 case MONO:
761 if (q->nb_channels != 1) {
762 DEBUGF("Container channels != 1, report sample!\n");
763 return -1;
765 DEBUGF("MONO\n");
766 break;
767 case STEREO:
768 if (q->nb_channels != 1) {
769 q->bits_per_subpacket = q->bits_per_subpacket/2;
771 DEBUGF("STEREO\n");
772 break;
773 case JOINT_STEREO:
774 if (q->nb_channels != 2) {
775 DEBUGF("Container channels != 2, report sample!\n");
776 return -1;
778 DEBUGF("JOINT_STEREO\n");
779 if (q->extradata_size >= 16){
780 q->total_subbands = q->subbands + q->js_subband_start;
781 q->joint_stereo = 1;
783 if (q->samples_per_channel > 256) {
784 q->log2_numvector_size = 6;
786 if (q->samples_per_channel > 512) {
787 q->log2_numvector_size = 7;
789 break;
790 case MC_COOK:
791 DEBUGF("MC_COOK not supported!\n");
792 return -1;
793 break;
794 default:
795 DEBUGF("Unknown Cook version, report sample!\n");
796 return -1;
797 break;
800 /* Initialize variable relations */
801 q->numvector_size = (1 << q->log2_numvector_size);
802 q->mdct_nbits = av_log2(q->samples_per_channel)+1;
804 /* Generate tables */
805 if (init_cook_vlc_tables(q) != 0)
806 return -1;
809 if(rmctx->block_align >= UINT16_MAX/2)
810 return -1;
812 q->gains1.now = q->gain_1;
813 q->gains1.previous = q->gain_2;
814 q->gains2.now = q->gain_3;
815 q->gains2.previous = q->gain_4;
818 /* Initialize COOK signal arithmetic handling */
819 if (1) {
820 q->scalar_dequant = scalar_dequant_math;
821 q->interpolate = interpolate_math;
824 /* Try to catch some obviously faulty streams, othervise it might be exploitable */
825 if (q->total_subbands > 53) {
826 DEBUGF("total_subbands > 53, report sample!\n");
827 return -1;
829 if (q->subbands > 50) {
830 DEBUGF("subbands > 50, report sample!\n");
831 return -1;
833 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
834 } else {
835 DEBUGF("unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
836 return -1;
838 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) {
839 DEBUGF("q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits);
840 return -1;
844 #ifdef COOKDEBUG
845 dump_cook_context(q);
846 #endif
847 return 0;