]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/flacdec.c
ensure first pts is set, according to specs
[frescor/ffmpeg.git] / libavformat / flacdec.c
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 "raw.h"
25 #include "id3v2.h"
26 #include "oggdec.h"
27
28 static int flac_read_header(AVFormatContext *s,
29                              AVFormatParameters *ap)
30 {
31     uint8_t buf[ID3v2_HEADER_SIZE];
32     int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
33     uint8_t header[4];
34     uint8_t *buffer=NULL;
35     AVStream *st = av_new_stream(s, 0);
36     if (!st)
37         return AVERROR(ENOMEM);
38     st->codec->codec_type = CODEC_TYPE_AUDIO;
39     st->codec->codec_id = CODEC_ID_FLAC;
40     st->need_parsing = AVSTREAM_PARSE_FULL;
41     /* the parameters will be extracted from the compressed bitstream */
42
43     /* skip ID3v2 header if found */
44     ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
45     if (ret == ID3v2_HEADER_SIZE && ff_id3v2_match(buf)) {
46         int len = ff_id3v2_tag_len(buf);
47         url_fseek(s->pb, len - ID3v2_HEADER_SIZE, SEEK_CUR);
48     } else {
49         url_fseek(s->pb, 0, SEEK_SET);
50     }
51
52     /* if fLaC marker is not found, assume there is no header */
53     if (get_le32(s->pb) != MKTAG('f','L','a','C')) {
54         url_fseek(s->pb, -4, SEEK_CUR);
55         return 0;
56     }
57
58     /* process metadata blocks */
59     while (!url_feof(s->pb) && !metadata_last) {
60         get_buffer(s->pb, header, 4);
61         ff_flac_parse_block_header(header, &metadata_last, &metadata_type,
62                                    &metadata_size);
63         switch (metadata_type) {
64         /* allocate and read metadata block for supported types */
65         case FLAC_METADATA_TYPE_STREAMINFO:
66         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
67             buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
68             if (!buffer) {
69                 return AVERROR_NOMEM;
70             }
71             if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
72                 av_freep(&buffer);
73                 return AVERROR_IO;
74             }
75             break;
76         /* skip metadata block for unsupported types */
77         default:
78             ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
79             if (ret < 0)
80                 return ret;
81         }
82
83         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
84             FLACStreaminfo si;
85             /* STREAMINFO can only occur once */
86             if (found_streaminfo) {
87                 av_freep(&buffer);
88                 return AVERROR_INVALIDDATA;
89             }
90             if (metadata_size != FLAC_STREAMINFO_SIZE) {
91                 av_freep(&buffer);
92                 return AVERROR_INVALIDDATA;
93             }
94             found_streaminfo = 1;
95             st->codec->extradata      = buffer;
96             st->codec->extradata_size = metadata_size;
97             buffer = NULL;
98
99             /* get codec params from STREAMINFO header */
100             ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
101
102             /* set time base and duration */
103             if (si.samplerate > 0) {
104                 av_set_pts_info(st, 64, 1, si.samplerate);
105                 if (si.samples > 0)
106                     st->duration = si.samples;
107             }
108         } else {
109             /* STREAMINFO must be the first block */
110             if (!found_streaminfo) {
111                 av_freep(&buffer);
112                 return AVERROR_INVALIDDATA;
113             }
114             /* process supported blocks other than STREAMINFO */
115             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
116                 if (vorbis_comment(s, buffer, metadata_size)) {
117                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
118                 }
119             }
120             av_freep(&buffer);
121         }
122     }
123
124     return 0;
125 }
126
127 static int flac_probe(AVProbeData *p)
128 {
129     uint8_t *bufptr = p->buf;
130     uint8_t *end    = p->buf + p->buf_size;
131
132     if(ff_id3v2_match(bufptr))
133         bufptr += ff_id3v2_tag_len(bufptr);
134
135     if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
136     else                                            return AVPROBE_SCORE_MAX/2;
137 }
138
139 AVInputFormat flac_demuxer = {
140     "flac",
141     NULL_IF_CONFIG_SMALL("raw FLAC"),
142     0,
143     flac_probe,
144     flac_read_header,
145     ff_raw_read_partial_packet,
146     .flags= AVFMT_GENERIC_INDEX,
147     .extensions = "flac",
148     .value = CODEC_ID_FLAC,
149 };