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"
30 #include "libavcodec/bytestream.h"
32 static int flac_read_header(AVFormatContext *s)
34 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
37 AVStream *st = avformat_new_stream(s, NULL);
39 return AVERROR(ENOMEM);
40 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
41 st->codec->codec_id = AV_CODEC_ID_FLAC;
42 st->need_parsing = AVSTREAM_PARSE_FULL;
43 /* the parameters will be extracted from the compressed bitstream */
45 /* if fLaC marker is not found, assume there is no header */
46 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
47 avio_seek(s->pb, -4, SEEK_CUR);
51 /* process metadata blocks */
52 while (!s->pb->eof_reached && !metadata_last) {
53 avio_read(s->pb, header, 4);
54 flac_parse_block_header(header, &metadata_last, &metadata_type,
56 switch (metadata_type) {
57 /* allocate and read metadata block for supported types */
58 case FLAC_METADATA_TYPE_STREAMINFO:
59 case FLAC_METADATA_TYPE_CUESHEET:
60 case FLAC_METADATA_TYPE_PICTURE:
61 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
62 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
64 return AVERROR(ENOMEM);
66 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
71 /* skip metadata block for unsupported types */
73 ret = avio_skip(s->pb, metadata_size);
78 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
80 /* STREAMINFO can only occur once */
81 if (found_streaminfo) {
83 return AVERROR_INVALIDDATA;
85 if (metadata_size != FLAC_STREAMINFO_SIZE) {
87 return AVERROR_INVALIDDATA;
90 st->codec->extradata = buffer;
91 st->codec->extradata_size = metadata_size;
94 /* get codec params from STREAMINFO header */
95 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
97 /* set time base and duration */
98 if (si.samplerate > 0) {
99 avpriv_set_pts_info(st, 64, 1, si.samplerate);
101 st->duration = si.samples;
103 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
106 const uint8_t *offset;
107 int i, chapters, track, ti;
108 if (metadata_size < 431)
109 return AVERROR_INVALIDDATA;
110 offset = buffer + 395;
111 chapters = bytestream_get_byte(&offset) - 1;
113 return AVERROR_INVALIDDATA;
114 for (i = 0; i < chapters; i++) {
115 if (offset + 36 - buffer > metadata_size)
116 return AVERROR_INVALIDDATA;
117 start = bytestream_get_be64(&offset);
118 track = bytestream_get_byte(&offset);
119 bytestream_get_buffer(&offset, isrc, 12);
122 ti = bytestream_get_byte(&offset);
123 if (ti <= 0) return AVERROR_INVALIDDATA;
125 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
127 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
128 ret = ff_flac_parse_picture(s, buffer, metadata_size);
131 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
135 /* STREAMINFO must be the first block */
136 if (!found_streaminfo) {
138 return AVERROR_INVALIDDATA;
140 /* process supported blocks other than STREAMINFO */
141 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
142 AVDictionaryEntry *chmask;
144 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1)) {
145 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
148 /* parse the channels mask if present */
149 chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
151 uint64_t mask = strtol(chmask->value, NULL, 0);
152 if (!mask || mask & ~0x3ffffULL) {
153 av_log(s, AV_LOG_WARNING,
154 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
156 st->codec->channel_layout = mask;
157 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
165 ret = ff_replaygain_export(st, s->metadata);
172 static int flac_probe(AVProbeData *p)
174 if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
176 return AVPROBE_SCORE_EXTENSION;
179 AVInputFormat ff_flac_demuxer = {
181 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
182 .read_probe = flac_probe,
183 .read_header = flac_read_header,
184 .read_packet = ff_raw_read_partial_packet,
185 .flags = AVFMT_GENERIC_INDEX,
186 .extensions = "flac",
187 .raw_codec_id = AV_CODEC_ID_FLAC,