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.

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