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