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.

1273 lines
44KB

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