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"
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 AVCodecID id = AV_CODEC_ID_NONE;
35 AVBufferRef *data = NULL;
36 uint8_t mimetype[64], *desc = NULL;
37 AVIOContext *pb = NULL;
39 int type, width, height;
42 pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
44 return AVERROR(ENOMEM);
46 /* read the picture type */
48 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
49 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
50 if (s->error_recognition & AV_EF_EXPLODE) {
51 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 if (s->error_recognition & AV_EF_EXPLODE)
64 ret = AVERROR_INVALIDDATA;
69 while (mime->id != AV_CODEC_ID_NONE) {
70 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
76 if (id == AV_CODEC_ID_NONE) {
77 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
79 if (s->error_recognition & AV_EF_EXPLODE)
80 ret = AVERROR_INVALIDDATA;
84 /* picture description */
87 if (!(desc = av_malloc(len + 1))) {
88 ret = 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 ret = AVERROR(ENOMEM);
118 if (avio_read(pb, data->data, len) != len) {
119 av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
120 if (s->error_recognition & AV_EF_EXPLODE)
125 st = avformat_new_stream(s, NULL);
127 ret = AVERROR(ENOMEM);
131 av_init_packet(&st->attached_pic);
132 st->attached_pic.buf = data;
133 st->attached_pic.data = data->data;
134 st->attached_pic.size = len;
135 st->attached_pic.stream_index = st->index;
136 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
138 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
139 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
140 st->codec->codec_id = id;
141 st->codec->width = width;
142 st->codec->height = height;
143 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
145 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
152 av_buffer_unref(&data);
159 static int flac_read_header(AVFormatContext *s)
161 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
163 uint8_t *buffer=NULL;
164 AVStream *st = avformat_new_stream(s, NULL);
166 return AVERROR(ENOMEM);
167 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
168 st->codec->codec_id = AV_CODEC_ID_FLAC;
169 st->need_parsing = AVSTREAM_PARSE_FULL;
170 /* the parameters will be extracted from the compressed bitstream */
172 /* if fLaC marker is not found, assume there is no header */
173 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
174 avio_seek(s->pb, -4, SEEK_CUR);
178 /* process metadata blocks */
179 while (!s->pb->eof_reached && !metadata_last) {
180 avio_read(s->pb, header, 4);
181 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
183 switch (metadata_type) {
184 /* allocate and read metadata block for supported types */
185 case FLAC_METADATA_TYPE_STREAMINFO:
186 case FLAC_METADATA_TYPE_CUESHEET:
187 case FLAC_METADATA_TYPE_PICTURE:
188 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
189 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
191 return AVERROR(ENOMEM);
193 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
198 /* skip metadata block for unsupported types */
200 ret = avio_skip(s->pb, metadata_size);
205 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
207 /* STREAMINFO can only occur once */
208 if (found_streaminfo) {
210 return AVERROR_INVALIDDATA;
212 if (metadata_size != FLAC_STREAMINFO_SIZE) {
214 return AVERROR_INVALIDDATA;
216 found_streaminfo = 1;
217 st->codec->extradata = buffer;
218 st->codec->extradata_size = metadata_size;
221 /* get codec params from STREAMINFO header */
222 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
224 /* set time base and duration */
225 if (si.samplerate > 0) {
226 avpriv_set_pts_info(st, 64, 1, si.samplerate);
228 st->duration = si.samples;
230 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
233 const uint8_t *offset;
234 int i, chapters, track, ti;
235 if (metadata_size < 431)
236 return AVERROR_INVALIDDATA;
237 offset = buffer + 395;
238 chapters = bytestream_get_byte(&offset) - 1;
240 return AVERROR_INVALIDDATA;
241 for (i = 0; i < chapters; i++) {
242 if (offset + 36 - buffer > metadata_size)
243 return AVERROR_INVALIDDATA;
244 start = bytestream_get_be64(&offset);
245 track = bytestream_get_byte(&offset);
246 bytestream_get_buffer(&offset, isrc, 12);
249 ti = bytestream_get_byte(&offset);
250 if (ti <= 0) return AVERROR_INVALIDDATA;
252 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
254 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
255 ret = parse_picture(s, buffer, metadata_size);
258 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
262 /* STREAMINFO must be the first block */
263 if (!found_streaminfo) {
265 return AVERROR_INVALIDDATA;
267 /* process supported blocks other than STREAMINFO */
268 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
269 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
270 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
280 static int flac_probe(AVProbeData *p)
282 if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
284 return AVPROBE_SCORE_EXTENSION;
287 AVInputFormat ff_flac_demuxer = {
289 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
290 .read_probe = flac_probe,
291 .read_header = flac_read_header,
292 .read_packet = ff_raw_read_partial_packet,
293 .flags = AVFMT_GENERIC_INDEX,
294 .extensions = "flac",
295 .raw_codec_id = AV_CODEC_ID_FLAC,