Generic codec-extradata parsing, in preparation for addition of AAC/RM.
[kugel-rb.git] / apps / codecs / libcook / cook.c
blob315fe0aaffb6a94070a904ef02376b2227056708
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>
51 #include "cook.h"
52 #include "cookdata.h"
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
62 //#define COOKDEBUG
63 #ifndef COOKDEBUG
64 #undef DEBUGF
65 #define DEBUGF(...)
66 #endif
68 /**
69 * Random bit stream generator.
71 static inline int cook_random(COOKContext *q)
73 q->random_state =
74 q->random_state * 214013 + 2531011; /* typical RNG numbers */
76 return (q->random_state/0x1000000)&1; /*>>31*/
78 #include "cook_fixpoint.h"
80 /* debug functions */
82 #ifdef COOKDEBUG
83 static void dump_int_table(int* table, int size, int delimiter) {
84 int i=0;
85 DEBUGF("\n[%d]: ",i);
86 for (i=0 ; i<size ; i++) {
87 DEBUGF("%d, ", table[i]);
88 if ((i+1)%delimiter == 0) DEBUGF("\n[%d]: ",i+1);
92 static void dump_short_table(short* table, int size, int delimiter) {
93 int i=0;
94 DEBUGF("\n[%d]: ",i);
95 for (i=0 ; i<size ; i++) {
96 DEBUGF("%d, ", table[i]);
97 if ((i+1)%delimiter == 0) DEBUGF("\n[%d]: ",i+1);
101 #endif
103 /*************** init functions ***************/
104 #define VLCBUFSIZE 1500
105 VLC_TYPE vlcbuf[21][VLCBUFSIZE][2];
107 static int init_cook_vlc_tables(COOKContext *q) {
108 int i, result;
110 result = 0;
111 for (i=0 ; i<13 ; i++) {
112 q->envelope_quant_index[i].table = vlcbuf[i];
113 q->envelope_quant_index[i].table_allocated = VLCBUFSIZE;
114 result |= init_vlc (&q->envelope_quant_index[i], 9, 24,
115 envelope_quant_index_huffbits[i], 1, 1,
116 envelope_quant_index_huffcodes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
118 DEBUGF("sqvh VLC init\n");
119 for (i=0 ; i<7 ; i++) {
120 q->sqvh[i].table = vlcbuf[i+13];
121 q->sqvh[i].table_allocated = VLCBUFSIZE;
122 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
123 cvh_huffbits[i], 1, 1,
124 cvh_huffcodes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
127 if (q->nb_channels==2 && q->joint_stereo==1){
128 q->ccpl.table = vlcbuf[20];
129 q->ccpl.table_allocated = VLCBUFSIZE;
130 result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1,
131 ccpl_huffbits[q->js_vlc_bits-2], 1, 1,
132 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, INIT_VLC_USE_NEW_STATIC);
133 DEBUGF("Joint-stereo VLC used.\n");
136 DEBUGF("VLC tables initialized. Result = %d\n",result);
137 return result;
139 /*************** init functions end ***********/
142 * Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
143 * Why? No idea, some checksum/error detection method maybe.
145 * Out buffer size: extra bytes are needed to cope with
146 * padding/misalignment.
147 * Subpackets passed to the decoder can contain two, consecutive
148 * half-subpackets, of identical but arbitrary size.
149 * 1234 1234 1234 1234 extraA extraB
150 * Case 1: AAAA BBBB 0 0
151 * Case 2: AAAA ABBB BB-- 3 3
152 * Case 3: AAAA AABB BBBB 2 2
153 * Case 4: AAAA AAAB BBBB BB-- 1 5
155 * Nice way to waste CPU cycles.
157 * @param inbuffer pointer to byte array of indata
158 * @param out pointer to byte array of outdata
159 * @param bytes number of bytes
161 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)
162 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
164 static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
165 int i, off;
166 uint32_t c;
167 const uint32_t* buf;
168 uint32_t* obuf = (uint32_t*) out;
169 /* FIXME: 64 bit platforms would be able to do 64 bits at a time.
170 * I'm too lazy though, should be something like
171 * for(i=0 ; i<bitamount/64 ; i++)
172 * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]);
173 * Buffer alignment needs to be checked. */
175 off = (intptr_t)inbuffer & 3;
176 buf = (const uint32_t*) (inbuffer - off);
177 c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));
178 bytes += 3 + off;
179 for (i = 0; i < bytes/4; i++)
180 obuf[i] = c ^ buf[i];
182 return off;
186 * Fill the gain array for the timedomain quantization.
188 * @param q pointer to the COOKContext
189 * @param gaininfo[9] array of gain indexes
192 static void decode_gain_info(GetBitContext *gb, int *gaininfo)
194 int i, n;
196 while (get_bits1(gb)) {}
197 n = get_bits_count(gb) - 1; //amount of elements*2 to update
199 i = 0;
200 while (n--) {
201 int index = get_bits(gb, 3);
202 int gain = get_bits1(gb) ? (int)get_bits(gb, 4) - 7 : -1;
204 while (i <= index) gaininfo[i++] = gain;
206 while (i <= 8) gaininfo[i++] = 0;
210 * Create the quant index table needed for the envelope.
212 * @param q pointer to the COOKContext
213 * @param quant_index_table pointer to the array
216 static void decode_envelope(COOKContext *q, int* quant_index_table) {
217 int i,j, vlc_index;
219 quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize
221 for (i=1 ; i < q->total_subbands ; i++){
222 vlc_index=i;
223 if (i >= q->js_subband_start * 2) {
224 vlc_index-=q->js_subband_start;
225 } else {
226 vlc_index/=2;
227 if(vlc_index < 1) vlc_index = 1;
229 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13
231 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,
232 q->envelope_quant_index[vlc_index-1].bits,2);
233 quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding
238 * Calculate the category and category_index vector.
240 * @param q pointer to the COOKContext
241 * @param quant_index_table pointer to the array
242 * @param category pointer to the category array
243 * @param category_index pointer to the category_index array
246 static void categorize(COOKContext *q, int* quant_index_table,
247 int* category, int* category_index){
248 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
249 int exp_index2[102];
250 int exp_index1[102];
252 int tmp_categorize_array[128*2];
253 int tmp_categorize_array1_idx=q->numvector_size;
254 int tmp_categorize_array2_idx=q->numvector_size;
256 bits_left = q->bits_per_subpacket - get_bits_count(&q->gb);
258 if(bits_left > q->samples_per_channel) {
259 bits_left = q->samples_per_channel +
260 ((bits_left - q->samples_per_channel)*5)/8;
261 //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left);
264 memset(&exp_index1,0,102*sizeof(int));
265 memset(&exp_index2,0,102*sizeof(int));
266 memset(&tmp_categorize_array,0,128*2*sizeof(int));
268 bias=-32;
270 /* Estimate bias. */
271 for (i=32 ; i>0 ; i=i/2){
272 num_bits = 0;
273 index = 0;
274 for (j=q->total_subbands ; j>0 ; j--){
275 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
276 index++;
277 num_bits+=expbits_tab[exp_idx];
279 if(num_bits >= bits_left - 32){
280 bias+=i;
284 /* Calculate total number of bits. */
285 num_bits=0;
286 for (i=0 ; i<q->total_subbands ; i++) {
287 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
288 num_bits += expbits_tab[exp_idx];
289 exp_index1[i] = exp_idx;
290 exp_index2[i] = exp_idx;
292 tmpbias1 = tmpbias2 = num_bits;
294 for (j = 1 ; j < q->numvector_size ; j++) {
295 if (tmpbias1 + tmpbias2 > 2*bits_left) { /* ---> */
296 int max = -999999;
297 index=-1;
298 for (i=0 ; i<q->total_subbands ; i++){
299 if (exp_index1[i] < 7) {
300 v = (-2*exp_index1[i]) - quant_index_table[i] + bias;
301 if ( v >= max) {
302 max = v;
303 index = i;
307 if(index==-1)break;
308 tmp_categorize_array[tmp_categorize_array1_idx++] = index;
309 tmpbias1 -= expbits_tab[exp_index1[index]] -
310 expbits_tab[exp_index1[index]+1];
311 ++exp_index1[index];
312 } else { /* <--- */
313 int min = 999999;
314 index=-1;
315 for (i=0 ; i<q->total_subbands ; i++){
316 if(exp_index2[i] > 0){
317 v = (-2*exp_index2[i])-quant_index_table[i]+bias;
318 if ( v < min) {
319 min = v;
320 index = i;
324 if(index == -1)break;
325 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
326 tmpbias2 -= expbits_tab[exp_index2[index]] -
327 expbits_tab[exp_index2[index]-1];
328 --exp_index2[index];
332 for(i=0 ; i<q->total_subbands ; i++)
333 category[i] = exp_index2[i];
335 for(i=0 ; i<q->numvector_size-1 ; i++)
336 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
342 * Expand the category vector.
344 * @param q pointer to the COOKContext
345 * @param category pointer to the category array
346 * @param category_index pointer to the category_index array
349 static inline void expand_category(COOKContext *q, int* category,
350 int* category_index){
351 int i;
352 for(i=0 ; i<q->num_vectors ; i++){
353 ++category[category_index[i]];
358 * Unpack the subband_coef_index and subband_coef_sign vectors.
360 * @param q pointer to the COOKContext
361 * @param category pointer to the category array
362 * @param subband_coef_index array of indexes to quant_centroid_tab
363 * @param subband_coef_sign signs of coefficients
366 static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index,
367 int* subband_coef_sign) {
368 int i,j;
369 int vlc, vd ,tmp, result;
371 vd = vd_tab[category];
372 result = 0;
373 for(i=0 ; i<vpr_tab[category] ; i++){
374 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
375 if (q->bits_per_subpacket < get_bits_count(&q->gb)){
376 vlc = 0;
377 result = 1;
379 for(j=vd-1 ; j>=0 ; j--){
380 tmp = (vlc * invradix_tab[category])/0x100000;
381 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1);
382 vlc = tmp;
384 for(j=0 ; j<vd ; j++){
385 if (subband_coef_index[i*vd + j]) {
386 if(get_bits_count(&q->gb) < q->bits_per_subpacket){
387 subband_coef_sign[i*vd+j] = get_bits1(&q->gb);
388 } else {
389 result=1;
390 subband_coef_sign[i*vd+j]=0;
392 } else {
393 subband_coef_sign[i*vd+j]=0;
397 return result;
402 * Fill the mlt_buffer with mlt coefficients.
404 * @param q pointer to the COOKContext
405 * @param category pointer to the category array
406 * @param quant_index_table pointer to the array
407 * @param mlt_buffer pointer to mlt coefficients
411 static void decode_vectors(COOKContext* q, int* category,
412 int *quant_index_table, REAL_T* mlt_buffer){
413 /* A zero in this table means that the subband coefficient is
414 random noise coded. */
415 int subband_coef_index[SUBBAND_SIZE];
416 /* A zero in this table means that the subband coefficient is a
417 positive multiplicator. */
418 int subband_coef_sign[SUBBAND_SIZE];
419 int band, j;
420 int index=0;
422 for(band=0 ; band<q->total_subbands ; band++){
423 index = category[band];
424 if(category[band] < 7){
425 if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){
426 index=7;
427 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
430 if(index>=7) {
431 memset(subband_coef_index, 0, sizeof(subband_coef_index));
432 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
434 q->scalar_dequant(q, index, quant_index_table[band],
435 subband_coef_index, subband_coef_sign,
436 &mlt_buffer[band * SUBBAND_SIZE]);
439 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
440 return;
441 } /* FIXME: should this be removed, or moved into loop above? */
446 * function for decoding mono data
448 * @param q pointer to the COOKContext
449 * @param mlt_buffer pointer to mlt coefficients
452 static void mono_decode(COOKContext *q, REAL_T* mlt_buffer) {
454 int category_index[128];
455 int quant_index_table[102];
456 int category[128];
458 memset(&category, 0, 128*sizeof(int));
459 memset(&category_index, 0, 128*sizeof(int));
461 decode_envelope(q, quant_index_table);
462 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
463 categorize(q, quant_index_table, category, category_index);
464 expand_category(q, category, category_index);
465 decode_vectors(q, category, quant_index_table, mlt_buffer);
469 * function for getting the jointstereo coupling information
471 * @param q pointer to the COOKContext
472 * @param decouple_tab decoupling array
476 static void decouple_info(COOKContext *q, int* decouple_tab){
477 int length, i;
479 if(get_bits1(&q->gb)) {
480 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
482 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
483 for (i=0 ; i<length ; i++) {
484 decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2);
486 return;
489 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
491 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
492 for (i=0 ; i<length ; i++) {
493 decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits);
495 return;
499 * function for decoding joint stereo data
501 * @param q pointer to the COOKContext
502 * @param mlt_buffer1 pointer to left channel mlt coefficients
503 * @param mlt_buffer2 pointer to right channel mlt coefficients
506 static void joint_decode(COOKContext *q, REAL_T* mlt_buffer1,
507 REAL_T* mlt_buffer2) {
508 int i,j;
509 int decouple_tab[SUBBAND_SIZE];
510 REAL_T *decode_buffer = q->decode_buffer_0;
511 int idx;
513 memset(decouple_tab, 0, sizeof(decouple_tab));
514 memset(decode_buffer, 0, sizeof(decode_buffer));
516 /* Make sure the buffers are zeroed out. */
517 memset(mlt_buffer1,0, 1024*sizeof(REAL_T));
518 memset(mlt_buffer2,0, 1024*sizeof(REAL_T));
519 decouple_info(q, decouple_tab);
520 mono_decode(q, decode_buffer);
522 /* The two channels are stored interleaved in decode_buffer. */
523 for (i=0 ; i<q->js_subband_start ; i++) {
524 for (j=0 ; j<SUBBAND_SIZE ; j++) {
525 mlt_buffer1[i*20+j] = decode_buffer[i*40+j];
526 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j];
530 /* When we reach js_subband_start (the higher frequencies)
531 the coefficients are stored in a coupling scheme. */
532 idx = (1 << q->js_vlc_bits) - 1;
533 for (i=q->js_subband_start ; i<q->subbands ; i++) {
534 int i1 = decouple_tab[cplband[i]];
535 int i2 = idx - i1 - 1;
536 for (j=0 ; j<SUBBAND_SIZE ; j++) {
537 REAL_T x = decode_buffer[((q->js_subband_start + i)*20)+j];
538 mlt_buffer1[20*i+j] = cplscale_math(x, q->js_vlc_bits, i1);
539 mlt_buffer2[20*i+j] = cplscale_math(x, q->js_vlc_bits, i2);
545 * First part of subpacket decoding:
546 * decode raw stream bytes and read gain info.
548 * @param q pointer to the COOKContext
549 * @param inbuffer pointer to raw stream data
550 * @param gain_ptr array of current/prev gain pointers
553 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
555 static inline void
556 decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer,
557 cook_gains *gains_ptr)
559 int offset;
561 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
562 q->bits_per_subpacket/8);
563 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
564 q->bits_per_subpacket);
565 decode_gain_info(&q->gb, gains_ptr->now);
567 /* Swap current and previous gains */
568 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
572 * Final part of subpacket decoding:
573 * Apply modulated lapped transform, gain compensation,
574 * clip and convert to integer.
576 * @param q pointer to the COOKContext
577 * @param decode_buffer pointer to the mlt coefficients
578 * @param gain_ptr array of current/prev gain pointers
579 * @param previous_buffer pointer to the previous buffer to be used for overlapping
580 * @param out pointer to the output buffer
581 * @param chan 0: left or single channel, 1: right channel
584 static inline void
585 mlt_compensate_output(COOKContext *q, REAL_T *decode_buffer,
586 cook_gains *gains, REAL_T *previous_buffer,
587 int16_t *out, int chan)
589 REAL_T *buffer = q->mono_mdct_output;
590 int i;
591 imlt_math(q, decode_buffer);
593 /* Overlap with the previous block. */
594 overlap_math(q, gains->previous[0], previous_buffer);
596 /* Apply gain profile */
597 for (i = 0; i < 8; i++) {
598 if (gains->now[i] || gains->now[i + 1])
599 interpolate_math(q, &buffer[q->samples_per_channel/8 * i],
600 gains->now[i], gains->now[i + 1]);
603 /* Save away the current to be previous block. */
604 memcpy(previous_buffer, buffer+q->samples_per_channel,
605 sizeof(REAL_T)*q->samples_per_channel);
607 output_math(q, out, chan);
612 * Cook subpacket decoding. This function returns one decoded subpacket,
613 * usually 1024 samples per channel.
615 * @param q pointer to the COOKContext
616 * @param inbuffer pointer to the inbuffer
617 * @param sub_packet_size subpacket size
618 * @param outbuffer pointer to the outbuffer
622 static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
623 int sub_packet_size, int16_t *outbuffer) {
624 /* packet dump */
625 // for (i=0 ; i<sub_packet_size ; i++) {
626 // DEBUGF("%02x", inbuffer[i]);
627 // }
628 // DEBUGF("\n");
630 decode_bytes_and_gain(q, inbuffer, &q->gains1);
632 if (q->joint_stereo) {
633 joint_decode(q, q->decode_buffer_1, q->decode_buffer_2);
634 } else {
635 mono_decode(q, q->decode_buffer_1);
637 if (q->nb_channels == 2) {
638 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2);
639 mono_decode(q, q->decode_buffer_2);
643 mlt_compensate_output(q, q->decode_buffer_1, &q->gains1,
644 q->mono_previous_buffer1, outbuffer, 0);
646 if (q->nb_channels == 2) {
647 if (q->joint_stereo) {
648 mlt_compensate_output(q, q->decode_buffer_2, &q->gains1,
649 q->mono_previous_buffer2, outbuffer, 1);
650 } else {
651 mlt_compensate_output(q, q->decode_buffer_2, &q->gains2,
652 q->mono_previous_buffer2, outbuffer, 1);
655 return q->samples_per_frame * sizeof(int16_t);
660 * Cook frame decoding
662 * @param rmctx pointer to the RMContext
665 int cook_decode_frame(RMContext *rmctx,COOKContext *q,
666 int16_t *outbuffer, int *data_size,
667 const uint8_t *inbuffer, int buf_size) {
668 //COOKContext *q = avctx->priv_data;
669 //COOKContext *q;
671 if (buf_size < rmctx->block_align)
672 return buf_size;
674 *data_size = decode_subpacket(q, inbuffer, rmctx->block_align, outbuffer);
676 /* Discard the first two frames: no valid audio. */
677 if (rmctx->frame_number < 2) *data_size = 0;
679 return rmctx->block_align;
682 #ifdef COOKDEBUG
683 static void dump_cook_context(COOKContext *q)
685 //int i=0;
686 #define PRINT(a,b) DEBUGF(" %s = %d\n", a, b);
687 DEBUGF("COOKextradata\n");
688 DEBUGF("cookversion=%x\n",q->cookversion);
689 if (q->cookversion > STEREO) {
690 PRINT("js_subband_start",q->js_subband_start);
691 PRINT("js_vlc_bits",q->js_vlc_bits);
693 PRINT("nb_channels",q->nb_channels);
694 PRINT("bit_rate",q->bit_rate);
695 PRINT("sample_rate",q->sample_rate);
696 PRINT("samples_per_channel",q->samples_per_channel);
697 PRINT("samples_per_frame",q->samples_per_frame);
698 PRINT("subbands",q->subbands);
699 PRINT("random_state",q->random_state);
700 PRINT("js_subband_start",q->js_subband_start);
701 PRINT("log2_numvector_size",q->log2_numvector_size);
702 PRINT("numvector_size",q->numvector_size);
703 PRINT("total_subbands",q->total_subbands);
705 #endif
708 * Cook initialization
711 static inline uint16_t get_uint16be(uint8_t *buf)
713 return (uint16_t)((buf[0] << 8)|buf[1]);
716 static inline uint32_t get_uint32be(uint8_t *buf)
718 return (uint32_t)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
721 int cook_decode_init(RMContext *rmctx, COOKContext *q)
724 /* cook extradata */
725 q->cookversion = get_uint32be(rmctx->codec_extradata);
726 q->samples_per_frame = get_uint16be(&rmctx->codec_extradata[4]);
727 q->subbands = get_uint16be(&rmctx->codec_extradata[6]);
728 q->extradata_size = rmctx->extradata_size;
729 if (q->extradata_size >= 16){
730 q->js_subband_start = get_uint16be(&rmctx->codec_extradata[12]);
731 q->js_vlc_bits = get_uint16be(&rmctx->codec_extradata[14]);
734 /* Take data from the RMContext (RM container). */
735 q->sample_rate = rmctx->sample_rate;
736 q->nb_channels = rmctx->nb_channels;
737 q->bit_rate = rmctx->bit_rate;
739 /* Initialize RNG. */
740 q->random_state = 0;
742 /* Initialize extradata related variables. */
743 q->samples_per_channel = q->samples_per_frame / q->nb_channels;
744 q->bits_per_subpacket = rmctx->block_align * 8;
746 /* Initialize default data states. */
747 q->log2_numvector_size = 5;
748 q->total_subbands = q->subbands;
750 /* Initialize version-dependent variables */
751 DEBUGF("q->cookversion=%x\n",q->cookversion);
752 q->joint_stereo = 0;
753 switch (q->cookversion) {
754 case MONO:
755 if (q->nb_channels != 1) {
756 DEBUGF("Container channels != 1, report sample!\n");
757 return -1;
759 DEBUGF("MONO\n");
760 break;
761 case STEREO:
762 if (q->nb_channels != 1) {
763 q->bits_per_subpacket = q->bits_per_subpacket/2;
765 DEBUGF("STEREO\n");
766 break;
767 case JOINT_STEREO:
768 if (q->nb_channels != 2) {
769 DEBUGF("Container channels != 2, report sample!\n");
770 return -1;
772 DEBUGF("JOINT_STEREO\n");
773 if (q->extradata_size >= 16){
774 q->total_subbands = q->subbands + q->js_subband_start;
775 q->joint_stereo = 1;
777 if (q->samples_per_channel > 256) {
778 q->log2_numvector_size = 6;
780 if (q->samples_per_channel > 512) {
781 q->log2_numvector_size = 7;
783 break;
784 case MC_COOK:
785 DEBUGF("MC_COOK not supported!\n");
786 return -1;
787 break;
788 default:
789 DEBUGF("Unknown Cook version, report sample!\n");
790 return -1;
791 break;
794 /* Initialize variable relations */
795 q->numvector_size = (1 << q->log2_numvector_size);
797 /* Generate tables */
798 if (init_cook_vlc_tables(q) != 0)
799 return -1;
802 if(rmctx->block_align >= UINT16_MAX/2)
803 return -1;
805 q->gains1.now = q->gain_1;
806 q->gains1.previous = q->gain_2;
807 q->gains2.now = q->gain_3;
808 q->gains2.previous = q->gain_4;
811 /* Initialize COOK signal arithmetic handling */
812 if (1) {
813 q->scalar_dequant = scalar_dequant_math;
814 q->interpolate = interpolate_math;
817 /* Try to catch some obviously faulty streams, othervise it might be exploitable */
818 if (q->total_subbands > 53) {
819 DEBUGF("total_subbands > 53, report sample!\n");
820 return -1;
822 if (q->subbands > 50) {
823 DEBUGF("subbands > 50, report sample!\n");
824 return -1;
826 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
827 } else {
828 DEBUGF("unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
829 return -1;
831 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) {
832 DEBUGF("q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits);
833 return -1;
837 #ifdef COOKDEBUG
838 dump_cook_context(q);
839 #endif
840 return 0;