Libav
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 "flac_picture.h"
25 #include "internal.h"
26 #include "rawdec.h"
27 #include "oggdec.h"
28 #include "vorbiscomment.h"
29 #include "libavcodec/bytestream.h"
30 
32 {
33  int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
34  uint8_t header[4];
37  if (!st)
38  return AVERROR(ENOMEM);
42  /* the parameters will be extracted from the compressed bitstream */
43 
44  /* if fLaC marker is not found, assume there is no header */
45  if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
46  avio_seek(s->pb, -4, SEEK_CUR);
47  return 0;
48  }
49 
50  /* process metadata blocks */
51  while (!s->pb->eof_reached && !metadata_last) {
52  avio_read(s->pb, header, 4);
53  avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
54  &metadata_size);
55  switch (metadata_type) {
56  /* allocate and read metadata block for supported types */
61  buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
62  if (!buffer) {
63  return AVERROR(ENOMEM);
64  }
65  if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
66  av_freep(&buffer);
67  return AVERROR(EIO);
68  }
69  break;
70  /* skip metadata block for unsupported types */
71  default:
72  ret = avio_skip(s->pb, metadata_size);
73  if (ret < 0)
74  return ret;
75  }
76 
77  if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
78  FLACStreaminfo si;
79  /* STREAMINFO can only occur once */
80  if (found_streaminfo) {
81  av_freep(&buffer);
82  return AVERROR_INVALIDDATA;
83  }
84  if (metadata_size != FLAC_STREAMINFO_SIZE) {
85  av_freep(&buffer);
86  return AVERROR_INVALIDDATA;
87  }
88  found_streaminfo = 1;
89  st->codec->extradata = buffer;
90  st->codec->extradata_size = metadata_size;
91  buffer = NULL;
92 
93  /* get codec params from STREAMINFO header */
95 
96  /* set time base and duration */
97  if (si.samplerate > 0) {
98  avpriv_set_pts_info(st, 64, 1, si.samplerate);
99  if (si.samples > 0)
100  st->duration = si.samples;
101  }
102  } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
103  uint8_t isrc[13];
104  uint64_t start;
105  const uint8_t *offset;
106  int i, chapters, track, ti;
107  if (metadata_size < 431)
108  return AVERROR_INVALIDDATA;
109  offset = buffer + 395;
110  chapters = bytestream_get_byte(&offset) - 1;
111  if (chapters <= 0)
112  return AVERROR_INVALIDDATA;
113  for (i = 0; i < chapters; i++) {
114  if (offset + 36 - buffer > metadata_size)
115  return AVERROR_INVALIDDATA;
116  start = bytestream_get_be64(&offset);
117  track = bytestream_get_byte(&offset);
118  bytestream_get_buffer(&offset, isrc, 12);
119  isrc[12] = 0;
120  offset += 14;
121  ti = bytestream_get_byte(&offset);
122  if (ti <= 0) return AVERROR_INVALIDDATA;
123  offset += ti * 12;
124  avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
125  }
126  } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
127  ret = ff_flac_parse_picture(s, buffer, metadata_size);
128  av_freep(&buffer);
129  if (ret < 0) {
130  av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
131  return ret;
132  }
133  } else {
134  /* STREAMINFO must be the first block */
135  if (!found_streaminfo) {
136  av_freep(&buffer);
137  return AVERROR_INVALIDDATA;
138  }
139  /* process supported blocks other than STREAMINFO */
140  if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
141  if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
142  av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
143  }
144  }
145  av_freep(&buffer);
146  }
147  }
148 
149  return 0;
150 }
151 
152 static int flac_probe(AVProbeData *p)
153 {
154  if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
155  return 0;
157 }
158 
160  .name = "flac",
161  .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
162  .read_probe = flac_probe,
163  .read_header = flac_read_header,
164  .read_packet = ff_raw_read_partial_packet,
165  .flags = AVFMT_GENERIC_INDEX,
166  .extensions = "flac",
167  .raw_codec_id = AV_CODEC_ID_FLAC,
168 };