FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "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 (!avio_feof(s->pb) && !metadata_last) {
52  if (avio_read(s->pb, header, 4) != 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 + AV_INPUT_BUFFER_PADDING_SIZE);
63  if (!buffer) {
64  return AVERROR(ENOMEM);
65  }
66  if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
67  RETURN_ERROR(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) {
84  }
85  if (metadata_size != FLAC_STREAMINFO_SIZE) {
87  }
88  found_streaminfo = 1;
89  st->codec->extradata = buffer;
90  st->codec->extradata_size = metadata_size;
91  buffer = NULL;
92 
93  /* get sample rate and sample count from STREAMINFO header;
94  * other parameters will be extracted by the parser */
95  samplerate = AV_RB24(st->codec->extradata + 10) >> 4;
96  samples = (AV_RB64(st->codec->extradata + 13) >> 24) & ((1ULL << 36) - 1);
97 
98  /* set time base and duration */
99  if (samplerate > 0) {
100  avpriv_set_pts_info(st, 64, 1, samplerate);
101  if (samples > 0)
102  st->duration = samples;
103  }
104  } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
105  uint8_t isrc[13];
106  uint64_t start;
107  const uint8_t *offset;
108  int i, chapters, track, ti;
109  if (metadata_size < 431)
111  offset = buffer + 395;
112  chapters = bytestream_get_byte(&offset) - 1;
113  if (chapters <= 0)
115  for (i = 0; i < chapters; i++) {
116  if (offset + 36 - buffer > metadata_size)
118  start = bytestream_get_be64(&offset);
119  track = bytestream_get_byte(&offset);
120  bytestream_get_buffer(&offset, isrc, 12);
121  isrc[12] = 0;
122  offset += 14;
123  ti = bytestream_get_byte(&offset);
124  if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
125  offset += ti * 12;
126  avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
127  }
128  av_freep(&buffer);
129  } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
130  ret = ff_flac_parse_picture(s, buffer, metadata_size);
131  av_freep(&buffer);
132  if (ret < 0) {
133  av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
134  return ret;
135  }
136  } else {
137  /* STREAMINFO must be the first block */
138  if (!found_streaminfo) {
140  }
141  /* process supported blocks other than STREAMINFO */
142  if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
143  AVDictionaryEntry *chmask;
144 
145  ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
146  if (ret < 0) {
147  av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
148  } else if (ret > 0) {
150  }
151 
152  /* parse the channels mask if present */
153  chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
154  if (chmask) {
155  uint64_t mask = strtol(chmask->value, NULL, 0);
156  if (!mask || mask & ~0x3ffffULL) {
158  "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
159  } else {
160  st->codec->channel_layout = mask;
161  av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
162  }
163  }
164  }
165  av_freep(&buffer);
166  }
167  }
168 
169  ret = ff_replaygain_export(st, s->metadata);
170  if (ret < 0)
171  return ret;
172 
173  return 0;
174 
175 fail:
176  av_free(buffer);
177  return ret;
178 }
179 
181 {
182  if ((p->buf[2] & 0xF0) == 0) // blocksize code invalid
183  return 0;
184  if ((p->buf[2] & 0x0F) == 0x0F) // sample rate code invalid
185  return 0;
186  if ((p->buf[3] & 0xF0) >= FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE << 4)
187  // channel mode invalid
188  return 0;
189  if ((p->buf[3] & 0x06) == 0x06) // bits per sample code invalid
190  return 0;
191  if ((p->buf[3] & 0x01) == 0x01) // reserved bit set
192  return 0;
193  return AVPROBE_SCORE_EXTENSION / 4 + 1;
194 }
195 
196 static int flac_probe(AVProbeData *p)
197 {
198  if ((AV_RB16(p->buf) & 0xFFFE) == 0xFFF8)
199  return raw_flac_probe(p);
200  if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
201  return 0;
203 }
204 
205 static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
206  int64_t *ppos, int64_t pos_limit)
207 {
208  AVPacket pkt, out_pkt;
209  AVStream *st = s->streams[stream_index];
210  AVCodecParserContext *parser;
211  int ret;
212  int64_t pts = AV_NOPTS_VALUE;
213 
214  if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
215  return AV_NOPTS_VALUE;
216 
217  av_init_packet(&pkt);
218  parser = av_parser_init(st->codec->codec_id);
219  if (!parser){
220  return AV_NOPTS_VALUE;
221  }
222  parser->flags |= PARSER_FLAG_USE_CODEC_TS;
223 
224  for (;;){
225  ret = ff_raw_read_partial_packet(s, &pkt);
226  if (ret < 0){
227  if (ret == AVERROR(EAGAIN))
228  continue;
229  else
230  break;
231  }
232  av_init_packet(&out_pkt);
233  ret = av_parser_parse2(parser, st->codec,
234  &out_pkt.data, &out_pkt.size, pkt.data, pkt.size,
235  pkt.pts, pkt.dts, *ppos);
236 
237  av_free_packet(&pkt);
238  if (out_pkt.size){
239  int size = out_pkt.size;
240  if (parser->pts != AV_NOPTS_VALUE){
241  // seeking may not have started from beginning of a frame
242  // calculate frame start position from next frame backwards
243  *ppos = parser->next_frame_offset - size;
244  pts = parser->pts;
245  break;
246  }
247  }
248  }
249  av_parser_close(parser);
250  return pts;
251 }
252 
254  .name = "flac",
255  .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
256  .read_probe = flac_probe,
257  .read_header = flac_read_header,
258  .read_packet = ff_raw_read_partial_packet,
259  .read_timestamp = flac_read_timestamp,
260  .flags = AVFMT_GENERIC_INDEX,
261  .extensions = "flac",
262  .raw_codec_id = AV_CODEC_ID_FLAC,
263 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: flacdec.c:205
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:284
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#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:4093
int64_t next_frame_offset
Definition: avcodec.h:4555
int size
Definition: avcodec.h:1434
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:204
#define AV_RB24
Definition: intreadwrite.h:64
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1572
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:279
static AVPacket pkt
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:3855
Format I/O context.
Definition: avformat.h:1285
uint8_t
enum AVStreamParseType need_parsing
Definition: avformat.h:1046
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1627
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3756
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1353
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:39
uint8_t * data
Definition: avcodec.h:1433
int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size, int parse_picture)
ptrdiff_t size
Definition: opengl_enc.c:101
static const uint8_t header[24]
Definition: sdr2.c:67
int ff_replaygain_export(AVStream *st, AVDictionary *metadata)
Parse replaygain tags and export them as per-stream side data.
Definition: replaygain.c:91
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:790
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:542
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1497
AVInputFormat ff_flac_demuxer
Definition: flacdec.c:253
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:663
#define AVERROR(e)
Definition: error.h:43
int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawdec.c:35
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:4587
static int flac_probe(AVProbeData *p)
Definition: flacdec.c:196
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define fail()
Definition: checkasm.h:57
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2333
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:463
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:462
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
Definition: parser.c:131
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:221
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1573
#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:28
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:359
Stream structure.
Definition: avformat.h:854
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1520
enum AVCodecID codec_id
Definition: avcodec.h:1529
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
int extradata_size
Definition: avcodec.h:1628
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:69
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:485
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
#define RETURN_ERROR(code)
Definition: flac_picture.h:27
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:470
This structure contains the data a format has to probe a file.
Definition: avformat.h:460
static int64_t pts
Global timestamp for the audio frames.
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:913
Main libavformat public API header.
static int raw_flac_probe(AVProbeData *p)
Definition: flacdec.c:180
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:49
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:636
#define av_free(p)
char * value
Definition: dict.h:88
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1432
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:640
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:303
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:896
#define MKTAG(a, b, c, d)
Definition: common.h:341
This structure stores compressed data.
Definition: avcodec.h:1410
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:252
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
GLuint buffer
Definition: opengl_enc.c:102
#define FLAC_MAX_CHANNELS
Definition: flac.h:35
#define av_unused
Definition: attributes.h:118