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, AVFormatParameters * ap)
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;
00163 int64_t pts;
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].size = ape->finalframeblocks * 4;
00296 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
00297
00298 for (i = 0; i < ape->totalframes; i++) {
00299 if(ape->frames[i].skip){
00300 ape->frames[i].pos -= ape->frames[i].skip;
00301 ape->frames[i].size += ape->frames[i].skip;
00302 }
00303 ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
00304 }
00305
00306
00307 ape_dumpinfo(s, ape);
00308
00309
00310 if (pb->seekable) {
00311 ff_ape_parse_tag(s);
00312 avio_seek(pb, 0, SEEK_SET);
00313 }
00314
00315 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %"PRIu16"\n",
00316 ape->fileversion / 1000, (ape->fileversion % 1000) / 10,
00317 ape->compressiontype);
00318
00319
00320 st = avformat_new_stream(s, NULL);
00321 if (!st)
00322 return -1;
00323
00324 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
00325
00326 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00327 st->codec->codec_id = CODEC_ID_APE;
00328 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' ');
00329 st->codec->channels = ape->channels;
00330 st->codec->sample_rate = ape->samplerate;
00331 st->codec->bits_per_coded_sample = ape->bps;
00332 st->codec->frame_size = MAC_SUBFRAME_SIZE;
00333
00334 st->nb_frames = ape->totalframes;
00335 st->start_time = 0;
00336 st->duration = total_blocks / MAC_SUBFRAME_SIZE;
00337 avpriv_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, 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 / MAC_SUBFRAME_SIZE;
00350 }
00351
00352 return 0;
00353 }
00354
00355 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
00356 {
00357 int ret;
00358 int nblocks;
00359 APEContext *ape = s->priv_data;
00360 uint32_t extra_size = 8;
00361
00362 if (url_feof(s->pb))
00363 return AVERROR(EIO);
00364 if (ape->currentframe > ape->totalframes)
00365 return AVERROR(EIO);
00366
00367 avio_seek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
00368
00369
00370 if (ape->currentframe == (ape->totalframes - 1))
00371 nblocks = ape->finalframeblocks;
00372 else
00373 nblocks = ape->blocksperframe;
00374
00375 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0)
00376 return AVERROR(ENOMEM);
00377
00378 AV_WL32(pkt->data , nblocks);
00379 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
00380 ret = avio_read(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
00381
00382 pkt->pts = ape->frames[ape->currentframe].pts;
00383 pkt->stream_index = 0;
00384
00385
00386
00387 pkt->size = ret + extra_size;
00388
00389 ape->currentframe++;
00390
00391 return 0;
00392 }
00393
00394 static int ape_read_close(AVFormatContext * s)
00395 {
00396 APEContext *ape = s->priv_data;
00397
00398 av_freep(&ape->frames);
00399 av_freep(&ape->seektable);
00400 return 0;
00401 }
00402
00403 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00404 {
00405 AVStream *st = s->streams[stream_index];
00406 APEContext *ape = s->priv_data;
00407 int index = av_index_search_timestamp(st, timestamp, flags);
00408
00409 if (index < 0)
00410 return -1;
00411
00412 ape->currentframe = index;
00413 return 0;
00414 }
00415
00416 AVInputFormat ff_ape_demuxer = {
00417 .name = "ape",
00418 .long_name = NULL_IF_CONFIG_SMALL("Monkey's Audio"),
00419 .priv_data_size = sizeof(APEContext),
00420 .read_probe = ape_probe,
00421 .read_header = ape_read_header,
00422 .read_packet = ape_read_packet,
00423 .read_close = ape_read_close,
00424 .read_seek = ape_read_seek,
00425 .extensions = "ape,apl,mac"
00426 };