60 #define MONO 0x1000001
61 #define STEREO 0x1000002
62 #define JOINT_STEREO 0x1000003
63 #define MC_COOK 0x2000000 // multichannel Cook, not supported
65 #define SUBBAND_SIZE 20
66 #define MAX_SUBPACKETS 5
91 float mono_previous_buffer1[1024];
92 float mono_previous_buffer2[1024];
102 typedef struct cook {
107 void (*scalar_dequant)(
struct cook *q,
int index,
int quant_index,
108 int *subband_coef_index,
int *subband_coef_sign,
111 void (*decouple)(
struct cook *q,
115 float *decode_buffer,
116 float *mlt_buffer1,
float *mlt_buffer2);
118 void (*imlt_window)(
struct cook *q,
float *buffer1,
119 cook_gains *gains_ptr,
float *previous_buffer);
122 int gain_index,
int gain_index_next);
124 void (*saturate_output)(
struct cook *q,
float *
out);
141 VLC envelope_quant_index[13];
146 float gain_table[23];
152 float decode_buffer_1[1024];
153 float decode_buffer_2[1024];
154 float decode_buffer_0[1060];
170 for (i = -63; i < 64; i++) {
181 for (i = 0; i < 23; i++)
192 for (i = 0; i < 13; i++) {
198 for (i = 0; i < 7; i++) {
228 for (j = 0; j < mlt_size; j++)
245 for (i = 0; i < 5; i++)
251 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes) + 3) % 4)
252 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
276 static const uint32_t
tab[4] = {
283 uint32_t *obuf = (uint32_t *) out;
290 off = (intptr_t) inbuffer & 3;
291 buf = (
const uint32_t *) (inbuffer - off);
294 for (i = 0; i < bytes / 4; i++)
295 obuf[i] = c ^ buf[i];
314 for (i = 0; i < 13; i++)
316 for (i = 0; i < 7; i++)
344 gaininfo[i++] = gain;
357 int *quant_index_table)
361 quant_index_table[0] =
get_bits(&q->
gb, 6) - 6;
377 quant_index_table[i] = quant_index_table[i - 1] + j - 12;
378 if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
380 "Invalid quantizer %d at position %d, outside [-63, 63] range\n",
381 quant_index_table[i], i);
400 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits,
index,
v, i, j;
401 int exp_index2[102] = { 0 };
402 int exp_index1[102] = { 0 };
404 int tmp_categorize_array[128 * 2] = { 0 };
417 for (i = 32; i > 0; i = i / 2) {
421 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
425 if (num_bits >= bits_left - 32)
432 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
434 exp_index1[i] = exp_idx;
435 exp_index2[i] = exp_idx;
437 tmpbias1 = tmpbias2 = num_bits;
440 if (tmpbias1 + tmpbias2 > 2 * bits_left) {
444 if (exp_index1[i] < 7) {
445 v = (-2 * exp_index1[i]) - quant_index_table[i] + bias;
454 tmp_categorize_array[tmp_categorize_array1_idx++] =
index;
462 if (exp_index2[i] > 0) {
463 v = (-2 * exp_index2[i]) - quant_index_table[i] + bias;
472 tmp_categorize_array[--tmp_categorize_array2_idx] =
index;
480 category[i] = exp_index2[i];
483 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
500 int idx = category_index[i];
517 int *subband_coef_index,
int *subband_coef_sign,
524 if (subband_coef_index[i]) {
526 if (subband_coef_sign[i])
546 int *subband_coef_index,
int *subband_coef_sign)
549 int vlc, vd, tmp, result;
559 for (j = vd - 1; j >= 0; j--) {
564 for (j = 0; j < vd; j++) {
565 if (subband_coef_index[i * vd + j]) {
570 subband_coef_sign[i * vd + j] = 0;
573 subband_coef_sign[i * vd + j] = 0;
590 int *quant_index_table,
float *mlt_buffer)
602 index = category[
band];
603 if (category[band] < 7) {
604 if (
unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)) {
607 category[band + j] = 7;
611 memset(subband_coef_index, 0,
sizeof(subband_coef_index));
612 memset(subband_coef_sign, 0,
sizeof(subband_coef_sign));
615 subband_coef_index, subband_coef_sign,
627 int category_index[128] = { 0 };
629 int quant_index_table[102];
635 categorize(q, p, quant_index_table, category, category_index);
656 int gain_index,
int gain_index_next)
660 fc1 =
pow2tab[gain_index + 63];
662 if (gain_index == gain_index_next) {
666 fc2 = q->
gain_table[11 + (gain_index_next - gain_index)];
683 cook_gains *gains_ptr,
float *previous_buffer)
695 inbuffer[i] = inbuffer[i] * fc * q->
mlt_window[i] -
711 cook_gains *gains_ptr,
float *previous_buffer)
720 q->
imlt_window(q, buffer1, gains_ptr, previous_buffer);
723 for (i = 0; i < 8; i++)
724 if (gains_ptr->
now[i] || gains_ptr->
now[i + 1])
726 gains_ptr->
now[i], gains_ptr->
now[i + 1]);
729 memcpy(previous_buffer, buffer0,
746 int length = end - start + 1;
752 for (i = 0; i <
length; i++)
757 for (i = 0; i <
length; i++) {
763 decouple_tab[start + i] =
v;
783 float *decode_buffer,
784 float *mlt_buffer1,
float *mlt_buffer2)
789 mlt_buffer1[SUBBAND_SIZE * subband + j] = f1 * decode_buffer[tmp_idx];
790 mlt_buffer2[SUBBAND_SIZE * subband + j] = f2 * decode_buffer[tmp_idx];
802 float *mlt_buffer_left,
float *mlt_buffer_right)
809 const float *cplscale;
814 memset(mlt_buffer_left, 0, 1024 *
sizeof(*mlt_buffer_left));
815 memset(mlt_buffer_right, 0, 1024 *
sizeof(*mlt_buffer_right));
823 mlt_buffer_left[i * 20 + j] = decode_buffer[i * 40 + j];
824 mlt_buffer_right[i * 20 + j] = decode_buffer[i * 40 + 20 + j];
833 idx -= decouple_tab[cpl_tmp];
835 f1 = cplscale[decouple_tab[cpl_tmp] + 1];
837 q->
decouple(q, p, i, f1, f2, decode_buffer,
838 mlt_buffer_left, mlt_buffer_right);
894 cook_gains *gains_ptr,
float *previous_buffer,
897 imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
912 const uint8_t *inbuffer,
float **outbuffer)
914 int sub_packet_size = p->
size;
936 outbuffer ? outbuffer[p->
ch_idx] : NULL);
942 outbuffer ? outbuffer[p->
ch_idx + 1] : NULL);
946 outbuffer ? outbuffer[p->
ch_idx + 1] : NULL);
954 int *got_frame_ptr,
AVPacket *avpkt)
958 int buf_size = avpkt->
size;
960 float **samples = NULL;
965 if (buf_size < avctx->block_align)
984 "frame subpacket size total > avctx->block_align!\n");
995 "subpacket[%i] size %i js %i %i block_align %i\n",
1023 #define PRINT(a, b) av_dlog(q->avctx, " %s = %d\n", a, b);
1055 unsigned int channel_mask = 0;
1056 int samples_per_frame = 0;
1061 if (extradata_size <= 0) {
1078 while (edata_ptr < edata_ptr_end) {
1081 if (extradata_size >= 8) {
1083 samples_per_frame = bytestream_get_be16(&edata_ptr);
1085 extradata_size -= 8;
1087 if (extradata_size >= 8) {
1088 bytestream_get_be32(&edata_ptr);
1096 extradata_size -= 8;
1149 if (extradata_size >= 4)
1273 dump_cook_context(q);