flacdec: export replaygain tags from Vorbis comments
[libav.git] / libavformat / flacdec.c
CommitLineData
81f052cb
JR
1/*
2 * Raw FLAC demuxer
3 * Copyright (c) 2001 Fabrice Bellard
4 *
2912e87a 5 * This file is part of Libav.
81f052cb 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
81f052cb
JR
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.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
81f052cb
JR
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
81f052cb
JR
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
f48b9304 22#include "libavcodec/flac.h"
81f052cb 23#include "avformat.h"
601d6228 24#include "flac_picture.h"
c3f9ebf7 25#include "internal.h"
4ca31edc 26#include "rawdec.h"
f48b9304 27#include "oggdec.h"
66061a12 28#include "vorbiscomment.h"
1d55f8d5 29#include "replaygain.h"
3c1f6086 30#include "libavcodec/bytestream.h"
81f052cb 31
6e9651d1 32static int flac_read_header(AVFormatContext *s)
81f052cb 33{
f48b9304
JR
34 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
35 uint8_t header[4];
36 uint8_t *buffer=NULL;
3b3bbdd3 37 AVStream *st = avformat_new_stream(s, NULL);
81f052cb
JR
38 if (!st)
39 return AVERROR(ENOMEM);
72415b2a 40 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
36ef5369 41 st->codec->codec_id = AV_CODEC_ID_FLAC;
81f052cb
JR
42 st->need_parsing = AVSTREAM_PARSE_FULL;
43 /* the parameters will be extracted from the compressed bitstream */
44
f48b9304 45 /* if fLaC marker is not found, assume there is no header */
b7effd4e 46 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
6b4aa5da 47 avio_seek(s->pb, -4, SEEK_CUR);
f48b9304 48 return 0;
29b0d168 49 }
f48b9304
JR
50
51 /* process metadata blocks */
66e5b1df 52 while (!s->pb->eof_reached && !metadata_last) {
b7effd4e 53 avio_read(s->pb, header, 4);
d9cca9fc 54 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
f48b9304
JR
55 &metadata_size);
56 switch (metadata_type) {
57 /* allocate and read metadata block for supported types */
58 case FLAC_METADATA_TYPE_STREAMINFO:
3c1f6086 59 case FLAC_METADATA_TYPE_CUESHEET:
ae702edf 60 case FLAC_METADATA_TYPE_PICTURE:
f48b9304
JR
61 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
62 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
63 if (!buffer) {
2874c81c 64 return AVERROR(ENOMEM);
f48b9304 65 }
b7effd4e 66 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
f48b9304 67 av_freep(&buffer);
5ae092ee 68 return AVERROR(EIO);
f48b9304
JR
69 }
70 break;
71 /* skip metadata block for unsupported types */
72 default:
45a8a02a 73 ret = avio_skip(s->pb, metadata_size);
f48b9304
JR
74 if (ret < 0)
75 return ret;
76 }
77
78 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
79 FLACStreaminfo si;
80 /* STREAMINFO can only occur once */
81 if (found_streaminfo) {
82 av_freep(&buffer);
83 return AVERROR_INVALIDDATA;
84 }
85 if (metadata_size != FLAC_STREAMINFO_SIZE) {
86 av_freep(&buffer);
87 return AVERROR_INVALIDDATA;
88 }
89 found_streaminfo = 1;
90 st->codec->extradata = buffer;
91 st->codec->extradata_size = metadata_size;
92 buffer = NULL;
93
94 /* get codec params from STREAMINFO header */
d9cca9fc 95 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
f48b9304
JR
96
97 /* set time base and duration */
98 if (si.samplerate > 0) {
c3f9ebf7 99 avpriv_set_pts_info(st, 64, 1, si.samplerate);
f48b9304
JR
100 if (si.samples > 0)
101 st->duration = si.samples;
102 }
3c1f6086
PM
103 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
104 uint8_t isrc[13];
105 uint64_t start;
106 const uint8_t *offset;
17c84f4e 107 int i, chapters, track, ti;
3c1f6086
PM
108 if (metadata_size < 431)
109 return AVERROR_INVALIDDATA;
110 offset = buffer + 395;
111 chapters = bytestream_get_byte(&offset) - 1;
112 if (chapters <= 0)
113 return AVERROR_INVALIDDATA;
114 for (i = 0; i < chapters; i++) {
115 if (offset + 36 - buffer > metadata_size)
116 return AVERROR_INVALIDDATA;
117 start = bytestream_get_be64(&offset);
118 track = bytestream_get_byte(&offset);
119 bytestream_get_buffer(&offset, isrc, 12);
120 isrc[12] = 0;
121 offset += 14;
122 ti = bytestream_get_byte(&offset);
123 if (ti <= 0) return AVERROR_INVALIDDATA;
17c84f4e 124 offset += ti * 12;
3c1f6086
PM
125 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
126 }
ae702edf 127 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
601d6228 128 ret = ff_flac_parse_picture(s, buffer, metadata_size);
ae702edf
AK
129 av_freep(&buffer);
130 if (ret < 0) {
131 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
132 return ret;
133 }
f48b9304
JR
134 } else {
135 /* STREAMINFO must be the first block */
136 if (!found_streaminfo) {
137 av_freep(&buffer);
138 return AVERROR_INVALIDDATA;
139 }
140 /* process supported blocks other than STREAMINFO */
141 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
b53cde48 142 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
f48b9304
JR
143 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
144 }
145 }
146 av_freep(&buffer);
147 }
148 }
149
1d55f8d5
AG
150 ret = ff_replaygain_export(st, s->metadata);
151 if (ret < 0)
152 return ret;
153
81f052cb
JR
154 return 0;
155}
156
157static int flac_probe(AVProbeData *p)
158{
8425d693
XW
159 if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
160 return 0;
e0f8be64 161 return AVPROBE_SCORE_EXTENSION;
81f052cb
JR
162}
163
c6610a21 164AVInputFormat ff_flac_demuxer = {
dfc2c4d9
AK
165 .name = "flac",
166 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
167 .read_probe = flac_probe,
168 .read_header = flac_read_header,
169 .read_packet = ff_raw_read_partial_packet,
20234a4b
MS
170 .flags = AVFMT_GENERIC_INDEX,
171 .extensions = "flac",
36ef5369 172 .raw_codec_id = AV_CODEC_ID_FLAC,
81f052cb 173};