FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/ipmovie.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 259 360 71.9%
Functions: 6 6 100.0%
Branches: 108 187 57.8%

Line Branch Exec Source
1 /*
2 * Interplay MVE File Demuxer
3 * Copyright (c) 2003 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 * Interplay MVE file demuxer
25 * by Mike Melanson (melanson@pcisys.net)
26 * For more information regarding the Interplay MVE file format, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 * The aforementioned site also contains a command line utility for parsing
29 * IP MVE files so that you can get a good idea of the typical structure of
30 * such files. This demuxer is not the best example to use if you are trying
31 * to write your own as it uses a rather roundabout approach for splitting
32 * up and sending out the chunks.
33 */
34
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/intreadwrite.h"
37 #include "avformat.h"
38 #include "avio_internal.h"
39 #include "demux.h"
40 #include "internal.h"
41
42 #define CHUNK_PREAMBLE_SIZE 4
43 #define OPCODE_PREAMBLE_SIZE 4
44
45 #define CHUNK_INIT_AUDIO 0x0000
46 #define CHUNK_AUDIO_ONLY 0x0001
47 #define CHUNK_INIT_VIDEO 0x0002
48 #define CHUNK_VIDEO 0x0003
49 #define CHUNK_SHUTDOWN 0x0004
50 #define CHUNK_END 0x0005
51 /* these last types are used internally */
52 #define CHUNK_HAVE_PACKET 0xFFFB
53 #define CHUNK_DONE 0xFFFC
54 #define CHUNK_NOMEM 0xFFFD
55 #define CHUNK_EOF 0xFFFE
56 #define CHUNK_BAD 0xFFFF
57
58 #define OPCODE_END_OF_STREAM 0x00
59 #define OPCODE_END_OF_CHUNK 0x01
60 #define OPCODE_CREATE_TIMER 0x02
61 #define OPCODE_INIT_AUDIO_BUFFERS 0x03
62 #define OPCODE_START_STOP_AUDIO 0x04
63 #define OPCODE_INIT_VIDEO_BUFFERS 0x05
64 #define OPCODE_VIDEO_DATA_06 0x06
65 #define OPCODE_SEND_BUFFER 0x07
66 #define OPCODE_AUDIO_FRAME 0x08
67 #define OPCODE_SILENCE_FRAME 0x09
68 #define OPCODE_INIT_VIDEO_MODE 0x0A
69 #define OPCODE_CREATE_GRADIENT 0x0B
70 #define OPCODE_SET_PALETTE 0x0C
71 #define OPCODE_SET_PALETTE_COMPRESSED 0x0D
72 #define OPCODE_SET_SKIP_MAP 0x0E
73 #define OPCODE_SET_DECODING_MAP 0x0F
74 #define OPCODE_VIDEO_DATA_10 0x10
75 #define OPCODE_VIDEO_DATA_11 0x11
76 #define OPCODE_UNKNOWN_12 0x12
77 #define OPCODE_UNKNOWN_13 0x13
78 #define OPCODE_UNKNOWN_14 0x14
79 #define OPCODE_UNKNOWN_15 0x15
80
81 #define PALETTE_COUNT 256
82
83 typedef struct IPMVEContext {
84 AVFormatContext *avf;
85 unsigned char *buf;
86 int buf_size;
87
88 uint64_t frame_pts_inc;
89
90 unsigned int video_bpp;
91 unsigned int video_width;
92 unsigned int video_height;
93 int64_t video_pts;
94 uint32_t palette[256];
95 int has_palette;
96 int changed;
97 uint8_t send_buffer;
98 uint8_t frame_format;
99
100 unsigned int audio_bits;
101 unsigned int audio_channels;
102 unsigned int audio_sample_rate;
103 enum AVCodecID audio_type;
104 unsigned int audio_frame_count;
105
106 int video_stream_index;
107 int audio_stream_index;
108
109 int64_t audio_chunk_offset;
110 int audio_chunk_size;
111 int64_t video_chunk_offset;
112 int video_chunk_size;
113 int64_t skip_map_chunk_offset;
114 int skip_map_chunk_size;
115 int64_t decode_map_chunk_offset;
116 int decode_map_chunk_size;
117
118 int64_t next_chunk_offset;
119
120 } IPMVEContext;
121
122 933 static int load_ipmovie_packet(IPMVEContext *s, AVIOContext *pb,
123 AVPacket *pkt) {
124
125 int chunk_type;
126
127
4/6
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 606 times.
✓ Branch 2 taken 327 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 327 times.
✗ Branch 5 not taken.
933 if (s->audio_chunk_offset && s->audio_channels && s->audio_bits) {
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 if (s->audio_type == AV_CODEC_ID_NONE) {
129 av_log(s->avf, AV_LOG_ERROR, "Can not read audio packet before"
130 "audio codec is known\n");
131 return CHUNK_BAD;
132 }
133
134 /* adjust for PCM audio by skipping chunk header */
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM) {
136 s->audio_chunk_offset += 6;
137 s->audio_chunk_size -= 6;
138 }
139
140 327 avio_seek(pb, s->audio_chunk_offset, SEEK_SET);
141 327 s->audio_chunk_offset = 0;
142
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 327 times.
327 if (s->audio_chunk_size != av_get_packet(pb, pkt, s->audio_chunk_size))
144 return CHUNK_EOF;
145
146 327 pkt->stream_index = s->audio_stream_index;
147 327 pkt->pts = s->audio_frame_count;
148
149 /* audio frame maintenance */
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 327 times.
327 if (s->audio_type != AV_CODEC_ID_INTERPLAY_DPCM)
151 s->audio_frame_count +=
152 (s->audio_chunk_size / s->audio_channels / (s->audio_bits / 8));
153 else
154 327 s->audio_frame_count +=
155 327 (s->audio_chunk_size - 6 - s->audio_channels) / s->audio_channels;
156
157 327 av_log(s->avf, AV_LOG_TRACE, "sending audio frame with pts %"PRId64" (%d audio frames)\n",
158 pkt->pts, s->audio_frame_count);
159
160 327 chunk_type = CHUNK_HAVE_PACKET;
161
162
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 336 times.
606 } else if (s->frame_format) {
163
164 /* send the frame format, decode map, the video data, skip map, and the send_buffer flag together */
165
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 270 times.
270 if (av_new_packet(pkt, 8 + s->decode_map_chunk_size + s->video_chunk_size + s->skip_map_chunk_size))
167 return CHUNK_NOMEM;
168
169
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 268 times.
270 if (s->has_palette) {
170 uint8_t *pal;
171
172 2 pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE,
173 AVPALETTE_SIZE);
174
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (pal) {
175 2 memcpy(pal, s->palette, AVPALETTE_SIZE);
176 2 s->has_palette = 0;
177 }
178 }
179
180
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 267 times.
270 if (s->changed) {
181 3 ff_add_param_change(pkt, 0, 0, 0, s->video_width, s->video_height);
182 3 s->changed = 0;
183 }
184
185 270 AV_WL8(pkt->data, s->frame_format);
186 270 AV_WL8(pkt->data + 1, s->send_buffer);
187 270 AV_WL16(pkt->data + 2, s->video_chunk_size);
188 270 AV_WL16(pkt->data + 4, s->decode_map_chunk_size);
189 270 AV_WL16(pkt->data + 6, s->skip_map_chunk_size);
190
191 270 s->frame_format = 0;
192 270 s->send_buffer = 0;
193
194 270 pkt->pos = s->video_chunk_offset;
195 270 avio_seek(pb, s->video_chunk_offset, SEEK_SET);
196 270 s->video_chunk_offset = 0;
197
198 270 if (avio_read(pb, pkt->data + 8, s->video_chunk_size) !=
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 s->video_chunk_size) {
200 return CHUNK_EOF;
201 }
202
203
1/2
✓ Branch 0 taken 270 times.
✗ Branch 1 not taken.
270 if (s->decode_map_chunk_size) {
204 270 pkt->pos = s->decode_map_chunk_offset;
205 270 avio_seek(pb, s->decode_map_chunk_offset, SEEK_SET);
206 270 s->decode_map_chunk_offset = 0;
207
208 270 if (avio_read(pb, pkt->data + 8 + s->video_chunk_size,
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 s->decode_map_chunk_size) != s->decode_map_chunk_size) {
210 return CHUNK_EOF;
211 }
212 }
213
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 if (s->skip_map_chunk_size) {
215 pkt->pos = s->skip_map_chunk_offset;
216 avio_seek(pb, s->skip_map_chunk_offset, SEEK_SET);
217 s->skip_map_chunk_offset = 0;
218
219 if (avio_read(pb, pkt->data + 8 + s->video_chunk_size + s->decode_map_chunk_size,
220 s->skip_map_chunk_size) != s->skip_map_chunk_size) {
221 return CHUNK_EOF;
222 }
223 }
224
225 270 s->video_chunk_size = 0;
226 270 s->decode_map_chunk_size = 0;
227 270 s->skip_map_chunk_size = 0;
228
229 270 pkt->stream_index = s->video_stream_index;
230 270 pkt->pts = s->video_pts;
231
232 270 av_log(s->avf, AV_LOG_TRACE, "sending video frame with pts %"PRId64"\n", pkt->pts);
233
234 270 s->video_pts += s->frame_pts_inc;
235
236 270 chunk_type = CHUNK_HAVE_PACKET;
237
238 } else {
239
240 336 avio_seek(pb, s->next_chunk_offset, SEEK_SET);
241 336 chunk_type = CHUNK_DONE;
242
243 }
244
245 933 return chunk_type;
246 }
247
248 3 static int init_audio(AVFormatContext *s)
249 {
250 3 IPMVEContext *ipmovie = s->priv_data;
251 3 AVStream *st = avformat_new_stream(s, NULL);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!st)
253 return AVERROR(ENOMEM);
254 3 avpriv_set_pts_info(st, 32, 1, ipmovie->audio_sample_rate);
255 3 ipmovie->audio_stream_index = st->index;
256 3 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
257 3 st->codecpar->codec_id = ipmovie->audio_type;
258 3 st->codecpar->codec_tag = 0; /* no tag */
259 3 av_channel_layout_default(&st->codecpar->ch_layout, ipmovie->audio_channels);
260 3 st->codecpar->sample_rate = ipmovie->audio_sample_rate;
261 3 st->codecpar->bits_per_coded_sample = ipmovie->audio_bits;
262 3 st->codecpar->bit_rate = ipmovie->audio_channels * st->codecpar->sample_rate *
263 3 st->codecpar->bits_per_coded_sample;
264
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (st->codecpar->codec_id == AV_CODEC_ID_INTERPLAY_DPCM)
265 3 st->codecpar->bit_rate /= 2;
266 3 st->codecpar->block_align = ipmovie->audio_channels * st->codecpar->bits_per_coded_sample;
267
268 3 return 0;
269 }
270
271 /* This function loads and processes a single chunk in an IP movie file.
272 * It returns the type of chunk that was processed. */
273 606 static int process_ipmovie_chunk(IPMVEContext *s, AVIOContext *pb,
274 AVPacket *pkt)
275 {
276 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
277 int chunk_type;
278 int chunk_size;
279 unsigned char opcode_preamble[OPCODE_PREAMBLE_SIZE];
280 unsigned char opcode_type;
281 unsigned char opcode_version;
282 int opcode_size;
283 unsigned char scratch[1024];
284 int i, j;
285 int first_color, last_color;
286 int audio_flags;
287 unsigned char r, g, b;
288 unsigned int width, height;
289
290 /* see if there are any pending packets */
291 606 chunk_type = load_ipmovie_packet(s, pb, pkt);
292
2/2
✓ Branch 0 taken 270 times.
✓ Branch 1 taken 336 times.
606 if (chunk_type != CHUNK_DONE)
293 270 return chunk_type;
294
295 /* read the next chunk, wherever the file happens to be pointing */
296
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 336 times.
336 if (avio_feof(pb))
297 return CHUNK_EOF;
298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 336 times.
336 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
299 CHUNK_PREAMBLE_SIZE)
300 return CHUNK_BAD;
301 336 chunk_size = AV_RL16(&chunk_preamble[0]);
302 336 chunk_type = AV_RL16(&chunk_preamble[2]);
303
304 336 av_log(s->avf, AV_LOG_TRACE, "chunk type 0x%04X, 0x%04X bytes: ", chunk_type, chunk_size);
305
306
4/7
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 273 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
336 switch (chunk_type) {
307
308 3 case CHUNK_INIT_AUDIO:
309 3 av_log(s->avf, AV_LOG_TRACE, "initialize audio\n");
310 3 break;
311
312 57 case CHUNK_AUDIO_ONLY:
313 57 av_log(s->avf, AV_LOG_TRACE, "audio only\n");
314 57 break;
315
316 3 case CHUNK_INIT_VIDEO:
317 3 av_log(s->avf, AV_LOG_TRACE, "initialize video\n");
318 3 break;
319
320 273 case CHUNK_VIDEO:
321 273 av_log(s->avf, AV_LOG_TRACE, "video (and audio)\n");
322 273 break;
323
324 case CHUNK_SHUTDOWN:
325 av_log(s->avf, AV_LOG_TRACE, "shutdown\n");
326 break;
327
328 case CHUNK_END:
329 av_log(s->avf, AV_LOG_TRACE, "end\n");
330 break;
331
332 default:
333 av_log(s->avf, AV_LOG_TRACE, "invalid chunk\n");
334 chunk_type = CHUNK_BAD;
335 break;
336
337 }
338
339
3/4
✓ Branch 0 taken 2367 times.
✓ Branch 1 taken 333 times.
✓ Branch 2 taken 2367 times.
✗ Branch 3 not taken.
2700 while ((chunk_size > 0) && (chunk_type != CHUNK_BAD)) {
340
341 /* read the next chunk, wherever the file happens to be pointing */
342
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2364 times.
2367 if (avio_feof(pb)) {
343 3 chunk_type = CHUNK_EOF;
344 3 break;
345 }
346
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2364 times.
2364 if (avio_read(pb, opcode_preamble, CHUNK_PREAMBLE_SIZE) !=
347 CHUNK_PREAMBLE_SIZE) {
348 chunk_type = CHUNK_BAD;
349 break;
350 }
351
352 2364 opcode_size = AV_RL16(&opcode_preamble[0]);
353 2364 opcode_type = opcode_preamble[2];
354 2364 opcode_version = opcode_preamble[3];
355
356 2364 chunk_size -= OPCODE_PREAMBLE_SIZE;
357 2364 chunk_size -= opcode_size;
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2364 times.
2364 if (chunk_size < 0) {
359 av_log(s->avf, AV_LOG_TRACE, "chunk_size countdown just went negative\n");
360 chunk_type = CHUNK_BAD;
361 break;
362 }
363
364 2364 av_log(s->avf, AV_LOG_TRACE, " opcode type %02X, version %d, 0x%04X bytes: ",
365 opcode_type, opcode_version, opcode_size);
366
13/18
✗ Branch 0 not taken.
✓ Branch 1 taken 333 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 270 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 273 times.
✓ Branch 7 taken 270 times.
✓ Branch 8 taken 330 times.
✓ Branch 9 taken 329 times.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 273 times.
✓ Branch 16 taken 272 times.
✗ Branch 17 not taken.
2364 switch (opcode_type) {
367
368 case OPCODE_END_OF_STREAM:
369 av_log(s->avf, AV_LOG_TRACE, "end of stream\n");
370 avio_skip(pb, opcode_size);
371 break;
372
373 333 case OPCODE_END_OF_CHUNK:
374 333 av_log(s->avf, AV_LOG_TRACE, "end of chunk\n");
375 333 avio_skip(pb, opcode_size);
376 333 break;
377
378 3 case OPCODE_CREATE_TIMER:
379 3 av_log(s->avf, AV_LOG_TRACE, "create timer\n");
380
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if ((opcode_version > 0) || (opcode_size != 6)) {
381 av_log(s->avf, AV_LOG_TRACE, "bad create_timer opcode\n");
382 chunk_type = CHUNK_BAD;
383 break;
384 }
385
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_read(pb, scratch, opcode_size) !=
386 opcode_size) {
387 chunk_type = CHUNK_BAD;
388 break;
389 }
390 3 s->frame_pts_inc = ((uint64_t)AV_RL32(&scratch[0])) * AV_RL16(&scratch[4]);
391 3 break;
392
393 3 case OPCODE_INIT_AUDIO_BUFFERS:
394 3 av_log(s->avf, AV_LOG_TRACE, "initialize audio buffers\n");
395
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (opcode_version > 1 || opcode_size > 10 || opcode_size < 6) {
396 av_log(s->avf, AV_LOG_TRACE, "bad init_audio_buffers opcode\n");
397 chunk_type = CHUNK_BAD;
398 break;
399 }
400
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_read(pb, scratch, opcode_size) !=
401 opcode_size) {
402 chunk_type = CHUNK_BAD;
403 break;
404 }
405 3 s->audio_sample_rate = AV_RL16(&scratch[4]);
406 3 audio_flags = AV_RL16(&scratch[2]);
407 /* bit 0 of the flags: 0 = mono, 1 = stereo */
408 3 s->audio_channels = (audio_flags & 1) + 1;
409 /* bit 1 of the flags: 0 = 8 bit, 1 = 16 bit */
410 3 s->audio_bits = (((audio_flags >> 1) & 1) + 1) * 8;
411 /* bit 2 indicates compressed audio in version 1 opcode */
412
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if ((opcode_version == 1) && (audio_flags & 0x4))
413 3 s->audio_type = AV_CODEC_ID_INTERPLAY_DPCM;
414 else if (s->audio_bits == 16)
415 s->audio_type = AV_CODEC_ID_PCM_S16LE;
416 else
417 s->audio_type = AV_CODEC_ID_PCM_U8;
418 6 av_log(s->avf, AV_LOG_TRACE, "audio: %d bits, %d Hz, %s, %s format\n",
419 s->audio_bits, s->audio_sample_rate,
420
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 (s->audio_channels == 2) ? "stereo" : "mono",
421
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 (s->audio_type == AV_CODEC_ID_INTERPLAY_DPCM) ?
422 "Interplay audio" : "PCM");
423 3 break;
424
425 270 case OPCODE_START_STOP_AUDIO:
426 270 av_log(s->avf, AV_LOG_TRACE, "start/stop audio\n");
427 270 avio_skip(pb, opcode_size);
428 270 break;
429
430 3 case OPCODE_INIT_VIDEO_BUFFERS:
431 3 av_log(s->avf, AV_LOG_TRACE, "initialize video buffers\n");
432
3/6
✓ 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.
3 if ((opcode_version > 2) || (opcode_size > 8) || opcode_size < 4
433
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 || opcode_version == 2 && opcode_size < 8
434 ) {
435 av_log(s->avf, AV_LOG_TRACE, "bad init_video_buffers opcode\n");
436 chunk_type = CHUNK_BAD;
437 break;
438 }
439
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_read(pb, scratch, opcode_size) !=
440 opcode_size) {
441 chunk_type = CHUNK_BAD;
442 break;
443 }
444 3 width = AV_RL16(&scratch[0]) * 8;
445 3 height = AV_RL16(&scratch[2]) * 8;
446
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (width != s->video_width) {
447 3 s->video_width = width;
448 3 s->changed++;
449 }
450
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (height != s->video_height) {
451 3 s->video_height = height;
452 3 s->changed++;
453 }
454
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 if (opcode_version < 2 || !AV_RL16(&scratch[6])) {
455 2 s->video_bpp = 8;
456 } else {
457 1 s->video_bpp = 16;
458 }
459 3 av_log(s->avf, AV_LOG_TRACE, "video resolution: %d x %d\n",
460 s->video_width, s->video_height);
461 3 break;
462
463 273 case OPCODE_UNKNOWN_12:
464 case OPCODE_UNKNOWN_13:
465 case OPCODE_UNKNOWN_14:
466 case OPCODE_UNKNOWN_15:
467 273 av_log(s->avf, AV_LOG_TRACE, "unknown (but documented) opcode %02X\n", opcode_type);
468 273 avio_skip(pb, opcode_size);
469 273 break;
470
471 270 case OPCODE_SEND_BUFFER:
472 270 av_log(s->avf, AV_LOG_TRACE, "send buffer\n");
473 270 avio_skip(pb, opcode_size);
474 270 s->send_buffer = 1;
475 270 break;
476
477 330 case OPCODE_AUDIO_FRAME:
478 330 av_log(s->avf, AV_LOG_TRACE, "audio frame\n");
479
480 /* log position and move on for now */
481 330 s->audio_chunk_offset = avio_tell(pb);
482 330 s->audio_chunk_size = opcode_size;
483 330 avio_skip(pb, opcode_size);
484 330 break;
485
486 329 case OPCODE_SILENCE_FRAME:
487 329 av_log(s->avf, AV_LOG_TRACE, "silence frame\n");
488 329 avio_skip(pb, opcode_size);
489 329 break;
490
491 3 case OPCODE_INIT_VIDEO_MODE:
492 3 av_log(s->avf, AV_LOG_TRACE, "initialize video mode\n");
493 3 avio_skip(pb, opcode_size);
494 3 break;
495
496 case OPCODE_CREATE_GRADIENT:
497 av_log(s->avf, AV_LOG_TRACE, "create gradient\n");
498 avio_skip(pb, opcode_size);
499 break;
500
501 2 case OPCODE_SET_PALETTE:
502 2 av_log(s->avf, AV_LOG_TRACE, "set palette\n");
503 /* check for the logical maximum palette size
504 * (3 * 256 + 4 bytes) */
505
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (opcode_size > 0x304 || opcode_size < 4) {
506 av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette opcode with invalid size\n");
507 chunk_type = CHUNK_BAD;
508 break;
509 }
510
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_read(pb, scratch, opcode_size) != opcode_size) {
511 chunk_type = CHUNK_BAD;
512 break;
513 }
514
515 /* load the palette into internal data structure */
516 2 first_color = AV_RL16(&scratch[0]);
517 2 last_color = first_color + AV_RL16(&scratch[2]) - 1;
518 /* sanity check (since they are 16 bit values) */
519
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if ( (first_color > 0xFF) || (last_color > 0xFF)
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 || (last_color - first_color + 1)*3 + 4 > opcode_size) {
521 av_log(s->avf, AV_LOG_TRACE, "demux_ipmovie: set_palette indexes out of range (%d -> %d)\n",
522 first_color, last_color);
523 chunk_type = CHUNK_BAD;
524 break;
525 }
526 2 j = 4; /* offset of first palette data */
527
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 2 times.
510 for (i = first_color; i <= last_color; i++) {
528 /* the palette is stored as a 6-bit VGA palette, thus each
529 * component is shifted up to a 8-bit range */
530 508 r = scratch[j++] * 4;
531 508 g = scratch[j++] * 4;
532 508 b = scratch[j++] * 4;
533 508 s->palette[i] = (0xFFU << 24) | (r << 16) | (g << 8) | (b);
534 508 s->palette[i] |= s->palette[i] >> 6 & 0x30303;
535 }
536 2 s->has_palette = 1;
537 2 break;
538
539 case OPCODE_SET_PALETTE_COMPRESSED:
540 av_log(s->avf, AV_LOG_TRACE, "set palette compressed\n");
541 avio_skip(pb, opcode_size);
542 break;
543
544 case OPCODE_SET_SKIP_MAP:
545 av_log(s->avf, AV_LOG_TRACE, "set skip map\n");
546
547 /* log position and move on for now */
548 s->skip_map_chunk_offset = avio_tell(pb);
549 s->skip_map_chunk_size = opcode_size;
550 avio_skip(pb, opcode_size);
551 break;
552
553 273 case OPCODE_SET_DECODING_MAP:
554 273 av_log(s->avf, AV_LOG_TRACE, "set decoding map\n");
555
556 /* log position and move on for now */
557 273 s->decode_map_chunk_offset = avio_tell(pb);
558 273 s->decode_map_chunk_size = opcode_size;
559 273 avio_skip(pb, opcode_size);
560 273 break;
561
562 272 case OPCODE_VIDEO_DATA_06:
563 case OPCODE_VIDEO_DATA_10:
564 case OPCODE_VIDEO_DATA_11:
565 272 s->frame_format = opcode_type;
566 272 av_log(s->avf, AV_LOG_TRACE, "set video data format 0x%02X\n",
567 opcode_type);
568
569 /* log position and move on for now */
570 272 s->video_chunk_offset = avio_tell(pb);
571 272 s->video_chunk_size = opcode_size;
572 272 avio_skip(pb, opcode_size);
573 272 break;
574
575 default:
576 av_log(s->avf, AV_LOG_TRACE, "*** unknown opcode type\n");
577 chunk_type = CHUNK_BAD;
578 break;
579
580 }
581 }
582
583
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
336 if (s->avf->nb_streams == 1 && s->audio_type)
584 init_audio(s->avf);
585
586 /* make a note of where the stream is sitting */
587 336 s->next_chunk_offset = avio_tell(pb);
588
589 336 return chunk_type;
590 }
591
592 static const char signature[] = "Interplay MVE File\x1A\0\x1A";
593
594 7211 static int ipmovie_probe(const AVProbeData *p)
595 {
596 7211 const uint8_t *b = p->buf;
597 7211 const uint8_t *b_end = p->buf + p->buf_size - sizeof(signature);
598 do {
599
4/4
✓ Branch 0 taken 747769 times.
✓ Branch 1 taken 385561190 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 747766 times.
386308959 if (b[0] == signature[0] && memcmp(b, signature, sizeof(signature)) == 0)
600 3 return AVPROBE_SCORE_MAX;
601 386308956 b++;
602
2/2
✓ Branch 0 taken 386301748 times.
✓ Branch 1 taken 7208 times.
386308956 } while (b < b_end);
603
604 7208 return 0;
605 }
606
607 3 static int ipmovie_read_header(AVFormatContext *s)
608 {
609 3 IPMVEContext *ipmovie = s->priv_data;
610 3 AVIOContext *pb = s->pb;
611 AVStream *st;
612 unsigned char chunk_preamble[CHUNK_PREAMBLE_SIZE];
613 int chunk_type, i;
614 uint8_t signature_buffer[sizeof(signature)];
615 int ret;
616
617 3 ipmovie->avf = s;
618
619 3 ret = ffio_read_size(pb, signature_buffer, sizeof(signature_buffer));
620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
621 return ret;
622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 while (memcmp(signature_buffer, signature, sizeof(signature))) {
623 memmove(signature_buffer, signature_buffer + 1, sizeof(signature_buffer) - 1);
624 signature_buffer[sizeof(signature_buffer) - 1] = avio_r8(pb);
625 if (avio_feof(pb))
626 return AVERROR_EOF;
627 }
628
629 /* on the first read, this will position the stream at the first chunk */
630 3 ipmovie->next_chunk_offset = avio_tell(pb) + 4;
631
632
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 3 times.
771 for (i = 0; i < 256; i++)
633 768 ipmovie->palette[i] = 0xFFU << 24;
634
635 /* process the first chunk which should be CHUNK_INIT_VIDEO */
636
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (process_ipmovie_chunk(ipmovie, pb, NULL) != CHUNK_INIT_VIDEO) {
637 return AVERROR_INVALIDDATA;
638 }
639
640 /* peek ahead to the next chunk-- if it is an init audio chunk, process
641 * it; if it is the first video chunk, this is a silent file */
642
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_read(pb, chunk_preamble, CHUNK_PREAMBLE_SIZE) !=
643 CHUNK_PREAMBLE_SIZE)
644 return AVERROR(EIO);
645 3 chunk_type = AV_RL16(&chunk_preamble[2]);
646 3 avio_seek(pb, -CHUNK_PREAMBLE_SIZE, SEEK_CUR);
647
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (chunk_type == CHUNK_VIDEO)
649 ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
650
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 else if (process_ipmovie_chunk(ipmovie, pb, ffformatcontext(s)->parse_pkt) != CHUNK_INIT_AUDIO) {
651 return AVERROR_INVALIDDATA;
652 }
653
654 /* initialize the stream decoders */
655 3 st = avformat_new_stream(s, NULL);
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!st)
657 return AVERROR(ENOMEM);
658 3 avpriv_set_pts_info(st, 63, 1, 1000000);
659 3 ipmovie->video_stream_index = st->index;
660 3 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
661 3 st->codecpar->codec_id = AV_CODEC_ID_INTERPLAY_VIDEO;
662 3 st->codecpar->codec_tag = 0; /* no fourcc */
663 3 st->codecpar->width = ipmovie->video_width;
664 3 st->codecpar->height = ipmovie->video_height;
665 3 st->codecpar->bits_per_coded_sample = ipmovie->video_bpp;
666
667
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ipmovie->audio_type) {
668 3 return init_audio(s);
669 } else
670 s->ctx_flags |= AVFMTCTX_NOHEADER;
671
672 return 0;
673 }
674
675 600 static int ipmovie_read_packet(AVFormatContext *s,
676 AVPacket *pkt)
677 {
678 600 IPMVEContext *ipmovie = s->priv_data;
679 600 AVIOContext *pb = s->pb;
680 int ret;
681
682 for (;;) {
683 600 ret = process_ipmovie_chunk(ipmovie, pb, pkt);
684 /* dispatch the first of any pending packets */
685
4/4
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 270 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 273 times.
600 if ((ret == CHUNK_VIDEO) || (ret == CHUNK_AUDIO_ONLY))
686 327 ret = load_ipmovie_packet(ipmovie, pb, pkt);
687
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (ret == CHUNK_BAD)
689 ret = AVERROR_INVALIDDATA;
690
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 597 times.
600 else if (ret == CHUNK_EOF)
691 3 ret = AVERROR(EIO);
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 597 times.
597 else if (ret == CHUNK_NOMEM)
693 ret = AVERROR(ENOMEM);
694
2/4
✓ Branch 0 taken 597 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 597 times.
597 else if (ret == CHUNK_END || ret == CHUNK_SHUTDOWN)
695 ret = AVERROR_EOF;
696
1/2
✓ Branch 0 taken 597 times.
✗ Branch 1 not taken.
597 else if (ret == CHUNK_HAVE_PACKET)
697 597 ret = 0;
698 else if (ret == CHUNK_INIT_VIDEO || ret == CHUNK_INIT_AUDIO)
699 continue;
700 else
701 continue;
702
703 600 return ret;
704 }
705 }
706
707 const FFInputFormat ff_ipmovie_demuxer = {
708 .p.name = "ipmovie",
709 .p.long_name = NULL_IF_CONFIG_SMALL("Interplay MVE"),
710 .priv_data_size = sizeof(IPMVEContext),
711 .read_probe = ipmovie_probe,
712 .read_header = ipmovie_read_header,
713 .read_packet = ipmovie_read_packet,
714 };
715