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