]> 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 #include "libavutil/avassert.h"
31
32 /* raw input */
33 int ff_raw_read_header(AVFormatContext *s)
34 {
35     AVStream *st;
36     enum AVCodecID id;
37
38     st = avformat_new_stream(s, NULL);
39     if (!st)
40         return AVERROR(ENOMEM);
41
42         id = s->iformat->raw_codec_id;
43         if (id == AV_CODEC_ID_RAWVIDEO) {
44             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
45         } else {
46             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
47         }
48         st->codec->codec_id = id;
49
50         switch(st->codec->codec_type) {
51         case AVMEDIA_TYPE_AUDIO: {
52             RawAudioDemuxerContext *s1 = s->priv_data;
53
54             st->codec->channels = 1;
55
56             if (id == AV_CODEC_ID_ADPCM_G722)
57                 st->codec->sample_rate = 16000;
58
59             if (s1 && s1->sample_rate)
60                 st->codec->sample_rate = s1->sample_rate;
61             if (st->codec->sample_rate <= 0) {
62                 av_log(s, AV_LOG_WARNING, "Invalid sample rate %d specified using default of 44100\n",
63                        st->codec->sample_rate);
64                 st->codec->sample_rate= 44100;
65             }
66
67             if (s1 && s1->channels)
68                 st->codec->channels    = s1->channels;
69
70             st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
71             av_assert0(st->codec->bits_per_coded_sample > 0);
72             st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
73             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
74             break;
75             }
76         case AVMEDIA_TYPE_VIDEO: {
77             FFRawVideoDemuxerContext *s1 = s->priv_data;
78             int width = 0, height = 0, ret = 0;
79             enum PixelFormat pix_fmt;
80             AVRational framerate;
81
82             if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
83                 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
84                 goto fail;
85             }
86             if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
87                 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
88                 ret = AVERROR(EINVAL);
89                 goto fail;
90             }
91             if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
92                 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
93                 goto fail;
94             }
95             avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
96             st->codec->width  = width;
97             st->codec->height = height;
98             st->codec->pix_fmt = pix_fmt;
99 fail:
100             return ret;
101             }
102         default:
103             return -1;
104         }
105     return 0;
106 }
107
108 #define RAW_PACKET_SIZE 1024
109
110 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
111 {
112     int ret, size;
113
114     size = RAW_PACKET_SIZE;
115
116     if (av_new_packet(pkt, size) < 0)
117         return AVERROR(ENOMEM);
118
119     pkt->pos= avio_tell(s->pb);
120     pkt->stream_index = 0;
121     ret = ffio_read_partial(s->pb, pkt->data, size);
122     if (ret < 0) {
123         av_free_packet(pkt);
124         return ret;
125     }
126     av_shrink_packet(pkt, ret);
127     return ret;
128 }
129
130 int ff_raw_audio_read_header(AVFormatContext *s)
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->raw_codec_id;
137     st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
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 {
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->raw_codec_id;
161     st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
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 = av_inv_q(framerate);
169     avpriv_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     .raw_codec_id   = AV_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     .raw_codec_id   = AV_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,mpo", AV_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     .raw_codec_id   = AV_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     .raw_codec_id   = AV_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     .raw_codec_id   = AV_CODEC_ID_SHORTEN,
245 };
246 #endif
247
248 #if CONFIG_VC1_DEMUXER
249 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)
250 #endif