]> 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)
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     av_shrink_packet(pkt, ret);
126     return ret;
127 }
128
129 int ff_raw_audio_read_header(AVFormatContext *s)
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 {
146     AVStream *st;
147     FFRawVideoDemuxerContext *s1 = s->priv_data;
148     AVRational framerate;
149     int ret = 0;
150
151
152     st = avformat_new_stream(s, NULL);
153     if (!st) {
154         ret = AVERROR(ENOMEM);
155         goto fail;
156     }
157
158     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
159     st->codec->codec_id = s->iformat->value;
160     st->need_parsing = AVSTREAM_PARSE_FULL;
161
162     if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
163         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
164         goto fail;
165     }
166
167     st->codec->time_base = (AVRational){framerate.den, framerate.num};
168     avpriv_set_pts_info(st, 64, 1, 1200000);
169
170 fail:
171     return ret;
172 }
173
174 /* Note: Do not forget to add new entries to the Makefile as well. */
175
176 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
177 #define DEC AV_OPT_FLAG_DECODING_PARAM
178 const AVOption ff_rawvideo_options[] = {
179     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
180     { NULL },
181 };
182
183 #if CONFIG_G722_DEMUXER
184 AVInputFormat ff_g722_demuxer = {
185     .name           = "g722",
186     .long_name      = NULL_IF_CONFIG_SMALL("raw G.722"),
187     .read_header    = ff_raw_read_header,
188     .read_packet    = ff_raw_read_partial_packet,
189     .flags= AVFMT_GENERIC_INDEX,
190     .extensions = "g722,722",
191     .value = CODEC_ID_ADPCM_G722,
192 };
193 #endif
194
195 #if CONFIG_LATM_DEMUXER
196 AVInputFormat ff_latm_demuxer = {
197     .name           = "latm",
198     .long_name      = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
199     .read_header    = ff_raw_audio_read_header,
200     .read_packet    = ff_raw_read_partial_packet,
201     .flags= AVFMT_GENERIC_INDEX,
202     .extensions = "latm",
203     .value = CODEC_ID_AAC_LATM,
204 };
205 #endif
206
207 #if CONFIG_MJPEG_DEMUXER
208 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", CODEC_ID_MJPEG)
209 #endif
210
211 #if CONFIG_MLP_DEMUXER
212 AVInputFormat ff_mlp_demuxer = {
213     .name           = "mlp",
214     .long_name      = NULL_IF_CONFIG_SMALL("raw MLP"),
215     .read_header    = ff_raw_audio_read_header,
216     .read_packet    = ff_raw_read_partial_packet,
217     .flags= AVFMT_GENERIC_INDEX,
218     .extensions = "mlp",
219     .value = CODEC_ID_MLP,
220 };
221 #endif
222
223 #if CONFIG_TRUEHD_DEMUXER
224 AVInputFormat ff_truehd_demuxer = {
225     .name           = "truehd",
226     .long_name      = NULL_IF_CONFIG_SMALL("raw TrueHD"),
227     .read_header    = ff_raw_audio_read_header,
228     .read_packet    = ff_raw_read_partial_packet,
229     .flags= AVFMT_GENERIC_INDEX,
230     .extensions = "thd",
231     .value = CODEC_ID_TRUEHD,
232 };
233 #endif
234
235 #if CONFIG_SHORTEN_DEMUXER
236 AVInputFormat ff_shorten_demuxer = {
237     .name           = "shn",
238     .long_name      = NULL_IF_CONFIG_SMALL("raw Shorten"),
239     .read_header    = ff_raw_audio_read_header,
240     .read_packet    = ff_raw_read_partial_packet,
241     .flags          = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
242     .extensions = "shn",
243     .value = CODEC_ID_SHORTEN,
244 };
245 #endif
246
247 #if CONFIG_VC1_DEMUXER
248 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
249 #endif