00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avformat.h"
00024 #include "internal.h"
00025 #include "avio_internal.h"
00026 #include "rawdec.h"
00027 #include "libavutil/opt.h"
00028 #include "libavutil/parseutils.h"
00029 #include "libavutil/pixdesc.h"
00030 #include "libavutil/avassert.h"
00031
00032
00033 int ff_raw_read_header(AVFormatContext *s)
00034 {
00035 AVStream *st;
00036 enum AVCodecID id;
00037
00038 st = avformat_new_stream(s, NULL);
00039 if (!st)
00040 return AVERROR(ENOMEM);
00041
00042 id = s->iformat->raw_codec_id;
00043 if (id == AV_CODEC_ID_RAWVIDEO) {
00044 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00045 } else {
00046 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00047 }
00048 st->codec->codec_id = id;
00049
00050 switch(st->codec->codec_type) {
00051 case AVMEDIA_TYPE_AUDIO: {
00052 RawAudioDemuxerContext *s1 = s->priv_data;
00053
00054 st->codec->channels = 1;
00055
00056 if (id == AV_CODEC_ID_ADPCM_G722)
00057 st->codec->sample_rate = 16000;
00058
00059 if (s1 && s1->sample_rate)
00060 st->codec->sample_rate = s1->sample_rate;
00061 if (st->codec->sample_rate <= 0) {
00062 av_log(s, AV_LOG_WARNING, "Invalid sample rate %d specified using default of 44100\n",
00063 st->codec->sample_rate);
00064 st->codec->sample_rate= 44100;
00065 }
00066
00067 if (s1 && s1->channels)
00068 st->codec->channels = s1->channels;
00069
00070 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
00071 av_assert0(st->codec->bits_per_coded_sample > 0);
00072 st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
00073 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00074 break;
00075 }
00076 case AVMEDIA_TYPE_VIDEO: {
00077 FFRawVideoDemuxerContext *s1 = s->priv_data;
00078 int width = 0, height = 0, ret = 0;
00079 enum PixelFormat pix_fmt;
00080 AVRational framerate;
00081
00082 if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
00083 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
00084 goto fail;
00085 }
00086 if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
00087 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
00088 ret = AVERROR(EINVAL);
00089 goto fail;
00090 }
00091 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
00092 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
00093 goto fail;
00094 }
00095 avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
00096 st->codec->width = width;
00097 st->codec->height = height;
00098 st->codec->pix_fmt = pix_fmt;
00099 fail:
00100 return ret;
00101 }
00102 default:
00103 return -1;
00104 }
00105 return 0;
00106 }
00107
00108 #define RAW_PACKET_SIZE 1024
00109
00110 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
00111 {
00112 int ret, size;
00113
00114 size = RAW_PACKET_SIZE;
00115
00116 if (av_new_packet(pkt, size) < 0)
00117 return AVERROR(ENOMEM);
00118
00119 pkt->pos= avio_tell(s->pb);
00120 pkt->stream_index = 0;
00121 ret = ffio_read_partial(s->pb, pkt->data, size);
00122 if (ret < 0) {
00123 av_free_packet(pkt);
00124 return ret;
00125 }
00126 av_shrink_packet(pkt, ret);
00127 return ret;
00128 }
00129
00130 int ff_raw_audio_read_header(AVFormatContext *s)
00131 {
00132 AVStream *st = avformat_new_stream(s, NULL);
00133 if (!st)
00134 return AVERROR(ENOMEM);
00135 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00136 st->codec->codec_id = s->iformat->raw_codec_id;
00137 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
00138 st->start_time = 0;
00139
00140
00141 return 0;
00142 }
00143
00144
00145 int ff_raw_video_read_header(AVFormatContext *s)
00146 {
00147 AVStream *st;
00148 FFRawVideoDemuxerContext *s1 = s->priv_data;
00149 AVRational framerate;
00150 int ret = 0;
00151
00152
00153 st = avformat_new_stream(s, NULL);
00154 if (!st) {
00155 ret = AVERROR(ENOMEM);
00156 goto fail;
00157 }
00158
00159 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00160 st->codec->codec_id = s->iformat->raw_codec_id;
00161 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
00162
00163 if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
00164 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
00165 goto fail;
00166 }
00167
00168 st->codec->time_base = av_inv_q(framerate);
00169 avpriv_set_pts_info(st, 64, 1, 1200000);
00170
00171 fail:
00172 return ret;
00173 }
00174
00175
00176
00177 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
00178 #define DEC AV_OPT_FLAG_DECODING_PARAM
00179 const AVOption ff_rawvideo_options[] = {
00180 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
00181 { NULL },
00182 };
00183
00184 #if CONFIG_G722_DEMUXER
00185 AVInputFormat ff_g722_demuxer = {
00186 .name = "g722",
00187 .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
00188 .read_header = ff_raw_read_header,
00189 .read_packet = ff_raw_read_partial_packet,
00190 .flags = AVFMT_GENERIC_INDEX,
00191 .extensions = "g722,722",
00192 .raw_codec_id = AV_CODEC_ID_ADPCM_G722,
00193 };
00194 #endif
00195
00196 #if CONFIG_LATM_DEMUXER
00197 AVInputFormat ff_latm_demuxer = {
00198 .name = "latm",
00199 .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
00200 .read_header = ff_raw_audio_read_header,
00201 .read_packet = ff_raw_read_partial_packet,
00202 .flags = AVFMT_GENERIC_INDEX,
00203 .extensions = "latm",
00204 .raw_codec_id = AV_CODEC_ID_AAC_LATM,
00205 };
00206 #endif
00207
00208 #if CONFIG_MJPEG_DEMUXER
00209 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG)
00210 #endif
00211
00212 #if CONFIG_MLP_DEMUXER
00213 AVInputFormat ff_mlp_demuxer = {
00214 .name = "mlp",
00215 .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
00216 .read_header = ff_raw_audio_read_header,
00217 .read_packet = ff_raw_read_partial_packet,
00218 .flags = AVFMT_GENERIC_INDEX,
00219 .extensions = "mlp",
00220 .raw_codec_id = AV_CODEC_ID_MLP,
00221 };
00222 #endif
00223
00224 #if CONFIG_TRUEHD_DEMUXER
00225 AVInputFormat ff_truehd_demuxer = {
00226 .name = "truehd",
00227 .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
00228 .read_header = ff_raw_audio_read_header,
00229 .read_packet = ff_raw_read_partial_packet,
00230 .flags = AVFMT_GENERIC_INDEX,
00231 .extensions = "thd",
00232 .raw_codec_id = AV_CODEC_ID_TRUEHD,
00233 };
00234 #endif
00235
00236 #if CONFIG_SHORTEN_DEMUXER
00237 AVInputFormat ff_shorten_demuxer = {
00238 .name = "shn",
00239 .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
00240 .read_header = ff_raw_audio_read_header,
00241 .read_packet = ff_raw_read_partial_packet,
00242 .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
00243 .extensions = "shn",
00244 .raw_codec_id = AV_CODEC_ID_SHORTEN,
00245 };
00246 #endif
00247
00248 #if CONFIG_VC1_DEMUXER
00249 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)
00250 #endif