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. #define BITSTREAM_READER_LE
  36. #include "libavutil/channel_layout.h"
  37. #include "avcodec.h"
  38. #include "get_bits.h"
  39. #include "internal.h"
  40. #include "rdft.h"
  41. #include "mpegaudiodsp.h"
  42. #include "mpegaudio.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 checksum'ed
  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. *
  571. * Called by process_subpacket_11 to process more data from subpacket 11
  572. * with sb 0-8.
  573. * Called by process_subpacket_12 to process data from subpacket 12 with
  574. * sb 8-sb_used.
  575. *
  576. * @param q context
  577. * @param gb bitreader context
  578. * @param length packet length in bits
  579. * @param sb_min lower subband processed (sb_min included)
  580. * @param sb_max higher subband processed (sb_max excluded)
  581. */
  582. static int synthfilt_build_sb_samples(QDM2Context *q, GetBitContext *gb,
  583. int length, int sb_min, int sb_max)
  584. {
  585. int sb, j, k, n, ch, run, channels;
  586. int joined_stereo, zero_encoding;
  587. int type34_first;
  588. float type34_div = 0;
  589. float type34_predictor;
  590. float samples[10];
  591. int sign_bits[16] = {0};
  592. if (length == 0) {
  593. // If no data use noise
  594. for (sb=sb_min; sb < sb_max; sb++)
  595. build_sb_samples_from_noise(q, sb);
  596. return 0;
  597. }
  598. for (sb = sb_min; sb < sb_max; sb++) {
  599. channels = q->nb_channels;
  600. if (q->nb_channels <= 1 || sb < 12)
  601. joined_stereo = 0;
  602. else if (sb >= 24)
  603. joined_stereo = 1;
  604. else
  605. joined_stereo = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
  606. if (joined_stereo) {
  607. if (get_bits_left(gb) >= 16)
  608. for (j = 0; j < 16; j++)
  609. sign_bits[j] = get_bits1(gb);
  610. for (j = 0; j < 64; j++)
  611. if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])
  612. q->coding_method[0][sb][j] = q->coding_method[1][sb][j];
  613. if (fix_coding_method_array(sb, q->nb_channels,
  614. q->coding_method)) {
  615. av_log(NULL, AV_LOG_ERROR, "coding method invalid\n");
  616. build_sb_samples_from_noise(q, sb);
  617. continue;
  618. }
  619. channels = 1;
  620. }
  621. for (ch = 0; ch < channels; ch++) {
  622. FIX_NOISE_IDX(q->noise_idx);
  623. zero_encoding = (get_bits_left(gb) >= 1) ? get_bits1(gb) : 0;
  624. type34_predictor = 0.0;
  625. type34_first = 1;
  626. for (j = 0; j < 128; ) {
  627. switch (q->coding_method[ch][sb][j / 2]) {
  628. case 8:
  629. if (get_bits_left(gb) >= 10) {
  630. if (zero_encoding) {
  631. for (k = 0; k < 5; k++) {
  632. if ((j + 2 * k) >= 128)
  633. break;
  634. samples[2 * k] = get_bits1(gb) ? dequant_1bit[joined_stereo][2 * get_bits1(gb)] : 0;
  635. }
  636. } else {
  637. n = get_bits(gb, 8);
  638. if (n >= 243) {
  639. av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
  640. return AVERROR_INVALIDDATA;
  641. }
  642. for (k = 0; k < 5; k++)
  643. samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
  644. }
  645. for (k = 0; k < 5; k++)
  646. samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);
  647. } else {
  648. for (k = 0; k < 10; k++)
  649. samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
  650. }
  651. run = 10;
  652. break;
  653. case 10:
  654. if (get_bits_left(gb) >= 1) {
  655. float f = 0.81;
  656. if (get_bits1(gb))
  657. f = -f;
  658. f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;
  659. samples[0] = f;
  660. } else {
  661. samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
  662. }
  663. run = 1;
  664. break;
  665. case 16:
  666. if (get_bits_left(gb) >= 10) {
  667. if (zero_encoding) {
  668. for (k = 0; k < 5; k++) {
  669. if ((j + k) >= 128)
  670. break;
  671. samples[k] = (get_bits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * get_bits1(gb)];
  672. }
  673. } else {
  674. n = get_bits (gb, 8);
  675. if (n >= 243) {
  676. av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
  677. return AVERROR_INVALIDDATA;
  678. }
  679. for (k = 0; k < 5; k++)
  680. samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
  681. }
  682. } else {
  683. for (k = 0; k < 5; k++)
  684. samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
  685. }
  686. run = 5;
  687. break;
  688. case 24:
  689. if (get_bits_left(gb) >= 7) {
  690. n = get_bits(gb, 7);
  691. if (n >= 125) {
  692. av_log(NULL, AV_LOG_ERROR, "Invalid 7bit codeword\n");
  693. return AVERROR_INVALIDDATA;
  694. }
  695. for (k = 0; k < 3; k++)
  696. samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
  697. } else {
  698. for (k = 0; k < 3; k++)
  699. samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);
  700. }
  701. run = 3;
  702. break;
  703. case 30:
  704. if (get_bits_left(gb) >= 4) {
  705. unsigned index = qdm2_get_vlc(gb, &vlc_tab_type30, 0, 1);
  706. if (index >= FF_ARRAY_ELEMS(type30_dequant)) {
  707. av_log(NULL, AV_LOG_ERROR, "index %d out of type30_dequant array\n", index);
  708. return AVERROR_INVALIDDATA;
  709. }
  710. samples[0] = type30_dequant[index];
  711. } else
  712. samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
  713. run = 1;
  714. break;
  715. case 34:
  716. if (get_bits_left(gb) >= 7) {
  717. if (type34_first) {
  718. type34_div = (float)(1 << get_bits(gb, 2));
  719. samples[0] = ((float)get_bits(gb, 5) - 16.0) / 15.0;
  720. type34_predictor = samples[0];
  721. type34_first = 0;
  722. } else {
  723. unsigned index = qdm2_get_vlc(gb, &vlc_tab_type34, 0, 1);
  724. if (index >= FF_ARRAY_ELEMS(type34_delta)) {
  725. av_log(NULL, AV_LOG_ERROR, "index %d out of type34_delta array\n", index);
  726. return AVERROR_INVALIDDATA;
  727. }
  728. samples[0] = type34_delta[index] / type34_div + type34_predictor;
  729. type34_predictor = samples[0];
  730. }
  731. } else {
  732. samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
  733. }
  734. run = 1;
  735. break;
  736. default:
  737. samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);
  738. run = 1;
  739. break;
  740. }
  741. if (joined_stereo) {
  742. for (k = 0; k < run && j + k < 128; k++) {
  743. q->sb_samples[0][j + k][sb] =
  744. q->tone_level[0][sb][(j + k) / 2] * samples[k];
  745. if (q->nb_channels == 2) {
  746. if (sign_bits[(j + k) / 8])
  747. q->sb_samples[1][j + k][sb] =
  748. q->tone_level[1][sb][(j + k) / 2] * -samples[k];
  749. else
  750. q->sb_samples[1][j + k][sb] =
  751. q->tone_level[1][sb][(j + k) / 2] * samples[k];
  752. }
  753. }
  754. } else {
  755. for (k = 0; k < run; k++)
  756. if ((j + k) < 128)
  757. q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];
  758. }
  759. j += run;
  760. } // j loop
  761. } // channel loop
  762. } // subband loop
  763. return 0;
  764. }
  765. /**
  766. * Init the first element of a channel in quantized_coeffs with data
  767. * from packet 10 (quantized_coeffs[ch][0]).
  768. * This is similar to process_subpacket_9, but for a single channel
  769. * and for element [0]
  770. * same VLC tables as process_subpacket_9 are used.
  771. *
  772. * @param quantized_coeffs pointer to quantized_coeffs[ch][0]
  773. * @param gb bitreader context
  774. */
  775. static int init_quantized_coeffs_elem0(int8_t *quantized_coeffs,
  776. GetBitContext *gb)
  777. {
  778. int i, k, run, level, diff;
  779. if (get_bits_left(gb) < 16)
  780. return -1;
  781. level = qdm2_get_vlc(gb, &vlc_tab_level, 0, 2);
  782. quantized_coeffs[0] = level;
  783. for (i = 0; i < 7; ) {
  784. if (get_bits_left(gb) < 16)
  785. return -1;
  786. run = qdm2_get_vlc(gb, &vlc_tab_run, 0, 1) + 1;
  787. if (i + run >= 8)
  788. return -1;
  789. if (get_bits_left(gb) < 16)
  790. return -1;
  791. diff = qdm2_get_se_vlc(&vlc_tab_diff, gb, 2);
  792. for (k = 1; k <= run; k++)
  793. quantized_coeffs[i + k] = (level + ((k * diff) / run));
  794. level += diff;
  795. i += run;
  796. }
  797. return 0;
  798. }
  799. /**
  800. * Related to synthesis filter, process data from packet 10
  801. * Init part of quantized_coeffs via function init_quantized_coeffs_elem0
  802. * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with
  803. * data from packet 10
  804. *
  805. * @param q context
  806. * @param gb bitreader context
  807. */
  808. static void init_tone_level_dequantization(QDM2Context *q, GetBitContext *gb)
  809. {
  810. int sb, j, k, n, ch;
  811. for (ch = 0; ch < q->nb_channels; ch++) {
  812. init_quantized_coeffs_elem0(q->quantized_coeffs[ch][0], gb);
  813. if (get_bits_left(gb) < 16) {
  814. memset(q->quantized_coeffs[ch][0], 0, 8);
  815. break;
  816. }
  817. }
  818. n = q->sub_sampling + 1;
  819. for (sb = 0; sb < n; sb++)
  820. for (ch = 0; ch < q->nb_channels; ch++)
  821. for (j = 0; j < 8; j++) {
  822. if (get_bits_left(gb) < 1)
  823. break;
  824. if (get_bits1(gb)) {
  825. for (k=0; k < 8; k++) {
  826. if (get_bits_left(gb) < 16)
  827. break;
  828. q->tone_level_idx_hi1[ch][sb][j][k] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi1, 0, 2);
  829. }
  830. } else {
  831. for (k=0; k < 8; k++)
  832. q->tone_level_idx_hi1[ch][sb][j][k] = 0;
  833. }
  834. }
  835. n = QDM2_SB_USED(q->sub_sampling) - 4;
  836. for (sb = 0; sb < n; sb++)
  837. for (ch = 0; ch < q->nb_channels; ch++) {
  838. if (get_bits_left(gb) < 16)
  839. break;
  840. q->tone_level_idx_hi2[ch][sb] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_hi2, 0, 2);
  841. if (sb > 19)
  842. q->tone_level_idx_hi2[ch][sb] -= 16;
  843. else
  844. for (j = 0; j < 8; j++)
  845. q->tone_level_idx_mid[ch][sb][j] = -16;
  846. }
  847. n = QDM2_SB_USED(q->sub_sampling) - 5;
  848. for (sb = 0; sb < n; sb++)
  849. for (ch = 0; ch < q->nb_channels; ch++)
  850. for (j = 0; j < 8; j++) {
  851. if (get_bits_left(gb) < 16)
  852. break;
  853. q->tone_level_idx_mid[ch][sb][j] = qdm2_get_vlc(gb, &vlc_tab_tone_level_idx_mid, 0, 2) - 32;
  854. }
  855. }
  856. /**
  857. * Process subpacket 9, init quantized_coeffs with data from it
  858. *
  859. * @param q context
  860. * @param node pointer to node with packet
  861. */
  862. static int process_subpacket_9(QDM2Context *q, QDM2SubPNode *node)
  863. {
  864. GetBitContext gb;
  865. int i, j, k, n, ch, run, level, diff;
  866. init_get_bits(&gb, node->packet->data, node->packet->size * 8);
  867. n = coeff_per_sb_for_avg[q->coeff_per_sb_select][QDM2_SB_USED(q->sub_sampling) - 1] + 1;
  868. for (i = 1; i < n; i++)
  869. for (ch = 0; ch < q->nb_channels; ch++) {
  870. level = qdm2_get_vlc(&gb, &vlc_tab_level, 0, 2);
  871. q->quantized_coeffs[ch][i][0] = level;
  872. for (j = 0; j < (8 - 1); ) {
  873. run = qdm2_get_vlc(&gb, &vlc_tab_run, 0, 1) + 1;
  874. diff = qdm2_get_se_vlc(&vlc_tab_diff, &gb, 2);
  875. if (j + run >= 8)
  876. return -1;
  877. for (k = 1; k <= run; k++)
  878. q->quantized_coeffs[ch][i][j + k] = (level + ((k * diff) / run));
  879. level += diff;
  880. j += run;
  881. }
  882. }
  883. for (ch = 0; ch < q->nb_channels; ch++)
  884. for (i = 0; i < 8; i++)
  885. q->quantized_coeffs[ch][0][i] = 0;
  886. return 0;
  887. }
  888. /**
  889. * Process subpacket 10 if not null, else
  890. *
  891. * @param q context
  892. * @param node pointer to node with packet
  893. */
  894. static void process_subpacket_10(QDM2Context *q, QDM2SubPNode *node)
  895. {
  896. GetBitContext gb;
  897. if (node) {
  898. init_get_bits(&gb, node->packet->data, node->packet->size * 8);
  899. init_tone_level_dequantization(q, &gb);
  900. fill_tone_level_array(q, 1);
  901. } else {
  902. fill_tone_level_array(q, 0);
  903. }
  904. }
  905. /**
  906. * Process subpacket 11
  907. *
  908. * @param q context
  909. * @param node pointer to node with packet
  910. */
  911. static void process_subpacket_11(QDM2Context *q, QDM2SubPNode *node)
  912. {
  913. GetBitContext gb;
  914. int length = 0;
  915. if (node) {
  916. length = node->packet->size * 8;
  917. init_get_bits(&gb, node->packet->data, length);
  918. }
  919. if (length >= 32) {
  920. int c = get_bits(&gb, 13);
  921. if (c > 3)
  922. fill_coding_method_array(q->tone_level_idx,
  923. q->tone_level_idx_temp, q->coding_method,
  924. q->nb_channels, 8 * c,
  925. q->superblocktype_2_3, q->cm_table_select);
  926. }
  927. synthfilt_build_sb_samples(q, &gb, length, 0, 8);
  928. }
  929. /**
  930. * Process subpacket 12
  931. *
  932. * @param q context
  933. * @param node pointer to node with packet
  934. */
  935. static void process_subpacket_12(QDM2Context *q, QDM2SubPNode *node)
  936. {
  937. GetBitContext gb;
  938. int length = 0;
  939. if (node) {
  940. length = node->packet->size * 8;
  941. init_get_bits(&gb, node->packet->data, length);
  942. }
  943. synthfilt_build_sb_samples(q, &gb, length, 8, QDM2_SB_USED(q->sub_sampling));
  944. }
  945. /**
  946. * Process new subpackets for synthesis filter
  947. *
  948. * @param q context
  949. * @param list list with synthesis filter packets (list D)
  950. */
  951. static void process_synthesis_subpackets(QDM2Context *q, QDM2SubPNode *list)
  952. {
  953. QDM2SubPNode *nodes[4];
  954. nodes[0] = qdm2_search_subpacket_type_in_list(list, 9);
  955. if (nodes[0])
  956. process_subpacket_9(q, nodes[0]);
  957. nodes[1] = qdm2_search_subpacket_type_in_list(list, 10);
  958. if (nodes[1])
  959. process_subpacket_10(q, nodes[1]);
  960. else
  961. process_subpacket_10(q, NULL);
  962. nodes[2] = qdm2_search_subpacket_type_in_list(list, 11);
  963. if (nodes[0] && nodes[1] && nodes[2])
  964. process_subpacket_11(q, nodes[2]);
  965. else
  966. process_subpacket_11(q, NULL);
  967. nodes[3] = qdm2_search_subpacket_type_in_list(list, 12);
  968. if (nodes[0] && nodes[1] && nodes[3])
  969. process_subpacket_12(q, nodes[3]);
  970. else
  971. process_subpacket_12(q, NULL);
  972. }
  973. /**
  974. * Decode superblock, fill packet lists.
  975. *
  976. * @param q context
  977. */
  978. static void qdm2_decode_super_block(QDM2Context *q)
  979. {
  980. GetBitContext gb;
  981. QDM2SubPacket header, *packet;
  982. int i, packet_bytes, sub_packet_size, sub_packets_D;
  983. unsigned int next_index = 0;
  984. memset(q->tone_level_idx_hi1, 0, sizeof(q->tone_level_idx_hi1));
  985. memset(q->tone_level_idx_mid, 0, sizeof(q->tone_level_idx_mid));
  986. memset(q->tone_level_idx_hi2, 0, sizeof(q->tone_level_idx_hi2));
  987. q->sub_packets_B = 0;
  988. sub_packets_D = 0;
  989. average_quantized_coeffs(q); // average elements in quantized_coeffs[max_ch][10][8]
  990. init_get_bits(&gb, q->compressed_data, q->compressed_size * 8);
  991. qdm2_decode_sub_packet_header(&gb, &header);
  992. if (header.type < 2 || header.type >= 8) {
  993. q->has_errors = 1;
  994. av_log(NULL, AV_LOG_ERROR, "bad superblock type\n");
  995. return;
  996. }
  997. q->superblocktype_2_3 = (header.type == 2 || header.type == 3);
  998. packet_bytes = (q->compressed_size - get_bits_count(&gb) / 8);
  999. init_get_bits(&gb, header.data, header.size * 8);
  1000. if (header.type == 2 || header.type == 4 || header.type == 5) {
  1001. int csum = 257 * get_bits(&gb, 8);
  1002. csum += 2 * get_bits(&gb, 8);
  1003. csum = qdm2_packet_checksum(q->compressed_data, q->checksum_size, csum);
  1004. if (csum != 0) {
  1005. q->has_errors = 1;
  1006. av_log(NULL, AV_LOG_ERROR, "bad packet checksum\n");
  1007. return;
  1008. }
  1009. }
  1010. q->sub_packet_list_B[0].packet = NULL;
  1011. q->sub_packet_list_D[0].packet = NULL;
  1012. for (i = 0; i < 6; i++)
  1013. if (--q->fft_level_exp[i] < 0)
  1014. q->fft_level_exp[i] = 0;
  1015. for (i = 0; packet_bytes > 0; i++) {
  1016. int j;
  1017. if (i >= FF_ARRAY_ELEMS(q->sub_packet_list_A)) {
  1018. SAMPLES_NEEDED_2("too many packet bytes");
  1019. return;
  1020. }
  1021. q->sub_packet_list_A[i].next = NULL;
  1022. if (i > 0) {
  1023. q->sub_packet_list_A[i - 1].next = &q->sub_packet_list_A[i];
  1024. /* seek to next block */
  1025. init_get_bits(&gb, header.data, header.size * 8);
  1026. skip_bits(&gb, next_index * 8);
  1027. if (next_index >= header.size)
  1028. break;
  1029. }
  1030. /* decode subpacket */
  1031. packet = &q->sub_packets[i];
  1032. qdm2_decode_sub_packet_header(&gb, packet);
  1033. next_index = packet->size + get_bits_count(&gb) / 8;
  1034. sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2;
  1035. if (packet->type == 0)
  1036. break;
  1037. if (sub_packet_size > packet_bytes) {
  1038. if (packet->type != 10 && packet->type != 11 && packet->type != 12)
  1039. break;
  1040. packet->size += packet_bytes - sub_packet_size;
  1041. }
  1042. packet_bytes -= sub_packet_size;
  1043. /* add subpacket to 'all subpackets' list */
  1044. q->sub_packet_list_A[i].packet = packet;
  1045. /* add subpacket to related list */
  1046. if (packet->type == 8) {
  1047. SAMPLES_NEEDED_2("packet type 8");
  1048. return;
  1049. } else if (packet->type >= 9 && packet->type <= 12) {
  1050. /* packets for MPEG Audio like Synthesis Filter */
  1051. QDM2_LIST_ADD(q->sub_packet_list_D, sub_packets_D, packet);
  1052. } else if (packet->type == 13) {
  1053. for (j = 0; j < 6; j++)
  1054. q->fft_level_exp[j] = get_bits(&gb, 6);
  1055. } else if (packet->type == 14) {
  1056. for (j = 0; j < 6; j++)
  1057. q->fft_level_exp[j] = qdm2_get_vlc(&gb, &fft_level_exp_vlc, 0, 2);
  1058. } else if (packet->type == 15) {
  1059. SAMPLES_NEEDED_2("packet type 15")
  1060. return;
  1061. } else if (packet->type >= 16 && packet->type < 48 &&
  1062. !fft_subpackets[packet->type - 16]) {
  1063. /* packets for FFT */
  1064. QDM2_LIST_ADD(q->sub_packet_list_B, q->sub_packets_B, packet);
  1065. }
  1066. } // Packet bytes loop
  1067. if (q->sub_packet_list_D[0].packet) {
  1068. process_synthesis_subpackets(q, q->sub_packet_list_D);
  1069. q->do_synth_filter = 1;
  1070. } else if (q->do_synth_filter) {
  1071. process_subpacket_10(q, NULL);
  1072. process_subpacket_11(q, NULL);
  1073. process_subpacket_12(q, NULL);
  1074. }
  1075. }
  1076. static void qdm2_fft_init_coefficient(QDM2Context *q, int sub_packet,
  1077. int offset, int duration, int channel,
  1078. int exp, int phase)
  1079. {
  1080. if (q->fft_coefs_min_index[duration] < 0)
  1081. q->fft_coefs_min_index[duration] = q->fft_coefs_index;
  1082. q->fft_coefs[q->fft_coefs_index].sub_packet =
  1083. ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet);
  1084. q->fft_coefs[q->fft_coefs_index].channel = channel;
  1085. q->fft_coefs[q->fft_coefs_index].offset = offset;
  1086. q->fft_coefs[q->fft_coefs_index].exp = exp;
  1087. q->fft_coefs[q->fft_coefs_index].phase = phase;
  1088. q->fft_coefs_index++;
  1089. }
  1090. static void qdm2_fft_decode_tones(QDM2Context *q, int duration,
  1091. GetBitContext *gb, int b)
  1092. {
  1093. int channel, stereo, phase, exp;
  1094. int local_int_4, local_int_8, stereo_phase, local_int_10;
  1095. int local_int_14, stereo_exp, local_int_20, local_int_28;
  1096. int n, offset;
  1097. local_int_4 = 0;
  1098. local_int_28 = 0;
  1099. local_int_20 = 2;
  1100. local_int_8 = (4 - duration);
  1101. local_int_10 = 1 << (q->group_order - duration - 1);
  1102. offset = 1;
  1103. while (get_bits_left(gb)>0) {
  1104. if (q->superblocktype_2_3) {
  1105. while ((n = qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2)) < 2) {
  1106. if (get_bits_left(gb)<0) {
  1107. if(local_int_4 < q->group_size)
  1108. av_log(NULL, AV_LOG_ERROR, "overread in qdm2_fft_decode_tones()\n");
  1109. return;
  1110. }
  1111. offset = 1;
  1112. if (n == 0) {
  1113. local_int_4 += local_int_10;
  1114. local_int_28 += (1 << local_int_8);
  1115. } else {
  1116. local_int_4 += 8 * local_int_10;
  1117. local_int_28 += (8 << local_int_8);
  1118. }
  1119. }
  1120. offset += (n - 2);
  1121. } else {
  1122. offset += qdm2_get_vlc(gb, &vlc_tab_fft_tone_offset[local_int_8], 1, 2);
  1123. while (offset >= (local_int_10 - 1)) {
  1124. offset += (1 - (local_int_10 - 1));
  1125. local_int_4 += local_int_10;
  1126. local_int_28 += (1 << local_int_8);
  1127. }
  1128. }
  1129. if (local_int_4 >= q->group_size)
  1130. return;
  1131. local_int_14 = (offset >> local_int_8);
  1132. if (local_int_14 >= FF_ARRAY_ELEMS(fft_level_index_table))
  1133. return;
  1134. if (q->nb_channels > 1) {
  1135. channel = get_bits1(gb);
  1136. stereo = get_bits1(gb);
  1137. } else {
  1138. channel = 0;
  1139. stereo = 0;
  1140. }
  1141. exp = qdm2_get_vlc(gb, (b ? &fft_level_exp_vlc : &fft_level_exp_alt_vlc), 0, 2);
  1142. exp += q->fft_level_exp[fft_level_index_table[local_int_14]];
  1143. exp = (exp < 0) ? 0 : exp;
  1144. phase = get_bits(gb, 3);
  1145. stereo_exp = 0;
  1146. stereo_phase = 0;
  1147. if (stereo) {
  1148. stereo_exp = (exp - qdm2_get_vlc(gb, &fft_stereo_exp_vlc, 0, 1));
  1149. stereo_phase = (phase - qdm2_get_vlc(gb, &fft_stereo_phase_vlc, 0, 1));
  1150. if (stereo_phase < 0)
  1151. stereo_phase += 8;
  1152. }
  1153. if (q->frequency_range > (local_int_14 + 1)) {
  1154. int sub_packet = (local_int_20 + local_int_28);
  1155. qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
  1156. channel, exp, phase);
  1157. if (stereo)
  1158. qdm2_fft_init_coefficient(q, sub_packet, offset, duration,
  1159. 1 - channel,
  1160. stereo_exp, stereo_phase);
  1161. }
  1162. offset++;
  1163. }
  1164. }
  1165. static void qdm2_decode_fft_packets(QDM2Context *q)
  1166. {
  1167. int i, j, min, max, value, type, unknown_flag;
  1168. GetBitContext gb;
  1169. if (!q->sub_packet_list_B[0].packet)
  1170. return;
  1171. /* reset minimum indexes for FFT coefficients */
  1172. q->fft_coefs_index = 0;
  1173. for (i = 0; i < 5; i++)
  1174. q->fft_coefs_min_index[i] = -1;
  1175. /* process subpackets ordered by type, largest type first */
  1176. for (i = 0, max = 256; i < q->sub_packets_B; i++) {
  1177. QDM2SubPacket *packet = NULL;
  1178. /* find subpacket with largest type less than max */
  1179. for (j = 0, min = 0; j < q->sub_packets_B; j++) {
  1180. value = q->sub_packet_list_B[j].packet->type;
  1181. if (value > min && value < max) {
  1182. min = value;
  1183. packet = q->sub_packet_list_B[j].packet;
  1184. }
  1185. }
  1186. max = min;
  1187. /* check for errors (?) */
  1188. if (!packet)
  1189. return;
  1190. if (i == 0 &&
  1191. (packet->type < 16 || packet->type >= 48 ||
  1192. fft_subpackets[packet->type - 16]))
  1193. return;
  1194. /* decode FFT tones */
  1195. init_get_bits(&gb, packet->data, packet->size * 8);
  1196. if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16])
  1197. unknown_flag = 1;
  1198. else
  1199. unknown_flag = 0;
  1200. type = packet->type;
  1201. if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) {
  1202. int duration = q->sub_sampling + 5 - (type & 15);
  1203. if (duration >= 0 && duration < 4)
  1204. qdm2_fft_decode_tones(q, duration, &gb, unknown_flag);
  1205. } else if (type == 31) {
  1206. for (j = 0; j < 4; j++)
  1207. qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
  1208. } else if (type == 46) {
  1209. for (j = 0; j < 6; j++)
  1210. q->fft_level_exp[j] = get_bits(&gb, 6);
  1211. for (j = 0; j < 4; j++)
  1212. qdm2_fft_decode_tones(q, j, &gb, unknown_flag);
  1213. }
  1214. } // Loop on B packets
  1215. /* calculate maximum indexes for FFT coefficients */
  1216. for (i = 0, j = -1; i < 5; i++)
  1217. if (q->fft_coefs_min_index[i] >= 0) {
  1218. if (j >= 0)
  1219. q->fft_coefs_max_index[j] = q->fft_coefs_min_index[i];
  1220. j = i;
  1221. }
  1222. if (j >= 0)
  1223. q->fft_coefs_max_index[j] = q->fft_coefs_index;
  1224. }
  1225. static void qdm2_fft_generate_tone(QDM2Context *q, FFTTone *tone)
  1226. {
  1227. float level, f[6];
  1228. int i;
  1229. QDM2Complex c;
  1230. const double iscale = 2.0 * M_PI / 512.0;
  1231. tone->phase += tone->phase_shift;
  1232. /* calculate current level (maximum amplitude) of tone */
  1233. level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level;
  1234. c.im = level * sin(tone->phase * iscale);
  1235. c.re = level * cos(tone->phase * iscale);
  1236. /* generate FFT coefficients for tone */
  1237. if (tone->duration >= 3 || tone->cutoff >= 3) {
  1238. tone->complex[0].im += c.im;
  1239. tone->complex[0].re += c.re;
  1240. tone->complex[1].im -= c.im;
  1241. tone->complex[1].re -= c.re;
  1242. } else {
  1243. f[1] = -tone->table[4];
  1244. f[0] = tone->table[3] - tone->table[0];
  1245. f[2] = 1.0 - tone->table[2] - tone->table[3];
  1246. f[3] = tone->table[1] + tone->table[4] - 1.0;
  1247. f[4] = tone->table[0] - tone->table[1];
  1248. f[5] = tone->table[2];
  1249. for (i = 0; i < 2; i++) {
  1250. tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re +=
  1251. c.re * f[i];
  1252. tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im +=
  1253. c.im * ((tone->cutoff <= i) ? -f[i] : f[i]);
  1254. }
  1255. for (i = 0; i < 4; i++) {
  1256. tone->complex[i].re += c.re * f[i + 2];
  1257. tone->complex[i].im += c.im * f[i + 2];
  1258. }
  1259. }
  1260. /* copy the tone if it has not yet died out */
  1261. if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) {
  1262. memcpy(&q->fft_tones[q->fft_tone_end], tone, sizeof(FFTTone));
  1263. q->fft_tone_end = (q->fft_tone_end + 1) % 1000;
  1264. }
  1265. }
  1266. static void qdm2_fft_tone_synthesizer(QDM2Context *q, int sub_packet)
  1267. {
  1268. int i, j, ch;
  1269. const double iscale = 0.25 * M_PI;
  1270. for (ch = 0; ch < q->channels; ch++) {
  1271. memset(q->fft.complex[ch], 0, q->fft_size * sizeof(QDM2Complex));
  1272. }
  1273. /* apply FFT tones with duration 4 (1 FFT period) */
  1274. if (q->fft_coefs_min_index[4] >= 0)
  1275. for (i = q->fft_coefs_min_index[4]; i < q->fft_coefs_max_index[4]; i++) {
  1276. float level;
  1277. QDM2Complex c;
  1278. if (q->fft_coefs[i].sub_packet != sub_packet)
  1279. break;
  1280. ch = (q->channels == 1) ? 0 : q->fft_coefs[i].channel;
  1281. 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];
  1282. c.re = level * cos(q->fft_coefs[i].phase * iscale);
  1283. c.im = level * sin(q->fft_coefs[i].phase * iscale);
  1284. q->fft.complex[ch][q->fft_coefs[i].offset + 0].re += c.re;
  1285. q->fft.complex[ch][q->fft_coefs[i].offset + 0].im += c.im;
  1286. q->fft.complex[ch][q->fft_coefs[i].offset + 1].re -= c.re;
  1287. q->fft.complex[ch][q->fft_coefs[i].offset + 1].im -= c.im;
  1288. }
  1289. /* generate existing FFT tones */
  1290. for (i = q->fft_tone_end; i != q->fft_tone_start; ) {
  1291. qdm2_fft_generate_tone(q, &q->fft_tones[q->fft_tone_start]);
  1292. q->fft_tone_start = (q->fft_tone_start + 1) % 1000;
  1293. }
  1294. /* create and generate new FFT tones with duration 0 (long) to 3 (short) */
  1295. for (i = 0; i < 4; i++)
  1296. if (q->fft_coefs_min_index[i] >= 0) {
  1297. for (j = q->fft_coefs_min_index[i]; j < q->fft_coefs_max_index[i]; j++) {
  1298. int offset, four_i;
  1299. FFTTone tone;
  1300. if (q->fft_coefs[j].sub_packet != sub_packet)
  1301. break;
  1302. four_i = (4 - i);
  1303. offset = q->fft_coefs[j].offset >> four_i;
  1304. ch = (q->channels == 1) ? 0 : q->fft_coefs[j].channel;
  1305. if (offset < q->frequency_range) {
  1306. if (offset < 2)
  1307. tone.cutoff = offset;
  1308. else
  1309. tone.cutoff = (offset >= 60) ? 3 : 2;
  1310. 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];
  1311. tone.complex = &q->fft.complex[ch][offset];
  1312. tone.table = fft_tone_sample_table[i][q->fft_coefs[j].offset - (offset << four_i)];
  1313. tone.phase = 64 * q->fft_coefs[j].phase - (offset << 8) - 128;
  1314. tone.phase_shift = (2 * q->fft_coefs[j].offset + 1) << (7 - four_i);
  1315. tone.duration = i;
  1316. tone.time_index = 0;
  1317. qdm2_fft_generate_tone(q, &tone);
  1318. }
  1319. }
  1320. q->fft_coefs_min_index[i] = j;
  1321. }
  1322. }
  1323. static void qdm2_calculate_fft(QDM2Context *q, int channel, int sub_packet)
  1324. {
  1325. const float gain = (q->channels == 1 && q->nb_channels == 2) ? 0.5f : 1.0f;
  1326. float *out = q->output_buffer + channel;
  1327. int i;
  1328. q->fft.complex[channel][0].re *= 2.0f;
  1329. q->fft.complex[channel][0].im = 0.0f;
  1330. q->rdft_ctx.rdft_calc(&q->rdft_ctx, (FFTSample *)q->fft.complex[channel]);
  1331. /* add samples to output buffer */
  1332. for (i = 0; i < FFALIGN(q->fft_size, 8); i++) {
  1333. out[0] += q->fft.complex[channel][i].re * gain;
  1334. out[q->channels] += q->fft.complex[channel][i].im * gain;
  1335. out += 2 * q->channels;
  1336. }
  1337. }
  1338. /**
  1339. * @param q context
  1340. * @param index subpacket number
  1341. */
  1342. static void qdm2_synthesis_filter(QDM2Context *q, int index)
  1343. {
  1344. int i, k, ch, sb_used, sub_sampling, dither_state = 0;
  1345. /* copy sb_samples */
  1346. sb_used = QDM2_SB_USED(q->sub_sampling);
  1347. for (ch = 0; ch < q->channels; ch++)
  1348. for (i = 0; i < 8; i++)
  1349. for (k = sb_used; k < SBLIMIT; k++)
  1350. q->sb_samples[ch][(8 * index) + i][k] = 0;
  1351. for (ch = 0; ch < q->nb_channels; ch++) {
  1352. float *samples_ptr = q->samples + ch;
  1353. for (i = 0; i < 8; i++) {
  1354. ff_mpa_synth_filter_float(&q->mpadsp,
  1355. q->synth_buf[ch], &(q->synth_buf_offset[ch]),
  1356. ff_mpa_synth_window_float, &dither_state,
  1357. samples_ptr, q->nb_channels,
  1358. q->sb_samples[ch][(8 * index) + i]);
  1359. samples_ptr += 32 * q->nb_channels;
  1360. }
  1361. }
  1362. /* add samples to output buffer */
  1363. sub_sampling = (4 >> q->sub_sampling);
  1364. for (ch = 0; ch < q->channels; ch++)
  1365. for (i = 0; i < q->frame_size; i++)
  1366. q->output_buffer[q->channels * i + ch] += (1 << 23) * q->samples[q->nb_channels * sub_sampling * i + ch];
  1367. }
  1368. /**
  1369. * Init static data (does not depend on specific file)
  1370. *
  1371. * @param q context
  1372. */
  1373. static av_cold void qdm2_init_static_data(void) {
  1374. static int done;
  1375. if(done)
  1376. return;
  1377. qdm2_init_vlc();
  1378. ff_mpa_synth_init_float(ff_mpa_synth_window_float);
  1379. softclip_table_init();
  1380. rnd_table_init();
  1381. init_noise_samples();
  1382. done = 1;
  1383. }
  1384. /**
  1385. * Init parameters from codec extradata
  1386. */
  1387. static av_cold int qdm2_decode_init(AVCodecContext *avctx)
  1388. {
  1389. QDM2Context *s = avctx->priv_data;
  1390. uint8_t *extradata;
  1391. int extradata_size;
  1392. int tmp_val, tmp, size;
  1393. qdm2_init_static_data();
  1394. /* extradata parsing
  1395. Structure:
  1396. wave {
  1397. frma (QDM2)
  1398. QDCA
  1399. QDCP
  1400. }
  1401. 32 size (including this field)
  1402. 32 tag (=frma)
  1403. 32 type (=QDM2 or QDMC)
  1404. 32 size (including this field, in bytes)
  1405. 32 tag (=QDCA) // maybe mandatory parameters
  1406. 32 unknown (=1)
  1407. 32 channels (=2)
  1408. 32 samplerate (=44100)
  1409. 32 bitrate (=96000)
  1410. 32 block size (=4096)
  1411. 32 frame size (=256) (for one channel)
  1412. 32 packet size (=1300)
  1413. 32 size (including this field, in bytes)
  1414. 32 tag (=QDCP) // maybe some tuneable parameters
  1415. 32 float1 (=1.0)
  1416. 32 zero ?
  1417. 32 float2 (=1.0)
  1418. 32 float3 (=1.0)
  1419. 32 unknown (27)
  1420. 32 unknown (8)
  1421. 32 zero ?
  1422. */
  1423. if (!avctx->extradata || (avctx->extradata_size < 48)) {
  1424. av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
  1425. return AVERROR_INVALIDDATA;
  1426. }
  1427. extradata = avctx->extradata;
  1428. extradata_size = avctx->extradata_size;
  1429. while (extradata_size > 7) {
  1430. if (!memcmp(extradata, "frmaQDM", 7))
  1431. break;
  1432. extradata++;
  1433. extradata_size--;
  1434. }
  1435. if (extradata_size < 12) {
  1436. av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
  1437. extradata_size);
  1438. return AVERROR_INVALIDDATA;
  1439. }
  1440. if (memcmp(extradata, "frmaQDM", 7)) {
  1441. av_log(avctx, AV_LOG_ERROR, "invalid headers, QDM? not found\n");
  1442. return AVERROR_INVALIDDATA;
  1443. }
  1444. if (extradata[7] == 'C') {
  1445. // s->is_qdmc = 1;
  1446. avpriv_report_missing_feature(avctx, "QDMC version 1");
  1447. return AVERROR_PATCHWELCOME;
  1448. }
  1449. extradata += 8;
  1450. extradata_size -= 8;
  1451. size = AV_RB32(extradata);
  1452. if(size > extradata_size){
  1453. av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
  1454. extradata_size, size);
  1455. return AVERROR_INVALIDDATA;
  1456. }
  1457. extradata += 4;
  1458. av_log(avctx, AV_LOG_DEBUG, "size: %d\n", size);
  1459. if (AV_RB32(extradata) != MKBETAG('Q','D','C','A')) {
  1460. av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
  1461. return AVERROR_INVALIDDATA;
  1462. }
  1463. extradata += 8;
  1464. avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
  1465. extradata += 4;
  1466. if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) {
  1467. av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
  1468. return AVERROR_INVALIDDATA;
  1469. }
  1470. avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
  1471. AV_CH_LAYOUT_MONO;
  1472. avctx->sample_rate = AV_RB32(extradata);
  1473. extradata += 4;
  1474. avctx->bit_rate = AV_RB32(extradata);
  1475. extradata += 4;
  1476. s->group_size = AV_RB32(extradata);
  1477. extradata += 4;
  1478. s->fft_size = AV_RB32(extradata);
  1479. extradata += 4;
  1480. s->checksum_size = AV_RB32(extradata);
  1481. if (s->checksum_size >= 1U << 28) {
  1482. av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
  1483. return AVERROR_INVALIDDATA;
  1484. }
  1485. s->fft_order = av_log2(s->fft_size) + 1;
  1486. // something like max decodable tones
  1487. s->group_order = av_log2(s->group_size) + 1;
  1488. s->frame_size = s->group_size / 16; // 16 iterations per super block
  1489. if (s->frame_size > QDM2_MAX_FRAME_SIZE)
  1490. return AVERROR_INVALIDDATA;
  1491. s->sub_sampling = s->fft_order - 7;
  1492. s->frequency_range = 255 / (1 << (2 - s->sub_sampling));
  1493. switch ((s->sub_sampling * 2 + s->channels - 1)) {
  1494. case 0: tmp = 40; break;
  1495. case 1: tmp = 48; break;
  1496. case 2: tmp = 56; break;
  1497. case 3: tmp = 72; break;
  1498. case 4: tmp = 80; break;
  1499. case 5: tmp = 100;break;
  1500. default: tmp=s->sub_sampling; break;
  1501. }
  1502. tmp_val = 0;
  1503. if ((tmp * 1000) < avctx->bit_rate) tmp_val = 1;
  1504. if ((tmp * 1440) < avctx->bit_rate) tmp_val = 2;
  1505. if ((tmp * 1760) < avctx->bit_rate) tmp_val = 3;
  1506. if ((tmp * 2240) < avctx->bit_rate) tmp_val = 4;
  1507. s->cm_table_select = tmp_val;
  1508. if (avctx->bit_rate <= 8000)
  1509. s->coeff_per_sb_select = 0;
  1510. else if (avctx->bit_rate < 16000)
  1511. s->coeff_per_sb_select = 1;
  1512. else
  1513. s->coeff_per_sb_select = 2;
  1514. // Fail on unknown fft order
  1515. if ((s->fft_order < 7) || (s->fft_order > 9)) {
  1516. avpriv_request_sample(avctx, "Unknown FFT order %d", s->fft_order);
  1517. return AVERROR_PATCHWELCOME;
  1518. }
  1519. if (s->fft_size != (1 << (s->fft_order - 1))) {
  1520. av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
  1521. return AVERROR_INVALIDDATA;
  1522. }
  1523. ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
  1524. ff_mpadsp_init(&s->mpadsp);
  1525. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  1526. return 0;
  1527. }
  1528. static av_cold int qdm2_decode_close(AVCodecContext *avctx)
  1529. {
  1530. QDM2Context *s = avctx->priv_data;
  1531. ff_rdft_end(&s->rdft_ctx);
  1532. return 0;
  1533. }
  1534. static int qdm2_decode(QDM2Context *q, const uint8_t *in, int16_t *out)
  1535. {
  1536. int ch, i;
  1537. const int frame_size = (q->frame_size * q->channels);
  1538. if((unsigned)frame_size > FF_ARRAY_ELEMS(q->output_buffer)/2)
  1539. return -1;
  1540. /* select input buffer */
  1541. q->compressed_data = in;
  1542. q->compressed_size = q->checksum_size;
  1543. /* copy old block, clear new block of output samples */
  1544. memmove(q->output_buffer, &q->output_buffer[frame_size], frame_size * sizeof(float));
  1545. memset(&q->output_buffer[frame_size], 0, frame_size * sizeof(float));
  1546. /* decode block of QDM2 compressed data */
  1547. if (q->sub_packet == 0) {
  1548. q->has_errors = 0; // zero it for a new super block
  1549. av_log(NULL,AV_LOG_DEBUG,"Superblock follows\n");
  1550. qdm2_decode_super_block(q);
  1551. }
  1552. /* parse subpackets */
  1553. if (!q->has_errors) {
  1554. if (q->sub_packet == 2)
  1555. qdm2_decode_fft_packets(q);
  1556. qdm2_fft_tone_synthesizer(q, q->sub_packet);
  1557. }
  1558. /* sound synthesis stage 1 (FFT) */
  1559. for (ch = 0; ch < q->channels; ch++) {
  1560. qdm2_calculate_fft(q, ch, q->sub_packet);
  1561. if (!q->has_errors && q->sub_packet_list_C[0].packet) {
  1562. SAMPLES_NEEDED_2("has errors, and C list is not empty")
  1563. return -1;
  1564. }
  1565. }
  1566. /* sound synthesis stage 2 (MPEG audio like synthesis filter) */
  1567. if (!q->has_errors && q->do_synth_filter)
  1568. qdm2_synthesis_filter(q, q->sub_packet);
  1569. q->sub_packet = (q->sub_packet + 1) % 16;
  1570. /* clip and convert output float[] to 16bit signed samples */
  1571. for (i = 0; i < frame_size; i++) {
  1572. int value = (int)q->output_buffer[i];
  1573. if (value > SOFTCLIP_THRESHOLD)
  1574. value = (value > HARDCLIP_THRESHOLD) ? 32767 : softclip_table[ value - SOFTCLIP_THRESHOLD];
  1575. else if (value < -SOFTCLIP_THRESHOLD)
  1576. value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -softclip_table[-value - SOFTCLIP_THRESHOLD];
  1577. out[i] = value;
  1578. }
  1579. return 0;
  1580. }
  1581. static int qdm2_decode_frame(AVCodecContext *avctx, void *data,
  1582. int *got_frame_ptr, AVPacket *avpkt)
  1583. {
  1584. AVFrame *frame = data;
  1585. const uint8_t *buf = avpkt->data;
  1586. int buf_size = avpkt->size;
  1587. QDM2Context *s = avctx->priv_data;
  1588. int16_t *out;
  1589. int i, ret;
  1590. if(!buf)
  1591. return 0;
  1592. if(buf_size < s->checksum_size)
  1593. return -1;
  1594. /* get output buffer */
  1595. frame->nb_samples = 16 * s->frame_size;
  1596. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1597. return ret;
  1598. out = (int16_t *)frame->data[0];
  1599. for (i = 0; i < 16; i++) {
  1600. if ((ret = qdm2_decode(s, buf, out)) < 0)
  1601. return ret;
  1602. out += s->channels * s->frame_size;
  1603. }
  1604. *got_frame_ptr = 1;
  1605. return s->checksum_size;
  1606. }
  1607. AVCodec ff_qdm2_decoder = {
  1608. .name = "qdm2",
  1609. .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 2"),
  1610. .type = AVMEDIA_TYPE_AUDIO,
  1611. .id = AV_CODEC_ID_QDM2,
  1612. .priv_data_size = sizeof(QDM2Context),
  1613. .init = qdm2_decode_init,
  1614. .close = qdm2_decode_close,
  1615. .decode = qdm2_decode_frame,
  1616. .capabilities = AV_CODEC_CAP_DR1,
  1617. };