FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/electronicarts.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 373 467 79.9%
Functions: 12 12 100.0%
Branches: 168 250 67.2%

Line Branch Exec Source
1 /* Electronic Arts Multimedia File Demuxer
2 * Copyright (c) 2004 The FFmpeg project
3 * Copyright (c) 2006-2008 Peter Ross
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 * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
25 * by Robin Kay (komadori at gekkou.co.uk)
26 */
27
28 #include <inttypes.h>
29
30 #include "libavutil/attributes.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/opt.h"
33 #include "avformat.h"
34 #include "demux.h"
35 #include "internal.h"
36
37 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
38 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
39 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
40 #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
41 #define SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
42 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
43 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
44 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
45 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
46 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
47 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
48 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
49 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
50 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
51 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
52 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV I-frame */
53 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV P-frame */
54 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
55 #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD I-frame */
56 #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD P-frame */
57 #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
58 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG-2 */
59 #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ I-frame (appears in .TGQ files) */
60 #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ I-frame (appears in .UV files) */
61 #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T') /* TQI/UV2 I-frame (.UV2/.WVE) */
62 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
63 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
64 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
65 #define AVhd_TAG MKTAG('A', 'V', 'h', 'd')
66 #define AV0K_TAG MKTAG('A', 'V', '0', 'K')
67 #define AV0F_TAG MKTAG('A', 'V', '0', 'F')
68 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
69 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV I-frame */
70 #define AVP6_TAG MKTAG('A', 'V', 'P', '6')
71
72 typedef struct VideoProperties {
73 enum AVCodecID codec;
74 AVRational time_base;
75 int width, height;
76 int nb_frames;
77 int stream_index;
78 } VideoProperties;
79
80 typedef struct EaDemuxContext {
81 const AVClass *class;
82
83 int big_endian;
84
85 VideoProperties video, alpha;
86
87 enum AVCodecID audio_codec;
88 int audio_stream_index;
89
90 int bytes;
91 int sample_rate;
92 int num_channels;
93 int num_samples;
94
95 int platform;
96 int merge_alpha;
97 } EaDemuxContext;
98
99 64 static uint32_t read_arbitrary(AVIOContext *pb)
100 {
101 uint8_t size, byte;
102 int i;
103 uint32_t word;
104
105 64 size = avio_r8(pb);
106
107 64 word = 0;
108
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 64 times.
182 for (i = 0; i < size; i++) {
109 118 byte = avio_r8(pb);
110 118 word <<= 8;
111 118 word |= byte;
112 }
113
114 64 return word;
115 }
116
117 9 static int process_audio_header_elements(AVFormatContext *s)
118 {
119 9 EaDemuxContext *ea = s->priv_data;
120 9 AVIOContext *pb = s->pb;
121 9 int in_header = 1;
122 9 int compression_type = -1, revision = -1, revision2 = -1;
123
124 9 ea->bytes = 2;
125 9 ea->sample_rate = -1;
126 9 ea->num_channels = 1;
127
128
3/4
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 9 times.
44 while (!avio_feof(pb) && in_header) {
129 int in_subheader;
130 uint8_t byte;
131 35 byte = avio_r8(pb);
132
133
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
35 switch (byte) {
134 9 case 0xFD:
135 9 av_log(s, AV_LOG_DEBUG, "entered audio subheader\n");
136 9 in_subheader = 1;
137
3/4
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 47 times.
✓ Branch 4 taken 9 times.
56 while (!avio_feof(pb) && in_subheader) {
138 uint8_t subbyte;
139 47 subbyte = avio_r8(pb);
140
141
9/9
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 9 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 4 times.
47 switch (subbyte) {
142 5 case 0x80:
143 5 revision = read_arbitrary(pb);
144 5 av_log(s, AV_LOG_DEBUG,
145 "revision (element 0x80) set to 0x%08x\n", revision);
146 5 break;
147 9 case 0x82:
148 9 ea->num_channels = read_arbitrary(pb);
149 9 av_log(s, AV_LOG_DEBUG,
150 "num_channels (element 0x82) set to 0x%08x\n",
151 ea->num_channels);
152 9 break;
153 4 case 0x83:
154 4 compression_type = read_arbitrary(pb);
155 4 av_log(s, AV_LOG_DEBUG,
156 "compression_type (element 0x83) set to 0x%08x\n",
157 compression_type);
158 4 break;
159 5 case 0x84:
160 5 ea->sample_rate = read_arbitrary(pb);
161 5 av_log(s, AV_LOG_DEBUG,
162 "sample_rate (element 0x84) set to %i\n",
163 ea->sample_rate);
164 5 break;
165 9 case 0x85:
166 9 ea->num_samples = read_arbitrary(pb);
167 9 av_log(s, AV_LOG_DEBUG,
168 "num_samples (element 0x85) set to 0x%08x\n",
169 ea->num_samples);
170 9 break;
171 4 case 0x8A:
172 4 av_log(s, AV_LOG_DEBUG,
173 "element 0x%02x set to 0x%08"PRIx32"\n",
174 subbyte, read_arbitrary(pb));
175 4 av_log(s, AV_LOG_DEBUG, "exited audio subheader\n");
176 4 in_subheader = 0;
177 4 break;
178 2 case 0xA0:
179 2 revision2 = read_arbitrary(pb);
180 2 av_log(s, AV_LOG_DEBUG,
181 "revision2 (element 0xA0) set to 0x%08x\n",
182 revision2);
183 2 break;
184 5 case 0xFF:
185 5 av_log(s, AV_LOG_DEBUG,
186 "end of header block reached (within audio subheader)\n");
187 5 in_subheader = 0;
188 5 in_header = 0;
189 5 break;
190 4 default:
191 4 av_log(s, AV_LOG_DEBUG,
192 "element 0x%02x set to 0x%08"PRIx32"\n",
193 subbyte, read_arbitrary(pb));
194 4 break;
195 }
196 }
197 9 break;
198 4 case 0xFF:
199 4 av_log(s, AV_LOG_DEBUG, "end of header block reached\n");
200 4 in_header = 0;
201 4 break;
202 case 0x1B:
203 ea->video.time_base = (AVRational) {1, read_arbitrary(pb)};
204 av_log(s, AV_LOG_DEBUG, "Setting framerate to %u\n", ea->video.time_base.den);
205 break;
206 22 default:
207 22 av_log(s, AV_LOG_DEBUG,
208 "header element 0x%02x set to 0x%08"PRIx32"\n",
209 byte, read_arbitrary(pb));
210 22 break;
211 }
212 }
213
214
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
9 switch (compression_type) {
215 case 0:
216 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
217 break;
218 4 case 7:
219 4 ea->audio_codec = AV_CODEC_ID_ADPCM_EA;
220 4 break;
221 5 case -1:
222
2/5
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
5 switch (revision) {
223 3 case 1:
224 3 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1;
225 3 break;
226 case 2:
227 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2;
228 break;
229 2 case 3:
230 2 ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3;
231 2 break;
232 case -1:
233 break;
234 default:
235 avpriv_request_sample(s, "stream type; revision=%i", revision);
236 return 0;
237 }
238
3/5
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
5 switch (revision2) {
239 1 case 8:
240 1 ea->audio_codec = AV_CODEC_ID_PCM_S16LE_PLANAR;
241 1 break;
242 1 case 10:
243
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 switch (revision) {
244 case -1:
245 case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
246 1 case 3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
247 default:
248 avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
249 return 0;
250 }
251 1 break;
252 case 15:
253 case 16:
254 ea->audio_codec = AV_CODEC_ID_MP3;
255 break;
256 3 case -1:
257 3 break;
258 default:
259 ea->audio_codec = AV_CODEC_ID_NONE;
260 avpriv_request_sample(s, "stream type; revision2=%i", revision2);
261 return 0;
262 }
263 5 break;
264 default:
265 avpriv_request_sample(s,
266 "stream type; compression_type=%i",
267 compression_type);
268 return 0;
269 }
270
271
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9 if (ea->audio_codec == AV_CODEC_ID_NONE && ea->platform == 0x01)
272 ea->audio_codec = AV_CODEC_ID_ADPCM_PSX;
273
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
9 if (ea->sample_rate == -1)
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 ea->sample_rate = revision == 3 ? 48000 : 22050;
275
276 9 return 1;
277 }
278
279 3 static void process_audio_header_eacs(AVFormatContext *s)
280 {
281 3 EaDemuxContext *ea = s->priv_data;
282 3 AVIOContext *pb = s->pb;
283 int compression_type;
284
285
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
286 3 ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
287 3 ea->num_channels = avio_r8(pb);
288 3 compression_type = avio_r8(pb);
289 3 avio_skip(pb, 13);
290
291
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
3 switch (compression_type) {
292 case 0:
293 switch (ea->bytes) {
294 case 1:
295 ea->audio_codec = AV_CODEC_ID_PCM_S8;
296 break;
297 case 2:
298 ea->audio_codec = AV_CODEC_ID_PCM_S16LE;
299 break;
300 }
301 break;
302 1 case 1:
303 1 ea->audio_codec = AV_CODEC_ID_PCM_MULAW;
304 1 ea->bytes = 1;
305 1 break;
306 2 case 2:
307 2 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS;
308 2 break;
309 default:
310 avpriv_request_sample(s,
311 "stream type; audio compression_type=%i",
312 compression_type);
313 }
314 3 }
315
316 2 static void process_audio_header_sead(AVFormatContext *s)
317 {
318 2 EaDemuxContext *ea = s->priv_data;
319 2 AVIOContext *pb = s->pb;
320
321 2 ea->sample_rate = avio_rl32(pb);
322 2 ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
323 2 ea->num_channels = avio_rl32(pb);
324 2 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
325 2 }
326
327 2 static void process_video_header_mdec(AVFormatContext *s, VideoProperties *video)
328 {
329 2 AVIOContext *pb = s->pb;
330 2 avio_skip(pb, 4);
331 2 video->width = avio_rl16(pb);
332 2 video->height = avio_rl16(pb);
333
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!video->time_base.num)
334 2 video->time_base = (AVRational) { 1, 15 };
335 2 video->codec = AV_CODEC_ID_MDEC;
336 2 }
337
338 5 static int process_video_header_vp6(AVFormatContext *s, VideoProperties *video)
339 {
340 5 AVIOContext *pb = s->pb;
341
342 5 avio_skip(pb, 8);
343 5 video->nb_frames = avio_rl32(pb);
344 5 avio_skip(pb, 4);
345 5 video->time_base.den = avio_rl32(pb);
346 5 video->time_base.num = avio_rl32(pb);
347
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (video->time_base.den <= 0 || video->time_base.num <= 0) {
348 av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
349 return AVERROR_INVALIDDATA;
350 }
351 5 video->codec = AV_CODEC_ID_VP6;
352
353 5 return 1;
354 }
355
356 1 static void process_video_header_cmv(AVFormatContext *s, VideoProperties *video)
357 {
358 int fps;
359
360 1 avio_skip(s->pb, 10);
361 1 fps = avio_rl16(s->pb);
362
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (fps)
363 1 video->time_base = (AVRational) { 1, fps };
364 1 video->codec = AV_CODEC_ID_CMV;
365 1 }
366
367 /* Process EA file header.
368 * Return 1 if the EA file is valid and successfully opened, 0 otherwise. */
369 18 static int process_ea_header(AVFormatContext *s)
370 {
371 18 uint32_t blockid, size = 0;
372 18 EaDemuxContext *ea = s->priv_data;
373 18 AVIOContext *pb = s->pb;
374 int i;
375
376
6/6
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 14 times.
67 for (i = 0; i < 5 && (!ea->audio_codec || !ea->video.codec); i++) {
377 49 uint64_t startpos = avio_tell(pb);
378 49 int err = 0;
379
380 49 blockid = avio_rl32(pb);
381 49 size = avio_rl32(pb);
382
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 31 times.
49 if (i == 0)
383 18 ea->big_endian = size > av_bswap32(size);
384
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 46 times.
49 if (ea->big_endian)
385 3 size = av_bswap32(size);
386
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (size < 8) {
388 av_log(s, AV_LOG_ERROR, "chunk size too small\n");
389 return AVERROR_INVALIDDATA;
390 }
391
392
13/13
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 16 times.
49 switch (blockid) {
393 3 case ISNh_TAG:
394
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_rl32(pb) != EACS_TAG) {
395 avpriv_request_sample(s, "unknown 1SNh headerid");
396 return 0;
397 }
398 3 process_audio_header_eacs(s);
399 33 break;
400
401 9 case SCHl_TAG:
402 case SHEN_TAG:
403 9 blockid = avio_rl32(pb);
404
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (blockid == GSTR_TAG) {
405 1 avio_skip(pb, 4);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 } else if ((blockid & 0xFF) != (PT00_TAG & 0xFF)) {
407 blockid = avio_rl32(pb);
408 }
409 9 ea->platform = (blockid >> 16) & 0xFF;
410 9 err = process_audio_header_elements(s);
411 9 break;
412
413 2 case SEAD_TAG:
414 2 process_audio_header_sead(s);
415 2 break;
416
417 1 case MVIh_TAG:
418 1 process_video_header_cmv(s, &ea->video);
419 1 break;
420
421 4 case kVGT_TAG:
422 4 ea->video.codec = AV_CODEC_ID_TGV;
423 4 break;
424
425 2 case mTCD_TAG:
426 2 process_video_header_mdec(s, &ea->video);
427 2 break;
428
429 1 case MPCh_TAG:
430 1 ea->video.codec = AV_CODEC_ID_MPEG2VIDEO;
431 1 break;
432
433 1 case pQGT_TAG:
434 case TGQs_TAG:
435 1 ea->video.codec = AV_CODEC_ID_TGQ;
436
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!ea->video.time_base.num)
437 1 ea->video.time_base = (AVRational) { 1, 15 };
438 1 break;
439
440 2 case pIQT_TAG:
441 2 ea->video.codec = AV_CODEC_ID_TQI;
442
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!ea->video.time_base.num)
443 2 ea->video.time_base = (AVRational) { 1, 15 };
444 2 break;
445
446 3 case MADk_TAG:
447 3 ea->video.codec = AV_CODEC_ID_MAD;
448 3 avio_skip(pb, 6);
449 3 ea->video.time_base = (AVRational) { avio_rl16(pb), 1000 };
450 3 break;
451
452 4 case MVhd_TAG:
453 4 err = process_video_header_vp6(s, &ea->video);
454 4 break;
455
456 1 case AVhd_TAG:
457 1 err = process_video_header_vp6(s, &ea->alpha);
458
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (err >= 0 && ea->video.codec == AV_CODEC_ID_VP6 && ea->merge_alpha) {
459 ea->alpha.codec = 0;
460 ea->video.codec = AV_CODEC_ID_VP6A;
461 }
462 1 break;
463 }
464
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (err < 0) {
466 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
467 return err;
468 }
469
470 49 avio_seek(pb, startpos + size, SEEK_SET);
471 }
472
473 18 avio_seek(pb, 0, SEEK_SET);
474
475 18 return 1;
476 }
477
478 7480 static int ea_probe(const AVProbeData *p)
479 {
480 unsigned big_endian, size;
481
482
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 7462 times.
7480 switch (AV_RL32(&p->buf[0])) {
483 18 case ISNh_TAG:
484 case SCHl_TAG:
485 case SEAD_TAG:
486 case SHEN_TAG:
487 case kVGT_TAG:
488 case MADk_TAG:
489 case MPCh_TAG:
490 case MVhd_TAG:
491 case MVIh_TAG:
492 case AVP6_TAG:
493 18 break;
494 7462 default:
495 7462 return 0;
496 }
497 18 size = AV_RL32(&p->buf[4]);
498 18 big_endian = size > 0x000FFFFF;
499
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17 times.
18 if (big_endian)
500 1 size = av_bswap32(size);
501
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (size > 0xfffff || size < 8)
502 return 0;
503
504 18 return AVPROBE_SCORE_MAX;
505 }
506
507 36 static int init_video_stream(AVFormatContext *s, VideoProperties *video)
508 {
509 AVStream *st;
510
511
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 19 times.
36 if (!video->codec)
512 17 return 0;
513
514 /* initialize the video decoder stream */
515 19 st = avformat_new_stream(s, NULL);
516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!st)
517 return AVERROR(ENOMEM);
518 19 video->stream_index = st->index;
519 19 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
520 19 st->codecpar->codec_id = video->codec;
521 // parsing is necessary to make FFmpeg generate correct timestamps
522
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18 times.
19 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
523 1 ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS;
524 19 st->codecpar->codec_tag = 0; /* no fourcc */
525 19 st->codecpar->width = video->width;
526 19 st->codecpar->height = video->height;
527 19 st->duration = st->nb_frames = video->nb_frames;
528
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
19 if (video->time_base.num)
529 14 avpriv_set_pts_info(st, 64, video->time_base.num, video->time_base.den);
530 19 st->r_frame_rate =
531 19 st->avg_frame_rate = av_inv_q(video->time_base);
532 19 return 0;
533 }
534
535 18 static int ea_read_header(AVFormatContext *s)
536 {
537 18 EaDemuxContext *ea = s->priv_data;
538 AVStream *st;
539
540
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if (process_ea_header(s)<=0)
541 return AVERROR_INVALIDDATA;
542
543
2/4
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
18 if (init_video_stream(s, &ea->video) || init_video_stream(s, &ea->alpha))
544 return AVERROR(ENOMEM);
545
546
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
18 if (ea->audio_codec) {
547
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (ea->num_channels <= 0 || ea->num_channels > 2) {
548 av_log(s, AV_LOG_WARNING,
549 "Unsupported number of channels: %d\n", ea->num_channels);
550 goto no_audio;
551 }
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ea->sample_rate <= 0) {
553 av_log(s, AV_LOG_ERROR,
554 "Unsupported sample rate: %d\n", ea->sample_rate);
555 goto no_audio;
556 }
557
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if (ea->bytes <= 0 || ea->bytes > 2) {
558 av_log(s, AV_LOG_ERROR,
559 "Invalid number of bytes per sample: %d\n", ea->bytes);
560 goto no_audio;
561 }
562
563 /* initialize the audio decoder stream */
564 14 st = avformat_new_stream(s, NULL);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (!st)
566 return AVERROR(ENOMEM);
567 14 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
568 14 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
569 14 st->codecpar->codec_id = ea->audio_codec;
570 14 st->codecpar->codec_tag = 0; /* no tag */
571 14 st->codecpar->ch_layout.nb_channels = ea->num_channels;
572 14 st->codecpar->sample_rate = ea->sample_rate;
573 14 st->codecpar->bits_per_coded_sample = ea->bytes * 8;
574 14 st->codecpar->bit_rate = (int64_t)ea->num_channels *
575 14 st->codecpar->sample_rate *
576 14 st->codecpar->bits_per_coded_sample / 4;
577 14 st->codecpar->block_align = ea->num_channels *
578 14 st->codecpar->bits_per_coded_sample;
579 14 ea->audio_stream_index = st->index;
580 14 st->start_time = 0;
581 14 return 0;
582 }
583 4 no_audio:
584 4 ea->audio_codec = AV_CODEC_ID_NONE;
585
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!ea->video.codec)
587 return AVERROR_INVALIDDATA;
588 4 return 0;
589 }
590
591 3390 static int ea_read_packet(AVFormatContext *s, AVPacket *pkt)
592 {
593 3390 EaDemuxContext *ea = s->priv_data;
594 3390 AVIOContext *pb = s->pb;
595 3390 int partial_packet = 0;
596 3390 int hit_end = 0;
597 unsigned int chunk_type, chunk_size;
598 3390 int ret = 0, packet_read = 0, key = 0, vp6a;
599 3390 int av_uninit(num_samples);
600
601
6/6
✓ Branch 0 taken 3427 times.
✓ Branch 1 taken 3368 times.
✓ Branch 2 taken 3419 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3374 times.
6795 while ((!packet_read && !hit_end) || partial_packet) {
602 3421 chunk_type = avio_rl32(pb);
603
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 3405 times.
3421 if (avio_feof(pb))
604 16 return AVERROR_EOF;
605
2/2
✓ Branch 0 taken 558 times.
✓ Branch 1 taken 2847 times.
3405 chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3405 times.
3405 if (chunk_size < 8)
607 return AVERROR_INVALIDDATA;
608 3405 chunk_size -= 8;
609
610
9/9
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1421 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 329 times.
✓ Branch 4 taken 654 times.
✓ Branch 5 taken 270 times.
✓ Branch 6 taken 220 times.
✓ Branch 7 taken 472 times.
✓ Branch 8 taken 28 times.
3405 switch (chunk_type) {
611 /* audio data */
612 3 case ISNh_TAG:
613 /* header chunk also contains data; skip over the header portion */
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (chunk_size < 32)
615 return AVERROR_INVALIDDATA;
616 3 avio_skip(pb, 32);
617 3 chunk_size -= 32;
618 av_fallthrough;
619 1424 case ISNd_TAG:
620 case SCDl_TAG:
621 case SNDC_TAG:
622 case SDEN_TAG:
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1424 times.
1424 if (!ea->audio_codec) {
624 avio_skip(pb, chunk_size);
625 break;
626
2/2
✓ Branch 0 taken 1282 times.
✓ Branch 1 taken 142 times.
1424 } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1282 times.
1282 ea->audio_codec == AV_CODEC_ID_MP3) {
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 142 times.
142 if (chunk_size < 12)
629 return AVERROR_INVALIDDATA;
630 142 num_samples = avio_rl32(pb);
631 142 avio_skip(pb, 8);
632 142 chunk_size -= 12;
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1282 times.
1282 } else if (ea->audio_codec == AV_CODEC_ID_ADPCM_PSX) {
634 if (chunk_size < 8)
635 return AVERROR_INVALIDDATA;
636 avio_skip(pb, 8);
637 chunk_size -= 8;
638 }
639
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1424 times.
1424 if (partial_packet) {
641 avpriv_request_sample(s, "video header followed by audio packet");
642 av_packet_unref(pkt);
643 partial_packet = 0;
644 }
645
646
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1423 times.
1424 if (!chunk_size)
647 1 continue;
648
649 1423 ret = av_get_packet(pb, pkt, chunk_size);
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1423 times.
1423 if (ret < 0)
651 return ret;
652 1423 pkt->stream_index = ea->audio_stream_index;
653
654
4/5
✓ Branch 0 taken 905 times.
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 278 times.
1423 switch (ea->audio_codec) {
655 905 case AV_CODEC_ID_ADPCM_EA:
656 case AV_CODEC_ID_ADPCM_EA_R1:
657 case AV_CODEC_ID_ADPCM_EA_R2:
658 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
659 case AV_CODEC_ID_ADPCM_EA_R3:
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 905 times.
905 if (pkt->size < 4) {
661 av_log(s, AV_LOG_ERROR, "Packet is too short\n");
662 return AVERROR_INVALIDDATA;
663 }
664
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 780 times.
905 if (ea->audio_codec == AV_CODEC_ID_ADPCM_EA_R3)
665 125 pkt->duration = AV_RB32(pkt->data);
666 else
667 780 pkt->duration = AV_RL32(pkt->data);
668 905 break;
669 98 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
670 98 pkt->duration = ret * 2 / ea->num_channels;
671 98 break;
672 142 case AV_CODEC_ID_PCM_S16LE_PLANAR:
673 case AV_CODEC_ID_MP3:
674 142 pkt->duration = num_samples;
675 142 break;
676 case AV_CODEC_ID_ADPCM_PSX:
677 pkt->duration = chunk_size / (16 * ea->num_channels) * 28;
678 break;
679 278 default:
680 278 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
681 }
682
683 1423 packet_read = 1;
684 1423 break;
685
686 /* ending tag */
687 8 case 0:
688 case ISNe_TAG:
689 case SCEl_TAG:
690 case SEND_TAG:
691 case SEEN_TAG:
692
2/2
✓ Branch 1 taken 3848 times.
✓ Branch 2 taken 8 times.
3856 while (!avio_feof(pb)) {
693 3848 int tag = avio_rl32(pb);
694
695
2/4
✓ Branch 0 taken 3848 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3848 times.
✗ Branch 3 not taken.
3848 if (tag == ISNh_TAG ||
696
1/2
✓ Branch 0 taken 3848 times.
✗ Branch 1 not taken.
3848 tag == SCHl_TAG ||
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3848 times.
3848 tag == SEAD_TAG ||
698 tag == SHEN_TAG) {
699 avio_skip(pb, -4);
700 break;
701 }
702 }
703
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 if (avio_feof(pb))
704 8 ret = AVERROR_EOF;
705 8 hit_end = 1;
706 8 break;
707
708 329 case MVIh_TAG:
709 case kVGT_TAG:
710 case pQGT_TAG:
711 case TGQs_TAG:
712 case MADk_TAG:
713 329 key = AV_PKT_FLAG_KEY;
714 av_fallthrough;
715 983 case MVIf_TAG:
716 case fVGT_TAG:
717 case MADm_TAG:
718 case MADe_TAG:
719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 983 times.
983 if (chunk_size > INT_MAX - 8)
720 return AVERROR_INVALIDDATA;
721 983 avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
722 983 chunk_size += 8;
723 983 goto get_video_packet;
724
725 270 case mTCD_TAG:
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 if (chunk_size < 8)
727 return AVERROR_INVALIDDATA;
728
729 270 avio_skip(pb, 8); // skip ea DCT header
730 270 chunk_size -= 8;
731 270 goto get_video_packet;
732
733 220 case MV0K_TAG:
734 case AV0K_TAG:
735 case MPCh_TAG:
736 case pIQT_TAG:
737 220 key = AV_PKT_FLAG_KEY;
738 av_fallthrough;
739 case MV0F_TAG:
740 case AV0F_TAG:
741 1945 get_video_packet:
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1945 times.
1945 if (!chunk_size)
743 continue;
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1945 times.
1945 if (chunk_size > INT_MAX - 3)
745 return AVERROR_INVALIDDATA;
746
747
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1945 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1945 vp6a = (ea->video.codec == AV_CODEC_ID_VP6A && (chunk_type == MV0F_TAG || chunk_type == MV0K_TAG));
748
749
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1943 times.
1945 if (partial_packet) {
750 2 ret = av_append_packet(pb, pkt, chunk_size);
751 } else {
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1943 times.
1943 if (vp6a)
753 avio_seek(pb, -3, SEEK_CUR);
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1943 times.
1943 ret = av_get_packet(pb, pkt, chunk_size + (vp6a ? 3 : 0));
755
2/4
✓ Branch 0 taken 1943 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1943 times.
1943 if (ret >= 0 && vp6a)
756 AV_WB24(pkt->data, chunk_size);
757 }
758 1945 packet_read = 1;
759
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1945 times.
1945 if (ret < 0) {
761 partial_packet = 0;
762 break;
763 }
764
3/4
✓ Branch 0 taken 1945 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1943 times.
1945 partial_packet = vp6a || chunk_type == MVIh_TAG;
765
6/6
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 1849 times.
✓ Branch 2 taken 94 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 46 times.
✓ Branch 5 taken 48 times.
1945 if (ea->alpha.codec && (chunk_type == AV0K_TAG || chunk_type == AV0F_TAG))
766 48 pkt->stream_index = ea->alpha.stream_index;
767 else
768 1897 pkt->stream_index = ea->video.stream_index;
769 1945 pkt->flags |= key;
770 1945 break;
771
772 28 default:
773 28 avio_skip(pb, chunk_size);
774 28 break;
775 }
776 }
777
778
3/6
✓ Branch 0 taken 3366 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3366 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3374 if (ret >= 0 && hit_end && !packet_read)
779 return AVERROR(EAGAIN);
780
781 3374 return ret;
782 }
783
784 #define OFFSET(x) offsetof(EaDemuxContext, x)
785 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
786 static const AVOption options[] = {
787 {"merge_alpha", "return VP6 alpha in the main video stream", OFFSET(merge_alpha), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
788 {NULL}
789 };
790
791 static const AVClass ea_class = {
792 .class_name = "ea demuxer",
793 .item_name = av_default_item_name,
794 .option = options,
795 .version = LIBAVUTIL_VERSION_INT,
796 };
797
798 const FFInputFormat ff_ea_demuxer = {
799 .p.name = "ea",
800 .p.long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
801 .p.priv_class = &ea_class,
802 .priv_data_size = sizeof(EaDemuxContext),
803 .read_probe = ea_probe,
804 .read_header = ea_read_header,
805 .read_packet = ea_read_packet,
806 };
807