FFmpeg
apedec.c
Go to the documentation of this file.
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"
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 "internal.h"
34 #include "get_bits.h"
35 #include "unary.h"
36 
37 /**
38  * @file
39  * Monkey's Audio lossless audio decoder
40  */
41 
42 #define MAX_CHANNELS 2
43 #define MAX_BYTESPERSAMPLE 3
44 
45 #define APE_FRAMECODE_MONO_SILENCE 1
46 #define APE_FRAMECODE_STEREO_SILENCE 3
47 #define APE_FRAMECODE_PSEUDO_STEREO 4
48 
49 #define HISTORY_SIZE 512
50 #define PREDICTOR_ORDER 8
51 /** Total size of all predictor histories */
52 #define PREDICTOR_SIZE 50
53 
54 #define YDELAYA (18 + PREDICTOR_ORDER*4)
55 #define YDELAYB (18 + PREDICTOR_ORDER*3)
56 #define XDELAYA (18 + PREDICTOR_ORDER*2)
57 #define XDELAYB (18 + PREDICTOR_ORDER)
58 
59 #define YADAPTCOEFFSA 18
60 #define XADAPTCOEFFSA 14
61 #define YADAPTCOEFFSB 10
62 #define XADAPTCOEFFSB 5
63 
64 /**
65  * Possible compression levels
66  * @{
67  */
74 };
75 /** @} */
76 
77 #define APE_FILTER_LEVELS 3
78 
79 /** Filter orders depending on compression level */
80 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
81  { 0, 0, 0 },
82  { 16, 0, 0 },
83  { 64, 0, 0 },
84  { 32, 256, 0 },
85  { 16, 256, 1280 }
86 };
87 
88 /** Filter fraction bits depending on compression level */
90  { 0, 0, 0 },
91  { 11, 0, 0 },
92  { 11, 0, 0 },
93  { 10, 13, 0 },
94  { 11, 13, 15 }
95 };
96 
97 
98 /** Filters applied to the decoded data */
99 typedef struct APEFilter {
100  int16_t *coeffs; ///< actual coefficients used in filtering
101  int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients
102  int16_t *historybuffer; ///< filter memory
103  int16_t *delay; ///< filtered values
104 
105  uint32_t avg;
106 } APEFilter;
107 
108 typedef struct APERice {
109  uint32_t k;
110  uint32_t ksum;
111 } APERice;
112 
113 typedef struct APERangecoder {
114  uint32_t low; ///< low end of interval
115  uint32_t range; ///< length of interval
116  uint32_t help; ///< bytes_to_follow resp. intermediate value
117  unsigned int buffer; ///< buffer for input/output
118 } APERangecoder;
119 
120 /** Filter histories */
121 typedef struct APEPredictor {
123 
125 
128 
129  uint32_t coeffsA[2][4]; ///< adaption coefficients
130  uint32_t coeffsB[2][5]; ///< adaption coefficients
132 
133  unsigned int sample_pos;
134 } APEPredictor;
135 
136 typedef struct APEPredictor64 {
137  int64_t *buf;
138 
139  int64_t lastA[2];
140 
141  int64_t filterA[2];
142  int64_t filterB[2];
143 
144  uint64_t coeffsA[2][4]; ///< adaption coefficients
145  uint64_t coeffsB[2][5]; ///< adaption coefficients
147 
148  unsigned int sample_pos;
150 
151 /** Decoder context */
152 typedef struct APEContext {
153  AVClass *class; ///< class for AVOptions
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 
174  int32_t *decoded[MAX_CHANNELS]; ///< decoded data for each channel
175  int blocks_per_loop; ///< maximum number of samples to decode for each call
176 
177  int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory
178 
179  APERangecoder rc; ///< rangecoder used to decode actual values
180  APERice riceX; ///< rice code parameters for the second channel
181  APERice riceY; ///< rice code parameters for the first channel
182  APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
184 
185  uint8_t *data; ///< current frame data
186  uint8_t *data_end; ///< frame data end
187  int data_size; ///< frame data allocated size
188  const uint8_t *ptr; ///< current position in frame data
189 
190  int error;
191 
192  void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
193  void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
194  void (*predictor_decode_mono)(struct APEContext *ctx, int count);
195  void (*predictor_decode_stereo)(struct APEContext *ctx, int count);
196 } APEContext;
197 
198 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
199  int32_t *decoded1, int count);
200 
201 static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
202 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
203 static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode);
204 static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode);
205 static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
206 static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
207 static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode);
208 static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
209 static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
210 
211 static void predictor_decode_mono_3800(APEContext *ctx, int count);
212 static void predictor_decode_stereo_3800(APEContext *ctx, int count);
213 static void predictor_decode_mono_3930(APEContext *ctx, int count);
214 static void predictor_decode_stereo_3930(APEContext *ctx, int count);
215 static void predictor_decode_mono_3950(APEContext *ctx, int count);
216 static void predictor_decode_stereo_3950(APEContext *ctx, int count);
217 
219 {
221  int i;
222 
223  for (i = 0; i < APE_FILTER_LEVELS; i++)
224  av_freep(&s->filterbuf[i]);
225 
226  av_freep(&s->decoded_buffer);
227  av_freep(&s->data);
228  s->decoded_size = s->data_size = 0;
229 
230  return 0;
231 }
232 
234 {
236  int i;
237 
238  if (avctx->extradata_size != 6) {
239  av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
240  return AVERROR(EINVAL);
241  }
242  if (avctx->channels > 2) {
243  av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
244  return AVERROR(EINVAL);
245  }
247  s->bps = avctx->bits_per_coded_sample;
248  switch (s->bps) {
249  case 8:
251  break;
252  case 16:
254  break;
255  case 24:
257  break;
258  default:
260  "%d bits per coded sample", s->bps);
261  return AVERROR_PATCHWELCOME;
262  }
263  s->avctx = avctx;
264  s->channels = avctx->channels;
265  s->fileversion = AV_RL16(avctx->extradata);
266  s->compression_level = AV_RL16(avctx->extradata + 2);
267  s->flags = AV_RL16(avctx->extradata + 4);
268 
269  av_log(avctx, AV_LOG_VERBOSE, "Compression Level: %d - Flags: %d\n",
270  s->compression_level, s->flags);
271  if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
272  !s->compression_level ||
273  (s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) {
274  av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
275  s->compression_level);
276  return AVERROR_INVALIDDATA;
277  }
278  s->fset = s->compression_level / 1000 - 1;
279  for (i = 0; i < APE_FILTER_LEVELS; i++) {
280  if (!ape_filter_orders[s->fset][i])
281  break;
282  if (!(s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4)))
283  return AVERROR(ENOMEM);
284  }
285 
286  if (s->fileversion < 3860) {
287  s->entropy_decode_mono = entropy_decode_mono_0000;
288  s->entropy_decode_stereo = entropy_decode_stereo_0000;
289  } else if (s->fileversion < 3900) {
290  s->entropy_decode_mono = entropy_decode_mono_3860;
291  s->entropy_decode_stereo = entropy_decode_stereo_3860;
292  } else if (s->fileversion < 3930) {
293  s->entropy_decode_mono = entropy_decode_mono_3900;
294  s->entropy_decode_stereo = entropy_decode_stereo_3900;
295  } else if (s->fileversion < 3990) {
296  s->entropy_decode_mono = entropy_decode_mono_3900;
297  s->entropy_decode_stereo = entropy_decode_stereo_3930;
298  } else {
299  s->entropy_decode_mono = entropy_decode_mono_3990;
300  s->entropy_decode_stereo = entropy_decode_stereo_3990;
301  }
302 
303  if (s->fileversion < 3930) {
304  s->predictor_decode_mono = predictor_decode_mono_3800;
305  s->predictor_decode_stereo = predictor_decode_stereo_3800;
306  } else if (s->fileversion < 3950) {
307  s->predictor_decode_mono = predictor_decode_mono_3930;
308  s->predictor_decode_stereo = predictor_decode_stereo_3930;
309  } else {
310  s->predictor_decode_mono = predictor_decode_mono_3950;
311  s->predictor_decode_stereo = predictor_decode_stereo_3950;
312  }
313 
314  ff_bswapdsp_init(&s->bdsp);
315  ff_llauddsp_init(&s->adsp);
317 
318  return 0;
319 }
320 
321 /**
322  * @name APE range decoding functions
323  * @{
324  */
325 
326 #define CODE_BITS 32
327 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
328 #define SHIFT_BITS (CODE_BITS - 9)
329 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
330 #define BOTTOM_VALUE (TOP_VALUE >> 8)
331 
332 /** Start the decoder */
333 static inline void range_start_decoding(APEContext *ctx)
334 {
335  ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
336  ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
337  ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
338 }
339 
340 /** Perform normalization */
341 static inline void range_dec_normalize(APEContext *ctx)
342 {
343  while (ctx->rc.range <= BOTTOM_VALUE) {
344  ctx->rc.buffer <<= 8;
345  if(ctx->ptr < ctx->data_end) {
346  ctx->rc.buffer += *ctx->ptr;
347  ctx->ptr++;
348  } else {
349  ctx->error = 1;
350  }
351  ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
352  ctx->rc.range <<= 8;
353  }
354 }
355 
356 /**
357  * Calculate cumulative frequency for next symbol. Does NO update!
358  * @param ctx decoder context
359  * @param tot_f is the total frequency or (code_value)1<<shift
360  * @return the cumulative frequency
361  */
362 static inline int range_decode_culfreq(APEContext *ctx, int tot_f)
363 {
365  ctx->rc.help = ctx->rc.range / tot_f;
366  return ctx->rc.low / ctx->rc.help;
367 }
368 
369 /**
370  * Decode value with given size in bits
371  * @param ctx decoder context
372  * @param shift number of bits to decode
373  */
374 static inline int range_decode_culshift(APEContext *ctx, int shift)
375 {
377  ctx->rc.help = ctx->rc.range >> shift;
378  return ctx->rc.low / ctx->rc.help;
379 }
380 
381 
382 /**
383  * Update decoding state
384  * @param ctx decoder context
385  * @param sy_f the interval length (frequency of the symbol)
386  * @param lt_f the lower end (frequency sum of < symbols)
387  */
388 static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
389 {
390  ctx->rc.low -= ctx->rc.help * lt_f;
391  ctx->rc.range = ctx->rc.help * sy_f;
392 }
393 
394 /** Decode n bits (n <= 16) without modelling */
395 static inline int range_decode_bits(APEContext *ctx, int n)
396 {
397  int sym = range_decode_culshift(ctx, n);
398  range_decode_update(ctx, 1, sym);
399  return sym;
400 }
401 
402 
403 #define MODEL_ELEMENTS 64
404 
405 /**
406  * Fixed probabilities for symbols in Monkey Audio version 3.97
407  */
408 static const uint16_t counts_3970[22] = {
409  0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
410  62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
411  65450, 65469, 65480, 65487, 65491, 65493,
412 };
413 
414 /**
415  * Probability ranges for symbols in Monkey Audio version 3.97
416  */
417 static const uint16_t counts_diff_3970[21] = {
418  14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
419  1104, 677, 415, 248, 150, 89, 54, 31,
420  19, 11, 7, 4, 2,
421 };
422 
423 /**
424  * Fixed probabilities for symbols in Monkey Audio version 3.98
425  */
426 static const uint16_t counts_3980[22] = {
427  0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
428  64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
429  65485, 65488, 65490, 65491, 65492, 65493,
430 };
431 
432 /**
433  * Probability ranges for symbols in Monkey Audio version 3.98
434  */
435 static const uint16_t counts_diff_3980[21] = {
436  19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
437  261, 119, 65, 31, 19, 10, 6, 3,
438  3, 2, 1, 1, 1,
439 };
440 
441 /**
442  * Decode symbol
443  * @param ctx decoder context
444  * @param counts probability range start position
445  * @param counts_diff probability range widths
446  */
447 static inline int range_get_symbol(APEContext *ctx,
448  const uint16_t counts[],
449  const uint16_t counts_diff[])
450 {
451  int symbol, cf;
452 
453  cf = range_decode_culshift(ctx, 16);
454 
455  if(cf > 65492){
456  symbol= cf - 65535 + 63;
457  range_decode_update(ctx, 1, cf);
458  if(cf > 65535)
459  ctx->error=1;
460  return symbol;
461  }
462  /* figure out the symbol inefficiently; a binary search would be much better */
463  for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
464 
465  range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
466 
467  return symbol;
468 }
469 /** @} */ // group rangecoder
470 
471 static inline void update_rice(APERice *rice, unsigned int x)
472 {
473  int lim = rice->k ? (1 << (rice->k + 4)) : 0;
474  rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
475 
476  if (rice->ksum < lim)
477  rice->k--;
478  else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
479  rice->k++;
480 }
481 
482 static inline int get_rice_ook(GetBitContext *gb, int k)
483 {
484  unsigned int x;
485 
486  x = get_unary(gb, 1, get_bits_left(gb));
487 
488  if (k)
489  x = (x << k) | get_bits(gb, k);
490 
491  return x;
492 }
493 
495  APERice *rice)
496 {
497  unsigned int x, overflow;
498 
500 
501  if (ctx->fileversion > 3880) {
502  while (overflow >= 16) {
503  overflow -= 16;
504  rice->k += 4;
505  }
506  }
507 
508  if (!rice->k)
509  x = overflow;
510  else if(rice->k <= MIN_CACHE_BITS) {
511  x = (overflow << rice->k) + get_bits(gb, rice->k);
512  } else {
513  av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %"PRIu32"\n", rice->k);
514  ctx->error = 1;
515  return AVERROR_INVALIDDATA;
516  }
517  rice->ksum += x - (rice->ksum + 8 >> 4);
518  if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
519  rice->k--;
520  else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
521  rice->k++;
522 
523  /* Convert to signed */
524  return ((x >> 1) ^ ((x & 1) - 1)) + 1;
525 }
526 
527 static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
528 {
529  unsigned int x, overflow;
530  int tmpk;
531 
533 
534  if (overflow == (MODEL_ELEMENTS - 1)) {
535  tmpk = range_decode_bits(ctx, 5);
536  overflow = 0;
537  } else
538  tmpk = (rice->k < 1) ? 0 : rice->k - 1;
539 
540  if (tmpk <= 16 || ctx->fileversion < 3910) {
541  if (tmpk > 23) {
542  av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
543  return AVERROR_INVALIDDATA;
544  }
545  x = range_decode_bits(ctx, tmpk);
546  } else if (tmpk <= 31) {
547  x = range_decode_bits(ctx, 16);
548  x |= (range_decode_bits(ctx, tmpk - 16) << 16);
549  } else {
550  av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
551  return AVERROR_INVALIDDATA;
552  }
553  x += overflow << tmpk;
554 
555  update_rice(rice, x);
556 
557  /* Convert to signed */
558  return ((x >> 1) ^ ((x & 1) - 1)) + 1;
559 }
560 
561 static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
562 {
563  unsigned int x, overflow, pivot;
564  int base;
565 
566  pivot = FFMAX(rice->ksum >> 5, 1);
567 
569 
570  if (overflow == (MODEL_ELEMENTS - 1)) {
571  overflow = (unsigned)range_decode_bits(ctx, 16) << 16;
573  }
574 
575  if (pivot < 0x10000) {
576  base = range_decode_culfreq(ctx, pivot);
578  } else {
579  int base_hi = pivot, base_lo;
580  int bbits = 0;
581 
582  while (base_hi & ~0xFFFF) {
583  base_hi >>= 1;
584  bbits++;
585  }
586  base_hi = range_decode_culfreq(ctx, base_hi + 1);
587  range_decode_update(ctx, 1, base_hi);
588  base_lo = range_decode_culfreq(ctx, 1 << bbits);
589  range_decode_update(ctx, 1, base_lo);
590 
591  base = (base_hi << bbits) + base_lo;
592  }
593 
594  x = base + overflow * pivot;
595 
596  update_rice(rice, x);
597 
598  /* Convert to signed */
599  return ((x >> 1) ^ ((x & 1) - 1)) + 1;
600 }
601 
602 static int get_k(int ksum)
603 {
604  return av_log2(ksum) + !!ksum;
605 }
606 
608  int32_t *out, APERice *rice, int blockstodecode)
609 {
610  int i;
611  unsigned ksummax, ksummin;
612 
613  rice->ksum = 0;
614  for (i = 0; i < FFMIN(blockstodecode, 5); i++) {
615  out[i] = get_rice_ook(&ctx->gb, 10);
616  rice->ksum += out[i];
617  }
618 
619  if (blockstodecode <= 5)
620  goto end;
621 
622  rice->k = get_k(rice->ksum / 10);
623  if (rice->k >= 24)
624  return;
625  for (; i < FFMIN(blockstodecode, 64); i++) {
626  out[i] = get_rice_ook(&ctx->gb, rice->k);
627  rice->ksum += out[i];
628  rice->k = get_k(rice->ksum / ((i + 1) * 2));
629  if (rice->k >= 24)
630  return;
631  }
632 
633  if (blockstodecode <= 64)
634  goto end;
635 
636  rice->k = get_k(rice->ksum >> 7);
637  ksummax = 1 << rice->k + 7;
638  ksummin = rice->k ? (1 << rice->k + 6) : 0;
639  for (; i < blockstodecode; i++) {
640  if (get_bits_left(&ctx->gb) < 1) {
641  ctx->error = 1;
642  return;
643  }
644  out[i] = get_rice_ook(&ctx->gb, rice->k);
645  rice->ksum += out[i] - (unsigned)out[i - 64];
646  while (rice->ksum < ksummin) {
647  rice->k--;
648  ksummin = rice->k ? ksummin >> 1 : 0;
649  ksummax >>= 1;
650  }
651  while (rice->ksum >= ksummax) {
652  rice->k++;
653  if (rice->k > 24)
654  return;
655  ksummax <<= 1;
656  ksummin = ksummin ? ksummin << 1 : 128;
657  }
658  }
659 
660 end:
661  for (i = 0; i < blockstodecode; i++)
662  out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1;
663 }
664 
665 static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
666 {
667  decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
668  blockstodecode);
669 }
670 
671 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
672 {
673  decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
674  blockstodecode);
675  decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
676  blockstodecode);
677 }
678 
679 static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
680 {
681  int32_t *decoded0 = ctx->decoded[0];
682 
683  while (blockstodecode--)
684  *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
685 }
686 
687 static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
688 {
689  int32_t *decoded0 = ctx->decoded[0];
690  int32_t *decoded1 = ctx->decoded[1];
691  int blocks = blockstodecode;
692 
693  while (blockstodecode--)
694  *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
695  while (blocks--)
696  *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
697 }
698 
699 static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
700 {
701  int32_t *decoded0 = ctx->decoded[0];
702 
703  while (blockstodecode--)
704  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
705 }
706 
707 static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
708 {
709  int32_t *decoded0 = ctx->decoded[0];
710  int32_t *decoded1 = ctx->decoded[1];
711  int blocks = blockstodecode;
712 
713  while (blockstodecode--)
714  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
716  // because of some implementation peculiarities we need to backpedal here
717  ctx->ptr -= 1;
719  while (blocks--)
720  *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
721 }
722 
723 static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
724 {
725  int32_t *decoded0 = ctx->decoded[0];
726  int32_t *decoded1 = ctx->decoded[1];
727 
728  while (blockstodecode--) {
729  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
730  *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
731  }
732 }
733 
734 static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
735 {
736  int32_t *decoded0 = ctx->decoded[0];
737 
738  while (blockstodecode--)
739  *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
740 }
741 
742 static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
743 {
744  int32_t *decoded0 = ctx->decoded[0];
745  int32_t *decoded1 = ctx->decoded[1];
746 
747  while (blockstodecode--) {
748  *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
749  *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
750  }
751 }
752 
754 {
755  /* Read the CRC */
756  if (ctx->fileversion >= 3900) {
757  if (ctx->data_end - ctx->ptr < 6)
758  return AVERROR_INVALIDDATA;
759  ctx->CRC = bytestream_get_be32(&ctx->ptr);
760  } else {
761  ctx->CRC = get_bits_long(&ctx->gb, 32);
762  }
763 
764  /* Read the frame flags if they exist */
765  ctx->frameflags = 0;
766  ctx->CRC_state = UINT32_MAX;
767  if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
768  ctx->CRC &= ~0x80000000;
769 
770  if (ctx->data_end - ctx->ptr < 6)
771  return AVERROR_INVALIDDATA;
772  ctx->frameflags = bytestream_get_be32(&ctx->ptr);
773  }
774 
775  /* Initialize the rice structs */
776  ctx->riceX.k = 10;
777  ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
778  ctx->riceY.k = 10;
779  ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
780 
781  if (ctx->fileversion >= 3900) {
782  /* The first 8 bits of input are ignored. */
783  ctx->ptr++;
784 
786  }
787 
788  return 0;
789 }
790 
792  375,
793 };
794 
795 static const int32_t initial_coeffs_a_3800[3] = {
796  64, 115, 64,
797 };
798 
799 static const int32_t initial_coeffs_b_3800[2] = {
800  740, 0
801 };
802 
803 static const int32_t initial_coeffs_3930[4] = {
804  360, 317, -109, 98
805 };
806 
807 static const int64_t initial_coeffs_3930_64bit[4] = {
808  360, 317, -109, 98
809 };
810 
812 {
813  APEPredictor *p = &ctx->predictor;
814  APEPredictor64 *p64 = &ctx->predictor64;
815 
816  /* Zero the history buffers */
817  memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer));
818  memset(p64->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p64->historybuffer));
819  p->buf = p->historybuffer;
820  p64->buf = p64->historybuffer;
821 
822  /* Initialize and zero the coefficients */
823  if (ctx->fileversion < 3930) {
824  if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
825  memcpy(p->coeffsA[0], initial_coeffs_fast_3320,
826  sizeof(initial_coeffs_fast_3320));
827  memcpy(p->coeffsA[1], initial_coeffs_fast_3320,
828  sizeof(initial_coeffs_fast_3320));
829  } else {
830  memcpy(p->coeffsA[0], initial_coeffs_a_3800,
831  sizeof(initial_coeffs_a_3800));
832  memcpy(p->coeffsA[1], initial_coeffs_a_3800,
833  sizeof(initial_coeffs_a_3800));
834  }
835  } else {
836  memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
837  memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
840  }
841  memset(p->coeffsB, 0, sizeof(p->coeffsB));
842  memset(p64->coeffsB, 0, sizeof(p64->coeffsB));
843  if (ctx->fileversion < 3930) {
844  memcpy(p->coeffsB[0], initial_coeffs_b_3800,
845  sizeof(initial_coeffs_b_3800));
846  memcpy(p->coeffsB[1], initial_coeffs_b_3800,
847  sizeof(initial_coeffs_b_3800));
848  }
849 
850  p->filterA[0] = p->filterA[1] = 0;
851  p->filterB[0] = p->filterB[1] = 0;
852  p->lastA[0] = p->lastA[1] = 0;
853 
854  p64->filterA[0] = p64->filterA[1] = 0;
855  p64->filterB[0] = p64->filterB[1] = 0;
856  p64->lastA[0] = p64->lastA[1] = 0;
857 
858  p->sample_pos = 0;
859 
860  p64->sample_pos = 0;
861 }
862 
863 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
864 static inline int APESIGN(int32_t x) {
865  return (x < 0) - (x > 0);
866 }
867 
869  const int decoded, const int filter,
870  const int delayA)
871 {
872  int32_t predictionA;
873 
874  p->buf[delayA] = p->lastA[filter];
875  if (p->sample_pos < 3) {
876  p->lastA[filter] = decoded;
877  p->filterA[filter] = decoded;
878  return decoded;
879  }
880 
881  predictionA = p->buf[delayA] * 2U - p->buf[delayA - 1];
882  p->lastA[filter] = decoded + (unsigned)((int32_t)(predictionA * p->coeffsA[filter][0]) >> 9);
883 
884  if ((decoded ^ predictionA) > 0)
885  p->coeffsA[filter][0]++;
886  else
887  p->coeffsA[filter][0]--;
888 
889  p->filterA[filter] += (unsigned)p->lastA[filter];
890 
891  return p->filterA[filter];
892 }
893 
895  const unsigned decoded, const int filter,
896  const int delayA, const int delayB,
897  const int start, const int shift)
898 {
899  int32_t predictionA, predictionB, sign;
900  int32_t d0, d1, d2, d3, d4;
901 
902  p->buf[delayA] = p->lastA[filter];
903  p->buf[delayB] = p->filterB[filter];
904  if (p->sample_pos < start) {
905  predictionA = decoded + p->filterA[filter];
906  p->lastA[filter] = decoded;
907  p->filterB[filter] = decoded;
908  p->filterA[filter] = predictionA;
909  return predictionA;
910  }
911  d2 = p->buf[delayA];
912  d1 = (p->buf[delayA] - (unsigned)p->buf[delayA - 1]) * 2;
913  d0 = p->buf[delayA] + ((p->buf[delayA - 2] - (unsigned)p->buf[delayA - 1]) * 8);
914  d3 = p->buf[delayB] * 2U - p->buf[delayB - 1];
915  d4 = p->buf[delayB];
916 
917  predictionA = d0 * p->coeffsA[filter][0] +
918  d1 * p->coeffsA[filter][1] +
919  d2 * p->coeffsA[filter][2];
920 
921  sign = APESIGN(decoded);
922  p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
923  p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
924  p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
925 
926  predictionB = d3 * p->coeffsB[filter][0] -
927  d4 * p->coeffsB[filter][1];
928  p->lastA[filter] = decoded + (predictionA >> 11);
929  sign = APESIGN(p->lastA[filter]);
930  p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
931  p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
932 
933  p->filterB[filter] = p->lastA[filter] + (predictionB >> shift);
934  p->filterA[filter] = p->filterB[filter] + (unsigned)((int)(p->filterA[filter] * 31U) >> 5);
935 
936  return p->filterA[filter];
937 }
938 
939 static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
940 {
941  int i, j;
942  int32_t dotprod, sign;
943  int32_t coeffs[256], delay[256];
944 
945  if (order >= length)
946  return;
947 
948  memset(coeffs, 0, order * sizeof(*coeffs));
949  for (i = 0; i < order; i++)
950  delay[i] = buffer[i];
951  for (i = order; i < length; i++) {
952  dotprod = 0;
953  sign = APESIGN(buffer[i]);
954  for (j = 0; j < order; j++) {
955  dotprod += delay[j] * (unsigned)coeffs[j];
956  coeffs[j] += ((delay[j] >> 31) | 1) * sign;
957  }
958  buffer[i] -= (unsigned)(dotprod >> shift);
959  for (j = 0; j < order - 1; j++)
960  delay[j] = delay[j + 1];
961  delay[order - 1] = buffer[i];
962  }
963 }
964 
965 static void long_filter_ehigh_3830(int32_t *buffer, int length)
966 {
967  int i, j;
968  int32_t dotprod, sign;
969  int32_t delay[8] = { 0 };
970  uint32_t coeffs[8] = { 0 };
971 
972  for (i = 0; i < length; i++) {
973  dotprod = 0;
974  sign = APESIGN(buffer[i]);
975  for (j = 7; j >= 0; j--) {
976  dotprod += delay[j] * coeffs[j];
977  coeffs[j] += ((delay[j] >> 31) | 1) * sign;
978  }
979  for (j = 7; j > 0; j--)
980  delay[j] = delay[j - 1];
981  delay[0] = buffer[i];
982  buffer[i] -= (unsigned)(dotprod >> 9);
983  }
984 }
985 
987 {
988  APEPredictor *p = &ctx->predictor;
989  int32_t *decoded0 = ctx->decoded[0];
990  int32_t *decoded1 = ctx->decoded[1];
991  int start = 4, shift = 10;
992 
993  if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
994  start = 16;
995  long_filter_high_3800(decoded0, 16, 9, count);
996  long_filter_high_3800(decoded1, 16, 9, count);
997  } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
998  int order = 128, shift2 = 11;
999 
1000  if (ctx->fileversion >= 3830) {
1001  order <<= 1;
1002  shift++;
1003  shift2++;
1004  long_filter_ehigh_3830(decoded0 + order, count - order);
1005  long_filter_ehigh_3830(decoded1 + order, count - order);
1006  }
1007  start = order;
1008  long_filter_high_3800(decoded0, order, shift2, count);
1009  long_filter_high_3800(decoded1, order, shift2, count);
1010  }
1011 
1012  while (count--) {
1013  int X = *decoded0, Y = *decoded1;
1014  if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
1015  *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA);
1016  decoded0++;
1017  *decoded1 = filter_fast_3320(p, X, 1, XDELAYA);
1018  decoded1++;
1019  } else {
1020  *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
1021  start, shift);
1022  decoded0++;
1023  *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
1024  start, shift);
1025  decoded1++;
1026  }
1027 
1028  /* Combined */
1029  p->buf++;
1030  p->sample_pos++;
1031 
1032  /* Have we filled the history buffer? */
1033  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1034  memmove(p->historybuffer, p->buf,
1035  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1036  p->buf = p->historybuffer;
1037  }
1038  }
1039 }
1040 
1042 {
1043  APEPredictor *p = &ctx->predictor;
1044  int32_t *decoded0 = ctx->decoded[0];
1045  int start = 4, shift = 10;
1046 
1047  if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
1048  start = 16;
1049  long_filter_high_3800(decoded0, 16, 9, count);
1050  } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
1051  int order = 128, shift2 = 11;
1052 
1053  if (ctx->fileversion >= 3830) {
1054  order <<= 1;
1055  shift++;
1056  shift2++;
1057  long_filter_ehigh_3830(decoded0 + order, count - order);
1058  }
1059  start = order;
1060  long_filter_high_3800(decoded0, order, shift2, count);
1061  }
1062 
1063  while (count--) {
1064  if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
1065  *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA);
1066  decoded0++;
1067  } else {
1068  *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB,
1069  start, shift);
1070  decoded0++;
1071  }
1072 
1073  /* Combined */
1074  p->buf++;
1075  p->sample_pos++;
1076 
1077  /* Have we filled the history buffer? */
1078  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1079  memmove(p->historybuffer, p->buf,
1080  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1081  p->buf = p->historybuffer;
1082  }
1083  }
1084 }
1085 
1087  const int decoded, const int filter,
1088  const int delayA)
1089 {
1090  int32_t predictionA, sign;
1091  uint32_t d0, d1, d2, d3;
1092 
1093  p->buf[delayA] = p->lastA[filter];
1094  d0 = p->buf[delayA ];
1095  d1 = p->buf[delayA ] - (unsigned)p->buf[delayA - 1];
1096  d2 = p->buf[delayA - 1] - (unsigned)p->buf[delayA - 2];
1097  d3 = p->buf[delayA - 2] - (unsigned)p->buf[delayA - 3];
1098 
1099  predictionA = d0 * p->coeffsA[filter][0] +
1100  d1 * p->coeffsA[filter][1] +
1101  d2 * p->coeffsA[filter][2] +
1102  d3 * p->coeffsA[filter][3];
1103 
1104  p->lastA[filter] = decoded + (predictionA >> 9);
1105  p->filterA[filter] = p->lastA[filter] + ((int)(p->filterA[filter] * 31U) >> 5);
1106 
1107  sign = APESIGN(decoded);
1108  p->coeffsA[filter][0] += (((int32_t)d0 < 0) * 2 - 1) * sign;
1109  p->coeffsA[filter][1] += (((int32_t)d1 < 0) * 2 - 1) * sign;
1110  p->coeffsA[filter][2] += (((int32_t)d2 < 0) * 2 - 1) * sign;
1111  p->coeffsA[filter][3] += (((int32_t)d3 < 0) * 2 - 1) * sign;
1112 
1113  return p->filterA[filter];
1114 }
1115 
1117 {
1118  APEPredictor *p = &ctx->predictor;
1119  int32_t *decoded0 = ctx->decoded[0];
1120  int32_t *decoded1 = ctx->decoded[1];
1121 
1122  ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1123 
1124  while (count--) {
1125  /* Predictor Y */
1126  int Y = *decoded1, X = *decoded0;
1127  *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
1128  decoded0++;
1129  *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
1130  decoded1++;
1131 
1132  /* Combined */
1133  p->buf++;
1134 
1135  /* Have we filled the history buffer? */
1136  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1137  memmove(p->historybuffer, p->buf,
1138  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1139  p->buf = p->historybuffer;
1140  }
1141  }
1142 }
1143 
1145 {
1146  APEPredictor *p = &ctx->predictor;
1147  int32_t *decoded0 = ctx->decoded[0];
1148 
1149  ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1150 
1151  while (count--) {
1152  *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
1153  decoded0++;
1154 
1155  p->buf++;
1156 
1157  /* Have we filled the history buffer? */
1158  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1159  memmove(p->historybuffer, p->buf,
1160  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1161  p->buf = p->historybuffer;
1162  }
1163  }
1164 }
1165 
1167  const int decoded, const int filter,
1168  const int delayA, const int delayB,
1169  const int adaptA, const int adaptB)
1170 {
1171  int64_t predictionA, predictionB;
1172  int32_t sign;
1173 
1174  p->buf[delayA] = p->lastA[filter];
1175  p->buf[adaptA] = APESIGN(p->buf[delayA]);
1176  p->buf[delayA - 1] = p->buf[delayA] - (uint64_t)p->buf[delayA - 1];
1177  p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
1178 
1179  predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
1180  p->buf[delayA - 1] * p->coeffsA[filter][1] +
1181  p->buf[delayA - 2] * p->coeffsA[filter][2] +
1182  p->buf[delayA - 3] * p->coeffsA[filter][3];
1183 
1184  /* Apply a scaled first-order filter compression */
1185  p->buf[delayB] = p->filterA[filter ^ 1] - ((int64_t)(p->filterB[filter] * 31ULL) >> 5);
1186  p->buf[adaptB] = APESIGN(p->buf[delayB]);
1187  p->buf[delayB - 1] = p->buf[delayB] - (uint64_t)p->buf[delayB - 1];
1188  p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
1189  p->filterB[filter] = p->filterA[filter ^ 1];
1190 
1191  predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
1192  p->buf[delayB - 1] * p->coeffsB[filter][1] +
1193  p->buf[delayB - 2] * p->coeffsB[filter][2] +
1194  p->buf[delayB - 3] * p->coeffsB[filter][3] +
1195  p->buf[delayB - 4] * p->coeffsB[filter][4];
1196 
1197  p->lastA[filter] = decoded + ((int64_t)((uint64_t)predictionA + (predictionB >> 1)) >> 10);
1198  p->filterA[filter] = p->lastA[filter] + ((int64_t)(p->filterA[filter] * 31ULL) >> 5);
1199 
1200  sign = APESIGN(decoded);
1201  p->coeffsA[filter][0] += p->buf[adaptA ] * sign;
1202  p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
1203  p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
1204  p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
1205  p->coeffsB[filter][0] += p->buf[adaptB ] * sign;
1206  p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
1207  p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
1208  p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
1209  p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
1210 
1211  return p->filterA[filter];
1212 }
1213 
1215 {
1216  APEPredictor64 *p = &ctx->predictor64;
1217  int32_t *decoded0 = ctx->decoded[0];
1218  int32_t *decoded1 = ctx->decoded[1];
1219 
1220  ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1221 
1222  while (count--) {
1223  /* Predictor Y */
1224  *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1226  decoded0++;
1227  *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1229  decoded1++;
1230 
1231  /* Combined */
1232  p->buf++;
1233 
1234  /* Have we filled the history buffer? */
1235  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1236  memmove(p->historybuffer, p->buf,
1237  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1238  p->buf = p->historybuffer;
1239  }
1240  }
1241 }
1242 
1244 {
1245  APEPredictor64 *p = &ctx->predictor64;
1246  int32_t *decoded0 = ctx->decoded[0];
1247  int32_t predictionA, currentA, A, sign;
1248 
1249  ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
1250 
1251  currentA = p->lastA[0];
1252 
1253  while (count--) {
1254  A = *decoded0;
1255 
1256  p->buf[YDELAYA] = currentA;
1257  p->buf[YDELAYA - 1] = p->buf[YDELAYA] - (uint64_t)p->buf[YDELAYA - 1];
1258 
1259  predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
1260  p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
1261  p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
1262  p->buf[YDELAYA - 3] * p->coeffsA[0][3];
1263 
1264  currentA = A + (uint64_t)(predictionA >> 10);
1265 
1266  p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
1267  p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
1268 
1269  sign = APESIGN(A);
1270  p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ] * sign;
1271  p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign;
1272  p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign;
1273  p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign;
1274 
1275  p->buf++;
1276 
1277  /* Have we filled the history buffer? */
1278  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1279  memmove(p->historybuffer, p->buf,
1280  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1281  p->buf = p->historybuffer;
1282  }
1283 
1284  p->filterA[0] = currentA + (uint64_t)((int64_t)(p->filterA[0] * 31U) >> 5);
1285  *(decoded0++) = p->filterA[0];
1286  }
1287 
1288  p->lastA[0] = currentA;
1289 }
1290 
1291 static void do_init_filter(APEFilter *f, int16_t *buf, int order)
1292 {
1293  f->coeffs = buf;
1294  f->historybuffer = buf + order;
1295  f->delay = f->historybuffer + order * 2;
1296  f->adaptcoeffs = f->historybuffer + order;
1297 
1298  memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer));
1299  memset(f->coeffs, 0, order * sizeof(*f->coeffs));
1300  f->avg = 0;
1301 }
1302 
1303 static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
1304 {
1305  do_init_filter(&f[0], buf, order);
1306  do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
1307 }
1308 
1310  int32_t *data, int count, int order, int fracbits)
1311 {
1312  int res;
1313  unsigned absres;
1314 
1315  while (count--) {
1316  /* round fixedpoint scalar product */
1317  res = ctx->adsp.scalarproduct_and_madd_int16(f->coeffs,
1318  f->delay - order,
1319  f->adaptcoeffs - order,
1320  order, APESIGN(*data));
1321  res = (int64_t)(res + (1LL << (fracbits - 1))) >> fracbits;
1322  res += (unsigned)*data;
1323  *data++ = res;
1324 
1325  /* Update the output history */
1326  *f->delay++ = av_clip_int16(res);
1327 
1328  if (version < 3980) {
1329  /* Version ??? to < 3.98 files (untested) */
1330  f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1331  f->adaptcoeffs[-4] >>= 1;
1332  f->adaptcoeffs[-8] >>= 1;
1333  } else {
1334  /* Version 3.98 and later files */
1335 
1336  /* Update the adaption coefficients */
1337  absres = FFABSU(res);
1338  if (absres)
1339  *f->adaptcoeffs = APESIGN(res) *
1340  (8 << ((absres > f->avg * 3LL) + (absres > (f->avg + f->avg / 3))));
1341  /* equivalent to the following code
1342  if (absres <= f->avg * 4 / 3)
1343  *f->adaptcoeffs = APESIGN(res) * 8;
1344  else if (absres <= f->avg * 3)
1345  *f->adaptcoeffs = APESIGN(res) * 16;
1346  else
1347  *f->adaptcoeffs = APESIGN(res) * 32;
1348  */
1349  else
1350  *f->adaptcoeffs = 0;
1351 
1352  f->avg += (int)(absres - (unsigned)f->avg) / 16;
1353 
1354  f->adaptcoeffs[-1] >>= 1;
1355  f->adaptcoeffs[-2] >>= 1;
1356  f->adaptcoeffs[-8] >>= 1;
1357  }
1358 
1359  f->adaptcoeffs++;
1360 
1361  /* Have we filled the history buffer? */
1362  if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
1363  memmove(f->historybuffer, f->delay - (order * 2),
1364  (order * 2) * sizeof(*f->historybuffer));
1365  f->delay = f->historybuffer + order * 2;
1366  f->adaptcoeffs = f->historybuffer + order;
1367  }
1368  }
1369 }
1370 
1372  int32_t *data0, int32_t *data1,
1373  int count, int order, int fracbits)
1374 {
1375  do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
1376  if (data1)
1377  do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
1378 }
1379 
1380 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
1381  int32_t *decoded1, int count)
1382 {
1383  int i;
1384 
1385  for (i = 0; i < APE_FILTER_LEVELS; i++) {
1386  if (!ape_filter_orders[ctx->fset][i])
1387  break;
1388  apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
1389  ape_filter_orders[ctx->fset][i],
1390  ape_filter_fracbits[ctx->fset][i]);
1391  }
1392 }
1393 
1395 {
1396  int i, ret;
1397  if ((ret = init_entropy_decoder(ctx)) < 0)
1398  return ret;
1400 
1401  for (i = 0; i < APE_FILTER_LEVELS; i++) {
1402  if (!ape_filter_orders[ctx->fset][i])
1403  break;
1404  init_filter(ctx, ctx->filters[i], ctx->filterbuf[i],
1405  ape_filter_orders[ctx->fset][i]);
1406  }
1407  return 0;
1408 }
1409 
1410 static void ape_unpack_mono(APEContext *ctx, int count)
1411 {
1412  if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
1413  /* We are pure silence, so we're done. */
1414  av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
1415  return;
1416  }
1417 
1418  ctx->entropy_decode_mono(ctx, count);
1419  if (ctx->error)
1420  return;
1421 
1422  /* Now apply the predictor decoding */
1423  ctx->predictor_decode_mono(ctx, count);
1424 
1425  /* Pseudo-stereo - just copy left channel to right channel */
1426  if (ctx->channels == 2) {
1427  memcpy(ctx->decoded[1], ctx->decoded[0], count * sizeof(*ctx->decoded[1]));
1428  }
1429 }
1430 
1431 static void ape_unpack_stereo(APEContext *ctx, int count)
1432 {
1433  unsigned left, right;
1434  int32_t *decoded0 = ctx->decoded[0];
1435  int32_t *decoded1 = ctx->decoded[1];
1436 
1438  /* We are pure silence, so we're done. */
1439  av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
1440  return;
1441  }
1442 
1443  ctx->entropy_decode_stereo(ctx, count);
1444  if (ctx->error)
1445  return;
1446 
1447  /* Now apply the predictor decoding */
1448  ctx->predictor_decode_stereo(ctx, count);
1449 
1450  /* Decorrelate and scale to output depth */
1451  while (count--) {
1452  left = *decoded1 - (unsigned)(*decoded0 / 2);
1453  right = left + *decoded0;
1454 
1455  *(decoded0++) = left;
1456  *(decoded1++) = right;
1457  }
1458 }
1459 
1461  int *got_frame_ptr, AVPacket *avpkt)
1462 {
1463  AVFrame *frame = data;
1464  const uint8_t *buf = avpkt->data;
1466  uint8_t *sample8;
1467  int16_t *sample16;
1468  int32_t *sample24;
1469  int i, ch, ret;
1470  int blockstodecode;
1471  uint64_t decoded_buffer_size;
1472 
1473  /* this should never be negative, but bad things will happen if it is, so
1474  check it just to make sure. */
1475  av_assert0(s->samples >= 0);
1476 
1477  if(!s->samples){
1478  uint32_t nblocks, offset;
1479  int buf_size;
1480 
1481  if (!avpkt->size) {
1482  *got_frame_ptr = 0;
1483  return 0;
1484  }
1485  if (avpkt->size < 8) {
1486  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1487  return AVERROR_INVALIDDATA;
1488  }
1489  buf_size = avpkt->size & ~3;
1490  if (buf_size != avpkt->size) {
1491  av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
1492  "extra bytes at the end will be skipped.\n");
1493  }
1494  if (s->fileversion < 3950) // previous versions overread two bytes
1495  buf_size += 2;
1496  av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
1497  if (!s->data)
1498  return AVERROR(ENOMEM);
1499  s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
1500  buf_size >> 2);
1501  memset(s->data + (buf_size & ~3), 0, buf_size & 3);
1502  s->ptr = s->data;
1503  s->data_end = s->data + buf_size;
1504 
1505  nblocks = bytestream_get_be32(&s->ptr);
1506  offset = bytestream_get_be32(&s->ptr);
1507  if (s->fileversion >= 3900) {
1508  if (offset > 3) {
1509  av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
1510  av_freep(&s->data);
1511  s->data_size = 0;
1512  return AVERROR_INVALIDDATA;
1513  }
1514  if (s->data_end - s->ptr < offset) {
1515  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1516  return AVERROR_INVALIDDATA;
1517  }
1518  s->ptr += offset;
1519  } else {
1520  if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0)
1521  return ret;
1522  if (s->fileversion > 3800)
1523  skip_bits_long(&s->gb, offset * 8);
1524  else
1525  skip_bits_long(&s->gb, offset);
1526  }
1527 
1528  if (!nblocks || nblocks > INT_MAX / 2 / sizeof(*s->decoded_buffer) - 8) {
1529  av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %"PRIu32".\n",
1530  nblocks);
1531  return AVERROR_INVALIDDATA;
1532  }
1533 
1534  /* Initialize the frame decoder */
1535  if (init_frame_decoder(s) < 0) {
1536  av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
1537  return AVERROR_INVALIDDATA;
1538  }
1539  s->samples = nblocks;
1540  }
1541 
1542  if (!s->data) {
1543  *got_frame_ptr = 0;
1544  return avpkt->size;
1545  }
1546 
1547  blockstodecode = FFMIN(s->blocks_per_loop, s->samples);
1548  // for old files coefficients were not interleaved,
1549  // so we need to decode all of them at once
1550  if (s->fileversion < 3930)
1551  blockstodecode = s->samples;
1552 
1553  /* reallocate decoded sample buffer if needed */
1554  decoded_buffer_size = 2LL * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer);
1555  av_assert0(decoded_buffer_size <= INT_MAX);
1556 
1557  /* get output buffer */
1558  frame->nb_samples = blockstodecode;
1559  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1560  s->samples=0;
1561  return ret;
1562  }
1563 
1564  av_fast_malloc(&s->decoded_buffer, &s->decoded_size, decoded_buffer_size);
1565  if (!s->decoded_buffer)
1566  return AVERROR(ENOMEM);
1567  memset(s->decoded_buffer, 0, decoded_buffer_size);
1568  s->decoded[0] = s->decoded_buffer;
1569  s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
1570 
1571  s->error=0;
1572 
1573  if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
1574  ape_unpack_mono(s, blockstodecode);
1575  else
1576  ape_unpack_stereo(s, blockstodecode);
1577  emms_c();
1578 
1579  if (s->error) {
1580  s->samples=0;
1581  av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
1582  return AVERROR_INVALIDDATA;
1583  }
1584 
1585  switch (s->bps) {
1586  case 8:
1587  for (ch = 0; ch < s->channels; ch++) {
1588  sample8 = (uint8_t *)frame->data[ch];
1589  for (i = 0; i < blockstodecode; i++)
1590  *sample8++ = (s->decoded[ch][i] + 0x80U) & 0xff;
1591  }
1592  break;
1593  case 16:
1594  for (ch = 0; ch < s->channels; ch++) {
1595  sample16 = (int16_t *)frame->data[ch];
1596  for (i = 0; i < blockstodecode; i++)
1597  *sample16++ = s->decoded[ch][i];
1598  }
1599  break;
1600  case 24:
1601  for (ch = 0; ch < s->channels; ch++) {
1602  sample24 = (int32_t *)frame->data[ch];
1603  for (i = 0; i < blockstodecode; i++)
1604  *sample24++ = s->decoded[ch][i] * 256U;
1605  }
1606  break;
1607  }
1608 
1609  s->samples -= blockstodecode;
1610 
1612  s->fileversion >= 3900 && s->bps < 24) {
1613  uint32_t crc = s->CRC_state;
1614  const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1615  for (i = 0; i < blockstodecode; i++) {
1616  for (ch = 0; ch < s->channels; ch++) {
1617  uint8_t *smp = frame->data[ch] + (i*(s->bps >> 3));
1618  crc = av_crc(crc_tab, crc, smp, s->bps >> 3);
1619  }
1620  }
1621 
1622  if (!s->samples && (~crc >> 1) ^ s->CRC) {
1623  av_log(avctx, AV_LOG_ERROR, "CRC mismatch! Previously decoded "
1624  "frames may have been affected as well.\n");
1626  return AVERROR_INVALIDDATA;
1627  }
1628 
1629  s->CRC_state = crc;
1630  }
1631 
1632  *got_frame_ptr = 1;
1633 
1634  return !s->samples ? avpkt->size : 0;
1635 }
1636 
1638 {
1640  s->samples= 0;
1641 }
1642 
1643 #define OFFSET(x) offsetof(APEContext, x)
1644 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1645 static const AVOption options[] = {
1646  { "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" },
1647  { "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" },
1648  { NULL},
1649 };
1650 
1651 static const AVClass ape_decoder_class = {
1652  .class_name = "APE decoder",
1653  .item_name = av_default_item_name,
1654  .option = options,
1655  .version = LIBAVUTIL_VERSION_INT,
1656 };
1657 
1659  .name = "ape",
1660  .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
1661  .type = AVMEDIA_TYPE_AUDIO,
1662  .id = AV_CODEC_ID_APE,
1663  .priv_data_size = sizeof(APEContext),
1664  .init = ape_decode_init,
1665  .close = ape_decode_close,
1667  .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DELAY |
1669  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1670  .flush = ape_flush,
1671  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
1675  .priv_class = &ape_decoder_class,
1676 };
APEContext::avctx
AVCodecContext * avctx
Definition: apedec.c:154
APEContext::riceX
APERice riceX
rice code parameters for the second channel
Definition: apedec.c:180
entropy_decode_stereo_3860
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
Definition: apedec.c:687
YADAPTCOEFFSB
#define YADAPTCOEFFSB
Definition: apedec.c:61
AVCodec
AVCodec.
Definition: codec.h:197
bswapdsp.h
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
XDELAYA
#define XDELAYA
Definition: apedec.c:56
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
init_frame_decoder
static int init_frame_decoder(APEContext *ctx)
Definition: apedec.c:1394
APEContext::data
uint8_t * data
current frame data
Definition: apedec.c:185
range_start_decoding
static void range_start_decoding(APEContext *ctx)
Start the decoder.
Definition: apedec.c:333
apply_filter
static void apply_filter(APEContext *ctx, APEFilter *f, int32_t *data0, int32_t *data1, int count, int order, int fracbits)
Definition: apedec.c:1371
PREDICTOR_SIZE
#define PREDICTOR_SIZE
Total size of all predictor histories.
Definition: apedec.c:52
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AV_CODEC_ID_APE
@ AV_CODEC_ID_APE
Definition: codec_id.h:456
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1247
out
FILE * out
Definition: movenc.c:54
APEContext::filterbuf
int16_t * filterbuf[APE_FILTER_LEVELS]
filter memory
Definition: apedec.c:177
AVCRC
uint32_t AVCRC
Definition: crc.h:47
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
APE_FILTER_LEVELS
#define APE_FILTER_LEVELS
Definition: apedec.c:77
APERice
Definition: apedec.c:108
APERangecoder::low
uint32_t low
low end of interval
Definition: apedec.c:114
APEPredictor64::coeffsA
uint64_t coeffsA[2][4]
adaption coefficients
Definition: apedec.c:144
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:90
AVCodecContext::err_recognition
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1645
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:546
X
@ X
Definition: vf_addroi.c:26
init_entropy_decoder
static int init_entropy_decoder(APEContext *ctx)
Definition: apedec.c:753
counts_diff_3980
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
Definition: apedec.c:435
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
ape_decoder_class
static const AVClass ape_decoder_class
Definition: apedec.c:1651
predictor_update_filter
static av_always_inline int predictor_update_filter(APEPredictor64 *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB)
Definition: apedec.c:1166
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:369
entropy_decode_stereo_3930
static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
Definition: apedec.c:723
AVOption
AVOption.
Definition: opt.h:248
predictor_decode_mono_3930
static void predictor_decode_mono_3930(APEContext *ctx, int count)
Definition: apedec.c:1144
APEContext::filters
APEFilter filters[APE_FILTER_LEVELS][2]
filters used for reconstruction
Definition: apedec.c:182
long_filter_ehigh_3830
static void long_filter_ehigh_3830(int32_t *buffer, int length)
Definition: apedec.c:965
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
update_rice
static void update_rice(APERice *rice, unsigned int x)
Definition: apedec.c:471
ff_ape_decoder
AVCodec ff_ape_decoder
Definition: apedec.c:1658
data
const char data[16]
Definition: mxf.c:142
APEContext::CRC
uint32_t CRC
signalled frame CRC
Definition: apedec.c:166
XADAPTCOEFFSA
#define XADAPTCOEFFSA
Definition: apedec.c:60
entropy_decode_mono_3990
static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
Definition: apedec.c:734
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
base
uint8_t base
Definition: vp3data.h:141
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
APEPredictor::coeffsA
uint32_t coeffsA[2][4]
adaption coefficients
Definition: apedec.c:129
get_k
static int get_k(int ksum)
Definition: apedec.c:602
ape_flush
static void ape_flush(AVCodecContext *avctx)
Definition: apedec.c:1637
APEContext::predictor_decode_mono
void(* predictor_decode_mono)(struct APEContext *ctx, int count)
Definition: apedec.c:194
APEPredictor64
Definition: apedec.c:136
ape_filter_fracbits
static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS]
Filter fraction bits depending on compression level.
Definition: apedec.c:89
COMPRESSION_LEVEL_HIGH
@ COMPRESSION_LEVEL_HIGH
Definition: apedec.c:71
APEContext::compression_level
int compression_level
compression levels
Definition: apedec.c:162
APEPredictor
Filter histories.
Definition: apedec.c:121
APEContext::entropy_decode_stereo
void(* entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode)
Definition: apedec.c:193
range_decode_bits
static int range_decode_bits(APEContext *ctx, int n)
Decode n bits (n <= 16) without modelling.
Definition: apedec.c:395
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
YADAPTCOEFFSA
#define YADAPTCOEFFSA
Definition: apedec.c:59
APEPredictor64::buf
int64_t * buf
Definition: apedec.c:137
ff_llauddsp_init
av_cold void ff_llauddsp_init(LLAudDSPContext *c)
Definition: lossless_audiodsp.c:56
A
#define A(x)
Definition: vp56_arith.h:28
crc.h
predictor_decode_stereo_3930
static void predictor_decode_stereo_3930(APEContext *ctx, int count)
Definition: apedec.c:1116
return
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a it should return
Definition: filter_design.txt:264
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
predictor_decode_mono_3800
static void predictor_decode_mono_3800(APEContext *ctx, int count)
Definition: apedec.c:1041
ape_decode_init
static av_cold int ape_decode_init(AVCodecContext *avctx)
Definition: apedec.c:233
U
#define U(x)
Definition: vp56_arith.h:37
ape_unpack_mono
static void ape_unpack_mono(APEContext *ctx, int count)
Definition: apedec.c:1410
APEContext::fileversion
int fileversion
codec version, very important in decoding process
Definition: apedec.c:161
GetBitContext
Definition: get_bits.h:61
ape_decode_value_3860
static int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, APERice *rice)
Definition: apedec.c:494
predictor_decode_stereo_3800
static void predictor_decode_stereo_3800(APEContext *ctx, int count)
Definition: apedec.c:986
APEPredictor::filterA
int32_t filterA[2]
Definition: apedec.c:126
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:91
options
static const AVOption options[]
Definition: apedec.c:1645
YDELAYB
#define YDELAYB
Definition: apedec.c:55
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
APEContext::rc
APERangecoder rc
rangecoder used to decode actual values
Definition: apedec.c:179
APEContext::samples
int samples
samples left to decode in current frame
Definition: apedec.c:158
APEContext::ptr
const uint8_t * ptr
current position in frame data
Definition: apedec.c:188
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:638
APEFilter::historybuffer
int16_t * historybuffer
filter memory
Definition: apedec.c:102
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
MODEL_ELEMENTS
#define MODEL_ELEMENTS
Definition: apedec.c:403
do_init_filter
static void do_init_filter(APEFilter *f, int16_t *buf, int order)
Definition: apedec.c:1291
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1747
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
ctx
AVFormatContext * ctx
Definition: movenc.c:48
long_filter_high_3800
static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
Definition: apedec.c:939
APEContext::decoded_buffer
int32_t * decoded_buffer
Definition: apedec.c:172
APE_FRAMECODE_STEREO_SILENCE
#define APE_FRAMECODE_STEREO_SILENCE
Definition: apedec.c:46
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
APERangecoder::buffer
unsigned int buffer
buffer for input/output
Definition: apedec.c:117
APERangecoder
Definition: apedec.c:113
do_apply_filter
static void do_apply_filter(APEContext *ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
Definition: apedec.c:1309
LLAudDSPContext
Definition: lossless_audiodsp.h:28
f
#define f(width, name)
Definition: cbs_vp9.c:255
entropy_decode_stereo_0000
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
Definition: apedec.c:671
APEContext::fset
int fset
which filter set to use (calculated from compression level)
Definition: apedec.c:163
int32_t
int32_t
Definition: audio_convert.c:194
COMPRESSION_LEVEL_FAST
@ COMPRESSION_LEVEL_FAST
Definition: apedec.c:69
if
if(ret)
Definition: filter_design.txt:179
OFFSET
#define OFFSET(x)
Definition: apedec.c:1643
ape_decode_value_3900
static int ape_decode_value_3900(APEContext *ctx, APERice *rice)
Definition: apedec.c:527
APEPredictor::historybuffer
int32_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
Definition: apedec.c:131
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
APEContext::CRC_state
uint32_t CRC_state
accumulated CRC
Definition: apedec.c:167
APEContext::frameflags
int frameflags
frame flags
Definition: apedec.c:168
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
ff_bswapdsp_init
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
Definition: bswapdsp.c:49
av_clip_int16
#define av_clip_int16
Definition: common.h:137
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:592
NULL
#define NULL
Definition: coverity.c:32
APEPredictor::sample_pos
unsigned int sample_pos
Definition: apedec.c:133
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
FFABSU
#define FFABSU(a)
Unsigned Absolute value.
Definition: common.h:89
range_decode_culshift
static int range_decode_culshift(APEContext *ctx, int shift)
Decode value with given size in bits.
Definition: apedec.c:374
initial_coeffs_3930_64bit
static const int64_t initial_coeffs_3930_64bit[4]
Definition: apedec.c:807
entropy_decode_stereo_3900
static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
Definition: apedec.c:707
entropy_decode_mono_3900
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
Definition: apedec.c:699
counts_3970
static const uint16_t counts_3970[22]
Fixed probabilities for symbols in Monkey Audio version 3.97.
Definition: apedec.c:408
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
predictor_update_3930
static av_always_inline int predictor_update_3930(APEPredictor *p, const int decoded, const int filter, const int delayA)
Definition: apedec.c:1086
init_predictor_decoder
static void init_predictor_decoder(APEContext *ctx)
Definition: apedec.c:811
COMPRESSION_LEVEL_EXTRA_HIGH
@ COMPRESSION_LEVEL_EXTRA_HIGH
Definition: apedec.c:72
APEContext
Decoder context.
Definition: apedec.c:152
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1656
range_decode_culfreq
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate cumulative frequency for next symbol.
Definition: apedec.c:362
APEPredictor64::historybuffer
int64_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
Definition: apedec.c:146
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1900
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:370
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:66
APE_FRAMECODE_PSEUDO_STEREO
#define APE_FRAMECODE_PSEUDO_STEREO
Definition: apedec.c:47
APEContext::entropy_decode_mono
void(* entropy_decode_mono)(struct APEContext *ctx, int blockstodecode)
Definition: apedec.c:192
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
APERice::ksum
uint32_t ksum
Definition: apedec.c:110
APEFilter::coeffs
int16_t * coeffs
actual coefficients used in filtering
Definition: apedec.c:100
PAR
#define PAR
Definition: apedec.c:1644
APEFilter::delay
int16_t * delay
filtered values
Definition: apedec.c:103
APERangecoder::range
uint32_t range
length of interval
Definition: apedec.c:115
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
initial_coeffs_a_3800
static const int32_t initial_coeffs_a_3800[3]
Definition: apedec.c:795
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
APEContext::predictor_decode_stereo
void(* predictor_decode_stereo)(struct APEContext *ctx, int count)
Definition: apedec.c:195
init_filter
static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
Definition: apedec.c:1303
APEContext::error
int error
Definition: apedec.c:190
APEPredictor64::coeffsB
uint64_t coeffsB[2][5]
adaption coefficients
Definition: apedec.c:145
APEContext::predictor64
APEPredictor64 predictor64
64bit predictor used for final reconstruction
Definition: apedec.c:170
ape_decode_value_3990
static int ape_decode_value_3990(APEContext *ctx, APERice *rice)
Definition: apedec.c:561
version
version
Definition: libkvazaar.c:326
unary.h
shift2
static const int shift2[6]
Definition: dxa.c:51
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
Y
#define Y
Definition: boxblur.h:38
decode_array_0000
static void decode_array_0000(APEContext *ctx, GetBitContext *gb, int32_t *out, APERice *rice, int blockstodecode)
Definition: apedec.c:607
range_get_symbol
static int range_get_symbol(APEContext *ctx, const uint16_t counts[], const uint16_t counts_diff[])
Decode symbol.
Definition: apedec.c:447
APEPredictor64::sample_pos
unsigned int sample_pos
Definition: apedec.c:148
APEPredictor::coeffsB
uint32_t coeffsB[2][5]
adaption coefficients
Definition: apedec.c:130
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1197
ape_unpack_stereo
static void ape_unpack_stereo(APEContext *ctx, int count)
Definition: apedec.c:1431
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1740
i
int i
Definition: input.c:407
APEPredictor::buf
int32_t * buf
Definition: apedec.c:122
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
range_decode_update
static void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
Update decoding state.
Definition: apedec.c:388
filter_3800
static av_always_inline int filter_3800(APEPredictor *p, const unsigned decoded, const int filter, const int delayA, const int delayB, const int start, const int shift)
Definition: apedec.c:894
APEContext::gb
GetBitContext gb
Definition: apedec.c:183
BOTTOM_VALUE
#define BOTTOM_VALUE
Definition: apedec.c:330
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
MIN_CACHE_BITS
#define MIN_CACHE_BITS
Definition: get_bits.h:128
range_dec_normalize
static void range_dec_normalize(APEContext *ctx)
Perform normalization.
Definition: apedec.c:341
initial_coeffs_fast_3320
static const int32_t initial_coeffs_fast_3320[1]
Definition: apedec.c:791
av_fast_padded_malloc
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:50
COMPRESSION_LEVEL_INSANE
@ COMPRESSION_LEVEL_INSANE
Definition: apedec.c:73
av_always_inline
#define av_always_inline
Definition: attributes.h:49
uint8_t
uint8_t
Definition: audio_convert.c:194
entropy_decode_stereo_3990
static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
Definition: apedec.c:742
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
APERangecoder::help
uint32_t help
bytes_to_follow resp. intermediate value
Definition: apedec.c:116
APECompressionLevel
APECompressionLevel
Possible compression levels.
Definition: apedec.c:68
YDELAYA
#define YDELAYA
Definition: apedec.c:54
entropy_decode_mono_3860
static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
Definition: apedec.c:679
ape_decode_close
static av_cold int ape_decode_close(AVCodecContext *avctx)
Definition: apedec.c:218
avcodec.h
APEContext::bdsp
BswapDSPContext bdsp
Definition: apedec.c:155
predictor_decode_stereo_3950
static void predictor_decode_stereo_3950(APEContext *ctx, int count)
Definition: apedec.c:1214
ret
ret
Definition: filter_design.txt:187
APEContext::adsp
LLAudDSPContext adsp
Definition: apedec.c:156
predictor_decode_mono_3950
static void predictor_decode_mono_3950(APEContext *ctx, int count)
Definition: apedec.c:1243
XDELAYB
#define XDELAYB
Definition: apedec.c:57
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
get_rice_ook
static int get_rice_ook(GetBitContext *gb, int k)
Definition: apedec.c:482
APEContext::data_size
int data_size
frame data allocated size
Definition: apedec.c:187
AV_EF_CRCCHECK
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition: avcodec.h:1653
APEContext::predictor
APEPredictor predictor
predictor used for final reconstruction
Definition: apedec.c:169
lossless_audiodsp.h
APEFilter
Filters applied to the decoded data.
Definition: apedec.c:99
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
APEPredictor::lastA
int32_t lastA[2]
Definition: apedec.c:124
AVCodecContext
main external API structure.
Definition: avcodec.h:536
counts_3980
static const uint16_t counts_3980[22]
Fixed probabilities for symbols in Monkey Audio version 3.98.
Definition: apedec.c:426
channel_layout.h
HISTORY_SIZE
#define HISTORY_SIZE
Definition: apedec.c:49
COMPRESSION_LEVEL_NORMAL
@ COMPRESSION_LEVEL_NORMAL
Definition: apedec.c:70
counts_diff_3970
static const uint16_t counts_diff_3970[21]
Probability ranges for symbols in Monkey Audio version 3.97.
Definition: apedec.c:417
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
initial_coeffs_b_3800
static const int32_t initial_coeffs_b_3800[2]
Definition: apedec.c:799
APEPredictor64::filterA
int64_t filterA[2]
Definition: apedec.c:141
APEContext::channels
int channels
Definition: apedec.c:157
APEContext::decoded
int32_t * decoded[MAX_CHANNELS]
decoded data for each channel
Definition: apedec.c:174
APEPredictor64::filterB
int64_t filterB[2]
Definition: apedec.c:142
APEPredictor64::lastA
int64_t lastA[2]
Definition: apedec.c:139
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
APESIGN
static int APESIGN(int32_t x)
Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero)
Definition: apedec.c:864
APEContext::riceY
APERice riceY
rice code parameters for the first channel
Definition: apedec.c:181
APEContext::data_end
uint8_t * data_end
frame data end
Definition: apedec.c:186
XADAPTCOEFFSB
#define XADAPTCOEFFSB
Definition: apedec.c:62
shift
static int shift(int a, int b)
Definition: sonic.c:82
AV_CRC_32_IEEE_LE
@ AV_CRC_32_IEEE_LE
Definition: crc.h:54
ape_filter_orders
static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS]
Filter orders depending on compression level.
Definition: apedec.c:80
initial_coeffs_3930
static const int32_t initial_coeffs_3930[4]
Definition: apedec.c:803
MAX_CHANNELS
#define MAX_CHANNELS
Definition: apedec.c:42
AV_CODEC_CAP_SUBFRAMES
#define AV_CODEC_CAP_SUBFRAMES
Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time,...
Definition: codec.h:95
overflow
Undefined Behavior In the C some operations are like signed integer overflow
Definition: undefined.txt:3
ape_decode_frame
static int ape_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: apedec.c:1460
APEContext::bps
int bps
Definition: apedec.c:159
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:39
APEPredictor::filterB
int32_t filterB[2]
Definition: apedec.c:127
APEContext::blocks_per_loop
int blocks_per_loop
maximum number of samples to decode for each call
Definition: apedec.c:175
filter_fast_3320
static av_always_inline int filter_fast_3320(APEPredictor *p, const int decoded, const int filter, const int delayA)
Definition: apedec.c:868
APEContext::flags
int flags
global decoder flags
Definition: apedec.c:164
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:48
AVPacket
This structure stores compressed data.
Definition: packet.h:346
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:563
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_fast_malloc
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:502
bytestream.h
APEFilter::adaptcoeffs
int16_t * adaptcoeffs
adaptive filter coefficients used for correcting of actual filter coefficients
Definition: apedec.c:101
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
APEContext::decoded_size
int decoded_size
Definition: apedec.c:173
BswapDSPContext
Definition: bswapdsp.h:24
ape_apply_filters
static void ape_apply_filters(APEContext *ctx, int32_t *decoded0, int32_t *decoded1, int count)
Definition: apedec.c:1380
APEFilter::avg
uint32_t avg
Definition: apedec.c:105
int
int
Definition: ffmpeg_filter.c:170
entropy_decode_mono_0000
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
Definition: apedec.c:665
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
EXTRA_BITS
#define EXTRA_BITS
Definition: apedec.c:329
APERice::k
uint32_t k
Definition: apedec.c:109