2 * Monkey's Audio APE demuxer
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4 * based upon libdemac from Dave Chapman.
6 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/intreadwrite.h"
29 #define ENABLE_DEBUG 0
31 /* The earliest and latest file formats supported by this library */
32 #define APE_MIN_VERSION 3950
33 #define APE_MAX_VERSION 3990
35 #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
36 #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
37 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
38 #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
39 #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
40 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
42 #define MAC_SUBFRAME_SIZE 4608
44 #define APE_EXTRADATA_SIZE 6
58 uint32_t totalsamples
;
62 /* Info from Descriptor Block */
66 uint32_t descriptorlength
;
67 uint32_t headerlength
;
68 uint32_t seektablelength
;
69 uint32_t wavheaderlength
;
70 uint32_t audiodatalength
;
71 uint32_t audiodatalength_high
;
72 uint32_t wavtaillength
;
75 /* Info from Header Block */
76 uint16_t compressiontype
;
78 uint32_t blocksperframe
;
79 uint32_t finalframeblocks
;
89 static int ape_probe(AVProbeData
* p
)
91 if (p
->buf
[0] == 'M' && p
->buf
[1] == 'A' && p
->buf
[2] == 'C' && p
->buf
[3] == ' ')
92 return AVPROBE_SCORE_MAX
;
97 static void ape_dumpinfo(AVFormatContext
* s
, APEContext
* ape_ctx
)
102 av_log(s
, AV_LOG_DEBUG
, "Descriptor Block:\n\n");
103 av_log(s
, 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]);
104 av_log(s
, AV_LOG_DEBUG
, "fileversion = %d\n", ape_ctx
->fileversion
);
105 av_log(s
, AV_LOG_DEBUG
, "descriptorlength = %d\n", ape_ctx
->descriptorlength
);
106 av_log(s
, AV_LOG_DEBUG
, "headerlength = %d\n", ape_ctx
->headerlength
);
107 av_log(s
, AV_LOG_DEBUG
, "seektablelength = %d\n", ape_ctx
->seektablelength
);
108 av_log(s
, AV_LOG_DEBUG
, "wavheaderlength = %d\n", ape_ctx
->wavheaderlength
);
109 av_log(s
, AV_LOG_DEBUG
, "audiodatalength = %d\n", ape_ctx
->audiodatalength
);
110 av_log(s
, AV_LOG_DEBUG
, "audiodatalength_high = %d\n", ape_ctx
->audiodatalength_high
);
111 av_log(s
, AV_LOG_DEBUG
, "wavtaillength = %d\n", ape_ctx
->wavtaillength
);
112 av_log(s
, AV_LOG_DEBUG
, "md5 = ");
113 for (i
= 0; i
< 16; i
++)
114 av_log(s
, AV_LOG_DEBUG
, "%02x", ape_ctx
->md5
[i
]);
115 av_log(s
, AV_LOG_DEBUG
, "\n");
117 av_log(s
, AV_LOG_DEBUG
, "\nHeader Block:\n\n");
119 av_log(s
, AV_LOG_DEBUG
, "compressiontype = %d\n", ape_ctx
->compressiontype
);
120 av_log(s
, AV_LOG_DEBUG
, "formatflags = %d\n", ape_ctx
->formatflags
);
121 av_log(s
, AV_LOG_DEBUG
, "blocksperframe = %d\n", ape_ctx
->blocksperframe
);
122 av_log(s
, AV_LOG_DEBUG
, "finalframeblocks = %d\n", ape_ctx
->finalframeblocks
);
123 av_log(s
, AV_LOG_DEBUG
, "totalframes = %d\n", ape_ctx
->totalframes
);
124 av_log(s
, AV_LOG_DEBUG
, "bps = %d\n", ape_ctx
->bps
);
125 av_log(s
, AV_LOG_DEBUG
, "channels = %d\n", ape_ctx
->channels
);
126 av_log(s
, AV_LOG_DEBUG
, "samplerate = %d\n", ape_ctx
->samplerate
);
128 av_log(s
, AV_LOG_DEBUG
, "\nSeektable\n\n");
129 if ((ape_ctx
->seektablelength
/ sizeof(uint32_t)) != ape_ctx
->totalframes
) {
130 av_log(s
, AV_LOG_DEBUG
, "No seektable\n");
132 for (i
= 0; i
< ape_ctx
->seektablelength
/ sizeof(uint32_t); i
++) {
133 if (i
< ape_ctx
->totalframes
- 1) {
134 av_log(s
, AV_LOG_DEBUG
, "%8d %d (%d bytes)\n", i
, ape_ctx
->seektable
[i
], ape_ctx
->seektable
[i
+ 1] - ape_ctx
->seektable
[i
]);
136 av_log(s
, AV_LOG_DEBUG
, "%8d %d\n", i
, ape_ctx
->seektable
[i
]);
141 av_log(s
, AV_LOG_DEBUG
, "\nFrames\n\n");
142 for (i
= 0; i
< ape_ctx
->totalframes
; i
++)
143 av_log(s
, 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
);
145 av_log(s
, AV_LOG_DEBUG
, "\nCalculated information:\n\n");
146 av_log(s
, AV_LOG_DEBUG
, "junklength = %d\n", ape_ctx
->junklength
);
147 av_log(s
, AV_LOG_DEBUG
, "firstframe = %d\n", ape_ctx
->firstframe
);
148 av_log(s
, AV_LOG_DEBUG
, "totalsamples = %d\n", ape_ctx
->totalsamples
);
152 static int ape_read_header(AVFormatContext
* s
, AVFormatParameters
* ap
)
154 ByteIOContext
*pb
= s
->pb
;
155 APEContext
*ape
= s
->priv_data
;
162 /* TODO: Skip any leading junk such as id3v2 tags */
166 if (tag
!= MKTAG('M', 'A', 'C', ' '))
169 ape
->fileversion
= get_le16(pb
);
171 if (ape
->fileversion
< APE_MIN_VERSION
|| ape
->fileversion
> APE_MAX_VERSION
) {
172 av_log(s
, AV_LOG_ERROR
, "Unsupported file version - %d.%02d\n", ape
->fileversion
/ 1000, (ape
->fileversion
% 1000) / 10);
176 if (ape
->fileversion
>= 3980) {
177 ape
->padding1
= get_le16(pb
);
178 ape
->descriptorlength
= get_le32(pb
);
179 ape
->headerlength
= get_le32(pb
);
180 ape
->seektablelength
= get_le32(pb
);
181 ape
->wavheaderlength
= get_le32(pb
);
182 ape
->audiodatalength
= get_le32(pb
);
183 ape
->audiodatalength_high
= get_le32(pb
);
184 ape
->wavtaillength
= get_le32(pb
);
185 get_buffer(pb
, ape
->md5
, 16);
187 /* Skip any unknown bytes at the end of the descriptor.
188 This is for future compatibility */
189 if (ape
->descriptorlength
> 52)
190 url_fseek(pb
, ape
->descriptorlength
- 52, SEEK_CUR
);
192 /* Read header data */
193 ape
->compressiontype
= get_le16(pb
);
194 ape
->formatflags
= get_le16(pb
);
195 ape
->blocksperframe
= get_le32(pb
);
196 ape
->finalframeblocks
= get_le32(pb
);
197 ape
->totalframes
= get_le32(pb
);
198 ape
->bps
= get_le16(pb
);
199 ape
->channels
= get_le16(pb
);
200 ape
->samplerate
= get_le32(pb
);
202 ape
->descriptorlength
= 0;
203 ape
->headerlength
= 32;
205 ape
->compressiontype
= get_le16(pb
);
206 ape
->formatflags
= get_le16(pb
);
207 ape
->channels
= get_le16(pb
);
208 ape
->samplerate
= get_le32(pb
);
209 ape
->wavheaderlength
= get_le32(pb
);
210 ape
->wavtaillength
= get_le32(pb
);
211 ape
->totalframes
= get_le32(pb
);
212 ape
->finalframeblocks
= get_le32(pb
);
214 if (ape
->formatflags
& MAC_FORMAT_FLAG_HAS_PEAK_LEVEL
) {
215 url_fseek(pb
, 4, SEEK_CUR
); /* Skip the peak level */
216 ape
->headerlength
+= 4;
219 if (ape
->formatflags
& MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS
) {
220 ape
->seektablelength
= get_le32(pb
);
221 ape
->headerlength
+= 4;
222 ape
->seektablelength
*= sizeof(int32_t);
224 ape
->seektablelength
= ape
->totalframes
* sizeof(int32_t);
226 if (ape
->formatflags
& MAC_FORMAT_FLAG_8_BIT
)
228 else if (ape
->formatflags
& MAC_FORMAT_FLAG_24_BIT
)
233 if (ape
->fileversion
>= 3950)
234 ape
->blocksperframe
= 73728 * 4;
235 else if (ape
->fileversion
>= 3900 || (ape
->fileversion
>= 3800 && ape
->compressiontype
>= 4000))
236 ape
->blocksperframe
= 73728;
238 ape
->blocksperframe
= 9216;
240 /* Skip any stored wav header */
241 if (!(ape
->formatflags
& MAC_FORMAT_FLAG_CREATE_WAV_HEADER
))
242 url_fskip(pb
, ape
->wavheaderlength
);
245 if(ape
->totalframes
> UINT_MAX
/ sizeof(APEFrame
)){
246 av_log(s
, AV_LOG_ERROR
, "Too many frames: %d\n", ape
->totalframes
);
249 ape
->frames
= av_malloc(ape
->totalframes
* sizeof(APEFrame
));
251 return AVERROR_NOMEM
;
252 ape
->firstframe
= ape
->junklength
+ ape
->descriptorlength
+ ape
->headerlength
+ ape
->seektablelength
+ ape
->wavheaderlength
;
253 ape
->currentframe
= 0;
256 ape
->totalsamples
= ape
->finalframeblocks
;
257 if (ape
->totalframes
> 1)
258 ape
->totalsamples
+= ape
->blocksperframe
* (ape
->totalframes
- 1);
260 if (ape
->seektablelength
> 0) {
261 ape
->seektable
= av_malloc(ape
->seektablelength
);
262 for (i
= 0; i
< ape
->seektablelength
/ sizeof(uint32_t); i
++)
263 ape
->seektable
[i
] = get_le32(pb
);
266 ape
->frames
[0].pos
= ape
->firstframe
;
267 ape
->frames
[0].nblocks
= ape
->blocksperframe
;
268 ape
->frames
[0].skip
= 0;
269 for (i
= 1; i
< ape
->totalframes
; i
++) {
270 ape
->frames
[i
].pos
= ape
->seektable
[i
]; //ape->frames[i-1].pos + ape->blocksperframe;
271 ape
->frames
[i
].nblocks
= ape
->blocksperframe
;
272 ape
->frames
[i
- 1].size
= ape
->frames
[i
].pos
- ape
->frames
[i
- 1].pos
;
273 ape
->frames
[i
].skip
= (ape
->frames
[i
].pos
- ape
->frames
[0].pos
) & 3;
275 ape
->frames
[ape
->totalframes
- 1].size
= ape
->finalframeblocks
* 4;
276 ape
->frames
[ape
->totalframes
- 1].nblocks
= ape
->finalframeblocks
;
278 for (i
= 0; i
< ape
->totalframes
; i
++) {
279 if(ape
->frames
[i
].skip
){
280 ape
->frames
[i
].pos
-= ape
->frames
[i
].skip
;
281 ape
->frames
[i
].size
+= ape
->frames
[i
].skip
;
283 ape
->frames
[i
].size
= (ape
->frames
[i
].size
+ 3) & ~3;
287 ape_dumpinfo(s
, ape
);
289 /* try to read APE tags */
290 if (!url_is_streamed(pb
)) {
292 url_fseek(pb
, 0, SEEK_SET
);
295 av_log(s
, AV_LOG_DEBUG
, "Decoding file - v%d.%02d, compression level %d\n", ape
->fileversion
/ 1000, (ape
->fileversion
% 1000) / 10, ape
->compressiontype
);
297 /* now we are ready: build format streams */
298 st
= av_new_stream(s
, 0);
302 total_blocks
= (ape
->totalframes
== 0) ? 0 : ((ape
->totalframes
- 1) * ape
->blocksperframe
) + ape
->finalframeblocks
;
304 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
305 st
->codec
->codec_id
= CODEC_ID_APE
;
306 st
->codec
->codec_tag
= MKTAG('A', 'P', 'E', ' ');
307 st
->codec
->channels
= ape
->channels
;
308 st
->codec
->sample_rate
= ape
->samplerate
;
309 st
->codec
->bits_per_coded_sample
= ape
->bps
;
310 st
->codec
->frame_size
= MAC_SUBFRAME_SIZE
;
312 st
->nb_frames
= ape
->totalframes
;
314 s
->duration
= (int64_t) total_blocks
* AV_TIME_BASE
/ ape
->samplerate
;
315 av_set_pts_info(st
, 64, MAC_SUBFRAME_SIZE
, ape
->samplerate
);
317 st
->codec
->extradata
= av_malloc(APE_EXTRADATA_SIZE
);
318 st
->codec
->extradata_size
= APE_EXTRADATA_SIZE
;
319 AV_WL16(st
->codec
->extradata
+ 0, ape
->fileversion
);
320 AV_WL16(st
->codec
->extradata
+ 2, ape
->compressiontype
);
321 AV_WL16(st
->codec
->extradata
+ 4, ape
->formatflags
);
324 for (i
= 0; i
< ape
->totalframes
; i
++) {
325 ape
->frames
[i
].pts
= pts
;
326 av_add_index_entry(st
, ape
->frames
[i
].pos
, ape
->frames
[i
].pts
, 0, 0, AVINDEX_KEYFRAME
);
327 pts
+= ape
->blocksperframe
/ MAC_SUBFRAME_SIZE
;
333 static int ape_read_packet(AVFormatContext
* s
, AVPacket
* pkt
)
337 APEContext
*ape
= s
->priv_data
;
338 uint32_t extra_size
= 8;
342 if (ape
->currentframe
> ape
->totalframes
)
345 url_fseek (s
->pb
, ape
->frames
[ape
->currentframe
].pos
, SEEK_SET
);
347 /* Calculate how many blocks there are in this frame */
348 if (ape
->currentframe
== (ape
->totalframes
- 1))
349 nblocks
= ape
->finalframeblocks
;
351 nblocks
= ape
->blocksperframe
;
353 if (av_new_packet(pkt
, ape
->frames
[ape
->currentframe
].size
+ extra_size
) < 0)
354 return AVERROR_NOMEM
;
356 AV_WL32(pkt
->data
, nblocks
);
357 AV_WL32(pkt
->data
+ 4, ape
->frames
[ape
->currentframe
].skip
);
358 ret
= get_buffer(s
->pb
, pkt
->data
+ extra_size
, ape
->frames
[ape
->currentframe
].size
);
360 pkt
->pts
= ape
->frames
[ape
->currentframe
].pts
;
361 pkt
->stream_index
= 0;
363 /* note: we need to modify the packet size here to handle the last
365 pkt
->size
= ret
+ extra_size
;
372 static int ape_read_close(AVFormatContext
* s
)
374 APEContext
*ape
= s
->priv_data
;
376 av_freep(&ape
->frames
);
377 av_freep(&ape
->seektable
);
381 static int ape_read_seek(AVFormatContext
*s
, int stream_index
, int64_t timestamp
, int flags
)
383 AVStream
*st
= s
->streams
[stream_index
];
384 APEContext
*ape
= s
->priv_data
;
385 int index
= av_index_search_timestamp(st
, timestamp
, flags
);
390 ape
->currentframe
= index
;
394 AVInputFormat ape_demuxer
= {
396 NULL_IF_CONFIG_SMALL("Monkey's Audio"),
403 .extensions
= "ape,apl,mac"