libavformat/flacdec.c
Go to the documentation of this file.
00001 /*
00002  * Raw FLAC demuxer
00003  * Copyright (c) 2001 Fabrice Bellard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     /* the parameters will be extracted from the compressed bitstream */
00043 
00044     /* if fLaC marker is not found, assume there is no header */
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     /* process metadata blocks */
00051     while (!s->pb->eof_reached && !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         /* allocate and read metadata block for supported types */
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         /* skip metadata block for unsupported types */
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             /* STREAMINFO can only occur once */
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             /* get codec params from STREAMINFO header */
00093             avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
00094 
00095             /* set time base and duration */
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, 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                 offset += ti * 12;
00123                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
00124             }
00125         } else {
00126             /* STREAMINFO must be the first block */
00127             if (!found_streaminfo) {
00128                 av_freep(&buffer);
00129                 return AVERROR_INVALIDDATA;
00130             }
00131             /* process supported blocks other than STREAMINFO */
00132             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
00133                 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
00134                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
00135                 }
00136             }
00137             av_freep(&buffer);
00138         }
00139     }
00140 
00141     return 0;
00142 }
00143 
00144 static int flac_probe(AVProbeData *p)
00145 {
00146     uint8_t *bufptr = p->buf;
00147     uint8_t *end    = p->buf + p->buf_size;
00148 
00149     if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
00150     else                                            return AVPROBE_SCORE_MAX/2;
00151 }
00152 
00153 AVInputFormat ff_flac_demuxer = {
00154     .name           = "flac",
00155     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
00156     .read_probe     = flac_probe,
00157     .read_header    = flac_read_header,
00158     .read_packet    = ff_raw_read_partial_packet,
00159     .flags= AVFMT_GENERIC_INDEX,
00160     .extensions = "flac",
00161     .value = CODEC_ID_FLAC,
00162 };