00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00045 #include "libavutil/lfg.h"
00046 #include "avcodec.h"
00047 #include "get_bits.h"
00048 #include "dsputil.h"
00049 #include "bytestream.h"
00050 #include "fft.h"
00051 #include "libavutil/audioconvert.h"
00052 #include "sinewin.h"
00053
00054 #include "cookdata.h"
00055
00056
00057 #define MONO 0x1000001
00058 #define STEREO 0x1000002
00059 #define JOINT_STEREO 0x1000003
00060 #define MC_COOK 0x2000000 // multichannel Cook, not supported
00061
00062 #define SUBBAND_SIZE 20
00063 #define MAX_SUBPACKETS 5
00064
00065 typedef struct {
00066 int *now;
00067 int *previous;
00068 } cook_gains;
00069
00070 typedef struct {
00071 int ch_idx;
00072 int size;
00073 int num_channels;
00074 int cookversion;
00075 int samples_per_frame;
00076 int subbands;
00077 int js_subband_start;
00078 int js_vlc_bits;
00079 int samples_per_channel;
00080 int log2_numvector_size;
00081 unsigned int channel_mask;
00082 VLC ccpl;
00083 int joint_stereo;
00084 int bits_per_subpacket;
00085 int bits_per_subpdiv;
00086 int total_subbands;
00087 int numvector_size;
00088
00089 float mono_previous_buffer1[1024];
00090 float mono_previous_buffer2[1024];
00092 cook_gains gains1;
00093 cook_gains gains2;
00094 int gain_1[9];
00095 int gain_2[9];
00096 int gain_3[9];
00097 int gain_4[9];
00098 } COOKSubpacket;
00099
00100 typedef struct cook {
00101
00102
00103
00104
00105 void (*scalar_dequant)(struct cook *q, int index, int quant_index,
00106 int *subband_coef_index, int *subband_coef_sign,
00107 float *mlt_p);
00108
00109 void (*decouple)(struct cook *q,
00110 COOKSubpacket *p,
00111 int subband,
00112 float f1, float f2,
00113 float *decode_buffer,
00114 float *mlt_buffer1, float *mlt_buffer2);
00115
00116 void (*imlt_window)(struct cook *q, float *buffer1,
00117 cook_gains *gains_ptr, float *previous_buffer);
00118
00119 void (*interpolate)(struct cook *q, float *buffer,
00120 int gain_index, int gain_index_next);
00121
00122 void (*saturate_output)(struct cook *q, int chan, float *out);
00123
00124 AVCodecContext* avctx;
00125 AVFrame frame;
00126 GetBitContext gb;
00127
00128 int nb_channels;
00129 int bit_rate;
00130 int sample_rate;
00131 int num_vectors;
00132 int samples_per_channel;
00133
00134 AVLFG random_state;
00135 int discarded_packets;
00136
00137
00138 FFTContext mdct_ctx;
00139 float* mlt_window;
00140
00141
00142 VLC envelope_quant_index[13];
00143 VLC sqvh[7];
00144
00145
00146 int gain_size_factor;
00147 float gain_table[23];
00148
00149
00150
00151 uint8_t* decoded_bytes_buffer;
00152 DECLARE_ALIGNED(32, float, mono_mdct_output)[2048];
00153 float decode_buffer_1[1024];
00154 float decode_buffer_2[1024];
00155 float decode_buffer_0[1060];
00156
00157 const float *cplscales[5];
00158 int num_subpackets;
00159 COOKSubpacket subpacket[MAX_SUBPACKETS];
00160 } COOKContext;
00161
00162 static float pow2tab[127];
00163 static float rootpow2tab[127];
00164
00165
00166
00167
00168 static av_cold void init_pow2table(void)
00169 {
00170 int i;
00171 for (i = -63; i < 64; i++) {
00172 pow2tab[63 + i] = pow(2, i);
00173 rootpow2tab[63 + i] = sqrt(pow(2, i));
00174 }
00175 }
00176
00177
00178 static av_cold void init_gain_table(COOKContext *q)
00179 {
00180 int i;
00181 q->gain_size_factor = q->samples_per_channel / 8;
00182 for (i = 0; i < 23; i++)
00183 q->gain_table[i] = pow(pow2tab[i + 52],
00184 (1.0 / (double) q->gain_size_factor));
00185 }
00186
00187
00188 static av_cold int init_cook_vlc_tables(COOKContext *q)
00189 {
00190 int i, result;
00191
00192 result = 0;
00193 for (i = 0; i < 13; i++) {
00194 result |= init_vlc(&q->envelope_quant_index[i], 9, 24,
00195 envelope_quant_index_huffbits[i], 1, 1,
00196 envelope_quant_index_huffcodes[i], 2, 2, 0);
00197 }
00198 av_log(q->avctx, AV_LOG_DEBUG, "sqvh VLC init\n");
00199 for (i = 0; i < 7; i++) {
00200 result |= init_vlc(&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
00201 cvh_huffbits[i], 1, 1,
00202 cvh_huffcodes[i], 2, 2, 0);
00203 }
00204
00205 for (i = 0; i < q->num_subpackets; i++) {
00206 if (q->subpacket[i].joint_stereo == 1) {
00207 result |= init_vlc(&q->subpacket[i].ccpl, 6, (1 << q->subpacket[i].js_vlc_bits) - 1,
00208 ccpl_huffbits[q->subpacket[i].js_vlc_bits - 2], 1, 1,
00209 ccpl_huffcodes[q->subpacket[i].js_vlc_bits - 2], 2, 2, 0);
00210 av_log(q->avctx, AV_LOG_DEBUG, "subpacket %i Joint-stereo VLC used.\n", i);
00211 }
00212 }
00213
00214 av_log(q->avctx, AV_LOG_DEBUG, "VLC tables initialized.\n");
00215 return result;
00216 }
00217
00218 static av_cold int init_cook_mlt(COOKContext *q)
00219 {
00220 int j, ret;
00221 int mlt_size = q->samples_per_channel;
00222
00223 if ((q->mlt_window = av_malloc(mlt_size * sizeof(*q->mlt_window))) == 0)
00224 return AVERROR(ENOMEM);
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 ((ret = ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size) + 1, 1, 1.0 / 32768.0))) {
00233 av_free(q->mlt_window);
00234 return ret;
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 {
00250 int i;
00251 for (i = 0; i < 5; i++)
00252 q->cplscales[i] = maybe_reformat_buffer32(q, cplscales[i], (1 << (i + 2)) - 1);
00253 }
00254
00255
00256
00257 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes) + 3) % 4)
00258 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
00259
00280 static inline int decode_bytes(const uint8_t *inbuffer, uint8_t *out, int bytes)
00281 {
00282 static const uint32_t tab[4] = {
00283 AV_BE2NE32C(0x37c511f2u), AV_BE2NE32C(0xf237c511u),
00284 AV_BE2NE32C(0x11f237c5u), AV_BE2NE32C(0xc511f237u),
00285 };
00286 int i, off;
00287 uint32_t c;
00288 const uint32_t *buf;
00289 uint32_t *obuf = (uint32_t *) out;
00290
00291
00292
00293
00294
00295
00296 off = (intptr_t) inbuffer & 3;
00297 buf = (const uint32_t *) (inbuffer - off);
00298 c = tab[off];
00299 bytes += 3 + off;
00300 for (i = 0; i < bytes / 4; i++)
00301 obuf[i] = c ^ buf[i];
00302
00303 return off;
00304 }
00305
00309 static av_cold int cook_decode_close(AVCodecContext *avctx)
00310 {
00311 int i;
00312 COOKContext *q = avctx->priv_data;
00313 av_log(avctx, AV_LOG_DEBUG, "Deallocating memory.\n");
00314
00315
00316 av_free(q->mlt_window);
00317 av_free(q->decoded_bytes_buffer);
00318
00319
00320 ff_mdct_end(&q->mdct_ctx);
00321
00322
00323 for (i = 0; i < 13; i++)
00324 ff_free_vlc(&q->envelope_quant_index[i]);
00325 for (i = 0; i < 7; i++)
00326 ff_free_vlc(&q->sqvh[i]);
00327 for (i = 0; i < q->num_subpackets; i++)
00328 ff_free_vlc(&q->subpacket[i].ccpl);
00329
00330 av_log(avctx, AV_LOG_DEBUG, "Memory deallocated.\n");
00331
00332 return 0;
00333 }
00334
00341 static void decode_gain_info(GetBitContext *gb, int *gaininfo)
00342 {
00343 int i, n;
00344
00345 while (get_bits1(gb)) {
00346
00347 }
00348
00349 n = get_bits_count(gb) - 1;
00350
00351 i = 0;
00352 while (n--) {
00353 int index = get_bits(gb, 3);
00354 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;
00355
00356 while (i <= index)
00357 gaininfo[i++] = gain;
00358 }
00359 while (i <= 8)
00360 gaininfo[i++] = 0;
00361 }
00362
00369 static int decode_envelope(COOKContext *q, COOKSubpacket *p,
00370 int *quant_index_table)
00371 {
00372 int i, j, vlc_index;
00373
00374 quant_index_table[0] = get_bits(&q->gb, 6) - 6;
00375
00376 for (i = 1; i < p->total_subbands; i++) {
00377 vlc_index = i;
00378 if (i >= p->js_subband_start * 2) {
00379 vlc_index -= p->js_subband_start;
00380 } else {
00381 vlc_index /= 2;
00382 if (vlc_index < 1)
00383 vlc_index = 1;
00384 }
00385 if (vlc_index > 13)
00386 vlc_index = 13;
00387
00388 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index - 1].table,
00389 q->envelope_quant_index[vlc_index - 1].bits, 2);
00390 quant_index_table[i] = quant_index_table[i - 1] + j - 12;
00391 if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
00392 av_log(q->avctx, AV_LOG_ERROR,
00393 "Invalid quantizer %d at position %d, outside [-63, 63] range\n",
00394 quant_index_table[i], i);
00395 return AVERROR_INVALIDDATA;
00396 }
00397 }
00398
00399 return 0;
00400 }
00401
00410 static void categorize(COOKContext *q, COOKSubpacket *p, const int *quant_index_table,
00411 int *category, int *category_index)
00412 {
00413 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
00414 int exp_index2[102] = { 0 };
00415 int exp_index1[102] = { 0 };
00416
00417 int tmp_categorize_array[128 * 2] = { 0 };
00418 int tmp_categorize_array1_idx = p->numvector_size;
00419 int tmp_categorize_array2_idx = p->numvector_size;
00420
00421 bits_left = p->bits_per_subpacket - get_bits_count(&q->gb);
00422
00423 if (bits_left > q->samples_per_channel) {
00424 bits_left = q->samples_per_channel +
00425 ((bits_left - q->samples_per_channel) * 5) / 8;
00426
00427 }
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 = p->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 num_bits = 0;
00446 for (i = 0; i < p->total_subbands; i++) {
00447 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
00448 num_bits += expbits_tab[exp_idx];
00449 exp_index1[i] = exp_idx;
00450 exp_index2[i] = exp_idx;
00451 }
00452 tmpbias1 = tmpbias2 = num_bits;
00453
00454 for (j = 1; j < p->numvector_size; j++) {
00455 if (tmpbias1 + tmpbias2 > 2 * bits_left) {
00456 int max = -999999;
00457 index = -1;
00458 for (i = 0; i < p->total_subbands; i++) {
00459 if (exp_index1[i] < 7) {
00460 v = (-2 * exp_index1[i]) - quant_index_table[i] + bias;
00461 if (v >= max) {
00462 max = v;
00463 index = i;
00464 }
00465 }
00466 }
00467 if (index == -1)
00468 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 < p->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)
00486 break;
00487 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
00488 tmpbias2 -= expbits_tab[exp_index2[index]] -
00489 expbits_tab[exp_index2[index] - 1];
00490 --exp_index2[index];
00491 }
00492 }
00493
00494 for (i = 0; i < p->total_subbands; i++)
00495 category[i] = exp_index2[i];
00496
00497 for (i = 0; i < p->numvector_size - 1; i++)
00498 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
00499 }
00500
00501
00509 static inline void expand_category(COOKContext *q, int *category,
00510 int *category_index)
00511 {
00512 int i;
00513 for (i = 0; i < q->num_vectors; i++)
00514 {
00515 int idx = category_index[i];
00516 if (++category[idx] >= FF_ARRAY_ELEMS(dither_tab))
00517 --category[idx];
00518 }
00519 }
00520
00531 static void scalar_dequant_float(COOKContext *q, int index, int quant_index,
00532 int *subband_coef_index, int *subband_coef_sign,
00533 float *mlt_p)
00534 {
00535 int i;
00536 float f1;
00537
00538 for (i = 0; i < SUBBAND_SIZE; i++) {
00539 if (subband_coef_index[i]) {
00540 f1 = quant_centroid_tab[index][subband_coef_index[i]];
00541 if (subband_coef_sign[i])
00542 f1 = -f1;
00543 } else {
00544
00545 f1 = dither_tab[index];
00546 if (av_lfg_get(&q->random_state) < 0x80000000)
00547 f1 = -f1;
00548 }
00549 mlt_p[i] = f1 * rootpow2tab[quant_index + 63];
00550 }
00551 }
00560 static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category,
00561 int *subband_coef_index, int *subband_coef_sign)
00562 {
00563 int i, j;
00564 int vlc, vd, tmp, result;
00565
00566 vd = vd_tab[category];
00567 result = 0;
00568 for (i = 0; i < vpr_tab[category]; i++) {
00569 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
00570 if (p->bits_per_subpacket < get_bits_count(&q->gb)) {
00571 vlc = 0;
00572 result = 1;
00573 }
00574 for (j = vd - 1; j >= 0; j--) {
00575 tmp = (vlc * invradix_tab[category]) / 0x100000;
00576 subband_coef_index[vd * i + j] = vlc - tmp * (kmax_tab[category] + 1);
00577 vlc = tmp;
00578 }
00579 for (j = 0; j < vd; j++) {
00580 if (subband_coef_index[i * vd + j]) {
00581 if (get_bits_count(&q->gb) < p->bits_per_subpacket) {
00582 subband_coef_sign[i * vd + j] = get_bits1(&q->gb);
00583 } else {
00584 result = 1;
00585 subband_coef_sign[i * vd + j] = 0;
00586 }
00587 } else {
00588 subband_coef_sign[i * vd + j] = 0;
00589 }
00590 }
00591 }
00592 return result;
00593 }
00594
00595
00604 static void decode_vectors(COOKContext *q, COOKSubpacket *p, int *category,
00605 int *quant_index_table, float *mlt_buffer)
00606 {
00607
00608
00609 int subband_coef_index[SUBBAND_SIZE];
00610
00611
00612 int subband_coef_sign[SUBBAND_SIZE];
00613 int band, j;
00614 int index = 0;
00615
00616 for (band = 0; band < p->total_subbands; band++) {
00617 index = category[band];
00618 if (category[band] < 7) {
00619 if (unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)) {
00620 index = 7;
00621 for (j = 0; j < p->total_subbands; j++)
00622 category[band + j] = 7;
00623 }
00624 }
00625 if (index >= 7) {
00626 memset(subband_coef_index, 0, sizeof(subband_coef_index));
00627 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
00628 }
00629 q->scalar_dequant(q, index, quant_index_table[band],
00630 subband_coef_index, subband_coef_sign,
00631 &mlt_buffer[band * SUBBAND_SIZE]);
00632 }
00633
00634
00635 if (p->total_subbands * SUBBAND_SIZE >= q->samples_per_channel)
00636 return;
00637 }
00638
00639
00646 static int mono_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer)
00647 {
00648 int category_index[128] = { 0 };
00649 int category[128] = { 0 };
00650 int quant_index_table[102];
00651 int res, i;
00652
00653 if ((res = decode_envelope(q, p, quant_index_table)) < 0)
00654 return res;
00655 q->num_vectors = get_bits(&q->gb, p->log2_numvector_size);
00656 categorize(q, p, quant_index_table, category, category_index);
00657 expand_category(q, category, category_index);
00658 for (i=0; i<p->total_subbands; i++) {
00659 if (category[i] > 7)
00660 return AVERROR_INVALIDDATA;
00661 }
00662 decode_vectors(q, p, category, quant_index_table, mlt_buffer);
00663
00664 return 0;
00665 }
00666
00667
00676 static void interpolate_float(COOKContext *q, float *buffer,
00677 int gain_index, int gain_index_next)
00678 {
00679 int i;
00680 float fc1, fc2;
00681 fc1 = pow2tab[gain_index + 63];
00682
00683 if (gain_index == gain_index_next) {
00684 for (i = 0; i < q->gain_size_factor; i++)
00685 buffer[i] *= fc1;
00686 } else {
00687 fc2 = q->gain_table[11 + (gain_index_next - gain_index)];
00688 for (i = 0; i < q->gain_size_factor; i++) {
00689 buffer[i] *= fc1;
00690 fc1 *= fc2;
00691 }
00692 }
00693 }
00694
00703 static void imlt_window_float(COOKContext *q, float *inbuffer,
00704 cook_gains *gains_ptr, float *previous_buffer)
00705 {
00706 const float fc = pow2tab[gains_ptr->previous[0] + 63];
00707 int i;
00708
00709
00710
00711
00712
00713
00714
00715 for (i = 0; i < q->samples_per_channel; i++)
00716 inbuffer[i] = inbuffer[i] * fc * q->mlt_window[i] -
00717 previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
00718 }
00719
00731 static void imlt_gain(COOKContext *q, float *inbuffer,
00732 cook_gains *gains_ptr, float *previous_buffer)
00733 {
00734 float *buffer0 = q->mono_mdct_output;
00735 float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
00736 int i;
00737
00738
00739 q->mdct_ctx.imdct_calc(&q->mdct_ctx, q->mono_mdct_output, inbuffer);
00740
00741 q->imlt_window(q, buffer1, gains_ptr, previous_buffer);
00742
00743
00744 for (i = 0; i < 8; i++)
00745 if (gains_ptr->now[i] || gains_ptr->now[i + 1])
00746 q->interpolate(q, &buffer1[q->gain_size_factor * i],
00747 gains_ptr->now[i], gains_ptr->now[i + 1]);
00748
00749
00750 memcpy(previous_buffer, buffer0,
00751 q->samples_per_channel * sizeof(*previous_buffer));
00752 }
00753
00754
00762 static int decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)
00763 {
00764 int i;
00765 int vlc = get_bits1(&q->gb);
00766 int start = cplband[p->js_subband_start];
00767 int end = cplband[p->subbands - 1];
00768 int length = end - start + 1;
00769
00770 if (start > end)
00771 return 0;
00772
00773 if (vlc)
00774 for (i = 0; i < length; i++)
00775 decouple_tab[start + i] = get_vlc2(&q->gb, p->ccpl.table, p->ccpl.bits, 2);
00776 else
00777 for (i = 0; i < length; i++) {
00778 int v = get_bits(&q->gb, p->js_vlc_bits);
00779 if (v == (1<<p->js_vlc_bits)-1) {
00780 av_log(q->avctx, AV_LOG_ERROR, "decouple value too large\n");
00781 return AVERROR_INVALIDDATA;
00782 }
00783 decouple_tab[start + i] = v;
00784 }
00785 return 0;
00786 }
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799 static void decouple_float(COOKContext *q,
00800 COOKSubpacket *p,
00801 int subband,
00802 float f1, float f2,
00803 float *decode_buffer,
00804 float *mlt_buffer1, float *mlt_buffer2)
00805 {
00806 int j, tmp_idx;
00807 for (j = 0; j < SUBBAND_SIZE; j++) {
00808 tmp_idx = ((p->js_subband_start + subband) * SUBBAND_SIZE) + j;
00809 mlt_buffer1[SUBBAND_SIZE * subband + j] = f1 * decode_buffer[tmp_idx];
00810 mlt_buffer2[SUBBAND_SIZE * subband + j] = f2 * decode_buffer[tmp_idx];
00811 }
00812 }
00813
00821 static int joint_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer1,
00822 float *mlt_buffer2)
00823 {
00824 int i, j, res;
00825 int decouple_tab[SUBBAND_SIZE] = { 0 };
00826 float *decode_buffer = q->decode_buffer_0;
00827 int idx, cpl_tmp;
00828 float f1, f2;
00829 const float *cplscale;
00830
00831 memset(decode_buffer, 0, sizeof(q->decode_buffer_0));
00832
00833
00834 memset(mlt_buffer1, 0, 1024 * sizeof(*mlt_buffer1));
00835 memset(mlt_buffer2, 0, 1024 * sizeof(*mlt_buffer2));
00836 if ((res = decouple_info(q, p, decouple_tab)) < 0)
00837 return res;
00838 if ((res = mono_decode(q, p, decode_buffer)) < 0)
00839 return res;
00840
00841 for (i = 0; i < p->js_subband_start; i++) {
00842 for (j = 0; j < SUBBAND_SIZE; j++) {
00843 mlt_buffer1[i * 20 + j] = decode_buffer[i * 40 + j];
00844 mlt_buffer2[i * 20 + j] = decode_buffer[i * 40 + 20 + j];
00845 }
00846 }
00847
00848
00849
00850 idx = (1 << p->js_vlc_bits) - 1;
00851 for (i = p->js_subband_start; i < p->subbands; i++) {
00852 cpl_tmp = cplband[i];
00853 idx -= decouple_tab[cpl_tmp];
00854 cplscale = q->cplscales[p->js_vlc_bits - 2];
00855 f1 = cplscale[decouple_tab[cpl_tmp] + 1];
00856 f2 = cplscale[idx];
00857 q->decouple(q, p, i, f1, f2, decode_buffer, mlt_buffer1, mlt_buffer2);
00858 idx = (1 << p->js_vlc_bits) - 1;
00859 }
00860
00861 return 0;
00862 }
00863
00872 static inline void decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p,
00873 const uint8_t *inbuffer,
00874 cook_gains *gains_ptr)
00875 {
00876 int offset;
00877
00878 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
00879 p->bits_per_subpacket / 8);
00880 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
00881 p->bits_per_subpacket);
00882 decode_gain_info(&q->gb, gains_ptr->now);
00883
00884
00885 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
00886 }
00887
00895 static void saturate_output_float(COOKContext *q, int chan, float *out)
00896 {
00897 int j;
00898 float *output = q->mono_mdct_output + q->samples_per_channel;
00899 for (j = 0; j < q->samples_per_channel; j++) {
00900 out[chan + q->nb_channels * j] = av_clipf(output[j], -1.0, 1.0);
00901 }
00902 }
00903
00916 static inline void mlt_compensate_output(COOKContext *q, float *decode_buffer,
00917 cook_gains *gains_ptr, float *previous_buffer,
00918 float *out, int chan)
00919 {
00920 imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
00921 if (out)
00922 q->saturate_output(q, chan, out);
00923 }
00924
00925
00934 static int decode_subpacket(COOKContext *q, COOKSubpacket *p,
00935 const uint8_t *inbuffer, float *outbuffer)
00936 {
00937 int sub_packet_size = p->size;
00938 int res;
00939
00940
00941
00942
00943 memset(q->decode_buffer_1, 0, sizeof(q->decode_buffer_1));
00944 decode_bytes_and_gain(q, p, inbuffer, &p->gains1);
00945
00946 if (p->joint_stereo) {
00947 if ((res = joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2)) < 0)
00948 return res;
00949 } else {
00950 if ((res = mono_decode(q, p, q->decode_buffer_1)) < 0)
00951 return res;
00952
00953 if (p->num_channels == 2) {
00954 decode_bytes_and_gain(q, p, inbuffer + sub_packet_size / 2, &p->gains2);
00955 if ((res = mono_decode(q, p, q->decode_buffer_2)) < 0)
00956 return res;
00957 }
00958 }
00959
00960 mlt_compensate_output(q, q->decode_buffer_1, &p->gains1,
00961 p->mono_previous_buffer1, outbuffer, p->ch_idx);
00962
00963 if (p->num_channels == 2)
00964 if (p->joint_stereo)
00965 mlt_compensate_output(q, q->decode_buffer_2, &p->gains1,
00966 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);
00967 else
00968 mlt_compensate_output(q, q->decode_buffer_2, &p->gains2,
00969 p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);
00970
00971 return 0;
00972 }
00973
00974
00980 static int cook_decode_frame(AVCodecContext *avctx, void *data,
00981 int *got_frame_ptr, AVPacket *avpkt)
00982 {
00983 const uint8_t *buf = avpkt->data;
00984 int buf_size = avpkt->size;
00985 COOKContext *q = avctx->priv_data;
00986 float *samples = NULL;
00987 int i, ret;
00988 int offset = 0;
00989 int chidx = 0;
00990
00991 if (buf_size < avctx->block_align)
00992 return buf_size;
00993
00994
00995 if (q->discarded_packets >= 2) {
00996 q->frame.nb_samples = q->samples_per_channel;
00997 if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
00998 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00999 return ret;
01000 }
01001 samples = (float *) q->frame.data[0];
01002 }
01003
01004
01005 q->subpacket[0].size = avctx->block_align;
01006
01007 for (i = 1; i < q->num_subpackets; i++) {
01008 q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i];
01009 q->subpacket[0].size -= q->subpacket[i].size + 1;
01010 if (q->subpacket[0].size < 0) {
01011 av_log(avctx, AV_LOG_DEBUG,
01012 "frame subpacket size total > avctx->block_align!\n");
01013 return AVERROR_INVALIDDATA;
01014 }
01015 }
01016
01017
01018 for (i = 0; i < q->num_subpackets; i++) {
01019 q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size * 8) >>
01020 q->subpacket[i].bits_per_subpdiv;
01021 q->subpacket[i].ch_idx = chidx;
01022 av_log(avctx, AV_LOG_DEBUG,
01023 "subpacket[%i] size %i js %i %i block_align %i\n",
01024 i, q->subpacket[i].size, q->subpacket[i].joint_stereo, offset,
01025 avctx->block_align);
01026
01027 if ((ret = decode_subpacket(q, &q->subpacket[i], buf + offset, samples)) < 0)
01028 return ret;
01029 offset += q->subpacket[i].size;
01030 chidx += q->subpacket[i].num_channels;
01031 av_log(avctx, AV_LOG_DEBUG, "subpacket[%i] %i %i\n",
01032 i, q->subpacket[i].size * 8, get_bits_count(&q->gb));
01033 }
01034
01035
01036 if (q->discarded_packets < 2) {
01037 q->discarded_packets++;
01038 *got_frame_ptr = 0;
01039 return avctx->block_align;
01040 }
01041
01042 *got_frame_ptr = 1;
01043 *(AVFrame *) data = q->frame;
01044
01045 return avctx->block_align;
01046 }
01047
01048 #ifdef DEBUG
01049 static void dump_cook_context(COOKContext *q)
01050 {
01051
01052 #define PRINT(a, b) av_log(q->avctx, AV_LOG_ERROR, " %s = %d\n", a, b);
01053 av_log(q->avctx, AV_LOG_ERROR, "COOKextradata\n");
01054 av_log(q->avctx, AV_LOG_ERROR, "cookversion=%x\n", q->subpacket[0].cookversion);
01055 if (q->subpacket[0].cookversion > STEREO) {
01056 PRINT("js_subband_start", q->subpacket[0].js_subband_start);
01057 PRINT("js_vlc_bits", q->subpacket[0].js_vlc_bits);
01058 }
01059 av_log(q->avctx, AV_LOG_ERROR, "COOKContext\n");
01060 PRINT("nb_channels", q->nb_channels);
01061 PRINT("bit_rate", q->bit_rate);
01062 PRINT("sample_rate", q->sample_rate);
01063 PRINT("samples_per_channel", q->subpacket[0].samples_per_channel);
01064 PRINT("samples_per_frame", q->subpacket[0].samples_per_frame);
01065 PRINT("subbands", q->subpacket[0].subbands);
01066 PRINT("js_subband_start", q->subpacket[0].js_subband_start);
01067 PRINT("log2_numvector_size", q->subpacket[0].log2_numvector_size);
01068 PRINT("numvector_size", q->subpacket[0].numvector_size);
01069 PRINT("total_subbands", q->subpacket[0].total_subbands);
01070 }
01071 #endif
01072
01073 static av_cold int cook_count_channels(unsigned int mask)
01074 {
01075 int i;
01076 int channels = 0;
01077 for (i = 0; i < 32; i++)
01078 if (mask & (1 << i))
01079 ++channels;
01080 return channels;
01081 }
01082
01088 static av_cold int cook_decode_init(AVCodecContext *avctx)
01089 {
01090 COOKContext *q = avctx->priv_data;
01091 const uint8_t *edata_ptr = avctx->extradata;
01092 const uint8_t *edata_ptr_end = edata_ptr + avctx->extradata_size;
01093 int extradata_size = avctx->extradata_size;
01094 int s = 0;
01095 unsigned int channel_mask = 0;
01096 int ret;
01097 q->avctx = avctx;
01098
01099
01100 if (extradata_size <= 0) {
01101 av_log(avctx, AV_LOG_ERROR, "Necessary extradata missing!\n");
01102 return AVERROR_INVALIDDATA;
01103 }
01104 av_log(avctx, AV_LOG_DEBUG, "codecdata_length=%d\n", avctx->extradata_size);
01105
01106
01107 q->sample_rate = avctx->sample_rate;
01108 q->nb_channels = avctx->channels;
01109 q->bit_rate = avctx->bit_rate;
01110 if (!q->nb_channels) {
01111 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
01112 return AVERROR_INVALIDDATA;
01113 }
01114
01115
01116 av_lfg_init(&q->random_state, 0);
01117
01118 while (edata_ptr < edata_ptr_end) {
01119
01120
01121 if (extradata_size >= 8) {
01122 q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr);
01123 q->subpacket[s].samples_per_frame = bytestream_get_be16(&edata_ptr);
01124 q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr);
01125 extradata_size -= 8;
01126 }
01127 if (extradata_size >= 8) {
01128 bytestream_get_be32(&edata_ptr);
01129 q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr);
01130 q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr);
01131 extradata_size -= 8;
01132 }
01133
01134
01135 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame / q->nb_channels;
01136 q->subpacket[s].bits_per_subpacket = avctx->block_align * 8;
01137
01138
01139 q->subpacket[s].log2_numvector_size = 5;
01140 q->subpacket[s].total_subbands = q->subpacket[s].subbands;
01141 q->subpacket[s].num_channels = 1;
01142
01143
01144
01145 av_log(avctx, AV_LOG_DEBUG, "subpacket[%i].cookversion=%x\n", s,
01146 q->subpacket[s].cookversion);
01147 q->subpacket[s].joint_stereo = 0;
01148 switch (q->subpacket[s].cookversion) {
01149 case MONO:
01150 if (q->nb_channels != 1) {
01151 av_log_ask_for_sample(avctx, "Container channels != 1.\n");
01152 return AVERROR_PATCHWELCOME;
01153 }
01154 av_log(avctx, AV_LOG_DEBUG, "MONO\n");
01155 break;
01156 case STEREO:
01157 if (q->nb_channels != 1) {
01158 q->subpacket[s].bits_per_subpdiv = 1;
01159 q->subpacket[s].num_channels = 2;
01160 }
01161 av_log(avctx, AV_LOG_DEBUG, "STEREO\n");
01162 break;
01163 case JOINT_STEREO:
01164 if (q->nb_channels != 2) {
01165 av_log_ask_for_sample(avctx, "Container channels != 2.\n");
01166 return AVERROR_PATCHWELCOME;
01167 }
01168 av_log(avctx, AV_LOG_DEBUG, "JOINT_STEREO\n");
01169 if (avctx->extradata_size >= 16) {
01170 q->subpacket[s].total_subbands = q->subpacket[s].subbands +
01171 q->subpacket[s].js_subband_start;
01172 q->subpacket[s].joint_stereo = 1;
01173 q->subpacket[s].num_channels = 2;
01174 }
01175 if (q->subpacket[s].samples_per_channel > 256) {
01176 q->subpacket[s].log2_numvector_size = 6;
01177 }
01178 if (q->subpacket[s].samples_per_channel > 512) {
01179 q->subpacket[s].log2_numvector_size = 7;
01180 }
01181 break;
01182 case MC_COOK:
01183 av_log(avctx, AV_LOG_DEBUG, "MULTI_CHANNEL\n");
01184 if (extradata_size >= 4)
01185 channel_mask |= q->subpacket[s].channel_mask = bytestream_get_be32(&edata_ptr);
01186
01187 if (cook_count_channels(q->subpacket[s].channel_mask) > 1) {
01188 q->subpacket[s].total_subbands = q->subpacket[s].subbands +
01189 q->subpacket[s].js_subband_start;
01190 q->subpacket[s].joint_stereo = 1;
01191 q->subpacket[s].num_channels = 2;
01192 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame >> 1;
01193
01194 if (q->subpacket[s].samples_per_channel > 256) {
01195 q->subpacket[s].log2_numvector_size = 6;
01196 }
01197 if (q->subpacket[s].samples_per_channel > 512) {
01198 q->subpacket[s].log2_numvector_size = 7;
01199 }
01200 } else
01201 q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame;
01202
01203 break;
01204 default:
01205 av_log_ask_for_sample(avctx, "Unknown Cook version.\n");
01206 return AVERROR_PATCHWELCOME;
01207 }
01208
01209 if (s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) {
01210 av_log(avctx, AV_LOG_ERROR, "different number of samples per channel!\n");
01211 return AVERROR_INVALIDDATA;
01212 } else
01213 q->samples_per_channel = q->subpacket[0].samples_per_channel;
01214
01215
01216
01217 q->subpacket[s].numvector_size = (1 << q->subpacket[s].log2_numvector_size);
01218
01219
01220 if (q->subpacket[s].total_subbands > 53) {
01221 av_log_ask_for_sample(avctx, "total_subbands > 53\n");
01222 return AVERROR_PATCHWELCOME;
01223 }
01224
01225 if ((q->subpacket[s].js_vlc_bits > 6) ||
01226 (q->subpacket[s].js_vlc_bits < 2 * q->subpacket[s].joint_stereo)) {
01227 av_log(avctx, AV_LOG_ERROR, "js_vlc_bits = %d, only >= %d and <= 6 allowed!\n",
01228 q->subpacket[s].js_vlc_bits, 2 * q->subpacket[s].joint_stereo);
01229 return AVERROR_INVALIDDATA;
01230 }
01231
01232 if (q->subpacket[s].subbands > 50) {
01233 av_log_ask_for_sample(avctx, "subbands > 50\n");
01234 return AVERROR_PATCHWELCOME;
01235 }
01236 q->subpacket[s].gains1.now = q->subpacket[s].gain_1;
01237 q->subpacket[s].gains1.previous = q->subpacket[s].gain_2;
01238 q->subpacket[s].gains2.now = q->subpacket[s].gain_3;
01239 q->subpacket[s].gains2.previous = q->subpacket[s].gain_4;
01240
01241 if (q->num_subpackets + q->subpacket[s].num_channels > q->nb_channels) {
01242 av_log(avctx, AV_LOG_ERROR, "Too many subpackets %d for channels %d\n", q->num_subpackets, q->nb_channels);
01243 return AVERROR_INVALIDDATA;
01244 }
01245
01246 q->num_subpackets++;
01247 s++;
01248 if (s > MAX_SUBPACKETS) {
01249 av_log_ask_for_sample(avctx, "Too many subpackets > 5\n");
01250 return AVERROR_PATCHWELCOME;
01251 }
01252 }
01253
01254 init_pow2table();
01255 init_gain_table(q);
01256 init_cplscales_table(q);
01257
01258 if ((ret = init_cook_vlc_tables(q)))
01259 return ret;
01260
01261
01262 if (avctx->block_align >= UINT_MAX / 2)
01263 return AVERROR(EINVAL);
01264
01265
01266
01267
01268 q->decoded_bytes_buffer =
01269 av_mallocz(avctx->block_align
01270 + DECODE_BYTES_PAD1(avctx->block_align)
01271 + FF_INPUT_BUFFER_PADDING_SIZE);
01272 if (q->decoded_bytes_buffer == NULL)
01273 return AVERROR(ENOMEM);
01274
01275
01276 if ((ret = init_cook_mlt(q)))
01277 return ret;
01278
01279
01280 if (1) {
01281 q->scalar_dequant = scalar_dequant_float;
01282 q->decouple = decouple_float;
01283 q->imlt_window = imlt_window_float;
01284 q->interpolate = interpolate_float;
01285 q->saturate_output = saturate_output_float;
01286 }
01287
01288
01289 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512)
01290 || (q->samples_per_channel == 1024)) {
01291 } else {
01292 av_log_ask_for_sample(avctx,
01293 "unknown amount of samples_per_channel = %d\n",
01294 q->samples_per_channel);
01295 return AVERROR_PATCHWELCOME;
01296 }
01297
01298 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01299 if (channel_mask)
01300 avctx->channel_layout = channel_mask;
01301 else
01302 avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
01303
01304 avcodec_get_frame_defaults(&q->frame);
01305 avctx->coded_frame = &q->frame;
01306
01307 #ifdef DEBUG
01308 dump_cook_context(q);
01309 #endif
01310 return 0;
01311 }
01312
01313 AVCodec ff_cook_decoder = {
01314 .name = "cook",
01315 .type = AVMEDIA_TYPE_AUDIO,
01316 .id = CODEC_ID_COOK,
01317 .priv_data_size = sizeof(COOKContext),
01318 .init = cook_decode_init,
01319 .close = cook_decode_close,
01320 .decode = cook_decode_frame,
01321 .capabilities = CODEC_CAP_DR1,
01322 .long_name = NULL_IF_CONFIG_SMALL("COOK"),
01323 };