x11grab: add framerate private option.
[libav.git] / libavformat / rawdec.c
CommitLineData
115329f1 1/*
4ca31edc 2 * RAW demuxers
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
84c63c01 4 * Copyright (c) 2005 Alex Beregszaszi
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
19720f15
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
19720f15 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
245976da 22
de6d9b64 23#include "avformat.h"
b3db9cee 24#include "avio_internal.h"
4ca31edc 25#include "rawdec.h"
5b3865fc 26#include "libavutil/opt.h"
973f686a 27#include "libavutil/parseutils.h"
2a85f218 28#include "libavutil/pixdesc.h"
de6d9b64 29
de6d9b64 30/* raw input */
e94204df 31int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64
FB
32{
33 AVStream *st;
0f87b771 34 enum CodecID id;
de6d9b64 35
c9a65ca8 36 st = av_new_stream(s, 0);
de6d9b64 37 if (!st)
769e10f0 38 return AVERROR(ENOMEM);
c04c3282 39
c9a65ca8
FB
40 id = s->iformat->value;
41 if (id == CODEC_ID_RAWVIDEO) {
72415b2a 42 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
de6d9b64 43 } else {
72415b2a 44 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
de6d9b64 45 }
01f4895c 46 st->codec->codec_id = id;
c9a65ca8 47
01f4895c 48 switch(st->codec->codec_type) {
5b3865fc
AK
49 case AVMEDIA_TYPE_AUDIO: {
50 RawAudioDemuxerContext *s1 = s->priv_data;
51
bffd4dd1 52#if FF_API_FORMAT_PARAMETERS
5b3865fc
AK
53 if (ap->sample_rate)
54 st->codec->sample_rate = ap->sample_rate;
55 if (ap->channels)
56 st->codec->channels = ap->channels;
57 else st->codec->channels = 1;
bffd4dd1 58#endif
5b3865fc
AK
59
60 if (s1->sample_rate)
61 st->codec->sample_rate = s1->sample_rate;
62 if (s1->channels)
63 st->codec->channels = s1->channels;
64
a3d23e15
BC
65 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
66 assert(st->codec->bits_per_coded_sample > 0);
67 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
01f4895c 68 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
de6d9b64 69 break;
5b3865fc 70 }
973f686a
AK
71 case AVMEDIA_TYPE_VIDEO: {
72 FFRawVideoDemuxerContext *s1 = s->priv_data;
1f94c31f 73 int width = 0, height = 0, ret = 0;
2a85f218
AK
74 enum PixelFormat pix_fmt;
75
9de0be61
MN
76 if(ap->time_base.num)
77 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
78 else
79 av_set_pts_info(st, 64, 1, 25);
2a85f218
AK
80 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
81 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
82 goto fail;
83 }
84 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
85 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
86 ret = AVERROR(EINVAL);
87 goto fail;
973f686a
AK
88 }
89#if FF_API_FORMAT_PARAMETERS
90 if (ap->width > 0)
91 width = ap->width;
92 if (ap->height > 0)
93 height = ap->height;
2a85f218
AK
94 if (ap->pix_fmt)
95 pix_fmt = ap->pix_fmt;
973f686a
AK
96#endif
97 st->codec->width = width;
98 st->codec->height = height;
2a85f218 99 st->codec->pix_fmt = pix_fmt;
2a85f218
AK
100fail:
101 av_freep(&s1->video_size);
102 av_freep(&s1->pixel_format);
103 return ret;
973f686a 104 }
de6d9b64 105 default:
27e084bd 106 return -1;
de6d9b64 107 }
de6d9b64
FB
108 return 0;
109}
110
2e93e3aa 111#define RAW_PACKET_SIZE 1024
de6d9b64 112
81f052cb 113int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
e15dec10
LS
114{
115 int ret, size;
116
117 size = RAW_PACKET_SIZE;
118
119 if (av_new_packet(pkt, size) < 0)
c67031e7 120 return AVERROR(ENOMEM);
115329f1 121
a2704c97 122 pkt->pos= avio_tell(s->pb);
e15dec10 123 pkt->stream_index = 0;
b3db9cee 124 ret = ffio_read_partial(s->pb, pkt->data, size);
c3db0bc6 125 if (ret < 0) {
e15dec10 126 av_free_packet(pkt);
c3db0bc6 127 return ret;
e15dec10
LS
128 }
129 pkt->size = ret;
130 return ret;
131}
76d32428 132
6d0678d1 133int ff_raw_audio_read_header(AVFormatContext *s,
a0af2fa4 134 AVFormatParameters *ap)
fda885c7 135{
a0af2fa4 136 AVStream *st = av_new_stream(s, 0);
fda885c7 137 if (!st)
769e10f0 138 return AVERROR(ENOMEM);
72415b2a 139 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
a0af2fa4 140 st->codec->codec_id = s->iformat->value;
57004ff1 141 st->need_parsing = AVSTREAM_PARSE_FULL;
fda885c7 142 /* the parameters will be extracted from the compressed bitstream */
6cde949a 143
fda885c7
MR
144 return 0;
145}
146
fb9f1117 147/* MPEG-1/H.263 input */
b47a5a95 148int ff_raw_video_read_header(AVFormatContext *s,
de6d9b64
FB
149 AVFormatParameters *ap)
150{
151 AVStream *st;
152
c9a65ca8 153 st = av_new_stream(s, 0);
de6d9b64 154 if (!st)
769e10f0 155 return AVERROR(ENOMEM);
de6d9b64 156
72415b2a 157 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c 158 st->codec->codec_id = s->iformat->value;
57004ff1 159 st->need_parsing = AVSTREAM_PARSE_FULL;
4986a429 160
fb9f1117
DB
161 /* for MJPEG, specify frame rate */
162 /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
c04c3282 163 if (ap->time_base.num) {
4022fe01 164 st->codec->time_base= ap->time_base;
115329f1 165 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
01f4895c 166 st->codec->codec_id == CODEC_ID_MPEG4 ||
17ac9f1c 167 st->codec->codec_id == CODEC_ID_DIRAC ||
0cd55b0c 168 st->codec->codec_id == CODEC_ID_DNXHD ||
1c169711 169 st->codec->codec_id == CODEC_ID_VC1 ||
01f4895c 170 st->codec->codec_id == CODEC_ID_H264) {
4022fe01 171 st->codec->time_base= (AVRational){1,25};
27e084bd 172 }
4022fe01 173 av_set_pts_info(st, 64, 1, 1200000);
80ce3254 174
de6d9b64
FB
175 return 0;
176}
177
900eb63d
DB
178/* Note: Do not forget to add new entries to the Makefile as well. */
179
5b3865fc
AK
180static const AVOption audio_options[] = {
181 { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
182 { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
183 { NULL },
184};
185
186const AVClass ff_rawaudio_demuxer_class = {
187 .class_name = "rawaudio demuxer",
188 .item_name = av_default_item_name,
189 .option = audio_options,
190 .version = LIBAVUTIL_VERSION_INT,
191};
192
973f686a
AK
193#define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
194#define DEC AV_OPT_FLAG_DECODING_PARAM
195static const AVOption video_options[] = {
196 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
2a85f218 197 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
973f686a
AK
198 { NULL },
199};
200#undef OFFSET
201#undef DEC
202
203const AVClass ff_rawvideo_demuxer_class = {
204 .class_name = "rawvideo demuxer",
205 .item_name = av_default_item_name,
206 .option = video_options,
207 .version = LIBAVUTIL_VERSION_INT,
208};
209
9013560f 210#if CONFIG_G722_DEMUXER
c6610a21 211AVInputFormat ff_g722_demuxer = {
9013560f
MS
212 "g722",
213 NULL_IF_CONFIG_SMALL("raw G.722"),
5b3865fc 214 sizeof(RawAudioDemuxerContext),
9013560f
MS
215 NULL,
216 ff_raw_read_header,
217 ff_raw_read_partial_packet,
218 .flags= AVFMT_GENERIC_INDEX,
219 .extensions = "g722,722",
220 .value = CODEC_ID_ADPCM_G722,
5b3865fc 221 .priv_class = &ff_rawaudio_demuxer_class,
9013560f
MS
222};
223#endif
224
b250f9c6 225#if CONFIG_GSM_DEMUXER
c6610a21 226AVInputFormat ff_gsm_demuxer = {
60711e95 227 "gsm",
b4ee1d39 228 NULL_IF_CONFIG_SMALL("raw GSM"),
60711e95
MN
229 0,
230 NULL,
6d0678d1 231 ff_raw_audio_read_header,
81f052cb 232 ff_raw_read_partial_packet,
60711e95
MN
233 .flags= AVFMT_GENERIC_INDEX,
234 .extensions = "gsm",
235 .value = CODEC_ID_GSM,
236};
7402ee23 237#endif
60711e95 238
b250f9c6 239#if CONFIG_MJPEG_DEMUXER
fa4924a3 240FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
7402ee23 241#endif
0da71265 242
b250f9c6 243#if CONFIG_MLP_DEMUXER
c6610a21 244AVInputFormat ff_mlp_demuxer = {
76d32428
DB
245 "mlp",
246 NULL_IF_CONFIG_SMALL("raw MLP"),
c9a65ca8 247 0,
76d32428 248 NULL,
6d0678d1 249 ff_raw_audio_read_header,
81f052cb 250 ff_raw_read_partial_packet,
e9b78eeb 251 .flags= AVFMT_GENERIC_INDEX,
76d32428
DB
252 .extensions = "mlp",
253 .value = CODEC_ID_MLP,
de6d9b64 254};
7402ee23 255#endif
de6d9b64 256
23d9cc45 257#if CONFIG_TRUEHD_DEMUXER
c6610a21 258AVInputFormat ff_truehd_demuxer = {
23d9cc45
RP
259 "truehd",
260 NULL_IF_CONFIG_SMALL("raw TrueHD"),
261 0,
262 NULL,
6d0678d1 263 ff_raw_audio_read_header,
23d9cc45
RP
264 ff_raw_read_partial_packet,
265 .flags= AVFMT_GENERIC_INDEX,
266 .extensions = "thd",
267 .value = CODEC_ID_TRUEHD,
268};
269#endif
270
b250f9c6 271#if CONFIG_SHORTEN_DEMUXER
c6610a21 272AVInputFormat ff_shorten_demuxer = {
76d32428
DB
273 "shn",
274 NULL_IF_CONFIG_SMALL("raw Shorten"),
275 0,
276 NULL,
6d0678d1 277 ff_raw_audio_read_header,
81f052cb 278 ff_raw_read_partial_packet,
76d32428
DB
279 .flags= AVFMT_GENERIC_INDEX,
280 .extensions = "shn",
281 .value = CODEC_ID_SHORTEN,
282};
7402ee23 283#endif
76d32428 284
b250f9c6 285#if CONFIG_VC1_DEMUXER
fa4924a3 286FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
7402ee23 287#endif