-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathpublic.rs
More file actions
1429 lines (1316 loc) · 60 KB
/
public.rs
File metadata and controls
1429 lines (1316 loc) · 60 KB
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// 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/.
use mp4parse as mp4;
use crate::mp4::{ParseStrictness, Status};
use std::convert::TryInto;
use std::fs::File;
use std::io::{Cursor, Read, Seek, SeekFrom};
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";
// This file contains invalid userdata in its copyright userdata. See
// https://bugzilla.mozilla.org/show_bug.cgi?id=1687357 for more information.
static VIDEO_INVALID_USERDATA: &str = "tests/invalid_userdata.mp4";
static IMAGE_AVIF: &str = "tests/valid.avif";
static IMAGE_AVIF_EXTENTS: &str = "tests/multiple-extents.avif";
static IMAGE_AVIF_ALPHA: &str = "tests/valid-alpha.avif";
static IMAGE_AVIF_ALPHA_PREMULTIPLIED: &str = "tests/1x1-black-alpha-50pct-premultiplied.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_IPMA_BAD_VERSION: &str = "tests/bad-ipma-version.avif";
static IMAGE_AVIF_IPMA_BAD_FLAGS: &str = "tests/bad-ipma-flags.avif";
static IMAGE_AVIF_IPMA_DUPLICATE_VERSION_AND_FLAGS: &str =
"tests/corrupt/ipma-duplicate-version-and-flags.avif";
static IMAGE_AVIF_IPMA_DUPLICATE_ITEM_ID: &str = "tests/corrupt/ipma-duplicate-item_id.avif";
static IMAGE_AVIF_IPMA_INVALID_PROPERTY_INDEX: &str =
"tests/corrupt/ipma-invalid-property-index.avif";
static IMAGE_AVIF_NO_HDLR: &str = "tests/corrupt/hdlr-not-first.avif";
static IMAGE_AVIF_HDLR_NOT_FIRST: &str = "tests/corrupt/no-hdlr.avif";
static IMAGE_AVIF_HDLR_NOT_PICT: &str = "tests/corrupt/hdlr-not-pict.avif";
static IMAGE_AVIF_HDLR_NONZERO_RESERVED: &str = "tests/hdlr-nonzero-reserved.avif";
static IMAGE_AVIF_HDLR_MULTIPLE_NUL: &str = "tests/invalid-avif-hdlr-name-multiple-nul.avif";
static IMAGE_AVIF_NO_MIF1: &str = "tests/no-mif1.avif";
static IMAGE_AVIF_NO_PITM: &str = "tests/corrupt/no-pitm.avif";
static IMAGE_AVIF_NO_PIXI: &str = "tests/corrupt/no-pixi.avif";
static IMAGE_AVIF_NO_AV1C: &str = "tests/corrupt/no-av1C.avif";
static IMAGE_AVIF_NO_ISPE: &str = "tests/corrupt/no-ispe.avif";
static IMAGE_AVIF_NO_ALPHA_ISPE: &str = "tests/corrupt/no-alpha-ispe.avif";
static IMAGE_AVIF_TRANSFORM_ORDER: &str = "tests/corrupt/invalid-transformation-order.avif";
static IMAGE_AVIF_TRANSFORM_BEFORE_ISPE: &str = "tests/corrupt/transformation-before-ispe.avif";
static IMAGE_AVIF_NO_ALPHA_AV1C: &str = "tests/corrupt/no-alpha-av1C.avif";
static IMAGE_AVIF_NO_ALPHA_PIXI: &str = "tests/corrupt/no-pixi-for-alpha.avif";
static IMAGE_AVIF_AV1C_MISSING_ESSENTIAL: &str = "tests/av1C-missing-essential.avif";
static IMAGE_AVIF_A1LX_MARKED_ESSENTIAL: &str = "tests/corrupt/a1lx-marked-essential.avif";
static IMAGE_AVIF_A1OP_MISSING_ESSENTIAL: &str = "tests/corrupt/a1op-missing-essential.avif";
static IMAGE_AVIF_IMIR_MISSING_ESSENTIAL: &str = "tests/imir-missing-essential.avif";
static IMAGE_AVIF_IROT_MISSING_ESSENTIAL: &str = "tests/irot-missing-essential.avif";
static IMAGE_AVIF_LSEL_MISSING_ESSENTIAL: &str = "tests/corrupt/lsel-missing-essential.avif";
static IMAGE_AVIF_CLAP_MISSING_ESSENTIAL: &str = "tests/clap-missing-essential.avif";
static AVIF_TEST_DIRS: &[&str] = &["tests", "av1-avif/testFiles", "link-u-avif-sample-images"];
// These files are
// av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1op.avif
// av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1lx.avif
// av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_lsel.avif
// respectively, before https://github.com/AOMediaCodec/av1-avif/pull/191
// and with https://github.com/AOMediaCodec/av1-avif/issues/174 fixed
static AVIF_A1OP: &str = "tests/a1op.avif";
static AVIF_A1LX: &str = "tests/a1lx.avif";
static AVIF_LSEL: &str = "tests/lsel.avif";
static AVIF_A1LX_NEW_LSEL: &str = "tests/a1lx-new-lsel.avif";
static AVIF_LSEL_BAD_LAYER_ID: &str = "tests/corrupt/lsel-bad-layer-id.avif";
static AVIF_CLAP: &str = "tests/clap-basic-1_3x3-to-1x1.avif";
static AVIF_GRID: &str = "av1-avif/testFiles/Microsoft/Summer_in_Tomsk_720p_5x4_grid.avif";
static AVIF_GRID_A1LX: &str =
"av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_grid_a1lx.avif";
static AVIF_AVIS_MAJOR_NO_PITM: &str =
"av1-avif/testFiles/Netflix/avis/Chimera-AV1-10bit-480x270.avif";
/// This is av1-avif/testFiles/Netflix/avis/alpha_video.avif
/// but with https://github.com/AOMediaCodec/av1-avif/issues/177 fixed
static AVIF_AVIS_MAJOR_WITH_PITM_AND_ALPHA: &str = "tests/alpha_video_fixed.avif";
static AVIF_AVIS_MAJOR_NO_MOOV: &str = "tests/corrupt/alpha_video_moov_is_moop.avif";
static AVIF_NO_PIXI_IMAGES: &[&str] = &[IMAGE_AVIF_NO_PIXI, IMAGE_AVIF_NO_ALPHA_PIXI];
static AVIF_UNSUPPORTED_IMAGES: &[&str] = &[
AVIF_A1LX,
AVIF_A1LX_NEW_LSEL,
AVIF_A1OP,
AVIF_CLAP,
IMAGE_AVIF_CLAP_MISSING_ESSENTIAL,
AVIF_GRID,
AVIF_GRID_A1LX,
AVIF_AVIS_MAJOR_NO_PITM,
AVIF_AVIS_MAJOR_WITH_PITM_AND_ALPHA,
"av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1lx.avif",
"av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1op.avif",
"av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_a1op_lsel.avif",
"av1-avif/testFiles/Apple/multilayer_examples/animals_00_multilayer_grid_lsel.avif",
"av1-avif/testFiles/Link-U/kimono.crop.avif",
"av1-avif/testFiles/Link-U/kimono.mirror-vertical.rotate270.crop.avif",
"av1-avif/testFiles/Microsoft/Chimera_10bit_cropped_to_1920x1008.avif",
"av1-avif/testFiles/Microsoft/Chimera_10bit_cropped_to_1920x1008_with_HDR_metadata.avif",
"av1-avif/testFiles/Microsoft/Chimera_8bit_cropped_480x256.avif",
"av1-avif/testFiles/Netflix/avis/alpha_video.avif",
"av1-avif/testFiles/Xiph/abandoned_filmgrain.avif",
"av1-avif/testFiles/Xiph/fruits_2layer_thumbsize.avif",
"av1-avif/testFiles/Xiph/quebec_3layer_op2.avif",
"av1-avif/testFiles/Xiph/tiger_3layer_1res.avif",
"av1-avif/testFiles/Xiph/tiger_3layer_3res.avif",
"link-u-avif-sample-images/kimono.crop.avif",
"link-u-avif-sample-images/kimono.mirror-vertical.rotate270.crop.avif",
"link-u-avif-sample-images/star-10bpc-with-alpha.avifs",
"link-u-avif-sample-images/star-10bpc.avifs",
"link-u-avif-sample-images/star-12bpc-with-alpha.avifs",
"link-u-avif-sample-images/star-12bpc.avifs",
"link-u-avif-sample-images/star-8bpc-with-alpha.avifs",
"link-u-avif-sample-images/star-8bpc.avifs",
];
/// See https://github.com/AOMediaCodec/av1-avif/issues/150
/// https://github.com/AOMediaCodec/av1-avif/issues/174
/// https://github.com/AOMediaCodec/av1-avif/issues/177
/// and https://github.com/AOMediaCodec/av1-avif/issues/178
// TODO: make this into a map of expected errors?
static AV1_AVIF_CORRUPT_IMAGES: &[&str] = &[
"av1-avif/testFiles/Link-U/kimono.crop.avif",
"av1-avif/testFiles/Link-U/kimono.mirror-horizontal.avif",
"av1-avif/testFiles/Link-U/kimono.mirror-vertical.avif",
"av1-avif/testFiles/Link-U/kimono.mirror-vertical.rotate270.avif",
"av1-avif/testFiles/Link-U/kimono.mirror-vertical.rotate270.crop.avif",
"av1-avif/testFiles/Link-U/kimono.rotate90.avif",
"av1-avif/testFiles/Link-U/kimono.rotate270.avif",
"link-u-avif-sample-images/kimono.crop.avif",
"link-u-avif-sample-images/kimono.mirror-horizontal.avif",
"link-u-avif-sample-images/kimono.mirror-vertical.avif",
"link-u-avif-sample-images/kimono.mirror-vertical.rotate270.avif",
"link-u-avif-sample-images/kimono.mirror-vertical.rotate270.crop.avif",
"link-u-avif-sample-images/kimono.rotate90.avif",
"link-u-avif-sample-images/kimono.rotate270.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.10bpc.yuv420.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile0.8bpc.yuv420.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile1.10bpc.yuv444.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile1.10bpc.yuv444.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile1.8bpc.yuv444.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile1.8bpc.yuv444.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.10bpc.yuv422.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv420.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv422.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.12bpc.yuv444.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-large.profile2.8bpc.yuv422.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.10bpc.yuv420.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile0.8bpc.yuv420.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile1.10bpc.yuv444.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile1.10bpc.yuv444.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile1.8bpc.yuv444.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile1.8bpc.yuv444.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.10bpc.yuv422.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv420.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv422.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.12bpc.yuv444.alpha-limited.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-full.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-full.monochrome.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-limited.avif",
"link-u-avif-sample-images/plum-blossom-small.profile2.8bpc.yuv422.alpha-limited.monochrome.avif",
];
static AVIF_CORRUPT_IMAGES_DIR: &str = "tests/corrupt";
// The 1 frame h263 3gp file can be generated by ffmpeg with command
// "ffmpeg -i [input file] -f 3gp -vcodec h263 -vf scale=176x144 -frames:v 1 -an output.3gp"
static VIDEO_H263_3GP: &str = "tests/bbb_sunflower_QCIF_30fps_h263_noaudio_1f.3gp";
// The 1 frame AMR-NB 3gp file can be generated by ffmpeg with command
// "ffmpeg -i [input file] -f 3gp -acodec amr_nb -ar 8000 -ac 1 -frames:a 1 -vn output.3gp"
#[cfg(feature = "3gpp")]
static AUDIO_AMRNB_3GP: &str = "tests/amr_nb_1f.3gp";
// The 1 frame AMR-WB 3gp file can be generated by ffmpeg with command
// "ffmpeg -i [input file] -f 3gp -acodec amr_wb -ar 16000 -ac 1 -frames:a 1 -vn output.3gp"
#[cfg(feature = "3gpp")]
static AUDIO_AMRWB_3GP: &str = "tests/amr_wb_1f.3gp";
#[cfg(feature = "mp4v")]
// The 1 frame mp4v mp4 file can be generated by ffmpeg with command
// "ffmpeg -i [input file] -f mp4 -c:v mpeg4 -vf scale=176x144 -frames:v 1 -an output.mp4"
static VIDEO_MP4V_MP4: &str = "tests/bbb_sunflower_QCIF_30fps_mp4v_noaudio_1f.mp4";
// 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!(!tkhd.disabled);
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"
}
mp4::VideoCodecSpecific::H263Config(ref _h263) => {
"H263"
}
},
"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!(!tkhd.disabled);
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"
}
#[cfg(feature = "3gpp")]
mp4::AudioCodecSpecific::AMRSpecificBox(_) => {
"AMR"
}
},
"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!(meta.compilation.unwrap());
assert!(!meta.gapless_playback.unwrap());
assert!(!meta.podcast.unwrap());
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!(meta.hd_video.unwrap());
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!(meta.compilation.unwrap());
assert!(!meta.gapless_playback.unwrap());
assert!(!meta.podcast.unwrap());
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!(meta.hd_video.unwrap());
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_invalid_metadata() {
// Test that reading userdata containing invalid metadata is not fatal to parsing and that
// expected values are still found.
let mut fd = File::open(VIDEO_INVALID_USERDATA).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");
// Should have userdata.
assert!(context.userdata.is_some());
// But it should contain an error.
assert!(context.userdata.unwrap().is_err());
// Smoke test that other data has been parsed. Don't check everything, just make sure some
// values are as expected.
assert_eq!(context.tracks.len(), 2);
for track in context.tracks {
match track.track_type {
mp4::TrackType::Video => {
// Check some of the values in the video tkhd.
let tkhd = track.tkhd.unwrap();
assert!(!tkhd.disabled);
assert_eq!(tkhd.duration, 231232);
assert_eq!(tkhd.width, 83_886_080);
assert_eq!(tkhd.height, 47_185_920);
}
mp4::TrackType::Audio => {
// Check some of the values in the audio tkhd.
let tkhd = track.tkhd.unwrap();
assert!(!tkhd.disabled);
assert_eq!(tkhd.duration, 231338);
assert_eq!(tkhd.width, 0);
assert_eq!(tkhd.height, 0);
}
_ => panic!("File should not contain other tracks."),
}
}
}
#[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(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(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!(!tkhd.disabled);
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!(!av1c.monochrome);
assert_eq!(av1c.chroma_subsampling_x, 1);
assert_eq!(av1c.chroma_subsampling_y, 1);
assert_eq!(av1c.chroma_sample_position, 0);
assert!(!av1c.initial_presentation_delay_present);
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_mp4_ctts_overflow() {
let input = &mut File::open("tests/clusterfuzz-testcase-minimized-mp4-6093954524250112")
.expect("Unknown file");
assert_eq!(Status::from(mp4::read_mp4(input)), Status::CttsBadSize);
}
#[test]
fn public_avif_primary_item() {
let input = &mut File::open(IMAGE_AVIF).expect("Unknown file");
let context = mp4::read_avif(input, ParseStrictness::Normal).expect("read_avif failed");
assert_eq!(
context.primary_item_coded_data().unwrap(),
[
0x12, 0x00, 0x0A, 0x07, 0x38, 0x00, 0x06, 0x90, 0x20, 0x20, 0x69, 0x32, 0x0C, 0x16,
0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x79, 0x4C, 0xD2, 0x02
]
);
}
#[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, ParseStrictness::Normal).expect("read_avif failed");
assert_eq!(context.primary_item_coded_data().unwrap().len(), 52);
}
#[test]
fn public_avif_alpha_item() {
for_strictness_result(IMAGE_AVIF_ALPHA, |_strictness, result| {
assert!(result.is_ok());
});
}
#[test]
fn public_avif_alpha_non_premultiplied() {
for_strictness_result(IMAGE_AVIF_ALPHA, |_strictness, result| {
let context = result.expect("read_avif failed");
assert!(context.primary_item_coded_data().is_some());
assert!(context.alpha_item_coded_data().is_some());
assert!(!context.premultiplied_alpha);
});
}
#[test]
fn public_avif_alpha_premultiplied() {
for_strictness_result(IMAGE_AVIF_ALPHA_PREMULTIPLIED, |_strictness, result| {
let context = result.expect("read_avif failed");
assert!(context.primary_item_coded_data().is_some());
assert!(context.alpha_item_coded_data().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, ParseStrictness::Normal).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, ParseStrictness::Normal).is_err());
}
fn for_strictness_result(
path: &str,
check: impl Fn(ParseStrictness, mp4::Result<mp4::AvifContext>),
) {
let input = &mut File::open(path).expect("Unknown file");
for strictness in [
ParseStrictness::Permissive,
ParseStrictness::Normal,
ParseStrictness::Strict,
] {
input.seek(SeekFrom::Start(0)).expect("rewind failed");
check(strictness, mp4::read_avif(input, strictness));
}
}
/// Check that input generates the expected error only in strict parsing mode
fn assert_avif_should(path: &str, expected: Status) {
for_strictness_result(path, |strictness, result| {
if strictness == ParseStrictness::Strict {
assert_eq!(expected, Status::from(result));
} else {
assert!(result.is_ok());
}
})
}
/// Check that input generates the expected error unless in permissive parsing mode
fn assert_avif_shall(path: &str, expected: Status) {
for_strictness_result(path, |strictness, result| {
if strictness == ParseStrictness::Permissive {
assert!(result.is_ok());
} else {
assert_eq!(expected, Status::from(result));
}
})
}
// Technically all transforms shall be essential, but this appears likely to change
// so we only enforce it in strict parsing
// See https://github.com/mozilla/mp4parse-rust/issues/284
#[test]
fn public_avif_av1c_missing_essential() {
assert_avif_should(IMAGE_AVIF_AV1C_MISSING_ESSENTIAL, Status::TxformNoEssential);
}
#[test]
fn public_avif_clap_missing_essential() {
for_strictness_result(IMAGE_AVIF_CLAP_MISSING_ESSENTIAL, |strictness, result| {
if strictness == ParseStrictness::Strict {
assert_eq!(Status::TxformNoEssential, Status::from(result));
} else {
assert_unsupported_nonfatal(&result, mp4::Feature::Clap);
}
})
}
#[test]
fn public_avif_imir_missing_essential() {
assert_avif_should(IMAGE_AVIF_IMIR_MISSING_ESSENTIAL, Status::TxformNoEssential);
}
#[test]
fn public_avif_irot_missing_essential() {
assert_avif_should(IMAGE_AVIF_IROT_MISSING_ESSENTIAL, Status::TxformNoEssential);