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 #include "libavcodec/bytestream.h"
31 
33 {
34  int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
35  uint8_t header[4];
38  if (!st)
39  return AVERROR(ENOMEM);
43  /* the parameters will be extracted from the compressed bitstream */
44 
45  /* if fLaC marker is not found, assume there is no header */
46  if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
47  avio_seek(s->pb, -4, SEEK_CUR);
48  return 0;
49  }
50 
51  /* process metadata blocks */
52  while (!s->pb->eof_reached && !metadata_last) {
53  avio_read(s->pb, header, 4);
54  flac_parse_block_header(header, &metadata_last, &metadata_type,
55  &metadata_size);
56  switch (metadata_type) {
57  /* allocate and read metadata block for supported types */
62  buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
63  if (!buffer) {
64  return AVERROR(ENOMEM);
65  }
66  if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
67  av_freep(&buffer);
68  return AVERROR(EIO);
69  }
70  break;
71  /* skip metadata block for unsupported types */
72  default:
73  ret = avio_skip(s->pb, metadata_size);
74  if (ret < 0)
75  return ret;
76  }
77 
78  if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
79  FLACStreaminfo si;
80  /* STREAMINFO can only occur once */
81  if (found_streaminfo) {
82  av_freep(&buffer);
83  return AVERROR_INVALIDDATA;
84  }
85  if (metadata_size != FLAC_STREAMINFO_SIZE) {
86  av_freep(&buffer);
87  return AVERROR_INVALIDDATA;
88  }
89  found_streaminfo = 1;
90  st->codec->extradata = buffer;
91  st->codec->extradata_size = metadata_size;
92  buffer = NULL;
93 
94  /* get codec params from STREAMINFO header */
96 
97  /* set time base and duration */
98  if (si.samplerate > 0) {
99  avpriv_set_pts_info(st, 64, 1, si.samplerate);
100  if (si.samples > 0)
101  st->duration = si.samples;
102  }
103  } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
104  uint8_t isrc[13];
105  uint64_t start;
106  const uint8_t *offset;
107  int i, chapters, track, ti;
108  if (metadata_size < 431)
109  return AVERROR_INVALIDDATA;
110  offset = buffer + 395;
111  chapters = bytestream_get_byte(&offset) - 1;
112  if (chapters <= 0)
113  return AVERROR_INVALIDDATA;
114  for (i = 0; i < chapters; i++) {
115  if (offset + 36 - buffer > metadata_size)
116  return AVERROR_INVALIDDATA;
117  start = bytestream_get_be64(&offset);
118  track = bytestream_get_byte(&offset);
119  bytestream_get_buffer(&offset, isrc, 12);
120  isrc[12] = 0;
121  offset += 14;
122  ti = bytestream_get_byte(&offset);
123  if (ti <= 0) return AVERROR_INVALIDDATA;
124  offset += ti * 12;
125  avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
126  }
127  } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
128  ret = ff_flac_parse_picture(s, buffer, metadata_size);
129  av_freep(&buffer);
130  if (ret < 0) {
131  av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
132  return ret;
133  }
134  } else {
135  /* STREAMINFO must be the first block */
136  if (!found_streaminfo) {
137  av_freep(&buffer);
138  return AVERROR_INVALIDDATA;
139  }
140  /* process supported blocks other than STREAMINFO */
141  if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
142  AVDictionaryEntry *chmask;
143 
144  ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
145  if (ret < 0) {
146  av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
147  } else if (ret > 0) {
149  }
150 
151  /* parse the channels mask if present */
152  chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
153  if (chmask) {
154  uint64_t mask = strtol(chmask->value, NULL, 0);
155  if (!mask || mask & ~0x3ffffULL) {
157  "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
158  } else {
159  st->codec->channel_layout = mask;
160  av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
161  }
162  }
163  }
164  av_freep(&buffer);
165  }
166  }
167 
168  ret = ff_replaygain_export(st, s->metadata);
169  if (ret < 0)
170  return ret;
171 
172  return 0;
173 }
174 
175 static int flac_probe(AVProbeData *p)
176 {
177  if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
178  return 0;
180 }
181 
183  .name = "flac",
184  .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
185  .read_probe = flac_probe,
186  .read_header = flac_read_header,
187  .read_packet = ff_raw_read_partial_packet,
188  .flags = AVFMT_GENERIC_INDEX,
189  .extensions = "flac",
190  .raw_codec_id = AV_CODEC_ID_FLAC,
191 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:2829
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1200
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.
Definition: utils.c:2601
Format I/O context.
Definition: avformat.h:922
uint8_t
enum AVStreamParseType need_parsing
Definition: avformat.h:867
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
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:110
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
AVInputFormat ff_flac_demuxer
Definition: flacdec.c:182
static const uint16_t mask[17]
Definition: lzw.c:38
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#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:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
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)
Definition: flacdec.c:175
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:398
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1201
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:34
static char buffer[20]
Definition: seek-test.c:31
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:354
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int extradata_size
Definition: avcodec.h:1165
static int flac_read_header(AVFormatContext *s)
Definition: flacdec.c:32
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:417
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:143
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:402
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:756
full parsing and repack
Definition: avformat.h:653
Main libavformat public API header.
char * value
Definition: dict.h:76
int eof_reached
true if eof reached
Definition: avio.h:96
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
#define MKTAG(a, b, c, d)
Definition: common.h:238
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
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228