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"
24 #include "flac_picture.h"
28 #include "vorbiscomment.h"
29 #include "libavcodec/bytestream.h"
31 static int flac_read_header(AVFormatContext *s)
33 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
36 AVStream *st = avformat_new_stream(s, NULL);
38 return AVERROR(ENOMEM);
39 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
40 st->codec->codec_id = AV_CODEC_ID_FLAC;
41 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
42 /* the parameters will be extracted from the compressed bitstream */
44 /* if fLaC marker is not found, assume there is no header */
45 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
46 avio_seek(s->pb, -4, SEEK_CUR);
50 /* process metadata blocks */
51 while (!url_feof(s->pb) && !metadata_last) {
52 avio_read(s->pb, header, 4);
53 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
55 switch (metadata_type) {
56 /* allocate and read metadata block for supported types */
57 case FLAC_METADATA_TYPE_STREAMINFO:
58 case FLAC_METADATA_TYPE_CUESHEET:
59 case FLAC_METADATA_TYPE_PICTURE:
60 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
61 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
63 return AVERROR(ENOMEM);
65 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
66 RETURN_ERROR(AVERROR(EIO));
69 /* skip metadata block for unsupported types */
71 ret = avio_skip(s->pb, metadata_size);
76 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
78 /* STREAMINFO can only occur once */
79 if (found_streaminfo) {
80 RETURN_ERROR(AVERROR_INVALIDDATA);
82 if (metadata_size != FLAC_STREAMINFO_SIZE) {
83 RETURN_ERROR(AVERROR_INVALIDDATA);
86 st->codec->extradata = buffer;
87 st->codec->extradata_size = metadata_size;
90 /* get codec params from STREAMINFO header */
91 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
93 /* set time base and duration */
94 if (si.samplerate > 0) {
95 avpriv_set_pts_info(st, 64, 1, si.samplerate);
97 st->duration = si.samples;
99 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
102 const uint8_t *offset;
103 int i, chapters, track, ti;
104 if (metadata_size < 431)
105 RETURN_ERROR(AVERROR_INVALIDDATA);
106 offset = buffer + 395;
107 chapters = bytestream_get_byte(&offset) - 1;
109 RETURN_ERROR(AVERROR_INVALIDDATA);
110 for (i = 0; i < chapters; i++) {
111 if (offset + 36 - buffer > metadata_size)
112 RETURN_ERROR(AVERROR_INVALIDDATA);
113 start = bytestream_get_be64(&offset);
114 track = bytestream_get_byte(&offset);
115 bytestream_get_buffer(&offset, isrc, 12);
118 ti = bytestream_get_byte(&offset);
119 if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
121 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
124 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
125 ret = ff_flac_parse_picture(s, buffer, metadata_size);
128 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
132 /* STREAMINFO must be the first block */
133 if (!found_streaminfo) {
134 RETURN_ERROR(AVERROR_INVALIDDATA);
136 /* process supported blocks other than STREAMINFO */
137 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
138 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
139 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
153 static int flac_probe(AVProbeData *p)
155 if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
157 return AVPROBE_SCORE_EXTENSION;
160 AVInputFormat ff_flac_demuxer = {
162 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
163 .read_probe = flac_probe,
164 .read_header = flac_read_header,
165 .read_packet = ff_raw_read_partial_packet,
166 .flags = AVFMT_GENERIC_INDEX,
167 .extensions = "flac",
168 .raw_codec_id = AV_CODEC_ID_FLAC,