FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/sierravmd.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 137 165 83.0%
Functions: 4 4 100.0%
Branches: 49 91 53.8%

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 7480 static int vmd_probe(const AVProbeData *p)
68 {
69 int w, h, sample_rate;
70
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 7369 times.
7480 if (p->buf_size < 806)
71 111 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 7366 times.
✓ Branch 1 taken 3 times.
7369 if (AV_RL16(&p->buf[0]) != VMD_HEADER_SIZE - 2)
75 7366 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
83 /* only return half certainty since this check is a bit sketchy */
84 3 return AVPROBE_SCORE_EXTENSION;
85 }
86
87 3 static int vmd_read_header(AVFormatContext *s)
88 {
89 3 VmdDemuxContext *vmd = s->priv_data;
90 3 AVIOContext *pb = s->pb;
91 3 AVStream *st = NULL, *vst = NULL;
92 unsigned int toc_offset;
93 unsigned char *raw_frame_table;
94 int raw_frame_table_size;
95 int64_t current_offset;
96 int i, j, ret;
97 int width, height;
98 unsigned int total_frames;
99 3 int64_t current_audio_pts = 0;
100 unsigned char chunk[BYTES_PER_FRAME_RECORD];
101 int num, den;
102 int sound_buffers;
103
104 /* fetch the main header, including the 2 header length bytes */
105 3 avio_seek(pb, 0, SEEK_SET);
106
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)
107 return ret;
108
109 3 width = AV_RL16(&vmd->vmd_header[12]);
110 3 height = AV_RL16(&vmd->vmd_header[14]);
111
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (width && height) {
112
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') {
113 vmd->is_indeo3 = 1;
114 } else {
115 3 vmd->is_indeo3 = 0;
116 }
117 /* start up the decoders */
118 3 vst = avformat_new_stream(s, NULL);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!vst)
120 return AVERROR(ENOMEM);
121 3 avpriv_set_pts_info(vst, 33, 1, 10);
122 3 vmd->video_stream_index = vst->index;
123 3 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
124
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;
125 3 vst->codecpar->codec_tag = 0; /* no fourcc */
126 3 vst->codecpar->width = width;
127 3 vst->codecpar->height = height;
128
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){
129 vst->codecpar->width >>= 1;
130 vst->codecpar->height >>= 1;
131 }
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_alloc_extradata(vst->codecpar, VMD_HEADER_SIZE)) < 0)
133 return ret;
134 3 memcpy(vst->codecpar->extradata, vmd->vmd_header, VMD_HEADER_SIZE);
135 }
136
137 /* if sample rate is 0, assume no audio */
138 3 vmd->sample_rate = AV_RL16(&vmd->vmd_header[804]);
139
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (vmd->sample_rate) {
140 int channels;
141 3 st = avformat_new_stream(s, NULL);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!st)
143 return AVERROR(ENOMEM);
144 3 vmd->audio_stream_index = st->index;
145 3 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
146 3 st->codecpar->codec_id = AV_CODEC_ID_VMDAUDIO;
147 3 st->codecpar->codec_tag = 0; /* no fourcc */
148 3 st->codecpar->sample_rate = vmd->sample_rate;
149 3 st->codecpar->block_align = AV_RL16(&vmd->vmd_header[806]);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (st->codecpar->block_align & 0x8000) {
151 st->codecpar->bits_per_coded_sample = 16;
152 st->codecpar->block_align = -(st->codecpar->block_align - 0x10000);
153 } else {
154 3 st->codecpar->bits_per_coded_sample = 8;
155 }
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (vmd->vmd_header[811] & 0x80) {
157 channels = 2;
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (vmd->vmd_header[811] & 0x2) {
159 /* Shivers 2 stereo audio */
160 /* Frame length is for 1 channel */
161 channels = 2;
162 st->codecpar->block_align = st->codecpar->block_align << 1;
163 } else {
164 3 channels = 1;
165 }
166 3 av_channel_layout_default(&st->codecpar->ch_layout, channels);
167 3 st->codecpar->bit_rate = st->codecpar->sample_rate *
168 3 st->codecpar->bits_per_coded_sample * channels;
169
170 /* calculate pts */
171 3 num = st->codecpar->block_align;
172 3 den = st->codecpar->sample_rate * channels;
173 3 av_reduce(&num, &den, num, den, (1UL<<31)-1);
174
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (vst)
175 3 avpriv_set_pts_info(vst, 33, num, den);
176 3 avpriv_set_pts_info(st, 33, num, den);
177 }
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!s->nb_streams)
179 return AVERROR_INVALIDDATA;
180
181 3 toc_offset = AV_RL32(&vmd->vmd_header[812]);
182 3 vmd->frame_count = AV_RL16(&vmd->vmd_header[6]);
183 3 vmd->frames_per_block = AV_RL16(&vmd->vmd_header[18]);
184 3 avio_seek(pb, toc_offset, SEEK_SET);
185
186 3 raw_frame_table = NULL;
187 3 vmd->frame_table = NULL;
188 3 sound_buffers = AV_RL16(&vmd->vmd_header[808]);
189 3 raw_frame_table_size = vmd->frame_count * 6;
190 3 raw_frame_table = av_malloc(raw_frame_table_size);
191 3 vmd->frame_table = av_malloc_array(vmd->frame_count * vmd->frames_per_block + sound_buffers, sizeof(vmd_frame));
192
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) {
193 ret = AVERROR(ENOMEM);
194 goto error;
195 }
196
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)
197 goto error;
198
199 3 total_frames = 0;
200
2/2
✓ Branch 0 taken 645 times.
✓ Branch 1 taken 3 times.
648 for (i = 0; i < vmd->frame_count; i++) {
201
202 645 current_offset = AV_RL32(&raw_frame_table[6 * i + 2]);
203
204 /* handle each entry in index block */
205
2/2
✓ Branch 0 taken 1290 times.
✓ Branch 1 taken 645 times.
1935 for (j = 0; j < vmd->frames_per_block; j++) {
206 int type;
207 uint32_t size;
208
209
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) {
210 av_log(s, AV_LOG_ERROR, "Failed to read frame record\n");
211 if (ret >= 0)
212 ret = AVERROR_INVALIDDATA;
213 goto error;
214 }
215 1290 type = chunk[0];
216 1290 size = AV_RL32(&chunk[2]);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1290 times.
1290 if (size > INT_MAX / 2) {
218 av_log(s, AV_LOG_ERROR, "Invalid frame size\n");
219 ret = AVERROR_INVALIDDATA;
220 goto error;
221 }
222
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)
223 294 continue;
224
2/3
✓ Branch 0 taken 645 times.
✓ Branch 1 taken 351 times.
✗ Branch 2 not taken.
996 switch(type) {
225 645 case 1: /* Audio Chunk */
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 645 times.
645 if (!st) break;
227 /* first audio chunk contains several audio buffers */
228 645 vmd->frame_table[total_frames].frame_offset = current_offset;
229 645 vmd->frame_table[total_frames].stream_index = vmd->audio_stream_index;
230 645 vmd->frame_table[total_frames].frame_size = size;
231 645 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
232 645 vmd->frame_table[total_frames].pts = current_audio_pts;
233 645 total_frames++;
234
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 642 times.
645 if(!current_audio_pts)
235 3 current_audio_pts += sound_buffers - 1;
236 else
237 642 current_audio_pts++;
238 645 break;
239 351 case 2: /* Video Chunk */
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 351 times.
351 if (!vst)
241 break;
242 351 vmd->frame_table[total_frames].frame_offset = current_offset;
243 351 vmd->frame_table[total_frames].stream_index = vmd->video_stream_index;
244 351 vmd->frame_table[total_frames].frame_size = size;
245 351 memcpy(vmd->frame_table[total_frames].frame_record, chunk, BYTES_PER_FRAME_RECORD);
246 351 vmd->frame_table[total_frames].pts = i;
247 351 total_frames++;
248 351 break;
249 }
250 996 current_offset += size;
251 }
252 }
253
254
255 3 vmd->current_frame = 0;
256 3 vmd->frame_count = total_frames;
257
258 3 ret = 0;
259 3 error:
260 3 av_freep(&raw_frame_table);
261 3 return ret;
262 }
263
264 731 static int vmd_read_packet(AVFormatContext *s,
265 AVPacket *pkt)
266 {
267 731 VmdDemuxContext *vmd = s->priv_data;
268 731 AVIOContext *pb = s->pb;
269 731 int ret = 0;
270 vmd_frame *frame;
271
272
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 729 times.
731 if (vmd->current_frame >= vmd->frame_count)
273 2 return AVERROR_EOF;
274
275 729 frame = &vmd->frame_table[vmd->current_frame];
276 /* position the stream (will probably be there already) */
277 729 avio_seek(pb, frame->frame_offset, SEEK_SET);
278
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 729 times.
729 if(ffio_limit(pb, frame->frame_size) != frame->frame_size)
280 return AVERROR_INVALIDDATA;
281 729 ret = av_new_packet(pkt, frame->frame_size + BYTES_PER_FRAME_RECORD);
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 729 times.
729 if (ret < 0)
283 return ret;
284 729 pkt->pos= avio_tell(pb);
285 729 memcpy(pkt->data, frame->frame_record, BYTES_PER_FRAME_RECORD);
286
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)
287 ret = ffio_read_size(pb, pkt->data, frame->frame_size);
288 else
289 729 ret = ffio_read_size(pb, pkt->data + BYTES_PER_FRAME_RECORD,
290 729 frame->frame_size);
291
292 729 pkt->stream_index = frame->stream_index;
293 729 pkt->pts = frame->pts;
294 729 av_log(s, AV_LOG_DEBUG, " dispatching %s frame with %d bytes and pts %"PRId64"\n",
295 729 (frame->frame_record[0] == 0x02) ? "video" : "audio",
296
2/2
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 462 times.
729 frame->frame_size + BYTES_PER_FRAME_RECORD,
297 pkt->pts);
298
299 729 vmd->current_frame++;
300
301 729 return ret;
302 }
303
304 3 static int vmd_read_close(AVFormatContext *s)
305 {
306 3 VmdDemuxContext *vmd = s->priv_data;
307
308 3 av_freep(&vmd->frame_table);
309
310 3 return 0;
311 }
312
313 const FFInputFormat ff_vmd_demuxer = {
314 .p.name = "vmd",
315 .p.long_name = NULL_IF_CONFIG_SMALL("Sierra VMD"),
316 .priv_data_size = sizeof(VmdDemuxContext),
317 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
318 .read_probe = vmd_probe,
319 .read_header = vmd_read_header,
320 .read_packet = vmd_read_packet,
321 .read_close = vmd_read_close,
322 };
323