25 #define BITSTREAM_READER_LE
80 0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
81 0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
82 0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
83 0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
84 0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
85 0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
86 0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
87 0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
92 1.18750000f, 1.68359380f, 2.37500000f, 3.36718750f, 4.75000000f,
93 6.73437500f, 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f,
94 38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, 152.000000f,
95 215.500000f, 304.000000f, 431.000000f, 608.000000f, 862.000000f,
96 1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, 4864.00000f,
97 6896.00000f, 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f,
98 38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, 155648.000f,
99 220672.000f, 311296.000f, 441344.000f, 622592.000f, 882688.000f,
100 1245184.00f, 1765376.00f, 2490368.00f, 3530752.00f, 4980736.00f,
101 7061504.00f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64,
106 80, 96, 120, 144, 176, 208, 240, 256,
107 0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104,
108 128, 160, 208, 256, 0, 0, 0, 0, 0,
109 0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208,
110 256, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256,
112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 0, 4, 16, 32, 64, 256, 0, 0, 0, 0, 0, 0, 0, 0,
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
118 19, 14, 11, 9, 4, 2, 0
127 { 1, 2 }, { 10, 7 }, { 26, 9 }, { 22, 9 }, { 24, 9 }, { 14, 9 },
128 { 8, 6 }, { 6, 5 }, { 7, 5 }, { 9, 7 }, { 30, 9 }, { 32, 10 },
129 { 13, 10 }, { 20, 9 }, { 28, 9 }, { 12, 7 }, { 15, 11 }, { 36, 12 },
130 { 0, 12 }, { 34, 10 }, { 18, 9 }, { 11, 9 }, { 16, 9 }, { 5, 3 },
131 { 2, 3 }, { 4, 3 }, { 3, 2 },
133 { 1, 1 }, { 2, 2 }, { 3, 4 }, { 8, 9 }, { 9, 10 }, { 0, 10 },
134 { 13, 8 }, { 7, 7 }, { 6, 6 }, { 17, 5 }, { 4, 4 }, { 5, 4 },
136 { 18, 3 }, { 16, 3 }, { 22, 7 }, { 8, 10 }, { 4, 10 }, { 3, 9 },
137 { 2, 8 }, { 23, 8 }, { 10, 8 }, { 11, 7 }, { 21, 5 }, { 20, 4 },
138 { 1, 7 }, { 7, 10 }, { 5, 10 }, { 9, 9 }, { 6, 10 }, { 25, 11 },
139 { 26, 12 }, { 27, 13 }, { 0, 13 }, { 24, 9 }, { 12, 6 }, { 13, 5 },
140 { 14, 4 }, { 19, 3 }, { 15, 3 }, { 17, 2 },
142 { 2, 4 }, { 14, 6 }, { 26, 7 }, { 31, 8 }, { 32, 9 }, { 35, 9 },
143 { 7, 5 }, { 10, 5 }, { 22, 7 }, { 27, 7 }, { 19, 7 }, { 20, 7 },
144 { 4, 5 }, { 13, 5 }, { 17, 6 }, { 15, 6 }, { 8, 5 }, { 5, 4 },
145 { 28, 7 }, { 33, 9 }, { 36, 11 }, { 38, 12 }, { 42, 14 }, { 45, 16 },
146 { 44, 18 }, { 0, 18 }, { 46, 17 }, { 43, 15 }, { 40, 13 }, { 37, 11 },
147 { 39, 12 }, { 41, 12 }, { 34, 8 }, { 16, 6 }, { 11, 5 }, { 9, 4 },
148 { 1, 2 }, { 3, 4 }, { 30, 7 }, { 29, 7 }, { 23, 6 }, { 24, 6 },
149 { 18, 6 }, { 6, 4 }, { 12, 5 }, { 21, 6 }, { 25, 6 },
151 { 1, 2 }, { 3, 3 }, { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
152 { 8, 8 }, { 0, 8 }, { 2, 1 },
154 { 2, 2 }, { 1, 2 }, { 3, 4 }, { 7, 4 }, { 6, 5 }, { 5, 6 },
155 { 0, 6 }, { 4, 4 }, { 8, 2 },
172 static VLCElem vlc_buffer[13698];
176 &hufftab[0][1], 2, &hufftab[0][0], 2, 1, -1,
182 for (
i = 0;
i < 512;
i++)
188 int i, j, n0, n1, n2,
diff;
195 nptr =
s->noise_buffer + 256 * j;
197 for (
i = 0;
i + n0 < n1;
i++, nptr++)
198 nptr[0] =
i / (
float)(n1 - n0);
201 nptr =
s->noise_buffer + (j << 8) + n1 - n0;
203 for (
i = n1;
i < n2;
i++, nptr++,
diff--)
204 nptr[0] =
diff / (
float)(n2 - n1);
212 int ret, fft_size, fft_order,
size,
g, j, x;
226 if (bytestream2_peek_be64(&
b) == (((uint64_t)
MKBETAG(
'f',
'r',
'm',
'a') << 32) |
227 (uint64_t)
MKBETAG(
'Q',
'D',
'M',
'C')))
239 size = bytestream2_get_be32u(&
b);
246 if (bytestream2_get_be32u(&
b) !=
MKBETAG(
'Q',
'D',
'C',
'A')) {
252 s->nb_channels = bytestream2_get_be32u(&
b);
253 if (
s->nb_channels <= 0 ||
s->nb_channels > 2) {
262 avctx->
bit_rate = bytestream2_get_be32u(&
b);
264 fft_size = bytestream2_get_be32u(&
b);
265 fft_order =
av_log2(fft_size) + 1;
266 s->checksum_size = bytestream2_get_be32u(&
b);
267 if (
s->checksum_size >= 1
U << 28) {
282 s->frame_size = 1 <<
s->frame_bits;
283 s->subframe_size =
s->frame_size >> 5;
289 if ((fft_order < 7) || (fft_order > 9)) {
294 if (fft_size != (1 << (fft_order - 1))) {
305 for (
g = 5;
g > 0;
g--) {
306 for (j = 0; j < (1 <<
g) - 1; j++)
307 s->alt_sin[5-
g][j] =
sin_table[(((j+1) << (8 -
g)) & 0x1FF)];
347 uint16_t sum = 226, checksum =
get_bits(gb, 16);
348 const uint8_t *ptr = gb->
buffer + 6;
351 if (label !=
MKTAG(
'Q',
'M',
'C', 1))
354 for (
i = 0;
i <
s->checksum_size - 6;
i++)
357 return sum != checksum;
362 int ch, j, k, v, idx, band, lastval, newval,
len;
364 for (ch = 0; ch <
s->nb_channels; ch++) {
376 s->noise[ch][band][0] = lastval - 1;
377 for (j = 0; j < 15;) {
388 newval = lastval + (v + 1) / 2;
390 newval = lastval - v / 2;
396 for (k = 1; idx <= j +
len; k++, idx++)
397 s->noise[ch][band][idx] = lastval + k * (newval - lastval) /
len - 1;
410 const int index =
s->nb_tones[group];
418 s->tones[group][
index].freq = freq;
419 s->tones[group][
index].mode = stereo_mode;
420 s->tones[group][
index].amplitude = amplitude;
421 s->tones[group][
index].phase = phase;
422 s->nb_tones[group]++;
427 int amp, phase, stereo_mode = 0,
i, group, freq, group_size, group_bits;
428 int amp2, phase2, pos2, off;
430 for (group = 0; group < 5; group++) {
431 group_size = 1 << (
s->frame_bits - group - 1);
432 group_bits = 4 - group;
436 for (
i = 1; ;
i = freq + 1) {
444 while (freq >= group_size - 1) {
445 freq += 2 - group_size;
447 off += 1 << group_bits;
450 if (pos2 >=
s->frame_size)
453 if (
s->nb_channels > 1)
461 if (stereo_mode > 1) {
470 phase2 = phase - phase2;
476 if ((freq >> group_bits) + 1 <
s->subframe_size) {
477 add_tone(
s, group, off, freq, stereo_mode & 1, amp, phase);
479 add_tone(
s, group, off, freq, ~stereo_mode & 1, amp2, phase2);
489 int subframe_size,
i, j, k, length;
490 float scale, *noise_ptr;
492 scale = 0.5 * amplitude;
493 subframe_size =
s->subframe_size;
494 if (subframe_size >= node2)
495 subframe_size = node2;
496 length = (subframe_size - node1) & 0xFFFC;
498 noise_ptr = &
s->noise_buffer[256 *
index];
500 for (
i = 0;
i < length;
i += 4, j+= 4, noise_ptr += 4) {
501 s->noise2_buffer[j ] +=
scale * noise_ptr[0];
502 s->noise2_buffer[j + 1] +=
scale * noise_ptr[1];
503 s->noise2_buffer[j + 2] +=
scale * noise_ptr[2];
504 s->noise2_buffer[j + 3] +=
scale * noise_ptr[3];
508 noise_ptr =
s->noise_buffer + length + (
index << 8);
509 for (
i = length;
i < subframe_size - node1;
i++, k++, noise_ptr++)
510 s->noise2_buffer[k] +=
scale * noise_ptr[0];
517 float *
im = &
s->fft_buffer[0 + ch][
s->fft_offset +
s->subframe_size * current_subframe];
518 float *
re = &
s->fft_buffer[2 + ch][
s->fft_offset +
s->subframe_size * current_subframe];
520 memset(
s->noise2_buffer, 0, 4 *
s->subframe_size);
523 if (
qdmc_nodes[
i + 21 *
s->band_index] >
s->subframe_size - 1)
526 aindex =
s->noise[ch][
i][current_subframe / 2];
527 amplitude = aindex > 0 ?
amplitude_tab[aindex & 0x3F] : 0.0f;
533 for (j = 2; j <
s->subframe_size - 1; j++) {
534 float rnd_re, rnd_im;
536 s->rndval = 214013
U *
s->rndval + 2531011;
537 rnd_im = ((
s->rndval & 0x7FFF) - 16384.0
f) * 0.000030517578f *
s->noise2_buffer[j];
538 s->rndval = 214013
U *
s->rndval + 2531011;
539 rnd_re = ((
s->rndval & 0x7FFF) - 16384.0
f) * 0.000030517578f *
s->noise2_buffer[j];
549 int j, group_bits,
pos, pindex;
550 float im,
re, amplitude,
level, *imptr, *reptr;
552 if (
s->nb_channels == 1)
555 group_bits = 4 - group;
556 pos = freqs >> (4 - group);
558 imptr = &
s->fft_buffer[ stereo_mode][
s->fft_offset +
s->subframe_size *
offset +
pos];
559 reptr = &
s->fft_buffer[2 + stereo_mode][
s->fft_offset +
s->subframe_size *
offset +
pos];
560 pindex = (phase << 6) - ((2 * (freqs >> (4 - group)) + 1) << 7);
561 for (j = 0; j < (1 << (group_bits + 1)) - 1; j++) {
562 pindex += (2 * freqs + 1) << (7 - group_bits);
563 level = amplitude *
s->alt_sin[group][j];
570 imptr +=
s->subframe_size;
571 reptr +=
s->subframe_size;
572 if (imptr >= &
s->fft_buffer[stereo_mode][2 *
s->frame_size]) {
573 imptr = &
s->fft_buffer[0 + stereo_mode][
pos];
574 reptr = &
s->fft_buffer[2 + stereo_mode][
pos];
584 if (
s->nb_channels == 1)
590 pos =
s->fft_offset + freqs +
s->subframe_size *
offset;
591 s->fft_buffer[ stereo_mode][
pos ] +=
im;
592 s->fft_buffer[2 + stereo_mode][
pos ] +=
re;
593 s->fft_buffer[ stereo_mode][
pos + 1] -=
im;
594 s->fft_buffer[2 + stereo_mode][
pos + 1] -=
re;
601 for (
g = 0;
g < 4;
g++) {
602 for (
w =
s->cur_tone[
g]; w < s->nb_tones[
g];
w++) {
605 if (current_subframe < t->
offset)
611 for (
w =
s->cur_tone[4]; w < s->nb_tones[4];
w++) {
614 if (current_subframe < t->
offset)
628 s->fft_offset =
s->frame_size -
s->fft_offset;
629 s->buffer_ptr = &
s->buffer[
s->nb_channels *
s->buffer_offset];
639 for (n = 0; n < 32; n++) {
642 for (ch = 0; ch <
s->nb_channels; ch++)
647 for (ch = 0; ch <
s->nb_channels; ch++) {
648 for (
i = 0;
i <
s->subframe_size;
i++) {
649 s->cmplx_in[ch][
i].re =
s->fft_buffer[ch + 2][
s->fft_offset + n *
s->subframe_size +
i];
650 s->cmplx_in[ch][
i].im =
s->fft_buffer[ch + 0][
s->fft_offset + n *
s->subframe_size +
i];
651 s->cmplx_in[ch][
s->subframe_size +
i].re = 0;
652 s->cmplx_in[ch][
s->subframe_size +
i].im = 0;
656 for (ch = 0; ch <
s->nb_channels; ch++) {
657 s->itx_fn(
s->fft_ctx,
s->cmplx_out[ch],
s->cmplx_in[ch],
sizeof(
float));
660 r = &
s->buffer_ptr[
s->nb_channels * n *
s->subframe_size];
661 for (
i = 0;
i < 2 *
s->subframe_size;
i++) {
662 for (ch = 0; ch <
s->nb_channels; ch++) {
663 *
r++ +=
s->cmplx_out[ch][
i].re;
667 r = &
s->buffer_ptr[n *
s->subframe_size *
s->nb_channels];
668 for (
i = 0;
i <
s->nb_channels *
s->subframe_size;
i++) {
671 out +=
s->subframe_size *
s->nb_channels;
673 for (ch = 0; ch <
s->nb_channels; ch++) {
674 memset(
s->fft_buffer[ch+0] +
s->fft_offset + n *
s->subframe_size, 0, 4 *
s->subframe_size);
675 memset(
s->fft_buffer[ch+2] +
s->fft_offset + n *
s->subframe_size, 0, 4 *
s->subframe_size);
677 memset(
s->buffer +
s->nb_channels * (n *
s->subframe_size +
s->frame_size +
s->buffer_offset), 0, 4 *
s->subframe_size *
s->nb_channels);
680 s->buffer_offset +=
s->frame_size;
681 if (
s->buffer_offset >= 32768 -
s->frame_size) {
682 memcpy(
s->buffer, &
s->buffer[
s->nb_channels *
s->buffer_offset], 4 *
s->frame_size *
s->nb_channels);
683 s->buffer_offset = 0;
693 memset(
s->buffer, 0,
sizeof(
s->buffer));
694 memset(
s->fft_buffer, 0,
sizeof(
s->fft_buffer));
696 s->buffer_offset = 0;
700 int *got_frame_ptr,
AVPacket *avpkt)
708 if (avpkt->
size <
s->checksum_size)
712 frame->nb_samples =
s->frame_size;
719 memset(
s->nb_tones, 0,
sizeof(
s->nb_tones));
720 memset(
s->cur_tone, 0,
sizeof(
s->cur_tone));
725 return s->checksum_size;