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