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.

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