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