FFmpeg coverage


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