FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/apedec.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 660 865 76.3%
Functions: 40 50 80.0%
Branches: 254 370 68.6%

Line Branch Exec Source
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 #include <inttypes.h>
24
25 #include "libavutil/avassert.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/crc.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/opt.h"
30 #include "lossless_audiodsp.h"
31 #include "avcodec.h"
32 #include "bswapdsp.h"
33 #include "bytestream.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "get_bits.h"
37 #include "unary.h"
38
39 /**
40 * @file
41 * Monkey's Audio lossless audio decoder
42 */
43
44 #define MAX_CHANNELS 2
45 #define MAX_BYTESPERSAMPLE 3
46
47 #define APE_FRAMECODE_MONO_SILENCE 1
48 #define APE_FRAMECODE_STEREO_SILENCE 3
49 #define APE_FRAMECODE_PSEUDO_STEREO 4
50
51 #define HISTORY_SIZE 512
52 #define PREDICTOR_ORDER 8
53 /** Total size of all predictor histories */
54 #define PREDICTOR_SIZE 50
55
56 #define YDELAYA (18 + PREDICTOR_ORDER*4)
57 #define YDELAYB (18 + PREDICTOR_ORDER*3)
58 #define XDELAYA (18 + PREDICTOR_ORDER*2)
59 #define XDELAYB (18 + PREDICTOR_ORDER)
60
61 #define YADAPTCOEFFSA 18
62 #define XADAPTCOEFFSA 14
63 #define YADAPTCOEFFSB 10
64 #define XADAPTCOEFFSB 5
65
66 /**
67 * Possible compression levels
68 * @{
69 */
70 enum APECompressionLevel {
71 COMPRESSION_LEVEL_FAST = 1000,
72 COMPRESSION_LEVEL_NORMAL = 2000,
73 COMPRESSION_LEVEL_HIGH = 3000,
74 COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
75 COMPRESSION_LEVEL_INSANE = 5000
76 };
77 /** @} */
78
79 #define APE_FILTER_LEVELS 3
80
81 /** Filter orders depending on compression level */
82 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
83 { 0, 0, 0 },
84 { 16, 0, 0 },
85 { 64, 0, 0 },
86 { 32, 256, 0 },
87 { 16, 256, 1280 }
88 };
89
90 /** Filter fraction bits depending on compression level */
91 static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
92 { 0, 0, 0 },
93 { 11, 0, 0 },
94 { 11, 0, 0 },
95 { 10, 13, 0 },
96 { 11, 13, 15 }
97 };
98
99
100 /** Filters applied to the decoded data */
101 typedef struct APEFilter {
102 int16_t *coeffs; ///< actual coefficients used in filtering
103 int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients
104 int16_t *historybuffer; ///< filter memory
105 int16_t *delay; ///< filtered values
106
107 uint32_t avg;
108 } APEFilter;
109
110 typedef struct APERice {
111 uint32_t k;
112 uint32_t ksum;
113 } APERice;
114
115 typedef struct APERangecoder {
116 uint32_t low; ///< low end of interval
117 uint32_t range; ///< length of interval
118 uint32_t help; ///< bytes_to_follow resp. intermediate value
119 unsigned int buffer; ///< buffer for input/output
120 } APERangecoder;
121
122 /** Filter histories */
123 typedef struct APEPredictor {
124 int32_t *buf;
125
126 int32_t lastA[2];
127
128 int32_t filterA[2];
129 int32_t filterB[2];
130
131 uint32_t coeffsA[2][4]; ///< adaption coefficients
132 uint32_t coeffsB[2][5]; ///< adaption coefficients
133 int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
134
135 unsigned int sample_pos;
136 } APEPredictor;
137
138 typedef struct APEPredictor64 {
139 int64_t *buf;
140
141 int64_t lastA[2];
142
143 int64_t filterA[2];
144 int64_t filterB[2];
145
146 uint64_t coeffsA[2][4]; ///< adaption coefficients
147 uint64_t coeffsB[2][5]; ///< adaption coefficients
148 int64_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
149 } APEPredictor64;
150
151 /** Decoder context */
152 typedef struct APEContext {
153 AVClass *class; ///< class for AVOptions
154 AVCodecContext *avctx;
155 BswapDSPContext bdsp;
156 LLAudDSPContext adsp;
157 int channels;
158 int samples; ///< samples left to decode in current frame
159 int bps;
160
161 int fileversion; ///< codec version, very important in decoding process
162 int compression_level; ///< compression levels
163 int fset; ///< which filter set to use (calculated from compression level)
164 int flags; ///< global decoder flags
165
166 uint32_t CRC; ///< signalled frame CRC
167 uint32_t CRC_state; ///< accumulated CRC
168 int frameflags; ///< frame flags
169 APEPredictor predictor; ///< predictor used for final reconstruction
170 APEPredictor64 predictor64; ///< 64bit predictor used for final reconstruction
171
172 int32_t *decoded_buffer;
173 int decoded_size;
174 int32_t *decoded[MAX_CHANNELS]; ///< decoded data for each channel
175 int32_t *interim_buffer;
176 int interim_size;
177 int32_t *interim[MAX_CHANNELS]; ///< decoded data for each channel
178 int blocks_per_loop; ///< maximum number of samples to decode for each call
179
180 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory
181
182 APERangecoder rc; ///< rangecoder used to decode actual values
183 APERice riceX; ///< rice code parameters for the second channel
184 APERice riceY; ///< rice code parameters for the first channel
185 APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
186 GetBitContext gb;
187
188 uint8_t *data; ///< current frame data
189 uint8_t *data_end; ///< frame data end
190 int data_size; ///< frame data allocated size
191 const uint8_t *ptr; ///< current position in frame data
192
193 int error;
194 int interim_mode;
195
196 void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
197 void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
198 void (*predictor_decode_mono)(struct APEContext *ctx, int count);
199 void (*predictor_decode_stereo)(struct APEContext *ctx, int count);
200 } APEContext;
201
202 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
203 int32_t *decoded1, int count);
204
205 static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
206 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
207 static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode);
208 static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode);
209 static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
210 static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
211 static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode);
212 static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
213 static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
214
215 static void predictor_decode_mono_3800(APEContext *ctx, int count);
216 static void predictor_decode_stereo_3800(APEContext *ctx, int count);
217 static void predictor_decode_mono_3930(APEContext *ctx, int count);
218 static void predictor_decode_stereo_3930(APEContext *ctx, int count);
219 static void predictor_decode_mono_3950(APEContext *ctx, int count);
220 static void predictor_decode_stereo_3950(APEContext *ctx, int count);
221
222 29 static av_cold int ape_decode_close(AVCodecContext *avctx)
223 {
224 29 APEContext *s = avctx->priv_data;
225 int i;
226
227
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 29 times.
116 for (i = 0; i < APE_FILTER_LEVELS; i++)
228 87 av_freep(&s->filterbuf[i]);
229
230 29 av_freep(&s->decoded_buffer);
231 29 av_freep(&s->interim_buffer);
232 29 av_freep(&s->data);
233 29 s->decoded_size = s->data_size = 0;
234
235 29 return 0;
236 }
237
238 29 static av_cold int ape_decode_init(AVCodecContext *avctx)
239 {
240 29 APEContext *s = avctx->priv_data;
241 29 int channels = avctx->ch_layout.nb_channels;
242 int i;
243
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (avctx->extradata_size != 6) {
245 av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
246 return AVERROR(EINVAL);
247 }
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (channels > 2) {
249 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
250 return AVERROR(EINVAL);
251 }
252 29 avctx->bits_per_raw_sample =
253 29 s->bps = avctx->bits_per_coded_sample;
254
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
29 switch (s->bps) {
255 case 8:
256 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
257 s->interim_mode = 0;
258 break;
259 27 case 16:
260 27 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
261 27 s->interim_mode = 0;
262 27 break;
263 2 case 24:
264 2 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
265 2 s->interim_mode = -1;
266 2 break;
267 default:
268 avpriv_request_sample(avctx,
269 "%d bits per coded sample", s->bps);
270 return AVERROR_PATCHWELCOME;
271 }
272 29 s->avctx = avctx;
273 29 s->channels = channels;
274 29 s->fileversion = AV_RL16(avctx->extradata);
275 29 s->compression_level = AV_RL16(avctx->extradata + 2);
276 29 s->flags = AV_RL16(avctx->extradata + 4);
277
278 29 av_log(avctx, AV_LOG_VERBOSE, "Compression Level: %d - Flags: %d\n",
279 s->compression_level, s->flags);
280
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
29 if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
281
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 !s->compression_level ||
282
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
29 (s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) {
283 av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
284 s->compression_level);
285 return AVERROR_INVALIDDATA;
286 }
287 29 s->fset = s->compression_level / 1000 - 1;
288
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 for (i = 0; i < APE_FILTER_LEVELS; i++) {
289
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 41 times.
70 if (!ape_filter_orders[s->fset][i])
290 29 break;
291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if (!(s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4)))
292 return AVERROR(ENOMEM);
293 }
294
295
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 25 times.
29 if (s->fileversion < 3860) {
296 4 s->entropy_decode_mono = entropy_decode_mono_0000;
297 4 s->entropy_decode_stereo = entropy_decode_stereo_0000;
298
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 17 times.
25 } else if (s->fileversion < 3900) {
299 8 s->entropy_decode_mono = entropy_decode_mono_3860;
300 8 s->entropy_decode_stereo = entropy_decode_stereo_3860;
301
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 } else if (s->fileversion < 3930) {
302 8 s->entropy_decode_mono = entropy_decode_mono_3900;
303 8 s->entropy_decode_stereo = entropy_decode_stereo_3900;
304
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 } else if (s->fileversion < 3990) {
305 4 s->entropy_decode_mono = entropy_decode_mono_3900;
306 4 s->entropy_decode_stereo = entropy_decode_stereo_3930;
307 } else {
308 5 s->entropy_decode_mono = entropy_decode_mono_3990;
309 5 s->entropy_decode_stereo = entropy_decode_stereo_3990;
310 }
311
312
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 9 times.
29 if (s->fileversion < 3930) {
313 20 s->predictor_decode_mono = predictor_decode_mono_3800;
314 20 s->predictor_decode_stereo = predictor_decode_stereo_3800;
315
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 } else if (s->fileversion < 3950) {
316 4 s->predictor_decode_mono = predictor_decode_mono_3930;
317 4 s->predictor_decode_stereo = predictor_decode_stereo_3930;
318 } else {
319 5 s->predictor_decode_mono = predictor_decode_mono_3950;
320 5 s->predictor_decode_stereo = predictor_decode_stereo_3950;
321 }
322
323 29 ff_bswapdsp_init(&s->bdsp);
324 29 ff_llauddsp_init(&s->adsp);
325 29 av_channel_layout_uninit(&avctx->ch_layout);
326 29 avctx->ch_layout = (channels == 2) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO
327
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 : (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
328
329 29 return 0;
330 }
331
332 /**
333 * @name APE range decoding functions
334 * @{
335 */
336
337 #define CODE_BITS 32
338 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
339 #define SHIFT_BITS (CODE_BITS - 9)
340 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
341 #define BOTTOM_VALUE (TOP_VALUE >> 8)
342
343 /** Start the decoder */
344 35 static inline void range_start_decoding(APEContext *ctx)
345 {
346 35 ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
347 35 ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
348 35 ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
349 35 }
350
351 /** Perform normalization */
352 6060175 static inline void range_dec_normalize(APEContext *ctx)
353 {
354
2/2
✓ Branch 0 taken 3145504 times.
✓ Branch 1 taken 6060175 times.
9205679 while (ctx->rc.range <= BOTTOM_VALUE) {
355 3145504 ctx->rc.buffer <<= 8;
356
2/2
✓ Branch 0 taken 2774866 times.
✓ Branch 1 taken 370638 times.
3145504 if(ctx->ptr < ctx->data_end) {
357 2774866 ctx->rc.buffer += *ctx->ptr;
358 2774866 ctx->ptr++;
359 } else {
360 370638 ctx->error = 1;
361 }
362 3145504 ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
363 3145504 ctx->rc.range <<= 8;
364 }
365 6060175 }
366
367 /**
368 * Calculate cumulative frequency for next symbol. Does NO update!
369 * @param ctx decoder context
370 * @param tot_f is the total frequency or (code_value)1<<shift
371 * @return the cumulative frequency
372 */
373 945271 static inline int range_decode_culfreq(APEContext *ctx, int tot_f)
374 {
375 945271 range_dec_normalize(ctx);
376 945271 ctx->rc.help = ctx->rc.range / tot_f;
377 945271 return ctx->rc.low / ctx->rc.help;
378 }
379
380 /**
381 * Decode value with given size in bits
382 * @param ctx decoder context
383 * @param shift number of bits to decode
384 */
385 5114892 static inline int range_decode_culshift(APEContext *ctx, int shift)
386 {
387 5114892 range_dec_normalize(ctx);
388 5114892 ctx->rc.help = ctx->rc.range >> shift;
389 5114892 return ctx->rc.low / ctx->rc.help;
390 }
391
392
393 /**
394 * Update decoding state
395 * @param ctx decoder context
396 * @param sy_f the interval length (frequency of the symbol)
397 * @param lt_f the lower end (frequency sum of < symbols)
398 */
399 6060163 static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
400 {
401 6060163 ctx->rc.low -= ctx->rc.help * lt_f;
402 6060163 ctx->rc.range = ctx->rc.help * sy_f;
403 6060163 }
404
405 /** Decode n bits (n <= 16) without modelling */
406 2101260 static inline int range_decode_bits(APEContext *ctx, int n)
407 {
408 2101260 int sym = range_decode_culshift(ctx, n);
409 2101260 range_decode_update(ctx, 1, sym);
410 2101260 return sym;
411 }
412
413
414 #define MODEL_ELEMENTS 64
415
416 /**
417 * Fixed probabilities for symbols in Monkey Audio version 3.97
418 */
419 static const uint16_t counts_3970[22] = {
420 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
421 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
422 65450, 65469, 65480, 65487, 65491, 65493,
423 };
424
425 /**
426 * Probability ranges for symbols in Monkey Audio version 3.97
427 */
428 static const uint16_t counts_diff_3970[21] = {
429 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
430 1104, 677, 415, 248, 150, 89, 54, 31,
431 19, 11, 7, 4, 2,
432 };
433
434 /**
435 * Fixed probabilities for symbols in Monkey Audio version 3.98
436 */
437 static const uint16_t counts_3980[22] = {
438 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
439 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
440 65485, 65488, 65490, 65491, 65492, 65493,
441 };
442
443 /**
444 * Probability ranges for symbols in Monkey Audio version 3.98
445 */
446 static const uint16_t counts_diff_3980[21] = {
447 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
448 261, 119, 65, 31, 19, 10, 6, 3,
449 3, 2, 1, 1, 1,
450 };
451
452 /**
453 * Decode symbol
454 * @param ctx decoder context
455 * @param counts probability range start position
456 * @param counts_diff probability range widths
457 */
458 3013632 static inline int range_get_symbol(APEContext *ctx,
459 const uint16_t counts[],
460 const uint16_t counts_diff[])
461 {
462 int symbol, cf;
463
464 3013632 cf = range_decode_culshift(ctx, 16);
465
466
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 3013348 times.
3013632 if(cf > 65492){
467 284 symbol= cf - 65535 + 63;
468 284 range_decode_update(ctx, 1, cf);
469
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 277 times.
284 if(cf > 65535)
470 7 ctx->error=1;
471 284 return symbol;
472 }
473 /* figure out the symbol inefficiently; a binary search would be much better */
474
2/2
✓ Branch 0 taken 5520748 times.
✓ Branch 1 taken 3013348 times.
8534096 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
475
476 3013348 range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
477
478 3013348 return symbol;
479 }
480 /** @} */ // group rangecoder
481
482 3013632 static inline void update_rice(APERice *rice, unsigned int x)
483 {
484
2/2
✓ Branch 0 taken 2585021 times.
✓ Branch 1 taken 428611 times.
3013632 int lim = rice->k ? (1 << (rice->k + 4)) : 0;
485 3013632 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
486
487
2/2
✓ Branch 0 taken 29711 times.
✓ Branch 1 taken 2983921 times.
3013632 if (rice->ksum < lim)
488 29711 rice->k--;
489
3/4
✓ Branch 0 taken 29539 times.
✓ Branch 1 taken 2954382 times.
✓ Branch 2 taken 29539 times.
✗ Branch 3 not taken.
2983921 else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
490 29539 rice->k++;
491 3013632 }
492
493 482165 static inline int get_rice_ook(GetBitContext *gb, int k)
494 {
495 unsigned int x;
496
497 482165 x = get_unary(gb, 1, get_bits_left(gb));
498
499
2/2
✓ Branch 0 taken 482106 times.
✓ Branch 1 taken 59 times.
482165 if (k)
500 482106 x = (x << k) | get_bits(gb, k);
501
502 482165 return x;
503 }
504
505 1253376 static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb,
506 APERice *rice)
507 {
508 unsigned int x, overflow;
509
510 1253376 overflow = get_unary(gb, 1, get_bits_left(gb));
511
512
2/2
✓ Branch 0 taken 626688 times.
✓ Branch 1 taken 626688 times.
1253376 if (ctx->fileversion > 3880) {
513
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 626688 times.
626689 while (overflow >= 16) {
514 1 overflow -= 16;
515 1 rice->k += 4;
516 }
517 }
518
519
2/2
✓ Branch 0 taken 285870 times.
✓ Branch 1 taken 967506 times.
1253376 if (!rice->k)
520 285870 x = overflow;
521
1/2
✓ Branch 0 taken 967506 times.
✗ Branch 1 not taken.
967506 else if(rice->k <= MIN_CACHE_BITS) {
522 967506 x = (overflow << rice->k) + get_bits(gb, rice->k);
523 } else {
524 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %"PRIu32"\n", rice->k);
525 ctx->error = 1;
526 return AVERROR_INVALIDDATA;
527 }
528 1253376 rice->ksum += x - (rice->ksum + 8 >> 4);
529
4/4
✓ Branch 0 taken 967506 times.
✓ Branch 1 taken 285870 times.
✓ Branch 2 taken 24811 times.
✓ Branch 3 taken 1228565 times.
1253376 if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
530 24811 rice->k--;
531
3/4
✓ Branch 0 taken 24603 times.
✓ Branch 1 taken 1203962 times.
✓ Branch 2 taken 24603 times.
✗ Branch 3 not taken.
1228565 else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
532 24603 rice->k++;
533
534 /* Convert to signed */
535 1253376 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
536 }
537
538 2101248 static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
539 {
540 unsigned int x, overflow;
541 int tmpk;
542
543 2101248 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
544
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2101248 times.
2101248 if (overflow == (MODEL_ELEMENTS - 1)) {
546 tmpk = range_decode_bits(ctx, 5);
547 overflow = 0;
548 } else
549
2/2
✓ Branch 0 taken 1679116 times.
✓ Branch 1 taken 422132 times.
2101248 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
550
551
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2101248 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2101248 if (tmpk <= 16 || ctx->fileversion < 3910) {
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2101248 times.
2101248 if (tmpk > 23) {
553 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
554 return AVERROR_INVALIDDATA;
555 }
556 2101248 x = range_decode_bits(ctx, tmpk);
557 } else if (tmpk <= 31) {
558 x = range_decode_bits(ctx, 16);
559 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
560 } else {
561 av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
562 return AVERROR_INVALIDDATA;
563 }
564 2101248 x += overflow << tmpk;
565
566 2101248 update_rice(rice, x);
567
568 /* Convert to signed */
569 2101248 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
570 }
571
572 912384 static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
573 {
574 unsigned int x, overflow, pivot;
575 int base;
576
577 912384 pivot = FFMAX(rice->ksum >> 5, 1);
578
579 912384 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
580
581
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 912378 times.
912384 if (overflow == (MODEL_ELEMENTS - 1)) {
582 6 overflow = (unsigned)range_decode_bits(ctx, 16) << 16;
583 6 overflow |= range_decode_bits(ctx, 16);
584 }
585
586
2/2
✓ Branch 0 taken 879497 times.
✓ Branch 1 taken 32887 times.
912384 if (pivot < 0x10000) {
587 879497 base = range_decode_culfreq(ctx, pivot);
588 879497 range_decode_update(ctx, 1, base);
589 } else {
590 32887 int base_hi = pivot, base_lo;
591 32887 int bbits = 0;
592
593
2/2
✓ Branch 0 taken 214168 times.
✓ Branch 1 taken 32887 times.
247055 while (base_hi & ~0xFFFF) {
594 214168 base_hi >>= 1;
595 214168 bbits++;
596 }
597 32887 base_hi = range_decode_culfreq(ctx, base_hi + 1);
598 32887 range_decode_update(ctx, 1, base_hi);
599 32887 base_lo = range_decode_culfreq(ctx, 1 << bbits);
600 32887 range_decode_update(ctx, 1, base_lo);
601
602 32887 base = (base_hi << bbits) + base_lo;
603 }
604
605 912384 x = base + overflow * pivot;
606
607 912384 update_rice(rice, x);
608
609 /* Convert to signed */
610 912384 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
611 }
612
613 1586 static int get_k(int ksum)
614 {
615 1586 return av_log2(ksum) + !!ksum;
616 }
617
618 26 static void decode_array_0000(APEContext *ctx, GetBitContext *gb,
619 int32_t *out, APERice *rice, int blockstodecode)
620 {
621 int i;
622 unsigned ksummax, ksummin;
623
624 26 rice->ksum = 0;
625
2/2
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 26 times.
156 for (i = 0; i < FFMIN(blockstodecode, 5); i++) {
626 130 out[i] = get_rice_ook(&ctx->gb, 10);
627 130 rice->ksum += out[i];
628 }
629
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (blockstodecode <= 5)
631 goto end;
632
633 26 rice->k = get_k(rice->ksum / 10);
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (rice->k >= 24)
635 return;
636
2/2
✓ Branch 0 taken 1534 times.
✓ Branch 1 taken 26 times.
1560 for (; i < FFMIN(blockstodecode, 64); i++) {
637 1534 out[i] = get_rice_ook(&ctx->gb, rice->k);
638 1534 rice->ksum += out[i];
639 1534 rice->k = get_k(rice->ksum / ((i + 1) * 2));
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
1534 if (rice->k >= 24)
641 return;
642 }
643
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (blockstodecode <= 64)
645 goto end;
646
647 26 rice->k = get_k(rice->ksum >> 7);
648 26 ksummax = 1 << rice->k + 7;
649
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 times.
26 ksummin = rice->k ? (1 << rice->k + 6) : 0;
650
2/2
✓ Branch 0 taken 480503 times.
✓ Branch 1 taken 24 times.
480527 for (; i < blockstodecode; i++) {
651
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 480501 times.
480503 if (get_bits_left(&ctx->gb) < 1) {
652 2 ctx->error = 1;
653 2 return;
654 }
655 480501 out[i] = get_rice_ook(&ctx->gb, rice->k);
656 480501 rice->ksum += out[i] - (unsigned)out[i - 64];
657
2/2
✓ Branch 0 taken 4040 times.
✓ Branch 1 taken 480501 times.
484541 while (rice->ksum < ksummin) {
658 4040 rice->k--;
659
1/2
✓ Branch 0 taken 4040 times.
✗ Branch 1 not taken.
4040 ksummin = rice->k ? ksummin >> 1 : 0;
660 4040 ksummax >>= 1;
661 }
662
2/2
✓ Branch 0 taken 4029 times.
✓ Branch 1 taken 480501 times.
484530 while (rice->ksum >= ksummax) {
663 4029 rice->k++;
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4029 times.
4029 if (rice->k > 24)
665 return;
666 4029 ksummax <<= 1;
667
1/2
✓ Branch 0 taken 4029 times.
✗ Branch 1 not taken.
4029 ksummin = ksummin ? ksummin << 1 : 128;
668 }
669 }
670
671 24 end:
672
2/2
✓ Branch 0 taken 479232 times.
✓ Branch 1 taken 24 times.
479256 for (i = 0; i < blockstodecode; i++)
673 479232 out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1;
674 }
675
676 static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
677 {
678 decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
679 blockstodecode);
680 }
681
682 13 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
683 {
684 13 decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
685 blockstodecode);
686 13 decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
687 blockstodecode);
688 13 }
689
690 static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
691 {
692 int32_t *decoded0 = ctx->decoded[0];
693
694 while (blockstodecode--)
695 *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
696 }
697
698 26 static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
699 {
700 26 int32_t *decoded0 = ctx->decoded[0];
701 26 int32_t *decoded1 = ctx->decoded[1];
702 26 int blocks = blockstodecode;
703
704
2/2
✓ Branch 0 taken 626688 times.
✓ Branch 1 taken 26 times.
626714 while (blockstodecode--)
705 626688 *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
706
2/2
✓ Branch 0 taken 626688 times.
✓ Branch 1 taken 26 times.
626714 while (blocks--)
707 626688 *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
708 26 }
709
710 static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
711 {
712 int32_t *decoded0 = ctx->decoded[0];
713
714 while (blockstodecode--)
715 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
716 }
717
718 12 static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
719 {
720 12 int32_t *decoded0 = ctx->decoded[0];
721 12 int32_t *decoded1 = ctx->decoded[1];
722 12 int blocks = blockstodecode;
723
724
2/2
✓ Branch 0 taken 884736 times.
✓ Branch 1 taken 12 times.
884748 while (blockstodecode--)
725 884736 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
726 12 range_dec_normalize(ctx);
727 // because of some implementation peculiarities we need to backpedal here
728 12 ctx->ptr -= 1;
729 12 range_start_decoding(ctx);
730
2/2
✓ Branch 0 taken 884736 times.
✓ Branch 1 taken 12 times.
884748 while (blocks--)
731 884736 *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
732 12 }
733
734 36 static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
735 {
736 36 int32_t *decoded0 = ctx->decoded[0];
737 36 int32_t *decoded1 = ctx->decoded[1];
738
739
2/2
✓ Branch 0 taken 165888 times.
✓ Branch 1 taken 36 times.
165924 while (blockstodecode--) {
740 165888 *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
741 165888 *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
742 }
743 36 }
744
745 static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
746 {
747 int32_t *decoded0 = ctx->decoded[0];
748
749 while (blockstodecode--)
750 *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
751 }
752
753 99 static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
754 {
755 99 int32_t *decoded0 = ctx->decoded[0];
756 99 int32_t *decoded1 = ctx->decoded[1];
757
758
2/2
✓ Branch 0 taken 456192 times.
✓ Branch 1 taken 99 times.
456291 while (blockstodecode--) {
759 456192 *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
760 456192 *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
761 }
762 99 }
763
764 62 static int init_entropy_decoder(APEContext *ctx)
765 {
766 /* Read the CRC */
767
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
62 if (ctx->fileversion >= 3900) {
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ctx->data_end - ctx->ptr < 6)
769 return AVERROR_INVALIDDATA;
770 23 ctx->CRC = bytestream_get_be32(&ctx->ptr);
771 } else {
772 39 ctx->CRC = get_bits_long(&ctx->gb, 32);
773 }
774
775 /* Read the frame flags if they exist */
776 62 ctx->frameflags = 0;
777 62 ctx->CRC_state = UINT32_MAX;
778
3/4
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
62 if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
779 ctx->CRC &= ~0x80000000;
780
781 if (ctx->data_end - ctx->ptr < 6)
782 return AVERROR_INVALIDDATA;
783 ctx->frameflags = bytestream_get_be32(&ctx->ptr);
784 }
785
786 /* Initialize the rice structs */
787 62 ctx->riceX.k = 10;
788 62 ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
789 62 ctx->riceY.k = 10;
790 62 ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
791
792
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
62 if (ctx->fileversion >= 3900) {
793 /* The first 8 bits of input are ignored. */
794 23 ctx->ptr++;
795
796 23 range_start_decoding(ctx);
797 }
798
799 62 return 0;
800 }
801
802 static const int32_t initial_coeffs_fast_3320[1] = {
803 375,
804 };
805
806 static const int32_t initial_coeffs_a_3800[3] = {
807 64, 115, 64,
808 };
809
810 static const int32_t initial_coeffs_b_3800[2] = {
811 740, 0
812 };
813
814 static const int32_t initial_coeffs_3930[4] = {
815 360, 317, -109, 98
816 };
817
818 static const int64_t initial_coeffs_3930_64bit[4] = {
819 360, 317, -109, 98
820 };
821
822 62 static void init_predictor_decoder(APEContext *ctx)
823 {
824 62 APEPredictor *p = &ctx->predictor;
825 62 APEPredictor64 *p64 = &ctx->predictor64;
826
827 /* Zero the history buffers */
828 62 memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer));
829 62 memset(p64->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p64->historybuffer));
830 62 p->buf = p->historybuffer;
831 62 p64->buf = p64->historybuffer;
832
833 /* Initialize and zero the coefficients */
834
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 11 times.
62 if (ctx->fileversion < 3930) {
835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
836 memcpy(p->coeffsA[0], initial_coeffs_fast_3320,
837 sizeof(initial_coeffs_fast_3320));
838 memcpy(p->coeffsA[1], initial_coeffs_fast_3320,
839 sizeof(initial_coeffs_fast_3320));
840 } else {
841 51 memcpy(p->coeffsA[0], initial_coeffs_a_3800,
842 sizeof(initial_coeffs_a_3800));
843 51 memcpy(p->coeffsA[1], initial_coeffs_a_3800,
844 sizeof(initial_coeffs_a_3800));
845 }
846 } else {
847 11 memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
848 11 memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
849 11 memcpy(p64->coeffsA[0], initial_coeffs_3930_64bit, sizeof(initial_coeffs_3930_64bit));
850 11 memcpy(p64->coeffsA[1], initial_coeffs_3930_64bit, sizeof(initial_coeffs_3930_64bit));
851 }
852 62 memset(p->coeffsB, 0, sizeof(p->coeffsB));
853 62 memset(p64->coeffsB, 0, sizeof(p64->coeffsB));
854
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 11 times.
62 if (ctx->fileversion < 3930) {
855 51 memcpy(p->coeffsB[0], initial_coeffs_b_3800,
856 sizeof(initial_coeffs_b_3800));
857 51 memcpy(p->coeffsB[1], initial_coeffs_b_3800,
858 sizeof(initial_coeffs_b_3800));
859 }
860
861 62 p->filterA[0] = p->filterA[1] = 0;
862 62 p->filterB[0] = p->filterB[1] = 0;
863 62 p->lastA[0] = p->lastA[1] = 0;
864
865 62 p64->filterA[0] = p64->filterA[1] = 0;
866 62 p64->filterB[0] = p64->filterB[1] = 0;
867 62 p64->lastA[0] = p64->lastA[1] = 0;
868
869 62 p->sample_pos = 0;
870 62 }
871
872 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
873 16129995 static inline int APESIGN(int32_t x) {
874 16129995 return (x < 0) - (x > 0);
875 }
876
877 static av_always_inline int filter_fast_3320(APEPredictor *p,
878 const int decoded, const int filter,
879 const int delayA)
880 {
881 int32_t predictionA;
882
883 p->buf[delayA] = p->lastA[filter];
884 if (p->sample_pos < 3) {
885 p->lastA[filter] = decoded;
886 p->filterA[filter] = decoded;
887 return decoded;
888 }
889
890 predictionA = p->buf[delayA] * 2U - p->buf[delayA - 1];
891 p->lastA[filter] = decoded + (unsigned)((int32_t)(predictionA * p->coeffsA[filter][0]) >> 9);
892
893 if ((decoded ^ predictionA) > 0)
894 p->coeffsA[filter][0]++;
895 else
896 p->coeffsA[filter][0]--;
897
898 p->filterA[filter] += (unsigned)p->lastA[filter];
899
900 return p->filterA[filter];
901 }
902
903 2912256 static av_always_inline int filter_3800(APEPredictor *p,
904 const unsigned decoded, const int filter,
905 const int delayA, const int delayB,
906 const int start, const int shift)
907 {
908 int32_t predictionA, predictionB, sign;
909 int32_t d0, d1, d2, d3, d4;
910
911 2912256 p->buf[delayA] = p->lastA[filter];
912 2912256 p->buf[delayB] = p->filterB[filter];
913
2/2
✓ Branch 0 taken 5904 times.
✓ Branch 1 taken 2906352 times.
2912256 if (p->sample_pos < start) {
914 5904 predictionA = decoded + p->filterA[filter];
915 5904 p->lastA[filter] = decoded;
916 5904 p->filterB[filter] = decoded;
917 5904 p->filterA[filter] = predictionA;
918 5904 return predictionA;
919 }
920 2906352 d2 = p->buf[delayA];
921 2906352 d1 = (p->buf[delayA] - (unsigned)p->buf[delayA - 1]) * 2;
922 2906352 d0 = p->buf[delayA] + ((p->buf[delayA - 2] - (unsigned)p->buf[delayA - 1]) * 8);
923 2906352 d3 = p->buf[delayB] * 2U - p->buf[delayB - 1];
924 2906352 d4 = p->buf[delayB];
925
926 2906352 predictionA = d0 * p->coeffsA[filter][0] +
927 2906352 d1 * p->coeffsA[filter][1] +
928 2906352 d2 * p->coeffsA[filter][2];
929
930 2906352 sign = APESIGN(decoded);
931 2906352 p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
932 2906352 p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
933 2906352 p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
934
935 2906352 predictionB = d3 * p->coeffsB[filter][0] -
936 2906352 d4 * p->coeffsB[filter][1];
937 2906352 p->lastA[filter] = decoded + (predictionA >> 11);
938 2906352 sign = APESIGN(p->lastA[filter]);
939 2906352 p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
940 2906352 p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
941
942 2906352 p->filterB[filter] = p->lastA[filter] + (unsigned)(predictionB >> shift);
943 2906352 p->filterA[filter] = p->filterB[filter] + (unsigned)((int)(p->filterA[filter] * 31U) >> 5);
944
945 2906352 return p->filterA[filter];
946 }
947
948 24 static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
949 {
950 int i, j;
951 int32_t dotprod, sign;
952 24 int32_t coeffs[256], delay[256+256], *delayp = delay;
953
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (order >= length)
955 return;
956
957 24 memset(coeffs, 0, order * sizeof(*coeffs));
958
2/2
✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 24 times.
5656 for (i = 0; i < order; i++)
959 5632 delay[i] = buffer[i];
960
2/2
✓ Branch 0 taken 1763840 times.
✓ Branch 1 taken 24 times.
1763864 for (i = order; i < length; i++) {
961 1763840 dotprod = 0;
962 1763840 sign = APESIGN(buffer[i]);
963
2/2
✓ Branch 0 taken 727881 times.
✓ Branch 1 taken 1035959 times.
1763840 if (sign == 1) {
964
2/2
✓ Branch 0 taken 167741056 times.
✓ Branch 1 taken 727881 times.
168468937 for (j = 0; j < order; j++) {
965 167741056 dotprod += delayp[j] * (unsigned)coeffs[j];
966 167741056 coeffs[j] += (delayp[j] >> 31) | 1;
967 }
968
2/2
✓ Branch 0 taken 739616 times.
✓ Branch 1 taken 296343 times.
1035959 } else if (sign == -1) {
969
2/2
✓ Branch 0 taken 170526720 times.
✓ Branch 1 taken 739616 times.
171266336 for (j = 0; j < order; j++) {
970 170526720 dotprod += delayp[j] * (unsigned)coeffs[j];
971 170526720 coeffs[j] -= (delayp[j] >> 31) | 1;
972 }
973 } else {
974
2/2
✓ Branch 0 taken 75592064 times.
✓ Branch 1 taken 296343 times.
75888407 for (j = 0; j < order; j++) {
975 75592064 dotprod += delayp[j] * (unsigned)coeffs[j];
976 }
977 }
978 1763840 buffer[i] -= (unsigned)(dotprod >> shift);
979 1763840 delayp ++;
980 1763840 delayp[order - 1] = buffer[i];
981
2/2
✓ Branch 0 taken 6888 times.
✓ Branch 1 taken 1756952 times.
1763840 if (delayp - delay == 256) {
982 6888 memcpy(delay, delayp, sizeof(*delay)*256);
983 6888 delayp = delay;
984 }
985 }
986 }
987
988 20 static void long_filter_ehigh_3830(int32_t *buffer, int length)
989 {
990 int i, j;
991 int32_t dotprod, sign;
992 20 int32_t delay[8] = { 0 };
993 20 uint32_t coeffs[8] = { 0 };
994
995
2/2
✓ Branch 0 taken 1469440 times.
✓ Branch 1 taken 20 times.
1469460 for (i = 0; i < length; i++) {
996 1469440 dotprod = 0;
997 1469440 sign = APESIGN(buffer[i]);
998
2/2
✓ Branch 0 taken 11755520 times.
✓ Branch 1 taken 1469440 times.
13224960 for (j = 7; j >= 0; j--) {
999 11755520 dotprod += delay[j] * coeffs[j];
1000 11755520 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
1001 }
1002
2/2
✓ Branch 0 taken 10286080 times.
✓ Branch 1 taken 1469440 times.
11755520 for (j = 7; j > 0; j--)
1003 10286080 delay[j] = delay[j - 1];
1004 1469440 delay[0] = buffer[i];
1005 1469440 buffer[i] -= (unsigned)(dotprod >> 9);
1006 }
1007 20 }
1008
1009 46 static void predictor_decode_stereo_3800(APEContext *ctx, int count)
1010 {
1011 46 APEPredictor *p = &ctx->predictor;
1012 46 int32_t *decoded0 = ctx->decoded[0];
1013 46 int32_t *decoded1 = ctx->decoded[1];
1014 46 int start = 4, shift = 10;
1015
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
1017 start = 16;
1018 long_filter_high_3800(decoded0, 16, 9, count);
1019 long_filter_high_3800(decoded1, 16, 9, count);
1020
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 34 times.
46 } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
1021 12 int order = 128, shift2 = 11;
1022
1023
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
12 if (ctx->fileversion >= 3830) {
1024 10 order <<= 1;
1025 10 shift++;
1026 10 shift2++;
1027 10 long_filter_ehigh_3830(decoded0 + order, count - order);
1028 10 long_filter_ehigh_3830(decoded1 + order, count - order);
1029 }
1030 12 start = order;
1031 12 long_filter_high_3800(decoded0, order, shift2, count);
1032 12 long_filter_high_3800(decoded1, order, shift2, count);
1033 }
1034
1035
2/2
✓ Branch 0 taken 1456128 times.
✓ Branch 1 taken 46 times.
1456174 while (count--) {
1036 1456128 int X = *decoded0, Y = *decoded1;
1037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1456128 times.
1456128 if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
1038 *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA);
1039 decoded0++;
1040 *decoded1 = filter_fast_3320(p, X, 1, XDELAYA);
1041 decoded1++;
1042 } else {
1043 1456128 *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
1044 start, shift);
1045 1456128 decoded0++;
1046 1456128 *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
1047 start, shift);
1048 1456128 decoded1++;
1049 }
1050
1051 /* Combined */
1052 1456128 p->buf++;
1053 1456128 p->sample_pos++;
1054
1055 /* Have we filled the history buffer? */
1056
2/2
✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 1453284 times.
1456128 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1057 2844 memmove(p->historybuffer, p->buf,
1058 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1059 2844 p->buf = p->historybuffer;
1060 }
1061 }
1062 46 }
1063
1064 static void predictor_decode_mono_3800(APEContext *ctx, int count)
1065 {
1066 APEPredictor *p = &ctx->predictor;
1067 int32_t *decoded0 = ctx->decoded[0];
1068 int start = 4, shift = 10;
1069
1070 if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
1071 start = 16;
1072 long_filter_high_3800(decoded0, 16, 9, count);
1073 } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
1074 int order = 128, shift2 = 11;
1075
1076 if (ctx->fileversion >= 3830) {
1077 order <<= 1;
1078 shift++;
1079 shift2++;
1080 long_filter_ehigh_3830(decoded0 + order, count - order);
1081 }
1082 start = order;
1083 long_filter_high_3800(decoded0, order, shift2, count);
1084 }
1085
1086 while (count--) {
1087 if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
1088 *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA);
1089 decoded0++;
1090 } else {
1091 *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB,
1092 start, shift);
1093 decoded0++;
1094 }
1095
1096 /* Combined */
1097 p->buf++;
1098 p->sample_pos++;
1099
1100 /* Have we filled the history buffer? */
1101 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1102 memmove(p->historybuffer, p->buf,
1103 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1104 p->buf = p->historybuffer;
1105 }
1106 }
1107 }
1108
1109 331776 static av_always_inline int predictor_update_3930(APEPredictor *p,
1110 const int decoded, const int filter,
1111 const int delayA)
1112 {
1113 int32_t predictionA, sign;
1114 uint32_t d0, d1, d2, d3;
1115
1116 331776 p->buf[delayA] = p->lastA[filter];
1117 331776 d0 = p->buf[delayA ];
1118 331776 d1 = p->buf[delayA ] - (unsigned)p->buf[delayA - 1];
1119 331776 d2 = p->buf[delayA - 1] - (unsigned)p->buf[delayA - 2];
1120 331776 d3 = p->buf[delayA - 2] - (unsigned)p->buf[delayA - 3];
1121
1122 331776 predictionA = d0 * p->coeffsA[filter][0] +
1123 331776 d1 * p->coeffsA[filter][1] +
1124 331776 d2 * p->coeffsA[filter][2] +
1125 331776 d3 * p->coeffsA[filter][3];
1126
1127 331776 p->lastA[filter] = decoded + (predictionA >> 9);
1128 331776 p->filterA[filter] = p->lastA[filter] + ((int)(p->filterA[filter] * 31U) >> 5);
1129
1130 331776 sign = APESIGN(decoded);
1131 331776 p->coeffsA[filter][0] += (((int32_t)d0 < 0) * 2 - 1) * sign;
1132 331776 p->coeffsA[filter][1] += (((int32_t)d1 < 0) * 2 - 1) * sign;
1133 331776 p->coeffsA[filter][2] += (((int32_t)d2 < 0) * 2 - 1) * sign;
1134 331776 p->coeffsA[filter][3] += (((int32_t)d3 < 0) * 2 - 1) * sign;
1135
1136 331776 return p->filterA[filter];
1137 }
1138
1139 36 static void predictor_decode_stereo_3930(APEContext *ctx, int count)
1140 {
1141 36 APEPredictor *p = &ctx->predictor;
1142 36 int32_t *decoded0 = ctx->decoded[0];
1143 36 int32_t *decoded1 = ctx->decoded[1];
1144
1145 36 ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1146
1147
2/2
✓ Branch 0 taken 165888 times.
✓ Branch 1 taken 36 times.
165924 while (count--) {
1148 /* Predictor Y */
1149 165888 int Y = *decoded1, X = *decoded0;
1150 165888 *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
1151 165888 decoded0++;
1152 165888 *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
1153 165888 decoded1++;
1154
1155 /* Combined */
1156 165888 p->buf++;
1157
1158 /* Have we filled the history buffer? */
1159
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 165564 times.
165888 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1160 324 memmove(p->historybuffer, p->buf,
1161 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1162 324 p->buf = p->historybuffer;
1163 }
1164 }
1165 36 }
1166
1167 static void predictor_decode_mono_3930(APEContext *ctx, int count)
1168 {
1169 APEPredictor *p = &ctx->predictor;
1170 int32_t *decoded0 = ctx->decoded[0];
1171
1172 ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1173
1174 while (count--) {
1175 *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
1176 decoded0++;
1177
1178 p->buf++;
1179
1180 /* Have we filled the history buffer? */
1181 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1182 memmove(p->historybuffer, p->buf,
1183 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1184 p->buf = p->historybuffer;
1185 }
1186 }
1187 }
1188
1189 893958 static av_always_inline int predictor_update_filter(APEPredictor64 *p,
1190 const int decoded, const int filter,
1191 const int delayA, const int delayB,
1192 const int adaptA, const int adaptB,
1193 int interim_mode)
1194 {
1195 int64_t predictionA, predictionB;
1196 int32_t sign;
1197
1198 893958 p->buf[delayA] = p->lastA[filter];
1199 893958 p->buf[adaptA] = APESIGN(p->buf[delayA]);
1200 893958 p->buf[delayA - 1] = p->buf[delayA] - (uint64_t)p->buf[delayA - 1];
1201 893958 p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
1202
1203 893958 predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
1204 893958 p->buf[delayA - 1] * p->coeffsA[filter][1] +
1205 893958 p->buf[delayA - 2] * p->coeffsA[filter][2] +
1206 893958 p->buf[delayA - 3] * p->coeffsA[filter][3];
1207
1208 /* Apply a scaled first-order filter compression */
1209 893958 p->buf[delayB] = p->filterA[filter ^ 1] - ((int64_t)(p->filterB[filter] * 31ULL) >> 5);
1210 893958 p->buf[adaptB] = APESIGN(p->buf[delayB]);
1211 893958 p->buf[delayB - 1] = p->buf[delayB] - (uint64_t)p->buf[delayB - 1];
1212 893958 p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
1213 893958 p->filterB[filter] = p->filterA[filter ^ 1];
1214
1215 893958 predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
1216 893958 p->buf[delayB - 1] * p->coeffsB[filter][1] +
1217 893958 p->buf[delayB - 2] * p->coeffsB[filter][2] +
1218 893958 p->buf[delayB - 3] * p->coeffsB[filter][3] +
1219 893958 p->buf[delayB - 4] * p->coeffsB[filter][4];
1220
1221
2/2
✓ Branch 0 taken 866310 times.
✓ Branch 1 taken 27648 times.
893958 if (interim_mode < 1) {
1222 866310 predictionA = (int32_t)predictionA;
1223 866310 predictionB = (int32_t)predictionB;
1224 866310 p->lastA[filter] = (int32_t)(decoded + (unsigned)((int32_t)(predictionA + (predictionB >> 1)) >> 10));
1225 } else {
1226 27648 p->lastA[filter] = decoded + ((int64_t)((uint64_t)predictionA + (predictionB >> 1)) >> 10);
1227 }
1228 893958 p->filterA[filter] = p->lastA[filter] + ((int64_t)(p->filterA[filter] * 31ULL) >> 5);
1229
1230 893958 sign = APESIGN(decoded);
1231 893958 p->coeffsA[filter][0] += p->buf[adaptA ] * sign;
1232 893958 p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
1233 893958 p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
1234 893958 p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
1235 893958 p->coeffsB[filter][0] += p->buf[adaptB ] * sign;
1236 893958 p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
1237 893958 p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
1238 893958 p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
1239 893958 p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
1240
1241 893958 return p->filterA[filter];
1242 }
1243
1244 97 static void predictor_decode_stereo_3950(APEContext *ctx, int count)
1245 {
1246 97 APEPredictor64 *p_default = &ctx->predictor64;
1247 APEPredictor64 p_interim;
1248 97 int lcount = count;
1249 97 int num_passes = 1;
1250
1251 97 ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1252
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96 times.
97 if (ctx->interim_mode == -1) {
1253 1 p_interim = *p_default;
1254 1 num_passes ++;
1255 1 memcpy(ctx->interim[0], ctx->decoded[0], sizeof(*ctx->interim[0])*count);
1256 1 memcpy(ctx->interim[1], ctx->decoded[1], sizeof(*ctx->interim[1])*count);
1257 }
1258
1259
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 97 times.
195 for (int pass = 0; pass < num_passes; pass++) {
1260 int32_t *decoded0, *decoded1;
1261
3/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
98 int interim_mode = ctx->interim_mode > 0 || pass;
1262 APEPredictor64 *p;
1263
1264
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 97 times.
98 if (pass) {
1265 1 p = &p_interim;
1266 1 decoded0 = ctx->interim[0];
1267 1 decoded1 = ctx->interim[1];
1268 } else {
1269 97 p = p_default;
1270 97 decoded0 = ctx->decoded[0];
1271 97 decoded1 = ctx->decoded[1];
1272 }
1273 98 p->buf = p->historybuffer;
1274
1275 98 count = lcount;
1276
2/2
✓ Branch 0 taken 446979 times.
✓ Branch 1 taken 97 times.
447076 while (count--) {
1277 /* Predictor Y */
1278 446979 int32_t a0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1279 YADAPTCOEFFSA, YADAPTCOEFFSB,
1280 interim_mode);
1281 446979 int32_t a1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1282 XADAPTCOEFFSA, XADAPTCOEFFSB,
1283 interim_mode);
1284 446979 *decoded0++ = a0;
1285 446979 *decoded1++ = a1;
1286
2/2
✓ Branch 0 taken 4611 times.
✓ Branch 1 taken 442368 times.
446979 if (num_passes > 1) {
1287 4611 int32_t left = a1 - (unsigned)(a0 / 2);
1288 4611 int32_t right = left + (unsigned)a0;
1289
1290
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4610 times.
4611 if (FFMIN(FFNABS(left), FFNABS(right)) < -(1<<23)) {
1291 1 ctx->interim_mode = !interim_mode;
1292 1 av_log(ctx->avctx, AV_LOG_VERBOSE, "Interim mode: %d\n", ctx->interim_mode);
1293 1 break;
1294 }
1295 }
1296
1297 /* Combined */
1298 446978 p->buf++;
1299
1300 /* Have we filled the history buffer? */
1301
2/2
✓ Branch 0 taken 873 times.
✓ Branch 1 taken 446105 times.
446978 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1302 873 memmove(p->historybuffer, p->buf,
1303 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1304 873 p->buf = p->historybuffer;
1305 }
1306 }
1307 }
1308
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
97 if (num_passes > 1 && ctx->interim_mode > 0) {
1309 1 memcpy(ctx->decoded[0], ctx->interim[0], sizeof(*ctx->interim[0])*lcount);
1310 1 memcpy(ctx->decoded[1], ctx->interim[1], sizeof(*ctx->interim[1])*lcount);
1311 1 *p_default = p_interim;
1312 1 p_default->buf = p_default->historybuffer;
1313 }
1314 97 }
1315
1316 static void predictor_decode_mono_3950(APEContext *ctx, int count)
1317 {
1318 APEPredictor64 *p = &ctx->predictor64;
1319 int32_t *decoded0 = ctx->decoded[0];
1320 int32_t predictionA, currentA, A, sign;
1321
1322 ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1323
1324 currentA = p->lastA[0];
1325
1326 while (count--) {
1327 A = *decoded0;
1328
1329 p->buf[YDELAYA] = currentA;
1330 p->buf[YDELAYA - 1] = p->buf[YDELAYA] - (uint64_t)p->buf[YDELAYA - 1];
1331
1332 predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
1333 p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
1334 p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
1335 p->buf[YDELAYA - 3] * p->coeffsA[0][3];
1336
1337 currentA = A + (uint64_t)(predictionA >> 10);
1338
1339 p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
1340 p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
1341
1342 sign = APESIGN(A);
1343 p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ] * sign;
1344 p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
1345 p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
1346 p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
1347
1348 p->buf++;
1349
1350 /* Have we filled the history buffer? */
1351 if (p->buf == p->historybuffer + HISTORY_SIZE) {
1352 memmove(p->historybuffer, p->buf,
1353 PREDICTOR_SIZE * sizeof(*p->historybuffer));
1354 p->buf = p->historybuffer;
1355 }
1356
1357 p->filterA[0] = currentA + (uint64_t)((int64_t)(p->filterA[0] * 31U) >> 5);
1358 *(decoded0++) = p->filterA[0];
1359 }
1360
1361 p->lastA[0] = currentA;
1362 }
1363
1364 158 static void do_init_filter(APEFilter *f, int16_t *buf, int order)
1365 {
1366 158 f->coeffs = buf;
1367 158 f->historybuffer = buf + order;
1368 158 f->delay = f->historybuffer + order * 2;
1369 158 f->adaptcoeffs = f->historybuffer + order;
1370
1371 158 memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer));
1372 158 memset(f->coeffs, 0, order * sizeof(*f->coeffs));
1373 158 f->avg = 0;
1374 158 }
1375
1376 79 static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
1377 {
1378 79 do_init_filter(&f[0], buf, order);
1379 79 do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
1380 79 }
1381
1382 302 static void do_apply_filter(APEContext *ctx, int version, APEFilter *f,
1383 int32_t *data, int count, int order, int fracbits)
1384 {
1385 int res;
1386 unsigned absres;
1387
1388
2/2
✓ Branch 0 taken 1391616 times.
✓ Branch 1 taken 302 times.
1391918 while (count--) {
1389 /* round fixedpoint scalar product */
1390 1391616 res = ctx->adsp.scalarproduct_and_madd_int16(f->coeffs,
1391 1391616 f->delay - order,
1392 1391616 f->adaptcoeffs - order,
1393 order, APESIGN(*data));
1394 1391616 res = (int64_t)(res + (1LL << (fracbits - 1))) >> fracbits;
1395 1391616 res += (unsigned)*data;
1396 1391616 *data++ = res;
1397
1398 /* Update the output history */
1399 1391616 *f->delay++ = av_clip_int16(res);
1400
1401
2/2
✓ Branch 0 taken 497664 times.
✓ Branch 1 taken 893952 times.
1391616 if (version < 3980) {
1402 /* Version ??? to < 3.98 files (untested) */
1403
2/2
✓ Branch 0 taken 492164 times.
✓ Branch 1 taken 5500 times.
497664 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1404 497664 f->adaptcoeffs[-4] >>= 1;
1405 497664 f->adaptcoeffs[-8] >>= 1;
1406 } else {
1407 /* Version 3.98 and later files */
1408
1409 /* Update the adaption coefficients */
1410
2/2
✓ Branch 0 taken 444730 times.
✓ Branch 1 taken 449222 times.
893952 absres = FFABSU(res);
1411
2/2
✓ Branch 0 taken 890829 times.
✓ Branch 1 taken 3123 times.
893952 if (absres)
1412 890829 *f->adaptcoeffs = APESIGN(res) *
1413 890829 (8 << ((absres > f->avg * 3LL) + (absres > (f->avg + f->avg / 3))));
1414 /* equivalent to the following code
1415 if (absres <= f->avg * 4 / 3)
1416 *f->adaptcoeffs = APESIGN(res) * 8;
1417 else if (absres <= f->avg * 3)
1418 *f->adaptcoeffs = APESIGN(res) * 16;
1419 else
1420 *f->adaptcoeffs = APESIGN(res) * 32;
1421 */
1422 else
1423 3123 *f->adaptcoeffs = 0;
1424
1425 893952 f->avg += (int)(absres - (unsigned)f->avg) / 16;
1426
1427 893952 f->adaptcoeffs[-1] >>= 1;
1428 893952 f->adaptcoeffs[-2] >>= 1;
1429 893952 f->adaptcoeffs[-8] >>= 1;
1430 }
1431
1432 1391616 f->adaptcoeffs++;
1433
1434 /* Have we filled the history buffer? */
1435
2/2
✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 1388898 times.
1391616 if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
1436 2718 memmove(f->historybuffer, f->delay - (order * 2),
1437 2718 (order * 2) * sizeof(*f->historybuffer));
1438 2718 f->delay = f->historybuffer + order * 2;
1439 2718 f->adaptcoeffs = f->historybuffer + order;
1440 }
1441 }
1442 302 }
1443
1444 151 static void apply_filter(APEContext *ctx, APEFilter *f,
1445 int32_t *data0, int32_t *data1,
1446 int count, int order, int fracbits)
1447 {
1448 151 do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
1449
1/2
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
151 if (data1)
1450 151 do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
1451 151 }
1452
1453 133 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
1454 int32_t *decoded1, int count)
1455 {
1456 int i;
1457
1458
1/2
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
284 for (i = 0; i < APE_FILTER_LEVELS; i++) {
1459
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 151 times.
284 if (!ape_filter_orders[ctx->fset][i])
1460 133 break;
1461 151 apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
1462 151 ape_filter_orders[ctx->fset][i],
1463 151 ape_filter_fracbits[ctx->fset][i]);
1464 }
1465 133 }
1466
1467 62 static int init_frame_decoder(APEContext *ctx)
1468 {
1469 int i, ret;
1470
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
62 if ((ret = init_entropy_decoder(ctx)) < 0)
1471 return ret;
1472 62 init_predictor_decoder(ctx);
1473
1474
1/2
✓ Branch 0 taken 141 times.
✗ Branch 1 not taken.
141 for (i = 0; i < APE_FILTER_LEVELS; i++) {
1475
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 79 times.
141 if (!ape_filter_orders[ctx->fset][i])
1476 62 break;
1477 79 init_filter(ctx, ctx->filters[i], ctx->filterbuf[i],
1478 79 ape_filter_orders[ctx->fset][i]);
1479 }
1480 62 return 0;
1481 }
1482
1483 static void ape_unpack_mono(APEContext *ctx, int count)
1484 {
1485 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1486 /* We are pure silence, so we're done. */
1487 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
1488 return;
1489 }
1490
1491 ctx->entropy_decode_mono(ctx, count);
1492 if (ctx->error)
1493 return;
1494
1495 /* Now apply the predictor decoding */
1496 ctx->predictor_decode_mono(ctx, count);
1497
1498 /* Pseudo-stereo - just copy left channel to right channel */
1499 if (ctx->channels == 2) {
1500 memcpy(ctx->decoded[1], ctx->decoded[0], count * sizeof(*ctx->decoded[1]));
1501 }
1502 }
1503
1504 186 static void ape_unpack_stereo(APEContext *ctx, int count)
1505 {
1506 unsigned left, right;
1507 186 int32_t *decoded0 = ctx->decoded[0];
1508 186 int32_t *decoded1 = ctx->decoded[1];
1509
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if ((ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) == APE_FRAMECODE_STEREO_SILENCE) {
1511 /* We are pure silence, so we're done. */
1512 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
1513 return;
1514 }
1515
1516 186 ctx->entropy_decode_stereo(ctx, count);
1517
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 179 times.
186 if (ctx->error)
1518 7 return;
1519
1520 /* Now apply the predictor decoding */
1521 179 ctx->predictor_decode_stereo(ctx, count);
1522
1523 /* Decorrelate and scale to output depth */
1524
2/2
✓ Branch 0 taken 2068992 times.
✓ Branch 1 taken 179 times.
2069171 while (count--) {
1525 2068992 left = *decoded1 - (unsigned)(*decoded0 / 2);
1526 2068992 right = left + *decoded0;
1527
1528 2068992 *(decoded0++) = left;
1529 2068992 *(decoded1++) = right;
1530 }
1531 }
1532
1533 193 static int ape_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1534 int *got_frame_ptr, AVPacket *avpkt)
1535 {
1536 193 const uint8_t *buf = avpkt->data;
1537 193 APEContext *s = avctx->priv_data;
1538 uint8_t *sample8;
1539 int16_t *sample16;
1540 int32_t *sample24;
1541 int i, ch, ret;
1542 int blockstodecode;
1543 uint64_t decoded_buffer_size;
1544
1545 /* this should never be negative, but bad things will happen if it is, so
1546 check it just to make sure. */
1547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 av_assert0(s->samples >= 0);
1548
1549
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 124 times.
193 if(!s->samples){
1550 uint32_t nblocks, offset;
1551 int buf_size;
1552
1553
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 62 times.
69 if (!avpkt->size) {
1554 7 *got_frame_ptr = 0;
1555 7 return 0;
1556 }
1557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (avpkt->size < 8) {
1558 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1559 return AVERROR_INVALIDDATA;
1560 }
1561 62 buf_size = avpkt->size & ~3;
1562
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 61 times.
62 if (buf_size != avpkt->size) {
1563 1 av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
1564 "extra bytes at the end will be skipped.\n");
1565 }
1566
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 7 times.
62 if (s->fileversion < 3950) // previous versions overread two bytes
1567 55 buf_size += 2;
1568 62 av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
1569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (!s->data)
1570 return AVERROR(ENOMEM);
1571 62 s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
1572 buf_size >> 2);
1573 62 memset(s->data + (buf_size & ~3), 0, buf_size & 3);
1574 62 s->ptr = s->data;
1575 62 s->data_end = s->data + buf_size;
1576
1577 62 nblocks = bytestream_get_be32(&s->ptr);
1578 62 offset = bytestream_get_be32(&s->ptr);
1579
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
62 if (s->fileversion >= 3900) {
1580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (offset > 3) {
1581 av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
1582 av_freep(&s->data);
1583 s->data_size = 0;
1584 return AVERROR_INVALIDDATA;
1585 }
1586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (s->data_end - s->ptr < offset) {
1587 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1588 return AVERROR_INVALIDDATA;
1589 }
1590 23 s->ptr += offset;
1591 } else {
1592
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
39 if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0)
1593 return ret;
1594
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 13 times.
39 if (s->fileversion > 3800)
1595 26 skip_bits_long(&s->gb, offset * 8);
1596 else
1597 13 skip_bits_long(&s->gb, offset);
1598 }
1599
1600
2/4
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
62 if (!nblocks || nblocks > INT_MAX / 2 / sizeof(*s->decoded_buffer) - 8) {
1601 av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %"PRIu32".\n",
1602 nblocks);
1603 return AVERROR_INVALIDDATA;
1604 }
1605
1606 /* Initialize the frame decoder */
1607
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
62 if (init_frame_decoder(s) < 0) {
1608 av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
1609 return AVERROR_INVALIDDATA;
1610 }
1611 62 s->samples = nblocks;
1612 }
1613
1614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (!s->data) {
1615 *got_frame_ptr = 0;
1616 return avpkt->size;
1617 }
1618
1619 186 blockstodecode = FFMIN(s->blocks_per_loop, s->samples);
1620 // for old files coefficients were not interleaved,
1621 // so we need to decode all of them at once
1622
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 135 times.
186 if (s->fileversion < 3930)
1623 51 blockstodecode = s->samples;
1624
1625 /* reallocate decoded sample buffer if needed */
1626 186 decoded_buffer_size = 2LL * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer);
1627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 av_assert0(decoded_buffer_size <= INT_MAX);
1628
1629 /* get output buffer */
1630 186 frame->nb_samples = blockstodecode;
1631
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
186 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1632 s->samples=0;
1633 return ret;
1634 }
1635
1636 186 av_fast_malloc(&s->decoded_buffer, &s->decoded_size, decoded_buffer_size);
1637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
186 if (!s->decoded_buffer)
1638 return AVERROR(ENOMEM);
1639 186 memset(s->decoded_buffer, 0, decoded_buffer_size);
1640 186 s->decoded[0] = s->decoded_buffer;
1641 186 s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
1642
1643
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 185 times.
186 if (s->interim_mode < 0) {
1644 1 av_fast_malloc(&s->interim_buffer, &s->interim_size, decoded_buffer_size);
1645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->interim_buffer)
1646 return AVERROR(ENOMEM);
1647 1 memset(s->interim_buffer, 0, decoded_buffer_size);
1648 1 s->interim[0] = s->interim_buffer;
1649 1 s->interim[1] = s->interim_buffer + FFALIGN(blockstodecode, 8);
1650 } else {
1651 185 av_freep(&s->interim_buffer);
1652 185 s->interim_size = 0;
1653 185 memset(s->interim, 0, sizeof(s->interim));
1654 }
1655
1656 186 s->error=0;
1657
1658
2/4
✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 186 times.
186 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
1659 ape_unpack_mono(s, blockstodecode);
1660 else
1661 186 ape_unpack_stereo(s, blockstodecode);
1662
1663
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 179 times.
186 if (s->error) {
1664 7 s->samples=0;
1665 7 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
1666 7 return AVERROR_INVALIDDATA;
1667 }
1668
1669
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
179 switch (s->bps) {
1670 case 8:
1671 for (ch = 0; ch < s->channels; ch++) {
1672 sample8 = (uint8_t *)frame->data[ch];
1673 for (i = 0; i < blockstodecode; i++)
1674 *sample8++ = (s->decoded[ch][i] + 0x80U) & 0xff;
1675 }
1676 break;
1677 176 case 16:
1678
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 176 times.
528 for (ch = 0; ch < s->channels; ch++) {
1679 352 sample16 = (int16_t *)frame->data[ch];
1680
2/2
✓ Branch 0 taken 4110336 times.
✓ Branch 1 taken 352 times.
4110688 for (i = 0; i < blockstodecode; i++)
1681 4110336 *sample16++ = s->decoded[ch][i];
1682 }
1683 176 break;
1684 3 case 24:
1685
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (ch = 0; ch < s->channels; ch++) {
1686 6 sample24 = (int32_t *)frame->data[ch];
1687
2/2
✓ Branch 0 taken 27648 times.
✓ Branch 1 taken 6 times.
27654 for (i = 0; i < blockstodecode; i++)
1688 27648 *sample24++ = s->decoded[ch][i] * 256U;
1689 }
1690 3 break;
1691 }
1692
1693 179 s->samples -= blockstodecode;
1694
1695
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 179 times.
179 if (avctx->err_recognition & AV_EF_CRCCHECK &&
1696 s->fileversion >= 3900) {
1697 uint32_t crc = s->CRC_state;
1698 const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1699 int stride = s->bps == 24 ? 4 : (s->bps>>3);
1700 int offset = s->bps == 24;
1701 int bytes = s->bps >> 3;
1702
1703 for (i = 0; i < blockstodecode; i++) {
1704 for (ch = 0; ch < s->channels; ch++) {
1705 #if HAVE_BIGENDIAN
1706 uint8_t *smp_native = frame->data[ch] + i*stride;
1707 uint8_t smp[4];
1708 for(int j = 0; j<stride; j++)
1709 smp[j] = smp_native[stride-j-1];
1710 #else
1711 uint8_t *smp = frame->data[ch] + i*stride;
1712 #endif
1713 crc = av_crc(crc_tab, crc, smp+offset, bytes);
1714 }
1715 }
1716
1717 if (!s->samples && (~crc >> 1) ^ s->CRC) {
1718 av_log(avctx, AV_LOG_ERROR, "CRC mismatch! Previously decoded "
1719 "frames may have been affected as well.\n");
1720 if (avctx->err_recognition & AV_EF_EXPLODE)
1721 return AVERROR_INVALIDDATA;
1722 }
1723
1724 s->CRC_state = crc;
1725 }
1726
1727 179 *got_frame_ptr = 1;
1728
1729
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 126 times.
179 return !s->samples ? avpkt->size : 0;
1730 }
1731
1732 static void ape_flush(AVCodecContext *avctx)
1733 {
1734 APEContext *s = avctx->priv_data;
1735 s->samples= 0;
1736 }
1737
1738 #define OFFSET(x) offsetof(APEContext, x)
1739 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1740 static const AVOption options[] = {
1741 { "max_samples", "maximum number of samples decoded per call", OFFSET(blocks_per_loop), AV_OPT_TYPE_INT, { .i64 = 4608 }, 1, INT_MAX, PAR, .unit = "max_samples" },
1742 { "all", "no maximum. decode all samples for each packet at once", 0, AV_OPT_TYPE_CONST, { .i64 = INT_MAX }, INT_MIN, INT_MAX, PAR, .unit = "max_samples" },
1743 { NULL},
1744 };
1745
1746 static const AVClass ape_decoder_class = {
1747 .class_name = "APE decoder",
1748 .item_name = av_default_item_name,
1749 .option = options,
1750 .version = LIBAVUTIL_VERSION_INT,
1751 };
1752
1753 const FFCodec ff_ape_decoder = {
1754 .p.name = "ape",
1755 CODEC_LONG_NAME("Monkey's Audio"),
1756 .p.type = AVMEDIA_TYPE_AUDIO,
1757 .p.id = AV_CODEC_ID_APE,
1758 .priv_data_size = sizeof(APEContext),
1759 .init = ape_decode_init,
1760 .close = ape_decode_close,
1761 FF_CODEC_DECODE_CB(ape_decode_frame),
1762 .p.capabilities =
1763 #if FF_API_SUBFRAMES
1764 AV_CODEC_CAP_SUBFRAMES |
1765 #endif
1766 AV_CODEC_CAP_DELAY |
1767 AV_CODEC_CAP_DR1,
1768 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1769 .flush = ape_flush,
1770 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
1771 AV_SAMPLE_FMT_S16P,
1772 AV_SAMPLE_FMT_S32P,
1773 AV_SAMPLE_FMT_NONE },
1774 .p.priv_class = &ape_decoder_class,
1775 };
1776