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 av_log(NULL, AV_LOG_ERROR, "Invalid APE Tags\n");
00160 return;
00161 }
00162
00163 val = get_le32(pb);
00164 if (val > APE_TAG_VERSION) {
00165 av_log(NULL, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION);
00166 return;
00167 }
00168
00169 tag_bytes = get_le32(pb);
00170 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) {
00171 av_log(NULL, AV_LOG_ERROR, "Tag size is way too big\n");
00172 return;
00173 }
00174
00175 fields = get_le32(pb);
00176 if (fields > 65536) {
00177 av_log(NULL, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields);
00178 return;
00179 }
00180
00181 val = get_le32(pb);
00182 if (val & APE_TAG_FLAG_IS_HEADER) {
00183 av_log(NULL, AV_LOG_ERROR, "APE Tag is a header\n");
00184 return;
00185 }
00186
00187 if (val & APE_TAG_FLAG_CONTAINS_HEADER)
00188 tag_bytes += 2*APE_TAG_FOOTER_BYTES;
00189
00190 url_fseek(pb, file_size - tag_bytes, SEEK_SET);
00191
00192 for (i=0; i<fields; i++)
00193 ape_tag_read_field(s);
00194
00195 #if ENABLE_DEBUG
00196 av_log(NULL, AV_LOG_DEBUG, "\nAPE Tags:\n\n");
00197 av_log(NULL, AV_LOG_DEBUG, "title = %s\n", s->title);
00198 av_log(NULL, AV_LOG_DEBUG, "author = %s\n", s->author);
00199 av_log(NULL, AV_LOG_DEBUG, "copyright = %s\n", s->copyright);
00200 av_log(NULL, AV_LOG_DEBUG, "comment = %s\n", s->comment);
00201 av_log(NULL, AV_LOG_DEBUG, "album = %s\n", s->album);
00202 av_log(NULL, AV_LOG_DEBUG, "year = %d\n", s->year);
00203 av_log(NULL, AV_LOG_DEBUG, "track = %d\n", s->track);
00204 av_log(NULL, AV_LOG_DEBUG, "genre = %s\n", s->genre);
00205 #endif
00206 }
00207
00208 static int ape_probe(AVProbeData * p)
00209 {
00210 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ')
00211 return AVPROBE_SCORE_MAX;
00212
00213 return 0;
00214 }
00215
00216 static void ape_dumpinfo(APEContext * ape_ctx)
00217 {
00218 #if ENABLE_DEBUG
00219 int i;
00220
00221 av_log(NULL, AV_LOG_DEBUG, "Descriptor Block:\n\n");
00222 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]);
00223 av_log(NULL, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion);
00224 av_log(NULL, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength);
00225 av_log(NULL, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength);
00226 av_log(NULL, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength);
00227 av_log(NULL, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength);
00228 av_log(NULL, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength);
00229 av_log(NULL, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high);
00230 av_log(NULL, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength);
00231 av_log(NULL, AV_LOG_DEBUG, "md5 = ");
00232 for (i = 0; i < 16; i++)
00233 av_log(NULL, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]);
00234 av_log(NULL, AV_LOG_DEBUG, "\n");
00235
00236 av_log(NULL, AV_LOG_DEBUG, "\nHeader Block:\n\n");
00237
00238 av_log(NULL, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype);
00239 av_log(NULL, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags);
00240 av_log(NULL, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe);
00241 av_log(NULL, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks);
00242 av_log(NULL, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes);
00243 av_log(NULL, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps);
00244 av_log(NULL, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels);
00245 av_log(NULL, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate);
00246
00247 av_log(NULL, AV_LOG_DEBUG, "\nSeektable\n\n");
00248 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) {
00249 av_log(NULL, AV_LOG_DEBUG, "No seektable\n");
00250 } else {
00251 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
00252 if (i < ape_ctx->totalframes - 1) {
00253 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]);
00254 } else {
00255 av_log(NULL, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]);
00256 }
00257 }
00258 }
00259
00260 av_log(NULL, AV_LOG_DEBUG, "\nFrames\n\n");
00261 for (i = 0; i < ape_ctx->totalframes; i++)
00262 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);
00263
00264 av_log(NULL, AV_LOG_DEBUG, "\nCalculated information:\n\n");
00265 av_log(NULL, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength);
00266 av_log(NULL, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe);
00267 av_log(NULL, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples);
00268 #endif
00269 }
00270
00271 static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap)
00272 {
00273 ByteIOContext *pb = s->pb;
00274 APEContext *ape = s->priv_data;
00275 AVStream *st;
00276 uint32_t tag;
00277 int i;
00278 int total_blocks;
00279 int64_t pts;
00280
00281
00282 ape->junklength = 0;
00283
00284 tag = get_le32(pb);
00285 if (tag != MKTAG('M', 'A', 'C', ' '))
00286 return -1;
00287
00288 ape->fileversion = get_le16(pb);
00289
00290 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) {
00291 av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10);
00292 return -1;
00293 }
00294
00295 if (ape->fileversion >= 3980) {
00296 ape->padding1 = get_le16(pb);
00297 ape->descriptorlength = get_le32(pb);
00298 ape->headerlength = get_le32(pb);
00299 ape->seektablelength = get_le32(pb);
00300 ape->wavheaderlength = get_le32(pb);
00301 ape->audiodatalength = get_le32(pb);
00302 ape->audiodatalength_high = get_le32(pb);
00303 ape->wavtaillength = get_le32(pb);
00304 get_buffer(pb, ape->md5, 16);
00305
00306
00307
00308 if (ape->descriptorlength > 52)
00309 url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
00310
00311
00312 ape->compressiontype = get_le16(pb);
00313 ape->formatflags = get_le16(pb);
00314 ape->blocksperframe = get_le32(pb);
00315 ape->finalframeblocks = get_le32(pb);
00316 ape->totalframes = get_le32(pb);
00317 ape->bps = get_le16(pb);
00318 ape->channels = get_le16(pb);
00319 ape->samplerate = get_le32(pb);
00320 } else {
00321 ape->descriptorlength = 0;
00322 ape->headerlength = 32;
00323
00324 ape->compressiontype = get_le16(pb);
00325 ape->formatflags = get_le16(pb);
00326 ape->channels = get_le16(pb);
00327 ape->samplerate = get_le32(pb);
00328 ape->wavheaderlength = get_le32(pb);
00329 ape->wavtaillength = get_le32(pb);
00330 ape->totalframes = get_le32(pb);
00331 ape->finalframeblocks = get_le32(pb);
00332
00333 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
00334 url_fseek(pb, 4, SEEK_CUR);
00335 ape->headerlength += 4;
00336 }
00337
00338 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
00339 ape->seektablelength = get_le32(pb);
00340 ape->headerlength += 4;
00341 ape->seektablelength *= sizeof(int32_t);
00342 } else
00343 ape->seektablelength = ape->totalframes * sizeof(int32_t);
00344
00345 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT)
00346 ape->bps = 8;
00347 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT)
00348 ape->bps = 24;
00349 else
00350 ape->bps = 16;
00351
00352 if (ape->fileversion >= 3950)
00353 ape->blocksperframe = 73728 * 4;
00354 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000))
00355 ape->blocksperframe = 73728;
00356 else
00357 ape->blocksperframe = 9216;
00358
00359
00360 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
00361 url_fskip(pb, ape->wavheaderlength);
00362 }
00363
00364 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
00365 av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
00366 return -1;
00367 }
00368 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame));
00369 if(!ape->frames)
00370 return AVERROR_NOMEM;
00371 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
00372 ape->currentframe = 0;
00373
00374
00375 ape->totalsamples = ape->finalframeblocks;
00376 if (ape->totalframes > 1)
00377 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
00378
00379 if (ape->seektablelength > 0) {
00380 ape->seektable = av_malloc(ape->seektablelength);
00381 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
00382 ape->seektable[i] = get_le32(pb);
00383 }
00384
00385 ape->frames[0].pos = ape->firstframe;
00386 ape->frames[0].nblocks = ape->blocksperframe;
00387 ape->frames[0].skip = 0;
00388 for (i = 1; i < ape->totalframes; i++) {
00389 ape->frames[i].pos = ape->seektable[i];
00390 ape->frames[i].nblocks = ape->blocksperframe;
00391 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
00392 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3;
00393 }
00394 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4;
00395 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
00396
00397 for (i = 0; i < ape->totalframes; i++) {
00398 if(ape->frames[i].skip){
00399 ape->frames[i].pos -= ape->frames[i].skip;
00400 ape->frames[i].size += ape->frames[i].skip;
00401 }
00402 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
00403 }
00404
00405
00406 ape_dumpinfo(ape);
00407
00408
00409 if (!url_is_streamed(pb)) {
00410 ape_parse_tag(s);
00411 url_fseek(pb, 0, SEEK_SET);
00412 }
00413
00414 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype);
00415
00416
00417 st = av_new_stream(s, 0);
00418 if (!st)
00419 return -1;
00420
00421 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
00422
00423 st->codec->codec_type = CODEC_TYPE_AUDIO;
00424 st->codec->codec_id = CODEC_ID_APE;
00425 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
00426 st->codec->channels = ape->channels;
00427 st->codec->sample_rate = ape->samplerate;
00428 st->codec->bits_per_sample = ape->bps;
00429 st->codec->frame_size = MAC_SUBFRAME_SIZE;
00430
00431 st->nb_frames = ape->totalframes;
00432 s->start_time = 0;
00433 s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate;
00434 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
00435
00436 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
00437 st->codec->extradata_size = APE_EXTRADATA_SIZE;
00438 AV_WL16(st->codec->extradata + 0, ape->fileversion);
00439 AV_WL16(st->codec->extradata + 2, ape->compressiontype);
00440 AV_WL16(st->codec->extradata + 4, ape->formatflags);
00441
00442 pts = 0;
00443 for (i = 0; i < ape->totalframes; i++) {
00444 ape->frames[i].pts = pts;
00445 av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME);
00446 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
00447 }
00448
00449 return 0;
00450 }
00451
00452 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
00453 {
00454 int ret;
00455 int nblocks;
00456 APEContext *ape = s->priv_data;
00457 uint32_t extra_size = 8;
00458
00459 if (url_feof(s->pb))
00460 return AVERROR_IO;
00461 if (ape->currentframe > ape->totalframes)
00462 return AVERROR_IO;
00463
00464 url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
00465
00466
00467 if (ape->currentframe == (ape->totalframes - 1))
00468 nblocks = ape->finalframeblocks;
00469 else
00470 nblocks = ape->blocksperframe;
00471
00472 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
00473 return AVERROR_NOMEM;
00474
00475 AV_WL32(pkt->data , nblocks);
00476 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
00477 ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
00478
00479 pkt->pts = ape->frames[ape->currentframe].pts;
00480 pkt->stream_index = 0;
00481
00482
00483
00484 pkt->size = ret + extra_size;
00485
00486 ape->currentframe++;
00487
00488 return 0;
00489 }
00490
00491 static int ape_read_close(AVFormatContext * s)
00492 {
00493 APEContext *ape = s->priv_data;
00494
00495 av_freep(&ape->frames);
00496 av_freep(&ape->seektable);
00497 return 0;
00498 }
00499
00500 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00501 {
00502 AVStream *st = s->streams[stream_index];
00503 APEContext *ape = s->priv_data;
00504 int index = av_index_search_timestamp(st, timestamp, flags);
00505
00506 if (index < 0)
00507 return -1;
00508
00509 ape->currentframe = index;
00510 return 0;
00511 }
00512
00513 AVInputFormat ape_demuxer = {
00514 "ape",
00515 "Monkey's Audio",
00516 sizeof(APEContext),
00517 ape_probe,
00518 ape_read_header,
00519 ape_read_packet,
00520 ape_read_close,
00521 ape_read_seek,
00522 .extensions = "ape,apl,mac"
00523 };