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/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
00057 #include "cookdata.h"
00058
00059
00060 #define MONO 0x1000001
00061 #define STEREO 0x1000002
00062 #define JOINT_STEREO 0x1000003
00063 #define MC_COOK 0x2000000 //multichannel Cook, not supported
00064
00065 #define SUBBAND_SIZE 20
00066 #define MAX_SUBPACKETS 5
00067
00068
00069 typedef struct {
00070 int *now;
00071 int *previous;
00072 } cook_gains;
00073
00074 typedef struct {
00075 int ch_idx;
00076 int size;
00077 int num_channels;
00078 int cookversion;
00079 int samples_per_frame;
00080 int subbands;
00081 int js_subband_start;
00082 int js_vlc_bits;
00083 int samples_per_channel;
00084 int log2_numvector_size;
00085 unsigned int channel_mask;
00086 VLC ccpl;
00087 int joint_stereo;
00088 int bits_per_subpacket;
00089 int bits_per_subpdiv;
00090 int total_subbands;
00091 int numvector_size;
00092
00093 float mono_previous_buffer1[1024];
00094 float mono_previous_buffer2[1024];
00096 cook_gains gains1;
00097 cook_gains gains2;
00098 int gain_1[9];
00099 int gain_2[9];
00100 int gain_3[9];
00101 int gain_4[9];
00102 } COOKSubpacket;
00103
00104 typedef struct cook {
00105
00106
00107
00108
00109 void (* scalar_dequant)(struct cook *q, int index, int quant_index,
00110 int* subband_coef_index, int* subband_coef_sign,
00111 float* mlt_p);
00112
00113 void (* decouple) (struct cook *q,
00114 COOKSubpacket *p,
00115 int subband,
00116 float f1, float f2,
00117 float *decode_buffer,
00118 float *mlt_buffer1, float *mlt_buffer2);
00119
00120 void (* imlt_window) (struct cook *q, float *buffer1,
00121 cook_gains *gains_ptr, float *previous_buffer);
00122
00123 void (* interpolate) (struct cook *q, float* buffer,
00124 int gain_index, int gain_index_next);
00125
00126 void (* saturate_output) (struct cook *q, int chan, int16_t *out);
00127
00128 AVCodecContext* avctx;
00129 GetBitContext gb;
00130
00131 int nb_channels;
00132 int bit_rate;
00133 int sample_rate;
00134 int num_vectors;
00135 int samples_per_channel;
00136
00137 AVLFG random_state;
00138
00139
00140 FFTContext mdct_ctx;
00141 float* mlt_window;
00142
00143
00144 VLC envelope_quant_index[13];
00145 VLC sqvh[7];
00146
00147
00148 int gain_size_factor;
00149 float gain_table[23];
00150
00151
00152
00153 uint8_t* decoded_bytes_buffer;
00154 DECLARE_ALIGNED(16, float,mono_mdct_output)[2048];
00155 float decode_buffer_1[1024];
00156 float decode_buffer_2[1024];
00157 float decode_buffer_0[1060];
00158
00159 const float *cplscales[5];
00160 int num_subpackets;
00161 COOKSubpacket subpacket[MAX_SUBPACKETS];
00162 } COOKContext;
00163
00164 static float pow2tab[127];
00165 static float rootpow2tab[127];
00166
00167
00168
00169 #ifdef COOKDEBUG
00170 static void dump_float_table(float* table, int size, int delimiter) {
00171 int i=0;
00172 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
00173 for (i=0 ; i<size ; i++) {
00174 av_log(NULL, AV_LOG_ERROR, "%5.1f, ", table[i]);
00175 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
00176 }
00177 }
00178
00179 static void dump_int_table(int* table, int size, int delimiter) {
00180 int i=0;
00181 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
00182 for (i=0 ; i<size ; i++) {
00183 av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);
00184 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
00185 }
00186 }
00187
00188 static void dump_short_table(short* table, int size, int delimiter) {
00189 int i=0;
00190 av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i);
00191 for (i=0 ; i<size ; i++) {
00192 av_log(NULL, AV_LOG_ERROR, "%d, ", table[i]);
00193 if ((i+1)%delimiter == 0) av_log(NULL,AV_LOG_ERROR,"\n[%d]: ",i+1);
00194 }
00195 }
00196
00197 #endif
00198
00199
00200
00201
00202 static av_cold void init_pow2table(void){
00203 int i;
00204 for (i=-63 ; i<64 ; i++){
00205 pow2tab[63+i]= pow(2, i);
00206 rootpow2tab[63+i]=sqrt(pow(2, i));
00207 }
00208 }
00209
00210
00211 static av_cold void init_gain_table(COOKContext *q) {
00212 int i;
00213 q->gain_size_factor = q->samples_per_channel/8;
00214 for (i=0 ; i<23 ; i++) {
00215 q->gain_table[i] = pow(pow2tab[i+52] ,
00216 (1.0/(double)q->gain_size_factor));
00217 }
00218 }
00219
00220
00221 static av_cold int init_cook_vlc_tables(COOKContext *q) {
00222 int i, result;
00223
00224 result = 0;
00225 for (i=0 ; i<13 ; i++) {
00226 result |= init_vlc (&q->envelope_quant_index[i], 9, 24,
00227 envelope_quant_index_huffbits[i], 1, 1,
00228 envelope_quant_index_huffcodes[i], 2, 2, 0);
00229 }
00230 av_log(q->avctx,AV_LOG_DEBUG,"sqvh VLC init\n");
00231 for (i=0 ; i<7 ; i++) {
00232 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
00233 cvh_huffbits[i], 1, 1,
00234 cvh_huffcodes[i], 2, 2, 0);
00235 }
00236
00237 for(i=0;i<q->num_subpackets;i++){
00238 if (q->subpacket[i].joint_stereo==1){
00239 result |= init_vlc (&q->subpacket[i].ccpl, 6, (1<<q->subpacket[i].js_vlc_bits)-1,
00240 ccpl_huffbits[q->subpacket[i].js_vlc_bits-2], 1, 1,
00241 ccpl_huffcodes[q->subpacket[i].js_vlc_bits-2], 2, 2, 0);
00242 av_log(q->avctx,AV_LOG_DEBUG,"subpacket %i Joint-stereo VLC used.\n",i);
00243 }
00244 }
00245
00246 av_log(q->avctx,AV_LOG_DEBUG,"VLC tables initialized.\n");
00247 return result;
00248 }
00249
00250 static av_cold int init_cook_mlt(COOKContext *q) {
00251 int j;
00252 int mlt_size = q->samples_per_channel;
00253
00254 if ((q->mlt_window = av_malloc(sizeof(float)*mlt_size)) == 0)
00255 return -1;
00256
00257
00258 ff_sine_window_init(q->mlt_window, mlt_size);
00259 for(j=0 ; j<mlt_size ; j++)
00260 q->mlt_window[j] *= sqrt(2.0 / q->samples_per_channel);
00261
00262
00263 if (ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size)+1, 1, 1.0)) {
00264 av_free(q->mlt_window);
00265 return -1;
00266 }
00267 av_log(q->avctx,AV_LOG_DEBUG,"MDCT initialized, order = %d.\n",
00268 av_log2(mlt_size)+1);
00269
00270 return 0;
00271 }
00272
00273 static const float *maybe_reformat_buffer32 (COOKContext *q, const float *ptr, int n)
00274 {
00275 if (1)
00276 return ptr;
00277 }
00278
00279 static av_cold void init_cplscales_table (COOKContext *q) {
00280 int i;
00281 for (i=0;i<5;i++)
00282 q->cplscales[i] = maybe_reformat_buffer32 (q, cplscales[i], (1<<(i+2))-1);
00283 }
00284
00285
00286
00307 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4)
00308 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
00309
00310 static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){
00311 int i, off;
00312 uint32_t c;
00313 const uint32_t* buf;
00314 uint32_t* obuf = (uint32_t*) out;
00315
00316
00317
00318
00319
00320
00321 off = (intptr_t)inbuffer & 3;
00322 buf = (const uint32_t*) (inbuffer - off);
00323 c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8))));
00324 bytes += 3 + off;
00325 for (i = 0; i < bytes/4; i++)
00326 obuf[i] = c ^ buf[i];
00327
00328 return off;
00329 }
00330
00335 static av_cold int cook_decode_close(AVCodecContext *avctx)
00336 {
00337 int i;
00338 COOKContext *q = avctx->priv_data;
00339 av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n");
00340
00341
00342 av_free(q->mlt_window);
00343 av_free(q->decoded_bytes_buffer);
00344
00345
00346 ff_mdct_end(&q->mdct_ctx);
00347
00348
00349 for (i=0 ; i<13 ; i++) {
00350 free_vlc(&q->envelope_quant_index[i]);
00351 }
00352 for (i=0 ; i<7 ; i++) {
00353 free_vlc(&q->sqvh[i]);
00354 }
00355 for (i=0 ; i<q->num_subpackets ; i++) {
00356 free_vlc(&q->subpacket[i].ccpl);
00357 }
00358
00359 av_log(avctx,AV_LOG_DEBUG,"Memory deallocated.\n");
00360
00361 return 0;
00362 }
00363
00371 static void decode_gain_info(GetBitContext *gb, int *gaininfo)
00372 {
00373 int i, n;
00374
00375 while (get_bits1(gb)) {}
00376 n = get_bits_count(gb) - 1;
00377
00378 i = 0;
00379 while (n--) {
00380 int index = get_bits(gb, 3);
00381 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;
00382
00383 while (i <= index) gaininfo[i++] = gain;
00384 }
00385 while (i <= 8) gaininfo[i++] = 0;
00386 }
00387
00395 static void decode_envelope(COOKContext *q, COOKSubpacket *p, int* quant_index_table) {
00396 int i,j, vlc_index;
00397
00398 quant_index_table[0]= get_bits(&q->gb,6) - 6;
00399
00400 for (i=1 ; i < p->total_subbands ; i++){
00401 vlc_index=i;
00402 if (i >= p->js_subband_start * 2) {
00403 vlc_index-=p->js_subband_start;
00404 } else {
00405 vlc_index/=2;
00406 if(vlc_index < 1) vlc_index = 1;
00407 }
00408 if (vlc_index>13) vlc_index = 13;
00409
00410 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table,
00411 q->envelope_quant_index[vlc_index-1].bits,2);
00412 quant_index_table[i] = quant_index_table[i-1] + j - 12;
00413 }
00414 }
00415
00425 static void categorize(COOKContext *q, COOKSubpacket *p, int* quant_index_table,
00426 int* category, int* category_index){
00427 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
00428 int exp_index2[102];
00429 int exp_index1[102];
00430
00431 int tmp_categorize_array[128*2];
00432 int tmp_categorize_array1_idx=p->numvector_size;
00433 int tmp_categorize_array2_idx=p->numvector_size;
00434
00435 bits_left = p->bits_per_subpacket - get_bits_count(&q->gb);
00436
00437 if(bits_left > q->samples_per_channel) {
00438 bits_left = q->samples_per_channel +
00439 ((bits_left - q->samples_per_channel)*5)/8;
00440
00441 }
00442
00443 memset(&exp_index1,0,102*sizeof(int));
00444 memset(&exp_index2,0,102*sizeof(int));
00445 memset(&tmp_categorize_array,0,128*2*sizeof(int));
00446
00447 bias=-32;
00448
00449
00450 for (i=32 ; i>0 ; i=i/2){
00451 num_bits = 0;
00452 index = 0;
00453 for (j=p->total_subbands ; j>0 ; j--){
00454 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
00455 index++;
00456 num_bits+=expbits_tab[exp_idx];
00457 }
00458 if(num_bits >= bits_left - 32){
00459 bias+=i;
00460 }
00461 }
00462
00463
00464 num_bits=0;
00465 for (i=0 ; i<p->total_subbands ; i++) {
00466 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
00467 num_bits += expbits_tab[exp_idx];
00468 exp_index1[i] = exp_idx;
00469 exp_index2[i] = exp_idx;
00470 }
00471 tmpbias1 = tmpbias2 = num_bits;
00472
00473 for (j = 1 ; j < p->numvector_size ; j++) {
00474 if (tmpbias1 + tmpbias2 > 2*bits_left) {
00475 int max = -999999;
00476 index=-1;
00477 for (i=0 ; i<p->total_subbands ; i++){
00478 if (exp_index1[i] < 7) {
00479 v = (-2*exp_index1[i]) - quant_index_table[i] + bias;
00480 if ( v >= max) {
00481 max = v;
00482 index = i;
00483 }
00484 }
00485 }
00486 if(index==-1)break;
00487 tmp_categorize_array[tmp_categorize_array1_idx++] = index;
00488 tmpbias1 -= expbits_tab[exp_index1[index]] -
00489 expbits_tab[exp_index1[index]+1];
00490 ++exp_index1[index];
00491 } else {
00492 int min = 999999;
00493 index=-1;
00494 for (i=0 ; i<p->total_subbands ; i++){
00495 if(exp_index2[i] > 0){
00496 v = (-2*exp_index2[i])-quant_index_table[i]+bias;
00497 if ( v < min) {
00498 min = v;
00499 index = i;
00500 }
00501 }
00502 }
00503 if(index == -1)break;
00504 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
00505 tmpbias2 -= expbits_tab[exp_index2[index]] -
00506 expbits_tab[exp_index2[index]-1];
00507 --exp_index2[index];
00508 }
00509 }
00510
00511 for(i=0 ; i<p->total_subbands ; i++)
00512 category[i] = exp_index2[i];
00513
00514 for(i=0 ; i<p->numvector_size-1 ; i++)
00515 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
00516
00517 }
00518
00519
00528 static inline void expand_category(COOKContext *q, int* category,
00529 int* category_index){
00530 int i;
00531 for(i=0 ; i<q->num_vectors ; i++){
00532 ++category[category_index[i]];
00533 }
00534 }
00535
00547 static void scalar_dequant_float(COOKContext *q, int index, int quant_index,
00548 int* subband_coef_index, int* subband_coef_sign,
00549 float* mlt_p){
00550 int i;
00551 float f1;
00552
00553 for(i=0 ; i<SUBBAND_SIZE ; i++) {
00554 if (subband_coef_index[i]) {
00555 f1 = quant_centroid_tab[index][subband_coef_index[i]];
00556 if (subband_coef_sign[i]) f1 = -f1;
00557 } else {
00558
00559 f1 = dither_tab[index];
00560 if (av_lfg_get(&q->random_state) < 0x80000000) f1 = -f1;
00561 }
00562 mlt_p[i] = f1 * rootpow2tab[quant_index+63];
00563 }
00564 }
00574 static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category, int* subband_coef_index,
00575 int* subband_coef_sign) {
00576 int i,j;
00577 int vlc, vd ,tmp, result;
00578
00579 vd = vd_tab[category];
00580 result = 0;
00581 for(i=0 ; i<vpr_tab[category] ; i++){
00582 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
00583 if (p->bits_per_subpacket < get_bits_count(&q->gb)){
00584 vlc = 0;
00585 result = 1;
00586 }
00587 for(j=vd-1 ; j>=0 ; j--){
00588 tmp = (vlc * invradix_tab[category])/0x100000;
00589 subband_coef_index[vd*i+j] = vlc - tmp * (kmax_tab[category]+1);
00590 vlc = tmp;
00591 }
00592 for(j=0 ; j<vd ; j++){
00593 if (subband_coef_index[i*vd + j]) {
00594 if(get_bits_count(&q->gb) < p->bits_per_subpacket){
00595 subband_coef_sign[i*vd+j] = get_bits1(&q->gb);
00596 } else {
00597 result=1;
00598 subband_coef_sign[i*vd+j]=0;
00599 }
00600 } else {
00601 subband_coef_sign[i*vd+j]=0;
00602 }
00603 }
00604 }
00605 return result;
00606 }
00607
00608
00619 static void decode_vectors(COOKContext* q, COOKSubpacket* p, int* category,
00620 int *quant_index_table, float* mlt_buffer){
00621
00622
00623 int subband_coef_index[SUBBAND_SIZE];
00624
00625
00626 int subband_coef_sign[SUBBAND_SIZE];
00627 int band, j;
00628 int index=0;
00629
00630 for(band=0 ; band<p->total_subbands ; band++){
00631 index = category[band];
00632 if(category[band] < 7){
00633 if(unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)){
00634 index=7;
00635 for(j=0 ; j<p->total_subbands ; j++) category[band+j]=7;
00636 }
00637 }
00638 if(index>=7) {
00639 memset(subband_coef_index, 0, sizeof(subband_coef_index));
00640 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
00641 }
00642 q->scalar_dequant(q, index, quant_index_table[band],
00643 subband_coef_index, subband_coef_sign,
00644 &mlt_buffer[band * SUBBAND_SIZE]);
00645 }
00646
00647 if(p->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){
00648 return;
00649 }
00650 }
00651
00652
00660 static void mono_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer) {
00661
00662 int category_index[128];
00663 int quant_index_table[102];
00664 int category[128];
00665
00666 memset(&category, 0, 128*sizeof(int));
00667 memset(&category_index, 0, 128*sizeof(int));
00668
00669 decode_envelope(q, p, quant_index_table);
00670 q->num_vectors = get_bits(&q->gb,p->log2_numvector_size);
00671 categorize(q, p, quant_index_table, category, category_index);
00672 expand_category(q, category, category_index);
00673 decode_vectors(q, p, category, quant_index_table, mlt_buffer);
00674 }
00675
00676
00686 static void interpolate_float(COOKContext *q, float* buffer,
00687 int gain_index, int gain_index_next){
00688 int i;
00689 float fc1, fc2;
00690 fc1 = pow2tab[gain_index+63];
00691
00692 if(gain_index == gain_index_next){
00693 for(i=0 ; i<q->gain_size_factor ; i++){
00694 buffer[i]*=fc1;
00695 }
00696 return;
00697 } else {
00698 fc2 = q->gain_table[11 + (gain_index_next-gain_index)];
00699 for(i=0 ; i<q->gain_size_factor ; i++){
00700 buffer[i]*=fc1;
00701 fc1*=fc2;
00702 }
00703 return;
00704 }
00705 }
00706
00716 static void imlt_window_float (COOKContext *q, float *buffer1,
00717 cook_gains *gains_ptr, float *previous_buffer)
00718 {
00719 const float fc = pow2tab[gains_ptr->previous[0] + 63];
00720 int i;
00721
00722
00723
00724
00725
00726
00727
00728 for(i = 0; i < q->samples_per_channel; i++){
00729 buffer1[i] = buffer1[i] * fc * q->mlt_window[i] -
00730 previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
00731 }
00732 }
00733
00746 static void imlt_gain(COOKContext *q, float *inbuffer,
00747 cook_gains *gains_ptr, float* previous_buffer)
00748 {
00749 float *buffer0 = q->mono_mdct_output;
00750 float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
00751 int i;
00752
00753
00754 ff_imdct_calc(&q->mdct_ctx, q->mono_mdct_output, inbuffer);
00755
00756 q->imlt_window (q, buffer1, gains_ptr, previous_buffer);
00757
00758
00759 for (i = 0; i < 8; i++) {
00760 if (gains_ptr->now[i] || gains_ptr->now[i + 1])
00761 q->interpolate(q, &buffer1[q->gain_size_factor * i],
00762 gains_ptr->now[i], gains_ptr->now[i + 1]);
00763 }
00764
00765
00766 memcpy(previous_buffer, buffer0, sizeof(float)*q->samples_per_channel);
00767 }
00768
00769
00778 static void decouple_info(COOKContext *q, COOKSubpacket *p, int* decouple_tab){
00779 int length, i;
00780
00781 if(get_bits1(&q->gb)) {
00782 if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return;
00783
00784 length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1;
00785 for (i=0 ; i<length ; i++) {
00786 decouple_tab[cplband[p->js_subband_start] + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2);
00787 }
00788 return;
00789 }
00790
00791 if(cplband[p->js_subband_start] > cplband[p->subbands-1]) return;
00792
00793 length = cplband[p->subbands-1] - cplband[p->js_subband_start] + 1;
00794 for (i=0 ; i<length ; i++) {
00795 decouple_tab[cplband[p->js_subband_start] + i] = get_bits(&q->gb, p->js_vlc_bits);
00796 }
00797 return;
00798 }
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 static void decouple_float (COOKContext *q,
00812 COOKSubpacket *p,
00813 int subband,
00814 float f1, float f2,
00815 float *decode_buffer,
00816 float *mlt_buffer1, float *mlt_buffer2)
00817 {
00818 int j, tmp_idx;
00819 for (j=0 ; j<SUBBAND_SIZE ; j++) {
00820 tmp_idx = ((p->js_subband_start + subband)*SUBBAND_SIZE)+j;
00821 mlt_buffer1[SUBBAND_SIZE*subband + j] = f1 * decode_buffer[tmp_idx];
00822 mlt_buffer2[SUBBAND_SIZE*subband + j] = f2 * decode_buffer[tmp_idx];
00823 }
00824 }
00825
00834 static void joint_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer1,
00835 float* mlt_buffer2) {
00836 int i,j;
00837 int decouple_tab[SUBBAND_SIZE];
00838 float *decode_buffer = q->decode_buffer_0;
00839 int idx, cpl_tmp;
00840 float f1,f2;
00841 const float* cplscale;
00842
00843 memset(decouple_tab, 0, sizeof(decouple_tab));
00844 memset(decode_buffer, 0, sizeof(decode_buffer));
00845
00846
00847 memset(mlt_buffer1,0, 1024*sizeof(float));
00848 memset(mlt_buffer2,0, 1024*sizeof(float));
00849 decouple_info(q, p, decouple_tab);
00850 mono_decode(q, p, decode_buffer);
00851
00852
00853 for (i=0 ; i<p->js_subband_start ; i++) {
00854 for (j=0 ; j<SUBBAND_SIZE ; j++) {
00855 mlt_buffer1[i*20+j] = decode_buffer[i*40+j];
00856 mlt_buffer2[i*20+j] = decode_buffer[i*40+20+j];
00857 }
00858 }
00859
00860
00861
00862 idx = (1 << p->js_vlc_bits) - 1;
00863 for (i=p->js_subband_start ; i<p->subbands ; i++) {
00864 cpl_tmp = cplband[i];
00865 idx -=decouple_tab[cpl_tmp];
00866 cplscale = q->cplscales[p->js_vlc_bits-2];
00867 f1 = cplscale[decouple_tab[cpl_tmp]];
00868 f2 = cplscale[idx-1];
00869 q->decouple (q, p, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2);
00870 idx = (1 << p->js_vlc_bits) - 1;
00871 }
00872 }
00873
00883 static inline void
00884 decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p, const uint8_t *inbuffer,
00885 cook_gains *gains_ptr)
00886 {
00887 int offset;
00888
00889 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
00890 p->bits_per_subpacket/8);
00891 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
00892 p->bits_per_subpacket);
00893 decode_gain_info(&q->gb, gains_ptr->now);
00894
00895
00896 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
00897 }
00898
00906 static void
00907 saturate_output_float (COOKContext *q, int chan, int16_t *out)
00908 {
00909 int j;
00910 float *output = q->mono_mdct_output + q->samples_per_channel;
00911
00912
00913 for (j = 0; j < q->samples_per_channel; j++) {
00914 out[chan + q->nb_channels * j] =
00915 av_clip_int16(lrintf(output[j]));
00916 }
00917 }
00918
00932 static inline void
00933 mlt_compensate_output(COOKContext *q, float *decode_buffer,
00934 cook_gains *gains, float *previous_buffer,
00935 int16_t *out, int chan)
00936 {
00937 imlt_gain(q, decode_buffer, gains, previous_buffer);
00938 q->saturate_output (q, chan, out);
00939 }
00940
00941
00953 static void decode_subpacket(COOKContext *q, COOKSubpacket* p, const uint8_t *inbuffer, int16_t *outbuffer) {
00954 int sub_packet_size = p->size;
00955
00956
00957
00958
00959
00960 memset(q->decode_buffer_1,0,sizeof(q->decode_buffer_1));
00961 decode_bytes_and_gain(q, p, inbuffer, &p->gains1);
00962
00963 if (p->joint_stereo) {
00964 joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2);
00965 } else {
00966 mono_decode(q, p, q->decode_buffer_1);
00967
00968 if (p->num_channels == 2) {
00969 decode_bytes_and_gain(q, p, inbuffer + sub_packet_size/2, &p->gains2);
00970 mono_decode(q, p, q->decode_buffer_2);
00971 }
00972 }
00973
00974 mlt_compensate_output(q, q->decode_buffer_1, &p->gains1,
00975 p->mono_previous_buffer1, outbuffer, p->ch_idx);
00976
00977 if (p->num_channels == 2) {
00978 if (p->joint_stereo) {
00979 mlt_compensate_output(q, q->decode_buffer_2, &p->gains1,
00980 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);
00981 } else {
00982 mlt_compensate_output(q, q->decode_buffer_2, &p->gains2,
00983 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);
00984 }
00985 }
00986
00987 }
00988
00989
00996 static int cook_decode_frame(AVCodecContext *avctx,
00997 void *data, int *data_size,
00998 AVPacket *avpkt) {
00999 const uint8_t *buf = avpkt->data;
01000 int buf_size = avpkt->size;
01001 COOKContext *q = avctx->priv_data;
01002 int i;
01003 int offset = 0;
01004 int chidx = 0;
01005
01006 if (buf_size < avctx->block_align)
01007 return buf_size;
01008
01009
01010 q->subpacket[0].size = avctx->block_align;
01011
01012 for(i=1;i<q->num_subpackets;i++){
01013 q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i];
01014 q->subpacket[0].size -= q->subpacket[i].size + 1;
01015 if (q->subpacket[0].size < 0) {
01016 av_log(avctx,AV_LOG_DEBUG,"frame subpacket size total > avctx->block_align!\n");
01017 return -1;
01018 }
01019 }
01020
01021
01022 *data_size = 0;
01023 for(i=0;i<q->num_subpackets;i++){
01024 q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size*8)>>q->subpacket[i].bits_per_subpdiv;
01025 q->subpacket[i].ch_idx = chidx;
01026 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);
01027 decode_subpacket(q, &q->subpacket[i], buf + offset, (int16_t*)data);
01028 offset += q->subpacket[i].size;
01029 chidx += q->subpacket[i].num_channels;
01030 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] %i %i\n",i,q->subpacket[i].size * 8,get_bits_count(&q->gb));
01031 }
01032 *data_size = sizeof(int16_t) * q->nb_channels * q->samples_per_channel;
01033
01034
01035 if (avctx->frame_number < 2) *data_size = 0;
01036
01037 return avctx->block_align;
01038 }
01039
01040 #ifdef COOKDEBUG
01041 static void dump_cook_context(COOKContext *q)
01042 {
01043
01044 #define PRINT(a,b) av_log(q->avctx,AV_LOG_ERROR," %s = %d\n", a, b);
01045 av_log(q->avctx,AV_LOG_ERROR,"COOKextradata\n");
01046 av_log(q->avctx,AV_LOG_ERROR,"cookversion=%x\n",q->subpacket[0].cookversion);
01047 if (q->subpacket[0].cookversion > STEREO) {
01048 PRINT("js_subband_start",q->subpacket[0].js_subband_start);
01049 PRINT("js_vlc_bits",q->subpacket[0].js_vlc_bits);
01050 }
01051 av_log(q->avctx,AV_LOG_ERROR,"COOKContext\n");
01052 PRINT("nb_channels",q->nb_channels);
01053 PRINT("bit_rate",q->bit_rate);
01054 PRINT("sample_rate",q->sample_rate);
01055 PRINT("samples_per_channel",q->subpacket[0].samples_per_channel);
01056 PRINT("samples_per_frame",q->subpacket[0].samples_per_frame);
01057 PRINT("subbands",q->subpacket[0].subbands);
01058 PRINT("random_state",q->random_state);
01059 PRINT("js_subband_start",q->subpacket[0].js_subband_start);
01060 PRINT("log2_numvector_size",q->subpacket[0].log2_numvector_size);
01061 PRINT("numvector_size",q->subpacket[0].numvector_size);
01062 PRINT("total_subbands",q->subpacket[0].total_subbands);
01063 }
01064 #endif
01065
01066 static av_cold int cook_count_channels(unsigned int mask){
01067 int i;
01068 int channels = 0;
01069 for(i = 0;i<32;i++){
01070 if(mask & (1<<i))
01071 ++channels;
01072 }
01073 return channels;
01074 }
01075
01082 static av_cold int cook_decode_init(AVCodecContext *avctx)
01083 {
01084 COOKContext *q = avctx->priv_data;
01085 const uint8_t *edata_ptr = avctx->extradata;
01086 const uint8_t *edata_ptr_end = edata_ptr + avctx->extradata_size;
01087 int extradata_size = avctx->extradata_size;
01088 int s = 0;
01089 unsigned int channel_mask = 0;
01090 q->avctx = avctx;
01091
01092
01093 if (extradata_size <= 0) {
01094 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n");
01095 return -1;
01096 }
01097 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size);
01098
01099
01100 q->sample_rate = avctx->sample_rate;
01101 q->nb_channels = avctx->channels;
01102 q->bit_rate = avctx->bit_rate;
01103
01104
01105 av_lfg_init(&q->random_state, 0);
01106
01107 while(edata_ptr < edata_ptr_end){
01108
01109
01110 if (extradata_size >= 8){
01111 q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr);
01112 q->subpacket[s].samples_per_frame = bytestream_get_be16(&edata_ptr);
01113 q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr);
01114 extradata_size -= 8;
01115 }
01116 if (avctx->extradata_size >= 8){
01117 bytestream_get_be32(&edata_ptr);
01118 q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr);
01119 q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr);
01120 extradata_size -= 8;
01121 }
01122
01123
01124 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame / q->nb_channels;
01125 q->subpacket[s].bits_per_subpacket = avctx->block_align * 8;
01126
01127
01128 q->subpacket[s].log2_numvector_size = 5;
01129 q->subpacket[s].total_subbands = q->subpacket[s].subbands;
01130 q->subpacket[s].num_channels = 1;
01131
01132
01133
01134 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i].cookversion=%x\n",s,q->subpacket[s].cookversion);
01135 q->subpacket[s].joint_stereo = 0;
01136 switch (q->subpacket[s].cookversion) {
01137 case MONO:
01138 if (q->nb_channels != 1) {
01139 av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n");
01140 return -1;
01141 }
01142 av_log(avctx,AV_LOG_DEBUG,"MONO\n");
01143 break;
01144 case STEREO:
01145 if (q->nb_channels != 1) {
01146 q->subpacket[s].bits_per_subpdiv = 1;
01147 q->subpacket[s].num_channels = 2;
01148 }
01149 av_log(avctx,AV_LOG_DEBUG,"STEREO\n");
01150 break;
01151 case JOINT_STEREO:
01152 if (q->nb_channels != 2) {
01153 av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n");
01154 return -1;
01155 }
01156 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n");
01157 if (avctx->extradata_size >= 16){
01158 q->subpacket[s].total_subbands = q->subpacket[s].subbands + q->subpacket[s].js_subband_start;
01159 q->subpacket[s].joint_stereo = 1;
01160 q->subpacket[s].num_channels = 2;
01161 }
01162 if (q->subpacket[s].samples_per_channel > 256) {
01163 q->subpacket[s].log2_numvector_size = 6;
01164 }
01165 if (q->subpacket[s].samples_per_channel > 512) {
01166 q->subpacket[s].log2_numvector_size = 7;
01167 }
01168 break;
01169 case MC_COOK:
01170 av_log(avctx,AV_LOG_DEBUG,"MULTI_CHANNEL\n");
01171 if(extradata_size >= 4)
01172 channel_mask |= q->subpacket[s].channel_mask = bytestream_get_be32(&edata_ptr);
01173
01174 if(cook_count_channels(q->subpacket[s].channel_mask) > 1){
01175 q->subpacket[s].total_subbands = q->subpacket[s].subbands + q->subpacket[s].js_subband_start;
01176 q->subpacket[s].joint_stereo = 1;
01177 q->subpacket[s].num_channels = 2;
01178 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame >> 1;
01179
01180 if (q->subpacket[s].samples_per_channel > 256) {
01181 q->subpacket[s].log2_numvector_size = 6;
01182 }
01183 if (q->subpacket[s].samples_per_channel > 512) {
01184 q->subpacket[s].log2_numvector_size = 7;
01185 }
01186 }else
01187 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame;
01188
01189 break;
01190 default:
01191 av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n");
01192 return -1;
01193 break;
01194 }
01195
01196 if(s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) {
01197 av_log(avctx,AV_LOG_ERROR,"different number of samples per channel!\n");
01198 return -1;
01199 } else
01200 q->samples_per_channel = q->subpacket[0].samples_per_channel;
01201
01202
01203
01204 q->subpacket[s].numvector_size = (1 << q->subpacket[s].log2_numvector_size);
01205
01206
01207 if (q->subpacket[s].total_subbands > 53) {
01208 av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n");
01209 return -1;
01210 }
01211
01212 if ((q->subpacket[s].js_vlc_bits > 6) || (q->subpacket[s].js_vlc_bits < 0)) {
01213 av_log(avctx,AV_LOG_ERROR,"js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->subpacket[s].js_vlc_bits);
01214 return -1;
01215 }
01216
01217 if (q->subpacket[s].subbands > 50) {
01218 av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n");
01219 return -1;
01220 }
01221 q->subpacket[s].gains1.now = q->subpacket[s].gain_1;
01222 q->subpacket[s].gains1.previous = q->subpacket[s].gain_2;
01223 q->subpacket[s].gains2.now = q->subpacket[s].gain_3;
01224 q->subpacket[s].gains2.previous = q->subpacket[s].gain_4;
01225
01226 q->num_subpackets++;
01227 s++;
01228 if (s > MAX_SUBPACKETS) {
01229 av_log(avctx,AV_LOG_ERROR,"Too many subpackets > 5, report file!\n");
01230 return -1;
01231 }
01232 }
01233
01234 init_pow2table();
01235 init_gain_table(q);
01236 init_cplscales_table(q);
01237
01238 if (init_cook_vlc_tables(q) != 0)
01239 return -1;
01240
01241
01242 if(avctx->block_align >= UINT_MAX/2)
01243 return -1;
01244
01245
01246
01247
01248 q->decoded_bytes_buffer =
01249 av_mallocz(avctx->block_align
01250 + DECODE_BYTES_PAD1(avctx->block_align)
01251 + FF_INPUT_BUFFER_PADDING_SIZE);
01252 if (q->decoded_bytes_buffer == NULL)
01253 return -1;
01254
01255
01256 if ( init_cook_mlt(q) != 0 )
01257 return -1;
01258
01259
01260 if (1) {
01261 q->scalar_dequant = scalar_dequant_float;
01262 q->decouple = decouple_float;
01263 q->imlt_window = imlt_window_float;
01264 q->interpolate = interpolate_float;
01265 q->saturate_output = saturate_output_float;
01266 }
01267
01268
01269 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
01270 } else {
01271 av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
01272 return -1;
01273 }
01274
01275 avctx->sample_fmt = SAMPLE_FMT_S16;
01276 if (channel_mask)
01277 avctx->channel_layout = channel_mask;
01278 else
01279 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
01280
01281 #ifdef COOKDEBUG
01282 dump_cook_context(q);
01283 #endif
01284 return 0;
01285 }
01286
01287
01288 AVCodec cook_decoder =
01289 {
01290 .name = "cook",
01291 .type = AVMEDIA_TYPE_AUDIO,
01292 .id = CODEC_ID_COOK,
01293 .priv_data_size = sizeof(COOKContext),
01294 .init = cook_decode_init,
01295 .close = cook_decode_close,
01296 .decode = cook_decode_frame,
01297 .long_name = NULL_IF_CONFIG_SMALL("COOK"),
01298 };