00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00045 #include <math.h>
00046 #include <stddef.h>
00047 #include <stdio.h>
00048
00049 #include "avcodec.h"
00050 #include "bitstream.h"
00051 #include "dsputil.h"
00052 #include "bytestream.h"
00053 #include "random.h"
00054
00055 #include "cookdata.h"
00056
00057
00058 #define MONO 0x1000001
00059 #define STEREO 0x1000002
00060 #define JOINT_STEREO 0x1000003
00061 #define MC_COOK 0x2000000 //multichannel Cook, not supported
00062
00063 #define SUBBAND_SIZE 20
00064
00065
00066 typedef struct {
00067 int *now;
00068 int *previous;
00069 } cook_gains;
00070
00071 typedef struct cook {
00072
00073
00074
00075
00076 void (* scalar_dequant)(struct cook *q, int index, int quant_index,
00077 int* subband_coef_index, int* subband_coef_sign,
00078 float* mlt_p);
00079
00080 void (* decouple) (struct cook *q,
00081 int subband,
00082 float f1, float f2,
00083 float *decode_buffer,
00084 float *mlt_buffer1, float *mlt_buffer2);
00085
00086 void (* imlt_window) (struct cook *q, float *buffer1,
00087 cook_gains *gains_ptr, float *previous_buffer);
00088
00089 void (* interpolate) (struct cook *q, float* buffer,
00090 int gain_index, int gain_index_next);
00091
00092 void (* saturate_output) (struct cook *q, int chan, int16_t *out);
00093
00094 GetBitContext gb;
00095
00096 int nb_channels;
00097 int joint_stereo;
00098 int bit_rate;
00099 int sample_rate;
00100 int samples_per_channel;
00101 int samples_per_frame;
00102 int subbands;
00103 int log2_numvector_size;
00104 int numvector_size;
00105 int js_subband_start;
00106 int total_subbands;
00107 int num_vectors;
00108 int bits_per_subpacket;
00109 int cookversion;
00110
00111 AVRandomState random_state;
00112
00113
00114 MDCTContext mdct_ctx;
00115 DECLARE_ALIGNED_16(FFTSample, mdct_tmp[1024]);
00116 float* mlt_window;
00117
00118
00119 cook_gains gains1;
00120 cook_gains gains2;
00121 int gain_1[9];
00122 int gain_2[9];
00123 int gain_3[9];
00124 int gain_4[9];
00125
00126
00127 int js_vlc_bits;
00128 VLC envelope_quant_index[13];
00129 VLC sqvh[7];
00130 VLC ccpl;
00131
00132
00133 int gain_size_factor;
00134 float gain_table[23];
00135 float pow2tab[127];
00136 float rootpow2tab[127];
00137
00138
00139
00140 uint8_t* decoded_bytes_buffer;
00141 DECLARE_ALIGNED_16(float,mono_mdct_output[2048]);
00142 float mono_previous_buffer1[1024];
00143 float mono_previous_buffer2[1024];
00144 float decode_buffer_1[1024];
00145 float decode_buffer_2[1024];
00146 float decode_buffer_0[1060];
00147
00148 const float *cplscales[5];
00149 } COOKContext;
00150
00151
00152
00153 #ifdef COOKDEBUG
00154 static void dump_float_table(float* table, int size, int delimiter) {
00155 int i=0;
00156 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
00157 for (i=0 ; i<size ; i++) {
00158 av_log(NULL, AV_LOG_ERROR, "%5.1f, ", table[i]);
00159 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
00160 }
00161 }
00162
00163 static void dump_int_table(int* table, int size, int delimiter) {
00164 int i=0;
00165 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
00166 for (i=0 ; i<size ; i++) {
00167 av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);
00168 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
00169 }
00170 }
00171
00172 static void dump_short_table(short* table, int size, int delimiter) {
00173 int i=0;
00174 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
00175 for (i=0 ; i<size ; i++) {
00176 av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);
00177 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
00178 }
00179 }
00180
00181 #endif
00182
00183
00184
00185
00186 static void init_pow2table(COOKContext *q){
00187 int i;
00188 q->pow2tab[63] = 1.0;
00189 for (i=1 ; i<64 ; i++){
00190 q->pow2tab[63+i]=(float)((uint64_t)1<<i);
00191 q->pow2tab[63-i]=1.0/(float)((uint64_t)1<<i);
00192 }
00193 }
00194
00195
00196 static void init_rootpow2table(COOKContext *q){
00197 int i;
00198 q->rootpow2tab[63] = 1.0;
00199 for (i=1 ; i<64 ; i++){
00200 q->rootpow2tab[63+i]=sqrt((float)((uint64_t)1<<i));
00201 q->rootpow2tab[63-i]=sqrt(1.0/(float)((uint64_t)1<<i));
00202 }
00203 }
00204
00205
00206 static void init_gain_table(COOKContext *q) {
00207 int i;
00208 q->gain_size_factor = q->samples_per_channel/8;
00209 for (i=0 ; i<23 ; i++) {
00210 q->gain_table[i] = pow((double)q->pow2tab[i+52] ,
00211 (1.0/(double)q->gain_size_factor));
00212 }
00213 }
00214
00215
00216 static int init_cook_vlc_tables(COOKContext *q) {
00217 int i, result;
00218
00219 result = 0;
00220 for (i=0 ; i<13 ; i++) {
00221 result |= init_vlc (&q->envelope_quant_index[i], 9, 24,
00222 envelope_quant_index_huffbits[i], 1, 1,
00223 envelope_quant_index_huffcodes[i], 2, 2, 0);
00224 }
00225 av_log(NULL,AV_LOG_DEBUG,"sqvh VLC init\n");
00226 for (i=0 ; i<7 ; i++) {
00227 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
00228 cvh_huffbits[i], 1, 1,
00229 cvh_huffcodes[i], 2, 2, 0);
00230 }
00231
00232 if (q->nb_channels==2 && q->joint_stereo==1){
00233 result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1,
00234 ccpl_huffbits[q->js_vlc_bits-2], 1, 1,
00235 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0);
00236 av_log(NULL,AV_LOG_DEBUG,"Joint-stereo VLC used.\n");
00237 }
00238
00239 av_log(NULL,AV_LOG_DEBUG,"VLC tables initialized.\n");
00240 return result;
00241 }
00242
00243 static int init_cook_mlt(COOKContext *q) {
00244 int j;
00245 float alpha;
00246 int mlt_size = q->samples_per_channel;
00247
00248 if ((q->mlt_window = av_malloc(sizeof(float)*mlt_size)) == 0)
00249 return -1;
00250
00251
00252 alpha = M_PI / (2.0 * (float)mlt_size);
00253 for(j=0 ; j<mlt_size ; j++)
00254 q->mlt_window[j] = sin((j + 0.5) * alpha) * sqrt(2.0 / q->samples_per_channel);
00255
00256
00257 if (ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size)+1, 1)) {
00258 av_free(q->mlt_window);
00259 return -1;
00260 }
00261 av_log(NULL,AV_LOG_DEBUG,"MDCT initialized, order = %d.\n",
00262 av_log2(mlt_size)+1);
00263
00264 return 0;
00265 }
00266
00267 static const float *maybe_reformat_buffer32 (COOKContext *q, const float *ptr, int n)
00268 {
00269 if (1)
00270 return ptr;
00271 }
00272
00273 static void init_cplscales_table (COOKContext *q) {
00274 int i;
00275 for (i=0;i<5;i++)
00276 q->cplscales[i] = maybe_reformat_buffer32 (q, cplscales[i], (1<<(i+2))-1);
00277 }
00278
00279
00280
00301 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)
00302 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
00303
00304 static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
00305 int i, off;
00306 uint32_t c;
00307 const uint32_t* buf;
00308 uint32_t* obuf = (uint32_t*) out;
00309
00310
00311
00312
00313
00314
00315 off = (int)((long)inbuffer & 3);
00316 buf = (const uint32_t*) (inbuffer - off);
00317 c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));
00318 bytes += 3 + off;
00319 for (i = 0; i < bytes/4; i++)
00320 obuf[i] = c ^ buf[i];
00321
00322 return off;
00323 }
00324
00329 static int cook_decode_close(AVCodecContext *avctx)
00330 {
00331 int i;
00332 COOKContext *q = avctx->priv_data;
00333 av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n");
00334
00335
00336 av_free(q->mlt_window);
00337 av_free(q->decoded_bytes_buffer);
00338
00339
00340 ff_mdct_end(&q->mdct_ctx);
00341
00342
00343 for (i=0 ; i<13 ; i++) {
00344 free_vlc(&q->envelope_quant_index[i]);
00345 }
00346 for (i=0 ; i<7 ; i++) {
00347 free_vlc(&q->sqvh[i]);
00348 }
00349 if(q->nb_channels==2 && q->joint_stereo==1 ){
00350 free_vlc(&q->ccpl);
00351 }
00352
00353 av_log(NULL,AV_LOG_DEBUG,"Memory deallocated.\n");
00354
00355 return 0;
00356 }
00357
00365 static void decode_gain_info(GetBitContext *gb, int *gaininfo)
00366 {
00367 int i, n;
00368
00369 while (get_bits1(gb)) {}
00370 n = get_bits_count(gb) - 1;
00371
00372 i = 0;
00373 while (n--) {
00374 int index = get_bits(gb, 3);
00375 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;
00376
00377 while (i <= index) gaininfo[i++] = gain;
00378 }
00379 while (i <= 8) gaininfo[i++] = 0;
00380 }
00381
00389 static void decode_envelope(COOKContext *q, int* quant_index_table) {
00390 int i,j, vlc_index;
00391
00392 quant_index_table[0]= get_bits(&q->gb,6) - 6;
00393
00394 for (i=1 ; i < q->total_subbands ; i++){
00395 vlc_index=i;
00396 if (i >= q->js_subband_start * 2) {
00397 vlc_index-=q->js_subband_start;
00398 } else {
00399 vlc_index/=2;
00400 if(vlc_index < 1) vlc_index = 1;
00401 }
00402 if (vlc_index>13) vlc_index = 13;
00403
00404 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,
00405 q->envelope_quant_index[vlc_index-1].bits,2);
00406 quant_index_table[i] = quant_index_table[i-1] + j - 12;
00407 }
00408 }
00409
00419 static void categorize(COOKContext *q, int* quant_index_table,
00420 int* category, int* category_index){
00421 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
00422 int exp_index2[102];
00423 int exp_index1[102];
00424
00425 int tmp_categorize_array[128*2];
00426 int tmp_categorize_array1_idx=q->numvector_size;
00427 int tmp_categorize_array2_idx=q->numvector_size;
00428
00429 bits_left = q->bits_per_subpacket - get_bits_count(&q->gb);
00430
00431 if(bits_left > q->samples_per_channel) {
00432 bits_left = q->samples_per_channel +
00433 ((bits_left - q->samples_per_channel)*5)/8;
00434
00435 }
00436
00437 memset(&exp_index1,0,102*sizeof(int));
00438 memset(&exp_index2,0,102*sizeof(int));
00439 memset(&tmp_categorize_array,0,128*2*sizeof(int));
00440
00441 bias=-32;
00442
00443
00444 for (i=32 ; i>0 ; i=i/2){
00445 num_bits = 0;
00446 index = 0;
00447 for (j=q->total_subbands ; j>0 ; j--){
00448 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
00449 index++;
00450 num_bits+=expbits_tab[exp_idx];
00451 }
00452 if(num_bits >= bits_left - 32){
00453 bias+=i;
00454 }
00455 }
00456
00457
00458 num_bits=0;
00459 for (i=0 ; i<q->total_subbands ; i++) {
00460 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
00461 num_bits += expbits_tab[exp_idx];
00462 exp_index1[i] = exp_idx;
00463 exp_index2[i] = exp_idx;
00464 }
00465 tmpbias1 = tmpbias2 = num_bits;
00466
00467 for (j = 1 ; j < q->numvector_size ; j++) {
00468 if (tmpbias1 + tmpbias2 > 2*bits_left) {
00469 int max = -999999;
00470 index=-1;
00471 for (i=0 ; i<q->total_subbands ; i++){
00472 if (exp_index1[i] < 7) {
00473 v = (-2*exp_index1[i]) - quant_index_table[i] + bias;
00474 if ( v >= max) {
00475 max = v;
00476 index = i;
00477 }
00478 }
00479 }
00480 if(index==-1)break;
00481 tmp_categorize_array[tmp_categorize_array1_idx++] = index;
00482 tmpbias1 -= expbits_tab[exp_index1[index]] -
00483 expbits_tab[exp_index1[index]+1];
00484 ++exp_index1[index];
00485 } else {
00486 int min = 999999;
00487 index=-1;
00488 for (i=0 ; i<q->total_subbands ; i++){
00489 if(exp_index2[i] > 0){
00490 v = (-2*exp_index2[i])-quant_index_table[i]+bias;
00491 if ( v < min) {
00492 min = v;
00493 index = i;
00494 }
00495 }
00496 }
00497 if(index == -1)break;
00498 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
00499 tmpbias2 -= expbits_tab[exp_index2[index]] -
00500 expbits_tab[exp_index2[index]-1];
00501 --exp_index2[index];
00502 }
00503 }
00504
00505 for(i=0 ; i<q->total_subbands ; i++)
00506 category[i] = exp_index2[i];
00507
00508 for(i=0 ; i<q->numvector_size-1 ; i++)
00509 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
00510
00511 }
00512
00513
00522 static inline void expand_category(COOKContext *q, int* category,
00523 int* category_index){
00524 int i;
00525 for(i=0 ; i<q->num_vectors ; i++){
00526 ++category[category_index[i]];
00527 }
00528 }
00529
00541 static void scalar_dequant_float(COOKContext *q, int index, int quant_index,
00542 int* subband_coef_index, int* subband_coef_sign,
00543 float* mlt_p){
00544 int i;
00545 float f1;
00546
00547 for(i=0 ; i<SUBBAND_SIZE ; i++) {
00548 if (subband_coef_index[i]) {
00549 f1 = quant_centroid_tab[index][subband_coef_index[i]];
00550 if (subband_coef_sign[i]) f1 = -f1;
00551 } else {
00552
00553 f1 = dither_tab[index];
00554 if (av_random(&q->random_state) < 0x80000000) f1 = -f1;
00555 }
00556 mlt_p[i] = f1 * q->rootpow2tab[quant_index+63];
00557 }
00558 }
00568 static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index,
00569 int* subband_coef_sign) {
00570 int i,j;
00571 int vlc, vd ,tmp, result;
00572
00573 vd = vd_tab[category];
00574 result = 0;
00575 for(i=0 ; i<vpr_tab[category] ; i++){
00576 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
00577 if (q->bits_per_subpacket < get_bits_count(&q->gb)){
00578 vlc = 0;
00579 result = 1;
00580 }
00581 for(j=vd-1 ; j>=0 ; j--){
00582 tmp = (vlc * invradix_tab[category])/0x100000;
00583 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1);
00584 vlc = tmp;
00585 }
00586 for(j=0 ; j<vd ; j++){
00587 if (subband_coef_index[i*vd + j]) {
00588 if(get_bits_count(&q->gb) < q->bits_per_subpacket){
00589 subband_coef_sign[i*vd+j] = get_bits1(&q->gb);
00590 } else {
00591 result=1;
00592 subband_coef_sign[i*vd+j]=0;
00593 }
00594 } else {
00595 subband_coef_sign[i*vd+j]=0;
00596 }
00597 }
00598 }
00599 return result;
00600 }
00601
00602
00613 static void decode_vectors(COOKContext* q, int* category,
00614 int *quant_index_table, float* mlt_buffer){
00615
00616
00617 int subband_coef_index[SUBBAND_SIZE];
00618
00619
00620 int subband_coef_sign[SUBBAND_SIZE];
00621 int band, j;
00622 int index=0;
00623
00624 for(band=0 ; band<q->total_subbands ; band++){
00625 index = category[band];
00626 if(category[band] < 7){
00627 if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){
00628 index=7;
00629 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
00630 }
00631 }
00632 if(index==7) {
00633 memset(subband_coef_index, 0, sizeof(subband_coef_index));
00634 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
00635 }
00636 q->scalar_dequant(q, index, quant_index_table[band],
00637 subband_coef_index, subband_coef_sign,
00638 &mlt_buffer[band * SUBBAND_SIZE]);
00639 }
00640
00641 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
00642 return;
00643 }
00644 }
00645
00646
00654 static void mono_decode(COOKContext *q, float* mlt_buffer) {
00655
00656 int category_index[128];
00657 int quant_index_table[102];
00658 int category[128];
00659
00660 memset(&category, 0, 128*sizeof(int));
00661 memset(&category_index, 0, 128*sizeof(int));
00662
00663 decode_envelope(q, quant_index_table);
00664 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
00665 categorize(q, quant_index_table, category, category_index);
00666 expand_category(q, category, category_index);
00667 decode_vectors(q, category, quant_index_table, mlt_buffer);
00668 }
00669
00670
00680 static void interpolate_float(COOKContext *q, float* buffer,
00681 int gain_index, int gain_index_next){
00682 int i;
00683 float fc1, fc2;
00684 fc1 = q->pow2tab[gain_index+63];
00685
00686 if(gain_index == gain_index_next){
00687 for(i=0 ; i<q->gain_size_factor ; i++){
00688 buffer[i]*=fc1;
00689 }
00690 return;
00691 } else {
00692 fc2 = q->gain_table[11 + (gain_index_next-gain_index)];
00693 for(i=0 ; i<q->gain_size_factor ; i++){
00694 buffer[i]*=fc1;
00695 fc1*=fc2;
00696 }
00697 return;
00698 }
00699 }
00700
00710 static void imlt_window_float (COOKContext *q, float *buffer1,
00711 cook_gains *gains_ptr, float *previous_buffer)
00712 {
00713 const float fc = q->pow2tab[gains_ptr->previous[0] + 63];
00714 int i;
00715
00716
00717
00718
00719
00720
00721
00722 for(i = 0; i < q->samples_per_channel; i++){
00723 buffer1[i] = buffer1[i] * fc * q->mlt_window[i] -
00724 previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
00725 }
00726 }
00727
00740 static void imlt_gain(COOKContext *q, float *inbuffer,
00741 cook_gains *gains_ptr, float* previous_buffer)
00742 {
00743 float *buffer0 = q->mono_mdct_output;
00744 float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
00745 int i;
00746
00747
00748 q->mdct_ctx.fft.imdct_calc(&q->mdct_ctx, q->mono_mdct_output,
00749 inbuffer, q->mdct_tmp);
00750
00751 q->imlt_window (q, buffer1, gains_ptr, previous_buffer);
00752
00753
00754 for (i = 0; i < 8; i++) {
00755 if (gains_ptr->now[i] || gains_ptr->now[i + 1])
00756 q->interpolate(q, &buffer1[q->gain_size_factor * i],
00757 gains_ptr->now[i], gains_ptr->now[i + 1]);
00758 }
00759
00760
00761 memcpy(previous_buffer, buffer0, sizeof(float)*q->samples_per_channel);
00762 }
00763
00764
00773 static void decouple_info(COOKContext *q, int* decouple_tab){
00774 int length, i;
00775
00776 if(get_bits1(&q->gb)) {
00777 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
00778
00779 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
00780 for (i=0 ; i<length ; i++) {
00781 decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2);
00782 }
00783 return;
00784 }
00785
00786 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
00787
00788 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
00789 for (i=0 ; i<length ; i++) {
00790 decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits);
00791 }
00792 return;
00793 }
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 static void decouple_float (COOKContext *q,
00807 int subband,
00808 float f1, float f2,
00809 float *decode_buffer,
00810 float *mlt_buffer1, float *mlt_buffer2)
00811 {
00812 int j, tmp_idx;
00813 for (j=0 ; j<SUBBAND_SIZE ; j++) {
00814 tmp_idx = ((q->js_subband_start + subband)*SUBBAND_SIZE)+j;
00815 mlt_buffer1[SUBBAND_SIZE*subband + j] = f1 * decode_buffer[tmp_idx];
00816 mlt_buffer2[SUBBAND_SIZE*subband + j] = f2 * decode_buffer[tmp_idx];
00817 }
00818 }
00819
00828 static void joint_decode(COOKContext *q, float* mlt_buffer1,
00829 float* mlt_buffer2) {
00830 int i,j;
00831 int decouple_tab[SUBBAND_SIZE];
00832 float *decode_buffer = q->decode_buffer_0;
00833 int idx, cpl_tmp;
00834 float f1,f2;
00835 const float* cplscale;
00836
00837 memset(decouple_tab, 0, sizeof(decouple_tab));
00838 memset(decode_buffer, 0, sizeof(decode_buffer));
00839
00840
00841 memset(mlt_buffer1,0, 1024*sizeof(float));
00842 memset(mlt_buffer2,0, 1024*sizeof(float));
00843 decouple_info(q, decouple_tab);
00844 mono_decode(q, decode_buffer);
00845
00846
00847 for (i=0 ; i<q->js_subband_start ; i++) {
00848 for (j=0 ; j<SUBBAND_SIZE ; j++) {
00849 mlt_buffer1[i*20+j] = decode_buffer[i*40+j];
00850 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j];
00851 }
00852 }
00853
00854
00855
00856 idx = (1 << q->js_vlc_bits) - 1;
00857 for (i=q->js_subband_start ; i<q->subbands ; i++) {
00858 cpl_tmp = cplband[i];
00859 idx -=decouple_tab[cpl_tmp];
00860 cplscale = q->cplscales[q->js_vlc_bits-2];
00861 f1 = cplscale[decouple_tab[cpl_tmp]];
00862 f2 = cplscale[idx-1];
00863 q->decouple (q, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2);
00864 idx = (1 << q->js_vlc_bits) - 1;
00865 }
00866 }
00867
00877 static inline void
00878 decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer,
00879 cook_gains *gains_ptr)
00880 {
00881 int offset;
00882
00883 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
00884 q->bits_per_subpacket/8);
00885 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
00886 q->bits_per_subpacket);
00887 decode_gain_info(&q->gb, gains_ptr->now);
00888
00889
00890 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
00891 }
00892
00900 static void
00901 saturate_output_float (COOKContext *q, int chan, int16_t *out)
00902 {
00903 int j;
00904 float *output = q->mono_mdct_output + q->samples_per_channel;
00905
00906
00907 for (j = 0; j < q->samples_per_channel; j++) {
00908 out[chan + q->nb_channels * j] =
00909 av_clip_int16(lrintf(output[j]));
00910 }
00911 }
00912
00926 static inline void
00927 mlt_compensate_output(COOKContext *q, float *decode_buffer,
00928 cook_gains *gains, float *previous_buffer,
00929 int16_t *out, int chan)
00930 {
00931 imlt_gain(q, decode_buffer, gains, previous_buffer);
00932 q->saturate_output (q, chan, out);
00933 }
00934
00935
00947 static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
00948 int sub_packet_size, int16_t *outbuffer) {
00949
00950
00951
00952
00953
00954
00955 decode_bytes_and_gain(q, inbuffer, &q->gains1);
00956
00957 if (q->joint_stereo) {
00958 joint_decode(q, q->decode_buffer_1, q->decode_buffer_2);
00959 } else {
00960 mono_decode(q, q->decode_buffer_1);
00961
00962 if (q->nb_channels == 2) {
00963 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2);
00964 mono_decode(q, q->decode_buffer_2);
00965 }
00966 }
00967
00968 mlt_compensate_output(q, q->decode_buffer_1, &q->gains1,
00969 q->mono_previous_buffer1, outbuffer, 0);
00970
00971 if (q->nb_channels == 2) {
00972 if (q->joint_stereo) {
00973 mlt_compensate_output(q, q->decode_buffer_2, &q->gains1,
00974 q->mono_previous_buffer2, outbuffer, 1);
00975 } else {
00976 mlt_compensate_output(q, q->decode_buffer_2, &q->gains2,
00977 q->mono_previous_buffer2, outbuffer, 1);
00978 }
00979 }
00980 return q->samples_per_frame * sizeof(int16_t);
00981 }
00982
00983
00990 static int cook_decode_frame(AVCodecContext *avctx,
00991 void *data, int *data_size,
00992 const uint8_t *buf, int buf_size) {
00993 COOKContext *q = avctx->priv_data;
00994
00995 if (buf_size < avctx->block_align)
00996 return buf_size;
00997
00998 *data_size = decode_subpacket(q, buf, avctx->block_align, data);
00999
01000
01001 if (avctx->frame_number < 2) *data_size = 0;
01002
01003 return avctx->block_align;
01004 }
01005
01006 #ifdef COOKDEBUG
01007 static void dump_cook_context(COOKContext *q)
01008 {
01009
01010 #define PRINT(a,b) av_log(NULL,AV_LOG_ERROR," %s = %d\n", a, b);
01011 av_log(NULL,AV_LOG_ERROR,"COOKextradata\n");
01012 av_log(NULL,AV_LOG_ERROR,"cookversion=%x\n",q->cookversion);
01013 if (q->cookversion > STEREO) {
01014 PRINT("js_subband_start",q->js_subband_start);
01015 PRINT("js_vlc_bits",q->js_vlc_bits);
01016 }
01017 av_log(NULL,AV_LOG_ERROR,"COOKContext\n");
01018 PRINT("nb_channels",q->nb_channels);
01019 PRINT("bit_rate",q->bit_rate);
01020 PRINT("sample_rate",q->sample_rate);
01021 PRINT("samples_per_channel",q->samples_per_channel);
01022 PRINT("samples_per_frame",q->samples_per_frame);
01023 PRINT("subbands",q->subbands);
01024 PRINT("random_state",q->random_state);
01025 PRINT("js_subband_start",q->js_subband_start);
01026 PRINT("log2_numvector_size",q->log2_numvector_size);
01027 PRINT("numvector_size",q->numvector_size);
01028 PRINT("total_subbands",q->total_subbands);
01029 }
01030 #endif
01031
01038 static int cook_decode_init(AVCodecContext *avctx)
01039 {
01040 COOKContext *q = avctx->priv_data;
01041 const uint8_t *edata_ptr = avctx->extradata;
01042
01043
01044 if (avctx->extradata_size <= 0) {
01045 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n");
01046 return -1;
01047 } else {
01048
01049
01050 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size);
01051 if (avctx->extradata_size >= 8){
01052 q->cookversion = bytestream_get_be32(&edata_ptr);
01053 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
01054 q->subbands = bytestream_get_be16(&edata_ptr);
01055 }
01056 if (avctx->extradata_size >= 16){
01057 bytestream_get_be32(&edata_ptr);
01058 q->js_subband_start = bytestream_get_be16(&edata_ptr);
01059 q->js_vlc_bits = bytestream_get_be16(&edata_ptr);
01060 }
01061 }
01062
01063
01064 q->sample_rate = avctx->sample_rate;
01065 q->nb_channels = avctx->channels;
01066 q->bit_rate = avctx->bit_rate;
01067
01068
01069 av_init_random(1, &q->random_state);
01070
01071
01072 q->samples_per_channel = q->samples_per_frame / q->nb_channels;
01073 q->bits_per_subpacket = avctx->block_align * 8;
01074
01075
01076 q->log2_numvector_size = 5;
01077 q->total_subbands = q->subbands;
01078
01079
01080 av_log(NULL,AV_LOG_DEBUG,"q->cookversion=%x\n",q->cookversion);
01081 q->joint_stereo = 0;
01082 switch (q->cookversion) {
01083 case MONO:
01084 if (q->nb_channels != 1) {
01085 av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n");
01086 return -1;
01087 }
01088 av_log(avctx,AV_LOG_DEBUG,"MONO\n");
01089 break;
01090 case STEREO:
01091 if (q->nb_channels != 1) {
01092 q->bits_per_subpacket = q->bits_per_subpacket/2;
01093 }
01094 av_log(avctx,AV_LOG_DEBUG,"STEREO\n");
01095 break;
01096 case JOINT_STEREO:
01097 if (q->nb_channels != 2) {
01098 av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n");
01099 return -1;
01100 }
01101 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n");
01102 if (avctx->extradata_size >= 16){
01103 q->total_subbands = q->subbands + q->js_subband_start;
01104 q->joint_stereo = 1;
01105 }
01106 if (q->samples_per_channel > 256) {
01107 q->log2_numvector_size = 6;
01108 }
01109 if (q->samples_per_channel > 512) {
01110 q->log2_numvector_size = 7;
01111 }
01112 break;
01113 case MC_COOK:
01114 av_log(avctx,AV_LOG_ERROR,"MC_COOK not supported!\n");
01115 return -1;
01116 break;
01117 default:
01118 av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n");
01119 return -1;
01120 break;
01121 }
01122
01123
01124 q->numvector_size = (1 << q->log2_numvector_size);
01125
01126
01127 init_rootpow2table(q);
01128 init_pow2table(q);
01129 init_gain_table(q);
01130 init_cplscales_table(q);
01131
01132 if (init_cook_vlc_tables(q) != 0)
01133 return -1;
01134
01135
01136 if(avctx->block_align >= UINT_MAX/2)
01137 return -1;
01138
01139
01140
01141
01142 if (q->nb_channels==2 && q->joint_stereo==0) {
01143 q->decoded_bytes_buffer =
01144 av_mallocz(avctx->block_align/2
01145 + DECODE_BYTES_PAD2(avctx->block_align/2)
01146 + FF_INPUT_BUFFER_PADDING_SIZE);
01147 } else {
01148 q->decoded_bytes_buffer =
01149 av_mallocz(avctx->block_align
01150 + DECODE_BYTES_PAD1(avctx->block_align)
01151 + FF_INPUT_BUFFER_PADDING_SIZE);
01152 }
01153 if (q->decoded_bytes_buffer == NULL)
01154 return -1;
01155
01156 q->gains1.now = q->gain_1;
01157 q->gains1.previous = q->gain_2;
01158 q->gains2.now = q->gain_3;
01159 q->gains2.previous = q->gain_4;
01160
01161
01162 if ( init_cook_mlt(q) != 0 )
01163 return -1;
01164
01165
01166 if (1) {
01167 q->scalar_dequant = scalar_dequant_float;
01168 q->decouple = decouple_float;
01169 q->imlt_window = imlt_window_float;
01170 q->interpolate = interpolate_float;
01171 q->saturate_output = saturate_output_float;
01172 }
01173
01174
01175 if (q->total_subbands > 53) {
01176 av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n");
01177 return -1;
01178 }
01179 if (q->subbands > 50) {
01180 av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n");
01181 return -1;
01182 }
01183 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
01184 } else {
01185 av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
01186 return -1;
01187 }
01188 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) {
01189 av_log(avctx,AV_LOG_ERROR,"q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits);
01190 return -1;
01191 }
01192
01193 #ifdef COOKDEBUG
01194 dump_cook_context(q);
01195 #endif
01196 return 0;
01197 }
01198
01199
01200 AVCodec cook_decoder =
01201 {
01202 .name = "cook",
01203 .type = CODEC_TYPE_AUDIO,
01204 .id = CODEC_ID_COOK,
01205 .priv_data_size = sizeof(COOKContext),
01206 .init = cook_decode_init,
01207 .close = cook_decode_close,
01208 .decode = cook_decode_frame,
01209 };