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 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
Bytestream IO Context.
Definition: avio.h:68
void avpriv_flac_parse_block_header(const uint8_t *block_header, int *last, int *type, int *size)
Parse the metadata block parameters from the header.
Definition: flac.c:235
enum AVCodecID id
Definition: mxfenc.c:85
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:3283
int index
stream index in AVFormatContext
Definition: avformat.h:623
int size
Definition: avcodec.h:916
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:2815
Format I/O context.
Definition: avformat.h:828
char str[32]
Definition: internal.h:41
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:577
enum AVStreamParseType need_parsing
Definition: avformat.h:775
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
enum AVCodecID id
Definition: internal.h:42
int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:446
void(* destruct)(struct AVPacket *)
Definition: avcodec.h:938
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition: aviobuf.c:107
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:95
AVDictionary * metadata
Definition: avformat.h:972
AVInputFormat ff_flac_demuxer
Definition: flacdec.c:286
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:546
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:33
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2736
void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, const uint8_t *buffer)
Parse the Streaminfo metadata block.
Definition: flac.c:202
static int flac_probe(AVProbeData *p)
Definition: flacdec.c:279
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:90
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:341
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:340
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:114
int width
picture width / height.
Definition: avcodec.h:1508
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:33
static char buffer[20]
Definition: seek-test.c:31
AVDictionary * metadata
Definition: avformat.h:699
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:318
Stream structure.
Definition: avformat.h:622
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
enum AVMediaType codec_type
Definition: avcodec.h:1347
enum AVCodecID codec_id
Definition: avcodec.h:1350
AVIOContext * pb
I/O context.
Definition: avformat.h:861
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:28
int extradata_size
Definition: avcodec.h:1455
static int flac_read_header(AVFormatContext *s)
Definition: flacdec.c:158
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:63
This structure contains the data a format has to probe a file.
Definition: avformat.h:338
int height
Definition: gxfenc.c:72
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
full parsing and repack
Definition: avformat.h:576
Main libavformat public API header.
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:994
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:688
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:42
static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
Definition: flacdec.c:31
int eof_reached
true if eof reached
Definition: avio.h:96
int len
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:455
int stream_index
Definition: avcodec.h:917
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:669
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:713