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