FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/apedec.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 582 799 72.8%
Branches: 208 338 61.5%

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