38 #define BITSTREAM_READER_LE
55 #define QDM2_LIST_ADD(list, size, packet) \
58 list[size - 1].next = &list[size]; \
60 list[size].packet = packet; \
61 list[size].next = NULL; \
66 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
68 #define FIX_NOISE_IDX(noise_idx) \
69 if ((noise_idx) >= 3840) \
70 (noise_idx) -= 3840; \
72 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
74 #define SAMPLES_NEEDED \
75 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
77 #define SAMPLES_NEEDED_2(why) \
78 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
80 #define QDM2_MAX_FRAME_SIZE 512
165 int fft_coefs_min_index[5];
166 int fft_coefs_max_index[5];
167 int fft_level_exp[6];
219 0,260,566,598,894,1166,1230,1294,1678,1950,2214,2278,2310,2570,2834,3124,3448,3838,
224 static int vlcs_initialized = 0;
225 static VLC_TYPE qdm2_table[3838][2];
227 if (!vlcs_initialized) {
235 vlc_tab_diff.
table = &qdm2_table[qdm2_vlc_offs[1]];
241 vlc_tab_run.
table = &qdm2_table[qdm2_vlc_offs[2]];
247 fft_level_exp_alt_vlc.
table = &qdm2_table[qdm2_vlc_offs[3]];
248 fft_level_exp_alt_vlc.
table_allocated = qdm2_vlc_offs[4] - qdm2_vlc_offs[3];
249 init_vlc (&fft_level_exp_alt_vlc, 8, 28,
254 fft_level_exp_vlc.
table = &qdm2_table[qdm2_vlc_offs[4]];
255 fft_level_exp_vlc.
table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4];
256 init_vlc (&fft_level_exp_vlc, 8, 20,
260 fft_stereo_exp_vlc.
table = &qdm2_table[qdm2_vlc_offs[5]];
261 fft_stereo_exp_vlc.
table_allocated = qdm2_vlc_offs[6] - qdm2_vlc_offs[5];
262 init_vlc (&fft_stereo_exp_vlc, 6, 7,
266 fft_stereo_phase_vlc.
table = &qdm2_table[qdm2_vlc_offs[6]];
267 fft_stereo_phase_vlc.
table_allocated = qdm2_vlc_offs[7] - qdm2_vlc_offs[6];
268 init_vlc (&fft_stereo_phase_vlc, 6, 9,
272 vlc_tab_tone_level_idx_hi1.
table = &qdm2_table[qdm2_vlc_offs[7]];
273 vlc_tab_tone_level_idx_hi1.
table_allocated = qdm2_vlc_offs[8] - qdm2_vlc_offs[7];
274 init_vlc (&vlc_tab_tone_level_idx_hi1, 8, 20,
278 vlc_tab_tone_level_idx_mid.
table = &qdm2_table[qdm2_vlc_offs[8]];
279 vlc_tab_tone_level_idx_mid.
table_allocated = qdm2_vlc_offs[9] - qdm2_vlc_offs[8];
280 init_vlc (&vlc_tab_tone_level_idx_mid, 8, 24,
284 vlc_tab_tone_level_idx_hi2.
table = &qdm2_table[qdm2_vlc_offs[9]];
285 vlc_tab_tone_level_idx_hi2.
table_allocated = qdm2_vlc_offs[10] - qdm2_vlc_offs[9];
286 init_vlc (&vlc_tab_tone_level_idx_hi2, 8, 24,
290 vlc_tab_type30.
table = &qdm2_table[qdm2_vlc_offs[10]];
291 vlc_tab_type30.
table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10];
296 vlc_tab_type34.
table = &qdm2_table[qdm2_vlc_offs[11]];
297 vlc_tab_type34.
table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11];
302 vlc_tab_fft_tone_offset[0].
table = &qdm2_table[qdm2_vlc_offs[12]];
303 vlc_tab_fft_tone_offset[0].
table_allocated = qdm2_vlc_offs[13] - qdm2_vlc_offs[12];
304 init_vlc (&vlc_tab_fft_tone_offset[0], 8, 23,
308 vlc_tab_fft_tone_offset[1].
table = &qdm2_table[qdm2_vlc_offs[13]];
309 vlc_tab_fft_tone_offset[1].
table_allocated = qdm2_vlc_offs[14] - qdm2_vlc_offs[13];
310 init_vlc (&vlc_tab_fft_tone_offset[1], 8, 28,
314 vlc_tab_fft_tone_offset[2].
table = &qdm2_table[qdm2_vlc_offs[14]];
315 vlc_tab_fft_tone_offset[2].
table_allocated = qdm2_vlc_offs[15] - qdm2_vlc_offs[14];
316 init_vlc (&vlc_tab_fft_tone_offset[2], 8, 32,
320 vlc_tab_fft_tone_offset[3].
table = &qdm2_table[qdm2_vlc_offs[15]];
321 vlc_tab_fft_tone_offset[3].
table_allocated = qdm2_vlc_offs[16] - qdm2_vlc_offs[15];
322 init_vlc (&vlc_tab_fft_tone_offset[3], 8, 35,
326 vlc_tab_fft_tone_offset[4].
table = &qdm2_table[qdm2_vlc_offs[16]];
327 vlc_tab_fft_tone_offset[4].
table_allocated = qdm2_vlc_offs[17] - qdm2_vlc_offs[16];
328 init_vlc (&vlc_tab_fft_tone_offset[4], 8, 38,
357 if ((value & ~3) > 0)
370 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
386 for (i=0; i < length; i++)
389 return (uint16_t)(value & 0xffff);
403 if (sub_packet->
type == 0) {
404 sub_packet->
size = 0;
409 if (sub_packet->
type & 0x80) {
410 sub_packet->
size <<= 8;
412 sub_packet->
type &= 0x7f;
415 if (sub_packet->
type == 0x7f)
452 int i, j, n, ch, sum;
457 for (i = 0; i < n; i++) {
460 for (j = 0; j < 8; j++)
467 for (j=0; j < 8; j++)
490 for (j = 0; j < 64; j++) {
510 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};
512 for (ch = 0; ch < channels; ch++) {
513 for (j = 0; j < 64; ) {
514 if((coding_method[ch][sb][j] - 8) > 22) {
518 switch (switchtable[coding_method[ch][sb][j]-8]) {
519 case 0: run = 10; case_val = 10;
break;
520 case 1: run = 1; case_val = 16;
break;
521 case 2: run = 5; case_val = 24;
break;
522 case 3: run = 3; case_val = 30;
break;
523 case 4: run = 1; case_val = 30;
break;
524 case 5: run = 1; case_val = 8;
break;
525 default: run = 1; case_val = 8;
break;
528 for (k = 0; k <
run; k++)
530 if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j])
534 memset(&coding_method[ch][sb][j + k], case_val, k *
sizeof(int8_t));
535 memset(&coding_method[ch][sb][j + k], case_val, 3 *
sizeof(int8_t));
552 int i, sb, ch, sb_used;
556 for (sb = 0; sb < 30; sb++)
557 for (i = 0; i < 8; i++) {
571 for (sb = 0; sb < sb_used; sb++)
573 for (i = 0; i < 64; i++) {
582 for (sb = 0; sb < sb_used; sb++) {
583 if ((sb >= 4) && (sb <= 23)) {
585 for (i = 0; i < 64; i++) {
599 for (i = 0; i < 64; i++) {
611 for (i = 0; i < 64; i++) {
643 int c,
int superblocktype_2_3,
int cm_table_select)
647 int add1, add2, add3, add4;
650 if (!superblocktype_2_3) {
654 for (sb = 0; sb < 30; sb++) {
655 for (j = 1; j < 63; j++) {
656 add1 = tone_level_idx[ch][sb][j] - 10;
659 add2 = add3 = add4 = 0;
675 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
678 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
680 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
684 for (sb = 0; sb < 30; sb++)
685 for (j = 0; j < 64; j++)
686 acc += tone_level_idx_temp[ch][sb][j];
688 multres = 0x66666667 * (acc * 10);
689 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
691 for (sb = 0; sb < 30; sb++)
692 for (j = 0; j < 64; j++) {
693 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
724 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
726 for (sb = 0; sb < 30; sb++)
729 for (sb = 0; sb < 30; sb++)
730 for (j = 0; j < 64; j++)
732 if (coding_method[ch][sb][j] < 10)
733 coding_method[ch][sb][j] = 10;
736 if (coding_method[ch][sb][j] < 16)
737 coding_method[ch][sb][j] = 16;
739 if (coding_method[ch][sb][j] < 30)
740 coding_method[ch][sb][j] = 30;
745 for (sb = 0; sb < 30; sb++)
746 for (j = 0; j < 64; j++)
767 int sb, j, k, n, ch,
run, channels;
768 int joined_stereo, zero_encoding, chs;
770 float type34_div = 0;
771 float type34_predictor;
772 float samples[10], sign_bits[16];
776 for (sb=sb_min; sb < sb_max; sb++)
782 for (sb = sb_min; sb < sb_max; sb++) {
796 for (j = 0; j < 16; j++)
804 for (j = 0; j < 64; j++)
812 for (ch = 0; ch < channels; ch++) {
814 type34_predictor = 0.0;
817 for (j = 0; j < 128; ) {
822 for (k = 0; k < 5; k++) {
823 if ((j + 2 * k) >= 128)
829 for (k = 0; k < 5; k++)
832 for (k = 0; k < 5; k++)
835 for (k = 0; k < 10; k++)
847 f -=
noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
858 for (k = 0; k < 5; k++) {
865 for (k = 0; k < 5; k++)
869 for (k = 0; k < 5; k++)
878 for (k = 0; k < 3; k++)
881 for (k = 0; k < 3; k++)
904 type34_div = (float)(1 <<
get_bits(gb, 2));
905 samples[0] = ((float)
get_bits(gb, 5) - 16.0) / 15.0;
906 type34_predictor = samples[0];
915 type34_predictor = samples[0];
932 for (k = 0; k <
run; k++) {
933 tmp[k][0] = samples[k];
934 tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k];
937 for (k = 0; k <
run; k++)
941 for (k = 0; k <
run; k++)
970 quantized_coeffs[0] =
level;
972 for (i = 0; i < 7; ) {
984 for (k = 1; k <=
run; k++)
985 quantized_coeffs[i + k] = (level + ((k * diff) / run));
1004 int sb, j, k, n, ch;
1017 for (sb = 0; sb < n; sb++)
1019 for (j = 0; j < 8; j++) {
1023 for (k=0; k < 8; k++) {
1029 for (k=0; k < 8; k++)
1036 for (sb = 0; sb < n; sb++)
1044 for (j = 0; j < 8; j++)
1050 for (sb = 0; sb < n; sb++)
1052 for (j = 0; j < 8; j++) {
1074 for (i = 1; i < n; i++)
1079 for (j = 0; j < (8 - 1); ) {
1086 for (k = 1; k <=
run; k++)
1095 for (i = 0; i < 8; i++)
1180 if (nodes[0] !=
NULL)
1184 if (nodes[1] !=
NULL)
1190 if (nodes[0] !=
NULL && nodes[1] !=
NULL && nodes[2] !=
NULL)
1196 if (nodes[0] !=
NULL && nodes[1] !=
NULL && nodes[3] !=
NULL)
1212 int i, packet_bytes, sub_packet_size, sub_packets_D;
1213 unsigned int next_index = 0;
1227 if (header.
type < 2 || header.
type >= 8) {
1238 if (header.
type == 2 || header.
type == 4 || header.
type == 5) {
1254 for (i = 0; i < 6; i++)
1258 for (i = 0; packet_bytes > 0; i++) {
1275 if (next_index >= header.
size)
1283 sub_packet_size = ((packet->
size > 0xff) ? 1 : 0) + packet->
size + 2;
1285 if (packet->
type == 0)
1288 if (sub_packet_size > packet_bytes) {
1289 if (packet->
type != 10 && packet->
type != 11 && packet->
type != 12)
1291 packet->
size += packet_bytes - sub_packet_size;
1294 packet_bytes -= sub_packet_size;
1300 if (packet->
type == 8) {
1303 }
else if (packet->
type >= 9 && packet->
type <= 12) {
1306 }
else if (packet->
type == 13) {
1307 for (j = 0; j < 6; j++)
1309 }
else if (packet->
type == 14) {
1310 for (j = 0; j < 6; j++)
1312 }
else if (packet->
type == 15) {
1352 int channel, stereo, phase, exp;
1353 int local_int_4, local_int_8, stereo_phase, local_int_10;
1354 int local_int_14, stereo_exp, local_int_20, local_int_28;
1361 local_int_10 = 1 << (q->
group_order - duration - 1);
1366 while ((n =
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1368 if(local_int_4 < q->group_size)
1374 local_int_4 += local_int_10;
1375 local_int_28 += (1 << local_int_8);
1377 local_int_4 += 8*local_int_10;
1378 local_int_28 += (8 << local_int_8);
1383 offset +=
qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1384 while (offset >= (local_int_10 - 1)) {
1385 offset += (1 - (local_int_10 - 1));
1386 local_int_4 += local_int_10;
1387 local_int_28 += (1 << local_int_8);
1394 local_int_14 = (offset >> local_int_8);
1406 exp =
qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1408 exp = (exp < 0) ? 0 : exp;
1415 stereo_exp = (exp -
qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1416 stereo_phase = (phase -
qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1417 if (stereo_phase < 0)
1422 int sub_packet = (local_int_20 + local_int_28);
1436 int i, j,
min, max,
value, type, unknown_flag;
1444 for (i=0; i < 5; i++)
1454 if (value > min && value < max) {
1477 type = packet->
type;
1479 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1482 if (duration >= 0 && duration < 4)
1484 }
else if (type == 31) {
1485 for (j=0; j < 4; j++)
1487 }
else if (type == 46) {
1488 for (j=0; j < 6; j++)
1490 for (j=0; j < 4; j++)
1496 for (i = 0, j = -1; i < 5; i++)
1512 const double iscale = 2.0*
M_PI / 512.0;
1518 c.
im = level * sin(tone->
phase*iscale);
1519 c.
re = level * cos(tone->
phase*iscale);
1528 f[1] = -tone->
table[4];
1530 f[2] = 1.0 - tone->
table[2] - tone->
table[3];
1531 f[3] = tone->
table[1] + tone->
table[4] - 1.0;
1533 f[5] = tone->
table[2];
1534 for (i = 0; i < 2; i++) {
1538 for (i = 0; i < 4; i++) {
1555 const double iscale = 0.25 *
M_PI;
1557 for (ch = 0; ch < q->
channels; ch++) {
1589 for (i = 0; i < 4; i++)
1602 if (offset < q->frequency_range) {
1606 tone.
cutoff = (offset >= 60) ? 3 : 2;
1647 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1652 for (ch = 0; ch < q->
channels; ch++)
1653 for (i = 0; i < 8; i++)
1654 for (k=sb_used; k <
SBLIMIT; k++)
1658 float *samples_ptr = q->
samples + ch;
1660 for (i = 0; i < 8; i++) {
1673 for (ch = 0; ch < q->
channels; ch++)
1685 static int initialized = 0;
1687 if (initialized != 0)
1709 int tmp_val, tmp,
size;
1753 while (extradata_size > 7) {
1754 if (!memcmp(extradata,
"frmaQDM", 7))
1760 if (extradata_size < 12) {
1766 if (memcmp(extradata,
"frmaQDM", 7)) {
1771 if (extradata[7] ==
'C') {
1778 extradata_size -= 8;
1782 if(size > extradata_size){
1784 extradata_size, size);
1837 case 0: tmp = 40;
break;
1838 case 1: tmp = 48;
break;
1839 case 2: tmp = 56;
break;
1840 case 3: tmp = 72;
break;
1841 case 4: tmp = 80;
break;
1842 case 5: tmp = 100;
break;
1846 if ((tmp * 1000) < avctx->
bit_rate) tmp_val = 1;
1847 if ((tmp * 1440) < avctx->
bit_rate) tmp_val = 2;
1848 if ((tmp * 1760) < avctx->
bit_rate) tmp_val = 3;
1849 if ((tmp * 2240) < avctx->
bit_rate) tmp_val = 4;
1863 else if (tmp <= 16000)
1916 memset(&q->
output_buffer[frame_size], 0, frame_size *
sizeof(
float));
1934 for (ch = 0; ch < q->
channels; ch++) {
1966 int *got_frame_ptr,
AVPacket *avpkt)
1969 int buf_size = avpkt->
size;
1976 if(buf_size < s->checksum_size)
1987 for (i = 0; i < 16; i++) {