2 * Copyright (C) 2005 Matthieu CASTET
4 * This file is part of FFmpeg.
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcodec/avcodec.h"
23 #include "libavcodec/bytestream.h"
24 #include "libavcodec/flac.h"
29 #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
32 flac_header (AVFormatContext * s, int idx)
34 struct ogg *ogg = s->priv_data;
35 struct ogg_stream *os = ogg->streams + idx;
36 AVStream *st = s->streams[idx];
40 if (os->buf[os->pstart] == 0xff)
43 bytestream2_init(&gb, os->buf + os->pstart, os->psize);
44 mdt = bytestream2_get_byte(&gb) & 0x7F;
46 if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
49 if (bytestream2_get_bytes_left(&gb) < 4 + 4 + 4 + 4 + FLAC_STREAMINFO_SIZE)
50 return AVERROR_INVALIDDATA;
51 bytestream2_skipu(&gb, 4); /* "FLAC" */
52 if (bytestream2_get_byteu(&gb) != 1) /* unsupported major version */
54 bytestream2_skipu(&gb, 1 + 2); /* minor version + header count */
55 bytestream2_skipu(&gb, 4); /* "fLaC" */
57 /* METADATA_BLOCK_HEADER */
58 if (bytestream2_get_be32u(&gb) != FLAC_STREAMINFO_SIZE)
61 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
62 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
63 ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
65 if ((ret = ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE)) < 0)
67 bytestream2_get_bufferu(&gb, st->codecpar->extradata, FLAC_STREAMINFO_SIZE);
69 samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
71 return AVERROR_INVALIDDATA;
73 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);
82 old_flac_header (AVFormatContext * s, int idx)
84 struct ogg *ogg = s->priv_data;
85 AVStream *st = s->streams[idx];
86 struct ogg_stream *os = ogg->streams + idx;
87 AVCodecParserContext *parser = av_parser_init(AV_CODEC_ID_FLAC);
88 AVCodecContext *avctx;
95 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
96 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
98 avctx = avcodec_alloc_context3(NULL);
100 ret = AVERROR(ENOMEM);
104 ret = avcodec_parameters_to_context(avctx, st->codecpar);
108 parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
109 av_parser_parse2(parser, avctx,
110 &data, &size, os->buf + os->pstart, os->psize,
111 AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1);
113 av_parser_close(parser);
115 if (avctx->sample_rate) {
116 avpriv_set_pts_info(st, 64, 1, avctx->sample_rate);
117 avcodec_free_context(&avctx);
121 avcodec_free_context(&avctx);
124 av_parser_close(parser);
125 avcodec_free_context(&avctx);
129 const struct ogg_codec ff_flac_codec = {
132 .header = flac_header,
136 const struct ogg_codec ff_old_flac_codec = {
139 .header = old_flac_header,