00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "libavutil/intreadwrite.h"
00031 #include "avformat.h"
00032 #include "internal.h"
00033 #include "avio_internal.h"
00034
00035 #define VMD_HEADER_SIZE 0x0330
00036 #define BYTES_PER_FRAME_RECORD 16
00037
00038 typedef struct {
00039 int stream_index;
00040 int64_t frame_offset;
00041 unsigned int frame_size;
00042 int64_t pts;
00043 int keyframe;
00044 unsigned char frame_record[BYTES_PER_FRAME_RECORD];
00045 } vmd_frame;
00046
00047 typedef struct VmdDemuxContext {
00048 int video_stream_index;
00049 int audio_stream_index;
00050
00051 unsigned int frame_count;
00052 unsigned int frames_per_block;
00053 vmd_frame *frame_table;
00054 unsigned int current_frame;
00055 int is_indeo3;
00056
00057 int sample_rate;
00058 int64_t audio_sample_counter;
00059 int skiphdr;
00060
00061 unsigned char vmd_header[VMD_HEADER_SIZE];
00062 } VmdDemuxContext;
00063
00064 static int vmd_probe(AVProbeData *p)
00065 {
00066 int w, h;
00067 if (p->buf_size < 16)
00068 return 0;
00069
00070
00071 if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
00072 return 0;
00073 w = AV_RL16(&p->buf[12]);
00074 h = AV_RL16(&p->buf[14]);
00075 if (!w || w > 2048 || !h || h > 2048)
00076 return 0;
00077
00078
00079 return AVPROBE_SCORE_MAX / 2;
00080 }
00081
00082 static int vmd_read_header(AVFormatContext *s)
00083 {
00084 VmdDemuxContext *vmd = s->priv_data;
00085 AVIOContext *pb = s->pb;
00086 AVStream *st = NULL, *vst;
00087 unsigned int toc_offset;
00088 unsigned char *raw_frame_table;
00089 int raw_frame_table_size;
00090 int64_t current_offset;
00091 int i, j;
00092 unsigned int total_frames;
00093 int64_t current_audio_pts = 0;
00094 unsigned char chunk[BYTES_PER_FRAME_RECORD];
00095 int num, den;
00096 int sound_buffers;
00097
00098
00099 avio_seek(pb, 0, SEEK_SET);
00100 if (avio_read(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
00101 return AVERROR(EIO);
00102
00103 if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3')
00104 vmd->is_indeo3 = 1;
00105 else
00106 vmd->is_indeo3 = 0;
00107
00108 vst = avformat_new_stream(s, NULL);
00109 if (!vst)
00110 return AVERROR(ENOMEM);
00111 avpriv_set_pts_info(vst, 33, 1, 10);
00112 vmd->video_stream_index = vst->index;
00113 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00114 vst->codec->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO;
00115 vst->codec->codec_tag = 0;
00116 vst->codec->width = AV_RL16(&vmd->vmd_header[12]);
00117 vst->codec->height = AV_RL16(&vmd->vmd_header[14]);
00118 if(vmd->is_indeo3 && vst->codec->width > 320){
00119 vst->codec->width >>= 1;
00120 vst->codec->height >>= 1;
00121 }
00122 vst->codec->extradata_size = VMD_HEADER_SIZE;
00123 vst->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
00124 memcpy(vst->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
00125
00126
00127 vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
00128 if (vmd->sample_rate) {
00129 st = avformat_new_stream(s, NULL);
00130 if (!st)
00131 return AVERROR(ENOMEM);
00132 vmd->audio_stream_index = st->index;
00133 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00134 st->codec->codec_id = AV_CODEC_ID_VMDAUDIO;
00135 st->codec->codec_tag = 0;
00136 st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
00137 st->codec->sample_rate = vmd->sample_rate;
00138 st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
00139 if (st->codec->block_align & 0x8000) {
00140 st->codec->bits_per_coded_sample = 16;
00141 st->codec->block_align = -(st->codec->block_align - 0x10000);
00142 } else {
00143 st->codec->bits_per_coded_sample = 8;
00144 }
00145 st->codec->bit_rate = st->codec->sample_rate *
00146 st->codec->bits_per_coded_sample * st->codec->channels;
00147
00148
00149 num = st->codec->block_align;
00150 den = st->codec->sample_rate * st->codec->channels;
00151 av_reduce(&den, &num, den, num, (1UL<<31)-1);
00152 avpriv_set_pts_info(vst, 33, num, den);
00153 avpriv_set_pts_info(st, 33, num, den);
00154 }
00155
00156 toc_offset = AV_RL32(&vmd->vmd_header[812]);
00157 vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
00158 vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
00159 avio_seek(pb, toc_offset, SEEK_SET);
00160
00161 raw_frame_table = NULL;
00162 vmd->frame_table = NULL;
00163 sound_buffers = AV_RL16(&vmd->vmd_header[808]);
00164 raw_frame_table_size = vmd->frame_count * 6;
00165 if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
00166 av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
00167 return -1;
00168 }
00169 raw_frame_table = av_malloc(raw_frame_table_size);
00170 vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
00171 if (!raw_frame_table || !vmd->frame_table) {
00172 av_free(raw_frame_table);
00173 av_free(vmd->frame_table);
00174 return AVERROR(ENOMEM);
00175 }
00176 if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
00177 raw_frame_table_size) {
00178 av_free(raw_frame_table);
00179 av_free(vmd->frame_table);
00180 return AVERROR(EIO);
00181 }
00182
00183 total_frames = 0;
00184 for (i = 0; i < vmd->frame_count; i++) {
00185
00186 current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
00187
00188
00189 for (j = 0; j < vmd->frames_per_block; j++) {
00190 int type;
00191 uint32_t size;
00192
00193 avio_read(pb, chunk, BYTES_PER_FRAME_RECORD);
00194 type = chunk[0];
00195 size = AV_RL32(&chunk[2]);
00196 if(!size && type != 1)
00197 continue;
00198 switch(type) {
00199 case 1:
00200 if (!st) break;
00201
00202 vmd->frame_table[total_frames].frame_offset = current_offset;
00203 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
00204 vmd->frame_table[total_frames].frame_size = size;
00205 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
00206 vmd->frame_table[total_frames].pts = current_audio_pts;
00207 total_frames++;
00208 if(!current_audio_pts)
00209 current_audio_pts += sound_buffers - 1;
00210 else
00211 current_audio_pts++;
00212 break;
00213 case 2:
00214 vmd->frame_table[total_frames].frame_offset = current_offset;
00215 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
00216 vmd->frame_table[total_frames].frame_size = size;
00217 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
00218 vmd->frame_table[total_frames].pts = i;
00219 total_frames++;
00220 break;
00221 }
00222 current_offset += size;
00223 }
00224 }
00225
00226 av_free(raw_frame_table);
00227
00228 vmd->current_frame = 0;
00229 vmd->frame_count = total_frames;
00230
00231 return 0;
00232 }
00233
00234 static int vmd_read_packet(AVFormatContext *s,
00235 AVPacket *pkt)
00236 {
00237 VmdDemuxContext *vmd = s->priv_data;
00238 AVIOContext *pb = s->pb;
00239 int ret = 0;
00240 vmd_frame *frame;
00241
00242 if (vmd->current_frame >= vmd->frame_count)
00243 return AVERROR(EIO);
00244
00245 frame = &vmd->frame_table[vmd->current_frame];
00246
00247 avio_seek(pb, frame->frame_offset, SEEK_SET);
00248
00249 if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
00250 return AVERROR(EIO);
00251 if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
00252 return AVERROR(ENOMEM);
00253 pkt->pos= avio_tell(pb);
00254 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
00255 if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
00256 ret = avio_read(pb, pkt->data, frame->frame_size);
00257 else
00258 ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
00259 frame->frame_size);
00260
00261 if (ret != frame->frame_size) {
00262 av_free_packet(pkt);
00263 ret = AVERROR(EIO);
00264 }
00265 pkt->stream_index = frame->stream_index;
00266 pkt->pts = frame->pts;
00267 av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
00268 (frame->frame_record[0] == 0x02) ? "video" : "audio",
00269 frame->frame_size + BYTES_PER_FRAME_RECORD,
00270 pkt->pts);
00271
00272 vmd->current_frame++;
00273
00274 return ret;
00275 }
00276
00277 static int vmd_read_close(AVFormatContext *s)
00278 {
00279 VmdDemuxContext *vmd = s->priv_data;
00280
00281 av_free(vmd->frame_table);
00282
00283 return 0;
00284 }
00285
00286 AVInputFormat ff_vmd_demuxer = {
00287 .name = "vmd",
00288 .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
00289 .priv_data_size = sizeof(VmdDemuxContext),
00290 .read_probe = vmd_probe,
00291 .read_header = vmd_read_header,
00292 .read_packet = vmd_read_packet,
00293 .read_close = vmd_read_close,
00294 };