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.

1081 lines
36KB

  1. /*
  2. * MLP decoder
  3. * Copyright (c) 2007-2008 Ian Caulfield
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/mlpdec.c
  23. * MLP decoder
  24. */
  25. #include <stdint.h>
  26. #include "avcodec.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "bitstream.h"
  29. #include "libavutil/crc.h"
  30. #include "parser.h"
  31. #include "mlp_parser.h"
  32. #include "mlp.h"
  33. /** number of bits used for VLC lookup - longest Huffman code is 9 */
  34. #define VLC_BITS 9
  35. static const char* sample_message =
  36. "Please file a bug report following the instructions at "
  37. "http://ffmpeg.org/bugreports.html and include "
  38. "a sample of this file.";
  39. typedef struct SubStream {
  40. //! Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
  41. uint8_t restart_seen;
  42. //@{
  43. /** restart header data */
  44. //! The type of noise to be used in the rematrix stage.
  45. uint16_t noise_type;
  46. //! The index of the first channel coded in this substream.
  47. uint8_t min_channel;
  48. //! The index of the last channel coded in this substream.
  49. uint8_t max_channel;
  50. //! The number of channels input into the rematrix stage.
  51. uint8_t max_matrix_channel;
  52. //! For each channel output by the matrix, the output channel to map it to
  53. uint8_t ch_assign[MAX_CHANNELS];
  54. //! The left shift applied to random noise in 0x31ea substreams.
  55. uint8_t noise_shift;
  56. //! The current seed value for the pseudorandom noise generator(s).
  57. uint32_t noisegen_seed;
  58. //! Set if the substream contains extra info to check the size of VLC blocks.
  59. uint8_t data_check_present;
  60. //! Bitmask of which parameter sets are conveyed in a decoding parameter block.
  61. uint8_t param_presence_flags;
  62. #define PARAM_BLOCKSIZE (1 << 7)
  63. #define PARAM_MATRIX (1 << 6)
  64. #define PARAM_OUTSHIFT (1 << 5)
  65. #define PARAM_QUANTSTEP (1 << 4)
  66. #define PARAM_FIR (1 << 3)
  67. #define PARAM_IIR (1 << 2)
  68. #define PARAM_HUFFOFFSET (1 << 1)
  69. #define PARAM_PRESENCE (1 << 0)
  70. //@}
  71. //@{
  72. /** matrix data */
  73. //! Number of matrices to be applied.
  74. uint8_t num_primitive_matrices;
  75. //! matrix output channel
  76. uint8_t matrix_out_ch[MAX_MATRICES];
  77. //! Whether the LSBs of the matrix output are encoded in the bitstream.
  78. uint8_t lsb_bypass[MAX_MATRICES];
  79. //! Matrix coefficients, stored as 2.14 fixed point.
  80. int32_t matrix_coeff[MAX_MATRICES][MAX_CHANNELS+2];
  81. //! Left shift to apply to noise values in 0x31eb substreams.
  82. uint8_t matrix_noise_shift[MAX_MATRICES];
  83. //@}
  84. //! Left shift to apply to Huffman-decoded residuals.
  85. uint8_t quant_step_size[MAX_CHANNELS];
  86. //! number of PCM samples in current audio block
  87. uint16_t blocksize;
  88. //! Number of PCM samples decoded so far in this frame.
  89. uint16_t blockpos;
  90. //! Left shift to apply to decoded PCM values to get final 24-bit output.
  91. int8_t output_shift[MAX_CHANNELS];
  92. //! Running XOR of all output samples.
  93. int32_t lossless_check_data;
  94. } SubStream;
  95. typedef struct MLPDecodeContext {
  96. AVCodecContext *avctx;
  97. //! Set if a valid major sync block has been read. Otherwise no decoding is possible.
  98. uint8_t params_valid;
  99. //! Number of substreams contained within this stream.
  100. uint8_t num_substreams;
  101. //! Index of the last substream to decode - further substreams are skipped.
  102. uint8_t max_decoded_substream;
  103. //! number of PCM samples contained in each frame
  104. int access_unit_size;
  105. //! next power of two above the number of samples in each frame
  106. int access_unit_size_pow2;
  107. SubStream substream[MAX_SUBSTREAMS];
  108. ChannelParams channel_params[MAX_CHANNELS];
  109. int8_t noise_buffer[MAX_BLOCKSIZE_POW2];
  110. int8_t bypassed_lsbs[MAX_BLOCKSIZE][MAX_CHANNELS];
  111. int32_t sample_buffer[MAX_BLOCKSIZE][MAX_CHANNELS+2];
  112. } MLPDecodeContext;
  113. static VLC huff_vlc[3];
  114. /** Initialize static data, constant between all invocations of the codec. */
  115. static av_cold void init_static(void)
  116. {
  117. INIT_VLC_STATIC(&huff_vlc[0], VLC_BITS, 18,
  118. &ff_mlp_huffman_tables[0][0][1], 2, 1,
  119. &ff_mlp_huffman_tables[0][0][0], 2, 1, 512);
  120. INIT_VLC_STATIC(&huff_vlc[1], VLC_BITS, 16,
  121. &ff_mlp_huffman_tables[1][0][1], 2, 1,
  122. &ff_mlp_huffman_tables[1][0][0], 2, 1, 512);
  123. INIT_VLC_STATIC(&huff_vlc[2], VLC_BITS, 15,
  124. &ff_mlp_huffman_tables[2][0][1], 2, 1,
  125. &ff_mlp_huffman_tables[2][0][0], 2, 1, 512);
  126. ff_mlp_init_crc();
  127. }
  128. static inline int32_t calculate_sign_huff(MLPDecodeContext *m,
  129. unsigned int substr, unsigned int ch)
  130. {
  131. ChannelParams *cp = &m->channel_params[ch];
  132. SubStream *s = &m->substream[substr];
  133. int lsb_bits = cp->huff_lsbs - s->quant_step_size[ch];
  134. int sign_shift = lsb_bits + (cp->codebook ? 2 - cp->codebook : -1);
  135. int32_t sign_huff_offset = cp->huff_offset;
  136. if (cp->codebook > 0)
  137. sign_huff_offset -= 7 << lsb_bits;
  138. if (sign_shift >= 0)
  139. sign_huff_offset -= 1 << sign_shift;
  140. return sign_huff_offset;
  141. }
  142. /** Read a sample, consisting of either, both or neither of entropy-coded MSBs
  143. * and plain LSBs. */
  144. static inline int read_huff_channels(MLPDecodeContext *m, GetBitContext *gbp,
  145. unsigned int substr, unsigned int pos)
  146. {
  147. SubStream *s = &m->substream[substr];
  148. unsigned int mat, channel;
  149. for (mat = 0; mat < s->num_primitive_matrices; mat++)
  150. if (s->lsb_bypass[mat])
  151. m->bypassed_lsbs[pos + s->blockpos][mat] = get_bits1(gbp);
  152. for (channel = s->min_channel; channel <= s->max_channel; channel++) {
  153. ChannelParams *cp = &m->channel_params[channel];
  154. int codebook = cp->codebook;
  155. int quant_step_size = s->quant_step_size[channel];
  156. int lsb_bits = cp->huff_lsbs - quant_step_size;
  157. int result = 0;
  158. if (codebook > 0)
  159. result = get_vlc2(gbp, huff_vlc[codebook-1].table,
  160. VLC_BITS, (9 + VLC_BITS - 1) / VLC_BITS);
  161. if (result < 0)
  162. return -1;
  163. if (lsb_bits > 0)
  164. result = (result << lsb_bits) + get_bits(gbp, lsb_bits);
  165. result += cp->sign_huff_offset;
  166. result <<= quant_step_size;
  167. m->sample_buffer[pos + s->blockpos][channel] = result;
  168. }
  169. return 0;
  170. }
  171. static av_cold int mlp_decode_init(AVCodecContext *avctx)
  172. {
  173. MLPDecodeContext *m = avctx->priv_data;
  174. int substr;
  175. init_static();
  176. m->avctx = avctx;
  177. for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
  178. m->substream[substr].lossless_check_data = 0xffffffff;
  179. return 0;
  180. }
  181. /** Read a major sync info header - contains high level information about
  182. * the stream - sample rate, channel arrangement etc. Most of this
  183. * information is not actually necessary for decoding, only for playback.
  184. */
  185. static int read_major_sync(MLPDecodeContext *m, GetBitContext *gb)
  186. {
  187. MLPHeaderInfo mh;
  188. int substr;
  189. if (ff_mlp_read_major_sync(m->avctx, &mh, gb) != 0)
  190. return -1;
  191. if (mh.group1_bits == 0) {
  192. av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown bits per sample\n");
  193. return -1;
  194. }
  195. if (mh.group2_bits > mh.group1_bits) {
  196. av_log(m->avctx, AV_LOG_ERROR,
  197. "Channel group 2 cannot have more bits per sample than group 1.\n");
  198. return -1;
  199. }
  200. if (mh.group2_samplerate && mh.group2_samplerate != mh.group1_samplerate) {
  201. av_log(m->avctx, AV_LOG_ERROR,
  202. "Channel groups with differing sample rates are not currently supported.\n");
  203. return -1;
  204. }
  205. if (mh.group1_samplerate == 0) {
  206. av_log(m->avctx, AV_LOG_ERROR, "invalid/unknown sampling rate\n");
  207. return -1;
  208. }
  209. if (mh.group1_samplerate > MAX_SAMPLERATE) {
  210. av_log(m->avctx, AV_LOG_ERROR,
  211. "Sampling rate %d is greater than the supported maximum (%d).\n",
  212. mh.group1_samplerate, MAX_SAMPLERATE);
  213. return -1;
  214. }
  215. if (mh.access_unit_size > MAX_BLOCKSIZE) {
  216. av_log(m->avctx, AV_LOG_ERROR,
  217. "Block size %d is greater than the supported maximum (%d).\n",
  218. mh.access_unit_size, MAX_BLOCKSIZE);
  219. return -1;
  220. }
  221. if (mh.access_unit_size_pow2 > MAX_BLOCKSIZE_POW2) {
  222. av_log(m->avctx, AV_LOG_ERROR,
  223. "Block size pow2 %d is greater than the supported maximum (%d).\n",
  224. mh.access_unit_size_pow2, MAX_BLOCKSIZE_POW2);
  225. return -1;
  226. }
  227. if (mh.num_substreams == 0)
  228. return -1;
  229. if (m->avctx->codec_id == CODEC_ID_MLP && mh.num_substreams > 2) {
  230. av_log(m->avctx, AV_LOG_ERROR, "MLP only supports up to 2 substreams.\n");
  231. return -1;
  232. }
  233. if (mh.num_substreams > MAX_SUBSTREAMS) {
  234. av_log(m->avctx, AV_LOG_ERROR,
  235. "Number of substreams %d is larger than the maximum supported "
  236. "by the decoder. %s\n", mh.num_substreams, sample_message);
  237. return -1;
  238. }
  239. m->access_unit_size = mh.access_unit_size;
  240. m->access_unit_size_pow2 = mh.access_unit_size_pow2;
  241. m->num_substreams = mh.num_substreams;
  242. m->max_decoded_substream = m->num_substreams - 1;
  243. m->avctx->sample_rate = mh.group1_samplerate;
  244. m->avctx->frame_size = mh.access_unit_size;
  245. m->avctx->bits_per_raw_sample = mh.group1_bits;
  246. if (mh.group1_bits > 16)
  247. m->avctx->sample_fmt = SAMPLE_FMT_S32;
  248. else
  249. m->avctx->sample_fmt = SAMPLE_FMT_S16;
  250. m->params_valid = 1;
  251. for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
  252. m->substream[substr].restart_seen = 0;
  253. return 0;
  254. }
  255. /** Read a restart header from a block in a substream. This contains parameters
  256. * required to decode the audio that do not change very often. Generally
  257. * (always) present only in blocks following a major sync. */
  258. static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
  259. const uint8_t *buf, unsigned int substr)
  260. {
  261. SubStream *s = &m->substream[substr];
  262. unsigned int ch;
  263. int sync_word, tmp;
  264. uint8_t checksum;
  265. uint8_t lossless_check;
  266. int start_count = get_bits_count(gbp);
  267. sync_word = get_bits(gbp, 13);
  268. if (sync_word != 0x31ea >> 1) {
  269. av_log(m->avctx, AV_LOG_ERROR,
  270. "restart header sync incorrect (got 0x%04x)\n", sync_word);
  271. return -1;
  272. }
  273. s->noise_type = get_bits1(gbp);
  274. skip_bits(gbp, 16); /* Output timestamp */
  275. s->min_channel = get_bits(gbp, 4);
  276. s->max_channel = get_bits(gbp, 4);
  277. s->max_matrix_channel = get_bits(gbp, 4);
  278. if (s->min_channel > s->max_channel) {
  279. av_log(m->avctx, AV_LOG_ERROR,
  280. "Substream min channel cannot be greater than max channel.\n");
  281. return -1;
  282. }
  283. if (m->avctx->request_channels > 0
  284. && s->max_channel + 1 >= m->avctx->request_channels
  285. && substr < m->max_decoded_substream) {
  286. av_log(m->avctx, AV_LOG_INFO,
  287. "Extracting %d channel downmix from substream %d. "
  288. "Further substreams will be skipped.\n",
  289. s->max_channel + 1, substr);
  290. m->max_decoded_substream = substr;
  291. }
  292. s->noise_shift = get_bits(gbp, 4);
  293. s->noisegen_seed = get_bits(gbp, 23);
  294. skip_bits(gbp, 19);
  295. s->data_check_present = get_bits1(gbp);
  296. lossless_check = get_bits(gbp, 8);
  297. if (substr == m->max_decoded_substream
  298. && s->lossless_check_data != 0xffffffff) {
  299. tmp = xor_32_to_8(s->lossless_check_data);
  300. if (tmp != lossless_check)
  301. av_log(m->avctx, AV_LOG_WARNING,
  302. "Lossless check failed - expected %02x, calculated %02x.\n",
  303. lossless_check, tmp);
  304. else
  305. dprintf(m->avctx, "Lossless check passed for substream %d (%x).\n",
  306. substr, tmp);
  307. }
  308. skip_bits(gbp, 16);
  309. memset(s->ch_assign, 0, sizeof(s->ch_assign));
  310. for (ch = 0; ch <= s->max_matrix_channel; ch++) {
  311. int ch_assign = get_bits(gbp, 6);
  312. dprintf(m->avctx, "ch_assign[%d][%d] = %d\n", substr, ch,
  313. ch_assign);
  314. if (ch_assign > s->max_matrix_channel) {
  315. av_log(m->avctx, AV_LOG_ERROR,
  316. "Assignment of matrix channel %d to invalid output channel %d. %s\n",
  317. ch, ch_assign, sample_message);
  318. return -1;
  319. }
  320. s->ch_assign[ch_assign] = ch;
  321. }
  322. checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
  323. if (checksum != get_bits(gbp, 8))
  324. av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
  325. /* Set default decoding parameters. */
  326. s->param_presence_flags = 0xff;
  327. s->num_primitive_matrices = 0;
  328. s->blocksize = 8;
  329. s->lossless_check_data = 0;
  330. memset(s->output_shift , 0, sizeof(s->output_shift ));
  331. memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
  332. for (ch = s->min_channel; ch <= s->max_channel; ch++) {
  333. ChannelParams *cp = &m->channel_params[ch];
  334. cp->filter_params[FIR].order = 0;
  335. cp->filter_params[IIR].order = 0;
  336. cp->filter_params[FIR].shift = 0;
  337. cp->filter_params[IIR].shift = 0;
  338. /* Default audio coding is 24-bit raw PCM. */
  339. cp->huff_offset = 0;
  340. cp->sign_huff_offset = (-1) << 23;
  341. cp->codebook = 0;
  342. cp->huff_lsbs = 24;
  343. }
  344. if (substr == m->max_decoded_substream) {
  345. m->avctx->channels = s->max_matrix_channel + 1;
  346. }
  347. return 0;
  348. }
  349. /** Read parameters for one of the prediction filters. */
  350. static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
  351. unsigned int channel, unsigned int filter)
  352. {
  353. FilterParams *fp = &m->channel_params[channel].filter_params[filter];
  354. const char fchar = filter ? 'I' : 'F';
  355. int i, order;
  356. // Filter is 0 for FIR, 1 for IIR.
  357. assert(filter < 2);
  358. order = get_bits(gbp, 4);
  359. if (order > MAX_FILTER_ORDER) {
  360. av_log(m->avctx, AV_LOG_ERROR,
  361. "%cIR filter order %d is greater than maximum %d.\n",
  362. fchar, order, MAX_FILTER_ORDER);
  363. return -1;
  364. }
  365. fp->order = order;
  366. if (order > 0) {
  367. int coeff_bits, coeff_shift;
  368. fp->shift = get_bits(gbp, 4);
  369. coeff_bits = get_bits(gbp, 5);
  370. coeff_shift = get_bits(gbp, 3);
  371. if (coeff_bits < 1 || coeff_bits > 16) {
  372. av_log(m->avctx, AV_LOG_ERROR,
  373. "%cIR filter coeff_bits must be between 1 and 16.\n",
  374. fchar);
  375. return -1;
  376. }
  377. if (coeff_bits + coeff_shift > 16) {
  378. av_log(m->avctx, AV_LOG_ERROR,
  379. "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
  380. fchar);
  381. return -1;
  382. }
  383. for (i = 0; i < order; i++)
  384. fp->coeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
  385. if (get_bits1(gbp)) {
  386. int state_bits, state_shift;
  387. if (filter == FIR) {
  388. av_log(m->avctx, AV_LOG_ERROR,
  389. "FIR filter has state data specified.\n");
  390. return -1;
  391. }
  392. state_bits = get_bits(gbp, 4);
  393. state_shift = get_bits(gbp, 4);
  394. /* TODO: Check validity of state data. */
  395. for (i = 0; i < order; i++)
  396. fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
  397. }
  398. }
  399. return 0;
  400. }
  401. /** Read decoding parameters that change more often than those in the restart
  402. * header. */
  403. static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
  404. unsigned int substr)
  405. {
  406. SubStream *s = &m->substream[substr];
  407. unsigned int mat, ch;
  408. if (s->param_presence_flags & PARAM_PRESENCE)
  409. if (get_bits1(gbp))
  410. s->param_presence_flags = get_bits(gbp, 8);
  411. if (s->param_presence_flags & PARAM_BLOCKSIZE)
  412. if (get_bits1(gbp)) {
  413. s->blocksize = get_bits(gbp, 9);
  414. if (s->blocksize > MAX_BLOCKSIZE) {
  415. av_log(m->avctx, AV_LOG_ERROR, "block size too large\n");
  416. s->blocksize = 0;
  417. return -1;
  418. }
  419. }
  420. if (s->param_presence_flags & PARAM_MATRIX)
  421. if (get_bits1(gbp)) {
  422. s->num_primitive_matrices = get_bits(gbp, 4);
  423. for (mat = 0; mat < s->num_primitive_matrices; mat++) {
  424. int frac_bits, max_chan;
  425. s->matrix_out_ch[mat] = get_bits(gbp, 4);
  426. frac_bits = get_bits(gbp, 4);
  427. s->lsb_bypass [mat] = get_bits1(gbp);
  428. if (s->matrix_out_ch[mat] > s->max_channel) {
  429. av_log(m->avctx, AV_LOG_ERROR,
  430. "Invalid channel %d specified as output from matrix.\n",
  431. s->matrix_out_ch[mat]);
  432. return -1;
  433. }
  434. if (frac_bits > 14) {
  435. av_log(m->avctx, AV_LOG_ERROR,
  436. "Too many fractional bits specified.\n");
  437. return -1;
  438. }
  439. max_chan = s->max_matrix_channel;
  440. if (!s->noise_type)
  441. max_chan+=2;
  442. for (ch = 0; ch <= max_chan; ch++) {
  443. int coeff_val = 0;
  444. if (get_bits1(gbp))
  445. coeff_val = get_sbits(gbp, frac_bits + 2);
  446. s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
  447. }
  448. if (s->noise_type)
  449. s->matrix_noise_shift[mat] = get_bits(gbp, 4);
  450. else
  451. s->matrix_noise_shift[mat] = 0;
  452. }
  453. }
  454. if (s->param_presence_flags & PARAM_OUTSHIFT)
  455. if (get_bits1(gbp))
  456. for (ch = 0; ch <= s->max_matrix_channel; ch++) {
  457. s->output_shift[ch] = get_bits(gbp, 4);
  458. dprintf(m->avctx, "output shift[%d] = %d\n",
  459. ch, s->output_shift[ch]);
  460. /* TODO: validate */
  461. }
  462. if (s->param_presence_flags & PARAM_QUANTSTEP)
  463. if (get_bits1(gbp))
  464. for (ch = 0; ch <= s->max_channel; ch++) {
  465. ChannelParams *cp = &m->channel_params[ch];
  466. s->quant_step_size[ch] = get_bits(gbp, 4);
  467. /* TODO: validate */
  468. cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
  469. }
  470. for (ch = s->min_channel; ch <= s->max_channel; ch++)
  471. if (get_bits1(gbp)) {
  472. ChannelParams *cp = &m->channel_params[ch];
  473. FilterParams *fir = &cp->filter_params[FIR];
  474. FilterParams *iir = &cp->filter_params[IIR];
  475. if (s->param_presence_flags & PARAM_FIR)
  476. if (get_bits1(gbp))
  477. if (read_filter_params(m, gbp, ch, FIR) < 0)
  478. return -1;
  479. if (s->param_presence_flags & PARAM_IIR)
  480. if (get_bits1(gbp))
  481. if (read_filter_params(m, gbp, ch, IIR) < 0)
  482. return -1;
  483. if (fir->order && iir->order &&
  484. fir->shift != iir->shift) {
  485. av_log(m->avctx, AV_LOG_ERROR,
  486. "FIR and IIR filters must use the same precision.\n");
  487. return -1;
  488. }
  489. /* The FIR and IIR filters must have the same precision.
  490. * To simplify the filtering code, only the precision of the
  491. * FIR filter is considered. If only the IIR filter is employed,
  492. * the FIR filter precision is set to that of the IIR filter, so
  493. * that the filtering code can use it. */
  494. if (!fir->order && iir->order)
  495. fir->shift = iir->shift;
  496. if (s->param_presence_flags & PARAM_HUFFOFFSET)
  497. if (get_bits1(gbp))
  498. cp->huff_offset = get_sbits(gbp, 15);
  499. cp->codebook = get_bits(gbp, 2);
  500. cp->huff_lsbs = get_bits(gbp, 5);
  501. cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
  502. /* TODO: validate */
  503. }
  504. return 0;
  505. }
  506. #define MSB_MASK(bits) (-1u << bits)
  507. /** Generate PCM samples using the prediction filters and residual values
  508. * read from the data stream, and update the filter state. */
  509. static void filter_channel(MLPDecodeContext *m, unsigned int substr,
  510. unsigned int channel)
  511. {
  512. SubStream *s = &m->substream[substr];
  513. int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FILTER_ORDER];
  514. FilterParams *fp[NUM_FILTERS] = { &m->channel_params[channel].filter_params[FIR],
  515. &m->channel_params[channel].filter_params[IIR], };
  516. unsigned int filter_shift = fp[FIR]->shift;
  517. int32_t mask = MSB_MASK(s->quant_step_size[channel]);
  518. int index = MAX_BLOCKSIZE;
  519. int j, i;
  520. for (j = 0; j < NUM_FILTERS; j++) {
  521. memcpy(&filter_state_buffer[j][MAX_BLOCKSIZE], &fp[j]->state[0],
  522. MAX_FILTER_ORDER * sizeof(int32_t));
  523. }
  524. for (i = 0; i < s->blocksize; i++) {
  525. int32_t residual = m->sample_buffer[i + s->blockpos][channel];
  526. unsigned int order;
  527. int64_t accum = 0;
  528. int32_t result;
  529. /* TODO: Move this code to DSPContext? */
  530. for (j = 0; j < NUM_FILTERS; j++)
  531. for (order = 0; order < fp[j]->order; order++)
  532. accum += (int64_t)filter_state_buffer[j][index + order] *
  533. fp[j]->coeff[order];
  534. accum = accum >> filter_shift;
  535. result = (accum + residual) & mask;
  536. --index;
  537. filter_state_buffer[FIR][index] = result;
  538. filter_state_buffer[IIR][index] = result - accum;
  539. m->sample_buffer[i + s->blockpos][channel] = result;
  540. }
  541. for (j = 0; j < NUM_FILTERS; j++) {
  542. memcpy(&fp[j]->state[0], &filter_state_buffer[j][index],
  543. MAX_FILTER_ORDER * sizeof(int32_t));
  544. }
  545. }
  546. /** Read a block of PCM residual data (or actual if no filtering active). */
  547. static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
  548. unsigned int substr)
  549. {
  550. SubStream *s = &m->substream[substr];
  551. unsigned int i, ch, expected_stream_pos = 0;
  552. if (s->data_check_present) {
  553. expected_stream_pos = get_bits_count(gbp);
  554. expected_stream_pos += get_bits(gbp, 16);
  555. av_log(m->avctx, AV_LOG_WARNING, "This file contains some features "
  556. "we have not tested yet. %s\n", sample_message);
  557. }
  558. if (s->blockpos + s->blocksize > m->access_unit_size) {
  559. av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
  560. return -1;
  561. }
  562. memset(&m->bypassed_lsbs[s->blockpos][0], 0,
  563. s->blocksize * sizeof(m->bypassed_lsbs[0]));
  564. for (i = 0; i < s->blocksize; i++) {
  565. if (read_huff_channels(m, gbp, substr, i) < 0)
  566. return -1;
  567. }
  568. for (ch = s->min_channel; ch <= s->max_channel; ch++) {
  569. filter_channel(m, substr, ch);
  570. }
  571. s->blockpos += s->blocksize;
  572. if (s->data_check_present) {
  573. if (get_bits_count(gbp) != expected_stream_pos)
  574. av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
  575. skip_bits(gbp, 8);
  576. }
  577. return 0;
  578. }
  579. /** Data table used for TrueHD noise generation function. */
  580. static const int8_t noise_table[256] = {
  581. 30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
  582. 52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
  583. 10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
  584. 51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
  585. 38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
  586. 61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
  587. 67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
  588. 48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
  589. 0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
  590. 16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
  591. 13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
  592. 89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
  593. 36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
  594. 39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
  595. 45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
  596. -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
  597. };
  598. /** Noise generation functions.
  599. * I'm not sure what these are for - they seem to be some kind of pseudorandom
  600. * sequence generators, used to generate noise data which is used when the
  601. * channels are rematrixed. I'm not sure if they provide a practical benefit
  602. * to compression, or just obfuscate the decoder. Are they for some kind of
  603. * dithering? */
  604. /** Generate two channels of noise, used in the matrix when
  605. * restart sync word == 0x31ea. */
  606. static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
  607. {
  608. SubStream *s = &m->substream[substr];
  609. unsigned int i;
  610. uint32_t seed = s->noisegen_seed;
  611. unsigned int maxchan = s->max_matrix_channel;
  612. for (i = 0; i < s->blockpos; i++) {
  613. uint16_t seed_shr7 = seed >> 7;
  614. m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
  615. m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) << s->noise_shift;
  616. seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
  617. }
  618. s->noisegen_seed = seed;
  619. }
  620. /** Generate a block of noise, used when restart sync word == 0x31eb. */
  621. static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
  622. {
  623. SubStream *s = &m->substream[substr];
  624. unsigned int i;
  625. uint32_t seed = s->noisegen_seed;
  626. for (i = 0; i < m->access_unit_size_pow2; i++) {
  627. uint8_t seed_shr15 = seed >> 15;
  628. m->noise_buffer[i] = noise_table[seed_shr15];
  629. seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
  630. }
  631. s->noisegen_seed = seed;
  632. }
  633. /** Apply the channel matrices in turn to reconstruct the original audio
  634. * samples. */
  635. static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
  636. {
  637. SubStream *s = &m->substream[substr];
  638. unsigned int mat, src_ch, i;
  639. unsigned int maxchan;
  640. maxchan = s->max_matrix_channel;
  641. if (!s->noise_type) {
  642. generate_2_noise_channels(m, substr);
  643. maxchan += 2;
  644. } else {
  645. fill_noise_buffer(m, substr);
  646. }
  647. for (mat = 0; mat < s->num_primitive_matrices; mat++) {
  648. int matrix_noise_shift = s->matrix_noise_shift[mat];
  649. unsigned int dest_ch = s->matrix_out_ch[mat];
  650. int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]);
  651. /* TODO: DSPContext? */
  652. for (i = 0; i < s->blockpos; i++) {
  653. int64_t accum = 0;
  654. for (src_ch = 0; src_ch <= maxchan; src_ch++) {
  655. accum += (int64_t)m->sample_buffer[i][src_ch]
  656. * s->matrix_coeff[mat][src_ch];
  657. }
  658. if (matrix_noise_shift) {
  659. uint32_t index = s->num_primitive_matrices - mat;
  660. index = (i * (index * 2 + 1) + index) & (m->access_unit_size_pow2 - 1);
  661. accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
  662. }
  663. m->sample_buffer[i][dest_ch] = ((accum >> 14) & mask)
  664. + m->bypassed_lsbs[i][mat];
  665. }
  666. }
  667. }
  668. /** Write the audio data into the output buffer. */
  669. static int output_data_internal(MLPDecodeContext *m, unsigned int substr,
  670. uint8_t *data, unsigned int *data_size, int is32)
  671. {
  672. SubStream *s = &m->substream[substr];
  673. unsigned int i, out_ch = 0;
  674. int32_t *data_32 = (int32_t*) data;
  675. int16_t *data_16 = (int16_t*) data;
  676. if (*data_size < (s->max_channel + 1) * s->blockpos * (is32 ? 4 : 2))
  677. return -1;
  678. for (i = 0; i < s->blockpos; i++) {
  679. for (out_ch = 0; out_ch <= s->max_matrix_channel; out_ch++) {
  680. int mat_ch = s->ch_assign[out_ch];
  681. int32_t sample = m->sample_buffer[i][mat_ch]
  682. << s->output_shift[mat_ch];
  683. s->lossless_check_data ^= (sample & 0xffffff) << mat_ch;
  684. if (is32) *data_32++ = sample << 8;
  685. else *data_16++ = sample >> 8;
  686. }
  687. }
  688. *data_size = i * out_ch * (is32 ? 4 : 2);
  689. return 0;
  690. }
  691. static int output_data(MLPDecodeContext *m, unsigned int substr,
  692. uint8_t *data, unsigned int *data_size)
  693. {
  694. if (m->avctx->sample_fmt == SAMPLE_FMT_S32)
  695. return output_data_internal(m, substr, data, data_size, 1);
  696. else
  697. return output_data_internal(m, substr, data, data_size, 0);
  698. }
  699. /** Read an access unit from the stream.
  700. * Returns < 0 on error, 0 if not enough data is present in the input stream
  701. * otherwise returns the number of bytes consumed. */
  702. static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
  703. const uint8_t *buf, int buf_size)
  704. {
  705. MLPDecodeContext *m = avctx->priv_data;
  706. GetBitContext gb;
  707. unsigned int length, substr;
  708. unsigned int substream_start;
  709. unsigned int header_size = 4;
  710. unsigned int substr_header_size = 0;
  711. uint8_t substream_parity_present[MAX_SUBSTREAMS];
  712. uint16_t substream_data_len[MAX_SUBSTREAMS];
  713. uint8_t parity_bits;
  714. if (buf_size < 4)
  715. return 0;
  716. length = (AV_RB16(buf) & 0xfff) * 2;
  717. if (length > buf_size)
  718. return -1;
  719. init_get_bits(&gb, (buf + 4), (length - 4) * 8);
  720. if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
  721. dprintf(m->avctx, "Found major sync.\n");
  722. if (read_major_sync(m, &gb) < 0)
  723. goto error;
  724. header_size += 28;
  725. }
  726. if (!m->params_valid) {
  727. av_log(m->avctx, AV_LOG_WARNING,
  728. "Stream parameters not seen; skipping frame.\n");
  729. *data_size = 0;
  730. return length;
  731. }
  732. substream_start = 0;
  733. for (substr = 0; substr < m->num_substreams; substr++) {
  734. int extraword_present, checkdata_present, end;
  735. extraword_present = get_bits1(&gb);
  736. skip_bits1(&gb);
  737. checkdata_present = get_bits1(&gb);
  738. skip_bits1(&gb);
  739. end = get_bits(&gb, 12) * 2;
  740. substr_header_size += 2;
  741. if (extraword_present) {
  742. skip_bits(&gb, 16);
  743. substr_header_size += 2;
  744. }
  745. if (end + header_size + substr_header_size > length) {
  746. av_log(m->avctx, AV_LOG_ERROR,
  747. "Indicated length of substream %d data goes off end of "
  748. "packet.\n", substr);
  749. end = length - header_size - substr_header_size;
  750. }
  751. if (end < substream_start) {
  752. av_log(avctx, AV_LOG_ERROR,
  753. "Indicated end offset of substream %d data "
  754. "is smaller than calculated start offset.\n",
  755. substr);
  756. goto error;
  757. }
  758. if (substr > m->max_decoded_substream)
  759. continue;
  760. substream_parity_present[substr] = checkdata_present;
  761. substream_data_len[substr] = end - substream_start;
  762. substream_start = end;
  763. }
  764. parity_bits = ff_mlp_calculate_parity(buf, 4);
  765. parity_bits ^= ff_mlp_calculate_parity(buf + header_size, substr_header_size);
  766. if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
  767. av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
  768. goto error;
  769. }
  770. buf += header_size + substr_header_size;
  771. for (substr = 0; substr <= m->max_decoded_substream; substr++) {
  772. SubStream *s = &m->substream[substr];
  773. init_get_bits(&gb, buf, substream_data_len[substr] * 8);
  774. s->blockpos = 0;
  775. do {
  776. if (get_bits1(&gb)) {
  777. if (get_bits1(&gb)) {
  778. /* A restart header should be present. */
  779. if (read_restart_header(m, &gb, buf, substr) < 0)
  780. goto next_substr;
  781. s->restart_seen = 1;
  782. }
  783. if (!s->restart_seen) {
  784. av_log(m->avctx, AV_LOG_ERROR,
  785. "No restart header present in substream %d.\n",
  786. substr);
  787. goto next_substr;
  788. }
  789. if (read_decoding_params(m, &gb, substr) < 0)
  790. goto next_substr;
  791. }
  792. if (!s->restart_seen) {
  793. av_log(m->avctx, AV_LOG_ERROR,
  794. "No restart header present in substream %d.\n",
  795. substr);
  796. goto next_substr;
  797. }
  798. if (read_block_data(m, &gb, substr) < 0)
  799. return -1;
  800. } while ((get_bits_count(&gb) < substream_data_len[substr] * 8)
  801. && get_bits1(&gb) == 0);
  802. skip_bits(&gb, (-get_bits_count(&gb)) & 15);
  803. if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32 &&
  804. (show_bits_long(&gb, 32) == END_OF_STREAM ||
  805. show_bits_long(&gb, 20) == 0xd234e)) {
  806. skip_bits(&gb, 18);
  807. if (substr == m->max_decoded_substream)
  808. av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
  809. if (get_bits1(&gb)) {
  810. int shorten_by = get_bits(&gb, 13);
  811. shorten_by = FFMIN(shorten_by, s->blockpos);
  812. s->blockpos -= shorten_by;
  813. } else
  814. skip_bits(&gb, 13);
  815. }
  816. if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 16 &&
  817. substream_parity_present[substr]) {
  818. uint8_t parity, checksum;
  819. parity = ff_mlp_calculate_parity(buf, substream_data_len[substr] - 2);
  820. if ((parity ^ get_bits(&gb, 8)) != 0xa9)
  821. av_log(m->avctx, AV_LOG_ERROR,
  822. "Substream %d parity check failed.\n", substr);
  823. checksum = ff_mlp_checksum8(buf, substream_data_len[substr] - 2);
  824. if (checksum != get_bits(&gb, 8))
  825. av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n",
  826. substr);
  827. }
  828. if (substream_data_len[substr] * 8 != get_bits_count(&gb)) {
  829. av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n",
  830. substr);
  831. return -1;
  832. }
  833. next_substr:
  834. buf += substream_data_len[substr];
  835. }
  836. rematrix_channels(m, m->max_decoded_substream);
  837. if (output_data(m, m->max_decoded_substream, data, data_size) < 0)
  838. return -1;
  839. return length;
  840. error:
  841. m->params_valid = 0;
  842. return -1;
  843. }
  844. #if CONFIG_MLP_DECODER
  845. AVCodec mlp_decoder = {
  846. "mlp",
  847. CODEC_TYPE_AUDIO,
  848. CODEC_ID_MLP,
  849. sizeof(MLPDecodeContext),
  850. mlp_decode_init,
  851. NULL,
  852. NULL,
  853. read_access_unit,
  854. .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
  855. };
  856. #endif /* CONFIG_MLP_DECODER */
  857. #if CONFIG_TRUEHD_DECODER
  858. AVCodec truehd_decoder = {
  859. "truehd",
  860. CODEC_TYPE_AUDIO,
  861. CODEC_ID_TRUEHD,
  862. sizeof(MLPDecodeContext),
  863. mlp_decode_init,
  864. NULL,
  865. NULL,
  866. read_access_unit,
  867. .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
  868. };
  869. #endif /* CONFIG_TRUEHD_DECODER */