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