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
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
55 /* the different Cook versions */
56 #define MONO 0x1000001
57 #define STEREO 0x1000002
58 #define JOINT_STEREO 0x1000003
59 #define MC_COOK 0x2000000 //multichannel Cook, not supported
61 #define SUBBAND_SIZE 20
62 #define MAX_SUBPACKETS 5
70 * Random bit stream generator.
72 static inline int 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 /* Codebook sizes (11586 * 4 bytes in total) */
106 /* Used for envelope_quant_index[]. */
107 static VLC_TYPE vlcbuf00
[ 520][2] IBSS_ATTR_COOK_LARGE_IRAM
;
108 static VLC_TYPE vlcbuf01
[ 640][2] IBSS_ATTR_COOK_LARGE_IRAM
;
109 static VLC_TYPE vlcbuf02
[ 544][2] IBSS_ATTR_COOK_LARGE_IRAM
;
110 static VLC_TYPE vlcbuf03
[ 528][2] IBSS_ATTR_COOK_VLCBUF
;
111 static VLC_TYPE vlcbuf04
[ 544][2] IBSS_ATTR_COOK_VLCBUF
;
112 static VLC_TYPE vlcbuf05
[ 544][2] IBSS_ATTR_COOK_VLCBUF
;
113 static VLC_TYPE vlcbuf06
[ 640][2] IBSS_ATTR_COOK_VLCBUF
;
114 static VLC_TYPE vlcbuf07
[ 576][2] IBSS_ATTR_COOK_VLCBUF
;
115 static VLC_TYPE vlcbuf08
[ 528][2] IBSS_ATTR_COOK_VLCBUF
;
116 static VLC_TYPE vlcbuf09
[ 544][2] IBSS_ATTR_COOK_VLCBUF
;
117 static VLC_TYPE vlcbuf10
[ 544][2] IBSS_ATTR_COOK_VLCBUF
;
118 static VLC_TYPE vlcbuf11
[ 640][2] IBSS_ATTR_COOK_VLCBUF
;
119 static VLC_TYPE vlcbuf12
[ 544][2] IBSS_ATTR_COOK_LARGE_IRAM
;
120 /* Used for sqvh[]. */
121 static VLC_TYPE vlcbuf13
[ 622][2] IBSS_ATTR_COOK_LARGE_IRAM
;
122 static VLC_TYPE vlcbuf14
[ 308][2] IBSS_ATTR_COOK_LARGE_IRAM
;
123 static VLC_TYPE vlcbuf15
[ 280][2] IBSS_ATTR_COOK_LARGE_IRAM
;
124 static VLC_TYPE vlcbuf16
[1456][2] IBSS_ATTR_COOK_LARGE_IRAM
;
125 static VLC_TYPE vlcbuf17
[ 694][2] IBSS_ATTR_COOK_LARGE_IRAM
;
126 static VLC_TYPE vlcbuf18
[ 698][2] IBSS_ATTR_COOK_LARGE_IRAM
;
127 static VLC_TYPE vlcbuf19
[ 104][2] IBSS_ATTR_COOK_LARGE_IRAM
;
129 static VLC_TYPE vlcbuf20
[ 88][2] IBSS_ATTR_COOK_VLCBUF
;
131 /* Code book sizes (11586 entries in total) */
132 static int env_size
[13] = {520,640,544, 528,544,544,640,576,528,544,544,640,544};
133 static int sqvh_size
[7] = {622,308,280,1456,694,698,104};
134 static int ccpl_size
= 88;
137 static int init_cook_vlc_tables(COOKContext
*q
) {
140 /* Set pointers for codebooks. */
141 q
->envelope_quant_index
[ 0].table
= vlcbuf00
;
142 q
->envelope_quant_index
[ 1].table
= vlcbuf01
;
143 q
->envelope_quant_index
[ 2].table
= vlcbuf02
;
144 q
->envelope_quant_index
[ 3].table
= vlcbuf03
;
145 q
->envelope_quant_index
[ 4].table
= vlcbuf04
;
146 q
->envelope_quant_index
[ 5].table
= vlcbuf05
;
147 q
->envelope_quant_index
[ 6].table
= vlcbuf06
;
148 q
->envelope_quant_index
[ 7].table
= vlcbuf07
;
149 q
->envelope_quant_index
[ 8].table
= vlcbuf08
;
150 q
->envelope_quant_index
[ 9].table
= vlcbuf09
;
151 q
->envelope_quant_index
[10].table
= vlcbuf10
;
152 q
->envelope_quant_index
[11].table
= vlcbuf11
;
153 q
->envelope_quant_index
[12].table
= vlcbuf12
;
154 q
->sqvh
[0].table
= vlcbuf13
;
155 q
->sqvh
[1].table
= vlcbuf14
;
156 q
->sqvh
[2].table
= vlcbuf15
;
157 q
->sqvh
[3].table
= vlcbuf16
;
158 q
->sqvh
[4].table
= vlcbuf17
;
159 q
->sqvh
[5].table
= vlcbuf18
;
160 q
->sqvh
[6].table
= vlcbuf19
;
161 q
->ccpl
.table
= vlcbuf20
;
163 /* Init envelope VLC (13 books) */
164 for (i
=0 ; i
<13 ; i
++) {
165 q
->envelope_quant_index
[i
].table_allocated
= env_size
[i
];
166 result
|= init_vlc (&q
->envelope_quant_index
[i
], 9, 24,
167 envelope_quant_index_huffbits
[i
], 1, 1,
168 envelope_quant_index_huffcodes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
171 /* Init subband VLC (7 books) */
172 for (i
=0 ; i
<7 ; i
++) {
173 q
->sqvh
[i
].table_allocated
= sqvh_size
[i
];
174 result
|= init_vlc (&q
->sqvh
[i
], vhvlcsize_tab
[i
], vhsize_tab
[i
],
175 cvh_huffbits
[i
], 1, 1,
176 cvh_huffcodes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
179 /* Init Joint-Stereo VLC (1 book) */
180 if (q
->nb_channels
==2 && q
->joint_stereo
==1){
181 q
->ccpl
.table_allocated
= ccpl_size
;
182 result
|= init_vlc (&q
->ccpl
, 6, (1<<q
->js_vlc_bits
)-1,
183 ccpl_huffbits
[q
->js_vlc_bits
-2], 1, 1,
184 ccpl_huffcodes
[q
->js_vlc_bits
-2], 2, 2, INIT_VLC_USE_NEW_STATIC
);
185 DEBUGF("Joint-stereo VLC used.\n");
188 DEBUGF("VLC tables initialized. Result = %d\n",result
);
191 /*************** init functions end ***********/
194 * Cook indata decoding, every 32 bits are XORed with 0x37c511f2.
195 * Why? No idea, some checksum/error detection method maybe.
197 * Out buffer size: extra bytes are needed to cope with
198 * padding/misalignment.
199 * Subpackets passed to the decoder can contain two, consecutive
200 * half-subpackets, of identical but arbitrary size.
201 * 1234 1234 1234 1234 extraA extraB
202 * Case 1: AAAA BBBB 0 0
203 * Case 2: AAAA ABBB BB-- 3 3
204 * Case 3: AAAA AABB BBBB 2 2
205 * Case 4: AAAA AAAB BBBB BB-- 1 5
207 * Nice way to waste CPU cycles.
209 * @param inbuffer pointer to byte array of indata
210 * @param out pointer to byte array of outdata
211 * @param bytes number of bytes
213 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)
214 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
216 static inline int decode_bytes(const uint8_t* inbuffer
, uint8_t* out
, int bytes
){
220 uint32_t* obuf
= (uint32_t*) out
;
221 /* FIXME: 64 bit platforms would be able to do 64 bits at a time.
222 * I'm too lazy though, should be something like
223 * for(i=0 ; i<bitamount/64 ; i++)
224 * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]);
225 * Buffer alignment needs to be checked. */
227 off
= (intptr_t)inbuffer
& 3;
228 buf
= (const uint32_t*) (inbuffer
- off
);
229 c
= be2me_32((0x37c511f2 >> (off
*8)) | (0x37c511f2 << (32-(off
*8))));
231 for (i
= 0; i
< bytes
/4; i
++)
232 obuf
[i
] = c
^ buf
[i
];
238 * Fill the gain array for the timedomain quantization.
240 * @param q pointer to the COOKContext
241 * @param gaininfo[9] array of gain indexes
244 static void decode_gain_info(GetBitContext
*gb
, int *gaininfo
)
248 while (get_bits1(gb
)) {}
249 n
= get_bits_count(gb
) - 1; //amount of elements*2 to update
253 int index
= get_bits(gb
, 3);
254 int gain
= get_bits1(gb
) ? (int)get_bits(gb
, 4) - 7 : -1;
256 while (i
<= index
) gaininfo
[i
++] = gain
;
258 while (i
<= 8) gaininfo
[i
++] = 0;
262 * Create the quant index table needed for the envelope.
264 * @param q pointer to the COOKContext
265 * @param quant_index_table pointer to the array
268 static void decode_envelope(COOKContext
*q
, int* quant_index_table
) {
271 quant_index_table
[0]= get_bits(&q
->gb
,6) - 6; //This is used later in categorize
273 for (i
=1 ; i
< q
->total_subbands
; i
++){
275 if (i
>= q
->js_subband_start
* 2) {
276 vlc_index
-=q
->js_subband_start
;
279 if(vlc_index
< 1) vlc_index
= 1;
281 if (vlc_index
>13) vlc_index
= 13; //the VLC tables >13 are identical to No. 13
283 j
= get_vlc2(&q
->gb
, q
->envelope_quant_index
[vlc_index
-1].table
,
284 q
->envelope_quant_index
[vlc_index
-1].bits
,2);
285 quant_index_table
[i
] = quant_index_table
[i
-1] + j
- 12; //differential encoding
290 * Calculate the category and category_index vector.
292 * @param q pointer to the COOKContext
293 * @param quant_index_table pointer to the array
294 * @param category pointer to the category array
295 * @param category_index pointer to the category_index array
298 static void categorize(COOKContext
*q
, int* quant_index_table
,
299 int* category
, int* category_index
){
300 int exp_idx
, bias
, tmpbias1
, tmpbias2
, bits_left
, num_bits
, index
, v
, i
, j
;
304 int tmp_categorize_array
[128*2];
305 int tmp_categorize_array1_idx
=q
->numvector_size
;
306 int tmp_categorize_array2_idx
=q
->numvector_size
;
308 bits_left
= q
->bits_per_subpacket
- get_bits_count(&q
->gb
);
310 if(bits_left
> q
->samples_per_channel
) {
311 bits_left
= q
->samples_per_channel
+
312 ((bits_left
- q
->samples_per_channel
)*5)/8;
313 //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left);
316 memset(&exp_index1
,0,102*sizeof(int));
317 memset(&exp_index2
,0,102*sizeof(int));
318 memset(&tmp_categorize_array
,0,128*2*sizeof(int));
323 for (i
=32 ; i
>0 ; i
=i
/2){
326 for (j
=q
->total_subbands
; j
>0 ; j
--){
327 exp_idx
= av_clip((i
- quant_index_table
[index
] + bias
) / 2, 0, 7);
329 num_bits
+=expbits_tab
[exp_idx
];
331 if(num_bits
>= bits_left
- 32){
336 /* Calculate total number of bits. */
338 for (i
=0 ; i
<q
->total_subbands
; i
++) {
339 exp_idx
= av_clip((bias
- quant_index_table
[i
]) / 2, 0, 7);
340 num_bits
+= expbits_tab
[exp_idx
];
341 exp_index1
[i
] = exp_idx
;
342 exp_index2
[i
] = exp_idx
;
344 tmpbias1
= tmpbias2
= num_bits
;
346 for (j
= 1 ; j
< q
->numvector_size
; j
++) {
347 if (tmpbias1
+ tmpbias2
> 2*bits_left
) { /* ---> */
350 for (i
=0 ; i
<q
->total_subbands
; i
++){
351 if (exp_index1
[i
] < 7) {
352 v
= (-2*exp_index1
[i
]) - quant_index_table
[i
] + bias
;
360 tmp_categorize_array
[tmp_categorize_array1_idx
++] = index
;
361 tmpbias1
-= expbits_tab
[exp_index1
[index
]] -
362 expbits_tab
[exp_index1
[index
]+1];
367 for (i
=0 ; i
<q
->total_subbands
; i
++){
368 if(exp_index2
[i
] > 0){
369 v
= (-2*exp_index2
[i
])-quant_index_table
[i
]+bias
;
376 if(index
== -1)break;
377 tmp_categorize_array
[--tmp_categorize_array2_idx
] = index
;
378 tmpbias2
-= expbits_tab
[exp_index2
[index
]] -
379 expbits_tab
[exp_index2
[index
]-1];
383 memcpy(category
, exp_index2
, sizeof(int) * q
->total_subbands
);
384 memcpy(category_index
, tmp_categorize_array
+tmp_categorize_array2_idx
, sizeof(int) * (q
->numvector_size
-1) );
389 * Expand the category vector.
391 * @param q pointer to the COOKContext
392 * @param category pointer to the category array
393 * @param category_index pointer to the category_index array
396 static inline void expand_category(COOKContext
*q
, int* category
,
397 int* category_index
){
399 for(i
=0 ; i
<q
->num_vectors
; i
++){
400 ++category
[category_index
[i
]];
405 * Unpack the subband_coef_index and subband_coef_sign vectors.
407 * @param q pointer to the COOKContext
408 * @param category pointer to the category array
409 * @param subband_coef_index array of indexes to quant_centroid_tab
410 * @param subband_coef_sign signs of coefficients
413 static int unpack_SQVH(COOKContext
*q
, int category
, int* subband_coef_index
,
414 int* subband_coef_sign
) {
416 int vlc
, vd
,tmp
, result
;
418 vd
= vd_tab
[category
];
420 for(i
=0 ; i
<vpr_tab
[category
] ; i
++)
422 vlc
= get_vlc2(&q
->gb
, q
->sqvh
[category
].table
, q
->sqvh
[category
].bits
, 3);
423 if (q
->bits_per_subpacket
< get_bits_count(&q
->gb
))
427 memset(subband_coef_index
, 0, sizeof(int)*vd
);
428 memset(subband_coef_sign
, 0, sizeof(int)*vd
);
429 subband_coef_index
+=vd
;
430 subband_coef_sign
+=vd
;
434 for(j
=vd
-1 ; j
>=0 ; j
--){
435 tmp
= (vlc
* invradix_tab
[category
])/0x100000;
436 subband_coef_index
[j
] = vlc
- tmp
* (kmax_tab
[category
]+1);
440 for(j
=0 ; j
<vd
; j
++)
442 if (*subband_coef_index
++) {
443 if(get_bits_count(&q
->gb
) < q
->bits_per_subpacket
) {
444 *subband_coef_sign
++ = get_bits1(&q
->gb
);
447 *subband_coef_sign
++=0;
450 *subband_coef_sign
++=0;
460 * Fill the mlt_buffer with mlt coefficients.
462 * @param q pointer to the COOKContext
463 * @param category pointer to the category array
464 * @param quant_index_table pointer to the array
465 * @param mlt_buffer pointer to mlt coefficients
468 static void decode_vectors(COOKContext
* q
, int* category
,
469 int *quant_index_table
, REAL_T
* mlt_buffer
)
470 ICODE_ATTR_COOK_DECODE
;
471 static void decode_vectors(COOKContext
* q
, int* category
,
472 int *quant_index_table
, REAL_T
* mlt_buffer
){
473 /* A zero in this table means that the subband coefficient is
474 random noise coded. */
475 int subband_coef_index
[SUBBAND_SIZE
];
476 /* A zero in this table means that the subband coefficient is a
477 positive multiplicator. */
478 int subband_coef_sign
[SUBBAND_SIZE
];
482 for(band
=0 ; band
<q
->total_subbands
; band
++){
483 index
= category
[band
];
484 if(category
[band
] < 7){
485 if(unpack_SQVH(q
, category
[band
], subband_coef_index
, subband_coef_sign
)){
487 for(j
=0 ; j
<q
->total_subbands
; j
++) category
[band
+j
]=7;
491 memset(subband_coef_index
, 0, sizeof(subband_coef_index
));
492 memset(subband_coef_sign
, 0, sizeof(subband_coef_sign
));
494 scalar_dequant_math(q
, index
, quant_index_table
[band
],
495 subband_coef_index
, subband_coef_sign
,
496 &mlt_buffer
[band
* SUBBAND_SIZE
]);
499 if(q
->total_subbands
*SUBBAND_SIZE
>= q
->samples_per_channel
){
501 } /* FIXME: should this be removed, or moved into loop above? */
506 * function for decoding mono data
508 * @param q pointer to the COOKContext
509 * @param mlt_buffer pointer to mlt coefficients
512 static void mono_decode(COOKContext
*q
, REAL_T
* mlt_buffer
) ICODE_ATTR_COOK_DECODE
;
513 static void mono_decode(COOKContext
*q
, REAL_T
* mlt_buffer
) {
515 int category_index
[128];
516 int quant_index_table
[102];
519 memset(&category
, 0, 128*sizeof(int));
520 memset(&category_index
, 0, 128*sizeof(int));
522 decode_envelope(q
, quant_index_table
);
523 q
->num_vectors
= get_bits(&q
->gb
,q
->log2_numvector_size
);
524 categorize(q
, quant_index_table
, category
, category_index
);
525 expand_category(q
, category
, category_index
);
526 decode_vectors(q
, category
, quant_index_table
, mlt_buffer
);
530 * function for getting the jointstereo coupling information
532 * @param q pointer to the COOKContext
533 * @param decouple_tab decoupling array
537 static void decouple_info(COOKContext
*q
, int* decouple_tab
){
540 if(get_bits1(&q
->gb
)) {
541 if(cplband
[q
->js_subband_start
] > cplband
[q
->subbands
-1]) return;
543 length
= cplband
[q
->subbands
-1] - cplband
[q
->js_subband_start
] + 1;
544 for (i
=0 ; i
<length
; i
++) {
545 decouple_tab
[cplband
[q
->js_subband_start
] + i
] = get_vlc2(&q
->gb
, q
->ccpl
.table
, q
->ccpl
.bits
, 2);
550 if(cplband
[q
->js_subband_start
] > cplband
[q
->subbands
-1]) return;
552 length
= cplband
[q
->subbands
-1] - cplband
[q
->js_subband_start
] + 1;
553 for (i
=0 ; i
<length
; i
++) {
554 decouple_tab
[cplband
[q
->js_subband_start
] + i
] = get_bits(&q
->gb
, q
->js_vlc_bits
);
560 * function for decoding joint stereo data
562 * @param q pointer to the COOKContext
563 * @param mlt_buffer1 pointer to left channel mlt coefficients
564 * @param mlt_buffer2 pointer to right channel mlt coefficients
567 static void joint_decode(COOKContext
*q
, REAL_T
* mlt_buffer1
,
568 REAL_T
* mlt_buffer2
) {
570 int decouple_tab
[SUBBAND_SIZE
];
571 REAL_T
*decode_buffer
= q
->decode_buffer_0
;
574 memset(decouple_tab
, 0, sizeof(decouple_tab
));
575 memset(decode_buffer
, 0, sizeof(decode_buffer
));
577 /* Make sure the buffers are zeroed out. */
578 memset(mlt_buffer1
,0, 1024*sizeof(REAL_T
));
579 memset(mlt_buffer2
,0, 1024*sizeof(REAL_T
));
580 decouple_info(q
, decouple_tab
);
581 mono_decode(q
, decode_buffer
);
583 /* The two channels are stored interleaved in decode_buffer. */
584 REAL_T
* mlt_buffer1_end
= mlt_buffer1
+ (q
->js_subband_start
*SUBBAND_SIZE
);
585 while(mlt_buffer1
< mlt_buffer1_end
)
587 memcpy(mlt_buffer1
,decode_buffer
,sizeof(REAL_T
)*SUBBAND_SIZE
);
588 memcpy(mlt_buffer2
,decode_buffer
+20,sizeof(REAL_T
)*SUBBAND_SIZE
);
594 /* When we reach js_subband_start (the higher frequencies)
595 the coefficients are stored in a coupling scheme. */
596 idx
= (1 << q
->js_vlc_bits
) - 1;
597 for (i
=q
->js_subband_start
; i
<q
->subbands
; i
++) {
598 int i1
= decouple_tab
[cplband
[i
]];
599 int i2
= idx
- i1
- 1;
600 mlt_buffer1_end
= mlt_buffer1
+ SUBBAND_SIZE
;
601 while(mlt_buffer1
< mlt_buffer1_end
)
603 *mlt_buffer1
++ = cplscale_math(*decode_buffer
, q
->js_vlc_bits
, i1
);
604 *mlt_buffer2
++ = cplscale_math(*decode_buffer
++, q
->js_vlc_bits
, i2
);
606 mlt_buffer1
+= (20-SUBBAND_SIZE
);
607 mlt_buffer2
+= (20-SUBBAND_SIZE
);
608 decode_buffer
+= (20-SUBBAND_SIZE
);
613 * First part of subpacket decoding:
614 * decode raw stream bytes and read gain info.
616 * @param q pointer to the COOKContext
617 * @param inbuffer pointer to raw stream data
618 * @param gain_ptr array of current/prev gain pointers
621 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
624 decode_bytes_and_gain(COOKContext
*q
, const uint8_t *inbuffer
,
625 cook_gains
*gains_ptr
)
629 offset
= decode_bytes(inbuffer
, q
->decoded_bytes_buffer
,
630 q
->bits_per_subpacket
/8);
631 init_get_bits(&q
->gb
, q
->decoded_bytes_buffer
+ offset
,
632 q
->bits_per_subpacket
);
633 decode_gain_info(&q
->gb
, gains_ptr
->now
);
635 /* Swap current and previous gains */
636 FFSWAP(int *, gains_ptr
->now
, gains_ptr
->previous
);
640 * Final part of subpacket decoding:
641 * Apply modulated lapped transform, gain compensation,
642 * clip and convert to integer.
644 * @param q pointer to the COOKContext
645 * @param decode_buffer pointer to the mlt coefficients
646 * @param gain_ptr array of current/prev gain pointers
647 * @param previous_buffer pointer to the previous buffer to be used for overlapping
648 * @param out pointer to the output buffer
649 * @param chan 0: left or single channel, 1: right channel
653 mlt_compensate_output(COOKContext
*q
, REAL_T
*decode_buffer
,
654 cook_gains
*gains
, REAL_T
*previous_buffer
,
655 int32_t *out
, int chan
)
657 REAL_T
*buffer
= q
->mono_mdct_output
;
659 imlt_math(q
, decode_buffer
);
661 /* Overlap with the previous block. */
662 overlap_math(q
, gains
->previous
[0], previous_buffer
);
664 /* Apply gain profile */
665 for (i
= 0; i
< 8; i
++) {
666 if (gains
->now
[i
] || gains
->now
[i
+ 1])
667 interpolate_math(q
, &buffer
[q
->samples_per_channel
/8 * i
],
668 gains
->now
[i
], gains
->now
[i
+ 1]);
671 /* Save away the current to be previous block. */
672 memcpy(previous_buffer
, buffer
+q
->samples_per_channel
,
673 sizeof(REAL_T
)*q
->samples_per_channel
);
675 /* Copy output to non-interleaved sample buffer */
676 memcpy(out
+ (chan
* q
->samples_per_channel
), buffer
,
677 sizeof(REAL_T
)*q
->samples_per_channel
);
682 * Cook subpacket decoding. This function returns one decoded subpacket,
683 * usually 1024 samples per channel.
685 * @param q pointer to the COOKContext
686 * @param inbuffer pointer to the inbuffer
687 * @param sub_packet_size subpacket size
688 * @param outbuffer pointer to the outbuffer
692 static int decode_subpacket(COOKContext
*q
, const uint8_t *inbuffer
,
693 int sub_packet_size
, int32_t *outbuffer
) {
695 // for (i=0 ; i<sub_packet_size ; i++) {
696 // DEBUGF("%02x", inbuffer[i]);
700 decode_bytes_and_gain(q
, inbuffer
, &q
->gains1
);
702 if (q
->joint_stereo
) {
703 joint_decode(q
, q
->decode_buffer_1
, q
->decode_buffer_2
);
705 mono_decode(q
, q
->decode_buffer_1
);
707 if (q
->nb_channels
== 2) {
708 decode_bytes_and_gain(q
, inbuffer
+ sub_packet_size
/2, &q
->gains2
);
709 mono_decode(q
, q
->decode_buffer_2
);
713 mlt_compensate_output(q
, q
->decode_buffer_1
, &q
->gains1
,
714 q
->mono_previous_buffer1
, outbuffer
, 0);
716 if (q
->nb_channels
== 2) {
717 if (q
->joint_stereo
) {
718 mlt_compensate_output(q
, q
->decode_buffer_2
, &q
->gains1
,
719 q
->mono_previous_buffer2
, outbuffer
, 1);
721 mlt_compensate_output(q
, q
->decode_buffer_2
, &q
->gains2
,
722 q
->mono_previous_buffer2
, outbuffer
, 1);
725 return q
->samples_per_frame
* sizeof(int32_t);
730 * Cook frame decoding
732 * @param rmctx pointer to the RMContext
735 int cook_decode_frame(RMContext
*rmctx
,COOKContext
*q
,
736 int32_t *outbuffer
, int *data_size
,
737 const uint8_t *inbuffer
, int buf_size
) {
738 //COOKContext *q = avctx->priv_data;
741 if (buf_size
< rmctx
->block_align
)
744 *data_size
= decode_subpacket(q
, inbuffer
, rmctx
->block_align
, outbuffer
);
746 /* Discard the first two frames: no valid audio. */
747 if (rmctx
->frame_number
< 2) *data_size
= 0;
749 return rmctx
->block_align
;
753 static void dump_cook_context(COOKContext
*q
)
756 #define PRINT(a,b) DEBUGF(" %s = %d\n", a, b);
757 DEBUGF("COOKextradata\n");
758 DEBUGF("cookversion=%x\n",q
->cookversion
);
759 if (q
->cookversion
> STEREO
) {
760 PRINT("js_subband_start",q
->js_subband_start
);
761 PRINT("js_vlc_bits",q
->js_vlc_bits
);
763 PRINT("nb_channels",q
->nb_channels
);
764 PRINT("bit_rate",q
->bit_rate
);
765 PRINT("sample_rate",q
->sample_rate
);
766 PRINT("samples_per_channel",q
->samples_per_channel
);
767 PRINT("samples_per_frame",q
->samples_per_frame
);
768 PRINT("subbands",q
->subbands
);
769 PRINT("random_state",q
->random_state
);
770 PRINT("js_subband_start",q
->js_subband_start
);
771 PRINT("log2_numvector_size",q
->log2_numvector_size
);
772 PRINT("numvector_size",q
->numvector_size
);
773 PRINT("total_subbands",q
->total_subbands
);
778 * Cook initialization
781 int cook_decode_init(RMContext
*rmctx
, COOKContext
*q
)
783 #if defined(CPU_COLDFIRE)
784 coldfire_set_macsr(EMAC_FRACTIONAL
| EMAC_SATURATE
);
787 q
->cookversion
= rm_get_uint32be(rmctx
->codec_extradata
);
788 q
->samples_per_frame
= rm_get_uint16be(&rmctx
->codec_extradata
[4]);
789 q
->subbands
= rm_get_uint16be(&rmctx
->codec_extradata
[6]);
790 q
->extradata_size
= rmctx
->extradata_size
;
791 if (q
->extradata_size
>= 16){
792 q
->js_subband_start
= rm_get_uint16be(&rmctx
->codec_extradata
[12]);
793 q
->js_vlc_bits
= rm_get_uint16be(&rmctx
->codec_extradata
[14]);
796 /* Take data from the RMContext (RM container). */
797 q
->sample_rate
= rmctx
->sample_rate
;
798 q
->nb_channels
= rmctx
->nb_channels
;
799 q
->bit_rate
= rmctx
->bit_rate
;
801 /* Initialize RNG. */
804 /* Initialize extradata related variables. */
805 q
->samples_per_channel
= q
->samples_per_frame
>> (q
->nb_channels
-1);
806 q
->bits_per_subpacket
= rmctx
->block_align
* 8;
808 /* Initialize default data states. */
809 q
->log2_numvector_size
= 5;
810 q
->total_subbands
= q
->subbands
;
812 /* Initialize version-dependent variables */
813 DEBUGF("q->cookversion=%x\n",q
->cookversion
);
815 switch (q
->cookversion
) {
817 if (q
->nb_channels
!= 1) {
818 DEBUGF("Container channels != 1, report sample!\n");
824 if (q
->nb_channels
!= 1) {
825 q
->bits_per_subpacket
= q
->bits_per_subpacket
/2;
830 if (q
->nb_channels
!= 2) {
831 DEBUGF("Container channels != 2, report sample!\n");
834 DEBUGF("JOINT_STEREO\n");
835 if (q
->extradata_size
>= 16){
836 q
->total_subbands
= q
->subbands
+ q
->js_subband_start
;
839 if (q
->samples_per_channel
> 256) {
840 q
->log2_numvector_size
= 6;
842 if (q
->samples_per_channel
> 512) {
843 q
->log2_numvector_size
= 7;
847 DEBUGF("MC_COOK not supported!\n");
851 DEBUGF("Unknown Cook version, report sample!\n");
856 /* Initialize variable relations */
857 q
->numvector_size
= (1 << q
->log2_numvector_size
);
858 q
->mdct_nbits
= av_log2(q
->samples_per_channel
)+1;
860 /* Generate tables */
861 if (init_cook_vlc_tables(q
) != 0)
865 if(rmctx
->block_align
>= UINT16_MAX
/2)
868 q
->gains1
.now
= q
->gain_1
;
869 q
->gains1
.previous
= q
->gain_2
;
870 q
->gains2
.now
= q
->gain_3
;
871 q
->gains2
.previous
= q
->gain_4
;
874 /* Initialize COOK signal arithmetic handling */
877 q->scalar_dequant = scalar_dequant_math;
878 q->interpolate = interpolate_math;
882 /* Try to catch some obviously faulty streams, othervise it might be exploitable */
883 if (q
->total_subbands
> 53) {
884 DEBUGF("total_subbands > 53, report sample!\n");
887 if (q
->subbands
> 50) {
888 DEBUGF("subbands > 50, report sample!\n");
891 if ((q
->samples_per_channel
== 256) || (q
->samples_per_channel
== 512) || (q
->samples_per_channel
== 1024)) {
893 DEBUGF("unknown amount of samples_per_channel = %d, report sample!\n",q
->samples_per_channel
);
896 if ((q
->js_vlc_bits
> 6) || (q
->js_vlc_bits
< 0)) {
897 DEBUGF("q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q
->js_vlc_bits
);
903 dump_cook_context(q
);