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