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
;
74 enum PixelFormat pix_fmt
;
77 av_set_pts_info(st
, 64, ap
->time_base
.num
, ap
->time_base
.den
);
79 av_set_pts_info(st
, 64, 1, 25);
80 if (s1
->video_size
&& (ret
= av_parse_video_size(&width
, &height
, s1
->video_size
)) < 0) {
81 av_log(s
, AV_LOG_ERROR
, "Couldn't parse video size.\n");
84 if ((pix_fmt
= av_get_pix_fmt(s1
->pixel_format
)) == PIX_FMT_NONE
) {
85 av_log(s
, AV_LOG_ERROR
, "No such pixel format: %s.\n", s1
->pixel_format
);
86 ret
= AVERROR(EINVAL
);
89 #if FF_API_FORMAT_PARAMETERS
95 pix_fmt
= ap
->pix_fmt
;
97 st
->codec
->width
= width
;
98 st
->codec
->height
= height
;
99 st
->codec
->pix_fmt
= pix_fmt
;
101 av_freep(&s1
->video_size
);
102 av_freep(&s1
->pixel_format
);
111 #define RAW_PACKET_SIZE 1024
113 int ff_raw_read_partial_packet(AVFormatContext
*s
, AVPacket
*pkt
)
117 size
= RAW_PACKET_SIZE
;
119 if (av_new_packet(pkt
, size
) < 0)
120 return AVERROR(ENOMEM
);
122 pkt
->pos
= avio_tell(s
->pb
);
123 pkt
->stream_index
= 0;
124 ret
= ffio_read_partial(s
->pb
, pkt
->data
, size
);
133 int ff_raw_audio_read_header(AVFormatContext
*s
,
134 AVFormatParameters
*ap
)
136 AVStream
*st
= av_new_stream(s
, 0);
138 return AVERROR(ENOMEM
);
139 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
140 st
->codec
->codec_id
= s
->iformat
->value
;
141 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
142 /* the parameters will be extracted from the compressed bitstream */
147 /* MPEG-1/H.263 input */
148 int ff_raw_video_read_header(AVFormatContext
*s
,
149 AVFormatParameters
*ap
)
153 st
= av_new_stream(s
, 0);
155 return AVERROR(ENOMEM
);
157 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
158 st
->codec
->codec_id
= s
->iformat
->value
;
159 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
161 /* for MJPEG, specify frame rate */
162 /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
163 if (ap
->time_base
.num
) {
164 st
->codec
->time_base
= ap
->time_base
;
165 } else if ( st
->codec
->codec_id
== CODEC_ID_MJPEG
||
166 st
->codec
->codec_id
== CODEC_ID_MPEG4
||
167 st
->codec
->codec_id
== CODEC_ID_DIRAC
||
168 st
->codec
->codec_id
== CODEC_ID_DNXHD
||
169 st
->codec
->codec_id
== CODEC_ID_VC1
||
170 st
->codec
->codec_id
== CODEC_ID_H264
) {
171 st
->codec
->time_base
= (AVRational
){1,25};
173 av_set_pts_info(st
, 64, 1, 1200000);
178 /* Note: Do not forget to add new entries to the Makefile as well. */
180 static const AVOption audio_options
[] = {
181 { "sample_rate", "", offsetof(RawAudioDemuxerContext
, sample_rate
), FF_OPT_TYPE_INT
, {.dbl
= 0}, 0, INT_MAX
, AV_OPT_FLAG_DECODING_PARAM
},
182 { "channels", "", offsetof(RawAudioDemuxerContext
, channels
), FF_OPT_TYPE_INT
, {.dbl
= 0}, 0, INT_MAX
, AV_OPT_FLAG_DECODING_PARAM
},
186 const AVClass ff_rawaudio_demuxer_class
= {
187 .class_name
= "rawaudio demuxer",
188 .item_name
= av_default_item_name
,
189 .option
= audio_options
,
190 .version
= LIBAVUTIL_VERSION_INT
,
193 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
194 #define DEC AV_OPT_FLAG_DECODING_PARAM
195 static const AVOption video_options
[] = {
196 { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size
), FF_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, DEC
},
197 { "pixel_format", "", OFFSET(pixel_format
), FF_OPT_TYPE_STRING
, {.str
= "yuv420p"}, 0, 0, DEC
},
203 const AVClass ff_rawvideo_demuxer_class
= {
204 .class_name
= "rawvideo demuxer",
205 .item_name
= av_default_item_name
,
206 .option
= video_options
,
207 .version
= LIBAVUTIL_VERSION_INT
,
210 #if CONFIG_G722_DEMUXER
211 AVInputFormat ff_g722_demuxer
= {
213 NULL_IF_CONFIG_SMALL("raw G.722"),
214 sizeof(RawAudioDemuxerContext
),
217 ff_raw_read_partial_packet
,
218 .flags
= AVFMT_GENERIC_INDEX
,
219 .extensions
= "g722,722",
220 .value
= CODEC_ID_ADPCM_G722
,
221 .priv_class
= &ff_rawaudio_demuxer_class
,
225 #if CONFIG_GSM_DEMUXER
226 AVInputFormat ff_gsm_demuxer
= {
228 NULL_IF_CONFIG_SMALL("raw GSM"),
231 ff_raw_audio_read_header
,
232 ff_raw_read_partial_packet
,
233 .flags
= AVFMT_GENERIC_INDEX
,
235 .value
= CODEC_ID_GSM
,
239 #if CONFIG_MJPEG_DEMUXER
240 FF_DEF_RAWVIDEO_DEMUXER(mjpeg
, "raw MJPEG video", NULL
, "mjpg,mjpeg", CODEC_ID_MJPEG
)
243 #if CONFIG_MLP_DEMUXER
244 AVInputFormat ff_mlp_demuxer
= {
246 NULL_IF_CONFIG_SMALL("raw MLP"),
249 ff_raw_audio_read_header
,
250 ff_raw_read_partial_packet
,
251 .flags
= AVFMT_GENERIC_INDEX
,
253 .value
= CODEC_ID_MLP
,
257 #if CONFIG_TRUEHD_DEMUXER
258 AVInputFormat ff_truehd_demuxer
= {
260 NULL_IF_CONFIG_SMALL("raw TrueHD"),
263 ff_raw_audio_read_header
,
264 ff_raw_read_partial_packet
,
265 .flags
= AVFMT_GENERIC_INDEX
,
267 .value
= CODEC_ID_TRUEHD
,
271 #if CONFIG_SHORTEN_DEMUXER
272 AVInputFormat ff_shorten_demuxer
= {
274 NULL_IF_CONFIG_SMALL("raw Shorten"),
277 ff_raw_audio_read_header
,
278 ff_raw_read_partial_packet
,
279 .flags
= AVFMT_GENERIC_INDEX
,
281 .value
= CODEC_ID_SHORTEN
,
285 #if CONFIG_VC1_DEMUXER
286 FF_DEF_RAWVIDEO_DEMUXER(vc1
, "raw VC-1", NULL
, "vc1", CODEC_ID_VC1
)