1 /*
2 * Monkey's Audio APE demuxer
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4 * based upon libdemac from Dave Chapman.
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdio.h>
24
29
30 /* The earliest and latest file formats supported by this library */
31 #define APE_MIN_VERSION 3800
32 #define APE_MAX_VERSION 3990
33
34 #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
35 #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
36 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
37 #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
38 #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
39 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
40
41 #define APE_EXTRADATA_SIZE 6
42
50
51 typedef struct {
52 /* Derived fields */
58
59 /* Info from Descriptor Block */
71
72 /* Info from Header Block */
81
82 /* Seektable */
86
88 {
89 if (p->
buf[0] ==
'M' && p->
buf[1] ==
'A' && p->
buf[2] ==
'C' && p->
buf[3] ==
' ')
91
92 return 0;
93 }
94
96 {
97 #ifdef DEBUG
98 int i;
99
111 for (i = 0; i < 16; i++)
114
116
125
129 } else {
131 if (i < ape_ctx->totalframes - 1) {
139 } else {
141 }
142 }
143 }
144
150
155 #endif
156 }
157
159 {
164 int i;
165 int total_blocks, final_size = 0;
166 int64_t pts, file_size;
167
168 /* Skip any leading junk such as id3v2 tags */
170
172 if (tag !=
MKTAG(
'M',
'A',
'C',
' '))
174
176
181 }
182
193
194 /* Skip any unknown bytes at the end of the descriptor.
195 This is for future compatibility */
198
199 /* Read header data */
208 } else {
211
220
222 avio_skip(pb, 4);
/* Skip the peak level */
224 }
225
230 } else
232
237 else
239
244 else
246
247 /* Skip any stored wav header */
250 }
251
255 }
260 }
263 "Number of seek entries is less than number of frames: %zu vs. %"PRIu32"\n",
266 }
274
275
279
292 }
293 }
294
303 }
305 /* calculate final packet size from total file size, if available */
307 if (file_size > 0) {
310 final_size -= final_size & 3;
311 }
312 if (file_size <= 0 || final_size <= 0)
315
320 }
322 }
325 if (i < ape->totalframes - 1 && ape->
bittable[i + 1])
329 }
330 }
331
333
337
338 /* now we are ready: build format streams */
340 if (!st)
342
344
351
356
362
363 pts = 0;
368 }
369
370 /* try to read APE tags */
374 }
375
376 return 0;
377 }
378
380 {
382 int nblocks;
385
390
393
394 /* Calculate how many blocks there are in this frame */
397 else
399
406 }
407
410
414 if (ret < 0) {
417 }
418
421
422 /* note: we need to modify the packet size here to handle the last
423 packet */
425
427
428 return 0;
429 }
430
432 {
434
438 return 0;
439 }
440
442 {
446
447 if (index < 0)
448 return -1;
449
451 return -1;
453 return 0;
454 }
455
465 .extensions = "ape,apl,mac",
466 };