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