38 #define BITSTREAM_READER_LE
54 #define QDM2_LIST_ADD(list, size, packet) \
57 list[size - 1].next = &list[size]; \
59 list[size].packet = packet; \
60 list[size].next = NULL; \
65 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
67 #define FIX_NOISE_IDX(noise_idx) \
68 if ((noise_idx) >= 3840) \
69 (noise_idx) -= 3840; \
71 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
73 #define SAMPLES_NEEDED \
74 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
76 #define SAMPLES_NEEDED_2(why) \
77 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
79 #define QDM2_MAX_FRAME_SIZE 512
162 int fft_coefs_min_index[5];
163 int fft_coefs_max_index[5];
164 int fft_level_exp[6];
216 0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
220 0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
225 static VLC_TYPE qdm2_table[3838][2];
234 vlc_tab_diff.
table = &qdm2_table[qdm2_vlc_offs[1]];
241 vlc_tab_run.
table = &qdm2_table[qdm2_vlc_offs[2]];
248 fft_level_exp_alt_vlc.
table = &qdm2_table[qdm2_vlc_offs[3]];
251 init_vlc(&fft_level_exp_alt_vlc, 8, 28,
256 fft_level_exp_vlc.
table = &qdm2_table[qdm2_vlc_offs[4]];
257 fft_level_exp_vlc.
table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
263 fft_stereo_exp_vlc.
table = &qdm2_table[qdm2_vlc_offs[5]];
271 fft_stereo_phase_vlc.
table = &qdm2_table[qdm2_vlc_offs[6]];
274 init_vlc(&fft_stereo_phase_vlc, 6, 9,
279 vlc_tab_tone_level_idx_hi1.
table =
280 &qdm2_table[qdm2_vlc_offs[7]];
283 init_vlc(&vlc_tab_tone_level_idx_hi1, 8, 20,
288 vlc_tab_tone_level_idx_mid.
table =
289 &qdm2_table[qdm2_vlc_offs[8]];
292 init_vlc(&vlc_tab_tone_level_idx_mid, 8, 24,
297 vlc_tab_tone_level_idx_hi2.
table =
298 &qdm2_table[qdm2_vlc_offs[9]];
301 init_vlc(&vlc_tab_tone_level_idx_hi2, 8, 24,
306 vlc_tab_type30.
table = &qdm2_table[qdm2_vlc_offs[10]];
307 vlc_tab_type30.
table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
313 vlc_tab_type34.
table = &qdm2_table[qdm2_vlc_offs[11]];
314 vlc_tab_type34.
table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
320 vlc_tab_fft_tone_offset[0].
table =
321 &qdm2_table[qdm2_vlc_offs[12]];
324 init_vlc(&vlc_tab_fft_tone_offset[0], 8, 23,
329 vlc_tab_fft_tone_offset[1].
table =
330 &qdm2_table[qdm2_vlc_offs[13]];
333 init_vlc(&vlc_tab_fft_tone_offset[1], 8, 28,
338 vlc_tab_fft_tone_offset[2].
table =
339 &qdm2_table[qdm2_vlc_offs[14]];
342 init_vlc(&vlc_tab_fft_tone_offset[2], 8, 32,
347 vlc_tab_fft_tone_offset[3].
table =
348 &qdm2_table[qdm2_vlc_offs[15]];
351 init_vlc(&vlc_tab_fft_tone_offset[3], 8, 35,
356 vlc_tab_fft_tone_offset[4].
table =
357 &qdm2_table[qdm2_vlc_offs[16]];
360 init_vlc(&vlc_tab_fft_tone_offset[4], 8, 38,
387 if ((value & ~3) > 0)
399 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
415 for (i = 0; i <
length; i++)
418 return (uint16_t)(value & 0xffff);
432 if (sub_packet->
type == 0) {
433 sub_packet->
size = 0;
434 sub_packet->
data = NULL;
438 if (sub_packet->
type & 0x80) {
439 sub_packet->
size <<= 8;
441 sub_packet->
type &= 0x7f;
444 if (sub_packet->
type == 0x7f)
465 while (list != NULL && list->
packet != NULL) {
481 int i, j,
n, ch, sum;
486 for (i = 0; i <
n; i++) {
489 for (j = 0; j < 8; j++)
496 for (j = 0; j < 8; j++)
518 for (j = 0; j < 64; j++) {
542 for (ch = 0; ch < channels; ch++) {
543 for (j = 0; j < 64; ) {
544 if ((coding_method[ch][sb][j] - 8) > 22) {
548 switch (
switchtable[coding_method[ch][sb][j] - 8]) {
572 for (k = 0; k <
run; k++) {
574 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) {
578 memset(&coding_method[ch][sb][j + k], case_val,
580 memset(&coding_method[ch][sb][j + k], case_val,
600 int i, sb, ch, sb_used;
604 for (sb = 0; sb < 30; sb++)
605 for (i = 0; i < 8; i++) {
619 for (sb = 0; sb < sb_used; sb++)
621 for (i = 0; i < 64; i++) {
630 for (sb = 0; sb < sb_used; sb++) {
631 if ((sb >= 4) && (sb <= 23)) {
633 for (i = 0; i < 64; i++) {
647 for (i = 0; i < 64; i++) {
659 for (i = 0; i < 64; i++) {
691 int c,
int superblocktype_2_3,
696 int add1, add2, add3, add4;
699 if (!superblocktype_2_3) {
704 for (sb = 0; sb < 30; sb++) {
705 for (j = 1; j < 63; j++) {
706 add1 = tone_level_idx[ch][sb][j] - 10;
709 add2 = add3 = add4 = 0;
725 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
728 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
730 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
734 for (sb = 0; sb < 30; sb++)
735 for (j = 0; j < 64; j++)
736 acc += tone_level_idx_temp[ch][sb][j];
738 multres = 0x66666667LL * (acc * 10);
739 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
741 for (sb = 0; sb < 30; sb++)
742 for (j = 0; j < 64; j++) {
743 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
774 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
776 for (sb = 0; sb < 30; sb++)
779 for (sb = 0; sb < 30; sb++)
780 for (j = 0; j < 64; j++)
782 if (coding_method[ch][sb][j] < 10)
783 coding_method[ch][sb][j] = 10;
786 if (coding_method[ch][sb][j] < 16)
787 coding_method[ch][sb][j] = 16;
789 if (coding_method[ch][sb][j] < 30)
790 coding_method[ch][sb][j] = 30;
795 for (sb = 0; sb < 30; sb++)
796 for (j = 0; j < 64; j++)
815 int length,
int sb_min,
int sb_max)
817 int sb, j, k,
n, ch,
run, channels;
818 int joined_stereo, zero_encoding, chs;
820 float type34_div = 0;
821 float type34_predictor;
822 float samples[10], sign_bits[16];
826 for (sb=sb_min; sb < sb_max; sb++)
832 for (sb = sb_min; sb < sb_max; sb++) {
844 for (j = 0; j < 16; j++)
852 for (j = 0; j < 64; j++)
860 for (ch = 0; ch < channels; ch++) {
863 type34_predictor = 0.0;
866 for (j = 0; j < 128; ) {
871 for (k = 0; k < 5; k++) {
872 if ((j + 2 * k) >= 128)
883 for (k = 0; k < 5; k++)
886 for (k = 0; k < 5; k++)
889 for (k = 0; k < 10; k++)
901 f -=
noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
912 for (k = 0; k < 5; k++) {
924 for (k = 0; k < 5; k++)
928 for (k = 0; k < 5; k++)
942 for (k = 0; k < 3; k++)
945 for (k = 0; k < 3; k++)
968 type34_div = (float)(1 <<
get_bits(gb, 2));
969 samples[0] = ((float)
get_bits(gb, 5) - 16.0) / 15.0;
970 type34_predictor = samples[0];
979 type34_predictor = samples[0];
995 for (k = 0; k <
run; k++) {
996 tmp[k][0] = samples[k];
998 tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
1001 for (k = 0; k <
run; k++)
1005 for (k = 0; k <
run; k++)
1036 quantized_coeffs[0] =
level;
1038 for (i = 0; i < 7; ) {
1050 for (k = 1; k <=
run; k++)
1051 quantized_coeffs[i + k] = (level + ((k * diff) / run));
1070 int sb, j, k,
n, ch;
1083 for (sb = 0; sb <
n; sb++)
1085 for (j = 0; j < 8; j++) {
1089 for (k=0; k < 8; k++) {
1095 for (k=0; k < 8; k++)
1102 for (sb = 0; sb <
n; sb++)
1110 for (j = 0; j < 8; j++)
1116 for (sb = 0; sb <
n; sb++)
1118 for (j = 0; j < 8; j++) {
1140 for (i = 1; i <
n; i++)
1145 for (j = 0; j < (8 - 1); ) {
1152 for (k = 1; k <=
run; k++)
1161 for (i = 0; i < 8; i++)
1245 if (nodes[0] != NULL)
1249 if (nodes[1] != NULL)
1255 if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL)
1261 if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL)
1276 int i, packet_bytes, sub_packet_size, sub_packets_D;
1277 unsigned int next_index = 0;
1291 if (header.
type < 2 || header.
type >= 8) {
1302 if (header.
type == 2 || header.
type == 4 || header.
type == 5) {
1318 for (i = 0; i < 6; i++)
1322 for (i = 0; packet_bytes > 0; i++) {
1339 if (next_index >= header.
size)
1347 sub_packet_size = ((packet->
size > 0xff) ? 1 : 0) + packet->
size + 2;
1349 if (packet->
type == 0)
1352 if (sub_packet_size > packet_bytes) {
1353 if (packet->
type != 10 && packet->
type != 11 && packet->
type != 12)
1355 packet->
size += packet_bytes - sub_packet_size;
1358 packet_bytes -= sub_packet_size;
1364 if (packet->
type == 8) {
1367 }
else if (packet->
type >= 9 && packet->
type <= 12) {
1370 }
else if (packet->
type == 13) {
1371 for (j = 0; j < 6; j++)
1373 }
else if (packet->
type == 14) {
1374 for (j = 0; j < 6; j++)
1376 }
else if (packet->
type == 15) {
1379 }
else if (packet->
type >= 16 && packet->
type < 48 &&
1404 ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1415 int channel, stereo, phase, exp;
1416 int local_int_4, local_int_8, stereo_phase, local_int_10;
1417 int local_int_14, stereo_exp, local_int_20, local_int_28;
1424 local_int_10 = 1 << (q->
group_order - duration - 1);
1429 while ((n =
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1431 if(local_int_4 < q->group_size)
1437 local_int_4 += local_int_10;
1438 local_int_28 += (1 << local_int_8);
1440 local_int_4 += 8 * local_int_10;
1441 local_int_28 += (8 << local_int_8);
1446 offset +=
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1447 while (offset >= (local_int_10 - 1)) {
1448 offset += (1 - (local_int_10 - 1));
1449 local_int_4 += local_int_10;
1450 local_int_28 += (1 << local_int_8);
1457 local_int_14 = (offset >> local_int_8);
1469 exp =
qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1471 exp = (exp < 0) ? 0 : exp;
1478 stereo_exp = (exp -
qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1479 stereo_phase = (phase -
qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1480 if (stereo_phase < 0)
1485 int sub_packet = (local_int_20 + local_int_28);
1488 channel, exp, phase);
1492 stereo_exp, stereo_phase);
1508 for (i = 0; i < 5; i++)
1518 if (value > min && value < max) {
1531 (packet->
type < 16 || packet->
type >= 48 ||
1543 type = packet->
type;
1545 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1548 if (duration >= 0 && duration < 4)
1550 }
else if (type == 31) {
1551 for (j = 0; j < 4; j++)
1553 }
else if (type == 46) {
1554 for (j = 0; j < 6; j++)
1556 for (j = 0; j < 4; j++)
1562 for (i = 0, j = -1; i < 5; i++)
1577 const double iscale = 2.0 *
M_PI / 512.0;
1583 c.
im = level * sin(tone->
phase * iscale);
1584 c.
re = level * cos(tone->
phase * iscale);
1593 f[1] = -tone->
table[4];
1595 f[2] = 1.0 - tone->
table[2] - tone->
table[3];
1596 f[3] = tone->
table[1] + tone->
table[4] - 1.0;
1598 f[5] = tone->
table[2];
1599 for (i = 0; i < 2; i++) {
1603 c.
im * ((tone->
cutoff <= i) ? -f[i] : f[i]);
1605 for (i = 0; i < 4; i++) {
1621 const double iscale = 0.25 *
M_PI;
1623 for (ch = 0; ch < q->
channels; ch++) {
1655 for (i = 0; i < 4; i++)
1668 if (offset < q->frequency_range) {
1672 tone.
cutoff = (offset >= 60) ? 3 : 2;
1711 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1716 for (ch = 0; ch < q->
channels; ch++)
1717 for (i = 0; i < 8; i++)
1718 for (k = sb_used; k <
SBLIMIT; k++)
1722 float *samples_ptr = q->
samples + ch;
1724 for (i = 0; i < 8; i++) {
1737 for (ch = 0; ch < q->
channels; ch++)
1763 int tmp_val, tmp,
size;
1807 while (extradata_size > 7) {
1808 if (!memcmp(extradata,
"frmaQDM", 7))
1814 if (extradata_size < 12) {
1820 if (memcmp(extradata,
"frmaQDM", 7)) {
1825 if (extradata[7] ==
'C') {
1832 extradata_size -= 8;
1836 if(size > extradata_size){
1838 extradata_size, size);
1891 case 0: tmp = 40;
break;
1892 case 1: tmp = 48;
break;
1893 case 2: tmp = 56;
break;
1894 case 3: tmp = 72;
break;
1895 case 4: tmp = 80;
break;
1896 case 5: tmp = 100;
break;
1900 if ((tmp * 1000) < avctx->
bit_rate) tmp_val = 1;
1901 if ((tmp * 1440) < avctx->
bit_rate) tmp_val = 2;
1902 if ((tmp * 1760) < avctx->
bit_rate) tmp_val = 3;
1903 if ((tmp * 2240) < avctx->
bit_rate) tmp_val = 4;
1954 memset(&q->
output_buffer[frame_size], 0, frame_size *
sizeof(
float));
1972 for (ch = 0; ch < q->
channels; ch++) {
2003 int *got_frame_ptr,
AVPacket *avpkt)
2007 int buf_size = avpkt->
size;
2014 if(buf_size < s->checksum_size)
2021 out = (int16_t *)frame->
data[0];
2023 for (i = 0; i < 16; i++) {