-
Notifications
You must be signed in to change notification settings - Fork 1
/
encodeframe.c
2416 lines (2164 loc) · 96.1 KB
/
encodeframe.c
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) 2016, Alliance for Open Media. 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.
*/
#include <limits.h>
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include "config/aom_config.h"
#include "config/aom_dsp_rtcd.h"
#include "config/av1_rtcd.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/binary_codes_writer.h"
#include "aom_ports/mem.h"
#include "aom_ports/aom_timer.h"
#include "aom_util/aom_pthread.h"
#if CONFIG_MISMATCH_DEBUG
#include "aom_util/debug_util.h"
#endif // CONFIG_MISMATCH_DEBUG
#include "av1/common/cfl.h"
#include "av1/common/common.h"
#include "av1/common/common_data.h"
#include "av1/common/entropy.h"
#include "av1/common/entropymode.h"
#include "av1/common/idct.h"
#include "av1/common/mv.h"
#include "av1/common/mvref_common.h"
#include "av1/common/pred_common.h"
#include "av1/common/quant_common.h"
#include "av1/common/reconintra.h"
#include "av1/common/reconinter.h"
#include "av1/common/seg_common.h"
#include "av1/common/tile_common.h"
#include "av1/common/warped_motion.h"
#include "av1/encoder/allintra_vis.h"
#include "av1/encoder/aq_complexity.h"
#include "av1/encoder/aq_cyclicrefresh.h"
#include "av1/encoder/aq_variance.h"
#include "av1/encoder/global_motion_facade.h"
#include "av1/encoder/encodeframe.h"
#include "av1/encoder/encodeframe_utils.h"
#include "av1/encoder/encodemb.h"
#include "av1/encoder/encodemv.h"
#include "av1/encoder/encodetxb.h"
#include "av1/encoder/ethread.h"
#include "av1/encoder/extend.h"
#include "av1/encoder/intra_mode_search_utils.h"
#include "av1/encoder/ml.h"
#include "av1/encoder/motion_search_facade.h"
#include "av1/encoder/partition_strategy.h"
#if !CONFIG_REALTIME_ONLY
#include "av1/encoder/partition_model_weights.h"
#endif
#include "av1/encoder/partition_search.h"
#include "av1/encoder/rd.h"
#include "av1/encoder/rdopt.h"
#include "av1/encoder/reconinter_enc.h"
#include "av1/encoder/segmentation.h"
#include "av1/encoder/tokenize.h"
#include "av1/encoder/tpl_model.h"
#include "av1/encoder/var_based_part.h"
#if CONFIG_TUNE_VMAF
#include "av1/encoder/tune_vmaf.h"
#endif
/*!\cond */
// This is used as a reference when computing the source variance for the
// purposes of activity masking.
// Eventually this should be replaced by custom no-reference routines,
// which will be faster.
static const uint8_t AV1_VAR_OFFS[MAX_SB_SIZE] = {
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128
};
#if CONFIG_AV1_HIGHBITDEPTH
static const uint16_t AV1_HIGH_VAR_OFFS_8[MAX_SB_SIZE] = {
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 128, 128, 128, 128, 128
};
static const uint16_t AV1_HIGH_VAR_OFFS_10[MAX_SB_SIZE] = {
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4,
128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4, 128 * 4
};
static const uint16_t AV1_HIGH_VAR_OFFS_12[MAX_SB_SIZE] = {
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16, 128 * 16,
128 * 16, 128 * 16
};
#endif // CONFIG_AV1_HIGHBITDEPTH
/*!\endcond */
// For the given bit depth, returns a constant array used to assist the
// calculation of source block variance, which will then be used to decide
// adaptive quantizers.
static const uint8_t *get_var_offs(int use_hbd, int bd) {
#if CONFIG_AV1_HIGHBITDEPTH
if (use_hbd) {
assert(bd == 8 || bd == 10 || bd == 12);
const int off_index = (bd - 8) >> 1;
static const uint16_t *high_var_offs[3] = { AV1_HIGH_VAR_OFFS_8,
AV1_HIGH_VAR_OFFS_10,
AV1_HIGH_VAR_OFFS_12 };
return CONVERT_TO_BYTEPTR(high_var_offs[off_index]);
}
#else
(void)use_hbd;
(void)bd;
assert(!use_hbd);
#endif
assert(bd == 8);
return AV1_VAR_OFFS;
}
void av1_init_rtc_counters(MACROBLOCK *const x) {
av1_init_cyclic_refresh_counters(x);
x->cnt_zeromv = 0;
}
void av1_accumulate_rtc_counters(AV1_COMP *cpi, const MACROBLOCK *const x) {
if (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ)
av1_accumulate_cyclic_refresh_counters(cpi->cyclic_refresh, x);
cpi->rc.cnt_zeromv += x->cnt_zeromv;
}
unsigned int av1_get_perpixel_variance(const AV1_COMP *cpi,
const MACROBLOCKD *xd,
const struct buf_2d *ref,
BLOCK_SIZE bsize, int plane,
int use_hbd) {
const int subsampling_x = xd->plane[plane].subsampling_x;
const int subsampling_y = xd->plane[plane].subsampling_y;
const BLOCK_SIZE plane_bsize =
get_plane_block_size(bsize, subsampling_x, subsampling_y);
unsigned int sse;
const unsigned int var = cpi->ppi->fn_ptr[plane_bsize].vf(
ref->buf, ref->stride, get_var_offs(use_hbd, xd->bd), 0, &sse);
return ROUND_POWER_OF_TWO(var, num_pels_log2_lookup[plane_bsize]);
}
unsigned int av1_get_perpixel_variance_facade(const AV1_COMP *cpi,
const MACROBLOCKD *xd,
const struct buf_2d *ref,
BLOCK_SIZE bsize, int plane) {
const int use_hbd = is_cur_buf_hbd(xd);
return av1_get_perpixel_variance(cpi, xd, ref, bsize, plane, use_hbd);
}
void av1_setup_src_planes(MACROBLOCK *x, const YV12_BUFFER_CONFIG *src,
int mi_row, int mi_col, const int num_planes,
BLOCK_SIZE bsize) {
// Set current frame pointer.
x->e_mbd.cur_buf = src;
// We use AOMMIN(num_planes, MAX_MB_PLANE) instead of num_planes to quiet
// the static analysis warnings.
for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); i++) {
const int is_uv = i > 0;
setup_pred_plane(
&x->plane[i].src, bsize, src->buffers[i], src->crop_widths[is_uv],
src->crop_heights[is_uv], src->strides[is_uv], mi_row, mi_col, NULL,
x->e_mbd.plane[i].subsampling_x, x->e_mbd.plane[i].subsampling_y);
}
}
#if !CONFIG_REALTIME_ONLY
/*!\brief Assigns different quantization parameters to each super
* block based on its TPL weight.
*
* \ingroup tpl_modelling
*
* \param[in] cpi Top level encoder instance structure
* \param[in,out] td Thread data structure
* \param[in,out] x Macro block level data for this block.
* \param[in] tile_info Tile infromation / identification
* \param[in] mi_row Block row (in "MI_SIZE" units) index
* \param[in] mi_col Block column (in "MI_SIZE" units) index
* \param[out] num_planes Number of image planes (e.g. Y,U,V)
*
* \remark No return value but updates macroblock and thread data
* related to the q / q delta to be used.
*/
static AOM_INLINE void setup_delta_q(AV1_COMP *const cpi, ThreadData *td,
MACROBLOCK *const x,
const TileInfo *const tile_info,
int mi_row, int mi_col, int num_planes) {
AV1_COMMON *const cm = &cpi->common;
const CommonModeInfoParams *const mi_params = &cm->mi_params;
const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
assert(delta_q_info->delta_q_present_flag);
const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
// Delta-q modulation based on variance
av1_setup_src_planes(x, cpi->source, mi_row, mi_col, num_planes, sb_size);
const int delta_q_res = delta_q_info->delta_q_res;
int current_qindex = cm->quant_params.base_qindex;
if (cpi->use_ducky_encode && cpi->ducky_encode_info.frame_info.qp_mode ==
DUCKY_ENCODE_FRAME_MODE_QINDEX) {
const int sb_row = mi_row >> cm->seq_params->mib_size_log2;
const int sb_col = mi_col >> cm->seq_params->mib_size_log2;
const int sb_cols =
CEIL_POWER_OF_TWO(cm->mi_params.mi_cols, cm->seq_params->mib_size_log2);
const int sb_index = sb_row * sb_cols + sb_col;
current_qindex =
cpi->ducky_encode_info.frame_info.superblock_encode_qindex[sb_index];
} else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL) {
if (DELTA_Q_PERCEPTUAL_MODULATION == 1) {
const int block_wavelet_energy_level =
av1_block_wavelet_energy_level(cpi, x, sb_size);
x->sb_energy_level = block_wavelet_energy_level;
current_qindex = av1_compute_q_from_energy_level_deltaq_mode(
cpi, block_wavelet_energy_level);
} else {
const int block_var_level = av1_log_block_var(cpi, x, sb_size);
x->sb_energy_level = block_var_level;
current_qindex =
av1_compute_q_from_energy_level_deltaq_mode(cpi, block_var_level);
}
} else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_OBJECTIVE &&
cpi->oxcf.algo_cfg.enable_tpl_model) {
// Setup deltaq based on tpl stats
current_qindex =
av1_get_q_for_deltaq_objective(cpi, td, NULL, sb_size, mi_row, mi_col);
} else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL_AI) {
current_qindex = av1_get_sbq_perceptual_ai(cpi, sb_size, mi_row, mi_col);
} else if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED) {
current_qindex = av1_get_sbq_user_rating_based(cpi, mi_row, mi_col);
} else if (cpi->oxcf.q_cfg.enable_hdr_deltaq) {
current_qindex = av1_get_q_for_hdr(cpi, x, sb_size, mi_row, mi_col);
}
x->rdmult_cur_qindex = current_qindex;
MACROBLOCKD *const xd = &x->e_mbd;
const int adjusted_qindex = av1_adjust_q_from_delta_q_res(
delta_q_res, xd->current_base_qindex, current_qindex);
if (cpi->use_ducky_encode) {
assert(adjusted_qindex == current_qindex);
}
current_qindex = adjusted_qindex;
x->delta_qindex = current_qindex - cm->quant_params.base_qindex;
x->rdmult_delta_qindex = x->delta_qindex;
av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
xd->mi[0]->current_qindex = current_qindex;
av1_init_plane_quantizers(cpi, x, xd->mi[0]->segment_id, 0);
// keep track of any non-zero delta-q used
td->deltaq_used |= (x->delta_qindex != 0);
if (cpi->oxcf.tool_cfg.enable_deltalf_mode) {
const int delta_lf_res = delta_q_info->delta_lf_res;
const int lfmask = ~(delta_lf_res - 1);
const int delta_lf_from_base =
((x->delta_qindex / 4 + delta_lf_res / 2) & lfmask);
const int8_t delta_lf =
(int8_t)clamp(delta_lf_from_base, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
const int frame_lf_count =
av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
const int mib_size = cm->seq_params->mib_size;
// pre-set the delta lf for loop filter. Note that this value is set
// before mi is assigned for each block in current superblock
for (int j = 0; j < AOMMIN(mib_size, mi_params->mi_rows - mi_row); j++) {
for (int k = 0; k < AOMMIN(mib_size, mi_params->mi_cols - mi_col); k++) {
const int grid_idx = get_mi_grid_idx(mi_params, mi_row + j, mi_col + k);
mi_params->mi_alloc[grid_idx].delta_lf_from_base = delta_lf;
for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
mi_params->mi_alloc[grid_idx].delta_lf[lf_id] = delta_lf;
}
}
}
}
}
static void init_ref_frame_space(AV1_COMP *cpi, ThreadData *td, int mi_row,
int mi_col) {
const AV1_COMMON *cm = &cpi->common;
const GF_GROUP *const gf_group = &cpi->ppi->gf_group;
const CommonModeInfoParams *const mi_params = &cm->mi_params;
MACROBLOCK *x = &td->mb;
const int frame_idx = cpi->gf_frame_index;
TplParams *const tpl_data = &cpi->ppi->tpl_data;
const uint8_t block_mis_log2 = tpl_data->tpl_stats_block_mis_log2;
av1_zero(x->tpl_keep_ref_frame);
if (!av1_tpl_stats_ready(tpl_data, frame_idx)) return;
if (!is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) return;
if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return;
const int is_overlay =
cpi->ppi->gf_group.update_type[frame_idx] == OVERLAY_UPDATE;
if (is_overlay) {
memset(x->tpl_keep_ref_frame, 1, sizeof(x->tpl_keep_ref_frame));
return;
}
TplDepFrame *tpl_frame = &tpl_data->tpl_frame[frame_idx];
TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
const int tpl_stride = tpl_frame->stride;
int64_t inter_cost[INTER_REFS_PER_FRAME] = { 0 };
const int step = 1 << block_mis_log2;
const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
const int mi_row_end =
AOMMIN(mi_size_high[sb_size] + mi_row, mi_params->mi_rows);
const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
const int mi_col_sr =
coded_to_superres_mi(mi_col, cm->superres_scale_denominator);
const int mi_col_end_sr =
AOMMIN(coded_to_superres_mi(mi_col + mi_size_wide[sb_size],
cm->superres_scale_denominator),
mi_cols_sr);
const int row_step = step;
const int col_step_sr =
coded_to_superres_mi(step, cm->superres_scale_denominator);
for (int row = mi_row; row < mi_row_end; row += row_step) {
for (int col = mi_col_sr; col < mi_col_end_sr; col += col_step_sr) {
const TplDepStats *this_stats =
&tpl_stats[av1_tpl_ptr_pos(row, col, tpl_stride, block_mis_log2)];
int64_t tpl_pred_error[INTER_REFS_PER_FRAME] = { 0 };
// Find the winner ref frame idx for the current block
int64_t best_inter_cost = this_stats->pred_error[0];
int best_rf_idx = 0;
for (int idx = 1; idx < INTER_REFS_PER_FRAME; ++idx) {
if ((this_stats->pred_error[idx] < best_inter_cost) &&
(this_stats->pred_error[idx] != 0)) {
best_inter_cost = this_stats->pred_error[idx];
best_rf_idx = idx;
}
}
// tpl_pred_error is the pred_error reduction of best_ref w.r.t.
// LAST_FRAME.
tpl_pred_error[best_rf_idx] = this_stats->pred_error[best_rf_idx] -
this_stats->pred_error[LAST_FRAME - 1];
for (int rf_idx = 1; rf_idx < INTER_REFS_PER_FRAME; ++rf_idx)
inter_cost[rf_idx] += tpl_pred_error[rf_idx];
}
}
int rank_index[INTER_REFS_PER_FRAME - 1];
for (int idx = 0; idx < INTER_REFS_PER_FRAME - 1; ++idx) {
rank_index[idx] = idx + 1;
for (int i = idx; i > 0; --i) {
if (inter_cost[rank_index[i - 1]] > inter_cost[rank_index[i]]) {
const int tmp = rank_index[i - 1];
rank_index[i - 1] = rank_index[i];
rank_index[i] = tmp;
}
}
}
x->tpl_keep_ref_frame[INTRA_FRAME] = 1;
x->tpl_keep_ref_frame[LAST_FRAME] = 1;
int cutoff_ref = 0;
for (int idx = 0; idx < INTER_REFS_PER_FRAME - 1; ++idx) {
x->tpl_keep_ref_frame[rank_index[idx] + LAST_FRAME] = 1;
if (idx > 2) {
if (!cutoff_ref) {
// If the predictive coding gains are smaller than the previous more
// relevant frame over certain amount, discard this frame and all the
// frames afterwards.
if (llabs(inter_cost[rank_index[idx]]) <
llabs(inter_cost[rank_index[idx - 1]]) / 8 ||
inter_cost[rank_index[idx]] == 0)
cutoff_ref = 1;
}
if (cutoff_ref) x->tpl_keep_ref_frame[rank_index[idx] + LAST_FRAME] = 0;
}
}
}
static AOM_INLINE void adjust_rdmult_tpl_model(AV1_COMP *cpi, MACROBLOCK *x,
int mi_row, int mi_col) {
const BLOCK_SIZE sb_size = cpi->common.seq_params->sb_size;
const int orig_rdmult = cpi->rd.RDMULT;
assert(IMPLIES(cpi->ppi->gf_group.size > 0,
cpi->gf_frame_index < cpi->ppi->gf_group.size));
const int gf_group_index = cpi->gf_frame_index;
if (cpi->oxcf.algo_cfg.enable_tpl_model && cpi->oxcf.q_cfg.aq_mode == NO_AQ &&
cpi->oxcf.q_cfg.deltaq_mode == NO_DELTA_Q && gf_group_index > 0 &&
cpi->ppi->gf_group.update_type[gf_group_index] == ARF_UPDATE) {
const int dr =
av1_get_rdmult_delta(cpi, sb_size, mi_row, mi_col, orig_rdmult);
x->rdmult = dr;
}
}
#endif // !CONFIG_REALTIME_ONLY
#if CONFIG_RT_ML_PARTITIONING
// Get a prediction(stored in x->est_pred) for the whole superblock.
static void get_estimated_pred(AV1_COMP *cpi, const TileInfo *const tile,
MACROBLOCK *x, int mi_row, int mi_col) {
AV1_COMMON *const cm = &cpi->common;
const int is_key_frame = frame_is_intra_only(cm);
MACROBLOCKD *xd = &x->e_mbd;
// TODO(kyslov) Extend to 128x128
assert(cm->seq_params->sb_size == BLOCK_64X64);
av1_set_offsets(cpi, tile, x, mi_row, mi_col, BLOCK_64X64);
if (!is_key_frame) {
MB_MODE_INFO *mi = xd->mi[0];
const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, LAST_FRAME);
assert(yv12 != NULL);
av1_setup_pre_planes(xd, 0, yv12, mi_row, mi_col,
get_ref_scale_factors(cm, LAST_FRAME), 1);
mi->ref_frame[0] = LAST_FRAME;
mi->ref_frame[1] = NONE;
mi->bsize = BLOCK_64X64;
mi->mv[0].as_int = 0;
mi->interp_filters = av1_broadcast_interp_filter(BILINEAR);
set_ref_ptrs(cm, xd, mi->ref_frame[0], mi->ref_frame[1]);
xd->plane[0].dst.buf = x->est_pred;
xd->plane[0].dst.stride = 64;
av1_enc_build_inter_predictor_y(xd, mi_row, mi_col);
} else {
#if CONFIG_AV1_HIGHBITDEPTH
switch (xd->bd) {
case 8: memset(x->est_pred, 128, 64 * 64 * sizeof(x->est_pred[0])); break;
case 10:
memset(x->est_pred, 128 * 4, 64 * 64 * sizeof(x->est_pred[0]));
break;
case 12:
memset(x->est_pred, 128 * 16, 64 * 64 * sizeof(x->est_pred[0]));
break;
}
#else
memset(x->est_pred, 128, 64 * 64 * sizeof(x->est_pred[0]));
#endif // CONFIG_VP9_HIGHBITDEPTH
}
}
#endif // CONFIG_RT_ML_PARTITIONING
#define AVG_CDF_WEIGHT_LEFT 3
#define AVG_CDF_WEIGHT_TOP_RIGHT 1
/*!\brief Encode a superblock (minimal RD search involved)
*
* \ingroup partition_search
* Encodes the superblock by a pre-determined partition pattern, only minor
* rd-based searches are allowed to adjust the initial pattern. It is only used
* by realtime encoding.
*/
static AOM_INLINE void encode_nonrd_sb(AV1_COMP *cpi, ThreadData *td,
TileDataEnc *tile_data, TokenExtra **tp,
const int mi_row, const int mi_col,
const int seg_skip) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCK *const x = &td->mb;
const SPEED_FEATURES *const sf = &cpi->sf;
const TileInfo *const tile_info = &tile_data->tile_info;
MB_MODE_INFO **mi = cm->mi_params.mi_grid_base +
get_mi_grid_idx(&cm->mi_params, mi_row, mi_col);
const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
PC_TREE *const pc_root = td->pc_root;
#if CONFIG_RT_ML_PARTITIONING
if (sf->part_sf.partition_search_type == ML_BASED_PARTITION) {
RD_STATS dummy_rdc;
get_estimated_pred(cpi, tile_info, x, mi_row, mi_col);
av1_nonrd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col,
BLOCK_64X64, &dummy_rdc, 1, INT64_MAX, pc_root);
return;
}
#endif
// Set the partition
if (sf->part_sf.partition_search_type == FIXED_PARTITION || seg_skip ||
(sf->rt_sf.use_fast_fixed_part && x->sb_force_fixed_part == 1 &&
(!frame_is_intra_only(cm) &&
(!cpi->ppi->use_svc ||
!cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame)))) {
// set a fixed-size partition
av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
BLOCK_SIZE bsize_select = sf->part_sf.fixed_partition_size;
if (sf->rt_sf.use_fast_fixed_part &&
x->content_state_sb.source_sad_nonrd < kLowSad) {
bsize_select = BLOCK_64X64;
}
const BLOCK_SIZE bsize = seg_skip ? sb_size : bsize_select;
av1_set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
} else if (sf->part_sf.partition_search_type == VAR_BASED_PARTITION) {
// set a variance-based partition
av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
av1_choose_var_based_partitioning(cpi, tile_info, td, x, mi_row, mi_col);
}
assert(sf->part_sf.partition_search_type == FIXED_PARTITION || seg_skip ||
sf->part_sf.partition_search_type == VAR_BASED_PARTITION);
set_cb_offsets(td->mb.cb_offset, 0, 0);
// Initialize the flag to skip cdef to 1.
if (sf->rt_sf.skip_cdef_sb) {
const int block64_in_sb = (sb_size == BLOCK_128X128) ? 2 : 1;
// If 128x128 block is used, we need to set the flag for all 4 64x64 sub
// "blocks".
for (int r = 0; r < block64_in_sb; ++r) {
for (int c = 0; c < block64_in_sb; ++c) {
const int idx_in_sb =
r * MI_SIZE_64X64 * cm->mi_params.mi_stride + c * MI_SIZE_64X64;
if (mi[idx_in_sb]) mi[idx_in_sb]->cdef_strength = 1;
}
}
}
#if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, nonrd_use_partition_time);
#endif
av1_nonrd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, sb_size,
pc_root);
#if CONFIG_COLLECT_COMPONENT_TIMING
end_timing(cpi, nonrd_use_partition_time);
#endif
}
// This function initializes the stats for encode_rd_sb.
static INLINE void init_encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
const TileDataEnc *tile_data,
SIMPLE_MOTION_DATA_TREE *sms_root,
RD_STATS *rd_cost, int mi_row, int mi_col,
int gather_tpl_data) {
const AV1_COMMON *cm = &cpi->common;
const TileInfo *tile_info = &tile_data->tile_info;
MACROBLOCK *x = &td->mb;
const SPEED_FEATURES *sf = &cpi->sf;
const int use_simple_motion_search =
(sf->part_sf.simple_motion_search_split ||
sf->part_sf.simple_motion_search_prune_rect ||
sf->part_sf.simple_motion_search_early_term_none ||
sf->part_sf.ml_early_term_after_part_split_level) &&
!frame_is_intra_only(cm);
if (use_simple_motion_search) {
av1_init_simple_motion_search_mvs_for_sb(cpi, tile_info, x, sms_root,
mi_row, mi_col);
}
#if !CONFIG_REALTIME_ONLY
if (!(has_no_stats_stage(cpi) && cpi->oxcf.mode == REALTIME &&
cpi->oxcf.gf_cfg.lag_in_frames == 0)) {
init_ref_frame_space(cpi, td, mi_row, mi_col);
x->sb_energy_level = 0;
x->part_search_info.cnn_output_valid = 0;
if (gather_tpl_data) {
if (cm->delta_q_info.delta_q_present_flag) {
const int num_planes = av1_num_planes(cm);
const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
setup_delta_q(cpi, td, x, tile_info, mi_row, mi_col, num_planes);
av1_tpl_rdmult_setup_sb(cpi, x, sb_size, mi_row, mi_col);
}
// TODO(jingning): revisit this function.
if (cpi->oxcf.algo_cfg.enable_tpl_model && (0)) {
adjust_rdmult_tpl_model(cpi, x, mi_row, mi_col);
}
}
}
#else
(void)tile_info;
(void)mi_row;
(void)mi_col;
(void)gather_tpl_data;
#endif
x->reuse_inter_pred = false;
x->txfm_search_params.mode_eval_type = DEFAULT_EVAL;
reset_mb_rd_record(x->txfm_search_info.mb_rd_record);
av1_zero(x->picked_ref_frames_mask);
av1_invalid_rd_stats(rd_cost);
}
#if !CONFIG_REALTIME_ONLY
static void sb_qp_sweep_init_quantizers(AV1_COMP *cpi, ThreadData *td,
const TileDataEnc *tile_data,
SIMPLE_MOTION_DATA_TREE *sms_tree,
RD_STATS *rd_cost, int mi_row,
int mi_col, int delta_qp_ofs) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCK *const x = &td->mb;
const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
const TileInfo *tile_info = &tile_data->tile_info;
const CommonModeInfoParams *const mi_params = &cm->mi_params;
const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
assert(delta_q_info->delta_q_present_flag);
const int delta_q_res = delta_q_info->delta_q_res;
const SPEED_FEATURES *sf = &cpi->sf;
const int use_simple_motion_search =
(sf->part_sf.simple_motion_search_split ||
sf->part_sf.simple_motion_search_prune_rect ||
sf->part_sf.simple_motion_search_early_term_none ||
sf->part_sf.ml_early_term_after_part_split_level) &&
!frame_is_intra_only(cm);
if (use_simple_motion_search) {
av1_init_simple_motion_search_mvs_for_sb(cpi, tile_info, x, sms_tree,
mi_row, mi_col);
}
int current_qindex = x->rdmult_cur_qindex + delta_qp_ofs;
MACROBLOCKD *const xd = &x->e_mbd;
current_qindex = av1_adjust_q_from_delta_q_res(
delta_q_res, xd->current_base_qindex, current_qindex);
x->delta_qindex = current_qindex - cm->quant_params.base_qindex;
av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
xd->mi[0]->current_qindex = current_qindex;
av1_init_plane_quantizers(cpi, x, xd->mi[0]->segment_id, 0);
// keep track of any non-zero delta-q used
td->deltaq_used |= (x->delta_qindex != 0);
if (cpi->oxcf.tool_cfg.enable_deltalf_mode) {
const int delta_lf_res = delta_q_info->delta_lf_res;
const int lfmask = ~(delta_lf_res - 1);
const int delta_lf_from_base =
((x->delta_qindex / 4 + delta_lf_res / 2) & lfmask);
const int8_t delta_lf =
(int8_t)clamp(delta_lf_from_base, -MAX_LOOP_FILTER, MAX_LOOP_FILTER);
const int frame_lf_count =
av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
const int mib_size = cm->seq_params->mib_size;
// pre-set the delta lf for loop filter. Note that this value is set
// before mi is assigned for each block in current superblock
for (int j = 0; j < AOMMIN(mib_size, mi_params->mi_rows - mi_row); j++) {
for (int k = 0; k < AOMMIN(mib_size, mi_params->mi_cols - mi_col); k++) {
const int grid_idx = get_mi_grid_idx(mi_params, mi_row + j, mi_col + k);
mi_params->mi_alloc[grid_idx].delta_lf_from_base = delta_lf;
for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
mi_params->mi_alloc[grid_idx].delta_lf[lf_id] = delta_lf;
}
}
}
}
x->reuse_inter_pred = false;
x->txfm_search_params.mode_eval_type = DEFAULT_EVAL;
reset_mb_rd_record(x->txfm_search_info.mb_rd_record);
av1_zero(x->picked_ref_frames_mask);
av1_invalid_rd_stats(rd_cost);
}
static int sb_qp_sweep(AV1_COMP *const cpi, ThreadData *td,
TileDataEnc *tile_data, TokenExtra **tp, int mi_row,
int mi_col, BLOCK_SIZE bsize,
SIMPLE_MOTION_DATA_TREE *sms_tree,
SB_FIRST_PASS_STATS *sb_org_stats) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCK *const x = &td->mb;
RD_STATS rdc_winner, cur_rdc;
av1_invalid_rd_stats(&rdc_winner);
int best_qindex = td->mb.rdmult_delta_qindex;
const int start = cm->current_frame.frame_type == KEY_FRAME ? -20 : -12;
const int end = cm->current_frame.frame_type == KEY_FRAME ? 20 : 12;
const int step = cm->delta_q_info.delta_q_res;
for (int sweep_qp_delta = start; sweep_qp_delta <= end;
sweep_qp_delta += step) {
sb_qp_sweep_init_quantizers(cpi, td, tile_data, sms_tree, &cur_rdc, mi_row,
mi_col, sweep_qp_delta);
const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
const int backup_current_qindex =
cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
av1_reset_mbmi(&cm->mi_params, bsize, mi_row, mi_col);
av1_restore_sb_state(sb_org_stats, cpi, td, tile_data, mi_row, mi_col);
cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex = backup_current_qindex;
td->pc_root = av1_alloc_pc_tree_node(bsize);
if (!td->pc_root)
aom_internal_error(x->e_mbd.error_info, AOM_CODEC_MEM_ERROR,
"Failed to allocate PC_TREE");
av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, bsize,
&cur_rdc, cur_rdc, td->pc_root, sms_tree, NULL,
SB_DRY_PASS, NULL);
if ((rdc_winner.rdcost > cur_rdc.rdcost) ||
(abs(sweep_qp_delta) < abs(best_qindex - x->rdmult_delta_qindex) &&
rdc_winner.rdcost == cur_rdc.rdcost)) {
rdc_winner = cur_rdc;
best_qindex = x->rdmult_delta_qindex + sweep_qp_delta;
}
}
return best_qindex;
}
#endif //! CONFIG_REALTIME_ONLY
/*!\brief Encode a superblock (RD-search-based)
*
* \ingroup partition_search
* Conducts partition search for a superblock, based on rate-distortion costs,
* from scratch or adjusting from a pre-calculated partition pattern.
*/
static AOM_INLINE void encode_rd_sb(AV1_COMP *cpi, ThreadData *td,
TileDataEnc *tile_data, TokenExtra **tp,
const int mi_row, const int mi_col,
const int seg_skip) {
AV1_COMMON *const cm = &cpi->common;
MACROBLOCK *const x = &td->mb;
MACROBLOCKD *const xd = &x->e_mbd;
const SPEED_FEATURES *const sf = &cpi->sf;
const TileInfo *const tile_info = &tile_data->tile_info;
MB_MODE_INFO **mi = cm->mi_params.mi_grid_base +
get_mi_grid_idx(&cm->mi_params, mi_row, mi_col);
const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
const int num_planes = av1_num_planes(cm);
int dummy_rate;
int64_t dummy_dist;
RD_STATS dummy_rdc;
SIMPLE_MOTION_DATA_TREE *const sms_root = td->sms_root;
#if CONFIG_REALTIME_ONLY
(void)seg_skip;
#endif // CONFIG_REALTIME_ONLY
init_encode_rd_sb(cpi, td, tile_data, sms_root, &dummy_rdc, mi_row, mi_col,
1);
// Encode the superblock
if (sf->part_sf.partition_search_type == VAR_BASED_PARTITION) {
// partition search starting from a variance-based partition
av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
av1_choose_var_based_partitioning(cpi, tile_info, td, x, mi_row, mi_col);
#if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, rd_use_partition_time);
#endif
td->pc_root = av1_alloc_pc_tree_node(sb_size);
if (!td->pc_root)
aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
"Failed to allocate PC_TREE");
av1_rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, sb_size,
&dummy_rate, &dummy_dist, 1, td->pc_root);
av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0,
sf->part_sf.partition_search_type);
td->pc_root = NULL;
#if CONFIG_COLLECT_COMPONENT_TIMING
end_timing(cpi, rd_use_partition_time);
#endif
}
#if !CONFIG_REALTIME_ONLY
else if (sf->part_sf.partition_search_type == FIXED_PARTITION || seg_skip) {
// partition search by adjusting a fixed-size partition
av1_set_offsets(cpi, tile_info, x, mi_row, mi_col, sb_size);
const BLOCK_SIZE bsize =
seg_skip ? sb_size : sf->part_sf.fixed_partition_size;
av1_set_fixed_partitioning(cpi, tile_info, mi, mi_row, mi_col, bsize);
td->pc_root = av1_alloc_pc_tree_node(sb_size);
if (!td->pc_root)
aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
"Failed to allocate PC_TREE");
av1_rd_use_partition(cpi, td, tile_data, mi, tp, mi_row, mi_col, sb_size,
&dummy_rate, &dummy_dist, 1, td->pc_root);
av1_free_pc_tree_recursive(td->pc_root, num_planes, 0, 0,
sf->part_sf.partition_search_type);
td->pc_root = NULL;
} else {
// The most exhaustive recursive partition search
SuperBlockEnc *sb_enc = &x->sb_enc;
// No stats for overlay frames. Exclude key frame.
av1_get_tpl_stats_sb(cpi, sb_size, mi_row, mi_col, sb_enc);
// Reset the tree for simple motion search data
av1_reset_simple_motion_tree_partition(sms_root, sb_size);
#if CONFIG_COLLECT_COMPONENT_TIMING
start_timing(cpi, rd_pick_partition_time);
#endif
// Estimate the maximum square partition block size, which will be used
// as the starting block size for partitioning the sb
set_max_min_partition_size(sb_enc, cpi, x, sf, sb_size, mi_row, mi_col);
// The superblock can be searched only once, or twice consecutively for
// better quality. Note that the meaning of passes here is different from
// the general concept of 1-pass/2-pass encoders.
const int num_passes =
cpi->oxcf.unit_test_cfg.sb_multipass_unit_test ? 2 : 1;
if (cpi->oxcf.sb_qp_sweep &&
!(has_no_stats_stage(cpi) && cpi->oxcf.mode == REALTIME &&
cpi->oxcf.gf_cfg.lag_in_frames == 0) &&
cm->delta_q_info.delta_q_present_flag) {
AOM_CHECK_MEM_ERROR(
x->e_mbd.error_info, td->mb.sb_stats_cache,
(SB_FIRST_PASS_STATS *)aom_malloc(sizeof(*td->mb.sb_stats_cache)));
av1_backup_sb_state(td->mb.sb_stats_cache, cpi, td, tile_data, mi_row,
mi_col);
assert(x->rdmult_delta_qindex == x->delta_qindex);
const int best_qp_diff =
sb_qp_sweep(cpi, td, tile_data, tp, mi_row, mi_col, sb_size, sms_root,
td->mb.sb_stats_cache) -
x->rdmult_delta_qindex;
sb_qp_sweep_init_quantizers(cpi, td, tile_data, sms_root, &dummy_rdc,
mi_row, mi_col, best_qp_diff);
const int alloc_mi_idx = get_alloc_mi_idx(&cm->mi_params, mi_row, mi_col);
const int backup_current_qindex =
cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex;
av1_reset_mbmi(&cm->mi_params, sb_size, mi_row, mi_col);
av1_restore_sb_state(td->mb.sb_stats_cache, cpi, td, tile_data, mi_row,
mi_col);
cm->mi_params.mi_alloc[alloc_mi_idx].current_qindex =
backup_current_qindex;
aom_free(td->mb.sb_stats_cache);
td->mb.sb_stats_cache = NULL;
}
if (num_passes == 1) {
#if CONFIG_PARTITION_SEARCH_ORDER
if (cpi->ext_part_controller.ready && !frame_is_intra_only(cm)) {
av1_reset_part_sf(&cpi->sf.part_sf);
av1_reset_sf_for_ext_part(cpi);
RD_STATS this_rdc;
av1_rd_partition_search(cpi, td, tile_data, tp, sms_root, mi_row,
mi_col, sb_size, &this_rdc);
} else {
td->pc_root = av1_alloc_pc_tree_node(sb_size);
if (!td->pc_root)
aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
"Failed to allocate PC_TREE");
av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
&dummy_rdc, dummy_rdc, td->pc_root, sms_root,
NULL, SB_SINGLE_PASS, NULL);
}
#else
td->pc_root = av1_alloc_pc_tree_node(sb_size);
if (!td->pc_root)
aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
"Failed to allocate PC_TREE");
av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
&dummy_rdc, dummy_rdc, td->pc_root, sms_root, NULL,
SB_SINGLE_PASS, NULL);
#endif // CONFIG_PARTITION_SEARCH_ORDER
} else {
// First pass
AOM_CHECK_MEM_ERROR(
x->e_mbd.error_info, td->mb.sb_fp_stats,
(SB_FIRST_PASS_STATS *)aom_malloc(sizeof(*td->mb.sb_fp_stats)));
av1_backup_sb_state(td->mb.sb_fp_stats, cpi, td, tile_data, mi_row,
mi_col);
td->pc_root = av1_alloc_pc_tree_node(sb_size);
if (!td->pc_root)
aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
"Failed to allocate PC_TREE");
av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
&dummy_rdc, dummy_rdc, td->pc_root, sms_root, NULL,
SB_DRY_PASS, NULL);
// Second pass
init_encode_rd_sb(cpi, td, tile_data, sms_root, &dummy_rdc, mi_row,
mi_col, 0);
av1_reset_mbmi(&cm->mi_params, sb_size, mi_row, mi_col);
av1_reset_simple_motion_tree_partition(sms_root, sb_size);
av1_restore_sb_state(td->mb.sb_fp_stats, cpi, td, tile_data, mi_row,
mi_col);
td->pc_root = av1_alloc_pc_tree_node(sb_size);
if (!td->pc_root)
aom_internal_error(xd->error_info, AOM_CODEC_MEM_ERROR,
"Failed to allocate PC_TREE");
av1_rd_pick_partition(cpi, td, tile_data, tp, mi_row, mi_col, sb_size,
&dummy_rdc, dummy_rdc, td->pc_root, sms_root, NULL,
SB_WET_PASS, NULL);
aom_free(td->mb.sb_fp_stats);
td->mb.sb_fp_stats = NULL;
}
// Reset to 0 so that it wouldn't be used elsewhere mistakenly.
sb_enc->tpl_data_count = 0;
#if CONFIG_COLLECT_COMPONENT_TIMING
end_timing(cpi, rd_pick_partition_time);
#endif
}
#endif // !CONFIG_REALTIME_ONLY
// Update the inter rd model
// TODO(angiebird): Let inter_mode_rd_model_estimation support multi-tile.
if (cpi->sf.inter_sf.inter_mode_rd_model_estimation == 1 &&
cm->tiles.cols == 1 && cm->tiles.rows == 1) {
av1_inter_mode_data_fit(tile_data, x->rdmult);
}
}
// Check if the cost update of symbols mode, coeff and dv are tile or off.
static AOM_INLINE int is_mode_coeff_dv_upd_freq_tile_or_off(
const AV1_COMP *const cpi) {
const INTER_MODE_SPEED_FEATURES *const inter_sf = &cpi->sf.inter_sf;
return (inter_sf->coeff_cost_upd_level <= INTERNAL_COST_UPD_TILE &&
inter_sf->mode_cost_upd_level <= INTERNAL_COST_UPD_TILE &&
cpi->sf.intra_sf.dv_cost_upd_level <= INTERNAL_COST_UPD_TILE);
}
// When row-mt is enabled and cost update frequencies are set to off/tile,
// processing of current SB can start even before processing of top-right SB
// is finished. This function checks if it is sufficient to wait for top SB
// to finish processing before current SB starts processing.
static AOM_INLINE int delay_wait_for_top_right_sb(const AV1_COMP *const cpi) {
const MODE mode = cpi->oxcf.mode;
if (mode == GOOD) return 0;
if (mode == ALLINTRA)
return is_mode_coeff_dv_upd_freq_tile_or_off(cpi);
else if (mode == REALTIME)
return (is_mode_coeff_dv_upd_freq_tile_or_off(cpi) &&
cpi->sf.inter_sf.mv_cost_upd_level <= INTERNAL_COST_UPD_TILE);
else
return 0;
}
/*!\brief Calculate source SAD at superblock level using 64x64 block source SAD
*
* \ingroup partition_search
* \callgraph
* \callergraph
*/
static AOM_INLINE uint64_t get_sb_source_sad(const AV1_COMP *cpi, int mi_row,
int mi_col) {
if (cpi->src_sad_blk_64x64 == NULL) return UINT64_MAX;
const AV1_COMMON *const cm = &cpi->common;