• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/ape.c

Go to the documentation of this file.
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 #include "apetag.h"
00028 
00029 #define ENABLE_DEBUG 0
00030 
00031 /* The earliest and latest file formats supported by this library */
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     /* Derived fields */
00056     uint32_t junklength;
00057     uint32_t firstframe;
00058     uint32_t totalsamples;
00059     int currentframe;
00060     APEFrame *frames;
00061 
00062     /* Info from Descriptor Block */
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     /* Info from Header Block */
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     /* Seektable */
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     ByteIOContext *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     /* TODO: Skip any leading junk such as id3v2 tags */
00163     ape->junklength = 0;
00164 
00165     tag = get_le32(pb);
00166     if (tag != MKTAG('M', 'A', 'C', ' '))
00167         return -1;
00168 
00169     ape->fileversion = get_le16(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             = get_le16(pb);
00178         ape->descriptorlength     = get_le32(pb);
00179         ape->headerlength         = get_le32(pb);
00180         ape->seektablelength      = get_le32(pb);
00181         ape->wavheaderlength      = get_le32(pb);
00182         ape->audiodatalength      = get_le32(pb);
00183         ape->audiodatalength_high = get_le32(pb);
00184         ape->wavtaillength        = get_le32(pb);
00185         get_buffer(pb, ape->md5, 16);
00186 
00187         /* Skip any unknown bytes at the end of the descriptor.
00188            This is for future compatibility */
00189         if (ape->descriptorlength > 52)
00190             url_fseek(pb, ape->descriptorlength - 52, SEEK_CUR);
00191 
00192         /* Read header data */
00193         ape->compressiontype      = get_le16(pb);
00194         ape->formatflags          = get_le16(pb);
00195         ape->blocksperframe       = get_le32(pb);
00196         ape->finalframeblocks     = get_le32(pb);
00197         ape->totalframes          = get_le32(pb);
00198         ape->bps                  = get_le16(pb);
00199         ape->channels             = get_le16(pb);
00200         ape->samplerate           = get_le32(pb);
00201     } else {
00202         ape->descriptorlength = 0;
00203         ape->headerlength = 32;
00204 
00205         ape->compressiontype      = get_le16(pb);
00206         ape->formatflags          = get_le16(pb);
00207         ape->channels             = get_le16(pb);
00208         ape->samplerate           = get_le32(pb);
00209         ape->wavheaderlength      = get_le32(pb);
00210         ape->wavtaillength        = get_le32(pb);
00211         ape->totalframes          = get_le32(pb);
00212         ape->finalframeblocks     = get_le32(pb);
00213 
00214         if (ape->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) {
00215             url_fseek(pb, 4, SEEK_CUR); /* Skip the peak level */
00216             ape->headerlength += 4;
00217         }
00218 
00219         if (ape->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) {
00220             ape->seektablelength = get_le32(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         /* Skip any stored wav header */
00241         if (!(ape->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER))
00242             url_fskip(pb, ape->wavheaderlength);
00243     }
00244 
00245     if(!ape->totalframes){
00246         av_log(s, AV_LOG_ERROR, "No frames in the file!\n");
00247         return AVERROR(EINVAL);
00248     }
00249     if(ape->totalframes > UINT_MAX / sizeof(APEFrame)){
00250         av_log(s, AV_LOG_ERROR, "Too many frames: %d\n", ape->totalframes);
00251         return -1;
00252     }
00253     ape->frames       = av_malloc(ape->totalframes * sizeof(APEFrame));
00254     if(!ape->frames)
00255         return AVERROR(ENOMEM);
00256     ape->firstframe   = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
00257     ape->currentframe = 0;
00258 
00259 
00260     ape->totalsamples = ape->finalframeblocks;
00261     if (ape->totalframes > 1)
00262         ape->totalsamples += ape->blocksperframe * (ape->totalframes - 1);
00263 
00264     if (ape->seektablelength > 0) {
00265         ape->seektable = av_malloc(ape->seektablelength);
00266         for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
00267             ape->seektable[i] = get_le32(pb);
00268     }
00269 
00270     ape->frames[0].pos     = ape->firstframe;
00271     ape->frames[0].nblocks = ape->blocksperframe;
00272     ape->frames[0].skip    = 0;
00273     for (i = 1; i < ape->totalframes; i++) {
00274         ape->frames[i].pos      = ape->seektable[i]; //ape->frames[i-1].pos + ape->blocksperframe;
00275         ape->frames[i].nblocks  = ape->blocksperframe;
00276         ape->frames[i - 1].size = ape->frames[i].pos - ape->frames[i - 1].pos;
00277         ape->frames[i].skip     = (ape->frames[i].pos - ape->frames[0].pos) & 3;
00278     }
00279     ape->frames[ape->totalframes - 1].size    = ape->finalframeblocks * 4;
00280     ape->frames[ape->totalframes - 1].nblocks = ape->finalframeblocks;
00281 
00282     for (i = 0; i < ape->totalframes; i++) {
00283         if(ape->frames[i].skip){
00284             ape->frames[i].pos  -= ape->frames[i].skip;
00285             ape->frames[i].size += ape->frames[i].skip;
00286         }
00287         ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
00288     }
00289 
00290 
00291     ape_dumpinfo(s, ape);
00292 
00293     /* try to read APE tags */
00294     if (!url_is_streamed(pb)) {
00295         ff_ape_parse_tag(s);
00296         url_fseek(pb, 0, SEEK_SET);
00297     }
00298 
00299     av_log(s, AV_LOG_DEBUG, "Decoding file - v%d.%02d, compression level %d\n", ape->fileversion / 1000, (ape->fileversion % 1000) / 10, ape->compressiontype);
00300 
00301     /* now we are ready: build format streams */
00302     st = av_new_stream(s, 0);
00303     if (!st)
00304         return -1;
00305 
00306     total_blocks = (ape->totalframes == 0) ? 0 : ((ape->totalframes - 1) * ape->blocksperframe) + ape->finalframeblocks;
00307 
00308     st->codec->codec_type      = AVMEDIA_TYPE_AUDIO;
00309     st->codec->codec_id        = CODEC_ID_APE;
00310     st->codec->codec_tag       = MKTAG('A', 'P', 'E', ' ');
00311     st->codec->channels        = ape->channels;
00312     st->codec->sample_rate     = ape->samplerate;
00313     st->codec->bits_per_coded_sample = ape->bps;
00314     st->codec->frame_size      = MAC_SUBFRAME_SIZE;
00315 
00316     st->nb_frames = ape->totalframes;
00317     st->start_time = 0;
00318     st->duration  = total_blocks / MAC_SUBFRAME_SIZE;
00319     av_set_pts_info(st, 64, MAC_SUBFRAME_SIZE, ape->samplerate);
00320 
00321     st->codec->extradata = av_malloc(APE_EXTRADATA_SIZE);
00322     st->codec->extradata_size = APE_EXTRADATA_SIZE;
00323     AV_WL16(st->codec->extradata + 0, ape->fileversion);
00324     AV_WL16(st->codec->extradata + 2, ape->compressiontype);
00325     AV_WL16(st->codec->extradata + 4, ape->formatflags);
00326 
00327     pts = 0;
00328     for (i = 0; i < ape->totalframes; i++) {
00329         ape->frames[i].pts = pts;
00330         av_add_index_entry(st, ape->frames[i].pos, ape->frames[i].pts, 0, 0, AVINDEX_KEYFRAME);
00331         pts += ape->blocksperframe / MAC_SUBFRAME_SIZE;
00332     }
00333 
00334     return 0;
00335 }
00336 
00337 static int ape_read_packet(AVFormatContext * s, AVPacket * pkt)
00338 {
00339     int ret;
00340     int nblocks;
00341     APEContext *ape = s->priv_data;
00342     uint32_t extra_size = 8;
00343 
00344     if (url_feof(s->pb))
00345         return AVERROR(EIO);
00346     if (ape->currentframe > ape->totalframes)
00347         return AVERROR(EIO);
00348 
00349     url_fseek (s->pb, ape->frames[ape->currentframe].pos, SEEK_SET);
00350 
00351     /* Calculate how many blocks there are in this frame */
00352     if (ape->currentframe == (ape->totalframes - 1))
00353         nblocks = ape->finalframeblocks;
00354     else
00355         nblocks = ape->blocksperframe;
00356 
00357     if (av_new_packet(pkt,  ape->frames[ape->currentframe].size + extra_size) < 0)
00358         return AVERROR(ENOMEM);
00359 
00360     AV_WL32(pkt->data    , nblocks);
00361     AV_WL32(pkt->data + 4, ape->frames[ape->currentframe].skip);
00362     ret = get_buffer(s->pb, pkt->data + extra_size, ape->frames[ape->currentframe].size);
00363 
00364     pkt->pts = ape->frames[ape->currentframe].pts;
00365     pkt->stream_index = 0;
00366 
00367     /* note: we need to modify the packet size here to handle the last
00368        packet */
00369     pkt->size = ret + extra_size;
00370 
00371     ape->currentframe++;
00372 
00373     return 0;
00374 }
00375 
00376 static int ape_read_close(AVFormatContext * s)
00377 {
00378     APEContext *ape = s->priv_data;
00379 
00380     av_freep(&ape->frames);
00381     av_freep(&ape->seektable);
00382     return 0;
00383 }
00384 
00385 static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00386 {
00387     AVStream *st = s->streams[stream_index];
00388     APEContext *ape = s->priv_data;
00389     int index = av_index_search_timestamp(st, timestamp, flags);
00390 
00391     if (index < 0)
00392         return -1;
00393 
00394     ape->currentframe = index;
00395     return 0;
00396 }
00397 
00398 AVInputFormat ape_demuxer = {
00399     "ape",
00400     NULL_IF_CONFIG_SMALL("Monkey's Audio"),
00401     sizeof(APEContext),
00402     ape_probe,
00403     ape_read_header,
00404     ape_read_packet,
00405     ape_read_close,
00406     ape_read_seek,
00407     .extensions = "ape,apl,mac"
00408 };

Generated on Fri Sep 16 2011 17:17:47 for FFmpeg by  doxygen 1.7.1