]> git.sesse.net Git - ffmpeg/blob - libavformat/flacdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / flacdec.c
1 /*
2  * Raw FLAC demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavcodec/flac.h"
23 #include "avformat.h"
24 #include "id3v2.h"
25 #include "internal.h"
26 #include "rawdec.h"
27 #include "oggdec.h"
28 #include "vorbiscomment.h"
29 #include "libavcodec/bytestream.h"
30
31 #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0)
32
33 static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
34 {
35     const CodecMime *mime = ff_id3v2_mime_tags;
36     enum  AVCodecID      id = AV_CODEC_ID_NONE;
37     AVBufferRef *data = NULL;
38     uint8_t mimetype[64], *desc = NULL;
39     AVIOContext *pb = NULL;
40     AVStream *st;
41     int type, width, height;
42     int len, ret = 0;
43
44     pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
45     if (!pb)
46         return AVERROR(ENOMEM);
47
48     /* read the picture type */
49     type      = avio_rb32(pb);
50     if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
51         av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
52         if (s->error_recognition & AV_EF_EXPLODE) {
53             RETURN_ERROR(AVERROR_INVALIDDATA);
54         }
55         type = 0;
56     }
57
58     /* picture mimetype */
59     len  = avio_rb32(pb);
60     if (len <= 0 ||
61         avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
62         av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
63                "picture.\n");
64         if (s->error_recognition & AV_EF_EXPLODE)
65             ret = AVERROR_INVALIDDATA;
66         goto fail;
67     }
68     mimetype[len] = 0;
69
70     while (mime->id != AV_CODEC_ID_NONE) {
71         if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
72             id = mime->id;
73             break;
74         }
75         mime++;
76     }
77     if (id == AV_CODEC_ID_NONE) {
78         av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
79                mimetype);
80         if (s->error_recognition & AV_EF_EXPLODE)
81             ret = AVERROR_INVALIDDATA;
82         goto fail;
83     }
84
85     /* picture description */
86     len = avio_rb32(pb);
87     if (len > 0) {
88         if (!(desc = av_malloc(len + 1))) {
89             RETURN_ERROR(AVERROR(ENOMEM));
90         }
91
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)
95                 ret = AVERROR(EIO);
96             goto fail;
97         }
98         desc[len] = 0;
99     }
100
101     /* picture metadata */
102     width  = avio_rb32(pb);
103     height = avio_rb32(pb);
104     avio_skip(pb, 8);
105
106     /* picture data */
107     len = avio_rb32(pb);
108     if (len <= 0) {
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;
112         goto fail;
113     }
114     if (!(data = av_buffer_alloc(len))) {
115         RETURN_ERROR(AVERROR(ENOMEM));
116     }
117     if (avio_read(pb, data->data, len) != len) {
118         av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
119         if (s->error_recognition & AV_EF_EXPLODE)
120             ret = AVERROR(EIO);
121         goto fail;
122     }
123
124     st = avformat_new_stream(s, NULL);
125     if (!st) {
126         RETURN_ERROR(AVERROR(ENOMEM));
127     }
128
129     av_init_packet(&st->attached_pic);
130     st->attached_pic.buf          = data;
131     st->attached_pic.data         = data->data;
132     st->attached_pic.size         = len;
133     st->attached_pic.stream_index = st->index;
134     st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
135
136     st->disposition      |= AV_DISPOSITION_ATTACHED_PIC;
137     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
138     st->codec->codec_id   = id;
139     st->codec->width      = width;
140     st->codec->height     = height;
141     av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
142     if (desc)
143         av_dict_set(&st->metadata, "title",   desc, AV_DICT_DONT_STRDUP_VAL);
144
145     av_freep(&pb);
146
147     return 0;
148
149 fail:
150     av_buffer_unref(&data);
151     av_freep(&desc);
152     av_freep(&pb);
153     return ret;
154
155 }
156
157 static int flac_read_header(AVFormatContext *s)
158 {
159     int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
160     uint8_t header[4];
161     uint8_t *buffer=NULL;
162     AVStream *st = avformat_new_stream(s, NULL);
163     if (!st)
164         return AVERROR(ENOMEM);
165     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
166     st->codec->codec_id = AV_CODEC_ID_FLAC;
167     st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
168     /* the parameters will be extracted from the compressed bitstream */
169
170     /* if fLaC marker is not found, assume there is no header */
171     if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
172         avio_seek(s->pb, -4, SEEK_CUR);
173         return 0;
174     }
175
176     /* process metadata blocks */
177     while (!url_feof(s->pb) && !metadata_last) {
178         avio_read(s->pb, header, 4);
179         avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
180                                    &metadata_size);
181         switch (metadata_type) {
182         /* allocate and read metadata block for supported types */
183         case FLAC_METADATA_TYPE_STREAMINFO:
184         case FLAC_METADATA_TYPE_CUESHEET:
185         case FLAC_METADATA_TYPE_PICTURE:
186         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
187             buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
188             if (!buffer) {
189                 return AVERROR(ENOMEM);
190             }
191             if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
192                 RETURN_ERROR(AVERROR(EIO));
193             }
194             break;
195         /* skip metadata block for unsupported types */
196         default:
197             ret = avio_skip(s->pb, metadata_size);
198             if (ret < 0)
199                 return ret;
200         }
201
202         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
203             FLACStreaminfo si;
204             /* STREAMINFO can only occur once */
205             if (found_streaminfo) {
206                 RETURN_ERROR(AVERROR_INVALIDDATA);
207             }
208             if (metadata_size != FLAC_STREAMINFO_SIZE) {
209                 RETURN_ERROR(AVERROR_INVALIDDATA);
210             }
211             found_streaminfo = 1;
212             st->codec->extradata      = buffer;
213             st->codec->extradata_size = metadata_size;
214             buffer = NULL;
215
216             /* get codec params from STREAMINFO header */
217             avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
218
219             /* set time base and duration */
220             if (si.samplerate > 0) {
221                 avpriv_set_pts_info(st, 64, 1, si.samplerate);
222                 if (si.samples > 0)
223                     st->duration = si.samples;
224             }
225         } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
226             uint8_t isrc[13];
227             uint64_t start;
228             const uint8_t *offset;
229             int i, chapters, track, ti;
230             if (metadata_size < 431)
231                 RETURN_ERROR(AVERROR_INVALIDDATA);
232             offset = buffer + 395;
233             chapters = bytestream_get_byte(&offset) - 1;
234             if (chapters <= 0)
235                 RETURN_ERROR(AVERROR_INVALIDDATA);
236             for (i = 0; i < chapters; i++) {
237                 if (offset + 36 - buffer > metadata_size)
238                     RETURN_ERROR(AVERROR_INVALIDDATA);
239                 start = bytestream_get_be64(&offset);
240                 track = bytestream_get_byte(&offset);
241                 bytestream_get_buffer(&offset, isrc, 12);
242                 isrc[12] = 0;
243                 offset += 14;
244                 ti = bytestream_get_byte(&offset);
245                 if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
246                 offset += ti * 12;
247                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
248             }
249             av_freep(&buffer);
250         } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
251             ret = parse_picture(s, buffer, metadata_size);
252             av_freep(&buffer);
253             if (ret < 0) {
254                 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
255                 return ret;
256             }
257         } else {
258             /* STREAMINFO must be the first block */
259             if (!found_streaminfo) {
260                 RETURN_ERROR(AVERROR_INVALIDDATA);
261             }
262             /* process supported blocks other than STREAMINFO */
263             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
264                 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
265                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
266                 }
267             }
268             av_freep(&buffer);
269         }
270     }
271
272     return 0;
273
274 fail:
275     av_free(buffer);
276     return ret;
277 }
278
279 static int flac_probe(AVProbeData *p)
280 {
281     if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
282         return 0;
283     return AVPROBE_SCORE_EXTENSION;
284 }
285
286 AVInputFormat ff_flac_demuxer = {
287     .name           = "flac",
288     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
289     .read_probe     = flac_probe,
290     .read_header    = flac_read_header,
291     .read_packet    = ff_raw_read_partial_packet,
292     .flags          = AVFMT_GENERIC_INDEX,
293     .extensions     = "flac",
294     .raw_codec_id   = AV_CODEC_ID_FLAC,
295 };