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