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