You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1893 lines
63KB

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