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