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.

1205 lines
40KB

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