mlpdec.c 35 KB

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