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