flacdec.c
Go to the documentation of this file.
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  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  pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
42  if (!pb)
43  return AVERROR(ENOMEM);
44 
45  /* read the picture type */
46  type = avio_rb32(pb);
47  if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
48  av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
50  ret = AVERROR_INVALIDDATA;
51  goto fail;
52  }
53  type = 0;
54  }
55 
56  /* picture mimetype */
57  len = avio_rb32(pb);
58  if (len <= 0 ||
59  avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
60  av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
61  "picture.\n");
63  ret = AVERROR_INVALIDDATA;
64  goto fail;
65  }
66  mimetype[len] = 0;
67 
68  while (mime->id != AV_CODEC_ID_NONE) {
69  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
70  id = mime->id;
71  break;
72  }
73  mime++;
74  }
75  if (id == AV_CODEC_ID_NONE) {
76  av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
77  mimetype);
79  ret = AVERROR_INVALIDDATA;
80  goto fail;
81  }
82 
83  /* picture description */
84  len = avio_rb32(pb);
85  if (len > 0) {
86  if (!(desc = av_malloc(len + 1))) {
87  ret = AVERROR(ENOMEM);
88  goto fail;
89  }
90 
91  if (avio_read(pb, desc, len) != len) {
92  av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
94  ret = AVERROR(EIO);
95  goto fail;
96  }
97  desc[len] = 0;
98  }
99 
100  /* picture metadata */
101  width = avio_rb32(pb);
102  height = avio_rb32(pb);
103  avio_skip(pb, 8);
104 
105  /* picture data */
106  len = avio_rb32(pb);
107  if (len <= 0) {
108  av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
110  ret = AVERROR_INVALIDDATA;
111  goto fail;
112  }
113  if (!(data = av_malloc(len))) {
114  ret = AVERROR(ENOMEM);
115  goto fail;
116  }
117  if (avio_read(pb, data, len) != len) {
118  av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
120  ret = AVERROR(EIO);
121  goto fail;
122  }
123 
124  st = avformat_new_stream(s, NULL);
125  if (!st) {
126  ret = AVERROR(ENOMEM);
127  goto fail;
128  }
129 
131  st->attached_pic.data = data;
132  st->attached_pic.size = len;
134  st->attached_pic.stream_index = st->index;
136 
139  st->codec->codec_id = id;
140  st->codec->width = width;
141  st->codec->height = height;
142  av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
143  if (desc)
144  av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
145 
146  av_freep(&pb);
147 
148  return 0;
149 
150 fail:
151  av_freep(&desc);
152  av_freep(&data);
153  av_freep(&pb);
154  return ret;
155 
156 }
157 
159 {
160  int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
161  uint8_t header[4];
164  if (!st)
165  return AVERROR(ENOMEM);
169  /* the parameters will be extracted from the compressed bitstream */
170 
171  /* if fLaC marker is not found, assume there is no header */
172  if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
173  avio_seek(s->pb, -4, SEEK_CUR);
174  return 0;
175  }
176 
177  /* process metadata blocks */
178  while (!s->pb->eof_reached && !metadata_last) {
179  avio_read(s->pb, header, 4);
180  avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
181  &metadata_size);
182  switch (metadata_type) {
183  /* allocate and read metadata block for supported types */
188  buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
189  if (!buffer) {
190  return AVERROR(ENOMEM);
191  }
192  if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
193  av_freep(&buffer);
194  return AVERROR(EIO);
195  }
196  break;
197  /* skip metadata block for unsupported types */
198  default:
199  ret = avio_skip(s->pb, metadata_size);
200  if (ret < 0)
201  return ret;
202  }
203 
204  if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
205  FLACStreaminfo si;
206  /* STREAMINFO can only occur once */
207  if (found_streaminfo) {
208  av_freep(&buffer);
209  return AVERROR_INVALIDDATA;
210  }
211  if (metadata_size != FLAC_STREAMINFO_SIZE) {
212  av_freep(&buffer);
213  return AVERROR_INVALIDDATA;
214  }
215  found_streaminfo = 1;
216  st->codec->extradata = buffer;
217  st->codec->extradata_size = metadata_size;
218  buffer = NULL;
219 
220  /* get codec params from STREAMINFO header */
222 
223  /* set time base and duration */
224  if (si.samplerate > 0) {
225  avpriv_set_pts_info(st, 64, 1, si.samplerate);
226  if (si.samples > 0)
227  st->duration = si.samples;
228  }
229  } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
230  uint8_t isrc[13];
231  uint64_t start;
232  const uint8_t *offset;
233  int i, chapters, track, ti;
234  if (metadata_size < 431)
235  return AVERROR_INVALIDDATA;
236  offset = buffer + 395;
237  chapters = bytestream_get_byte(&offset) - 1;
238  if (chapters <= 0)
239  return AVERROR_INVALIDDATA;
240  for (i = 0; i < chapters; i++) {
241  if (offset + 36 - buffer > metadata_size)
242  return AVERROR_INVALIDDATA;
243  start = bytestream_get_be64(&offset);
244  track = bytestream_get_byte(&offset);
245  bytestream_get_buffer(&offset, isrc, 12);
246  isrc[12] = 0;
247  offset += 14;
248  ti = bytestream_get_byte(&offset);
249  if (ti <= 0) return AVERROR_INVALIDDATA;
250  offset += ti * 12;
251  avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
252  }
253  } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
254  ret = parse_picture(s, buffer, metadata_size);
255  av_freep(&buffer);
256  if (ret < 0) {
257  av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
258  return ret;
259  }
260  } else {
261  /* STREAMINFO must be the first block */
262  if (!found_streaminfo) {
263  av_freep(&buffer);
264  return AVERROR_INVALIDDATA;
265  }
266  /* process supported blocks other than STREAMINFO */
267  if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
268  if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
269  av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
270  }
271  }
272  av_freep(&buffer);
273  }
274  }
275 
276  return 0;
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_MAX/2;
284 }
285 
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 };