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