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"
30 #include "libavutil/avassert.h"
33 int ff_raw_read_header(AVFormatContext *s)
38 st = avformat_new_stream(s, NULL);
40 return AVERROR(ENOMEM);
42 id = s->iformat->raw_codec_id;
43 if (id == AV_CODEC_ID_RAWVIDEO) {
44 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
46 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
48 st->codec->codec_id = id;
50 switch(st->codec->codec_type) {
51 case AVMEDIA_TYPE_AUDIO: {
52 RawAudioDemuxerContext *s1 = s->priv_data;
54 st->codec->channels = 1;
56 if (id == AV_CODEC_ID_ADPCM_G722)
57 st->codec->sample_rate = 16000;
59 if (s1 && s1->sample_rate)
60 st->codec->sample_rate = s1->sample_rate;
61 if (st->codec->sample_rate <= 0) {
62 av_log(s, AV_LOG_WARNING, "Invalid sample rate %d specified using default of 44100\n",
63 st->codec->sample_rate);
64 st->codec->sample_rate= 44100;
67 if (s1 && s1->channels)
68 st->codec->channels = s1->channels;
70 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
71 av_assert0(st->codec->bits_per_coded_sample > 0);
72 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
73 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
76 case AVMEDIA_TYPE_VIDEO: {
77 FFRawVideoDemuxerContext *s1 = s->priv_data;
78 int width = 0, height = 0, ret = 0;
79 enum PixelFormat pix_fmt;
82 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
83 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
86 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
87 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
88 ret = AVERROR(EINVAL);
91 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
92 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
95 avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
96 st->codec->width = width;
97 st->codec->height = height;
98 st->codec->pix_fmt = pix_fmt;
108 #define RAW_PACKET_SIZE 1024
110 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
114 size = RAW_PACKET_SIZE;
116 if (av_new_packet(pkt, size) < 0)
117 return AVERROR(ENOMEM);
119 pkt->pos= avio_tell(s->pb);
120 pkt->stream_index = 0;
121 ret = ffio_read_partial(s->pb, pkt->data, size);
126 av_shrink_packet(pkt, ret);
130 int ff_raw_audio_read_header(AVFormatContext *s)
132 AVStream *st = avformat_new_stream(s, NULL);
134 return AVERROR(ENOMEM);
135 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
136 st->codec->codec_id = s->iformat->raw_codec_id;
137 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
139 /* the parameters will be extracted from the compressed bitstream */
144 /* MPEG-1/H.263 input */
145 int ff_raw_video_read_header(AVFormatContext *s)
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->raw_codec_id;
161 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
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 = av_inv_q(framerate);
169 avpriv_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 .raw_codec_id = AV_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 .raw_codec_id = AV_CODEC_ID_AAC_LATM,
208 #if CONFIG_MJPEG_DEMUXER
209 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", AV_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 .raw_codec_id = AV_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 .raw_codec_id = AV_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 .raw_codec_id = AV_CODEC_ID_SHORTEN,
248 #if CONFIG_VC1_DEMUXER
249 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)