rawdec: add a pixel_format private option.
[libav.git] / libavformat / rawdec.c
1 /*
2 * RAW demuxers
3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2005 Alex Beregszaszi
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "rawdec.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/parseutils.h"
28 #include "libavutil/pixdesc.h"
29
30 /* raw input */
31 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
32 {
33 AVStream *st;
34 enum CodecID id;
35
36 st = av_new_stream(s, 0);
37 if (!st)
38 return AVERROR(ENOMEM);
39
40 id = s->iformat->value;
41 if (id == CODEC_ID_RAWVIDEO) {
42 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
43 } else {
44 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
45 }
46 st->codec->codec_id = id;
47
48 switch(st->codec->codec_type) {
49 case AVMEDIA_TYPE_AUDIO: {
50 RawAudioDemuxerContext *s1 = s->priv_data;
51
52 #if FF_API_FORMAT_PARAMETERS
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;
58 #endif
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
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;
68 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
69 break;
70 }
71 case AVMEDIA_TYPE_VIDEO: {
72 FFRawVideoDemuxerContext *s1 = s->priv_data;
73 int width = 0, height = 0, ret;
74 enum PixelFormat pix_fmt;
75
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);
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;
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;
94 if (ap->pix_fmt)
95 pix_fmt = ap->pix_fmt;
96 #endif
97 st->codec->width = width;
98 st->codec->height = height;
99 st->codec->pix_fmt = pix_fmt;
100 break;
101 fail:
102 av_freep(&s1->video_size);
103 av_freep(&s1->pixel_format);
104 return ret;
105 }
106 default:
107 return -1;
108 }
109 return 0;
110 }
111
112 #define RAW_PACKET_SIZE 1024
113
114 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
115 {
116 int ret, size;
117
118 size = RAW_PACKET_SIZE;
119
120 if (av_new_packet(pkt, size) < 0)
121 return AVERROR(ENOMEM);
122
123 pkt->pos= avio_tell(s->pb);
124 pkt->stream_index = 0;
125 ret = ffio_read_partial(s->pb, pkt->data, size);
126 if (ret < 0) {
127 av_free_packet(pkt);
128 return ret;
129 }
130 pkt->size = ret;
131 return ret;
132 }
133
134 int ff_raw_audio_read_header(AVFormatContext *s,
135 AVFormatParameters *ap)
136 {
137 AVStream *st = av_new_stream(s, 0);
138 if (!st)
139 return AVERROR(ENOMEM);
140 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
141 st->codec->codec_id = s->iformat->value;
142 st->need_parsing = AVSTREAM_PARSE_FULL;
143 /* the parameters will be extracted from the compressed bitstream */
144
145 return 0;
146 }
147
148 /* MPEG-1/H.263 input */
149 int ff_raw_video_read_header(AVFormatContext *s,
150 AVFormatParameters *ap)
151 {
152 AVStream *st;
153
154 st = av_new_stream(s, 0);
155 if (!st)
156 return AVERROR(ENOMEM);
157
158 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
159 st->codec->codec_id = s->iformat->value;
160 st->need_parsing = AVSTREAM_PARSE_FULL;
161
162 /* for MJPEG, specify frame rate */
163 /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
164 if (ap->time_base.num) {
165 st->codec->time_base= ap->time_base;
166 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
167 st->codec->codec_id == CODEC_ID_MPEG4 ||
168 st->codec->codec_id == CODEC_ID_DIRAC ||
169 st->codec->codec_id == CODEC_ID_DNXHD ||
170 st->codec->codec_id == CODEC_ID_VC1 ||
171 st->codec->codec_id == CODEC_ID_H264) {
172 st->codec->time_base= (AVRational){1,25};
173 }
174 av_set_pts_info(st, 64, 1, 1200000);
175
176 return 0;
177 }
178
179 /* Note: Do not forget to add new entries to the Makefile as well. */
180
181 static const AVOption audio_options[] = {
182 { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
183 { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
184 { NULL },
185 };
186
187 const AVClass ff_rawaudio_demuxer_class = {
188 .class_name = "rawaudio demuxer",
189 .item_name = av_default_item_name,
190 .option = audio_options,
191 .version = LIBAVUTIL_VERSION_INT,
192 };
193
194 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
195 #define DEC AV_OPT_FLAG_DECODING_PARAM
196 static const AVOption video_options[] = {
197 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
198 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
199 { NULL },
200 };
201 #undef OFFSET
202 #undef DEC
203
204 const AVClass ff_rawvideo_demuxer_class = {
205 .class_name = "rawvideo demuxer",
206 .item_name = av_default_item_name,
207 .option = video_options,
208 .version = LIBAVUTIL_VERSION_INT,
209 };
210
211 #if CONFIG_G722_DEMUXER
212 AVInputFormat ff_g722_demuxer = {
213 "g722",
214 NULL_IF_CONFIG_SMALL("raw G.722"),
215 sizeof(RawAudioDemuxerContext),
216 NULL,
217 ff_raw_read_header,
218 ff_raw_read_partial_packet,
219 .flags= AVFMT_GENERIC_INDEX,
220 .extensions = "g722,722",
221 .value = CODEC_ID_ADPCM_G722,
222 .priv_class = &ff_rawaudio_demuxer_class,
223 };
224 #endif
225
226 #if CONFIG_GSM_DEMUXER
227 AVInputFormat ff_gsm_demuxer = {
228 "gsm",
229 NULL_IF_CONFIG_SMALL("raw GSM"),
230 0,
231 NULL,
232 ff_raw_audio_read_header,
233 ff_raw_read_partial_packet,
234 .flags= AVFMT_GENERIC_INDEX,
235 .extensions = "gsm",
236 .value = CODEC_ID_GSM,
237 };
238 #endif
239
240 #if CONFIG_MJPEG_DEMUXER
241 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
242 #endif
243
244 #if CONFIG_MLP_DEMUXER
245 AVInputFormat ff_mlp_demuxer = {
246 "mlp",
247 NULL_IF_CONFIG_SMALL("raw MLP"),
248 0,
249 NULL,
250 ff_raw_audio_read_header,
251 ff_raw_read_partial_packet,
252 .flags= AVFMT_GENERIC_INDEX,
253 .extensions = "mlp",
254 .value = CODEC_ID_MLP,
255 };
256 #endif
257
258 #if CONFIG_TRUEHD_DEMUXER
259 AVInputFormat ff_truehd_demuxer = {
260 "truehd",
261 NULL_IF_CONFIG_SMALL("raw TrueHD"),
262 0,
263 NULL,
264 ff_raw_audio_read_header,
265 ff_raw_read_partial_packet,
266 .flags= AVFMT_GENERIC_INDEX,
267 .extensions = "thd",
268 .value = CODEC_ID_TRUEHD,
269 };
270 #endif
271
272 #if CONFIG_SHORTEN_DEMUXER
273 AVInputFormat ff_shorten_demuxer = {
274 "shn",
275 NULL_IF_CONFIG_SMALL("raw Shorten"),
276 0,
277 NULL,
278 ff_raw_audio_read_header,
279 ff_raw_read_partial_packet,
280 .flags= AVFMT_GENERIC_INDEX,
281 .extensions = "shn",
282 .value = CODEC_ID_SHORTEN,
283 };
284 #endif
285
286 #if CONFIG_VC1_DEMUXER
287 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
288 #endif