x11grab: add video_size 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"
de6d9b64 27
de6d9b64 28/* raw input */
e94204df 29int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64
FB
30{
31 AVStream *st;
0f87b771 32 enum CodecID id;
de6d9b64 33
c9a65ca8 34 st = av_new_stream(s, 0);
de6d9b64 35 if (!st)
769e10f0 36 return AVERROR(ENOMEM);
c04c3282 37
c9a65ca8
FB
38 id = s->iformat->value;
39 if (id == CODEC_ID_RAWVIDEO) {
72415b2a 40 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
de6d9b64 41 } else {
72415b2a 42 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
de6d9b64 43 }
01f4895c 44 st->codec->codec_id = id;
c9a65ca8 45
01f4895c 46 switch(st->codec->codec_type) {
5b3865fc
AK
47 case AVMEDIA_TYPE_AUDIO: {
48 RawAudioDemuxerContext *s1 = s->priv_data;
49
bffd4dd1 50#if FF_API_FORMAT_PARAMETERS
5b3865fc
AK
51 if (ap->sample_rate)
52 st->codec->sample_rate = ap->sample_rate;
53 if (ap->channels)
54 st->codec->channels = ap->channels;
55 else st->codec->channels = 1;
bffd4dd1 56#endif
5b3865fc
AK
57
58 if (s1->sample_rate)
59 st->codec->sample_rate = s1->sample_rate;
60 if (s1->channels)
61 st->codec->channels = s1->channels;
62
a3d23e15
BC
63 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
64 assert(st->codec->bits_per_coded_sample > 0);
65 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
01f4895c 66 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
de6d9b64 67 break;
5b3865fc 68 }
72415b2a 69 case AVMEDIA_TYPE_VIDEO:
9de0be61
MN
70 if(ap->time_base.num)
71 av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
72 else
73 av_set_pts_info(st, 64, 1, 25);
01f4895c
MN
74 st->codec->width = ap->width;
75 st->codec->height = ap->height;
76 st->codec->pix_fmt = ap->pix_fmt;
77 if(st->codec->pix_fmt == PIX_FMT_NONE)
78 st->codec->pix_fmt= PIX_FMT_YUV420P;
de6d9b64
FB
79 break;
80 default:
27e084bd 81 return -1;
de6d9b64 82 }
de6d9b64
FB
83 return 0;
84}
85
2e93e3aa 86#define RAW_PACKET_SIZE 1024
de6d9b64 87
81f052cb 88int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
e15dec10
LS
89{
90 int ret, size;
91
92 size = RAW_PACKET_SIZE;
93
94 if (av_new_packet(pkt, size) < 0)
c67031e7 95 return AVERROR(ENOMEM);
115329f1 96
a2704c97 97 pkt->pos= avio_tell(s->pb);
e15dec10 98 pkt->stream_index = 0;
b3db9cee 99 ret = ffio_read_partial(s->pb, pkt->data, size);
c3db0bc6 100 if (ret < 0) {
e15dec10 101 av_free_packet(pkt);
c3db0bc6 102 return ret;
e15dec10
LS
103 }
104 pkt->size = ret;
105 return ret;
106}
76d32428 107
6d0678d1 108int ff_raw_audio_read_header(AVFormatContext *s,
a0af2fa4 109 AVFormatParameters *ap)
fda885c7 110{
a0af2fa4 111 AVStream *st = av_new_stream(s, 0);
fda885c7 112 if (!st)
769e10f0 113 return AVERROR(ENOMEM);
72415b2a 114 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
a0af2fa4 115 st->codec->codec_id = s->iformat->value;
57004ff1 116 st->need_parsing = AVSTREAM_PARSE_FULL;
fda885c7 117 /* the parameters will be extracted from the compressed bitstream */
6cde949a 118
fda885c7
MR
119 return 0;
120}
121
fb9f1117 122/* MPEG-1/H.263 input */
b47a5a95 123int ff_raw_video_read_header(AVFormatContext *s,
de6d9b64
FB
124 AVFormatParameters *ap)
125{
126 AVStream *st;
127
c9a65ca8 128 st = av_new_stream(s, 0);
de6d9b64 129 if (!st)
769e10f0 130 return AVERROR(ENOMEM);
de6d9b64 131
72415b2a 132 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c 133 st->codec->codec_id = s->iformat->value;
57004ff1 134 st->need_parsing = AVSTREAM_PARSE_FULL;
4986a429 135
fb9f1117
DB
136 /* for MJPEG, specify frame rate */
137 /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
c04c3282 138 if (ap->time_base.num) {
4022fe01 139 st->codec->time_base= ap->time_base;
115329f1 140 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
01f4895c 141 st->codec->codec_id == CODEC_ID_MPEG4 ||
17ac9f1c 142 st->codec->codec_id == CODEC_ID_DIRAC ||
0cd55b0c 143 st->codec->codec_id == CODEC_ID_DNXHD ||
1c169711 144 st->codec->codec_id == CODEC_ID_VC1 ||
01f4895c 145 st->codec->codec_id == CODEC_ID_H264) {
4022fe01 146 st->codec->time_base= (AVRational){1,25};
27e084bd 147 }
4022fe01 148 av_set_pts_info(st, 64, 1, 1200000);
80ce3254 149
de6d9b64
FB
150 return 0;
151}
152
900eb63d
DB
153/* Note: Do not forget to add new entries to the Makefile as well. */
154
5b3865fc
AK
155static const AVOption audio_options[] = {
156 { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
157 { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
158 { NULL },
159};
160
161const AVClass ff_rawaudio_demuxer_class = {
162 .class_name = "rawaudio demuxer",
163 .item_name = av_default_item_name,
164 .option = audio_options,
165 .version = LIBAVUTIL_VERSION_INT,
166};
167
9013560f 168#if CONFIG_G722_DEMUXER
c6610a21 169AVInputFormat ff_g722_demuxer = {
9013560f
MS
170 "g722",
171 NULL_IF_CONFIG_SMALL("raw G.722"),
5b3865fc 172 sizeof(RawAudioDemuxerContext),
9013560f
MS
173 NULL,
174 ff_raw_read_header,
175 ff_raw_read_partial_packet,
176 .flags= AVFMT_GENERIC_INDEX,
177 .extensions = "g722,722",
178 .value = CODEC_ID_ADPCM_G722,
5b3865fc 179 .priv_class = &ff_rawaudio_demuxer_class,
9013560f
MS
180};
181#endif
182
b250f9c6 183#if CONFIG_GSM_DEMUXER
c6610a21 184AVInputFormat ff_gsm_demuxer = {
60711e95 185 "gsm",
b4ee1d39 186 NULL_IF_CONFIG_SMALL("raw GSM"),
60711e95
MN
187 0,
188 NULL,
6d0678d1 189 ff_raw_audio_read_header,
81f052cb 190 ff_raw_read_partial_packet,
60711e95
MN
191 .flags= AVFMT_GENERIC_INDEX,
192 .extensions = "gsm",
193 .value = CODEC_ID_GSM,
194};
7402ee23 195#endif
60711e95 196
b250f9c6 197#if CONFIG_MJPEG_DEMUXER
c6610a21 198AVInputFormat ff_mjpeg_demuxer = {
76d32428 199 "mjpeg",
b4ee1d39 200 NULL_IF_CONFIG_SMALL("raw MJPEG video"),
0da71265 201 0,
76d32428 202 NULL,
b47a5a95 203 ff_raw_video_read_header,
81f052cb 204 ff_raw_read_partial_packet,
e9b78eeb 205 .flags= AVFMT_GENERIC_INDEX,
76d32428
DB
206 .extensions = "mjpg,mjpeg",
207 .value = CODEC_ID_MJPEG,
0da71265 208};
7402ee23 209#endif
0da71265 210
b250f9c6 211#if CONFIG_MLP_DEMUXER
c6610a21 212AVInputFormat ff_mlp_demuxer = {
76d32428
DB
213 "mlp",
214 NULL_IF_CONFIG_SMALL("raw MLP"),
c9a65ca8 215 0,
76d32428 216 NULL,
6d0678d1 217 ff_raw_audio_read_header,
81f052cb 218 ff_raw_read_partial_packet,
e9b78eeb 219 .flags= AVFMT_GENERIC_INDEX,
76d32428
DB
220 .extensions = "mlp",
221 .value = CODEC_ID_MLP,
de6d9b64 222};
7402ee23 223#endif
de6d9b64 224
23d9cc45 225#if CONFIG_TRUEHD_DEMUXER
c6610a21 226AVInputFormat ff_truehd_demuxer = {
23d9cc45
RP
227 "truehd",
228 NULL_IF_CONFIG_SMALL("raw TrueHD"),
229 0,
230 NULL,
6d0678d1 231 ff_raw_audio_read_header,
23d9cc45
RP
232 ff_raw_read_partial_packet,
233 .flags= AVFMT_GENERIC_INDEX,
234 .extensions = "thd",
235 .value = CODEC_ID_TRUEHD,
236};
237#endif
238
b250f9c6 239#if CONFIG_SHORTEN_DEMUXER
c6610a21 240AVInputFormat ff_shorten_demuxer = {
76d32428
DB
241 "shn",
242 NULL_IF_CONFIG_SMALL("raw Shorten"),
243 0,
244 NULL,
6d0678d1 245 ff_raw_audio_read_header,
81f052cb 246 ff_raw_read_partial_packet,
76d32428
DB
247 .flags= AVFMT_GENERIC_INDEX,
248 .extensions = "shn",
249 .value = CODEC_ID_SHORTEN,
250};
7402ee23 251#endif
76d32428 252
b250f9c6 253#if CONFIG_VC1_DEMUXER
c6610a21 254AVInputFormat ff_vc1_demuxer = {
7bb5c2a6 255 "vc1",
bde15e74 256 NULL_IF_CONFIG_SMALL("raw VC-1"),
7bb5c2a6
KS
257 0,
258 NULL /* vc1_probe */,
b47a5a95 259 ff_raw_video_read_header,
81f052cb 260 ff_raw_read_partial_packet,
7bb5c2a6
KS
261 .extensions = "vc1",
262 .value = CODEC_ID_VC1,
263};
7402ee23 264#endif