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. avpriv_request_sample(m->avctx,
  273. "%d substreams (more than the "
  274. "maximum supported by the decoder)",
  275. mh.num_substreams);
  276. return AVERROR_PATCHWELCOME;
  277. }
  278. m->access_unit_size = mh.access_unit_size;
  279. m->access_unit_size_pow2 = mh.access_unit_size_pow2;
  280. m->num_substreams = mh.num_substreams;
  281. m->max_decoded_substream = m->num_substreams - 1;
  282. m->avctx->sample_rate = mh.group1_samplerate;
  283. m->avctx->frame_size = mh.access_unit_size;
  284. m->avctx->bits_per_raw_sample = mh.group1_bits;
  285. if (mh.group1_bits > 16)
  286. m->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  287. else
  288. m->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  289. m->params_valid = 1;
  290. for (substr = 0; substr < MAX_SUBSTREAMS; substr++)
  291. m->substream[substr].restart_seen = 0;
  292. /* Set the layout for each substream. When there's more than one, the first
  293. * substream is Stereo. Subsequent substreams' layouts are indicated in the
  294. * major sync. */
  295. if (m->avctx->codec_id == AV_CODEC_ID_MLP) {
  296. if ((substr = (mh.num_substreams > 1)))
  297. m->substream[0].ch_layout = AV_CH_LAYOUT_STEREO;
  298. m->substream[substr].ch_layout = mh.channel_layout_mlp;
  299. } else {
  300. if ((substr = (mh.num_substreams > 1)))
  301. m->substream[0].ch_layout = AV_CH_LAYOUT_STEREO;
  302. if (mh.num_substreams > 2)
  303. if (mh.channel_layout_thd_stream2)
  304. m->substream[2].ch_layout = mh.channel_layout_thd_stream2;
  305. else
  306. m->substream[2].ch_layout = mh.channel_layout_thd_stream1;
  307. m->substream[substr].ch_layout = mh.channel_layout_thd_stream1;
  308. if (m->avctx->channels<=2 && m->substream[substr].ch_layout == AV_CH_LAYOUT_MONO && m->max_decoded_substream == 1) {
  309. av_log(m->avctx, AV_LOG_DEBUG, "Mono stream with 2 substreams, ignoring 2nd\n");
  310. m->max_decoded_substream = 0;
  311. if (m->avctx->channels==2)
  312. m->avctx->channel_layout = AV_CH_LAYOUT_STEREO;
  313. }
  314. }
  315. m->needs_reordering = mh.channel_arrangement >= 18 && mh.channel_arrangement <= 20;
  316. return 0;
  317. }
  318. /** Read a restart header from a block in a substream. This contains parameters
  319. * required to decode the audio that do not change very often. Generally
  320. * (always) present only in blocks following a major sync. */
  321. static int read_restart_header(MLPDecodeContext *m, GetBitContext *gbp,
  322. const uint8_t *buf, unsigned int substr)
  323. {
  324. SubStream *s = &m->substream[substr];
  325. unsigned int ch;
  326. int sync_word, tmp;
  327. uint8_t checksum;
  328. uint8_t lossless_check;
  329. int start_count = get_bits_count(gbp);
  330. const int max_matrix_channel = m->avctx->codec_id == AV_CODEC_ID_MLP
  331. ? MAX_MATRIX_CHANNEL_MLP
  332. : MAX_MATRIX_CHANNEL_TRUEHD;
  333. int max_channel, min_channel, matrix_channel;
  334. sync_word = get_bits(gbp, 13);
  335. if (sync_word != 0x31ea >> 1) {
  336. av_log(m->avctx, AV_LOG_ERROR,
  337. "restart header sync incorrect (got 0x%04x)\n", sync_word);
  338. return AVERROR_INVALIDDATA;
  339. }
  340. s->noise_type = get_bits1(gbp);
  341. if (m->avctx->codec_id == AV_CODEC_ID_MLP && s->noise_type) {
  342. av_log(m->avctx, AV_LOG_ERROR, "MLP must have 0x31ea sync word.\n");
  343. return AVERROR_INVALIDDATA;
  344. }
  345. skip_bits(gbp, 16); /* Output timestamp */
  346. min_channel = get_bits(gbp, 4);
  347. max_channel = get_bits(gbp, 4);
  348. matrix_channel = get_bits(gbp, 4);
  349. if (matrix_channel > max_matrix_channel) {
  350. av_log(m->avctx, AV_LOG_ERROR,
  351. "Max matrix channel cannot be greater than %d.\n",
  352. max_matrix_channel);
  353. return AVERROR_INVALIDDATA;
  354. }
  355. if (max_channel != matrix_channel) {
  356. av_log(m->avctx, AV_LOG_ERROR,
  357. "Max channel must be equal max matrix channel.\n");
  358. return AVERROR_INVALIDDATA;
  359. }
  360. /* This should happen for TrueHD streams with >6 channels and MLP's noise
  361. * type. It is not yet known if this is allowed. */
  362. if (max_channel > MAX_MATRIX_CHANNEL_MLP && !s->noise_type) {
  363. avpriv_request_sample(m->avctx,
  364. "%d channels (more than the "
  365. "maximum supported by the decoder)",
  366. max_channel + 2);
  367. return AVERROR_PATCHWELCOME;
  368. }
  369. if (min_channel > max_channel) {
  370. av_log(m->avctx, AV_LOG_ERROR,
  371. "Substream min channel cannot be greater than max channel.\n");
  372. return AVERROR_INVALIDDATA;
  373. }
  374. s->min_channel = min_channel;
  375. s->max_channel = max_channel;
  376. s->max_matrix_channel = matrix_channel;
  377. #if FF_API_REQUEST_CHANNELS
  378. if (m->avctx->request_channels > 0 &&
  379. m->avctx->request_channels <= s->max_channel + 1 &&
  380. m->max_decoded_substream > substr) {
  381. av_log(m->avctx, AV_LOG_DEBUG,
  382. "Extracting %d-channel downmix from substream %d. "
  383. "Further substreams will be skipped.\n",
  384. s->max_channel + 1, substr);
  385. m->max_decoded_substream = substr;
  386. } else
  387. #endif
  388. if (m->avctx->request_channel_layout == s->ch_layout &&
  389. m->max_decoded_substream > substr) {
  390. av_log(m->avctx, AV_LOG_DEBUG,
  391. "Extracting %d-channel downmix (0x%"PRIx64") from substream %d. "
  392. "Further substreams will be skipped.\n",
  393. s->max_channel + 1, s->ch_layout, substr);
  394. m->max_decoded_substream = substr;
  395. }
  396. s->noise_shift = get_bits(gbp, 4);
  397. s->noisegen_seed = get_bits(gbp, 23);
  398. skip_bits(gbp, 19);
  399. s->data_check_present = get_bits1(gbp);
  400. lossless_check = get_bits(gbp, 8);
  401. if (substr == m->max_decoded_substream
  402. && s->lossless_check_data != 0xffffffff) {
  403. tmp = xor_32_to_8(s->lossless_check_data);
  404. if (tmp != lossless_check)
  405. av_log(m->avctx, AV_LOG_WARNING,
  406. "Lossless check failed - expected %02x, calculated %02x.\n",
  407. lossless_check, tmp);
  408. }
  409. skip_bits(gbp, 16);
  410. memset(s->ch_assign, 0, sizeof(s->ch_assign));
  411. for (ch = 0; ch <= s->max_matrix_channel; ch++) {
  412. int ch_assign = get_bits(gbp, 6);
  413. if (m->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
  414. uint64_t channel = thd_channel_layout_extract_channel(s->ch_layout,
  415. ch_assign);
  416. ch_assign = av_get_channel_layout_channel_index(s->ch_layout,
  417. channel);
  418. }
  419. if ((unsigned)ch_assign > s->max_matrix_channel) {
  420. avpriv_request_sample(m->avctx,
  421. "Assignment of matrix channel %d to invalid output channel %d",
  422. ch, ch_assign);
  423. return AVERROR_PATCHWELCOME;
  424. }
  425. s->ch_assign[ch_assign] = ch;
  426. }
  427. checksum = ff_mlp_restart_checksum(buf, get_bits_count(gbp) - start_count);
  428. if (checksum != get_bits(gbp, 8))
  429. av_log(m->avctx, AV_LOG_ERROR, "restart header checksum error\n");
  430. /* Set default decoding parameters. */
  431. s->param_presence_flags = 0xff;
  432. s->num_primitive_matrices = 0;
  433. s->blocksize = 8;
  434. s->lossless_check_data = 0;
  435. memset(s->output_shift , 0, sizeof(s->output_shift ));
  436. memset(s->quant_step_size, 0, sizeof(s->quant_step_size));
  437. for (ch = s->min_channel; ch <= s->max_channel; ch++) {
  438. ChannelParams *cp = &s->channel_params[ch];
  439. cp->filter_params[FIR].order = 0;
  440. cp->filter_params[IIR].order = 0;
  441. cp->filter_params[FIR].shift = 0;
  442. cp->filter_params[IIR].shift = 0;
  443. /* Default audio coding is 24-bit raw PCM. */
  444. cp->huff_offset = 0;
  445. cp->sign_huff_offset = (-1) << 23;
  446. cp->codebook = 0;
  447. cp->huff_lsbs = 24;
  448. }
  449. if (substr == m->max_decoded_substream) {
  450. m->avctx->channels = s->max_matrix_channel + 1;
  451. m->avctx->channel_layout = s->ch_layout;
  452. if (m->avctx->codec_id == AV_CODEC_ID_MLP && m->needs_reordering) {
  453. if (m->avctx->channel_layout == (AV_CH_LAYOUT_QUAD|AV_CH_LOW_FREQUENCY) ||
  454. m->avctx->channel_layout == AV_CH_LAYOUT_5POINT0_BACK) {
  455. int i = s->ch_assign[4];
  456. s->ch_assign[4] = s->ch_assign[3];
  457. s->ch_assign[3] = s->ch_assign[2];
  458. s->ch_assign[2] = i;
  459. } else if (m->avctx->channel_layout == AV_CH_LAYOUT_5POINT1_BACK) {
  460. FFSWAP(int, s->ch_assign[2], s->ch_assign[4]);
  461. FFSWAP(int, s->ch_assign[3], s->ch_assign[5]);
  462. }
  463. }
  464. }
  465. return 0;
  466. }
  467. /** Read parameters for one of the prediction filters. */
  468. static int read_filter_params(MLPDecodeContext *m, GetBitContext *gbp,
  469. unsigned int substr, unsigned int channel,
  470. unsigned int filter)
  471. {
  472. SubStream *s = &m->substream[substr];
  473. FilterParams *fp = &s->channel_params[channel].filter_params[filter];
  474. const int max_order = filter ? MAX_IIR_ORDER : MAX_FIR_ORDER;
  475. const char fchar = filter ? 'I' : 'F';
  476. int i, order;
  477. // Filter is 0 for FIR, 1 for IIR.
  478. av_assert0(filter < 2);
  479. if (m->filter_changed[channel][filter]++ > 1) {
  480. av_log(m->avctx, AV_LOG_ERROR, "Filters may change only once per access unit.\n");
  481. return AVERROR_INVALIDDATA;
  482. }
  483. order = get_bits(gbp, 4);
  484. if (order > max_order) {
  485. av_log(m->avctx, AV_LOG_ERROR,
  486. "%cIR filter order %d is greater than maximum %d.\n",
  487. fchar, order, max_order);
  488. return AVERROR_INVALIDDATA;
  489. }
  490. fp->order = order;
  491. if (order > 0) {
  492. int32_t *fcoeff = s->channel_params[channel].coeff[filter];
  493. int coeff_bits, coeff_shift;
  494. fp->shift = get_bits(gbp, 4);
  495. coeff_bits = get_bits(gbp, 5);
  496. coeff_shift = get_bits(gbp, 3);
  497. if (coeff_bits < 1 || coeff_bits > 16) {
  498. av_log(m->avctx, AV_LOG_ERROR,
  499. "%cIR filter coeff_bits must be between 1 and 16.\n",
  500. fchar);
  501. return AVERROR_INVALIDDATA;
  502. }
  503. if (coeff_bits + coeff_shift > 16) {
  504. av_log(m->avctx, AV_LOG_ERROR,
  505. "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
  506. fchar);
  507. return AVERROR_INVALIDDATA;
  508. }
  509. for (i = 0; i < order; i++)
  510. fcoeff[i] = get_sbits(gbp, coeff_bits) << coeff_shift;
  511. if (get_bits1(gbp)) {
  512. int state_bits, state_shift;
  513. if (filter == FIR) {
  514. av_log(m->avctx, AV_LOG_ERROR,
  515. "FIR filter has state data specified.\n");
  516. return AVERROR_INVALIDDATA;
  517. }
  518. state_bits = get_bits(gbp, 4);
  519. state_shift = get_bits(gbp, 4);
  520. /* TODO: Check validity of state data. */
  521. for (i = 0; i < order; i++)
  522. fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
  523. }
  524. }
  525. return 0;
  526. }
  527. /** Read parameters for primitive matrices. */
  528. static int read_matrix_params(MLPDecodeContext *m, unsigned int substr, GetBitContext *gbp)
  529. {
  530. SubStream *s = &m->substream[substr];
  531. unsigned int mat, ch;
  532. const int max_primitive_matrices = m->avctx->codec_id == AV_CODEC_ID_MLP
  533. ? MAX_MATRICES_MLP
  534. : MAX_MATRICES_TRUEHD;
  535. if (m->matrix_changed++ > 1) {
  536. av_log(m->avctx, AV_LOG_ERROR, "Matrices may change only once per access unit.\n");
  537. return AVERROR_INVALIDDATA;
  538. }
  539. s->num_primitive_matrices = get_bits(gbp, 4);
  540. if (s->num_primitive_matrices > max_primitive_matrices) {
  541. av_log(m->avctx, AV_LOG_ERROR,
  542. "Number of primitive matrices cannot be greater than %d.\n",
  543. max_primitive_matrices);
  544. return AVERROR_INVALIDDATA;
  545. }
  546. for (mat = 0; mat < s->num_primitive_matrices; mat++) {
  547. int frac_bits, max_chan;
  548. s->matrix_out_ch[mat] = get_bits(gbp, 4);
  549. frac_bits = get_bits(gbp, 4);
  550. s->lsb_bypass [mat] = get_bits1(gbp);
  551. if (s->matrix_out_ch[mat] > s->max_matrix_channel) {
  552. av_log(m->avctx, AV_LOG_ERROR,
  553. "Invalid channel %d specified as output from matrix.\n",
  554. s->matrix_out_ch[mat]);
  555. return AVERROR_INVALIDDATA;
  556. }
  557. if (frac_bits > 14) {
  558. av_log(m->avctx, AV_LOG_ERROR,
  559. "Too many fractional bits specified.\n");
  560. return AVERROR_INVALIDDATA;
  561. }
  562. max_chan = s->max_matrix_channel;
  563. if (!s->noise_type)
  564. max_chan+=2;
  565. for (ch = 0; ch <= max_chan; ch++) {
  566. int coeff_val = 0;
  567. if (get_bits1(gbp))
  568. coeff_val = get_sbits(gbp, frac_bits + 2);
  569. s->matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
  570. }
  571. if (s->noise_type)
  572. s->matrix_noise_shift[mat] = get_bits(gbp, 4);
  573. else
  574. s->matrix_noise_shift[mat] = 0;
  575. }
  576. return 0;
  577. }
  578. /** Read channel parameters. */
  579. static int read_channel_params(MLPDecodeContext *m, unsigned int substr,
  580. GetBitContext *gbp, unsigned int ch)
  581. {
  582. SubStream *s = &m->substream[substr];
  583. ChannelParams *cp = &s->channel_params[ch];
  584. FilterParams *fir = &cp->filter_params[FIR];
  585. FilterParams *iir = &cp->filter_params[IIR];
  586. int ret;
  587. if (s->param_presence_flags & PARAM_FIR)
  588. if (get_bits1(gbp))
  589. if ((ret = read_filter_params(m, gbp, substr, ch, FIR)) < 0)
  590. return ret;
  591. if (s->param_presence_flags & PARAM_IIR)
  592. if (get_bits1(gbp))
  593. if ((ret = read_filter_params(m, gbp, substr, ch, IIR)) < 0)
  594. return ret;
  595. if (fir->order + iir->order > 8) {
  596. av_log(m->avctx, AV_LOG_ERROR, "Total filter orders too high.\n");
  597. return AVERROR_INVALIDDATA;
  598. }
  599. if (fir->order && iir->order &&
  600. fir->shift != iir->shift) {
  601. av_log(m->avctx, AV_LOG_ERROR,
  602. "FIR and IIR filters must use the same precision.\n");
  603. return AVERROR_INVALIDDATA;
  604. }
  605. /* The FIR and IIR filters must have the same precision.
  606. * To simplify the filtering code, only the precision of the
  607. * FIR filter is considered. If only the IIR filter is employed,
  608. * the FIR filter precision is set to that of the IIR filter, so
  609. * that the filtering code can use it. */
  610. if (!fir->order && iir->order)
  611. fir->shift = iir->shift;
  612. if (s->param_presence_flags & PARAM_HUFFOFFSET)
  613. if (get_bits1(gbp))
  614. cp->huff_offset = get_sbits(gbp, 15);
  615. cp->codebook = get_bits(gbp, 2);
  616. cp->huff_lsbs = get_bits(gbp, 5);
  617. if (cp->huff_lsbs > 24) {
  618. av_log(m->avctx, AV_LOG_ERROR, "Invalid huff_lsbs.\n");
  619. cp->huff_lsbs = 0;
  620. return AVERROR_INVALIDDATA;
  621. }
  622. cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
  623. return 0;
  624. }
  625. /** Read decoding parameters that change more often than those in the restart
  626. * header. */
  627. static int read_decoding_params(MLPDecodeContext *m, GetBitContext *gbp,
  628. unsigned int substr)
  629. {
  630. SubStream *s = &m->substream[substr];
  631. unsigned int ch;
  632. int ret;
  633. if (s->param_presence_flags & PARAM_PRESENCE)
  634. if (get_bits1(gbp))
  635. s->param_presence_flags = get_bits(gbp, 8);
  636. if (s->param_presence_flags & PARAM_BLOCKSIZE)
  637. if (get_bits1(gbp)) {
  638. s->blocksize = get_bits(gbp, 9);
  639. if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {
  640. av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.\n");
  641. s->blocksize = 0;
  642. return AVERROR_INVALIDDATA;
  643. }
  644. }
  645. if (s->param_presence_flags & PARAM_MATRIX)
  646. if (get_bits1(gbp))
  647. if ((ret = read_matrix_params(m, substr, gbp)) < 0)
  648. return ret;
  649. if (s->param_presence_flags & PARAM_OUTSHIFT)
  650. if (get_bits1(gbp))
  651. for (ch = 0; ch <= s->max_matrix_channel; ch++)
  652. s->output_shift[ch] = get_sbits(gbp, 4);
  653. if (s->param_presence_flags & PARAM_QUANTSTEP)
  654. if (get_bits1(gbp))
  655. for (ch = 0; ch <= s->max_channel; ch++) {
  656. ChannelParams *cp = &s->channel_params[ch];
  657. s->quant_step_size[ch] = get_bits(gbp, 4);
  658. cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);
  659. }
  660. for (ch = s->min_channel; ch <= s->max_channel; ch++)
  661. if (get_bits1(gbp))
  662. if ((ret = read_channel_params(m, substr, gbp, ch)) < 0)
  663. return ret;
  664. return 0;
  665. }
  666. #define MSB_MASK(bits) (-1u << bits)
  667. /** Generate PCM samples using the prediction filters and residual values
  668. * read from the data stream, and update the filter state. */
  669. static void filter_channel(MLPDecodeContext *m, unsigned int substr,
  670. unsigned int channel)
  671. {
  672. SubStream *s = &m->substream[substr];
  673. const int32_t *fircoeff = s->channel_params[channel].coeff[FIR];
  674. int32_t state_buffer[NUM_FILTERS][MAX_BLOCKSIZE + MAX_FIR_ORDER];
  675. int32_t *firbuf = state_buffer[FIR] + MAX_BLOCKSIZE;
  676. int32_t *iirbuf = state_buffer[IIR] + MAX_BLOCKSIZE;
  677. FilterParams *fir = &s->channel_params[channel].filter_params[FIR];
  678. FilterParams *iir = &s->channel_params[channel].filter_params[IIR];
  679. unsigned int filter_shift = fir->shift;
  680. int32_t mask = MSB_MASK(s->quant_step_size[channel]);
  681. memcpy(firbuf, fir->state, MAX_FIR_ORDER * sizeof(int32_t));
  682. memcpy(iirbuf, iir->state, MAX_IIR_ORDER * sizeof(int32_t));
  683. m->dsp.mlp_filter_channel(firbuf, fircoeff,
  684. fir->order, iir->order,
  685. filter_shift, mask, s->blocksize,
  686. &m->sample_buffer[s->blockpos][channel]);
  687. memcpy(fir->state, firbuf - s->blocksize, MAX_FIR_ORDER * sizeof(int32_t));
  688. memcpy(iir->state, iirbuf - s->blocksize, MAX_IIR_ORDER * sizeof(int32_t));
  689. }
  690. /** Read a block of PCM residual data (or actual if no filtering active). */
  691. static int read_block_data(MLPDecodeContext *m, GetBitContext *gbp,
  692. unsigned int substr)
  693. {
  694. SubStream *s = &m->substream[substr];
  695. unsigned int i, ch, expected_stream_pos = 0;
  696. int ret;
  697. if (s->data_check_present) {
  698. expected_stream_pos = get_bits_count(gbp);
  699. expected_stream_pos += get_bits(gbp, 16);
  700. avpriv_request_sample(m->avctx,
  701. "Substreams with VLC block size check info");
  702. }
  703. if (s->blockpos + s->blocksize > m->access_unit_size) {
  704. av_log(m->avctx, AV_LOG_ERROR, "too many audio samples in frame\n");
  705. return AVERROR_INVALIDDATA;
  706. }
  707. memset(&m->bypassed_lsbs[s->blockpos][0], 0,
  708. s->blocksize * sizeof(m->bypassed_lsbs[0]));
  709. for (i = 0; i < s->blocksize; i++)
  710. if ((ret = read_huff_channels(m, gbp, substr, i)) < 0)
  711. return ret;
  712. for (ch = s->min_channel; ch <= s->max_channel; ch++)
  713. filter_channel(m, substr, ch);
  714. s->blockpos += s->blocksize;
  715. if (s->data_check_present) {
  716. if (get_bits_count(gbp) != expected_stream_pos)
  717. av_log(m->avctx, AV_LOG_ERROR, "block data length mismatch\n");
  718. skip_bits(gbp, 8);
  719. }
  720. return 0;
  721. }
  722. /** Data table used for TrueHD noise generation function. */
  723. static const int8_t noise_table[256] = {
  724. 30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
  725. 52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
  726. 10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
  727. 51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
  728. 38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
  729. 61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
  730. 67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
  731. 48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
  732. 0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
  733. 16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
  734. 13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
  735. 89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
  736. 36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
  737. 39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
  738. 45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
  739. -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
  740. };
  741. /** Noise generation functions.
  742. * I'm not sure what these are for - they seem to be some kind of pseudorandom
  743. * sequence generators, used to generate noise data which is used when the
  744. * channels are rematrixed. I'm not sure if they provide a practical benefit
  745. * to compression, or just obfuscate the decoder. Are they for some kind of
  746. * dithering? */
  747. /** Generate two channels of noise, used in the matrix when
  748. * restart sync word == 0x31ea. */
  749. static void generate_2_noise_channels(MLPDecodeContext *m, unsigned int substr)
  750. {
  751. SubStream *s = &m->substream[substr];
  752. unsigned int i;
  753. uint32_t seed = s->noisegen_seed;
  754. unsigned int maxchan = s->max_matrix_channel;
  755. for (i = 0; i < s->blockpos; i++) {
  756. uint16_t seed_shr7 = seed >> 7;
  757. m->sample_buffer[i][maxchan+1] = ((int8_t)(seed >> 15)) << s->noise_shift;
  758. m->sample_buffer[i][maxchan+2] = ((int8_t) seed_shr7) << s->noise_shift;
  759. seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
  760. }
  761. s->noisegen_seed = seed;
  762. }
  763. /** Generate a block of noise, used when restart sync word == 0x31eb. */
  764. static void fill_noise_buffer(MLPDecodeContext *m, unsigned int substr)
  765. {
  766. SubStream *s = &m->substream[substr];
  767. unsigned int i;
  768. uint32_t seed = s->noisegen_seed;
  769. for (i = 0; i < m->access_unit_size_pow2; i++) {
  770. uint8_t seed_shr15 = seed >> 15;
  771. m->noise_buffer[i] = noise_table[seed_shr15];
  772. seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
  773. }
  774. s->noisegen_seed = seed;
  775. }
  776. /** Apply the channel matrices in turn to reconstruct the original audio
  777. * samples. */
  778. static void rematrix_channels(MLPDecodeContext *m, unsigned int substr)
  779. {
  780. SubStream *s = &m->substream[substr];
  781. unsigned int mat, src_ch, i;
  782. unsigned int maxchan;
  783. maxchan = s->max_matrix_channel;
  784. if (!s->noise_type) {
  785. generate_2_noise_channels(m, substr);
  786. maxchan += 2;
  787. } else {
  788. fill_noise_buffer(m, substr);
  789. }
  790. for (mat = 0; mat < s->num_primitive_matrices; mat++) {
  791. int matrix_noise_shift = s->matrix_noise_shift[mat];
  792. unsigned int dest_ch = s->matrix_out_ch[mat];
  793. int32_t mask = MSB_MASK(s->quant_step_size[dest_ch]);
  794. int32_t *coeffs = s->matrix_coeff[mat];
  795. int index = s->num_primitive_matrices - mat;
  796. int index2 = 2 * index + 1;
  797. /* TODO: DSPContext? */
  798. for (i = 0; i < s->blockpos; i++) {
  799. int32_t bypassed_lsb = m->bypassed_lsbs[i][mat];
  800. int32_t *samples = m->sample_buffer[i];
  801. int64_t accum = 0;
  802. for (src_ch = 0; src_ch <= maxchan; src_ch++)
  803. accum += (int64_t) samples[src_ch] * coeffs[src_ch];
  804. if (matrix_noise_shift) {
  805. index &= m->access_unit_size_pow2 - 1;
  806. accum += m->noise_buffer[index] << (matrix_noise_shift + 7);
  807. index += index2;
  808. }
  809. samples[dest_ch] = ((accum >> 14) & mask) + bypassed_lsb;
  810. }
  811. }
  812. }
  813. /** Write the audio data into the output buffer. */
  814. static int output_data(MLPDecodeContext *m, unsigned int substr,
  815. AVFrame *frame, int *got_frame_ptr)
  816. {
  817. AVCodecContext *avctx = m->avctx;
  818. SubStream *s = &m->substream[substr];
  819. unsigned int i, out_ch = 0;
  820. int32_t *data_32;
  821. int16_t *data_16;
  822. int ret;
  823. int is32 = (m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);
  824. if (m->avctx->channels != s->max_matrix_channel + 1) {
  825. av_log(m->avctx, AV_LOG_ERROR, "channel count mismatch\n");
  826. return AVERROR_INVALIDDATA;
  827. }
  828. if (!s->blockpos) {
  829. av_log(avctx, AV_LOG_ERROR, "No samples to output.\n");
  830. return AVERROR_INVALIDDATA;
  831. }
  832. /* get output buffer */
  833. frame->nb_samples = s->blockpos;
  834. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  835. return ret;
  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 */