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.

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