Debian Codegraph @

.cargo/vendor/rav1e/src/api/test.rs

raw
1 // Copyright (c) 2018-2022, The rav1e contributors. All rights reserved
2 //
3 // This source code is subject to the terms of the BSD 2 Clause License and
4 // the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
5 // was not distributed with this source code in the LICENSE file, you can
6 // obtain it at www.aomedia.org/license/software. If the Alliance for Open
7 // Media Patent License 1.0 was not distributed with this source code in the
8 // PATENTS file, you can obtain it at www.aomedia.org/license/patent.
9
10 use crate::encoder::FrameInvariants;
11 use crate::prelude::*;
12
13 use std::sync::Arc;
14
15 use interpolate_name::interpolate_test;
16
17 fn setup_config(
18 w: usize, h: usize, speed: u8, quantizer: usize, bit_depth: usize,
19 chroma_sampling: ChromaSampling, min_keyint: u64, max_keyint: u64,
20 bitrate: i32, low_latency: bool, switch_frame_interval: u64,
21 no_scene_detection: bool, rdo_lookahead_frames: usize,
22 min_quantizer: Option<u8>,
23 ) -> Config {
24 let mut enc = EncoderConfig::with_speed_preset(speed);
25 enc.quantizer = quantizer;
26 enc.min_key_frame_interval = min_keyint;
27 enc.max_key_frame_interval = max_keyint;
28 enc.low_latency = low_latency;
29 enc.switch_frame_interval = switch_frame_interval;
30 enc.width = w;
31 enc.height = h;
32 enc.bit_depth = bit_depth;
33 enc.chroma_sampling = chroma_sampling;
34 enc.bitrate = bitrate;
35 if no_scene_detection {
36 enc.speed_settings.scene_detection_mode = SceneDetectionSpeed::None;
37 }
38 enc.speed_settings.rdo_lookahead_frames = rdo_lookahead_frames;
39 if let Some(min_quantizer) = min_quantizer {
40 enc.min_quantizer = min_quantizer;
41 }
42
43 Config::new().with_encoder_config(enc).with_threads(1)
44 }
45
46 fn setup_encoder<T: Pixel>(
47 w: usize, h: usize, speed: u8, quantizer: usize, bit_depth: usize,
48 chroma_sampling: ChromaSampling, min_keyint: u64, max_keyint: u64,
49 bitrate: i32, low_latency: bool, switch_frame_interval: u64,
50 no_scene_detection: bool, rdo_lookahead_frames: usize,
51 min_quantizer: Option<u8>,
52 ) -> Context<T> {
53 let cfg = setup_config(
54 w,
55 h,
56 speed,
57 quantizer,
58 bit_depth,
59 chroma_sampling,
60 min_keyint,
61 max_keyint,
62 bitrate,
63 low_latency,
64 switch_frame_interval,
65 no_scene_detection,
66 rdo_lookahead_frames,
67 min_quantizer,
68 );
69 cfg.new_context().unwrap()
70 }
71
72 /*
73 fn fill_frame<T: Pixel>(ra: &mut ChaChaRng, frame: &mut Frame<T>) {
74 for plane in frame.planes.iter_mut() {
75 let stride = plane.cfg.stride;
76 for row in plane.data.chunks_mut(stride) {
77 for pixel in row {
78 let v: u8 = ra.random();
79 *pixel = T::cast_from(v);
80 }
81 }
82 }
83 }
84 */
85
86 fn fill_frame_const<T: Pixel>(frame: &mut Frame<T>, value: T) {
87 for plane in frame.planes.iter_mut() {
88 let stride = plane.cfg.stride;
89 for row in plane.data.chunks_mut(stride) {
90 for pixel in row {
91 *pixel = value;
92 }
93 }
94 }
95 }
96
97 #[cfg(feature = "channel-api")]
98 mod channel {
99 use super::*;
100
101 #[interpolate_test(low_latency_no_scene_change, true, true)]
102 #[interpolate_test(reorder_no_scene_change, false, true)]
103 #[interpolate_test(low_latency_scene_change_detection, true, false)]
104 #[interpolate_test(reorder_scene_change_detection, false, false)]
105 fn flush(low_lantency: bool, no_scene_detection: bool) {
106 let cfg = setup_config(
107 64,
108 80,
109 10,
110 100,
111 8,
112 ChromaSampling::Cs420,
113 150,
114 200,
115 0,
116 low_lantency,
117 0,
118 no_scene_detection,
119 10,
120 None,
121 );
122
123 let limit = 41;
124
125 let (mut sf, rp) = cfg.new_channel::<u8>().unwrap();
126
127 for _ in 0..limit {
128 let input = sf.new_frame();
129 let _ = sf.send(input);
130 }
131
132 drop(sf);
133
134 let mut count = 0;
135
136 for _ in 0..limit {
137 let _ = rp
138 .recv()
139 .map(|_| {
140 eprintln!("Packet Received {}/{}", count, limit);
141 count += 1;
142 })
143 .unwrap();
144 }
145
146 assert_eq!(limit, count);
147 }
148 }
149
150 #[interpolate_test(low_latency_no_scene_change, true, true)]
151 #[interpolate_test(reorder_no_scene_change, false, true)]
152 #[interpolate_test(low_latency_scene_change_detection, true, false)]
153 #[interpolate_test(reorder_scene_change_detection, false, false)]
154 fn flush(low_lantency: bool, no_scene_detection: bool) {
155 let mut ctx = setup_encoder::<u8>(
156 64,
157 80,
158 10,
159 100,
160 8,
161 ChromaSampling::Cs420,
162 150,
163 200,
164 0,
165 low_lantency,
166 0,
167 no_scene_detection,
168 10,
169 None,
170 );
171 let limit = 41;
172
173 for _ in 0..limit {
174 let input = ctx.new_frame();
175 let _ = ctx.send_frame(input);
176 }
177
178 ctx.flush();
179
180 let mut count = 0;
181
182 'out: for _ in 0..limit {
183 loop {
184 match ctx.receive_packet() {
185 Ok(_) => {
186 eprintln!("Packet Received {}/{}", count, limit);
187 count += 1;
188 }
189 Err(EncoderStatus::EnoughData) => {
190 eprintln!("{:?}", EncoderStatus::EnoughData);
191
192 break 'out;
193 }
194 Err(e) => {
195 eprintln!("{:?}", e);
196 break;
197 }
198 }
199 }
200 }
201
202 assert_eq!(limit, count);
203 }
204
205 #[interpolate_test(low_latency_no_scene_change, true, true)]
206 #[interpolate_test(reorder_no_scene_change, false, true)]
207 #[interpolate_test(low_latency_scene_change_detection, true, false)]
208 #[interpolate_test(reorder_scene_change_detection, false, false)]
209 fn flush_unlimited(low_lantency: bool, no_scene_detection: bool) {
210 let mut ctx = setup_encoder::<u8>(
211 64,
212 80,
213 10,
214 100,
215 8,
216 ChromaSampling::Cs420,
217 150,
218 200,
219 0,
220 low_lantency,
221 0,
222 no_scene_detection,
223 10,
224 None,
225 );
226 let limit = 41;
227
228 for _ in 0..limit {
229 let input = ctx.new_frame();
230 let _ = ctx.send_frame(input);
231 }
232
233 ctx.flush();
234
235 let mut count = 0;
236
237 'out: for _ in 0..limit {
238 loop {
239 match ctx.receive_packet() {
240 Ok(_) => {
241 eprintln!("Packet Received {}/{}", count, limit);
242 count += 1;
243 }
244 Err(EncoderStatus::EnoughData) => {
245 eprintln!("{:?}", EncoderStatus::EnoughData);
246
247 break 'out;
248 }
249 Err(e) => {
250 eprintln!("{:?}", e);
251 break;
252 }
253 }
254 }
255 }
256
257 assert_eq!(limit, count);
258 }
259
260 fn send_frames<T: Pixel>(
261 ctx: &mut Context<T>, limit: u64, scene_change_at: u64,
262 ) {
263 for i in 0..limit {
264 if i < scene_change_at {
265 send_test_frame(ctx, T::min_value());
266 } else {
267 send_test_frame(ctx, T::max_value());
268 }
269 }
270 }
271
272 fn send_test_frame<T: Pixel>(ctx: &mut Context<T>, content_value: T) {
273 let mut input = ctx.new_frame();
274 fill_frame_const(&mut input, content_value);
275 let _ = ctx.send_frame(Arc::new(input));
276 }
277
278 fn get_frame_invariants<T: Pixel>(
279 ctx: Context<T>,
280 ) -> impl Iterator<Item = Option<FrameInvariants<T>>> {
281 ctx.inner.frame_data.into_values().map(|v| v.map(|v| v.fi))
282 }
283
284 #[interpolate_test(0, 0)]
285 #[interpolate_test(1, 1)]
286 fn output_frameno_low_latency_minus(missing: u64) {
287 // Test output_frameno configurations when there are <missing> less frames
288 // than the perfect subgop size, in no-reorder mode.
289
290 let mut ctx = setup_encoder::<u8>(
291 64,
292 80,
293 10,
294 100,
295 8,
296 ChromaSampling::Cs420,
297 5,
298 5,
299 0,
300 true,
301 0,
302 true,
303 10,
304 None,
305 );
306 let limit = 10 - missing;
307 send_frames(&mut ctx, limit, 0);
308 ctx.flush();
309
310 let data = get_frame_invariants(ctx)
311 .map(|fi| fi.map(|fi| fi.input_frameno))
312 .collect::<Vec<_>>();
313
314 assert_eq!(
315 &data[..],
316 match missing {
317 0 => {
318 &[
319 Some(0), // I-frame
320 Some(1),
321 Some(2),
322 Some(3),
323 Some(4),
324 Some(5), // I-frame
325 Some(6),
326 Some(7),
327 Some(8),
328 Some(9),
329 ][..]
330 }
331 1 => {
332 &[
333 Some(0), // I-frame
334 Some(1),
335 Some(2),
336 Some(3),
337 Some(4),
338 Some(5), // I-frame
339 Some(6),
340 Some(7),
341 Some(8),
342 ][..]
343 }
344 _ => unreachable!(),
345 }
346 );
347 }
348
349 #[test]
350 fn switch_frame_interval() {
351 // Test output_frameno configurations when there are <missing> less frames
352 // than the perfect subgop size, in no-reorder mode.
353
354 let mut ctx = setup_encoder::<u8>(
355 64,
356 80,
357 10,
358 100,
359 8,
360 ChromaSampling::Cs420,
361 5,
362 5,
363 0,
364 true,
365 2,
366 true,
367 10,
368 None,
369 );
370 let limit = 10;
371 send_frames(&mut ctx, limit, 0);
372 ctx.flush();
373
374 let data = get_frame_invariants(ctx)
375 .map(|fi| fi.map(|fi| (fi.input_frameno, fi.frame_type)))
376 .collect::<Vec<_>>();
377
378 assert_eq!(
379 &data[..],
380 &[
381 Some((0, FrameType::KEY)),
382 Some((1, FrameType::INTER)),
383 Some((2, FrameType::SWITCH)),
384 Some((3, FrameType::INTER)),
385 Some((4, FrameType::SWITCH)),
386 Some((5, FrameType::KEY)),
387 Some((6, FrameType::INTER)),
388 Some((7, FrameType::SWITCH)),
389 Some((8, FrameType::INTER)),
390 Some((9, FrameType::SWITCH)),
391 ][..]
392 );
393 }
394
395 #[test]
396 fn minimum_frame_delay() {
397 let mut ctx = setup_encoder::<u8>(
398 64,
399 80,
400 10,
401 100,
402 8,
403 ChromaSampling::Cs420,
404 5,
405 5,
406 0,
407 true,
408 0,
409 true,
410 1,
411 None,
412 );
413
414 let limit = 4; // 4 frames in for 1 frame out (delay of 3 frames)
415 send_frames(&mut ctx, limit, 0);
416
417 let data = get_frame_invariants(ctx)
418 .map(|fi| fi.map(|fi| (fi.input_frameno, fi.frame_type)))
419 .collect::<Vec<_>>();
420
421 assert_eq!(&data[..], &[Some((0, FrameType::KEY))][..]);
422 }
423
424 #[interpolate_test(0, 0)]
425 #[interpolate_test(1, 1)]
426 fn pyramid_level_low_latency_minus(missing: u64) {
427 // Test pyramid_level configurations when there are <missing> less frames
428 // than the perfect subgop size, in no-reorder mode.
429
430 let mut ctx = setup_encoder::<u8>(
431 64,
432 80,
433 10,
434 100,
435 8,
436 ChromaSampling::Cs420,
437 5,
438 5,
439 0,
440 true,
441 0,
442 true,
443 10,
444 None,
445 );
446 let limit = 10 - missing;
447 send_frames(&mut ctx, limit, 0);
448 ctx.flush();
449
450 // data[output_frameno] = pyramid_level
451
452 assert!(get_frame_invariants(ctx)
453 .map(|fi| fi.unwrap().pyramid_level)
454 .all(|pyramid_level| pyramid_level == 0));
455 }
456
457 #[interpolate_test(0, 0)]
458 #[interpolate_test(1, 1)]
459 #[interpolate_test(2, 2)]
460 #[interpolate_test(3, 3)]
461 #[interpolate_test(4, 4)]
462 fn output_frameno_reorder_minus(missing: u64) {
463 // Test output_frameno configurations when there are <missing> less frames
464 // than the perfect subgop size.
465
466 let mut ctx = setup_encoder::<u8>(
467 64,
468 80,
469 10,
470 100,
471 8,
472 ChromaSampling::Cs420,
473 5,
474 5,
475 0,
476 false,
477 0,
478 true,
479 10,
480 None,
481 );
482
483 // TODO: when we support more pyramid depths, this test will need tweaks.
484 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
485
486 let limit = 10 - missing;
487 send_frames(&mut ctx, limit, 0);
488 ctx.flush();
489
490 // data[output_frameno] = (input_frameno, !invalid)
491 let data = get_frame_invariants(ctx)
492 .map(|fi| fi.map(|fi| fi.input_frameno))
493 .collect::<Vec<_>>();
494
495 assert_eq!(
496 &data[..],
497 match missing {
498 0 => {
499 &[
500 Some(0), // I-frame
501 Some(4),
502 Some(2),
503 Some(1),
504 Some(2),
505 Some(3),
506 Some(4),
507 Some(5), // I-frame
508 Some(9),
509 Some(7),
510 Some(6),
511 Some(7),
512 Some(8),
513 Some(9),
514 ][..]
515 }
516 1 => {
517 &[
518 Some(0), // I-frame
519 Some(4),
520 Some(2),
521 Some(1),
522 Some(2),
523 Some(3),
524 Some(4),
525 Some(5), // I-frame
526 None,
527 Some(7),
528 Some(6),
529 Some(7),
530 Some(8),
531 None,
532 ][..]
533 }
534 2 => {
535 &[
536 Some(0), // I-frame
537 Some(4),
538 Some(2),
539 Some(1),
540 Some(2),
541 Some(3),
542 Some(4),
543 Some(5), // I-frame
544 None,
545 Some(7),
546 Some(6),
547 Some(7),
548 None,
549 None,
550 ][..]
551 }
552 3 => {
553 &[
554 Some(0), // I-frame
555 Some(4),
556 Some(2),
557 Some(1),
558 Some(2),
559 Some(3),
560 Some(4),
561 Some(5), // I-frame
562 None,
563 None,
564 Some(6),
565 None,
566 None,
567 None,
568 ][..]
569 }
570 4 => {
571 &[
572 Some(0), // I-frame
573 Some(4),
574 Some(2),
575 Some(1),
576 Some(2),
577 Some(3),
578 Some(4),
579 Some(5), // I-frame
580 ][..]
581 }
582 _ => unreachable!(),
583 }
584 );
585 }
586
587 #[interpolate_test(0, 0)]
588 #[interpolate_test(1, 1)]
589 #[interpolate_test(2, 2)]
590 #[interpolate_test(3, 3)]
591 #[interpolate_test(4, 4)]
592 fn pyramid_level_reorder_minus(missing: u64) {
593 // Test pyramid_level configurations when there are <missing> less frames
594 // than the perfect subgop size.
595
596 let mut ctx = setup_encoder::<u8>(
597 64,
598 80,
599 10,
600 100,
601 8,
602 ChromaSampling::Cs420,
603 5,
604 5,
605 0,
606 false,
607 0,
608 true,
609 10,
610 None,
611 );
612
613 // TODO: when we support more pyramid depths, this test will need tweaks.
614 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
615
616 let limit = 10 - missing;
617 send_frames(&mut ctx, limit, 0);
618 ctx.flush();
619
620 // data[output_frameno] = pyramid_level
621 let data = get_frame_invariants(ctx)
622 .map(|fi| fi.map(|fi| fi.pyramid_level))
623 .collect::<Vec<_>>();
624
625 assert_eq!(
626 &data[..],
627 match missing {
628 0 => {
629 &[
630 Some(0), // I-frame
631 Some(0),
632 Some(1),
633 Some(2),
634 Some(1),
635 Some(2),
636 Some(0),
637 Some(0), // I-frame
638 Some(0),
639 Some(1),
640 Some(2),
641 Some(1),
642 Some(2),
643 Some(0),
644 ][..]
645 }
646 1 => {
647 &[
648 Some(0), // I-frame
649 Some(0),
650 Some(1),
651 Some(2),
652 Some(1),
653 Some(2),
654 Some(0),
655 Some(0), // I-frame
656 None,
657 Some(1),
658 Some(2),
659 Some(1),
660 Some(2),
661 None,
662 ][..]
663 }
664 2 => {
665 &[
666 Some(0), // I-frame
667 Some(0),
668 Some(1),
669 Some(2),
670 Some(1),
671 Some(2),
672 Some(0),
673 Some(0), // I-frame
674 None,
675 Some(1),
676 Some(2),
677 Some(1),
678 None,
679 None,
680 ][..]
681 }
682 3 => {
683 &[
684 Some(0), // I-frame
685 Some(0),
686 Some(1),
687 Some(2),
688 Some(1),
689 Some(2),
690 Some(0),
691 Some(0), // I-frame
692 None,
693 None,
694 Some(2),
695 None,
696 None,
697 None,
698 ][..]
699 }
700 4 => {
701 &[
702 Some(0), // I-frame
703 Some(0),
704 Some(1),
705 Some(2),
706 Some(1),
707 Some(2),
708 Some(0),
709 Some(0), // I-frame
710 ][..]
711 }
712 _ => unreachable!(),
713 }
714 );
715 }
716
717 #[interpolate_test(0, 0)]
718 #[interpolate_test(1, 1)]
719 #[interpolate_test(2, 2)]
720 #[interpolate_test(3, 3)]
721 #[interpolate_test(4, 4)]
722 fn output_frameno_reorder_scene_change_at(scene_change_at: u64) {
723 // Test output_frameno configurations when there's a scene change at the
724 // <scene_change_at>th frame.
725
726 let mut ctx = setup_encoder::<u8>(
727 64,
728 80,
729 10,
730 100,
731 8,
732 ChromaSampling::Cs420,
733 0,
734 5,
735 0,
736 false,
737 0,
738 false,
739 10,
740 None,
741 );
742
743 // TODO: when we support more pyramid depths, this test will need tweaks.
744 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
745
746 let limit = 10;
747 send_frames(&mut ctx, limit, scene_change_at);
748 ctx.flush();
749
750 // data[output_frameno] = (input_frameno, !invalid)
751 let data = get_frame_invariants(ctx)
752 .map(|fi| fi.map(|fi| fi.input_frameno))
753 .collect::<Vec<_>>();
754
755 assert_eq!(
756 &data[..],
757 match scene_change_at {
758 0 => {
759 &[
760 Some(0), // I-frame
761 Some(4),
762 Some(2),
763 Some(1),
764 Some(2),
765 Some(3),
766 Some(4),
767 Some(5),
768 Some(9),
769 Some(7),
770 Some(6),
771 Some(7),
772 Some(8),
773 Some(9),
774 ][..]
775 }
776 1 => {
777 &[
778 Some(0), // I-frame
779 Some(1), // I-frame
780 Some(5),
781 Some(3),
782 Some(2),
783 Some(3),
784 Some(4),
785 Some(5),
786 Some(6),
787 None,
788 Some(8),
789 Some(7),
790 Some(8),
791 Some(9),
792 None,
793 ][..]
794 }
795 2 => {
796 &[
797 Some(0), // I-frame
798 None,
799 None,
800 Some(1),
801 None,
802 None,
803 None,
804 Some(2), // I-frame
805 Some(6),
806 Some(4),
807 Some(3),
808 Some(4),
809 Some(5),
810 Some(6),
811 Some(7),
812 None,
813 Some(9),
814 Some(8),
815 Some(9),
816 None,
817 None,
818 ][..]
819 }
820 3 => {
821 &[
822 Some(0), // I-frame
823 None,
824 Some(2),
825 Some(1),
826 Some(2),
827 None,
828 None,
829 Some(3), // I-frame
830 Some(7),
831 Some(5),
832 Some(4),
833 Some(5),
834 Some(6),
835 Some(7),
836 Some(8),
837 None,
838 None,
839 Some(9),
840 None,
841 None,
842 None,
843 ][..]
844 }
845 4 => {
846 &[
847 Some(0), // I-frame
848 None,
849 Some(2),
850 Some(1),
851 Some(2),
852 Some(3),
853 None,
854 Some(4), // I-frame
855 Some(8),
856 Some(6),
857 Some(5),
858 Some(6),
859 Some(7),
860 Some(8),
861 Some(9),
862 ][..]
863 }
864 _ => unreachable!(),
865 }
866 );
867 }
868
869 #[interpolate_test(0, 0)]
870 #[interpolate_test(1, 1)]
871 #[interpolate_test(2, 2)]
872 #[interpolate_test(3, 3)]
873 #[interpolate_test(4, 4)]
874 fn pyramid_level_reorder_scene_change_at(scene_change_at: u64) {
875 // Test pyramid_level configurations when there's a scene change at the
876 // <scene_change_at>th frame.
877
878 let mut ctx = setup_encoder::<u8>(
879 64,
880 80,
881 10,
882 100,
883 8,
884 ChromaSampling::Cs420,
885 0,
886 5,
887 0,
888 false,
889 0,
890 false,
891 10,
892 None,
893 );
894
895 // TODO: when we support more pyramid depths, this test will need tweaks.
896 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
897
898 let limit = 10;
899 send_frames(&mut ctx, limit, scene_change_at);
900 ctx.flush();
901
902 // data[output_frameno] = pyramid_level
903 let data = get_frame_invariants(ctx)
904 .map(|fi| fi.map(|fi| fi.pyramid_level))
905 .collect::<Vec<_>>();
906
907 assert_eq!(
908 &data[..],
909 match scene_change_at {
910 0 => {
911 &[
912 Some(0), // I-frame
913 Some(0),
914 Some(1),
915 Some(2),
916 Some(1),
917 Some(2),
918 Some(0),
919 Some(0),
920 Some(0),
921 Some(1),
922 Some(2),
923 Some(1),
924 Some(2),
925 Some(0),
926 ][..]
927 }
928 1 => {
929 &[
930 Some(0), // I-frame
931 Some(0), // I-frame
932 Some(0),
933 Some(1),
934 Some(2),
935 Some(1),
936 Some(2),
937 Some(0),
938 Some(0),
939 None,
940 Some(1),
941 Some(2),
942 Some(1),
943 Some(2),
944 None,
945 ][..]
946 }
947 2 => {
948 &[
949 Some(0), // I-frame
950 None,
951 None,
952 Some(2),
953 None,
954 None,
955 None,
956 Some(0), // I-frame
957 Some(0),
958 Some(1),
959 Some(2),
960 Some(1),
961 Some(2),
962 Some(0),
963 Some(0),
964 None,
965 Some(1),
966 Some(2),
967 Some(1),
968 None,
969 None,
970 ][..]
971 }
972 3 => {
973 &[
974 Some(0), // I-frame
975 None,
976 Some(1),
977 Some(2),
978 Some(1),
979 None,
980 None,
981 Some(0), // I-frame
982 Some(0),
983 Some(1),
984 Some(2),
985 Some(1),
986 Some(2),
987 Some(0),
988 Some(0),
989 None,
990 None,
991 Some(2),
992 None,
993 None,
994 None,
995 ][..]
996 }
997 4 => {
998 &[
999 Some(0), // I-frame
1000 None,
1001 Some(1),
1002 Some(2),
1003 Some(1),
1004 Some(2),
1005 None,
1006 Some(0), // I-frame
1007 Some(0),
1008 Some(1),
1009 Some(2),
1010 Some(1),
1011 Some(2),
1012 Some(0),
1013 Some(0),
1014 ][..]
1015 }
1016 _ => unreachable!(),
1017 }
1018 );
1019 }
1020
1021 #[interpolate_test(0, 0)]
1022 #[interpolate_test(1, 1)]
1023 #[interpolate_test(2, 2)]
1024 #[interpolate_test(3, 3)]
1025 #[interpolate_test(4, 4)]
1026 fn output_frameno_incremental_reorder_minus(missing: u64) {
1027 // Test output_frameno configurations when there are <missing> less frames
1028 // than the perfect subgop size, computing the lookahead data incrementally.
1029
1030 let mut ctx = setup_encoder::<u8>(
1031 64,
1032 80,
1033 10,
1034 100,
1035 8,
1036 ChromaSampling::Cs420,
1037 5,
1038 5,
1039 0,
1040 false,
1041 0,
1042 true,
1043 10,
1044 None,
1045 );
1046
1047 // TODO: when we support more pyramid depths, this test will need tweaks.
1048 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
1049
1050 let limit = 10 - missing;
1051 for _ in 0..limit {
1052 send_frames(&mut ctx, 1, 0);
1053 }
1054 ctx.flush();
1055
1056 // data[output_frameno] = (input_frameno, !invalid)
1057 let data = get_frame_invariants(ctx)
1058 .map(|fi| fi.map(|fi| fi.input_frameno))
1059 .collect::<Vec<_>>();
1060
1061 assert_eq!(
1062 &data[..],
1063 match missing {
1064 0 => {
1065 &[
1066 Some(0), // I-frame
1067 Some(4),
1068 Some(2),
1069 Some(1),
1070 Some(2),
1071 Some(3),
1072 Some(4),
1073 Some(5), // I-frame
1074 Some(9),
1075 Some(7),
1076 Some(6),
1077 Some(7),
1078 Some(8),
1079 Some(9),
1080 ][..]
1081 }
1082 1 => {
1083 &[
1084 Some(0), // I-frame
1085 Some(4),
1086 Some(2),
1087 Some(1),
1088 Some(2),
1089 Some(3),
1090 Some(4),
1091 Some(5), // I-frame
1092 None,
1093 Some(7),
1094 Some(6),
1095 Some(7),
1096 Some(8),
1097 None,
1098 ][..]
1099 }
1100 2 => {
1101 &[
1102 Some(0), // I-frame
1103 Some(4),
1104 Some(2),
1105 Some(1),
1106 Some(2),
1107 Some(3),
1108 Some(4),
1109 Some(5), // I-frame
1110 None,
1111 Some(7),
1112 Some(6),
1113 Some(7),
1114 None,
1115 None,
1116 ][..]
1117 }
1118 3 => {
1119 &[
1120 Some(0), // I-frame
1121 Some(4),
1122 Some(2),
1123 Some(1),
1124 Some(2),
1125 Some(3),
1126 Some(4),
1127 Some(5), // I-frame
1128 None,
1129 None,
1130 Some(6),
1131 None,
1132 None,
1133 None,
1134 ][..]
1135 }
1136 4 => {
1137 &[
1138 Some(0), // I-frame
1139 Some(4),
1140 Some(2),
1141 Some(1),
1142 Some(2),
1143 Some(3),
1144 Some(4),
1145 Some(5), // I-frame
1146 ][..]
1147 }
1148 _ => unreachable!(),
1149 }
1150 );
1151 }
1152
1153 #[interpolate_test(0, 0)]
1154 #[interpolate_test(1, 1)]
1155 #[interpolate_test(2, 2)]
1156 #[interpolate_test(3, 3)]
1157 #[interpolate_test(4, 4)]
1158 fn output_frameno_incremental_reorder_scene_change_at(scene_change_at: u64) {
1159 // Test output_frameno configurations when there's a scene change at the
1160 // <scene_change_at>th frame, computing the lookahead data incrementally.
1161
1162 let mut ctx = setup_encoder::<u8>(
1163 64,
1164 80,
1165 10,
1166 100,
1167 8,
1168 ChromaSampling::Cs420,
1169 0,
1170 5,
1171 0,
1172 false,
1173 0,
1174 false,
1175 10,
1176 None,
1177 );
1178
1179 // TODO: when we support more pyramid depths, this test will need tweaks.
1180 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
1181
1182 let limit = 10;
1183 for i in 0..limit {
1184 send_frames(&mut ctx, 1, scene_change_at.saturating_sub(i));
1185 }
1186 ctx.flush();
1187
1188 // data[output_frameno] = (input_frameno, !invalid)
1189 let data = get_frame_invariants(ctx)
1190 .map(|fi| fi.map(|fi| fi.input_frameno))
1191 .collect::<Vec<_>>();
1192
1193 assert_eq!(
1194 &data[..],
1195 match scene_change_at {
1196 0 => {
1197 &[
1198 Some(0), // I-frame
1199 Some(4),
1200 Some(2),
1201 Some(1),
1202 Some(2),
1203 Some(3),
1204 Some(4),
1205 Some(5),
1206 Some(9),
1207 Some(7),
1208 Some(6),
1209 Some(7),
1210 Some(8),
1211 Some(9),
1212 ][..]
1213 }
1214 1 => {
1215 &[
1216 Some(0), // I-frame
1217 Some(1), // I-frame
1218 Some(5),
1219 Some(3),
1220 Some(2),
1221 Some(3),
1222 Some(4),
1223 Some(5),
1224 Some(6),
1225 None,
1226 Some(8),
1227 Some(7),
1228 Some(8),
1229 Some(9),
1230 None,
1231 ][..]
1232 }
1233 2 => {
1234 &[
1235 Some(0), // I-frame
1236 None,
1237 None,
1238 Some(1),
1239 None,
1240 None,
1241 None,
1242 Some(2), // I-frame
1243 Some(6),
1244 Some(4),
1245 Some(3),
1246 Some(4),
1247 Some(5),
1248 Some(6),
1249 Some(7),
1250 None,
1251 Some(9),
1252 Some(8),
1253 Some(9),
1254 None,
1255 None,
1256 ][..]
1257 }
1258 3 => {
1259 &[
1260 Some(0), // I-frame
1261 None,
1262 Some(2),
1263 Some(1),
1264 Some(2),
1265 None,
1266 None,
1267 Some(3), // I-frame
1268 Some(7),
1269 Some(5),
1270 Some(4),
1271 Some(5),
1272 Some(6),
1273 Some(7),
1274 Some(8),
1275 None,
1276 None,
1277 Some(9),
1278 None,
1279 None,
1280 None,
1281 ][..]
1282 }
1283 4 => {
1284 &[
1285 Some(0), // I-frame
1286 None,
1287 Some(2),
1288 Some(1),
1289 Some(2),
1290 Some(3),
1291 None,
1292 Some(4), // I-frame
1293 Some(8),
1294 Some(6),
1295 Some(5),
1296 Some(6),
1297 Some(7),
1298 Some(8),
1299 Some(9),
1300 ][..]
1301 }
1302 _ => unreachable!(),
1303 }
1304 );
1305 }
1306
1307 fn send_frame_kf<T: Pixel>(ctx: &mut Context<T>, keyframe: bool) {
1308 let input = ctx.new_frame();
1309
1310 let frame_type_override =
1311 if keyframe { FrameTypeOverride::Key } else { FrameTypeOverride::No };
1312
1313 let opaque = Some(Opaque::new(keyframe));
1314
1315 let fp = FrameParameters {
1316 frame_type_override,
1317 opaque,
1318 t35_metadata: Box::new([]),
1319 };
1320
1321 let _ = ctx.send_frame((input, fp));
1322 }
1323
1324 #[test]
1325 fn test_opaque_delivery() {
1326 let mut ctx = setup_encoder::<u8>(
1327 64,
1328 80,
1329 10,
1330 100,
1331 8,
1332 ChromaSampling::Cs420,
1333 0,
1334 5,
1335 0,
1336 false,
1337 0,
1338 false,
1339 10,
1340 None,
1341 );
1342
1343 let kf_at = 3;
1344
1345 let limit = 5;
1346 for i in 0..limit {
1347 send_frame_kf(&mut ctx, kf_at == i);
1348 }
1349 ctx.flush();
1350
1351 while let Ok(pkt) = ctx.receive_packet() {
1352 let Packet { opaque, input_frameno, .. } = pkt;
1353 if let Some(opaque) = opaque {
1354 let kf = opaque.downcast::<bool>().unwrap();
1355 assert_eq!(kf, Box::new(input_frameno == kf_at));
1356 }
1357 }
1358 }
1359
1360 fn send_frame_t35<T: Pixel>(ctx: &mut Context<T>) {
1361 let input = ctx.new_frame();
1362
1363 let frame_type_override = FrameTypeOverride::No;
1364
1365 let opaque = None;
1366
1367 let t35_metadata = Box::new([T35 {
1368 country_code: 0xFF,
1369 country_code_extension_byte: 0x00,
1370 data: Box::new(*b"AYAYA"),
1371 }]);
1372
1373 let fp = FrameParameters { frame_type_override, opaque, t35_metadata };
1374
1375 let _ = ctx.send_frame((input, fp));
1376 }
1377
1378 #[test]
1379 fn test_t35_parameter() {
1380 let mut ctx = setup_encoder::<u8>(
1381 64,
1382 80,
1383 10,
1384 100,
1385 8,
1386 ChromaSampling::Cs420,
1387 0,
1388 5,
1389 0,
1390 false,
1391 0,
1392 false,
1393 10,
1394 None,
1395 );
1396
1397 let limit = 2;
1398 for _ in 0..limit {
1399 send_frame_t35(&mut ctx);
1400 }
1401 ctx.flush();
1402
1403 while ctx.receive_packet().is_ok() {}
1404 }
1405
1406 #[interpolate_test(0, 0)]
1407 #[interpolate_test(1, 1)]
1408 #[interpolate_test(2, 2)]
1409 #[interpolate_test(3, 3)]
1410 #[interpolate_test(4, 4)]
1411 fn output_frameno_incremental_reorder_keyframe_at(kf_at: u64) {
1412 // Test output_frameno configurations when there's a forced keyframe at the
1413 // <kf_at>th frame, computing the lookahead data incrementally.
1414
1415 let mut ctx = setup_encoder::<u8>(
1416 64,
1417 80,
1418 10,
1419 100,
1420 8,
1421 ChromaSampling::Cs420,
1422 0,
1423 5,
1424 0,
1425 false,
1426 0,
1427 false,
1428 10,
1429 None,
1430 );
1431
1432 // TODO: when we support more pyramid depths, this test will need tweaks.
1433 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
1434
1435 let limit = 5;
1436 for i in 0..limit {
1437 send_frame_kf(&mut ctx, kf_at == i);
1438 }
1439 ctx.flush();
1440
1441 // data[output_frameno] = (input_frameno, !invalid)
1442 let data = get_frame_invariants(ctx)
1443 .map(|fi| fi.map(|fi| fi.input_frameno))
1444 .collect::<Vec<_>>();
1445
1446 assert_eq!(
1447 &data[..],
1448 match kf_at {
1449 0 => {
1450 &[
1451 Some(0), // I-frame
1452 Some(4),
1453 Some(2),
1454 Some(1),
1455 Some(2),
1456 Some(3),
1457 Some(4),
1458 ][..]
1459 }
1460 1 => {
1461 &[
1462 Some(0), // I-frame
1463 Some(1), // I-frame
1464 None,
1465 Some(3),
1466 Some(2),
1467 Some(3),
1468 Some(4),
1469 None,
1470 ][..]
1471 }
1472 2 => {
1473 &[
1474 Some(0), // I-frame
1475 None,
1476 None,
1477 Some(1),
1478 None,
1479 None,
1480 None,
1481 Some(2), // I-frame
1482 None,
1483 Some(4),
1484 Some(3),
1485 Some(4),
1486 None,
1487 None,
1488 ][..]
1489 }
1490 3 => {
1491 &[
1492 Some(0), // I-frame
1493 None,
1494 Some(2),
1495 Some(1),
1496 Some(2),
1497 None,
1498 None,
1499 Some(3), // I-frame
1500 None,
1501 None,
1502 Some(4),
1503 None,
1504 None,
1505 None,
1506 ][..]
1507 }
1508 4 => {
1509 &[
1510 Some(0), // I-frame
1511 None,
1512 Some(2),
1513 Some(1),
1514 Some(2),
1515 Some(3),
1516 None,
1517 Some(4), // I-frame
1518 ][..]
1519 }
1520 _ => unreachable!(),
1521 }
1522 );
1523 }
1524
1525 #[interpolate_test(1, 1)]
1526 #[interpolate_test(2, 2)]
1527 #[interpolate_test(3, 3)]
1528 fn output_frameno_no_scene_change_at_short_flash(flash_at: u64) {
1529 // Test output_frameno configurations when there's a single-frame flash at the
1530 // <flash_at>th frame.
1531 let mut ctx = setup_encoder::<u8>(
1532 64,
1533 80,
1534 10,
1535 100,
1536 8,
1537 ChromaSampling::Cs420,
1538 0,
1539 5,
1540 0,
1541 false,
1542 0,
1543 false,
1544 10,
1545 None,
1546 );
1547
1548 // TODO: when we support more pyramid depths, this test will need tweaks.
1549 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
1550
1551 let limit = 5;
1552 for i in 0..limit {
1553 if i == flash_at {
1554 send_test_frame(&mut ctx, u8::MIN);
1555 } else {
1556 send_test_frame(&mut ctx, u8::MAX);
1557 }
1558 }
1559 ctx.flush();
1560
1561 // data[output_frameno] = (input_frameno, !invalid)
1562 let data = get_frame_invariants(ctx)
1563 .map(|fi| fi.map(|fi| fi.input_frameno))
1564 .collect::<Vec<_>>();
1565
1566 assert_eq!(
1567 &data[..],
1568 &[
1569 Some(0), // I-frame
1570 Some(4),
1571 Some(2),
1572 Some(1),
1573 Some(2),
1574 Some(3),
1575 Some(4),
1576 ]
1577 );
1578 }
1579
1580 #[test]
1581 fn output_frameno_no_scene_change_at_flash_smaller_than_max_len_flash() {
1582 // Test output_frameno configurations when there's a multi-frame flash
1583 // with length equal to the max flash length
1584
1585 let mut ctx = setup_encoder::<u8>(
1586 64,
1587 80,
1588 10,
1589 100,
1590 8,
1591 ChromaSampling::Cs420,
1592 0,
1593 10,
1594 0,
1595 false,
1596 0,
1597 false,
1598 10,
1599 None,
1600 );
1601
1602 // TODO: when we support more pyramid depths, this test will need tweaks.
1603 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
1604 assert_eq!(ctx.inner.inter_cfg.group_input_len, 4);
1605
1606 send_test_frame(&mut ctx, u8::MIN);
1607 send_test_frame(&mut ctx, u8::MIN);
1608 send_test_frame(&mut ctx, u8::MAX);
1609 send_test_frame(&mut ctx, u8::MAX);
1610 send_test_frame(&mut ctx, u8::MAX);
1611 send_test_frame(&mut ctx, u8::MAX);
1612 send_test_frame(&mut ctx, u8::MIN);
1613 send_test_frame(&mut ctx, u8::MIN);
1614 ctx.flush();
1615
1616 let data = get_frame_invariants(ctx)
1617 .map(|fi| fi.map(|fi| fi.input_frameno))
1618 .collect::<Vec<_>>();
1619
1620 assert_eq!(
1621 &data[..],
1622 &[
1623 Some(0), // I-frame
1624 Some(4),
1625 Some(2),
1626 Some(1),
1627 Some(2),
1628 Some(3),
1629 Some(4),
1630 None,
1631 Some(6),
1632 Some(5),
1633 Some(6),
1634 Some(7),
1635 None,
1636 ]
1637 );
1638 }
1639
1640 #[test]
1641 fn output_frameno_scene_change_before_flash_longer_than_max_flash_len() {
1642 // Test output_frameno configurations when there's a multi-frame flash
1643 // with length greater than the max flash length
1644
1645 let mut ctx = setup_encoder::<u8>(
1646 64,
1647 80,
1648 10,
1649 100,
1650 8,
1651 ChromaSampling::Cs420,
1652 0,
1653 10,
1654 0,
1655 false,
1656 0,
1657 false,
1658 10,
1659 None,
1660 );
1661
1662 // TODO: when we support more pyramid depths, this test will need tweaks.
1663 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
1664 assert_eq!(ctx.inner.inter_cfg.group_input_len, 4);
1665
1666 send_test_frame(&mut ctx, u8::MIN);
1667 send_test_frame(&mut ctx, u8::MIN);
1668 send_test_frame(&mut ctx, u8::MAX);
1669 send_test_frame(&mut ctx, u8::MAX);
1670 send_test_frame(&mut ctx, u8::MAX);
1671 send_test_frame(&mut ctx, u8::MAX);
1672 send_test_frame(&mut ctx, u8::MAX);
1673 send_test_frame(&mut ctx, u8::MIN);
1674 send_test_frame(&mut ctx, u8::MIN);
1675 send_test_frame(&mut ctx, u8::MIN);
1676 send_test_frame(&mut ctx, u8::MIN);
1677 send_test_frame(&mut ctx, u8::MIN);
1678 ctx.flush();
1679
1680 let data = get_frame_invariants(ctx)
1681 .map(|fi| fi.map(|fi| fi.input_frameno))
1682 .collect::<Vec<_>>();
1683
1684 assert_eq!(
1685 &data[..],
1686 &[
1687 Some(0), // I-frame
1688 None,
1689 None,
1690 Some(1),
1691 None,
1692 None,
1693 None,
1694 Some(2), // I-frame
1695 Some(6),
1696 Some(4),
1697 Some(3),
1698 Some(4),
1699 Some(5),
1700 Some(6),
1701 Some(10),
1702 Some(8),
1703 Some(7),
1704 Some(8),
1705 Some(9),
1706 Some(10),
1707 None,
1708 None,
1709 Some(11),
1710 None,
1711 None,
1712 None,
1713 ]
1714 );
1715 }
1716
1717 #[test]
1718 fn output_frameno_scene_change_after_multiple_flashes() {
1719 // Test output_frameno configurations when there are multiple consecutive flashes
1720
1721 let mut ctx = setup_encoder::<u8>(
1722 64,
1723 80,
1724 10,
1725 100,
1726 8,
1727 ChromaSampling::Cs420,
1728 0,
1729 10,
1730 0,
1731 false,
1732 0,
1733 false,
1734 10,
1735 None,
1736 );
1737
1738 // TODO: when we support more pyramid depths, this test will need tweaks.
1739 assert_eq!(ctx.inner.inter_cfg.pyramid_depth, 2);
1740 assert_eq!(ctx.inner.inter_cfg.group_input_len, 4);
1741
1742 send_test_frame(&mut ctx, u8::MIN);
1743 send_test_frame(&mut ctx, u8::MIN);
1744 send_test_frame(&mut ctx, 40);
1745 send_test_frame(&mut ctx, 100);
1746 send_test_frame(&mut ctx, 160);
1747 send_test_frame(&mut ctx, 240);
1748 send_test_frame(&mut ctx, 240);
1749 send_test_frame(&mut ctx, 240);
1750 send_test_frame(&mut ctx, 240);
1751 send_test_frame(&mut ctx, 240);
1752 send_test_frame(&mut ctx, 240);
1753 ctx.flush();
1754
1755 let data = get_frame_invariants(ctx)
1756 .map(|fi| fi.map(|fi| fi.input_frameno))
1757 .collect::<Vec<_>>();
1758
1759 assert_eq!(
1760 &data[..],
1761 &[
1762 Some(0), // I-frame
1763 Some(4),
1764 Some(2),
1765 Some(1),
1766 Some(2),
1767 Some(3),
1768 Some(4),
1769 Some(5),
1770 Some(9),
1771 Some(7),
1772 Some(6),
1773 Some(7),
1774 Some(8),
1775 Some(9),
1776 None,
1777 None,
1778 Some(10),
1779 None,
1780 None,
1781 None,
1782 ]
1783 );
1784 }
1785
1786 #[derive(Clone, Copy)]
1787 struct LookaheadTestExpectations {
1788 pre_receive_frame_q_lens: [usize; 60],
1789 pre_receive_fi_lens: [usize; 60],
1790 post_receive_frame_q_lens: [usize; 60],
1791 post_receive_fi_lens: [usize; 60],
1792 }
1793
1794 #[test]
1795 fn lookahead_size_properly_bounded_8() {
1796 const LOOKAHEAD_SIZE: usize = 8;
1797 const EXPECTATIONS: LookaheadTestExpectations = LookaheadTestExpectations {
1798 pre_receive_frame_q_lens: [
1799 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1800 21, 19, 20, 20, 21, 19, 20, 20, 21, 19, 20, 20, 21, 19, 20, 20, 21, 19,
1801 20, 20, 21, 19, 20, 20, 21, 19, 20, 20, 21, 19, 20, 20, 21, 19, 20, 20,
1802 21, 19, 20, 20,
1803 ],
1804 pre_receive_fi_lens: [
1805 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 7, 7, 7, 7, 13, 13, 13, 13, 19, 19, 19,
1806 14, 20, 19, 19, 14, 20, 19, 19, 14, 20, 19, 19, 14, 20, 19, 19, 14, 20,
1807 19, 19, 14, 20, 19, 19, 14, 20, 19, 19, 14, 20, 19, 19, 14, 20, 19, 19,
1808 14, 20, 19,
1809 ],
1810 post_receive_frame_q_lens: [
1811 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1812 18, 19, 19, 20, 18, 19, 19, 20, 18, 19, 19, 20, 18, 19, 19, 20, 18, 19,
1813 19, 20, 18, 19, 19, 20, 18, 19, 19, 20, 18, 19, 19, 20, 18, 19, 19, 20,
1814 18, 19, 19, 20,
1815 ],
1816 post_receive_fi_lens: [
1817 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 7, 7, 7, 7, 13, 13, 13, 13, 19, 19, 14,
1818 14, 19, 19, 14, 14, 19, 19, 14, 14, 19, 19, 14, 14, 19, 19, 14, 14, 19,
1819 19, 14, 14, 19, 19, 14, 14, 19, 19, 14, 14, 19, 19, 14, 14, 19, 19, 14,
1820 14, 19, 19,
1821 ],
1822 };
1823 lookahead_size_properly_bounded(LOOKAHEAD_SIZE, false, &EXPECTATIONS);
1824 }
1825
1826 #[test]
1827 fn lookahead_size_properly_bounded_10() {
1828 const LOOKAHEAD_SIZE: usize = 10;
1829 const EXPECTATIONS: LookaheadTestExpectations = LookaheadTestExpectations {
1830 pre_receive_frame_q_lens: [
1831 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1832 21, 22, 23, 20, 21, 22, 23, 20, 21, 22, 23, 20, 21, 22, 23, 20, 21, 22,
1833 23, 20, 21, 22, 23, 20, 21, 22, 23, 20, 21, 22, 23, 20, 21, 22, 23, 20,
1834 21, 22, 23, 20,
1835 ],
1836 pre_receive_fi_lens: [
1837 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 7, 7, 7, 7, 13, 13, 13, 13, 19, 19, 19,
1838 19, 25, 19, 19, 19, 25, 19, 19, 19, 25, 19, 19, 19, 25, 19, 19, 19, 25,
1839 19, 19, 19, 25, 19, 19, 19, 25, 19, 19, 19, 25, 19, 19, 19, 25, 19, 19,
1840 19, 25, 19,
1841 ],
1842 post_receive_frame_q_lens: [
1843 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1844 21, 22, 19, 20, 21, 22, 19, 20, 21, 22, 19, 20, 21, 22, 19, 20, 21, 22,
1845 19, 20, 21, 22, 19, 20, 21, 22, 19, 20, 21, 22, 19, 20, 21, 22, 19, 20,
1846 21, 22, 19, 20,
1847 ],
1848 post_receive_fi_lens: [
1849 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 7, 7, 7, 7, 13, 13, 13, 13, 19, 19, 19,
1850 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
1851 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
1852 19, 19, 19,
1853 ],
1854 };
1855 lookahead_size_properly_bounded(LOOKAHEAD_SIZE, false, &EXPECTATIONS);
1856 }
1857
1858 #[test]
1859 fn lookahead_size_properly_bounded_16() {
1860 const LOOKAHEAD_SIZE: usize = 16;
1861 const EXPECTATIONS: LookaheadTestExpectations = LookaheadTestExpectations {
1862 pre_receive_frame_q_lens: [
1863 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1864 21, 22, 23, 24, 25, 26, 27, 28, 29, 27, 28, 28, 29, 27, 28, 28, 29, 27,
1865 28, 28, 29, 27, 28, 28, 29, 27, 28, 28, 29, 27, 28, 28, 29, 27, 28, 28,
1866 29, 27, 28, 28,
1867 ],
1868 pre_receive_fi_lens: [
1869 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 7, 7, 7, 7, 13, 13, 13, 13, 19, 19, 19,
1870 19, 25, 25, 25, 25, 31, 31, 31, 26, 32, 31, 31, 26, 32, 31, 31, 26, 32,
1871 31, 31, 26, 32, 31, 31, 26, 32, 31, 31, 26, 32, 31, 31, 26, 32, 31, 31,
1872 26, 32, 31,
1873 ],
1874 post_receive_frame_q_lens: [
1875 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1876 21, 22, 23, 24, 25, 26, 27, 28, 26, 27, 27, 28, 26, 27, 27, 28, 26, 27,
1877 27, 28, 26, 27, 27, 28, 26, 27, 27, 28, 26, 27, 27, 28, 26, 27, 27, 28,
1878 26, 27, 27, 28,
1879 ],
1880 post_receive_fi_lens: [
1881 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 7, 7, 7, 7, 13, 13, 13, 13, 19, 19, 19,
1882 19, 25, 25, 25, 25, 31, 31, 26, 26, 31, 31, 26, 26, 31, 31, 26, 26, 31,
1883 31, 26, 26, 31, 31, 26, 26, 31, 31, 26, 26, 31, 31, 26, 26, 31, 31, 26,
1884 26, 31, 31,
1885 ],
1886 };
1887 lookahead_size_properly_bounded(LOOKAHEAD_SIZE, false, &EXPECTATIONS);
1888 }
1889
1890 #[test]
1891 fn lookahead_size_properly_bounded_lowlatency_8() {
1892 const LOOKAHEAD_SIZE: usize = 8;
1893 const EXPECTATIONS: LookaheadTestExpectations = LookaheadTestExpectations {
1894 pre_receive_frame_q_lens: [
1895 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 13, 13, 13, 13, 13, 13,
1896 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
1897 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
1898 13, 13, 13, 13,
1899 ],
1900 pre_receive_fi_lens: [
1901 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10,
1902 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
1903 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
1904 10, 10, 10,
1905 ],
1906 post_receive_frame_q_lens: [
1907 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12,
1908 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
1909 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
1910 12, 12, 12, 12,
1911 ],
1912 post_receive_fi_lens: [
1913 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1914 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1915 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1916 ],
1917 };
1918 lookahead_size_properly_bounded(LOOKAHEAD_SIZE, true, &EXPECTATIONS);
1919 }
1920
1921 #[test]
1922 fn lookahead_size_properly_bounded_lowlatency_1() {
1923 const LOOKAHEAD_SIZE: usize = 1;
1924 const EXPECTATIONS: LookaheadTestExpectations = LookaheadTestExpectations {
1925 pre_receive_frame_q_lens: [
1926 1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1927 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1928 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1929 ],
1930 pre_receive_fi_lens: [
1931 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1932 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1933 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1934 ],
1935 post_receive_frame_q_lens: [
1936 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1937 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1938 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1939 ],
1940 post_receive_fi_lens: [
1941 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1942 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1943 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1944 ],
1945 };
1946 lookahead_size_properly_bounded(LOOKAHEAD_SIZE, true, &EXPECTATIONS);
1947 }
1948
1949 fn lookahead_size_properly_bounded(
1950 rdo_lookahead: usize, low_latency: bool,
1951 expectations: &LookaheadTestExpectations,
1952 ) {
1953 // Test that lookahead reads in the proper number of frames at once
1954
1955 let mut ctx = setup_encoder::<u8>(
1956 64,
1957 80,
1958 10,
1959 100,
1960 8,
1961 ChromaSampling::Cs420,
1962 0,
1963 100,
1964 0,
1965 low_latency,
1966 0,
1967 true,
1968 rdo_lookahead,
1969 None,
1970 );
1971
1972 const LIMIT: usize = 60;
1973
1974 let mut pre_receive_frame_q_lens = [0; LIMIT];
1975 let mut pre_receive_fi_lens = [0; LIMIT];
1976 let mut post_receive_frame_q_lens = [0; LIMIT];
1977 let mut post_receive_fi_lens = [0; LIMIT];
1978
1979 for i in 0..LIMIT {
1980 let input = ctx.new_frame();
1981 let _ = ctx.send_frame(input);
1982 pre_receive_frame_q_lens[i] = ctx.inner.frame_q.len();
1983 pre_receive_fi_lens[i] = ctx.inner.frame_data.len();
1984 while ctx.receive_packet().is_ok() {
1985 // Receive packets until lookahead consumed, due to pyramids receiving frames in groups
1986 }
1987 post_receive_frame_q_lens[i] = ctx.inner.frame_q.len();
1988 post_receive_fi_lens[i] = ctx.inner.frame_data.len();
1989 }
1990
1991 assert_eq!(
1992 &pre_receive_frame_q_lens[..],
1993 &expectations.pre_receive_frame_q_lens[..]
1994 );
1995 assert_eq!(&pre_receive_fi_lens[..], &expectations.pre_receive_fi_lens[..]);
1996 assert_eq!(
1997 &post_receive_frame_q_lens[..],
1998 &expectations.post_receive_frame_q_lens[..]
1999 );
2000 assert_eq!(
2001 &post_receive_fi_lens[..],
2002 &expectations.post_receive_fi_lens[..]
2003 );
2004
2005 ctx.flush();
2006 let end = ctx.inner.frame_q.get(&(LIMIT as u64));
2007 assert!(end.is_some());
2008 assert!(end.unwrap().is_none());
2009
2010 while let Ok(_) | Err(EncoderStatus::Encoded) = ctx.receive_packet() {
2011 // Receive packets until all frames consumed
2012 }
2013 assert_eq!(ctx.inner.frames_processed, LIMIT as u64);
2014 }
2015
2016 #[test]
2017 fn zero_frames() {
2018 let config = Config::default();
2019 let mut ctx: Context<u8> = config.new_context().unwrap();
2020 ctx.flush();
2021 assert_eq!(ctx.receive_packet(), Err(EncoderStatus::LimitReached));
2022 }
2023
2024 #[test]
2025 fn tile_cols_overflow() {
2026 let enc = EncoderConfig { tile_cols: usize::MAX, ..Default::default() };
2027 let config = Config::new().with_encoder_config(enc);
2028 let _: Result<Context<u8>, _> = config.new_context();
2029 }
2030
2031 #[test]
2032 fn max_key_frame_interval_overflow() {
2033 let enc = EncoderConfig {
2034 max_key_frame_interval: i32::MAX as u64,
2035 reservoir_frame_delay: None,
2036 ..Default::default()
2037 };
2038 let config = Config::new().with_encoder_config(enc);
2039 let _: Result<Context<u8>, _> = config.new_context();
2040 }
2041
2042 #[test]
2043 fn target_bitrate_overflow() {
2044 let enc = EncoderConfig {
2045 bitrate: i32::MAX,
2046 time_base: Rational::new(i64::MAX as u64, 1),
2047 ..Default::default()
2048 };
2049 let config = Config::new().with_encoder_config(enc);
2050 let _: Result<Context<u8>, _> = config.new_context();
2051 }
2052
2053 #[test]
2054 fn time_base_den_divide_by_zero() {
2055 let enc =
2056 EncoderConfig { time_base: Rational::new(1, 0), ..Default::default() };
2057 let config = Config::new().with_encoder_config(enc);
2058 let _: Result<Context<u8>, _> = config.new_context();
2059 }
2060
2061 #[test]
2062 fn large_width_assert() {
2063 let enc = EncoderConfig { width: u32::MAX as usize, ..Default::default() };
2064 let config = Config::new().with_encoder_config(enc);
2065 let _: Result<Context<u8>, _> = config.new_context();
2066 }
2067
2068 #[test]
2069 fn reservoir_max_overflow() {
2070 let enc = EncoderConfig {
2071 reservoir_frame_delay: Some(i32::MAX),
2072 bitrate: i32::MAX,
2073 time_base: Rational::new(i32::MAX as u64 * 2, 1),
2074 ..Default::default()
2075 };
2076 let config = Config::new().with_encoder_config(enc);
2077 let _: Result<Context<u8>, _> = config.new_context();
2078 }
2079
2080 #[test]
2081 fn zero_width() {
2082 let enc = EncoderConfig { width: 0, ..Default::default() };
2083 let config = Config::new().with_encoder_config(enc);
2084 let res: Result<Context<u8>, _> = config.new_context();
2085 assert!(res.is_err());
2086 }
2087
2088 #[test]
2089 fn rdo_lookahead_frames_overflow() {
2090 let enc = EncoderConfig {
2091 speed_settings: SpeedSettings {
2092 rdo_lookahead_frames: usize::MAX,
2093 ..Default::default()
2094 },
2095 ..Default::default()
2096 };
2097 let config = Config::new().with_encoder_config(enc);
2098 let res: Result<Context<u8>, _> = config.new_context();
2099 assert!(res.is_err());
2100 }
2101
2102 #[test]
2103 fn log_q_exp_overflow() {
2104 let enc = EncoderConfig {
2105 width: 16,
2106 height: 16,
2107 sample_aspect_ratio: Rational::new(1, 1),
2108 bit_depth: 8,
2109 chroma_sampling: ChromaSampling::Cs420,
2110 chroma_sample_position: ChromaSamplePosition::Unknown,
2111 pixel_range: PixelRange::Limited,
2112 color_description: None,
2113 mastering_display: None,
2114 content_light: None,
2115 level_idx: Some(31),
2116 enable_timing_info: false,
2117 still_picture: false,
2118 error_resilient: false,
2119 switch_frame_interval: 0,
2120 time_base: Rational { num: 1, den: 25 },
2121 min_key_frame_interval: 12,
2122 max_key_frame_interval: 240,
2123 reservoir_frame_delay: None,
2124 low_latency: false,
2125 quantizer: 100,
2126 min_quantizer: 64,
2127 bitrate: 1,
2128 tune: Tune::Psychovisual,
2129 film_grain_params: None,
2130 tile_cols: 0,
2131 tile_rows: 0,
2132 tiles: 0,
2133 speed_settings: SpeedSettings {
2134 multiref: false,
2135 fast_deblock: true,
2136 rdo_lookahead_frames: 40,
2137 scene_detection_mode: SceneDetectionSpeed::None,
2138 cdef: true,
2139 lrf: true,
2140 partition: PartitionSpeedSettings {
2141 partition_range: PartitionRange::new(
2142 BlockSize::BLOCK_64X64,
2143 BlockSize::BLOCK_64X64,
2144 ),
2145 encode_bottomup: false,
2146 non_square_partition_max_threshold: BlockSize::BLOCK_4X4,
2147 },
2148 transform: TransformSpeedSettings {
2149 reduced_tx_set: true,
2150 tx_domain_distortion: true,
2151 tx_domain_rate: false,
2152 rdo_tx_decision: false,
2153 ..Default::default()
2154 },
2155 prediction: PredictionSpeedSettings {
2156 prediction_modes: PredictionModesSetting::Simple,
2157 ..Default::default()
2158 },
2159 motion: MotionSpeedSettings {
2160 include_near_mvs: false,
2161 use_satd_subpel: false,
2162 ..Default::default()
2163 },
2164 ..Default::default()
2165 },
2166 };
2167 let config = Config::new().with_encoder_config(enc).with_threads(1);
2168
2169 let mut ctx: Context<u8> = config.new_context().unwrap();
2170 for _ in 0..2 {
2171 ctx.send_frame(ctx.new_frame()).unwrap();
2172 }
2173 ctx.flush();
2174
2175 ctx.receive_packet().unwrap();
2176 let _ = ctx.receive_packet();
2177 }
2178
2179 #[test]
2180 fn guess_frame_subtypes_assert() {
2181 let enc = EncoderConfig {
2182 width: 16,
2183 height: 16,
2184 sample_aspect_ratio: Rational::new(1, 1),
2185 bit_depth: 8,
2186 chroma_sampling: ChromaSampling::Cs420,
2187 chroma_sample_position: ChromaSamplePosition::Unknown,
2188 pixel_range: PixelRange::Limited,
2189 color_description: None,
2190 mastering_display: None,
2191 content_light: None,
2192 level_idx: Some(31),
2193 enable_timing_info: false,
2194 still_picture: false,
2195 error_resilient: false,
2196 switch_frame_interval: 0,
2197 time_base: Rational { num: 1, den: 25 },
2198 min_key_frame_interval: 0,
2199 max_key_frame_interval: 1,
2200 reservoir_frame_delay: None,
2201 low_latency: false,
2202 quantizer: 100,
2203 min_quantizer: 0,
2204 bitrate: 16384,
2205 tune: Tune::Psychovisual,
2206 film_grain_params: None,
2207 tile_cols: 0,
2208 tile_rows: 0,
2209 tiles: 0,
2210 speed_settings: SpeedSettings {
2211 multiref: false,
2212 fast_deblock: true,
2213 rdo_lookahead_frames: 40,
2214 scene_detection_mode: SceneDetectionSpeed::None,
2215 cdef: true,
2216 lrf: true,
2217 partition: PartitionSpeedSettings {
2218 partition_range: PartitionRange::new(
2219 BlockSize::BLOCK_64X64,
2220 BlockSize::BLOCK_64X64,
2221 ),
2222 encode_bottomup: false,
2223 non_square_partition_max_threshold: BlockSize::BLOCK_4X4,
2224 },
2225 transform: TransformSpeedSettings {
2226 reduced_tx_set: true,
2227 tx_domain_distortion: true,
2228 tx_domain_rate: false,
2229 rdo_tx_decision: false,
2230 ..Default::default()
2231 },
2232 prediction: PredictionSpeedSettings {
2233 prediction_modes: PredictionModesSetting::Simple,
2234 ..Default::default()
2235 },
2236 motion: MotionSpeedSettings {
2237 include_near_mvs: false,
2238 use_satd_subpel: false,
2239 ..Default::default()
2240 },
2241 ..Default::default()
2242 },
2243 };
2244 let config = Config::new().with_encoder_config(enc).with_threads(1);
2245
2246 let mut ctx: Context<u8> = config.new_context().unwrap();
2247 ctx.send_frame(ctx.new_frame()).unwrap();
2248 ctx.flush();
2249
2250 ctx.receive_packet().unwrap();
2251 }
2252
2253 #[test]
2254 fn min_quantizer_bounds_correctly() {
2255 let mut ctx = setup_encoder::<u8>(
2256 64,
2257 80,
2258 10,
2259 255,
2260 8,
2261 ChromaSampling::Cs420,
2262 25,
2263 25,
2264 25000,
2265 true,
2266 0,
2267 true,
2268 1,
2269 Some(100),
2270 );
2271
2272 let limit = 25;
2273 send_frames(&mut ctx, limit, 0);
2274 ctx.flush();
2275
2276 for i in 0..limit {
2277 ctx.inner.encode_packet(i).unwrap();
2278 let frame_data = ctx.inner.frame_data.get(&i).unwrap().as_ref().unwrap();
2279 if i == 0 {
2280 assert_eq!(68, frame_data.fi.base_q_idx);
2281 } else {
2282 assert_eq!(96, frame_data.fi.base_q_idx);
2283 }
2284 }
2285
2286 let mut ctx = setup_encoder::<u8>(
2287 64,
2288 80,
2289 10,
2290 255,
2291 8,
2292 ChromaSampling::Cs420,
2293 25,
2294 25,
2295 2000,
2296 true,
2297 0,
2298 true,
2299 1,
2300 Some(100),
2301 );
2302
2303 let limit = 25;
2304 send_frames(&mut ctx, limit, 0);
2305 ctx.flush();
2306
2307 for i in 0..limit {
2308 ctx.inner.encode_packet(i).unwrap();
2309 let frame_data = ctx.inner.frame_data.get(&i).unwrap().as_ref().unwrap();
2310 if i == 0 {
2311 assert!(frame_data.fi.base_q_idx > 68);
2312 } else {
2313 assert!(frame_data.fi.base_q_idx > 96);
2314 }
2315 }
2316 }
2317
2318 #[test]
2319 fn max_quantizer_bounds_correctly() {
2320 let mut ctx = setup_encoder::<u8>(
2321 64,
2322 80,
2323 10,
2324 120,
2325 8,
2326 ChromaSampling::Cs420,
2327 25,
2328 25,
2329 2000,
2330 true,
2331 0,
2332 true,
2333 1,
2334 None,
2335 );
2336
2337 let limit = 25;
2338 send_frames(&mut ctx, limit, 0);
2339 ctx.flush();
2340
2341 for i in 0..limit {
2342 ctx.inner.encode_packet(i).unwrap();
2343 let frame_data = ctx.inner.frame_data.get(&i).unwrap().as_ref().unwrap();
2344 if i == 0 {
2345 assert_eq!(95, frame_data.fi.base_q_idx);
2346 } else {
2347 assert_eq!(115, frame_data.fi.base_q_idx);
2348 }
2349 }
2350
2351 let mut ctx = setup_encoder::<u8>(
2352 64,
2353 80,
2354 10,
2355 120,
2356 8,
2357 ChromaSampling::Cs420,
2358 25,
2359 25,
2360 20000,
2361 true,
2362 0,
2363 true,
2364 1,
2365 None,
2366 );
2367
2368 let limit = 25;
2369 send_frames(&mut ctx, limit, 0);
2370 ctx.flush();
2371
2372 for i in 0..limit {
2373 ctx.inner.encode_packet(i).unwrap();
2374 let frame_data = ctx.inner.frame_data.get(&i).unwrap().as_ref().unwrap();
2375 if i == 0 {
2376 assert!(frame_data.fi.base_q_idx < 95);
2377 } else {
2378 assert!(frame_data.fi.base_q_idx < 115);
2379 }
2380 }
2381 }