3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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"
24 #include "flac_picture.h"
28 #include "vorbiscomment.h"
29 #include "replaygain.h"
31 static int flac_read_header(AVFormatContext *s)
33 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
36 AVStream *st = avformat_new_stream(s, NULL);
38 return AVERROR(ENOMEM);
39 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
40 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
41 st->need_parsing = AVSTREAM_PARSE_FULL;
42 /* the parameters will be extracted from the compressed bitstream */
44 /* if fLaC marker is not found, assume there is no header */
45 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
46 avio_seek(s->pb, -4, SEEK_CUR);
50 /* process metadata blocks */
51 while (!s->pb->eof_reached && !metadata_last) {
52 avio_read(s->pb, header, 4);
53 flac_parse_block_header(header, &metadata_last, &metadata_type,
55 switch (metadata_type) {
56 /* allocate and read metadata block for supported types */
57 case FLAC_METADATA_TYPE_STREAMINFO:
58 case FLAC_METADATA_TYPE_CUESHEET:
59 case FLAC_METADATA_TYPE_PICTURE:
60 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
61 buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
63 return AVERROR(ENOMEM);
65 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
70 /* skip metadata block for unsupported types */
72 ret = avio_skip(s->pb, metadata_size);
77 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
81 /* STREAMINFO can only occur once */
82 if (found_streaminfo) {
84 return AVERROR_INVALIDDATA;
86 if (metadata_size != FLAC_STREAMINFO_SIZE) {
88 return AVERROR_INVALIDDATA;
91 st->codecpar->extradata = buffer;
92 st->codecpar->extradata_size = metadata_size;
95 /* get sample rate and sample count from STREAMINFO header;
96 * other parameters will be extracted by the parser */
97 samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
98 samples = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
100 /* set time base and duration */
101 if (samplerate > 0) {
102 avpriv_set_pts_info(st, 64, 1, samplerate);
104 st->duration = samples;
106 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
109 const uint8_t *offset;
110 int i, chapters, track, ti;
111 if (metadata_size < 431)
112 return AVERROR_INVALIDDATA;
113 offset = buffer + 395;
114 chapters = bytestream_get_byte(&offset) - 1;
116 return AVERROR_INVALIDDATA;
117 for (i = 0; i < chapters; i++) {
118 if (offset + 36 - buffer > metadata_size)
119 return AVERROR_INVALIDDATA;
120 start = bytestream_get_be64(&offset);
121 track = bytestream_get_byte(&offset);
122 bytestream_get_buffer(&offset, isrc, 12);
125 ti = bytestream_get_byte(&offset);
126 if (ti <= 0) return AVERROR_INVALIDDATA;
128 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
130 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
131 ret = ff_flac_parse_picture(s, buffer, metadata_size);
134 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
138 /* STREAMINFO must be the first block */
139 if (!found_streaminfo) {
141 return AVERROR_INVALIDDATA;
143 /* process supported blocks other than STREAMINFO */
144 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
145 AVDictionaryEntry *chmask;
147 ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
149 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
150 } else if (ret > 0) {
151 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
154 /* parse the channels mask if present */
155 chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
157 uint64_t mask = strtol(chmask->value, NULL, 0);
158 if (!mask || mask & ~0x3ffffULL) {
159 av_log(s, AV_LOG_WARNING,
160 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
162 st->codecpar->channel_layout = mask;
163 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
171 ret = ff_replaygain_export(st, s->metadata);
178 static int flac_probe(AVProbeData *p)
180 /* file header + metadata header + checked bytes of streaminfo */
181 if (p->buf_size >= 4 + 4 + 13) {
182 int type = p->buf[4] & 0x7f;
183 int size = AV_RB24(p->buf + 5);
184 int min_block_size = AV_RB16(p->buf + 8);
185 int max_block_size = AV_RB16(p->buf + 10);
186 int sample_rate = AV_RB24(p->buf + 18) >> 4;
188 if (!memcmp(p->buf, "fLaC", 4) &&
189 type == FLAC_METADATA_TYPE_STREAMINFO &&
190 size == FLAC_STREAMINFO_SIZE &&
191 min_block_size >= 16 &&
192 max_block_size >= min_block_size &&
193 sample_rate && sample_rate <= 655350)
194 return AVPROBE_SCORE_MAX;
200 AVInputFormat ff_flac_demuxer = {
202 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
203 .read_probe = flac_probe,
204 .read_header = flac_read_header,
205 .read_packet = ff_raw_read_partial_packet,
206 .flags = AVFMT_GENERIC_INDEX,
207 .extensions = "flac",
208 .raw_codec_id = AV_CODEC_ID_FLAC,