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 "replaygain.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  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 + AV_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  uint32_t samplerate;
79  uint64_t samples;
80 
81  /* STREAMINFO can only occur once */
82  if (found_streaminfo) {
83  av_freep(&buffer);
84  return AVERROR_INVALIDDATA;
85  }
86  if (metadata_size != FLAC_STREAMINFO_SIZE) {
87  av_freep(&buffer);
88  return AVERROR_INVALIDDATA;
89  }
90  found_streaminfo = 1;
91  st->codecpar->extradata = buffer;
92  st->codecpar->extradata_size = metadata_size;
93  buffer = NULL;
94 
95  /* get sample rate and sample count from STREAMINFO header;
96  * other parameters will be extracted by the parser */
97  samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
98  samples = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
99 
100  /* set time base and duration */
101  if (samplerate > 0) {
102  avpriv_set_pts_info(st, 64, 1, samplerate);
103  if (samples > 0)
104  st->duration = samples;
105  }
106  } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
107  uint8_t isrc[13];
108  uint64_t start;
109  const uint8_t *offset;
110  int i, chapters, track, ti;
111  if (metadata_size < 431)
112  return AVERROR_INVALIDDATA;
113  offset = buffer + 395;
114  chapters = bytestream_get_byte(&offset) - 1;
115  if (chapters <= 0)
116  return AVERROR_INVALIDDATA;
117  for (i = 0; i < chapters; i++) {
118  if (offset + 36 - buffer > metadata_size)
119  return AVERROR_INVALIDDATA;
120  start = bytestream_get_be64(&offset);
121  track = bytestream_get_byte(&offset);
122  bytestream_get_buffer(&offset, isrc, 12);
123  isrc[12] = 0;
124  offset += 14;
125  ti = bytestream_get_byte(&offset);
126  if (ti <= 0) return AVERROR_INVALIDDATA;
127  offset += ti * 12;
128  avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
129  }
130  } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
131  ret = ff_flac_parse_picture(s, buffer, metadata_size);
132  av_freep(&buffer);
133  if (ret < 0) {
134  av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
135  return ret;
136  }
137  } else {
138  /* STREAMINFO must be the first block */
139  if (!found_streaminfo) {
140  av_freep(&buffer);
141  return AVERROR_INVALIDDATA;
142  }
143  /* process supported blocks other than STREAMINFO */
144  if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
145  AVDictionaryEntry *chmask;
146 
147  ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
148  if (ret < 0) {
149  av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
150  } else if (ret > 0) {
152  }
153 
154  /* parse the channels mask if present */
155  chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
156  if (chmask) {
157  uint64_t mask = strtol(chmask->value, NULL, 0);
158  if (!mask || mask & ~0x3ffffULL) {
160  "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
161  } else {
163  av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
164  }
165  }
166  }
167  av_freep(&buffer);
168  }
169  }
170 
171  ret = ff_replaygain_export(st, s->metadata);
172  if (ret < 0)
173  return ret;
174 
175  return 0;
176 }
177 
178 static int flac_probe(AVProbeData *p)
179 {
180  /* file header + metadata header + checked bytes of streaminfo */
181  if (p->buf_size >= 4 + 4 + 13) {
182  int type = p->buf[4] & 0x7f;
183  int size = AV_RB24(p->buf + 5);
184  int min_block_size = AV_RB16(p->buf + 8);
185  int max_block_size = AV_RB16(p->buf + 10);
186  int sample_rate = AV_RB24(p->buf + 18) >> 4;
187 
188  if (!memcmp(p->buf, "fLaC", 4) &&
190  size == FLAC_STREAMINFO_SIZE &&
191  min_block_size >= 16 &&
192  max_block_size >= min_block_size &&
193  sample_rate && sample_rate <= 655350)
194  return AVPROBE_SCORE_MAX;
195  }
196 
197  return 0;
198 }
199 
201  .name = "flac",
202  .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
203  .read_probe = flac_probe,
204  .read_header = flac_read_header,
205  .read_packet = ff_raw_read_partial_packet,
206  .flags = AVFMT_GENERIC_INDEX,
207  .extensions = "flac",
208  .raw_codec_id = AV_CODEC_ID_FLAC,
209 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
#define AV_RB64
Definition: intreadwrite.h:164
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:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
#define AV_RB24
Definition: intreadwrite.h:64
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1218
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:202
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:2758
Format I/O context.
Definition: avformat.h:940
static char buffer[20]
Definition: seek.c:32
uint8_t
enum AVStreamParseType need_parsing
Definition: avformat.h:879
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size, int parse_picture)
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:91
uint64_t channel_layout
Audio only.
Definition: avcodec.h:3556
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
AVInputFormat ff_flac_demuxer
Definition: flacdec.c:200
static const uint16_t mask[17]
Definition: lzw.c:38
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
#define AV_RB16
Definition: intreadwrite.h:53
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#define AVERROR(e)
Definition: error.h:43
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:34
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
static int flac_probe(AVProbeData *p)
Definition: flacdec.c:178
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:401
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1219
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
int ff_flac_parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
Definition: flac_picture.c:26
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:356
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
static int flac_read_header(AVFormatContext *s)
Definition: flacdec.c:31
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:68
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:421
static av_always_inline void 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.h:151
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
full parsing and repack
Definition: avformat.h:657
Main libavformat public API header.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
char * value
Definition: dict.h:74
int eof_reached
true if eof reached
Definition: avio.h:132
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
#define MKTAG(a, b, c, d)
Definition: common.h:256
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:211
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235