00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023
00024 #define MVI_FRAC_BITS 10
00025
00026 #define MVI_AUDIO_STREAM_INDEX 0
00027 #define MVI_VIDEO_STREAM_INDEX 1
00028
00029 typedef struct MviDemuxContext {
00030 unsigned int (*get_int)(AVIOContext *);
00031 uint32_t audio_data_size;
00032 uint64_t audio_size_counter;
00033 uint64_t audio_frame_size;
00034 int audio_size_left;
00035 int video_frame_size;
00036 } MviDemuxContext;
00037
00038 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
00039 {
00040 MviDemuxContext *mvi = s->priv_data;
00041 AVIOContext *pb = s->pb;
00042 AVStream *ast, *vst;
00043 unsigned int version, frames_count, msecs_per_frame, player_version;
00044
00045 ast = av_new_stream(s, 0);
00046 if (!ast)
00047 return AVERROR(ENOMEM);
00048
00049 vst = av_new_stream(s, 0);
00050 if (!vst)
00051 return AVERROR(ENOMEM);
00052
00053 vst->codec->extradata_size = 2;
00054 vst->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
00055
00056 version = avio_r8(pb);
00057 vst->codec->extradata[0] = avio_r8(pb);
00058 vst->codec->extradata[1] = avio_r8(pb);
00059 frames_count = avio_rl32(pb);
00060 msecs_per_frame = avio_rl32(pb);
00061 vst->codec->width = avio_rl16(pb);
00062 vst->codec->height = avio_rl16(pb);
00063 avio_r8(pb);
00064 ast->codec->sample_rate = avio_rl16(pb);
00065 mvi->audio_data_size = avio_rl32(pb);
00066 avio_r8(pb);
00067 player_version = avio_rl32(pb);
00068 avio_rl16(pb);
00069 avio_r8(pb);
00070
00071 if (frames_count == 0 || mvi->audio_data_size == 0)
00072 return AVERROR_INVALIDDATA;
00073
00074 if (version != 7 || player_version > 213) {
00075 av_log(s, AV_LOG_ERROR, "unhandled version (%d,%d)\n", version, player_version);
00076 return AVERROR_INVALIDDATA;
00077 }
00078
00079 av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
00080 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00081 ast->codec->codec_id = CODEC_ID_PCM_U8;
00082 ast->codec->channels = 1;
00083 ast->codec->bits_per_coded_sample = 8;
00084 ast->codec->bit_rate = ast->codec->sample_rate * 8;
00085
00086 av_set_pts_info(vst, 64, msecs_per_frame, 1000000);
00087 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00088 vst->codec->codec_id = CODEC_ID_MOTIONPIXELS;
00089
00090 mvi->get_int = (vst->codec->width * vst->codec->height < (1 << 16)) ? avio_rl16 : avio_rl24;
00091
00092 mvi->audio_frame_size = ((uint64_t)mvi->audio_data_size << MVI_FRAC_BITS) / frames_count;
00093 mvi->audio_size_counter = (ast->codec->sample_rate * 830 / mvi->audio_frame_size - 1) * mvi->audio_frame_size;
00094 mvi->audio_size_left = mvi->audio_data_size;
00095
00096 return 0;
00097 }
00098
00099 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00100 {
00101 int ret, count;
00102 MviDemuxContext *mvi = s->priv_data;
00103 AVIOContext *pb = s->pb;
00104
00105 if (mvi->video_frame_size == 0) {
00106 mvi->video_frame_size = (mvi->get_int)(pb);
00107 if (mvi->audio_size_left == 0)
00108 return AVERROR(EIO);
00109 count = (mvi->audio_size_counter + mvi->audio_frame_size + 512) >> MVI_FRAC_BITS;
00110 if (count > mvi->audio_size_left)
00111 count = mvi->audio_size_left;
00112 if ((ret = av_get_packet(pb, pkt, count)) < 0)
00113 return ret;
00114 pkt->stream_index = MVI_AUDIO_STREAM_INDEX;
00115 mvi->audio_size_left -= count;
00116 mvi->audio_size_counter += mvi->audio_frame_size - (count << MVI_FRAC_BITS);
00117 } else {
00118 if ((ret = av_get_packet(pb, pkt, mvi->video_frame_size)) < 0)
00119 return ret;
00120 pkt->stream_index = MVI_VIDEO_STREAM_INDEX;
00121 mvi->video_frame_size = 0;
00122 }
00123 return 0;
00124 }
00125
00126 AVInputFormat ff_mvi_demuxer = {
00127 "mvi",
00128 NULL_IF_CONFIG_SMALL("Motion Pixels MVI format"),
00129 sizeof(MviDemuxContext),
00130 NULL,
00131 read_header,
00132 read_packet,
00133 .extensions = "mvi"
00134 };