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 uint8_t buf[ID3v2_HEADER_SIZE];
33 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
36 AVStream *st = av_new_stream(s, 0);
38 return AVERROR(ENOMEM);
39 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
40 st->codec->codec_id = CODEC_ID_FLAC;
41 st->need_parsing = AVSTREAM_PARSE_FULL;
42 /* the parameters will be extracted from the compressed bitstream */
44 /* skip ID3v2 header if found */
45 ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
46 if (ret == ID3v2_HEADER_SIZE && ff_id3v2_match(buf, ID3v2_DEFAULT_MAGIC)) {
47 int len = ff_id3v2_tag_len(buf);
48 url_fseek(s->pb, len - ID3v2_HEADER_SIZE, SEEK_CUR);
50 url_fseek(s->pb, 0, SEEK_SET);
53 /* if fLaC marker is not found, assume there is no header */
54 if (get_le32(s->pb) != MKTAG('f','L','a','C')) {
55 url_fseek(s->pb, -4, SEEK_CUR);
59 /* process metadata blocks */
60 while (!url_feof(s->pb) && !metadata_last) {
61 get_buffer(s->pb, header, 4);
62 ff_flac_parse_block_header(header, &metadata_last, &metadata_type,
64 switch (metadata_type) {
65 /* allocate and read metadata block for supported types */
66 case FLAC_METADATA_TYPE_STREAMINFO:
67 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
68 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
70 return AVERROR(ENOMEM);
72 if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
77 /* skip metadata block for unsupported types */
79 ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
84 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
86 /* STREAMINFO can only occur once */
87 if (found_streaminfo) {
89 return AVERROR_INVALIDDATA;
91 if (metadata_size != FLAC_STREAMINFO_SIZE) {
93 return AVERROR_INVALIDDATA;
96 st->codec->extradata = buffer;
97 st->codec->extradata_size = metadata_size;
100 /* get codec params from STREAMINFO header */
101 ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
103 /* set time base and duration */
104 if (si.samplerate > 0) {
105 av_set_pts_info(st, 64, 1, si.samplerate);
107 st->duration = si.samples;
110 /* STREAMINFO must be the first block */
111 if (!found_streaminfo) {
113 return AVERROR_INVALIDDATA;
115 /* process supported blocks other than STREAMINFO */
116 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
117 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
118 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
128 static int flac_probe(AVProbeData *p)
130 uint8_t *bufptr = p->buf;
131 uint8_t *end = p->buf + p->buf_size;
133 if(ff_id3v2_match(bufptr, ID3v2_DEFAULT_MAGIC))
134 bufptr += ff_id3v2_tag_len(bufptr);
136 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
137 else return AVPROBE_SCORE_MAX/2;
140 AVInputFormat flac_demuxer = {
142 NULL_IF_CONFIG_SMALL("raw FLAC"),
146 ff_raw_read_partial_packet,
147 .flags= AVFMT_GENERIC_INDEX,
148 .extensions = "flac",
149 .value = CODEC_ID_FLAC,
150 .metadata_conv = ff_vorbiscomment_metadata_conv,