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.

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