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