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.

1290 lines
46KB

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