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