3 * Copyright (c) 2001 Fabrice Bellard
4 * Copyright (c) 2005 Alex Beregszaszi
6 * This file is part of FFmpeg.
8 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 = avformat_new_stream(s, NULL);
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 st->codec->channels = 1;
54 if (id == CODEC_ID_ADPCM_G722)
55 st->codec->sample_rate = 16000;
57 if (s1 && s1->sample_rate)
58 st->codec->sample_rate = s1->sample_rate;
59 if (st->codec->sample_rate <= 0) {
60 av_log(s, AV_LOG_ERROR, "Invalid sample rate %d specified\n",
61 st->codec->sample_rate);
62 return AVERROR(EINVAL);
65 if (s1 && s1->channels)
66 st->codec->channels = s1->channels;
68 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
69 assert(st->codec->bits_per_coded_sample > 0);
70 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
71 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
74 case AVMEDIA_TYPE_VIDEO: {
75 FFRawVideoDemuxerContext *s1 = s->priv_data;
76 int width = 0, height = 0, ret = 0;
77 enum PixelFormat pix_fmt;
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 ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
90 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
93 av_set_pts_info(st, 64, framerate.den, framerate.num);
94 st->codec->width = width;
95 st->codec->height = height;
96 st->codec->pix_fmt = pix_fmt;
106 #define RAW_PACKET_SIZE 1024
108 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
112 size = RAW_PACKET_SIZE;
114 if (av_new_packet(pkt, size) < 0)
115 return AVERROR(ENOMEM);
117 pkt->pos= avio_tell(s->pb);
118 pkt->stream_index = 0;
119 ret = ffio_read_partial(s->pb, pkt->data, size);
128 int ff_raw_audio_read_header(AVFormatContext *s,
129 AVFormatParameters *ap)
131 AVStream *st = avformat_new_stream(s, NULL);
133 return AVERROR(ENOMEM);
134 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
135 st->codec->codec_id = s->iformat->value;
136 st->need_parsing = AVSTREAM_PARSE_FULL;
138 /* the parameters will be extracted from the compressed bitstream */
143 /* MPEG-1/H.263 input */
144 int ff_raw_video_read_header(AVFormatContext *s,
145 AVFormatParameters *ap)
148 FFRawVideoDemuxerContext *s1 = s->priv_data;
149 AVRational framerate;
153 st = avformat_new_stream(s, NULL);
155 ret = AVERROR(ENOMEM);
159 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
160 st->codec->codec_id = s->iformat->value;
161 st->need_parsing = AVSTREAM_PARSE_FULL;
163 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
164 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
168 st->codec->time_base = (AVRational){framerate.den, framerate.num};
169 av_set_pts_info(st, 64, 1, 1200000);
175 /* Note: Do not forget to add new entries to the Makefile as well. */
177 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
178 #define DEC AV_OPT_FLAG_DECODING_PARAM
179 const AVOption ff_rawvideo_options[] = {
180 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
184 #if CONFIG_G722_DEMUXER
185 AVInputFormat ff_g722_demuxer = {
187 .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
188 .read_header = ff_raw_read_header,
189 .read_packet = ff_raw_read_partial_packet,
190 .flags= AVFMT_GENERIC_INDEX,
191 .extensions = "g722,722",
192 .value = CODEC_ID_ADPCM_G722,
196 #if CONFIG_LATM_DEMUXER
197 AVInputFormat ff_latm_demuxer = {
199 .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
200 .read_header = ff_raw_audio_read_header,
201 .read_packet = ff_raw_read_partial_packet,
202 .flags= AVFMT_GENERIC_INDEX,
203 .extensions = "latm",
204 .value = CODEC_ID_AAC_LATM,
208 #if CONFIG_MJPEG_DEMUXER
209 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", CODEC_ID_MJPEG)
212 #if CONFIG_MLP_DEMUXER
213 AVInputFormat ff_mlp_demuxer = {
215 .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
216 .read_header = ff_raw_audio_read_header,
217 .read_packet = ff_raw_read_partial_packet,
218 .flags= AVFMT_GENERIC_INDEX,
220 .value = CODEC_ID_MLP,
224 #if CONFIG_TRUEHD_DEMUXER
225 AVInputFormat ff_truehd_demuxer = {
227 .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
228 .read_header = ff_raw_audio_read_header,
229 .read_packet = ff_raw_read_partial_packet,
230 .flags= AVFMT_GENERIC_INDEX,
232 .value = CODEC_ID_TRUEHD,
236 #if CONFIG_SHORTEN_DEMUXER
237 AVInputFormat ff_shorten_demuxer = {
239 .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
240 .read_header = ff_raw_audio_read_header,
241 .read_packet = ff_raw_read_partial_packet,
242 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
244 .value = CODEC_ID_SHORTEN,
248 #if CONFIG_VC1_DEMUXER
249 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)