Libav 0.7.1
|
00001 /* 00002 * COOK compatible decoder 00003 * Copyright (c) 2003 Sascha Sommer 00004 * Copyright (c) 2005 Benjamin Larsson 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00045 #include <math.h> 00046 #include <stddef.h> 00047 #include <stdio.h> 00048 00049 #include "libavutil/lfg.h" 00050 #include "libavutil/random_seed.h" 00051 #include "avcodec.h" 00052 #include "get_bits.h" 00053 #include "dsputil.h" 00054 #include "bytestream.h" 00055 #include "fft.h" 00056 #include "libavutil/audioconvert.h" 00057 #include "sinewin.h" 00058 00059 #include "cookdata.h" 00060 00061 /* the different Cook versions */ 00062 #define MONO 0x1000001 00063 #define STEREO 0x1000002 00064 #define JOINT_STEREO 0x1000003 00065 #define MC_COOK 0x2000000 //multichannel Cook, not supported 00066 00067 #define SUBBAND_SIZE 20 00068 #define MAX_SUBPACKETS 5 00069 00070 typedef struct { 00071 int *now; 00072 int *previous; 00073 } cook_gains; 00074 00075 typedef struct { 00076 int ch_idx; 00077 int size; 00078 int num_channels; 00079 int cookversion; 00080 int samples_per_frame; 00081 int subbands; 00082 int js_subband_start; 00083 int js_vlc_bits; 00084 int samples_per_channel; 00085 int log2_numvector_size; 00086 unsigned int channel_mask; 00087 VLC ccpl; 00088 int joint_stereo; 00089 int bits_per_subpacket; 00090 int bits_per_subpdiv; 00091 int total_subbands; 00092 int numvector_size; 00093 00094 float mono_previous_buffer1[1024]; 00095 float mono_previous_buffer2[1024]; 00097 cook_gains gains1; 00098 cook_gains gains2; 00099 int gain_1[9]; 00100 int gain_2[9]; 00101 int gain_3[9]; 00102 int gain_4[9]; 00103 } COOKSubpacket; 00104 00105 typedef struct cook { 00106 /* 00107 * The following 5 functions provide the lowlevel arithmetic on 00108 * the internal audio buffers. 00109 */ 00110 void (* scalar_dequant)(struct cook *q, int index, int quant_index, 00111 int* subband_coef_index, int* subband_coef_sign, 00112 float* mlt_p); 00113 00114 void (* decouple) (struct cook *q, 00115 COOKSubpacket *p, 00116 int subband, 00117 float f1, float f2, 00118 float *decode_buffer, 00119 float *mlt_buffer1, float *mlt_buffer2); 00120 00121 void (* imlt_window) (struct cook *q, float *buffer1, 00122 cook_gains *gains_ptr, float *previous_buffer); 00123 00124 void (* interpolate) (struct cook *q, float* buffer, 00125 int gain_index, int gain_index_next); 00126 00127 void (* saturate_output) (struct cook *q, int chan, int16_t *out); 00128 00129 AVCodecContext* avctx; 00130 GetBitContext gb; 00131 /* stream data */ 00132 int nb_channels; 00133 int bit_rate; 00134 int sample_rate; 00135 int num_vectors; 00136 int samples_per_channel; 00137 /* states */ 00138 AVLFG random_state; 00139 00140 /* transform data */ 00141 FFTContext mdct_ctx; 00142 float* mlt_window; 00143 00144 /* VLC data */ 00145 VLC envelope_quant_index[13]; 00146 VLC sqvh[7]; //scalar quantization 00147 00148 /* generatable tables and related variables */ 00149 int gain_size_factor; 00150 float gain_table[23]; 00151 00152 /* data buffers */ 00153 00154 uint8_t* decoded_bytes_buffer; 00155 DECLARE_ALIGNED(32, float, mono_mdct_output)[2048]; 00156 float decode_buffer_1[1024]; 00157 float decode_buffer_2[1024]; 00158 float decode_buffer_0[1060]; /* static allocation for joint decode */ 00159 00160 const float *cplscales[5]; 00161 int num_subpackets; 00162 COOKSubpacket subpacket[MAX_SUBPACKETS]; 00163 } COOKContext; 00164 00165 static float pow2tab[127]; 00166 static float rootpow2tab[127]; 00167 00168 /*************** init functions ***************/ 00169 00170 /* table generator */ 00171 static av_cold void init_pow2table(void){ 00172 int i; 00173 for (i=-63 ; i<64 ; i++){ 00174 pow2tab[63+i]= pow(2, i); 00175 rootpow2tab[63+i]=sqrt(pow(2, i)); 00176 } 00177 } 00178 00179 /* table generator */ 00180 static av_cold void init_gain_table(COOKContext *q) { 00181 int i; 00182 q->gain_size_factor = q->samples_per_channel/8; 00183 for (i=0 ; i<23 ; i++) { 00184 q->gain_table[i] = pow(pow2tab[i+52] , 00185 (1.0/(double)q->gain_size_factor)); 00186 } 00187 } 00188 00189 00190 static av_cold int init_cook_vlc_tables(COOKContext *q) { 00191 int i, result; 00192 00193 result = 0; 00194 for (i=0 ; i<13 ; i++) { 00195 result |= init_vlc (&q->envelope_quant_index[i], 9, 24, 00196 envelope_quant_index_huffbits[i], 1, 1, 00197 envelope_quant_index_huffcodes[i], 2, 2, 0); 00198 } 00199 av_log(q->avctx,AV_LOG_DEBUG,"sqvh VLC init\n"); 00200 for (i=0 ; i<7 ; i++) { 00201 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i], 00202 cvh_huffbits[i], 1, 1, 00203 cvh_huffcodes[i], 2, 2, 0); 00204 } 00205 00206 for(i=0;i<q->num_subpackets;i++){ 00207 if (q->subpacket[i].joint_stereo==1){ 00208 result |= init_vlc (&q->subpacket[i].ccpl, 6, (1<<q->subpacket[i].js_vlc_bits)-1, 00209 ccpl_huffbits[q->subpacket[i].js_vlc_bits-2], 1, 1, 00210 ccpl_huffcodes[q->subpacket[i].js_vlc_bits-2], 2, 2, 0); 00211 av_log(q->avctx,AV_LOG_DEBUG,"subpacket %i Joint-stereo VLC used.\n",i); 00212 } 00213 } 00214 00215 av_log(q->avctx,AV_LOG_DEBUG,"VLC tables initialized.\n"); 00216 return result; 00217 } 00218 00219 static av_cold int init_cook_mlt(COOKContext *q) { 00220 int j; 00221 int mlt_size = q->samples_per_channel; 00222 00223 if ((q->mlt_window = av_malloc(sizeof(float)*mlt_size)) == 0) 00224 return -1; 00225 00226 /* Initialize the MLT window: simple sine window. */ 00227 ff_sine_window_init(q->mlt_window, mlt_size); 00228 for(j=0 ; j<mlt_size ; j++) 00229 q->mlt_window[j] *= sqrt(2.0 / q->samples_per_channel); 00230 00231 /* Initialize the MDCT. */ 00232 if (ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size)+1, 1, 1.0)) { 00233 av_free(q->mlt_window); 00234 return -1; 00235 } 00236 av_log(q->avctx,AV_LOG_DEBUG,"MDCT initialized, order = %d.\n", 00237 av_log2(mlt_size)+1); 00238 00239 return 0; 00240 } 00241 00242 static const float *maybe_reformat_buffer32 (COOKContext *q, const float *ptr, int n) 00243 { 00244 if (1) 00245 return ptr; 00246 } 00247 00248 static av_cold void init_cplscales_table (COOKContext *q) { 00249 int i; 00250 for (i=0;i<5;i++) 00251 q->cplscales[i] = maybe_reformat_buffer32 (q, cplscales[i], (1<<(i+2))-1); 00252 } 00253 00254 /*************** init functions end ***********/ 00255 00256 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4) 00257 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes))) 00258 00280 static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){ 00281 int i, off; 00282 uint32_t c; 00283 const uint32_t* buf; 00284 uint32_t* obuf = (uint32_t*) out; 00285 /* FIXME: 64 bit platforms would be able to do 64 bits at a time. 00286 * I'm too lazy though, should be something like 00287 * for(i=0 ; i<bitamount/64 ; i++) 00288 * (int64_t)out[i] = 0x37c511f237c511f2^av_be2ne64(int64_t)in[i]); 00289 * Buffer alignment needs to be checked. */ 00290 00291 off = (intptr_t)inbuffer & 3; 00292 buf = (const uint32_t*) (inbuffer - off); 00293 c = av_be2ne32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8)))); 00294 bytes += 3 + off; 00295 for (i = 0; i < bytes/4; i++) 00296 obuf[i] = c ^ buf[i]; 00297 00298 return off; 00299 } 00300 00305 static av_cold int cook_decode_close(AVCodecContext *avctx) 00306 { 00307 int i; 00308 COOKContext *q = avctx->priv_data; 00309 av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n"); 00310 00311 /* Free allocated memory buffers. */ 00312 av_free(q->mlt_window); 00313 av_free(q->decoded_bytes_buffer); 00314 00315 /* Free the transform. */ 00316 ff_mdct_end(&q->mdct_ctx); 00317 00318 /* Free the VLC tables. */ 00319 for (i=0 ; i<13 ; i++) { 00320 free_vlc(&q->envelope_quant_index[i]); 00321 } 00322 for (i=0 ; i<7 ; i++) { 00323 free_vlc(&q->sqvh[i]); 00324 } 00325 for (i=0 ; i<q->num_subpackets ; i++) { 00326 free_vlc(&q->subpacket[i].ccpl); 00327 } 00328 00329 av_log(avctx,AV_LOG_DEBUG,"Memory deallocated.\n"); 00330 00331 return 0; 00332 } 00333 00341 static void decode_gain_info(GetBitContext *gb, int *gaininfo) 00342 { 00343 int i, n; 00344 00345 while (get_bits1(gb)) {} 00346 n = get_bits_count(gb) - 1; //amount of elements*2 to update 00347 00348 i = 0; 00349 while (n--) { 00350 int index = get_bits(gb, 3); 00351 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1; 00352 00353 while (i <= index) gaininfo[i++] = gain; 00354 } 00355 while (i <= 8) gaininfo[i++] = 0; 00356 } 00357 00365 static void decode_envelope(COOKContext *q, COOKSubpacket *p, int* quant_index_table) { 00366 int i,j, vlc_index; 00367 00368 quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize 00369 00370 for (i=1 ; i < p->total_subbands ; i++){ 00371 vlc_index=i; 00372 if (i >= p->js_subband_start * 2) { 00373 vlc_index-=p->js_subband_start; 00374 } else { 00375 vlc_index/=2; 00376 if(vlc_index < 1) vlc_index = 1; 00377 } 00378 if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13 00379 00380 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table, 00381 q->envelope_quant_index[vlc_index-1].bits,2); 00382 quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding 00383 } 00384 } 00385 00395 static void categorize(COOKContext *q, COOKSubpacket *p, int* quant_index_table, 00396 int* category, int* category_index){ 00397 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j; 00398 int exp_index2[102]; 00399 int exp_index1[102]; 00400 00401 int tmp_categorize_array[128*2]; 00402 int tmp_categorize_array1_idx=p->numvector_size; 00403 int tmp_categorize_array2_idx=p->numvector_size; 00404 00405 bits_left = p->bits_per_subpacket - get_bits_count(&q->gb); 00406 00407 if(bits_left > q->samples_per_channel) { 00408 bits_left = q->samples_per_channel + 00409 ((bits_left - q->samples_per_channel)*5)/8; 00410 //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left); 00411 } 00412 00413 memset(&exp_index1,0,102*sizeof(int)); 00414 memset(&exp_index2,0,102*sizeof(int)); 00415 memset(&tmp_categorize_array,0,128*2*sizeof(int)); 00416 00417 bias=-32; 00418 00419 /* Estimate bias. */ 00420 for (i=32 ; i>0 ; i=i/2){ 00421 num_bits = 0; 00422 index = 0; 00423 for (j=p->total_subbands ; j>0 ; j--){ 00424 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7); 00425 index++; 00426 num_bits+=expbits_tab[exp_idx]; 00427 } 00428 if(num_bits >= bits_left - 32){ 00429 bias+=i; 00430 } 00431 } 00432 00433 /* Calculate total number of bits. */ 00434 num_bits=0; 00435 for (i=0 ; i<p->total_subbands ; i++) { 00436 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7); 00437 num_bits += expbits_tab[exp_idx]; 00438 exp_index1[i] = exp_idx; 00439 exp_index2[i] = exp_idx; 00440 } 00441 tmpbias1 = tmpbias2 = num_bits; 00442 00443 for (j = 1 ; j < p->numvector_size ; j++) { 00444 if (tmpbias1 + tmpbias2 > 2*bits_left) { /* ---> */ 00445 int max = -999999; 00446 index=-1; 00447 for (i=0 ; i<p->total_subbands ; i++){ 00448 if (exp_index1[i] < 7) { 00449 v = (-2*exp_index1[i]) - quant_index_table[i] + bias; 00450 if ( v >= max) { 00451 max = v; 00452 index = i; 00453 } 00454 } 00455 } 00456 if(index==-1)break; 00457 tmp_categorize_array[tmp_categorize_array1_idx++] = index; 00458 tmpbias1 -= expbits_tab[exp_index1[index]] - 00459 expbits_tab[exp_index1[index]+1]; 00460 ++exp_index1[index]; 00461 } else { /* <--- */ 00462 int min = 999999; 00463 index=-1; 00464 for (i=0 ; i<p->total_subbands ; i++){ 00465 if(exp_index2[i] > 0){ 00466 v = (-2*exp_index2[i])-quant_index_table[i]+bias; 00467 if ( v < min) { 00468 min = v; 00469 index = i; 00470 } 00471 } 00472 } 00473 if(index == -1)break; 00474 tmp_categorize_array[--tmp_categorize_array2_idx] = index; 00475 tmpbias2 -= expbits_tab[exp_index2[index]] - 00476 expbits_tab[exp_index2[index]-1]; 00477 --exp_index2[index]; 00478 } 00479 } 00480 00481 for(i=0 ; i<p->total_subbands ; i++) 00482 category[i] = exp_index2[i]; 00483 00484 for(i=0 ; i<p->numvector_size-1 ; i++) 00485 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++]; 00486 00487 } 00488 00489 00498 static inline void expand_category(COOKContext *q, int* category, 00499 int* category_index){ 00500 int i; 00501 for(i=0 ; i<q->num_vectors ; i++){ 00502 ++category[category_index[i]]; 00503 } 00504 } 00505 00517 static void scalar_dequant_float(COOKContext *q, int index, int quant_index, 00518 int* subband_coef_index, int* subband_coef_sign, 00519 float* mlt_p){ 00520 int i; 00521 float f1; 00522 00523 for(i=0 ; i<SUBBAND_SIZE ; i++) { 00524 if (subband_coef_index[i]) { 00525 f1 = quant_centroid_tab[index][subband_coef_index[i]]; 00526 if (subband_coef_sign[i]) f1 = -f1; 00527 } else { 00528 /* noise coding if subband_coef_index[i] == 0 */ 00529 f1 = dither_tab[index]; 00530 if (av_lfg_get(&q->random_state) < 0x80000000) f1 = -f1; 00531 } 00532 mlt_p[i] = f1 * rootpow2tab[quant_index+63]; 00533 } 00534 } 00544 static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category, int* subband_coef_index, 00545 int* subband_coef_sign) { 00546 int i,j; 00547 int vlc, vd ,tmp, result; 00548 00549 vd = vd_tab[category]; 00550 result = 0; 00551 for(i=0 ; i<vpr_tab[category] ; i++){ 00552 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3); 00553 if (p->bits_per_subpacket < get_bits_count(&q->gb)){ 00554 vlc = 0; 00555 result = 1; 00556 } 00557 for(j=vd-1 ; j>=0 ; j--){ 00558 tmp = (vlc * invradix_tab[category])/0x100000; 00559 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1); 00560 vlc = tmp; 00561 } 00562 for(j=0 ; j<vd ; j++){ 00563 if (subband_coef_index[i*vd + j]) { 00564 if(get_bits_count(&q->gb) < p->bits_per_subpacket){ 00565 subband_coef_sign[i*vd+j] = get_bits1(&q->gb); 00566 } else { 00567 result=1; 00568 subband_coef_sign[i*vd+j]=0; 00569 } 00570 } else { 00571 subband_coef_sign[i*vd+j]=0; 00572 } 00573 } 00574 } 00575 return result; 00576 } 00577 00578 00589 static void decode_vectors(COOKContext* q, COOKSubpacket* p, int* category, 00590 int *quant_index_table, float* mlt_buffer){ 00591 /* A zero in this table means that the subband coefficient is 00592 random noise coded. */ 00593 int subband_coef_index[SUBBAND_SIZE]; 00594 /* A zero in this table means that the subband coefficient is a 00595 positive multiplicator. */ 00596 int subband_coef_sign[SUBBAND_SIZE]; 00597 int band, j; 00598 int index=0; 00599 00600 for(band=0 ; band<p->total_subbands ; band++){ 00601 index = category[band]; 00602 if(category[band] < 7){ 00603 if(unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)){ 00604 index=7; 00605 for(j=0 ; j<p->total_subbands ; j++) category[band+j]=7; 00606 } 00607 } 00608 if(index>=7) { 00609 memset(subband_coef_index, 0, sizeof(subband_coef_index)); 00610 memset(subband_coef_sign, 0, sizeof(subband_coef_sign)); 00611 } 00612 q->scalar_dequant(q, index, quant_index_table[band], 00613 subband_coef_index, subband_coef_sign, 00614 &mlt_buffer[band * SUBBAND_SIZE]); 00615 } 00616 00617 if(p->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){ 00618 return; 00619 } /* FIXME: should this be removed, or moved into loop above? */ 00620 } 00621 00622 00630 static void mono_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer) { 00631 00632 int category_index[128]; 00633 int quant_index_table[102]; 00634 int category[128]; 00635 00636 memset(&category, 0, 128*sizeof(int)); 00637 memset(&category_index, 0, 128*sizeof(int)); 00638 00639 decode_envelope(q, p, quant_index_table); 00640 q->num_vectors = get_bits(&q->gb,p->log2_numvector_size); 00641 categorize(q, p, quant_index_table, category, category_index); 00642 expand_category(q, category, category_index); 00643 decode_vectors(q, p, category, quant_index_table, mlt_buffer); 00644 } 00645 00646 00656 static void interpolate_float(COOKContext *q, float* buffer, 00657 int gain_index, int gain_index_next){ 00658 int i; 00659 float fc1, fc2; 00660 fc1 = pow2tab[gain_index+63]; 00661 00662 if(gain_index == gain_index_next){ //static gain 00663 for(i=0 ; i<q->gain_size_factor ; i++){ 00664 buffer[i]*=fc1; 00665 } 00666 return; 00667 } else { //smooth gain 00668 fc2 = q->gain_table[11 + (gain_index_next-gain_index)]; 00669 for(i=0 ; i<q->gain_size_factor ; i++){ 00670 buffer[i]*=fc1; 00671 fc1*=fc2; 00672 } 00673 return; 00674 } 00675 } 00676 00686 static void imlt_window_float (COOKContext *q, float *inbuffer, 00687 cook_gains *gains_ptr, float *previous_buffer) 00688 { 00689 const float fc = pow2tab[gains_ptr->previous[0] + 63]; 00690 int i; 00691 /* The weird thing here, is that the two halves of the time domain 00692 * buffer are swapped. Also, the newest data, that we save away for 00693 * next frame, has the wrong sign. Hence the subtraction below. 00694 * Almost sounds like a complex conjugate/reverse data/FFT effect. 00695 */ 00696 00697 /* Apply window and overlap */ 00698 for(i = 0; i < q->samples_per_channel; i++){ 00699 inbuffer[i] = inbuffer[i] * fc * q->mlt_window[i] - 00700 previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i]; 00701 } 00702 } 00703 00716 static void imlt_gain(COOKContext *q, float *inbuffer, 00717 cook_gains *gains_ptr, float* previous_buffer) 00718 { 00719 float *buffer0 = q->mono_mdct_output; 00720 float *buffer1 = q->mono_mdct_output + q->samples_per_channel; 00721 int i; 00722 00723 /* Inverse modified discrete cosine transform */ 00724 q->mdct_ctx.imdct_calc(&q->mdct_ctx, q->mono_mdct_output, inbuffer); 00725 00726 q->imlt_window (q, buffer1, gains_ptr, previous_buffer); 00727 00728 /* Apply gain profile */ 00729 for (i = 0; i < 8; i++) { 00730 if (gains_ptr->now[i] || gains_ptr->now[i + 1]) 00731 q->interpolate(q, &buffer1[q->gain_size_factor * i], 00732 gains_ptr->now[i], gains_ptr->now[i + 1]); 00733 } 00734 00735 /* Save away the current to be previous block. */ 00736 memcpy(previous_buffer, buffer0, sizeof(float)*q->samples_per_channel); 00737 } 00738 00739 00748 static void decouple_info(COOKContext *q, COOKSubpacket *p, int* decouple_tab){ 00749 int length, i; 00750 00751 if(get_bits1(&q->gb)) { 00752 if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return; 00753 00754 length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1; 00755 for (i=0 ; i<length ; i++) { 00756 decouple_tab[cplband[p->js_subband_start] + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2); 00757 } 00758 return; 00759 } 00760 00761 if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return; 00762 00763 length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1; 00764 for (i=0 ; i<length ; i++) { 00765 decouple_tab[cplband[p->js_subband_start] + i] = get_bits(&q->gb, p->js_vlc_bits); 00766 } 00767 return; 00768 } 00769 00770 /* 00771 * function decouples a pair of signals from a single signal via multiplication. 00772 * 00773 * @param q pointer to the COOKContext 00774 * @param subband index of the current subband 00775 * @param f1 multiplier for channel 1 extraction 00776 * @param f2 multiplier for channel 2 extraction 00777 * @param decode_buffer input buffer 00778 * @param mlt_buffer1 pointer to left channel mlt coefficients 00779 * @param mlt_buffer2 pointer to right channel mlt coefficients 00780 */ 00781 static void decouple_float (COOKContext *q, 00782 COOKSubpacket *p, 00783 int subband, 00784 float f1, float f2, 00785 float *decode_buffer, 00786 float *mlt_buffer1, float *mlt_buffer2) 00787 { 00788 int j, tmp_idx; 00789 for (j=0 ; j<SUBBAND_SIZE ; j++) { 00790 tmp_idx = ((p->js_subband_start + subband)*SUBBAND_SIZE)+j; 00791 mlt_buffer1[SUBBAND_SIZE*subband + j] = f1 * decode_buffer[tmp_idx]; 00792 mlt_buffer2[SUBBAND_SIZE*subband + j] = f2 * decode_buffer[tmp_idx]; 00793 } 00794 } 00795 00804 static void joint_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer1, 00805 float* mlt_buffer2) { 00806 int i,j; 00807 int decouple_tab[SUBBAND_SIZE]; 00808 float *decode_buffer = q->decode_buffer_0; 00809 int idx, cpl_tmp; 00810 float f1,f2; 00811 const float* cplscale; 00812 00813 memset(decouple_tab, 0, sizeof(decouple_tab)); 00814 memset(decode_buffer, 0, sizeof(decode_buffer)); 00815 00816 /* Make sure the buffers are zeroed out. */ 00817 memset(mlt_buffer1,0, 1024*sizeof(float)); 00818 memset(mlt_buffer2,0, 1024*sizeof(float)); 00819 decouple_info(q, p, decouple_tab); 00820 mono_decode(q, p, decode_buffer); 00821 00822 /* The two channels are stored interleaved in decode_buffer. */ 00823 for (i=0 ; i<p->js_subband_start ; i++) { 00824 for (j=0 ; j<SUBBAND_SIZE ; j++) { 00825 mlt_buffer1[i*20+j] = decode_buffer[i*40+j]; 00826 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j]; 00827 } 00828 } 00829 00830 /* When we reach js_subband_start (the higher frequencies) 00831 the coefficients are stored in a coupling scheme. */ 00832 idx = (1 << p->js_vlc_bits) - 1; 00833 for (i=p->js_subband_start ; i<p->subbands ; i++) { 00834 cpl_tmp = cplband[i]; 00835 idx -=decouple_tab[cpl_tmp]; 00836 cplscale = q->cplscales[p->js_vlc_bits-2]; //choose decoupler table 00837 f1 = cplscale[decouple_tab[cpl_tmp]]; 00838 f2 = cplscale[idx-1]; 00839 q->decouple (q, p, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2); 00840 idx = (1 << p->js_vlc_bits) - 1; 00841 } 00842 } 00843 00853 static inline void 00854 decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer, 00855 cook_gains *gains_ptr) 00856 { 00857 int offset; 00858 00859 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer, 00860 p->bits_per_subpacket/8); 00861 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset, 00862 p->bits_per_subpacket); 00863 decode_gain_info(&q->gb, gains_ptr->now); 00864 00865 /* Swap current and previous gains */ 00866 FFSWAP(int *, gains_ptr->now, gains_ptr->previous); 00867 } 00868 00876 static void 00877 saturate_output_float (COOKContext *q, int chan, int16_t *out) 00878 { 00879 int j; 00880 float *output = q->mono_mdct_output + q->samples_per_channel; 00881 /* Clip and convert floats to 16 bits. 00882 */ 00883 for (j = 0; j < q->samples_per_channel; j++) { 00884 out[chan + q->nb_channels * j] = 00885 av_clip_int16(lrintf(output[j])); 00886 } 00887 } 00888 00902 static inline void 00903 mlt_compensate_output(COOKContext *q, float *decode_buffer, 00904 cook_gains *gains_ptr, float *previous_buffer, 00905 int16_t *out, int chan) 00906 { 00907 imlt_gain(q, decode_buffer, gains_ptr, previous_buffer); 00908 q->saturate_output (q, chan, out); 00909 } 00910 00911 00920 static void decode_subpacket(COOKContext *q, COOKSubpacket* p, const uint8_t *inbuffer, int16_t *outbuffer) { 00921 int sub_packet_size = p->size; 00922 /* packet dump */ 00923 // for (i=0 ; i<sub_packet_size ; i++) { 00924 // av_log(q->avctx, AV_LOG_ERROR, "%02x", inbuffer[i]); 00925 // } 00926 // av_log(q->avctx, AV_LOG_ERROR, "\n"); 00927 memset(q->decode_buffer_1,0,sizeof(q->decode_buffer_1)); 00928 decode_bytes_and_gain(q, p, inbuffer, &p->gains1); 00929 00930 if (p->joint_stereo) { 00931 joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2); 00932 } else { 00933 mono_decode(q, p, q->decode_buffer_1); 00934 00935 if (p->num_channels == 2) { 00936 decode_bytes_and_gain(q, p, inbuffer + sub_packet_size/2, &p->gains2); 00937 mono_decode(q, p, q->decode_buffer_2); 00938 } 00939 } 00940 00941 mlt_compensate_output(q, q->decode_buffer_1, &p->gains1, 00942 p->mono_previous_buffer1, outbuffer, p->ch_idx); 00943 00944 if (p->num_channels == 2) { 00945 if (p->joint_stereo) { 00946 mlt_compensate_output(q, q->decode_buffer_2, &p->gains1, 00947 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1); 00948 } else { 00949 mlt_compensate_output(q, q->decode_buffer_2, &p->gains2, 00950 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1); 00951 } 00952 } 00953 00954 } 00955 00956 00963 static int cook_decode_frame(AVCodecContext *avctx, 00964 void *data, int *data_size, 00965 AVPacket *avpkt) { 00966 const uint8_t *buf = avpkt->data; 00967 int buf_size = avpkt->size; 00968 COOKContext *q = avctx->priv_data; 00969 int i; 00970 int offset = 0; 00971 int chidx = 0; 00972 00973 if (buf_size < avctx->block_align) 00974 return buf_size; 00975 00976 /* estimate subpacket sizes */ 00977 q->subpacket[0].size = avctx->block_align; 00978 00979 for(i=1;i<q->num_subpackets;i++){ 00980 q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i]; 00981 q->subpacket[0].size -= q->subpacket[i].size + 1; 00982 if (q->subpacket[0].size < 0) { 00983 av_log(avctx,AV_LOG_DEBUG,"frame subpacket size total > avctx->block_align!\n"); 00984 return -1; 00985 } 00986 } 00987 00988 /* decode supbackets */ 00989 *data_size = 0; 00990 for(i=0;i<q->num_subpackets;i++){ 00991 q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size*8)>>q->subpacket[i].bits_per_subpdiv; 00992 q->subpacket[i].ch_idx = chidx; 00993 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] size %i js %i %i block_align %i\n",i,q->subpacket[i].size,q->subpacket[i].joint_stereo,offset,avctx->block_align); 00994 decode_subpacket(q, &q->subpacket[i], buf + offset, (int16_t*)data); 00995 offset += q->subpacket[i].size; 00996 chidx += q->subpacket[i].num_channels; 00997 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] %i %i\n",i,q->subpacket[i].size * 8,get_bits_count(&q->gb)); 00998 } 00999 *data_size = sizeof(int16_t) * q->nb_channels * q->samples_per_channel; 01000 01001 /* Discard the first two frames: no valid audio. */ 01002 if (avctx->frame_number < 2) *data_size = 0; 01003 01004 return avctx->block_align; 01005 } 01006 01007 #ifdef DEBUG 01008 static void dump_cook_context(COOKContext *q) 01009 { 01010 //int i=0; 01011 #define PRINT(a,b) av_log(q->avctx,AV_LOG_ERROR," %s = %d\n", a, b); 01012 av_log(q->avctx,AV_LOG_ERROR,"COOKextradata\n"); 01013 av_log(q->avctx,AV_LOG_ERROR,"cookversion=%x\n",q->subpacket[0].cookversion); 01014 if (q->subpacket[0].cookversion > STEREO) { 01015 PRINT("js_subband_start",q->subpacket[0].js_subband_start); 01016 PRINT("js_vlc_bits",q->subpacket[0].js_vlc_bits); 01017 } 01018 av_log(q->avctx,AV_LOG_ERROR,"COOKContext\n"); 01019 PRINT("nb_channels",q->nb_channels); 01020 PRINT("bit_rate",q->bit_rate); 01021 PRINT("sample_rate",q->sample_rate); 01022 PRINT("samples_per_channel",q->subpacket[0].samples_per_channel); 01023 PRINT("samples_per_frame",q->subpacket[0].samples_per_frame); 01024 PRINT("subbands",q->subpacket[0].subbands); 01025 PRINT("js_subband_start",q->subpacket[0].js_subband_start); 01026 PRINT("log2_numvector_size",q->subpacket[0].log2_numvector_size); 01027 PRINT("numvector_size",q->subpacket[0].numvector_size); 01028 PRINT("total_subbands",q->subpacket[0].total_subbands); 01029 } 01030 #endif 01031 01032 static av_cold int cook_count_channels(unsigned int mask){ 01033 int i; 01034 int channels = 0; 01035 for(i = 0;i<32;i++){ 01036 if(mask & (1<<i)) 01037 ++channels; 01038 } 01039 return channels; 01040 } 01041 01048 static av_cold int cook_decode_init(AVCodecContext *avctx) 01049 { 01050 COOKContext *q = avctx->priv_data; 01051 const uint8_t *edata_ptr = avctx->extradata; 01052 const uint8_t *edata_ptr_end = edata_ptr + avctx->extradata_size; 01053 int extradata_size = avctx->extradata_size; 01054 int s = 0; 01055 unsigned int channel_mask = 0; 01056 q->avctx = avctx; 01057 01058 /* Take care of the codec specific extradata. */ 01059 if (extradata_size <= 0) { 01060 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n"); 01061 return -1; 01062 } 01063 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size); 01064 01065 /* Take data from the AVCodecContext (RM container). */ 01066 q->sample_rate = avctx->sample_rate; 01067 q->nb_channels = avctx->channels; 01068 q->bit_rate = avctx->bit_rate; 01069 01070 /* Initialize RNG. */ 01071 av_lfg_init(&q->random_state, 0); 01072 01073 while(edata_ptr < edata_ptr_end){ 01074 /* 8 for mono, 16 for stereo, ? for multichannel 01075 Swap to right endianness so we don't need to care later on. */ 01076 if (extradata_size >= 8){ 01077 q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr); 01078 q->subpacket[s].samples_per_frame = bytestream_get_be16(&edata_ptr); 01079 q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr); 01080 extradata_size -= 8; 01081 } 01082 if (avctx->extradata_size >= 8){ 01083 bytestream_get_be32(&edata_ptr); //Unknown unused 01084 q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr); 01085 q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr); 01086 extradata_size -= 8; 01087 } 01088 01089 /* Initialize extradata related variables. */ 01090 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame / q->nb_channels; 01091 q->subpacket[s].bits_per_subpacket = avctx->block_align * 8; 01092 01093 /* Initialize default data states. */ 01094 q->subpacket[s].log2_numvector_size = 5; 01095 q->subpacket[s].total_subbands = q->subpacket[s].subbands; 01096 q->subpacket[s].num_channels = 1; 01097 01098 /* Initialize version-dependent variables */ 01099 01100 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i].cookversion=%x\n",s,q->subpacket[s].cookversion); 01101 q->subpacket[s].joint_stereo = 0; 01102 switch (q->subpacket[s].cookversion) { 01103 case MONO: 01104 if (q->nb_channels != 1) { 01105 av_log_ask_for_sample(avctx, "Container channels != 1.\n"); 01106 return -1; 01107 } 01108 av_log(avctx,AV_LOG_DEBUG,"MONO\n"); 01109 break; 01110 case STEREO: 01111 if (q->nb_channels != 1) { 01112 q->subpacket[s].bits_per_subpdiv = 1; 01113 q->subpacket[s].num_channels = 2; 01114 } 01115 av_log(avctx,AV_LOG_DEBUG,"STEREO\n"); 01116 break; 01117 case JOINT_STEREO: 01118 if (q->nb_channels != 2) { 01119 av_log_ask_for_sample(avctx, "Container channels != 2.\n"); 01120 return -1; 01121 } 01122 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n"); 01123 if (avctx->extradata_size >= 16){ 01124 q->subpacket[s].total_subbands = q->subpacket[s].subbands + q->subpacket[s].js_subband_start; 01125 q->subpacket[s].joint_stereo = 1; 01126 q->subpacket[s].num_channels = 2; 01127 } 01128 if (q->subpacket[s].samples_per_channel > 256) { 01129 q->subpacket[s].log2_numvector_size = 6; 01130 } 01131 if (q->subpacket[s].samples_per_channel > 512) { 01132 q->subpacket[s].log2_numvector_size = 7; 01133 } 01134 break; 01135 case MC_COOK: 01136 av_log(avctx,AV_LOG_DEBUG,"MULTI_CHANNEL\n"); 01137 if(extradata_size >= 4) 01138 channel_mask |= q->subpacket[s].channel_mask = bytestream_get_be32(&edata_ptr); 01139 01140 if(cook_count_channels(q->subpacket[s].channel_mask) > 1){ 01141 q->subpacket[s].total_subbands = q->subpacket[s].subbands + q->subpacket[s].js_subband_start; 01142 q->subpacket[s].joint_stereo = 1; 01143 q->subpacket[s].num_channels = 2; 01144 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame >> 1; 01145 01146 if (q->subpacket[s].samples_per_channel > 256) { 01147 q->subpacket[s].log2_numvector_size = 6; 01148 } 01149 if (q->subpacket[s].samples_per_channel > 512) { 01150 q->subpacket[s].log2_numvector_size = 7; 01151 } 01152 }else 01153 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame; 01154 01155 break; 01156 default: 01157 av_log_ask_for_sample(avctx, "Unknown Cook version.\n"); 01158 return -1; 01159 break; 01160 } 01161 01162 if(s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) { 01163 av_log(avctx,AV_LOG_ERROR,"different number of samples per channel!\n"); 01164 return -1; 01165 } else 01166 q->samples_per_channel = q->subpacket[0].samples_per_channel; 01167 01168 01169 /* Initialize variable relations */ 01170 q->subpacket[s].numvector_size = (1 << q->subpacket[s].log2_numvector_size); 01171 01172 /* Try to catch some obviously faulty streams, othervise it might be exploitable */ 01173 if (q->subpacket[s].total_subbands > 53) { 01174 av_log_ask_for_sample(avctx, "total_subbands > 53\n"); 01175 return -1; 01176 } 01177 01178 if ((q->subpacket[s].js_vlc_bits > 6) || (q->subpacket[s].js_vlc_bits < 0)) { 01179 av_log(avctx,AV_LOG_ERROR,"js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->subpacket[s].js_vlc_bits); 01180 return -1; 01181 } 01182 01183 if (q->subpacket[s].subbands > 50) { 01184 av_log_ask_for_sample(avctx, "subbands > 50\n"); 01185 return -1; 01186 } 01187 q->subpacket[s].gains1.now = q->subpacket[s].gain_1; 01188 q->subpacket[s].gains1.previous = q->subpacket[s].gain_2; 01189 q->subpacket[s].gains2.now = q->subpacket[s].gain_3; 01190 q->subpacket[s].gains2.previous = q->subpacket[s].gain_4; 01191 01192 q->num_subpackets++; 01193 s++; 01194 if (s > MAX_SUBPACKETS) { 01195 av_log_ask_for_sample(avctx, "Too many subpackets > 5\n"); 01196 return -1; 01197 } 01198 } 01199 /* Generate tables */ 01200 init_pow2table(); 01201 init_gain_table(q); 01202 init_cplscales_table(q); 01203 01204 if (init_cook_vlc_tables(q) != 0) 01205 return -1; 01206 01207 01208 if(avctx->block_align >= UINT_MAX/2) 01209 return -1; 01210 01211 /* Pad the databuffer with: 01212 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(), 01213 FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */ 01214 q->decoded_bytes_buffer = 01215 av_mallocz(avctx->block_align 01216 + DECODE_BYTES_PAD1(avctx->block_align) 01217 + FF_INPUT_BUFFER_PADDING_SIZE); 01218 if (q->decoded_bytes_buffer == NULL) 01219 return -1; 01220 01221 /* Initialize transform. */ 01222 if ( init_cook_mlt(q) != 0 ) 01223 return -1; 01224 01225 /* Initialize COOK signal arithmetic handling */ 01226 if (1) { 01227 q->scalar_dequant = scalar_dequant_float; 01228 q->decouple = decouple_float; 01229 q->imlt_window = imlt_window_float; 01230 q->interpolate = interpolate_float; 01231 q->saturate_output = saturate_output_float; 01232 } 01233 01234 /* Try to catch some obviously faulty streams, othervise it might be exploitable */ 01235 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) { 01236 } else { 01237 av_log_ask_for_sample(avctx, 01238 "unknown amount of samples_per_channel = %d\n", 01239 q->samples_per_channel); 01240 return -1; 01241 } 01242 01243 avctx->sample_fmt = AV_SAMPLE_FMT_S16; 01244 if (channel_mask) 01245 avctx->channel_layout = channel_mask; 01246 else 01247 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO; 01248 01249 #ifdef DEBUG 01250 dump_cook_context(q); 01251 #endif 01252 return 0; 01253 } 01254 01255 01256 AVCodec ff_cook_decoder = 01257 { 01258 .name = "cook", 01259 .type = AVMEDIA_TYPE_AUDIO, 01260 .id = CODEC_ID_COOK, 01261 .priv_data_size = sizeof(COOKContext), 01262 .init = cook_decode_init, 01263 .close = cook_decode_close, 01264 .decode = cook_decode_frame, 01265 .long_name = NULL_IF_CONFIG_SMALL("COOK"), 01266 };