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.

1217 lines
41KB

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