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.

1192 lines
39KB

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