265822b2dae5b90402c30c9885b100894d5bdcec
[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
29 /* raw input */
30 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
31 {
32 AVStream *st;
33 enum CodecID id;
34
35 st = av_new_stream(s, 0);
36 if (!st)
37 return AVERROR(ENOMEM);
38
39 id = s->iformat->value;
40 if (id == CODEC_ID_RAWVIDEO) {
41 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
42 } else {
43 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
44 }
45 st->codec->codec_id = id;
46
47 switch(st->codec->codec_type) {
48 case AVMEDIA_TYPE_AUDIO: {
49 RawAudioDemuxerContext *s1 = s->priv_data;
50
51 #if FF_API_FORMAT_PARAMETERS
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;
57 #endif
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
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;
67 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
68 break;
69 }
70 case AVMEDIA_TYPE_VIDEO: {
71 FFRawVideoDemuxerContext *s1 = s->priv_data;
72 int width = 0, height = 0, ret;
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);
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;
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;
96 break;
97 }
98 default:
99 return -1;
100 }
101 return 0;
102 }
103
104 #define RAW_PACKET_SIZE 1024
105
106 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
107 {
108 int ret, size;
109
110 size = RAW_PACKET_SIZE;
111
112 if (av_new_packet(pkt, size) < 0)
113 return AVERROR(ENOMEM);
114
115 pkt->pos= avio_tell(s->pb);
116 pkt->stream_index = 0;
117 ret = ffio_read_partial(s->pb, pkt->data, size);
118 if (ret < 0) {
119 av_free_packet(pkt);
120 return ret;
121 }
122 pkt->size = ret;
123 return ret;
124 }
125
126 int ff_raw_audio_read_header(AVFormatContext *s,
127 AVFormatParameters *ap)
128 {
129 AVStream *st = av_new_stream(s, 0);
130 if (!st)
131 return AVERROR(ENOMEM);
132 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
133 st->codec->codec_id = s->iformat->value;
134 st->need_parsing = AVSTREAM_PARSE_FULL;
135 /* the parameters will be extracted from the compressed bitstream */
136
137 return 0;
138 }
139
140 /* MPEG-1/H.263 input */
141 int ff_raw_video_read_header(AVFormatContext *s,
142 AVFormatParameters *ap)
143 {
144 AVStream *st;
145
146 st = av_new_stream(s, 0);
147 if (!st)
148 return AVERROR(ENOMEM);
149
150 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
151 st->codec->codec_id = s->iformat->value;
152 st->need_parsing = AVSTREAM_PARSE_FULL;
153
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 ...)*/
156 if (ap->time_base.num) {
157 st->codec->time_base= ap->time_base;
158 } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
159 st->codec->codec_id == CODEC_ID_MPEG4 ||
160 st->codec->codec_id == CODEC_ID_DIRAC ||
161 st->codec->codec_id == CODEC_ID_DNXHD ||
162 st->codec->codec_id == CODEC_ID_VC1 ||
163 st->codec->codec_id == CODEC_ID_H264) {
164 st->codec->time_base= (AVRational){1,25};
165 }
166 av_set_pts_info(st, 64, 1, 1200000);
167
168 return 0;
169 }
170
171 /* Note: Do not forget to add new entries to the Makefile as well. */
172
173 static 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
179 const 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
186 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
187 #define DEC AV_OPT_FLAG_DECODING_PARAM
188 static 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
195 const 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
202 #if CONFIG_G722_DEMUXER
203 AVInputFormat ff_g722_demuxer = {
204 "g722",
205 NULL_IF_CONFIG_SMALL("raw G.722"),
206 sizeof(RawAudioDemuxerContext),
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,
213 .priv_class = &ff_rawaudio_demuxer_class,
214 };
215 #endif
216
217 #if CONFIG_GSM_DEMUXER
218 AVInputFormat ff_gsm_demuxer = {
219 "gsm",
220 NULL_IF_CONFIG_SMALL("raw GSM"),
221 0,
222 NULL,
223 ff_raw_audio_read_header,
224 ff_raw_read_partial_packet,
225 .flags= AVFMT_GENERIC_INDEX,
226 .extensions = "gsm",
227 .value = CODEC_ID_GSM,
228 };
229 #endif
230
231 #if CONFIG_MJPEG_DEMUXER
232 AVInputFormat ff_mjpeg_demuxer = {
233 "mjpeg",
234 NULL_IF_CONFIG_SMALL("raw MJPEG video"),
235 0,
236 NULL,
237 ff_raw_video_read_header,
238 ff_raw_read_partial_packet,
239 .flags= AVFMT_GENERIC_INDEX,
240 .extensions = "mjpg,mjpeg",
241 .value = CODEC_ID_MJPEG,
242 };
243 #endif
244
245 #if CONFIG_MLP_DEMUXER
246 AVInputFormat ff_mlp_demuxer = {
247 "mlp",
248 NULL_IF_CONFIG_SMALL("raw MLP"),
249 0,
250 NULL,
251 ff_raw_audio_read_header,
252 ff_raw_read_partial_packet,
253 .flags= AVFMT_GENERIC_INDEX,
254 .extensions = "mlp",
255 .value = CODEC_ID_MLP,
256 };
257 #endif
258
259 #if CONFIG_TRUEHD_DEMUXER
260 AVInputFormat ff_truehd_demuxer = {
261 "truehd",
262 NULL_IF_CONFIG_SMALL("raw TrueHD"),
263 0,
264 NULL,
265 ff_raw_audio_read_header,
266 ff_raw_read_partial_packet,
267 .flags= AVFMT_GENERIC_INDEX,
268 .extensions = "thd",
269 .value = CODEC_ID_TRUEHD,
270 };
271 #endif
272
273 #if CONFIG_SHORTEN_DEMUXER
274 AVInputFormat ff_shorten_demuxer = {
275 "shn",
276 NULL_IF_CONFIG_SMALL("raw Shorten"),
277 0,
278 NULL,
279 ff_raw_audio_read_header,
280 ff_raw_read_partial_packet,
281 .flags= AVFMT_GENERIC_INDEX,
282 .extensions = "shn",
283 .value = CODEC_ID_SHORTEN,
284 };
285 #endif
286
287 #if CONFIG_VC1_DEMUXER
288 AVInputFormat ff_vc1_demuxer = {
289 "vc1",
290 NULL_IF_CONFIG_SMALL("raw VC-1"),
291 0,
292 NULL /* vc1_probe */,
293 ff_raw_video_read_header,
294 ff_raw_read_partial_packet,
295 .extensions = "vc1",
296 .value = CODEC_ID_VC1,
297 };
298 #endif