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