00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024
00025 #include "libavutil/intreadwrite.h"
00026 #include "avformat.h"
00027 #include "apetag.h"
00028
00029 #define ENABLE_DEBUG 0
00030
00031
00032 #define APE_MIN_VERSION 3950
00033 #define APE_MAX_VERSION 3990
00034
00035 #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
00036 #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
00037 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
00038 #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
00039 #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
00040 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
00041
00042 #define MAC_SUBFRAME_SIZE 4608
00043
00044 #define APE_EXTRADATA_SIZE 6
00045
00046 typedef struct {
00047 int64_t pos;
00048 int nblocks;
00049 int size;
00050 int skip;
00051 int64_t pts;
00052 } APEFrame;
00053
00054 typedef struct {
00055
00056 uint32_t junklength;
00057 uint32_t firstframe;
00058 uint32_t totalsamples;
00059 int currentframe;
00060 APEFrame *frames;
00061
00062
00063 char magic[4];
00064 int16_t fileversion;
00065 int16_t padding1;
00066 uint32_t descriptorlength;
00067 uint32_t headerlength;
00068 uint32_t seektablelength;
00069 uint32_t wavheaderlength;
00070 uint32_t audiodatalength;
00071 uint32_t audiodatalength_high;
00072 uint32_t wavtaillength;
00073 uint8_t md5[16];
00074
00075
00076 uint16_t compressiontype;
00077 uint16_t formatflags;
00078 uint32_t blocksperframe;
00079 uint32_t finalframeblocks;
00080 uint32_t totalframes;
00081 uint16_t bps;
00082 uint16_t channels;
00083 uint32_t samplerate;
00084
00085
00086 uint32_t *seektable;
00087 } APEContext;
00088
00089 static int ape_probe(AVProbeData * p)
00090 {
00091 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
00092 return AVPROBE_SCORE_MAX;
00093
00094 return 0;
00095 }
00096
00097 static void ape_dumpinfo(AVFormatContext * s, APEContext * ape_ctx)
00098 {
00099 #if ENABLE_DEBUG
00100 int i;
00101
00102 av_log(s, AV_LOG_DEBUG, "Descriptor Block:\n\n");
00103 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]);
00104 av_log(s, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion);
00105 av_log(s, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength);
00106 av_log(s, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength);
00107 av_log(s, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength);
00108 av_log(s, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength);
00109 av_log(s, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength);
00110 av_log(s, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
00111 av_log(s, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength);
00112 av_log(s, AV_LOG_DEBUG, "md5 = ");
00113 for (i = 0; i < 16; i++)
00114 av_log(s, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
00115 av_log(s, AV_LOG_DEBUG, "\n");
00116
00117 av_log(s, AV_LOG_DEBUG, "\nHeader Block:\n\n");
00118
00119 av_log(s, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype);
00120 av_log(s, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags);
00121 av_log(s, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe);
00122 av_log(s, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks);
00123 av_log(s, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes);
00124 av_log(s, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps);
00125 av_log(s, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels);
00126 av_log(s, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate);
00127
00128 av_log(s, AV_LOG_DEBUG, "\nSeektable\n\n");
00129 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
00130 av_log(s, AV_LOG_DEBUG, "No seektable\n");
00131 } else {
00132 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
00133 if (i < ape_ctx->totalframes - 1) {
00134 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]);
00135 } else {
00136 av_log(s, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]);
00137 }
00138 }
00139 }
00140
00141 av_log(s, AV_LOG_DEBUG, "\nFrames\n\n");
00142 for (i = 0; i < ape_ctx->totalframes; i++)
00143 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);
00144
00145 av_log(s, AV_LOG_DEBUG, "\nCalculated information:\n\n");
00146 av_log(s, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength);
00147 av_log(s, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe);
00148 av_log(s, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples);
00149 #endif
00150 }
00151
00152 static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap)
00153 {
00154 ByteIOContext *pb = s->pb;
00155 APEContext *ape = s->priv_data;
00156 AVStream *st;
00157 uint32_t tag;
00158 int i;
00159 int total_blocks;
00160 int64_t pts;
00161
00162
00163 ape->junklength = 0;
00164
00165 tag = get_le32(pb);
00166 if (tag != MKTAG('M', 'A', 'C', ' '))
00167 return -1;
00168
00169 ape->fileversion = get_le16(pb);
00170
00171 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
00172 av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10);
00173 return -1;
00174 }
00175
00176 if (ape->fileversion >= 3980) {
00177 ape->padding1 = get_le16(pb);
00178 ape->descriptorlength = get_le32(pb);
00179 ape->headerlength = get_le32(pb);
00180 ape->seektablelength = get_le32(pb);
00181 ape->wavheaderlength = get_le32(pb);
00182 ape->audiodatalength = get_le32(pb);
00183 ape->audiodatalength_high = get_le32(pb);
00184 ape->wavtaillength = get_le32(pb);
00185 get_buffer(pb, ape->md5, 16);
00186
00187
00188
00189 if (ape->descriptorlength > 52)
00190 url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
00191
00192
00193 ape->compressiontype = get_le16(pb);
00194 ape->formatflags = get_le16(pb);
00195 ape->blocksperframe = get_le32(pb);
00196 ape->finalframeblocks = get_le32(pb);
00197 ape->totalframes = get_le32(pb);
00198 ape->bps = get_le16(pb);
00199 ape->channels = get_le16(pb);
00200 ape->samplerate = get_le32(pb);
00201 } else {
00202 ape->descriptorlength = 0;
00203 ape->headerlength = 32;
00204
00205 ape->compressiontype = get_le16(pb);
00206 ape->formatflags = get_le16(pb);
00207 ape->channels = get_le16(pb);
00208 ape->samplerate = get_le32(pb);
00209 ape->wavheaderlength = get_le32(pb);
00210 ape->wavtaillength = get_le32(pb);
00211 ape->totalframes = get_le32(pb);
00212 ape->finalframeblocks = get_le32(pb);
00213
00214 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
00215 url_fseek(pb, 4, SEEK_CUR);
00216 ape->headerlength += 4;
00217 }
00218
00219 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
00220 ape->seektablelength = get_le32(pb);
00221 ape->headerlength += 4;
00222 ape->seektablelength *= sizeof(int32_t);
00223 } else
00224 ape->seektablelength = ape->totalframes * sizeof(int32_t);
00225
00226 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
00227 ape->bps = 8;
00228 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
00229 ape->bps = 24;
00230 else
00231 ape->bps = 16;
00232
00233 if (ape->fileversion >= 3950)
00234 ape->blocksperframe = 73728 * 4;
00235 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
00236 ape->blocksperframe = 73728;
00237 else
00238 ape->blocksperframe = 9216;
00239
00240
00241 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
00242 url_fskip(pb, ape->wavheaderlength);
00243 }
00244
00245 if(!ape->totalframes){
00246 av_log(s, AV_LOG_ERROR, "No frames in the file!\n");
00247 return AVERROR(EINVAL);
00248 }
00249 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
00250 av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
00251 return -1;
00252 }
00253 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
00254 if(!ape->frames)
00255 return AVERROR(ENOMEM);
00256 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
00257 ape->currentframe = 0;
00258
00259
00260 ape->totalsamples = ape->finalframeblocks;
00261 if (ape->totalframes > 1)
00262 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
00263
00264 if (ape->seektablelength > 0) {
00265 ape->seektable = av_malloc(ape->seektablelength);
00266 if (!ape->seektable)
00267 return AVERROR(ENOMEM);
00268 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
00269 ape->seektable[i] = get_le32(pb);
00270 }
00271
00272 ape->frames[0].pos = ape->firstframe;
00273 ape->frames[0].nblocks = ape->blocksperframe;
00274 ape->frames[0].skip = 0;
00275 for (i = 1; i < ape->totalframes; i++) {
00276 ape->frames[i].pos = ape->seektable[i];
00277 ape->frames[i].nblocks = ape->blocksperframe;
00278 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
00279 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
00280 }
00281 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
00282 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
00283
00284 for (i = 0; i < ape->totalframes; i++) {
00285 if(ape->frames[i].skip){
00286 ape->frames[i].pos -= ape->frames[i].skip;
00287 ape->frames[i].size += ape->frames[i].skip;
00288 }
00289 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
00290 }
00291
00292
00293 ape_dumpinfo(s, ape);
00294
00295
00296 if (!url_is_streamed(pb)) {
00297 ff_ape_parse_tag(s);
00298 url_fseek(pb, 0, SEEK_SET);
00299 }
00300
00301 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype);
00302
00303
00304 st = av_new_stream(s, 0);
00305 if (!st)
00306 return -1;
00307
00308 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
00309
00310 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00311 st->codec->codec_id = CODEC_ID_APE;
00312 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
00313 st->codec->channels = ape->channels;
00314 st->codec->sample_rate = ape->samplerate;
00315 st->codec->bits_per_coded_sample = ape->bps;
00316 st->codec->frame_size = MAC_SUBFRAME_SIZE;
00317
00318 st->nb_frames = ape->totalframes;
00319 st->start_time = 0;
00320 st->duration = total_blocks / MAC_SUBFRAME_SIZE;
00321 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
00322
00323 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
00324 st->codec->extradata_size = APE_EXTRADATA_SIZE;
00325 AV_WL16(st->codec->extradata + 0, ape->fileversion);
00326 AV_WL16(st->codec->extradata + 2, ape->compressiontype);
00327 AV_WL16(st->codec->extradata + 4, ape->formatflags);
00328
00329 pts = 0;
00330 for (i = 0; i < ape->totalframes; i++) {
00331 ape->frames[i].pts = pts;
00332 av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME);
00333 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
00334 }
00335
00336 return 0;
00337 }
00338
00339 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
00340 {
00341 int ret;
00342 int nblocks;
00343 APEContext *ape = s->priv_data;
00344 uint32_t extra_size = 8;
00345
00346 if (url_feof(s->pb))
00347 return AVERROR(EIO);
00348 if (ape->currentframe > ape->totalframes)
00349 return AVERROR(EIO);
00350
00351 url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
00352
00353
00354 if (ape->currentframe == (ape->totalframes - 1))
00355 nblocks = ape->finalframeblocks;
00356 else
00357 nblocks = ape->blocksperframe;
00358
00359 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
00360 return AVERROR(ENOMEM);
00361
00362 AV_WL32(pkt->data , nblocks);
00363 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
00364 ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
00365
00366 pkt->pts = ape->frames[ape->currentframe].pts;
00367 pkt->stream_index = 0;
00368
00369
00370
00371 pkt->size = ret + extra_size;
00372
00373 ape->currentframe++;
00374
00375 return 0;
00376 }
00377
00378 static int ape_read_close(AVFormatContext * s)
00379 {
00380 APEContext *ape = s->priv_data;
00381
00382 av_freep(&ape->frames);
00383 av_freep(&ape->seektable);
00384 return 0;
00385 }
00386
00387 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00388 {
00389 AVStream *st = s->streams[stream_index];
00390 APEContext *ape = s->priv_data;
00391 int index = av_index_search_timestamp(st, timestamp, flags);
00392
00393 if (index < 0)
00394 return -1;
00395
00396 ape->currentframe = index;
00397 return 0;
00398 }
00399
00400 AVInputFormat ape_demuxer = {
00401 "ape",
00402 NULL_IF_CONFIG_SMALL("Monkey's Audio"),
00403 sizeof(APEContext),
00404 ape_probe,
00405 ape_read_header,
00406 ape_read_packet,
00407 ape_read_close,
00408 ape_read_seek,
00409 .extensions = "ape,apl,mac"
00410 };