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.

2008 lines
68KB

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