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,
221 static int vlcs_initialized = 0;
222 static VLC_TYPE qdm2_table[3838][2];
224 if (!vlcs_initialized) {
232 vlc_tab_diff.
table = &qdm2_table[qdm2_vlc_offs[1]];
238 vlc_tab_run.
table = &qdm2_table[qdm2_vlc_offs[2]];
244 fft_level_exp_alt_vlc.
table = &qdm2_table[qdm2_vlc_offs[3]];
245 fft_level_exp_alt_vlc.
table_allocated = qdm2_vlc_offs[4] - qdm2_vlc_offs[3];
246 init_vlc (&fft_level_exp_alt_vlc, 8, 28,
251 fft_level_exp_vlc.
table = &qdm2_table[qdm2_vlc_offs[4]];
252 fft_level_exp_vlc.
table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
253 init_vlc (&fft_level_exp_vlc, 8, 20,
257 fft_stereo_exp_vlc.
table = &qdm2_table[qdm2_vlc_offs[5]];
258 fft_stereo_exp_vlc.
table_allocated = qdm2_vlc_offs[6] - qdm2_vlc_offs[5];
259 init_vlc (&fft_stereo_exp_vlc, 6, 7,
263 fft_stereo_phase_vlc.
table = &qdm2_table[qdm2_vlc_offs[6]];
264 fft_stereo_phase_vlc.
table_allocated = qdm2_vlc_offs[7] - qdm2_vlc_offs[6];
265 init_vlc (&fft_stereo_phase_vlc, 6, 9,
269 vlc_tab_tone_level_idx_hi1.
table = &qdm2_table[qdm2_vlc_offs[7]];
270 vlc_tab_tone_level_idx_hi1.
table_allocated = qdm2_vlc_offs[8] - qdm2_vlc_offs[7];
271 init_vlc (&vlc_tab_tone_level_idx_hi1, 8, 20,
275 vlc_tab_tone_level_idx_mid.
table = &qdm2_table[qdm2_vlc_offs[8]];
276 vlc_tab_tone_level_idx_mid.
table_allocated = qdm2_vlc_offs[9] - qdm2_vlc_offs[8];
277 init_vlc (&vlc_tab_tone_level_idx_mid, 8, 24,
281 vlc_tab_tone_level_idx_hi2.
table = &qdm2_table[qdm2_vlc_offs[9]];
282 vlc_tab_tone_level_idx_hi2.
table_allocated = qdm2_vlc_offs[10] - qdm2_vlc_offs[9];
283 init_vlc (&vlc_tab_tone_level_idx_hi2, 8, 24,
287 vlc_tab_type30.
table = &qdm2_table[qdm2_vlc_offs[10]];
288 vlc_tab_type30.
table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
293 vlc_tab_type34.
table = &qdm2_table[qdm2_vlc_offs[11]];
294 vlc_tab_type34.
table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
299 vlc_tab_fft_tone_offset[0].
table = &qdm2_table[qdm2_vlc_offs[12]];
300 vlc_tab_fft_tone_offset[0].
table_allocated = qdm2_vlc_offs[13] - qdm2_vlc_offs[12];
301 init_vlc (&vlc_tab_fft_tone_offset[0], 8, 23,
305 vlc_tab_fft_tone_offset[1].
table = &qdm2_table[qdm2_vlc_offs[13]];
306 vlc_tab_fft_tone_offset[1].
table_allocated = qdm2_vlc_offs[14] - qdm2_vlc_offs[13];
307 init_vlc (&vlc_tab_fft_tone_offset[1], 8, 28,
311 vlc_tab_fft_tone_offset[2].
table = &qdm2_table[qdm2_vlc_offs[14]];
312 vlc_tab_fft_tone_offset[2].
table_allocated = qdm2_vlc_offs[15] - qdm2_vlc_offs[14];
313 init_vlc (&vlc_tab_fft_tone_offset[2], 8, 32,
317 vlc_tab_fft_tone_offset[3].
table = &qdm2_table[qdm2_vlc_offs[15]];
318 vlc_tab_fft_tone_offset[3].
table_allocated = qdm2_vlc_offs[16] - qdm2_vlc_offs[15];
319 init_vlc (&vlc_tab_fft_tone_offset[3], 8, 35,
323 vlc_tab_fft_tone_offset[4].
table = &qdm2_table[qdm2_vlc_offs[16]];
324 vlc_tab_fft_tone_offset[4].
table_allocated = qdm2_vlc_offs[17] - qdm2_vlc_offs[16];
325 init_vlc (&vlc_tab_fft_tone_offset[4], 8, 38,
354 if ((value & ~3) > 0)
367 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
383 for (i=0; i < length; i++)
386 return (uint16_t)(value & 0xffff);
400 if (sub_packet->
type == 0) {
401 sub_packet->
size = 0;
406 if (sub_packet->
type & 0x80) {
407 sub_packet->
size <<= 8;
409 sub_packet->
type &= 0x7f;
412 if (sub_packet->
type == 0x7f)
449 int i, j, n, ch, sum;
454 for (i = 0; i < n; i++) {
457 for (j = 0; j < 8; j++)
464 for (j=0; j < 8; j++)
487 for (j = 0; j < 64; j++) {
507 static const int switchtable[23] = {0,5,1,5,5,5,5,5,2,5,5,5,5,5,5,5,3,5,5,5,5,5,4};
509 for (ch = 0; ch < channels; ch++) {
510 for (j = 0; j < 64; ) {
511 if((coding_method[ch][sb][j] - 8) > 22) {
515 switch (switchtable[coding_method[ch][sb][j]-8]) {
516 case 0: run = 10; case_val = 10;
break;
517 case 1: run = 1; case_val = 16;
break;
518 case 2: run = 5; case_val = 24;
break;
519 case 3: run = 3; case_val = 30;
break;
520 case 4: run = 1; case_val = 30;
break;
521 case 5: run = 1; case_val = 8;
break;
522 default: run = 1; case_val = 8;
break;
525 for (k = 0; k <
run; k++)
527 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j])
531 memset(&coding_method[ch][sb][j + k], case_val, k *
sizeof(int8_t));
532 memset(&coding_method[ch][sb][j + k], case_val, 3 *
sizeof(int8_t));
549 int i, sb, ch, sb_used;
553 for (sb = 0; sb < 30; sb++)
554 for (i = 0; i < 8; i++) {
568 for (sb = 0; sb < sb_used; sb++)
570 for (i = 0; i < 64; i++) {
579 for (sb = 0; sb < sb_used; sb++) {
580 if ((sb >= 4) && (sb <= 23)) {
582 for (i = 0; i < 64; i++) {
596 for (i = 0; i < 64; i++) {
608 for (i = 0; i < 64; i++) {
640 int c,
int superblocktype_2_3,
int cm_table_select)
644 int add1, add2, add3, add4;
647 if (!superblocktype_2_3) {
652 for (sb = 0; sb < 30; sb++) {
653 for (j = 1; j < 63; j++) {
654 add1 = tone_level_idx[ch][sb][j] - 10;
657 add2 = add3 = add4 = 0;
673 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
676 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
678 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
682 for (sb = 0; sb < 30; sb++)
683 for (j = 0; j < 64; j++)
684 acc += tone_level_idx_temp[ch][sb][j];
686 multres = 0x66666667LL * (acc * 10);
687 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
689 for (sb = 0; sb < 30; sb++)
690 for (j = 0; j < 64; j++) {
691 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
722 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
724 for (sb = 0; sb < 30; sb++)
727 for (sb = 0; sb < 30; sb++)
728 for (j = 0; j < 64; j++)
730 if (coding_method[ch][sb][j] < 10)
731 coding_method[ch][sb][j] = 10;
734 if (coding_method[ch][sb][j] < 16)
735 coding_method[ch][sb][j] = 16;
737 if (coding_method[ch][sb][j] < 30)
738 coding_method[ch][sb][j] = 30;
743 for (sb = 0; sb < 30; sb++)
744 for (j = 0; j < 64; j++)
765 int sb, j, k, n, ch,
run, channels;
766 int joined_stereo, zero_encoding, chs;
768 float type34_div = 0;
769 float type34_predictor;
770 float samples[10], sign_bits[16];
774 for (sb=sb_min; sb < sb_max; sb++)
780 for (sb = sb_min; sb < sb_max; sb++) {
794 for (j = 0; j < 16; j++)
802 for (j = 0; j < 64; j++)
810 for (ch = 0; ch < channels; ch++) {
812 type34_predictor = 0.0;
815 for (j = 0; j < 128; ) {
820 for (k = 0; k < 5; k++) {
821 if ((j + 2 * k) >= 128)
832 for (k = 0; k < 5; k++)
835 for (k = 0; k < 5; k++)
838 for (k = 0; k < 10; k++)
850 f -=
noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
861 for (k = 0; k < 5; k++) {
873 for (k = 0; k < 5; k++)
877 for (k = 0; k < 5; k++)
891 for (k = 0; k < 3; k++)
894 for (k = 0; k < 3; k++)
917 type34_div = (float)(1 <<
get_bits(gb, 2));
918 samples[0] = ((float)
get_bits(gb, 5) - 16.0) / 15.0;
919 type34_predictor = samples[0];
928 type34_predictor = samples[0];
944 for (k = 0; k <
run; k++) {
945 tmp[k][0] = samples[k];
947 tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
950 for (k = 0; k <
run; k++)
954 for (k = 0; k <
run; k++)
983 quantized_coeffs[0] =
level;
985 for (i = 0; i < 7; ) {
997 for (k = 1; k <=
run; k++)
998 quantized_coeffs[i + k] = (level + ((k * diff) / run));
1017 int sb, j, k, n, ch;
1030 for (sb = 0; sb < n; sb++)
1032 for (j = 0; j < 8; j++) {
1036 for (k=0; k < 8; k++) {
1042 for (k=0; k < 8; k++)
1049 for (sb = 0; sb < n; sb++)
1057 for (j = 0; j < 8; j++)
1063 for (sb = 0; sb < n; sb++)
1065 for (j = 0; j < 8; j++) {
1087 for (i = 1; i < n; i++)
1092 for (j = 0; j < (8 - 1); ) {
1099 for (k = 1; k <=
run; k++)
1108 for (i = 0; i < 8; i++)
1193 if (nodes[0] !=
NULL)
1197 if (nodes[1] !=
NULL)
1203 if (nodes[0] !=
NULL && nodes[1] !=
NULL && nodes[2] !=
NULL)
1209 if (nodes[0] !=
NULL && nodes[1] !=
NULL && nodes[3] !=
NULL)
1225 int i, packet_bytes, sub_packet_size, sub_packets_D;
1226 unsigned int next_index = 0;
1240 if (header.
type < 2 || header.
type >= 8) {
1251 if (header.
type == 2 || header.
type == 4 || header.
type == 5) {
1267 for (i = 0; i < 6; i++)
1271 for (i = 0; packet_bytes > 0; i++) {
1288 if (next_index >= header.
size)
1296 sub_packet_size = ((packet->
size > 0xff) ? 1 : 0) + packet->
size + 2;
1298 if (packet->
type == 0)
1301 if (sub_packet_size > packet_bytes) {
1302 if (packet->
type != 10 && packet->
type != 11 && packet->
type != 12)
1304 packet->
size += packet_bytes - sub_packet_size;
1307 packet_bytes -= sub_packet_size;
1313 if (packet->
type == 8) {
1316 }
else if (packet->
type >= 9 && packet->
type <= 12) {
1319 }
else if (packet->
type == 13) {
1320 for (j = 0; j < 6; j++)
1322 }
else if (packet->
type == 14) {
1323 for (j = 0; j < 6; j++)
1325 }
else if (packet->
type == 15) {
1365 int channel, stereo, phase, exp;
1366 int local_int_4, local_int_8, stereo_phase, local_int_10;
1367 int local_int_14, stereo_exp, local_int_20, local_int_28;
1374 local_int_10 = 1 << (q->
group_order - duration - 1);
1379 while ((n =
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1381 if(local_int_4 < q->group_size)
1387 local_int_4 += local_int_10;
1388 local_int_28 += (1 << local_int_8);
1390 local_int_4 += 8*local_int_10;
1391 local_int_28 += (8 << local_int_8);
1396 offset +=
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1397 while (offset >= (local_int_10 - 1)) {
1398 offset += (1 - (local_int_10 - 1));
1399 local_int_4 += local_int_10;
1400 local_int_28 += (1 << local_int_8);
1407 local_int_14 = (offset >> local_int_8);
1419 exp =
qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1421 exp = (exp < 0) ? 0 : exp;
1428 stereo_exp = (exp -
qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1429 stereo_phase = (phase -
qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1430 if (stereo_phase < 0)
1435 int sub_packet = (local_int_20 + local_int_28);
1449 int i, j,
min, max,
value, type, unknown_flag;
1457 for (i=0; i < 5; i++)
1467 if (value > min && value < max) {
1490 type = packet->
type;
1492 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1495 if (duration >= 0 && duration < 4)
1497 }
else if (type == 31) {
1498 for (j=0; j < 4; j++)
1500 }
else if (type == 46) {
1501 for (j=0; j < 6; j++)
1503 for (j=0; j < 4; j++)
1509 for (i = 0, j = -1; i < 5; i++)
1525 const double iscale = 2.0*
M_PI / 512.0;
1531 c.
im = level * sin(tone->
phase*iscale);
1532 c.
re = level * cos(tone->
phase*iscale);
1541 f[1] = -tone->
table[4];
1543 f[2] = 1.0 - tone->
table[2] - tone->
table[3];
1544 f[3] = tone->
table[1] + tone->
table[4] - 1.0;
1546 f[5] = tone->
table[2];
1547 for (i = 0; i < 2; i++) {
1551 for (i = 0; i < 4; i++) {
1568 const double iscale = 0.25 *
M_PI;
1570 for (ch = 0; ch < q->
channels; ch++) {
1602 for (i = 0; i < 4; i++)
1615 if (offset < q->frequency_range) {
1619 tone.
cutoff = (offset >= 60) ? 3 : 2;
1660 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1665 for (ch = 0; ch < q->
channels; ch++)
1666 for (i = 0; i < 8; i++)
1667 for (k=sb_used; k <
SBLIMIT; k++)
1671 float *samples_ptr = q->
samples + ch;
1673 for (i = 0; i < 8; i++) {
1686 for (ch = 0; ch < q->
channels; ch++)
1698 static int initialized = 0;
1700 if (initialized != 0)
1722 int tmp_val, tmp,
size;
1766 while (extradata_size > 7) {
1767 if (!memcmp(extradata,
"frmaQDM", 7))
1773 if (extradata_size < 12) {
1779 if (memcmp(extradata,
"frmaQDM", 7)) {
1784 if (extradata[7] ==
'C') {
1791 extradata_size -= 8;
1795 if(size > extradata_size){
1797 extradata_size, size);
1850 case 0: tmp = 40;
break;
1851 case 1: tmp = 48;
break;
1852 case 2: tmp = 56;
break;
1853 case 3: tmp = 72;
break;
1854 case 4: tmp = 80;
break;
1855 case 5: tmp = 100;
break;
1859 if ((tmp * 1000) < avctx->
bit_rate) tmp_val = 1;
1860 if ((tmp * 1440) < avctx->
bit_rate) tmp_val = 2;
1861 if ((tmp * 1760) < avctx->
bit_rate) tmp_val = 3;
1862 if ((tmp * 2240) < avctx->
bit_rate) tmp_val = 4;
1876 else if (tmp <= 16000)
1922 memset(&q->
output_buffer[frame_size], 0, frame_size *
sizeof(
float));
1940 for (ch = 0; ch < q->
channels; ch++) {
1972 int *got_frame_ptr,
AVPacket *avpkt)
1976 int buf_size = avpkt->
size;
1983 if(buf_size < s->checksum_size)
1992 out = (int16_t *)frame->
data[0];
1994 for (i = 0; i < 16; i++) {