rtsp: Move the pollfd setup out of the for loop
[libav.git] / libavformat / oggparseflac.c
CommitLineData
bcfc40ae
MR
1/*
2 * Copyright (C) 2005 Matthieu CASTET
115329f1 3 *
2912e87a 4 * This file is part of Libav.
b78e7197 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
bcfc40ae
MR
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
b78e7197 9 * version 2.1 of the License, or (at your option) any later version.
bcfc40ae 10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
bcfc40ae
MR
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
5509bffa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bcfc40ae
MR
19 */
20
21#include <stdlib.h>
2cef81a8
AH
22
23#include "libavcodec/bitstream.h"
7fa9a0a2 24#include "libavcodec/flac.h"
2cef81a8 25
bcfc40ae 26#include "avformat.h"
c3f9ebf7 27#include "internal.h"
a0ddef24 28#include "oggdec.h"
bcfc40ae 29
7fa9a0a2 30#define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
bcfc40ae
MR
31
32static int
33flac_header (AVFormatContext * s, int idx)
34{
77be08ee
MR
35 struct ogg *ogg = s->priv_data;
36 struct ogg_stream *os = ogg->streams + idx;
bcfc40ae 37 AVStream *st = s->streams[idx];
2cef81a8 38 BitstreamContext bc;
bcfc40ae
MR
39 int mdt;
40
41 if (os->buf[os->pstart] == 0xff)
42 return 0;
43
a25dac97 44 bitstream_init8(&bc, os->buf + os->pstart, os->psize);
2cef81a8
AH
45 bitstream_skip(&bc, 1); /* metadata_last */
46 mdt = bitstream_read(&bc, 7);
bcfc40ae 47
7fa9a0a2
JR
48 if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
49 uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
7784f477
AK
50 uint32_t samplerate;
51
2cef81a8
AH
52 bitstream_skip(&bc, 4 * 8); /* "FLAC" */
53 if (bitstream_read(&bc, 8) != 1) /* unsupported major version */
bcfc40ae 54 return -1;
2cef81a8
AH
55 bitstream_skip(&bc, 8 + 16); /* minor version + header count */
56 bitstream_skip(&bc, 4 * 8); /* "fLaC" */
115329f1 57
bcfc40ae 58 /* METADATA_BLOCK_HEADER */
2cef81a8 59 if (bitstream_read(&bc, 32) != FLAC_STREAMINFO_SIZE)
bcfc40ae
MR
60 return -1;
61
9200514a
AK
62 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
63 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
eed691f7 64 st->need_parsing = AVSTREAM_PARSE_HEADERS;
bcfc40ae 65
9200514a 66 st->codecpar->extradata =
059a9348 67 av_malloc(FLAC_STREAMINFO_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
9200514a
AK
68 memcpy(st->codecpar->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);
69 st->codecpar->extradata_size = FLAC_STREAMINFO_SIZE;
3644cb8f 70
9200514a 71 samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
7784f477
AK
72 if (!samplerate)
73 return AVERROR_INVALIDDATA;
74
75 avpriv_set_pts_info(st, 64, 1, samplerate);
7fa9a0a2 76 } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
db68ef89 77 ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 4, os->psize - 4);
bcfc40ae
MR
78 }
79
80 return 1;
81}
82
880e3ef4
MN
83static int
84old_flac_header (AVFormatContext * s, int idx)
85{
86 AVStream *st = s->streams[idx];
9200514a
AK
87 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
88 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
880e3ef4
MN
89
90 return 0;
91}
92
77be08ee 93const struct ogg_codec ff_flac_codec = {
bcfc40ae
MR
94 .magic = "\177FLAC",
95 .magicsize = 5,
7751e469
LB
96 .header = flac_header,
97 .nb_header = 2,
bcfc40ae 98};
880e3ef4 99
77be08ee 100const struct ogg_codec ff_old_flac_codec = {
880e3ef4
MN
101 .magic = "fLaC",
102 .magicsize = 4,
7751e469
LB
103 .header = old_flac_header,
104 .nb_header = 0,
880e3ef4 105};