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.

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