-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathinternal.rs
1525 lines (1385 loc) · 51.5 KB
/
internal.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
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
// Copyright (c) 2018-2021, The rav1e contributors. All rights reserved
//
// This source code is subject to the terms of the BSD 2 Clause License and
// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
// was not distributed with this source code in the LICENSE file, you can
// obtain it at www.aomedia.org/license/software. If the Alliance for Open
// Media Patent License 1.0 was not distributed with this source code in the
// PATENTS file, you can obtain it at www.aomedia.org/license/patent.
#![deny(missing_docs)]
use crate::activity::ActivityMask;
use crate::api::lookahead::*;
use crate::api::{EncoderConfig, EncoderStatus, FrameType, Opaque, Packet};
use crate::color::ChromaSampling::Cs400;
use crate::cpu_features::CpuFeatureLevel;
use crate::dist::get_satd;
use crate::encoder::*;
use crate::frame::*;
use crate::partition::*;
use crate::rate::{
RCState, FRAME_NSUBTYPES, FRAME_SUBTYPE_I, FRAME_SUBTYPE_P,
FRAME_SUBTYPE_SEF,
};
use crate::rayon::prelude::*;
use crate::scenechange::SceneChangeDetector;
use crate::stats::EncoderStats;
use crate::tiling::Area;
use crate::util::Pixel;
use arrayvec::ArrayVec;
use rust_hawktracer::*;
use std::cmp;
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use super::{DefaultProgress, GranularProgress};
/// The set of options that controls frame re-ordering and reference picture
/// selection.
/// The options stored here are invariant over the whole encode.
#[derive(Debug, Clone, Copy)]
pub struct InterConfig {
/// Whether frame re-ordering is enabled.
reorder: bool,
/// Whether P-frames can use multiple references.
pub(crate) multiref: bool,
/// The depth of the re-ordering pyramid.
/// The current code cannot support values larger than 2.
pub(crate) pyramid_depth: u64,
/// Number of input frames in group.
pub(crate) group_input_len: u64,
/// Number of output frames in group.
/// This includes both hidden frames and "show existing frame" frames.
group_output_len: u64,
/// Interval between consecutive S-frames.
/// Keyframes reset this interval.
/// This MUST be a multiple of group_input_len.
pub(crate) switch_frame_interval: u64,
}
impl InterConfig {
pub(crate) fn new(enc_config: &EncoderConfig) -> InterConfig {
let reorder = !enc_config.low_latency;
// A group always starts with (group_output_len - group_input_len) hidden
// frames, followed by group_input_len shown frames.
// The shown frames iterate over the input frames in order, with frames
// already encoded as hidden frames now displayed with Show Existing
// Frame.
// For example, for a pyramid depth of 2, the group is as follows:
// |TU |TU |TU |TU
// idx_in_group_output: 0 1 2 3 4 5
// input_frameno: 4 2 1 SEF 3 SEF
// output_frameno: 1 2 3 4 5 6
// level: 0 1 2 1 2 0
// ^^^^^ ^^^^^^^^^^^^^
// hidden shown
// TODO: This only works for pyramid_depth <= 2 --- after that we need
// more hidden frames in the middle of the group.
let pyramid_depth = if reorder { 2 } else { 0 };
let group_input_len = 1 << pyramid_depth;
let group_output_len = group_input_len + pyramid_depth;
let switch_frame_interval = enc_config.switch_frame_interval;
assert!(switch_frame_interval % group_input_len == 0);
InterConfig {
reorder,
multiref: reorder || enc_config.speed_settings.multiref,
pyramid_depth,
group_input_len,
group_output_len,
switch_frame_interval,
}
}
/// Get the index of an output frame in its re-ordering group given the output
/// frame number of the frame in the current keyframe gop.
/// When re-ordering is disabled, this always returns 0.
pub(crate) fn get_idx_in_group_output(
&self, output_frameno_in_gop: u64,
) -> u64 {
// The first frame in the GOP should be a keyframe and is not re-ordered,
// so we should not be calling this function on it.
debug_assert!(output_frameno_in_gop > 0);
(output_frameno_in_gop - 1) % self.group_output_len
}
/// Get the order-hint of an output frame given the output frame number of the
/// frame in the current keyframe gop and the index of that output frame
/// in its re-ordering gorup.
pub(crate) fn get_order_hint(
&self, output_frameno_in_gop: u64, idx_in_group_output: u64,
) -> u32 {
// The first frame in the GOP should be a keyframe, but currently this
// function only handles inter frames.
// We could return 0 for keyframes if keyframe support is needed.
debug_assert!(output_frameno_in_gop > 0);
// Which P-frame group in the current gop is this output frame in?
// Subtract 1 because the first frame in the gop is always a keyframe.
let group_idx = (output_frameno_in_gop - 1) / self.group_output_len;
// Get the offset to the corresponding input frame.
// TODO: This only works with pyramid_depth <= 2.
let offset = if idx_in_group_output < self.pyramid_depth {
self.group_input_len >> idx_in_group_output
} else {
idx_in_group_output - self.pyramid_depth + 1
};
// Construct the final order hint relative to the start of the group.
(self.group_input_len * group_idx + offset) as u32
}
/// Get the level of the current frame in the pyramid.
pub(crate) fn get_level(&self, idx_in_group_output: u64) -> u64 {
if !self.reorder {
0
} else if idx_in_group_output < self.pyramid_depth {
// Hidden frames are output first (to be shown in the future).
idx_in_group_output
} else {
// Shown frames
// TODO: This only works with pyramid_depth <= 2.
pos_to_lvl(
idx_in_group_output - self.pyramid_depth + 1,
self.pyramid_depth,
)
}
}
pub(crate) fn get_slot_idx(&self, level: u64, order_hint: u32) -> u32 {
// Frames with level == 0 are stored in slots 0..4, and frames with higher
// values of level in slots 4..8
if level == 0 {
(order_hint >> self.pyramid_depth) & 3
} else {
// This only works with pyramid_depth <= 4.
3 + level as u32
}
}
pub(crate) const fn get_show_frame(&self, idx_in_group_output: u64) -> bool {
idx_in_group_output >= self.pyramid_depth
}
pub(crate) fn get_show_existing_frame(
&self, idx_in_group_output: u64,
) -> bool {
// The self.reorder test here is redundant, but short-circuits the rest,
// avoiding a bunch of work when it's false.
self.reorder
&& self.get_show_frame(idx_in_group_output)
&& (idx_in_group_output - self.pyramid_depth + 1).count_ones() == 1
&& idx_in_group_output != self.pyramid_depth
}
pub(crate) fn get_input_frameno(
&self, output_frameno_in_gop: u64, gop_input_frameno_start: u64,
) -> u64 {
if output_frameno_in_gop == 0 {
gop_input_frameno_start
} else {
let idx_in_group_output =
self.get_idx_in_group_output(output_frameno_in_gop);
let order_hint =
self.get_order_hint(output_frameno_in_gop, idx_in_group_output);
gop_input_frameno_start + order_hint as u64
}
}
const fn max_reordering_latency(&self) -> u64 {
self.group_input_len
}
pub(crate) fn keyframe_lookahead_distance(&self) -> u64 {
self.max_reordering_latency() + 1
}
pub(crate) fn allowed_ref_frames(&self) -> &[RefType] {
use crate::partition::RefType::*;
if self.reorder {
&ALL_INTER_REFS
} else if self.multiref {
&[LAST_FRAME, LAST2_FRAME, LAST3_FRAME, GOLDEN_FRAME]
} else {
&[LAST_FRAME]
}
}
}
// Thin wrapper for frame-related data
// that gets cached and reused throughout the life of a frame.
#[derive(Clone)]
pub(crate) struct FrameData<T: Pixel> {
pub(crate) fi: FrameInvariants<T>,
pub(crate) fs: FrameState<T>,
}
impl<T: Pixel> FrameData<T> {
pub(crate) fn new(fi: FrameInvariants<T>, frame: Arc<Frame<T>>) -> Self {
let fs = FrameState::new_with_frame(&fi, frame);
FrameData { fi, fs }
}
}
type FrameQueue<T> = BTreeMap<u64, Option<Arc<Frame<T>>>>;
type FrameDataQueue<T> = BTreeMap<u64, FrameData<T>>;
// the fields pub(super) are accessed only by the tests
pub(crate) struct ContextInner<T: Pixel> {
pub(crate) frame_count: u64,
pub(crate) limit: Option<u64>,
pub(crate) output_frameno: u64,
pub(super) inter_cfg: InterConfig,
pub(super) frames_processed: u64,
/// Maps *input_frameno* to frames
pub(super) frame_q: FrameQueue<T>,
/// Maps *output_frameno* to frame data
pub(super) frame_data: FrameDataQueue<T>,
/// A list of the input_frameno for keyframes in this encode.
/// Needed so that we don't need to keep all of the frame_invariants in
/// memory for the whole life of the encode.
// TODO: Is this needed at all?
keyframes: BTreeSet<u64>,
// TODO: Is this needed at all?
keyframes_forced: BTreeSet<u64>,
/// A storage space for reordered frames.
packet_data: Vec<u8>,
/// Maps `output_frameno` to `gop_output_frameno_start`.
gop_output_frameno_start: BTreeMap<u64, u64>,
/// Maps `output_frameno` to `gop_input_frameno_start`.
pub(crate) gop_input_frameno_start: BTreeMap<u64, u64>,
keyframe_detector: SceneChangeDetector<T>,
pub(crate) config: Arc<EncoderConfig>,
seq: Arc<Sequence>,
pub(crate) rc_state: RCState,
maybe_prev_log_base_q: Option<i64>,
/// The next `input_frameno` to be processed by lookahead.
next_lookahead_frame: u64,
/// The next `output_frameno` to be computed by lookahead.
next_lookahead_output_frameno: u64,
/// Optional opaque to be sent back to the user
opaque_q: BTreeMap<u64, Opaque>,
/// Progress callback
pub(crate) progress: Arc<dyn GranularProgress>,
}
impl<T: Pixel> ContextInner<T> {
pub fn new(enc: &EncoderConfig) -> Self {
// initialize with temporal delimiter
let packet_data = TEMPORAL_DELIMITER.to_vec();
let mut keyframes = BTreeSet::new();
keyframes.insert(0);
let maybe_ac_qi_max =
if enc.quantizer < 255 { Some(enc.quantizer as u8) } else { None };
let seq = Arc::new(Sequence::new(enc));
let inter_cfg = InterConfig::new(enc);
let lookahead_distance = inter_cfg.keyframe_lookahead_distance() as usize;
ContextInner {
frame_count: 0,
limit: None,
inter_cfg,
output_frameno: 0,
frames_processed: 0,
frame_q: BTreeMap::new(),
frame_data: BTreeMap::new(),
keyframes,
keyframes_forced: BTreeSet::new(),
packet_data,
gop_output_frameno_start: BTreeMap::new(),
gop_input_frameno_start: BTreeMap::new(),
keyframe_detector: SceneChangeDetector::new(
*enc,
CpuFeatureLevel::default(),
lookahead_distance,
seq.clone(),
),
config: Arc::new(*enc),
seq,
rc_state: RCState::new(
enc.width as i32,
enc.height as i32,
enc.time_base.den as i64,
enc.time_base.num as i64,
enc.bitrate,
maybe_ac_qi_max,
enc.min_quantizer,
enc.max_key_frame_interval as i32,
enc.reservoir_frame_delay,
),
maybe_prev_log_base_q: None,
next_lookahead_frame: 1,
next_lookahead_output_frameno: 0,
opaque_q: BTreeMap::new(),
progress: Arc::new(DefaultProgress {}) as Arc<dyn GranularProgress>,
}
}
#[hawktracer(send_frame)]
pub fn send_frame(
&mut self, frame: Option<Arc<Frame<T>>>, params: Option<FrameParameters>,
) -> Result<(), EncoderStatus> {
let input_frameno = self.frame_count;
let is_flushing = frame.is_none();
if !is_flushing {
self.frame_count += 1;
}
self.frame_q.insert(input_frameno, frame);
if let Some(params) = params {
if params.frame_type_override == FrameTypeOverride::Key {
self.keyframes_forced.insert(input_frameno);
}
if let Some(op) = params.opaque {
self.opaque_q.insert(input_frameno, op);
}
}
if !self.needs_more_frame_q_lookahead(self.next_lookahead_frame) {
let lookahead_frames = self
.frame_q
.range(self.next_lookahead_frame - 1..)
.filter_map(|(&_input_frameno, frame)| frame.clone())
.collect::<Vec<_>>();
if is_flushing {
// This is the last time send_frame is called, process all the
// remaining frames.
for cur_lookahead_frames in
std::iter::successors(Some(&lookahead_frames[..]), |s| s.get(1..))
{
if cur_lookahead_frames.len() < 2 {
// All frames have been processed
break;
}
self.compute_keyframe_placement(cur_lookahead_frames);
}
} else {
self.compute_keyframe_placement(&lookahead_frames);
}
}
self.compute_frame_invariants();
Ok(())
}
/// Indicates whether more frames need to be read into the frame queue
/// in order for frame queue lookahead to be full.
fn needs_more_frame_q_lookahead(&self, input_frameno: u64) -> bool {
let lookahead_end = self.frame_q.keys().last().cloned().unwrap_or(0);
let frames_needed =
input_frameno + self.inter_cfg.keyframe_lookahead_distance() + 1;
lookahead_end < frames_needed && self.needs_more_frames(lookahead_end)
}
/// Indicates whether more frames need to be processed into FrameInvariants
/// in order for FI lookahead to be full.
pub fn needs_more_fi_lookahead(&self) -> bool {
let ready_frames = self.get_rdo_lookahead_frames().count();
ready_frames < self.config.rdo_lookahead_frames + 1
&& self.needs_more_frames(self.next_lookahead_frame)
}
pub fn needs_more_frames(&self, frame_count: u64) -> bool {
self.limit.map(|limit| frame_count < limit).unwrap_or(true)
}
fn get_rdo_lookahead_frames(
&self,
) -> impl Iterator<Item = (&u64, &FrameData<T>)> {
self
.frame_data
.iter()
.skip_while(move |(&output_frameno, _)| {
output_frameno < self.output_frameno
})
.filter(|(_, data)| !data.fi.invalid && !data.fi.show_existing_frame)
.take(self.config.rdo_lookahead_frames + 1)
}
fn next_keyframe_input_frameno(
&self, gop_input_frameno_start: u64, ignore_limit: bool,
) -> u64 {
let next_detected = self
.keyframes
.iter()
.find(|&&input_frameno| input_frameno > gop_input_frameno_start)
.cloned();
let mut next_limit =
gop_input_frameno_start + self.config.max_key_frame_interval;
if !ignore_limit && self.limit.is_some() {
next_limit = next_limit.min(self.limit.unwrap());
}
if next_detected.is_none() {
return next_limit;
}
cmp::min(next_detected.unwrap(), next_limit)
}
fn set_frame_properties(
&mut self, output_frameno: u64,
) -> Result<(), EncoderStatus> {
let fi = self.build_frame_properties(output_frameno)?;
let frame =
self.frame_q.get(&fi.input_frameno).as_ref().unwrap().as_ref().unwrap();
self.frame_data.insert(output_frameno, FrameData::new(fi, frame.clone()));
Ok(())
}
#[allow(unused)]
pub fn build_dump_properties() -> PathBuf {
let mut data_location = PathBuf::new();
if env::var_os("RAV1E_DATA_PATH").is_some() {
data_location.push(&env::var_os("RAV1E_DATA_PATH").unwrap());
} else {
data_location.push(&env::current_dir().unwrap());
data_location.push(".lookahead_data");
}
fs::create_dir_all(&data_location).unwrap();
data_location
}
fn build_frame_properties(
&mut self, output_frameno: u64,
) -> Result<FrameInvariants<T>, EncoderStatus> {
let (prev_gop_output_frameno_start, prev_gop_input_frameno_start) =
if output_frameno == 0 {
(0, 0)
} else {
(
self.gop_output_frameno_start[&(output_frameno - 1)],
self.gop_input_frameno_start[&(output_frameno - 1)],
)
};
self
.gop_output_frameno_start
.insert(output_frameno, prev_gop_output_frameno_start);
self
.gop_input_frameno_start
.insert(output_frameno, prev_gop_input_frameno_start);
let output_frameno_in_gop =
output_frameno - self.gop_output_frameno_start[&output_frameno];
let mut input_frameno = self.inter_cfg.get_input_frameno(
output_frameno_in_gop,
self.gop_input_frameno_start[&output_frameno],
);
if self.needs_more_frame_q_lookahead(input_frameno) {
return Err(EncoderStatus::NeedMoreData);
}
if output_frameno_in_gop > 0 {
let next_keyframe_input_frameno = self.next_keyframe_input_frameno(
self.gop_input_frameno_start[&output_frameno],
false,
);
let prev_input_frameno =
self.frame_data[&(output_frameno - 1)].fi.input_frameno;
if input_frameno >= next_keyframe_input_frameno {
if !self.inter_cfg.reorder
|| ((output_frameno_in_gop - 1) % self.inter_cfg.group_output_len
== 0
&& prev_input_frameno == (next_keyframe_input_frameno - 1))
{
input_frameno = next_keyframe_input_frameno;
// If we'll return early, do it before modifying the state.
match self.frame_q.get(&input_frameno) {
Some(Some(_)) => {}
_ => {
return Err(EncoderStatus::NeedMoreData);
}
}
*self.gop_output_frameno_start.get_mut(&output_frameno).unwrap() =
output_frameno;
*self.gop_input_frameno_start.get_mut(&output_frameno).unwrap() =
next_keyframe_input_frameno;
} else {
let fi = FrameInvariants::new_inter_frame(
&self.frame_data[&(output_frameno - 1)].fi,
&self.inter_cfg,
self.gop_input_frameno_start[&output_frameno],
output_frameno_in_gop,
next_keyframe_input_frameno,
self.config.error_resilient,
);
assert!(fi.invalid);
return Ok(fi);
}
}
}
match self.frame_q.get(&input_frameno) {
Some(Some(_)) => {}
_ => {
return Err(EncoderStatus::NeedMoreData);
}
}
// Now that we know the input_frameno, look up the correct frame type
let frame_type = if self.keyframes.contains(&input_frameno) {
FrameType::KEY
} else {
FrameType::INTER
};
if frame_type == FrameType::KEY {
*self.gop_output_frameno_start.get_mut(&output_frameno).unwrap() =
output_frameno;
*self.gop_input_frameno_start.get_mut(&output_frameno).unwrap() =
input_frameno;
}
let output_frameno_in_gop =
output_frameno - self.gop_output_frameno_start[&output_frameno];
if output_frameno_in_gop == 0 {
let fi = FrameInvariants::new_key_frame(
self.config.clone(),
self.seq.clone(),
self.gop_input_frameno_start[&output_frameno],
);
assert!(!fi.invalid);
Ok(fi)
} else {
let next_keyframe_input_frameno = self.next_keyframe_input_frameno(
self.gop_input_frameno_start[&output_frameno],
false,
);
let fi = FrameInvariants::new_inter_frame(
&self.frame_data[&(output_frameno - 1)].fi,
&self.inter_cfg,
self.gop_input_frameno_start[&output_frameno],
output_frameno_in_gop,
next_keyframe_input_frameno,
self.config.error_resilient,
);
assert!(!fi.invalid);
Ok(fi)
}
}
pub(crate) fn done_processing(&self) -> bool {
self.limit.map(|limit| self.frames_processed == limit).unwrap_or(false)
}
/// Computes lookahead motion vectors and fills in `lookahead_mvs`,
/// `rec_buffer` and `lookahead_rec_buffer` on the `FrameInvariants`. This
/// function must be called after every new `FrameInvariants` is initially
/// computed.
#[hawktracer(compute_lookahead_motion_vectors)]
fn compute_lookahead_motion_vectors(&mut self, output_frameno: u64) {
let qps = {
let frame_data = self.frame_data.get(&output_frameno).unwrap();
let fti = frame_data.fi.get_frame_subtype();
self.rc_state.select_qi(
self,
output_frameno,
fti,
self.maybe_prev_log_base_q,
)
};
let frame_data = self.frame_data.get_mut(&output_frameno).unwrap();
let fs = &mut frame_data.fs;
let fi = &mut frame_data.fi;
// We're only interested in valid frames which are not show-existing-frame.
// Those two don't modify the rec_buffer so there's no need to do anything
// special about it either, it'll propagate on its own.
if fi.invalid || fi.show_existing_frame {
return;
}
#[cfg(feature = "dump_lookahead_data")]
{
let data_location = Self::build_dump_properties();
let plane = &fs.input_qres;
let mut file_name = format!("{:010}-qres", fi.input_frameno);
let buf: Vec<_> = plane.iter().map(|p| p.as_()).collect();
image::GrayImage::from_vec(
plane.cfg.width as u32,
plane.cfg.height as u32,
buf,
)
.unwrap()
.save(data_location.join(file_name).with_extension("png"))
.unwrap();
let plane = &fs.input_hres;
file_name = format!("{:010}-hres", fi.input_frameno);
let buf: Vec<_> = plane.iter().map(|p| p.as_()).collect();
image::GrayImage::from_vec(
plane.cfg.width as u32,
plane.cfg.height as u32,
buf,
)
.unwrap()
.save(data_location.join(file_name).with_extension("png"))
.unwrap();
}
// Do not modify the next output frame's FrameInvariants.
if self.output_frameno == output_frameno {
// We do want to propagate the lookahead_rec_buffer though.
let rfs = Arc::new(ReferenceFrame {
order_hint: fi.order_hint,
width: fi.width as u32,
height: fi.height as u32,
render_width: fi.render_width,
render_height: fi.render_height,
// Use the original frame contents.
frame: fs.input.clone(),
input_hres: fs.input_hres.clone(),
input_qres: fs.input_qres.clone(),
cdfs: fs.cdfs,
frame_me_stats: fs.frame_me_stats.clone(),
output_frameno,
segmentation: fs.segmentation,
});
for i in 0..(REF_FRAMES as usize) {
if (fi.refresh_frame_flags & (1 << i)) != 0 {
fi.lookahead_rec_buffer.frames[i] = Some(Arc::clone(&rfs));
fi.lookahead_rec_buffer.deblock[i] = fs.deblock;
}
}
return;
}
// Our lookahead_rec_buffer should be filled with correct original frame
// data from the previous frames. Copy it into rec_buffer because that's
// what the MV search uses. During the actual encoding rec_buffer is
// overwritten with its correct values anyway.
fi.rec_buffer = fi.lookahead_rec_buffer.clone();
// Estimate lambda with rate-control dry-run
fi.set_quantizers(&qps);
// TODO: as in the encoding code, key frames will have no references.
// However, for block importance purposes we want key frames to act as
// P-frames in this instance.
//
// Compute the motion vectors.
compute_motion_vectors(fi, fs, &self.inter_cfg);
// Save the motion vectors to FrameInvariants.
fi.lookahead_me_stats = fs.frame_me_stats.clone();
#[cfg(feature = "dump_lookahead_data")]
{
use crate::partition::RefType::*;
let data_location = Self::build_dump_properties();
let file_name = format!("{:010}-mvs", fi.input_frameno);
let second_ref_frame = if !self.inter_cfg.multiref {
LAST_FRAME // make second_ref_frame match first
} else if fi.idx_in_group_output == 0 {
LAST2_FRAME
} else {
ALTREF_FRAME
};
// Use the default index, it corresponds to the last P-frame or to the
// backwards lower reference (so the closest previous frame).
let index = if second_ref_frame.to_index() != 0 { 0 } else { 1 };
let me_stats = &fs.frame_me_stats[index];
use byteorder::{NativeEndian, WriteBytesExt};
// dynamic allocation: debugging only
let mut buf = vec![];
buf.write_u64::<NativeEndian>(me_stats.rows as u64).unwrap();
buf.write_u64::<NativeEndian>(me_stats.cols as u64).unwrap();
for y in 0..me_stats.rows {
for x in 0..me_stats.cols {
let mv = me_stats[y][x].mv;
buf.write_i16::<NativeEndian>(mv.row).unwrap();
buf.write_i16::<NativeEndian>(mv.col).unwrap();
}
}
::std::fs::write(
data_location.join(file_name).with_extension("bin"),
buf,
)
.unwrap();
}
// Set lookahead_rec_buffer on this FrameInvariants for future
// FrameInvariants to pick it up.
let rfs = Arc::new(ReferenceFrame {
order_hint: fi.order_hint,
width: fi.width as u32,
height: fi.height as u32,
render_width: fi.render_width,
render_height: fi.render_height,
// Use the original frame contents.
frame: fs.input.clone(),
input_hres: fs.input_hres.clone(),
input_qres: fs.input_qres.clone(),
cdfs: fs.cdfs,
frame_me_stats: fs.frame_me_stats.clone(),
output_frameno,
segmentation: fs.segmentation,
});
for i in 0..(REF_FRAMES as usize) {
if (fi.refresh_frame_flags & (1 << i)) != 0 {
fi.lookahead_rec_buffer.frames[i] = Some(Arc::clone(&rfs));
fi.lookahead_rec_buffer.deblock[i] = fs.deblock;
}
}
}
/// Computes lookahead intra cost approximations and fills in
/// `lookahead_intra_costs` on the `FrameInvariants`.
#[hawktracer(compute_lookahead_intra_costs)]
fn compute_lookahead_intra_costs(&mut self, output_frameno: u64) {
let frame_data = self.frame_data.get(&output_frameno).unwrap();
let fi = &frame_data.fi;
// We're only interested in valid frames which are not show-existing-frame.
if fi.invalid || fi.show_existing_frame {
return;
}
self
.frame_data
.get_mut(&output_frameno)
.unwrap()
.fi
.lookahead_intra_costs = estimate_intra_costs(
&*self.frame_q[&fi.input_frameno].as_ref().unwrap(),
fi.sequence.bit_depth,
fi.cpu_feature_level,
);
}
#[hawktracer(compute_keyframe_placement)]
pub fn compute_keyframe_placement(
&mut self, lookahead_frames: &[Arc<Frame<T>>],
) {
if self.keyframes_forced.contains(&self.next_lookahead_frame)
|| self.keyframe_detector.analyze_next_frame(
lookahead_frames,
self.next_lookahead_frame,
*self.keyframes.iter().last().unwrap(),
)
{
self.keyframes.insert(self.next_lookahead_frame);
}
self.next_lookahead_frame += 1;
}
#[hawktracer(compute_frame_invariants)]
pub fn compute_frame_invariants(&mut self) {
while self.set_frame_properties(self.next_lookahead_output_frameno).is_ok()
{
self
.compute_lookahead_motion_vectors(self.next_lookahead_output_frameno);
if self.config.temporal_rdo() {
self.compute_lookahead_intra_costs(self.next_lookahead_output_frameno);
}
self.next_lookahead_output_frameno += 1;
}
}
#[hawktracer(update_block_importances)]
fn update_block_importances(
fi: &FrameInvariants<T>, me_stats: &crate::me::FrameMEStats,
frame: &Frame<T>, reference_frame: &Frame<T>, bit_depth: usize,
bsize: BlockSize, len: usize,
reference_frame_block_importances: &mut [f32],
) {
let plane_org = &frame.planes[0];
let plane_ref = &reference_frame.planes[0];
let lookahead_intra_costs_lines =
fi.lookahead_intra_costs.par_chunks_exact(fi.w_in_imp_b);
let block_importances_lines =
fi.block_importances.par_chunks_exact(fi.w_in_imp_b);
let costs: Vec<_> = lookahead_intra_costs_lines
.zip(block_importances_lines)
.enumerate()
.flat_map_iter(|(y, (lookahead_intra_costs, block_importances))| {
lookahead_intra_costs
.iter()
.zip(block_importances.iter())
.enumerate()
.map(move |(x, (&intra_cost, &future_importance))| {
let mv = me_stats[y * 2][x * 2].mv;
// Coordinates of the top-left corner of the reference block, in MV
// units.
let reference_x =
x as i64 * IMP_BLOCK_SIZE_IN_MV_UNITS + mv.col as i64;
let reference_y =
y as i64 * IMP_BLOCK_SIZE_IN_MV_UNITS + mv.row as i64;
let region_org = plane_org.region(Area::Rect {
x: (x * IMPORTANCE_BLOCK_SIZE) as isize,
y: (y * IMPORTANCE_BLOCK_SIZE) as isize,
width: IMPORTANCE_BLOCK_SIZE,
height: IMPORTANCE_BLOCK_SIZE,
});
let region_ref = plane_ref.region(Area::Rect {
x: reference_x as isize / IMP_BLOCK_MV_UNITS_PER_PIXEL as isize,
y: reference_y as isize / IMP_BLOCK_MV_UNITS_PER_PIXEL as isize,
width: IMPORTANCE_BLOCK_SIZE,
height: IMPORTANCE_BLOCK_SIZE,
});
let inter_cost = get_satd(
®ion_org,
®ion_ref,
bsize,
bit_depth,
fi.cpu_feature_level,
) as f32;
let intra_cost = intra_cost as f32;
// let intra_cost = lookahead_intra_costs[x] as f32;
// let future_importance = block_importances[x];
let propagate_fraction = if intra_cost <= inter_cost {
0.
} else {
1. - inter_cost / intra_cost
};
let propagate_amount = (intra_cost + future_importance)
* propagate_fraction
/ len as f32;
(propagate_amount, reference_x, reference_y)
})
})
.collect();
costs.into_iter().for_each(
|(propagate_amount, reference_x, reference_y)| {
let mut propagate =
|block_x_in_mv_units, block_y_in_mv_units, fraction| {
let x = block_x_in_mv_units / IMP_BLOCK_SIZE_IN_MV_UNITS;
let y = block_y_in_mv_units / IMP_BLOCK_SIZE_IN_MV_UNITS;
// TODO: propagate partially if the block is partially off-frame
// (possible on right and bottom edges)?
if x >= 0
&& y >= 0
&& (x as usize) < fi.w_in_imp_b
&& (y as usize) < fi.h_in_imp_b
{
reference_frame_block_importances
[y as usize * fi.w_in_imp_b + x as usize] +=
propagate_amount * fraction;
}
};
// Coordinates of the top-left corner of the block intersecting the
// reference block from the top-left.
let top_left_block_x = (reference_x
- if reference_x < 0 { IMP_BLOCK_SIZE_IN_MV_UNITS - 1 } else { 0 })
/ IMP_BLOCK_SIZE_IN_MV_UNITS
* IMP_BLOCK_SIZE_IN_MV_UNITS;
let top_left_block_y = (reference_y
- if reference_y < 0 { IMP_BLOCK_SIZE_IN_MV_UNITS - 1 } else { 0 })
/ IMP_BLOCK_SIZE_IN_MV_UNITS
* IMP_BLOCK_SIZE_IN_MV_UNITS;
debug_assert!(reference_x >= top_left_block_x);
debug_assert!(reference_y >= top_left_block_y);
let top_right_block_x = top_left_block_x + IMP_BLOCK_SIZE_IN_MV_UNITS;
let top_right_block_y = top_left_block_y;
let bottom_left_block_x = top_left_block_x;
let bottom_left_block_y =
top_left_block_y + IMP_BLOCK_SIZE_IN_MV_UNITS;
let bottom_right_block_x = top_right_block_x;
let bottom_right_block_y = bottom_left_block_y;
let top_left_block_fraction = ((top_right_block_x - reference_x)
* (bottom_left_block_y - reference_y))
as f32
/ IMP_BLOCK_AREA_IN_MV_UNITS as f32;
propagate(top_left_block_x, top_left_block_y, top_left_block_fraction);
let top_right_block_fraction =
((reference_x + IMP_BLOCK_SIZE_IN_MV_UNITS - top_right_block_x)
* (bottom_left_block_y - reference_y)) as f32
/ IMP_BLOCK_AREA_IN_MV_UNITS as f32;
propagate(
top_right_block_x,
top_right_block_y,
top_right_block_fraction,
);
let bottom_left_block_fraction = ((top_right_block_x - reference_x)
* (reference_y + IMP_BLOCK_SIZE_IN_MV_UNITS - bottom_left_block_y))
as f32
/ IMP_BLOCK_AREA_IN_MV_UNITS as f32;
propagate(
bottom_left_block_x,
bottom_left_block_y,
bottom_left_block_fraction,
);
let bottom_right_block_fraction =
((reference_x + IMP_BLOCK_SIZE_IN_MV_UNITS - top_right_block_x)
* (reference_y + IMP_BLOCK_SIZE_IN_MV_UNITS - bottom_left_block_y))
as f32
/ IMP_BLOCK_AREA_IN_MV_UNITS as f32;
propagate(
bottom_right_block_x,
bottom_right_block_y,
bottom_right_block_fraction,
);
},
);
}
/// Computes the block importances for the current output frame.
#[hawktracer(compute_block_importances)]
fn compute_block_importances(&mut self) {
// SEF don't need block importances.
if self.frame_data[&self.output_frameno].fi.show_existing_frame {
return;
}
// Get a list of output_framenos that we want to propagate through.
let output_framenos = self
.get_rdo_lookahead_frames()
.map(|(&output_frameno, _)| output_frameno)
.collect::<Vec<_>>();
// The first one should be the current output frame.
assert_eq!(output_framenos[0], self.output_frameno);
// First, initialize them all with zeros.
for output_frameno in output_framenos.iter() {
let fi = &mut self.frame_data.get_mut(output_frameno).unwrap().fi;
for x in fi.block_importances.iter_mut() {
*x = 0.;
}
}
// Now compute and propagate the block importances from the end. The
// current output frame will get its block importances from the future
// frames.
let bsize = BlockSize::from_width_and_height(
IMPORTANCE_BLOCK_SIZE,
IMPORTANCE_BLOCK_SIZE,
);
for &output_frameno in output_framenos.iter().skip(1).rev() {
// TODO: see comment above about key frames not having references.
if self.frame_data.get(&output_frameno).unwrap().fi.frame_type
== FrameType::KEY
{
continue;
}
// Remove fi from the map temporarily and put it back in in the end of
// the iteration. This is required because we need to mutably borrow
// referenced fis from the map, and that wouldn't be possible if this was
// an active borrow.
//
// Performance note: Contrary to intuition,
// removing the data and re-inserting it at the end
// is more performant because it avoids a very expensive clone.
let output_frame_data = self.frame_data.remove(&output_frameno).unwrap();
let fi = &output_frame_data.fi;