oggparseflac.c 3.56 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 "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
    AVStream *st = s->streams[idx];
    GetBitContext gb;
37
    FLACStreaminfo si;
Måns Rullgård's avatar
Måns Rullgård committed
38 39 40 41 42 43
    int mdt;

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

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

47 48
    if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
        uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
49
        skip_bits_long(&gb, 4*8); /* "FLAC" */
Måns Rullgård's avatar
Måns Rullgård committed
50 51
        if(get_bits(&gb, 8) != 1) /* unsupported major version */
            return -1;
52 53
        skip_bits_long(&gb, 8 + 16); /* minor version + header count */
        skip_bits_long(&gb, 4*8); /* "fLaC" */
54

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

59
        avpriv_flac_parse_streaminfo(st->codec, &si, streaminfo_start);
60

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

65 66
        if (ff_alloc_extradata(st->codec, FLAC_STREAMINFO_SIZE) < 0)
            return AVERROR(ENOMEM);
67
        memcpy(st->codec->extradata, streaminfo_start, st->codec->extradata_size);
68

69
        avpriv_set_pts_info(st, 64, 1, 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
static int
old_flac_header (AVFormatContext * s, int idx)
{
80
    struct ogg *ogg = s->priv_data;
81
    AVStream *st = s->streams[idx];
82 83 84 85 86 87 88 89
    struct ogg_stream *os = ogg->streams + idx;
    AVCodecParserContext *parser = av_parser_init(AV_CODEC_ID_FLAC);
    int size;
    uint8_t *data;

    if (!parser)
        return -1;

90
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
91
    st->codec->codec_id = AV_CODEC_ID_FLAC;
92

93 94 95 96 97 98 99 100 101 102 103 104 105
    parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
    av_parser_parse2(parser, st->codec,
                     &data, &size, os->buf + os->pstart, os->psize,
                     AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);

    av_parser_close(parser);

    if (st->codec->sample_rate) {
        avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
        return 0;
    }

    return 1;
106 107
}

108
const struct ogg_codec ff_flac_codec = {
Måns Rullgård's avatar
Måns Rullgård committed
109 110
    .magic = "\177FLAC",
    .magicsize = 5,
111 112
    .header = flac_header,
    .nb_header = 2,
Måns Rullgård's avatar
Måns Rullgård committed
113
};
114

115
const struct ogg_codec ff_old_flac_codec = {
116 117
    .magic = "fLaC",
    .magicsize = 4,
118 119
    .header = old_flac_header,
    .nb_header = 0,
120
};