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 "codec_internal.h"
34 #include "decode.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  */
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 {
124 
126 
129 
130  uint32_t coeffsA[2][4]; ///< adaption coefficients
131  uint32_t coeffsB[2][5]; ///< adaption coefficients
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
148 
149  unsigned int sample_pos;
151 
152 /** Decoder context */
153 typedef struct APEContext {
154  AVClass *class; ///< class for AVOptions
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 
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
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 
220 {
222  int i;
223 
224  for (i = 0; i < APE_FILTER_LEVELS; i++)
225  av_freep(&s->filterbuf[i]);
226 
227  av_freep(&s->decoded_buffer);
228  av_freep(&s->data);
229  s->decoded_size = s->data_size = 0;
230 
231  return 0;
232 }
233 
235 {
238  int i;
239 
240  if (avctx->extradata_size != 6) {
241  av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
242  return AVERROR(EINVAL);
243  }
244  if (channels > 2) {
245  av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
246  return AVERROR(EINVAL);
247  }
249  s->bps = avctx->bits_per_coded_sample;
250  switch (s->bps) {
251  case 8:
253  break;
254  case 16:
256  break;
257  case 24:
259  break;
260  default:
262  "%d bits per coded sample", s->bps);
263  return AVERROR_PATCHWELCOME;
264  }
265  s->avctx = avctx;
266  s->channels = channels;
267  s->fileversion = AV_RL16(avctx->extradata);
268  s->compression_level = AV_RL16(avctx->extradata + 2);
269  s->flags = AV_RL16(avctx->extradata + 4);
270 
271  av_log(avctx, AV_LOG_VERBOSE, "Compression Level: %d - Flags: %d\n",
272  s->compression_level, s->flags);
273  if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
274  !s->compression_level ||
275  (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  s->fset = s->compression_level / 1000 - 1;
281  for (i = 0; i < APE_FILTER_LEVELS; i++) {
282  if (!ape_filter_orders[s->fset][i])
283  break;
284  if (!(s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4)))
285  return AVERROR(ENOMEM);
286  }
287 
288  if (s->fileversion < 3860) {
289  s->entropy_decode_mono = entropy_decode_mono_0000;
290  s->entropy_decode_stereo = entropy_decode_stereo_0000;
291  } else if (s->fileversion < 3900) {
292  s->entropy_decode_mono = entropy_decode_mono_3860;
293  s->entropy_decode_stereo = entropy_decode_stereo_3860;
294  } else if (s->fileversion < 3930) {
295  s->entropy_decode_mono = entropy_decode_mono_3900;
296  s->entropy_decode_stereo = entropy_decode_stereo_3900;
297  } else if (s->fileversion < 3990) {
298  s->entropy_decode_mono = entropy_decode_mono_3900;
299  s->entropy_decode_stereo = entropy_decode_stereo_3930;
300  } else {
301  s->entropy_decode_mono = entropy_decode_mono_3990;
302  s->entropy_decode_stereo = entropy_decode_stereo_3990;
303  }
304 
305  if (s->fileversion < 3930) {
306  s->predictor_decode_mono = predictor_decode_mono_3800;
307  s->predictor_decode_stereo = predictor_decode_stereo_3800;
308  } else if (s->fileversion < 3950) {
309  s->predictor_decode_mono = predictor_decode_mono_3930;
310  s->predictor_decode_stereo = predictor_decode_stereo_3930;
311  } else {
312  s->predictor_decode_mono = predictor_decode_mono_3950;
313  s->predictor_decode_stereo = predictor_decode_stereo_3950;
314  }
315 
316  ff_bswapdsp_init(&s->bdsp);
317  ff_llauddsp_init(&s->adsp);
321 
322  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 static inline void range_start_decoding(APEContext *ctx)
338 {
339  ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
340  ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
341  ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
342 }
343 
344 /** Perform normalization */
345 static inline void range_dec_normalize(APEContext *ctx)
346 {
347  while (ctx->rc.range <= BOTTOM_VALUE) {
348  ctx->rc.buffer <<= 8;
349  if(ctx->ptr < ctx->data_end) {
350  ctx->rc.buffer += *ctx->ptr;
351  ctx->ptr++;
352  } else {
353  ctx->error = 1;
354  }
355  ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
356  ctx->rc.range <<= 8;
357  }
358 }
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 static inline int range_decode_culfreq(APEContext *ctx, int tot_f)
367 {
369  ctx->rc.help = ctx->rc.range / tot_f;
370  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 static inline int range_decode_culshift(APEContext *ctx, int shift)
379 {
381  ctx->rc.help = ctx->rc.range >> shift;
382  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 static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
393 {
394  ctx->rc.low -= ctx->rc.help * lt_f;
395  ctx->rc.range = ctx->rc.help * sy_f;
396 }
397 
398 /** Decode n bits (n <= 16) without modelling */
399 static inline int range_decode_bits(APEContext *ctx, int n)
400 {
401  int sym = range_decode_culshift(ctx, n);
402  range_decode_update(ctx, 1, sym);
403  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 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  cf = range_decode_culshift(ctx, 16);
458 
459  if(cf > 65492){
460  symbol= cf - 65535 + 63;
461  range_decode_update(ctx, 1, cf);
462  if(cf > 65535)
463  ctx->error=1;
464  return symbol;
465  }
466  /* figure out the symbol inefficiently; a binary search would be much better */
467  for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
468 
469  range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
470 
471  return symbol;
472 }
473 /** @} */ // group rangecoder
474 
475 static inline void update_rice(APERice *rice, unsigned int x)
476 {
477  int lim = rice->k ? (1 << (rice->k + 4)) : 0;
478  rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
479 
480  if (rice->ksum < lim)
481  rice->k--;
482  else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
483  rice->k++;
484 }
485 
486 static inline int get_rice_ook(GetBitContext *gb, int k)
487 {
488  unsigned int x;
489 
490  x = get_unary(gb, 1, get_bits_left(gb));
491 
492  if (k)
493  x = (x << k) | get_bits(gb, k);
494 
495  return x;
496 }
497 
499  APERice *rice)
500 {
501  unsigned int x, overflow;
502 
504 
505  if (ctx->fileversion > 3880) {
506  while (overflow >= 16) {
507  overflow -= 16;
508  rice->k += 4;
509  }
510  }
511 
512  if (!rice->k)
513  x = overflow;
514  else if(rice->k <= MIN_CACHE_BITS) {
515  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  rice->ksum += x - (rice->ksum + 8 >> 4);
522  if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
523  rice->k--;
524  else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
525  rice->k++;
526 
527  /* Convert to signed */
528  return ((x >> 1) ^ ((x & 1) - 1)) + 1;
529 }
530 
531 static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
532 {
533  unsigned int x, overflow;
534  int tmpk;
535 
537 
538  if (overflow == (MODEL_ELEMENTS - 1)) {
539  tmpk = range_decode_bits(ctx, 5);
540  overflow = 0;
541  } else
542  tmpk = (rice->k < 1) ? 0 : rice->k - 1;
543 
544  if (tmpk <= 16 || ctx->fileversion < 3910) {
545  if (tmpk > 23) {
546  av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
547  return AVERROR_INVALIDDATA;
548  }
549  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  x += overflow << tmpk;
558 
559  update_rice(rice, x);
560 
561  /* Convert to signed */
562  return ((x >> 1) ^ ((x & 1) - 1)) + 1;
563 }
564 
565 static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
566 {
567  unsigned int x, overflow, pivot;
568  int base;
569 
570  pivot = FFMAX(rice->ksum >> 5, 1);
571 
573 
574  if (overflow == (MODEL_ELEMENTS - 1)) {
575  overflow = (unsigned)range_decode_bits(ctx, 16) << 16;
577  }
578 
579  if (pivot < 0x10000) {
580  base = range_decode_culfreq(ctx, pivot);
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  x = base + overflow * pivot;
599 
600  update_rice(rice, x);
601 
602  /* Convert to signed */
603  return ((x >> 1) ^ ((x & 1) - 1)) + 1;
604 }
605 
606 static int get_k(int ksum)
607 {
608  return av_log2(ksum) + !!ksum;
609 }
610 
612  int32_t *out, APERice *rice, int blockstodecode)
613 {
614  int i;
615  unsigned ksummax, ksummin;
616 
617  rice->ksum = 0;
618  for (i = 0; i < FFMIN(blockstodecode, 5); i++) {
619  out[i] = get_rice_ook(&ctx->gb, 10);
620  rice->ksum += out[i];
621  }
622 
623  if (blockstodecode <= 5)
624  goto end;
625 
626  rice->k = get_k(rice->ksum / 10);
627  if (rice->k >= 24)
628  return;
629  for (; i < FFMIN(blockstodecode, 64); i++) {
630  out[i] = get_rice_ook(&ctx->gb, rice->k);
631  rice->ksum += out[i];
632  rice->k = get_k(rice->ksum / ((i + 1) * 2));
633  if (rice->k >= 24)
634  return;
635  }
636 
637  if (blockstodecode <= 64)
638  goto end;
639 
640  rice->k = get_k(rice->ksum >> 7);
641  ksummax = 1 << rice->k + 7;
642  ksummin = rice->k ? (1 << rice->k + 6) : 0;
643  for (; i < blockstodecode; i++) {
644  if (get_bits_left(&ctx->gb) < 1) {
645  ctx->error = 1;
646  return;
647  }
648  out[i] = get_rice_ook(&ctx->gb, rice->k);
649  rice->ksum += out[i] - (unsigned)out[i - 64];
650  while (rice->ksum < ksummin) {
651  rice->k--;
652  ksummin = rice->k ? ksummin >> 1 : 0;
653  ksummax >>= 1;
654  }
655  while (rice->ksum >= ksummax) {
656  rice->k++;
657  if (rice->k > 24)
658  return;
659  ksummax <<= 1;
660  ksummin = ksummin ? ksummin << 1 : 128;
661  }
662  }
663 
664 end:
665  for (i = 0; i < blockstodecode; i++)
666  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 static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
676 {
677  decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
678  blockstodecode);
679  decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
680  blockstodecode);
681 }
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 static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
692 {
693  int32_t *decoded0 = ctx->decoded[0];
694  int32_t *decoded1 = ctx->decoded[1];
695  int blocks = blockstodecode;
696 
697  while (blockstodecode--)
698  *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
699  while (blocks--)
700  *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
701 }
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 static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
712 {
713  int32_t *decoded0 = ctx->decoded[0];
714  int32_t *decoded1 = ctx->decoded[1];
715  int blocks = blockstodecode;
716 
717  while (blockstodecode--)
718  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
720  // because of some implementation peculiarities we need to backpedal here
721  ctx->ptr -= 1;
723  while (blocks--)
724  *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
725 }
726 
727 static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
728 {
729  int32_t *decoded0 = ctx->decoded[0];
730  int32_t *decoded1 = ctx->decoded[1];
731 
732  while (blockstodecode--) {
733  *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
734  *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
735  }
736 }
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 static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
747 {
748  int32_t *decoded0 = ctx->decoded[0];
749  int32_t *decoded1 = ctx->decoded[1];
750 
751  while (blockstodecode--) {
752  *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
753  *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
754  }
755 }
756 
758 {
759  /* Read the CRC */
760  if (ctx->fileversion >= 3900) {
761  if (ctx->data_end - ctx->ptr < 6)
762  return AVERROR_INVALIDDATA;
763  ctx->CRC = bytestream_get_be32(&ctx->ptr);
764  } else {
765  ctx->CRC = get_bits_long(&ctx->gb, 32);
766  }
767 
768  /* Read the frame flags if they exist */
769  ctx->frameflags = 0;
770  ctx->CRC_state = UINT32_MAX;
771  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  ctx->riceX.k = 10;
781  ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
782  ctx->riceY.k = 10;
783  ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
784 
785  if (ctx->fileversion >= 3900) {
786  /* The first 8 bits of input are ignored. */
787  ctx->ptr++;
788 
790  }
791 
792  return 0;
793 }
794 
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 
816 {
817  APEPredictor *p = &ctx->predictor;
818  APEPredictor64 *p64 = &ctx->predictor64;
819 
820  /* Zero the history buffers */
821  memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer));
822  memset(p64->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p64->historybuffer));
823  p->buf = p->historybuffer;
824  p64->buf = p64->historybuffer;
825 
826  /* Initialize and zero the coefficients */
827  if (ctx->fileversion < 3930) {
828  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  memcpy(p->coeffsA[0], initial_coeffs_a_3800,
835  sizeof(initial_coeffs_a_3800));
836  memcpy(p->coeffsA[1], initial_coeffs_a_3800,
837  sizeof(initial_coeffs_a_3800));
838  }
839  } else {
840  memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
841  memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
844  }
845  memset(p->coeffsB, 0, sizeof(p->coeffsB));
846  memset(p64->coeffsB, 0, sizeof(p64->coeffsB));
847  if (ctx->fileversion < 3930) {
848  memcpy(p->coeffsB[0], initial_coeffs_b_3800,
849  sizeof(initial_coeffs_b_3800));
850  memcpy(p->coeffsB[1], initial_coeffs_b_3800,
851  sizeof(initial_coeffs_b_3800));
852  }
853 
854  p->filterA[0] = p->filterA[1] = 0;
855  p->filterB[0] = p->filterB[1] = 0;
856  p->lastA[0] = p->lastA[1] = 0;
857 
858  p64->filterA[0] = p64->filterA[1] = 0;
859  p64->filterB[0] = p64->filterB[1] = 0;
860  p64->lastA[0] = p64->lastA[1] = 0;
861 
862  p->sample_pos = 0;
863 
864  p64->sample_pos = 0;
865 }
866 
867 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
868 static inline int APESIGN(int32_t x) {
869  return (x < 0) - (x > 0);
870 }
871 
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 
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  p->buf[delayA] = p->lastA[filter];
907  p->buf[delayB] = p->filterB[filter];
908  if (p->sample_pos < start) {
909  predictionA = decoded + p->filterA[filter];
910  p->lastA[filter] = decoded;
911  p->filterB[filter] = decoded;
912  p->filterA[filter] = predictionA;
913  return predictionA;
914  }
915  d2 = p->buf[delayA];
916  d1 = (p->buf[delayA] - (unsigned)p->buf[delayA - 1]) * 2;
917  d0 = p->buf[delayA] + ((p->buf[delayA - 2] - (unsigned)p->buf[delayA - 1]) * 8);
918  d3 = p->buf[delayB] * 2U - p->buf[delayB - 1];
919  d4 = p->buf[delayB];
920 
921  predictionA = d0 * p->coeffsA[filter][0] +
922  d1 * p->coeffsA[filter][1] +
923  d2 * p->coeffsA[filter][2];
924 
925  sign = APESIGN(decoded);
926  p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
927  p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
928  p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
929 
930  predictionB = d3 * p->coeffsB[filter][0] -
931  d4 * p->coeffsB[filter][1];
932  p->lastA[filter] = decoded + (predictionA >> 11);
933  sign = APESIGN(p->lastA[filter]);
934  p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
935  p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
936 
937  p->filterB[filter] = p->lastA[filter] + (unsigned)(predictionB >> shift);
938  p->filterA[filter] = p->filterB[filter] + (unsigned)((int)(p->filterA[filter] * 31U) >> 5);
939 
940  return p->filterA[filter];
941 }
942 
943 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  if (order >= length)
950  return;
951 
952  memset(coeffs, 0, order * sizeof(*coeffs));
953  for (i = 0; i < order; i++)
954  delay[i] = buffer[i];
955  for (i = order; i < length; i++) {
956  dotprod = 0;
957  sign = APESIGN(buffer[i]);
958  for (j = 0; j < order; j++) {
959  dotprod += delay[j] * (unsigned)coeffs[j];
960  coeffs[j] += ((delay[j] >> 31) | 1) * sign;
961  }
962  buffer[i] -= (unsigned)(dotprod >> shift);
963  for (j = 0; j < order - 1; j++)
964  delay[j] = delay[j + 1];
965  delay[order - 1] = buffer[i];
966  }
967 }
968 
969 static void long_filter_ehigh_3830(int32_t *buffer, int length)
970 {
971  int i, j;
972  int32_t dotprod, sign;
973  int32_t delay[8] = { 0 };
974  uint32_t coeffs[8] = { 0 };
975 
976  for (i = 0; i < length; i++) {
977  dotprod = 0;
978  sign = APESIGN(buffer[i]);
979  for (j = 7; j >= 0; j--) {
980  dotprod += delay[j] * coeffs[j];
981  coeffs[j] += ((delay[j] >> 31) | 1) * sign;
982  }
983  for (j = 7; j > 0; j--)
984  delay[j] = delay[j - 1];
985  delay[0] = buffer[i];
986  buffer[i] -= (unsigned)(dotprod >> 9);
987  }
988 }
989 
991 {
992  APEPredictor *p = &ctx->predictor;
993  int32_t *decoded0 = ctx->decoded[0];
994  int32_t *decoded1 = ctx->decoded[1];
995  int start = 4, shift = 10;
996 
997  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  } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
1002  int order = 128, shift2 = 11;
1003 
1004  if (ctx->fileversion >= 3830) {
1005  order <<= 1;
1006  shift++;
1007  shift2++;
1008  long_filter_ehigh_3830(decoded0 + order, count - order);
1009  long_filter_ehigh_3830(decoded1 + order, count - order);
1010  }
1011  start = order;
1012  long_filter_high_3800(decoded0, order, shift2, count);
1013  long_filter_high_3800(decoded1, order, shift2, count);
1014  }
1015 
1016  while (count--) {
1017  int X = *decoded0, Y = *decoded1;
1018  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  *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
1025  start, shift);
1026  decoded0++;
1027  *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
1028  start, shift);
1029  decoded1++;
1030  }
1031 
1032  /* Combined */
1033  p->buf++;
1034  p->sample_pos++;
1035 
1036  /* Have we filled the history buffer? */
1037  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1038  memmove(p->historybuffer, p->buf,
1039  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1040  p->buf = p->historybuffer;
1041  }
1042  }
1043 }
1044 
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 
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  p->buf[delayA] = p->lastA[filter];
1098  d0 = p->buf[delayA ];
1099  d1 = p->buf[delayA ] - (unsigned)p->buf[delayA - 1];
1100  d2 = p->buf[delayA - 1] - (unsigned)p->buf[delayA - 2];
1101  d3 = p->buf[delayA - 2] - (unsigned)p->buf[delayA - 3];
1102 
1103  predictionA = d0 * p->coeffsA[filter][0] +
1104  d1 * p->coeffsA[filter][1] +
1105  d2 * p->coeffsA[filter][2] +
1106  d3 * p->coeffsA[filter][3];
1107 
1108  p->lastA[filter] = decoded + (predictionA >> 9);
1109  p->filterA[filter] = p->lastA[filter] + ((int)(p->filterA[filter] * 31U) >> 5);
1110 
1111  sign = APESIGN(decoded);
1112  p->coeffsA[filter][0] += (((int32_t)d0 < 0) * 2 - 1) * sign;
1113  p->coeffsA[filter][1] += (((int32_t)d1 < 0) * 2 - 1) * sign;
1114  p->coeffsA[filter][2] += (((int32_t)d2 < 0) * 2 - 1) * sign;
1115  p->coeffsA[filter][3] += (((int32_t)d3 < 0) * 2 - 1) * sign;
1116 
1117  return p->filterA[filter];
1118 }
1119 
1121 {
1122  APEPredictor *p = &ctx->predictor;
1123  int32_t *decoded0 = ctx->decoded[0];
1124  int32_t *decoded1 = ctx->decoded[1];
1125 
1126  ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1127 
1128  while (count--) {
1129  /* Predictor Y */
1130  int Y = *decoded1, X = *decoded0;
1131  *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
1132  decoded0++;
1133  *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
1134  decoded1++;
1135 
1136  /* Combined */
1137  p->buf++;
1138 
1139  /* Have we filled the history buffer? */
1140  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1141  memmove(p->historybuffer, p->buf,
1142  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1143  p->buf = p->historybuffer;
1144  }
1145  }
1146 }
1147 
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 
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  p->buf[delayA] = p->lastA[filter];
1179  p->buf[adaptA] = APESIGN(p->buf[delayA]);
1180  p->buf[delayA - 1] = p->buf[delayA] - (uint64_t)p->buf[delayA - 1];
1181  p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
1182 
1183  predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
1184  p->buf[delayA - 1] * p->coeffsA[filter][1] +
1185  p->buf[delayA - 2] * p->coeffsA[filter][2] +
1186  p->buf[delayA - 3] * p->coeffsA[filter][3];
1187 
1188  /* Apply a scaled first-order filter compression */
1189  p->buf[delayB] = p->filterA[filter ^ 1] - ((int64_t)(p->filterB[filter] * 31ULL) >> 5);
1190  p->buf[adaptB] = APESIGN(p->buf[delayB]);
1191  p->buf[delayB - 1] = p->buf[delayB] - (uint64_t)p->buf[delayB - 1];
1192  p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
1193  p->filterB[filter] = p->filterA[filter ^ 1];
1194 
1195  predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
1196  p->buf[delayB - 1] * p->coeffsB[filter][1] +
1197  p->buf[delayB - 2] * p->coeffsB[filter][2] +
1198  p->buf[delayB - 3] * p->coeffsB[filter][3] +
1199  p->buf[delayB - 4] * p->coeffsB[filter][4];
1200 
1201  p->lastA[filter] = decoded + ((int64_t)((uint64_t)predictionA + (predictionB >> 1)) >> 10);
1202  p->filterA[filter] = p->lastA[filter] + ((int64_t)(p->filterA[filter] * 31ULL) >> 5);
1203 
1204  sign = APESIGN(decoded);
1205  p->coeffsA[filter][0] += p->buf[adaptA ] * sign;
1206  p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign;
1207  p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign;
1208  p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign;
1209  p->coeffsB[filter][0] += p->buf[adaptB ] * sign;
1210  p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign;
1211  p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign;
1212  p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign;
1213  p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign;
1214 
1215  return p->filterA[filter];
1216 }
1217 
1219 {
1220  APEPredictor64 *p = &ctx->predictor64;
1221  int32_t *decoded0 = ctx->decoded[0];
1222  int32_t *decoded1 = ctx->decoded[1];
1223 
1224  ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1225 
1226  while (count--) {
1227  /* Predictor Y */
1228  *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1230  decoded0++;
1231  *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1233  decoded1++;
1234 
1235  /* Combined */
1236  p->buf++;
1237 
1238  /* Have we filled the history buffer? */
1239  if (p->buf == p->historybuffer + HISTORY_SIZE) {
1240  memmove(p->historybuffer, p->buf,
1241  PREDICTOR_SIZE * sizeof(*p->historybuffer));
1242  p->buf = p->historybuffer;
1243  }
1244  }
1245 }
1246 
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 static void do_init_filter(APEFilter *f, int16_t *buf, int order)
1296 {
1297  f->coeffs = buf;
1298  f->historybuffer = buf + order;
1299  f->delay = f->historybuffer + order * 2;
1300  f->adaptcoeffs = f->historybuffer + order;
1301 
1302  memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer));
1303  memset(f->coeffs, 0, order * sizeof(*f->coeffs));
1304  f->avg = 0;
1305 }
1306 
1307 static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
1308 {
1309  do_init_filter(&f[0], buf, order);
1310  do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
1311 }
1312 
1314  int32_t *data, int count, int order, int fracbits)
1315 {
1316  int res;
1317  unsigned absres;
1318 
1319  while (count--) {
1320  /* round fixedpoint scalar product */
1321  res = ctx->adsp.scalarproduct_and_madd_int16(f->coeffs,
1322  f->delay - order,
1323  f->adaptcoeffs - order,
1324  order, APESIGN(*data));
1325  res = (int64_t)(res + (1LL << (fracbits - 1))) >> fracbits;
1326  res += (unsigned)*data;
1327  *data++ = res;
1328 
1329  /* Update the output history */
1330  *f->delay++ = av_clip_int16(res);
1331 
1332  if (version < 3980) {
1333  /* Version ??? to < 3.98 files (untested) */
1334  f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1335  f->adaptcoeffs[-4] >>= 1;
1336  f->adaptcoeffs[-8] >>= 1;
1337  } else {
1338  /* Version 3.98 and later files */
1339 
1340  /* Update the adaption coefficients */
1341  absres = FFABSU(res);
1342  if (absres)
1343  *f->adaptcoeffs = APESIGN(res) *
1344  (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  *f->adaptcoeffs = 0;
1355 
1356  f->avg += (int)(absres - (unsigned)f->avg) / 16;
1357 
1358  f->adaptcoeffs[-1] >>= 1;
1359  f->adaptcoeffs[-2] >>= 1;
1360  f->adaptcoeffs[-8] >>= 1;
1361  }
1362 
1363  f->adaptcoeffs++;
1364 
1365  /* Have we filled the history buffer? */
1366  if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
1367  memmove(f->historybuffer, f->delay - (order * 2),
1368  (order * 2) * sizeof(*f->historybuffer));
1369  f->delay = f->historybuffer + order * 2;
1370  f->adaptcoeffs = f->historybuffer + order;
1371  }
1372  }
1373 }
1374 
1376  int32_t *data0, int32_t *data1,
1377  int count, int order, int fracbits)
1378 {
1379  do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
1380  if (data1)
1381  do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
1382 }
1383 
1384 static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
1385  int32_t *decoded1, int count)
1386 {
1387  int i;
1388 
1389  for (i = 0; i < APE_FILTER_LEVELS; i++) {
1390  if (!ape_filter_orders[ctx->fset][i])
1391  break;
1392  apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count,
1393  ape_filter_orders[ctx->fset][i],
1394  ape_filter_fracbits[ctx->fset][i]);
1395  }
1396 }
1397 
1399 {
1400  int i, ret;
1401  if ((ret = init_entropy_decoder(ctx)) < 0)
1402  return ret;
1404 
1405  for (i = 0; i < APE_FILTER_LEVELS; i++) {
1406  if (!ape_filter_orders[ctx->fset][i])
1407  break;
1408  init_filter(ctx, ctx->filters[i], ctx->filterbuf[i],
1409  ape_filter_orders[ctx->fset][i]);
1410  }
1411  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 static void ape_unpack_stereo(APEContext *ctx, int count)
1436 {
1437  unsigned left, right;
1438  int32_t *decoded0 = ctx->decoded[0];
1439  int32_t *decoded1 = ctx->decoded[1];
1440 
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  ctx->entropy_decode_stereo(ctx, count);
1448  if (ctx->error)
1449  return;
1450 
1451  /* Now apply the predictor decoding */
1452  ctx->predictor_decode_stereo(ctx, count);
1453 
1454  /* Decorrelate and scale to output depth */
1455  while (count--) {
1456  left = *decoded1 - (unsigned)(*decoded0 / 2);
1457  right = left + *decoded0;
1458 
1459  *(decoded0++) = left;
1460  *(decoded1++) = right;
1461  }
1462 }
1463 
1465  int *got_frame_ptr, AVPacket *avpkt)
1466 {
1467  const uint8_t *buf = avpkt->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  av_assert0(s->samples >= 0);
1479 
1480  if(!s->samples){
1481  uint32_t nblocks, offset;
1482  int buf_size;
1483 
1484  if (!avpkt->size) {
1485  *got_frame_ptr = 0;
1486  return 0;
1487  }
1488  if (avpkt->size < 8) {
1489  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
1490  return AVERROR_INVALIDDATA;
1491  }
1492  buf_size = avpkt->size & ~3;
1493  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  if (s->fileversion < 3950) // previous versions overread two bytes
1498  buf_size += 2;
1499  av_fast_padded_malloc(&s->data, &s->data_size, buf_size);
1500  if (!s->data)
1501  return AVERROR(ENOMEM);
1502  s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf,
1503  buf_size >> 2);
1504  memset(s->data + (buf_size & ~3), 0, buf_size & 3);
1505  s->ptr = s->data;
1506  s->data_end = s->data + buf_size;
1507 
1508  nblocks = bytestream_get_be32(&s->ptr);
1509  offset = bytestream_get_be32(&s->ptr);
1510  if (s->fileversion >= 3900) {
1511  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  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  s->ptr += offset;
1522  } else {
1523  if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0)
1524  return ret;
1525  if (s->fileversion > 3800)
1526  skip_bits_long(&s->gb, offset * 8);
1527  else
1528  skip_bits_long(&s->gb, offset);
1529  }
1530 
1531  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  if (init_frame_decoder(s) < 0) {
1539  av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n");
1540  return AVERROR_INVALIDDATA;
1541  }
1542  s->samples = nblocks;
1543  }
1544 
1545  if (!s->data) {
1546  *got_frame_ptr = 0;
1547  return avpkt->size;
1548  }
1549 
1550  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  if (s->fileversion < 3930)
1554  blockstodecode = s->samples;
1555 
1556  /* reallocate decoded sample buffer if needed */
1557  decoded_buffer_size = 2LL * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer);
1558  av_assert0(decoded_buffer_size <= INT_MAX);
1559 
1560  /* get output buffer */
1561  frame->nb_samples = blockstodecode;
1562  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1563  s->samples=0;
1564  return ret;
1565  }
1566 
1567  av_fast_malloc(&s->decoded_buffer, &s->decoded_size, decoded_buffer_size);
1568  if (!s->decoded_buffer)
1569  return AVERROR(ENOMEM);
1570  memset(s->decoded_buffer, 0, decoded_buffer_size);
1571  s->decoded[0] = s->decoded_buffer;
1572  s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
1573 
1574  s->error=0;
1575 
1576  if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
1577  ape_unpack_mono(s, blockstodecode);
1578  else
1579  ape_unpack_stereo(s, blockstodecode);
1580 
1581  if (s->error) {
1582  s->samples=0;
1583  av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
1584  return AVERROR_INVALIDDATA;
1585  }
1586 
1587  switch (s->bps) {
1588  case 8:
1589  for (ch = 0; ch < s->channels; ch++) {
1590  sample8 = (uint8_t *)frame->data[ch];
1591  for (i = 0; i < blockstodecode; i++)
1592  *sample8++ = (s->decoded[ch][i] + 0x80U) & 0xff;
1593  }
1594  break;
1595  case 16:
1596  for (ch = 0; ch < s->channels; ch++) {
1597  sample16 = (int16_t *)frame->data[ch];
1598  for (i = 0; i < blockstodecode; i++)
1599  *sample16++ = s->decoded[ch][i];
1600  }
1601  break;
1602  case 24:
1603  for (ch = 0; ch < s->channels; ch++) {
1604  sample24 = (int32_t *)frame->data[ch];
1605  for (i = 0; i < blockstodecode; i++)
1606  *sample24++ = s->decoded[ch][i] * 256U;
1607  }
1608  break;
1609  }
1610 
1611  s->samples -= blockstodecode;
1612 
1614  s->fileversion >= 3900 && s->bps < 24) {
1615  uint32_t crc = s->CRC_state;
1616  const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE);
1617  for (i = 0; i < blockstodecode; i++) {
1618  for (ch = 0; ch < s->channels; ch++) {
1619  uint8_t *smp = frame->data[ch] + (i*(s->bps >> 3));
1620  crc = av_crc(crc_tab, crc, smp, s->bps >> 3);
1621  }
1622  }
1623 
1624  if (!s->samples && (~crc >> 1) ^ s->CRC) {
1625  av_log(avctx, AV_LOG_ERROR, "CRC mismatch! Previously decoded "
1626  "frames may have been affected as well.\n");
1628  return AVERROR_INVALIDDATA;
1629  }
1630 
1631  s->CRC_state = crc;
1632  }
1633 
1634  *got_frame_ptr = 1;
1635 
1636  return !s->samples ? avpkt->size : 0;
1637 }
1638 
1640 {
1642  s->samples= 0;
1643 }
1644 
1645 #define OFFSET(x) offsetof(APEContext, x)
1646 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1647 static const AVOption options[] = {
1648  { "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" },
1649  { "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" },
1650  { NULL},
1651 };
1652 
1653 static const AVClass ape_decoder_class = {
1654  .class_name = "APE decoder",
1655  .item_name = av_default_item_name,
1656  .option = options,
1657  .version = LIBAVUTIL_VERSION_INT,
1658 };
1659 
1661  .p.name = "ape",
1662  CODEC_LONG_NAME("Monkey's Audio"),
1663  .p.type = AVMEDIA_TYPE_AUDIO,
1664  .p.id = AV_CODEC_ID_APE,
1665  .priv_data_size = sizeof(APEContext),
1666  .init = ape_decode_init,
1667  .close = ape_decode_close,
1669  .p.capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DELAY |
1671  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1672  .flush = ape_flush,
1673  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
1677  .p.priv_class = &ape_decoder_class,
1678 };
APEContext::avctx
AVCodecContext * avctx
Definition: apedec.c:155
APEContext::riceX
APERice riceX
rice code parameters for the second channel
Definition: apedec.c:181
entropy_decode_stereo_3860
static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
Definition: apedec.c:691
A
#define A(x)
Definition: vpx_arith.h:28
YADAPTCOEFFSB
#define YADAPTCOEFFSB
Definition: apedec.c:62
bswapdsp.h
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:268
XDELAYA
#define XDELAYA
Definition: apedec.c:57
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
init_frame_decoder
static int init_frame_decoder(APEContext *ctx)
Definition: apedec.c:1398
APEContext::data
uint8_t * data
current frame data
Definition: apedec.c:186
range_start_decoding
static void range_start_decoding(APEContext *ctx)
Start the decoder.
Definition: apedec.c:337
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:1375
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
PREDICTOR_SIZE
#define PREDICTOR_SIZE
Total size of all predictor histories.
Definition: apedec.c:53
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: codec_internal.h:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:664
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:470
out
FILE * out
Definition: movenc.c:54
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:369
APEContext::filterbuf
int16_t * filterbuf[APE_FILTER_LEVELS]
filter memory
Definition: apedec.c:178
AVCRC
uint32_t AVCRC
Definition: crc.h:46
APE_FILTER_LEVELS
#define APE_FILTER_LEVELS
Definition: apedec.c:78
APERice
Definition: apedec.c:109
APERangecoder::low
uint32_t low
low end of interval
Definition: apedec.c:115
APEPredictor64::coeffsA
uint64_t coeffsA[2][4]
adaption coefficients
Definition: apedec.c:145
AVCodecContext::err_recognition
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1382
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:411
init_entropy_decoder
static int init_entropy_decoder(APEContext *ctx)
Definition: apedec.c:757
counts_diff_3980
static const uint16_t counts_diff_3980[21]
Probability ranges for symbols in Monkey Audio version 3.98.
Definition: apedec.c:439
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
ape_decoder_class
static const AVClass ape_decoder_class
Definition: apedec.c:1653
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:1170
AVPacket::data
uint8_t * data
Definition: packet.h:374
entropy_decode_stereo_3930
static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
Definition: apedec.c:727
AVOption
AVOption.
Definition: opt.h:251
predictor_decode_mono_3930
static void predictor_decode_mono_3930(APEContext *ctx, int count)
Definition: apedec.c:1148
APEContext::filters
APEFilter filters[APE_FILTER_LEVELS][2]
filters used for reconstruction
Definition: apedec.c:183
long_filter_ehigh_3830
static void long_filter_ehigh_3830(int32_t *buffer, int length)
Definition: apedec.c:969
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
update_rice
static void update_rice(APERice *rice, unsigned int x)
Definition: apedec.c:475
data
const char data[16]
Definition: mxf.c:146
APEContext::CRC
uint32_t CRC
signalled frame CRC
Definition: apedec.c:167
XADAPTCOEFFSA
#define XADAPTCOEFFSA
Definition: apedec.c:61
entropy_decode_mono_3990
static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
Definition: apedec.c:738
FFCodec
Definition: codec_internal.h:127
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
base
uint8_t base
Definition: vp3data.h:128
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
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
APEPredictor::coeffsA
uint32_t coeffsA[2][4]
adaption coefficients
Definition: apedec.c:130
get_k
static int get_k(int ksum)
Definition: apedec.c:606
ape_flush
static void ape_flush(AVCodecContext *avctx)
Definition: apedec.c:1639
APEContext::predictor_decode_mono
void(* predictor_decode_mono)(struct APEContext *ctx, int count)
Definition: apedec.c:195
APEPredictor64
Definition: apedec.c:137
ape_filter_fracbits
static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS]
Filter fraction bits depending on compression level.
Definition: apedec.c:90
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
COMPRESSION_LEVEL_HIGH
@ COMPRESSION_LEVEL_HIGH
Definition: apedec.c:72
APEContext::compression_level
int compression_level
compression levels
Definition: apedec.c:163
APEPredictor
Filter histories.
Definition: apedec.c:122
APEContext::entropy_decode_stereo
void(* entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode)
Definition: apedec.c:194
range_decode_bits
static int range_decode_bits(APEContext *ctx, int n)
Decode n bits (n <= 16) without modelling.
Definition: apedec.c:399
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
YADAPTCOEFFSA
#define YADAPTCOEFFSA
Definition: apedec.c:60
APEPredictor64::buf
int64_t * buf
Definition: apedec.c:138
ff_llauddsp_init
av_cold void ff_llauddsp_init(LLAudDSPContext *c)
Definition: lossless_audiodsp.c:57
crc.h
predictor_decode_stereo_3930
static void predictor_decode_stereo_3930(APEContext *ctx, int count)
Definition: apedec.c:1120
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:325
predictor_decode_mono_3800
static void predictor_decode_mono_3800(APEContext *ctx, int count)
Definition: apedec.c:1045
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ape_decode_init
static av_cold int ape_decode_init(AVCodecContext *avctx)
Definition: apedec.c:234
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2054
ape_unpack_mono
static void ape_unpack_mono(APEContext *ctx, int count)
Definition: apedec.c:1414
APEContext::fileversion
int fileversion
codec version, very important in decoding process
Definition: apedec.c:162
GetBitContext
Definition: get_bits.h:107
ape_decode_value_3860
static int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, APERice *rice)
Definition: apedec.c:498
predictor_decode_stereo_3800
static void predictor_decode_stereo_3800(APEContext *ctx, int count)
Definition: apedec.c:990
APEPredictor::filterA
int32_t filterA[2]
Definition: apedec.c:127
options
static const AVOption options[]
Definition: apedec.c:1647
YDELAYB
#define YDELAYB
Definition: apedec.c:56
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
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:524
APEContext::rc
APERangecoder rc
rangecoder used to decode actual values
Definition: apedec.c:180
APEContext::samples
int samples
samples left to decode in current frame
Definition: apedec.c:159
APEContext::ptr
const uint8_t * ptr
current position in frame data
Definition: apedec.c:189
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:528
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
APEFilter::historybuffer
int16_t * historybuffer
filter memory
Definition: apedec.c:103
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
MODEL_ELEMENTS
#define MODEL_ELEMENTS
Definition: apedec.c:407
do_init_filter
static void do_init_filter(APEFilter *f, int16_t *buf, int order)
Definition: apedec.c:1295
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:1487
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
decode.h
long_filter_high_3800
static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length)
Definition: apedec.c:943
APEContext::decoded_buffer
int32_t * decoded_buffer
Definition: apedec.c:173
APE_FRAMECODE_STEREO_SILENCE
#define APE_FRAMECODE_STEREO_SILENCE
Definition: apedec.c:47
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:118
APERangecoder
Definition: apedec.c:114
ff_ape_decoder
const FFCodec ff_ape_decoder
Definition: apedec.c:1660
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:1313
LLAudDSPContext
Definition: lossless_audiodsp.h:28
ape_decode_frame
static int ape_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: apedec.c:1464
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
entropy_decode_stereo_0000
static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
Definition: apedec.c:675
APEContext::fset
int fset
which filter set to use (calculated from compression level)
Definition: apedec.c:164
COMPRESSION_LEVEL_FAST
@ COMPRESSION_LEVEL_FAST
Definition: apedec.c:70
if
if(ret)
Definition: filter_design.txt:179
OFFSET
#define OFFSET(x)
Definition: apedec.c:1645
ape_decode_value_3900
static int ape_decode_value_3900(APEContext *ctx, APERice *rice)
Definition: apedec.c:531
APEPredictor::historybuffer
int32_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
Definition: apedec.c:132
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
APEContext::CRC_state
uint32_t CRC_state
accumulated CRC
Definition: apedec.c:168
APEContext::frameflags
int frameflags
frame flags
Definition: apedec.c:169
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:110
NULL
#define NULL
Definition: coverity.c:32
APEPredictor::sample_pos
unsigned int sample_pos
Definition: apedec.c:134
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
FFABSU
#define FFABSU(a)
Unsigned Absolute value.
Definition: common.h:81
range_decode_culshift
static int range_decode_culshift(APEContext *ctx, int shift)
Decode value with given size in bits.
Definition: apedec.c:378
initial_coeffs_3930_64bit
static const int64_t initial_coeffs_3930_64bit[4]
Definition: apedec.c:811
entropy_decode_stereo_3900
static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
Definition: apedec.c:711
entropy_decode_mono_3900
static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
Definition: apedec.c:703
counts_3970
static const uint16_t counts_3970[22]
Fixed probabilities for symbols in Monkey Audio version 3.97.
Definition: apedec.c:412
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
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:1090
init_predictor_decoder
static void init_predictor_decoder(APEContext *ctx)
Definition: apedec.c:815
COMPRESSION_LEVEL_EXTRA_HIGH
@ COMPRESSION_LEVEL_EXTRA_HIGH
Definition: apedec.c:73
APEContext
Decoder context.
Definition: apedec.c:153
AV_EF_CRCCHECK
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition: defs.h:48
range_decode_culfreq
static int range_decode_culfreq(APEContext *ctx, int tot_f)
Calculate cumulative frequency for next symbol.
Definition: apedec.c:366
APEPredictor64::historybuffer
int64_t historybuffer[HISTORY_SIZE+PREDICTOR_SIZE]
Definition: apedec.c:147
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
f
f
Definition: af_crystalizer.c:122
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1473
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:375
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:301
codec_internal.h
APE_FRAMECODE_PSEUDO_STEREO
#define APE_FRAMECODE_PSEUDO_STEREO
Definition: apedec.c:48
shift
static int shift(int a, int b)
Definition: bonk.c:257
APEContext::entropy_decode_mono
void(* entropy_decode_mono)(struct APEContext *ctx, int blockstodecode)
Definition: apedec.c:193
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1050
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
APERice::ksum
uint32_t ksum
Definition: apedec.c:111
APEFilter::coeffs
int16_t * coeffs
actual coefficients used in filtering
Definition: apedec.c:101
PAR
#define PAR
Definition: apedec.c:1646
APEFilter::delay
int16_t * delay
filtered values
Definition: apedec.c:104
APERangecoder::range
uint32_t range
length of interval
Definition: apedec.c:116
initial_coeffs_a_3800
static const int32_t initial_coeffs_a_3800[3]
Definition: apedec.c:799
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:196
init_filter
static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order)
Definition: apedec.c:1307
APEContext::error
int error
Definition: apedec.c:191
APEPredictor64::coeffsB
uint64_t coeffsB[2][5]
adaption coefficients
Definition: apedec.c:146
APEContext::predictor64
APEPredictor64 predictor64
64bit predictor used for final reconstruction
Definition: apedec.c:171
ape_decode_value_3990
static int ape_decode_value_3990(APEContext *ctx, APERice *rice)
Definition: apedec.c:565
version
version
Definition: libkvazaar.c:313
unary.h
shift2
static const int shift2[6]
Definition: dxa.c:49
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
Y
#define Y
Definition: boxblur.h:37
decode_array_0000
static void decode_array_0000(APEContext *ctx, GetBitContext *gb, int32_t *out, APERice *rice, int blockstodecode)
Definition: apedec.c:611
range_get_symbol
static int range_get_symbol(APEContext *ctx, const uint16_t counts[], const uint16_t counts_diff[])
Decode symbol.
Definition: apedec.c:451
APEPredictor64::sample_pos
unsigned int sample_pos
Definition: apedec.c:149
APEPredictor::coeffsB
uint32_t coeffsB[2][5]
adaption coefficients
Definition: apedec.c:131
ape_unpack_stereo
static void ape_unpack_stereo(APEContext *ctx, int count)
Definition: apedec.c:1435
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1480
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
APEPredictor::buf
int32_t * buf
Definition: apedec.c:123
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:527
range_decode_update
static void range_decode_update(APEContext *ctx, int sy_f, int lt_f)
Update decoding state.
Definition: apedec.c:392
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:898
APEContext::gb
GetBitContext gb
Definition: apedec.c:184
BOTTOM_VALUE
#define BOTTOM_VALUE
Definition: apedec.c:334
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
MIN_CACHE_BITS
#define MIN_CACHE_BITS
Definition: get_bits.h:168
range_dec_normalize
static void range_dec_normalize(APEContext *ctx)
Perform normalization.
Definition: apedec.c:345
initial_coeffs_fast_3320
static const int32_t initial_coeffs_fast_3320[1]
Definition: apedec.c:795
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:49
COMPRESSION_LEVEL_INSANE
@ COMPRESSION_LEVEL_INSANE
Definition: apedec.c:74
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
entropy_decode_stereo_3990
static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
Definition: apedec.c:746
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
APERangecoder::help
uint32_t help
bytes_to_follow resp. intermediate value
Definition: apedec.c:117
APECompressionLevel
APECompressionLevel
Possible compression levels.
Definition: apedec.c:69
YDELAYA
#define YDELAYA
Definition: apedec.c:55
entropy_decode_mono_3860
static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
Definition: apedec.c:683
ape_decode_close
static av_cold int ape_decode_close(AVCodecContext *avctx)
Definition: apedec.c:219
avcodec.h
APEContext::bdsp
BswapDSPContext bdsp
Definition: apedec.c:156
predictor_decode_stereo_3950
static void predictor_decode_stereo_3950(APEContext *ctx, int count)
Definition: apedec.c:1218
ret
ret
Definition: filter_design.txt:187
APEContext::adsp
LLAudDSPContext adsp
Definition: apedec.c:157
predictor_decode_mono_3950
static void predictor_decode_mono_3950(APEContext *ctx, int count)
Definition: apedec.c:1247
XDELAYB
#define XDELAYB
Definition: apedec.c:58
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:71
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:486
APEContext::data_size
int data_size
frame data allocated size
Definition: apedec.c:188
APEContext::predictor
APEPredictor predictor
predictor used for final reconstruction
Definition: apedec.c:170
lossless_audiodsp.h
APEFilter
Filters applied to the decoded data.
Definition: apedec.c:100
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
U
#define U(x)
Definition: vpx_arith.h:37
APEPredictor::lastA
int32_t lastA[2]
Definition: apedec.c:125
AVCodecContext
main external API structure.
Definition: avcodec.h:426
counts_3980
static const uint16_t counts_3980[22]
Fixed probabilities for symbols in Monkey Audio version 3.98.
Definition: apedec.c:430
channel_layout.h
HISTORY_SIZE
#define HISTORY_SIZE
Definition: apedec.c:50
COMPRESSION_LEVEL_NORMAL
@ COMPRESSION_LEVEL_NORMAL
Definition: apedec.c:71
counts_diff_3970
static const uint16_t counts_diff_3970[21]
Probability ranges for symbols in Monkey Audio version 3.97.
Definition: apedec.c:421
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
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:632
initial_coeffs_b_3800
static const int32_t initial_coeffs_b_3800[2]
Definition: apedec.c:803
APEPredictor64::filterA
int64_t filterA[2]
Definition: apedec.c:142
APEContext::channels
int channels
Definition: apedec.c:158
APEContext::decoded
int32_t * decoded[MAX_CHANNELS]
decoded data for each channel
Definition: apedec.c:175
APEPredictor64::filterB
int64_t filterB[2]
Definition: apedec.c:143
APEPredictor64::lastA
int64_t lastA[2]
Definition: apedec.c:140
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:76
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:868
APEContext::riceY
APERice riceY
rice code parameters for the first channel
Definition: apedec.c:182
APEContext::data_end
uint8_t * data_end
frame data end
Definition: apedec.c:187
XADAPTCOEFFSB
#define XADAPTCOEFFSB
Definition: apedec.c:63
AV_CRC_32_IEEE_LE
@ AV_CRC_32_IEEE_LE
Definition: crc.h:53
ape_filter_orders
static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS]
Filter orders depending on compression level.
Definition: apedec.c:81
initial_coeffs_3930
static const int32_t initial_coeffs_3930[4]
Definition: apedec.c:807
MAX_CHANNELS
#define MAX_CHANNELS
Definition: apedec.c:43
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:94
overflow
Undefined Behavior In the C some operations are like signed integer overflow
Definition: undefined.txt:3
APEContext::bps
int bps
Definition: apedec.c:160
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
APEPredictor::filterB
int32_t filterB[2]
Definition: apedec.c:128
APEContext::blocks_per_loop
int blocks_per_loop
maximum number of samples to decode for each call
Definition: apedec.c:176
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:368
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:872
APEContext::flags
int flags
global decoder flags
Definition: apedec.c:165
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
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:555
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
X
@ X
Definition: vf_addroi.c:26
APEFilter::adaptcoeffs
int16_t * adaptcoeffs
adaptive filter coefficients used for correcting of actual filter coefficients
Definition: apedec.c:102
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
APEContext::decoded_size
int decoded_size
Definition: apedec.c:174
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:1384
APEFilter::avg
uint32_t avg
Definition: apedec.c:106
int
int
Definition: ffmpeg_filter.c:156
entropy_decode_mono_0000
static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
Definition: apedec.c:669
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:333
APERice::k
uint32_t k
Definition: apedec.c:110