3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2005 Alex Beregszaszi
6 * This file is part of Libav.
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.
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.
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
24 #include "avio_internal.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/parseutils.h"
28 #include "libavutil/pixdesc.h"
31 int ff_raw_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
36 st
= av_new_stream(s
, 0);
38 return AVERROR(ENOMEM
);
40 id
= s
->iformat
->value
;
41 if (id
== CODEC_ID_RAWVIDEO
) {
42 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
44 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
46 st
->codec
->codec_id
= id
;
48 switch(st
->codec
->codec_type
) {
49 case AVMEDIA_TYPE_AUDIO
: {
50 RawAudioDemuxerContext
*s1
= s
->priv_data
;
52 #if FF_API_FORMAT_PARAMETERS
54 st
->codec
->sample_rate
= ap
->sample_rate
;
56 st
->codec
->channels
= ap
->channels
;
57 else st
->codec
->channels
= 1;
61 st
->codec
->sample_rate
= s1
->sample_rate
;
63 st
->codec
->channels
= s1
->channels
;
65 st
->codec
->bits_per_coded_sample
= av_get_bits_per_sample(st
->codec
->codec_id
);
66 assert(st
->codec
->bits_per_coded_sample
> 0);
67 st
->codec
->block_align
= st
->codec
->bits_per_coded_sample
*st
->codec
->channels
/8;
68 av_set_pts_info(st
, 64, 1, st
->codec
->sample_rate
);
71 case AVMEDIA_TYPE_VIDEO
: {
72 FFRawVideoDemuxerContext
*s1
= s
->priv_data
;
73 int width
= 0, height
= 0, ret
= 0;
74 enum PixelFormat pix_fmt
;
77 if (s1
->video_size
&& (ret
= av_parse_video_size(&width
, &height
, s1
->video_size
)) < 0) {
78 av_log(s
, AV_LOG_ERROR
, "Couldn't parse video size.\n");
81 if ((pix_fmt
= av_get_pix_fmt(s1
->pixel_format
)) == PIX_FMT_NONE
) {
82 av_log(s
, AV_LOG_ERROR
, "No such pixel format: %s.\n", s1
->pixel_format
);
83 ret
= AVERROR(EINVAL
);
86 if ((ret
= av_parse_video_rate(&framerate
, s1
->framerate
)) < 0) {
87 av_log(s
, AV_LOG_ERROR
, "Could not parse framerate: %s.\n", s1
->framerate
);
90 #if FF_API_FORMAT_PARAMETERS
96 pix_fmt
= ap
->pix_fmt
;
97 if (ap
->time_base
.num
)
98 framerate
= (AVRational
){ap
->time_base
.den
, ap
->time_base
.num
};
100 av_set_pts_info(st
, 64, framerate
.den
, framerate
.num
);
101 st
->codec
->width
= width
;
102 st
->codec
->height
= height
;
103 st
->codec
->pix_fmt
= pix_fmt
;
113 #define RAW_PACKET_SIZE 1024
115 int ff_raw_read_partial_packet(AVFormatContext
*s
, AVPacket
*pkt
)
119 size
= RAW_PACKET_SIZE
;
121 if (av_new_packet(pkt
, size
) < 0)
122 return AVERROR(ENOMEM
);
124 pkt
->pos
= avio_tell(s
->pb
);
125 pkt
->stream_index
= 0;
126 ret
= ffio_read_partial(s
->pb
, pkt
->data
, size
);
135 int ff_raw_audio_read_header(AVFormatContext
*s
,
136 AVFormatParameters
*ap
)
138 AVStream
*st
= av_new_stream(s
, 0);
140 return AVERROR(ENOMEM
);
141 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
142 st
->codec
->codec_id
= s
->iformat
->value
;
143 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
144 /* the parameters will be extracted from the compressed bitstream */
149 /* MPEG-1/H.263 input */
150 int ff_raw_video_read_header(AVFormatContext
*s
,
151 AVFormatParameters
*ap
)
154 FFRawVideoDemuxerContext
*s1
= s
->priv_data
;
155 AVRational framerate
;
159 st
= av_new_stream(s
, 0);
161 ret
= AVERROR(ENOMEM
);
165 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
166 st
->codec
->codec_id
= s
->iformat
->value
;
167 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
169 if ((ret
= av_parse_video_rate(&framerate
, s1
->framerate
)) < 0) {
170 av_log(s
, AV_LOG_ERROR
, "Could not parse framerate: %s.\n", s1
->framerate
);
173 #if FF_API_FORMAT_PARAMETERS
174 if (ap
->time_base
.num
)
175 framerate
= (AVRational
){ap
->time_base
.den
, ap
->time_base
.num
};
178 st
->codec
->time_base
= (AVRational
){framerate
.den
, framerate
.num
};
179 av_set_pts_info(st
, 64, 1, 1200000);
185 /* Note: Do not forget to add new entries to the Makefile as well. */
187 static const AVOption audio_options
[] = {
188 { "sample_rate", "", offsetof(RawAudioDemuxerContext
, sample_rate
), FF_OPT_TYPE_INT
, {.dbl
= 0}, 0, INT_MAX
, AV_OPT_FLAG_DECODING_PARAM
},
189 { "channels", "", offsetof(RawAudioDemuxerContext
, channels
), FF_OPT_TYPE_INT
, {.dbl
= 0}, 0, INT_MAX
, AV_OPT_FLAG_DECODING_PARAM
},
193 const AVClass ff_rawaudio_demuxer_class
= {
194 .class_name
= "rawaudio demuxer",
195 .item_name
= av_default_item_name
,
196 .option
= audio_options
,
197 .version
= LIBAVUTIL_VERSION_INT
,
200 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
201 #define DEC AV_OPT_FLAG_DECODING_PARAM
202 static const AVOption video_options
[] = {
203 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size
), FF_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, DEC
},
204 { "pixel_format", "", OFFSET(pixel_format
), FF_OPT_TYPE_STRING
, {.str
= "yuv420p"}, 0, 0, DEC
},
205 { "framerate", "", OFFSET(framerate
), FF_OPT_TYPE_STRING
, {.str
= "25"}, 0, 0, DEC
},
211 const AVClass ff_rawvideo_demuxer_class
= {
212 .class_name
= "rawvideo demuxer",
213 .item_name
= av_default_item_name
,
214 .option
= video_options
,
215 .version
= LIBAVUTIL_VERSION_INT
,
218 #if CONFIG_G722_DEMUXER
219 AVInputFormat ff_g722_demuxer
= {
221 .long_name
= NULL_IF_CONFIG_SMALL("raw G.722"),
222 .priv_data_size
= sizeof(RawAudioDemuxerContext
),
223 .read_header
= ff_raw_read_header
,
224 .read_packet
= ff_raw_read_partial_packet
,
225 .flags
= AVFMT_GENERIC_INDEX
,
226 .extensions
= "g722,722",
227 .value
= CODEC_ID_ADPCM_G722
,
228 .priv_class
= &ff_rawaudio_demuxer_class
,
232 #if CONFIG_GSM_DEMUXER
233 AVInputFormat ff_gsm_demuxer
= {
235 .long_name
= NULL_IF_CONFIG_SMALL("raw GSM"),
236 .read_header
= ff_raw_audio_read_header
,
237 .read_packet
= ff_raw_read_partial_packet
,
238 .flags
= AVFMT_GENERIC_INDEX
,
240 .value
= CODEC_ID_GSM
,
244 #if CONFIG_MJPEG_DEMUXER
245 FF_DEF_RAWVIDEO_DEMUXER(mjpeg
, "raw MJPEG video", NULL
, "mjpg,mjpeg", CODEC_ID_MJPEG
)
248 #if CONFIG_MLP_DEMUXER
249 AVInputFormat ff_mlp_demuxer
= {
251 .long_name
= NULL_IF_CONFIG_SMALL("raw MLP"),
252 .read_header
= ff_raw_audio_read_header
,
253 .read_packet
= ff_raw_read_partial_packet
,
254 .flags
= AVFMT_GENERIC_INDEX
,
256 .value
= CODEC_ID_MLP
,
260 #if CONFIG_TRUEHD_DEMUXER
261 AVInputFormat ff_truehd_demuxer
= {
263 .long_name
= NULL_IF_CONFIG_SMALL("raw TrueHD"),
264 .read_header
= ff_raw_audio_read_header
,
265 .read_packet
= ff_raw_read_partial_packet
,
266 .flags
= AVFMT_GENERIC_INDEX
,
268 .value
= CODEC_ID_TRUEHD
,
272 #if CONFIG_SHORTEN_DEMUXER
273 AVInputFormat ff_shorten_demuxer
= {
275 .long_name
= NULL_IF_CONFIG_SMALL("raw Shorten"),
276 .read_header
= ff_raw_audio_read_header
,
277 .read_packet
= ff_raw_read_partial_packet
,
278 .flags
= AVFMT_GENERIC_INDEX
,
280 .value
= CODEC_ID_SHORTEN
,
284 #if CONFIG_VC1_DEMUXER
285 FF_DEF_RAWVIDEO_DEMUXER(vc1
, "raw VC-1", NULL
, "vc1", CODEC_ID_VC1
)