Originally committed as revision 10484 to svn://svn.ffmpeg.org/ffmpeg/trunk
Kostya Shishkov authored on 2007/09/13 12:22:47... | ... |
@@ -116,6 +116,7 @@ different game cutscenes repacked for use with ScummVM. |
116 | 116 |
@tab Used in some games from Bethesda Softworks. |
117 | 117 |
@item CRYO APC @tab @tab X |
118 | 118 |
@tab Audio format used in some games by CRYO Interactive Entertainment. |
119 |
+@item Monkey's Audio @tab @tab X |
|
119 | 120 |
@end multitable |
120 | 121 |
|
121 | 122 |
@code{X} means that encoding (resp. decoding) is supported. |
... | ... |
@@ -311,6 +312,7 @@ following image formats are supported: |
311 | 311 |
@tab Only SV7 is supported |
312 | 312 |
@item DT$ Coherent Audio @tab @tab X |
313 | 313 |
@item ATRAC 3 @tab @tab X |
314 |
+@item Monkey's Audio @tab @tab X @tab Only versions 3.97-3.99 are supported |
|
314 | 315 |
@end multitable |
315 | 316 |
|
316 | 317 |
@code{X} means that encoding (resp. decoding) is supported. |
... | ... |
@@ -35,6 +35,7 @@ OBJS-$(CONFIG_AASC_DECODER) += aasc.o |
35 | 35 |
OBJS-$(CONFIG_AC3_DECODER) += ac3dec.o ac3tab.o ac3.o mdct.o fft.o |
36 | 36 |
OBJS-$(CONFIG_AC3_ENCODER) += ac3enc.o ac3tab.o ac3.o |
37 | 37 |
OBJS-$(CONFIG_ALAC_DECODER) += alac.o |
38 |
+OBJS-$(CONFIG_APE_DECODER) += apedec.o |
|
38 | 39 |
OBJS-$(CONFIG_ASV1_DECODER) += asv1.o |
39 | 40 |
OBJS-$(CONFIG_ASV1_ENCODER) += asv1.o |
40 | 41 |
OBJS-$(CONFIG_ASV2_DECODER) += asv1.o |
... | ... |
@@ -168,6 +168,7 @@ void avcodec_register_all(void) |
168 | 168 |
REGISTER_DECODER (MPEG4AAC, mpeg4aac); |
169 | 169 |
REGISTER_ENCDEC (AC3, ac3); |
170 | 170 |
REGISTER_DECODER (ALAC, alac); |
171 |
+ REGISTER_DECODER (APE, ape); |
|
171 | 172 |
REGISTER_DECODER (ATRAC3, atrac3); |
172 | 173 |
REGISTER_DECODER (COOK, cook); |
173 | 174 |
REGISTER_DECODER (DCA, dca); |
85 | 86 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,922 @@ |
0 |
+/* |
|
1 |
+ * Monkey's Audio lossless audio decoder |
|
2 |
+ * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org> |
|
3 |
+ * based upon libdemac from Dave Chapman. |
|
4 |
+ * |
|
5 |
+ * This file is part of FFmpeg. |
|
6 |
+ * |
|
7 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
8 |
+ * modify it under the terms of the GNU Lesser General Public |
|
9 |
+ * License as published by the Free Software Foundation; either |
|
10 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
11 |
+ * |
|
12 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
+ * Lesser General Public License for more details. |
|
16 |
+ * |
|
17 |
+ * You should have received a copy of the GNU Lesser General Public |
|
18 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
19 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
20 |
+ */ |
|
21 |
+ |
|
22 |
+#define ALT_BITSTREAM_READER_LE |
|
23 |
+#include "avcodec.h" |
|
24 |
+#include "dsputil.h" |
|
25 |
+#include "bitstream.h" |
|
26 |
+#include "bytestream.h" |
|
27 |
+ |
|
28 |
+/** |
|
29 |
+ * @file apedec.c |
|
30 |
+ * Monkey's Audio lossless audio decoder |
|
31 |
+ */ |
|
32 |
+ |
|
33 |
+#define BLOCKS_PER_LOOP 4608 |
|
34 |
+#define MAX_CHANNELS 2 |
|
35 |
+#define MAX_BYTESPERSAMPLE 3 |
|
36 |
+ |
|
37 |
+#define APE_FRAMECODE_MONO_SILENCE 1 |
|
38 |
+#define APE_FRAMECODE_STEREO_SILENCE 3 |
|
39 |
+#define APE_FRAMECODE_PSEUDO_STEREO 4 |
|
40 |
+ |
|
41 |
+#define HISTORY_SIZE 512 |
|
42 |
+#define PREDICTOR_ORDER 8 |
|
43 |
+/** Total size of all predictor histories */ |
|
44 |
+#define PREDICTOR_SIZE 50 |
|
45 |
+ |
|
46 |
+#define YDELAYA (18 + PREDICTOR_ORDER*4) |
|
47 |
+#define YDELAYB (18 + PREDICTOR_ORDER*3) |
|
48 |
+#define XDELAYA (18 + PREDICTOR_ORDER*2) |
|
49 |
+#define XDELAYB (18 + PREDICTOR_ORDER) |
|
50 |
+ |
|
51 |
+#define YADAPTCOEFFSA 18 |
|
52 |
+#define XADAPTCOEFFSA 14 |
|
53 |
+#define YADAPTCOEFFSB 10 |
|
54 |
+#define XADAPTCOEFFSB 5 |
|
55 |
+ |
|
56 |
+/** |
|
57 |
+ * Possible compression levels |
|
58 |
+ * @{ |
|
59 |
+ */ |
|
60 |
+enum APECompressionLevel { |
|
61 |
+ COMPRESSION_LEVEL_FAST = 1000, |
|
62 |
+ COMPRESSION_LEVEL_NORMAL = 2000, |
|
63 |
+ COMPRESSION_LEVEL_HIGH = 3000, |
|
64 |
+ COMPRESSION_LEVEL_EXTRA_HIGH = 4000, |
|
65 |
+ COMPRESSION_LEVEL_INSANE = 5000 |
|
66 |
+}; |
|
67 |
+/** @} */ |
|
68 |
+ |
|
69 |
+#define APE_FILTER_LEVELS 3 |
|
70 |
+ |
|
71 |
+/** Filter orders depending on compression level */ |
|
72 |
+static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = { |
|
73 |
+ { 0, 0, 0 }, |
|
74 |
+ { 16, 0, 0 }, |
|
75 |
+ { 64, 0, 0 }, |
|
76 |
+ { 32, 256, 0 }, |
|
77 |
+ { 16, 256, 1280 } |
|
78 |
+}; |
|
79 |
+ |
|
80 |
+/** Filter fraction bits depending on compression level */ |
|
81 |
+static const uint16_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = { |
|
82 |
+ { 0, 0, 0 }, |
|
83 |
+ { 11, 0, 0 }, |
|
84 |
+ { 11, 0, 0 }, |
|
85 |
+ { 10, 13, 0 }, |
|
86 |
+ { 11, 13, 15 } |
|
87 |
+}; |
|
88 |
+ |
|
89 |
+ |
|
90 |
+/** Filters applied to the decoded data */ |
|
91 |
+typedef struct APEFilter { |
|
92 |
+ int16_t *coeffs; ///< actual coefficients used in filtering |
|
93 |
+ int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients |
|
94 |
+ int16_t *historybuffer; ///< filter memory |
|
95 |
+ int16_t *delay; ///< filtered values |
|
96 |
+ |
|
97 |
+ int avg; |
|
98 |
+} APEFilter; |
|
99 |
+ |
|
100 |
+typedef struct APERice { |
|
101 |
+ uint32_t k; |
|
102 |
+ uint32_t ksum; |
|
103 |
+} APERice; |
|
104 |
+ |
|
105 |
+typedef struct APERangecoder { |
|
106 |
+ uint32_t low; ///< low end of interval |
|
107 |
+ uint32_t range; ///< length of interval |
|
108 |
+ uint32_t help; ///< bytes_to_follow resp. intermediate value |
|
109 |
+ unsigned int buffer; ///< buffer for input/output |
|
110 |
+} APERangecoder; |
|
111 |
+ |
|
112 |
+/** Filter histories */ |
|
113 |
+typedef struct APEPredictor { |
|
114 |
+ int32_t *buf; |
|
115 |
+ |
|
116 |
+ int32_t lastA[2]; |
|
117 |
+ |
|
118 |
+ int32_t filterA[2]; |
|
119 |
+ int32_t filterB[2]; |
|
120 |
+ |
|
121 |
+ int32_t coeffsA[2][4]; ///< adaption coefficients |
|
122 |
+ int32_t coeffsB[2][5]; ///< adaption coefficients |
|
123 |
+ int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE]; |
|
124 |
+} APEPredictor; |
|
125 |
+ |
|
126 |
+/** Decoder context */ |
|
127 |
+typedef struct APEContext { |
|
128 |
+ AVCodecContext *avctx; |
|
129 |
+ DSPContext dsp; |
|
130 |
+ int channels; |
|
131 |
+ int samples; ///< samples left to decode in current frame |
|
132 |
+ |
|
133 |
+ int fileversion; ///< codec version, very important in decoding process |
|
134 |
+ int compression_level; ///< compression levels |
|
135 |
+ int fset; ///< which filter set to use (calculated from compression level) |
|
136 |
+ int flags; ///< global decoder flags |
|
137 |
+ |
|
138 |
+ uint32_t CRC; ///< frame CRC |
|
139 |
+ int frameflags; ///< frame flags |
|
140 |
+ int currentframeblocks; ///< samples (per channel) in current frame |
|
141 |
+ int blocksdecoded; ///< count of decoded samples in current frame |
|
142 |
+ APEPredictor predictor; ///< predictor used for final reconstruction |
|
143 |
+ |
|
144 |
+ int32_t decoded0[BLOCKS_PER_LOOP]; ///< decoded data for the first channel |
|
145 |
+ int32_t decoded1[BLOCKS_PER_LOOP]; ///< decoded data for the second channel |
|
146 |
+ |
|
147 |
+ int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory |
|
148 |
+ |
|
149 |
+ APERangecoder rc; ///< rangecoder used to decode actual values |
|
150 |
+ APERice riceX; ///< rice code parameters for the second channel |
|
151 |
+ APERice riceY; ///< rice code parameters for the first channel |
|
152 |
+ APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction |
|
153 |
+ |
|
154 |
+ uint8_t *data; ///< current frame data |
|
155 |
+ uint8_t *data_end; ///< frame data end |
|
156 |
+ uint8_t *ptr; ///< current position in frame data |
|
157 |
+ uint8_t *last_ptr; ///< position where last 4608-sample block ended |
|
158 |
+} APEContext; |
|
159 |
+ |
|
160 |
+// TODO: dsputilize |
|
161 |
+static inline void vector_add(int16_t * v1, int16_t * v2, int order) |
|
162 |
+{ |
|
163 |
+ while (order--) |
|
164 |
+ *v1++ += *v2++; |
|
165 |
+} |
|
166 |
+ |
|
167 |
+// TODO: dsputilize |
|
168 |
+static inline void vector_sub(int16_t * v1, int16_t * v2, int order) |
|
169 |
+{ |
|
170 |
+ while (order--) |
|
171 |
+ *v1++ -= *v2++; |
|
172 |
+} |
|
173 |
+ |
|
174 |
+// TODO: dsputilize |
|
175 |
+static inline int32_t scalarproduct(int16_t * v1, int16_t * v2, int order) |
|
176 |
+{ |
|
177 |
+ int res = 0; |
|
178 |
+ |
|
179 |
+ while (order--) |
|
180 |
+ res += *v1++ * *v2++; |
|
181 |
+ |
|
182 |
+ return res; |
|
183 |
+} |
|
184 |
+ |
|
185 |
+static int ape_decode_init(AVCodecContext * avctx) |
|
186 |
+{ |
|
187 |
+ APEContext *s = avctx->priv_data; |
|
188 |
+ int i; |
|
189 |
+ |
|
190 |
+ if (avctx->extradata_size != 6) { |
|
191 |
+ av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n"); |
|
192 |
+ return -1; |
|
193 |
+ } |
|
194 |
+ if (avctx->bits_per_sample != 16) { |
|
195 |
+ av_log(avctx, AV_LOG_ERROR, "Only 16-bit samples are supported\n"); |
|
196 |
+ return -1; |
|
197 |
+ } |
|
198 |
+ if (avctx->channels > 2) { |
|
199 |
+ av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n"); |
|
200 |
+ return -1; |
|
201 |
+ } |
|
202 |
+ s->avctx = avctx; |
|
203 |
+ s->channels = avctx->channels; |
|
204 |
+ s->fileversion = AV_RL16(avctx->extradata); |
|
205 |
+ s->compression_level = AV_RL16(avctx->extradata + 2); |
|
206 |
+ s->flags = AV_RL16(avctx->extradata + 4); |
|
207 |
+ |
|
208 |
+ av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n", s->compression_level, s->flags); |
|
209 |
+ if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE) { |
|
210 |
+ av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", s->compression_level); |
|
211 |
+ return -1; |
|
212 |
+ } |
|
213 |
+ s->fset = s->compression_level / 1000 - 1; |
|
214 |
+ for (i = 0; i < APE_FILTER_LEVELS; i++) { |
|
215 |
+ if (!ape_filter_orders[s->fset][i]) |
|
216 |
+ break; |
|
217 |
+ s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4); |
|
218 |
+ } |
|
219 |
+ |
|
220 |
+ dsputil_init(&s->dsp, avctx); |
|
221 |
+ return 0; |
|
222 |
+} |
|
223 |
+ |
|
224 |
+static int ape_decode_close(AVCodecContext * avctx) |
|
225 |
+{ |
|
226 |
+ APEContext *s = avctx->priv_data; |
|
227 |
+ int i; |
|
228 |
+ |
|
229 |
+ for (i = 0; i < APE_FILTER_LEVELS; i++) |
|
230 |
+ av_freep(&s->filterbuf[i]); |
|
231 |
+ |
|
232 |
+ return 0; |
|
233 |
+} |
|
234 |
+ |
|
235 |
+/** |
|
236 |
+ * @defgroup rangecoder APE range decoder |
|
237 |
+ * @{ |
|
238 |
+ */ |
|
239 |
+ |
|
240 |
+#define CODE_BITS 32 |
|
241 |
+#define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1)) |
|
242 |
+#define SHIFT_BITS (CODE_BITS - 9) |
|
243 |
+#define EXTRA_BITS ((CODE_BITS-2) % 8 + 1) |
|
244 |
+#define BOTTOM_VALUE (TOP_VALUE >> 8) |
|
245 |
+ |
|
246 |
+/** Start the decoder */ |
|
247 |
+static inline void range_start_decoding(APEContext * ctx) |
|
248 |
+{ |
|
249 |
+ ctx->rc.buffer = bytestream_get_byte(&ctx->ptr); |
|
250 |
+ ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS); |
|
251 |
+ ctx->rc.range = (uint32_t) 1 << EXTRA_BITS; |
|
252 |
+} |
|
253 |
+ |
|
254 |
+/** Perform normalization */ |
|
255 |
+static inline void range_dec_normalize(APEContext * ctx) |
|
256 |
+{ |
|
257 |
+ while (ctx->rc.range <= BOTTOM_VALUE) { |
|
258 |
+ ctx->rc.buffer = (ctx->rc.buffer << 8) | bytestream_get_byte(&ctx->ptr); |
|
259 |
+ ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF); |
|
260 |
+ ctx->rc.range <<= 8; |
|
261 |
+ } |
|
262 |
+} |
|
263 |
+ |
|
264 |
+/** |
|
265 |
+ * Calculate culmulative frequency for next symbol. Does NO update! |
|
266 |
+ * @param tot_f is the total frequency or (code_value)1<<shift |
|
267 |
+ * @return the culmulative frequency |
|
268 |
+ */ |
|
269 |
+static inline int range_decode_culfreq(APEContext * ctx, int tot_f) |
|
270 |
+{ |
|
271 |
+ range_dec_normalize(ctx); |
|
272 |
+ ctx->rc.help = ctx->rc.range / tot_f; |
|
273 |
+ return ctx->rc.low / ctx->rc.help; |
|
274 |
+} |
|
275 |
+ |
|
276 |
+/** |
|
277 |
+ * Decode value with given size in bits |
|
278 |
+ * @param shift number of bits to decode |
|
279 |
+ */ |
|
280 |
+static inline int range_decode_culshift(APEContext * ctx, int shift) |
|
281 |
+{ |
|
282 |
+ range_dec_normalize(ctx); |
|
283 |
+ ctx->rc.help = ctx->rc.range >> shift; |
|
284 |
+ return ctx->rc.low / ctx->rc.help; |
|
285 |
+} |
|
286 |
+ |
|
287 |
+ |
|
288 |
+/** |
|
289 |
+ * Update decoding state |
|
290 |
+ * @param sy_f the interval length (frequency of the symbol) |
|
291 |
+ * @param lt_f the lower end (frequency sum of < symbols) |
|
292 |
+ */ |
|
293 |
+static inline void range_decode_update(APEContext * ctx, int sy_f, int lt_f) |
|
294 |
+{ |
|
295 |
+ ctx->rc.low -= ctx->rc.help * lt_f; |
|
296 |
+ ctx->rc.range = ctx->rc.help * sy_f; |
|
297 |
+} |
|
298 |
+ |
|
299 |
+/** Decode n bits (n <= 16) without modelling */ |
|
300 |
+static inline int range_decode_bits(APEContext * ctx, int n) |
|
301 |
+{ |
|
302 |
+ int sym = range_decode_culshift(ctx, n); |
|
303 |
+ range_decode_update(ctx, 1, sym); |
|
304 |
+ return sym; |
|
305 |
+} |
|
306 |
+ |
|
307 |
+ |
|
308 |
+#define MODEL_ELEMENTS 64 |
|
309 |
+ |
|
310 |
+/** |
|
311 |
+ * Fixed probabilities for symbols in Monkey Audio version 3.97 |
|
312 |
+ */ |
|
313 |
+static const uint32_t counts_3970[65] = { |
|
314 |
+ 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926, |
|
315 |
+ 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419, |
|
316 |
+ 65450, 65469, 65480, 65487, 65491, 65493, 65494, 65495, |
|
317 |
+ 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, |
|
318 |
+ 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, |
|
319 |
+ 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, |
|
320 |
+ 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, |
|
321 |
+ 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535, |
|
322 |
+ 65536 |
|
323 |
+}; |
|
324 |
+ |
|
325 |
+/** |
|
326 |
+ * Probability ranges for symbols in Monkey Audio version 3.97 |
|
327 |
+ */ |
|
328 |
+static const uint16_t counts_diff_3970[64] = { |
|
329 |
+ 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756, |
|
330 |
+ 1104, 677, 415, 248, 150, 89, 54, 31, |
|
331 |
+ 19, 11, 7, 4, 2, 1, 1, 1, |
|
332 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
333 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
334 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
335 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
336 |
+ 1, 1, 1, 1, 1, 1, 1, 1 |
|
337 |
+}; |
|
338 |
+ |
|
339 |
+/** |
|
340 |
+ * Fixed probabilities for symbols in Monkey Audio version 3.98 |
|
341 |
+ */ |
|
342 |
+static const uint32_t counts_3980[65] = { |
|
343 |
+ 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435, |
|
344 |
+ 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482, |
|
345 |
+ 65485, 65488, 65490, 65491, 65492, 65493, 65494, 65495, |
|
346 |
+ 65496, 65497, 65498, 65499, 65500, 65501, 65502, 65503, |
|
347 |
+ 65504, 65505, 65506, 65507, 65508, 65509, 65510, 65511, |
|
348 |
+ 65512, 65513, 65514, 65515, 65516, 65517, 65518, 65519, |
|
349 |
+ 65520, 65521, 65522, 65523, 65524, 65525, 65526, 65527, |
|
350 |
+ 65528, 65529, 65530, 65531, 65532, 65533, 65534, 65535, |
|
351 |
+ 65536 |
|
352 |
+}; |
|
353 |
+ |
|
354 |
+/** |
|
355 |
+ * Probability ranges for symbols in Monkey Audio version 3.98 |
|
356 |
+ */ |
|
357 |
+static const uint16_t counts_diff_3980[64] = { |
|
358 |
+ 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536, |
|
359 |
+ 261, 119, 65, 31, 19, 10, 6, 3, |
|
360 |
+ 3, 2, 1, 1, 1, 1, 1, 1, |
|
361 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
362 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
363 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
364 |
+ 1, 1, 1, 1, 1, 1, 1, 1, |
|
365 |
+ 1, 1, 1, 1, 1, 1, 1, 1 |
|
366 |
+}; |
|
367 |
+ |
|
368 |
+/** |
|
369 |
+ * Decode symbol |
|
370 |
+ * @param counts probability range start position |
|
371 |
+ * @param count_diffs probability range widths |
|
372 |
+ */ |
|
373 |
+static inline int range_get_symbol(APEContext * ctx, |
|
374 |
+ const uint32_t counts[], |
|
375 |
+ const uint16_t counts_diff[]) |
|
376 |
+{ |
|
377 |
+ int symbol, cf; |
|
378 |
+ |
|
379 |
+ cf = range_decode_culshift(ctx, 16); |
|
380 |
+ |
|
381 |
+ /* figure out the symbol inefficiently; a binary search would be much better */ |
|
382 |
+ for (symbol = 0; counts[symbol + 1] <= cf; symbol++); |
|
383 |
+ |
|
384 |
+ range_decode_update(ctx, counts_diff[symbol], counts[symbol]); |
|
385 |
+ |
|
386 |
+ return symbol; |
|
387 |
+} |
|
388 |
+/** @} */ // group rangecoder |
|
389 |
+ |
|
390 |
+static inline void update_rice(APERice *rice, int x) |
|
391 |
+{ |
|
392 |
+ rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5); |
|
393 |
+ |
|
394 |
+ if (rice->k == 0) |
|
395 |
+ rice->k = 1; |
|
396 |
+ else if (rice->ksum < (1 << (rice->k + 4))) |
|
397 |
+ rice->k--; |
|
398 |
+ else if (rice->ksum >= (1 << (rice->k + 5))) |
|
399 |
+ rice->k++; |
|
400 |
+} |
|
401 |
+ |
|
402 |
+static inline int ape_decode_value(APEContext * ctx, APERice *rice) |
|
403 |
+{ |
|
404 |
+ int x, overflow; |
|
405 |
+ |
|
406 |
+ if (ctx->fileversion < 3980) { |
|
407 |
+ int tmpk; |
|
408 |
+ |
|
409 |
+ overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970); |
|
410 |
+ |
|
411 |
+ if (overflow == (MODEL_ELEMENTS - 1)) { |
|
412 |
+ tmpk = range_decode_bits(ctx, 5); |
|
413 |
+ overflow = 0; |
|
414 |
+ } else |
|
415 |
+ tmpk = (rice->k < 1) ? 0 : rice->k - 1; |
|
416 |
+ |
|
417 |
+ if (tmpk <= 16) |
|
418 |
+ x = range_decode_bits(ctx, tmpk); |
|
419 |
+ else { |
|
420 |
+ x = range_decode_bits(ctx, 16); |
|
421 |
+ x |= (range_decode_bits(ctx, tmpk - 16) << 16); |
|
422 |
+ } |
|
423 |
+ x += overflow << tmpk; |
|
424 |
+ } else { |
|
425 |
+ int base, pivot; |
|
426 |
+ |
|
427 |
+ pivot = rice->ksum >> 5; |
|
428 |
+ if (pivot == 0) |
|
429 |
+ pivot = 1; |
|
430 |
+ |
|
431 |
+ overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980); |
|
432 |
+ |
|
433 |
+ if (overflow == (MODEL_ELEMENTS - 1)) { |
|
434 |
+ overflow = range_decode_bits(ctx, 16) << 16; |
|
435 |
+ overflow |= range_decode_bits(ctx, 16); |
|
436 |
+ } |
|
437 |
+ |
|
438 |
+ base = range_decode_culfreq(ctx, pivot); |
|
439 |
+ range_decode_update(ctx, 1, base); |
|
440 |
+ |
|
441 |
+ x = base + overflow * pivot; |
|
442 |
+ } |
|
443 |
+ |
|
444 |
+ update_rice(rice, x); |
|
445 |
+ |
|
446 |
+ /* Convert to signed */ |
|
447 |
+ if (x & 1) |
|
448 |
+ return (x >> 1) + 1; |
|
449 |
+ else |
|
450 |
+ return -(x >> 1); |
|
451 |
+} |
|
452 |
+ |
|
453 |
+static void entropy_decode(APEContext * ctx, int blockstodecode, int stereo) |
|
454 |
+{ |
|
455 |
+ int32_t *decoded0 = ctx->decoded0; |
|
456 |
+ int32_t *decoded1 = ctx->decoded1; |
|
457 |
+ |
|
458 |
+ ctx->blocksdecoded = blockstodecode; |
|
459 |
+ |
|
460 |
+ if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { |
|
461 |
+ /* We are pure silence, just memset the output buffer. */ |
|
462 |
+ memset(decoded0, 0, blockstodecode * sizeof(int32_t)); |
|
463 |
+ memset(decoded1, 0, blockstodecode * sizeof(int32_t)); |
|
464 |
+ } else { |
|
465 |
+ while (blockstodecode--) { |
|
466 |
+ *decoded0++ = ape_decode_value(ctx, &ctx->riceY); |
|
467 |
+ if (stereo) |
|
468 |
+ *decoded1++ = ape_decode_value(ctx, &ctx->riceX); |
|
469 |
+ } |
|
470 |
+ } |
|
471 |
+ |
|
472 |
+ if (ctx->blocksdecoded == ctx->currentframeblocks) |
|
473 |
+ range_dec_normalize(ctx); /* normalize to use up all bytes */ |
|
474 |
+} |
|
475 |
+ |
|
476 |
+static void init_entropy_decoder(APEContext * ctx) |
|
477 |
+{ |
|
478 |
+ /* Read the CRC */ |
|
479 |
+ ctx->CRC = bytestream_get_be32(&ctx->ptr); |
|
480 |
+ |
|
481 |
+ /* Read the frame flags if they exist */ |
|
482 |
+ ctx->frameflags = 0; |
|
483 |
+ if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) { |
|
484 |
+ ctx->CRC &= ~0x80000000; |
|
485 |
+ |
|
486 |
+ ctx->frameflags = bytestream_get_be32(&ctx->ptr); |
|
487 |
+ } |
|
488 |
+ |
|
489 |
+ /* Keep a count of the blocks decoded in this frame */ |
|
490 |
+ ctx->blocksdecoded = 0; |
|
491 |
+ |
|
492 |
+ /* Initialise the rice structs */ |
|
493 |
+ ctx->riceX.k = 10; |
|
494 |
+ ctx->riceX.ksum = (1 << ctx->riceX.k) * 16; |
|
495 |
+ ctx->riceY.k = 10; |
|
496 |
+ ctx->riceY.ksum = (1 << ctx->riceY.k) * 16; |
|
497 |
+ |
|
498 |
+ /* The first 8 bits of input are ignored. */ |
|
499 |
+ ctx->ptr++; |
|
500 |
+ |
|
501 |
+ range_start_decoding(ctx); |
|
502 |
+} |
|
503 |
+ |
|
504 |
+static const int32_t initial_coeffs[4] = { |
|
505 |
+ 360, 317, -109, 98 |
|
506 |
+}; |
|
507 |
+ |
|
508 |
+static void init_predictor_decoder(APEContext * ctx) |
|
509 |
+{ |
|
510 |
+ APEPredictor *p = &ctx->predictor; |
|
511 |
+ |
|
512 |
+ /* Zero the history buffers */ |
|
513 |
+ memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(int32_t)); |
|
514 |
+ p->buf = p->historybuffer; |
|
515 |
+ |
|
516 |
+ /* Initialise and zero the co-efficients */ |
|
517 |
+ memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs)); |
|
518 |
+ memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs)); |
|
519 |
+ memset(p->coeffsB, 0, sizeof(p->coeffsB)); |
|
520 |
+ |
|
521 |
+ p->filterA[0] = p->filterA[1] = 0; |
|
522 |
+ p->filterB[0] = p->filterB[1] = 0; |
|
523 |
+ p->lastA[0] = p->lastA[1] = 0; |
|
524 |
+} |
|
525 |
+ |
|
526 |
+/** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */ |
|
527 |
+static inline int APESIGN(int32_t x) { |
|
528 |
+ return (x < 0) - (x > 0); |
|
529 |
+} |
|
530 |
+ |
|
531 |
+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) |
|
532 |
+{ |
|
533 |
+ int32_t predictionA, predictionB; |
|
534 |
+ |
|
535 |
+ p->buf[delayA] = p->lastA[filter]; |
|
536 |
+ p->buf[adaptA] = APESIGN(p->buf[delayA]); |
|
537 |
+ p->buf[delayA - 1] = p->buf[delayA] - p->buf[delayA - 1]; |
|
538 |
+ p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]); |
|
539 |
+ |
|
540 |
+ predictionA = p->buf[delayA ] * p->coeffsA[filter][0] + |
|
541 |
+ p->buf[delayA - 1] * p->coeffsA[filter][1] + |
|
542 |
+ p->buf[delayA - 2] * p->coeffsA[filter][2] + |
|
543 |
+ p->buf[delayA - 3] * p->coeffsA[filter][3]; |
|
544 |
+ |
|
545 |
+ /* Apply a scaled first-order filter compression */ |
|
546 |
+ p->buf[delayB] = p->filterA[filter ^ 1] - ((p->filterB[filter] * 31) >> 5); |
|
547 |
+ p->buf[adaptB] = APESIGN(p->buf[delayB]); |
|
548 |
+ p->buf[delayB - 1] = p->buf[delayB] - p->buf[delayB - 1]; |
|
549 |
+ p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]); |
|
550 |
+ p->filterB[filter] = p->filterA[filter ^ 1]; |
|
551 |
+ |
|
552 |
+ predictionB = p->buf[delayB ] * p->coeffsB[filter][0] + |
|
553 |
+ p->buf[delayB - 1] * p->coeffsB[filter][1] + |
|
554 |
+ p->buf[delayB - 2] * p->coeffsB[filter][2] + |
|
555 |
+ p->buf[delayB - 3] * p->coeffsB[filter][3] + |
|
556 |
+ p->buf[delayB - 4] * p->coeffsB[filter][4]; |
|
557 |
+ |
|
558 |
+ p->lastA[filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10); |
|
559 |
+ p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5); |
|
560 |
+ |
|
561 |
+ if (!decoded) // no need updating filter coefficients |
|
562 |
+ return p->filterA[filter]; |
|
563 |
+ |
|
564 |
+ if (decoded > 0) { |
|
565 |
+ p->coeffsA[filter][0] -= p->buf[adaptA ]; |
|
566 |
+ p->coeffsA[filter][1] -= p->buf[adaptA - 1]; |
|
567 |
+ p->coeffsA[filter][2] -= p->buf[adaptA - 2]; |
|
568 |
+ p->coeffsA[filter][3] -= p->buf[adaptA - 3]; |
|
569 |
+ |
|
570 |
+ p->coeffsB[filter][0] -= p->buf[adaptB ]; |
|
571 |
+ p->coeffsB[filter][1] -= p->buf[adaptB - 1]; |
|
572 |
+ p->coeffsB[filter][2] -= p->buf[adaptB - 2]; |
|
573 |
+ p->coeffsB[filter][3] -= p->buf[adaptB - 3]; |
|
574 |
+ p->coeffsB[filter][4] -= p->buf[adaptB - 4]; |
|
575 |
+ } else { |
|
576 |
+ p->coeffsA[filter][0] += p->buf[adaptA ]; |
|
577 |
+ p->coeffsA[filter][1] += p->buf[adaptA - 1]; |
|
578 |
+ p->coeffsA[filter][2] += p->buf[adaptA - 2]; |
|
579 |
+ p->coeffsA[filter][3] += p->buf[adaptA - 3]; |
|
580 |
+ |
|
581 |
+ p->coeffsB[filter][0] += p->buf[adaptB ]; |
|
582 |
+ p->coeffsB[filter][1] += p->buf[adaptB - 1]; |
|
583 |
+ p->coeffsB[filter][2] += p->buf[adaptB - 2]; |
|
584 |
+ p->coeffsB[filter][3] += p->buf[adaptB - 3]; |
|
585 |
+ p->coeffsB[filter][4] += p->buf[adaptB - 4]; |
|
586 |
+ } |
|
587 |
+ return p->filterA[filter]; |
|
588 |
+} |
|
589 |
+ |
|
590 |
+static void predictor_decode_stereo(APEContext * ctx, int count) |
|
591 |
+{ |
|
592 |
+ int32_t predictionA, predictionB; |
|
593 |
+ APEPredictor *p = &ctx->predictor; |
|
594 |
+ int32_t *decoded0 = ctx->decoded0; |
|
595 |
+ int32_t *decoded1 = ctx->decoded1; |
|
596 |
+ |
|
597 |
+ while (count--) { |
|
598 |
+ /* Predictor Y */ |
|
599 |
+ predictionA = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, YADAPTCOEFFSA, YADAPTCOEFFSB); |
|
600 |
+ predictionB = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, XADAPTCOEFFSA, XADAPTCOEFFSB); |
|
601 |
+ *(decoded0++) = predictionA; |
|
602 |
+ *(decoded1++) = predictionB; |
|
603 |
+ |
|
604 |
+ /* Combined */ |
|
605 |
+ p->buf++; |
|
606 |
+ |
|
607 |
+ /* Have we filled the history buffer? */ |
|
608 |
+ if (p->buf == p->historybuffer + HISTORY_SIZE) { |
|
609 |
+ memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t)); |
|
610 |
+ p->buf = p->historybuffer; |
|
611 |
+ } |
|
612 |
+ } |
|
613 |
+} |
|
614 |
+ |
|
615 |
+static void predictor_decode_mono(APEContext * ctx, int count) |
|
616 |
+{ |
|
617 |
+ APEPredictor *p = &ctx->predictor; |
|
618 |
+ int32_t *decoded0 = ctx->decoded0; |
|
619 |
+ int32_t predictionA, currentA, A; |
|
620 |
+ |
|
621 |
+ currentA = p->lastA[0]; |
|
622 |
+ |
|
623 |
+ while (count--) { |
|
624 |
+ A = *decoded0; |
|
625 |
+ |
|
626 |
+ p->buf[YDELAYA] = currentA; |
|
627 |
+ p->buf[YDELAYA - 1] = p->buf[YDELAYA] - p->buf[YDELAYA - 1]; |
|
628 |
+ |
|
629 |
+ predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] + |
|
630 |
+ p->buf[YDELAYA - 1] * p->coeffsA[0][1] + |
|
631 |
+ p->buf[YDELAYA - 2] * p->coeffsA[0][2] + |
|
632 |
+ p->buf[YDELAYA - 3] * p->coeffsA[0][3]; |
|
633 |
+ |
|
634 |
+ currentA = A + (predictionA >> 10); |
|
635 |
+ |
|
636 |
+ p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]); |
|
637 |
+ p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]); |
|
638 |
+ |
|
639 |
+ if (A > 0) { |
|
640 |
+ p->coeffsA[0][0] -= p->buf[YADAPTCOEFFSA ]; |
|
641 |
+ p->coeffsA[0][1] -= p->buf[YADAPTCOEFFSA - 1]; |
|
642 |
+ p->coeffsA[0][2] -= p->buf[YADAPTCOEFFSA - 2]; |
|
643 |
+ p->coeffsA[0][3] -= p->buf[YADAPTCOEFFSA - 3]; |
|
644 |
+ } else if (A < 0) { |
|
645 |
+ p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ]; |
|
646 |
+ p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1]; |
|
647 |
+ p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2]; |
|
648 |
+ p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3]; |
|
649 |
+ } |
|
650 |
+ |
|
651 |
+ p->buf++; |
|
652 |
+ |
|
653 |
+ /* Have we filled the history buffer? */ |
|
654 |
+ if (p->buf == p->historybuffer + HISTORY_SIZE) { |
|
655 |
+ memmove(p->historybuffer, p->buf, PREDICTOR_SIZE * sizeof(int32_t)); |
|
656 |
+ p->buf = p->historybuffer; |
|
657 |
+ } |
|
658 |
+ |
|
659 |
+ p->filterA[0] = currentA + ((p->filterA[0] * 31) >> 5); |
|
660 |
+ *(decoded0++) = p->filterA[0]; |
|
661 |
+ } |
|
662 |
+ |
|
663 |
+ p->lastA[0] = currentA; |
|
664 |
+} |
|
665 |
+ |
|
666 |
+static void do_init_filter(APEFilter *f, int16_t * buf, int order) |
|
667 |
+{ |
|
668 |
+ f->coeffs = buf; |
|
669 |
+ f->historybuffer = buf + order; |
|
670 |
+ f->delay = f->historybuffer + order * 2; |
|
671 |
+ f->adaptcoeffs = f->historybuffer + order; |
|
672 |
+ |
|
673 |
+ memset(f->historybuffer, 0, (order * 2) * sizeof(int16_t)); |
|
674 |
+ memset(f->coeffs, 0, order * sizeof(int16_t)); |
|
675 |
+ f->avg = 0; |
|
676 |
+} |
|
677 |
+ |
|
678 |
+static void init_filter(APEContext * ctx, APEFilter *f, int16_t * buf, int order) |
|
679 |
+{ |
|
680 |
+ do_init_filter(&f[0], buf, order); |
|
681 |
+ do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order); |
|
682 |
+} |
|
683 |
+ |
|
684 |
+static inline void do_apply_filter(int version, APEFilter *f, int32_t *data, int count, int order, int fracbits) |
|
685 |
+{ |
|
686 |
+ int res; |
|
687 |
+ int absres; |
|
688 |
+ |
|
689 |
+ while (count--) { |
|
690 |
+ /* round fixedpoint scalar product */ |
|
691 |
+ res = (scalarproduct(f->delay - order, f->coeffs, order) + (1 << (fracbits - 1))) >> fracbits; |
|
692 |
+ |
|
693 |
+ if (*data < 0) |
|
694 |
+ vector_add(f->coeffs, f->adaptcoeffs - order, order); |
|
695 |
+ else if (*data > 0) |
|
696 |
+ vector_sub(f->coeffs, f->adaptcoeffs - order, order); |
|
697 |
+ |
|
698 |
+ res += *data; |
|
699 |
+ |
|
700 |
+ *data++ = res; |
|
701 |
+ |
|
702 |
+ /* Update the output history */ |
|
703 |
+ *f->delay++ = av_clip_int16(res); |
|
704 |
+ |
|
705 |
+ if (version < 3980) { |
|
706 |
+ /* Version ??? to < 3.98 files (untested) */ |
|
707 |
+ f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4; |
|
708 |
+ f->adaptcoeffs[-4] >>= 1; |
|
709 |
+ f->adaptcoeffs[-8] >>= 1; |
|
710 |
+ } else { |
|
711 |
+ /* Version 3.98 and later files */ |
|
712 |
+ |
|
713 |
+ /* Update the adaption coefficients */ |
|
714 |
+ absres = (res < 0 ? -res : res); |
|
715 |
+ |
|
716 |
+ if (absres > (f->avg * 3)) |
|
717 |
+ *f->adaptcoeffs = ((res >> 25) & 64) - 32; |
|
718 |
+ else if (absres > (f->avg * 4) / 3) |
|
719 |
+ *f->adaptcoeffs = ((res >> 26) & 32) - 16; |
|
720 |
+ else if (absres > 0) |
|
721 |
+ *f->adaptcoeffs = ((res >> 27) & 16) - 8; |
|
722 |
+ else |
|
723 |
+ *f->adaptcoeffs = 0; |
|
724 |
+ |
|
725 |
+ f->avg += (absres - f->avg) / 16; |
|
726 |
+ |
|
727 |
+ f->adaptcoeffs[-1] >>= 1; |
|
728 |
+ f->adaptcoeffs[-2] >>= 1; |
|
729 |
+ f->adaptcoeffs[-8] >>= 1; |
|
730 |
+ } |
|
731 |
+ |
|
732 |
+ f->adaptcoeffs++; |
|
733 |
+ |
|
734 |
+ /* Have we filled the history buffer? */ |
|
735 |
+ if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) { |
|
736 |
+ memmove(f->historybuffer, f->delay - (order * 2), |
|
737 |
+ (order * 2) * sizeof(int16_t)); |
|
738 |
+ f->delay = f->historybuffer + order * 2; |
|
739 |
+ f->adaptcoeffs = f->historybuffer + order; |
|
740 |
+ } |
|
741 |
+ } |
|
742 |
+} |
|
743 |
+ |
|
744 |
+static void apply_filter(APEContext * ctx, APEFilter *f, |
|
745 |
+ int32_t * data0, int32_t * data1, |
|
746 |
+ int count, int order, int fracbits) |
|
747 |
+{ |
|
748 |
+ do_apply_filter(ctx->fileversion, &f[0], data0, count, order, fracbits); |
|
749 |
+ if (data1) |
|
750 |
+ do_apply_filter(ctx->fileversion, &f[1], data1, count, order, fracbits); |
|
751 |
+} |
|
752 |
+ |
|
753 |
+static void ape_apply_filters(APEContext * ctx, int32_t * decoded0, |
|
754 |
+ int32_t * decoded1, int count) |
|
755 |
+{ |
|
756 |
+ int i; |
|
757 |
+ |
|
758 |
+ for (i = 0; i < APE_FILTER_LEVELS; i++) { |
|
759 |
+ if (!ape_filter_orders[ctx->fset][i]) |
|
760 |
+ break; |
|
761 |
+ apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, ape_filter_orders[ctx->fset][i], ape_filter_fracbits[ctx->fset][i]); |
|
762 |
+ } |
|
763 |
+} |
|
764 |
+ |
|
765 |
+static void init_frame_decoder(APEContext * ctx) |
|
766 |
+{ |
|
767 |
+ int i; |
|
768 |
+ init_entropy_decoder(ctx); |
|
769 |
+ init_predictor_decoder(ctx); |
|
770 |
+ |
|
771 |
+ for (i = 0; i < APE_FILTER_LEVELS; i++) { |
|
772 |
+ if (!ape_filter_orders[ctx->fset][i]) |
|
773 |
+ break; |
|
774 |
+ init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], ape_filter_orders[ctx->fset][i]); |
|
775 |
+ } |
|
776 |
+} |
|
777 |
+ |
|
778 |
+static void ape_unpack_mono(APEContext * ctx, int count) |
|
779 |
+{ |
|
780 |
+ int32_t left; |
|
781 |
+ int32_t *decoded0 = ctx->decoded0; |
|
782 |
+ int32_t *decoded1 = ctx->decoded1; |
|
783 |
+ |
|
784 |
+ if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { |
|
785 |
+ entropy_decode(ctx, count, 0); |
|
786 |
+ /* We are pure silence, so we're done. */ |
|
787 |
+ av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n"); |
|
788 |
+ return; |
|
789 |
+ } |
|
790 |
+ |
|
791 |
+ entropy_decode(ctx, count, 0); |
|
792 |
+ ape_apply_filters(ctx, decoded0, NULL, count); |
|
793 |
+ |
|
794 |
+ /* Now apply the predictor decoding */ |
|
795 |
+ predictor_decode_mono(ctx, count); |
|
796 |
+ |
|
797 |
+ /* Pseudo-stereo - just copy left channel to right channel */ |
|
798 |
+ if (ctx->channels == 2) { |
|
799 |
+ while (count--) { |
|
800 |
+ left = *decoded0; |
|
801 |
+ *(decoded1++) = *(decoded0++) = left; |
|
802 |
+ } |
|
803 |
+ } |
|
804 |
+} |
|
805 |
+ |
|
806 |
+static void ape_unpack_stereo(APEContext * ctx, int count) |
|
807 |
+{ |
|
808 |
+ int32_t left, right; |
|
809 |
+ int32_t *decoded0 = ctx->decoded0; |
|
810 |
+ int32_t *decoded1 = ctx->decoded1; |
|
811 |
+ |
|
812 |
+ if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { |
|
813 |
+ /* We are pure silence, so we're done. */ |
|
814 |
+ av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n"); |
|
815 |
+ return; |
|
816 |
+ } |
|
817 |
+ |
|
818 |
+ entropy_decode(ctx, count, 1); |
|
819 |
+ ape_apply_filters(ctx, decoded0, decoded1, count); |
|
820 |
+ |
|
821 |
+ /* Now apply the predictor decoding */ |
|
822 |
+ predictor_decode_stereo(ctx, count); |
|
823 |
+ |
|
824 |
+ /* Decorrelate and scale to output depth */ |
|
825 |
+ while (count--) { |
|
826 |
+ left = *decoded1 - (*decoded0 / 2); |
|
827 |
+ right = left + *decoded0; |
|
828 |
+ |
|
829 |
+ *(decoded0++) = left; |
|
830 |
+ *(decoded1++) = right; |
|
831 |
+ } |
|
832 |
+} |
|
833 |
+ |
|
834 |
+static int ape_decode_frame(AVCodecContext * avctx, |
|
835 |
+ void *data, int *data_size, |
|
836 |
+ uint8_t * buf, int buf_size) |
|
837 |
+{ |
|
838 |
+ APEContext *s = avctx->priv_data; |
|
839 |
+ int16_t *samples = data; |
|
840 |
+ int nblocks; |
|
841 |
+ int i, n; |
|
842 |
+ int blockstodecode; |
|
843 |
+ int bytes_used; |
|
844 |
+ |
|
845 |
+ if (buf_size == 0 && !s->samples) { |
|
846 |
+ *data_size = 0; |
|
847 |
+ return 0; |
|
848 |
+ } |
|
849 |
+ |
|
850 |
+ /* should not happen but who knows */ |
|
851 |
+ if (BLOCKS_PER_LOOP * 2 * avctx->channels > *data_size) { |
|
852 |
+ 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); |
|
853 |
+ return -1; |
|
854 |
+ } |
|
855 |
+ |
|
856 |
+ if(!s->samples){ |
|
857 |
+ s->data = av_realloc(s->data, (buf_size + 3) & ~3); |
|
858 |
+ s->dsp.bswap_buf(s->data, buf, buf_size >> 2); |
|
859 |
+ s->ptr = s->last_ptr = s->data; |
|
860 |
+ s->data_end = s->data + buf_size; |
|
861 |
+ |
|
862 |
+ nblocks = s->samples = bytestream_get_be32(&s->ptr); |
|
863 |
+ n = bytestream_get_be32(&s->ptr); |
|
864 |
+ if(n < 0 || n > 3){ |
|
865 |
+ av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n"); |
|
866 |
+ s->data = NULL; |
|
867 |
+ return -1; |
|
868 |
+ } |
|
869 |
+ s->ptr += n; |
|
870 |
+ |
|
871 |
+ s->currentframeblocks = nblocks; |
|
872 |
+ buf += 4; |
|
873 |
+ if (s->samples <= 0) { |
|
874 |
+ *data_size = 0; |
|
875 |
+ return buf_size; |
|
876 |
+ } |
|
877 |
+ |
|
878 |
+ memset(s->decoded0, 0, sizeof(s->decoded0)); |
|
879 |
+ memset(s->decoded1, 0, sizeof(s->decoded1)); |
|
880 |
+ |
|
881 |
+ /* Initialize the frame decoder */ |
|
882 |
+ init_frame_decoder(s); |
|
883 |
+ } |
|
884 |
+ |
|
885 |
+ if (!s->data) { |
|
886 |
+ *data_size = 0; |
|
887 |
+ return buf_size; |
|
888 |
+ } |
|
889 |
+ |
|
890 |
+ nblocks = s->samples; |
|
891 |
+ blockstodecode = FFMIN(BLOCKS_PER_LOOP, nblocks); |
|
892 |
+ |
|
893 |
+ if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO)) |
|
894 |
+ ape_unpack_mono(s, blockstodecode); |
|
895 |
+ else |
|
896 |
+ ape_unpack_stereo(s, blockstodecode); |
|
897 |
+ |
|
898 |
+ for (i = 0; i < blockstodecode; i++) { |
|
899 |
+ *samples++ = s->decoded0[i]; |
|
900 |
+ if(s->channels == 2) |
|
901 |
+ *samples++ = s->decoded1[i]; |
|
902 |
+ } |
|
903 |
+ |
|
904 |
+ s->samples -= blockstodecode; |
|
905 |
+ |
|
906 |
+ *data_size = blockstodecode * 2 * s->channels; |
|
907 |
+ bytes_used = s->samples ? s->ptr - s->last_ptr : buf_size; |
|
908 |
+ s->last_ptr = s->ptr; |
|
909 |
+ return bytes_used; |
|
910 |
+} |
|
911 |
+ |
|
912 |
+AVCodec ape_decoder = { |
|
913 |
+ "ape", |
|
914 |
+ CODEC_TYPE_AUDIO, |
|
915 |
+ CODEC_ID_APE, |
|
916 |
+ sizeof(APEContext), |
|
917 |
+ ape_decode_init, |
|
918 |
+ NULL, |
|
919 |
+ ape_decode_close, |
|
920 |
+ ape_decode_frame, |
|
921 |
+}; |
... | ... |
@@ -33,8 +33,8 @@ |
33 | 33 |
#define AV_STRINGIFY(s) AV_TOSTRING(s) |
34 | 34 |
#define AV_TOSTRING(s) #s |
35 | 35 |
|
36 |
-#define LIBAVCODEC_VERSION_INT ((51<<16)+(43<<8)+0) |
|
37 |
-#define LIBAVCODEC_VERSION 51.43.0 |
|
36 |
+#define LIBAVCODEC_VERSION_INT ((51<<16)+(44<<8)+0) |
|
37 |
+#define LIBAVCODEC_VERSION 51.44.0 |
|
38 | 38 |
#define LIBAVCODEC_BUILD LIBAVCODEC_VERSION_INT |
39 | 39 |
|
40 | 40 |
#define LIBAVCODEC_IDENT "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION) |
... | ... |
@@ -260,6 +260,7 @@ enum CodecID { |
260 | 260 |
CODEC_ID_GSM_MS, /* as found in WAV */ |
261 | 261 |
CODEC_ID_ATRAC3, |
262 | 262 |
CODEC_ID_VOXWARE, |
263 |
+ CODEC_ID_APE, |
|
263 | 264 |
|
264 | 265 |
/* subtitle codecs */ |
265 | 266 |
CODEC_ID_DVD_SUBTITLE= 0x17000, |
... | ... |
@@ -20,6 +20,7 @@ OBJS-$(CONFIG_AIFF_MUXER) += aiff.o riff.o |
20 | 20 |
OBJS-$(CONFIG_AMR_DEMUXER) += amr.o |
21 | 21 |
OBJS-$(CONFIG_AMR_MUXER) += amr.o |
22 | 22 |
OBJS-$(CONFIG_APC_DEMUXER) += apc.o |
23 |
+OBJS-$(CONFIG_APE_DEMUXER) += ape.o |
|
23 | 24 |
OBJS-$(CONFIG_ASF_DEMUXER) += asf.o riff.o |
24 | 25 |
OBJS-$(CONFIG_ASF_MUXER) += asf-enc.o riff.o |
25 | 26 |
OBJS-$(CONFIG_ASF_STREAM_MUXER) += asf-enc.o riff.o |
... | ... |
@@ -53,6 +53,7 @@ void av_register_all(void) |
53 | 53 |
REGISTER_MUXDEMUX (AIFF, aiff); |
54 | 54 |
REGISTER_MUXDEMUX (AMR, amr); |
55 | 55 |
REGISTER_DEMUXER (APC, apc); |
56 |
+ REGISTER_DEMUXER (APE, ape); |
|
56 | 57 |
REGISTER_MUXDEMUX (ASF, asf); |
57 | 58 |
REGISTER_MUXER (ASF_STREAM, asf_stream); |
58 | 59 |
REGISTER_MUXDEMUX (AU, au); |
... | ... |
@@ -29,6 +29,7 @@ extern AVInputFormat ac3_demuxer; |
29 | 29 |
extern AVInputFormat aiff_demuxer; |
30 | 30 |
extern AVInputFormat amr_demuxer; |
31 | 31 |
extern AVInputFormat apc_demuxer; |
32 |
+extern AVInputFormat ape_demuxer; |
|
32 | 33 |
extern AVInputFormat asf_demuxer; |
33 | 34 |
extern AVInputFormat au_demuxer; |
34 | 35 |
extern AVInputFormat audio_beos_demuxer; |
35 | 36 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,392 @@ |
0 |
+/* |
|
1 |
+ * Monkey's Audio APE demuxer |
|
2 |
+ * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org> |
|
3 |
+ * based upon libdemac from Dave Chapman. |
|
4 |
+ * |
|
5 |
+ * This file is part of FFmpeg. |
|
6 |
+ * |
|
7 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
8 |
+ * modify it under the terms of the GNU Lesser General Public |
|
9 |
+ * License as published by the Free Software Foundation; either |
|
10 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
11 |
+ * |
|
12 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
+ * Lesser General Public License for more details. |
|
16 |
+ * |
|
17 |
+ * You should have received a copy of the GNU Lesser General Public |
|
18 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
19 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
20 |
+ */ |
|
21 |
+ |
|
22 |
+#include <stdio.h> |
|
23 |
+ |
|
24 |
+#include "avformat.h" |
|
25 |
+ |
|
26 |
+/* The earliest and latest file formats supported by this library */ |
|
27 |
+#define APE_MIN_VERSION 3970 |
|
28 |
+#define APE_MAX_VERSION 3990 |
|
29 |
+ |
|
30 |
+#define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE] |
|
31 |
+#define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE] |
|
32 |
+#define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE] |
|
33 |
+#define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE] |
|
34 |
+#define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level |
|
35 |
+#define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored) |
|
36 |
+ |
|
37 |
+#define MAC_SUBFRAME_SIZE 4608 |
|
38 |
+ |
|
39 |
+#define APE_EXTRADATA_SIZE 6 |
|
40 |
+ |
|
41 |
+typedef struct { |
|
42 |
+ int64_t pos; |
|
43 |
+ int nblocks; |
|
44 |
+ int size; |
|
45 |
+ int skip; |
|
46 |
+ int64_t pts; |
|
47 |
+} APEFrame; |
|
48 |
+ |
|
49 |
+typedef struct { |
|
50 |
+ /* Derived fields */ |
|
51 |
+ uint32_t junklength; |
|
52 |
+ uint32_t firstframe; |
|
53 |
+ uint32_t totalsamples; |
|
54 |
+ int currentframe; |
|
55 |
+ APEFrame *frames; |
|
56 |
+ |
|
57 |
+ /* Info from Descriptor Block */ |
|
58 |
+ char magic[4]; |
|
59 |
+ int16_t fileversion; |
|
60 |
+ int16_t padding1; |
|
61 |
+ uint32_t descriptorlength; |
|
62 |
+ uint32_t headerlength; |
|
63 |
+ uint32_t seektablelength; |
|
64 |
+ uint32_t wavheaderlength; |
|
65 |
+ uint32_t audiodatalength; |
|
66 |
+ uint32_t audiodatalength_high; |
|
67 |
+ uint32_t wavtaillength; |
|
68 |
+ uint8_t md5[16]; |
|
69 |
+ |
|
70 |
+ /* Info from Header Block */ |
|
71 |
+ uint16_t compressiontype; |
|
72 |
+ uint16_t formatflags; |
|
73 |
+ uint32_t blocksperframe; |
|
74 |
+ uint32_t finalframeblocks; |
|
75 |
+ uint32_t totalframes; |
|
76 |
+ uint16_t bps; |
|
77 |
+ uint16_t channels; |
|
78 |
+ uint32_t samplerate; |
|
79 |
+ |
|
80 |
+ /* Seektable */ |
|
81 |
+ uint32_t *seektable; |
|
82 |
+} APEContext; |
|
83 |
+ |
|
84 |
+static int ape_probe(AVProbeData * p) |
|
85 |
+{ |
|
86 |
+ if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ') |
|
87 |
+ return AVPROBE_SCORE_MAX; |
|
88 |
+ |
|
89 |
+ return 0; |
|
90 |
+} |
|
91 |
+ |
|
92 |
+static void ape_dumpinfo(APEContext * ape_ctx) |
|
93 |
+{ |
|
94 |
+ int i; |
|
95 |
+ |
|
96 |
+ av_log(NULL, AV_LOG_DEBUG, "Descriptor Block:\n\n"); |
|
97 |
+ av_log(NULL, AV_LOG_DEBUG, "magic = \"%c%c%c%c\"\n", ape_ctx->magic[0], ape_ctx->magic[1], ape_ctx->magic[2], ape_ctx->magic[3]); |
|
98 |
+ av_log(NULL, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion); |
|
99 |
+ av_log(NULL, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength); |
|
100 |
+ av_log(NULL, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength); |
|
101 |
+ av_log(NULL, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength); |
|
102 |
+ av_log(NULL, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength); |
|
103 |
+ av_log(NULL, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength); |
|
104 |
+ av_log(NULL, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high); |
|
105 |
+ av_log(NULL, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength); |
|
106 |
+ av_log(NULL, AV_LOG_DEBUG, "md5 = "); |
|
107 |
+ for (i = 0; i < 16; i++) |
|
108 |
+ av_log(NULL, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]); |
|
109 |
+ av_log(NULL, AV_LOG_DEBUG, "\n"); |
|
110 |
+ |
|
111 |
+ av_log(NULL, AV_LOG_DEBUG, "\nHeader Block:\n\n"); |
|
112 |
+ |
|
113 |
+ av_log(NULL, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype); |
|
114 |
+ av_log(NULL, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags); |
|
115 |
+ av_log(NULL, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe); |
|
116 |
+ av_log(NULL, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks); |
|
117 |
+ av_log(NULL, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes); |
|
118 |
+ av_log(NULL, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps); |
|
119 |
+ av_log(NULL, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels); |
|
120 |
+ av_log(NULL, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate); |
|
121 |
+ |
|
122 |
+ av_log(NULL, AV_LOG_DEBUG, "\nSeektable\n\n"); |
|
123 |
+ if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) { |
|
124 |
+ av_log(NULL, AV_LOG_DEBUG, "No seektable\n"); |
|
125 |
+ } else { |
|
126 |
+ for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) { |
|
127 |
+ if (i < ape_ctx->totalframes - 1) { |
|
128 |
+ av_log(NULL, AV_LOG_DEBUG, "%8d %d (%d bytes)\n", i, ape_ctx->seektable[i], ape_ctx->seektable[i + 1] - ape_ctx->seektable[i]); |
|
129 |
+ } else { |
|
130 |
+ av_log(NULL, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]); |
|
131 |
+ } |
|
132 |
+ } |
|
133 |
+ } |
|
134 |
+ |
|
135 |
+ av_log(NULL, AV_LOG_DEBUG, "\nFrames\n\n"); |
|
136 |
+ for (i = 0; i < ape_ctx->totalframes; i++) |
|
137 |
+ av_log(NULL, AV_LOG_DEBUG, "%8d %8lld %8d (%d samples)\n", i, ape_ctx->frames[i].pos, ape_ctx->frames[i].size, ape_ctx->frames[i].nblocks); |
|
138 |
+ |
|
139 |
+ av_log(NULL, AV_LOG_DEBUG, "\nCalculated information:\n\n"); |
|
140 |
+ av_log(NULL, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength); |
|
141 |
+ av_log(NULL, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe); |
|
142 |
+ av_log(NULL, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples); |
|
143 |
+} |
|
144 |
+ |
|
145 |
+static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap) |
|
146 |
+{ |
|
147 |
+ ByteIOContext *pb = &s->pb; |
|
148 |
+ APEContext *ape = s->priv_data; |
|
149 |
+ AVStream *st; |
|
150 |
+ uint32_t tag; |
|
151 |
+ int i; |
|
152 |
+ int total_blocks; |
|
153 |
+ int64_t pts; |
|
154 |
+ |
|
155 |
+ /* TODO: Skip any leading junk such as id3v2 tags */ |
|
156 |
+ ape->junklength = 0; |
|
157 |
+ |
|
158 |
+ tag = get_le32(pb); |
|
159 |
+ if (tag != MKTAG('M', 'A', 'C', ' ')) |
|
160 |
+ return -1; |
|
161 |
+ |
|
162 |
+ ape->fileversion = get_le16(pb); |
|
163 |
+ |
|
164 |
+ if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) { |
|
165 |
+ av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10); |
|
166 |
+ return -1; |
|
167 |
+ } |
|
168 |
+ |
|
169 |
+ if (ape->fileversion >= 3980) { |
|
170 |
+ ape->padding1 = get_le16(pb); |
|
171 |
+ ape->descriptorlength = get_le32(pb); |
|
172 |
+ ape->headerlength = get_le32(pb); |
|
173 |
+ ape->seektablelength = get_le32(pb); |
|
174 |
+ ape->wavheaderlength = get_le32(pb); |
|
175 |
+ ape->audiodatalength = get_le32(pb); |
|
176 |
+ ape->audiodatalength_high = get_le32(pb); |
|
177 |
+ ape->wavtaillength = get_le32(pb); |
|
178 |
+ get_buffer(pb, ape->md5, 16); |
|
179 |
+ |
|
180 |
+ /* Skip any unknown bytes at the end of the descriptor. |
|
181 |
+ This is for future compatibility */ |
|
182 |
+ if (ape->descriptorlength > 52) |
|
183 |
+ url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR); |
|
184 |
+ |
|
185 |
+ /* Read header data */ |
|
186 |
+ ape->compressiontype = get_le16(pb); |
|
187 |
+ ape->formatflags = get_le16(pb); |
|
188 |
+ ape->blocksperframe = get_le32(pb); |
|
189 |
+ ape->finalframeblocks = get_le32(pb); |
|
190 |
+ ape->totalframes = get_le32(pb); |
|
191 |
+ ape->bps = get_le16(pb); |
|
192 |
+ ape->channels = get_le16(pb); |
|
193 |
+ ape->samplerate = get_le32(pb); |
|
194 |
+ } else { |
|
195 |
+ ape->descriptorlength = 0; |
|
196 |
+ ape->headerlength = 32; |
|
197 |
+ |
|
198 |
+ ape->compressiontype = get_le16(pb); |
|
199 |
+ ape->formatflags = get_le16(pb); |
|
200 |
+ ape->channels = get_le16(pb); |
|
201 |
+ ape->samplerate = get_le32(pb); |
|
202 |
+ ape->wavheaderlength = get_le32(pb); |
|
203 |
+ ape->wavtaillength = get_le32(pb); |
|
204 |
+ ape->totalframes = get_le32(pb); |
|
205 |
+ ape->finalframeblocks = get_le32(pb); |
|
206 |
+ |
|
207 |
+ if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) { |
|
208 |
+ url_fseek(pb, 4, SEEK_CUR); /* Skip the peak level */ |
|
209 |
+ ape->headerlength += 4; |
|
210 |
+ } |
|
211 |
+ |
|
212 |
+ if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) { |
|
213 |
+ ape->seektablelength = get_le32(pb); |
|
214 |
+ ape->headerlength += 4; |
|
215 |
+ ape->seektablelength *= sizeof(int32_t); |
|
216 |
+ } else |
|
217 |
+ ape->seektablelength = ape->totalframes * sizeof(int32_t); |
|
218 |
+ |
|
219 |
+ if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT) |
|
220 |
+ ape->bps = 8; |
|
221 |
+ else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT) |
|
222 |
+ ape->bps = 24; |
|
223 |
+ else |
|
224 |
+ ape->bps = 16; |
|
225 |
+ |
|
226 |
+ if (ape->fileversion >= 3950) |
|
227 |
+ ape->blocksperframe = 73728 * 4; |
|
228 |
+ else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000)) |
|
229 |
+ ape->blocksperframe = 73728; |
|
230 |
+ else |
|
231 |
+ ape->blocksperframe = 9216; |
|
232 |
+ |
|
233 |
+ /* Skip any stored wav header */ |
|
234 |
+ if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER)) |
|
235 |
+ url_fskip(pb, ape->wavheaderlength); |
|
236 |
+ } |
|
237 |
+ |
|
238 |
+ if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){ |
|
239 |
+ av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes); |
|
240 |
+ return -1; |
|
241 |
+ } |
|
242 |
+ ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame)); |
|
243 |
+ if(!ape->frames) |
|
244 |
+ return AVERROR_NOMEM; |
|
245 |
+ ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength; |
|
246 |
+ ape->currentframe = 0; |
|
247 |
+ |
|
248 |
+ |
|
249 |
+ ape->totalsamples = ape->finalframeblocks; |
|
250 |
+ if (ape->totalframes > 1) |
|
251 |
+ ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1); |
|
252 |
+ |
|
253 |
+ if (ape->seektablelength > 0) { |
|
254 |
+ ape->seektable = av_malloc(ape->seektablelength); |
|
255 |
+ for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++) |
|
256 |
+ ape->seektable[i] = get_le32(pb); |
|
257 |
+ } |
|
258 |
+ |
|
259 |
+ ape->frames[0].pos = ape->firstframe; |
|
260 |
+ ape->frames[0].nblocks = ape->blocksperframe; |
|
261 |
+ ape->frames[0].skip = 0; |
|
262 |
+ for (i = 1; i < ape->totalframes; i++) { |
|
263 |
+ ape->frames[i].pos = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe; |
|
264 |
+ ape->frames[i].nblocks = ape->blocksperframe; |
|
265 |
+ ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos; |
|
266 |
+ ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3; |
|
267 |
+ } |
|
268 |
+ ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4; |
|
269 |
+ ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks; |
|
270 |
+ |
|
271 |
+ for (i = 0; i < ape->totalframes; i++) { |
|
272 |
+ if(ape->frames[i].skip){ |
|
273 |
+ ape->frames[i].pos -= ape->frames[i].skip; |
|
274 |
+ ape->frames[i].size += ape->frames[i].skip; |
|
275 |
+ } |
|
276 |
+ ape->frames[i].size = (ape->frames[i].size + 3) & ~3; |
|
277 |
+ } |
|
278 |
+ |
|
279 |
+ |
|
280 |
+ ape_dumpinfo(ape); |
|
281 |
+ |
|
282 |
+ av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype); |
|
283 |
+ |
|
284 |
+ /* now we are ready: build format streams */ |
|
285 |
+ st = av_new_stream(s, 0); |
|
286 |
+ if (!st) |
|
287 |
+ return -1; |
|
288 |
+ |
|
289 |
+ total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks; |
|
290 |
+ |
|
291 |
+ st->codec->codec_type = CODEC_TYPE_AUDIO; |
|
292 |
+ st->codec->codec_id = CODEC_ID_APE; |
|
293 |
+ st->codec->codec_tag = MKTAG('A', 'P', 'E', ' '); |
|
294 |
+ st->codec->channels = ape->channels; |
|
295 |
+ st->codec->sample_rate = ape->samplerate; |
|
296 |
+ st->codec->bits_per_sample = ape->bps; |
|
297 |
+ st->codec->frame_size = MAC_SUBFRAME_SIZE; |
|
298 |
+ |
|
299 |
+ st->nb_frames = ape->totalframes; |
|
300 |
+ s->start_time = 0; |
|
301 |
+ s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate; |
|
302 |
+ av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate); |
|
303 |
+ |
|
304 |
+ st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE); |
|
305 |
+ st->codec->extradata_size = APE_EXTRADATA_SIZE; |
|
306 |
+ AV_WL16(st->codec->extradata + 0, ape->fileversion); |
|
307 |
+ AV_WL16(st->codec->extradata + 2, ape->compressiontype); |
|
308 |
+ AV_WL16(st->codec->extradata + 4, ape->formatflags); |
|
309 |
+ |
|
310 |
+ pts = 0; |
|
311 |
+ for (i = 0; i < ape->totalframes; i++) { |
|
312 |
+ ape->frames[i].pts = pts; |
|
313 |
+ av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME); |
|
314 |
+ pts += ape->blocksperframe / MAC_SUBFRAME_SIZE; |
|
315 |
+ } |
|
316 |
+ |
|
317 |
+ return 0; |
|
318 |
+} |
|
319 |
+ |
|
320 |
+static int ape_read_packet(AVFormatContext * s, AVPacket * pkt) |
|
321 |
+{ |
|
322 |
+ int ret; |
|
323 |
+ int nblocks; |
|
324 |
+ APEContext *ape = s->priv_data; |
|
325 |
+ uint32_t extra_size = 8; |
|
326 |
+ |
|
327 |
+ if (url_feof(&s->pb)) |
|
328 |
+ return AVERROR_IO; |
|
329 |
+ if (ape->currentframe > ape->totalframes) |
|
330 |
+ return AVERROR_IO; |
|
331 |
+ |
|
332 |
+ url_fseek (&s->pb, ape->frames[ape->currentframe].pos, SEEK_SET); |
|
333 |
+ |
|
334 |
+ /* Calculate how many blocks there are in this frame */ |
|
335 |
+ if (ape->currentframe == (ape->totalframes - 1)) |
|
336 |
+ nblocks = ape->finalframeblocks; |
|
337 |
+ else |
|
338 |
+ nblocks = ape->blocksperframe; |
|
339 |
+ |
|
340 |
+ if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0) |
|
341 |
+ return AVERROR_NOMEM; |
|
342 |
+ |
|
343 |
+ AV_WL32(pkt->data , nblocks); |
|
344 |
+ AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip); |
|
345 |
+ ret = get_buffer(&s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size); |
|
346 |
+ |
|
347 |
+ pkt->pts = ape->frames[ape->currentframe].pts; |
|
348 |
+ pkt->stream_index = 0; |
|
349 |
+ |
|
350 |
+ /* note: we need to modify the packet size here to handle the last |
|
351 |
+ packet */ |
|
352 |
+ pkt->size = ret + extra_size; |
|
353 |
+ |
|
354 |
+ ape->currentframe++; |
|
355 |
+ |
|
356 |
+ return 0; |
|
357 |
+} |
|
358 |
+ |
|
359 |
+static int ape_read_close(AVFormatContext * s) |
|
360 |
+{ |
|
361 |
+ APEContext *ape = s->priv_data; |
|
362 |
+ |
|
363 |
+ av_freep(&ape->frames); |
|
364 |
+ av_freep(&ape->seektable); |
|
365 |
+ return 0; |
|
366 |
+} |
|
367 |
+ |
|
368 |
+static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) |
|
369 |
+{ |
|
370 |
+ AVStream *st = s->streams[stream_index]; |
|
371 |
+ APEContext *ape = s->priv_data; |
|
372 |
+ int index = av_index_search_timestamp(st, timestamp, flags); |
|
373 |
+ |
|
374 |
+ if (index < 0) |
|
375 |
+ return -1; |
|
376 |
+ |
|
377 |
+ ape->currentframe = index; |
|
378 |
+ return 0; |
|
379 |
+} |
|
380 |
+ |
|
381 |
+AVInputFormat ape_demuxer = { |
|
382 |
+ "ape", |
|
383 |
+ "Monkey's Audio", |
|
384 |
+ sizeof(APEContext), |
|
385 |
+ ape_probe, |
|
386 |
+ ape_read_header, |
|
387 |
+ ape_read_packet, |
|
388 |
+ ape_read_close, |
|
389 |
+ ape_read_seek, |
|
390 |
+ .extensions = "ape,apl,mac" |
|
391 |
+}; |
... | ... |
@@ -21,8 +21,8 @@ |
21 | 21 |
#ifndef AVFORMAT_H |
22 | 22 |
#define AVFORMAT_H |
23 | 23 |
|
24 |
-#define LIBAVFORMAT_VERSION_INT ((51<<16)+(12<<8)+3) |
|
25 |
-#define LIBAVFORMAT_VERSION 51.12.3 |
|
24 |
+#define LIBAVFORMAT_VERSION_INT ((51<<16)+(13<<8)+3) |
|
25 |
+#define LIBAVFORMAT_VERSION 51.13.3 |
|
26 | 26 |
#define LIBAVFORMAT_BUILD LIBAVFORMAT_VERSION_INT |
27 | 27 |
|
28 | 28 |
#define LIBAVFORMAT_IDENT "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION) |