FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/qdm2.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 471 850 55.4%
Branches: 278 671 41.4%

Line Branch Exec Source
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 #include <stdio.h>
37
38 #include "libavutil/channel_layout.h"
39 #include "libavutil/mem_internal.h"
40 #include "libavutil/thread.h"
41
42 #define BITSTREAM_READER_LE
43 #include "avcodec.h"
44 #include "get_bits.h"
45 #include "bytestream.h"
46 #include "internal.h"
47 #include "mpegaudio.h"
48 #include "mpegaudiodsp.h"
49 #include "rdft.h"
50
51 #include "qdm2_tablegen.h"
52
53 #define QDM2_LIST_ADD(list, size, packet) \
54 do { \
55 if (size > 0) { \
56 list[size - 1].next = &list[size]; \
57 } \
58 list[size].packet = packet; \
59 list[size].next = NULL; \
60 size++; \
61 } while(0)
62
63 // Result is 8, 16 or 30
64 #define QDM2_SB_USED(sub_sampling) (((sub_sampling) >= 2) ? 30 : 8 << (sub_sampling))
65
66 #define FIX_NOISE_IDX(noise_idx) \
67 if ((noise_idx) >= 3840) \
68 (noise_idx) -= 3840; \
69
70 #define SB_DITHERING_NOISE(sb,noise_idx) (noise_table[(noise_idx)++] * sb_noise_attenuation[(sb)])
71
72 #define SAMPLES_NEEDED \
73 av_log (NULL,AV_LOG_INFO,"This file triggers some untested code. Please contact the developers.\n");
74
75 #define SAMPLES_NEEDED_2(why) \
76 av_log (NULL,AV_LOG_INFO,"This file triggers some missing code. Please contact the developers.\nPosition: %s\n",why);
77
78 #define QDM2_MAX_FRAME_SIZE 512
79
80 typedef int8_t sb_int8_array[2][30][64];
81
82 /**
83 * Subpacket
84 */
85 typedef struct QDM2SubPacket {
86 int type; ///< subpacket type
87 unsigned int size; ///< subpacket size
88 const uint8_t *data; ///< pointer to subpacket data (points to input data buffer, it's not a private copy)
89 } QDM2SubPacket;
90
91 /**
92 * A node in the subpacket list
93 */
94 typedef struct QDM2SubPNode {
95 QDM2SubPacket *packet; ///< packet
96 struct QDM2SubPNode *next; ///< pointer to next packet in the list, NULL if leaf node
97 } QDM2SubPNode;
98
99 typedef struct QDM2Complex {
100 float re;
101 float im;
102 } QDM2Complex;
103
104 typedef struct FFTTone {
105 float level;
106 QDM2Complex *complex;
107 const float *table;
108 int phase;
109 int phase_shift;
110 int duration;
111 short time_index;
112 short cutoff;
113 } FFTTone;
114
115 typedef struct FFTCoefficient {
116 int16_t sub_packet;
117 uint8_t channel;
118 int16_t offset;
119 int16_t exp;
120 uint8_t phase;
121 } FFTCoefficient;
122
123 typedef struct QDM2FFT {
124 DECLARE_ALIGNED(32, QDM2Complex, complex)[MPA_MAX_CHANNELS][256];
125 } QDM2FFT;
126
127 /**
128 * QDM2 decoder context
129 */
130 typedef struct QDM2Context {
131 /// Parameters from codec header, do not change during playback
132 int nb_channels; ///< number of channels
133 int channels; ///< number of channels
134 int group_size; ///< size of frame group (16 frames per group)
135 int fft_size; ///< size of FFT, in complex numbers
136 int checksum_size; ///< size of data block, used also for checksum
137
138 /// Parameters built from header parameters, do not change during playback
139 int group_order; ///< order of frame group
140 int fft_order; ///< order of FFT (actually fftorder+1)
141 int frame_size; ///< size of data frame
142 int frequency_range;
143 int sub_sampling; ///< subsampling: 0=25%, 1=50%, 2=100% */
144 int coeff_per_sb_select; ///< selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
145 int cm_table_select; ///< selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
146
147 /// Packets and packet lists
148 QDM2SubPacket sub_packets[16]; ///< the packets themselves
149 QDM2SubPNode sub_packet_list_A[16]; ///< list of all packets
150 QDM2SubPNode sub_packet_list_B[16]; ///< FFT packets B are on list
151 int sub_packets_B; ///< number of packets on 'B' list
152 QDM2SubPNode sub_packet_list_C[16]; ///< packets with errors?
153 QDM2SubPNode sub_packet_list_D[16]; ///< DCT packets
154
155 /// FFT and tones
156 FFTTone fft_tones[1000];
157 int fft_tone_start;
158 int fft_tone_end;
159 FFTCoefficient fft_coefs[1000];
160 int fft_coefs_index;
161 int fft_coefs_min_index[5];
162 int fft_coefs_max_index[5];
163 int fft_level_exp[6];
164 RDFTContext rdft_ctx;
165 QDM2FFT fft;
166
167 /// I/O data
168 const uint8_t *compressed_data;
169 int compressed_size;
170 float output_buffer[QDM2_MAX_FRAME_SIZE * MPA_MAX_CHANNELS * 2];
171
172 /// Synthesis filter
173 MPADSPContext mpadsp;
174 DECLARE_ALIGNED(32, float, synth_buf)[MPA_MAX_CHANNELS][512*2];
175 int synth_buf_offset[MPA_MAX_CHANNELS];
176 DECLARE_ALIGNED(32, float, sb_samples)[MPA_MAX_CHANNELS][128][SBLIMIT];
177 DECLARE_ALIGNED(32, float, samples)[MPA_MAX_CHANNELS * MPA_FRAME_SIZE];
178
179 /// Mixed temporary data used in decoding
180 float tone_level[MPA_MAX_CHANNELS][30][64];
181 int8_t coding_method[MPA_MAX_CHANNELS][30][64];
182 int8_t quantized_coeffs[MPA_MAX_CHANNELS][10][8];
183 int8_t tone_level_idx_base[MPA_MAX_CHANNELS][30][8];
184 int8_t tone_level_idx_hi1[MPA_MAX_CHANNELS][3][8][8];
185 int8_t tone_level_idx_mid[MPA_MAX_CHANNELS][26][8];
186 int8_t tone_level_idx_hi2[MPA_MAX_CHANNELS][26];
187 int8_t tone_level_idx[MPA_MAX_CHANNELS][30][64];
188 int8_t tone_level_idx_temp[MPA_MAX_CHANNELS][30][64];
189
190 // Flags
191 int has_errors; ///< packet has errors
192 int superblocktype_2_3; ///< select fft tables and some algorithm based on superblock type
193 int do_synth_filter; ///< used to perform or skip synthesis filter
194
195 int sub_packet;
196 int noise_idx; ///< index for dithering noise table
197 } QDM2Context;
198
199 static const int switchtable[23] = {
200 0, 5, 1, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 3, 5, 5, 5, 5, 5, 4
201 };
202
203 67245 static int qdm2_get_vlc(GetBitContext *gb, const VLC *vlc, int flag, int depth)
204 {
205 int value;
206
207 67245 value = get_vlc2(gb, vlc->table, vlc->bits, depth);
208
209 /* stage-2, 3 bits exponent escape sequence */
210
2/2
✓ Branch 0 taken 556 times.
✓ Branch 1 taken 66689 times.
67245 if (value < 0)
211 556 value = get_bits(gb, get_bits(gb, 3) + 1);
212
213 /* stage-3, optional */
214
2/2
✓ Branch 0 taken 18212 times.
✓ Branch 1 taken 49033 times.
67245 if (flag) {
215 int tmp;
216
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18212 times.
18212 if (value >= 60) {
218 av_log(NULL, AV_LOG_ERROR, "value %d in qdm2_get_vlc too large\n", value);
219 return 0;
220 }
221
222 18212 tmp= vlc_stage3_values[value];
223
224
2/2
✓ Branch 0 taken 14145 times.
✓ Branch 1 taken 4067 times.
18212 if ((value & ~3) > 0)
225 14145 tmp += get_bits(gb, (value >> 2));
226 18212 value = tmp;
227 }
228
229 67245 return value;
230 }
231
232 10996 static int qdm2_get_se_vlc(const VLC *vlc, GetBitContext *gb, int depth)
233 {
234 10996 int value = qdm2_get_vlc(gb, vlc, 0, depth);
235
236
2/2
✓ Branch 0 taken 4344 times.
✓ Branch 1 taken 6652 times.
10996 return (value & 1) ? ((value + 1) >> 1) : -(value >> 1);
237 }
238
239 /**
240 * QDM2 checksum
241 *
242 * @param data pointer to data to be checksummed
243 * @param length data length
244 * @param value checksum value
245 *
246 * @return 0 if checksum is OK
247 */
248 139 static uint16_t qdm2_packet_checksum(const uint8_t *data, int length, int value)
249 {
250 int i;
251
252
2/2
✓ Branch 0 taken 51430 times.
✓ Branch 1 taken 139 times.
51569 for (i = 0; i < length; i++)
253 51430 value -= data[i];
254
255 139 return (uint16_t)(value & 0xffff);
256 }
257
258 /**
259 * Fill a QDM2SubPacket structure with packet type, size, and data pointer.
260 *
261 * @param gb bitreader context
262 * @param sub_packet packet under analysis
263 */
264 1112 static void qdm2_decode_sub_packet_header(GetBitContext *gb,
265 QDM2SubPacket *sub_packet)
266 {
267 1112 sub_packet->type = get_bits(gb, 8);
268
269
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 973 times.
1112 if (sub_packet->type == 0) {
270 139 sub_packet->size = 0;
271 139 sub_packet->data = NULL;
272 } else {
273 973 sub_packet->size = get_bits(gb, 8);
274
275
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 834 times.
973 if (sub_packet->type & 0x80) {
276 139 sub_packet->size <<= 8;
277 139 sub_packet->size |= get_bits(gb, 8);
278 139 sub_packet->type &= 0x7f;
279 }
280
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 973 times.
973 if (sub_packet->type == 0x7f)
282 sub_packet->type |= (get_bits(gb, 8) << 8);
283
284 // FIXME: this depends on bitreader-internal data
285 973 sub_packet->data = &gb->buffer[get_bits_count(gb) / 8];
286 }
287
288 1112 av_log(NULL, AV_LOG_DEBUG, "Subpacket: type=%d size=%d start_offs=%x\n",
289 1112 sub_packet->type, sub_packet->size, get_bits_count(gb) / 8);
290 1112 }
291
292 /**
293 * Return node pointer to first packet of requested type in list.
294 *
295 * @param list list of subpackets to be scanned
296 * @param type type of searched subpacket
297 * @return node pointer for subpacket if found, else NULL
298 */
299 556 static QDM2SubPNode *qdm2_search_subpacket_type_in_list(QDM2SubPNode *list,
300 int type)
301 {
302
3/4
✓ Branch 0 taken 556 times.
✓ Branch 1 taken 417 times.
✓ Branch 2 taken 556 times.
✗ Branch 3 not taken.
973 while (list && list->packet) {
303
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 417 times.
556 if (list->packet->type == type)
304 139 return list;
305 417 list = list->next;
306 }
307 417 return NULL;
308 }
309
310 /**
311 * Replace 8 elements with their average value.
312 * Called by qdm2_decode_superblock before starting subblock decoding.
313 *
314 * @param q context
315 */
316 139 static void average_quantized_coeffs(QDM2Context *q)
317 {
318 int i, j, n, ch, sum;
319
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
321
322
2/2
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 139 times.
417 for (ch = 0; ch < q->nb_channels; ch++)
323
2/2
✓ Branch 0 taken 2780 times.
✓ Branch 1 taken 278 times.
3058 for (i = 0; i < n; i++) {
324 2780 sum = 0;
325
326
2/2
✓ Branch 0 taken 22240 times.
✓ Branch 1 taken 2780 times.
25020 for (j = 0; j < 8; j++)
327 22240 sum += q->quantized_coeffs[ch][i][j];
328
329 2780 sum /= 8;
330
2/2
✓ Branch 0 taken 2466 times.
✓ Branch 1 taken 314 times.
2780 if (sum > 0)
331 2466 sum--;
332
333
2/2
✓ Branch 0 taken 22240 times.
✓ Branch 1 taken 2780 times.
25020 for (j = 0; j < 8; j++)
334 22240 q->quantized_coeffs[ch][i][j] = sum;
335 }
336 139 }
337
338 /**
339 * Build subband samples with noise weighted by q->tone_level.
340 * Called by synthfilt_build_sb_samples.
341 *
342 * @param q context
343 * @param sb subband index
344 */
345 4170 static void build_sb_samples_from_noise(QDM2Context *q, int sb)
346 {
347 int ch, j;
348
349
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 3894 times.
4170 FIX_NOISE_IDX(q->noise_idx);
350
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4170 times.
4170 if (!q->nb_channels)
352 return;
353
354
2/2
✓ Branch 0 taken 8340 times.
✓ Branch 1 taken 4170 times.
12510 for (ch = 0; ch < q->nb_channels; ch++) {
355
2/2
✓ Branch 0 taken 533760 times.
✓ Branch 1 taken 8340 times.
542100 for (j = 0; j < 64; j++) {
356 533760 q->sb_samples[ch][j * 2][sb] =
357 533760 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
358 533760 q->sb_samples[ch][j * 2 + 1][sb] =
359 533760 SB_DITHERING_NOISE(sb, q->noise_idx) * q->tone_level[ch][sb][j];
360 }
361 }
362 }
363
364 /**
365 * Called while processing data from subpackets 11 and 12.
366 * Used after making changes to coding_method array.
367 *
368 * @param sb subband index
369 * @param channels number of channels
370 * @param coding_method q->coding_method[0][0][0]
371 */
372 static int fix_coding_method_array(int sb, int channels,
373 sb_int8_array coding_method)
374 {
375 int j, k;
376 int ch;
377 int run, case_val;
378
379 for (ch = 0; ch < channels; ch++) {
380 for (j = 0; j < 64; ) {
381 if (coding_method[ch][sb][j] < 8)
382 return -1;
383 if ((coding_method[ch][sb][j] - 8) > 22) {
384 run = 1;
385 case_val = 8;
386 } else {
387 switch (switchtable[coding_method[ch][sb][j] - 8]) {
388 case 0: run = 10;
389 case_val = 10;
390 break;
391 case 1: run = 1;
392 case_val = 16;
393 break;
394 case 2: run = 5;
395 case_val = 24;
396 break;
397 case 3: run = 3;
398 case_val = 30;
399 break;
400 case 4: run = 1;
401 case_val = 30;
402 break;
403 case 5: run = 1;
404 case_val = 8;
405 break;
406 default: run = 1;
407 case_val = 8;
408 break;
409 }
410 }
411 for (k = 0; k < run; k++) {
412 if (j + k < 128) {
413 int sbjk = sb + (j + k) / 64;
414 if (sbjk > 29) {
415 SAMPLES_NEEDED
416 continue;
417 }
418 if (coding_method[ch][sbjk][(j + k) % 64] > coding_method[ch][sb][j]) {
419 if (k > 0) {
420 SAMPLES_NEEDED
421 //not debugged, almost never used
422 memset(&coding_method[ch][sb][j + k], case_val,
423 k *sizeof(int8_t));
424 memset(&coding_method[ch][sb][j + k], case_val,
425 3 * sizeof(int8_t));
426 }
427 }
428 }
429 }
430 j += run;
431 }
432 }
433 return 0;
434 }
435
436 /**
437 * Related to synthesis filter
438 * Called by process_subpacket_10
439 *
440 * @param q context
441 * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10
442 */
443 139 static void fill_tone_level_array(QDM2Context *q, int flag)
444 {
445 int i, sb, ch, sb_used;
446 int tmp, tab;
447
448
2/2
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 139 times.
417 for (ch = 0; ch < q->nb_channels; ch++)
449
2/2
✓ Branch 0 taken 8340 times.
✓ Branch 1 taken 278 times.
8618 for (sb = 0; sb < 30; sb++)
450
2/2
✓ Branch 0 taken 66720 times.
✓ Branch 1 taken 8340 times.
75060 for (i = 0; i < 8; i++) {
451
2/2
✓ Branch 0 taken 53376 times.
✓ Branch 1 taken 13344 times.
66720 if ((tab=coeff_per_sb_for_dequant[q->coeff_per_sb_select][sb]) < (last_coeff[q->coeff_per_sb_select] - 1))
452 53376 tmp = q->quantized_coeffs[ch][tab + 1][i] * dequant_table[q->coeff_per_sb_select][tab + 1][sb]+
453 53376 q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
454 else
455 13344 tmp = q->quantized_coeffs[ch][tab][i] * dequant_table[q->coeff_per_sb_select][tab][sb];
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66720 times.
66720 if(tmp < 0)
457 tmp += 0xff;
458 66720 q->tone_level_idx_base[ch][sb][i] = (tmp / 256) & 0xff;
459 }
460
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 sb_used = QDM2_SB_USED(q->sub_sampling);
462
463
2/4
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
139 if ((q->superblocktype_2_3 != 0) && !flag) {
464
2/2
✓ Branch 0 taken 4170 times.
✓ Branch 1 taken 139 times.
4309 for (sb = 0; sb < sb_used; sb++)
465
2/2
✓ Branch 0 taken 8340 times.
✓ Branch 1 taken 4170 times.
12510 for (ch = 0; ch < q->nb_channels; ch++)
466
2/2
✓ Branch 0 taken 533760 times.
✓ Branch 1 taken 8340 times.
542100 for (i = 0; i < 64; i++) {
467 533760 q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 533760 times.
533760 if (q->tone_level_idx[ch][sb][i] < 0)
469 q->tone_level[ch][sb][i] = 0;
470 else
471 533760 q->tone_level[ch][sb][i] = fft_tone_level_table[0][q->tone_level_idx[ch][sb][i] & 0x3f];
472 }
473 } else {
474 tab = q->superblocktype_2_3 ? 0 : 1;
475 for (sb = 0; sb < sb_used; sb++) {
476 if ((sb >= 4) && (sb <= 23)) {
477 for (ch = 0; ch < q->nb_channels; ch++)
478 for (i = 0; i < 64; i++) {
479 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
480 q->tone_level_idx_hi1[ch][sb / 8][i / 8][i % 8] -
481 q->tone_level_idx_mid[ch][sb - 4][i / 8] -
482 q->tone_level_idx_hi2[ch][sb - 4];
483 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
484 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
485 q->tone_level[ch][sb][i] = 0;
486 else
487 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
488 }
489 } else {
490 if (sb > 4) {
491 for (ch = 0; ch < q->nb_channels; ch++)
492 for (i = 0; i < 64; i++) {
493 tmp = q->tone_level_idx_base[ch][sb][i / 8] -
494 q->tone_level_idx_hi1[ch][2][i / 8][i % 8] -
495 q->tone_level_idx_hi2[ch][sb - 4];
496 q->tone_level_idx[ch][sb][i] = tmp & 0xff;
497 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
498 q->tone_level[ch][sb][i] = 0;
499 else
500 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
501 }
502 } else {
503 for (ch = 0; ch < q->nb_channels; ch++)
504 for (i = 0; i < 64; i++) {
505 tmp = q->tone_level_idx[ch][sb][i] = q->tone_level_idx_base[ch][sb][i / 8];
506 if ((tmp < 0) || (!q->superblocktype_2_3 && !tmp))
507 q->tone_level[ch][sb][i] = 0;
508 else
509 q->tone_level[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f];
510 }
511 }
512 }
513 }
514 }
515 139 }
516
517 /**
518 * Related to synthesis filter
519 * Called by process_subpacket_11
520 * c is built with data from subpacket 11
521 * Most of this function is used only if superblock_type_2_3 == 0,
522 * never seen it in samples.
523 *
524 * @param tone_level_idx
525 * @param tone_level_idx_temp
526 * @param coding_method q->coding_method[0][0][0]
527 * @param nb_channels number of channels
528 * @param c coming from subpacket 11, passed as 8*c
529 * @param superblocktype_2_3 flag based on superblock packet type
530 * @param cm_table_select q->cm_table_select
531 */
532 static void fill_coding_method_array(sb_int8_array tone_level_idx,
533 sb_int8_array tone_level_idx_temp,
534 sb_int8_array coding_method,
535 int nb_channels,
536 int c, int superblocktype_2_3,
537 int cm_table_select)
538 {
539 int ch, sb, j;
540 int tmp, acc, esp_40, comp;
541 int add1, add2, add3, add4;
542 int64_t multres;
543
544 if (!superblocktype_2_3) {
545 /* This case is untested, no samples available */
546 avpriv_request_sample(NULL, "!superblocktype_2_3");
547 return;
548 for (ch = 0; ch < nb_channels; ch++) {
549 for (sb = 0; sb < 30; sb++) {
550 for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer
551 add1 = tone_level_idx[ch][sb][j] - 10;
552 if (add1 < 0)
553 add1 = 0;
554 add2 = add3 = add4 = 0;
555 if (sb > 1) {
556 add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6;
557 if (add2 < 0)
558 add2 = 0;
559 }
560 if (sb > 0) {
561 add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6;
562 if (add3 < 0)
563 add3 = 0;
564 }
565 if (sb < 29) {
566 add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6;
567 if (add4 < 0)
568 add4 = 0;
569 }
570 tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1;
571 if (tmp < 0)
572 tmp = 0;
573 tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff;
574 }
575 tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1];
576 }
577 }
578 acc = 0;
579 for (ch = 0; ch < nb_channels; ch++)
580 for (sb = 0; sb < 30; sb++)
581 for (j = 0; j < 64; j++)
582 acc += tone_level_idx_temp[ch][sb][j];
583
584 multres = 0x66666667LL * (acc * 10);
585 esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31);
586 for (ch = 0; ch < nb_channels; ch++)
587 for (sb = 0; sb < 30; sb++)
588 for (j = 0; j < 64; j++) {
589 comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10;
590 if (comp < 0)
591 comp += 0xff;
592 comp /= 256; // signed shift
593 switch(sb) {
594 case 0:
595 if (comp < 30)
596 comp = 30;
597 comp += 15;
598 break;
599 case 1:
600 if (comp < 24)
601 comp = 24;
602 comp += 10;
603 break;
604 case 2:
605 case 3:
606 case 4:
607 if (comp < 16)
608 comp = 16;
609 }
610 if (comp <= 5)
611 tmp = 0;
612 else if (comp <= 10)
613 tmp = 10;
614 else if (comp <= 16)
615 tmp = 16;
616 else if (comp <= 24)
617 tmp = -1;
618 else
619 tmp = 0;
620 coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff;
621 }
622 for (sb = 0; sb < 30; sb++)
623 fix_coding_method_array(sb, nb_channels, coding_method);
624 for (ch = 0; ch < nb_channels; ch++)
625 for (sb = 0; sb < 30; sb++)
626 for (j = 0; j < 64; j++)
627 if (sb >= 10) {
628 if (coding_method[ch][sb][j] < 10)
629 coding_method[ch][sb][j] = 10;
630 } else {
631 if (sb >= 2) {
632 if (coding_method[ch][sb][j] < 16)
633 coding_method[ch][sb][j] = 16;
634 } else {
635 if (coding_method[ch][sb][j] < 30)
636 coding_method[ch][sb][j] = 30;
637 }
638 }
639 } else { // superblocktype_2_3 != 0
640 for (ch = 0; ch < nb_channels; ch++)
641 for (sb = 0; sb < 30; sb++)
642 for (j = 0; j < 64; j++)
643 coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb];
644 }
645 }
646
647 /**
648 * Called by process_subpacket_11 to process more data from subpacket 11
649 * with sb 0-8.
650 * Called by process_subpacket_12 to process data from subpacket 12 with
651 * sb 8-sb_used.
652 *
653 * @param q context
654 * @param gb bitreader context
655 * @param length packet length in bits
656 * @param sb_min lower subband processed (sb_min included)
657 * @param sb_max higher subband processed (sb_max excluded)
658 */
659 278 static int synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb,
660 int length, int sb_min, int sb_max)
661 {
662 int sb, j, k, n, ch, run, channels;
663 int joined_stereo, zero_encoding;
664 int type34_first;
665 278 float type34_div = 0;
666 float type34_predictor;
667 float samples[10];
668 278 int sign_bits[16] = {0};
669
670
1/2
✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
278 if (length == 0) {
671 // If no data use noise
672
2/2
✓ Branch 0 taken 4170 times.
✓ Branch 1 taken 278 times.
4448 for (sb=sb_min; sb < sb_max; sb++)
673 4170 build_sb_samples_from_noise(q, sb);
674
675 278 return 0;
676 }
677
678 for (sb = sb_min; sb < sb_max; sb++) {
679 channels = q->nb_channels;
680
681 if (q->nb_channels <= 1 || sb < 12)
682 joined_stereo = 0;
683 else if (sb >= 24)
684 joined_stereo = 1;
685 else
686 joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
687
688 if (joined_stereo) {
689 if (get_bits_left(gb) >= 16)
690 for (j = 0; j < 16; j++)
691 sign_bits[j] = get_bits1(gb);
692
693 for (j = 0; j < 64; j++)
694 if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
695 q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
696
697 if (fix_coding_method_array(sb, q->nb_channels,
698 q->coding_method)) {
699 av_log(NULL, AV_LOG_ERROR, "coding method invalid\n");
700 build_sb_samples_from_noise(q, sb);
701 continue;
702 }
703 channels = 1;
704 }
705
706 for (ch = 0; ch < channels; ch++) {
707 FIX_NOISE_IDX(q->noise_idx);
708 zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
709 type34_predictor = 0.0;
710 type34_first = 1;
711
712 for (j = 0; j < 128; ) {
713 switch (q->coding_method[ch][sb][j / 2]) {
714 case 8:
715 if (get_bits_left(gb) >= 10) {
716 if (zero_encoding) {
717 for (k = 0; k < 5; k++) {
718 if ((j + 2 * k) >= 128)
719 break;
720 samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
721 }
722 } else {
723 n = get_bits(gb, 8);
724 if (n >= 243) {
725 av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
726 return AVERROR_INVALIDDATA;
727 }
728
729 for (k = 0; k < 5; k++)
730 samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
731 }
732 for (k = 0; k < 5; k++)
733 samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
734 } else {
735 for (k = 0; k < 10; k++)
736 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
737 }
738 run = 10;
739 break;
740
741 case 10:
742 if (get_bits_left(gb) >= 1) {
743 float f = 0.81;
744
745 if (get_bits1(gb))
746 f = -f;
747 f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
748 samples[0] = f;
749 } else {
750 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
751 }
752 run = 1;
753 break;
754
755 case 16:
756 if (get_bits_left(gb) >= 10) {
757 if (zero_encoding) {
758 for (k = 0; k < 5; k++) {
759 if ((j + k) >= 128)
760 break;
761 samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
762 }
763 } else {
764 n = get_bits (gb, 8);
765 if (n >= 243) {
766 av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
767 return AVERROR_INVALIDDATA;
768 }
769
770 for (k = 0; k < 5; k++)
771 samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
772 }
773 } else {
774 for (k = 0; k < 5; k++)
775 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
776 }
777 run = 5;
778 break;
779
780 case 24:
781 if (get_bits_left(gb) >= 7) {
782 n = get_bits(gb, 7);
783 if (n >= 125) {
784 av_log(NULL, AV_LOG_ERROR, "Invalid 7bit codeword\n");
785 return AVERROR_INVALIDDATA;
786 }
787
788 for (k = 0; k < 3; k++)
789 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
790 } else {
791 for (k = 0; k < 3; k++)
792 samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
793 }
794 run = 3;
795 break;
796
797 case 30:
798 if (get_bits_left(gb) >= 4) {
799 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
800 if (index >= FF_ARRAY_ELEMS(type30_dequant)) {
801 av_log(NULL, AV_LOG_ERROR, "index %d out of type30_dequant array\n", index);
802 return AVERROR_INVALIDDATA;
803 }
804 samples[0] = type30_dequant[index];
805 } else
806 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
807
808 run = 1;
809 break;
810
811 case 34:
812 if (get_bits_left(gb) >= 7) {
813 if (type34_first) {
814 type34_div = (float)(1 << get_bits(gb, 2));
815 samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
816 type34_predictor = samples[0];
817 type34_first = 0;
818 } else {
819 unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
820 if (index >= FF_ARRAY_ELEMS(type34_delta)) {
821 av_log(NULL, AV_LOG_ERROR, "index %d out of type34_delta array\n", index);
822 return AVERROR_INVALIDDATA;
823 }
824 samples[0] = type34_delta[index] / type34_div + type34_predictor;
825 type34_predictor = samples[0];
826 }
827 } else {
828 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
829 }
830 run = 1;
831 break;
832
833 default:
834 samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
835 run = 1;
836 break;
837 }
838
839 if (joined_stereo) {
840 for (k = 0; k < run && j + k < 128; k++) {
841 q->sb_samples[0][j + k][sb] =
842 q->tone_level[0][sb][(j + k) / 2] * samples[k];
843 if (q->nb_channels == 2) {
844 if (sign_bits[(j + k) / 8])
845 q->sb_samples[1][j + k][sb] =
846 q->tone_level[1][sb][(j + k) / 2] * -samples[k];
847 else
848 q->sb_samples[1][j + k][sb] =
849 q->tone_level[1][sb][(j + k) / 2] * samples[k];
850 }
851 }
852 } else {
853 for (k = 0; k < run; k++)
854 if ((j + k) < 128)
855 q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
856 }
857
858 j += run;
859 } // j loop
860 } // channel loop
861 } // subband loop
862 return 0;
863 }
864
865 /**
866 * Init the first element of a channel in quantized_coeffs with data
867 * from packet 10 (quantized_coeffs[ch][0]).
868 * This is similar to process_subpacket_9, but for a single channel
869 * and for element [0]
870 * same VLC tables as process_subpacket_9 are used.
871 *
872 * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
873 * @param gb bitreader context
874 */
875 static int init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
876 GetBitContext *gb)
877 {
878 int i, k, run, level, diff;
879
880 if (get_bits_left(gb) < 16)
881 return -1;
882 level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
883
884 quantized_coeffs[0] = level;
885
886 for (i = 0; i < 7; ) {
887 if (get_bits_left(gb) < 16)
888 return -1;
889 run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
890
891 if (i + run >= 8)
892 return -1;
893
894 if (get_bits_left(gb) < 16)
895 return -1;
896 diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
897
898 for (k = 1; k <= run; k++)
899 quantized_coeffs[i + k] = (level + ((k * diff) / run));
900
901 level += diff;
902 i += run;
903 }
904 return 0;
905 }
906
907 /**
908 * Related to synthesis filter, process data from packet 10
909 * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
910 * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with
911 * data from packet 10
912 *
913 * @param q context
914 * @param gb bitreader context
915 */
916 static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb)
917 {
918 int sb, j, k, n, ch;
919
920 for (ch = 0; ch < q->nb_channels; ch++) {
921 init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb);
922
923 if (get_bits_left(gb) < 16) {
924 memset(q->quantized_coeffs[ch][0], 0, 8);
925 break;
926 }
927 }
928
929 n = q->sub_sampling + 1;
930
931 for (sb = 0; sb < n; sb++)
932 for (ch = 0; ch < q->nb_channels; ch++)
933 for (j = 0; j < 8; j++) {
934 if (get_bits_left(gb) < 1)
935 break;
936 if (get_bits1(gb)) {
937 for (k=0; k < 8; k++) {
938 if (get_bits_left(gb) < 16)
939 break;
940 q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
941 }
942 } else {
943 for (k=0; k < 8; k++)
944 q->tone_level_idx_hi1[ch][sb][j][k] = 0;
945 }
946 }
947
948 n = QDM2_SB_USED(q->sub_sampling) - 4;
949
950 for (sb = 0; sb < n; sb++)
951 for (ch = 0; ch < q->nb_channels; ch++) {
952 if (get_bits_left(gb) < 16)
953 break;
954 q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
955 if (sb > 19)
956 q->tone_level_idx_hi2[ch][sb] -= 16;
957 else
958 for (j = 0; j < 8; j++)
959 q->tone_level_idx_mid[ch][sb][j] = -16;
960 }
961
962 n = QDM2_SB_USED(q->sub_sampling) - 5;
963
964 for (sb = 0; sb < n; sb++)
965 for (ch = 0; ch < q->nb_channels; ch++)
966 for (j = 0; j < 8; j++) {
967 if (get_bits_left(gb) < 16)
968 break;
969 q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
970 }
971 }
972
973 /**
974 * Process subpacket 9, init quantized_coeffs with data from it
975 *
976 * @param q context
977 * @param node pointer to node with packet
978 */
979 139 static int process_subpacket_9(QDM2Context *q, QDM2SubPNode *node)
980 {
981 GetBitContext gb;
982 int i, j, k, n, ch, run, level, diff;
983
984 139 init_get_bits(&gb, node->packet->data, node->packet->size * 8);
985
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
987
988
2/2
✓ Branch 0 taken 1251 times.
✓ Branch 1 taken 139 times.
1390 for (i = 1; i < n; i++)
989
2/2
✓ Branch 0 taken 2502 times.
✓ Branch 1 taken 1251 times.
3753 for (ch = 0; ch < q->nb_channels; ch++) {
990 2502 level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
991 2502 q->quantized_coeffs[ch][i][0] = level;
992
993
2/2
✓ Branch 0 taken 10996 times.
✓ Branch 1 taken 2502 times.
13498 for (j = 0; j < (8 - 1); ) {
994 10996 run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
995 10996 diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
996
997
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10996 times.
10996 if (j + run >= 8)
998 return -1;
999
1000
2/2
✓ Branch 0 taken 17514 times.
✓ Branch 1 taken 10996 times.
28510 for (k = 1; k <= run; k++)
1001 17514 q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
1002
1003 10996 level += diff;
1004 10996 j += run;
1005 }
1006 }
1007
1008
2/2
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 139 times.
417 for (ch = 0; ch < q->nb_channels; ch++)
1009
2/2
✓ Branch 0 taken 2224 times.
✓ Branch 1 taken 278 times.
2502 for (i = 0; i < 8; i++)
1010 2224 q->quantized_coeffs[ch][0][i] = 0;
1011
1012 139 return 0;
1013 }
1014
1015 /**
1016 * Process subpacket 10 if not null, else
1017 *
1018 * @param q context
1019 * @param node pointer to node with packet
1020 */
1021 139 static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node)
1022 {
1023 GetBitContext gb;
1024
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (node) {
1026 init_get_bits(&gb, node->packet->data, node->packet->size * 8);
1027 init_tone_level_dequantization(q, &gb);
1028 fill_tone_level_array(q, 1);
1029 } else {
1030 139 fill_tone_level_array(q, 0);
1031 }
1032 139 }
1033
1034 /**
1035 * Process subpacket 11
1036 *
1037 * @param q context
1038 * @param node pointer to node with packet
1039 */
1040 139 static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node)
1041 {
1042 GetBitContext gb;
1043 139 int length = 0;
1044
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (node) {
1046 length = node->packet->size * 8;
1047 init_get_bits(&gb, node->packet->data, length);
1048 }
1049
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (length >= 32) {
1051 int c = get_bits(&gb, 13);
1052
1053 if (c > 3)
1054 fill_coding_method_array(q->tone_level_idx,
1055 q->tone_level_idx_temp, q->coding_method,
1056 q->nb_channels, 8 * c,
1057 q->superblocktype_2_3, q->cm_table_select);
1058 }
1059
1060 139 synthfilt_build_sb_samples(q, &gb, length, 0, 8);
1061 139 }
1062
1063 /**
1064 * Process subpacket 12
1065 *
1066 * @param q context
1067 * @param node pointer to node with packet
1068 */
1069 139 static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node)
1070 {
1071 GetBitContext gb;
1072 139 int length = 0;
1073
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (node) {
1075 length = node->packet->size * 8;
1076 init_get_bits(&gb, node->packet->data, length);
1077 }
1078
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
1080 139 }
1081
1082 /**
1083 * Process new subpackets for synthesis filter
1084 *
1085 * @param q context
1086 * @param list list with synthesis filter packets (list D)
1087 */
1088 139 static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
1089 {
1090 QDM2SubPNode *nodes[4];
1091
1092 139 nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
1093
1/2
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
139 if (nodes[0])
1094 139 process_subpacket_9(q, nodes[0]);
1095
1096 139 nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (nodes[1])
1098 process_subpacket_10(q, nodes[1]);
1099 else
1100 139 process_subpacket_10(q, NULL);
1101
1102 139 nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
1103
2/6
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 139 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
139 if (nodes[0] && nodes[1] && nodes[2])
1104 process_subpacket_11(q, nodes[2]);
1105 else
1106 139 process_subpacket_11(q, NULL);
1107
1108 139 nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
1109
2/6
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 139 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
139 if (nodes[0] && nodes[1] && nodes[3])
1110 process_subpacket_12(q, nodes[3]);
1111 else
1112 139 process_subpacket_12(q, NULL);
1113 139 }
1114
1115 /**
1116 * Decode superblock, fill packet lists.
1117 *
1118 * @param q context
1119 */
1120 139 static void qdm2_decode_super_block(QDM2Context *q)
1121 {
1122 GetBitContext gb;
1123 QDM2SubPacket header, *packet;
1124 int i, packet_bytes, sub_packet_size, sub_packets_D;
1125 139 unsigned int next_index = 0;
1126
1127 139 memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
1128 139 memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
1129 139 memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
1130
1131 139 q->sub_packets_B = 0;
1132 139 sub_packets_D = 0;
1133
1134 139 average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
1135
1136 139 init_get_bits(&gb, q->compressed_data, q->compressed_size * 8);
1137 139 qdm2_decode_sub_packet_header(&gb, &header);
1138
1139
2/4
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 139 times.
139 if (header.type < 2 || header.type >= 8) {
1140 q->has_errors = 1;
1141 av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
1142 return;
1143 }
1144
1145
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
139 q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
1146 139 packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
1147
1148 139 init_get_bits(&gb, header.data, header.size * 8);
1149
1150
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
139 if (header.type == 2 || header.type == 4 || header.type == 5) {
1151 139 int csum = 257 * get_bits(&gb, 8);
1152 139 csum += 2 * get_bits(&gb, 8);
1153
1154 139 csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
1155
1156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (csum != 0) {
1157 q->has_errors = 1;
1158 av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
1159 return;
1160 }
1161 }
1162
1163 139 q->sub_packet_list_B[0].packet = NULL;
1164 139 q->sub_packet_list_D[0].packet = NULL;
1165
1166
2/2
✓ Branch 0 taken 834 times.
✓ Branch 1 taken 139 times.
973 for (i = 0; i < 6; i++)
1167
1/2
✓ Branch 0 taken 834 times.
✗ Branch 1 not taken.
834 if (--q->fft_level_exp[i] < 0)
1168 834 q->fft_level_exp[i] = 0;
1169
1170
1/2
✓ Branch 0 taken 973 times.
✗ Branch 1 not taken.
973 for (i = 0; packet_bytes > 0; i++) {
1171 int j;
1172
1173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 973 times.
973 if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
1174 SAMPLES_NEEDED_2("too many packet bytes");
1175 return;
1176 }
1177
1178 973 q->sub_packet_list_A[i].next = NULL;
1179
1180
2/2
✓ Branch 0 taken 834 times.
✓ Branch 1 taken 139 times.
973 if (i > 0) {
1181 834 q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
1182
1183 /* seek to next block */
1184 834 init_get_bits(&gb, header.data, header.size * 8);
1185 834 skip_bits(&gb, next_index * 8);
1186
1187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 834 times.
834 if (next_index >= header.size)
1188 break;
1189 }
1190
1191 /* decode subpacket */
1192 973 packet = &q->sub_packets[i];
1193 973 qdm2_decode_sub_packet_header(&gb, packet);
1194 973 next_index = packet->size + get_bits_count(&gb) / 8;
1195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 973 times.
973 sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
1196
1197
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 834 times.
973 if (packet->type == 0)
1198 139 break;
1199
1200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 834 times.
834 if (sub_packet_size > packet_bytes) {
1201 if (packet->type != 10 && packet->type != 11 && packet->type != 12)
1202 break;
1203 packet->size += packet_bytes - sub_packet_size;
1204 }
1205
1206 834 packet_bytes -= sub_packet_size;
1207
1208 /* add subpacket to 'all subpackets' list */
1209 834 q->sub_packet_list_A[i].packet = packet;
1210
1211 /* add subpacket to related list */
1212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 834 times.
834 if (packet->type == 8) {
1213 SAMPLES_NEEDED_2("packet type 8");
1214 return;
1215
3/4
✓ Branch 0 taken 834 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 139 times.
✓ Branch 3 taken 695 times.
834 } else if (packet->type >= 9 && packet->type <= 12) {
1216 /* packets for MPEG Audio like Synthesis Filter */
1217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
1218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
695 } else if (packet->type == 13) {
1219 for (j = 0; j < 6; j++)
1220 q->fft_level_exp[j] = get_bits(&gb, 6);
1221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
695 } else if (packet->type == 14) {
1222 for (j = 0; j < 6; j++)
1223 q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
695 } else if (packet->type == 15) {
1225 SAMPLES_NEEDED_2("packet type 15")
1226 return;
1227
2/4
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 695 times.
✗ Branch 3 not taken.
695 } else if (packet->type >= 16 && packet->type < 48 &&
1228
1/2
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
695 !fft_subpackets[packet->type - 16]) {
1229 /* packets for FFT */
1230
2/2
✓ Branch 0 taken 556 times.
✓ Branch 1 taken 139 times.
695 QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
1231 }
1232 } // Packet bytes loop
1233
1234
1/2
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
139 if (q->sub_packet_list_D[0].packet) {
1235 139 process_synthesis_subpackets(q, q->sub_packet_list_D);
1236 139 q->do_synth_filter = 1;
1237 } else if (q->do_synth_filter) {
1238 process_subpacket_10(q, NULL);
1239 process_subpacket_11(q, NULL);
1240 process_subpacket_12(q, NULL);
1241 }
1242 }
1243
1244 20097 static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
1245 int offset, int duration, int channel,
1246 int exp, int phase)
1247 {
1248
2/2
✓ Branch 0 taken 545 times.
✓ Branch 1 taken 19552 times.
20097 if (q->fft_coefs_min_index[duration] < 0)
1249 545 q->fft_coefs_min_index[duration] = q->fft_coefs_index;
1250
1251
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 19809 times.
20097 q->fft_coefs[q->fft_coefs_index].sub_packet =
1252 288 ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
1253 20097 q->fft_coefs[q->fft_coefs_index].channel = channel;
1254 20097 q->fft_coefs[q->fft_coefs_index].offset = offset;
1255 20097 q->fft_coefs[q->fft_coefs_index].exp = exp;
1256 20097 q->fft_coefs[q->fft_coefs_index].phase = phase;
1257 20097 q->fft_coefs_index++;
1258 20097 }
1259
1260 556 static void qdm2_fft_decode_tones(QDM2Context *q, int duration,
1261 GetBitContext *gb, int b)
1262 {
1263 int channel, stereo, phase, exp;
1264 int local_int_4, local_int_8, stereo_phase, local_int_10;
1265 int local_int_14, stereo_exp, local_int_20, local_int_28;
1266 int n, offset;
1267
1268 556 local_int_4 = 0;
1269 556 local_int_28 = 0;
1270 556 local_int_20 = 2;
1271 556 local_int_8 = (4 - duration);
1272 556 local_int_10 = 1 << (q->group_order - duration - 1);
1273 556 offset = 1;
1274
1275
1/2
✓ Branch 1 taken 16211 times.
✗ Branch 2 not taken.
16211 while (get_bits_left(gb)>0) {
1276
1/2
✓ Branch 0 taken 16211 times.
✗ Branch 1 not taken.
16211 if (q->superblocktype_2_3) {
1277
2/2
✓ Branch 1 taken 2068 times.
✓ Branch 2 taken 16144 times.
18212 while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
1278
2/2
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 2001 times.
2068 if (get_bits_left(gb)<0) {
1279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
67 if(local_int_4 < q->group_size)
1280 av_log(NULL, AV_LOG_ERROR, "overread in qdm2_fft_decode_tones()\n");
1281 67 return;
1282 }
1283 2001 offset = 1;
1284
2/2
✓ Branch 0 taken 1989 times.
✓ Branch 1 taken 12 times.
2001 if (n == 0) {
1285 1989 local_int_4 += local_int_10;
1286 1989 local_int_28 += (1 << local_int_8);
1287 } else {
1288 12 local_int_4 += 8 * local_int_10;
1289 12 local_int_28 += (8 << local_int_8);
1290 }
1291 }
1292 16144 offset += (n - 2);
1293 } else {
1294 if (local_int_10 <= 2) {
1295 av_log(NULL, AV_LOG_ERROR, "qdm2_fft_decode_tones() stuck\n");
1296 return;
1297 }
1298 offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
1299 while (offset >= (local_int_10 - 1)) {
1300 offset += (1 - (local_int_10 - 1));
1301 local_int_4 += local_int_10;
1302 local_int_28 += (1 << local_int_8);
1303 }
1304 }
1305
1306
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 15655 times.
16144 if (local_int_4 >= q->group_size)
1307 489 return;
1308
1309 15655 local_int_14 = (offset >> local_int_8);
1310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15655 times.
15655 if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
1311 return;
1312
1313
1/2
✓ Branch 0 taken 15655 times.
✗ Branch 1 not taken.
15655 if (q->nb_channels > 1) {
1314 15655 channel = get_bits1(gb);
1315 15655 stereo = get_bits1(gb);
1316 } else {
1317 channel = 0;
1318 stereo = 0;
1319 }
1320
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15655 times.
15655 exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
1322 15655 exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
1323 15655 exp = (exp < 0) ? 0 : exp;
1324
1325 15655 phase = get_bits(gb, 3);
1326 15655 stereo_exp = 0;
1327 15655 stereo_phase = 0;
1328
1329
2/2
✓ Branch 0 taken 4442 times.
✓ Branch 1 taken 11213 times.
15655 if (stereo) {
1330 4442 stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
1331 4442 stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
1332
2/2
✓ Branch 0 taken 497 times.
✓ Branch 1 taken 3945 times.
4442 if (stereo_phase < 0)
1333 497 stereo_phase += 8;
1334 }
1335
1336
1/2
✓ Branch 0 taken 15655 times.
✗ Branch 1 not taken.
15655 if (q->frequency_range > (local_int_14 + 1)) {
1337 15655 int sub_packet = (local_int_20 + local_int_28);
1338
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15655 times.
15655 if (q->fft_coefs_index + stereo >= FF_ARRAY_ELEMS(q->fft_coefs))
1340 return;
1341
1342 15655 qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1343 channel, exp, phase);
1344
2/2
✓ Branch 0 taken 4442 times.
✓ Branch 1 taken 11213 times.
15655 if (stereo)
1345 4442 qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
1346 1 - channel,
1347 stereo_exp, stereo_phase);
1348 }
1349 15655 offset++;
1350 }
1351 }
1352
1353 139 static void qdm2_decode_fft_packets(QDM2Context *q)
1354 {
1355 int i, j, min, max, value, type, unknown_flag;
1356 GetBitContext gb;
1357
1358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if (!q->sub_packet_list_B[0].packet)
1359 return;
1360
1361 /* reset minimum indexes for FFT coefficients */
1362 139 q->fft_coefs_index = 0;
1363
2/2
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 139 times.
834 for (i = 0; i < 5; i++)
1364 695 q->fft_coefs_min_index[i] = -1;
1365
1366 /* process subpackets ordered by type, largest type first */
1367
2/2
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 139 times.
834 for (i = 0, max = 256; i < q->sub_packets_B; i++) {
1368 695 QDM2SubPacket *packet = NULL;
1369
1370 /* find subpacket with largest type less than max */
1371
2/2
✓ Branch 0 taken 3475 times.
✓ Branch 1 taken 695 times.
4170 for (j = 0, min = 0; j < q->sub_packets_B; j++) {
1372 3475 value = q->sub_packet_list_B[j].packet->type;
1373
4/4
✓ Branch 0 taken 2085 times.
✓ Branch 1 taken 1390 times.
✓ Branch 2 taken 695 times.
✓ Branch 3 taken 1390 times.
3475 if (value > min && value < max) {
1374 695 min = value;
1375 695 packet = q->sub_packet_list_B[j].packet;
1376 }
1377 }
1378
1379 695 max = min;
1380
1381 /* check for errors (?) */
1382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
695 if (!packet)
1383 return;
1384
1385
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 556 times.
695 if (i == 0 &&
1386
2/4
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
139 (packet->type < 16 || packet->type >= 48 ||
1387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 fft_subpackets[packet->type - 16]))
1388 return;
1389
1390 /* decode FFT tones */
1391 695 init_get_bits(&gb, packet->data, packet->size * 8);
1392
1393
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 695 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
695 if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
1394 unknown_flag = 1;
1395 else
1396 695 unknown_flag = 0;
1397
1398 695 type = packet->type;
1399
1400
2/8
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 695 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1390 if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
1401 695 int duration = q->sub_sampling + 5 - (type & 15);
1402
1403
3/4
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 556 times.
✓ Branch 3 taken 139 times.
695 if (duration >= 0 && duration < 4)
1404 556 qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
1405 } else if (type == 31) {
1406 for (j = 0; j < 4; j++)
1407 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1408 } else if (type == 46) {
1409 for (j = 0; j < 6; j++)
1410 q->fft_level_exp[j] = get_bits(&gb, 6);
1411 for (j = 0; j < 4; j++)
1412 qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
1413 }
1414 } // Loop on B packets
1415
1416 /* calculate maximum indexes for FFT coefficients */
1417
2/2
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 139 times.
834 for (i = 0, j = -1; i < 5; i++)
1418
2/2
✓ Branch 0 taken 545 times.
✓ Branch 1 taken 150 times.
695 if (q->fft_coefs_min_index[i] >= 0) {
1419
2/2
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 139 times.
545 if (j >= 0)
1420 406 q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
1421 545 j = i;
1422 }
1423
1/2
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
139 if (j >= 0)
1424 139 q->fft_coefs_max_index[j] = q->fft_coefs_index;
1425 }
1426
1427 341224 static void qdm2_fft_generate_tone(QDM2Context *q, FFTTone *tone)
1428 {
1429 float level, f[6];
1430 int i;
1431 QDM2Complex c;
1432 341224 const double iscale = 2.0 * M_PI / 512.0;
1433
1434 341224 tone->phase += tone->phase_shift;
1435
1436 /* calculate current level (maximum amplitude) of tone */
1437 341224 level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
1438 341224 c.im = level * sin(tone->phase * iscale);
1439 341224 c.re = level * cos(tone->phase * iscale);
1440
1441 /* generate FFT coefficients for tone */
1442
4/4
✓ Branch 0 taken 333235 times.
✓ Branch 1 taken 7989 times.
✓ Branch 2 taken 38528 times.
✓ Branch 3 taken 294707 times.
341224 if (tone->duration >= 3 || tone->cutoff >= 3) {
1443 46517 tone->complex[0].im += c.im;
1444 46517 tone->complex[0].re += c.re;
1445 46517 tone->complex[1].im -= c.im;
1446 46517 tone->complex[1].re -= c.re;
1447 } else {
1448 294707 f[1] = -tone->table[4];
1449 294707 f[0] = tone->table[3] - tone->table[0];
1450 294707 f[2] = 1.0 - tone->table[2] - tone->table[3];
1451 294707 f[3] = tone->table[1] + tone->table[4] - 1.0;
1452 294707 f[4] = tone->table[0] - tone->table[1];
1453 294707 f[5] = tone->table[2];
1454
2/2
✓ Branch 0 taken 589414 times.
✓ Branch 1 taken 294707 times.
884121 for (i = 0; i < 2; i++) {
1455 589414 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
1456 589414 c.re * f[i];
1457 1178828 tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
1458
2/2
✓ Branch 0 taken 65711 times.
✓ Branch 1 taken 523703 times.
589414 c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
1459 }
1460
2/2
✓ Branch 0 taken 1178828 times.
✓ Branch 1 taken 294707 times.
1473535 for (i = 0; i < 4; i++) {
1461 1178828 tone->complex[i].re += c.re * f[i + 2];
1462 1178828 tone->complex[i].im += c.im * f[i + 2];
1463 }
1464 }
1465
1466 /* copy the tone if it has not yet died out */
1467
2/2
✓ Branch 0 taken 321446 times.
✓ Branch 1 taken 19778 times.
341224 if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
1468 321446 memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
1469 321446 q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
1470 }
1471 341224 }
1472
1473 2224 static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
1474 {
1475 int i, j, ch;
1476 2224 const double iscale = 0.25 * M_PI;
1477
1478
2/2
✓ Branch 0 taken 4448 times.
✓ Branch 1 taken 2224 times.
6672 for (ch = 0; ch < q->channels; ch++) {
1479 4448 memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
1480 }
1481
1482
1483 /* apply FFT tones with duration 4 (1 FFT period) */
1484
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2220 times.
2224 if (q->fft_coefs_min_index[4] >= 0)
1485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
1486 float level;
1487 QDM2Complex c;
1488
1489 if (q->fft_coefs[i].sub_packet != sub_packet)
1490 break;
1491
1492 ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
1493 level = (q->fft_coefs[i].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[i].exp & 63];
1494
1495 c.re = level * cos(q->fft_coefs[i].phase * iscale);
1496 c.im = level * sin(q->fft_coefs[i].phase * iscale);
1497 q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
1498 q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
1499 q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
1500 q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
1501 }
1502
1503 /* generate existing FFT tones */
1504
2/2
✓ Branch 0 taken 321131 times.
✓ Branch 1 taken 2224 times.
323355 for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
1505 321131 qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
1506 321131 q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
1507 }
1508
1509 /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
1510
2/2
✓ Branch 0 taken 8896 times.
✓ Branch 1 taken 2224 times.
11120 for (i = 0; i < 4; i++)
1511
2/2
✓ Branch 0 taken 8724 times.
✓ Branch 1 taken 172 times.
8896 if (q->fft_coefs_min_index[i] >= 0) {
1512
2/2
✓ Branch 0 taken 24369 times.
✓ Branch 1 taken 4448 times.
28817 for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
1513 int offset, four_i;
1514 FFTTone tone;
1515
1516
2/2
✓ Branch 0 taken 4276 times.
✓ Branch 1 taken 20093 times.
24369 if (q->fft_coefs[j].sub_packet != sub_packet)
1517 4276 break;
1518
1519 20093 four_i = (4 - i);
1520 20093 offset = q->fft_coefs[j].offset >> four_i;
1521
1/2
✓ Branch 0 taken 20093 times.
✗ Branch 1 not taken.
20093 ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
1522
1523
1/2
✓ Branch 0 taken 20093 times.
✗ Branch 1 not taken.
20093 if (offset < q->frequency_range) {
1524
2/2
✓ Branch 0 taken 2785 times.
✓ Branch 1 taken 17308 times.
20093 if (offset < 2)
1525 2785 tone.cutoff = offset;
1526 else
1527
2/2
✓ Branch 0 taken 2532 times.
✓ Branch 1 taken 14776 times.
17308 tone.cutoff = (offset >= 60) ? 3 : 2;
1528
1529
1/2
✓ Branch 0 taken 20093 times.
✗ Branch 1 not taken.
20093 tone.level = (q->fft_coefs[j].exp < 0) ? 0.0 : fft_tone_level_table[q->superblocktype_2_3 ? 0 : 1][q->fft_coefs[j].exp & 63];
1530 20093 tone.complex = &q->fft.complex[ch][offset];
1531 20093 tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
1532 20093 tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
1533 20093 tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
1534 20093 tone.duration = i;
1535 20093 tone.time_index = 0;
1536
1537 20093 qdm2_fft_generate_tone(q, &tone);
1538 }
1539 }
1540 8724 q->fft_coefs_min_index[i] = j;
1541 }
1542 2224 }
1543
1544 4448 static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
1545 {
1546
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4448 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4448 const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
1547 4448 float *out = q->output_buffer + channel;
1548 int i;
1549 4448 q->fft.complex[channel][0].re *= 2.0f;
1550 4448 q->fft.complex[channel][0].im = 0.0f;
1551 4448 q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
1552 /* add samples to output buffer */
1553
2/2
✓ Branch 0 taken 1138688 times.
✓ Branch 1 taken 4448 times.
1143136 for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
1554 1138688 out[0] += q->fft.complex[channel][i].re * gain;
1555 1138688 out[q->channels] += q->fft.complex[channel][i].im * gain;
1556 1138688 out += 2 * q->channels;
1557 }
1558 4448 }
1559
1560 /**
1561 * @param q context
1562 * @param index subpacket number
1563 */
1564 2224 static void qdm2_synthesis_filter(QDM2Context *q, int index)
1565 {
1566 2224 int i, k, ch, sb_used, sub_sampling, dither_state = 0;
1567
1568 /* copy sb_samples */
1569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2224 times.
2224 sb_used = QDM2_SB_USED(q->sub_sampling);
1570
1571
2/2
✓ Branch 0 taken 4448 times.
✓ Branch 1 taken 2224 times.
6672 for (ch = 0; ch < q->channels; ch++)
1572
2/2
✓ Branch 0 taken 35584 times.
✓ Branch 1 taken 4448 times.
40032 for (i = 0; i < 8; i++)
1573
2/2
✓ Branch 0 taken 71168 times.
✓ Branch 1 taken 35584 times.
106752 for (k = sb_used; k < SBLIMIT; k++)
1574 71168 q->sb_samples[ch][(8 * index) + i][k] = 0;
1575
1576
2/2
✓ Branch 0 taken 4448 times.
✓ Branch 1 taken 2224 times.
6672 for (ch = 0; ch < q->nb_channels; ch++) {
1577 4448 float *samples_ptr = q->samples + ch;
1578
1579
2/2
✓ Branch 0 taken 35584 times.
✓ Branch 1 taken 4448 times.
40032 for (i = 0; i < 8; i++) {
1580 35584 ff_mpa_synth_filter_float(&q->mpadsp,
1581 35584 q->synth_buf[ch], &(q->synth_buf_offset[ch]),
1582 ff_mpa_synth_window_float, &dither_state,
1583 35584 samples_ptr, q->nb_channels,
1584 35584 q->sb_samples[ch][(8 * index) + i]);
1585 35584 samples_ptr += 32 * q->nb_channels;
1586 }
1587 }
1588
1589 /* add samples to output buffer */
1590 2224 sub_sampling = (4 >> q->sub_sampling);
1591
1592
2/2
✓ Branch 0 taken 4448 times.
✓ Branch 1 taken 2224 times.
6672 for (ch = 0; ch < q->channels; ch++)
1593
2/2
✓ Branch 0 taken 1138688 times.
✓ Branch 1 taken 4448 times.
1143136 for (i = 0; i < q->frame_size; i++)
1594 1138688 q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
1595 2224 }
1596
1597 /**
1598 * Init static data (does not depend on specific file)
1599 */
1600 1 static av_cold void qdm2_init_static_data(void) {
1601 1 qdm2_init_vlc();
1602 1 softclip_table_init();
1603 1 rnd_table_init();
1604 1 init_noise_samples();
1605
1606 1 ff_mpa_synth_init_float();
1607 1 }
1608
1609 /**
1610 * Init parameters from codec extradata
1611 */
1612 2 static av_cold int qdm2_decode_init(AVCodecContext *avctx)
1613 {
1614 static AVOnce init_static_once = AV_ONCE_INIT;
1615 2 QDM2Context *s = avctx->priv_data;
1616 int tmp_val, tmp, size;
1617 GetByteContext gb;
1618
1619 /* extradata parsing
1620
1621 Structure:
1622 wave {
1623 frma (QDM2)
1624 QDCA
1625 QDCP
1626 }
1627
1628 32 size (including this field)
1629 32 tag (=frma)
1630 32 type (=QDM2 or QDMC)
1631
1632 32 size (including this field, in bytes)
1633 32 tag (=QDCA) // maybe mandatory parameters
1634 32 unknown (=1)
1635 32 channels (=2)
1636 32 samplerate (=44100)
1637 32 bitrate (=96000)
1638 32 block size (=4096)
1639 32 frame size (=256) (for one channel)
1640 32 packet size (=1300)
1641
1642 32 size (including this field, in bytes)
1643 32 tag (=QDCP) // maybe some tuneable parameters
1644 32 float1 (=1.0)
1645 32 zero ?
1646 32 float2 (=1.0)
1647 32 float3 (=1.0)
1648 32 unknown (27)
1649 32 unknown (8)
1650 32 zero ?
1651 */
1652
1653
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!avctx->extradata || (avctx->extradata_size < 48)) {
1654 av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
1655 return AVERROR_INVALIDDATA;
1656 }
1657
1658 2 bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
1659
1660
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 while (bytestream2_get_bytes_left(&gb) > 8) {
1661
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 8 times.
10 if (bytestream2_peek_be64(&gb) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
1662 (uint64_t)MKBETAG('Q','D','M','2')))
1663 2 break;
1664 8 bytestream2_skip(&gb, 1);
1665 }
1666
1667
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(&gb) < 12) {
1668 av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
1669 bytestream2_get_bytes_left(&gb));
1670 return AVERROR_INVALIDDATA;
1671 }
1672
1673 2 bytestream2_skip(&gb, 8);
1674 2 size = bytestream2_get_be32(&gb);
1675
1676
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (size > bytestream2_get_bytes_left(&gb)) {
1677 av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
1678 bytestream2_get_bytes_left(&gb), size);
1679 return AVERROR_INVALIDDATA;
1680 }
1681
1682 2 av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
1683
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_be32(&gb) != MKBETAG('Q','D','C','A')) {
1684 av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
1685 return AVERROR_INVALIDDATA;
1686 }
1687
1688 2 bytestream2_skip(&gb, 4);
1689
1690 2 avctx->channels = s->nb_channels = s->channels = bytestream2_get_be32(&gb);
1691
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) {
1692 av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
1693 return AVERROR_INVALIDDATA;
1694 }
1695
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
1696 AV_CH_LAYOUT_MONO;
1697
1698 2 avctx->sample_rate = bytestream2_get_be32(&gb);
1699 2 avctx->bit_rate = bytestream2_get_be32(&gb);
1700 2 s->group_size = bytestream2_get_be32(&gb);
1701 2 s->fft_size = bytestream2_get_be32(&gb);
1702 2 s->checksum_size = bytestream2_get_be32(&gb);
1703
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (s->checksum_size >= 1U << 28 || s->checksum_size <= 1) {
1704 av_log(avctx, AV_LOG_ERROR, "data block size invalid (%u)\n", s->checksum_size);
1705 return AVERROR_INVALIDDATA;
1706 }
1707
1708 2 s->fft_order = av_log2(s->fft_size) + 1;
1709
1710 // Fail on unknown fft order
1711
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((s->fft_order < 7) || (s->fft_order > 9)) {
1712 avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order);
1713 return AVERROR_PATCHWELCOME;
1714 }
1715
1716 // something like max decodable tones
1717 2 s->group_order = av_log2(s->group_size) + 1;
1718 2 s->frame_size = s->group_size / 16; // 16 iterations per super block
1719
1720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->frame_size > QDM2_MAX_FRAME_SIZE)
1721 return AVERROR_INVALIDDATA;
1722
1723 2 s->sub_sampling = s->fft_order - 7;
1724 2 s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
1725
1726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->frame_size * 4 >> s->sub_sampling > MPA_FRAME_SIZE) {
1727 avpriv_request_sample(avctx, "large frames");
1728 return AVERROR_PATCHWELCOME;
1729 }
1730
1731
1/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
2 switch ((s->sub_sampling * 2 + s->channels - 1)) {
1732 case 0: tmp = 40; break;
1733 case 1: tmp = 48; break;
1734 case 2: tmp = 56; break;
1735 case 3: tmp = 72; break;
1736 case 4: tmp = 80; break;
1737 2 case 5: tmp = 100;break;
1738 default: tmp=s->sub_sampling; break;
1739 }
1740 2 tmp_val = 0;
1741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
1743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
1744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
1745 2 s->cm_table_select = tmp_val;
1746
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->bit_rate <= 8000)
1748 s->coeff_per_sb_select = 0;
1749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 else if (avctx->bit_rate < 16000)
1750 s->coeff_per_sb_select = 1;
1751 else
1752 2 s->coeff_per_sb_select = 2;
1753
1754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->fft_size != (1 << (s->fft_order - 1))) {
1755 av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
1756 return AVERROR_INVALIDDATA;
1757 }
1758
1759 2 ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
1760 2 ff_mpadsp_init(&s->mpadsp);
1761
1762 2 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1763
1764 2 ff_thread_once(&init_static_once, qdm2_init_static_data);
1765
1766 2 return 0;
1767 }
1768
1769 2 static av_cold int qdm2_decode_close(AVCodecContext *avctx)
1770 {
1771 2 QDM2Context *s = avctx->priv_data;
1772
1773 2 ff_rdft_end(&s->rdft_ctx);
1774
1775 2 return 0;
1776 }
1777
1778 2224 static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
1779 {
1780 int ch, i;
1781 2224 const int frame_size = (q->frame_size * q->channels);
1782
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2224 times.
2224 if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
1784 return -1;
1785
1786 /* select input buffer */
1787 2224 q->compressed_data = in;
1788 2224 q->compressed_size = q->checksum_size;
1789
1790 /* copy old block, clear new block of output samples */
1791 2224 memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
1792 2224 memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
1793
1794 /* decode block of QDM2 compressed data */
1795
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 2085 times.
2224 if (q->sub_packet == 0) {
1796 139 q->has_errors = 0; // zero it for a new super block
1797 139 av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
1798 139 qdm2_decode_super_block(q);
1799 }
1800
1801 /* parse subpackets */
1802
1/2
✓ Branch 0 taken 2224 times.
✗ Branch 1 not taken.
2224 if (!q->has_errors) {
1803
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 2085 times.
2224 if (q->sub_packet == 2)
1804 139 qdm2_decode_fft_packets(q);
1805
1806 2224 qdm2_fft_tone_synthesizer(q, q->sub_packet);
1807 }
1808
1809 /* sound synthesis stage 1 (FFT) */
1810
2/2
✓ Branch 0 taken 4448 times.
✓ Branch 1 taken 2224 times.
6672 for (ch = 0; ch < q->channels; ch++) {
1811 4448 qdm2_calculate_fft(q, ch, q->sub_packet);
1812
1813
2/4
✓ Branch 0 taken 4448 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4448 times.
4448 if (!q->has_errors && q->sub_packet_list_C[0].packet) {
1814 SAMPLES_NEEDED_2("has errors, and C list is not empty")
1815 return -1;
1816 }
1817 }
1818
1819 /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
1820
2/4
✓ Branch 0 taken 2224 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2224 times.
✗ Branch 3 not taken.
2224 if (!q->has_errors && q->do_synth_filter)
1821 2224 qdm2_synthesis_filter(q, q->sub_packet);
1822
1823 2224 q->sub_packet = (q->sub_packet + 1) % 16;
1824
1825 /* clip and convert output float[] to 16-bit signed samples */
1826
2/2
✓ Branch 0 taken 1138688 times.
✓ Branch 1 taken 2224 times.
1140912 for (i = 0; i < frame_size; i++) {
1827 1138688 int value = (int)q->output_buffer[i];
1828
1829
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 1138443 times.
1138688 if (value > SOFTCLIP_THRESHOLD)
1830
2/2
✓ Branch 0 taken 244 times.
✓ Branch 1 taken 1 times.
245 value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
1831
2/2
✓ Branch 0 taken 839 times.
✓ Branch 1 taken 1137604 times.
1138443 else if (value < -SOFTCLIP_THRESHOLD)
1832
2/2
✓ Branch 0 taken 813 times.
✓ Branch 1 taken 26 times.
839 value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
1833
1834 1138688 out[i] = value;
1835 }
1836
1837 2224 return 0;
1838 }
1839
1840 139 static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
1841 int *got_frame_ptr, AVPacket *avpkt)
1842 {
1843 139 AVFrame *frame = data;
1844 139 const uint8_t *buf = avpkt->data;
1845 139 int buf_size = avpkt->size;
1846 139 QDM2Context *s = avctx->priv_data;
1847 int16_t *out;
1848 int i, ret;
1849
1850
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if(!buf)
1851 return 0;
1852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139 times.
139 if(buf_size < s->checksum_size)
1853 return -1;
1854
1855 /* get output buffer */
1856 139 frame->nb_samples = 16 * s->frame_size;
1857
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 139 times.
139 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1858 return ret;
1859 139 out = (int16_t *)frame->data[0];
1860
1861
2/2
✓ Branch 0 taken 2224 times.
✓ Branch 1 taken 139 times.
2363 for (i = 0; i < 16; i++) {
1862
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2224 times.
2224 if ((ret = qdm2_decode(s, buf, out)) < 0)
1863 return ret;
1864 2224 out += s->channels * s->frame_size;
1865 }
1866
1867 139 *got_frame_ptr = 1;
1868
1869 139 return s->checksum_size;
1870 }
1871
1872 const AVCodec ff_qdm2_decoder = {
1873 .name = "qdm2",
1874 .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
1875 .type = AVMEDIA_TYPE_AUDIO,
1876 .id = AV_CODEC_ID_QDM2,
1877 .priv_data_size = sizeof(QDM2Context),
1878 .init = qdm2_decode_init,
1879 .close = qdm2_decode_close,
1880 .decode = qdm2_decode_frame,
1881 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1882 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1883 };
1884