| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Sierra VMD Format Demuxer | ||
| 3 | * Copyright (c) 2004 The FFmpeg project | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | /** | ||
| 23 | * @file | ||
| 24 | * Sierra VMD file demuxer | ||
| 25 | * by Vladimir "VAG" Gneushev (vagsoft at mail.ru) | ||
| 26 | * for more information on the Sierra VMD file format, visit: | ||
| 27 | * http://www.pcisys.net/~melanson/codecs/ | ||
| 28 | */ | ||
| 29 | |||
| 30 | #include "libavutil/channel_layout.h" | ||
| 31 | #include "libavutil/intreadwrite.h" | ||
| 32 | #include "libavutil/mem.h" | ||
| 33 | #include "avformat.h" | ||
| 34 | #include "avio_internal.h" | ||
| 35 | #include "demux.h" | ||
| 36 | #include "internal.h" | ||
| 37 | #include "avio_internal.h" | ||
| 38 | |||
| 39 | #define VMD_HEADER_SIZE 0x0330 | ||
| 40 | #define BYTES_PER_FRAME_RECORD 16 | ||
| 41 | |||
| 42 | typedef struct vmd_frame { | ||
| 43 | int stream_index; | ||
| 44 | unsigned int frame_size; | ||
| 45 | int64_t frame_offset; | ||
| 46 | int64_t pts; | ||
| 47 | unsigned char frame_record[BYTES_PER_FRAME_RECORD]; | ||
| 48 | } vmd_frame; | ||
| 49 | |||
| 50 | typedef struct VmdDemuxContext { | ||
| 51 | int video_stream_index; | ||
| 52 | int audio_stream_index; | ||
| 53 | |||
| 54 | unsigned int frame_count; | ||
| 55 | unsigned int frames_per_block; | ||
| 56 | vmd_frame *frame_table; | ||
| 57 | unsigned int current_frame; | ||
| 58 | int is_indeo3; | ||
| 59 | |||
| 60 | int sample_rate; | ||
| 61 | int64_t audio_sample_counter; | ||
| 62 | int skiphdr; | ||
| 63 | |||
| 64 | unsigned char vmd_header[VMD_HEADER_SIZE]; | ||
| 65 | } VmdDemuxContext; | ||
| 66 | |||
| 67 | 7961 | static int vmd_probe(const AVProbeData *p) | |
| 68 | { | ||
| 69 | int w, h, sample_rate; | ||
| 70 |
2/2✓ Branch 0 taken 124 times.
✓ Branch 1 taken 7837 times.
|
7961 | if (p->buf_size < 806) |
| 71 | 124 | return 0; | |
| 72 | /* check if the first 2 bytes of the file contain the appropriate size | ||
| 73 | * of a VMD header chunk */ | ||
| 74 |
2/2✓ Branch 0 taken 7834 times.
✓ Branch 1 taken 3 times.
|
7837 | if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2) |
| 75 | 7834 | return 0; | |
| 76 | 3 | w = AV_RL16(&p->buf[12]); | |
| 77 | 3 | h = AV_RL16(&p->buf[14]); | |
| 78 | 3 | sample_rate = AV_RL16(&p->buf[804]); | |
| 79 |
4/10✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
3 | if ((!w || w > 2048 || !h || h > 2048) && |
| 80 | sample_rate != 22050) | ||
| 81 | ✗ | return 0; | |
| 82 | /* the header of a file with a video stream carries the 6bit VGA | ||
| 83 | * palette that vmdvideo_decode_init() loads from offset 28 */ | ||
| 84 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (w && h) |
| 85 |
2/2✓ Branch 0 taken 2304 times.
✓ Branch 1 taken 3 times.
|
2307 | for (int i = 28; i < 28 + 3 * 256; i++) |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2304 times.
|
2304 | if (p->buf[i] > 0x3F) |
| 87 | ✗ | return 0; | |
| 88 | |||
| 89 | /* only return half certainty since this check is a bit sketchy */ | ||
| 90 | 3 | return AVPROBE_SCORE_EXTENSION; | |
| 91 | } | ||
| 92 | |||
| 93 | 3 | static int vmd_read_header(AVFormatContext *s) | |
| 94 | { | ||
| 95 | 3 | VmdDemuxContext *vmd = s->priv_data; | |
| 96 | 3 | AVIOContext *pb = s->pb; | |
| 97 | 3 | AVStream *st = NULL, *vst = NULL; | |
| 98 | unsigned int toc_offset; | ||
| 99 | unsigned char *raw_frame_table; | ||
| 100 | int raw_frame_table_size; | ||
| 101 | int64_t current_offset; | ||
| 102 | int i, j, ret; | ||
| 103 | int width, height; | ||
| 104 | unsigned int total_frames; | ||
| 105 | 3 | int64_t current_audio_pts = 0; | |
| 106 | unsigned char chunk[BYTES_PER_FRAME_RECORD]; | ||
| 107 | int num, den; | ||
| 108 | int sound_buffers; | ||
| 109 | |||
| 110 | /* fetch the main header, including the 2 header length bytes */ | ||
| 111 | 3 | avio_seek(pb, 0, SEEK_SET); | |
| 112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ffio_read_size(pb, vmd->vmd_header, VMD_HEADER_SIZE)) < 0) |
| 113 | ✗ | return ret; | |
| 114 | |||
| 115 | 3 | width = AV_RL16(&vmd->vmd_header[12]); | |
| 116 | 3 | height = AV_RL16(&vmd->vmd_header[14]); | |
| 117 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if (width && height) { |
| 118 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
3 | if(vmd->vmd_header[24] == 'i' && vmd->vmd_header[25] == 'v' && vmd->vmd_header[26] == '3') { |
| 119 | ✗ | vmd->is_indeo3 = 1; | |
| 120 | } else { | ||
| 121 | 3 | vmd->is_indeo3 = 0; | |
| 122 | } | ||
| 123 | /* start up the decoders */ | ||
| 124 | 3 | vst = avformat_new_stream(s, NULL); | |
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!vst) |
| 126 | ✗ | return AVERROR(ENOMEM); | |
| 127 | 3 | avpriv_set_pts_info(vst, 33, 1, 10); | |
| 128 | 3 | vmd->video_stream_index = vst->index; | |
| 129 | 3 | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | vst->codecpar->codec_id = vmd->is_indeo3 ? AV_CODEC_ID_INDEO3 : AV_CODEC_ID_VMDVIDEO; |
| 131 | 3 | vst->codecpar->codec_tag = 0; /* no fourcc */ | |
| 132 | 3 | vst->codecpar->width = width; | |
| 133 | 3 | vst->codecpar->height = height; | |
| 134 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3 | if(vmd->is_indeo3 && vst->codecpar->width > 320){ |
| 135 | ✗ | vst->codecpar->width >>= 1; | |
| 136 | ✗ | vst->codecpar->height >>= 1; | |
| 137 | } | ||
| 138 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0) |
| 139 | ✗ | return ret; | |
| 140 | 3 | memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE); | |
| 141 | } | ||
| 142 | |||
| 143 | /* if sample rate is 0, assume no audio */ | ||
| 144 | 3 | vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]); | |
| 145 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (vmd->sample_rate) { |
| 146 | int channels; | ||
| 147 | 3 | st = avformat_new_stream(s, NULL); | |
| 148 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!st) |
| 149 | ✗ | return AVERROR(ENOMEM); | |
| 150 | 3 | vmd->audio_stream_index = st->index; | |
| 151 | 3 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 152 | 3 | st->codecpar->codec_id = AV_CODEC_ID_VMDAUDIO; | |
| 153 | 3 | st->codecpar->codec_tag = 0; /* no fourcc */ | |
| 154 | 3 | st->codecpar->sample_rate = vmd->sample_rate; | |
| 155 | 3 | st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]); | |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (st->codecpar->block_align & 0x8000) { |
| 157 | ✗ | st->codecpar->bits_per_coded_sample = 16; | |
| 158 | ✗ | st->codecpar->block_align = -(st->codecpar->block_align - 0x10000); | |
| 159 | } else { | ||
| 160 | 3 | st->codecpar->bits_per_coded_sample = 8; | |
| 161 | } | ||
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (vmd->vmd_header[811] & 0x80) { |
| 163 | ✗ | channels = 2; | |
| 164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | } else if (vmd->vmd_header[811] & 0x2) { |
| 165 | /* Shivers 2 stereo audio */ | ||
| 166 | /* Frame length is for 1 channel */ | ||
| 167 | ✗ | channels = 2; | |
| 168 | ✗ | st->codecpar->block_align = st->codecpar->block_align << 1; | |
| 169 | } else { | ||
| 170 | 3 | channels = 1; | |
| 171 | } | ||
| 172 | 3 | av_channel_layout_default(&st->codecpar->ch_layout, channels); | |
| 173 | 3 | st->codecpar->bit_rate = st->codecpar->sample_rate * | |
| 174 | 3 | st->codecpar->bits_per_coded_sample * channels; | |
| 175 | |||
| 176 | /* calculate pts */ | ||
| 177 | 3 | num = st->codecpar->block_align; | |
| 178 | 3 | den = st->codecpar->sample_rate * channels; | |
| 179 | 3 | av_reduce(&num, &den, num, den, (1UL<<31)-1); | |
| 180 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (vst) |
| 181 | 3 | avpriv_set_pts_info(vst, 33, num, den); | |
| 182 | 3 | avpriv_set_pts_info(st, 33, num, den); | |
| 183 | } | ||
| 184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!s->nb_streams) |
| 185 | ✗ | return AVERROR_INVALIDDATA; | |
| 186 | |||
| 187 | 3 | toc_offset = AV_RL32(&vmd->vmd_header[812]); | |
| 188 | 3 | vmd->frame_count = AV_RL16(&vmd->vmd_header[6]); | |
| 189 | 3 | vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]); | |
| 190 | 3 | avio_seek(pb, toc_offset, SEEK_SET); | |
| 191 | |||
| 192 | 3 | raw_frame_table = NULL; | |
| 193 | 3 | vmd->frame_table = NULL; | |
| 194 | 3 | sound_buffers = AV_RL16(&vmd->vmd_header[808]); | |
| 195 | 3 | raw_frame_table_size = vmd->frame_count * 6; | |
| 196 | 3 | raw_frame_table = av_malloc(raw_frame_table_size); | |
| 197 | 3 | vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame)); | |
| 198 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (!raw_frame_table || !vmd->frame_table) { |
| 199 | ✗ | ret = AVERROR(ENOMEM); | |
| 200 | ✗ | goto error; | |
| 201 | } | ||
| 202 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if ((ret = ffio_read_size(pb, raw_frame_table, raw_frame_table_size)) < 0) |
| 203 | ✗ | goto error; | |
| 204 | |||
| 205 | 3 | total_frames = 0; | |
| 206 |
2/2✓ Branch 0 taken 645 times.
✓ Branch 1 taken 3 times.
|
648 | for (i = 0; i < vmd->frame_count; i++) { |
| 207 | |||
| 208 | 645 | current_offset = AV_RL32(&raw_frame_table[6 * i + 2]); | |
| 209 | |||
| 210 | /* handle each entry in index block */ | ||
| 211 |
2/2✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 645 times.
|
1935 | for (j = 0; j < vmd->frames_per_block; j++) { |
| 212 | int type; | ||
| 213 | uint32_t size; | ||
| 214 | |||
| 215 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1290 times.
|
1290 | if ((ret = avio_read(pb, chunk, BYTES_PER_FRAME_RECORD)) != BYTES_PER_FRAME_RECORD) { |
| 216 | ✗ | av_log(s, AV_LOG_ERROR, "Failed to read frame record\n"); | |
| 217 | ✗ | if (ret >= 0) | |
| 218 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 219 | ✗ | goto error; | |
| 220 | } | ||
| 221 | 1290 | type = chunk[0]; | |
| 222 | 1290 | size = AV_RL32(&chunk[2]); | |
| 223 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1290 times.
|
1290 | if (size > INT_MAX / 2) { |
| 224 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid frame size\n"); | |
| 225 | ✗ | ret = AVERROR_INVALIDDATA; | |
| 226 | ✗ | goto error; | |
| 227 | } | ||
| 228 |
4/4✓ Branch 0 taken 381 times.
✓ Branch 1 taken 909 times.
✓ Branch 2 taken 294 times.
✓ Branch 3 taken 87 times.
|
1290 | if(!size && type != 1) |
| 229 | 294 | continue; | |
| 230 |
2/3✓ Branch 0 taken 645 times.
✓ Branch 1 taken 351 times.
✗ Branch 2 not taken.
|
996 | switch(type) { |
| 231 | 645 | case 1: /* Audio Chunk */ | |
| 232 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
|
645 | if (!st) break; |
| 233 | /* first audio chunk contains several audio buffers */ | ||
| 234 | 645 | vmd->frame_table[total_frames].frame_offset = current_offset; | |
| 235 | 645 | vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index; | |
| 236 | 645 | vmd->frame_table[total_frames].frame_size = size; | |
| 237 | 645 | memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD); | |
| 238 | 645 | vmd->frame_table[total_frames].pts = current_audio_pts; | |
| 239 | 645 | total_frames++; | |
| 240 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 642 times.
|
645 | if(!current_audio_pts) |
| 241 | 3 | current_audio_pts += sound_buffers - 1; | |
| 242 | else | ||
| 243 | 642 | current_audio_pts++; | |
| 244 | 645 | break; | |
| 245 | 351 | case 2: /* Video Chunk */ | |
| 246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 351 times.
|
351 | if (!vst) |
| 247 | ✗ | break; | |
| 248 | 351 | vmd->frame_table[total_frames].frame_offset = current_offset; | |
| 249 | 351 | vmd->frame_table[total_frames].stream_index = vmd->video_stream_index; | |
| 250 | 351 | vmd->frame_table[total_frames].frame_size = size; | |
| 251 | 351 | memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD); | |
| 252 | 351 | vmd->frame_table[total_frames].pts = i; | |
| 253 | 351 | total_frames++; | |
| 254 | 351 | break; | |
| 255 | } | ||
| 256 | 996 | current_offset += size; | |
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | 3 | vmd->current_frame = 0; | |
| 262 | 3 | vmd->frame_count = total_frames; | |
| 263 | |||
| 264 | 3 | ret = 0; | |
| 265 | 3 | error: | |
| 266 | 3 | av_freep(&raw_frame_table); | |
| 267 | 3 | return ret; | |
| 268 | } | ||
| 269 | |||
| 270 | 731 | static int vmd_read_packet(AVFormatContext *s, | |
| 271 | AVPacket *pkt) | ||
| 272 | { | ||
| 273 | 731 | VmdDemuxContext *vmd = s->priv_data; | |
| 274 | 731 | AVIOContext *pb = s->pb; | |
| 275 | 731 | int ret = 0; | |
| 276 | vmd_frame *frame; | ||
| 277 | |||
| 278 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 729 times.
|
731 | if (vmd->current_frame >= vmd->frame_count) |
| 279 | 2 | return AVERROR_EOF; | |
| 280 | |||
| 281 | 729 | frame = &vmd->frame_table[vmd->current_frame]; | |
| 282 | /* position the stream (will probably be there already) */ | ||
| 283 | 729 | avio_seek(pb, frame->frame_offset, SEEK_SET); | |
| 284 | |||
| 285 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 729 times.
|
729 | if(ffio_limit(pb, frame->frame_size) != frame->frame_size) |
| 286 | ✗ | return AVERROR_INVALIDDATA; | |
| 287 | 729 | ret = av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD); | |
| 288 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 729 times.
|
729 | if (ret < 0) |
| 289 | ✗ | return ret; | |
| 290 | 729 | pkt->pos= avio_tell(pb); | |
| 291 | 729 | memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD); | |
| 292 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 729 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
729 | if(vmd->is_indeo3 && frame->frame_record[0] == 0x02) |
| 293 | ✗ | ret = ffio_read_size(pb, pkt->data, frame->frame_size); | |
| 294 | else | ||
| 295 | 729 | ret = ffio_read_size(pb, pkt->data + BYTES_PER_FRAME_RECORD, | |
| 296 | 729 | frame->frame_size); | |
| 297 | |||
| 298 | 729 | pkt->stream_index = frame->stream_index; | |
| 299 | 729 | pkt->pts = frame->pts; | |
| 300 | 729 | av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n", | |
| 301 | 729 | (frame->frame_record[0] == 0x02) ? "video" : "audio", | |
| 302 |
2/2✓ Branch 0 taken 267 times.
✓ Branch 1 taken 462 times.
|
729 | frame->frame_size + BYTES_PER_FRAME_RECORD, |
| 303 | pkt->pts); | ||
| 304 | |||
| 305 | 729 | vmd->current_frame++; | |
| 306 | |||
| 307 | 729 | return ret; | |
| 308 | } | ||
| 309 | |||
| 310 | 3 | static int vmd_read_close(AVFormatContext *s) | |
| 311 | { | ||
| 312 | 3 | VmdDemuxContext *vmd = s->priv_data; | |
| 313 | |||
| 314 | 3 | av_freep(&vmd->frame_table); | |
| 315 | |||
| 316 | 3 | return 0; | |
| 317 | } | ||
| 318 | |||
| 319 | const FFInputFormat ff_vmd_demuxer = { | ||
| 320 | .p.name = "vmd", | ||
| 321 | .p.long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"), | ||
| 322 | .priv_data_size = sizeof(VmdDemuxContext), | ||
| 323 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
| 324 | .read_probe = vmd_probe, | ||
| 325 | .read_header = vmd_read_header, | ||
| 326 | .read_packet = vmd_read_packet, | ||
| 327 | .read_close = vmd_read_close, | ||
| 328 | }; | ||
| 329 |