2 * Copyright (C) 2005 Matthieu CASTET
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * 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
23 #include "libavcodec/bitstream.h"
24 #include "libavcodec/flac.h"
30 #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
33 flac_header (AVFormatContext * s, int idx)
35 struct ogg *ogg = s->priv_data;
36 struct ogg_stream *os = ogg->streams + idx;
37 AVStream *st = s->streams[idx];
41 if (os->buf[os->pstart] == 0xff)
44 bitstream_init(&bc, os->buf + os->pstart, os->psize * 8);
45 bitstream_skip(&bc, 1); /* metadata_last */
46 mdt = bitstream_read(&bc, 7);
48 if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
49 uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
52 bitstream_skip(&bc, 4 * 8); /* "FLAC" */
53 if (bitstream_read(&bc, 8) != 1) /* unsupported major version */
55 bitstream_skip(&bc, 8 + 16); /* minor version + header count */
56 bitstream_skip(&bc, 4 * 8); /* "fLaC" */
58 /* METADATA_BLOCK_HEADER */
59 if (bitstream_read(&bc, 32) != FLAC_STREAMINFO_SIZE)
62 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
63 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
64 st->need_parsing = AVSTREAM_PARSE_HEADERS;
66 st->codecpar->extradata =
67 av_malloc(FLAC_STREAMINFO_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
68 memcpy(st->codecpar->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);
69 st->codecpar->extradata_size = FLAC_STREAMINFO_SIZE;
71 samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
73 return AVERROR_INVALIDDATA;
75 avpriv_set_pts_info(st, 64, 1, samplerate);
76 } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
77 ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 4, os->psize - 4);
84 old_flac_header (AVFormatContext * s, int idx)
86 AVStream *st = s->streams[idx];
87 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
88 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
93 const struct ogg_codec ff_flac_codec = {
96 .header = flac_header,
100 const struct ogg_codec ff_old_flac_codec = {
103 .header = old_flac_header,