libavformat/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 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 #define RETURN_ERROR(code) do { ret = (code); goto fail; } while (0)
32 
33 static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
34 {
35  const CodecMime *mime = ff_id3v2_mime_tags;
36  enum AVCodecID id = AV_CODEC_ID_NONE;
38  uint8_t mimetype[64], *desc = NULL;
39  AVIOContext *pb = NULL;
40  AVStream *st;
41  int type, width, height;
42  int len, ret = 0;
43 
44  pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
45  if (!pb)
46  return AVERROR(ENOMEM);
47 
48  /* read the picture type */
49  type = avio_rb32(pb);
50  if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
51  av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
54  }
55  type = 0;
56  }
57 
58  /* picture mimetype */
59  len = avio_rb32(pb);
60  if (len <= 0 ||
61  avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
62  av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
63  "picture.\n");
65  ret = AVERROR_INVALIDDATA;
66  goto fail;
67  }
68  mimetype[len] = 0;
69 
70  while (mime->id != AV_CODEC_ID_NONE) {
71  if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
72  id = mime->id;
73  break;
74  }
75  mime++;
76  }
77  if (id == AV_CODEC_ID_NONE) {
78  av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
79  mimetype);
81  ret = AVERROR_INVALIDDATA;
82  goto fail;
83  }
84 
85  /* picture description */
86  len = avio_rb32(pb);
87  if (len > 0) {
88  if (!(desc = av_malloc(len + 1))) {
89  RETURN_ERROR(AVERROR(ENOMEM));
90  }
91 
92  if (avio_read(pb, desc, len) != len) {
93  av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
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);
111  ret = AVERROR_INVALIDDATA;
112  goto fail;
113  }
114  if (!(data = av_buffer_alloc(len))) {
115  RETURN_ERROR(AVERROR(ENOMEM));
116  }
117  if (avio_read(pb, data->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  RETURN_ERROR(AVERROR(ENOMEM));
127  }
128 
130  st->attached_pic.buf = data;
131  st->attached_pic.data = data->data;
132  st->attached_pic.size = len;
133  st->attached_pic.stream_index = st->index;
135 
138  st->codec->codec_id = id;
139  st->codec->width = width;
140  st->codec->height = height;
141  av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
142  if (desc)
143  av_dict_set(&st->metadata, "title", desc, AV_DICT_DONT_STRDUP_VAL);
144 
145  av_freep(&pb);
146 
147  return 0;
148 
149 fail:
150  av_buffer_unref(&data);
151  av_freep(&desc);
152  av_freep(&pb);
153  return ret;
154 
155 }
156 
158 {
159  int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
160  uint8_t header[4];
163  if (!st)
164  return AVERROR(ENOMEM);
168  /* the parameters will be extracted from the compressed bitstream */
169 
170  /* if fLaC marker is not found, assume there is no header */
171  if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
172  avio_seek(s->pb, -4, SEEK_CUR);
173  return 0;
174  }
175 
176  /* process metadata blocks */
177  while (!url_feof(s->pb) && !metadata_last) {
178  avio_read(s->pb, header, 4);
179  avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
180  &metadata_size);
181  switch (metadata_type) {
182  /* allocate and read metadata block for supported types */
187  buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
188  if (!buffer) {
189  return AVERROR(ENOMEM);
190  }
191  if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
192  RETURN_ERROR(AVERROR(EIO));
193  }
194  break;
195  /* skip metadata block for unsupported types */
196  default:
197  ret = avio_skip(s->pb, metadata_size);
198  if (ret < 0)
199  return ret;
200  }
201 
202  if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
203  FLACStreaminfo si;
204  /* STREAMINFO can only occur once */
205  if (found_streaminfo) {
207  }
208  if (metadata_size != FLAC_STREAMINFO_SIZE) {
210  }
211  found_streaminfo = 1;
212  st->codec->extradata = buffer;
213  st->codec->extradata_size = metadata_size;
214  buffer = NULL;
215 
216  /* get codec params from STREAMINFO header */
218 
219  /* set time base and duration */
220  if (si.samplerate > 0) {
221  avpriv_set_pts_info(st, 64, 1, si.samplerate);
222  if (si.samples > 0)
223  st->duration = si.samples;
224  }
225  } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
226  uint8_t isrc[13];
227  uint64_t start;
228  const uint8_t *offset;
229  int i, chapters, track, ti;
230  if (metadata_size < 431)
232  offset = buffer + 395;
233  chapters = bytestream_get_byte(&offset) - 1;
234  if (chapters <= 0)
236  for (i = 0; i < chapters; i++) {
237  if (offset + 36 - buffer > metadata_size)
239  start = bytestream_get_be64(&offset);
240  track = bytestream_get_byte(&offset);
241  bytestream_get_buffer(&offset, isrc, 12);
242  isrc[12] = 0;
243  offset += 14;
244  ti = bytestream_get_byte(&offset);
245  if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
246  offset += ti * 12;
247  avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
248  }
249  av_freep(&buffer);
250  } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
251  ret = parse_picture(s, buffer, metadata_size);
252  av_freep(&buffer);
253  if (ret < 0) {
254  av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
255  return ret;
256  }
257  } else {
258  /* STREAMINFO must be the first block */
259  if (!found_streaminfo) {
261  }
262  /* process supported blocks other than STREAMINFO */
263  if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
264  if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
265  av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
266  }
267  }
268  av_freep(&buffer);
269  }
270  }
271 
272  return 0;
273 
274 fail:
275  av_free(buffer);
276  return ret;
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_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
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:236
enum AVCodecID id
Definition: mxfenc.c:89
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.
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
int index
stream index in AVFormatContext
Definition: avformat.h:644
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define FF_ARRAY_ELEMS(a)
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:198
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Format I/O context.
Definition: avformat.h:944
uint8_t
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:610
enum AVStreamParseType need_parsing
Definition: avformat.h:811
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
enum AVCodecID id
int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size)
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:586
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
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:109
AVCodecID
Identify the syntax and semantics of the bitstream.
AVDictionary * metadata
Definition: avformat.h:1092
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
AVInputFormat ff_flac_demuxer
#define AV_EF_EXPLODE
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Spectrum Plot time data
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, const uint8_t *buffer)
Parse the Streaminfo metadata block.
Definition: flac.c:204
static int flac_probe(AVProbeData *p)
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
const char * ff_id3v2_picture_types[21]
Definition: id3v2.c:103
int flags
A combination of AV_PKT_FLAG values.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:127
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
#define FFMIN(a, b)
Definition: common.h:58
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() and chilren.
Definition: dict.h:72
ret
Definition: avfilter.c:821
int width
picture width / height.
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:33
AVDictionary * metadata
Definition: avformat.h:711
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:328
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:627
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
static int width
Definition: tests/utils.c:158
enum AVMediaType codec_type
enum AVCodecID codec_id
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
AVIOContext * pb
I/O context.
Definition: avformat.h:977
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
static int flac_read_header(AVFormatContext *s)
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:62
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
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:73
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:353
synthesis window for stochastic i
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define type
track
Definition: brsc.py:18
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
A reference to a data buffer.
Definition: buffer.h:81
Main libavformat public API header.
#define RETURN_ERROR(code)
int error_recognition
Error recognition; higher values will detect more errors but may misdetect some more or less valid pa...
Definition: avformat.h:1114
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:700
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:56
static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
int len
void INT64 start
Definition: avisynth_c.h:594
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:679
#define MKTAG(a, b, c, d)
Definition: common.h:282
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:725
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190