oggparseflac.c 2.93 KB
Newer Older
Måns Rullgård's avatar
Måns Rullgård committed
1 2
/*
 *    Copyright (C) 2005  Matthieu CASTET
3
 *
4 5 6
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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 "oggdec.h"
Måns Rullgård's avatar
Måns Rullgård committed
26

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

static int
flac_header (AVFormatContext * s, int idx)
{
32 33
    struct ogg *ogg = s->priv_data;
    struct ogg_stream *os = ogg->streams + idx;
Måns Rullgård's avatar
Måns Rullgård committed
34 35
    AVStream *st = s->streams[idx];
    GetBitContext gb;
36
    FLACStreaminfo si;
Måns Rullgård's avatar
Måns Rullgård committed
37 38 39 40 41 42
    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
        skip_bits_long(&gb, 4*8); /* "FLAC" */
Måns Rullgård's avatar
Måns Rullgård committed
49 50
        if(get_bits(&gb, 8) != 1) /* unsupported major version */
            return -1;
51 52
        skip_bits_long(&gb, 8 + 16); /* minor version + header count */
        skip_bits_long(&gb, 4*8); /* "fLaC" */
53

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

58
        ff_flac_parse_streaminfo(st->codec, &si, streaminfo_start);
59

60 61
        st->codec->codec_type = CODEC_TYPE_AUDIO;
        st->codec->codec_id = CODEC_ID_FLAC;
Måns Rullgård's avatar
Måns Rullgård committed
62

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

        st->time_base.num = 1;
        st->time_base.den = st->codec->sample_rate;
70
    } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
71
        ff_vorbis_comment (s, &st->metadata, os->buf + os->pstart + 4, os->psize - 4);
Måns Rullgård's avatar
Måns Rullgård committed
72 73 74 75 76
    }

    return 1;
}

77 78 79 80 81 82 83 84 85 86
static int
old_flac_header (AVFormatContext * s, int idx)
{
    AVStream *st = s->streams[idx];
    st->codec->codec_type = CODEC_TYPE_AUDIO;
    st->codec->codec_id = CODEC_ID_FLAC;

    return 0;
}

87
const struct ogg_codec ff_flac_codec = {
Måns Rullgård's avatar
Måns Rullgård committed
88 89 90 91
    .magic = "\177FLAC",
    .magicsize = 5,
    .header = flac_header
};
92

93
const struct ogg_codec ff_old_flac_codec = {
94 95 96 97
    .magic = "fLaC",
    .magicsize = 4,
    .header = old_flac_header
};