]> git.sesse.net Git - ffmpeg/blob - libavformat/oggparseflac.c
crypto: consistently use size_t as type for length parameters
[ffmpeg] / libavformat / oggparseflac.c
1 /*
2  *    Copyright (C) 2005  Matthieu CASTET
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdlib.h>
22
23 #include "libavcodec/bitstream.h"
24 #include "libavcodec/flac.h"
25
26 #include "avformat.h"
27 #include "internal.h"
28 #include "oggdec.h"
29
30 #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
31
32 static int
33 flac_header (AVFormatContext * s, int idx)
34 {
35     struct ogg *ogg = s->priv_data;
36     struct ogg_stream *os = ogg->streams + idx;
37     AVStream *st = s->streams[idx];
38     BitstreamContext bc;
39     int mdt;
40
41     if (os->buf[os->pstart] == 0xff)
42         return 0;
43
44     bitstream_init(&bc, os->buf + os->pstart, os->psize * 8);
45     bitstream_skip(&bc, 1); /* metadata_last */
46     mdt = bitstream_read(&bc, 7);
47
48     if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
49         uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
50         uint32_t samplerate;
51
52         bitstream_skip(&bc, 4 * 8); /* "FLAC" */
53         if (bitstream_read(&bc, 8) != 1) /* unsupported major version */
54             return -1;
55         bitstream_skip(&bc, 8 + 16); /* minor version + header count */
56         bitstream_skip(&bc, 4 * 8); /* "fLaC" */
57
58         /* METADATA_BLOCK_HEADER */
59         if (bitstream_read(&bc, 32) != FLAC_STREAMINFO_SIZE)
60             return -1;
61
62         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
63         st->codecpar->codec_id = AV_CODEC_ID_FLAC;
64         st->need_parsing = AVSTREAM_PARSE_HEADERS;
65
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;
70
71         samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
72         if (!samplerate)
73             return AVERROR_INVALIDDATA;
74
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);
78     }
79
80     return 1;
81 }
82
83 static int
84 old_flac_header (AVFormatContext * s, int idx)
85 {
86     AVStream *st = s->streams[idx];
87     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
88     st->codecpar->codec_id = AV_CODEC_ID_FLAC;
89
90     return 0;
91 }
92
93 const struct ogg_codec ff_flac_codec = {
94     .magic = "\177FLAC",
95     .magicsize = 5,
96     .header = flac_header,
97     .nb_header = 2,
98 };
99
100 const struct ogg_codec ff_old_flac_codec = {
101     .magic = "fLaC",
102     .magicsize = 4,
103     .header = old_flac_header,
104     .nb_header = 0,
105 };