]> 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 = avformat_new_stream(s, NULL);
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 (id == CODEC_ID_ADPCM_G722)
55                 st->codec->sample_rate = 16000;
56
57             if (s1 && s1->sample_rate)
58                 st->codec->sample_rate = s1->sample_rate;
59             if (st->codec->sample_rate <= 0) {
60                 av_log(s, AV_LOG_ERROR, "Invalid sample rate %d specified\n",
61                        st->codec->sample_rate);
62                 return AVERROR(EINVAL);
63             }
64
65             if (s1 && s1->channels)
66                 st->codec->channels    = s1->channels;
67
68             st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
69             assert(st->codec->bits_per_coded_sample > 0);
70             st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
71             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
72             break;
73             }
74         case AVMEDIA_TYPE_VIDEO: {
75             FFRawVideoDemuxerContext *s1 = s->priv_data;
76             int width = 0, height = 0, ret = 0;
77             enum PixelFormat pix_fmt;
78             AVRational framerate;
79
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 ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
90                 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
91                 goto fail;
92             }
93             av_set_pts_info(st, 64, framerate.den, framerate.num);
94             st->codec->width  = width;
95             st->codec->height = height;
96             st->codec->pix_fmt = pix_fmt;
97 fail:
98             return ret;
99             }
100         default:
101             return -1;
102         }
103     return 0;
104 }
105
106 #define RAW_PACKET_SIZE 1024
107
108 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
109 {
110     int ret, size;
111
112     size = RAW_PACKET_SIZE;
113
114     if (av_new_packet(pkt, size) < 0)
115         return AVERROR(ENOMEM);
116
117     pkt->pos= avio_tell(s->pb);
118     pkt->stream_index = 0;
119     ret = ffio_read_partial(s->pb, pkt->data, size);
120     if (ret < 0) {
121         av_free_packet(pkt);
122         return ret;
123     }
124     pkt->size = ret;
125     return ret;
126 }
127
128 int ff_raw_audio_read_header(AVFormatContext *s,
129                              AVFormatParameters *ap)
130 {
131     AVStream *st = avformat_new_stream(s, NULL);
132     if (!st)
133         return AVERROR(ENOMEM);
134     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
135     st->codec->codec_id = s->iformat->value;
136     st->need_parsing = AVSTREAM_PARSE_FULL;
137     st->start_time = 0;
138     /* the parameters will be extracted from the compressed bitstream */
139
140     return 0;
141 }
142
143 /* MPEG-1/H.263 input */
144 int ff_raw_video_read_header(AVFormatContext *s,
145                              AVFormatParameters *ap)
146 {
147     AVStream *st;
148     FFRawVideoDemuxerContext *s1 = s->priv_data;
149     AVRational framerate;
150     int ret = 0;
151
152
153     st = avformat_new_stream(s, NULL);
154     if (!st) {
155         ret = AVERROR(ENOMEM);
156         goto fail;
157     }
158
159     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
160     st->codec->codec_id = s->iformat->value;
161     st->need_parsing = AVSTREAM_PARSE_FULL;
162
163     if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
164         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
165         goto fail;
166     }
167
168     st->codec->time_base = (AVRational){framerate.den, framerate.num};
169     av_set_pts_info(st, 64, 1, 1200000);
170
171 fail:
172     return ret;
173 }
174
175 /* Note: Do not forget to add new entries to the Makefile as well. */
176
177 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
178 #define DEC AV_OPT_FLAG_DECODING_PARAM
179 const AVOption ff_rawvideo_options[] = {
180     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
181     { NULL },
182 };
183
184 #if CONFIG_G722_DEMUXER
185 AVInputFormat ff_g722_demuxer = {
186     .name           = "g722",
187     .long_name      = NULL_IF_CONFIG_SMALL("raw G.722"),
188     .read_header    = ff_raw_read_header,
189     .read_packet    = ff_raw_read_partial_packet,
190     .flags= AVFMT_GENERIC_INDEX,
191     .extensions = "g722,722",
192     .value = CODEC_ID_ADPCM_G722,
193 };
194 #endif
195
196 #if CONFIG_LATM_DEMUXER
197 AVInputFormat ff_latm_demuxer = {
198     .name           = "latm",
199     .long_name      = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
200     .read_header    = ff_raw_audio_read_header,
201     .read_packet    = ff_raw_read_partial_packet,
202     .flags= AVFMT_GENERIC_INDEX,
203     .extensions = "latm",
204     .value = CODEC_ID_AAC_LATM,
205 };
206 #endif
207
208 #if CONFIG_MJPEG_DEMUXER
209 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
210 #endif
211
212 #if CONFIG_MLP_DEMUXER
213 AVInputFormat ff_mlp_demuxer = {
214     .name           = "mlp",
215     .long_name      = NULL_IF_CONFIG_SMALL("raw MLP"),
216     .read_header    = ff_raw_audio_read_header,
217     .read_packet    = ff_raw_read_partial_packet,
218     .flags= AVFMT_GENERIC_INDEX,
219     .extensions = "mlp",
220     .value = CODEC_ID_MLP,
221 };
222 #endif
223
224 #if CONFIG_TRUEHD_DEMUXER
225 AVInputFormat ff_truehd_demuxer = {
226     .name           = "truehd",
227     .long_name      = NULL_IF_CONFIG_SMALL("raw TrueHD"),
228     .read_header    = ff_raw_audio_read_header,
229     .read_packet    = ff_raw_read_partial_packet,
230     .flags= AVFMT_GENERIC_INDEX,
231     .extensions = "thd",
232     .value = CODEC_ID_TRUEHD,
233 };
234 #endif
235
236 #if CONFIG_SHORTEN_DEMUXER
237 AVInputFormat ff_shorten_demuxer = {
238     .name           = "shn",
239     .long_name      = NULL_IF_CONFIG_SMALL("raw Shorten"),
240     .read_header    = ff_raw_audio_read_header,
241     .read_packet    = ff_raw_read_partial_packet,
242     .flags          = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
243     .extensions = "shn",
244     .value = CODEC_ID_SHORTEN,
245 };
246 #endif
247
248 #if CONFIG_VC1_DEMUXER
249 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
250 #endif