FFmpeg coverage


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