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 uint8_t mimetype[64], *desc = NULL, *data = NULL;
36 AVIOContext *pb = NULL;
38 int type, width, height;
41 pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
43 return AVERROR(ENOMEM);
45 /* read the picture type */
47 if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
48 av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
49 if (s->error_recognition & AV_EF_EXPLODE) {
50 ret = AVERROR_INVALIDDATA;
56 /* picture mimetype */
59 avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
60 av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
62 if (s->error_recognition & AV_EF_EXPLODE)
63 ret = AVERROR_INVALIDDATA;
68 while (mime->id != AV_CODEC_ID_NONE) {
69 if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
75 if (id == AV_CODEC_ID_NONE) {
76 av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
78 if (s->error_recognition & AV_EF_EXPLODE)
79 ret = AVERROR_INVALIDDATA;
83 /* picture description */
86 if (!(desc = av_malloc(len + 1))) {
87 ret = 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 ret = AVERROR(ENOMEM);
117 if (avio_read(pb, 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 ret = AVERROR(ENOMEM);
130 av_init_packet(&st->attached_pic);
131 st->attached_pic.data = data;
132 st->attached_pic.size = len;
133 st->attached_pic.destruct = av_destruct_packet;
134 st->attached_pic.stream_index = st->index;
135 st->attached_pic.flags |= AV_PKT_FLAG_KEY;
137 st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
138 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
139 st->codec->codec_id = id;
140 st->codec->width = width;
141 st->codec->height = height;
142 av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
144 av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
158 static int flac_read_header(AVFormatContext *s)
160 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
162 uint8_t *buffer=NULL;
163 AVStream *st = avformat_new_stream(s, NULL);
165 return AVERROR(ENOMEM);
166 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
167 st->codec->codec_id = AV_CODEC_ID_FLAC;
168 st->need_parsing = AVSTREAM_PARSE_FULL;
169 /* the parameters will be extracted from the compressed bitstream */
171 /* if fLaC marker is not found, assume there is no header */
172 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
173 avio_seek(s->pb, -4, SEEK_CUR);
177 /* process metadata blocks */
178 while (!s->pb->eof_reached && !metadata_last) {
179 avio_read(s->pb, header, 4);
180 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
182 switch (metadata_type) {
183 /* allocate and read metadata block for supported types */
184 case FLAC_METADATA_TYPE_STREAMINFO:
185 case FLAC_METADATA_TYPE_CUESHEET:
186 case FLAC_METADATA_TYPE_PICTURE:
187 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
188 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
190 return AVERROR(ENOMEM);
192 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
197 /* skip metadata block for unsupported types */
199 ret = avio_skip(s->pb, metadata_size);
204 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
206 /* STREAMINFO can only occur once */
207 if (found_streaminfo) {
209 return AVERROR_INVALIDDATA;
211 if (metadata_size != FLAC_STREAMINFO_SIZE) {
213 return AVERROR_INVALIDDATA;
215 found_streaminfo = 1;
216 st->codec->extradata = buffer;
217 st->codec->extradata_size = metadata_size;
220 /* get codec params from STREAMINFO header */
221 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
223 /* set time base and duration */
224 if (si.samplerate > 0) {
225 avpriv_set_pts_info(st, 64, 1, si.samplerate);
227 st->duration = si.samples;
229 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
232 const uint8_t *offset;
233 int i, chapters, track, ti;
234 if (metadata_size < 431)
235 return AVERROR_INVALIDDATA;
236 offset = buffer + 395;
237 chapters = bytestream_get_byte(&offset) - 1;
239 return AVERROR_INVALIDDATA;
240 for (i = 0; i < chapters; i++) {
241 if (offset + 36 - buffer > metadata_size)
242 return AVERROR_INVALIDDATA;
243 start = bytestream_get_be64(&offset);
244 track = bytestream_get_byte(&offset);
245 bytestream_get_buffer(&offset, isrc, 12);
248 ti = bytestream_get_byte(&offset);
249 if (ti <= 0) return AVERROR_INVALIDDATA;
251 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
253 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
254 ret = parse_picture(s, buffer, metadata_size);
257 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
261 /* STREAMINFO must be the first block */
262 if (!found_streaminfo) {
264 return AVERROR_INVALIDDATA;
266 /* process supported blocks other than STREAMINFO */
267 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
268 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
269 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
279 static int flac_probe(AVProbeData *p)
281 uint8_t *bufptr = p->buf;
282 uint8_t *end = p->buf + p->buf_size;
284 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
285 else return AVPROBE_SCORE_MAX/2;
288 AVInputFormat ff_flac_demuxer = {
290 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
291 .read_probe = flac_probe,
292 .read_header = flac_read_header,
293 .read_packet = ff_raw_read_partial_packet,
294 .flags = AVFMT_GENERIC_INDEX,
295 .extensions = "flac",
296 .raw_codec_id = AV_CODEC_ID_FLAC,