oggparseflac.c 3.08 KB
Newer Older
Måns Rullgård's avatar
Måns Rullgård committed
1 2
/*
 *    Copyright (C) 2005  Matthieu CASTET
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
Måns Rullgård's avatar
Måns Rullgård committed
7 8
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
Måns Rullgård's avatar
Måns Rullgård committed
10
 *
11
 * Libav is distributed in the hope that it will be useful,
Måns Rullgård's avatar
Måns Rullgård committed
12 13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with Libav; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Måns Rullgård's avatar
Måns Rullgård committed
19 20 21
 */

#include <stdlib.h>
22
#include "libavcodec/get_bits.h"
23
#include "libavcodec/flac.h"
Måns Rullgård's avatar
Måns Rullgård committed
24
#include "avformat.h"
25
#include "internal.h"
26
#include "oggdec.h"
Måns Rullgård's avatar
Måns Rullgård committed
27

28
#define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
Måns Rullgård's avatar
Måns Rullgård committed
29 30 31 32

static int
flac_header (AVFormatContext * s, int idx)
{
33 34
    struct ogg *ogg = s->priv_data;
    struct ogg_stream *os = ogg->streams + idx;
Måns Rullgård's avatar
Måns Rullgård committed
35 36 37 38 39 40 41 42
    AVStream *st = s->streams[idx];
    GetBitContext gb;
    int mdt;

    if (os->buf[os->pstart] == 0xff)
        return 0;

    init_get_bits(&gb, os->buf + os->pstart, os->psize*8);
43
    skip_bits1(&gb); /* metadata_last */
Måns Rullgård's avatar
Måns Rullgård committed
44 45
    mdt = get_bits(&gb, 7);

46 47
    if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
        uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
48 49
        uint32_t samplerate;

50
        skip_bits_long(&gb, 4*8); /* "FLAC" */
Måns Rullgård's avatar
Måns Rullgård committed
51 52
        if(get_bits(&gb, 8) != 1) /* unsupported major version */
            return -1;
53 54
        skip_bits_long(&gb, 8 + 16); /* minor version + header count */
        skip_bits_long(&gb, 4*8); /* "fLaC" */
55

Måns Rullgård's avatar
Måns Rullgård committed
56
        /* METADATA_BLOCK_HEADER */
57
        if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE)
Måns Rullgård's avatar
Måns Rullgård committed
58 59
            return -1;

60
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
61
        st->codec->codec_id = AV_CODEC_ID_FLAC;
62
        st->need_parsing = AVSTREAM_PARSE_HEADERS;
Måns Rullgård's avatar
Måns Rullgård committed
63

64
        st->codec->extradata =
Måns Rullgård's avatar
Måns Rullgård committed
65
            av_malloc(FLAC_STREAMINFO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
66
        memcpy(st->codec->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);
67
        st->codec->extradata_size = FLAC_STREAMINFO_SIZE;
68

69 70 71 72 73
        samplerate = AV_RB24(st->codec->extradata + 10) >> 4;
        if (!samplerate)
            return AVERROR_INVALIDDATA;

        avpriv_set_pts_info(st, 64, 1, samplerate);
74
    } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
75
        ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 4, os->psize - 4);
Måns Rullgård's avatar
Måns Rullgård committed
76 77 78 79 80
    }

    return 1;
}

81 82 83 84
static int
old_flac_header (AVFormatContext * s, int idx)
{
    AVStream *st = s->streams[idx];
85
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
86
    st->codec->codec_id = AV_CODEC_ID_FLAC;
87 88 89 90

    return 0;
}

91
const struct ogg_codec ff_flac_codec = {
Måns Rullgård's avatar
Måns Rullgård committed
92 93
    .magic = "\177FLAC",
    .magicsize = 5,
94 95
    .header = flac_header,
    .nb_header = 2,
Måns Rullgård's avatar
Måns Rullgård committed
96
};
97

98
const struct ogg_codec ff_old_flac_codec = {
99 100
    .magic = "fLaC",
    .magicsize = 4,
101 102
    .header = old_flac_header,
    .nb_header = 0,
103
};