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