OSDN Git Service

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