3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcodec/flac.h"
27 #include "vorbiscomment.h"
29 static int flac_read_header(AVFormatContext *s,
30 AVFormatParameters *ap)
32 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
35 AVStream *st = avformat_new_stream(s, NULL);
37 return AVERROR(ENOMEM);
38 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
39 st->codec->codec_id = CODEC_ID_FLAC;
40 st->need_parsing = AVSTREAM_PARSE_FULL;
41 /* the parameters will be extracted from the compressed bitstream */
43 /* if fLaC marker is not found, assume there is no header */
44 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
45 avio_seek(s->pb, -4, SEEK_CUR);
49 /* process metadata blocks */
50 while (!url_feof(s->pb) && !metadata_last) {
51 avio_read(s->pb, header, 4);
52 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
54 switch (metadata_type) {
55 /* allocate and read metadata block for supported types */
56 case FLAC_METADATA_TYPE_STREAMINFO:
57 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
58 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
60 return AVERROR(ENOMEM);
62 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
67 /* skip metadata block for unsupported types */
69 ret = avio_skip(s->pb, metadata_size);
74 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
76 /* STREAMINFO can only occur once */
77 if (found_streaminfo) {
79 return AVERROR_INVALIDDATA;
81 if (metadata_size != FLAC_STREAMINFO_SIZE) {
83 return AVERROR_INVALIDDATA;
86 st->codec->extradata = buffer;
87 st->codec->extradata_size = metadata_size;
90 /* get codec params from STREAMINFO header */
91 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
93 /* set time base and duration */
94 if (si.samplerate > 0) {
95 avpriv_set_pts_info(st, 64, 1, si.samplerate);
97 st->duration = si.samples;
100 /* STREAMINFO must be the first block */
101 if (!found_streaminfo) {
103 return AVERROR_INVALIDDATA;
105 /* process supported blocks other than STREAMINFO */
106 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
107 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
108 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
118 static int flac_probe(AVProbeData *p)
120 uint8_t *bufptr = p->buf;
121 uint8_t *end = p->buf + p->buf_size;
123 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
124 else return AVPROBE_SCORE_MAX/2;
127 AVInputFormat ff_flac_demuxer = {
129 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
130 .read_probe = flac_probe,
131 .read_header = flac_read_header,
132 .read_packet = ff_raw_read_partial_packet,
133 .flags= AVFMT_GENERIC_INDEX,
134 .extensions = "flac",
135 .value = CODEC_ID_FLAC,