00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #include "libavcodec/flac.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "rawdec.h"
00026 #include "oggdec.h"
00027 #include "vorbiscomment.h"
00028 #include "libavcodec/bytestream.h"
00029 
00030 static int flac_read_header(AVFormatContext *s,
00031                              AVFormatParameters *ap)
00032 {
00033     int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
00034     uint8_t header[4];
00035     uint8_t *buffer=NULL;
00036     AVStream *st = avformat_new_stream(s, NULL);
00037     if (!st)
00038         return AVERROR(ENOMEM);
00039     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00040     st->codec->codec_id = CODEC_ID_FLAC;
00041     st->need_parsing = AVSTREAM_PARSE_FULL;
00042     
00043 
00044     
00045     if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
00046         avio_seek(s->pb, -4, SEEK_CUR);
00047         return 0;
00048     }
00049 
00050     
00051     while (!url_feof(s->pb) && !metadata_last) {
00052         avio_read(s->pb, header, 4);
00053         avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
00054                                    &metadata_size);
00055         switch (metadata_type) {
00056         
00057         case FLAC_METADATA_TYPE_STREAMINFO:
00058         case FLAC_METADATA_TYPE_CUESHEET:
00059         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
00060             buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00061             if (!buffer) {
00062                 return AVERROR(ENOMEM);
00063             }
00064             if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
00065                 av_freep(&buffer);
00066                 return AVERROR(EIO);
00067             }
00068             break;
00069         
00070         default:
00071             ret = avio_skip(s->pb, metadata_size);
00072             if (ret < 0)
00073                 return ret;
00074         }
00075 
00076         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
00077             FLACStreaminfo si;
00078             
00079             if (found_streaminfo) {
00080                 av_freep(&buffer);
00081                 return AVERROR_INVALIDDATA;
00082             }
00083             if (metadata_size != FLAC_STREAMINFO_SIZE) {
00084                 av_freep(&buffer);
00085                 return AVERROR_INVALIDDATA;
00086             }
00087             found_streaminfo = 1;
00088             st->codec->extradata      = buffer;
00089             st->codec->extradata_size = metadata_size;
00090             buffer = NULL;
00091 
00092             
00093             avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
00094 
00095             
00096             if (si.samplerate > 0) {
00097                 avpriv_set_pts_info(st, 64, 1, si.samplerate);
00098                 if (si.samples > 0)
00099                     st->duration = si.samples;
00100             }
00101         } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
00102             uint8_t isrc[13];
00103             uint64_t start;
00104             const uint8_t *offset;
00105             int i, j, chapters, track, ti;
00106             if (metadata_size < 431)
00107                 return AVERROR_INVALIDDATA;
00108             offset = buffer + 395;
00109             chapters = bytestream_get_byte(&offset) - 1;
00110             if (chapters <= 0)
00111                 return AVERROR_INVALIDDATA;
00112             for (i = 0; i < chapters; i++) {
00113                 if (offset + 36 - buffer > metadata_size)
00114                     return AVERROR_INVALIDDATA;
00115                 start = bytestream_get_be64(&offset);
00116                 track = bytestream_get_byte(&offset);
00117                 bytestream_get_buffer(&offset, isrc, 12);
00118                 isrc[12] = 0;
00119                 offset += 14;
00120                 ti = bytestream_get_byte(&offset);
00121                 if (ti <= 0) return AVERROR_INVALIDDATA;
00122                 for (j = 0; j < ti; j++)
00123                     offset += 12;
00124                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
00125             }
00126         } else {
00127             
00128             if (!found_streaminfo) {
00129                 av_freep(&buffer);
00130                 return AVERROR_INVALIDDATA;
00131             }
00132             
00133             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
00134                 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
00135                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
00136                 }
00137             }
00138             av_freep(&buffer);
00139         }
00140     }
00141 
00142     return 0;
00143 }
00144 
00145 static int flac_probe(AVProbeData *p)
00146 {
00147     uint8_t *bufptr = p->buf;
00148     uint8_t *end    = p->buf + p->buf_size;
00149 
00150     if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
00151     else                                            return AVPROBE_SCORE_MAX/2;
00152 }
00153 
00154 AVInputFormat ff_flac_demuxer = {
00155     .name           = "flac",
00156     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
00157     .read_probe     = flac_probe,
00158     .read_header    = flac_read_header,
00159     .read_packet    = ff_raw_read_partial_packet,
00160     .flags= AVFMT_GENERIC_INDEX,
00161     .extensions = "flac",
00162     .value = CODEC_ID_FLAC,
00163 };