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