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.

1171 lines
39KB

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