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/channel_layout.h"
00046 #include "libavutil/lfg.h"
00047 #include "avcodec.h"
00048 #include "get_bits.h"
00049 #include "dsputil.h"
00050 #include "bytestream.h"
00051 #include "fft.h"
00052 #include "internal.h"
00053 #include "sinewin.h"
00054
00055 #include "cookdata.h"
00056
00057
00058 #define MONO 0x1000001
00059 #define STEREO 0x1000002
00060 #define JOINT_STEREO 0x1000003
00061 #define MC_COOK 0x2000000 // multichannel Cook, not supported
00062
00063 #define SUBBAND_SIZE 20
00064 #define MAX_SUBPACKETS 5
00065
00066 typedef struct {
00067 int *now;
00068 int *previous;
00069 } cook_gains;
00070
00071 typedef struct {
00072 int ch_idx;
00073 int size;
00074 int num_channels;
00075 int cookversion;
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 channel_coupling;
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];
00091
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, float *out);
00123
00124 AVCodecContext* avctx;
00125 DSPContext dsp;
00126 AVFrame frame;
00127 GetBitContext gb;
00128
00129 int num_vectors;
00130 int samples_per_channel;
00131
00132 AVLFG random_state;
00133 int discarded_packets;
00134
00135
00136 FFTContext mdct_ctx;
00137 float* mlt_window;
00138
00139
00140 VLC envelope_quant_index[13];
00141 VLC sqvh[7];
00142
00143
00144 int gain_size_factor;
00145 float gain_table[23];
00146
00147
00148
00149 uint8_t* decoded_bytes_buffer;
00150 DECLARE_ALIGNED(32, float, mono_mdct_output)[2048];
00151 float decode_buffer_1[1024];
00152 float decode_buffer_2[1024];
00153 float decode_buffer_0[1060];
00154
00155 const float *cplscales[5];
00156 int num_subpackets;
00157 COOKSubpacket subpacket[MAX_SUBPACKETS];
00158 } COOKContext;
00159
00160 static float pow2tab[127];
00161 static float rootpow2tab[127];
00162
00163
00164
00165
00166 static av_cold void init_pow2table(void)
00167 {
00168 int i;
00169 for (i = -63; i < 64; i++) {
00170 pow2tab[63 + i] = pow(2, i);
00171 rootpow2tab[63 + i] = sqrt(pow(2, i));
00172 }
00173 }
00174
00175
00176 static av_cold void init_gain_table(COOKContext *q)
00177 {
00178 int i;
00179 q->gain_size_factor = q->samples_per_channel / 8;
00180 for (i = 0; i < 23; i++)
00181 q->gain_table[i] = pow(pow2tab[i + 52],
00182 (1.0 / (double) q->gain_size_factor));
00183 }
00184
00185
00186 static av_cold int init_cook_vlc_tables(COOKContext *q)
00187 {
00188 int i, result;
00189
00190 result = 0;
00191 for (i = 0; i < 13; i++) {
00192 result |= init_vlc(&q->envelope_quant_index[i], 9, 24,
00193 envelope_quant_index_huffbits[i], 1, 1,
00194 envelope_quant_index_huffcodes[i], 2, 2, 0);
00195 }
00196 av_log(q->avctx, AV_LOG_DEBUG, "sqvh VLC init\n");
00197 for (i = 0; i < 7; i++) {
00198 result |= init_vlc(&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
00199 cvh_huffbits[i], 1, 1,
00200 cvh_huffcodes[i], 2, 2, 0);
00201 }
00202
00203 for (i = 0; i < q->num_subpackets; i++) {
00204 if (q->subpacket[i].joint_stereo == 1) {
00205 result |= init_vlc(&q->subpacket[i].channel_coupling, 6,
00206 (1 << q->subpacket[i].js_vlc_bits) - 1,
00207 ccpl_huffbits[q->subpacket[i].js_vlc_bits - 2], 1, 1,
00208 ccpl_huffcodes[q->subpacket[i].js_vlc_bits - 2], 2, 2, 0);
00209 av_log(q->avctx, AV_LOG_DEBUG, "subpacket %i Joint-stereo VLC used.\n", i);
00210 }
00211 }
00212
00213 av_log(q->avctx, AV_LOG_DEBUG, "VLC tables initialized.\n");
00214 return result;
00215 }
00216
00217 static av_cold int init_cook_mlt(COOKContext *q)
00218 {
00219 int j, ret;
00220 int mlt_size = q->samples_per_channel;
00221
00222 if ((q->mlt_window = av_malloc(mlt_size * sizeof(*q->mlt_window))) == 0)
00223 return AVERROR(ENOMEM);
00224
00225
00226 ff_sine_window_init(q->mlt_window, mlt_size);
00227 for (j = 0; j < mlt_size; j++)
00228 q->mlt_window[j] *= sqrt(2.0 / q->samples_per_channel);
00229
00230
00231 if ((ret = ff_mdct_init(&q->mdct_ctx, av_log2(mlt_size) + 1, 1, 1.0 / 32768.0))) {
00232 av_free(q->mlt_window);
00233 return ret;
00234 }
00235 av_log(q->avctx, AV_LOG_DEBUG, "MDCT initialized, order = %d.\n",
00236 av_log2(mlt_size) + 1);
00237
00238 return 0;
00239 }
00240
00241 static av_cold void init_cplscales_table(COOKContext *q)
00242 {
00243 int i;
00244 for (i = 0; i < 5; i++)
00245 q->cplscales[i] = cplscales[i];
00246 }
00247
00248
00249
00250 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes) + 3) % 4)
00251 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
00252
00273 static inline int decode_bytes(const uint8_t *inbuffer, uint8_t *out, int bytes)
00274 {
00275 static const uint32_t tab[4] = {
00276 AV_BE2NE32C(0x37c511f2u), AV_BE2NE32C(0xf237c511u),
00277 AV_BE2NE32C(0x11f237c5u), AV_BE2NE32C(0xc511f237u),
00278 };
00279 int i, off;
00280 uint32_t c;
00281 const uint32_t *buf;
00282 uint32_t *obuf = (uint32_t *) out;
00283
00284
00285
00286
00287
00288
00289 off = (intptr_t) inbuffer & 3;
00290 buf = (const uint32_t *) (inbuffer - off);
00291 c = tab[off];
00292 bytes += 3 + off;
00293 for (i = 0; i < bytes / 4; i++)
00294 obuf[i] = c ^ buf[i];
00295
00296 return off;
00297 }
00298
00299 static av_cold int cook_decode_close(AVCodecContext *avctx)
00300 {
00301 int i;
00302 COOKContext *q = avctx->priv_data;
00303 av_log(avctx, AV_LOG_DEBUG, "Deallocating memory.\n");
00304
00305
00306 av_free(q->mlt_window);
00307 av_free(q->decoded_bytes_buffer);
00308
00309
00310 ff_mdct_end(&q->mdct_ctx);
00311
00312
00313 for (i = 0; i < 13; i++)
00314 ff_free_vlc(&q->envelope_quant_index[i]);
00315 for (i = 0; i < 7; i++)
00316 ff_free_vlc(&q->sqvh[i]);
00317 for (i = 0; i < q->num_subpackets; i++)
00318 ff_free_vlc(&q->subpacket[i].channel_coupling);
00319
00320 av_log(avctx, AV_LOG_DEBUG, "Memory deallocated.\n");
00321
00322 return 0;
00323 }
00324
00331 static void decode_gain_info(GetBitContext *gb, int *gaininfo)
00332 {
00333 int i, n;
00334
00335 while (get_bits1(gb)) {
00336
00337 }
00338
00339 n = get_bits_count(gb) - 1;
00340
00341 i = 0;
00342 while (n--) {
00343 int index = get_bits(gb, 3);
00344 int gain = get_bits1(gb) ? get_bits(gb, 4) - 7 : -1;
00345
00346 while (i <= index)
00347 gaininfo[i++] = gain;
00348 }
00349 while (i <= 8)
00350 gaininfo[i++] = 0;
00351 }
00352
00359 static int decode_envelope(COOKContext *q, COOKSubpacket *p,
00360 int *quant_index_table)
00361 {
00362 int i, j, vlc_index;
00363
00364 quant_index_table[0] = get_bits(&q->gb, 6) - 6;
00365
00366 for (i = 1; i < p->total_subbands; i++) {
00367 vlc_index = i;
00368 if (i >= p->js_subband_start * 2) {
00369 vlc_index -= p->js_subband_start;
00370 } else {
00371 vlc_index /= 2;
00372 if (vlc_index < 1)
00373 vlc_index = 1;
00374 }
00375 if (vlc_index > 13)
00376 vlc_index = 13;
00377
00378 j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index - 1].table,
00379 q->envelope_quant_index[vlc_index - 1].bits, 2);
00380 quant_index_table[i] = quant_index_table[i - 1] + j - 12;
00381 if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
00382 av_log(q->avctx, AV_LOG_ERROR,
00383 "Invalid quantizer %d at position %d, outside [-63, 63] range\n",
00384 quant_index_table[i], i);
00385 return AVERROR_INVALIDDATA;
00386 }
00387 }
00388
00389 return 0;
00390 }
00391
00400 static void categorize(COOKContext *q, COOKSubpacket *p, const int *quant_index_table,
00401 int *category, int *category_index)
00402 {
00403 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
00404 int exp_index2[102] = { 0 };
00405 int exp_index1[102] = { 0 };
00406
00407 int tmp_categorize_array[128 * 2] = { 0 };
00408 int tmp_categorize_array1_idx = p->numvector_size;
00409 int tmp_categorize_array2_idx = p->numvector_size;
00410
00411 bits_left = p->bits_per_subpacket - get_bits_count(&q->gb);
00412
00413 if (bits_left > q->samples_per_channel)
00414 bits_left = q->samples_per_channel +
00415 ((bits_left - q->samples_per_channel) * 5) / 8;
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 num_bits = 0;
00434 for (i = 0; i < p->total_subbands; i++) {
00435 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
00436 num_bits += expbits_tab[exp_idx];
00437 exp_index1[i] = exp_idx;
00438 exp_index2[i] = exp_idx;
00439 }
00440 tmpbias1 = tmpbias2 = num_bits;
00441
00442 for (j = 1; j < p->numvector_size; j++) {
00443 if (tmpbias1 + tmpbias2 > 2 * bits_left) {
00444 int max = -999999;
00445 index = -1;
00446 for (i = 0; i < p->total_subbands; i++) {
00447 if (exp_index1[i] < 7) {
00448 v = (-2 * exp_index1[i]) - quant_index_table[i] + bias;
00449 if (v >= max) {
00450 max = v;
00451 index = i;
00452 }
00453 }
00454 }
00455 if (index == -1)
00456 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)
00474 break;
00475 tmp_categorize_array[--tmp_categorize_array2_idx] = index;
00476 tmpbias2 -= expbits_tab[exp_index2[index]] -
00477 expbits_tab[exp_index2[index] - 1];
00478 --exp_index2[index];
00479 }
00480 }
00481
00482 for (i = 0; i < p->total_subbands; i++)
00483 category[i] = exp_index2[i];
00484
00485 for (i = 0; i < p->numvector_size - 1; i++)
00486 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
00487 }
00488
00489
00497 static inline void expand_category(COOKContext *q, int *category,
00498 int *category_index)
00499 {
00500 int i;
00501 for (i = 0; i < q->num_vectors; i++)
00502 {
00503 int idx = category_index[i];
00504 if (++category[idx] >= FF_ARRAY_ELEMS(dither_tab))
00505 --category[idx];
00506 }
00507 }
00508
00519 static void scalar_dequant_float(COOKContext *q, int index, int quant_index,
00520 int *subband_coef_index, int *subband_coef_sign,
00521 float *mlt_p)
00522 {
00523 int i;
00524 float f1;
00525
00526 for (i = 0; i < SUBBAND_SIZE; i++) {
00527 if (subband_coef_index[i]) {
00528 f1 = quant_centroid_tab[index][subband_coef_index[i]];
00529 if (subband_coef_sign[i])
00530 f1 = -f1;
00531 } else {
00532
00533 f1 = dither_tab[index];
00534 if (av_lfg_get(&q->random_state) < 0x80000000)
00535 f1 = -f1;
00536 }
00537 mlt_p[i] = f1 * rootpow2tab[quant_index + 63];
00538 }
00539 }
00548 static int unpack_SQVH(COOKContext *q, COOKSubpacket *p, int category,
00549 int *subband_coef_index, int *subband_coef_sign)
00550 {
00551 int i, j;
00552 int vlc, vd, tmp, result;
00553
00554 vd = vd_tab[category];
00555 result = 0;
00556 for (i = 0; i < vpr_tab[category]; i++) {
00557 vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3);
00558 if (p->bits_per_subpacket < get_bits_count(&q->gb)) {
00559 vlc = 0;
00560 result = 1;
00561 }
00562 for (j = vd - 1; j >= 0; j--) {
00563 tmp = (vlc * invradix_tab[category]) / 0x100000;
00564 subband_coef_index[vd * i + j] = vlc - tmp * (kmax_tab[category] + 1);
00565 vlc = tmp;
00566 }
00567 for (j = 0; j < vd; j++) {
00568 if (subband_coef_index[i * vd + j]) {
00569 if (get_bits_count(&q->gb) < p->bits_per_subpacket) {
00570 subband_coef_sign[i * vd + j] = get_bits1(&q->gb);
00571 } else {
00572 result = 1;
00573 subband_coef_sign[i * vd + j] = 0;
00574 }
00575 } else {
00576 subband_coef_sign[i * vd + j] = 0;
00577 }
00578 }
00579 }
00580 return result;
00581 }
00582
00583
00592 static void decode_vectors(COOKContext *q, COOKSubpacket *p, int *category,
00593 int *quant_index_table, float *mlt_buffer)
00594 {
00595
00596
00597 int subband_coef_index[SUBBAND_SIZE];
00598
00599
00600 int subband_coef_sign[SUBBAND_SIZE];
00601 int band, j;
00602 int index = 0;
00603
00604 for (band = 0; band < p->total_subbands; band++) {
00605 index = category[band];
00606 if (category[band] < 7) {
00607 if (unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)) {
00608 index = 7;
00609 for (j = 0; j < p->total_subbands; j++)
00610 category[band + j] = 7;
00611 }
00612 }
00613 if (index >= 7) {
00614 memset(subband_coef_index, 0, sizeof(subband_coef_index));
00615 memset(subband_coef_sign, 0, sizeof(subband_coef_sign));
00616 }
00617 q->scalar_dequant(q, index, quant_index_table[band],
00618 subband_coef_index, subband_coef_sign,
00619 &mlt_buffer[band * SUBBAND_SIZE]);
00620 }
00621
00622
00623 if (p->total_subbands * SUBBAND_SIZE >= q->samples_per_channel)
00624 return;
00625 }
00626
00627
00628 static int mono_decode(COOKContext *q, COOKSubpacket *p, float *mlt_buffer)
00629 {
00630 int category_index[128] = { 0 };
00631 int category[128] = { 0 };
00632 int quant_index_table[102];
00633 int res, i;
00634
00635 if ((res = decode_envelope(q, p, quant_index_table)) < 0)
00636 return res;
00637 q->num_vectors = get_bits(&q->gb, p->log2_numvector_size);
00638 categorize(q, p, quant_index_table, category, category_index);
00639 expand_category(q, category, category_index);
00640 for (i=0; i<p->total_subbands; i++) {
00641 if (category[i] > 7)
00642 return AVERROR_INVALIDDATA;
00643 }
00644 decode_vectors(q, p, category, quant_index_table, mlt_buffer);
00645
00646 return 0;
00647 }
00648
00649
00658 static void interpolate_float(COOKContext *q, float *buffer,
00659 int gain_index, int gain_index_next)
00660 {
00661 int i;
00662 float fc1, fc2;
00663 fc1 = pow2tab[gain_index + 63];
00664
00665 if (gain_index == gain_index_next) {
00666 for (i = 0; i < q->gain_size_factor; i++)
00667 buffer[i] *= fc1;
00668 } else {
00669 fc2 = q->gain_table[11 + (gain_index_next - gain_index)];
00670 for (i = 0; i < q->gain_size_factor; i++) {
00671 buffer[i] *= fc1;
00672 fc1 *= fc2;
00673 }
00674 }
00675 }
00676
00685 static void imlt_window_float(COOKContext *q, float *inbuffer,
00686 cook_gains *gains_ptr, float *previous_buffer)
00687 {
00688 const float fc = pow2tab[gains_ptr->previous[0] + 63];
00689 int i;
00690
00691
00692
00693
00694
00695
00696
00697 for (i = 0; i < q->samples_per_channel; i++)
00698 inbuffer[i] = inbuffer[i] * fc * q->mlt_window[i] -
00699 previous_buffer[i] * q->mlt_window[q->samples_per_channel - 1 - i];
00700 }
00701
00713 static void imlt_gain(COOKContext *q, float *inbuffer,
00714 cook_gains *gains_ptr, float *previous_buffer)
00715 {
00716 float *buffer0 = q->mono_mdct_output;
00717 float *buffer1 = q->mono_mdct_output + q->samples_per_channel;
00718 int i;
00719
00720
00721 q->mdct_ctx.imdct_calc(&q->mdct_ctx, q->mono_mdct_output, inbuffer);
00722
00723 q->imlt_window(q, buffer1, gains_ptr, previous_buffer);
00724
00725
00726 for (i = 0; i < 8; i++)
00727 if (gains_ptr->now[i] || gains_ptr->now[i + 1])
00728 q->interpolate(q, &buffer1[q->gain_size_factor * i],
00729 gains_ptr->now[i], gains_ptr->now[i + 1]);
00730
00731
00732 memcpy(previous_buffer, buffer0,
00733 q->samples_per_channel * sizeof(*previous_buffer));
00734 }
00735
00736
00743 static int decouple_info(COOKContext *q, COOKSubpacket *p, int *decouple_tab)
00744 {
00745 int i;
00746 int vlc = get_bits1(&q->gb);
00747 int start = cplband[p->js_subband_start];
00748 int end = cplband[p->subbands - 1];
00749 int length = end - start + 1;
00750
00751 if (start > end)
00752 return 0;
00753
00754 if (vlc)
00755 for (i = 0; i < length; i++)
00756 decouple_tab[start + i] = get_vlc2(&q->gb,
00757 p->channel_coupling.table,
00758 p->channel_coupling.bits, 2);
00759 else
00760 for (i = 0; i < length; i++) {
00761 int v = get_bits(&q->gb, p->js_vlc_bits);
00762 if (v == (1<<p->js_vlc_bits)-1) {
00763 av_log(q->avctx, AV_LOG_ERROR, "decouple value too large\n");
00764 return AVERROR_INVALIDDATA;
00765 }
00766 decouple_tab[start + i] = v;
00767 }
00768 return 0;
00769 }
00770
00782 static void decouple_float(COOKContext *q,
00783 COOKSubpacket *p,
00784 int subband,
00785 float f1, float f2,
00786 float *decode_buffer,
00787 float *mlt_buffer1, float *mlt_buffer2)
00788 {
00789 int j, tmp_idx;
00790 for (j = 0; j < SUBBAND_SIZE; j++) {
00791 tmp_idx = ((p->js_subband_start + subband) * SUBBAND_SIZE) + j;
00792 mlt_buffer1[SUBBAND_SIZE * subband + j] = f1 * decode_buffer[tmp_idx];
00793 mlt_buffer2[SUBBAND_SIZE * subband + j] = f2 * decode_buffer[tmp_idx];
00794 }
00795 }
00796
00804 static int joint_decode(COOKContext *q, COOKSubpacket *p,
00805 float *mlt_buffer_left, float *mlt_buffer_right)
00806 {
00807 int i, j, res;
00808 int decouple_tab[SUBBAND_SIZE] = { 0 };
00809 float *decode_buffer = q->decode_buffer_0;
00810 int idx, cpl_tmp;
00811 float f1, f2;
00812 const float *cplscale;
00813
00814 memset(decode_buffer, 0, sizeof(q->decode_buffer_0));
00815
00816
00817 memset(mlt_buffer_left, 0, 1024 * sizeof(*mlt_buffer_left));
00818 memset(mlt_buffer_right, 0, 1024 * sizeof(*mlt_buffer_right));
00819 if ((res = decouple_info(q, p, decouple_tab)) < 0)
00820 return res;
00821 if ((res = mono_decode(q, p, decode_buffer)) < 0)
00822 return res;
00823
00824 for (i = 0; i < p->js_subband_start; i++) {
00825 for (j = 0; j < SUBBAND_SIZE; j++) {
00826 mlt_buffer_left[i * 20 + j] = decode_buffer[i * 40 + j];
00827 mlt_buffer_right[i * 20 + j] = decode_buffer[i * 40 + 20 + j];
00828 }
00829 }
00830
00831
00832
00833 idx = (1 << p->js_vlc_bits) - 1;
00834 for (i = p->js_subband_start; i < p->subbands; i++) {
00835 cpl_tmp = cplband[i];
00836 idx -= decouple_tab[cpl_tmp];
00837 cplscale = q->cplscales[p->js_vlc_bits - 2];
00838 f1 = cplscale[decouple_tab[cpl_tmp] + 1];
00839 f2 = cplscale[idx];
00840 q->decouple(q, p, i, f1, f2, decode_buffer,
00841 mlt_buffer_left, mlt_buffer_right);
00842 idx = (1 << p->js_vlc_bits) - 1;
00843 }
00844
00845 return 0;
00846 }
00847
00856 static inline void decode_bytes_and_gain(COOKContext *q, COOKSubpacket *p,
00857 const uint8_t *inbuffer,
00858 cook_gains *gains_ptr)
00859 {
00860 int offset;
00861
00862 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer,
00863 p->bits_per_subpacket / 8);
00864 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset,
00865 p->bits_per_subpacket);
00866 decode_gain_info(&q->gb, gains_ptr->now);
00867
00868
00869 FFSWAP(int *, gains_ptr->now, gains_ptr->previous);
00870 }
00871
00878 static void saturate_output_float(COOKContext *q, float *out)
00879 {
00880 q->dsp.vector_clipf(out, q->mono_mdct_output + q->samples_per_channel,
00881 -1.0f, 1.0f, FFALIGN(q->samples_per_channel, 8));
00882 }
00883
00884
00896 static inline void mlt_compensate_output(COOKContext *q, float *decode_buffer,
00897 cook_gains *gains_ptr, float *previous_buffer,
00898 float *out)
00899 {
00900 imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
00901 if (out)
00902 q->saturate_output(q, out);
00903 }
00904
00905
00914 static int decode_subpacket(COOKContext *q, COOKSubpacket *p,
00915 const uint8_t *inbuffer, float **outbuffer)
00916 {
00917 int sub_packet_size = p->size;
00918 int res;
00919
00920 memset(q->decode_buffer_1, 0, sizeof(q->decode_buffer_1));
00921 decode_bytes_and_gain(q, p, inbuffer, &p->gains1);
00922
00923 if (p->joint_stereo) {
00924 if ((res = joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2)) < 0)
00925 return res;
00926 } else {
00927 if ((res = mono_decode(q, p, q->decode_buffer_1)) < 0)
00928 return res;
00929
00930 if (p->num_channels == 2) {
00931 decode_bytes_and_gain(q, p, inbuffer + sub_packet_size / 2, &p->gains2);
00932 if ((res = mono_decode(q, p, q->decode_buffer_2)) < 0)
00933 return res;
00934 }
00935 }
00936
00937 mlt_compensate_output(q, q->decode_buffer_1, &p->gains1,
00938 p->mono_previous_buffer1,
00939 outbuffer ? outbuffer[p->ch_idx] : NULL);
00940
00941 if (p->num_channels == 2) {
00942 if (p->joint_stereo)
00943 mlt_compensate_output(q, q->decode_buffer_2, &p->gains1,
00944 p->mono_previous_buffer2,
00945 outbuffer ? outbuffer[p->ch_idx + 1] : NULL);
00946 else
00947 mlt_compensate_output(q, q->decode_buffer_2, &p->gains2,
00948 p->mono_previous_buffer2,
00949 outbuffer ? outbuffer[p->ch_idx + 1] : NULL);
00950 }
00951
00952 return 0;
00953 }
00954
00955
00956 static int cook_decode_frame(AVCodecContext *avctx, void *data,
00957 int *got_frame_ptr, AVPacket *avpkt)
00958 {
00959 const uint8_t *buf = avpkt->data;
00960 int buf_size = avpkt->size;
00961 COOKContext *q = avctx->priv_data;
00962 float **samples = NULL;
00963 int i, ret;
00964 int offset = 0;
00965 int chidx = 0;
00966
00967 if (buf_size < avctx->block_align)
00968 return buf_size;
00969
00970
00971 if (q->discarded_packets >= 2) {
00972 q->frame.nb_samples = q->samples_per_channel;
00973 if ((ret = ff_get_buffer(avctx, &q->frame)) < 0) {
00974 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00975 return ret;
00976 }
00977 samples = (float **)q->frame.extended_data;
00978 }
00979
00980
00981 q->subpacket[0].size = avctx->block_align;
00982
00983 for (i = 1; i < q->num_subpackets; i++) {
00984 q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i];
00985 q->subpacket[0].size -= q->subpacket[i].size + 1;
00986 if (q->subpacket[0].size < 0) {
00987 av_log(avctx, AV_LOG_DEBUG,
00988 "frame subpacket size total > avctx->block_align!\n");
00989 return AVERROR_INVALIDDATA;
00990 }
00991 }
00992
00993
00994 for (i = 0; i < q->num_subpackets; i++) {
00995 q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size * 8) >>
00996 q->subpacket[i].bits_per_subpdiv;
00997 q->subpacket[i].ch_idx = chidx;
00998 av_log(avctx, AV_LOG_DEBUG,
00999 "subpacket[%i] size %i js %i %i block_align %i\n",
01000 i, q->subpacket[i].size, q->subpacket[i].joint_stereo, offset,
01001 avctx->block_align);
01002
01003 if ((ret = decode_subpacket(q, &q->subpacket[i], buf + offset, samples)) < 0)
01004 return ret;
01005 offset += q->subpacket[i].size;
01006 chidx += q->subpacket[i].num_channels;
01007 av_log(avctx, AV_LOG_DEBUG, "subpacket[%i] %i %i\n",
01008 i, q->subpacket[i].size * 8, get_bits_count(&q->gb));
01009 }
01010
01011
01012 if (q->discarded_packets < 2) {
01013 q->discarded_packets++;
01014 *got_frame_ptr = 0;
01015 return avctx->block_align;
01016 }
01017
01018 *got_frame_ptr = 1;
01019 *(AVFrame *) data = q->frame;
01020
01021 return avctx->block_align;
01022 }
01023
01024 #ifdef DEBUG
01025 static void dump_cook_context(COOKContext *q)
01026 {
01027
01028 #define PRINT(a, b) av_dlog(q->avctx, " %s = %d\n", a, b);
01029 av_dlog(q->avctx, "COOKextradata\n");
01030 av_dlog(q->avctx, "cookversion=%x\n", q->subpacket[0].cookversion);
01031 if (q->subpacket[0].cookversion > STEREO) {
01032 PRINT("js_subband_start", q->subpacket[0].js_subband_start);
01033 PRINT("js_vlc_bits", q->subpacket[0].js_vlc_bits);
01034 }
01035 av_dlog(q->avctx, "COOKContext\n");
01036 PRINT("nb_channels", q->avctx->channels);
01037 PRINT("bit_rate", q->avctx->bit_rate);
01038 PRINT("sample_rate", q->avctx->sample_rate);
01039 PRINT("samples_per_channel", q->subpacket[0].samples_per_channel);
01040 PRINT("subbands", q->subpacket[0].subbands);
01041 PRINT("js_subband_start", q->subpacket[0].js_subband_start);
01042 PRINT("log2_numvector_size", q->subpacket[0].log2_numvector_size);
01043 PRINT("numvector_size", q->subpacket[0].numvector_size);
01044 PRINT("total_subbands", q->subpacket[0].total_subbands);
01045 }
01046 #endif
01047
01053 static av_cold int cook_decode_init(AVCodecContext *avctx)
01054 {
01055 COOKContext *q = avctx->priv_data;
01056 const uint8_t *edata_ptr = avctx->extradata;
01057 const uint8_t *edata_ptr_end = edata_ptr + avctx->extradata_size;
01058 int extradata_size = avctx->extradata_size;
01059 int s = 0;
01060 unsigned int channel_mask = 0;
01061 int samples_per_frame = 0;
01062 int ret;
01063 q->avctx = avctx;
01064
01065
01066 if (extradata_size <= 0) {
01067 av_log(avctx, AV_LOG_ERROR, "Necessary extradata missing!\n");
01068 return AVERROR_INVALIDDATA;
01069 }
01070 av_log(avctx, AV_LOG_DEBUG, "codecdata_length=%d\n", avctx->extradata_size);
01071
01072
01073 if (!avctx->channels) {
01074 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
01075 return AVERROR_INVALIDDATA;
01076 }
01077
01078
01079 av_lfg_init(&q->random_state, 0);
01080
01081 ff_dsputil_init(&q->dsp, avctx);
01082
01083 while (edata_ptr < edata_ptr_end) {
01084
01085
01086 if (extradata_size >= 8) {
01087 q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr);
01088 samples_per_frame = bytestream_get_be16(&edata_ptr);
01089 q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr);
01090 extradata_size -= 8;
01091 }
01092 if (extradata_size >= 8) {
01093 bytestream_get_be32(&edata_ptr);
01094 q->subpacket[s].js_subband_start = bytestream_get_be16(&edata_ptr);
01095 q->subpacket[s].js_vlc_bits = bytestream_get_be16(&edata_ptr);
01096 extradata_size -= 8;
01097 }
01098
01099
01100 q->subpacket[s].samples_per_channel = samples_per_frame / avctx->channels;
01101 q->subpacket[s].bits_per_subpacket = avctx->block_align * 8;
01102
01103
01104 q->subpacket[s].log2_numvector_size = 5;
01105 q->subpacket[s].total_subbands = q->subpacket[s].subbands;
01106 q->subpacket[s].num_channels = 1;
01107
01108
01109
01110 av_log(avctx, AV_LOG_DEBUG, "subpacket[%i].cookversion=%x\n", s,
01111 q->subpacket[s].cookversion);
01112 q->subpacket[s].joint_stereo = 0;
01113 switch (q->subpacket[s].cookversion) {
01114 case MONO:
01115 if (avctx->channels != 1) {
01116 av_log_ask_for_sample(avctx, "Container channels != 1.\n");
01117 return AVERROR_PATCHWELCOME;
01118 }
01119 av_log(avctx, AV_LOG_DEBUG, "MONO\n");
01120 break;
01121 case STEREO:
01122 if (avctx->channels != 1) {
01123 q->subpacket[s].bits_per_subpdiv = 1;
01124 q->subpacket[s].num_channels = 2;
01125 }
01126 av_log(avctx, AV_LOG_DEBUG, "STEREO\n");
01127 break;
01128 case JOINT_STEREO:
01129 if (avctx->channels != 2) {
01130 av_log_ask_for_sample(avctx, "Container channels != 2.\n");
01131 return AVERROR_PATCHWELCOME;
01132 }
01133 av_log(avctx, AV_LOG_DEBUG, "JOINT_STEREO\n");
01134 if (avctx->extradata_size >= 16) {
01135 q->subpacket[s].total_subbands = q->subpacket[s].subbands +
01136 q->subpacket[s].js_subband_start;
01137 q->subpacket[s].joint_stereo = 1;
01138 q->subpacket[s].num_channels = 2;
01139 }
01140 if (q->subpacket[s].samples_per_channel > 256) {
01141 q->subpacket[s].log2_numvector_size = 6;
01142 }
01143 if (q->subpacket[s].samples_per_channel > 512) {
01144 q->subpacket[s].log2_numvector_size = 7;
01145 }
01146 break;
01147 case MC_COOK:
01148 av_log(avctx, AV_LOG_DEBUG, "MULTI_CHANNEL\n");
01149 if (extradata_size >= 4)
01150 channel_mask |= q->subpacket[s].channel_mask = bytestream_get_be32(&edata_ptr);
01151
01152 if (av_get_channel_layout_nb_channels(q->subpacket[s].channel_mask) > 1) {
01153 q->subpacket[s].total_subbands = q->subpacket[s].subbands +
01154 q->subpacket[s].js_subband_start;
01155 q->subpacket[s].joint_stereo = 1;
01156 q->subpacket[s].num_channels = 2;
01157 q->subpacket[s].samples_per_channel = samples_per_frame >> 1;
01158
01159 if (q->subpacket[s].samples_per_channel > 256) {
01160 q->subpacket[s].log2_numvector_size = 6;
01161 }
01162 if (q->subpacket[s].samples_per_channel > 512) {
01163 q->subpacket[s].log2_numvector_size = 7;
01164 }
01165 } else
01166 q->subpacket[s].samples_per_channel = samples_per_frame;
01167
01168 break;
01169 default:
01170 av_log_ask_for_sample(avctx, "Unknown Cook version.\n");
01171 return AVERROR_PATCHWELCOME;
01172 }
01173
01174 if (s > 1 && q->subpacket[s].samples_per_channel != q->samples_per_channel) {
01175 av_log(avctx, AV_LOG_ERROR, "different number of samples per channel!\n");
01176 return AVERROR_INVALIDDATA;
01177 } else
01178 q->samples_per_channel = q->subpacket[0].samples_per_channel;
01179
01180
01181
01182 q->subpacket[s].numvector_size = (1 << q->subpacket[s].log2_numvector_size);
01183
01184
01185 if (q->subpacket[s].total_subbands > 53) {
01186 av_log_ask_for_sample(avctx, "total_subbands > 53\n");
01187 return AVERROR_PATCHWELCOME;
01188 }
01189
01190 if ((q->subpacket[s].js_vlc_bits > 6) ||
01191 (q->subpacket[s].js_vlc_bits < 2 * q->subpacket[s].joint_stereo)) {
01192 av_log(avctx, AV_LOG_ERROR, "js_vlc_bits = %d, only >= %d and <= 6 allowed!\n",
01193 q->subpacket[s].js_vlc_bits, 2 * q->subpacket[s].joint_stereo);
01194 return AVERROR_INVALIDDATA;
01195 }
01196
01197 if (q->subpacket[s].subbands > 50) {
01198 av_log_ask_for_sample(avctx, "subbands > 50\n");
01199 return AVERROR_PATCHWELCOME;
01200 }
01201 if (q->subpacket[s].subbands == 0) {
01202 av_log_ask_for_sample(avctx, "subbands is 0\n");
01203 return AVERROR_PATCHWELCOME;
01204 }
01205 q->subpacket[s].gains1.now = q->subpacket[s].gain_1;
01206 q->subpacket[s].gains1.previous = q->subpacket[s].gain_2;
01207 q->subpacket[s].gains2.now = q->subpacket[s].gain_3;
01208 q->subpacket[s].gains2.previous = q->subpacket[s].gain_4;
01209
01210 if (q->num_subpackets + q->subpacket[s].num_channels > q->avctx->channels) {
01211 av_log(avctx, AV_LOG_ERROR, "Too many subpackets %d for channels %d\n", q->num_subpackets, q->avctx->channels);
01212 return AVERROR_INVALIDDATA;
01213 }
01214
01215 q->num_subpackets++;
01216 s++;
01217 if (s > MAX_SUBPACKETS) {
01218 av_log_ask_for_sample(avctx, "Too many subpackets > 5\n");
01219 return AVERROR_PATCHWELCOME;
01220 }
01221 }
01222
01223 init_pow2table();
01224 init_gain_table(q);
01225 init_cplscales_table(q);
01226
01227 if ((ret = init_cook_vlc_tables(q)))
01228 return ret;
01229
01230
01231 if (avctx->block_align >= UINT_MAX / 2)
01232 return AVERROR(EINVAL);
01233
01234
01235
01236
01237 q->decoded_bytes_buffer =
01238 av_mallocz(avctx->block_align
01239 + DECODE_BYTES_PAD1(avctx->block_align)
01240 + FF_INPUT_BUFFER_PADDING_SIZE);
01241 if (q->decoded_bytes_buffer == NULL)
01242 return AVERROR(ENOMEM);
01243
01244
01245 if ((ret = init_cook_mlt(q)))
01246 return ret;
01247
01248
01249 if (1) {
01250 q->scalar_dequant = scalar_dequant_float;
01251 q->decouple = decouple_float;
01252 q->imlt_window = imlt_window_float;
01253 q->interpolate = interpolate_float;
01254 q->saturate_output = saturate_output_float;
01255 }
01256
01257
01258 if (q->samples_per_channel != 256 && q->samples_per_channel != 512 &&
01259 q->samples_per_channel != 1024) {
01260 av_log_ask_for_sample(avctx,
01261 "unknown amount of samples_per_channel = %d\n",
01262 q->samples_per_channel);
01263 return AVERROR_PATCHWELCOME;
01264 }
01265
01266 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
01267 if (channel_mask)
01268 avctx->channel_layout = channel_mask;
01269 else
01270 avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
01271
01272 avcodec_get_frame_defaults(&q->frame);
01273 avctx->coded_frame = &q->frame;
01274
01275 #ifdef DEBUG
01276 dump_cook_context(q);
01277 #endif
01278 return 0;
01279 }
01280
01281 AVCodec ff_cook_decoder = {
01282 .name = "cook",
01283 .type = AVMEDIA_TYPE_AUDIO,
01284 .id = AV_CODEC_ID_COOK,
01285 .priv_data_size = sizeof(COOKContext),
01286 .init = cook_decode_init,
01287 .close = cook_decode_close,
01288 .decode = cook_decode_frame,
01289 .capabilities = CODEC_CAP_DR1,
01290 .long_name = NULL_IF_CONFIG_SMALL("Cook / Cooker / Gecko (RealAudio G2)"),
01291 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
01292 AV_SAMPLE_FMT_NONE },
01293 };