00001 /* 00002 * Monkey's Audio APE demuxer 00003 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org> 00004 * based upon libdemac from Dave Chapman. 00005 * 00006 * This file is part of FFmpeg. 00007 * 00008 * FFmpeg is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * FFmpeg is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with FFmpeg; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include <stdio.h> 00024 00025 #include "libavutil/intreadwrite.h" 00026 #include "avformat.h" 00027 00028 #define ENABLE_DEBUG 0 00029 00030 /* The earliest and latest file formats supported by this library */ 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 /* APE tags */ 00046 #define APE_TAG_VERSION 2000 00047 #define APE_TAG_FOOTER_BYTES 32 00048 #define APE_TAG_FLAG_CONTAINS_HEADER (1 << 31) 00049 #define APE_TAG_FLAG_IS_HEADER (1 << 29) 00050 00051 typedef struct { 00052 int64_t pos; 00053 int nblocks; 00054 int size; 00055 int skip; 00056 int64_t pts; 00057 } APEFrame; 00058 00059 typedef struct { 00060 /* Derived fields */ 00061 uint32_t junklength; 00062 uint32_t firstframe; 00063 uint32_t totalsamples; 00064 int currentframe; 00065 APEFrame *frames; 00066 00067 /* Info from Descriptor Block */ 00068 char magic[4]; 00069 int16_t fileversion; 00070 int16_t padding1; 00071 uint32_t descriptorlength; 00072 uint32_t headerlength; 00073 uint32_t seektablelength; 00074 uint32_t wavheaderlength; 00075 uint32_t audiodatalength; 00076 uint32_t audiodatalength_high; 00077 uint32_t wavtaillength; 00078 uint8_t md5[16]; 00079 00080 /* Info from Header Block */ 00081 uint16_t compressiontype; 00082 uint16_t formatflags; 00083 uint32_t blocksperframe; 00084 uint32_t finalframeblocks; 00085 uint32_t totalframes; 00086 uint16_t bps; 00087 uint16_t channels; 00088 uint32_t samplerate; 00089 00090 /* Seektable */ 00091 uint32_t *seektable; 00092 } APEContext; 00093 00094 static void ape_tag_read_field(AVFormatContext *s) 00095 { 00096 ByteIOContext *pb = s->pb; 00097 uint8_t key[1024], value[1024]; 00098 uint32_t size; 00099 int i, l; 00100 00101 size = get_le32(pb); /* field size */ 00102 url_fskip(pb, 4); /* skip field flags */ 00103 00104 for (i=0; pb->buf_ptr[i]!='0' && pb->buf_ptr[i]>=0x20 && pb->buf_ptr[i]<=0x7E; i++); 00105 00106 l = FFMIN(i, sizeof(key) -1); 00107 get_buffer(pb, key, l); 00108 key[l] = 0; 00109 url_fskip(pb, 1 + i-l); 00110 l = FFMIN(size, sizeof(value)-1); 00111 get_buffer(pb, value, l); 00112 value[l] = 0; 00113 url_fskip(pb, size-l); 00114 if (l < size) 00115 av_log(s, AV_LOG_WARNING, "Too long '%s' tag was truncated.\n", key); 00116 av_metadata_set(&s->metadata, key, value); 00117 } 00118 00119 static void ape_parse_tag(AVFormatContext *s) 00120 { 00121 ByteIOContext *pb = s->pb; 00122 int file_size = url_fsize(pb); 00123 uint32_t val, fields, tag_bytes; 00124 uint8_t buf[8]; 00125 int i; 00126 00127 if (file_size < APE_TAG_FOOTER_BYTES) 00128 return; 00129 00130 url_fseek(pb, file_size - APE_TAG_FOOTER_BYTES, SEEK_SET); 00131 00132 get_buffer(pb, buf, 8); /* APETAGEX */ 00133 if (strncmp(buf, "APETAGEX", 8)) { 00134 return; 00135 } 00136 00137 val = get_le32(pb); /* APE tag version */ 00138 if (val > APE_TAG_VERSION) { 00139 av_log(s, AV_LOG_ERROR, "Unsupported tag version. (>=%d)\n", APE_TAG_VERSION); 00140 return; 00141 } 00142 00143 tag_bytes = get_le32(pb); /* tag size */ 00144 if (tag_bytes - APE_TAG_FOOTER_BYTES > (1024 * 1024 * 16)) { 00145 av_log(s, AV_LOG_ERROR, "Tag size is way too big\n"); 00146 return; 00147 } 00148 00149 fields = get_le32(pb); /* number of fields */ 00150 if (fields > 65536) { 00151 av_log(s, AV_LOG_ERROR, "Too many tag fields (%d)\n", fields); 00152 return; 00153 } 00154 00155 val = get_le32(pb); /* flags */ 00156 if (val & APE_TAG_FLAG_IS_HEADER) { 00157 av_log(s, AV_LOG_ERROR, "APE Tag is a header\n"); 00158 return; 00159 } 00160 00161 if (val & APE_TAG_FLAG_CONTAINS_HEADER) 00162 tag_bytes += 2*APE_TAG_FOOTER_BYTES; 00163 00164 url_fseek(pb, file_size - tag_bytes, SEEK_SET); 00165 00166 for (i=0; i<fields; i++) 00167 ape_tag_read_field(s); 00168 00169 #if ENABLE_DEBUG 00170 av_log(s, AV_LOG_DEBUG, "\nAPE Tags:\n\n"); 00171 av_log(s, AV_LOG_DEBUG, "title = %s\n", s->title); 00172 av_log(s, AV_LOG_DEBUG, "author = %s\n", s->author); 00173 av_log(s, AV_LOG_DEBUG, "copyright = %s\n", s->copyright); 00174 av_log(s, AV_LOG_DEBUG, "comment = %s\n", s->comment); 00175 av_log(s, AV_LOG_DEBUG, "album = %s\n", s->album); 00176 av_log(s, AV_LOG_DEBUG, "year = %d\n", s->year); 00177 av_log(s, AV_LOG_DEBUG, "track = %d\n", s->track); 00178 av_log(s, AV_LOG_DEBUG, "genre = %s\n", s->genre); 00179 #endif 00180 } 00181 00182 static int ape_probe(AVProbeData * p) 00183 { 00184 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ') 00185 return AVPROBE_SCORE_MAX; 00186 00187 return 0; 00188 } 00189 00190 static void ape_dumpinfo(AVFormatContext * s, APEContext * ape_ctx) 00191 { 00192 #if ENABLE_DEBUG 00193 int i; 00194 00195 av_log(s, AV_LOG_DEBUG, "Descriptor Block:\n\n"); 00196 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]); 00197 av_log(s, AV_LOG_DEBUG, "fileversion = %d\n", ape_ctx->fileversion); 00198 av_log(s, AV_LOG_DEBUG, "descriptorlength = %d\n", ape_ctx->descriptorlength); 00199 av_log(s, AV_LOG_DEBUG, "headerlength = %d\n", ape_ctx->headerlength); 00200 av_log(s, AV_LOG_DEBUG, "seektablelength = %d\n", ape_ctx->seektablelength); 00201 av_log(s, AV_LOG_DEBUG, "wavheaderlength = %d\n", ape_ctx->wavheaderlength); 00202 av_log(s, AV_LOG_DEBUG, "audiodatalength = %d\n", ape_ctx->audiodatalength); 00203 av_log(s, AV_LOG_DEBUG, "audiodatalength_high = %d\n", ape_ctx->audiodatalength_high); 00204 av_log(s, AV_LOG_DEBUG, "wavtaillength = %d\n", ape_ctx->wavtaillength); 00205 av_log(s, AV_LOG_DEBUG, "md5 = "); 00206 for (i = 0; i < 16; i++) 00207 av_log(s, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]); 00208 av_log(s, AV_LOG_DEBUG, "\n"); 00209 00210 av_log(s, AV_LOG_DEBUG, "\nHeader Block:\n\n"); 00211 00212 av_log(s, AV_LOG_DEBUG, "compressiontype = %d\n", ape_ctx->compressiontype); 00213 av_log(s, AV_LOG_DEBUG, "formatflags = %d\n", ape_ctx->formatflags); 00214 av_log(s, AV_LOG_DEBUG, "blocksperframe = %d\n", ape_ctx->blocksperframe); 00215 av_log(s, AV_LOG_DEBUG, "finalframeblocks = %d\n", ape_ctx->finalframeblocks); 00216 av_log(s, AV_LOG_DEBUG, "totalframes = %d\n", ape_ctx->totalframes); 00217 av_log(s, AV_LOG_DEBUG, "bps = %d\n", ape_ctx->bps); 00218 av_log(s, AV_LOG_DEBUG, "channels = %d\n", ape_ctx->channels); 00219 av_log(s, AV_LOG_DEBUG, "samplerate = %d\n", ape_ctx->samplerate); 00220 00221 av_log(s, AV_LOG_DEBUG, "\nSeektable\n\n"); 00222 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) { 00223 av_log(s, AV_LOG_DEBUG, "No seektable\n"); 00224 } else { 00225 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) { 00226 if (i < ape_ctx->totalframes - 1) { 00227 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]); 00228 } else { 00229 av_log(s, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]); 00230 } 00231 } 00232 } 00233 00234 av_log(s, AV_LOG_DEBUG, "\nFrames\n\n"); 00235 for (i = 0; i < ape_ctx->totalframes; i++) 00236 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); 00237 00238 av_log(s, AV_LOG_DEBUG, "\nCalculated information:\n\n"); 00239 av_log(s, AV_LOG_DEBUG, "junklength = %d\n", ape_ctx->junklength); 00240 av_log(s, AV_LOG_DEBUG, "firstframe = %d\n", ape_ctx->firstframe); 00241 av_log(s, AV_LOG_DEBUG, "totalsamples = %d\n", ape_ctx->totalsamples); 00242 #endif 00243 } 00244 00245 static int ape_read_header(AVFormatContext * s, AVFormatParameters * ap) 00246 { 00247 ByteIOContext *pb = s->pb; 00248 APEContext *ape = s->priv_data; 00249 AVStream *st; 00250 uint32_t tag; 00251 int i; 00252 int total_blocks; 00253 int64_t pts; 00254 00255 /* TODO: Skip any leading junk such as id3v2 tags */ 00256 ape->junklength = 0; 00257 00258 tag = get_le32(pb); 00259 if (tag != MKTAG('M', 'A', 'C', ' ')) 00260 return -1; 00261 00262 ape->fileversion = get_le16(pb); 00263 00264 if (ape->fileversion < APE_MIN_VERSION || ape->fileversion > APE_MAX_VERSION) { 00265 av_log(s, AV_LOG_ERROR, "Unsupported file version - %d.%02d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10); 00266 return -1; 00267 } 00268 00269 if (ape->fileversion >= 3980) { 00270 ape->padding1 = get_le16(pb); 00271 ape->descriptorlength = get_le32(pb); 00272 ape->headerlength = get_le32(pb); 00273 ape->seektablelength = get_le32(pb); 00274 ape->wavheaderlength = get_le32(pb); 00275 ape->audiodatalength = get_le32(pb); 00276 ape->audiodatalength_high = get_le32(pb); 00277 ape->wavtaillength = get_le32(pb); 00278 get_buffer(pb, ape->md5, 16); 00279 00280 /* Skip any unknown bytes at the end of the descriptor. 00281 This is for future compatibility */ 00282 if (ape->descriptorlength > 52) 00283 url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR); 00284 00285 /* Read header data */ 00286 ape->compressiontype = get_le16(pb); 00287 ape->formatflags = get_le16(pb); 00288 ape->blocksperframe = get_le32(pb); 00289 ape->finalframeblocks = get_le32(pb); 00290 ape->totalframes = get_le32(pb); 00291 ape->bps = get_le16(pb); 00292 ape->channels = get_le16(pb); 00293 ape->samplerate = get_le32(pb); 00294 } else { 00295 ape->descriptorlength = 0; 00296 ape->headerlength = 32; 00297 00298 ape->compressiontype = get_le16(pb); 00299 ape->formatflags = get_le16(pb); 00300 ape->channels = get_le16(pb); 00301 ape->samplerate = get_le32(pb); 00302 ape->wavheaderlength = get_le32(pb); 00303 ape->wavtaillength = get_le32(pb); 00304 ape->totalframes = get_le32(pb); 00305 ape->finalframeblocks = get_le32(pb); 00306 00307 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) { 00308 url_fseek(pb, 4, SEEK_CUR); /* Skip the peak level */ 00309 ape->headerlength += 4; 00310 } 00311 00312 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) { 00313 ape->seektablelength = get_le32(pb); 00314 ape->headerlength += 4; 00315 ape->seektablelength *= sizeof(int32_t); 00316 } else 00317 ape->seektablelength = ape->totalframes * sizeof(int32_t); 00318 00319 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT) 00320 ape->bps = 8; 00321 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT) 00322 ape->bps = 24; 00323 else 00324 ape->bps = 16; 00325 00326 if (ape->fileversion >= 3950) 00327 ape->blocksperframe = 73728 * 4; 00328 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000)) 00329 ape->blocksperframe = 73728; 00330 else 00331 ape->blocksperframe = 9216; 00332 00333 /* Skip any stored wav header */ 00334 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER)) 00335 url_fskip(pb, ape->wavheaderlength); 00336 } 00337 00338 if(!ape->totalframes){ 00339 av_log(s, AV_LOG_ERROR, "No frames in the file!\n"); 00340 return AVERROR(EINVAL); 00341 } 00342 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){ 00343 av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes); 00344 return -1; 00345 } 00346 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame)); 00347 if(!ape->frames) 00348 return AVERROR_NOMEM; 00349 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength; 00350 ape->currentframe = 0; 00351 00352 00353 ape->totalsamples = ape->finalframeblocks; 00354 if (ape->totalframes > 1) 00355 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1); 00356 00357 if (ape->seektablelength > 0) { 00358 ape->seektable = av_malloc(ape->seektablelength); 00359 if (!ape->seektable) 00360 return AVERROR(ENOMEM); 00361 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++) 00362 ape->seektable[i] = get_le32(pb); 00363 } 00364 00365 ape->frames[0].pos = ape->firstframe; 00366 ape->frames[0].nblocks = ape->blocksperframe; 00367 ape->frames[0].skip = 0; 00368 for (i = 1; i < ape->totalframes; i++) { 00369 ape->frames[i].pos = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe; 00370 ape->frames[i].nblocks = ape->blocksperframe; 00371 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos; 00372 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3; 00373 } 00374 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4; 00375 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks; 00376 00377 for (i = 0; i < ape->totalframes; i++) { 00378 if(ape->frames[i].skip){ 00379 ape->frames[i].pos -= ape->frames[i].skip; 00380 ape->frames[i].size += ape->frames[i].skip; 00381 } 00382 ape->frames[i].size = (ape->frames[i].size + 3) & ~3; 00383 } 00384 00385 00386 ape_dumpinfo(s, ape); 00387 00388 /* try to read APE tags */ 00389 if (!url_is_streamed(pb)) { 00390 ape_parse_tag(s); 00391 url_fseek(pb, 0, SEEK_SET); 00392 } 00393 00394 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype); 00395 00396 /* now we are ready: build format streams */ 00397 st = av_new_stream(s, 0); 00398 if (!st) 00399 return -1; 00400 00401 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks; 00402 00403 st->codec->codec_type = CODEC_TYPE_AUDIO; 00404 st->codec->codec_id = CODEC_ID_APE; 00405 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' '); 00406 st->codec->channels = ape->channels; 00407 st->codec->sample_rate = ape->samplerate; 00408 st->codec->bits_per_coded_sample = ape->bps; 00409 st->codec->frame_size = MAC_SUBFRAME_SIZE; 00410 00411 st->nb_frames = ape->totalframes; 00412 s->start_time = 0; 00413 s->duration = (int64_t) total_blocks * AV_TIME_BASE / ape->samplerate; 00414 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate); 00415 00416 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE); 00417 st->codec->extradata_size = APE_EXTRADATA_SIZE; 00418 AV_WL16(st->codec->extradata + 0, ape->fileversion); 00419 AV_WL16(st->codec->extradata + 2, ape->compressiontype); 00420 AV_WL16(st->codec->extradata + 4, ape->formatflags); 00421 00422 pts = 0; 00423 for (i = 0; i < ape->totalframes; i++) { 00424 ape->frames[i].pts = pts; 00425 av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME); 00426 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE; 00427 } 00428 00429 return 0; 00430 } 00431 00432 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt) 00433 { 00434 int ret; 00435 int nblocks; 00436 APEContext *ape = s->priv_data; 00437 uint32_t extra_size = 8; 00438 00439 if (url_feof(s->pb)) 00440 return AVERROR_IO; 00441 if (ape->currentframe > ape->totalframes) 00442 return AVERROR_IO; 00443 00444 url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET); 00445 00446 /* Calculate how many blocks there are in this frame */ 00447 if (ape->currentframe == (ape->totalframes - 1)) 00448 nblocks = ape->finalframeblocks; 00449 else 00450 nblocks = ape->blocksperframe; 00451 00452 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0) 00453 return AVERROR_NOMEM; 00454 00455 AV_WL32(pkt->data , nblocks); 00456 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip); 00457 ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size); 00458 00459 pkt->pts = ape->frames[ape->currentframe].pts; 00460 pkt->stream_index = 0; 00461 00462 /* note: we need to modify the packet size here to handle the last 00463 packet */ 00464 pkt->size = ret + extra_size; 00465 00466 ape->currentframe++; 00467 00468 return 0; 00469 } 00470 00471 static int ape_read_close(AVFormatContext * s) 00472 { 00473 APEContext *ape = s->priv_data; 00474 00475 av_freep(&ape->frames); 00476 av_freep(&ape->seektable); 00477 return 0; 00478 } 00479 00480 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) 00481 { 00482 AVStream *st = s->streams[stream_index]; 00483 APEContext *ape = s->priv_data; 00484 int index = av_index_search_timestamp(st, timestamp, flags); 00485 00486 if (index < 0) 00487 return -1; 00488 00489 ape->currentframe = index; 00490 return 0; 00491 } 00492 00493 AVInputFormat ape_demuxer = { 00494 "ape", 00495 NULL_IF_CONFIG_SMALL("Monkey's Audio"), 00496 sizeof(APEContext), 00497 ape_probe, 00498 ape_read_header, 00499 ape_read_packet, 00500 ape_read_close, 00501 ape_read_seek, 00502 .extensions = "ape,apl,mac" 00503 };