]> git.sesse.net Git - ffmpeg/blob - libavformat/rawdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / 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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     st->start_time = 0;
145     /* the parameters will be extracted from the compressed bitstream */
146
147     return 0;
148 }
149
150 /* MPEG-1/H.263 input */
151 int ff_raw_video_read_header(AVFormatContext *s,
152                              AVFormatParameters *ap)
153 {
154     AVStream *st;
155     FFRawVideoDemuxerContext *s1 = s->priv_data;
156     AVRational framerate;
157     int ret = 0;
158
159
160     st = av_new_stream(s, 0);
161     if (!st) {
162         ret = AVERROR(ENOMEM);
163         goto fail;
164     }
165
166     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
167     st->codec->codec_id = s->iformat->value;
168     st->need_parsing = AVSTREAM_PARSE_FULL;
169
170     if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
171         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
172         goto fail;
173     }
174 #if FF_API_FORMAT_PARAMETERS
175     if (ap->time_base.num)
176         framerate = (AVRational){ap->time_base.den, ap->time_base.num};
177 #endif
178
179     st->codec->time_base = (AVRational){framerate.den, framerate.num};
180     av_set_pts_info(st, 64, 1, 1200000);
181
182 fail:
183     return ret;
184 }
185
186 /* Note: Do not forget to add new entries to the Makefile as well. */
187
188 static const AVOption audio_options[] = {
189     { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
190     { "channels",    "", offsetof(RawAudioDemuxerContext, channels),    FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
191     { NULL },
192 };
193
194 const AVClass ff_rawaudio_demuxer_class = {
195     .class_name     = "rawaudio demuxer",
196     .item_name      = av_default_item_name,
197     .option         = audio_options,
198     .version        = LIBAVUTIL_VERSION_INT,
199 };
200
201 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
202 #define DEC AV_OPT_FLAG_DECODING_PARAM
203 static const AVOption video_options[] = {
204     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
205     { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
206     { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
207     { NULL },
208 };
209 #undef OFFSET
210 #undef DEC
211
212 const AVClass ff_rawvideo_demuxer_class = {
213     .class_name     = "rawvideo demuxer",
214     .item_name      = av_default_item_name,
215     .option         = video_options,
216     .version        = LIBAVUTIL_VERSION_INT,
217 };
218
219 #if CONFIG_G722_DEMUXER
220 AVInputFormat ff_g722_demuxer = {
221     .name           = "g722",
222     .long_name      = NULL_IF_CONFIG_SMALL("raw G.722"),
223     .priv_data_size = sizeof(RawAudioDemuxerContext),
224     .read_header    = ff_raw_read_header,
225     .read_packet    = ff_raw_read_partial_packet,
226     .flags= AVFMT_GENERIC_INDEX,
227     .extensions = "g722,722",
228     .value = CODEC_ID_ADPCM_G722,
229     .priv_class = &ff_rawaudio_demuxer_class,
230 };
231 #endif
232
233 #if CONFIG_GSM_DEMUXER
234 AVInputFormat ff_gsm_demuxer = {
235     .name           = "gsm",
236     .long_name      = NULL_IF_CONFIG_SMALL("raw GSM"),
237     .read_header    = ff_raw_audio_read_header,
238     .read_packet    = ff_raw_read_partial_packet,
239     .flags= AVFMT_GENERIC_INDEX,
240     .extensions = "gsm",
241     .value = CODEC_ID_GSM,
242 };
243 #endif
244
245 #if CONFIG_MJPEG_DEMUXER
246 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
247 #endif
248
249 #if CONFIG_MLP_DEMUXER
250 AVInputFormat ff_mlp_demuxer = {
251     .name           = "mlp",
252     .long_name      = NULL_IF_CONFIG_SMALL("raw MLP"),
253     .read_header    = ff_raw_audio_read_header,
254     .read_packet    = ff_raw_read_partial_packet,
255     .flags= AVFMT_GENERIC_INDEX,
256     .extensions = "mlp",
257     .value = CODEC_ID_MLP,
258 };
259 #endif
260
261 #if CONFIG_TRUEHD_DEMUXER
262 AVInputFormat ff_truehd_demuxer = {
263     .name           = "truehd",
264     .long_name      = NULL_IF_CONFIG_SMALL("raw TrueHD"),
265     .read_header    = ff_raw_audio_read_header,
266     .read_packet    = 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     .name           = "shn",
276     .long_name      = NULL_IF_CONFIG_SMALL("raw Shorten"),
277     .read_header    = ff_raw_audio_read_header,
278     .read_packet    = 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