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.

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