83758b2be09e9b34b218a008a6a2724fb072b157
[libav.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 "avformat.h"
23 #include "raw.h"
24 #include "id3v2.h"
25
26 static int flac_read_header(AVFormatContext *s,
27 AVFormatParameters *ap)
28 {
29 uint8_t buf[ID3v2_HEADER_SIZE];
30 int ret;
31 AVStream *st = av_new_stream(s, 0);
32 if (!st)
33 return AVERROR(ENOMEM);
34 st->codec->codec_type = CODEC_TYPE_AUDIO;
35 st->codec->codec_id = CODEC_ID_FLAC;
36 st->need_parsing = AVSTREAM_PARSE_FULL;
37 /* the parameters will be extracted from the compressed bitstream */
38
39 /* skip ID3v2 header if found */
40 ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
41 if (ret == ID3v2_HEADER_SIZE && ff_id3v2_match(buf)) {
42 int len = ff_id3v2_tag_len(buf);
43 url_fseek(s->pb, len - ID3v2_HEADER_SIZE, SEEK_CUR);
44 } else {
45 url_fseek(s->pb, 0, SEEK_SET);
46 }
47 return 0;
48 }
49
50 static int flac_probe(AVProbeData *p)
51 {
52 uint8_t *bufptr = p->buf;
53 uint8_t *end = p->buf + p->buf_size;
54
55 if(ff_id3v2_match(bufptr))
56 bufptr += ff_id3v2_tag_len(bufptr);
57
58 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
59 else return AVPROBE_SCORE_MAX/2;
60 }
61
62 AVInputFormat flac_demuxer = {
63 "flac",
64 NULL_IF_CONFIG_SMALL("raw FLAC"),
65 0,
66 flac_probe,
67 flac_read_header,
68 ff_raw_read_partial_packet,
69 .flags= AVFMT_GENERIC_INDEX,
70 .extensions = "flac",
71 .value = CODEC_ID_FLAC,
72 };