Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavformat / oggparseflac.c
1 /*
2  *    Copyright (C) 2005  Matthieu CASTET
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdlib.h>
22 #include "libavcodec/avcodec.h"
23 #include "libavcodec/bytestream.h"
24 #include "libavcodec/flac.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "oggdec.h"
28
29 #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
30
31 static int
32 flac_header (AVFormatContext * s, int idx)
33 {
34     struct ogg *ogg = s->priv_data;
35     struct ogg_stream *os = ogg->streams + idx;
36     AVStream *st = s->streams[idx];
37     GetByteContext gb;
38     int mdt, ret;
39
40     if (os->buf[os->pstart] == 0xff)
41         return 0;
42
43     bytestream2_init(&gb, os->buf + os->pstart, os->psize);
44     mdt = bytestream2_get_byte(&gb) & 0x7F;
45
46     if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
47         uint32_t samplerate;
48
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 */
53             return -1;
54         bytestream2_skipu(&gb, 1 + 2); /* minor version + header count */
55         bytestream2_skipu(&gb, 4); /* "fLaC" */
56
57         /* METADATA_BLOCK_HEADER */
58         if (bytestream2_get_be32u(&gb) != FLAC_STREAMINFO_SIZE)
59             return -1;
60
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;
64
65         if ((ret = ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE)) < 0)
66             return ret;
67         bytestream2_get_bufferu(&gb, st->codecpar->extradata, FLAC_STREAMINFO_SIZE);
68
69         samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
70         if (!samplerate)
71             return AVERROR_INVALIDDATA;
72
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);
76     }
77
78     return 1;
79 }
80
81 static int
82 old_flac_header (AVFormatContext * s, int idx)
83 {
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;
89     int size, ret;
90     uint8_t *data;
91
92     if (!parser)
93         return -1;
94
95     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
96     st->codecpar->codec_id = AV_CODEC_ID_FLAC;
97
98     avctx = avcodec_alloc_context3(NULL);
99     if (!avctx) {
100         ret = AVERROR(ENOMEM);
101         goto fail;
102     }
103
104     ret = avcodec_parameters_to_context(avctx, st->codecpar);
105     if (ret < 0)
106         goto fail;
107
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);
112
113     av_parser_close(parser);
114
115     if (avctx->sample_rate) {
116         avpriv_set_pts_info(st, 64, 1, avctx->sample_rate);
117         avcodec_free_context(&avctx);
118         return 0;
119     }
120
121     avcodec_free_context(&avctx);
122     return 1;
123 fail:
124     av_parser_close(parser);
125     avcodec_free_context(&avctx);
126     return ret;
127 }
128
129 const struct ogg_codec ff_flac_codec = {
130     .magic = "\177FLAC",
131     .magicsize = 5,
132     .header = flac_header,
133     .nb_header = 2,
134 };
135
136 const struct ogg_codec ff_old_flac_codec = {
137     .magic = "fLaC",
138     .magicsize = 4,
139     .header = old_flac_header,
140     .nb_header = 0,
141 };