Mercurial > libavcodec.hg
annotate apedec.c @ 6442:28e45bf84973 libavcodec
Prevent segfault due to reading over the end of the input buffer.
author | michael |
---|---|
date | Tue, 04 Mar 2008 21:39:21 +0000 |
parents | f07ad27251e5 |
children | e3adb7e96812 |
rev | line source |
---|---|
5673 | 1 /* |
2 * Monkey's Audio lossless audio decoder | |
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org> | |
4 * based upon libdemac from Dave Chapman. | |
5 * | |
6 * This file is part of FFmpeg. | |
7 * | |
8 * FFmpeg is free software; you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public | |
10 * License as published by the Free Software Foundation; either | |
11 * version 2.1 of the License, or (at your option) any later version. | |
12 * | |
13 * FFmpeg is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with FFmpeg; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 */ | |
22 | |
23 #define ALT_BITSTREAM_READER_LE | |
24 #include "avcodec.h" | |
25 #include "dsputil.h" | |
26 #include "bitstream.h" | |
27 #include "bytestream.h" | |
28 | |
29 /** | |
30 * @file apedec.c | |
31 * Monkey's Audio lossless audio decoder | |
32 */ | |
33 | |
34 #define BLOCKS_PER_LOOP 4608 | |
35 #define MAX_CHANNELS 2 | |
36 #define MAX_BYTESPERSAMPLE 3 | |
37 | |
38 #define APE_FRAMECODE_MONO_SILENCE 1 | |
39 #define APE_FRAMECODE_STEREO_SILENCE 3 | |
40 #define APE_FRAMECODE_PSEUDO_STEREO 4 | |
41 | |
42 #define HISTORY_SIZE 512 | |
43 #define PREDICTOR_ORDER 8 | |
44 /** Total size of all predictor histories */ | |
45 #define PREDICTOR_SIZE 50 | |
46 | |
47 #define YDELAYA (18 + PREDICTOR_ORDER*4) | |
48 #define YDELAYB (18 + PREDICTOR_ORDER*3) | |
49 #define XDELAYA (18 + PREDICTOR_ORDER*2) | |
50 #define XDELAYB (18 + PREDICTOR_ORDER) | |
51 | |
52 #define YADAPTCOEFFSA 18 | |
53 #define XADAPTCOEFFSA 14 | |
54 #define YADAPTCOEFFSB 10 | |
55 #define XADAPTCOEFFSB 5 | |
56 | |
57 /** | |
58 * Possible compression levels | |
59 * @{ | |
60 */ | |
61 enum APECompressionLevel { | |
62 COMPRESSION_LEVEL_FAST = 1000, | |
63 COMPRESSION_LEVEL_NORMAL = 2000, | |
64 COMPRESSION_LEVEL_HIGH = 3000, | |
65 COMPRESSION_LEVEL_EXTRA_HIGH = 4000, | |
66 COMPRESSION_LEVEL_INSANE = 5000 | |
67 }; | |
68 /** @} */ | |
69 | |
70 #define APE_FILTER_LEVELS 3 | |
71 | |
72 /** Filter orders depending on compression level */ | |
73 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = { | |
74 { 0, 0, 0 }, | |
75 { 16, 0, 0 }, | |
76 { 64, 0, 0 }, | |
77 { 32, 256, 0 }, | |
78 { 16, 256, 1280 } | |
79 }; | |
80 | |
81 /** Filter fraction bits depending on compression level */ | |
82 static const uint16_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = { | |
83 { 0, 0, 0 }, | |
84 { 11, 0, 0 }, | |
85 { 11, 0, 0 }, | |
86 { 10, 13, 0 }, | |
87 { 11, 13, 15 } | |
88 }; | |
89 | |
90 | |
91 /** Filters applied to the decoded data */ | |
92 typedef struct APEFilter { | |
93 int16_t *coeffs; ///< actual coefficients used in filtering | |
94 int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients | |
95 int16_t *historybuffer; ///< filter memory | |
96 int16_t *delay; ///< filtered values | |
97 | |
98 int avg; | |
99 } APEFilter; | |
100 | |
101 typedef struct APERice { | |
102 uint32_t k; | |
103 uint32_t ksum; | |
104 } APERice; | |
105 | |
106 typedef struct APERangecoder { | |
107 uint32_t low; ///< low end of interval | |
108 uint32_t range; ///< length of interval | |
109 uint32_t help; ///< bytes_to_follow resp. intermediate value | |
110 unsigned int buffer; ///< buffer for input/output | |
111 } APERangecoder; | |
112 | |
113 /** Filter histories */ | |
114 typedef struct APEPredictor { | |
115 int32_t *buf; | |
116 | |
117 int32_t lastA[2]; | |
118 | |
119 int32_t filterA[2]; | |
120 int32_t filterB[2]; | |
121 | |
122 int32_t coeffsA[2][4]; ///< adaption coefficients | |
123 int32_t coeffsB[2][5]; ///< adaption coefficients | |
124 int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE]; | |
125 } APEPredictor; | |
126 | |
127 /** Decoder context */ | |
128 typedef struct APEContext { | |
129 AVCodecContext *avctx; | |
130 DSPContext dsp; | |
131 int channels; | |
132 int samples; ///< samples left to decode in current frame | |
133 | |
134 int fileversion; ///< codec version, very important in decoding process | |
135 int compression_level; ///< compression levels | |
136 int fset; ///< which filter set to use (calculated from compression level) | |
137 int flags; ///< global decoder flags | |
138 | |
139 uint32_t CRC; ///< frame CRC | |
140 int frameflags; ///< frame flags | |
141 int currentframeblocks; ///< samples (per channel) in current frame | |
142 int blocksdecoded; ///< count of decoded samples in current frame | |
143 APEPredictor predictor; ///< predictor used for final reconstruction | |
144 | |
145 int32_t decoded0[BLOCKS_PER_LOOP]; ///< decoded data for the first channel | |
146 int32_t decoded1[BLOCKS_PER_LOOP]; ///< decoded data for the second channel | |
147 | |
148 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory | |
149 | |
150 APERangecoder rc; ///< rangecoder used to decode actual values | |
151 APERice riceX; ///< rice code parameters for the second channel | |
152 APERice riceY; ///< rice code parameters for the first channel | |
153 APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction | |
154 | |
155 uint8_t *data; ///< current frame data | |
156 uint8_t *data_end; ///< frame data end | |
6223 | 157 const uint8_t *ptr; ///< current position in frame data |
158 const uint8_t *last_ptr; ///< position where last 4608-sample block ended | |
5673 | 159 } APEContext; |
160 | |
161 // TODO: dsputilize | |
162 static inline void vector_add(int16_t * v1, int16_t * v2, int order) | |
163 { | |
164 while (order--) | |
165 *v1++ += *v2++; | |
166 } | |
167 | |
168 // TODO: dsputilize | |
169 static inline void vector_sub(int16_t * v1, int16_t * v2, int order) | |
170 { | |
171 while (order--) | |
172 *v1++ -= *v2++; | |
173 } | |
174 | |
175 // TODO: dsputilize | |
176 static inline int32_t scalarproduct(int16_t * v1, int16_t * v2, int order) | |
177 { | |
178 int res = 0; | |
179 | |
180 while (order--) | |
181 res += *v1++ * *v2++; | |
182 | |
183 return res; | |
184 } | |
185 | |
186 static int ape_decode_init(AVCodecContext * avctx) | |
187 { | |
188 APEContext *s = avctx->priv_data; | |
189 int i; | |
190 | |
191 if (avctx->extradata_size != 6) { | |
192 av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n"); | |
193 return -1; | |
194 } | |
195 if (avctx->bits_per_sample != 16) { | |
196 av_log(avctx, AV_LOG_ERROR, "Only 16-bit samples are supported\n"); | |
197 return -1; | |
198 } | |
199 if (avctx->channels > 2) { | |
200 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n"); | |
201 return -1; | |
202 } | |
203 s->avctx = avctx; | |
204 s->channels = avctx->channels; | |
205 s->fileversion = AV_RL16(avctx->extradata); | |
206 s->compression_level = AV_RL16(avctx->extradata + 2); | |
207 s->flags = AV_RL16(avctx->extradata + 4); | |
208 | |
209 av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n", s->compression_level, s->flags); | |
210 if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE) { | |
211 av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", s->compression_level); | |
212 return -1; | |
213 } | |
214 s->fset = s->compression_level / 1000 - 1; | |
215 for (i = 0; i < APE_FILTER_LEVELS; i++) { | |
216 if (!ape_filter_orders[s->fset][i]) | |
217 break; | |
218 s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4); | |
219 } | |
220 | |
221 dsputil_init(&s->dsp, avctx); | |
222 return 0; | |
223 } | |
224 | |
225 static int ape_decode_close(AVCodecContext * avctx) | |
226 { | |
227 APEContext *s = avctx->priv_data; | |
228 int i; | |
229 | |
230 for (i = 0; i < APE_FILTER_LEVELS; i++) | |
231 av_freep(&s->filterbuf[i]); | |
232 | |
233 return 0; | |
234 } | |
235 | |
236 /** | |
237 * @defgroup rangecoder APE range decoder | |
238 * @{ | |
239 */ | |
240 | |
241 #define CODE_BITS 32 | |
242 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1)) | |
243 #define SHIFT_BITS (CODE_BITS - 9) | |
244 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1) | |
245 #define BOTTOM_VALUE (TOP_VALUE >> 8) | |
246 | |
247 /** Start the decoder */ | |
248 static inline void range_start_decoding(APEContext * ctx) | |
249 { | |
250 ctx->rc.buffer = bytestream_get_byte(&ctx->ptr); | |
251 ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS); | |
252 ctx->rc.range = (uint32_t) 1 << EXTRA_BITS; | |
253 } | |
254 | |
255 /** Perform normalization */ | |
256 static inline void range_dec_normalize(APEContext * ctx) | |
257 { | |
258 while (ctx->rc.range <= BOTTOM_VALUE) { | |
6442
28e45bf84973
Prevent segfault due to reading over the end of the input buffer.
michael
parents:
6223
diff
changeset
|
259 ctx->rc.buffer <<= 8; |
28e45bf84973
Prevent segfault due to reading over the end of the input buffer.
michael
parents:
6223
diff
changeset
|
260 if(ctx->ptr < ctx->data_end) |
28e45bf84973
Prevent segfault due to reading over the end of the input buffer.
michael
parents:
6223
diff
changeset
|
261 ctx->rc.buffer += *ctx->ptr; |
28e45bf84973
Prevent segfault due to reading over the end of the input buffer.
michael
parents:
6223
diff
changeset
|
262 ctx->ptr++; |
5673 | 263 ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF); |
264 ctx->rc.range <<= 8; | |
265 } | |
266 } | |
267 | |
268 /** | |
269 * Calculate culmulative frequency for next symbol. Does NO update! | |
270 * @param tot_f is the total frequency or (code_value)1<<shift | |
271 * @return the culmulative frequency | |
272 */ | |
273 static inline int range_decode_culfreq(APEContext * ctx, int tot_f) | |
274 { | |
275 range_dec_normalize(ctx); | |
276 ctx->rc.help = ctx->rc.range / tot_f; | |
277 return ctx->rc.low / ctx->rc.help; | |
278 } | |
279 | |
280 /** | |
281 * Decode value with given size in bits | |
282 * @param shift number of bits to decode | |
283 */ | |
284 static inline int range_decode_culshift(APEContext * ctx, int shift) | |
285 { | |
286 range_dec_normalize(ctx); | |
287 ctx->rc.help = ctx->rc.range >> shift; | |
288 return ctx->rc.low / ctx->rc.help; | |
289 } | |
290 | |
291 | |
292 /** | |
293 * Update decoding state | |
294 * @param sy_f the interval length (frequency of the symbol) | |
295 * @param lt_f the lower end (frequency sum of < symbols) | |
296 */ | |
297 static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f) | |
298 { | |
299 ctx->rc.low -= ctx->rc.help * lt_f; | |
300 ctx->rc.range = ctx->rc.help * sy_f; | |
301 } | |
302 | |
303 /** Decode n bits (n <= 16) without modelling */ | |
304 static inline int range_decode_bits(APEContext * ctx, int n) | |
305 { | |
306 int sym = range_decode_culshift(ctx, n); | |
307 range_decode_update(ctx, 1, sym); | |
308 return sym; | |
309 } | |
310 | |
311 | |
312 #define MODEL_ELEMENTS 64 | |
313 | |
314 /** | |
315 * Fixed probabilities for symbols in Monkey Audio version 3.97 | |
316 */ | |
317 static const uint32_t counts_3970[65] = { | |
318 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926, | |
319 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419, | |
320 65450, 65469, 65480, 65487, 65491, 65493, 65494, 65495, | |
321 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, | |
322 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, | |
323 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, | |
324 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, | |
325 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535, | |
326 65536 | |
327 }; | |
328 | |
329 /** | |
330 * Probability ranges for symbols in Monkey Audio version 3.97 | |
331 */ | |
332 static const uint16_t counts_diff_3970[64] = { | |
333 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756, | |
334 1104, 677, 415, 248, 150, 89, 54, 31, | |
335 19, 11, 7, 4, 2, 1, 1, 1, | |
336 1, 1, 1, 1, 1, 1, 1, 1, | |
337 1, 1, 1, 1, 1, 1, 1, 1, | |
338 1, 1, 1, 1, 1, 1, 1, 1, | |
339 1, 1, 1, 1, 1, 1, 1, 1, | |
340 1, 1, 1, 1, 1, 1, 1, 1 | |
341 }; | |
342 | |
343 /** | |
344 * Fixed probabilities for symbols in Monkey Audio version 3.98 | |
345 */ | |
346 static const uint32_t counts_3980[65] = { | |
347 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435, | |
348 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482, | |
349 65485, 65488, 65490, 65491, 65492, 65493, 65494, 65495, | |
350 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, | |
351 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, | |
352 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, | |
353 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, | |
354 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535, | |
355 65536 | |
356 }; | |
357 | |
358 /** | |
359 * Probability ranges for symbols in Monkey Audio version 3.98 | |
360 */ | |
361 static const uint16_t counts_diff_3980[64] = { | |
362 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536, | |
363 261, 119, 65, 31, 19, 10, 6, 3, | |
364 3, 2, 1, 1, 1, 1, 1, 1, | |
365 1, 1, 1, 1, 1, 1, 1, 1, | |
366 1, 1, 1, 1, 1, 1, 1, 1, | |
367 1, 1, 1, 1, 1, 1, 1, 1, | |
368 1, 1, 1, 1, 1, 1, 1, 1, | |
369 1, 1, 1, 1, 1, 1, 1, 1 | |
370 }; | |
371 | |
372 /** | |
373 * Decode symbol | |
374 * @param counts probability range start position | |
375 * @param count_diffs probability range widths | |
376 */ | |
377 static inline int range_get_symbol(APEContext * ctx, | |
378 const uint32_t counts[], | |
379 const uint16_t counts_diff[]) | |
380 { | |
381 int symbol, cf; | |
382 | |
383 cf = range_decode_culshift(ctx, 16); | |
384 | |
385 /* figure out the symbol inefficiently; a binary search would be much better */ | |
386 for (symbol = 0; counts[symbol + 1] <= cf; symbol++); | |
387 | |
388 range_decode_update(ctx, counts_diff[symbol], counts[symbol]); | |
389 | |
390 return symbol; | |
391 } | |
392 /** @} */ // group rangecoder | |
393 | |
394 static inline void update_rice(APERice *rice, int x) | |
395 { | |
396 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5); | |
397 | |
398 if (rice->k == 0) | |
399 rice->k = 1; | |
400 else if (rice->ksum < (1 << (rice->k + 4))) | |
401 rice->k--; | |
402 else if (rice->ksum >= (1 << (rice->k + 5))) | |
403 rice->k++; | |
404 } | |
405 | |
406 static inline int ape_decode_value(APEContext * ctx, APERice *rice) | |
407 { | |
408 int x, overflow; | |
409 | |
410 if (ctx->fileversion < 3980) { | |
411 int tmpk; | |
412 | |
413 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970); | |
414 | |
415 if (overflow == (MODEL_ELEMENTS - 1)) { | |
416 tmpk = range_decode_bits(ctx, 5); | |
417 overflow = 0; | |
418 } else | |
419 tmpk = (rice->k < 1) ? 0 : rice->k - 1; | |
420 | |
421 if (tmpk <= 16) | |
422 x = range_decode_bits(ctx, tmpk); | |
423 else { | |
424 x = range_decode_bits(ctx, 16); | |
425 x |= (range_decode_bits(ctx, tmpk - 16) << 16); | |
426 } | |
427 x += overflow << tmpk; | |
428 } else { | |
429 int base, pivot; | |
430 | |
431 pivot = rice->ksum >> 5; | |
432 if (pivot == 0) | |
433 pivot = 1; | |
434 | |
435 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980); | |
436 | |
437 if (overflow == (MODEL_ELEMENTS - 1)) { | |
438 overflow = range_decode_bits(ctx, 16) << 16; | |
439 overflow |= range_decode_bits(ctx, 16); | |
440 } | |
441 | |
442 base = range_decode_culfreq(ctx, pivot); | |
443 range_decode_update(ctx, 1, base); | |
444 | |
445 x = base + overflow * pivot; | |
446 } | |
447 | |
448 update_rice(rice, x); | |
449 | |
450 /* Convert to signed */ | |
451 if (x & 1) | |
452 return (x >> 1) + 1; | |
453 else | |
454 return -(x >> 1); | |
455 } | |
456 | |
457 static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo) | |
458 { | |
459 int32_t *decoded0 = ctx->decoded0; | |
460 int32_t *decoded1 = ctx->decoded1; | |
461 | |
462 ctx->blocksdecoded = blockstodecode; | |
463 | |
464 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { | |
465 /* We are pure silence, just memset the output buffer. */ | |
466 memset(decoded0, 0, blockstodecode * sizeof(int32_t)); | |
467 memset(decoded1, 0, blockstodecode * sizeof(int32_t)); | |
468 } else { | |
469 while (blockstodecode--) { | |
470 *decoded0++ = ape_decode_value(ctx, &ctx->riceY); | |
471 if (stereo) | |
472 *decoded1++ = ape_decode_value(ctx, &ctx->riceX); | |
473 } | |
474 } | |
475 | |
476 if (ctx->blocksdecoded == ctx->currentframeblocks) | |
477 range_dec_normalize(ctx); /* normalize to use up all bytes */ | |
478 } | |
479 | |
480 static void init_entropy_decoder(APEContext * ctx) | |
481 { | |
482 /* Read the CRC */ | |
483 ctx->CRC = bytestream_get_be32(&ctx->ptr); | |
484 | |
485 /* Read the frame flags if they exist */ | |
486 ctx->frameflags = 0; | |
487 if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) { | |
488 ctx->CRC &= ~0x80000000; | |
489 | |
490 ctx->frameflags = bytestream_get_be32(&ctx->ptr); | |
491 } | |
492 | |
493 /* Keep a count of the blocks decoded in this frame */ | |
494 ctx->blocksdecoded = 0; | |
495 | |
5963 | 496 /* Initialize the rice structs */ |
5673 | 497 ctx->riceX.k = 10; |
498 ctx->riceX.ksum = (1 << ctx->riceX.k) * 16; | |
499 ctx->riceY.k = 10; | |
500 ctx->riceY.ksum = (1 << ctx->riceY.k) * 16; | |
501 | |
502 /* The first 8 bits of input are ignored. */ | |
503 ctx->ptr++; | |
504 | |
505 range_start_decoding(ctx); | |
506 } | |
507 | |
508 static const int32_t initial_coeffs[4] = { | |
509 360, 317, -109, 98 | |
510 }; | |
511 | |
512 static void init_predictor_decoder(APEContext * ctx) | |
513 { | |
514 APEPredictor *p = &ctx->predictor; | |
515 | |
516 /* Zero the history buffers */ | |
517 memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t)); | |
518 p->buf = p->historybuffer; | |
519 | |
5966 | 520 /* Initialize and zero the coefficients */ |
5673 | 521 memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs)); |
522 memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs)); | |
523 memset(p->coeffsB, 0, sizeof(p->coeffsB)); | |
524 | |
525 p->filterA[0] = p->filterA[1] = 0; | |
526 p->filterB[0] = p->filterB[1] = 0; | |
527 p->lastA[0] = p->lastA[1] = 0; | |
528 } | |
529 | |
530 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */ | |
531 static inline int APESIGN(int32_t x) { | |
532 return (x < 0) - (x > 0); | |
533 } | |
534 | |
535 static int predictor_update_filter(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB) | |
536 { | |
537 int32_t predictionA, predictionB; | |
538 | |
539 p->buf[delayA] = p->lastA[filter]; | |
540 p->buf[adaptA] = APESIGN(p->buf[delayA]); | |
541 p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1]; | |
542 p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]); | |
543 | |
544 predictionA = p->buf[delayA ] * p->coeffsA[filter][0] + | |
545 p->buf[delayA - 1] * p->coeffsA[filter][1] + | |
546 p->buf[delayA - 2] * p->coeffsA[filter][2] + | |
547 p->buf[delayA - 3] * p->coeffsA[filter][3]; | |
548 | |
549 /* Apply a scaled first-order filter compression */ | |
550 p->buf[delayB] = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5); | |
551 p->buf[adaptB] = APESIGN(p->buf[delayB]); | |
552 p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1]; | |
553 p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]); | |
554 p->filterB[filter] = p->filterA[filter ^ 1]; | |
555 | |
556 predictionB = p->buf[delayB ] * p->coeffsB[filter][0] + | |
557 p->buf[delayB - 1] * p->coeffsB[filter][1] + | |
558 p->buf[delayB - 2] * p->coeffsB[filter][2] + | |
559 p->buf[delayB - 3] * p->coeffsB[filter][3] + | |
560 p->buf[delayB - 4] * p->coeffsB[filter][4]; | |
561 | |
562 p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10); | |
563 p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5); | |
564 | |
565 if (!decoded) // no need updating filter coefficients | |
566 return p->filterA[filter]; | |
567 | |
568 if (decoded > 0) { | |
569 p->coeffsA[filter][0] -= p->buf[adaptA ]; | |
570 p->coeffsA[filter][1] -= p->buf[adaptA - 1]; | |
571 p->coeffsA[filter][2] -= p->buf[adaptA - 2]; | |
572 p->coeffsA[filter][3] -= p->buf[adaptA - 3]; | |
573 | |
574 p->coeffsB[filter][0] -= p->buf[adaptB ]; | |
575 p->coeffsB[filter][1] -= p->buf[adaptB - 1]; | |
576 p->coeffsB[filter][2] -= p->buf[adaptB - 2]; | |
577 p->coeffsB[filter][3] -= p->buf[adaptB - 3]; | |
578 p->coeffsB[filter][4] -= p->buf[adaptB - 4]; | |
579 } else { | |
580 p->coeffsA[filter][0] += p->buf[adaptA ]; | |
581 p->coeffsA[filter][1] += p->buf[adaptA - 1]; | |
582 p->coeffsA[filter][2] += p->buf[adaptA - 2]; | |
583 p->coeffsA[filter][3] += p->buf[adaptA - 3]; | |
584 | |
585 p->coeffsB[filter][0] += p->buf[adaptB ]; | |
586 p->coeffsB[filter][1] += p->buf[adaptB - 1]; | |
587 p->coeffsB[filter][2] += p->buf[adaptB - 2]; | |
588 p->coeffsB[filter][3] += p->buf[adaptB - 3]; | |
589 p->coeffsB[filter][4] += p->buf[adaptB - 4]; | |
590 } | |
591 return p->filterA[filter]; | |
592 } | |
593 | |
594 static void predictor_decode_stereo(APEContext * ctx, int count) | |
595 { | |
596 int32_t predictionA, predictionB; | |
597 APEPredictor *p = &ctx->predictor; | |
598 int32_t *decoded0 = ctx->decoded0; | |
599 int32_t *decoded1 = ctx->decoded1; | |
600 | |
601 while (count--) { | |
602 /* Predictor Y */ | |
603 predictionA = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB); | |
604 predictionB = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB); | |
605 *(decoded0++) = predictionA; | |
606 *(decoded1++) = predictionB; | |
607 | |
608 /* Combined */ | |
609 p->buf++; | |
610 | |
611 /* Have we filled the history buffer? */ | |
612 if (p->buf == p->historybuffer + HISTORY_SIZE) { | |
613 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t)); | |
614 p->buf = p->historybuffer; | |
615 } | |
616 } | |
617 } | |
618 | |
619 static void predictor_decode_mono(APEContext * ctx, int count) | |
620 { | |
621 APEPredictor *p = &ctx->predictor; | |
622 int32_t *decoded0 = ctx->decoded0; | |
623 int32_t predictionA, currentA, A; | |
624 | |
625 currentA = p->lastA[0]; | |
626 | |
627 while (count--) { | |
628 A = *decoded0; | |
629 | |
630 p->buf[YDELAYA] = currentA; | |
631 p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1]; | |
632 | |
633 predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] + | |
634 p->buf[YDELAYA - 1] * p->coeffsA[0][1] + | |
635 p->buf[YDELAYA - 2] * p->coeffsA[0][2] + | |
636 p->buf[YDELAYA - 3] * p->coeffsA[0][3]; | |
637 | |
638 currentA = A + (predictionA >> 10); | |
639 | |
640 p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]); | |
641 p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]); | |
642 | |
643 if (A > 0) { | |
644 p->coeffsA[0][0] -= p->buf[YADAPTCOEFFSA ]; | |
645 p->coeffsA[0][1] -= p->buf[YADAPTCOEFFSA - 1]; | |
646 p->coeffsA[0][2] -= p->buf[YADAPTCOEFFSA - 2]; | |
647 p->coeffsA[0][3] -= p->buf[YADAPTCOEFFSA - 3]; | |
648 } else if (A < 0) { | |
649 p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ]; | |
650 p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1]; | |
651 p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2]; | |
652 p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3]; | |
653 } | |
654 | |
655 p->buf++; | |
656 | |
657 /* Have we filled the history buffer? */ | |
658 if (p->buf == p->historybuffer + HISTORY_SIZE) { | |
659 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t)); | |
660 p->buf = p->historybuffer; | |
661 } | |
662 | |
663 p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5); | |
664 *(decoded0++) = p->filterA[0]; | |
665 } | |
666 | |
667 p->lastA[0] = currentA; | |
668 } | |
669 | |
670 static void do_init_filter(APEFilter *f, int16_t * buf, int order) | |
671 { | |
672 f->coeffs = buf; | |
673 f->historybuffer = buf + order; | |
674 f->delay = f->historybuffer + order * 2; | |
675 f->adaptcoeffs = f->historybuffer + order; | |
676 | |
677 memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t)); | |
678 memset(f->coeffs, 0, order * sizeof(int16_t)); | |
679 f->avg = 0; | |
680 } | |
681 | |
682 static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order) | |
683 { | |
684 do_init_filter(&f[0], buf, order); | |
685 do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order); | |
686 } | |
687 | |
688 static inline void do_apply_filter(int version, APEFilter *f, int32_t *data, int count, int order, int fracbits) | |
689 { | |
690 int res; | |
691 int absres; | |
692 | |
693 while (count--) { | |
694 /* round fixedpoint scalar product */ | |
695 res = (scalarproduct(f->delay - order, f->coeffs, order) + (1 << (fracbits - 1))) >> fracbits; | |
696 | |
697 if (*data < 0) | |
698 vector_add(f->coeffs, f->adaptcoeffs - order, order); | |
699 else if (*data > 0) | |
700 vector_sub(f->coeffs, f->adaptcoeffs - order, order); | |
701 | |
702 res += *data; | |
703 | |
704 *data++ = res; | |
705 | |
706 /* Update the output history */ | |
707 *f->delay++ = av_clip_int16(res); | |
708 | |
709 if (version < 3980) { | |
710 /* Version ??? to < 3.98 files (untested) */ | |
711 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4; | |
712 f->adaptcoeffs[-4] >>= 1; | |
713 f->adaptcoeffs[-8] >>= 1; | |
714 } else { | |
715 /* Version 3.98 and later files */ | |
716 | |
717 /* Update the adaption coefficients */ | |
718 absres = (res < 0 ? -res : res); | |
719 | |
720 if (absres > (f->avg * 3)) | |
721 *f->adaptcoeffs = ((res >> 25) & 64) - 32; | |
722 else if (absres > (f->avg * 4) / 3) | |
723 *f->adaptcoeffs = ((res >> 26) & 32) - 16; | |
724 else if (absres > 0) | |
725 *f->adaptcoeffs = ((res >> 27) & 16) - 8; | |
726 else | |
727 *f->adaptcoeffs = 0; | |
728 | |
729 f->avg += (absres - f->avg) / 16; | |
730 | |
731 f->adaptcoeffs[-1] >>= 1; | |
732 f->adaptcoeffs[-2] >>= 1; | |
733 f->adaptcoeffs[-8] >>= 1; | |
734 } | |
735 | |
736 f->adaptcoeffs++; | |
737 | |
738 /* Have we filled the history buffer? */ | |
739 if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) { | |
740 memmove(f->historybuffer, f->delay - (order * 2), | |
741 (order * 2) * sizeof(int16_t)); | |
742 f->delay = f->historybuffer + order * 2; | |
743 f->adaptcoeffs = f->historybuffer + order; | |
744 } | |
745 } | |
746 } | |
747 | |
748 static void apply_filter(APEContext * ctx, APEFilter *f, | |
749 int32_t * data0, int32_t * data1, | |
750 int count, int order, int fracbits) | |
751 { | |
752 do_apply_filter(ctx->fileversion, &f[0], data0, count, order, fracbits); | |
753 if (data1) | |
754 do_apply_filter(ctx->fileversion, &f[1], data1, count, order, fracbits); | |
755 } | |
756 | |
757 static void ape_apply_filters(APEContext * ctx, int32_t * decoded0, | |
758 int32_t * decoded1, int count) | |
759 { | |
760 int i; | |
761 | |
762 for (i = 0; i < APE_FILTER_LEVELS; i++) { | |
763 if (!ape_filter_orders[ctx->fset][i]) | |
764 break; | |
765 apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]); | |
766 } | |
767 } | |
768 | |
769 static void init_frame_decoder(APEContext * ctx) | |
770 { | |
771 int i; | |
772 init_entropy_decoder(ctx); | |
773 init_predictor_decoder(ctx); | |
774 | |
775 for (i = 0; i < APE_FILTER_LEVELS; i++) { | |
776 if (!ape_filter_orders[ctx->fset][i]) | |
777 break; | |
778 init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]); | |
779 } | |
780 } | |
781 | |
782 static void ape_unpack_mono(APEContext * ctx, int count) | |
783 { | |
784 int32_t left; | |
785 int32_t *decoded0 = ctx->decoded0; | |
786 int32_t *decoded1 = ctx->decoded1; | |
787 | |
788 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { | |
789 entropy_decode(ctx, count, 0); | |
790 /* We are pure silence, so we're done. */ | |
791 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n"); | |
792 return; | |
793 } | |
794 | |
795 entropy_decode(ctx, count, 0); | |
796 ape_apply_filters(ctx, decoded0, NULL, count); | |
797 | |
798 /* Now apply the predictor decoding */ | |
799 predictor_decode_mono(ctx, count); | |
800 | |
801 /* Pseudo-stereo - just copy left channel to right channel */ | |
802 if (ctx->channels == 2) { | |
803 while (count--) { | |
804 left = *decoded0; | |
805 *(decoded1++) = *(decoded0++) = left; | |
806 } | |
807 } | |
808 } | |
809 | |
810 static void ape_unpack_stereo(APEContext * ctx, int count) | |
811 { | |
812 int32_t left, right; | |
813 int32_t *decoded0 = ctx->decoded0; | |
814 int32_t *decoded1 = ctx->decoded1; | |
815 | |
816 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { | |
817 /* We are pure silence, so we're done. */ | |
818 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n"); | |
819 return; | |
820 } | |
821 | |
822 entropy_decode(ctx, count, 1); | |
823 ape_apply_filters(ctx, decoded0, decoded1, count); | |
824 | |
825 /* Now apply the predictor decoding */ | |
826 predictor_decode_stereo(ctx, count); | |
827 | |
828 /* Decorrelate and scale to output depth */ | |
829 while (count--) { | |
830 left = *decoded1 - (*decoded0 / 2); | |
831 right = left + *decoded0; | |
832 | |
833 *(decoded0++) = left; | |
834 *(decoded1++) = right; | |
835 } | |
836 } | |
837 | |
838 static int ape_decode_frame(AVCodecContext * avctx, | |
839 void *data, int *data_size, | |
6223 | 840 const uint8_t * buf, int buf_size) |
5673 | 841 { |
842 APEContext *s = avctx->priv_data; | |
843 int16_t *samples = data; | |
844 int nblocks; | |
845 int i, n; | |
846 int blockstodecode; | |
847 int bytes_used; | |
848 | |
849 if (buf_size == 0 && !s->samples) { | |
850 *data_size = 0; | |
851 return 0; | |
852 } | |
853 | |
854 /* should not happen but who knows */ | |
855 if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) { | |
856 av_log (avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\n", *data_size, s->samples * 2 * avctx->channels); | |
857 return -1; | |
858 } | |
859 | |
860 if(!s->samples){ | |
861 s->data = av_realloc(s->data, (buf_size + 3) & ~3); | |
6223 | 862 s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2); |
5673 | 863 s->ptr = s->last_ptr = s->data; |
864 s->data_end = s->data + buf_size; | |
865 | |
866 nblocks = s->samples = bytestream_get_be32(&s->ptr); | |
867 n = bytestream_get_be32(&s->ptr); | |
868 if(n < 0 || n > 3){ | |
869 av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n"); | |
870 s->data = NULL; | |
871 return -1; | |
872 } | |
873 s->ptr += n; | |
874 | |
875 s->currentframeblocks = nblocks; | |
876 buf += 4; | |
877 if (s->samples <= 0) { | |
878 *data_size = 0; | |
879 return buf_size; | |
880 } | |
881 | |
882 memset(s->decoded0, 0, sizeof(s->decoded0)); | |
883 memset(s->decoded1, 0, sizeof(s->decoded1)); | |
884 | |
885 /* Initialize the frame decoder */ | |
886 init_frame_decoder(s); | |
887 } | |
888 | |
889 if (!s->data) { | |
890 *data_size = 0; | |
891 return buf_size; | |
892 } | |
893 | |
894 nblocks = s->samples; | |
895 blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks); | |
896 | |
897 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO)) | |
898 ape_unpack_mono(s, blockstodecode); | |
899 else | |
900 ape_unpack_stereo(s, blockstodecode); | |
901 | |
902 for (i = 0; i < blockstodecode; i++) { | |
903 *samples++ = s->decoded0[i]; | |
904 if(s->channels == 2) | |
905 *samples++ = s->decoded1[i]; | |
906 } | |
907 | |
908 s->samples -= blockstodecode; | |
909 | |
910 *data_size = blockstodecode * 2 * s->channels; | |
911 bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size; | |
912 s->last_ptr = s->ptr; | |
913 return bytes_used; | |
914 } | |
915 | |
916 AVCodec ape_decoder = { | |
917 "ape", | |
918 CODEC_TYPE_AUDIO, | |
919 CODEC_ID_APE, | |
920 sizeof(APEContext), | |
921 ape_decode_init, | |
922 NULL, | |
923 ape_decode_close, | |
924 ape_decode_frame, | |
925 }; |