forked from mozilla/mp4parse-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.rs
724 lines (670 loc) · 29.7 KB
/
public.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/// Check if needed fields are still public.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
extern crate mp4parse as mp4;
use mp4::Error;
use std::convert::TryInto;
use std::fs::File;
use std::io::{Cursor, Read};
use std::path::Path;
static MINI_MP4: &str = "tests/minimal.mp4";
static MINI_MP4_WITH_METADATA: &str = "tests/metadata.mp4";
static MINI_MP4_WITH_METADATA_STD_GENRE: &str = "tests/metadata_gnre.mp4";
static AUDIO_EME_CENC_MP4: &str = "tests/bipbop-cenc-audioinit.mp4";
static VIDEO_EME_CENC_MP4: &str = "tests/bipbop_480wp_1001kbps-cenc-video-key1-init.mp4";
// The cbcs files were created via shaka-packager from Firefox's test suite's bipbop.mp4 using:
// packager-win.exe
// in=bipbop.mp4,stream=audio,init_segment=bipbop_cbcs_audio_init.mp4,segment_template=bipbop_cbcs_audio_$Number$.m4s
// in=bipbop.mp4,stream=video,init_segment=bipbop_cbcs_video_init.mp4,segment_template=bipbop_cbcs_video_$Number$.m4s
// --protection_scheme cbcs --enable_raw_key_encryption
// --keys label=:key_id=7e571d047e571d047e571d047e571d21:key=7e5744447e5744447e5744447e574421
// --iv 11223344556677889900112233445566
// --generate_static_mpd --mpd_output bipbop_cbcs.mpd
// note: only the init files are needed for these tests
static AUDIO_EME_CBCS_MP4: &str = "tests/bipbop_cbcs_audio_init.mp4";
static VIDEO_EME_CBCS_MP4: &str = "tests/bipbop_cbcs_video_init.mp4";
static VIDEO_AV1_MP4: &str = "tests/tiny_av1.mp4";
static IMAGE_AVIF: &str = "av1-avif/testFiles/Microsoft/Monochrome.avif";
static IMAGE_AVIF_EXTENTS: &str = "tests/kodim-extents.avif";
static IMAGE_AVIF_ALPHA: &str = "tests/bug-1661347.avif";
static IMAGE_AVIF_CORRUPT: &str = "tests/corrupt/bug-1655846.avif";
static IMAGE_AVIF_CORRUPT_2: &str = "tests/corrupt/bug-1661347.avif";
static IMAGE_AVIF_CORRUPT_3: &str = "tests/corrupt/bad-ipma-version.avif";
static IMAGE_AVIF_CORRUPT_4: &str = "tests/corrupt/bad-ipma-flags.avif";
static IMAGE_AVIF_GRID: &str = "av1-avif/testFiles/Microsoft/Summer_in_Tomsk_720p_5x4_grid.avif";
static AVIF_TEST_DIRS: &[&str] = &["tests", "av1-avif/testFiles"];
static AVIF_CORRUPT_IMAGES: &str = "tests/corrupt";
// Adapted from https://github.com/GuillaumeGomez/audio-video-metadata/blob/9dff40f565af71d5502e03a2e78ae63df95cfd40/src/metadata.rs#L53
#[test]
fn public_api() {
let mut fd = File::open(MINI_MP4).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
assert_eq!(context.timescale, Some(mp4::MediaTimeScale(1000)));
for track in context.tracks {
match track.track_type {
mp4::TrackType::Video => {
// track part
assert_eq!(track.duration, Some(mp4::TrackScaledTime(512, 0)));
assert_eq!(track.empty_duration, Some(mp4::MediaScaledTime(0)));
assert_eq!(track.media_time, Some(mp4::TrackScaledTime(0, 0)));
assert_eq!(track.timescale, Some(mp4::TrackTimeScale(12800, 0)));
// track.tkhd part
let tkhd = track.tkhd.unwrap();
assert_eq!(tkhd.disabled, false);
assert_eq!(tkhd.duration, 40);
assert_eq!(tkhd.width, 20_971_520);
assert_eq!(tkhd.height, 15_728_640);
// track.stsd part
let stsd = track.stsd.expect("expected an stsd");
let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
mp4::SampleEntry::Video(v) => v,
_ => panic!("expected a VideoSampleEntry"),
};
assert_eq!(v.width, 320);
assert_eq!(v.height, 240);
assert_eq!(
match v.codec_specific {
mp4::VideoCodecSpecific::AVCConfig(ref avc) => {
assert!(!avc.is_empty());
"AVC"
}
mp4::VideoCodecSpecific::VPxConfig(ref vpx) => {
// We don't enter in here, we just check if fields are public.
assert!(vpx.bit_depth > 0);
assert!(vpx.colour_primaries > 0);
assert!(vpx.chroma_subsampling > 0);
assert!(!vpx.codec_init.is_empty());
"VPx"
}
mp4::VideoCodecSpecific::ESDSConfig(ref mp4v) => {
assert!(!mp4v.is_empty());
"MP4V"
}
mp4::VideoCodecSpecific::AV1Config(ref _av1c) => {
"AV1"
}
},
"AVC"
);
}
mp4::TrackType::Audio => {
// track part
assert_eq!(track.duration, Some(mp4::TrackScaledTime(2944, 1)));
assert_eq!(track.empty_duration, Some(mp4::MediaScaledTime(0)));
assert_eq!(track.media_time, Some(mp4::TrackScaledTime(1024, 1)));
assert_eq!(track.timescale, Some(mp4::TrackTimeScale(48000, 1)));
// track.tkhd part
let tkhd = track.tkhd.unwrap();
assert_eq!(tkhd.disabled, false);
assert_eq!(tkhd.duration, 62);
assert_eq!(tkhd.width, 0);
assert_eq!(tkhd.height, 0);
// track.stsd part
let stsd = track.stsd.expect("expected an stsd");
let a = match stsd.descriptions.first().expect("expected a SampleEntry") {
mp4::SampleEntry::Audio(a) => a,
_ => panic!("expected a AudioSampleEntry"),
};
assert_eq!(
match a.codec_specific {
mp4::AudioCodecSpecific::ES_Descriptor(ref esds) => {
assert_eq!(esds.audio_codec, mp4::CodecType::AAC);
assert_eq!(esds.audio_sample_rate.unwrap(), 48000);
assert_eq!(esds.audio_object_type.unwrap(), 2);
"ES"
}
mp4::AudioCodecSpecific::FLACSpecificBox(ref flac) => {
// STREAMINFO block must be present and first.
assert!(!flac.blocks.is_empty());
assert_eq!(flac.blocks[0].block_type, 0);
assert_eq!(flac.blocks[0].data.len(), 34);
"FLAC"
}
mp4::AudioCodecSpecific::OpusSpecificBox(ref opus) => {
// We don't enter in here, we just check if fields are public.
assert!(opus.version > 0);
"Opus"
}
mp4::AudioCodecSpecific::ALACSpecificBox(ref alac) => {
assert!(alac.data.len() == 24 || alac.data.len() == 48);
"ALAC"
}
mp4::AudioCodecSpecific::MP3 => {
"MP3"
}
mp4::AudioCodecSpecific::LPCM => {
"LPCM"
}
},
"ES"
);
assert!(a.samplesize > 0);
assert!(a.samplerate > 0.0);
}
mp4::TrackType::Metadata | mp4::TrackType::Unknown => {}
}
}
}
#[test]
fn public_metadata() {
let mut fd = File::open(MINI_MP4_WITH_METADATA).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
let udta = context
.userdata
.expect("didn't find udta")
.expect("failed to parse udta");
let meta = udta.meta.expect("didn't find meta");
assert_eq!(meta.title.unwrap(), "Title");
assert_eq!(meta.artist.unwrap(), "Artist");
assert_eq!(meta.album_artist.unwrap(), "Album Artist");
assert_eq!(meta.comment.unwrap(), "Comments");
assert_eq!(meta.year.unwrap(), "2019");
assert_eq!(
meta.genre.unwrap(),
mp4::Genre::CustomGenre("Custom Genre".try_into().unwrap())
);
assert_eq!(meta.encoder.unwrap(), "Lavf56.40.101");
assert_eq!(meta.encoded_by.unwrap(), "Encoded-by");
assert_eq!(meta.copyright.unwrap(), "Copyright");
assert_eq!(meta.track_number.unwrap(), 3);
assert_eq!(meta.total_tracks.unwrap(), 6);
assert_eq!(meta.disc_number.unwrap(), 5);
assert_eq!(meta.total_discs.unwrap(), 10);
assert_eq!(meta.beats_per_minute.unwrap(), 128);
assert_eq!(meta.composer.unwrap(), "Composer");
assert_eq!(meta.compilation.unwrap(), true);
assert_eq!(meta.gapless_playback.unwrap(), false);
assert_eq!(meta.podcast.unwrap(), false);
assert_eq!(meta.advisory.unwrap(), mp4::AdvisoryRating::Clean);
assert_eq!(meta.media_type.unwrap(), mp4::MediaType::Normal);
assert_eq!(meta.rating.unwrap(), "50");
assert_eq!(meta.grouping.unwrap(), "Grouping");
assert_eq!(meta.category.unwrap(), "Category");
assert_eq!(meta.keyword.unwrap(), "Keyword");
assert_eq!(meta.description.unwrap(), "Description");
assert_eq!(meta.lyrics.unwrap(), "Lyrics");
assert_eq!(meta.long_description.unwrap(), "Long Description");
assert_eq!(meta.tv_episode_name.unwrap(), "Episode Name");
assert_eq!(meta.tv_network_name.unwrap(), "Network Name");
assert_eq!(meta.tv_episode_number.unwrap(), 15);
assert_eq!(meta.tv_season.unwrap(), 10);
assert_eq!(meta.tv_show_name.unwrap(), "Show Name");
assert_eq!(meta.hd_video.unwrap(), true);
assert_eq!(meta.owner.unwrap(), "Owner");
assert_eq!(meta.sort_name.unwrap(), "Sort Name");
assert_eq!(meta.sort_album.unwrap(), "Sort Album");
assert_eq!(meta.sort_artist.unwrap(), "Sort Artist");
assert_eq!(meta.sort_album_artist.unwrap(), "Sort Album Artist");
assert_eq!(meta.sort_composer.unwrap(), "Sort Composer");
// Check for valid JPEG header
let covers = meta.cover_art.unwrap();
let cover = &covers[0];
let mut bytes = [0u8; 4];
bytes[0] = cover[0];
bytes[1] = cover[1];
bytes[2] = cover[2];
assert_eq!(u32::from_le_bytes(bytes), 0x00ff_d8ff);
}
#[test]
fn public_metadata_gnre() {
let mut fd = File::open(MINI_MP4_WITH_METADATA_STD_GENRE).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
let udta = context
.userdata
.expect("didn't find udta")
.expect("failed to parse udta");
let meta = udta.meta.expect("didn't find meta");
assert_eq!(meta.title.unwrap(), "Title");
assert_eq!(meta.artist.unwrap(), "Artist");
assert_eq!(meta.album_artist.unwrap(), "Album Artist");
assert_eq!(meta.comment.unwrap(), "Comments");
assert_eq!(meta.year.unwrap(), "2019");
assert_eq!(meta.genre.unwrap(), mp4::Genre::StandardGenre(3));
assert_eq!(meta.encoder.unwrap(), "Lavf56.40.101");
assert_eq!(meta.encoded_by.unwrap(), "Encoded-by");
assert_eq!(meta.copyright.unwrap(), "Copyright");
assert_eq!(meta.track_number.unwrap(), 3);
assert_eq!(meta.total_tracks.unwrap(), 6);
assert_eq!(meta.disc_number.unwrap(), 5);
assert_eq!(meta.total_discs.unwrap(), 10);
assert_eq!(meta.beats_per_minute.unwrap(), 128);
assert_eq!(meta.composer.unwrap(), "Composer");
assert_eq!(meta.compilation.unwrap(), true);
assert_eq!(meta.gapless_playback.unwrap(), false);
assert_eq!(meta.podcast.unwrap(), false);
assert_eq!(meta.advisory.unwrap(), mp4::AdvisoryRating::Clean);
assert_eq!(meta.media_type.unwrap(), mp4::MediaType::Normal);
assert_eq!(meta.rating.unwrap(), "50");
assert_eq!(meta.grouping.unwrap(), "Grouping");
assert_eq!(meta.category.unwrap(), "Category");
assert_eq!(meta.keyword.unwrap(), "Keyword");
assert_eq!(meta.description.unwrap(), "Description");
assert_eq!(meta.lyrics.unwrap(), "Lyrics");
assert_eq!(meta.long_description.unwrap(), "Long Description");
assert_eq!(meta.tv_episode_name.unwrap(), "Episode Name");
assert_eq!(meta.tv_network_name.unwrap(), "Network Name");
assert_eq!(meta.tv_episode_number.unwrap(), 15);
assert_eq!(meta.tv_season.unwrap(), 10);
assert_eq!(meta.tv_show_name.unwrap(), "Show Name");
assert_eq!(meta.hd_video.unwrap(), true);
assert_eq!(meta.owner.unwrap(), "Owner");
assert_eq!(meta.sort_name.unwrap(), "Sort Name");
assert_eq!(meta.sort_album.unwrap(), "Sort Album");
assert_eq!(meta.sort_artist.unwrap(), "Sort Artist");
assert_eq!(meta.sort_album_artist.unwrap(), "Sort Album Artist");
assert_eq!(meta.sort_composer.unwrap(), "Sort Composer");
// Check for valid JPEG header
let covers = meta.cover_art.unwrap();
let cover = &covers[0];
let mut bytes = [0u8; 4];
bytes[0] = cover[0];
bytes[1] = cover[1];
bytes[2] = cover[2];
assert_eq!(u32::from_le_bytes(bytes), 0x00ff_d8ff);
}
#[test]
fn public_audio_tenc() {
let kid = vec![
0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d,
0x04,
];
let mut fd = File::open(AUDIO_EME_CENC_MP4).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
for track in context.tracks {
let stsd = track.stsd.expect("expected an stsd");
let a = match stsd.descriptions.first().expect("expected a SampleEntry") {
mp4::SampleEntry::Audio(a) => a,
_ => panic!("expected a AudioSampleEntry"),
};
assert_eq!(a.codec_type, mp4::CodecType::EncryptedAudio);
match a.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
Some(ref p) => {
assert_eq!(p.original_format, b"mp4a");
if let Some(ref schm) = p.scheme_type {
assert_eq!(schm.scheme_type, b"cenc");
} else {
panic!("Expected scheme type info");
}
if let Some(ref tenc) = p.tenc {
assert!(tenc.is_encrypted > 0);
assert_eq!(tenc.iv_size, 16);
assert_eq!(tenc.kid, kid);
assert_eq!(tenc.crypt_byte_block_count, None);
assert_eq!(tenc.skip_byte_block_count, None);
assert_eq!(tenc.constant_iv, None);
} else {
panic!("Invalid test condition");
}
}
_ => {
panic!("Invalid test condition");
}
}
}
}
#[test]
fn public_video_cenc() {
let system_id = vec![
0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb,
0x4b,
];
let kid = vec![
0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d,
0x11,
];
let pssh_box = vec![
0x00, 0x00, 0x00, 0x34, 0x70, 0x73, 0x73, 0x68, 0x01, 0x00, 0x00, 0x00, 0x10, 0x77, 0xef,
0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b, 0x00, 0x00,
0x00, 0x01, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e, 0x57, 0x1d, 0x03, 0x7e,
0x57, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00,
];
let mut fd = File::open(VIDEO_EME_CENC_MP4).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
for track in context.tracks {
let stsd = track.stsd.expect("expected an stsd");
let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
mp4::SampleEntry::Video(ref v) => v,
_ => panic!("expected a VideoSampleEntry"),
};
assert_eq!(v.codec_type, mp4::CodecType::EncryptedVideo);
match v.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
Some(ref p) => {
assert_eq!(p.original_format, b"avc1");
if let Some(ref schm) = p.scheme_type {
assert_eq!(schm.scheme_type, b"cenc");
} else {
panic!("Expected scheme type info");
}
if let Some(ref tenc) = p.tenc {
assert!(tenc.is_encrypted > 0);
assert_eq!(tenc.iv_size, 16);
assert_eq!(tenc.kid, kid);
assert_eq!(tenc.crypt_byte_block_count, None);
assert_eq!(tenc.skip_byte_block_count, None);
assert_eq!(tenc.constant_iv, None);
} else {
panic!("Invalid test condition");
}
}
_ => {
panic!("Invalid test condition");
}
}
}
for pssh in context.psshs {
assert_eq!(pssh.system_id, system_id);
for kid_id in pssh.kid {
assert_eq!(kid_id, kid);
}
assert!(pssh.data.is_empty());
assert_eq!(pssh.box_content, pssh_box);
}
}
#[test]
fn public_audio_cbcs() {
let system_id = vec![
0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb,
0x4b,
];
let kid = vec![
0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d,
0x21,
];
let default_iv = vec![
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
0x66,
];
let pssh_box = vec![
0x00, 0x00, 0x00, 0x34, 0x70, 0x73, 0x73, 0x68, 0x01, 0x00, 0x00, 0x00, 0x10, 0x77, 0xef,
0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b, 0x00, 0x00,
0x00, 0x01, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e,
0x57, 0x1d, 0x21, 0x00, 0x00, 0x00, 0x00,
];
let mut fd = File::open(AUDIO_EME_CBCS_MP4).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
for track in context.tracks {
let stsd = track.stsd.expect("expected an stsd");
assert_eq!(stsd.descriptions.len(), 2);
let mut found_encrypted_sample_description = false;
for description in stsd.descriptions {
match description {
mp4::SampleEntry::Audio(ref a) => {
if let Some(p) = a.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
found_encrypted_sample_description = true;
assert_eq!(p.original_format, b"mp4a");
if let Some(ref schm) = p.scheme_type {
assert_eq!(schm.scheme_type, b"cbcs");
} else {
panic!("Expected scheme type info");
}
if let Some(ref tenc) = p.tenc {
assert!(tenc.is_encrypted > 0);
assert_eq!(tenc.iv_size, 0);
assert_eq!(tenc.kid, kid);
// Note: 0 for both crypt and skip seems odd but
// that's what shaka-packager produced. It appears
// to indicate full encryption.
assert_eq!(tenc.crypt_byte_block_count, Some(0));
assert_eq!(tenc.skip_byte_block_count, Some(0));
assert_eq!(tenc.constant_iv, Some(default_iv.clone().into()));
} else {
panic!("Invalid test condition");
}
}
}
_ => {
panic!("expected a VideoSampleEntry");
}
}
}
assert!(
found_encrypted_sample_description,
"Should have found an encrypted sample description"
);
}
for pssh in context.psshs {
assert_eq!(pssh.system_id, system_id);
for kid_id in pssh.kid {
assert_eq!(kid_id, kid);
}
assert!(pssh.data.is_empty());
assert_eq!(pssh.box_content, pssh_box);
}
}
#[test]
fn public_video_cbcs() {
let system_id = vec![
0x10, 0x77, 0xef, 0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb,
0x4b,
];
let kid = vec![
0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d,
0x21,
];
let default_iv = vec![
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
0x66,
];
let pssh_box = vec![
0x00, 0x00, 0x00, 0x34, 0x70, 0x73, 0x73, 0x68, 0x01, 0x00, 0x00, 0x00, 0x10, 0x77, 0xef,
0xec, 0xc0, 0xb2, 0x4d, 0x02, 0xac, 0xe3, 0x3c, 0x1e, 0x52, 0xe2, 0xfb, 0x4b, 0x00, 0x00,
0x00, 0x01, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e, 0x57, 0x1d, 0x04, 0x7e,
0x57, 0x1d, 0x21, 0x00, 0x00, 0x00, 0x00,
];
let mut fd = File::open(VIDEO_EME_CBCS_MP4).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
for track in context.tracks {
let stsd = track.stsd.expect("expected an stsd");
assert_eq!(stsd.descriptions.len(), 2);
let mut found_encrypted_sample_description = false;
for description in stsd.descriptions {
match description {
mp4::SampleEntry::Video(ref v) => {
assert_eq!(v.width, 400);
assert_eq!(v.height, 300);
if let Some(p) = v.protection_info.iter().find(|sinf| sinf.tenc.is_some()) {
found_encrypted_sample_description = true;
assert_eq!(p.original_format, b"avc1");
if let Some(ref schm) = p.scheme_type {
assert_eq!(schm.scheme_type, b"cbcs");
} else {
panic!("Expected scheme type info");
}
if let Some(ref tenc) = p.tenc {
assert!(tenc.is_encrypted > 0);
assert_eq!(tenc.iv_size, 0);
assert_eq!(tenc.kid, kid);
assert_eq!(tenc.crypt_byte_block_count, Some(1));
assert_eq!(tenc.skip_byte_block_count, Some(9));
assert_eq!(tenc.constant_iv, Some(default_iv.clone().into()));
} else {
panic!("Invalid test condition");
}
}
}
_ => {
panic!("expected a VideoSampleEntry");
}
}
}
assert!(
found_encrypted_sample_description,
"Should have found an encrypted sample description"
);
}
for pssh in context.psshs {
assert_eq!(pssh.system_id, system_id);
for kid_id in pssh.kid {
assert_eq!(kid_id, kid);
}
assert!(pssh.data.is_empty());
assert_eq!(pssh.box_content, pssh_box);
}
}
#[test]
fn public_video_av1() {
let mut fd = File::open(VIDEO_AV1_MP4).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");
let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
for track in context.tracks {
// track part
assert_eq!(track.duration, Some(mp4::TrackScaledTime(512, 0)));
assert_eq!(track.empty_duration, Some(mp4::MediaScaledTime(0)));
assert_eq!(track.media_time, Some(mp4::TrackScaledTime(0, 0)));
assert_eq!(track.timescale, Some(mp4::TrackTimeScale(12288, 0)));
// track.tkhd part
let tkhd = track.tkhd.unwrap();
assert_eq!(tkhd.disabled, false);
assert_eq!(tkhd.duration, 42);
assert_eq!(tkhd.width, 4_194_304);
assert_eq!(tkhd.height, 4_194_304);
// track.stsd part
let stsd = track.stsd.expect("expected an stsd");
let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
mp4::SampleEntry::Video(ref v) => v,
_ => panic!("expected a VideoSampleEntry"),
};
assert_eq!(v.codec_type, mp4::CodecType::AV1);
assert_eq!(v.width, 64);
assert_eq!(v.height, 64);
match v.codec_specific {
mp4::VideoCodecSpecific::AV1Config(ref av1c) => {
// TODO: test av1c fields once ffmpeg is updated
assert_eq!(av1c.profile, 0);
assert_eq!(av1c.level, 0);
assert_eq!(av1c.tier, 0);
assert_eq!(av1c.bit_depth, 8);
assert_eq!(av1c.monochrome, false);
assert_eq!(av1c.chroma_subsampling_x, 1);
assert_eq!(av1c.chroma_subsampling_y, 1);
assert_eq!(av1c.chroma_sample_position, 0);
assert_eq!(av1c.initial_presentation_delay_present, false);
assert_eq!(av1c.initial_presentation_delay_minus_one, 0);
}
_ => panic!("Invalid test condition"),
}
}
}
#[test]
fn public_mp4_bug_1185230() {
let input = &mut File::open("tests/test_case_1185230.mp4").expect("Unknown file");
let context = mp4::read_mp4(input).expect("read_mp4 failed");
let number_video_tracks = context
.tracks
.iter()
.filter(|t| t.track_type == mp4::TrackType::Video)
.count();
let number_audio_tracks = context
.tracks
.iter()
.filter(|t| t.track_type == mp4::TrackType::Audio)
.count();
assert_eq!(number_video_tracks, 2);
assert_eq!(number_audio_tracks, 2);
}
#[test]
fn public_avif_primary_item() {
let input = &mut File::open(IMAGE_AVIF).expect("Unknown file");
let context = mp4::read_avif(input).expect("read_avif failed");
assert_eq!(context.primary_item().len(), 6979);
assert_eq!(context.primary_item()[0..4], [0x12, 0x00, 0x0a, 0x0a]);
}
#[test]
fn public_avif_primary_item_split_extents() {
let input = &mut File::open(IMAGE_AVIF_EXTENTS).expect("Unknown file");
let context = mp4::read_avif(input).expect("read_avif failed");
assert_eq!(context.primary_item().len(), 4387);
}
#[test]
fn public_avif_alpha_item() {
let input = &mut File::open(IMAGE_AVIF_ALPHA).expect("Unknown file");
let context = mp4::read_avif(input).expect("read_avif failed");
assert!(context.alpha_item().is_some());
assert!(!context.premultiplied_alpha);
}
#[test]
fn public_avif_bug_1655846() {
let input = &mut File::open(IMAGE_AVIF_CORRUPT).expect("Unknown file");
assert!(mp4::read_avif(input).is_err());
}
#[test]
fn public_avif_bug_1661347() {
let input = &mut File::open(IMAGE_AVIF_CORRUPT_2).expect("Unknown file");
assert!(mp4::read_avif(input).is_err());
}
fn assert_invalid_data<T: std::fmt::Debug>(result: mp4::Result<T>, expected_msg: &str) {
match result {
Err(Error::InvalidData(msg)) if msg == expected_msg => {}
r => panic!(
"Expected Err(Error::InvalidData({:?})), found {:?}",
expected_msg, r
),
}
}
#[test]
fn public_avif_bad_ipma_version() {
let input = &mut File::open(IMAGE_AVIF_CORRUPT_3).expect("Unknown file");
let expected_msg = "The version 0 should be used unless 32-bit item_ID values are needed";
assert_invalid_data(mp4::read_avif(input), expected_msg);
}
#[test]
fn public_avif_bad_ipma_flags() {
let input = &mut File::open(IMAGE_AVIF_CORRUPT_4).expect("Unknown file");
let expected_msg = "flags should be equal to 0 unless there are more than 127 properties in the ItemPropertyContainerBox";
assert_invalid_data(mp4::read_avif(input), expected_msg);
}
#[test]
#[ignore] // Remove when we add support; see https://github.com/mozilla/mp4parse-rust/issues/198
fn public_avif_primary_item_is_grid() {
let input = &mut File::open(IMAGE_AVIF_GRID).expect("Unknown file");
mp4::read_avif(input).expect("read_avif failed");
// Add some additional checks
}
#[test]
fn public_avif_read_samples() {
for dir in AVIF_TEST_DIRS {
for entry in walkdir::WalkDir::new(dir) {
let entry = entry.expect("AVIF entry");
let path = entry.path();
if !path.is_file() || path.extension().unwrap_or_default() != "avif" {
eprintln!("Skipping {:?}", path);
continue; // Skip directories, ReadMe.txt, etc.
}
if path.parent().unwrap() == Path::new(AVIF_CORRUPT_IMAGES) {
eprintln!("Skipping {:?}", path);
continue;
}
if path == Path::new(IMAGE_AVIF_GRID) {
eprintln!("Skipping {:?}", path);
continue; // Remove when public_avif_primary_item_is_grid passes
}
println!("parsing {:?}", path);
let input = &mut File::open(path).expect("Unknow file");
mp4::read_avif(input).expect("read_avif failed");
}
}
}