Statistics
| Branch: | Revision:

ffmpeg / libavformat / flacdec.c @ 66061a12

History | View | Annotate | Download (5.07 KB)

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
#include "vorbiscomment.h"
28

    
29
static int flac_read_header(AVFormatContext *s,
30
                             AVFormatParameters *ap)
31
{
32
    uint8_t buf[ID3v2_HEADER_SIZE];
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 = av_new_stream(s, 0);
37
    if (!st)
38
        return AVERROR(ENOMEM);
39
    st->codec->codec_type = CODEC_TYPE_AUDIO;
40
    st->codec->codec_id = CODEC_ID_FLAC;
41
    st->need_parsing = AVSTREAM_PARSE_FULL;
42
    /* the parameters will be extracted from the compressed bitstream */
43

    
44
    /* skip ID3v2 header if found */
45
    ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
46
    if (ret == ID3v2_HEADER_SIZE && ff_id3v2_match(buf)) {
47
        int len = ff_id3v2_tag_len(buf);
48
        url_fseek(s->pb, len - ID3v2_HEADER_SIZE, SEEK_CUR);
49
    } else {
50
        url_fseek(s->pb, 0, SEEK_SET);
51
    }
52

    
53
    /* if fLaC marker is not found, assume there is no header */
54
    if (get_le32(s->pb) != MKTAG('f','L','a','C')) {
55
        url_fseek(s->pb, -4, SEEK_CUR);
56
        return 0;
57
    }
58

    
59
    /* process metadata blocks */
60
    while (!url_feof(s->pb) && !metadata_last) {
61
        get_buffer(s->pb, header, 4);
62
        ff_flac_parse_block_header(header, &metadata_last, &metadata_type,
63
                                   &metadata_size);
64
        switch (metadata_type) {
65
        /* allocate and read metadata block for supported types */
66
        case FLAC_METADATA_TYPE_STREAMINFO:
67
        case FLAC_METADATA_TYPE_VORBIS_COMMENT:
68
            buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
69
            if (!buffer) {
70
                return AVERROR_NOMEM;
71
            }
72
            if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
73
                av_freep(&buffer);
74
                return AVERROR(EIO);
75
            }
76
            break;
77
        /* skip metadata block for unsupported types */
78
        default:
79
            ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
80
            if (ret < 0)
81
                return ret;
82
        }
83

    
84
        if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
85
            FLACStreaminfo si;
86
            /* STREAMINFO can only occur once */
87
            if (found_streaminfo) {
88
                av_freep(&buffer);
89
                return AVERROR_INVALIDDATA;
90
            }
91
            if (metadata_size != FLAC_STREAMINFO_SIZE) {
92
                av_freep(&buffer);
93
                return AVERROR_INVALIDDATA;
94
            }
95
            found_streaminfo = 1;
96
            st->codec->extradata      = buffer;
97
            st->codec->extradata_size = metadata_size;
98
            buffer = NULL;
99

    
100
            /* get codec params from STREAMINFO header */
101
            ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
102

    
103
            /* set time base and duration */
104
            if (si.samplerate > 0) {
105
                av_set_pts_info(st, 64, 1, si.samplerate);
106
                if (si.samples > 0)
107
                    st->duration = si.samples;
108
            }
109
        } else {
110
            /* STREAMINFO must be the first block */
111
            if (!found_streaminfo) {
112
                av_freep(&buffer);
113
                return AVERROR_INVALIDDATA;
114
            }
115
            /* process supported blocks other than STREAMINFO */
116
            if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
117
                if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
118
                    av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
119
                }
120
            }
121
            av_freep(&buffer);
122
        }
123
    }
124

    
125
    return 0;
126
}
127

    
128
static int flac_probe(AVProbeData *p)
129
{
130
    uint8_t *bufptr = p->buf;
131
    uint8_t *end    = p->buf + p->buf_size;
132

    
133
    if(ff_id3v2_match(bufptr))
134
        bufptr += ff_id3v2_tag_len(bufptr);
135

    
136
    if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
137
    else                                            return AVPROBE_SCORE_MAX/2;
138
}
139

    
140
AVInputFormat flac_demuxer = {
141
    "flac",
142
    NULL_IF_CONFIG_SMALL("raw FLAC"),
143
    0,
144
    flac_probe,
145
    flac_read_header,
146
    ff_raw_read_partial_packet,
147
    .flags= AVFMT_GENERIC_INDEX,
148
    .extensions = "flac",
149
    .value = CODEC_ID_FLAC,
150
    .metadata_conv = ff_vorbiscomment_metadata_conv,
151
};