lavf: use designated initialisers for all (de)muxers.
[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 = 0;
74 enum PixelFormat pix_fmt;
75 AVRational framerate;
76
77 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
78 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
79 goto fail;
80 }
81 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
82 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
83 ret = AVERROR(EINVAL);
84 goto fail;
85 }
86 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
87 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
88 goto fail;
89 }
90 #if FF_API_FORMAT_PARAMETERS
91 if (ap->width > 0)
92 width = ap->width;
93 if (ap->height > 0)
94 height = ap->height;
95 if (ap->pix_fmt)
96 pix_fmt = ap->pix_fmt;
97 if (ap->time_base.num)
98 framerate = (AVRational){ap->time_base.den, ap->time_base.num};
99 #endif
100 av_set_pts_info(st, 64, framerate.den, framerate.num);
101 st->codec->width = width;
102 st->codec->height = height;
103 st->codec->pix_fmt = pix_fmt;
104 fail:
105 return ret;
106 }
107 default:
108 return -1;
109 }
110 return 0;
111 }
112
113 #define RAW_PACKET_SIZE 1024
114
115 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
116 {
117 int ret, size;
118
119 size = RAW_PACKET_SIZE;
120
121 if (av_new_packet(pkt, size) < 0)
122 return AVERROR(ENOMEM);
123
124 pkt->pos= avio_tell(s->pb);
125 pkt->stream_index = 0;
126 ret = ffio_read_partial(s->pb, pkt->data, size);
127 if (ret < 0) {
128 av_free_packet(pkt);
129 return ret;
130 }
131 pkt->size = ret;
132 return ret;
133 }
134
135 int ff_raw_audio_read_header(AVFormatContext *s,
136 AVFormatParameters *ap)
137 {
138 AVStream *st = av_new_stream(s, 0);
139 if (!st)
140 return AVERROR(ENOMEM);
141 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
142 st->codec->codec_id = s->iformat->value;
143 st->need_parsing = AVSTREAM_PARSE_FULL;
144 /* the parameters will be extracted from the compressed bitstream */
145
146 return 0;
147 }
148
149 /* MPEG-1/H.263 input */
150 int ff_raw_video_read_header(AVFormatContext *s,
151 AVFormatParameters *ap)
152 {
153 AVStream *st;
154 FFRawVideoDemuxerContext *s1 = s->priv_data;
155 AVRational framerate;
156 int ret = 0;
157
158
159 st = av_new_stream(s, 0);
160 if (!st) {
161 ret = AVERROR(ENOMEM);
162 goto fail;
163 }
164
165 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
166 st->codec->codec_id = s->iformat->value;
167 st->need_parsing = AVSTREAM_PARSE_FULL;
168
169 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
170 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
171 goto fail;
172 }
173 #if FF_API_FORMAT_PARAMETERS
174 if (ap->time_base.num)
175 framerate = (AVRational){ap->time_base.den, ap->time_base.num};
176 #endif
177
178 st->codec->time_base = (AVRational){framerate.den, framerate.num};
179 av_set_pts_info(st, 64, 1, 1200000);
180
181 fail:
182 return ret;
183 }
184
185 /* Note: Do not forget to add new entries to the Makefile as well. */
186
187 static const AVOption audio_options[] = {
188 { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
189 { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
190 { NULL },
191 };
192
193 const AVClass ff_rawaudio_demuxer_class = {
194 .class_name = "rawaudio demuxer",
195 .item_name = av_default_item_name,
196 .option = audio_options,
197 .version = LIBAVUTIL_VERSION_INT,
198 };
199
200 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
201 #define DEC AV_OPT_FLAG_DECODING_PARAM
202 static const AVOption video_options[] = {
203 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
204 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
205 { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
206 { NULL },
207 };
208 #undef OFFSET
209 #undef DEC
210
211 const AVClass ff_rawvideo_demuxer_class = {
212 .class_name = "rawvideo demuxer",
213 .item_name = av_default_item_name,
214 .option = video_options,
215 .version = LIBAVUTIL_VERSION_INT,
216 };
217
218 #if CONFIG_G722_DEMUXER
219 AVInputFormat ff_g722_demuxer = {
220 .name = "g722",
221 .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
222 .priv_data_size = sizeof(RawAudioDemuxerContext),
223 .read_header = ff_raw_read_header,
224 .read_packet = ff_raw_read_partial_packet,
225 .flags= AVFMT_GENERIC_INDEX,
226 .extensions = "g722,722",
227 .value = CODEC_ID_ADPCM_G722,
228 .priv_class = &ff_rawaudio_demuxer_class,
229 };
230 #endif
231
232 #if CONFIG_GSM_DEMUXER
233 AVInputFormat ff_gsm_demuxer = {
234 .name = "gsm",
235 .long_name = NULL_IF_CONFIG_SMALL("raw GSM"),
236 .read_header = ff_raw_audio_read_header,
237 .read_packet = ff_raw_read_partial_packet,
238 .flags= AVFMT_GENERIC_INDEX,
239 .extensions = "gsm",
240 .value = CODEC_ID_GSM,
241 };
242 #endif
243
244 #if CONFIG_MJPEG_DEMUXER
245 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
246 #endif
247
248 #if CONFIG_MLP_DEMUXER
249 AVInputFormat ff_mlp_demuxer = {
250 .name = "mlp",
251 .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
252 .read_header = ff_raw_audio_read_header,
253 .read_packet = ff_raw_read_partial_packet,
254 .flags= AVFMT_GENERIC_INDEX,
255 .extensions = "mlp",
256 .value = CODEC_ID_MLP,
257 };
258 #endif
259
260 #if CONFIG_TRUEHD_DEMUXER
261 AVInputFormat ff_truehd_demuxer = {
262 .name = "truehd",
263 .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
264 .read_header = ff_raw_audio_read_header,
265 .read_packet = 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 .name = "shn",
275 .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
276 .read_header = ff_raw_audio_read_header,
277 .read_packet = ff_raw_read_partial_packet,
278 .flags= AVFMT_GENERIC_INDEX,
279 .extensions = "shn",
280 .value = CODEC_ID_SHORTEN,
281 };
282 #endif
283
284 #if CONFIG_VC1_DEMUXER
285 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
286 #endif