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