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.

1247 lines
43KB

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