59 #define MONO 0x1000001
60 #define STEREO 0x1000002
61 #define JOINT_STEREO 0x1000003
62 #define MC_COOK 0x2000000 // multichannel Cook, not supported
64 #define SUBBAND_SIZE 20
65 #define MAX_SUBPACKETS 5
90 float mono_previous_buffer1[1024];
91 float mono_previous_buffer2[1024];
101 typedef struct cook {
106 void (*scalar_dequant)(
struct cook *q,
int index,
int quant_index,
107 int *subband_coef_index,
int *subband_coef_sign,
110 void (*decouple)(
struct cook *q,
114 float *decode_buffer,
115 float *mlt_buffer1,
float *mlt_buffer2);
117 void (*imlt_window)(
struct cook *q,
float *buffer1,
118 cook_gains *gains_ptr,
float *previous_buffer);
121 int gain_index,
int gain_index_next);
123 void (*saturate_output)(
struct cook *q,
float *
out);
140 VLC envelope_quant_index[13];
145 float gain_table[23];
151 float decode_buffer_1[1024];
152 float decode_buffer_2[1024];
153 float decode_buffer_0[1060];
169 for (i = -63; i < 64; i++) {
180 for (i = 0; i < 23; i++)
191 for (i = 0; i < 13; i++) {
197 for (i = 0; i < 7; i++) {
227 for (j = 0; j < mlt_size; j++)
244 for (i = 0; i < 5; i++)
250 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes) + 3) % 4)
251 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes)))
275 static const uint32_t
tab[4] = {
282 uint32_t *obuf = (uint32_t *) out;
289 off = (intptr_t) inbuffer & 3;
290 buf = (
const uint32_t *) (inbuffer - off);
293 for (i = 0; i < bytes / 4; i++)
294 obuf[i] = c ^ buf[i];
313 for (i = 0; i < 13; i++)
315 for (i = 0; i < 7; i++)
343 gaininfo[i++] = gain;
356 int *quant_index_table)
360 quant_index_table[0] =
get_bits(&q->
gb, 6) - 6;
376 quant_index_table[i] = quant_index_table[i - 1] + j - 12;
377 if (quant_index_table[i] > 63 || quant_index_table[i] < -63) {
379 "Invalid quantizer %d at position %d, outside [-63, 63] range\n",
380 quant_index_table[i], i);
397 int *category,
int *category_index)
399 int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits,
index,
v, i, j;
400 int exp_index2[102] = { 0 };
401 int exp_index1[102] = { 0 };
403 int tmp_categorize_array[128 * 2] = { 0 };
416 for (i = 32; i > 0; i = i / 2) {
420 exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
424 if (num_bits >= bits_left - 32)
431 exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
433 exp_index1[i] = exp_idx;
434 exp_index2[i] = exp_idx;
436 tmpbias1 = tmpbias2 = num_bits;
439 if (tmpbias1 + tmpbias2 > 2 * bits_left) {
443 if (exp_index1[i] < 7) {
444 v = (-2 * exp_index1[i]) - quant_index_table[i] + bias;
453 tmp_categorize_array[tmp_categorize_array1_idx++] =
index;
461 if (exp_index2[i] > 0) {
462 v = (-2 * exp_index2[i]) - quant_index_table[i] + bias;
471 tmp_categorize_array[--tmp_categorize_array2_idx] =
index;
479 category[i] = exp_index2[i];
482 category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
499 int idx = category_index[i];
516 int *subband_coef_index,
int *subband_coef_sign,
523 if (subband_coef_index[i]) {
525 if (subband_coef_sign[i])
545 int *subband_coef_index,
int *subband_coef_sign)
548 int vlc, vd, tmp, result;
552 for (i = 0; i <
vpr_tab[category]; i++) {
558 for (j = vd - 1; j >= 0; j--) {
560 subband_coef_index[vd * i + j] = vlc - tmp * (
kmax_tab[category] + 1);
563 for (j = 0; j < vd; j++) {
564 if (subband_coef_index[i * vd + j]) {
569 subband_coef_sign[i * vd + j] = 0;
572 subband_coef_sign[i * vd + j] = 0;
589 int *quant_index_table,
float *mlt_buffer)
601 index = category[
band];
602 if (category[band] < 7) {
603 if (
unpack_SQVH(q, p, category[band], subband_coef_index, subband_coef_sign)) {
606 category[band + j] = 7;
610 memset(subband_coef_index, 0,
sizeof(subband_coef_index));
611 memset(subband_coef_sign, 0,
sizeof(subband_coef_sign));
614 subband_coef_index, subband_coef_sign,
626 int category_index[128] = { 0 };
627 int category[128] = { 0 };
628 int quant_index_table[102];
634 categorize(q, p, quant_index_table, category, category_index);
655 int gain_index,
int gain_index_next)
659 fc1 =
pow2tab[gain_index + 63];
661 if (gain_index == gain_index_next) {
665 fc2 = q->
gain_table[11 + (gain_index_next - gain_index)];
682 cook_gains *gains_ptr,
float *previous_buffer)
694 inbuffer[i] = inbuffer[i] * fc * q->
mlt_window[i] -
710 cook_gains *gains_ptr,
float *previous_buffer)
719 q->
imlt_window(q, buffer1, gains_ptr, previous_buffer);
722 for (i = 0; i < 8; i++)
723 if (gains_ptr->
now[i] || gains_ptr->
now[i + 1])
725 gains_ptr->
now[i], gains_ptr->
now[i + 1]);
728 memcpy(previous_buffer, buffer0,
745 int length = end - start + 1;
751 for (i = 0; i <
length; i++)
756 for (i = 0; i <
length; i++) {
762 decouple_tab[start + i] =
v;
782 float *decode_buffer,
783 float *mlt_buffer1,
float *mlt_buffer2)
788 mlt_buffer1[SUBBAND_SIZE * subband + j] = f1 * decode_buffer[tmp_idx];
789 mlt_buffer2[SUBBAND_SIZE * subband + j] = f2 * decode_buffer[tmp_idx];
801 float *mlt_buffer_left,
float *mlt_buffer_right)
808 const float *cplscale;
813 memset(mlt_buffer_left, 0, 1024 *
sizeof(*mlt_buffer_left));
814 memset(mlt_buffer_right, 0, 1024 *
sizeof(*mlt_buffer_right));
822 mlt_buffer_left[i * 20 + j] = decode_buffer[i * 40 + j];
823 mlt_buffer_right[i * 20 + j] = decode_buffer[i * 40 + 20 + j];
832 idx -= decouple_tab[cpl_tmp];
834 f1 = cplscale[decouple_tab[cpl_tmp] + 1];
836 q->
decouple(q, p, i, f1, f2, decode_buffer,
837 mlt_buffer_left, mlt_buffer_right);
893 cook_gains *gains_ptr,
float *previous_buffer,
896 imlt_gain(q, decode_buffer, gains_ptr, previous_buffer);
911 const uint8_t *inbuffer,
float **outbuffer)
913 int sub_packet_size = p->
size;
935 outbuffer ? outbuffer[p->
ch_idx] : NULL);
941 outbuffer ? outbuffer[p->
ch_idx + 1] : NULL);
945 outbuffer ? outbuffer[p->
ch_idx + 1] : NULL);
953 int *got_frame_ptr,
AVPacket *avpkt)
957 int buf_size = avpkt->
size;
959 float **samples = NULL;
964 if (buf_size < avctx->block_align)
983 "frame subpacket size total > avctx->block_align!\n");
994 "subpacket[%i] size %i js %i %i block_align %i\n",
1022 #define PRINT(a, b) av_dlog(q->avctx, " %s = %d\n", a, b);
1054 unsigned int channel_mask = 0;
1055 int samples_per_frame = 0;
1060 if (extradata_size <= 0) {
1077 while (edata_ptr < edata_ptr_end) {
1080 if (extradata_size >= 8) {
1082 samples_per_frame = bytestream_get_be16(&edata_ptr);
1084 extradata_size -= 8;
1086 if (extradata_size >= 8) {
1087 bytestream_get_be32(&edata_ptr);
1095 extradata_size -= 8;
1148 if (extradata_size >= 4)
1272 dump_cook_context(q);