Libav 0.7.1
|
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 Libav. 00007 * 00008 * Libav 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 * Libav 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 Libav; 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 #include "apetag.h" 00028 00029 /* The earliest and latest file formats supported by this library */ 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 typedef struct { 00045 int64_t pos; 00046 int nblocks; 00047 int size; 00048 int skip; 00049 int64_t pts; 00050 } APEFrame; 00051 00052 typedef struct { 00053 /* Derived fields */ 00054 uint32_t junklength; 00055 uint32_t firstframe; 00056 uint32_t totalsamples; 00057 int currentframe; 00058 APEFrame *frames; 00059 00060 /* Info from Descriptor Block */ 00061 char magic[4]; 00062 int16_t fileversion; 00063 int16_t padding1; 00064 uint32_t descriptorlength; 00065 uint32_t headerlength; 00066 uint32_t seektablelength; 00067 uint32_t wavheaderlength; 00068 uint32_t audiodatalength; 00069 uint32_t audiodatalength_high; 00070 uint32_t wavtaillength; 00071 uint8_t md5[16]; 00072 00073 /* Info from Header Block */ 00074 uint16_t compressiontype; 00075 uint16_t formatflags; 00076 uint32_t blocksperframe; 00077 uint32_t finalframeblocks; 00078 uint32_t totalframes; 00079 uint16_t bps; 00080 uint16_t channels; 00081 uint32_t samplerate; 00082 00083 /* Seektable */ 00084 uint32_t *seektable; 00085 } APEContext; 00086 00087 static int ape_probe(AVProbeData * p) 00088 { 00089 if (p->buf[0] == 'M' && p->buf[1] == 'A' && p->buf[2] == 'C' && p->buf[3] == ' ') 00090 return AVPROBE_SCORE_MAX; 00091 00092 return 0; 00093 } 00094 00095 static void ape_dumpinfo(AVFormatContext * s, APEContext * ape_ctx) 00096 { 00097 #ifdef DEBUG 00098 int i; 00099 00100 av_log(s, AV_LOG_DEBUG, "Descriptor Block:\n\n"); 00101 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]); 00102 av_log(s, AV_LOG_DEBUG, "fileversion = %"PRId16"\n", ape_ctx->fileversion); 00103 av_log(s, AV_LOG_DEBUG, "descriptorlength = %"PRIu32"\n", ape_ctx->descriptorlength); 00104 av_log(s, AV_LOG_DEBUG, "headerlength = %"PRIu32"\n", ape_ctx->headerlength); 00105 av_log(s, AV_LOG_DEBUG, "seektablelength = %"PRIu32"\n", ape_ctx->seektablelength); 00106 av_log(s, AV_LOG_DEBUG, "wavheaderlength = %"PRIu32"\n", ape_ctx->wavheaderlength); 00107 av_log(s, AV_LOG_DEBUG, "audiodatalength = %"PRIu32"\n", ape_ctx->audiodatalength); 00108 av_log(s, AV_LOG_DEBUG, "audiodatalength_high = %"PRIu32"\n", ape_ctx->audiodatalength_high); 00109 av_log(s, AV_LOG_DEBUG, "wavtaillength = %"PRIu32"\n", ape_ctx->wavtaillength); 00110 av_log(s, AV_LOG_DEBUG, "md5 = "); 00111 for (i = 0; i < 16; i++) 00112 av_log(s, AV_LOG_DEBUG, "%02x", ape_ctx->md5[i]); 00113 av_log(s, AV_LOG_DEBUG, "\n"); 00114 00115 av_log(s, AV_LOG_DEBUG, "\nHeader Block:\n\n"); 00116 00117 av_log(s, AV_LOG_DEBUG, "compressiontype = %"PRIu16"\n", ape_ctx->compressiontype); 00118 av_log(s, AV_LOG_DEBUG, "formatflags = %"PRIu16"\n", ape_ctx->formatflags); 00119 av_log(s, AV_LOG_DEBUG, "blocksperframe = %"PRIu32"\n", ape_ctx->blocksperframe); 00120 av_log(s, AV_LOG_DEBUG, "finalframeblocks = %"PRIu32"\n", ape_ctx->finalframeblocks); 00121 av_log(s, AV_LOG_DEBUG, "totalframes = %"PRIu32"\n", ape_ctx->totalframes); 00122 av_log(s, AV_LOG_DEBUG, "bps = %"PRIu16"\n", ape_ctx->bps); 00123 av_log(s, AV_LOG_DEBUG, "channels = %"PRIu16"\n", ape_ctx->channels); 00124 av_log(s, AV_LOG_DEBUG, "samplerate = %"PRIu32"\n", ape_ctx->samplerate); 00125 00126 av_log(s, AV_LOG_DEBUG, "\nSeektable\n\n"); 00127 if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) { 00128 av_log(s, AV_LOG_DEBUG, "No seektable\n"); 00129 } else { 00130 for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) { 00131 if (i < ape_ctx->totalframes - 1) { 00132 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]); 00133 } else { 00134 av_log(s, AV_LOG_DEBUG, "%8d %d\n", i, ape_ctx->seektable[i]); 00135 } 00136 } 00137 } 00138 00139 av_log(s, AV_LOG_DEBUG, "\nFrames\n\n"); 00140 for (i = 0; i < ape_ctx->totalframes; i++) 00141 av_log(s, AV_LOG_DEBUG, "%8d %8"PRId64" %8d (%d samples)\n", i, 00142 ape_ctx->frames[i].pos, ape_ctx->frames[i].size, 00143 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 = %"PRIu32"\n", ape_ctx->junklength); 00147 av_log(s, AV_LOG_DEBUG, "firstframe = %"PRIu32"\n", ape_ctx->firstframe); 00148 av_log(s, AV_LOG_DEBUG, "totalsamples = %"PRIu32"\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 /* Skip any leading junk such as id3v2 tags */ 00163 ape->junklength = avio_tell(pb); 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 - %"PRId16".%02"PRId16"\n", 00173 ape->fileversion / 1000, (ape->fileversion % 1000) / 10); 00174 return -1; 00175 } 00176 00177 if (ape->fileversion >= 3980) { 00178 ape->padding1 = avio_rl16(pb); 00179 ape->descriptorlength = avio_rl32(pb); 00180 ape->headerlength = avio_rl32(pb); 00181 ape->seektablelength = avio_rl32(pb); 00182 ape->wavheaderlength = avio_rl32(pb); 00183 ape->audiodatalength = avio_rl32(pb); 00184 ape->audiodatalength_high = avio_rl32(pb); 00185 ape->wavtaillength = avio_rl32(pb); 00186 avio_read(pb, ape->md5, 16); 00187 00188 /* Skip any unknown bytes at the end of the descriptor. 00189 This is for future compatibility */ 00190 if (ape->descriptorlength > 52) 00191 avio_skip(pb, ape->descriptorlength - 52); 00192 00193 /* Read header data */ 00194 ape->compressiontype = avio_rl16(pb); 00195 ape->formatflags = avio_rl16(pb); 00196 ape->blocksperframe = avio_rl32(pb); 00197 ape->finalframeblocks = avio_rl32(pb); 00198 ape->totalframes = avio_rl32(pb); 00199 ape->bps = avio_rl16(pb); 00200 ape->channels = avio_rl16(pb); 00201 ape->samplerate = avio_rl32(pb); 00202 } else { 00203 ape->descriptorlength = 0; 00204 ape->headerlength = 32; 00205 00206 ape->compressiontype = avio_rl16(pb); 00207 ape->formatflags = avio_rl16(pb); 00208 ape->channels = avio_rl16(pb); 00209 ape->samplerate = avio_rl32(pb); 00210 ape->wavheaderlength = avio_rl32(pb); 00211 ape->wavtaillength = avio_rl32(pb); 00212 ape->totalframes = avio_rl32(pb); 00213 ape->finalframeblocks = avio_rl32(pb); 00214 00215 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) { 00216 avio_skip(pb, 4); /* Skip the peak level */ 00217 ape->headerlength += 4; 00218 } 00219 00220 if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) { 00221 ape->seektablelength = avio_rl32(pb); 00222 ape->headerlength += 4; 00223 ape->seektablelength *= sizeof(int32_t); 00224 } else 00225 ape->seektablelength = ape->totalframes * sizeof(int32_t); 00226 00227 if (ape->formatflags & MAC_FORMAT_FLAG_8_BIT) 00228 ape->bps = 8; 00229 else if (ape->formatflags & MAC_FORMAT_FLAG_24_BIT) 00230 ape->bps = 24; 00231 else 00232 ape->bps = 16; 00233 00234 if (ape->fileversion >= 3950) 00235 ape->blocksperframe = 73728 * 4; 00236 else if (ape->fileversion >= 3900 || (ape->fileversion >= 3800 && ape->compressiontype >= 4000)) 00237 ape->blocksperframe = 73728; 00238 else 00239 ape->blocksperframe = 9216; 00240 00241 /* Skip any stored wav header */ 00242 if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER)) 00243 avio_skip(pb, ape->wavheaderlength); 00244 } 00245 00246 if(!ape->totalframes){ 00247 av_log(s, AV_LOG_ERROR, "No frames in the file!\n"); 00248 return AVERROR(EINVAL); 00249 } 00250 if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){ 00251 av_log(s, AV_LOG_ERROR, "Too many frames: %"PRIu32"\n", 00252 ape->totalframes); 00253 return -1; 00254 } 00255 if (ape->seektablelength && (ape->seektablelength / sizeof(*ape->seektable)) < ape->totalframes) { 00256 av_log(s, AV_LOG_ERROR, "Number of seek entries is less than number of frames: %ld vs. %"PRIu32"\n", 00257 ape->seektablelength / sizeof(*ape->seektable), ape->totalframes); 00258 return AVERROR_INVALIDDATA; 00259 } 00260 ape->frames = av_malloc(ape->totalframes * sizeof(APEFrame)); 00261 if(!ape->frames) 00262 return AVERROR(ENOMEM); 00263 ape->firstframe = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength; 00264 ape->currentframe = 0; 00265 00266 00267 ape->totalsamples = ape->finalframeblocks; 00268 if (ape->totalframes > 1) 00269 ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1); 00270 00271 if (ape->seektablelength > 0) { 00272 ape->seektable = av_malloc(ape->seektablelength); 00273 if (!ape->seektable) 00274 return AVERROR(ENOMEM); 00275 for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++) 00276 ape->seektable[i] = avio_rl32(pb); 00277 } 00278 00279 ape->frames[0].pos = ape->firstframe; 00280 ape->frames[0].nblocks = ape->blocksperframe; 00281 ape->frames[0].skip = 0; 00282 for (i = 1; i < ape->totalframes; i++) { 00283 ape->frames[i].pos = ape->seektable[i] + ape->junklength; 00284 ape->frames[i].nblocks = ape->blocksperframe; 00285 ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos; 00286 ape->frames[i].skip = (ape->frames[i].pos - ape->frames[0].pos) & 3; 00287 } 00288 ape->frames[ape->totalframes - 1].size = ape->finalframeblocks * 4; 00289 ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks; 00290 00291 for (i = 0; i < ape->totalframes; i++) { 00292 if(ape->frames[i].skip){ 00293 ape->frames[i].pos -= ape->frames[i].skip; 00294 ape->frames[i].size += ape->frames[i].skip; 00295 } 00296 ape->frames[i].size = (ape->frames[i].size + 3) & ~3; 00297 } 00298 00299 00300 ape_dumpinfo(s, ape); 00301 00302 /* try to read APE tags */ 00303 if (pb->seekable) { 00304 ff_ape_parse_tag(s); 00305 avio_seek(pb, 0, SEEK_SET); 00306 } 00307 00308 av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %"PRIu16"\n", 00309 ape->fileversion / 1000, (ape->fileversion % 1000) / 10, 00310 ape->compressiontype); 00311 00312 /* now we are ready: build format streams */ 00313 st = av_new_stream(s, 0); 00314 if (!st) 00315 return -1; 00316 00317 total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks; 00318 00319 st->codec->codec_type = AVMEDIA_TYPE_AUDIO; 00320 st->codec->codec_id = CODEC_ID_APE; 00321 st->codec->codec_tag = MKTAG('A', 'P', 'E', ' '); 00322 st->codec->channels = ape->channels; 00323 st->codec->sample_rate = ape->samplerate; 00324 st->codec->bits_per_coded_sample = ape->bps; 00325 st->codec->frame_size = MAC_SUBFRAME_SIZE; 00326 00327 st->nb_frames = ape->totalframes; 00328 st->start_time = 0; 00329 st->duration = total_blocks / MAC_SUBFRAME_SIZE; 00330 av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate); 00331 00332 st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE); 00333 st->codec->extradata_size = APE_EXTRADATA_SIZE; 00334 AV_WL16(st->codec->extradata + 0, ape->fileversion); 00335 AV_WL16(st->codec->extradata + 2, ape->compressiontype); 00336 AV_WL16(st->codec->extradata + 4, ape->formatflags); 00337 00338 pts = 0; 00339 for (i = 0; i < ape->totalframes; i++) { 00340 ape->frames[i].pts = pts; 00341 av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME); 00342 pts += ape->blocksperframe / MAC_SUBFRAME_SIZE; 00343 } 00344 00345 return 0; 00346 } 00347 00348 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt) 00349 { 00350 int ret; 00351 int nblocks; 00352 APEContext *ape = s->priv_data; 00353 uint32_t extra_size = 8; 00354 00355 if (s->pb->eof_reached) 00356 return AVERROR(EIO); 00357 if (ape->currentframe > ape->totalframes) 00358 return AVERROR(EIO); 00359 00360 avio_seek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET); 00361 00362 /* Calculate how many blocks there are in this frame */ 00363 if (ape->currentframe == (ape->totalframes - 1)) 00364 nblocks = ape->finalframeblocks; 00365 else 00366 nblocks = ape->blocksperframe; 00367 00368 if (av_new_packet(pkt, ape->frames[ape->currentframe].size + extra_size) < 0) 00369 return AVERROR(ENOMEM); 00370 00371 AV_WL32(pkt->data , nblocks); 00372 AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip); 00373 ret = avio_read(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size); 00374 00375 pkt->pts = ape->frames[ape->currentframe].pts; 00376 pkt->stream_index = 0; 00377 00378 /* note: we need to modify the packet size here to handle the last 00379 packet */ 00380 pkt->size = ret + extra_size; 00381 00382 ape->currentframe++; 00383 00384 return 0; 00385 } 00386 00387 static int ape_read_close(AVFormatContext * s) 00388 { 00389 APEContext *ape = s->priv_data; 00390 00391 av_freep(&ape->frames); 00392 av_freep(&ape->seektable); 00393 return 0; 00394 } 00395 00396 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) 00397 { 00398 AVStream *st = s->streams[stream_index]; 00399 APEContext *ape = s->priv_data; 00400 int index = av_index_search_timestamp(st, timestamp, flags); 00401 00402 if (index < 0) 00403 return -1; 00404 00405 ape->currentframe = index; 00406 return 0; 00407 } 00408 00409 AVInputFormat ff_ape_demuxer = { 00410 "ape", 00411 NULL_IF_CONFIG_SMALL("Monkey's Audio"), 00412 sizeof(APEContext), 00413 ape_probe, 00414 ape_read_header, 00415 ape_read_packet, 00416 ape_read_close, 00417 ape_read_seek, 00418 .extensions = "ape,apl,mac" 00419 };