rawdec: don't leak option strings.
[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 fail:
101 av_freep(&s1->video_size);
102 av_freep(&s1->pixel_format);
103 return ret;
104 }
105 default:
106 return -1;
107 }
108 return 0;
109 }
110
111 #define RAW_PACKET_SIZE 1024
112
113 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
114 {
115 int ret, size;
116
117 size = RAW_PACKET_SIZE;
118
119 if (av_new_packet(pkt, size) < 0)
120 return AVERROR(ENOMEM);
121
122 pkt->pos= avio_tell(s->pb);
123 pkt->stream_index = 0;
124 ret = ffio_read_partial(s->pb, pkt->data, size);
125 if (ret < 0) {
126 av_free_packet(pkt);
127 return ret;
128 }
129 pkt->size = ret;
130 return ret;
131 }
132
133 int ff_raw_audio_read_header(AVFormatContext *s,
134 AVFormatParameters *ap)
135 {
136 AVStream *st = av_new_stream(s, 0);
137 if (!st)
138 return AVERROR(ENOMEM);
139 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
140 st->codec->codec_id = s->iformat->value;
141 st->need_parsing = AVSTREAM_PARSE_FULL;
142 /* the parameters will be extracted from the compressed bitstream */
143
144 return 0;
145 }
146
147 /* MPEG-1/H.263 input */
148 int ff_raw_video_read_header(AVFormatContext *s,
149 AVFormatParameters *ap)
150 {
151 AVStream *st;
152
153 st = av_new_stream(s, 0);
154 if (!st)
155 return AVERROR(ENOMEM);
156
157 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
158 st->codec->codec_id = s->iformat->value;
159 st->need_parsing = AVSTREAM_PARSE_FULL;
160
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 ...)*/
163 if (ap->time_base.num) {
164 st->codec->time_base= ap->time_base;
165 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
166 st->codec->codec_id == CODEC_ID_MPEG4 ||
167 st->codec->codec_id == CODEC_ID_DIRAC ||
168 st->codec->codec_id == CODEC_ID_DNXHD ||
169 st->codec->codec_id == CODEC_ID_VC1 ||
170 st->codec->codec_id == CODEC_ID_H264) {
171 st->codec->time_base= (AVRational){1,25};
172 }
173 av_set_pts_info(st, 64, 1, 1200000);
174
175 return 0;
176 }
177
178 /* Note: Do not forget to add new entries to the Makefile as well. */
179
180 static 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
186 const 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
193 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
194 #define DEC AV_OPT_FLAG_DECODING_PARAM
195 static 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 },
197 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
198 { NULL },
199 };
200 #undef OFFSET
201 #undef DEC
202
203 const 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
210 #if CONFIG_G722_DEMUXER
211 AVInputFormat ff_g722_demuxer = {
212 "g722",
213 NULL_IF_CONFIG_SMALL("raw G.722"),
214 sizeof(RawAudioDemuxerContext),
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,
221 .priv_class = &ff_rawaudio_demuxer_class,
222 };
223 #endif
224
225 #if CONFIG_GSM_DEMUXER
226 AVInputFormat ff_gsm_demuxer = {
227 "gsm",
228 NULL_IF_CONFIG_SMALL("raw GSM"),
229 0,
230 NULL,
231 ff_raw_audio_read_header,
232 ff_raw_read_partial_packet,
233 .flags= AVFMT_GENERIC_INDEX,
234 .extensions = "gsm",
235 .value = CODEC_ID_GSM,
236 };
237 #endif
238
239 #if CONFIG_MJPEG_DEMUXER
240 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
241 #endif
242
243 #if CONFIG_MLP_DEMUXER
244 AVInputFormat ff_mlp_demuxer = {
245 "mlp",
246 NULL_IF_CONFIG_SMALL("raw MLP"),
247 0,
248 NULL,
249 ff_raw_audio_read_header,
250 ff_raw_read_partial_packet,
251 .flags= AVFMT_GENERIC_INDEX,
252 .extensions = "mlp",
253 .value = CODEC_ID_MLP,
254 };
255 #endif
256
257 #if CONFIG_TRUEHD_DEMUXER
258 AVInputFormat ff_truehd_demuxer = {
259 "truehd",
260 NULL_IF_CONFIG_SMALL("raw TrueHD"),
261 0,
262 NULL,
263 ff_raw_audio_read_header,
264 ff_raw_read_partial_packet,
265 .flags= AVFMT_GENERIC_INDEX,
266 .extensions = "thd",
267 .value = CODEC_ID_TRUEHD,
268 };
269 #endif
270
271 #if CONFIG_SHORTEN_DEMUXER
272 AVInputFormat ff_shorten_demuxer = {
273 "shn",
274 NULL_IF_CONFIG_SMALL("raw Shorten"),
275 0,
276 NULL,
277 ff_raw_audio_read_header,
278 ff_raw_read_partial_packet,
279 .flags= AVFMT_GENERIC_INDEX,
280 .extensions = "shn",
281 .value = CODEC_ID_SHORTEN,
282 };
283 #endif
284
285 #if CONFIG_VC1_DEMUXER
286 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
287 #endif