26 #define BITSTREAM_READER_LE
77 0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
78 0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
79 0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
80 0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
81 0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
82 0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
83 0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
84 0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
89 1.18750000f, 1.68359380f, 2.37500000f, 3.36718750f, 4.75000000f,
90 6.73437500f, 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f,
91 38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, 152.000000f,
92 215.500000f, 304.000000f, 431.000000f, 608.000000f, 862.000000f,
93 1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, 4864.00000f,
94 6896.00000f, 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f,
95 38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, 155648.000f,
96 220672.000f, 311296.000f, 441344.000f, 622592.000f, 882688.000f,
97 1245184.00f, 1765376.00f, 2490368.00f, 3530752.00f, 4980736.00f,
98 7061504.00f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102 0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64,
103 80, 96, 120, 144, 176, 208, 240, 256,
104 0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104,
105 128, 160, 208, 256, 0, 0, 0, 0, 0,
106 0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208,
107 256, 0, 0, 0, 0, 0, 0, 0, 0,
108 0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110 0, 4, 16, 32, 64, 256, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
115 19, 14, 11, 9, 4, 2, 0
123 12, 7, 9, 7, 10, 9, 11, 9, 9, 2, 9, 9, 9, 9,
124 9, 3, 9, 10, 10, 12, 2, 3, 3, 5, 5, 6, 7,
128 0, 10, 11, 12, 13, 14, 15, 16, 18, 1, 20, 22, 24,
129 26, 28, 2, 30, 32, 34, 36, 3, 4, 5, 6, 7, 8, 9,
133 0xC7A, 0x002, 0x0FA, 0x03A, 0x35A, 0x1C2, 0x07A, 0x1FA,
134 0x17A, 0x000, 0x0DA, 0x142, 0x0C2, 0x042, 0x1DA, 0x001,
135 0x05A, 0x15A, 0x27A, 0x47A, 0x003, 0x005, 0x006, 0x012,
140 10, 8, 5, 1, 2, 4, 4, 4, 6, 7, 9, 10,
144 0, 13, 17, 1, 2, 3, 4, 5, 6, 7, 8, 9,
148 0x30B, 0x8B, 0x1B, 0x0, 0x1, 0x3, 0x7, 0xF, 0x2b, 0x4B, 0xB, 0x10B,
152 18, 2, 4, 4, 5, 4, 4, 5, 5, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 6,
153 7, 6, 6, 6, 7, 7, 7, 7, 7, 8, 9, 9, 8, 9, 11, 11, 12, 12, 13, 12,
158 0x2AD46, 0x1, 0x0, 0x3, 0xC, 0xA, 0x7, 0x18, 0x12, 0xE, 0x4, 0x16,
159 0xF, 0x1C, 0x8, 0x22, 0x26, 0x2, 0x3B, 0x34, 0x74, 0x1F, 0x14, 0x2B,
160 0x1B, 0x3F, 0x28, 0x54, 0x6, 0x4B, 0xB, 0x68, 0xE8, 0x46, 0xC6, 0x1E8,
161 0x146, 0x346, 0x546, 0x746, 0x1D46, 0xF46, 0xD46, 0x6D46, 0xAD46, 0x2D46,
166 13, 7, 8, 9, 10, 10, 10, 10, 10, 9, 8, 7, 6,
167 5, 4, 3, 3, 2, 3, 3, 4, 5, 7, 8, 9, 11, 12, 13,
171 0x1EC6, 0x6, 0xC2, 0x142, 0x242, 0x246, 0xC6, 0x46, 0x42, 0x146, 0xA2,
172 0x62, 0x26, 0x16, 0xE, 0x5, 0x4, 0x3, 0x0, 0x1, 0xA, 0x12, 0x2, 0x22,
173 0x1C6, 0x2C6, 0x6C6, 0xEC6,
177 8, 2, 1, 3, 4, 5, 6, 7, 8,
181 0xFE, 0x0, 0x1, 0x2, 0x6, 0xE, 0x1E, 0x3E, 0x7E,
185 6, 2, 2, 4, 4, 6, 5, 4, 2,
189 0x35, 0x2, 0x0, 0x1, 0xD, 0x15, 0x5, 0x9, 0x3,
192 #define INIT_VLC_STATIC_LE(vlc, nb_bits, nb_codes, \
193 bits, bits_wrap, bits_size, \
194 codes, codes_wrap, codes_size, \
195 symbols, symbols_wrap, symbols_size, \
198 static VLC_TYPE table[static_size][2]; \
199 (vlc)->table = table; \
200 (vlc)->table_allocated = static_size; \
201 ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
202 bits, bits_wrap, bits_size, \
203 codes, codes_wrap, codes_size, \
204 symbols, symbols_wrap, symbols_size, \
205 INIT_VLC_LE | INIT_VLC_USE_NEW_STATIC); \
226 for (
i = 0;
i < 512;
i++)
232 int i, j, n0, n1, n2,
diff;
239 nptr =
s->noise_buffer + 256 * j;
241 for (
i = 0;
i + n0 < n1;
i++, nptr++)
242 nptr[0] =
i / (
float)(n1 - n0);
245 nptr =
s->noise_buffer + (j << 8) + n1 - n0;
247 for (
i = n1;
i < n2;
i++, nptr++,
diff--)
248 nptr[0] =
diff / (
float)(n2 - n1);
256 int ret, fft_size, fft_order,
size,
g, j, x;
269 if (bytestream2_peek_be64(&
b) == (((uint64_t)
MKBETAG(
'f',
'r',
'm',
'a') << 32) |
270 (uint64_t)
MKBETAG(
'Q',
'D',
'M',
'C')))
282 size = bytestream2_get_be32u(&
b);
289 if (bytestream2_get_be32u(&
b) !=
MKBETAG(
'Q',
'D',
'C',
'A')) {
295 avctx->
channels =
s->nb_channels = bytestream2_get_be32u(&
b);
296 if (
s->nb_channels <= 0 ||
s->nb_channels > 2) {
304 avctx->
bit_rate = bytestream2_get_be32u(&
b);
306 fft_size = bytestream2_get_be32u(&
b);
307 fft_order =
av_log2(fft_size) + 1;
308 s->checksum_size = bytestream2_get_be32u(&
b);
309 if (
s->checksum_size >= 1
U << 28) {
324 s->frame_size = 1 <<
s->frame_bits;
325 s->subframe_size =
s->frame_size >> 5;
331 if ((fft_order < 7) || (fft_order > 9)) {
336 if (fft_size != (1 << (fft_order - 1))) {
347 for (
g = 5;
g > 0;
g--) {
348 for (j = 0; j < (1 <<
g) - 1; j++)
349 s->alt_sin[5-
g][j] =
sin_table[(((j+1) << (8 -
g)) & 0x1FF)];
397 if (label !=
MKTAG(
'Q',
'M',
'C', 1))
400 for (
i = 0;
i <
s->checksum_size - 6;
i++)
408 int ch, j, k, v, idx, band, lastval, newval,
len;
410 for (ch = 0; ch <
s->nb_channels; ch++) {
422 s->noise[ch][band][0] = lastval - 1;
423 for (j = 0; j < 15;) {
434 newval = lastval + (v + 1) / 2;
436 newval = lastval - v / 2;
442 for (k = 1; idx <= j +
len; k++, idx++)
443 s->noise[ch][band][idx] = lastval + k * (newval - lastval) /
len - 1;
456 const int index =
s->nb_tones[group];
464 s->tones[group][
index].freq = freq;
465 s->tones[group][
index].mode = stereo_mode;
466 s->tones[group][
index].amplitude = amplitude;
467 s->tones[group][
index].phase = phase;
468 s->nb_tones[group]++;
473 int amp, phase, stereo_mode = 0,
i, group, freq, group_size, group_bits;
474 int amp2, phase2, pos2, off;
476 for (group = 0; group < 5; group++) {
477 group_size = 1 << (
s->frame_bits - group - 1);
478 group_bits = 4 - group;
482 for (
i = 1; ;
i = freq + 1) {
490 while (freq >= group_size - 1) {
491 freq += 2 - group_size;
493 off += 1 << group_bits;
496 if (pos2 >=
s->frame_size)
499 if (
s->nb_channels > 1)
507 if (stereo_mode > 1) {
516 phase2 = phase - phase2;
522 if ((freq >> group_bits) + 1 <
s->subframe_size) {
523 add_tone(
s, group, off, freq, stereo_mode & 1, amp, phase);
525 add_tone(
s, group, off, freq, ~stereo_mode & 1, amp2, phase2);
535 int subframe_size,
i, j, k, length;
536 float scale, *noise_ptr;
538 scale = 0.5 * amplitude;
539 subframe_size =
s->subframe_size;
540 if (subframe_size >= node2)
541 subframe_size = node2;
542 length = (subframe_size - node1) & 0xFFFC;
544 noise_ptr = &
s->noise_buffer[256 *
index];
546 for (
i = 0;
i < length;
i += 4, j+= 4, noise_ptr += 4) {
547 s->noise2_buffer[j ] += scale * noise_ptr[0];
548 s->noise2_buffer[j + 1] += scale * noise_ptr[1];
549 s->noise2_buffer[j + 2] += scale * noise_ptr[2];
550 s->noise2_buffer[j + 3] += scale * noise_ptr[3];
554 noise_ptr =
s->noise_buffer + length + (
index << 8);
555 for (
i = length;
i < subframe_size - node1;
i++, k++, noise_ptr++)
556 s->noise2_buffer[k] += scale * noise_ptr[0];
563 float *
im = &
s->fft_buffer[0 + ch][
s->fft_offset +
s->subframe_size * current_subframe];
564 float *
re = &
s->fft_buffer[2 + ch][
s->fft_offset +
s->subframe_size * current_subframe];
566 memset(
s->noise2_buffer, 0, 4 *
s->subframe_size);
569 if (
qdmc_nodes[
i + 21 *
s->band_index] >
s->subframe_size - 1)
572 aindex =
s->noise[ch][
i][current_subframe / 2];
573 amplitude = aindex > 0 ?
amplitude_tab[aindex & 0x3F] : 0.0f;
579 for (j = 2; j <
s->subframe_size - 1; j++) {
580 float rnd_re, rnd_im;
582 s->rndval = 214013
U *
s->rndval + 2531011;
583 rnd_im = ((
s->rndval & 0x7FFF) - 16384.0
f) * 0.000030517578f *
s->noise2_buffer[j];
584 s->rndval = 214013
U *
s->rndval + 2531011;
585 rnd_re = ((
s->rndval & 0x7FFF) - 16384.0
f) * 0.000030517578f *
s->noise2_buffer[j];
595 int j, group_bits,
pos, pindex;
596 float im,
re, amplitude,
level, *imptr, *reptr;
598 if (
s->nb_channels == 1)
601 group_bits = 4 - group;
602 pos = freqs >> (4 - group);
604 imptr = &
s->fft_buffer[ stereo_mode][
s->fft_offset +
s->subframe_size *
offset +
pos];
605 reptr = &
s->fft_buffer[2 + stereo_mode][
s->fft_offset +
s->subframe_size *
offset +
pos];
606 pindex = (phase << 6) - ((2 * (freqs >> (4 - group)) + 1) << 7);
607 for (j = 0; j < (1 << (group_bits + 1)) - 1; j++) {
608 pindex += (2 * freqs + 1) << (7 - group_bits);
609 level = amplitude *
s->alt_sin[group][j];
616 imptr +=
s->subframe_size;
617 reptr +=
s->subframe_size;
618 if (imptr >= &
s->fft_buffer[stereo_mode][2 *
s->frame_size]) {
619 imptr = &
s->fft_buffer[0 + stereo_mode][
pos];
620 reptr = &
s->fft_buffer[2 + stereo_mode][
pos];
630 if (
s->nb_channels == 1)
636 pos =
s->fft_offset + freqs +
s->subframe_size *
offset;
637 s->fft_buffer[ stereo_mode][
pos ] +=
im;
638 s->fft_buffer[2 + stereo_mode][
pos ] +=
re;
639 s->fft_buffer[ stereo_mode][
pos + 1] -=
im;
640 s->fft_buffer[2 + stereo_mode][
pos + 1] -=
re;
647 for (
g = 0;
g < 4;
g++) {
648 for (
w =
s->cur_tone[
g]; w < s->nb_tones[
g];
w++) {
651 if (current_subframe < t->
offset)
657 for (
w =
s->cur_tone[4]; w < s->nb_tones[4];
w++) {
660 if (current_subframe < t->
offset)
674 s->fft_offset =
s->frame_size -
s->fft_offset;
675 s->buffer_ptr = &
s->buffer[
s->nb_channels *
s->buffer_offset];
685 for (n = 0; n < 32; n++) {
688 for (ch = 0; ch <
s->nb_channels; ch++)
693 for (ch = 0; ch <
s->nb_channels; ch++) {
694 for (
i = 0;
i <
s->subframe_size;
i++) {
695 s->cmplx[ch][
i].re =
s->fft_buffer[ch + 2][
s->fft_offset + n *
s->subframe_size +
i];
696 s->cmplx[ch][
i].im =
s->fft_buffer[ch + 0][
s->fft_offset + n *
s->subframe_size +
i];
697 s->cmplx[ch][
s->subframe_size +
i].re = 0;
698 s->cmplx[ch][
s->subframe_size +
i].im = 0;
702 for (ch = 0; ch <
s->nb_channels; ch++) {
703 s->fft_ctx.fft_permute(&
s->fft_ctx,
s->cmplx[ch]);
704 s->fft_ctx.fft_calc(&
s->fft_ctx,
s->cmplx[ch]);
707 r = &
s->buffer_ptr[
s->nb_channels * n *
s->subframe_size];
708 for (
i = 0;
i < 2 *
s->subframe_size;
i++) {
709 for (ch = 0; ch <
s->nb_channels; ch++) {
710 *
r++ +=
s->cmplx[ch][
i].re;
714 r = &
s->buffer_ptr[n *
s->subframe_size *
s->nb_channels];
715 for (
i = 0;
i <
s->nb_channels *
s->subframe_size;
i++) {
716 out[
i] = av_clipf(
r[
i], INT16_MIN, INT16_MAX);
718 out +=
s->subframe_size *
s->nb_channels;
720 for (ch = 0; ch <
s->nb_channels; ch++) {
721 memset(
s->fft_buffer[ch+0] +
s->fft_offset + n *
s->subframe_size, 0, 4 *
s->subframe_size);
722 memset(
s->fft_buffer[ch+2] +
s->fft_offset + n *
s->subframe_size, 0, 4 *
s->subframe_size);
724 memset(
s->buffer +
s->nb_channels * (n *
s->subframe_size +
s->frame_size +
s->buffer_offset), 0, 4 *
s->subframe_size *
s->nb_channels);
727 s->buffer_offset +=
s->frame_size;
728 if (
s->buffer_offset >= 32768 -
s->frame_size) {
729 memcpy(
s->buffer, &
s->buffer[
s->nb_channels *
s->buffer_offset], 4 *
s->frame_size *
s->nb_channels);
730 s->buffer_offset = 0;
740 memset(
s->buffer, 0,
sizeof(
s->buffer));
741 memset(
s->fft_buffer, 0,
sizeof(
s->fft_buffer));
743 s->buffer_offset = 0;
747 int *got_frame_ptr,
AVPacket *avpkt)
756 if (avpkt->
size <
s->checksum_size)
760 frame->nb_samples =
s->frame_size;
767 memset(
s->nb_tones, 0,
sizeof(
s->nb_tones));
768 memset(
s->cur_tone, 0,
sizeof(
s->cur_tone));
773 return s->checksum_size;