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
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)
37 st = avformat_new_stream(s, NULL);
39 return AVERROR(ENOMEM);
41 id = s->iformat->raw_codec_id;
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 (st->codec->sample_rate <= 0) {
61 av_log(s, AV_LOG_WARNING, "Invalid sample rate %d specified using default of 44100\n",
62 st->codec->sample_rate);
63 st->codec->sample_rate= 44100;
66 if (s1 && s1->channels)
67 st->codec->channels = s1->channels;
69 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
70 assert(st->codec->bits_per_coded_sample > 0);
71 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
72 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
75 case AVMEDIA_TYPE_VIDEO: {
76 FFRawVideoDemuxerContext *s1 = s->priv_data;
77 int width = 0, height = 0, ret = 0;
78 enum PixelFormat pix_fmt;
81 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
82 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
85 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
86 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
87 ret = AVERROR(EINVAL);
90 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
91 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
94 avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
95 st->codec->width = width;
96 st->codec->height = height;
97 st->codec->pix_fmt = pix_fmt;
107 #define RAW_PACKET_SIZE 1024
109 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
113 size = RAW_PACKET_SIZE;
115 if (av_new_packet(pkt, size) < 0)
116 return AVERROR(ENOMEM);
118 pkt->pos= avio_tell(s->pb);
119 pkt->stream_index = 0;
120 ret = ffio_read_partial(s->pb, pkt->data, size);
125 av_shrink_packet(pkt, ret);
129 int ff_raw_audio_read_header(AVFormatContext *s)
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->raw_codec_id;
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)
147 FFRawVideoDemuxerContext *s1 = s->priv_data;
148 AVRational framerate;
152 st = avformat_new_stream(s, NULL);
154 ret = AVERROR(ENOMEM);
158 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
159 st->codec->codec_id = s->iformat->raw_codec_id;
160 st->need_parsing = AVSTREAM_PARSE_FULL;
162 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
163 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
167 st->codec->time_base = (AVRational){framerate.den, framerate.num};
168 avpriv_set_pts_info(st, 64, 1, 1200000);
174 /* Note: Do not forget to add new entries to the Makefile as well. */
176 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
177 #define DEC AV_OPT_FLAG_DECODING_PARAM
178 const AVOption ff_rawvideo_options[] = {
179 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
183 #if CONFIG_G722_DEMUXER
184 AVInputFormat ff_g722_demuxer = {
186 .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
187 .read_header = ff_raw_read_header,
188 .read_packet = ff_raw_read_partial_packet,
189 .flags= AVFMT_GENERIC_INDEX,
190 .extensions = "g722,722",
191 .raw_codec_id = CODEC_ID_ADPCM_G722,
195 #if CONFIG_LATM_DEMUXER
196 AVInputFormat ff_latm_demuxer = {
198 .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
199 .read_header = ff_raw_audio_read_header,
200 .read_packet = ff_raw_read_partial_packet,
201 .flags= AVFMT_GENERIC_INDEX,
202 .extensions = "latm",
203 .raw_codec_id = CODEC_ID_AAC_LATM,
207 #if CONFIG_MJPEG_DEMUXER
208 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", CODEC_ID_MJPEG)
211 #if CONFIG_MLP_DEMUXER
212 AVInputFormat ff_mlp_demuxer = {
214 .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
215 .read_header = ff_raw_audio_read_header,
216 .read_packet = ff_raw_read_partial_packet,
217 .flags= AVFMT_GENERIC_INDEX,
219 .raw_codec_id = CODEC_ID_MLP,
223 #if CONFIG_TRUEHD_DEMUXER
224 AVInputFormat ff_truehd_demuxer = {
226 .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
227 .read_header = ff_raw_audio_read_header,
228 .read_packet = ff_raw_read_partial_packet,
229 .flags= AVFMT_GENERIC_INDEX,
231 .raw_codec_id = CODEC_ID_TRUEHD,
235 #if CONFIG_SHORTEN_DEMUXER
236 AVInputFormat ff_shorten_demuxer = {
238 .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
239 .read_header = ff_raw_audio_read_header,
240 .read_packet = ff_raw_read_partial_packet,
241 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
243 .raw_codec_id = CODEC_ID_SHORTEN,
247 #if CONFIG_VC1_DEMUXER
248 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)