annotate apedec.c @ 10554:deabe9f7571b libavcodec

Add support for two pass encoding in libtheora
author conrad
date Sun, 22 Nov 2009 21:08:40 +0000
parents e5a8a6b9a717
children 79f3ead3ebc1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
1 /*
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
2 * Monkey's Audio lossless audio decoder
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
4 * based upon libdemac from Dave Chapman.
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
5 *
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
6 * This file is part of FFmpeg.
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
7 *
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
12 *
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
16 * Lesser General Public License for more details.
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
17 *
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
21 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
22
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
23 #define ALT_BITSTREAM_READER_LE
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
24 #include "avcodec.h"
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
25 #include "dsputil.h"
9428
0dce4fe6e6f3 Rename bitstream.h to get_bits.h.
stefano
parents: 9355
diff changeset
26 #include "get_bits.h"
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
27 #include "bytestream.h"
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
28
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
29 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8683
diff changeset
30 * @file libavcodec/apedec.c
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
31 * Monkey's Audio lossless audio decoder
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
32 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
33
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
34 #define BLOCKS_PER_LOOP 4608
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
35 #define MAX_CHANNELS 2
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
36 #define MAX_BYTESPERSAMPLE 3
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
37
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
38 #define APE_FRAMECODE_MONO_SILENCE 1
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
39 #define APE_FRAMECODE_STEREO_SILENCE 3
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
40 #define APE_FRAMECODE_PSEUDO_STEREO 4
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
41
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
42 #define HISTORY_SIZE 512
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
43 #define PREDICTOR_ORDER 8
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
44 /** Total size of all predictor histories */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
45 #define PREDICTOR_SIZE 50
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
46
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
47 #define YDELAYA (18 + PREDICTOR_ORDER*4)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
48 #define YDELAYB (18 + PREDICTOR_ORDER*3)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
49 #define XDELAYA (18 + PREDICTOR_ORDER*2)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
50 #define XDELAYB (18 + PREDICTOR_ORDER)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
51
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
52 #define YADAPTCOEFFSA 18
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
53 #define XADAPTCOEFFSA 14
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
54 #define YADAPTCOEFFSB 10
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
55 #define XADAPTCOEFFSB 5
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
56
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
57 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
58 * Possible compression levels
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
59 * @{
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
60 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
61 enum APECompressionLevel {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
62 COMPRESSION_LEVEL_FAST = 1000,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
63 COMPRESSION_LEVEL_NORMAL = 2000,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
64 COMPRESSION_LEVEL_HIGH = 3000,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
65 COMPRESSION_LEVEL_EXTRA_HIGH = 4000,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
66 COMPRESSION_LEVEL_INSANE = 5000
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
67 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
68 /** @} */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
69
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
70 #define APE_FILTER_LEVELS 3
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
71
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
72 /** Filter orders depending on compression level */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
73 static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
74 { 0, 0, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
75 { 16, 0, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
76 { 64, 0, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
77 { 32, 256, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
78 { 16, 256, 1280 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
79 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
80
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
81 /** Filter fraction bits depending on compression level */
6482
258f569d30ca smaller tables
michael
parents: 6444
diff changeset
82 static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = {
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
83 { 0, 0, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
84 { 11, 0, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
85 { 11, 0, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
86 { 10, 13, 0 },
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
87 { 11, 13, 15 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
88 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
89
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
90
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
91 /** Filters applied to the decoded data */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
92 typedef struct APEFilter {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
93 int16_t *coeffs; ///< actual coefficients used in filtering
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
94 int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
95 int16_t *historybuffer; ///< filter memory
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
96 int16_t *delay; ///< filtered values
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
97
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
98 int avg;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
99 } APEFilter;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
100
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
101 typedef struct APERice {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
102 uint32_t k;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
103 uint32_t ksum;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
104 } APERice;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
105
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
106 typedef struct APERangecoder {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
107 uint32_t low; ///< low end of interval
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
108 uint32_t range; ///< length of interval
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
109 uint32_t help; ///< bytes_to_follow resp. intermediate value
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
110 unsigned int buffer; ///< buffer for input/output
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
111 } APERangecoder;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
112
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
113 /** Filter histories */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
114 typedef struct APEPredictor {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
115 int32_t *buf;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
116
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
117 int32_t lastA[2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
118
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
119 int32_t filterA[2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
120 int32_t filterB[2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
121
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
122 int32_t coeffsA[2][4]; ///< adaption coefficients
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
123 int32_t coeffsB[2][5]; ///< adaption coefficients
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
124 int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
125 } APEPredictor;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
126
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
127 /** Decoder context */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
128 typedef struct APEContext {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
129 AVCodecContext *avctx;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
130 DSPContext dsp;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
131 int channels;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
132 int samples; ///< samples left to decode in current frame
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
133
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
134 int fileversion; ///< codec version, very important in decoding process
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
135 int compression_level; ///< compression levels
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
136 int fset; ///< which filter set to use (calculated from compression level)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
137 int flags; ///< global decoder flags
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
138
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
139 uint32_t CRC; ///< frame CRC
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
140 int frameflags; ///< frame flags
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
141 int currentframeblocks; ///< samples (per channel) in current frame
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
142 int blocksdecoded; ///< count of decoded samples in current frame
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
143 APEPredictor predictor; ///< predictor used for final reconstruction
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
144
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
145 int32_t decoded0[BLOCKS_PER_LOOP]; ///< decoded data for the first channel
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
146 int32_t decoded1[BLOCKS_PER_LOOP]; ///< decoded data for the second channel
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
147
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
148 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
149
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
150 APERangecoder rc; ///< rangecoder used to decode actual values
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
151 APERice riceX; ///< rice code parameters for the second channel
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
152 APERice riceY; ///< rice code parameters for the first channel
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
153 APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
154
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
155 uint8_t *data; ///< current frame data
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
156 uint8_t *data_end; ///< frame data end
6223
michael
parents: 6202
diff changeset
157 const uint8_t *ptr; ///< current position in frame data
michael
parents: 6202
diff changeset
158 const uint8_t *last_ptr; ///< position where last 4608-sample block ended
6443
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
159
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
160 int error;
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
161 } APEContext;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
162
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
163 // TODO: dsputilize
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
164
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6482
diff changeset
165 static av_cold int ape_decode_init(AVCodecContext * avctx)
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
166 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
167 APEContext *s = avctx->priv_data;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
168 int i;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
169
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
170 if (avctx->extradata_size != 6) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
171 av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n");
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
172 return -1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
173 }
7823
4525dcd81357 Bump Major version, this commit is almost just renaming bits_per_sample to
michael
parents: 7723
diff changeset
174 if (avctx->bits_per_coded_sample != 16) {
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
175 av_log(avctx, AV_LOG_ERROR, "Only 16-bit samples are supported\n");
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
176 return -1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
177 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
178 if (avctx->channels > 2) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
179 av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n");
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
180 return -1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
181 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
182 s->avctx = avctx;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
183 s->channels = avctx->channels;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
184 s->fileversion = AV_RL16(avctx->extradata);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
185 s->compression_level = AV_RL16(avctx->extradata + 2);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
186 s->flags = AV_RL16(avctx->extradata + 4);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
187
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
188 av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n", s->compression_level, s->flags);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
189 if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
190 av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", s->compression_level);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
191 return -1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
192 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
193 s->fset = s->compression_level / 1000 - 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
194 for (i = 0; i < APE_FILTER_LEVELS; i++) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
195 if (!ape_filter_orders[s->fset][i])
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
196 break;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
197 s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
198 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
199
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
200 dsputil_init(&s->dsp, avctx);
7451
85ab7655ad4d Modify all codecs to report their supported input and output sample format(s).
pross
parents: 7203
diff changeset
201 avctx->sample_fmt = SAMPLE_FMT_S16;
8174
f11197441364 Add channel layout to several audio decoders I maintain
kostya
parents: 7914
diff changeset
202 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
203 return 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
204 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
205
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6482
diff changeset
206 static av_cold int ape_decode_close(AVCodecContext * avctx)
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
207 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
208 APEContext *s = avctx->priv_data;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
209 int i;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
210
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
211 for (i = 0; i < APE_FILTER_LEVELS; i++)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
212 av_freep(&s->filterbuf[i]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
213
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
214 return 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
215 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
216
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
217 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
218 * @defgroup rangecoder APE range decoder
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
219 * @{
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
220 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
221
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
222 #define CODE_BITS 32
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
223 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
224 #define SHIFT_BITS (CODE_BITS - 9)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
225 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
226 #define BOTTOM_VALUE (TOP_VALUE >> 8)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
227
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
228 /** Start the decoder */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
229 static inline void range_start_decoding(APEContext * ctx)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
230 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
231 ctx->rc.buffer = bytestream_get_byte(&ctx->ptr);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
232 ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
233 ctx->rc.range = (uint32_t) 1 << EXTRA_BITS;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
234 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
235
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
236 /** Perform normalization */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
237 static inline void range_dec_normalize(APEContext * ctx)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
238 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
239 while (ctx->rc.range <= BOTTOM_VALUE) {
6442
28e45bf84973 Prevent segfault due to reading over the end of the input buffer.
michael
parents: 6223
diff changeset
240 ctx->rc.buffer <<= 8;
28e45bf84973 Prevent segfault due to reading over the end of the input buffer.
michael
parents: 6223
diff changeset
241 if(ctx->ptr < ctx->data_end)
28e45bf84973 Prevent segfault due to reading over the end of the input buffer.
michael
parents: 6223
diff changeset
242 ctx->rc.buffer += *ctx->ptr;
28e45bf84973 Prevent segfault due to reading over the end of the input buffer.
michael
parents: 6223
diff changeset
243 ctx->ptr++;
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
244 ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
245 ctx->rc.range <<= 8;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
246 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
247 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
248
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
249 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
250 * Calculate culmulative frequency for next symbol. Does NO update!
8683
a262391f1ac6 Fix Doxygen comments for APE decoder.
kostya
parents: 8174
diff changeset
251 * @param ctx decoder context
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
252 * @param tot_f is the total frequency or (code_value)1<<shift
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
253 * @return the culmulative frequency
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
254 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
255 static inline int range_decode_culfreq(APEContext * ctx, int tot_f)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
256 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
257 range_dec_normalize(ctx);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
258 ctx->rc.help = ctx->rc.range / tot_f;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
259 return ctx->rc.low / ctx->rc.help;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
260 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
261
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
262 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
263 * Decode value with given size in bits
8683
a262391f1ac6 Fix Doxygen comments for APE decoder.
kostya
parents: 8174
diff changeset
264 * @param ctx decoder context
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
265 * @param shift number of bits to decode
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
266 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
267 static inline int range_decode_culshift(APEContext * ctx, int shift)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
268 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
269 range_dec_normalize(ctx);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
270 ctx->rc.help = ctx->rc.range >> shift;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
271 return ctx->rc.low / ctx->rc.help;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
272 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
273
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
274
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
275 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
276 * Update decoding state
8683
a262391f1ac6 Fix Doxygen comments for APE decoder.
kostya
parents: 8174
diff changeset
277 * @param ctx decoder context
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
278 * @param sy_f the interval length (frequency of the symbol)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
279 * @param lt_f the lower end (frequency sum of < symbols)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
280 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
281 static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
282 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
283 ctx->rc.low -= ctx->rc.help * lt_f;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
284 ctx->rc.range = ctx->rc.help * sy_f;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
285 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
286
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
287 /** Decode n bits (n <= 16) without modelling */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
288 static inline int range_decode_bits(APEContext * ctx, int n)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
289 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
290 int sym = range_decode_culshift(ctx, n);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
291 range_decode_update(ctx, 1, sym);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
292 return sym;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
293 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
294
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
295
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
296 #define MODEL_ELEMENTS 64
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
297
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
298 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
299 * Fixed probabilities for symbols in Monkey Audio version 3.97
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
300 */
6482
258f569d30ca smaller tables
michael
parents: 6444
diff changeset
301 static const uint16_t counts_3970[22] = {
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
302 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
303 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
6444
3572cc5bc8ff Remove unused elements from tables.
michael
parents: 6443
diff changeset
304 65450, 65469, 65480, 65487, 65491, 65493,
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
305 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
306
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
307 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
308 * Probability ranges for symbols in Monkey Audio version 3.97
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
309 */
6444
3572cc5bc8ff Remove unused elements from tables.
michael
parents: 6443
diff changeset
310 static const uint16_t counts_diff_3970[21] = {
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
311 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
312 1104, 677, 415, 248, 150, 89, 54, 31,
6444
3572cc5bc8ff Remove unused elements from tables.
michael
parents: 6443
diff changeset
313 19, 11, 7, 4, 2,
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
314 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
315
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
316 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
317 * Fixed probabilities for symbols in Monkey Audio version 3.98
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
318 */
6482
258f569d30ca smaller tables
michael
parents: 6444
diff changeset
319 static const uint16_t counts_3980[22] = {
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
320 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
321 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
6444
3572cc5bc8ff Remove unused elements from tables.
michael
parents: 6443
diff changeset
322 65485, 65488, 65490, 65491, 65492, 65493,
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
323 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
324
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
325 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
326 * Probability ranges for symbols in Monkey Audio version 3.98
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
327 */
6444
3572cc5bc8ff Remove unused elements from tables.
michael
parents: 6443
diff changeset
328 static const uint16_t counts_diff_3980[21] = {
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
329 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
330 261, 119, 65, 31, 19, 10, 6, 3,
6444
3572cc5bc8ff Remove unused elements from tables.
michael
parents: 6443
diff changeset
331 3, 2, 1, 1, 1,
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
332 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
333
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
334 /**
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
335 * Decode symbol
8683
a262391f1ac6 Fix Doxygen comments for APE decoder.
kostya
parents: 8174
diff changeset
336 * @param ctx decoder context
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
337 * @param counts probability range start position
8683
a262391f1ac6 Fix Doxygen comments for APE decoder.
kostya
parents: 8174
diff changeset
338 * @param counts_diff probability range widths
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
339 */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
340 static inline int range_get_symbol(APEContext * ctx,
6482
258f569d30ca smaller tables
michael
parents: 6444
diff changeset
341 const uint16_t counts[],
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
342 const uint16_t counts_diff[])
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
343 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
344 int symbol, cf;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
345
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
346 cf = range_decode_culshift(ctx, 16);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
347
6443
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
348 if(cf > 65492){
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
349 symbol= cf - 65535 + 63;
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
350 range_decode_update(ctx, 1, cf);
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
351 if(cf > 65535)
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
352 ctx->error=1;
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
353 return symbol;
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
354 }
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
355 /* figure out the symbol inefficiently; a binary search would be much better */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
356 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
357
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
358 range_decode_update(ctx, counts_diff[symbol], counts[symbol]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
359
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
360 return symbol;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
361 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
362 /** @} */ // group rangecoder
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
363
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
364 static inline void update_rice(APERice *rice, int x)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
365 {
7914
c15b1e83d27d Correct wrong lower limit and condition used in APE decoder
kostya
parents: 7823
diff changeset
366 int lim = rice->k ? (1 << (rice->k + 4)) : 0;
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
367 rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
368
7914
c15b1e83d27d Correct wrong lower limit and condition used in APE decoder
kostya
parents: 7823
diff changeset
369 if (rice->ksum < lim)
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
370 rice->k--;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
371 else if (rice->ksum >= (1 << (rice->k + 5)))
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
372 rice->k++;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
373 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
374
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
375 static inline int ape_decode_value(APEContext * ctx, APERice *rice)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
376 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
377 int x, overflow;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
378
7723
20b105281e87 While APE changed container format in 3.98, frequency tables for range coding
kostya
parents: 7451
diff changeset
379 if (ctx->fileversion < 3990) {
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
380 int tmpk;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
381
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
382 overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
383
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
384 if (overflow == (MODEL_ELEMENTS - 1)) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
385 tmpk = range_decode_bits(ctx, 5);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
386 overflow = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
387 } else
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
388 tmpk = (rice->k < 1) ? 0 : rice->k - 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
389
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
390 if (tmpk <= 16)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
391 x = range_decode_bits(ctx, tmpk);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
392 else {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
393 x = range_decode_bits(ctx, 16);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
394 x |= (range_decode_bits(ctx, tmpk - 16) << 16);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
395 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
396 x += overflow << tmpk;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
397 } else {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
398 int base, pivot;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
399
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
400 pivot = rice->ksum >> 5;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
401 if (pivot == 0)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
402 pivot = 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
403
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
404 overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
405
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
406 if (overflow == (MODEL_ELEMENTS - 1)) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
407 overflow = range_decode_bits(ctx, 16) << 16;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
408 overflow |= range_decode_bits(ctx, 16);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
409 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
410
10545
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
411 if (pivot < 0x10000) {
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
412 base = range_decode_culfreq(ctx, pivot);
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
413 range_decode_update(ctx, 1, base);
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
414 } else {
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
415 int base_hi = pivot, base_lo;
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
416 int bbits = 0;
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
417
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
418 while (base_hi & ~0xFFFF) {
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
419 base_hi >>= 1;
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
420 bbits++;
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
421 }
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
422 base_hi = range_decode_culfreq(ctx, base_hi + 1);
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
423 range_decode_update(ctx, 1, base_hi);
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
424 base_lo = range_decode_culfreq(ctx, 1 << bbits);
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
425 range_decode_update(ctx, 1, base_lo);
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
426
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
427 base = (base_hi << bbits) + base_lo;
e5a8a6b9a717 Implement missing case for decoding samples with large pivot value in APE
kostya
parents: 10181
diff changeset
428 }
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
429
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
430 x = base + overflow * pivot;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
431 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
432
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
433 update_rice(rice, x);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
434
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
435 /* Convert to signed */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
436 if (x & 1)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
437 return (x >> 1) + 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
438 else
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
439 return -(x >> 1);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
440 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
441
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
442 static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
443 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
444 int32_t *decoded0 = ctx->decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
445 int32_t *decoded1 = ctx->decoded1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
446
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
447 ctx->blocksdecoded = blockstodecode;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
448
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
449 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
450 /* We are pure silence, just memset the output buffer. */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
451 memset(decoded0, 0, blockstodecode * sizeof(int32_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
452 memset(decoded1, 0, blockstodecode * sizeof(int32_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
453 } else {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
454 while (blockstodecode--) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
455 *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
456 if (stereo)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
457 *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
458 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
459 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
460
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
461 if (ctx->blocksdecoded == ctx->currentframeblocks)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
462 range_dec_normalize(ctx); /* normalize to use up all bytes */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
463 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
464
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
465 static void init_entropy_decoder(APEContext * ctx)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
466 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
467 /* Read the CRC */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
468 ctx->CRC = bytestream_get_be32(&ctx->ptr);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
469
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
470 /* Read the frame flags if they exist */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
471 ctx->frameflags = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
472 if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
473 ctx->CRC &= ~0x80000000;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
474
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
475 ctx->frameflags = bytestream_get_be32(&ctx->ptr);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
476 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
477
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
478 /* Keep a count of the blocks decoded in this frame */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
479 ctx->blocksdecoded = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
480
5963
80103098c797 spelling
vitor
parents: 5673
diff changeset
481 /* Initialize the rice structs */
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
482 ctx->riceX.k = 10;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
483 ctx->riceX.ksum = (1 << ctx->riceX.k) * 16;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
484 ctx->riceY.k = 10;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
485 ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
486
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
487 /* The first 8 bits of input are ignored. */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
488 ctx->ptr++;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
489
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
490 range_start_decoding(ctx);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
491 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
492
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
493 static const int32_t initial_coeffs[4] = {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
494 360, 317, -109, 98
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
495 };
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
496
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
497 static void init_predictor_decoder(APEContext * ctx)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
498 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
499 APEPredictor *p = &ctx->predictor;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
500
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
501 /* Zero the history buffers */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
502 memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
503 p->buf = p->historybuffer;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
504
5966
1275461ae6f8 Fix some spelling mistakes.
diego
parents: 5963
diff changeset
505 /* Initialize and zero the coefficients */
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
506 memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
507 memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
508 memset(p->coeffsB, 0, sizeof(p->coeffsB));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
509
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
510 p->filterA[0] = p->filterA[1] = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
511 p->filterB[0] = p->filterB[1] = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
512 p->lastA[0] = p->lastA[1] = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
513 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
514
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
515 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
516 static inline int APESIGN(int32_t x) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
517 return (x < 0) - (x > 0);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
518 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
519
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
520 static int predictor_update_filter(APEPredictor *p, const int decoded, const int filter, const int delayA, const int delayB, const int adaptA, const int adaptB)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
521 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
522 int32_t predictionA, predictionB;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
523
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
524 p->buf[delayA] = p->lastA[filter];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
525 p->buf[adaptA] = APESIGN(p->buf[delayA]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
526 p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
527 p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
528
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
529 predictionA = p->buf[delayA ] * p->coeffsA[filter][0] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
530 p->buf[delayA - 1] * p->coeffsA[filter][1] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
531 p->buf[delayA - 2] * p->coeffsA[filter][2] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
532 p->buf[delayA - 3] * p->coeffsA[filter][3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
533
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
534 /* Apply a scaled first-order filter compression */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
535 p->buf[delayB] = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
536 p->buf[adaptB] = APESIGN(p->buf[delayB]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
537 p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
538 p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
539 p->filterB[filter] = p->filterA[filter ^ 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
540
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
541 predictionB = p->buf[delayB ] * p->coeffsB[filter][0] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
542 p->buf[delayB - 1] * p->coeffsB[filter][1] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
543 p->buf[delayB - 2] * p->coeffsB[filter][2] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
544 p->buf[delayB - 3] * p->coeffsB[filter][3] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
545 p->buf[delayB - 4] * p->coeffsB[filter][4];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
546
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
547 p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
548 p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
549
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
550 if (!decoded) // no need updating filter coefficients
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
551 return p->filterA[filter];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
552
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
553 if (decoded > 0) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
554 p->coeffsA[filter][0] -= p->buf[adaptA ];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
555 p->coeffsA[filter][1] -= p->buf[adaptA - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
556 p->coeffsA[filter][2] -= p->buf[adaptA - 2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
557 p->coeffsA[filter][3] -= p->buf[adaptA - 3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
558
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
559 p->coeffsB[filter][0] -= p->buf[adaptB ];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
560 p->coeffsB[filter][1] -= p->buf[adaptB - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
561 p->coeffsB[filter][2] -= p->buf[adaptB - 2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
562 p->coeffsB[filter][3] -= p->buf[adaptB - 3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
563 p->coeffsB[filter][4] -= p->buf[adaptB - 4];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
564 } else {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
565 p->coeffsA[filter][0] += p->buf[adaptA ];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
566 p->coeffsA[filter][1] += p->buf[adaptA - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
567 p->coeffsA[filter][2] += p->buf[adaptA - 2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
568 p->coeffsA[filter][3] += p->buf[adaptA - 3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
569
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
570 p->coeffsB[filter][0] += p->buf[adaptB ];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
571 p->coeffsB[filter][1] += p->buf[adaptB - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
572 p->coeffsB[filter][2] += p->buf[adaptB - 2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
573 p->coeffsB[filter][3] += p->buf[adaptB - 3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
574 p->coeffsB[filter][4] += p->buf[adaptB - 4];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
575 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
576 return p->filterA[filter];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
577 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
578
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
579 static void predictor_decode_stereo(APEContext * ctx, int count)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
580 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
581 int32_t predictionA, predictionB;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
582 APEPredictor *p = &ctx->predictor;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
583 int32_t *decoded0 = ctx->decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
584 int32_t *decoded1 = ctx->decoded1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
585
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
586 while (count--) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
587 /* Predictor Y */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
588 predictionA = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
589 predictionB = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
590 *(decoded0++) = predictionA;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
591 *(decoded1++) = predictionB;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
592
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
593 /* Combined */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
594 p->buf++;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
595
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
596 /* Have we filled the history buffer? */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
597 if (p->buf == p->historybuffer + HISTORY_SIZE) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
598 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
599 p->buf = p->historybuffer;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
600 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
601 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
602 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
603
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
604 static void predictor_decode_mono(APEContext * ctx, int count)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
605 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
606 APEPredictor *p = &ctx->predictor;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
607 int32_t *decoded0 = ctx->decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
608 int32_t predictionA, currentA, A;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
609
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
610 currentA = p->lastA[0];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
611
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
612 while (count--) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
613 A = *decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
614
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
615 p->buf[YDELAYA] = currentA;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
616 p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
617
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
618 predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
619 p->buf[YDELAYA - 1] * p->coeffsA[0][1] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
620 p->buf[YDELAYA - 2] * p->coeffsA[0][2] +
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
621 p->buf[YDELAYA - 3] * p->coeffsA[0][3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
622
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
623 currentA = A + (predictionA >> 10);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
624
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
625 p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
626 p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
627
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
628 if (A > 0) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
629 p->coeffsA[0][0] -= p->buf[YADAPTCOEFFSA ];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
630 p->coeffsA[0][1] -= p->buf[YADAPTCOEFFSA - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
631 p->coeffsA[0][2] -= p->buf[YADAPTCOEFFSA - 2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
632 p->coeffsA[0][3] -= p->buf[YADAPTCOEFFSA - 3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
633 } else if (A < 0) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
634 p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
635 p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
636 p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
637 p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
638 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
639
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
640 p->buf++;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
641
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
642 /* Have we filled the history buffer? */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
643 if (p->buf == p->historybuffer + HISTORY_SIZE) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
644 memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
645 p->buf = p->historybuffer;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
646 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
647
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
648 p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
649 *(decoded0++) = p->filterA[0];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
650 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
651
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
652 p->lastA[0] = currentA;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
653 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
654
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
655 static void do_init_filter(APEFilter *f, int16_t * buf, int order)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
656 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
657 f->coeffs = buf;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
658 f->historybuffer = buf + order;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
659 f->delay = f->historybuffer + order * 2;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
660 f->adaptcoeffs = f->historybuffer + order;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
661
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
662 memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
663 memset(f->coeffs, 0, order * sizeof(int16_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
664 f->avg = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
665 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
666
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
667 static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
668 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
669 do_init_filter(&f[0], buf, order);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
670 do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
671 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
672
7203
87b1dfb5a98d Add several vector functions used by Monkey's Audio decoder to dsputil
kostya
parents: 7040
diff changeset
673 static inline void do_apply_filter(APEContext * ctx, int version, APEFilter *f, int32_t *data, int count, int order, int fracbits)
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
674 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
675 int res;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
676 int absres;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
677
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
678 while (count--) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
679 /* round fixedpoint scalar product */
7203
87b1dfb5a98d Add several vector functions used by Monkey's Audio decoder to dsputil
kostya
parents: 7040
diff changeset
680 res = (ctx->dsp.scalarproduct_int16(f->delay - order, f->coeffs, order, 0) + (1 << (fracbits - 1))) >> fracbits;
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
681
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
682 if (*data < 0)
7203
87b1dfb5a98d Add several vector functions used by Monkey's Audio decoder to dsputil
kostya
parents: 7040
diff changeset
683 ctx->dsp.add_int16(f->coeffs, f->adaptcoeffs - order, order);
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
684 else if (*data > 0)
7203
87b1dfb5a98d Add several vector functions used by Monkey's Audio decoder to dsputil
kostya
parents: 7040
diff changeset
685 ctx->dsp.sub_int16(f->coeffs, f->adaptcoeffs - order, order);
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
686
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
687 res += *data;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
688
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
689 *data++ = res;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
690
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
691 /* Update the output history */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
692 *f->delay++ = av_clip_int16(res);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
693
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
694 if (version < 3980) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
695 /* Version ??? to < 3.98 files (untested) */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
696 f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
697 f->adaptcoeffs[-4] >>= 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
698 f->adaptcoeffs[-8] >>= 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
699 } else {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
700 /* Version 3.98 and later files */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
701
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
702 /* Update the adaption coefficients */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
703 absres = (res < 0 ? -res : res);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
704
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
705 if (absres > (f->avg * 3))
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
706 *f->adaptcoeffs = ((res >> 25) & 64) - 32;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
707 else if (absres > (f->avg * 4) / 3)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
708 *f->adaptcoeffs = ((res >> 26) & 32) - 16;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
709 else if (absres > 0)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
710 *f->adaptcoeffs = ((res >> 27) & 16) - 8;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
711 else
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
712 *f->adaptcoeffs = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
713
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
714 f->avg += (absres - f->avg) / 16;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
715
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
716 f->adaptcoeffs[-1] >>= 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
717 f->adaptcoeffs[-2] >>= 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
718 f->adaptcoeffs[-8] >>= 1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
719 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
720
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
721 f->adaptcoeffs++;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
722
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
723 /* Have we filled the history buffer? */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
724 if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
725 memmove(f->historybuffer, f->delay - (order * 2),
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
726 (order * 2) * sizeof(int16_t));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
727 f->delay = f->historybuffer + order * 2;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
728 f->adaptcoeffs = f->historybuffer + order;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
729 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
730 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
731 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
732
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
733 static void apply_filter(APEContext * ctx, APEFilter *f,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
734 int32_t * data0, int32_t * data1,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
735 int count, int order, int fracbits)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
736 {
7203
87b1dfb5a98d Add several vector functions used by Monkey's Audio decoder to dsputil
kostya
parents: 7040
diff changeset
737 do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits);
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
738 if (data1)
7203
87b1dfb5a98d Add several vector functions used by Monkey's Audio decoder to dsputil
kostya
parents: 7040
diff changeset
739 do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits);
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
740 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
741
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
742 static void ape_apply_filters(APEContext * ctx, int32_t * decoded0,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
743 int32_t * decoded1, int count)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
744 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
745 int i;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
746
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
747 for (i = 0; i < APE_FILTER_LEVELS; i++) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
748 if (!ape_filter_orders[ctx->fset][i])
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
749 break;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
750 apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
751 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
752 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
753
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
754 static void init_frame_decoder(APEContext * ctx)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
755 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
756 int i;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
757 init_entropy_decoder(ctx);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
758 init_predictor_decoder(ctx);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
759
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
760 for (i = 0; i < APE_FILTER_LEVELS; i++) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
761 if (!ape_filter_orders[ctx->fset][i])
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
762 break;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
763 init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
764 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
765 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
766
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
767 static void ape_unpack_mono(APEContext * ctx, int count)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
768 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
769 int32_t left;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
770 int32_t *decoded0 = ctx->decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
771 int32_t *decoded1 = ctx->decoded1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
772
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
773 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
774 entropy_decode(ctx, count, 0);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
775 /* We are pure silence, so we're done. */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
776 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n");
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
777 return;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
778 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
779
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
780 entropy_decode(ctx, count, 0);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
781 ape_apply_filters(ctx, decoded0, NULL, count);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
782
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
783 /* Now apply the predictor decoding */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
784 predictor_decode_mono(ctx, count);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
785
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
786 /* Pseudo-stereo - just copy left channel to right channel */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
787 if (ctx->channels == 2) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
788 while (count--) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
789 left = *decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
790 *(decoded1++) = *(decoded0++) = left;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
791 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
792 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
793 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
794
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
795 static void ape_unpack_stereo(APEContext * ctx, int count)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
796 {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
797 int32_t left, right;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
798 int32_t *decoded0 = ctx->decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
799 int32_t *decoded1 = ctx->decoded1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
800
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
801 if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
802 /* We are pure silence, so we're done. */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
803 av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n");
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
804 return;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
805 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
806
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
807 entropy_decode(ctx, count, 1);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
808 ape_apply_filters(ctx, decoded0, decoded1, count);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
809
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
810 /* Now apply the predictor decoding */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
811 predictor_decode_stereo(ctx, count);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
812
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
813 /* Decorrelate and scale to output depth */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
814 while (count--) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
815 left = *decoded1 - (*decoded0 / 2);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
816 right = left + *decoded0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
817
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
818 *(decoded0++) = left;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
819 *(decoded1++) = right;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
820 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
821 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
822
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
823 static int ape_decode_frame(AVCodecContext * avctx,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
824 void *data, int *data_size,
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
825 AVPacket *avpkt)
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
826 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
827 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
828 int buf_size = avpkt->size;
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
829 APEContext *s = avctx->priv_data;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
830 int16_t *samples = data;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
831 int nblocks;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
832 int i, n;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
833 int blockstodecode;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
834 int bytes_used;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
835
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
836 if (buf_size == 0 && !s->samples) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
837 *data_size = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
838 return 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
839 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
840
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
841 /* should not happen but who knows */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
842 if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
843 av_log (avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc! (max is %d where you have %d)\n", *data_size, s->samples * 2 * avctx->channels);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
844 return -1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
845 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
846
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
847 if(!s->samples){
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
848 s->data = av_realloc(s->data, (buf_size + 3) & ~3);
6223
michael
parents: 6202
diff changeset
849 s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
850 s->ptr = s->last_ptr = s->data;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
851 s->data_end = s->data + buf_size;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
852
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
853 nblocks = s->samples = bytestream_get_be32(&s->ptr);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
854 n = bytestream_get_be32(&s->ptr);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
855 if(n < 0 || n > 3){
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
856 av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
857 s->data = NULL;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
858 return -1;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
859 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
860 s->ptr += n;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
861
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
862 s->currentframeblocks = nblocks;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
863 buf += 4;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
864 if (s->samples <= 0) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
865 *data_size = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
866 return buf_size;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
867 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
868
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
869 memset(s->decoded0, 0, sizeof(s->decoded0));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
870 memset(s->decoded1, 0, sizeof(s->decoded1));
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
871
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
872 /* Initialize the frame decoder */
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
873 init_frame_decoder(s);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
874 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
875
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
876 if (!s->data) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
877 *data_size = 0;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
878 return buf_size;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
879 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
880
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
881 nblocks = s->samples;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
882 blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
883
6443
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
884 s->error=0;
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
885
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
886 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
887 ape_unpack_mono(s, blockstodecode);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
888 else
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
889 ape_unpack_stereo(s, blockstodecode);
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
890
6443
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
891 if(s->error || s->ptr > s->data_end){
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
892 s->samples=0;
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
893 av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n");
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
894 return -1;
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
895 }
e3adb7e96812 Detect and prevent reading over the end of counts_*. We pass the error
michael
parents: 6442
diff changeset
896
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
897 for (i = 0; i < blockstodecode; i++) {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
898 *samples++ = s->decoded0[i];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
899 if(s->channels == 2)
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
900 *samples++ = s->decoded1[i];
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
901 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
902
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
903 s->samples -= blockstodecode;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
904
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
905 *data_size = blockstodecode * 2 * s->channels;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
906 bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
907 s->last_ptr = s->ptr;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
908 return bytes_used;
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
909 }
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
910
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
911 AVCodec ape_decoder = {
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
912 "ape",
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
913 CODEC_TYPE_AUDIO,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
914 CODEC_ID_APE,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
915 sizeof(APEContext),
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
916 ape_decode_init,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
917 NULL,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
918 ape_decode_close,
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
919 ape_decode_frame,
10181
b33c75c58ffe Monkey's Audio uses subframes too, declare that in its capabilities
kostya
parents: 9428
diff changeset
920 .capabilities = CODEC_CAP_SUBFRAMES,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6712
diff changeset
921 .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
5673
9810f0bbacb2 Monkey Audio decoder
kostya
parents:
diff changeset
922 };