FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / libcook / cook.c
blob74f695e6271a0f154edbd904d7c40154ebb932e2
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];
331 memcpy(category, exp_index2, sizeof(int) * q->total_subbands );
332 memcpy(category_index, tmp_categorize_array+tmp_categorize_array2_idx, sizeof(int) * (q->numvector_size-1) );
337 * Expand the category vector.
339 * @param q pointer to the COOKContext
340 * @param category pointer to the category array
341 * @param category_index pointer to the category_index array
344 static inline void expand_category(COOKContext *q, int* category,
345 int* category_index){
346 int i;
347 for(i=0 ; i<q->num_vectors ; i++){
348 ++category[category_index[i]];
353 * Unpack the subband_coef_index and subband_coef_sign vectors.
355 * @param q pointer to the COOKContext
356 * @param category pointer to the category array
357 * @param subband_coef_index array of indexes to quant_centroid_tab
358 * @param subband_coef_sign signs of coefficients
361 static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index,
362 int* subband_coef_sign) {
363 int i,j;
364 int vlc, vd ,tmp, result;
366 vd = vd_tab[category];
367 result = 0;
368 for(i=0 ; i<vpr_tab[category] ; i++)
370 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
371 if (q->bits_per_subpacket < get_bits_count(&q->gb))
373 vlc = 0;
374 result = 1;
375 memset(subband_coef_index, 0, sizeof(int)*vd);
376 memset(subband_coef_sign, 0, sizeof(int)*vd);
377 subband_coef_index+=vd;
378 subband_coef_sign+=vd;
380 else
382 for(j=vd-1 ; j>=0 ; j--){
383 tmp = (vlc * invradix_tab[category])/0x100000;
384 subband_coef_index[j] = vlc - tmp * (kmax_tab[category]+1);
385 vlc = tmp;
388 for(j=0 ; j<vd ; j++)
390 if (*subband_coef_index++) {
391 if(get_bits_count(&q->gb) < q->bits_per_subpacket) {
392 *subband_coef_sign++ = get_bits1(&q->gb);
393 } else {
394 result=1;
395 *subband_coef_sign++=0;
397 } else {
398 *subband_coef_sign++=0;
403 return result;
408 * Fill the mlt_buffer with mlt coefficients.
410 * @param q pointer to the COOKContext
411 * @param category pointer to the category array
412 * @param quant_index_table pointer to the array
413 * @param mlt_buffer pointer to mlt coefficients
417 static void decode_vectors(COOKContext* q, int* category,
418 int *quant_index_table, REAL_T* mlt_buffer){
419 /* A zero in this table means that the subband coefficient is
420 random noise coded. */
421 int subband_coef_index[SUBBAND_SIZE];
422 /* A zero in this table means that the subband coefficient is a
423 positive multiplicator. */
424 int subband_coef_sign[SUBBAND_SIZE];
425 int band, j;
426 int index=0;
428 for(band=0 ; band<q->total_subbands ; band++){
429 index = category[band];
430 if(category[band] < 7){
431 if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){
432 index=7;
433 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
436 if(index>=7) {
437 memset(subband_coef_index, 0, sizeof(subband_coef_index));
438 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
440 q->scalar_dequant(q, index, quant_index_table[band],
441 subband_coef_index, subband_coef_sign,
442 &mlt_buffer[band * SUBBAND_SIZE]);
445 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
446 return;
447 } /* FIXME: should this be removed, or moved into loop above? */
452 * function for decoding mono data
454 * @param q pointer to the COOKContext
455 * @param mlt_buffer pointer to mlt coefficients
458 static void mono_decode(COOKContext *q, REAL_T* mlt_buffer) {
460 int category_index[128];
461 int quant_index_table[102];
462 int category[128];
464 memset(&category, 0, 128*sizeof(int));
465 memset(&category_index, 0, 128*sizeof(int));
467 decode_envelope(q, quant_index_table);
468 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
469 categorize(q, quant_index_table, category, category_index);
470 expand_category(q, category, category_index);
471 decode_vectors(q, category, quant_index_table, mlt_buffer);
475 * function for getting the jointstereo coupling information
477 * @param q pointer to the COOKContext
478 * @param decouple_tab decoupling array
482 static void decouple_info(COOKContext *q, int* decouple_tab){
483 int length, i;
485 if(get_bits1(&q->gb)) {
486 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
488 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
489 for (i=0 ; i<length ; i++) {
490 decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2);
492 return;
495 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
497 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
498 for (i=0 ; i<length ; i++) {
499 decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits);
501 return;
505 * function for decoding joint stereo data
507 * @param q pointer to the COOKContext
508 * @param mlt_buffer1 pointer to left channel mlt coefficients
509 * @param mlt_buffer2 pointer to right channel mlt coefficients
512 static void joint_decode(COOKContext *q, REAL_T* mlt_buffer1,
513 REAL_T* mlt_buffer2) {
514 int i;
515 int decouple_tab[SUBBAND_SIZE];
516 REAL_T *decode_buffer = q->decode_buffer_0;
517 int idx;
519 memset(decouple_tab, 0, sizeof(decouple_tab));
520 memset(decode_buffer, 0, sizeof(decode_buffer));
522 /* Make sure the buffers are zeroed out. */
523 memset(mlt_buffer1,0, 1024*sizeof(REAL_T));
524 memset(mlt_buffer2,0, 1024*sizeof(REAL_T));
525 decouple_info(q, decouple_tab);
526 mono_decode(q, decode_buffer);
528 /* The two channels are stored interleaved in decode_buffer. */
529 REAL_T * mlt_buffer1_end = mlt_buffer1 + (q->js_subband_start*SUBBAND_SIZE);
530 while(mlt_buffer1 < mlt_buffer1_end)
532 memcpy(mlt_buffer1,decode_buffer,sizeof(REAL_T)*SUBBAND_SIZE);
533 memcpy(mlt_buffer2,decode_buffer+20,sizeof(REAL_T)*SUBBAND_SIZE);
534 mlt_buffer1 += 20;
535 mlt_buffer2 += 20;
536 decode_buffer += 40;
539 /* When we reach js_subband_start (the higher frequencies)
540 the coefficients are stored in a coupling scheme. */
541 idx = (1 << q->js_vlc_bits) - 1;
542 for (i=q->js_subband_start ; i<q->subbands ; i++) {
543 int i1 = decouple_tab[cplband[i]];
544 int i2 = idx - i1 - 1;
545 mlt_buffer1_end = mlt_buffer1 + SUBBAND_SIZE;
546 while(mlt_buffer1 < mlt_buffer1_end)
548 *mlt_buffer1++ = cplscale_math(*decode_buffer, q->js_vlc_bits, i1);
549 *mlt_buffer2++ = cplscale_math(*decode_buffer++, q->js_vlc_bits, i2);
551 mlt_buffer1 += (20-SUBBAND_SIZE);
552 mlt_buffer2 += (20-SUBBAND_SIZE);
553 decode_buffer += (20-SUBBAND_SIZE);
558 * First part of subpacket decoding:
559 * decode raw stream bytes and read gain info.
561 * @param q pointer to the COOKContext
562 * @param inbuffer pointer to raw stream data
563 * @param gain_ptr array of current/prev gain pointers
566 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
568 static inline void
569 decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer,
570 cook_gains *gains_ptr)
572 int offset;
574 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
575 q->bits_per_subpacket/8);
576 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
577 q->bits_per_subpacket);
578 decode_gain_info(&q->gb, gains_ptr->now);
580 /* Swap current and previous gains */
581 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
585 * Final part of subpacket decoding:
586 * Apply modulated lapped transform, gain compensation,
587 * clip and convert to integer.
589 * @param q pointer to the COOKContext
590 * @param decode_buffer pointer to the mlt coefficients
591 * @param gain_ptr array of current/prev gain pointers
592 * @param previous_buffer pointer to the previous buffer to be used for overlapping
593 * @param out pointer to the output buffer
594 * @param chan 0: left or single channel, 1: right channel
597 static void
598 mlt_compensate_output(COOKContext *q, REAL_T *decode_buffer,
599 cook_gains *gains, REAL_T *previous_buffer,
600 int16_t *out, int chan)
602 REAL_T *buffer = q->mono_mdct_output;
603 int i;
604 imlt_math(q, decode_buffer);
606 /* Overlap with the previous block. */
607 overlap_math(q, gains->previous[0], previous_buffer);
609 /* Apply gain profile */
610 for (i = 0; i < 8; i++) {
611 if (gains->now[i] || gains->now[i + 1])
612 interpolate_math(q, &buffer[q->samples_per_channel/8 * i],
613 gains->now[i], gains->now[i + 1]);
616 /* Save away the current to be previous block. */
617 memcpy(previous_buffer, buffer+q->samples_per_channel,
618 sizeof(REAL_T)*q->samples_per_channel);
620 output_math(q, out, chan);
625 * Cook subpacket decoding. This function returns one decoded subpacket,
626 * usually 1024 samples per channel.
628 * @param q pointer to the COOKContext
629 * @param inbuffer pointer to the inbuffer
630 * @param sub_packet_size subpacket size
631 * @param outbuffer pointer to the outbuffer
635 static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
636 int sub_packet_size, int16_t *outbuffer) {
637 /* packet dump */
638 // for (i=0 ; i<sub_packet_size ; i++) {
639 // DEBUGF("%02x", inbuffer[i]);
640 // }
641 // DEBUGF("\n");
643 decode_bytes_and_gain(q, inbuffer, &q->gains1);
645 if (q->joint_stereo) {
646 joint_decode(q, q->decode_buffer_1, q->decode_buffer_2);
647 } else {
648 mono_decode(q, q->decode_buffer_1);
650 if (q->nb_channels == 2) {
651 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2);
652 mono_decode(q, q->decode_buffer_2);
656 mlt_compensate_output(q, q->decode_buffer_1, &q->gains1,
657 q->mono_previous_buffer1, outbuffer, 0);
659 if (q->nb_channels == 2) {
660 if (q->joint_stereo) {
661 mlt_compensate_output(q, q->decode_buffer_2, &q->gains1,
662 q->mono_previous_buffer2, outbuffer, 1);
663 } else {
664 mlt_compensate_output(q, q->decode_buffer_2, &q->gains2,
665 q->mono_previous_buffer2, outbuffer, 1);
668 return q->samples_per_frame * sizeof(int16_t);
673 * Cook frame decoding
675 * @param rmctx pointer to the RMContext
678 int cook_decode_frame(RMContext *rmctx,COOKContext *q,
679 int16_t *outbuffer, int *data_size,
680 const uint8_t *inbuffer, int buf_size) {
681 //COOKContext *q = avctx->priv_data;
682 //COOKContext *q;
684 if (buf_size < rmctx->block_align)
685 return buf_size;
687 *data_size = decode_subpacket(q, inbuffer, rmctx->block_align, outbuffer);
689 /* Discard the first two frames: no valid audio. */
690 if (rmctx->frame_number < 2) *data_size = 0;
692 return rmctx->block_align;
695 #ifdef COOKDEBUG
696 static void dump_cook_context(COOKContext *q)
698 //int i=0;
699 #define PRINT(a,b) DEBUGF(" %s = %d\n", a, b);
700 DEBUGF("COOKextradata\n");
701 DEBUGF("cookversion=%x\n",q->cookversion);
702 if (q->cookversion > STEREO) {
703 PRINT("js_subband_start",q->js_subband_start);
704 PRINT("js_vlc_bits",q->js_vlc_bits);
706 PRINT("nb_channels",q->nb_channels);
707 PRINT("bit_rate",q->bit_rate);
708 PRINT("sample_rate",q->sample_rate);
709 PRINT("samples_per_channel",q->samples_per_channel);
710 PRINT("samples_per_frame",q->samples_per_frame);
711 PRINT("subbands",q->subbands);
712 PRINT("random_state",q->random_state);
713 PRINT("js_subband_start",q->js_subband_start);
714 PRINT("log2_numvector_size",q->log2_numvector_size);
715 PRINT("numvector_size",q->numvector_size);
716 PRINT("total_subbands",q->total_subbands);
718 #endif
721 * Cook initialization
724 int cook_decode_init(RMContext *rmctx, COOKContext *q)
727 /* cook extradata */
728 q->cookversion = rm_get_uint32be(rmctx->codec_extradata);
729 q->samples_per_frame = rm_get_uint16be(&rmctx->codec_extradata[4]);
730 q->subbands = rm_get_uint16be(&rmctx->codec_extradata[6]);
731 q->extradata_size = rmctx->extradata_size;
732 if (q->extradata_size >= 16){
733 q->js_subband_start = rm_get_uint16be(&rmctx->codec_extradata[12]);
734 q->js_vlc_bits = rm_get_uint16be(&rmctx->codec_extradata[14]);
737 /* Take data from the RMContext (RM container). */
738 q->sample_rate = rmctx->sample_rate;
739 q->nb_channels = rmctx->nb_channels;
740 q->bit_rate = rmctx->bit_rate;
742 /* Initialize RNG. */
743 q->random_state = 0;
745 /* Initialize extradata related variables. */
746 q->samples_per_channel = q->samples_per_frame >> (q->nb_channels-1);
747 q->bits_per_subpacket = rmctx->block_align * 8;
749 /* Initialize default data states. */
750 q->log2_numvector_size = 5;
751 q->total_subbands = q->subbands;
753 /* Initialize version-dependent variables */
754 DEBUGF("q->cookversion=%x\n",q->cookversion);
755 q->joint_stereo = 0;
756 switch (q->cookversion) {
757 case MONO:
758 if (q->nb_channels != 1) {
759 DEBUGF("Container channels != 1, report sample!\n");
760 return -1;
762 DEBUGF("MONO\n");
763 break;
764 case STEREO:
765 if (q->nb_channels != 1) {
766 q->bits_per_subpacket = q->bits_per_subpacket/2;
768 DEBUGF("STEREO\n");
769 break;
770 case JOINT_STEREO:
771 if (q->nb_channels != 2) {
772 DEBUGF("Container channels != 2, report sample!\n");
773 return -1;
775 DEBUGF("JOINT_STEREO\n");
776 if (q->extradata_size >= 16){
777 q->total_subbands = q->subbands + q->js_subband_start;
778 q->joint_stereo = 1;
780 if (q->samples_per_channel > 256) {
781 q->log2_numvector_size = 6;
783 if (q->samples_per_channel > 512) {
784 q->log2_numvector_size = 7;
786 break;
787 case MC_COOK:
788 DEBUGF("MC_COOK not supported!\n");
789 return -1;
790 break;
791 default:
792 DEBUGF("Unknown Cook version, report sample!\n");
793 return -1;
794 break;
797 /* Initialize variable relations */
798 q->numvector_size = (1 << q->log2_numvector_size);
800 /* Generate tables */
801 if (init_cook_vlc_tables(q) != 0)
802 return -1;
805 if(rmctx->block_align >= UINT16_MAX/2)
806 return -1;
808 q->gains1.now = q->gain_1;
809 q->gains1.previous = q->gain_2;
810 q->gains2.now = q->gain_3;
811 q->gains2.previous = q->gain_4;
814 /* Initialize COOK signal arithmetic handling */
815 if (1) {
816 q->scalar_dequant = scalar_dequant_math;
817 q->interpolate = interpolate_math;
820 /* Try to catch some obviously faulty streams, othervise it might be exploitable */
821 if (q->total_subbands > 53) {
822 DEBUGF("total_subbands > 53, report sample!\n");
823 return -1;
825 if (q->subbands > 50) {
826 DEBUGF("subbands > 50, report sample!\n");
827 return -1;
829 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
830 } else {
831 DEBUGF("unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
832 return -1;
834 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) {
835 DEBUGF("q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits);
836 return -1;
840 #ifdef COOKDEBUG
841 dump_cook_context(q);
842 #endif
843 return 0;