FFmpeg coverage


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