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.

1232 lines
42KB

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