libx264: introduce slice-max-size option
[libav.git] / libavformat / rawdec.c
CommitLineData
115329f1 1/*
4ca31edc 2 * RAW demuxers
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
84c63c01 4 * Copyright (c) 2005 Alex Beregszaszi
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
19720f15
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
19720f15 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
245976da 22
de6d9b64 23#include "avformat.h"
b3db9cee 24#include "avio_internal.h"
4ca31edc 25#include "rawdec.h"
5b3865fc 26#include "libavutil/opt.h"
973f686a 27#include "libavutil/parseutils.h"
2a85f218 28#include "libavutil/pixdesc.h"
de6d9b64 29
de6d9b64 30/* raw input */
e94204df 31int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
de6d9b64
FB
32{
33 AVStream *st;
0f87b771 34 enum CodecID id;
de6d9b64 35
c9a65ca8 36 st = av_new_stream(s, 0);
de6d9b64 37 if (!st)
769e10f0 38 return AVERROR(ENOMEM);
c04c3282 39
c9a65ca8
FB
40 id = s->iformat->value;
41 if (id == CODEC_ID_RAWVIDEO) {
72415b2a 42 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
de6d9b64 43 } else {
72415b2a 44 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
de6d9b64 45 }
01f4895c 46 st->codec->codec_id = id;
c9a65ca8 47
01f4895c 48 switch(st->codec->codec_type) {
5b3865fc
AK
49 case AVMEDIA_TYPE_AUDIO: {
50 RawAudioDemuxerContext *s1 = s->priv_data;
51
c14fe6bc 52 st->codec->channels = 1;
5b3865fc
AK
53
54 if (s1->sample_rate)
55 st->codec->sample_rate = s1->sample_rate;
56 if (s1->channels)
57 st->codec->channels = s1->channels;
58
a3d23e15
BC
59 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
60 assert(st->codec->bits_per_coded_sample > 0);
61 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
01f4895c 62 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
de6d9b64 63 break;
5b3865fc 64 }
973f686a
AK
65 case AVMEDIA_TYPE_VIDEO: {
66 FFRawVideoDemuxerContext *s1 = s->priv_data;
1f94c31f 67 int width = 0, height = 0, ret = 0;
2a85f218 68 enum PixelFormat pix_fmt;
e762b1ce 69 AVRational framerate;
2a85f218 70
2a85f218
AK
71 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
72 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
73 goto fail;
74 }
75 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
76 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
77 ret = AVERROR(EINVAL);
78 goto fail;
973f686a 79 }
e762b1ce
AK
80 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
81 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
82 goto fail;
83 }
e762b1ce 84 av_set_pts_info(st, 64, framerate.den, framerate.num);
973f686a
AK
85 st->codec->width = width;
86 st->codec->height = height;
2a85f218 87 st->codec->pix_fmt = pix_fmt;
2a85f218 88fail:
2a85f218 89 return ret;
973f686a 90 }
de6d9b64 91 default:
27e084bd 92 return -1;
de6d9b64 93 }
de6d9b64
FB
94 return 0;
95}
96
2e93e3aa 97#define RAW_PACKET_SIZE 1024
de6d9b64 98
81f052cb 99int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
e15dec10
LS
100{
101 int ret, size;
102
103 size = RAW_PACKET_SIZE;
104
105 if (av_new_packet(pkt, size) < 0)
c67031e7 106 return AVERROR(ENOMEM);
115329f1 107
a2704c97 108 pkt->pos= avio_tell(s->pb);
e15dec10 109 pkt->stream_index = 0;
b3db9cee 110 ret = ffio_read_partial(s->pb, pkt->data, size);
c3db0bc6 111 if (ret < 0) {
e15dec10 112 av_free_packet(pkt);
c3db0bc6 113 return ret;
e15dec10
LS
114 }
115 pkt->size = ret;
116 return ret;
117}
76d32428 118
6d0678d1 119int ff_raw_audio_read_header(AVFormatContext *s,
a0af2fa4 120 AVFormatParameters *ap)
fda885c7 121{
a0af2fa4 122 AVStream *st = av_new_stream(s, 0);
fda885c7 123 if (!st)
769e10f0 124 return AVERROR(ENOMEM);
72415b2a 125 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
a0af2fa4 126 st->codec->codec_id = s->iformat->value;
57004ff1 127 st->need_parsing = AVSTREAM_PARSE_FULL;
fda885c7 128 /* the parameters will be extracted from the compressed bitstream */
6cde949a 129
fda885c7
MR
130 return 0;
131}
132
fb9f1117 133/* MPEG-1/H.263 input */
b47a5a95 134int ff_raw_video_read_header(AVFormatContext *s,
de6d9b64
FB
135 AVFormatParameters *ap)
136{
137 AVStream *st;
e762b1ce
AK
138 FFRawVideoDemuxerContext *s1 = s->priv_data;
139 AVRational framerate;
140 int ret = 0;
141
de6d9b64 142
c9a65ca8 143 st = av_new_stream(s, 0);
e762b1ce
AK
144 if (!st) {
145 ret = AVERROR(ENOMEM);
146 goto fail;
147 }
de6d9b64 148
72415b2a 149 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c 150 st->codec->codec_id = s->iformat->value;
57004ff1 151 st->need_parsing = AVSTREAM_PARSE_FULL;
4986a429 152
e762b1ce
AK
153 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
154 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
155 goto fail;
27e084bd 156 }
e762b1ce
AK
157
158 st->codec->time_base = (AVRational){framerate.den, framerate.num};
4022fe01 159 av_set_pts_info(st, 64, 1, 1200000);
80ce3254 160
e762b1ce 161fail:
e762b1ce 162 return ret;
de6d9b64
FB
163}
164
900eb63d
DB
165/* Note: Do not forget to add new entries to the Makefile as well. */
166
5b3865fc
AK
167static const AVOption audio_options[] = {
168 { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
169 { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
170 { NULL },
171};
172
173const AVClass ff_rawaudio_demuxer_class = {
174 .class_name = "rawaudio demuxer",
175 .item_name = av_default_item_name,
176 .option = audio_options,
177 .version = LIBAVUTIL_VERSION_INT,
178};
179
973f686a
AK
180#define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
181#define DEC AV_OPT_FLAG_DECODING_PARAM
182static const AVOption video_options[] = {
183 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
2a85f218 184 { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
e762b1ce 185 { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
973f686a
AK
186 { NULL },
187};
188#undef OFFSET
189#undef DEC
190
191const AVClass ff_rawvideo_demuxer_class = {
192 .class_name = "rawvideo demuxer",
193 .item_name = av_default_item_name,
194 .option = video_options,
195 .version = LIBAVUTIL_VERSION_INT,
196};
197
9013560f 198#if CONFIG_G722_DEMUXER
c6610a21 199AVInputFormat ff_g722_demuxer = {
dfc2c4d9
AK
200 .name = "g722",
201 .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
202 .priv_data_size = sizeof(RawAudioDemuxerContext),
203 .read_header = ff_raw_read_header,
204 .read_packet = ff_raw_read_partial_packet,
9013560f
MS
205 .flags= AVFMT_GENERIC_INDEX,
206 .extensions = "g722,722",
207 .value = CODEC_ID_ADPCM_G722,
5b3865fc 208 .priv_class = &ff_rawaudio_demuxer_class,
9013560f
MS
209};
210#endif
211
b250f9c6 212#if CONFIG_GSM_DEMUXER
c6610a21 213AVInputFormat ff_gsm_demuxer = {
dfc2c4d9
AK
214 .name = "gsm",
215 .long_name = NULL_IF_CONFIG_SMALL("raw GSM"),
216 .read_header = ff_raw_audio_read_header,
217 .read_packet = ff_raw_read_partial_packet,
60711e95
MN
218 .flags= AVFMT_GENERIC_INDEX,
219 .extensions = "gsm",
220 .value = CODEC_ID_GSM,
221};
7402ee23 222#endif
60711e95 223
b250f9c6 224#if CONFIG_MJPEG_DEMUXER
fa4924a3 225FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
7402ee23 226#endif
0da71265 227
b250f9c6 228#if CONFIG_MLP_DEMUXER
c6610a21 229AVInputFormat ff_mlp_demuxer = {
dfc2c4d9
AK
230 .name = "mlp",
231 .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
232 .read_header = ff_raw_audio_read_header,
233 .read_packet = ff_raw_read_partial_packet,
e9b78eeb 234 .flags= AVFMT_GENERIC_INDEX,
76d32428
DB
235 .extensions = "mlp",
236 .value = CODEC_ID_MLP,
de6d9b64 237};
7402ee23 238#endif
de6d9b64 239
23d9cc45 240#if CONFIG_TRUEHD_DEMUXER
c6610a21 241AVInputFormat ff_truehd_demuxer = {
dfc2c4d9
AK
242 .name = "truehd",
243 .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
244 .read_header = ff_raw_audio_read_header,
245 .read_packet = ff_raw_read_partial_packet,
23d9cc45
RP
246 .flags= AVFMT_GENERIC_INDEX,
247 .extensions = "thd",
248 .value = CODEC_ID_TRUEHD,
249};
250#endif
251
b250f9c6 252#if CONFIG_SHORTEN_DEMUXER
c6610a21 253AVInputFormat ff_shorten_demuxer = {
dfc2c4d9
AK
254 .name = "shn",
255 .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
256 .read_header = ff_raw_audio_read_header,
257 .read_packet = ff_raw_read_partial_packet,
76d32428
DB
258 .flags= AVFMT_GENERIC_INDEX,
259 .extensions = "shn",
260 .value = CODEC_ID_SHORTEN,
261};
7402ee23 262#endif
76d32428 263
b250f9c6 264#if CONFIG_VC1_DEMUXER
fa4924a3 265FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
7402ee23 266#endif