• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/rawdec.c

Go to the documentation of this file.
00001 /*
00002  * RAW demuxers
00003  * Copyright (c) 2001 Fabrice Bellard
00004  * Copyright (c) 2005 Alex Beregszaszi
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "avformat.h"
00024 #include "avio_internal.h"
00025 #include "rawdec.h"
00026 #include "libavutil/opt.h"
00027 #include "libavutil/parseutils.h"
00028 #include "libavutil/pixdesc.h"
00029 
00030 /* raw input */
00031 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
00032 {
00033     AVStream *st;
00034     enum CodecID id;
00035 
00036     st = av_new_stream(s, 0);
00037     if (!st)
00038         return AVERROR(ENOMEM);
00039 
00040         id = s->iformat->value;
00041         if (id == CODEC_ID_RAWVIDEO) {
00042             st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00043         } else {
00044             st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00045         }
00046         st->codec->codec_id = id;
00047 
00048         switch(st->codec->codec_type) {
00049         case AVMEDIA_TYPE_AUDIO: {
00050             RawAudioDemuxerContext *s1 = s->priv_data;
00051 
00052 #if FF_API_FORMAT_PARAMETERS
00053             if (ap->sample_rate)
00054                 st->codec->sample_rate = ap->sample_rate;
00055             if (ap->channels)
00056                 st->codec->channels    = ap->channels;
00057             else st->codec->channels   = 1;
00058 #endif
00059 
00060             if (s1->sample_rate)
00061                 st->codec->sample_rate = s1->sample_rate;
00062             if (st->codec->sample_rate <= 0) {
00063                 av_log(s, AV_LOG_WARNING, "Invalid sample rate %d specified using default of 44100\n",
00064                        st->codec->sample_rate);
00065                 st->codec->sample_rate= 44100;
00066             }
00067 
00068             if (s1->channels)
00069                 st->codec->channels    = s1->channels;
00070 
00071             st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
00072             assert(st->codec->bits_per_coded_sample > 0);
00073             st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
00074             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00075             break;
00076             }
00077         case AVMEDIA_TYPE_VIDEO: {
00078             FFRawVideoDemuxerContext *s1 = s->priv_data;
00079             int width = 0, height = 0, ret = 0;
00080             enum PixelFormat pix_fmt;
00081             AVRational framerate;
00082 
00083             if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
00084                 av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
00085                 goto fail;
00086             }
00087             if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
00088                 av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
00089                 ret = AVERROR(EINVAL);
00090                 goto fail;
00091             }
00092             if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
00093                 av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
00094                 goto fail;
00095             }
00096 #if FF_API_FORMAT_PARAMETERS
00097             if (ap->width > 0)
00098                 width = ap->width;
00099             if (ap->height > 0)
00100                 height = ap->height;
00101             if (ap->pix_fmt)
00102                 pix_fmt = ap->pix_fmt;
00103             if (ap->time_base.num)
00104                 framerate = (AVRational){ap->time_base.den, ap->time_base.num};
00105 #endif
00106             av_set_pts_info(st, 64, framerate.den, framerate.num);
00107             st->codec->width  = width;
00108             st->codec->height = height;
00109             st->codec->pix_fmt = pix_fmt;
00110 fail:
00111             return ret;
00112             }
00113         default:
00114             return -1;
00115         }
00116     return 0;
00117 }
00118 
00119 #define RAW_PACKET_SIZE 1024
00120 
00121 int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
00122 {
00123     int ret, size;
00124 
00125     size = RAW_PACKET_SIZE;
00126 
00127     if (av_new_packet(pkt, size) < 0)
00128         return AVERROR(ENOMEM);
00129 
00130     pkt->pos= avio_tell(s->pb);
00131     pkt->stream_index = 0;
00132     ret = ffio_read_partial(s->pb, pkt->data, size);
00133     if (ret < 0) {
00134         av_free_packet(pkt);
00135         return ret;
00136     }
00137     pkt->size = ret;
00138     return ret;
00139 }
00140 
00141 int ff_raw_audio_read_header(AVFormatContext *s,
00142                              AVFormatParameters *ap)
00143 {
00144     AVStream *st = av_new_stream(s, 0);
00145     if (!st)
00146         return AVERROR(ENOMEM);
00147     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00148     st->codec->codec_id = s->iformat->value;
00149     st->need_parsing = AVSTREAM_PARSE_FULL;
00150     st->start_time = 0;
00151     /* the parameters will be extracted from the compressed bitstream */
00152 
00153     return 0;
00154 }
00155 
00156 /* MPEG-1/H.263 input */
00157 int ff_raw_video_read_header(AVFormatContext *s,
00158                              AVFormatParameters *ap)
00159 {
00160     AVStream *st;
00161     FFRawVideoDemuxerContext *s1 = s->priv_data;
00162     AVRational framerate;
00163     int ret = 0;
00164 
00165 
00166     st = av_new_stream(s, 0);
00167     if (!st) {
00168         ret = AVERROR(ENOMEM);
00169         goto fail;
00170     }
00171 
00172     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00173     st->codec->codec_id = s->iformat->value;
00174     st->need_parsing = AVSTREAM_PARSE_FULL;
00175 
00176     if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
00177         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
00178         goto fail;
00179     }
00180 #if FF_API_FORMAT_PARAMETERS
00181     if (ap->time_base.num)
00182         framerate = (AVRational){ap->time_base.den, ap->time_base.num};
00183 #endif
00184 
00185     st->codec->time_base = (AVRational){framerate.den, framerate.num};
00186     av_set_pts_info(st, 64, 1, 1200000);
00187 
00188 fail:
00189     return ret;
00190 }
00191 
00192 /* Note: Do not forget to add new entries to the Makefile as well. */
00193 
00194 static const AVOption audio_options[] = {
00195     { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00196     { "channels",    "", offsetof(RawAudioDemuxerContext, channels),    FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
00197     { NULL },
00198 };
00199 
00200 const AVClass ff_rawaudio_demuxer_class = {
00201     .class_name     = "rawaudio demuxer",
00202     .item_name      = av_default_item_name,
00203     .option         = audio_options,
00204     .version        = LIBAVUTIL_VERSION_INT,
00205 };
00206 
00207 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
00208 #define DEC AV_OPT_FLAG_DECODING_PARAM
00209 static const AVOption video_options[] = {
00210     { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
00211     { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
00212     { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
00213     { NULL },
00214 };
00215 #undef OFFSET
00216 #undef DEC
00217 
00218 const AVClass ff_rawvideo_demuxer_class = {
00219     .class_name     = "rawvideo demuxer",
00220     .item_name      = av_default_item_name,
00221     .option         = video_options,
00222     .version        = LIBAVUTIL_VERSION_INT,
00223 };
00224 
00225 #if CONFIG_G722_DEMUXER
00226 AVInputFormat ff_g722_demuxer = {
00227     "g722",
00228     NULL_IF_CONFIG_SMALL("raw G.722"),
00229     sizeof(RawAudioDemuxerContext),
00230     NULL,
00231     ff_raw_read_header,
00232     ff_raw_read_partial_packet,
00233     .flags= AVFMT_GENERIC_INDEX,
00234     .extensions = "g722,722",
00235     .value = CODEC_ID_ADPCM_G722,
00236     .priv_class = &ff_rawaudio_demuxer_class,
00237 };
00238 #endif
00239 
00240 #if CONFIG_GSM_DEMUXER
00241 AVInputFormat ff_gsm_demuxer = {
00242     "gsm",
00243     NULL_IF_CONFIG_SMALL("raw GSM"),
00244     0,
00245     NULL,
00246     ff_raw_audio_read_header,
00247     ff_raw_read_partial_packet,
00248     .flags= AVFMT_GENERIC_INDEX,
00249     .extensions = "gsm",
00250     .value = CODEC_ID_GSM,
00251 };
00252 #endif
00253 
00254 #if CONFIG_MJPEG_DEMUXER
00255 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", CODEC_ID_MJPEG)
00256 #endif
00257 
00258 #if CONFIG_MLP_DEMUXER
00259 AVInputFormat ff_mlp_demuxer = {
00260     "mlp",
00261     NULL_IF_CONFIG_SMALL("raw MLP"),
00262     0,
00263     NULL,
00264     ff_raw_audio_read_header,
00265     ff_raw_read_partial_packet,
00266     .flags= AVFMT_GENERIC_INDEX,
00267     .extensions = "mlp",
00268     .value = CODEC_ID_MLP,
00269 };
00270 #endif
00271 
00272 #if CONFIG_TRUEHD_DEMUXER
00273 AVInputFormat ff_truehd_demuxer = {
00274     "truehd",
00275     NULL_IF_CONFIG_SMALL("raw TrueHD"),
00276     0,
00277     NULL,
00278     ff_raw_audio_read_header,
00279     ff_raw_read_partial_packet,
00280     .flags= AVFMT_GENERIC_INDEX,
00281     .extensions = "thd",
00282     .value = CODEC_ID_TRUEHD,
00283 };
00284 #endif
00285 
00286 #if CONFIG_SHORTEN_DEMUXER
00287 AVInputFormat ff_shorten_demuxer = {
00288     "shn",
00289     NULL_IF_CONFIG_SMALL("raw Shorten"),
00290     0,
00291     NULL,
00292     ff_raw_audio_read_header,
00293     ff_raw_read_partial_packet,
00294     .flags= AVFMT_GENERIC_INDEX,
00295     .extensions = "shn",
00296     .value = CODEC_ID_SHORTEN,
00297 };
00298 #endif
00299 
00300 #if CONFIG_VC1_DEMUXER
00301 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
00302 #endif

Generated on Wed Apr 11 2012 07:31:34 for FFmpeg by  doxygen 1.7.1