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"
28 #include "vorbiscomment.h"
29 #include "libavcodec/bytestream.h"
31 static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
33 const CodecMime *mime = ff_id3v2_mime_tags;
34 enum CodecID id = CODEC_ID_NONE;
35 uint8_t mimetype[64], *desc = NULL, *data = NULL;
36 AVIOContext *pb = NULL;
38 int type, width, height;
41 st = avformat_new_stream(s, NULL);
43 return AVERROR(ENOMEM);
45 pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
47 return AVERROR(ENOMEM);
49 /* read the picture type */
51 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
52 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
53 ret = AVERROR_INVALIDDATA;
57 /* picture mimetype */
60 avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
61 av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
63 ret = AVERROR_INVALIDDATA;
68 while (mime->id != CODEC_ID_NONE) {
69 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
75 if (id == CODEC_ID_NONE) {
76 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
78 ret = AVERROR_INVALIDDATA;
82 /* picture description */
85 if (!(desc = av_malloc(len + 1))) {
86 ret = AVERROR(ENOMEM);
90 if (avio_read(pb, desc, len) != len) {
97 /* picture metadata */
98 width = avio_rb32(pb);
99 height = avio_rb32(pb);
105 ret = AVERROR_INVALIDDATA;
108 if (!(data = av_malloc(len))) {
109 ret = AVERROR(ENOMEM);
112 if (avio_read(pb, data, len) != len) {
117 av_init_packet(&st->attached_pic);
118 st->attached_pic.data = data;
119 st->attached_pic.size = len;
120 st->attached_pic.destruct = av_destruct_packet;
121 st->attached_pic.stream_index = st->index;
122 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
124 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
125 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
126 st->codec->codec_id = id;
127 st->codec->width = width;
128 st->codec->height = height;
129 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
131 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
145 static int flac_read_header(AVFormatContext *s)
147 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
149 uint8_t *buffer=NULL;
150 AVStream *st = avformat_new_stream(s, NULL);
152 return AVERROR(ENOMEM);
153 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
154 st->codec->codec_id = CODEC_ID_FLAC;
155 st->need_parsing = AVSTREAM_PARSE_FULL;
156 /* the parameters will be extracted from the compressed bitstream */
158 /* if fLaC marker is not found, assume there is no header */
159 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
160 avio_seek(s->pb, -4, SEEK_CUR);
164 /* process metadata blocks */
165 while (!url_feof(s->pb) && !metadata_last) {
166 avio_read(s->pb, header, 4);
167 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
169 switch (metadata_type) {
170 /* allocate and read metadata block for supported types */
171 case FLAC_METADATA_TYPE_STREAMINFO:
172 case FLAC_METADATA_TYPE_CUESHEET:
173 case FLAC_METADATA_TYPE_PICTURE:
174 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
175 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
177 return AVERROR(ENOMEM);
179 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
184 /* skip metadata block for unsupported types */
186 ret = avio_skip(s->pb, metadata_size);
191 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
193 /* STREAMINFO can only occur once */
194 if (found_streaminfo) {
196 return AVERROR_INVALIDDATA;
198 if (metadata_size != FLAC_STREAMINFO_SIZE) {
200 return AVERROR_INVALIDDATA;
202 found_streaminfo = 1;
203 st->codec->extradata = buffer;
204 st->codec->extradata_size = metadata_size;
207 /* get codec params from STREAMINFO header */
208 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
210 /* set time base and duration */
211 if (si.samplerate > 0) {
212 avpriv_set_pts_info(st, 64, 1, si.samplerate);
214 st->duration = si.samples;
216 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
219 const uint8_t *offset;
220 int i, chapters, track, ti;
221 if (metadata_size < 431)
222 return AVERROR_INVALIDDATA;
223 offset = buffer + 395;
224 chapters = bytestream_get_byte(&offset) - 1;
226 return AVERROR_INVALIDDATA;
227 for (i = 0; i < chapters; i++) {
228 if (offset + 36 - buffer > metadata_size)
229 return AVERROR_INVALIDDATA;
230 start = bytestream_get_be64(&offset);
231 track = bytestream_get_byte(&offset);
232 bytestream_get_buffer(&offset, isrc, 12);
235 ti = bytestream_get_byte(&offset);
236 if (ti <= 0) return AVERROR_INVALIDDATA;
238 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
240 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
241 ret = parse_picture(s, buffer, metadata_size);
244 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
248 /* STREAMINFO must be the first block */
249 if (!found_streaminfo) {
251 return AVERROR_INVALIDDATA;
253 /* process supported blocks other than STREAMINFO */
254 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
255 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
256 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
266 static int flac_probe(AVProbeData *p)
268 uint8_t *bufptr = p->buf;
269 uint8_t *end = p->buf + p->buf_size;
271 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
272 else return AVPROBE_SCORE_MAX/2;
275 AVInputFormat ff_flac_demuxer = {
277 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
278 .read_probe = flac_probe,
279 .read_header = flac_read_header,
280 .read_packet = ff_raw_read_partial_packet,
281 .flags = AVFMT_GENERIC_INDEX,
282 .extensions = "flac",
283 .raw_codec_id = CODEC_ID_FLAC,