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.

2002 lines
67KB

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