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