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.

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