From 5b8cc860a1ba0096287d33eb5e015babd6f5dcdd Mon Sep 17 00:00:00 2001 From: Gregory Montoir Date: Thu, 10 Jul 2008 03:02:19 +0000 Subject: [PATCH] Motion Pixels MVI Demuxer. Patch by Gregory Montoir users sourceforge net> Originally committed as revision 14147 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/avformat.h | 2 +- libavformat/mvi.c | 135 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 libavformat/mvi.c diff --git a/libavformat/Makefile b/libavformat/Makefile index bf54059004..9ae3b724c6 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -107,6 +107,7 @@ OBJS-$(CONFIG_MPEGVIDEO_DEMUXER) += raw.o OBJS-$(CONFIG_MPJPEG_MUXER) += mpjpeg.o OBJS-$(CONFIG_MSNWC_TCP_DEMUXER) += msnwc_tcp.o OBJS-$(CONFIG_MTV_DEMUXER) += mtv.o +OBJS-$(CONFIG_MVI_DEMUXER) += mvi.o OBJS-$(CONFIG_MXF_DEMUXER) += mxf.o OBJS-$(CONFIG_NSV_DEMUXER) += nsvdec.o OBJS-$(CONFIG_NULL_MUXER) += raw.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index bcf14bd57c..3ada80194b 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -123,6 +123,7 @@ void av_register_all(void) REGISTER_MUXER (MPJPEG, mpjpeg); REGISTER_DEMUXER (MSNWC_TCP, msnwc_tcp); REGISTER_DEMUXER (MTV, mtv); + REGISTER_DEMUXER (MVI, mvi); REGISTER_DEMUXER (MXF, mxf); REGISTER_DEMUXER (NSV, nsv); REGISTER_MUXER (NULL, null); diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 93ed4ae1f0..500c0cf377 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -22,7 +22,7 @@ #define FFMPEG_AVFORMAT_H #define LIBAVFORMAT_VERSION_MAJOR 52 -#define LIBAVFORMAT_VERSION_MINOR 16 +#define LIBAVFORMAT_VERSION_MINOR 17 #define LIBAVFORMAT_VERSION_MICRO 0 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ diff --git a/libavformat/mvi.c b/libavformat/mvi.c new file mode 100644 index 0000000000..03d4a5d6b5 --- /dev/null +++ b/libavformat/mvi.c @@ -0,0 +1,135 @@ +/* + * Motion Pixels MVI Demuxer + * Copyright (c) 2008 Gregory Montoir (cyx@users.sourceforge.net) + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avformat.h" + +#define MVI_FRAC_BITS 10 + +#define MVI_AUDIO_STREAM_INDEX 0 +#define MVI_VIDEO_STREAM_INDEX 1 + +typedef struct MviDemuxContext { + unsigned int (*get_int)(ByteIOContext *); + uint32_t audio_data_size; + uint64_t audio_size_counter; + uint64_t audio_frame_size; + int audio_size_left; + int video_frame_size; +} MviDemuxContext; + +static int read_header(AVFormatContext *s, AVFormatParameters *ap) +{ + MviDemuxContext *mvi = s->priv_data; + ByteIOContext *pb = s->pb; + AVStream *ast, *vst; + unsigned int version, frames_count, msecs_per_frame, player_version; + + ast = av_new_stream(s, 0); + if (!ast) + return AVERROR(ENOMEM); + + vst = av_new_stream(s, 0); + if (!vst) + return AVERROR(ENOMEM); + + vst->codec->extradata_size = 2; + vst->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE); + + version = get_byte(pb); + vst->codec->extradata[0] = get_byte(pb); + vst->codec->extradata[1] = get_byte(pb); + frames_count = get_le32(pb); + msecs_per_frame = get_le32(pb); + vst->codec->width = get_le16(pb); + vst->codec->height = get_le16(pb); + get_byte(pb); + ast->codec->sample_rate = get_le16(pb); + mvi->audio_data_size = get_le32(pb); + get_byte(pb); + player_version = get_le32(pb); + get_le16(pb); + get_byte(pb); + + if (frames_count == 0 || mvi->audio_data_size == 0) + return AVERROR_INVALIDDATA; + + if (version != 7 || player_version > 213) { + av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version); + return AVERROR_INVALIDDATA; + } + + av_set_pts_info(ast, 64, 1, ast->codec->sample_rate); + ast->codec->codec_type = CODEC_TYPE_AUDIO; + ast->codec->codec_id = CODEC_ID_PCM_U8; + ast->codec->channels = 1; + ast->codec->bits_per_sample = 8; + ast->codec->bit_rate = ast->codec->sample_rate * 8; + + av_set_pts_info(vst, 64, msecs_per_frame, 1000000); + vst->codec->codec_type = CODEC_TYPE_VIDEO; + vst->codec->codec_id = CODEC_ID_MOTIONPIXELS; + vst->codec->pix_fmt = PIX_FMT_RGB555; + + mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? get_le16 : get_le24; + + mvi->audio_frame_size = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count; + mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size; + mvi->audio_size_left = mvi->audio_data_size; + + return 0; +} + +static int read_packet(AVFormatContext *s, AVPacket *pkt) +{ + int ret, count; + MviDemuxContext *mvi = s->priv_data; + ByteIOContext *pb = s->pb; + + if (mvi->video_frame_size == 0) { + mvi->video_frame_size = (mvi->get_int)(pb); + if (mvi->audio_size_left == 0) + return AVERROR(EIO); + count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS; + if (count > mvi->audio_size_left) + count = mvi->audio_size_left; + if ((ret = av_get_packet(pb, pkt, count)) < 0) + return ret; + pkt->stream_index = MVI_AUDIO_STREAM_INDEX; + mvi->audio_size_left -= count; + mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS); + } else { + if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0) + return ret; + pkt->stream_index = MVI_VIDEO_STREAM_INDEX; + mvi->video_frame_size = 0; + } + return 0; +} + +AVInputFormat mvi_demuxer = { + "mvi", + NULL_IF_CONFIG_SMALL("Motion Pixels MVI format"), + sizeof(MviDemuxContext), + NULL, + read_header, + read_packet, + .extensions = "mvi" +};