FFmpeg  2.1.1
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 "libavcodec/bytestream.h"
30 
32 {
33  int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
34  uint8_t header[4];
35  uint8_t *buffer=NULL;
36  AVStream *st = avformat_new_stream(s, NULL);
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 (!url_feof(s->pb) && !metadata_last) {
52  avio_read(s->pb, header, 4);
53  avpriv_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 + FF_INPUT_BUFFER_PADDING_SIZE);
62  if (!buffer) {
63  return AVERROR(ENOMEM);
64  }
65  if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
66  RETURN_ERROR(AVERROR(EIO));
67  }
68  break;
69  /* skip metadata block for unsupported types */
70  default:
71  ret = avio_skip(s->pb, metadata_size);
72  if (ret < 0)
73  return ret;
74  }
75 
76  if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
77  FLACStreaminfo si;
78  /* STREAMINFO can only occur once */
79  if (found_streaminfo) {
81  }
82  if (metadata_size != FLAC_STREAMINFO_SIZE) {
84  }
85  found_streaminfo = 1;
86  st->codec->extradata = buffer;
87  st->codec->extradata_size = metadata_size;
88  buffer = NULL;
89 
90  /* get codec params from STREAMINFO header */
92 
93  /* set time base and duration */
94  if (si.samplerate > 0) {
95  avpriv_set_pts_info(st, 64, 1, si.samplerate);
96  if (si.samples > 0)
97  st->duration = si.samples;
98  }
99  } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
100  uint8_t isrc[13];
101  uint64_t start;
102  const uint8_t *offset;
103  int i, chapters, track, ti;
104  if (metadata_size < 431)
106  offset = buffer + 395;
107  chapters = bytestream_get_byte(&offset) - 1;
108  if (chapters <= 0)
110  for (i = 0; i < chapters; i++) {
111  if (offset + 36 - buffer > metadata_size)
113  start = bytestream_get_be64(&offset);
114  track = bytestream_get_byte(&offset);
115  bytestream_get_buffer(&offset, isrc, 12);
116  isrc[12] = 0;
117  offset += 14;
118  ti = bytestream_get_byte(&offset);
119  if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
120  offset += ti * 12;
121  avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
122  }
123  av_freep(&buffer);
124  } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
125  ret = ff_flac_parse_picture(s, buffer, metadata_size);
126  av_freep(&buffer);
127  if (ret < 0) {
128  av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
129  return ret;
130  }
131  } else {
132  /* STREAMINFO must be the first block */
133  if (!found_streaminfo) {
135  }
136  /* process supported blocks other than STREAMINFO */
137  if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
138  if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
139  av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
140  }
141  }
142  av_freep(&buffer);
143  }
144  }
145 
146  return 0;
147 
148 fail:
149  av_free(buffer);
150  return ret;
151 }
152 
153 static int flac_probe(AVProbeData *p)
154 {
155  if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
156  return 0;
158 }
159 
161  .name = "flac",
162  .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
163  .read_probe = flac_probe,
164  .read_header = flac_read_header,
165  .read_packet = ff_raw_read_partial_packet,
166  .flags = AVFMT_GENERIC_INDEX,
167  .extensions = "flac",
168  .raw_codec_id = AV_CODEC_ID_FLAC,
169 };
const char * s
Definition: avisynth_c.h:668
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
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:478
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
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:3922
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:686
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:593
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1254
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
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:3435
Format I/O context.
Definition: avformat.h:968
uint8_t
static const uint8_t offset[511][2]
Definition: vf_uspp.c:58
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
enum AVStreamParseType need_parsing
Definition: avformat.h:812
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3348
int ff_vorbis_comment(AVFormatContext *ms, AVDictionary **m, const uint8_t *buf, int size)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
AVInputFormat ff_flac_demuxer
Definition: flacdec.c:160
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
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:151
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:153
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:355
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
goto fail
Definition: avfilter.c:963
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
ret
Definition: avfilter.c:961
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:341
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:33
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:28
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:331
Stream structure.
Definition: avformat.h:667
enum AVMediaType codec_type
Definition: avcodec.h:1154
enum AVCodecID codec_id
Definition: avcodec.h:1157
AVIOContext * pb
I/O context.
Definition: avformat.h:1001
int extradata_size
Definition: avcodec.h:1255
static int flac_read_header(AVFormatContext *s)
Definition: flacdec.c:31
#define MKTAG(a, b, c, d)
AVDictionary * metadata
Definition: avformat.h:1128
#define RETURN_ERROR(code)
Definition: flac_picture.h:27
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:720
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:480
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
Main libavformat public API header.
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:603
#define AVERROR_INVALIDDATA
#define AVERROR(e)
void INT64 start
Definition: avisynth_c.h:594
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:703
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avcodec.h:2278
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241