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 && list->
packet) {
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)
546 if ((coding_method[ch][sb][j] - 8) > 22) {
550 switch (
switchtable[coding_method[ch][sb][j] - 8]) {
574 for (k = 0; k <
run; k++) {
576 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) {
580 memset(&coding_method[ch][sb][j + k], case_val,
582 memset(&coding_method[ch][sb][j + k], case_val,
603 int i, sb, ch, sb_used;
607 for (sb = 0; sb < 30; sb++)
608 for (i = 0; i < 8; i++) {
622 for (sb = 0; sb < sb_used; sb++)
624 for (i = 0; i < 64; i++) {
633 for (sb = 0; sb < sb_used; sb++) {
634 if ((sb >= 4) && (sb <= 23)) {
636 for (i = 0; i < 64; i++) {
650 for (i = 0; i < 64; i++) {
662 for (i = 0; i < 64; i++) {
694 int c,
int superblocktype_2_3,
699 int add1, add2, add3, add4;
702 if (!superblocktype_2_3) {
707 for (sb = 0; sb < 30; sb++) {
708 for (j = 1; j < 63; j++) {
709 add1 = tone_level_idx[ch][sb][j] - 10;
712 add2 = add3 = add4 = 0;
728 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
731 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
733 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
737 for (sb = 0; sb < 30; sb++)
738 for (j = 0; j < 64; j++)
739 acc += tone_level_idx_temp[ch][sb][j];
741 multres = 0x66666667LL * (acc * 10);
742 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
744 for (sb = 0; sb < 30; sb++)
745 for (j = 0; j < 64; j++) {
746 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
777 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
779 for (sb = 0; sb < 30; sb++)
782 for (sb = 0; sb < 30; sb++)
783 for (j = 0; j < 64; j++)
785 if (coding_method[ch][sb][j] < 10)
786 coding_method[ch][sb][j] = 10;
789 if (coding_method[ch][sb][j] < 16)
790 coding_method[ch][sb][j] = 16;
792 if (coding_method[ch][sb][j] < 30)
793 coding_method[ch][sb][j] = 30;
798 for (sb = 0; sb < 30; sb++)
799 for (j = 0; j < 64; j++)
818 int length,
int sb_min,
int sb_max)
820 int sb, j, k,
n, ch,
run, channels;
821 int joined_stereo, zero_encoding;
823 float type34_div = 0;
824 float type34_predictor;
826 int sign_bits[16] = {0};
830 for (sb=sb_min; sb < sb_max; sb++)
836 for (sb = sb_min; sb < sb_max; sb++) {
848 for (j = 0; j < 16; j++)
851 for (j = 0; j < 64; j++)
864 for (ch = 0; ch < channels; ch++) {
867 type34_predictor = 0.0;
870 for (j = 0; j < 128; ) {
875 for (k = 0; k < 5; k++) {
876 if ((j + 2 * k) >= 128)
887 for (k = 0; k < 5; k++)
890 for (k = 0; k < 5; k++)
893 for (k = 0; k < 10; k++)
905 f -=
noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
916 for (k = 0; k < 5; k++) {
928 for (k = 0; k < 5; k++)
932 for (k = 0; k < 5; k++)
946 for (k = 0; k < 3; k++)
949 for (k = 0; k < 3; k++)
972 type34_div = (float)(1 <<
get_bits(gb, 2));
973 samples[0] = ((float)
get_bits(gb, 5) - 16.0) / 15.0;
974 type34_predictor = samples[0];
983 type34_predictor = samples[0];
998 for (k = 0; k < run && j + k < 128; k++) {
1000 q->
tone_level[0][sb][(j + k) / 2] * samples[k];
1002 if (sign_bits[(j + k) / 8])
1004 q->
tone_level[1][sb][(j + k) / 2] * -samples[k];
1007 q->
tone_level[1][sb][(j + k) / 2] * samples[k];
1011 for (k = 0; k <
run; k++)
1042 quantized_coeffs[0] =
level;
1044 for (i = 0; i < 7; ) {
1056 for (k = 1; k <=
run; k++)
1057 quantized_coeffs[i + k] = (level + ((k * diff) / run));
1076 int sb, j, k,
n, ch;
1089 for (sb = 0; sb <
n; sb++)
1091 for (j = 0; j < 8; j++) {
1095 for (k=0; k < 8; k++) {
1101 for (k=0; k < 8; k++)
1108 for (sb = 0; sb <
n; sb++)
1116 for (j = 0; j < 8; j++)
1122 for (sb = 0; sb <
n; sb++)
1124 for (j = 0; j < 8; j++) {
1146 for (i = 1; i <
n; i++)
1151 for (j = 0; j < (8 - 1); ) {
1158 for (k = 1; k <=
run; k++)
1167 for (i = 0; i < 8; i++)
1261 if (nodes[0] && nodes[1] && nodes[2])
1267 if (nodes[0] && nodes[1] && nodes[3])
1282 int i, packet_bytes, sub_packet_size, sub_packets_D;
1283 unsigned int next_index = 0;
1297 if (header.
type < 2 || header.
type >= 8) {
1308 if (header.
type == 2 || header.
type == 4 || header.
type == 5) {
1324 for (i = 0; i < 6; i++)
1328 for (i = 0; packet_bytes > 0; i++) {
1345 if (next_index >= header.
size)
1353 sub_packet_size = ((packet->
size > 0xff) ? 1 : 0) + packet->
size + 2;
1355 if (packet->
type == 0)
1358 if (sub_packet_size > packet_bytes) {
1359 if (packet->
type != 10 && packet->
type != 11 && packet->
type != 12)
1361 packet->
size += packet_bytes - sub_packet_size;
1364 packet_bytes -= sub_packet_size;
1370 if (packet->
type == 8) {
1373 }
else if (packet->
type >= 9 && packet->
type <= 12) {
1376 }
else if (packet->
type == 13) {
1377 for (j = 0; j < 6; j++)
1379 }
else if (packet->
type == 14) {
1380 for (j = 0; j < 6; j++)
1382 }
else if (packet->
type == 15) {
1385 }
else if (packet->
type >= 16 && packet->
type < 48 &&
1410 ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1421 int channel, stereo, phase, exp;
1422 int local_int_4, local_int_8, stereo_phase, local_int_10;
1423 int local_int_14, stereo_exp, local_int_20, local_int_28;
1430 local_int_10 = 1 << (q->
group_order - duration - 1);
1435 while ((n =
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1437 if(local_int_4 < q->group_size)
1443 local_int_4 += local_int_10;
1444 local_int_28 += (1 << local_int_8);
1446 local_int_4 += 8 * local_int_10;
1447 local_int_28 += (8 << local_int_8);
1452 offset +=
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1453 while (offset >= (local_int_10 - 1)) {
1454 offset += (1 - (local_int_10 - 1));
1455 local_int_4 += local_int_10;
1456 local_int_28 += (1 << local_int_8);
1463 local_int_14 = (offset >> local_int_8);
1475 exp =
qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1477 exp = (exp < 0) ? 0 : exp;
1484 stereo_exp = (exp -
qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1485 stereo_phase = (phase -
qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1486 if (stereo_phase < 0)
1491 int sub_packet = (local_int_20 + local_int_28);
1494 channel, exp, phase);
1498 stereo_exp, stereo_phase);
1514 for (i = 0; i < 5; i++)
1524 if (value > min && value < max) {
1537 (packet->
type < 16 || packet->
type >= 48 ||
1549 type = packet->
type;
1551 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1554 if (duration >= 0 && duration < 4)
1556 }
else if (type == 31) {
1557 for (j = 0; j < 4; j++)
1559 }
else if (type == 46) {
1560 for (j = 0; j < 6; j++)
1562 for (j = 0; j < 4; j++)
1568 for (i = 0, j = -1; i < 5; i++)
1583 const double iscale = 2.0 *
M_PI / 512.0;
1589 c.
im = level * sin(tone->
phase * iscale);
1590 c.
re = level * cos(tone->
phase * iscale);
1599 f[1] = -tone->
table[4];
1601 f[2] = 1.0 - tone->
table[2] - tone->
table[3];
1602 f[3] = tone->
table[1] + tone->
table[4] - 1.0;
1604 f[5] = tone->
table[2];
1605 for (i = 0; i < 2; i++) {
1609 c.
im * ((tone->
cutoff <= i) ? -f[i] : f[i]);
1611 for (i = 0; i < 4; i++) {
1627 const double iscale = 0.25 *
M_PI;
1629 for (ch = 0; ch < q->
channels; ch++) {
1661 for (i = 0; i < 4; i++)
1674 if (offset < q->frequency_range) {
1678 tone.
cutoff = (offset >= 60) ? 3 : 2;
1717 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1722 for (ch = 0; ch < q->
channels; ch++)
1723 for (i = 0; i < 8; i++)
1724 for (k = sb_used; k <
SBLIMIT; k++)
1728 float *samples_ptr = q->
samples + ch;
1730 for (i = 0; i < 8; i++) {
1743 for (ch = 0; ch < q->
channels; ch++)
1776 int tmp_val, tmp,
size;
1822 while (extradata_size > 7) {
1823 if (!memcmp(extradata,
"frmaQDM", 7))
1829 if (extradata_size < 12) {
1835 if (memcmp(extradata,
"frmaQDM", 7)) {
1840 if (extradata[7] ==
'C') {
1847 extradata_size -= 8;
1851 if(size > extradata_size){
1853 extradata_size, size);
1906 case 0: tmp = 40;
break;
1907 case 1: tmp = 48;
break;
1908 case 2: tmp = 56;
break;
1909 case 3: tmp = 72;
break;
1910 case 4: tmp = 80;
break;
1911 case 5: tmp = 100;
break;
1915 if ((tmp * 1000) < avctx->
bit_rate) tmp_val = 1;
1916 if ((tmp * 1440) < avctx->
bit_rate) tmp_val = 2;
1917 if ((tmp * 1760) < avctx->
bit_rate) tmp_val = 3;
1918 if ((tmp * 2240) < avctx->
bit_rate) tmp_val = 4;
1969 memset(&q->
output_buffer[frame_size], 0, frame_size *
sizeof(
float));
1987 for (ch = 0; ch < q->
channels; ch++) {
2018 int *got_frame_ptr,
AVPacket *avpkt)
2022 int buf_size = avpkt->
size;
2029 if(buf_size < s->checksum_size)
2036 out = (int16_t *)frame->
data[0];
2038 for (i = 0; i < 16; i++) {