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 #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0)
33 static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
35 const CodecMime *mime = ff_id3v2_mime_tags;
36 enum AVCodecID id = AV_CODEC_ID_NONE;
37 AVBufferRef *data = NULL;
38 uint8_t mimetype[64], *desc = NULL;
39 AVIOContext *pb = NULL;
41 int type, width, height;
44 pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
46 return AVERROR(ENOMEM);
48 /* read the picture type */
50 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
51 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
52 if (s->error_recognition & AV_EF_EXPLODE) {
53 RETURN_ERROR(AVERROR_INVALIDDATA);
58 /* picture mimetype */
61 avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
62 av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
64 if (s->error_recognition & AV_EF_EXPLODE)
65 ret = AVERROR_INVALIDDATA;
70 while (mime->id != AV_CODEC_ID_NONE) {
71 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
77 if (id == AV_CODEC_ID_NONE) {
78 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
80 if (s->error_recognition & AV_EF_EXPLODE)
81 ret = AVERROR_INVALIDDATA;
85 /* picture description */
88 if (!(desc = av_malloc(len + 1))) {
89 RETURN_ERROR(AVERROR(ENOMEM));
92 if (avio_read(pb, desc, len) != len) {
93 av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
94 if (s->error_recognition & AV_EF_EXPLODE)
101 /* picture metadata */
102 width = avio_rb32(pb);
103 height = avio_rb32(pb);
109 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
110 if (s->error_recognition & AV_EF_EXPLODE)
111 ret = AVERROR_INVALIDDATA;
114 if (!(data = av_buffer_alloc(len))) {
115 RETURN_ERROR(AVERROR(ENOMEM));
117 if (avio_read(pb, data->data, len) != len) {
118 av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
119 if (s->error_recognition & AV_EF_EXPLODE)
124 st = avformat_new_stream(s, NULL);
126 RETURN_ERROR(AVERROR(ENOMEM));
129 av_init_packet(&st->attached_pic);
130 st->attached_pic.buf = data;
131 st->attached_pic.data = data->data;
132 st->attached_pic.size = len;
133 st->attached_pic.stream_index = st->index;
134 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
136 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
137 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
138 st->codec->codec_id = id;
139 st->codec->width = width;
140 st->codec->height = height;
141 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
143 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
150 av_buffer_unref(&data);
157 static int flac_read_header(AVFormatContext *s)
159 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
161 uint8_t *buffer=NULL;
162 AVStream *st = avformat_new_stream(s, NULL);
164 return AVERROR(ENOMEM);
165 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
166 st->codec->codec_id = AV_CODEC_ID_FLAC;
167 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
168 /* the parameters will be extracted from the compressed bitstream */
170 /* if fLaC marker is not found, assume there is no header */
171 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
172 avio_seek(s->pb, -4, SEEK_CUR);
176 /* process metadata blocks */
177 while (!url_feof(s->pb) && !metadata_last) {
178 avio_read(s->pb, header, 4);
179 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
181 switch (metadata_type) {
182 /* allocate and read metadata block for supported types */
183 case FLAC_METADATA_TYPE_STREAMINFO:
184 case FLAC_METADATA_TYPE_CUESHEET:
185 case FLAC_METADATA_TYPE_PICTURE:
186 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
187 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
189 return AVERROR(ENOMEM);
191 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
192 RETURN_ERROR(AVERROR(EIO));
195 /* skip metadata block for unsupported types */
197 ret = avio_skip(s->pb, metadata_size);
202 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
204 /* STREAMINFO can only occur once */
205 if (found_streaminfo) {
206 RETURN_ERROR(AVERROR_INVALIDDATA);
208 if (metadata_size != FLAC_STREAMINFO_SIZE) {
209 RETURN_ERROR(AVERROR_INVALIDDATA);
211 found_streaminfo = 1;
212 st->codec->extradata = buffer;
213 st->codec->extradata_size = metadata_size;
216 /* get codec params from STREAMINFO header */
217 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
219 /* set time base and duration */
220 if (si.samplerate > 0) {
221 avpriv_set_pts_info(st, 64, 1, si.samplerate);
223 st->duration = si.samples;
225 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
228 const uint8_t *offset;
229 int i, chapters, track, ti;
230 if (metadata_size < 431)
231 RETURN_ERROR(AVERROR_INVALIDDATA);
232 offset = buffer + 395;
233 chapters = bytestream_get_byte(&offset) - 1;
235 RETURN_ERROR(AVERROR_INVALIDDATA);
236 for (i = 0; i < chapters; i++) {
237 if (offset + 36 - buffer > metadata_size)
238 RETURN_ERROR(AVERROR_INVALIDDATA);
239 start = bytestream_get_be64(&offset);
240 track = bytestream_get_byte(&offset);
241 bytestream_get_buffer(&offset, isrc, 12);
244 ti = bytestream_get_byte(&offset);
245 if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
247 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
250 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
251 ret = parse_picture(s, buffer, metadata_size);
254 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
258 /* STREAMINFO must be the first block */
259 if (!found_streaminfo) {
260 RETURN_ERROR(AVERROR_INVALIDDATA);
262 /* process supported blocks other than STREAMINFO */
263 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
264 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
265 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
279 static int flac_probe(AVProbeData *p)
281 if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
283 return AVPROBE_SCORE_MAX/2;
286 AVInputFormat ff_flac_demuxer = {
288 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
289 .read_probe = flac_probe,
290 .read_header = flac_read_header,
291 .read_packet = ff_raw_read_partial_packet,
292 .flags = AVFMT_GENERIC_INDEX,
293 .extensions = "flac",
294 .raw_codec_id = AV_CODEC_ID_FLAC,