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