1 /*
2 * QDM2 compatible decoder
3 * Copyright (c) 2003 Ewald Snel
4 * Copyright (c) 2005 Benjamin Larsson
5 * Copyright (c) 2005 Alex Beregszaszi
6 * Copyright (c) 2005 Roberto Togni
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * QDM2 decoder
28 * @author Ewald Snel, Benjamin Larsson, Alex Beregszaszi, Roberto Togni
29 *
30 * The decoder is not perfect yet, there are still some distortions
31 * especially on files encoded with 16 or 8 subbands.
32 */
33
34 #include <math.h>
35 #include <stddef.h>
36
42
43 #define BITSTREAM_READER_LE
51
53
54 #define QDM2_LIST_ADD(list, size, packet) \
55 do { \
56 if (size > 0) { \
57 list[size - 1].next = &list[size]; \
58 } \
59 list[size].packet = packet; \
60 list[size].next = NULL; \
61 size++; \
62 } while(0)
63
64 // Result is 8, 16 or 30
65 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
66
67 #define FIX_NOISE_IDX(noise_idx) \
68 if ((noise_idx) >= 3840) \
69 (noise_idx) -= 3840; \
70
71 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
72
73 #define SAMPLES_NEEDED \
74 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
75
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);
78
79 #define QDM2_MAX_FRAME_SIZE 512
80
82
83 /**
84 * Subpacket
85 */
87 int type;
///< subpacket type
88 unsigned int size;
///< subpacket size
89 const uint8_t *
data;
///< pointer to subpacket data (points to input data buffer, it's not a private copy)
91
92 /**
93 * A node in the subpacket list
94 */
99
110
118
123
124 /**
125 * QDM2 decoder context
126 */
128 /// Parameters from codec header, do not change during playback
131 int group_size;
///< size of frame group (16 frames per group)
134
135 /// Parameters built from header parameters, do not change during playback
142 int cm_table_select;
///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
143
144 /// Packets and packet lists
151
152 /// FFT and tones
164
165 /// I/O data
169
170 /// Synthesis filter
176
177 /// Mixed temporary data used in decoding
187
188 // Flags
192
196
198 0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
199 };
200
202 {
205
206 /* stage-2, 3 bits exponent escape sequence */
209
210 /* stage-3, optional */
213
216 return 0;
217 }
218
220
221 if ((
value & ~3) > 0)
224 }
225
227 }
228
230 {
232
234 }
235
236 /**
237 * QDM2 checksum
238 *
239 * @param data pointer to data to be checksummed
240 * @param length data length
241 * @param value checksum value
242 *
243 * @return 0 if checksum is OK
244 */
246 {
248
249 for (
i = 0;
i < length;
i++)
251
252 return (uint16_t)(
value & 0xffff);
253 }
254
255 /**
256 * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
257 *
258 * @param gb bitreader context
259 * @param sub_packet packet under analysis
260 */
263 {
265
266 if (sub_packet->
type == 0) {
267 sub_packet->
size = 0;
269 } else {
271
272 if (sub_packet->
type & 0x80) {
273 sub_packet->
size <<= 8;
275 sub_packet->
type &= 0x7f;
276 }
277
278 if (sub_packet->
type == 0x7f)
280
281 // FIXME: this depends on bitreader-internal data
283 }
284
287 }
288
289 /**
290 * Return node pointer to first packet of requested type in list.
291 *
292 * @param list list of subpackets to be scanned
293 * @param type type of searched subpacket
294 * @return node pointer for subpacket if found, else NULL
295 */
298 {
303 }
305 }
306
307 /**
308 * Replace 8 elements with their average value.
309 * Called by qdm2_decode_superblock before starting subblock decoding.
310 *
311 * @param q context
312 */
314 {
315 int i, j, n, ch, sum;
316
318
320 for (
i = 0;
i < n;
i++) {
321 sum = 0;
322
323 for (j = 0; j < 8; j++)
325
326 sum /= 8;
328 sum--;
329
330 for (j = 0; j < 8; j++)
332 }
333 }
334
335 /**
336 * Build subband samples with noise weighted by q->tone_level.
337 * Called by synthfilt_build_sb_samples.
338 *
339 * @param q context
340 * @param sb subband index
341 */
343 {
344 int ch, j;
345
347
349 return;
350
352 for (j = 0; j < 64; j++) {
357 }
358 }
359 }
360
361 /**
362 * Called while processing data from subpackets 11 and 12.
363 * Used after making changes to coding_method array.
364 *
365 * @param sb subband index
366 * @param channels number of channels
367 * @param coding_method q->coding_method[0][0][0]
368 */
371 {
372 int j, k;
373 int ch;
375
377 for (j = 0; j < 64; ) {
378 if (coding_method[ch][sb][j] < 8)
379 return -1;
380 if ((coding_method[ch][sb][j] - 8) > 22) {
382 case_val = 8;
383 } else {
384 switch (
switchtable[coding_method[ch][sb][j] - 8]) {
386 case_val = 10;
387 break;
389 case_val = 16;
390 break;
392 case_val = 24;
393 break;
395 case_val = 30;
396 break;
398 case_val = 30;
399 break;
401 case_val = 8;
402 break;
404 case_val = 8;
405 break;
406 }
407 }
408 for (k = 0; k <
run; k++) {
409 if (j + k < 128) {
410 int sbjk = sb + (j + k) / 64;
411 if (sbjk > 29) {
413 continue;
414 }
415 if (coding_method[ch][sbjk][(j + k) % 64] > coding_method[ch][sb][j]) {
416 if (k > 0) {
418 //not debugged, almost never used
419 memset(&coding_method[ch][sb][j + k], case_val,
420 k *sizeof(int8_t));
421 memset(&coding_method[ch][sb][j + k], case_val,
422 3 * sizeof(int8_t));
423 }
424 }
425 }
426 }
428 }
429 }
430 return 0;
431 }
432
433 /**
434 * Related to synthesis filter
435 * Called by process_subpacket_10
436 *
437 * @param q context
438 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
439 */
441 {
442 int i, sb, ch, sb_used;
444
446 for (sb = 0; sb < 30; sb++)
447 for (
i = 0;
i < 8;
i++) {
451 else
456 }
457
459
461 for (sb = 0; sb < sb_used; sb++)
463 for (
i = 0;
i < 64;
i++) {
467 else
469 }
470 } else {
472 for (sb = 0; sb < sb_used; sb++) {
473 if ((sb >= 4) && (sb <= 23)) {
475 for (
i = 0;
i < 64;
i++) {
483 else
485 }
486 } else {
487 if (sb > 4) {
489 for (
i = 0;
i < 64;
i++) {
496 else
498 }
499 } else {
501 for (
i = 0;
i < 64;
i++) {
505 else
507 }
508 }
509 }
510 }
511 }
512 }
513
514 /**
515 * Related to synthesis filter
516 * Called by process_subpacket_11
517 * c is built with data from subpacket 11
518 * Most of this function is used only if superblock_type_2_3 == 0,
519 * never seen it in samples.
520 *
521 * @param tone_level_idx
522 * @param tone_level_idx_temp
523 * @param coding_method q->coding_method[0][0][0]
524 * @param nb_channels number of channels
525 * @param c coming from subpacket 11, passed as 8*c
526 * @param superblocktype_2_3 flag based on superblock packet type
527 * @param cm_table_select q->cm_table_select
528 */
532 int nb_channels,
533 int c,
int superblocktype_2_3,
534 int cm_table_select)
535 {
536 int ch, sb, j;
538 int add1, add2, add3, add4;
540
541 if (!superblocktype_2_3) {
542 /* This case is untested, no samples available */
544 return;
545 for (ch = 0; ch < nb_channels; ch++) {
546 for (sb = 0; sb < 30; sb++) {
547 for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
548 add1 = tone_level_idx[ch][sb][j] - 10;
549 if (add1 < 0)
550 add1 = 0;
551 add2 = add3 = add4 = 0;
552 if (sb > 1) {
554 if (add2 < 0)
555 add2 = 0;
556 }
557 if (sb > 0) {
559 if (add3 < 0)
560 add3 = 0;
561 }
562 if (sb < 29) {
564 if (add4 < 0)
565 add4 = 0;
566 }
567 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
570 tone_level_idx_temp[ch][sb][j + 1] =
tmp & 0xff;
571 }
572 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
573 }
574 }
575 acc = 0;
576 for (ch = 0; ch < nb_channels; ch++)
577 for (sb = 0; sb < 30; sb++)
578 for (j = 0; j < 64; j++)
579 acc += tone_level_idx_temp[ch][sb][j];
580
581 multres = 0x66666667LL * (acc * 10);
582 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
583 for (ch = 0; ch < nb_channels; ch++)
584 for (sb = 0; sb < 30; sb++)
585 for (j = 0; j < 64; j++) {
586 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
589 comp /= 256;
// signed shift
590 switch(sb) {
591 case 0:
595 break;
596 case 1:
600 break;
601 case 2:
602 case 3:
603 case 4:
606 }
615 else
617 coding_method[ch][sb][j] = ((
tmp & 0xfffa) + 30 )& 0xff;
618 }
619 for (sb = 0; sb < 30; sb++)
621 for (ch = 0; ch < nb_channels; ch++)
622 for (sb = 0; sb < 30; sb++)
623 for (j = 0; j < 64; j++)
624 if (sb >= 10) {
625 if (coding_method[ch][sb][j] < 10)
626 coding_method[ch][sb][j] = 10;
627 } else {
628 if (sb >= 2) {
629 if (coding_method[ch][sb][j] < 16)
630 coding_method[ch][sb][j] = 16;
631 } else {
632 if (coding_method[ch][sb][j] < 30)
633 coding_method[ch][sb][j] = 30;
634 }
635 }
636 } else { // superblocktype_2_3 != 0
637 for (ch = 0; ch < nb_channels; ch++)
638 for (sb = 0; sb < 30; sb++)
639 for (j = 0; j < 64; j++)
641 }
642 }
643
644 /**
645 * Called by process_subpacket_11 to process more data from subpacket 11
646 * with sb 0-8.
647 * Called by process_subpacket_12 to process data from subpacket 12 with
648 * sb 8-sb_used.
649 *
650 * @param q context
651 * @param gb bitreader context
652 * @param length packet length in bits
653 * @param sb_min lower subband processed (sb_min included)
654 * @param sb_max higher subband processed (sb_max excluded)
655 */
657 int length, int sb_min, int sb_max)
658 {
660 int joined_stereo, zero_encoding;
661 int type34_first;
662 float type34_div = 0;
663 float type34_predictor;
665 int sign_bits[16] = {0};
666
667 if (length == 0) {
668 // If no data use noise
669 for (sb=sb_min; sb < sb_max; sb++)
671
672 return 0;
673 }
674
675 for (sb = sb_min; sb < sb_max; sb++) {
677
679 joined_stereo = 0;
680 else if (sb >= 24)
681 joined_stereo = 1;
682 else
684
685 if (joined_stereo) {
687 for (j = 0; j < 16; j++)
689
690 for (j = 0; j < 64; j++)
693
698 continue;
699 }
701 }
702
706 type34_predictor = 0.0;
707 type34_first = 1;
708
709 for (j = 0; j < 128; ) {
711 case 8:
713 if (zero_encoding) {
714 for (k = 0; k < 5; k++) {
715 if ((j + 2 * k) >= 128)
716 break;
718 }
719 } else {
721 if (n >= 243) {
724 }
725
726 for (k = 0; k < 5; k++)
728 }
729 for (k = 0; k < 5; k++)
731 } else {
732 for (k = 0; k < 10; k++)
734 }
736 break;
737
738 case 10:
741
744 f -=
noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
746 } else {
748 }
750 break;
751
752 case 16:
754 if (zero_encoding) {
755 for (k = 0; k < 5; k++) {
756 if ((j + k) >= 128)
757 break;
759 }
760 } else {
762 if (n >= 243) {
765 }
766
767 for (k = 0; k < 5; k++)
769 }
770 } else {
771 for (k = 0; k < 5; k++)
773 }
775 break;
776
777 case 24:
780 if (n >= 125) {
783 }
784
785 for (k = 0; k < 3; k++)
787 } else {
788 for (k = 0; k < 3; k++)
790 }
792 break;
793
794 case 30:
800 }
802 } else
804
806 break;
807
808 case 34:
810 if (type34_first) {
814 type34_first = 0;
815 } else {
820 }
823 }
824 } else {
826 }
828 break;
829
830 default:
833 break;
834 }
835
836 if (joined_stereo) {
837 for (k = 0; k <
run && j + k < 128; k++) {
841 if (sign_bits[(j + k) / 8])
844 else
847 }
848 }
849 } else {
850 for (k = 0; k <
run; k++)
851 if ((j + k) < 128)
853 }
854
856 } // j loop
857 } // channel loop
858 } // subband loop
859 return 0;
860 }
861
862 /**
863 * Init the first element of a channel in quantized_coeffs with data
864 * from packet 10 (quantized_coeffs[ch][0]).
865 * This is similar to process_subpacket_9, but for a single channel
866 * and for element [0]
867 * same VLC tables as process_subpacket_9 are used.
868 *
869 * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
870 * @param gb bitreader context
871 */
874 {
876
878 return -1;
880
881 quantized_coeffs[0] =
level;
882
883 for (
i = 0;
i < 7; ) {
885 return -1;
887
889 return -1;
890
892 return -1;
894
895 for (k = 1; k <=
run; k++)
897
900 }
901 return 0;
902 }
903
904 /**
905 * Related to synthesis filter, process data from packet 10
906 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
907 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with
908 * data from packet 10
909 *
910 * @param q context
911 * @param gb bitreader context
912 */
914 {
915 int sb, j, k, n, ch;
916
919
922 break;
923 }
924 }
925
927
928 for (sb = 0; sb < n; sb++)
930 for (j = 0; j < 8; j++) {
932 break;
934 for (k=0; k < 8; k++) {
936 break;
938 }
939 } else {
940 for (k=0; k < 8; k++)
942 }
943 }
944
946
947 for (sb = 0; sb < n; sb++)
950 break;
952 if (sb > 19)
954 else
955 for (j = 0; j < 8; j++)
957 }
958
960
961 for (sb = 0; sb < n; sb++)
963 for (j = 0; j < 8; j++) {
965 break;
967 }
968 }
969
970 /**
971 * Process subpacket 9, init quantized_coeffs with data from it
972 *
973 * @param q context
974 * @param node pointer to node with packet
975 */
977 {
980
982
984
985 for (
i = 1;
i < n;
i++)
989
990 for (j = 0; j < (8 - 1); ) {
993
995 return -1;
996
997 for (k = 1; k <=
run; k++)
999
1002 }
1003 }
1004
1006 for (
i = 0;
i < 8;
i++)
1008
1009 return 0;
1010 }
1011
1012 /**
1013 * Process subpacket 10 if not null, else
1014 *
1015 * @param q context
1016 * @param node pointer to node with packet
1017 */
1019 {
1021
1022 if (node) {
1026 } else {
1028 }
1029 }
1030
1031 /**
1032 * Process subpacket 11
1033 *
1034 * @param q context
1035 * @param node pointer to node with packet
1036 */
1038 {
1040 int length = 0;
1041
1042 if (node) {
1045 }
1046
1047 if (length >= 32) {
1049
1055 }
1056
1058 }
1059
1060 /**
1061 * Process subpacket 12
1062 *
1063 * @param q context
1064 * @param node pointer to node with packet
1065 */
1067 {
1069 int length = 0;
1070
1071 if (node) {
1074 }
1075
1077 }
1078
1079 /**
1080 * Process new subpackets for synthesis filter
1081 *
1082 * @param q context
1083 * @param list list with synthesis filter packets (list D)
1084 */
1086 {
1088
1090 if (nodes[0])
1092
1094 if (nodes[1])
1096 else
1098
1100 if (nodes[0] && nodes[1] && nodes[2])
1102 else
1104
1106 if (nodes[0] && nodes[1] && nodes[3])
1108 else
1110 }
1111
1112 /**
1113 * Decode superblock, fill packet lists.
1114 *
1115 * @param q context
1116 */
1118 {
1121 int i, packet_bytes, sub_packet_size, sub_packets_D;
1122 unsigned int next_index = 0;
1123
1127
1129 sub_packets_D = 0;
1130
1132
1135
1139 return;
1140 }
1141
1144
1146
1150
1152
1153 if (csum != 0) {
1156 return;
1157 }
1158 }
1159
1162
1163 for (
i = 0;
i < 6;
i++)
1166
1167 for (
i = 0; packet_bytes > 0;
i++) {
1168 int j;
1169
1172 return;
1173 }
1174
1176
1179
1180 /* seek to next block */
1183
1184 if (next_index >=
header.size)
1185 break;
1186 }
1187
1188 /* decode subpacket */
1192 sub_packet_size = ((packet->
size > 0xff) ? 1 : 0) + packet->
size + 2;
1193
1194 if (packet->
type == 0)
1195 break;
1196
1197 if (sub_packet_size > packet_bytes) {
1198 if (packet->
type != 10 && packet->
type != 11 && packet->
type != 12)
1199 break;
1200 packet->
size += packet_bytes - sub_packet_size;
1201 }
1202
1203 packet_bytes -= sub_packet_size;
1204
1205 /* add subpacket to 'all subpackets' list */
1207
1208 /* add subpacket to related list */
1209 if (packet->
type == 8) {
1211 return;
1212 }
else if (packet->
type >= 9 && packet->
type <= 12) {
1213 /* packets for MPEG Audio like Synthesis Filter */
1215 }
else if (packet->
type == 13) {
1216 for (j = 0; j < 6; j++)
1218 }
else if (packet->
type == 14) {
1219 for (j = 0; j < 6; j++)
1221 }
else if (packet->
type == 15) {
1223 return;
1224 }
else if (packet->
type >= 16 && packet->
type < 48 &&
1226 /* packets for FFT */
1228 }
1229 } // Packet bytes loop
1230
1238 }
1239 }
1240
1244 {
1247
1249 ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1255 }
1256
1259 {
1261 int local_int_4, local_int_8, stereo_phase, local_int_10;
1262 int local_int_14, stereo_exp, local_int_20, local_int_28;
1264
1265 local_int_4 = 0;
1266 local_int_28 = 0;
1267 local_int_20 = 2;
1271
1276 if(local_int_4 < q->group_size)
1278 return;
1279 }
1281 if (n == 0) {
1282 local_int_4 += local_int_10;
1283 local_int_28 += (1 << local_int_8);
1284 } else {
1285 local_int_4 += 8 * local_int_10;
1286 local_int_28 += (8 << local_int_8);
1287 }
1288 }
1290 } else {
1291 if (local_int_10 <= 2) {
1293 return;
1294 }
1296 while (
offset >= (local_int_10 - 1)) {
1297 offset += (1 - (local_int_10 - 1));
1298 local_int_4 += local_int_10;
1299 local_int_28 += (1 << local_int_8);
1300 }
1301 }
1302
1304 return;
1305
1306 local_int_14 = (
offset >> local_int_8);
1308 return;
1309
1313 } else {
1315 stereo = 0;
1316 }
1317
1321
1323 stereo_exp = 0;
1324 stereo_phase = 0;
1325
1326 if (stereo) {
1329 if (stereo_phase < 0)
1330 stereo_phase += 8;
1331 }
1332
1334 int sub_packet = (local_int_20 + local_int_28);
1335
1337 return;
1338
1341 if (stereo)
1344 stereo_exp, stereo_phase);
1345 }
1347 }
1348 }
1349
1351 {
1354
1356 return;
1357
1358 /* reset minimum indexes for FFT coefficients */
1360 for (
i = 0;
i < 5;
i++)
1362
1363 /* process subpackets ordered by type, largest type first */
1366
1367 /* find subpacket with largest type less than max */
1373 }
1374 }
1375
1377
1378 /* check for errors (?) */
1379 if (!packet)
1380 return;
1381
1383 (packet->
type < 16 || packet->
type >= 48 ||
1385 return;
1386
1387 /* decode FFT tones */
1389
1391 unknown_flag = 1;
1392 else
1393 unknown_flag = 0;
1394
1396
1399
1402 }
else if (
type == 31) {
1403 for (j = 0; j < 4; j++)
1405 }
else if (
type == 46) {
1406 for (j = 0; j < 6; j++)
1408 for (j = 0; j < 4; j++)
1410 }
1411 } // Loop on B packets
1412
1413 /* calculate maximum indexes for FFT coefficients */
1414 for (
i = 0, j = -1;
i < 5;
i++)
1416 if (j >= 0)
1419 }
1420 if (j >= 0)
1422 }
1423
1425 {
1429 const double iscale = 2.0 *
M_PI / 512.0;
1430
1432
1433 /* calculate current level (maximum amplitude) of tone */
1437
1438 /* generate FFT coefficients for tone */
1444 } else {
1451 for (
i = 0;
i < 2;
i++) {
1456 }
1457 for (
i = 0;
i < 4;
i++) {
1460 }
1461 }
1462
1463 /* copy the tone if it has not yet died out */
1467 }
1468 }
1469
1471 {
1473 const double iscale = 0.25 *
M_PI;
1474
1475 for (ch = 0; ch < q->
channels; ch++) {
1477 }
1478
1479
1480 /* apply FFT tones with duration 4 (1 FFT period) */
1485
1487 break;
1488
1491
1498 }
1499
1500 /* generate existing FFT tones */
1504 }
1505
1506 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1507 for (
i = 0;
i < 4;
i++)
1512
1514 break;
1515
1519
1520 if (offset < q->frequency_range) {
1523 else
1525
1533
1535 }
1536 }
1538 }
1539 }
1540
1542 {
1545
1550
1553
1554 /* add samples to output buffer */
1559 }
1560 }
1561
1562 /**
1563 * @param q context
1564 * @param index subpacket number
1565 */
1567 {
1568 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1569
1570 /* copy sb_samples */
1572
1573 for (ch = 0; ch < q->
channels; ch++)
1574 for (
i = 0;
i < 8;
i++)
1575 for (k = sb_used; k <
SBLIMIT; k++)
1577
1579 float *samples_ptr = q->
samples + ch;
1580
1581 for (
i = 0;
i < 8;
i++) {
1588 }
1589 }
1590
1591 /* add samples to output buffer */
1593
1594 for (ch = 0; ch < q->
channels; ch++)
1597 }
1598
1599 /**
1600 * Init static data (does not depend on specific file)
1601 */
1607
1609 }
1610
1611 /**
1612 * Init parameters from codec extradata
1613 */
1615 {
1619 float scale = 1.0f / 2.0f;
1621
1622 /* extradata parsing
1623
1624 Structure:
1625 wave {
1626 frma (QDM2)
1627 QDCA
1628 QDCP
1629 }
1630
1631 32 size (including this field)
1632 32 tag (=frma)
1633 32 type (=QDM2 or QDMC)
1634
1635 32 size (including this field, in bytes)
1636 32 tag (=QDCA) // maybe mandatory parameters
1637 32 unknown (=1)
1638 32 channels (=2)
1639 32 samplerate (=44100)
1640 32 bitrate (=96000)
1641 32 block size (=4096)
1642 32 frame size (=256) (for one channel)
1643 32 packet size (=1300)
1644
1645 32 size (including this field, in bytes)
1646 32 tag (=QDCP) // maybe some tuneable parameters
1647 32 float1 (=1.0)
1648 32 zero ?
1649 32 float2 (=1.0)
1650 32 float3 (=1.0)
1651 32 unknown (27)
1652 32 unknown (8)
1653 32 zero ?
1654 */
1655
1659 }
1660
1662
1664 if (bytestream2_peek_be64u(&gb) == (((uint64_t)
MKBETAG(
'f',
'r',
'm',
'a') << 32) |
1665 (uint64_t)
MKBETAG(
'Q',
'D',
'M',
'2')))
1666 break;
1668 }
1669
1674 }
1675
1677 size = bytestream2_get_be32u(&gb);
1678
1683 }
1684
1686 if (bytestream2_get_be32u(&gb) !=
MKBETAG(
'Q',
'D',
'C',
'A')) {
1689 }
1690
1692
1693 s->nb_channels =
s->channels = bytestream2_get_be32u(&gb);
1697 }
1700
1702 avctx->
bit_rate = bytestream2_get_be32u(&gb);
1703 s->group_size = bytestream2_get_be32u(&gb);
1704 s->fft_size = bytestream2_get_be32u(&gb);
1705 s->checksum_size = bytestream2_get_be32u(&gb);
1706 if (
s->checksum_size >= 1
U << 28 ||
s->checksum_size <= 1) {
1709 }
1710
1711 s->fft_order =
av_log2(
s->fft_size) + 1;
1712
1713 // Fail on unknown fft order
1714 if ((
s->fft_order < 7) || (
s->fft_order > 9)) {
1717 }
1718
1719 // something like max decodable tones
1720 s->group_order =
av_log2(
s->group_size) + 1;
1721 s->frame_size =
s->group_size / 16;
// 16 iterations per super block
1722
1725
1726 s->sub_sampling =
s->fft_order - 7;
1727 s->frequency_range = 255 / (1 << (2 -
s->sub_sampling));
1728
1732 }
1733
1734 switch ((
s->sub_sampling * 2 +
s->channels - 1)) {
1735 case 0:
tmp = 40;
break;
1736 case 1:
tmp = 48;
break;
1737 case 2:
tmp = 56;
break;
1738 case 3:
tmp = 72;
break;
1739 case 4:
tmp = 80;
break;
1740 case 5:
tmp = 100;
break;
1741 default:
tmp=
s->sub_sampling;
break;
1742 }
1743 tmp_val = 0;
1748 s->cm_table_select = tmp_val;
1749
1751 s->coeff_per_sb_select = 0;
1753 s->coeff_per_sb_select = 1;
1754 else
1755 s->coeff_per_sb_select = 2;
1756
1757 if (
s->fft_size != (1 << (
s->fft_order - 1))) {
1760 }
1761
1765
1767
1769
1771
1772 return 0;
1773 }
1774
1776 {
1778
1780
1781 return 0;
1782 }
1783
1785 {
1788
1790 return -1;
1791
1792 /* select input buffer */
1795
1796 /* copy old block, clear new block of output samples */
1799
1800 /* decode block of QDM2 compressed data */
1802 q->
has_errors = 0;
// zero it for a new super block
1805 }
1806
1807 /* parse subpackets */
1811
1813 }
1814
1815 /* sound synthesis stage 1 (FFT) */
1816 for (ch = 0; ch < q->
channels; ch++) {
1818
1821 return -1;
1822 }
1823 }
1824
1825 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1828
1830
1831 /* clip and convert output float[] to 16-bit signed samples */
1834
1839
1841 }
1842
1843 return 0;
1844 }
1845
1847 int *got_frame_ptr,
AVPacket *avpkt)
1848 {
1849 const uint8_t *buf = avpkt->
data;
1850 int buf_size = avpkt->
size;
1854
1855 if(!buf)
1856 return 0;
1857 if(buf_size < s->checksum_size)
1858 return -1;
1859
1860 /* get output buffer */
1861 frame->nb_samples = 16 *
s->frame_size;
1865
1866 for (
i = 0;
i < 16;
i++) {
1869 out +=
s->channels *
s->frame_size;
1870 }
1871
1872 *got_frame_ptr = 1;
1873
1874 return s->checksum_size;
1875 }
1876
1887 };