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 #include "libavutil/audioconvert.h"
00057 #include "sinewin.h"
00058
00059 #include "cookdata.h"
00060
00061
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
00108
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
00132 int nb_channels;
00133 int bit_rate;
00134 int sample_rate;
00135 int num_vectors;
00136 int samples_per_channel;
00137
00138 AVLFG random_state;
00139
00140
00141 FFTContext mdct_ctx;
00142 float* mlt_window;
00143
00144
00145 VLC envelope_quant_index[13];
00146 VLC sqvh[7];
00147
00148
00149 int gain_size_factor;
00150 float gain_table[23];
00151
00152
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];
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
00169
00170
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
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
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
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
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
00286
00287
00288
00289
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
00312 av_free(q->mlt_window);
00313 av_free(q->decoded_bytes_buffer);
00314
00315
00316 ff_mdct_end(&q->mdct_ctx);
00317
00318
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;
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;
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;
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;
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
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
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
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
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
00592
00593 int subband_coef_index[SUBBAND_SIZE];
00594
00595
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 }
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){
00663 for(i=0 ; i<q->gain_size_factor ; i++){
00664 buffer[i]*=fc1;
00665 }
00666 return;
00667 } else {
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
00692
00693
00694
00695
00696
00697
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
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
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
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
00772
00773
00774
00775
00776
00777
00778
00779
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
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
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
00831
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];
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
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
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
00923
00924
00925
00926
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
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
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
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
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
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
01066 q->sample_rate = avctx->sample_rate;
01067 q->nb_channels = avctx->channels;
01068 q->bit_rate = avctx->bit_rate;
01069 if (!q->nb_channels) {
01070 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
01071 return AVERROR_INVALIDDATA;
01072 }
01073
01074
01075 av_lfg_init(&q->random_state, 0);
01076
01077 while(edata_ptr < edata_ptr_end){
01078
01079
01080 if (extradata_size >= 8){
01081 q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr);
01082 q->subpacket[s].samples_per_frame = bytestream_get_be16(&edata_ptr);
01083 q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr);
01084 extradata_size -= 8;
01085 }
01086 if (extradata_size >= 8){
01087 bytestream_get_be32(&edata_ptr);
01088 q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr);
01089 q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr);
01090 extradata_size -= 8;
01091 }
01092
01093
01094 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame / q->nb_channels;
01095 q->subpacket[s].bits_per_subpacket = avctx->block_align * 8;
01096
01097
01098 q->subpacket[s].log2_numvector_size = 5;
01099 q->subpacket[s].total_subbands = q->subpacket[s].subbands;
01100 q->subpacket[s].num_channels = 1;
01101
01102
01103
01104 av_log(avctx,AV_LOG_DEBUG,"subpacket[%i].cookversion=%x\n",s,q->subpacket[s].cookversion);
01105 q->subpacket[s].joint_stereo = 0;
01106 switch (q->subpacket[s].cookversion) {
01107 case MONO:
01108 if (q->nb_channels != 1) {
01109 av_log_ask_for_sample(avctx, "Container channels != 1.\n");
01110 return -1;
01111 }
01112 av_log(avctx,AV_LOG_DEBUG,"MONO\n");
01113 break;
01114 case STEREO:
01115 if (q->nb_channels != 1) {
01116 q->subpacket[s].bits_per_subpdiv = 1;
01117 q->subpacket[s].num_channels = 2;
01118 }
01119 av_log(avctx,AV_LOG_DEBUG,"STEREO\n");
01120 break;
01121 case JOINT_STEREO:
01122 if (q->nb_channels != 2) {
01123 av_log_ask_for_sample(avctx, "Container channels != 2.\n");
01124 return -1;
01125 }
01126 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n");
01127 if (avctx->extradata_size >= 16){
01128 q->subpacket[s].total_subbands = q->subpacket[s].subbands + q->subpacket[s].js_subband_start;
01129 q->subpacket[s].joint_stereo = 1;
01130 q->subpacket[s].num_channels = 2;
01131 }
01132 if (q->subpacket[s].samples_per_channel > 256) {
01133 q->subpacket[s].log2_numvector_size = 6;
01134 }
01135 if (q->subpacket[s].samples_per_channel > 512) {
01136 q->subpacket[s].log2_numvector_size = 7;
01137 }
01138 break;
01139 case MC_COOK:
01140 av_log(avctx,AV_LOG_DEBUG,"MULTI_CHANNEL\n");
01141 if(extradata_size >= 4)
01142 channel_mask |= q->subpacket[s].channel_mask = bytestream_get_be32(&edata_ptr);
01143
01144 if(cook_count_channels(q->subpacket[s].channel_mask) > 1){
01145 q->subpacket[s].total_subbands = q->subpacket[s].subbands + q->subpacket[s].js_subband_start;
01146 q->subpacket[s].joint_stereo = 1;
01147 q->subpacket[s].num_channels = 2;
01148 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame >> 1;
01149
01150 if (q->subpacket[s].samples_per_channel > 256) {
01151 q->subpacket[s].log2_numvector_size = 6;
01152 }
01153 if (q->subpacket[s].samples_per_channel > 512) {
01154 q->subpacket[s].log2_numvector_size = 7;
01155 }
01156 }else
01157 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame;
01158
01159 break;
01160 default:
01161 av_log_ask_for_sample(avctx, "Unknown Cook version.\n");
01162 return -1;
01163 break;
01164 }
01165
01166 if(s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) {
01167 av_log(avctx,AV_LOG_ERROR,"different number of samples per channel!\n");
01168 return -1;
01169 } else
01170 q->samples_per_channel = q->subpacket[0].samples_per_channel;
01171
01172
01173
01174 q->subpacket[s].numvector_size = (1 << q->subpacket[s].log2_numvector_size);
01175
01176
01177 if (q->subpacket[s].total_subbands > 53) {
01178 av_log_ask_for_sample(avctx, "total_subbands > 53\n");
01179 return -1;
01180 }
01181
01182 if ((q->subpacket[s].js_vlc_bits > 6) || (q->subpacket[s].js_vlc_bits < 2*q->subpacket[s].joint_stereo)) {
01183 av_log(avctx,AV_LOG_ERROR,"js_vlc_bits = %d, only >= %d and <= 6 allowed!\n",
01184 q->subpacket[s].js_vlc_bits, 2*q->subpacket[s].joint_stereo);
01185 return -1;
01186 }
01187
01188 if (q->subpacket[s].subbands > 50) {
01189 av_log_ask_for_sample(avctx, "subbands > 50\n");
01190 return -1;
01191 }
01192 q->subpacket[s].gains1.now = q->subpacket[s].gain_1;
01193 q->subpacket[s].gains1.previous = q->subpacket[s].gain_2;
01194 q->subpacket[s].gains2.now = q->subpacket[s].gain_3;
01195 q->subpacket[s].gains2.previous = q->subpacket[s].gain_4;
01196
01197 q->num_subpackets++;
01198 s++;
01199 if (s > MAX_SUBPACKETS) {
01200 av_log_ask_for_sample(avctx, "Too many subpackets > 5\n");
01201 return -1;
01202 }
01203 }
01204
01205 init_pow2table();
01206 init_gain_table(q);
01207 init_cplscales_table(q);
01208
01209 if (init_cook_vlc_tables(q) != 0)
01210 return -1;
01211
01212
01213 if(avctx->block_align >= UINT_MAX/2)
01214 return -1;
01215
01216
01217
01218
01219 q->decoded_bytes_buffer =
01220 av_mallocz(avctx->block_align
01221 + DECODE_BYTES_PAD1(avctx->block_align)
01222 + FF_INPUT_BUFFER_PADDING_SIZE);
01223 if (q->decoded_bytes_buffer == NULL)
01224 return -1;
01225
01226
01227 if ( init_cook_mlt(q) != 0 )
01228 return -1;
01229
01230
01231 if (1) {
01232 q->scalar_dequant = scalar_dequant_float;
01233 q->decouple = decouple_float;
01234 q->imlt_window = imlt_window_float;
01235 q->interpolate = interpolate_float;
01236 q->saturate_output = saturate_output_float;
01237 }
01238
01239
01240 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
01241 } else {
01242 av_log_ask_for_sample(avctx,
01243 "unknown amount of samples_per_channel = %d\n",
01244 q->samples_per_channel);
01245 return -1;
01246 }
01247
01248 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01249 if (channel_mask)
01250 avctx->channel_layout = channel_mask;
01251 else
01252 avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
01253
01254 #ifdef DEBUG
01255 dump_cook_context(q);
01256 #endif
01257 return 0;
01258 }
01259
01260
01261 AVCodec ff_cook_decoder =
01262 {
01263 .name = "cook",
01264 .type = AVMEDIA_TYPE_AUDIO,
01265 .id = CODEC_ID_COOK,
01266 .priv_data_size = sizeof(COOKContext),
01267 .init = cook_decode_init,
01268 .close = cook_decode_close,
01269 .decode = cook_decode_frame,
01270 .long_name = NULL_IF_CONFIG_SMALL("COOK"),
01271 };