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 "avformat.h"
00026
00027 #define ENABLE_DEBUG 0
00028
00029
00030 #define APE_MIN_VERSION 3950
00031 #define APE_MAX_VERSION 3990
00032
00033 #define MAC_FORMAT_FLAG_8_BIT 1 // is 8-bit [OBSOLETE]
00034 #define MAC_FORMAT_FLAG_CRC 2 // uses the new CRC32 error detection [OBSOLETE]
00035 #define MAC_FORMAT_FLAG_HAS_PEAK_LEVEL 4 // uint32 nPeakLevel after the header [OBSOLETE]
00036 #define MAC_FORMAT_FLAG_24_BIT 8 // is 24-bit [OBSOLETE]
00037 #define MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS 16 // has the number of seek elements after the peak level
00038 #define MAC_FORMAT_FLAG_CREATE_WAV_HEADER 32 // create the wave header on decompression (not stored)
00039
00040 #define MAC_SUBFRAME_SIZE 4608
00041
00042 #define APE_EXTRADATA_SIZE 6
00043
00044
00045 #define APE_TAG_VERSION 2000
00046 #define APE_TAG_FOOTER_BYTES 32
00047 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31)
00048 #define APE_TAG_FLAG_IS_HEADER (1 << 29)
00049
00050 #define TAG(name, field) {name, offsetof(AVFormatContext, field), sizeof(((AVFormatContext *)0)->field)}
00051
00052 static const struct {
00053 const char *name;
00054 int offset;
00055 int size;
00056 } tags[] = {
00057 TAG("Title" , title ),
00058 TAG("Artist" , author ),
00059 TAG("Copyright", copyright),
00060 TAG("Comment" , comment ),
00061 TAG("Album" , album ),
00062 TAG("Year" , year ),
00063 TAG("Track" , track ),
00064 TAG("Genre" , genre ),
00065 { NULL }
00066 };
00067
00068 typedef struct {
00069 int64_t pos;
00070 int nblocks;
00071 int size;
00072 int skip;
00073 int64_t pts;
00074 } APEFrame;
00075
00076 typedef struct {
00077
00078 uint32_t junklength;
00079 uint32_t firstframe;
00080 uint32_t totalsamples;
00081 int currentframe;
00082 APEFrame *frames;
00083
00084
00085 char magic[4];
00086 int16_t fileversion;
00087 int16_t padding1;
00088 uint32_t descriptorlength;
00089 uint32_t headerlength;
00090 uint32_t seektablelength;
00091 uint32_t wavheaderlength;
00092 uint32_t audiodatalength;
00093 uint32_t audiodatalength_high;
00094 uint32_t wavtaillength;
00095 uint8_t md5[16];
00096
00097
00098 uint16_t compressiontype;
00099 uint16_t formatflags;
00100 uint32_t blocksperframe;
00101 uint32_t finalframeblocks;
00102 uint32_t totalframes;
00103 uint16_t bps;
00104 uint16_t channels;
00105 uint32_t samplerate;
00106
00107
00108 uint32_t *seektable;
00109 } APEContext;
00110
00111 static void ape_tag_read_field(AVFormatContext *s)
00112 {
00113 ByteIOContext *pb = s->pb;
00114 uint8_t buf[1024];
00115 uint32_t size;
00116 int i;
00117
00118 memset(buf, 0, 1024);
00119 size = get_le32(pb);
00120 url_fskip(pb, 4);
00121
00122 for (i=0; pb->buf_ptr[i]!='0' && pb->buf_ptr[i]>=0x20 && pb->buf_ptr[i]<=0x7E; i++);
00123
00124 get_buffer(pb, buf, FFMIN(i, 1024));
00125 url_fskip(pb, 1);
00126
00127 for (i=0; tags[i].name; i++)
00128 if (!strcmp (buf, tags[i].name)) {
00129 if (tags[i].size == sizeof(int)) {
00130 char tmp[16];
00131 get_buffer(pb, tmp, FFMIN(sizeof(tmp), size));
00132 *(int *)(((char *)s)+tags[i].offset) = atoi(tmp);
00133 } else {
00134 get_buffer(pb, ((char *)s) + tags[i].offset,
00135 FFMIN(tags[i].size, size));
00136 }
00137 break;
00138 }
00139
00140 if (!tags[i].name)
00141 url_fskip(pb, size);
00142 }
00143
00144 static void ape_parse_tag(AVFormatContext *s)
00145 {
00146 ByteIOContext *pb = s->pb;
00147 int file_size = url_fsize(pb);
00148 uint32_t val, fields, tag_bytes;
00149 uint8_t buf[8];
00150 int i;
00151
00152 if (file_size < APE_TAG_FOOTER_BYTES)
00153 return;
00154
00155 url_fseek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET);
00156
00157 get_buffer(pb, buf, 8);
00158 if (strncmp(buf, "APETAGEX", 8)) {
00159 return;
00160 }
00161
00162 val = get_le32(pb);
00163 if (val > APE_TAG_VERSION) {
00164 av_log(NULL, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
00165 return;
00166 }
00167
00168 tag_bytes = get_le32(pb);
00169 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
00170 av_log(NULL, AV_LOG_ERROR, "Tag size is way too big\n");
00171 return;
00172 }
00173
00174 fields = get_le32(pb);
00175 if (fields > 65536) {
00176 av_log(NULL, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
00177 return;
00178 }
00179
00180 val = get_le32(pb);
00181 if (val & APE_TAG_FLAG_IS_HEADER) {
00182 av_log(NULL, AV_LOG_ERROR, "APE Tag is a header\n");
00183 return;
00184 }
00185
00186 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
00187 tag_bytes += 2*APE_TAG_FOOTER_BYTES;
00188
00189 url_fseek(pb, file_size - tag_bytes, SEEK_SET);
00190
00191 for (i=0; i<fields; i++)
00192 ape_tag_read_field(s);
00193
00194 #if ENABLE_DEBUG
00195 av_log(NULL, AV_LOG_DEBUG, "\nAPE Tags:\n\n");
00196 av_log(NULL, AV_LOG_DEBUG, "title = %s\n", s->title);
00197 av_log(NULL, AV_LOG_DEBUG, "author = %s\n", s->author);
00198 av_log(NULL, AV_LOG_DEBUG, "copyright = %s\n", s->copyright);
00199 av_log(NULL, AV_LOG_DEBUG, "comment = %s\n", s->comment);
00200 av_log(NULL, AV_LOG_DEBUG, "album = %s\n", s->album);
00201 av_log(NULL, AV_LOG_DEBUG, "year = %d\n", s->year);
00202 av_log(NULL, AV_LOG_DEBUG, "track = %d\n", s->track);
00203 av_log(NULL, AV_LOG_DEBUG, "genre = %s\n", s->genre);
00204 #endif
00205 }
00206
00207 static int ape_probe(AVProbeData * p)
00208 {
00209 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
00210 return AVPROBE_SCORE_MAX;
00211
00212 return 0;
00213 }
00214
00215 static void ape_dumpinfo(APEContext * ape_ctx)
00216 {
00217 #if ENABLE_DEBUG
00218 int i;
00219
00220 av_log(NULL, AV_LOG_DEBUG, "Descriptor Block:\n\n");
00221 av_log(NULL, 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]);
00222 av_log(NULL, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion);
00223 av_log(NULL, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength);
00224 av_log(NULL, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength);
00225 av_log(NULL, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength);
00226 av_log(NULL, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength);
00227 av_log(NULL, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength);
00228 av_log(NULL, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
00229 av_log(NULL, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength);
00230 av_log(NULL, AV_LOG_DEBUG, "md5 = ");
00231 for (i = 0; i < 16; i++)
00232 av_log(NULL, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
00233 av_log(NULL, AV_LOG_DEBUG, "\n");
00234
00235 av_log(NULL, AV_LOG_DEBUG, "\nHeader Block:\n\n");
00236
00237 av_log(NULL, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype);
00238 av_log(NULL, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags);
00239 av_log(NULL, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe);
00240 av_log(NULL, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks);
00241 av_log(NULL, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes);
00242 av_log(NULL, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps);
00243 av_log(NULL, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels);
00244 av_log(NULL, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate);
00245
00246 av_log(NULL, AV_LOG_DEBUG, "\nSeektable\n\n");
00247 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
00248 av_log(NULL, AV_LOG_DEBUG, "No seektable\n");
00249 } else {
00250 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
00251 if (i < ape_ctx->totalframes - 1) {
00252 av_log(NULL, AV_LOG_DEBUG, "%8d %d (%d bytes)\n", i, ape_ctx->seektable[i], ape_ctx->seektable[i + 1] - ape_ctx->seektable[i]);
00253 } else {
00254 av_log(NULL, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]);
00255 }
00256 }
00257 }
00258
00259 av_log(NULL, AV_LOG_DEBUG, "\nFrames\n\n");
00260 for (i = 0; i < ape_ctx->totalframes; i++)
00261 av_log(NULL, 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);
00262
00263 av_log(NULL, AV_LOG_DEBUG, "\nCalculated information:\n\n");
00264 av_log(NULL, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength);
00265 av_log(NULL, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe);
00266 av_log(NULL, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples);
00267 #endif
00268 }
00269
00270 static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap)
00271 {
00272 ByteIOContext *pb = s->pb;
00273 APEContext *ape = s->priv_data;
00274 AVStream *st;
00275 uint32_t tag;
00276 int i;
00277 int total_blocks;
00278 int64_t pts;
00279
00280
00281 ape->junklength = 0;
00282
00283 tag = get_le32(pb);
00284 if (tag != MKTAG('M', 'A', 'C', ' '))
00285 return -1;
00286
00287 ape->fileversion = get_le16(pb);
00288
00289 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
00290 av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10);
00291 return -1;
00292 }
00293
00294 if (ape->fileversion >= 3980) {
00295 ape->padding1 = get_le16(pb);
00296 ape->descriptorlength = get_le32(pb);
00297 ape->headerlength = get_le32(pb);
00298 ape->seektablelength = get_le32(pb);
00299 ape->wavheaderlength = get_le32(pb);
00300 ape->audiodatalength = get_le32(pb);
00301 ape->audiodatalength_high = get_le32(pb);
00302 ape->wavtaillength = get_le32(pb);
00303 get_buffer(pb, ape->md5, 16);
00304
00305
00306
00307 if (ape->descriptorlength > 52)
00308 url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
00309
00310
00311 ape->compressiontype = get_le16(pb);
00312 ape->formatflags = get_le16(pb);
00313 ape->blocksperframe = get_le32(pb);
00314 ape->finalframeblocks = get_le32(pb);
00315 ape->totalframes = get_le32(pb);
00316 ape->bps = get_le16(pb);
00317 ape->channels = get_le16(pb);
00318 ape->samplerate = get_le32(pb);
00319 } else {
00320 ape->descriptorlength = 0;
00321 ape->headerlength = 32;
00322
00323 ape->compressiontype = get_le16(pb);
00324 ape->formatflags = get_le16(pb);
00325 ape->channels = get_le16(pb);
00326 ape->samplerate = get_le32(pb);
00327 ape->wavheaderlength = get_le32(pb);
00328 ape->wavtaillength = get_le32(pb);
00329 ape->totalframes = get_le32(pb);
00330 ape->finalframeblocks = get_le32(pb);
00331
00332 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
00333 url_fseek(pb, 4, SEEK_CUR);
00334 ape->headerlength += 4;
00335 }
00336
00337 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
00338 ape->seektablelength = get_le32(pb);
00339 ape->headerlength += 4;
00340 ape->seektablelength *= sizeof(int32_t);
00341 } else
00342 ape->seektablelength = ape->totalframes * sizeof(int32_t);
00343
00344 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
00345 ape->bps = 8;
00346 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
00347 ape->bps = 24;
00348 else
00349 ape->bps = 16;
00350
00351 if (ape->fileversion >= 3950)
00352 ape->blocksperframe = 73728 * 4;
00353 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
00354 ape->blocksperframe = 73728;
00355 else
00356 ape->blocksperframe = 9216;
00357
00358
00359 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
00360 url_fskip(pb, ape->wavheaderlength);
00361 }
00362
00363 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
00364 av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
00365 return -1;
00366 }
00367 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
00368 if(!ape->frames)
00369 return AVERROR_NOMEM;
00370 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
00371 ape->currentframe = 0;
00372
00373
00374 ape->totalsamples = ape->finalframeblocks;
00375 if (ape->totalframes > 1)
00376 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
00377
00378 if (ape->seektablelength > 0) {
00379 ape->seektable = av_malloc(ape->seektablelength);
00380 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
00381 ape->seektable[i] = get_le32(pb);
00382 }
00383
00384 ape->frames[0].pos = ape->firstframe;
00385 ape->frames[0].nblocks = ape->blocksperframe;
00386 ape->frames[0].skip = 0;
00387 for (i = 1; i < ape->totalframes; i++) {
00388 ape->frames[i].pos = ape->seektable[i];
00389 ape->frames[i].nblocks = ape->blocksperframe;
00390 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
00391 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
00392 }
00393 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
00394 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
00395
00396 for (i = 0; i < ape->totalframes; i++) {
00397 if(ape->frames[i].skip){
00398 ape->frames[i].pos -= ape->frames[i].skip;
00399 ape->frames[i].size += ape->frames[i].skip;
00400 }
00401 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
00402 }
00403
00404
00405 ape_dumpinfo(ape);
00406
00407
00408 if (!url_is_streamed(pb)) {
00409 ape_parse_tag(s);
00410 url_fseek(pb, 0, SEEK_SET);
00411 }
00412
00413 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype);
00414
00415
00416 st = av_new_stream(s, 0);
00417 if (!st)
00418 return -1;
00419
00420 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
00421
00422 st->codec->codec_type = CODEC_TYPE_AUDIO;
00423 st->codec->codec_id = CODEC_ID_APE;
00424 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
00425 st->codec->channels = ape->channels;
00426 st->codec->sample_rate = ape->samplerate;
00427 st->codec->bits_per_sample = ape->bps;
00428 st->codec->frame_size = MAC_SUBFRAME_SIZE;
00429
00430 st->nb_frames = ape->totalframes;
00431 s->start_time = 0;
00432 s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate;
00433 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
00434
00435 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
00436 st->codec->extradata_size = APE_EXTRADATA_SIZE;
00437 AV_WL16(st->codec->extradata + 0, ape->fileversion);
00438 AV_WL16(st->codec->extradata + 2, ape->compressiontype);
00439 AV_WL16(st->codec->extradata + 4, ape->formatflags);
00440
00441 pts = 0;
00442 for (i = 0; i < ape->totalframes; i++) {
00443 ape->frames[i].pts = pts;
00444 av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME);
00445 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
00446 }
00447
00448 return 0;
00449 }
00450
00451 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
00452 {
00453 int ret;
00454 int nblocks;
00455 APEContext *ape = s->priv_data;
00456 uint32_t extra_size = 8;
00457
00458 if (url_feof(s->pb))
00459 return AVERROR_IO;
00460 if (ape->currentframe > ape->totalframes)
00461 return AVERROR_IO;
00462
00463 url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
00464
00465
00466 if (ape->currentframe == (ape->totalframes - 1))
00467 nblocks = ape->finalframeblocks;
00468 else
00469 nblocks = ape->blocksperframe;
00470
00471 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
00472 return AVERROR_NOMEM;
00473
00474 AV_WL32(pkt->data , nblocks);
00475 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
00476 ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
00477
00478 pkt->pts = ape->frames[ape->currentframe].pts;
00479 pkt->stream_index = 0;
00480
00481
00482
00483 pkt->size = ret + extra_size;
00484
00485 ape->currentframe++;
00486
00487 return 0;
00488 }
00489
00490 static int ape_read_close(AVFormatContext * s)
00491 {
00492 APEContext *ape = s->priv_data;
00493
00494 av_freep(&ape->frames);
00495 av_freep(&ape->seektable);
00496 return 0;
00497 }
00498
00499 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00500 {
00501 AVStream *st = s->streams[stream_index];
00502 APEContext *ape = s->priv_data;
00503 int index = av_index_search_timestamp(st, timestamp, flags);
00504
00505 if (index < 0)
00506 return -1;
00507
00508 ape->currentframe = index;
00509 return 0;
00510 }
00511
00512 AVInputFormat ape_demuxer = {
00513 "ape",
00514 NULL_IF_CONFIG_SMALL("Monkey's Audio"),
00515 sizeof(APEContext),
00516 ape_probe,
00517 ape_read_header,
00518 ape_read_packet,
00519 ape_read_close,
00520 ape_read_seek,
00521 .extensions = "ape,apl,mac"
00522 };