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 AVFormatParameters *ap)
00084 {
00085 VmdDemuxContext *vmd = s->priv_data;
00086 AVIOContext *pb = s->pb;
00087 AVStream *st = NULL, *vst;
00088 unsigned int toc_offset;
00089 unsigned char *raw_frame_table;
00090 int raw_frame_table_size;
00091 int64_t current_offset;
00092 int i, j;
00093 unsigned int total_frames;
00094 int64_t current_audio_pts = 0;
00095 unsigned char chunk[BYTES_PER_FRAME_RECORD];
00096 int num, den;
00097 int sound_buffers;
00098
00099
00100 avio_seek(pb, 0, SEEK_SET);
00101 if (avio_read(pb, vmd->vmd_header, VMD_HEADER_SIZE) != VMD_HEADER_SIZE)
00102 return AVERROR(EIO);
00103
00104 if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3')
00105 vmd->is_indeo3 = 1;
00106 else
00107 vmd->is_indeo3 = 0;
00108
00109 vst = avformat_new_stream(s, NULL);
00110 if (!vst)
00111 return AVERROR(ENOMEM);
00112 avpriv_set_pts_info(vst, 33, 1, 10);
00113 vmd->video_stream_index = vst->index;
00114 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00115 vst->codec->codec_id = vmd->is_indeo3 ? CODEC_ID_INDEO3 : CODEC_ID_VMDVIDEO;
00116 vst->codec->codec_tag = 0;
00117 vst->codec->width = AV_RL16(&vmd->vmd_header[12]);
00118 vst->codec->height = AV_RL16(&vmd->vmd_header[14]);
00119 if(vmd->is_indeo3 && vst->codec->width > 320){
00120 vst->codec->width >>= 1;
00121 vst->codec->height >>= 1;
00122 }
00123 vst->codec->extradata_size = VMD_HEADER_SIZE;
00124 vst->codec->extradata = av_mallocz(VMD_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
00125 memcpy(vst->codec->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
00126
00127
00128 vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
00129 if (vmd->sample_rate) {
00130 st = avformat_new_stream(s, NULL);
00131 if (!st)
00132 return AVERROR(ENOMEM);
00133 vmd->audio_stream_index = st->index;
00134 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00135 st->codec->codec_id = CODEC_ID_VMDAUDIO;
00136 st->codec->codec_tag = 0;
00137 st->codec->channels = (vmd->vmd_header[811] & 0x80) ? 2 : 1;
00138 st->codec->sample_rate = vmd->sample_rate;
00139 st->codec->block_align = AV_RL16(&vmd->vmd_header[806]);
00140 if (st->codec->block_align & 0x8000) {
00141 st->codec->bits_per_coded_sample = 16;
00142 st->codec->block_align = -(st->codec->block_align - 0x10000);
00143 } else {
00144 st->codec->bits_per_coded_sample = 8;
00145 }
00146 st->codec->bit_rate = st->codec->sample_rate *
00147 st->codec->bits_per_coded_sample * st->codec->channels;
00148
00149
00150 num = st->codec->block_align;
00151 den = st->codec->sample_rate * st->codec->channels;
00152 av_reduce(&den, &num, den, num, (1UL<<31)-1);
00153 avpriv_set_pts_info(vst, 33, num, den);
00154 avpriv_set_pts_info(st, 33, num, den);
00155 }
00156
00157 toc_offset = AV_RL32(&vmd->vmd_header[812]);
00158 vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
00159 vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
00160 avio_seek(pb, toc_offset, SEEK_SET);
00161
00162 raw_frame_table = NULL;
00163 vmd->frame_table = NULL;
00164 sound_buffers = AV_RL16(&vmd->vmd_header[808]);
00165 raw_frame_table_size = vmd->frame_count * 6;
00166 if(vmd->frame_count * vmd->frames_per_block >= UINT_MAX / sizeof(vmd_frame) - sound_buffers){
00167 av_log(s, AV_LOG_ERROR, "vmd->frame_count * vmd->frames_per_block too large\n");
00168 return -1;
00169 }
00170 raw_frame_table = av_malloc(raw_frame_table_size);
00171 vmd->frame_table = av_malloc((vmd->frame_count * vmd->frames_per_block + sound_buffers) * sizeof(vmd_frame));
00172 if (!raw_frame_table || !vmd->frame_table) {
00173 av_free(raw_frame_table);
00174 av_free(vmd->frame_table);
00175 return AVERROR(ENOMEM);
00176 }
00177 if (avio_read(pb, raw_frame_table, raw_frame_table_size) !=
00178 raw_frame_table_size) {
00179 av_free(raw_frame_table);
00180 av_free(vmd->frame_table);
00181 return AVERROR(EIO);
00182 }
00183
00184 total_frames = 0;
00185 for (i = 0; i < vmd->frame_count; i++) {
00186
00187 current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
00188
00189
00190 for (j = 0; j < vmd->frames_per_block; j++) {
00191 int type;
00192 uint32_t size;
00193
00194 avio_read(pb, chunk, BYTES_PER_FRAME_RECORD);
00195 type = chunk[0];
00196 size = AV_RL32(&chunk[2]);
00197 if(!size && type != 1)
00198 continue;
00199 switch(type) {
00200 case 1:
00201 if (!st) break;
00202
00203 vmd->frame_table[total_frames].frame_offset = current_offset;
00204 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
00205 vmd->frame_table[total_frames].frame_size = size;
00206 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
00207 vmd->frame_table[total_frames].pts = current_audio_pts;
00208 total_frames++;
00209 if(!current_audio_pts)
00210 current_audio_pts += sound_buffers;
00211 else
00212 current_audio_pts++;
00213 break;
00214 case 2:
00215 vmd->frame_table[total_frames].frame_offset = current_offset;
00216 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
00217 vmd->frame_table[total_frames].frame_size = size;
00218 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
00219 vmd->frame_table[total_frames].pts = i;
00220 total_frames++;
00221 break;
00222 }
00223 current_offset += size;
00224 }
00225 }
00226
00227 av_free(raw_frame_table);
00228
00229 vmd->current_frame = 0;
00230 vmd->frame_count = total_frames;
00231
00232 return 0;
00233 }
00234
00235 static int vmd_read_packet(AVFormatContext *s,
00236 AVPacket *pkt)
00237 {
00238 VmdDemuxContext *vmd = s->priv_data;
00239 AVIOContext *pb = s->pb;
00240 int ret = 0;
00241 vmd_frame *frame;
00242
00243 if (vmd->current_frame >= vmd->frame_count)
00244 return AVERROR(EIO);
00245
00246 frame = &vmd->frame_table[vmd->current_frame];
00247
00248 avio_seek(pb, frame->frame_offset, SEEK_SET);
00249
00250 if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
00251 return AVERROR(EIO);
00252 if (av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD))
00253 return AVERROR(ENOMEM);
00254 pkt->pos= avio_tell(pb);
00255 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
00256 if(vmd->is_indeo3 && frame->frame_record[0] == 0x02)
00257 ret = avio_read(pb, pkt->data, frame->frame_size);
00258 else
00259 ret = avio_read(pb, pkt->data + BYTES_PER_FRAME_RECORD,
00260 frame->frame_size);
00261
00262 if (ret != frame->frame_size) {
00263 av_free_packet(pkt);
00264 ret = AVERROR(EIO);
00265 }
00266 pkt->stream_index = frame->stream_index;
00267 pkt->pts = frame->pts;
00268 av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
00269 (frame->frame_record[0] == 0x02) ? "video" : "audio",
00270 frame->frame_size + BYTES_PER_FRAME_RECORD,
00271 pkt->pts);
00272
00273 vmd->current_frame++;
00274
00275 return ret;
00276 }
00277
00278 static int vmd_read_close(AVFormatContext *s)
00279 {
00280 VmdDemuxContext *vmd = s->priv_data;
00281
00282 av_free(vmd->frame_table);
00283
00284 return 0;
00285 }
00286
00287 AVInputFormat ff_vmd_demuxer = {
00288 .name = "vmd",
00289 .long_name = NULL_IF_CONFIG_SMALL("Sierra VMD format"),
00290 .priv_data_size = sizeof(VmdDemuxContext),
00291 .read_probe = vmd_probe,
00292 .read_header = vmd_read_header,
00293 .read_packet = vmd_read_packet,
00294 .read_close = vmd_read_close,
00295 };