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 "libavutil/random.h"
00050 #include "avcodec.h"
00051 #include "bitstream.h"
00052 #include "dsputil.h"
00053 #include "bytestream.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 float* mlt_window;
00116
00117
00118 cook_gains gains1;
00119 cook_gains gains2;
00120 int gain_1[9];
00121 int gain_2[9];
00122 int gain_3[9];
00123 int gain_4[9];
00124
00125
00126 int js_vlc_bits;
00127 VLC envelope_quant_index[13];
00128 VLC sqvh[7];
00129 VLC ccpl;
00130
00131
00132 int gain_size_factor;
00133 float gain_table[23];
00134
00135
00136
00137 uint8_t* decoded_bytes_buffer;
00138 DECLARE_ALIGNED_16(float,mono_mdct_output[2048]);
00139 float mono_previous_buffer1[1024];
00140 float mono_previous_buffer2[1024];
00141 float decode_buffer_1[1024];
00142 float decode_buffer_2[1024];
00143 float decode_buffer_0[1060];
00144
00145 const float *cplscales[5];
00146 } COOKContext;
00147
00148 static float pow2tab[127];
00149 static float rootpow2tab[127];
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 av_cold void init_pow2table(void){
00187 int i;
00188 for (i=-63 ; i<64 ; i++){
00189 pow2tab[63+i]= pow(2, i);
00190 rootpow2tab[63+i]=sqrt(pow(2, i));
00191 }
00192 }
00193
00194
00195 static av_cold void init_gain_table(COOKContext *q) {
00196 int i;
00197 q->gain_size_factor = q->samples_per_channel/8;
00198 for (i=0 ; i<23 ; i++) {
00199 q->gain_table[i] = pow(pow2tab[i+52] ,
00200 (1.0/(double)q->gain_size_factor));
00201 }
00202 }
00203
00204
00205 static av_cold int init_cook_vlc_tables(COOKContext *q) {
00206 int i, result;
00207
00208 result = 0;
00209 for (i=0 ; i<13 ; i++) {
00210 result |= init_vlc (&q->envelope_quant_index[i], 9, 24,
00211 envelope_quant_index_huffbits[i], 1, 1,
00212 envelope_quant_index_huffcodes[i], 2, 2, 0);
00213 }
00214 av_log(NULL,AV_LOG_DEBUG,"sqvh VLC init\n");
00215 for (i=0 ; i<7 ; i++) {
00216 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
00217 cvh_huffbits[i], 1, 1,
00218 cvh_huffcodes[i], 2, 2, 0);
00219 }
00220
00221 if (q->nb_channels==2 && q->joint_stereo==1){
00222 result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1,
00223 ccpl_huffbits[q->js_vlc_bits-2], 1, 1,
00224 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0);
00225 av_log(NULL,AV_LOG_DEBUG,"Joint-stereo VLC used.\n");
00226 }
00227
00228 av_log(NULL,AV_LOG_DEBUG,"VLC tables initialized.\n");
00229 return result;
00230 }
00231
00232 static av_cold int init_cook_mlt(COOKContext *q) {
00233 int j;
00234 int mlt_size = q->samples_per_channel;
00235
00236 if ((q->mlt_window = av_malloc(sizeof(float)*mlt_size)) == 0)
00237 return -1;
00238
00239
00240 ff_sine_window_init(q->mlt_window, mlt_size);
00241 for(j=0 ; j<mlt_size ; j++)
00242 q->mlt_window[j] *= sqrt(2.0 / q->samples_per_channel);
00243
00244
00245 if (ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size)+1, 1)) {
00246 av_free(q->mlt_window);
00247 return -1;
00248 }
00249 av_log(NULL,AV_LOG_DEBUG,"MDCT initialized, order = %d.\n",
00250 av_log2(mlt_size)+1);
00251
00252 return 0;
00253 }
00254
00255 static const float *maybe_reformat_buffer32 (COOKContext *q, const float *ptr, int n)
00256 {
00257 if (1)
00258 return ptr;
00259 }
00260
00261 static av_cold void init_cplscales_table (COOKContext *q) {
00262 int i;
00263 for (i=0;i<5;i++)
00264 q->cplscales[i] = maybe_reformat_buffer32 (q, cplscales[i], (1<<(i+2))-1);
00265 }
00266
00267
00268
00289 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)
00290 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
00291
00292 static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
00293 int i, off;
00294 uint32_t c;
00295 const uint32_t* buf;
00296 uint32_t* obuf = (uint32_t*) out;
00297
00298
00299
00300
00301
00302
00303 off = (int)((long)inbuffer & 3);
00304 buf = (const uint32_t*) (inbuffer - off);
00305 c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));
00306 bytes += 3 + off;
00307 for (i = 0; i < bytes/4; i++)
00308 obuf[i] = c ^ buf[i];
00309
00310 return off;
00311 }
00312
00317 static av_cold int cook_decode_close(AVCodecContext *avctx)
00318 {
00319 int i;
00320 COOKContext *q = avctx->priv_data;
00321 av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n");
00322
00323
00324 av_free(q->mlt_window);
00325 av_free(q->decoded_bytes_buffer);
00326
00327
00328 ff_mdct_end(&q->mdct_ctx);
00329
00330
00331 for (i=0 ; i<13 ; i++) {
00332 free_vlc(&q->envelope_quant_index[i]);
00333 }
00334 for (i=0 ; i<7 ; i++) {
00335 free_vlc(&q->sqvh[i]);
00336 }
00337 if(q->nb_channels==2 && q->joint_stereo==1 ){
00338 free_vlc(&q->ccpl);
00339 }
00340
00341 av_log(NULL,AV_LOG_DEBUG,"Memory deallocated.\n");
00342
00343 return 0;
00344 }
00345
00353 static void decode_gain_info(GetBitContext *gb, int *gaininfo)
00354 {
00355 int i, n;
00356
00357 while (get_bits1(gb)) {}
00358 n = get_bits_count(gb) - 1;
00359
00360 i = 0;
00361 while (n--) {
00362 int index = get_bits(gb, 3);
00363 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;
00364
00365 while (i <= index) gaininfo[i++] = gain;
00366 }
00367 while (i <= 8) gaininfo[i++] = 0;
00368 }
00369
00377 static void decode_envelope(COOKContext *q, int* quant_index_table) {
00378 int i,j, vlc_index;
00379
00380 quant_index_table[0]= get_bits(&q->gb,6) - 6;
00381
00382 for (i=1 ; i < q->total_subbands ; i++){
00383 vlc_index=i;
00384 if (i >= q->js_subband_start * 2) {
00385 vlc_index-=q->js_subband_start;
00386 } else {
00387 vlc_index/=2;
00388 if(vlc_index < 1) vlc_index = 1;
00389 }
00390 if (vlc_index>13) vlc_index = 13;
00391
00392 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,
00393 q->envelope_quant_index[vlc_index-1].bits,2);
00394 quant_index_table[i] = quant_index_table[i-1] + j - 12;
00395 }
00396 }
00397
00407 static void categorize(COOKContext *q, int* quant_index_table,
00408 int* category, int* category_index){
00409 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
00410 int exp_index2[102];
00411 int exp_index1[102];
00412
00413 int tmp_categorize_array[128*2];
00414 int tmp_categorize_array1_idx=q->numvector_size;
00415 int tmp_categorize_array2_idx=q->numvector_size;
00416
00417 bits_left = q->bits_per_subpacket - get_bits_count(&q->gb);
00418
00419 if(bits_left > q->samples_per_channel) {
00420 bits_left = q->samples_per_channel +
00421 ((bits_left - q->samples_per_channel)*5)/8;
00422
00423 }
00424
00425 memset(&exp_index1,0,102*sizeof(int));
00426 memset(&exp_index2,0,102*sizeof(int));
00427 memset(&tmp_categorize_array,0,128*2*sizeof(int));
00428
00429 bias=-32;
00430
00431
00432 for (i=32 ; i>0 ; i=i/2){
00433 num_bits = 0;
00434 index = 0;
00435 for (j=q->total_subbands ; j>0 ; j--){
00436 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
00437 index++;
00438 num_bits+=expbits_tab[exp_idx];
00439 }
00440 if(num_bits >= bits_left - 32){
00441 bias+=i;
00442 }
00443 }
00444
00445
00446 num_bits=0;
00447 for (i=0 ; i<q->total_subbands ; i++) {
00448 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
00449 num_bits += expbits_tab[exp_idx];
00450 exp_index1[i] = exp_idx;
00451 exp_index2[i] = exp_idx;
00452 }
00453 tmpbias1 = tmpbias2 = num_bits;
00454
00455 for (j = 1 ; j < q->numvector_size ; j++) {
00456 if (tmpbias1 + tmpbias2 > 2*bits_left) {
00457 int max = -999999;
00458 index=-1;
00459 for (i=0 ; i<q->total_subbands ; i++){
00460 if (exp_index1[i] < 7) {
00461 v = (-2*exp_index1[i]) - quant_index_table[i] + bias;
00462 if ( v >= max) {
00463 max = v;
00464 index = i;
00465 }
00466 }
00467 }
00468 if(index==-1)break;
00469 tmp_categorize_array[tmp_categorize_array1_idx++] = index;
00470 tmpbias1 -= expbits_tab[exp_index1[index]] -
00471 expbits_tab[exp_index1[index]+1];
00472 ++exp_index1[index];
00473 } else {
00474 int min = 999999;
00475 index=-1;
00476 for (i=0 ; i<q->total_subbands ; i++){
00477 if(exp_index2[i] > 0){
00478 v = (-2*exp_index2[i])-quant_index_table[i]+bias;
00479 if ( v < min) {
00480 min = v;
00481 index = i;
00482 }
00483 }
00484 }
00485 if(index == -1)break;
00486 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
00487 tmpbias2 -= expbits_tab[exp_index2[index]] -
00488 expbits_tab[exp_index2[index]-1];
00489 --exp_index2[index];
00490 }
00491 }
00492
00493 for(i=0 ; i<q->total_subbands ; i++)
00494 category[i] = exp_index2[i];
00495
00496 for(i=0 ; i<q->numvector_size-1 ; i++)
00497 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
00498
00499 }
00500
00501
00510 static inline void expand_category(COOKContext *q, int* category,
00511 int* category_index){
00512 int i;
00513 for(i=0 ; i<q->num_vectors ; i++){
00514 ++category[category_index[i]];
00515 }
00516 }
00517
00529 static void scalar_dequant_float(COOKContext *q, int index, int quant_index,
00530 int* subband_coef_index, int* subband_coef_sign,
00531 float* mlt_p){
00532 int i;
00533 float f1;
00534
00535 for(i=0 ; i<SUBBAND_SIZE ; i++) {
00536 if (subband_coef_index[i]) {
00537 f1 = quant_centroid_tab[index][subband_coef_index[i]];
00538 if (subband_coef_sign[i]) f1 = -f1;
00539 } else {
00540
00541 f1 = dither_tab[index];
00542 if (av_random(&q->random_state) < 0x80000000) f1 = -f1;
00543 }
00544 mlt_p[i] = f1 * rootpow2tab[quant_index+63];
00545 }
00546 }
00556 static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index,
00557 int* subband_coef_sign) {
00558 int i,j;
00559 int vlc, vd ,tmp, result;
00560
00561 vd = vd_tab[category];
00562 result = 0;
00563 for(i=0 ; i<vpr_tab[category] ; i++){
00564 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
00565 if (q->bits_per_subpacket < get_bits_count(&q->gb)){
00566 vlc = 0;
00567 result = 1;
00568 }
00569 for(j=vd-1 ; j>=0 ; j--){
00570 tmp = (vlc * invradix_tab[category])/0x100000;
00571 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1);
00572 vlc = tmp;
00573 }
00574 for(j=0 ; j<vd ; j++){
00575 if (subband_coef_index[i*vd + j]) {
00576 if(get_bits_count(&q->gb) < q->bits_per_subpacket){
00577 subband_coef_sign[i*vd+j] = get_bits1(&q->gb);
00578 } else {
00579 result=1;
00580 subband_coef_sign[i*vd+j]=0;
00581 }
00582 } else {
00583 subband_coef_sign[i*vd+j]=0;
00584 }
00585 }
00586 }
00587 return result;
00588 }
00589
00590
00601 static void decode_vectors(COOKContext* q, int* category,
00602 int *quant_index_table, float* mlt_buffer){
00603
00604
00605 int subband_coef_index[SUBBAND_SIZE];
00606
00607
00608 int subband_coef_sign[SUBBAND_SIZE];
00609 int band, j;
00610 int index=0;
00611
00612 for(band=0 ; band<q->total_subbands ; band++){
00613 index = category[band];
00614 if(category[band] < 7){
00615 if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){
00616 index=7;
00617 for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7;
00618 }
00619 }
00620 if(index==7) {
00621 memset(subband_coef_index, 0, sizeof(subband_coef_index));
00622 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
00623 }
00624 q->scalar_dequant(q, index, quant_index_table[band],
00625 subband_coef_index, subband_coef_sign,
00626 &mlt_buffer[band * SUBBAND_SIZE]);
00627 }
00628
00629 if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
00630 return;
00631 }
00632 }
00633
00634
00642 static void mono_decode(COOKContext *q, float* mlt_buffer) {
00643
00644 int category_index[128];
00645 int quant_index_table[102];
00646 int category[128];
00647
00648 memset(&category, 0, 128*sizeof(int));
00649 memset(&category_index, 0, 128*sizeof(int));
00650
00651 decode_envelope(q, quant_index_table);
00652 q->num_vectors = get_bits(&q->gb,q->log2_numvector_size);
00653 categorize(q, quant_index_table, category, category_index);
00654 expand_category(q, category, category_index);
00655 decode_vectors(q, category, quant_index_table, mlt_buffer);
00656 }
00657
00658
00668 static void interpolate_float(COOKContext *q, float* buffer,
00669 int gain_index, int gain_index_next){
00670 int i;
00671 float fc1, fc2;
00672 fc1 = pow2tab[gain_index+63];
00673
00674 if(gain_index == gain_index_next){
00675 for(i=0 ; i<q->gain_size_factor ; i++){
00676 buffer[i]*=fc1;
00677 }
00678 return;
00679 } else {
00680 fc2 = q->gain_table[11 + (gain_index_next-gain_index)];
00681 for(i=0 ; i<q->gain_size_factor ; i++){
00682 buffer[i]*=fc1;
00683 fc1*=fc2;
00684 }
00685 return;
00686 }
00687 }
00688
00698 static void imlt_window_float (COOKContext *q, float *buffer1,
00699 cook_gains *gains_ptr, float *previous_buffer)
00700 {
00701 const float fc = pow2tab[gains_ptr->previous[0] + 63];
00702 int i;
00703
00704
00705
00706
00707
00708
00709
00710 for(i = 0; i < q->samples_per_channel; i++){
00711 buffer1[i] = buffer1[i] * fc * q->mlt_window[i] -
00712 previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
00713 }
00714 }
00715
00728 static void imlt_gain(COOKContext *q, float *inbuffer,
00729 cook_gains *gains_ptr, float* previous_buffer)
00730 {
00731 float *buffer0 = q->mono_mdct_output;
00732 float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
00733 int i;
00734
00735
00736 ff_imdct_calc(&q->mdct_ctx, q->mono_mdct_output, inbuffer);
00737
00738 q->imlt_window (q, buffer1, gains_ptr, previous_buffer);
00739
00740
00741 for (i = 0; i < 8; i++) {
00742 if (gains_ptr->now[i] || gains_ptr->now[i + 1])
00743 q->interpolate(q, &buffer1[q->gain_size_factor * i],
00744 gains_ptr->now[i], gains_ptr->now[i + 1]);
00745 }
00746
00747
00748 memcpy(previous_buffer, buffer0, sizeof(float)*q->samples_per_channel);
00749 }
00750
00751
00760 static void decouple_info(COOKContext *q, int* decouple_tab){
00761 int length, i;
00762
00763 if(get_bits1(&q->gb)) {
00764 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
00765
00766 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
00767 for (i=0 ; i<length ; i++) {
00768 decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2);
00769 }
00770 return;
00771 }
00772
00773 if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return;
00774
00775 length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1;
00776 for (i=0 ; i<length ; i++) {
00777 decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits);
00778 }
00779 return;
00780 }
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793 static void decouple_float (COOKContext *q,
00794 int subband,
00795 float f1, float f2,
00796 float *decode_buffer,
00797 float *mlt_buffer1, float *mlt_buffer2)
00798 {
00799 int j, tmp_idx;
00800 for (j=0 ; j<SUBBAND_SIZE ; j++) {
00801 tmp_idx = ((q->js_subband_start + subband)*SUBBAND_SIZE)+j;
00802 mlt_buffer1[SUBBAND_SIZE*subband + j] = f1 * decode_buffer[tmp_idx];
00803 mlt_buffer2[SUBBAND_SIZE*subband + j] = f2 * decode_buffer[tmp_idx];
00804 }
00805 }
00806
00815 static void joint_decode(COOKContext *q, float* mlt_buffer1,
00816 float* mlt_buffer2) {
00817 int i,j;
00818 int decouple_tab[SUBBAND_SIZE];
00819 float *decode_buffer = q->decode_buffer_0;
00820 int idx, cpl_tmp;
00821 float f1,f2;
00822 const float* cplscale;
00823
00824 memset(decouple_tab, 0, sizeof(decouple_tab));
00825 memset(decode_buffer, 0, sizeof(decode_buffer));
00826
00827
00828 memset(mlt_buffer1,0, 1024*sizeof(float));
00829 memset(mlt_buffer2,0, 1024*sizeof(float));
00830 decouple_info(q, decouple_tab);
00831 mono_decode(q, decode_buffer);
00832
00833
00834 for (i=0 ; i<q->js_subband_start ; i++) {
00835 for (j=0 ; j<SUBBAND_SIZE ; j++) {
00836 mlt_buffer1[i*20+j] = decode_buffer[i*40+j];
00837 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j];
00838 }
00839 }
00840
00841
00842
00843 idx = (1 << q->js_vlc_bits) - 1;
00844 for (i=q->js_subband_start ; i<q->subbands ; i++) {
00845 cpl_tmp = cplband[i];
00846 idx -=decouple_tab[cpl_tmp];
00847 cplscale = q->cplscales[q->js_vlc_bits-2];
00848 f1 = cplscale[decouple_tab[cpl_tmp]];
00849 f2 = cplscale[idx-1];
00850 q->decouple (q, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2);
00851 idx = (1 << q->js_vlc_bits) - 1;
00852 }
00853 }
00854
00864 static inline void
00865 decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer,
00866 cook_gains *gains_ptr)
00867 {
00868 int offset;
00869
00870 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
00871 q->bits_per_subpacket/8);
00872 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
00873 q->bits_per_subpacket);
00874 decode_gain_info(&q->gb, gains_ptr->now);
00875
00876
00877 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
00878 }
00879
00887 static void
00888 saturate_output_float (COOKContext *q, int chan, int16_t *out)
00889 {
00890 int j;
00891 float *output = q->mono_mdct_output + q->samples_per_channel;
00892
00893
00894 for (j = 0; j < q->samples_per_channel; j++) {
00895 out[chan + q->nb_channels * j] =
00896 av_clip_int16(lrintf(output[j]));
00897 }
00898 }
00899
00913 static inline void
00914 mlt_compensate_output(COOKContext *q, float *decode_buffer,
00915 cook_gains *gains, float *previous_buffer,
00916 int16_t *out, int chan)
00917 {
00918 imlt_gain(q, decode_buffer, gains, previous_buffer);
00919 q->saturate_output (q, chan, out);
00920 }
00921
00922
00934 static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
00935 int sub_packet_size, int16_t *outbuffer) {
00936
00937
00938
00939
00940
00941
00942 decode_bytes_and_gain(q, inbuffer, &q->gains1);
00943
00944 if (q->joint_stereo) {
00945 joint_decode(q, q->decode_buffer_1, q->decode_buffer_2);
00946 } else {
00947 mono_decode(q, q->decode_buffer_1);
00948
00949 if (q->nb_channels == 2) {
00950 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2);
00951 mono_decode(q, q->decode_buffer_2);
00952 }
00953 }
00954
00955 mlt_compensate_output(q, q->decode_buffer_1, &q->gains1,
00956 q->mono_previous_buffer1, outbuffer, 0);
00957
00958 if (q->nb_channels == 2) {
00959 if (q->joint_stereo) {
00960 mlt_compensate_output(q, q->decode_buffer_2, &q->gains1,
00961 q->mono_previous_buffer2, outbuffer, 1);
00962 } else {
00963 mlt_compensate_output(q, q->decode_buffer_2, &q->gains2,
00964 q->mono_previous_buffer2, outbuffer, 1);
00965 }
00966 }
00967 return q->samples_per_frame * sizeof(int16_t);
00968 }
00969
00970
00977 static int cook_decode_frame(AVCodecContext *avctx,
00978 void *data, int *data_size,
00979 const uint8_t *buf, int buf_size) {
00980 COOKContext *q = avctx->priv_data;
00981
00982 if (buf_size < avctx->block_align)
00983 return buf_size;
00984
00985 *data_size = decode_subpacket(q, buf, avctx->block_align, data);
00986
00987
00988 if (avctx->frame_number < 2) *data_size = 0;
00989
00990 return avctx->block_align;
00991 }
00992
00993 #ifdef COOKDEBUG
00994 static void dump_cook_context(COOKContext *q)
00995 {
00996
00997 #define PRINT(a,b) av_log(NULL,AV_LOG_ERROR," %s = %d\n", a, b);
00998 av_log(NULL,AV_LOG_ERROR,"COOKextradata\n");
00999 av_log(NULL,AV_LOG_ERROR,"cookversion=%x\n",q->cookversion);
01000 if (q->cookversion > STEREO) {
01001 PRINT("js_subband_start",q->js_subband_start);
01002 PRINT("js_vlc_bits",q->js_vlc_bits);
01003 }
01004 av_log(NULL,AV_LOG_ERROR,"COOKContext\n");
01005 PRINT("nb_channels",q->nb_channels);
01006 PRINT("bit_rate",q->bit_rate);
01007 PRINT("sample_rate",q->sample_rate);
01008 PRINT("samples_per_channel",q->samples_per_channel);
01009 PRINT("samples_per_frame",q->samples_per_frame);
01010 PRINT("subbands",q->subbands);
01011 PRINT("random_state",q->random_state);
01012 PRINT("js_subband_start",q->js_subband_start);
01013 PRINT("log2_numvector_size",q->log2_numvector_size);
01014 PRINT("numvector_size",q->numvector_size);
01015 PRINT("total_subbands",q->total_subbands);
01016 }
01017 #endif
01018
01025 static av_cold int cook_decode_init(AVCodecContext *avctx)
01026 {
01027 COOKContext *q = avctx->priv_data;
01028 const uint8_t *edata_ptr = avctx->extradata;
01029
01030
01031 if (avctx->extradata_size <= 0) {
01032 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n");
01033 return -1;
01034 } else {
01035
01036
01037 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size);
01038 if (avctx->extradata_size >= 8){
01039 q->cookversion = bytestream_get_be32(&edata_ptr);
01040 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
01041 q->subbands = bytestream_get_be16(&edata_ptr);
01042 }
01043 if (avctx->extradata_size >= 16){
01044 bytestream_get_be32(&edata_ptr);
01045 q->js_subband_start = bytestream_get_be16(&edata_ptr);
01046 q->js_vlc_bits = bytestream_get_be16(&edata_ptr);
01047 }
01048 }
01049
01050
01051 q->sample_rate = avctx->sample_rate;
01052 q->nb_channels = avctx->channels;
01053 q->bit_rate = avctx->bit_rate;
01054
01055
01056 av_random_init(&q->random_state, 1);
01057
01058
01059 q->samples_per_channel = q->samples_per_frame / q->nb_channels;
01060 q->bits_per_subpacket = avctx->block_align * 8;
01061
01062
01063 q->log2_numvector_size = 5;
01064 q->total_subbands = q->subbands;
01065
01066
01067 av_log(NULL,AV_LOG_DEBUG,"q->cookversion=%x\n",q->cookversion);
01068 q->joint_stereo = 0;
01069 switch (q->cookversion) {
01070 case MONO:
01071 if (q->nb_channels != 1) {
01072 av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n");
01073 return -1;
01074 }
01075 av_log(avctx,AV_LOG_DEBUG,"MONO\n");
01076 break;
01077 case STEREO:
01078 if (q->nb_channels != 1) {
01079 q->bits_per_subpacket = q->bits_per_subpacket/2;
01080 }
01081 av_log(avctx,AV_LOG_DEBUG,"STEREO\n");
01082 break;
01083 case JOINT_STEREO:
01084 if (q->nb_channels != 2) {
01085 av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n");
01086 return -1;
01087 }
01088 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n");
01089 if (avctx->extradata_size >= 16){
01090 q->total_subbands = q->subbands + q->js_subband_start;
01091 q->joint_stereo = 1;
01092 }
01093 if (q->samples_per_channel > 256) {
01094 q->log2_numvector_size = 6;
01095 }
01096 if (q->samples_per_channel > 512) {
01097 q->log2_numvector_size = 7;
01098 }
01099 break;
01100 case MC_COOK:
01101 av_log(avctx,AV_LOG_ERROR,"MC_COOK not supported!\n");
01102 return -1;
01103 break;
01104 default:
01105 av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n");
01106 return -1;
01107 break;
01108 }
01109
01110
01111 q->numvector_size = (1 << q->log2_numvector_size);
01112
01113
01114 init_pow2table();
01115 init_gain_table(q);
01116 init_cplscales_table(q);
01117
01118 if (init_cook_vlc_tables(q) != 0)
01119 return -1;
01120
01121
01122 if(avctx->block_align >= UINT_MAX/2)
01123 return -1;
01124
01125
01126
01127
01128 if (q->nb_channels==2 && q->joint_stereo==0) {
01129 q->decoded_bytes_buffer =
01130 av_mallocz(avctx->block_align/2
01131 + DECODE_BYTES_PAD2(avctx->block_align/2)
01132 + FF_INPUT_BUFFER_PADDING_SIZE);
01133 } else {
01134 q->decoded_bytes_buffer =
01135 av_mallocz(avctx->block_align
01136 + DECODE_BYTES_PAD1(avctx->block_align)
01137 + FF_INPUT_BUFFER_PADDING_SIZE);
01138 }
01139 if (q->decoded_bytes_buffer == NULL)
01140 return -1;
01141
01142 q->gains1.now = q->gain_1;
01143 q->gains1.previous = q->gain_2;
01144 q->gains2.now = q->gain_3;
01145 q->gains2.previous = q->gain_4;
01146
01147
01148 if ( init_cook_mlt(q) != 0 )
01149 return -1;
01150
01151
01152 if (1) {
01153 q->scalar_dequant = scalar_dequant_float;
01154 q->decouple = decouple_float;
01155 q->imlt_window = imlt_window_float;
01156 q->interpolate = interpolate_float;
01157 q->saturate_output = saturate_output_float;
01158 }
01159
01160
01161 if (q->total_subbands > 53) {
01162 av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n");
01163 return -1;
01164 }
01165 if (q->subbands > 50) {
01166 av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n");
01167 return -1;
01168 }
01169 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
01170 } else {
01171 av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
01172 return -1;
01173 }
01174 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) {
01175 av_log(avctx,AV_LOG_ERROR,"q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits);
01176 return -1;
01177 }
01178
01179 avctx->sample_fmt = SAMPLE_FMT_S16;
01180 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
01181
01182 #ifdef COOKDEBUG
01183 dump_cook_context(q);
01184 #endif
01185 return 0;
01186 }
01187
01188
01189 AVCodec cook_decoder =
01190 {
01191 .name = "cook",
01192 .type = CODEC_TYPE_AUDIO,
01193 .id = CODEC_ID_COOK,
01194 .priv_data_size = sizeof(COOKContext),
01195 .init = cook_decode_init,
01196 .close = cook_decode_close,
01197 .decode = cook_decode_frame,
01198 .long_name = NULL_IF_CONFIG_SMALL("COOK"),
01199 };