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
24 * @file libavcodec/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
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
54 /* the different Cook versions */
55 #define MONO 0x1000001
56 #define STEREO 0x1000002
57 #define JOINT_STEREO 0x1000003
58 #define MC_COOK 0x2000000 //multichannel Cook, not supported
60 #define SUBBAND_SIZE 20
61 #define MAX_SUBPACKETS 5
64 #define DEBUGF(message,args ...) printf
70 * Random bit stream generator.
72 static int inline cook_random(COOKContext
*q
)
75 q
->random_state
* 214013 + 2531011; /* typical RNG numbers */
77 return (q
->random_state
/0x1000000)&1; /*>>31*/
79 #include "cook_fixpoint.h"
84 static void dump_int_table(int* table
, int size
, int delimiter
) {
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
) {
96 for (i
=0 ; i
<size
; i
++) {
97 DEBUGF("%d, ", table
[i
]);
98 if ((i
+1)%delimiter
== 0) DEBUGF("\n[%d]: ",i
+1);
104 /*************** init functions ***************/
105 #define VLCBUFSIZE 1500
106 VLC_TYPE vlcbuf
[21][VLCBUFSIZE
][2];
108 static int init_cook_vlc_tables(COOKContext
*q
) {
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
);
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
){
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))));
180 for (i
= 0; i
< bytes
/4; i
++)
181 obuf
[i
] = c
^ buf
[i
];
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
)
197 while (get_bits1(gb
)) {}
198 n
= get_bits_count(gb
) - 1; //amount of elements*2 to update
202 int index
= get_bits(gb
, 3);
203 int gain
= get_bits1(gb
) ? 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
) {
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
++){
224 if (i
>= q
->js_subband_start
* 2) {
225 vlc_index
-=q
->js_subband_start
;
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
;
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));
272 for (i
=32 ; i
>0 ; i
=i
/2){
275 for (j
=q
->total_subbands
; j
>0 ; j
--){
276 exp_idx
= av_clip((i
- quant_index_table
[index
] + bias
) / 2, 0, 7);
278 num_bits
+=expbits_tab
[exp_idx
];
280 if(num_bits
>= bits_left
- 32){
285 /* Calculate total number of bits. */
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
) { /* ---> */
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
;
309 tmp_categorize_array
[tmp_categorize_array1_idx
++] = index
;
310 tmpbias1
-= expbits_tab
[exp_index1
[index
]] -
311 expbits_tab
[exp_index1
[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
;
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];
333 for(i
=0 ; i
<q
->total_subbands
; i
++)
334 category
[i
] = exp_index2
[i
];
336 for(i
=0 ; i
<q
->numvector_size
-1 ; i
++)
337 category_index
[i
] = tmp_categorize_array
[tmp_categorize_array2_idx
++];
343 * Expand the category vector.
345 * @param q pointer to the COOKContext
346 * @param category pointer to the category array
347 * @param category_index pointer to the category_index array
350 static inline void expand_category(COOKContext
*q
, int* category
,
351 int* category_index
){
353 for(i
=0 ; i
<q
->num_vectors
; i
++){
354 ++category
[category_index
[i
]];
359 * Unpack the subband_coef_index and subband_coef_sign vectors.
361 * @param q pointer to the COOKContext
362 * @param category pointer to the category array
363 * @param subband_coef_index array of indexes to quant_centroid_tab
364 * @param subband_coef_sign signs of coefficients
367 static int unpack_SQVH(COOKContext
*q
, int category
, int* subband_coef_index
,
368 int* subband_coef_sign
) {
370 int vlc
, vd
,tmp
, result
;
372 vd
= vd_tab
[category
];
374 for(i
=0 ; i
<vpr_tab
[category
] ; i
++){
375 vlc
= get_vlc2(&q
->gb
, q
->sqvh
[category
].table
, q
->sqvh
[category
].bits
, 3);
376 if (q
->bits_per_subpacket
< get_bits_count(&q
->gb
)){
380 for(j
=vd
-1 ; j
>=0 ; j
--){
381 tmp
= (vlc
* invradix_tab
[category
])/0x100000;
382 subband_coef_index
[vd
*i
+j
] = vlc
- tmp
* (kmax_tab
[category
]+1);
385 for(j
=0 ; j
<vd
; j
++){
386 if (subband_coef_index
[i
*vd
+ j
]) {
387 if(get_bits_count(&q
->gb
) < q
->bits_per_subpacket
){
388 subband_coef_sign
[i
*vd
+j
] = get_bits1(&q
->gb
);
391 subband_coef_sign
[i
*vd
+j
]=0;
394 subband_coef_sign
[i
*vd
+j
]=0;
403 * Fill the mlt_buffer with mlt coefficients.
405 * @param q pointer to the COOKContext
406 * @param category pointer to the category array
407 * @param quant_index_table pointer to the array
408 * @param mlt_buffer pointer to mlt coefficients
412 static void decode_vectors(COOKContext
* q
, int* category
,
413 int *quant_index_table
, REAL_T
* mlt_buffer
){
414 /* A zero in this table means that the subband coefficient is
415 random noise coded. */
416 int subband_coef_index
[SUBBAND_SIZE
];
417 /* A zero in this table means that the subband coefficient is a
418 positive multiplicator. */
419 int subband_coef_sign
[SUBBAND_SIZE
];
423 for(band
=0 ; band
<q
->total_subbands
; band
++){
424 index
= category
[band
];
425 if(category
[band
] < 7){
426 if(unpack_SQVH(q
, category
[band
], subband_coef_index
, subband_coef_sign
)){
428 for(j
=0 ; j
<q
->total_subbands
; j
++) category
[band
+j
]=7;
432 memset(subband_coef_index
, 0, sizeof(subband_coef_index
));
433 memset(subband_coef_sign
, 0, sizeof(subband_coef_sign
));
435 q
->scalar_dequant(q
, index
, quant_index_table
[band
],
436 subband_coef_index
, subband_coef_sign
,
437 &mlt_buffer
[band
* SUBBAND_SIZE
]);
440 if(q
->total_subbands
*SUBBAND_SIZE
>= q
->samples_per_channel
){
442 } /* FIXME: should this be removed, or moved into loop above? */
447 * function for decoding mono data
449 * @param q pointer to the COOKContext
450 * @param mlt_buffer pointer to mlt coefficients
453 static void mono_decode(COOKContext
*q
, REAL_T
* mlt_buffer
) {
455 int category_index
[128];
456 int quant_index_table
[102];
459 memset(&category
, 0, 128*sizeof(int));
460 memset(&category_index
, 0, 128*sizeof(int));
462 decode_envelope(q
, quant_index_table
);
463 q
->num_vectors
= get_bits(&q
->gb
,q
->log2_numvector_size
);
464 categorize(q
, quant_index_table
, category
, category_index
);
465 expand_category(q
, category
, category_index
);
466 decode_vectors(q
, category
, quant_index_table
, mlt_buffer
);
470 * function for getting the jointstereo coupling information
472 * @param q pointer to the COOKContext
473 * @param decouple_tab decoupling array
477 static void decouple_info(COOKContext
*q
, int* decouple_tab
){
480 if(get_bits1(&q
->gb
)) {
481 if(cplband
[q
->js_subband_start
] > cplband
[q
->subbands
-1]) return;
483 length
= cplband
[q
->subbands
-1] - cplband
[q
->js_subband_start
] + 1;
484 for (i
=0 ; i
<length
; i
++) {
485 decouple_tab
[cplband
[q
->js_subband_start
] + i
] = get_vlc2(&q
->gb
, q
->ccpl
.table
, q
->ccpl
.bits
, 2);
490 if(cplband
[q
->js_subband_start
] > cplband
[q
->subbands
-1]) return;
492 length
= cplband
[q
->subbands
-1] - cplband
[q
->js_subband_start
] + 1;
493 for (i
=0 ; i
<length
; i
++) {
494 decouple_tab
[cplband
[q
->js_subband_start
] + i
] = get_bits(&q
->gb
, q
->js_vlc_bits
);
500 * function for decoding joint stereo data
502 * @param q pointer to the COOKContext
503 * @param mlt_buffer1 pointer to left channel mlt coefficients
504 * @param mlt_buffer2 pointer to right channel mlt coefficients
507 static void joint_decode(COOKContext
*q
, REAL_T
* mlt_buffer1
,
508 REAL_T
* mlt_buffer2
) {
510 int decouple_tab
[SUBBAND_SIZE
];
511 REAL_T
*decode_buffer
= q
->decode_buffer_0
;
514 memset(decouple_tab
, 0, sizeof(decouple_tab
));
515 memset(decode_buffer
, 0, sizeof(decode_buffer
));
517 /* Make sure the buffers are zeroed out. */
518 memset(mlt_buffer1
,0, 1024*sizeof(REAL_T
));
519 memset(mlt_buffer2
,0, 1024*sizeof(REAL_T
));
520 decouple_info(q
, decouple_tab
);
521 mono_decode(q
, decode_buffer
);
523 /* The two channels are stored interleaved in decode_buffer. */
524 for (i
=0 ; i
<q
->js_subband_start
; i
++) {
525 for (j
=0 ; j
<SUBBAND_SIZE
; j
++) {
526 mlt_buffer1
[i
*20+j
] = decode_buffer
[i
*40+j
];
527 mlt_buffer2
[i
*20+j
] = decode_buffer
[i
*40+20+j
];
531 /* When we reach js_subband_start (the higher frequencies)
532 the coefficients are stored in a coupling scheme. */
533 idx
= (1 << q
->js_vlc_bits
) - 1;
534 for (i
=q
->js_subband_start
; i
<q
->subbands
; i
++) {
535 int i1
= decouple_tab
[cplband
[i
]];
536 int i2
= idx
- i1
- 1;
537 for (j
=0 ; j
<SUBBAND_SIZE
; j
++) {
538 REAL_T x
= decode_buffer
[((q
->js_subband_start
+ i
)*20)+j
];
539 mlt_buffer1
[20*i
+j
] = cplscale_math(x
, q
->js_vlc_bits
, i1
);
540 mlt_buffer2
[20*i
+j
] = cplscale_math(x
, q
->js_vlc_bits
, i2
);
546 * First part of subpacket decoding:
547 * decode raw stream bytes and read gain info.
549 * @param q pointer to the COOKContext
550 * @param inbuffer pointer to raw stream data
551 * @param gain_ptr array of current/prev gain pointers
554 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
557 decode_bytes_and_gain(COOKContext
*q
, const uint8_t *inbuffer
,
558 cook_gains
*gains_ptr
)
562 offset
= decode_bytes(inbuffer
, q
->decoded_bytes_buffer
,
563 q
->bits_per_subpacket
/8);
564 init_get_bits(&q
->gb
, q
->decoded_bytes_buffer
+ offset
,
565 q
->bits_per_subpacket
);
566 decode_gain_info(&q
->gb
, gains_ptr
->now
);
568 /* Swap current and previous gains */
569 FFSWAP(int *, gains_ptr
->now
, gains_ptr
->previous
);
573 * Final part of subpacket decoding:
574 * Apply modulated lapped transform, gain compensation,
575 * clip and convert to integer.
577 * @param q pointer to the COOKContext
578 * @param decode_buffer pointer to the mlt coefficients
579 * @param gain_ptr array of current/prev gain pointers
580 * @param previous_buffer pointer to the previous buffer to be used for overlapping
581 * @param out pointer to the output buffer
582 * @param chan 0: left or single channel, 1: right channel
586 mlt_compensate_output(COOKContext
*q
, REAL_T
*decode_buffer
,
587 cook_gains
*gains
, REAL_T
*previous_buffer
,
588 int16_t *out
, int chan
)
590 REAL_T
*buffer
= q
->mono_mdct_output
;
592 imlt_math(q
, decode_buffer
);
594 /* Overlap with the previous block. */
595 overlap_math(q
, gains
->previous
[0], previous_buffer
);
597 /* Apply gain profile */
598 for (i
= 0; i
< 8; i
++) {
599 if (gains
->now
[i
] || gains
->now
[i
+ 1])
600 interpolate_math(q
, &buffer
[q
->samples_per_channel
/8 * i
],
601 gains
->now
[i
], gains
->now
[i
+ 1]);
604 /* Save away the current to be previous block. */
605 memcpy(previous_buffer
, buffer
+q
->samples_per_channel
,
606 sizeof(REAL_T
)*q
->samples_per_channel
);
608 output_math(q
, out
, chan
);
613 * Cook subpacket decoding. This function returns one decoded subpacket,
614 * usually 1024 samples per channel.
616 * @param q pointer to the COOKContext
617 * @param inbuffer pointer to the inbuffer
618 * @param sub_packet_size subpacket size
619 * @param outbuffer pointer to the outbuffer
623 static int decode_subpacket(COOKContext
*q
, const uint8_t *inbuffer
,
624 int sub_packet_size
, int16_t *outbuffer
) {
626 // for (i=0 ; i<sub_packet_size ; i++) {
627 // DEBUGF("%02x", inbuffer[i]);
631 decode_bytes_and_gain(q
, inbuffer
, &q
->gains1
);
633 if (q
->joint_stereo
) {
634 joint_decode(q
, q
->decode_buffer_1
, q
->decode_buffer_2
);
636 mono_decode(q
, q
->decode_buffer_1
);
638 if (q
->nb_channels
== 2) {
639 decode_bytes_and_gain(q
, inbuffer
+ sub_packet_size
/2, &q
->gains2
);
640 mono_decode(q
, q
->decode_buffer_2
);
644 mlt_compensate_output(q
, q
->decode_buffer_1
, &q
->gains1
,
645 q
->mono_previous_buffer1
, outbuffer
, 0);
647 if (q
->nb_channels
== 2) {
648 if (q
->joint_stereo
) {
649 mlt_compensate_output(q
, q
->decode_buffer_2
, &q
->gains1
,
650 q
->mono_previous_buffer2
, outbuffer
, 1);
652 mlt_compensate_output(q
, q
->decode_buffer_2
, &q
->gains2
,
653 q
->mono_previous_buffer2
, outbuffer
, 1);
656 return q
->samples_per_frame
* sizeof(int16_t);
661 * Cook frame decoding
663 * @param rmctx pointer to the RMContext
666 int cook_decode_frame(RMContext
*rmctx
,COOKContext
*q
,
667 int16_t *outbuffer
, int *data_size
,
668 const uint8_t *inbuffer
, int buf_size
) {
669 //COOKContext *q = avctx->priv_data;
672 if (buf_size
< rmctx
->block_align
)
675 *data_size
= decode_subpacket(q
, inbuffer
, rmctx
->block_align
, outbuffer
);
677 /* Discard the first two frames: no valid audio. */
678 if (rmctx
->frame_number
< 2) *data_size
= 0;
680 return rmctx
->block_align
;
684 static void dump_cook_context(COOKContext
*q
)
687 #define PRINT(a,b) DEBUGF(" %s = %d\n", a, b);
688 DEBUGF("COOKextradata\n");
689 DEBUGF("cookversion=%x\n",q
->cookversion
);
690 if (q
->cookversion
> STEREO
) {
691 PRINT("js_subband_start",q
->js_subband_start
);
692 PRINT("js_vlc_bits",q
->js_vlc_bits
);
694 PRINT("nb_channels",q
->nb_channels
);
695 PRINT("bit_rate",q
->bit_rate
);
696 PRINT("sample_rate",q
->sample_rate
);
697 PRINT("samples_per_channel",q
->samples_per_channel
);
698 PRINT("samples_per_frame",q
->samples_per_frame
);
699 PRINT("subbands",q
->subbands
);
700 PRINT("random_state",q
->random_state
);
701 PRINT("js_subband_start",q
->js_subband_start
);
702 PRINT("log2_numvector_size",q
->log2_numvector_size
);
703 PRINT("numvector_size",q
->numvector_size
);
704 PRINT("total_subbands",q
->total_subbands
);
709 * Cook initialization
712 int cook_decode_init(RMContext
*rmctx
, COOKContext
*q
)
715 q
->cookversion
= rmctx
->cook_version
;
716 q
->samples_per_frame
= rmctx
->samples_pf_pc
;
717 q
->subbands
= rmctx
->nb_subbands
;
718 q
->extradata_size
= rmctx
->extradata_size
;
719 if (q
->extradata_size
>= 16){
720 q
->js_subband_start
= rmctx
->js_subband_start
;
721 q
->js_vlc_bits
= rmctx
->js_vlc_bits
;
724 /* Take data from the RMContext (RM container). */
725 q
->sample_rate
= rmctx
->sample_rate
;
726 q
->nb_channels
= rmctx
->nb_channels
;
727 q
->bit_rate
= rmctx
->bit_rate
;
729 /* Initialize RNG. */
732 /* Initialize extradata related variables. */
733 q
->samples_per_channel
= q
->samples_per_frame
/ q
->nb_channels
;
734 q
->bits_per_subpacket
= rmctx
->block_align
* 8;
736 /* Initialize default data states. */
737 q
->log2_numvector_size
= 5;
738 q
->total_subbands
= q
->subbands
;
740 /* Initialize version-dependent variables */
741 DEBUGF("q->cookversion=%x\n",q
->cookversion
);
743 switch (q
->cookversion
) {
745 if (q
->nb_channels
!= 1) {
746 DEBUGF("Container channels != 1, report sample!\n");
752 if (q
->nb_channels
!= 1) {
753 q
->bits_per_subpacket
= q
->bits_per_subpacket
/2;
758 if (q
->nb_channels
!= 2) {
759 DEBUGF("Container channels != 2, report sample!\n");
762 DEBUGF("JOINT_STEREO\n");
763 if (q
->extradata_size
>= 16){
764 q
->total_subbands
= q
->subbands
+ q
->js_subband_start
;
767 if (q
->samples_per_channel
> 256) {
768 q
->log2_numvector_size
= 6;
770 if (q
->samples_per_channel
> 512) {
771 q
->log2_numvector_size
= 7;
775 DEBUGF("MC_COOK not supported!\n");
779 DEBUGF("Unknown Cook version, report sample!\n");
784 /* Initialize variable relations */
785 q
->numvector_size
= (1 << q
->log2_numvector_size
);
787 /* Generate tables */
788 if (init_cook_vlc_tables(q
) != 0)
792 if(q
->block_align
>= UINT_MAX
/2)
795 q
->gains1
.now
= q
->gain_1
;
796 q
->gains1
.previous
= q
->gain_2
;
797 q
->gains2
.now
= q
->gain_3
;
798 q
->gains2
.previous
= q
->gain_4
;
801 /* Initialize COOK signal arithmetic handling */
803 q
->scalar_dequant
= scalar_dequant_math
;
804 q
->interpolate
= interpolate_math
;
807 /* Try to catch some obviously faulty streams, othervise it might be exploitable */
808 if (q
->total_subbands
> 53) {
809 DEBUGF("total_subbands > 53, report sample!\n");
812 if (q
->subbands
> 50) {
813 DEBUGF("subbands > 50, report sample!\n");
816 if ((q
->samples_per_channel
== 256) || (q
->samples_per_channel
== 512) || (q
->samples_per_channel
== 1024)) {
818 DEBUGF("unknown amount of samples_per_channel = %d, report sample!\n",q
->samples_per_channel
);
821 if ((q
->js_vlc_bits
> 6) || (q
->js_vlc_bits
< 0)) {
822 DEBUGF("q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q
->js_vlc_bits
);
828 dump_cook_context(q
);