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