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 "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 /* raw input */
00032 int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
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->value;
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                              AVFormatParameters *ap)
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->value;
00137     st->need_parsing = AVSTREAM_PARSE_FULL;
00138     st->start_time = 0;
00139     /* the parameters will be extracted from the compressed bitstream */
00140 
00141     return 0;
00142 }
00143 
00144 /* MPEG-1/H.263 input */
00145 int ff_raw_video_read_header(AVFormatContext *s,
00146                              AVFormatParameters *ap)
00147 {
00148     AVStream *st;
00149     FFRawVideoDemuxerContext *s1 = s->priv_data;
00150     AVRational framerate;
00151     int ret = 0;
00152 
00153 
00154     st = avformat_new_stream(s, NULL);
00155     if (!st) {
00156         ret = AVERROR(ENOMEM);
00157         goto fail;
00158     }
00159 
00160     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00161     st->codec->codec_id = s->iformat->value;
00162     st->need_parsing = AVSTREAM_PARSE_FULL;
00163 
00164     if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
00165         av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
00166         goto fail;
00167     }
00168 
00169     st->codec->time_base = (AVRational){framerate.den, framerate.num};
00170     avpriv_set_pts_info(st, 64, 1, 1200000);
00171 
00172 fail:
00173     return ret;
00174 }
00175 
00176 /* Note: Do not forget to add new entries to the Makefile as well. */
00177 
00178 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
00179 #define DEC AV_OPT_FLAG_DECODING_PARAM
00180 const AVOption ff_rawvideo_options[] = {
00181     { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
00182     { NULL },
00183 };
00184 
00185 #if CONFIG_G722_DEMUXER
00186 AVInputFormat ff_g722_demuxer = {
00187     .name           = "g722",
00188     .long_name      = NULL_IF_CONFIG_SMALL("raw G.722"),
00189     .read_header    = ff_raw_read_header,
00190     .read_packet    = ff_raw_read_partial_packet,
00191     .flags= AVFMT_GENERIC_INDEX,
00192     .extensions = "g722,722",
00193     .value = CODEC_ID_ADPCM_G722,
00194 };
00195 #endif
00196 
00197 #if CONFIG_LATM_DEMUXER
00198 AVInputFormat ff_latm_demuxer = {
00199     .name           = "latm",
00200     .long_name      = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
00201     .read_header    = ff_raw_audio_read_header,
00202     .read_packet    = ff_raw_read_partial_packet,
00203     .flags= AVFMT_GENERIC_INDEX,
00204     .extensions = "latm",
00205     .value = CODEC_ID_AAC_LATM,
00206 };
00207 #endif
00208 
00209 #if CONFIG_MJPEG_DEMUXER
00210 FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", CODEC_ID_MJPEG)
00211 #endif
00212 
00213 #if CONFIG_MLP_DEMUXER
00214 AVInputFormat ff_mlp_demuxer = {
00215     .name           = "mlp",
00216     .long_name      = NULL_IF_CONFIG_SMALL("raw MLP"),
00217     .read_header    = ff_raw_audio_read_header,
00218     .read_packet    = ff_raw_read_partial_packet,
00219     .flags= AVFMT_GENERIC_INDEX,
00220     .extensions = "mlp",
00221     .value = CODEC_ID_MLP,
00222 };
00223 #endif
00224 
00225 #if CONFIG_TRUEHD_DEMUXER
00226 AVInputFormat ff_truehd_demuxer = {
00227     .name           = "truehd",
00228     .long_name      = NULL_IF_CONFIG_SMALL("raw TrueHD"),
00229     .read_header    = ff_raw_audio_read_header,
00230     .read_packet    = ff_raw_read_partial_packet,
00231     .flags= AVFMT_GENERIC_INDEX,
00232     .extensions = "thd",
00233     .value = CODEC_ID_TRUEHD,
00234 };
00235 #endif
00236 
00237 #if CONFIG_SHORTEN_DEMUXER
00238 AVInputFormat ff_shorten_demuxer = {
00239     .name           = "shn",
00240     .long_name      = NULL_IF_CONFIG_SMALL("raw Shorten"),
00241     .read_header    = ff_raw_audio_read_header,
00242     .read_packet    = ff_raw_read_partial_packet,
00243     .flags          = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
00244     .extensions = "shn",
00245     .value = CODEC_ID_SHORTEN,
00246 };
00247 #endif
00248 
00249 #if CONFIG_VC1_DEMUXER
00250 FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
00251 #endif