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
25 #include "avio_internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/pixdesc.h"
32 int ff_raw_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
37 st
= avformat_new_stream(s
, NULL
);
39 return AVERROR(ENOMEM
);
41 id
= s
->iformat
->value
;
42 if (id
== CODEC_ID_RAWVIDEO
) {
43 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
45 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
47 st
->codec
->codec_id
= id
;
49 switch(st
->codec
->codec_type
) {
50 case AVMEDIA_TYPE_AUDIO
: {
51 RawAudioDemuxerContext
*s1
= s
->priv_data
;
53 st
->codec
->channels
= 1;
55 if (id
== CODEC_ID_ADPCM_G722
)
56 st
->codec
->sample_rate
= 16000;
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
;
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
);
69 case AVMEDIA_TYPE_VIDEO
: {
70 FFRawVideoDemuxerContext
*s1
= s
->priv_data
;
71 int width
= 0, height
= 0, ret
= 0;
72 enum PixelFormat pix_fmt
;
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");
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
);
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
);
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
;
101 #define RAW_PACKET_SIZE 1024
103 int ff_raw_read_partial_packet(AVFormatContext
*s
, AVPacket
*pkt
)
107 size
= RAW_PACKET_SIZE
;
109 if (av_new_packet(pkt
, size
) < 0)
110 return AVERROR(ENOMEM
);
112 pkt
->pos
= avio_tell(s
->pb
);
113 pkt
->stream_index
= 0;
114 ret
= ffio_read_partial(s
->pb
, pkt
->data
, size
);
123 int ff_raw_audio_read_header(AVFormatContext
*s
,
124 AVFormatParameters
*ap
)
126 AVStream
*st
= avformat_new_stream(s
, NULL
);
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 /* the parameters will be extracted from the compressed bitstream */
137 /* MPEG-1/H.263 input */
138 int ff_raw_video_read_header(AVFormatContext
*s
,
139 AVFormatParameters
*ap
)
142 FFRawVideoDemuxerContext
*s1
= s
->priv_data
;
143 AVRational framerate
;
147 st
= avformat_new_stream(s
, NULL
);
149 ret
= AVERROR(ENOMEM
);
153 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
154 st
->codec
->codec_id
= s
->iformat
->value
;
155 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
157 if ((ret
= av_parse_video_rate(&framerate
, s1
->framerate
)) < 0) {
158 av_log(s
, AV_LOG_ERROR
, "Could not parse framerate: %s.\n", s1
->framerate
);
162 st
->r_frame_rate
= st
->avg_frame_rate
= framerate
;
163 avpriv_set_pts_info(st
, 64, 1, 1200000);
169 /* Note: Do not forget to add new entries to the Makefile as well. */
171 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
172 #define DEC AV_OPT_FLAG_DECODING_PARAM
173 const AVOption ff_rawvideo_options
[] = {
174 { "framerate", "", OFFSET(framerate
), AV_OPT_TYPE_STRING
, {.str
= "25"}, 0, 0, DEC
},
178 #if CONFIG_G722_DEMUXER
179 AVInputFormat ff_g722_demuxer
= {
181 .long_name
= NULL_IF_CONFIG_SMALL("raw G.722"),
182 .read_header
= ff_raw_read_header
,
183 .read_packet
= ff_raw_read_partial_packet
,
184 .flags
= AVFMT_GENERIC_INDEX
,
185 .extensions
= "g722,722",
186 .value
= CODEC_ID_ADPCM_G722
,
190 #if CONFIG_LATM_DEMUXER
191 AVInputFormat ff_latm_demuxer
= {
193 .long_name
= NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
194 .read_header
= ff_raw_audio_read_header
,
195 .read_packet
= ff_raw_read_partial_packet
,
196 .flags
= AVFMT_GENERIC_INDEX
,
197 .extensions
= "latm",
198 .value
= CODEC_ID_AAC_LATM
,
202 #if CONFIG_MJPEG_DEMUXER
203 FF_DEF_RAWVIDEO_DEMUXER(mjpeg
, "raw MJPEG video", NULL
, "mjpg,mjpeg", CODEC_ID_MJPEG
)
206 #if CONFIG_MLP_DEMUXER
207 AVInputFormat ff_mlp_demuxer
= {
209 .long_name
= NULL_IF_CONFIG_SMALL("raw MLP"),
210 .read_header
= ff_raw_audio_read_header
,
211 .read_packet
= ff_raw_read_partial_packet
,
212 .flags
= AVFMT_GENERIC_INDEX
,
214 .value
= CODEC_ID_MLP
,
218 #if CONFIG_TRUEHD_DEMUXER
219 AVInputFormat ff_truehd_demuxer
= {
221 .long_name
= NULL_IF_CONFIG_SMALL("raw TrueHD"),
222 .read_header
= ff_raw_audio_read_header
,
223 .read_packet
= ff_raw_read_partial_packet
,
224 .flags
= AVFMT_GENERIC_INDEX
,
226 .value
= CODEC_ID_TRUEHD
,
230 #if CONFIG_SHORTEN_DEMUXER
231 AVInputFormat ff_shorten_demuxer
= {
233 .long_name
= NULL_IF_CONFIG_SMALL("raw Shorten"),
234 .read_header
= ff_raw_audio_read_header
,
235 .read_packet
= ff_raw_read_partial_packet
,
236 .flags
= AVFMT_NOBINSEARCH
| AVFMT_NOGENSEARCH
| AVFMT_NO_BYTE_SEEK
,
238 .value
= CODEC_ID_SHORTEN
,
242 #if CONFIG_VC1_DEMUXER
243 FF_DEF_RAWVIDEO_DEMUXER(vc1
, "raw VC-1", NULL
, "vc1", CODEC_ID_VC1
)