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 uint8_t mimetype[64], *desc = NULL, *data = NULL;
38 AVIOContext *pb = NULL;
40 int type, width, height;
43 pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
45 return AVERROR(ENOMEM);
47 /* read the picture type */
49 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
50 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
51 if (s->error_recognition & AV_EF_EXPLODE) {
52 RETURN_ERROR(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 RETURN_ERROR(AVERROR(ENOMEM));
91 if (avio_read(pb, desc, len) != len) {
92 av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
93 if (s->error_recognition & AV_EF_EXPLODE)
100 /* picture metadata */
101 width = avio_rb32(pb);
102 height = avio_rb32(pb);
108 av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
109 if (s->error_recognition & AV_EF_EXPLODE)
110 ret = AVERROR_INVALIDDATA;
113 if (!(data = av_malloc(len))) {
114 RETURN_ERROR(AVERROR(ENOMEM));
116 if (avio_read(pb, data, len) != len) {
117 av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
118 if (s->error_recognition & AV_EF_EXPLODE)
123 st = avformat_new_stream(s, NULL);
125 RETURN_ERROR(AVERROR(ENOMEM));
128 av_init_packet(&st->attached_pic);
129 st->attached_pic.data = data;
130 st->attached_pic.size = len;
131 st->attached_pic.destruct = av_destruct_packet;
132 st->attached_pic.stream_index = st->index;
133 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
135 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
136 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
137 st->codec->codec_id = id;
138 st->codec->width = width;
139 st->codec->height = height;
140 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
142 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
156 static int flac_read_header(AVFormatContext *s)
158 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
160 uint8_t *buffer=NULL;
161 AVStream *st = avformat_new_stream(s, NULL);
163 return AVERROR(ENOMEM);
164 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
165 st->codec->codec_id = AV_CODEC_ID_FLAC;
166 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
167 /* the parameters will be extracted from the compressed bitstream */
169 /* if fLaC marker is not found, assume there is no header */
170 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
171 avio_seek(s->pb, -4, SEEK_CUR);
175 /* process metadata blocks */
176 while (!url_feof(s->pb) && !metadata_last) {
177 avio_read(s->pb, header, 4);
178 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
180 switch (metadata_type) {
181 /* allocate and read metadata block for supported types */
182 case FLAC_METADATA_TYPE_STREAMINFO:
183 case FLAC_METADATA_TYPE_CUESHEET:
184 case FLAC_METADATA_TYPE_PICTURE:
185 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
186 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
188 return AVERROR(ENOMEM);
190 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
191 RETURN_ERROR(AVERROR(EIO));
194 /* skip metadata block for unsupported types */
196 ret = avio_skip(s->pb, metadata_size);
201 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
203 /* STREAMINFO can only occur once */
204 if (found_streaminfo) {
205 RETURN_ERROR(AVERROR_INVALIDDATA);
207 if (metadata_size != FLAC_STREAMINFO_SIZE) {
208 RETURN_ERROR(AVERROR_INVALIDDATA);
210 found_streaminfo = 1;
211 st->codec->extradata = buffer;
212 st->codec->extradata_size = metadata_size;
215 /* get codec params from STREAMINFO header */
216 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
218 /* set time base and duration */
219 if (si.samplerate > 0) {
220 avpriv_set_pts_info(st, 64, 1, si.samplerate);
222 st->duration = si.samples;
224 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
227 const uint8_t *offset;
228 int i, chapters, track, ti;
229 if (metadata_size < 431)
230 RETURN_ERROR(AVERROR_INVALIDDATA);
231 offset = buffer + 395;
232 chapters = bytestream_get_byte(&offset) - 1;
234 RETURN_ERROR(AVERROR_INVALIDDATA);
235 for (i = 0; i < chapters; i++) {
236 if (offset + 36 - buffer > metadata_size)
237 RETURN_ERROR(AVERROR_INVALIDDATA);
238 start = bytestream_get_be64(&offset);
239 track = bytestream_get_byte(&offset);
240 bytestream_get_buffer(&offset, isrc, 12);
243 ti = bytestream_get_byte(&offset);
244 if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
246 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
249 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
250 ret = parse_picture(s, buffer, metadata_size);
253 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
257 /* STREAMINFO must be the first block */
258 if (!found_streaminfo) {
259 RETURN_ERROR(AVERROR_INVALIDDATA);
261 /* process supported blocks other than STREAMINFO */
262 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
263 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
264 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
278 static int flac_probe(AVProbeData *p)
280 const uint8_t *bufptr = p->buf;
281 const uint8_t *end = p->buf + p->buf_size;
283 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
284 else return AVPROBE_SCORE_MAX/2;
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,