-Remove all dynamic allocations, hence remove cook_decode_close() which was basically
[kugel-rb.git] / apps / codecs / libcook / cook.c
blob8caa3992bddbdca06b1b97e0a49903093a5bfd2a
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 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
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 #if 0
64 #define DEBUGF(message,args ...) printf
65 #else
66 #define DEBUGF(...)
67 #endif
69 /**
70 * Random bit stream generator.
72 static int inline 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) ? 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];
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){
352 int i;
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) {
369 int i,j;
370 int vlc, vd ,tmp, result;
372 vd = vd_tab[category];
373 result = 0;
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)){
377 vlc = 0;
378 result = 1;
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);
383 vlc = tmp;
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);
389 } else {
390 result=1;
391 subband_coef_sign[i*vd+j]=0;
393 } else {
394 subband_coef_sign[i*vd+j]=0;
398 return result;
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];
420 int band, j;
421 int index=0;
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)){
427 index=7;
428 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
431 if(index>=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){
441 return;
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];
457 int category[128];
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){
478 int length, i;
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);
487 return;
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);
496 return;
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) {
509 int i,j;
510 int decouple_tab[SUBBAND_SIZE];
511 REAL_T *decode_buffer = q->decode_buffer_0;
512 int idx;
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)
556 static inline void
557 decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer,
558 cook_gains *gains_ptr)
560 int offset;
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
585 static inline void
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;
591 int i;
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) {
625 /* packet dump */
626 // for (i=0 ; i<sub_packet_size ; i++) {
627 // DEBUGF("%02x", inbuffer[i]);
628 // }
629 // DEBUGF("\n");
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);
635 } else {
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);
651 } else {
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;
670 //COOKContext *q;
672 if (buf_size < rmctx->block_align)
673 return buf_size;
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;
683 #ifdef COOKDEBUG
684 static void dump_cook_context(COOKContext *q)
686 //int i=0;
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);
706 #endif
709 * Cook initialization
712 int cook_decode_init(RMContext *rmctx, COOKContext *q)
714 /* cook extradata */
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. */
730 q->random_state = 0;
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);
742 q->joint_stereo = 0;
743 switch (q->cookversion) {
744 case MONO:
745 if (q->nb_channels != 1) {
746 DEBUGF("Container channels != 1, report sample!\n");
747 return -1;
749 DEBUGF("MONO\n");
750 break;
751 case STEREO:
752 if (q->nb_channels != 1) {
753 q->bits_per_subpacket = q->bits_per_subpacket/2;
755 DEBUGF("STEREO\n");
756 break;
757 case JOINT_STEREO:
758 if (q->nb_channels != 2) {
759 DEBUGF("Container channels != 2, report sample!\n");
760 return -1;
762 DEBUGF("JOINT_STEREO\n");
763 if (q->extradata_size >= 16){
764 q->total_subbands = q->subbands + q->js_subband_start;
765 q->joint_stereo = 1;
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;
773 break;
774 case MC_COOK:
775 DEBUGF("MC_COOK not supported!\n");
776 return -1;
777 break;
778 default:
779 DEBUGF("Unknown Cook version, report sample!\n");
780 return -1;
781 break;
784 /* Initialize variable relations */
785 q->numvector_size = (1 << q->log2_numvector_size);
787 /* Generate tables */
788 if (init_cook_vlc_tables(q) != 0)
789 return -1;
792 if(q->block_align >= UINT_MAX/2)
793 return -1;
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 */
802 if (1) {
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");
810 return -1;
812 if (q->subbands > 50) {
813 DEBUGF("subbands > 50, report sample!\n");
814 return -1;
816 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
817 } else {
818 DEBUGF("unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
819 return -1;
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);
823 return -1;
827 #ifdef COOKDEBUG
828 dump_cook_context(q);
829 #endif
830 return 0;