Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020, Alliance for Open Media. All rights reserved |
| 3 | * |
| 4 | * This source code is subject to the terms of the BSD 2 Clause License and |
| 5 | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 6 | * was not distributed with this source code in the LICENSE file, you can |
| 7 | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 8 | * Media Patent License 1.0 was not distributed with this source code in the |
| 9 | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 10 | */ |
| 11 | |
Jingning Han | 4aefbde | 2021-02-03 15:09:04 -0800 | [diff] [blame] | 12 | #include "aom/aomcx.h" |
| 13 | |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 14 | #include "av1/encoder/bitstream.h" |
| 15 | #include "av1/encoder/encodeframe.h" |
| 16 | #include "av1/encoder/encoder.h" |
| 17 | #include "av1/encoder/encoder_alloc.h" |
| 18 | #include "av1/encoder/encodetxb.h" |
| 19 | #include "av1/encoder/encoder_utils.h" |
| 20 | #include "av1/encoder/grain_test_vectors.h" |
| 21 | #include "av1/encoder/mv_prec.h" |
| 22 | #include "av1/encoder/rc_utils.h" |
| 23 | #include "av1/encoder/rdopt.h" |
| 24 | #include "av1/encoder/segmentation.h" |
| 25 | #include "av1/encoder/superres_scale.h" |
Jingning Han | e9187e3 | 2021-08-02 23:57:30 -0700 | [diff] [blame] | 26 | #include "av1/encoder/tpl_model.h" |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 27 | #include "av1/encoder/var_based_part.h" |
| 28 | |
Christopher Degawa | 4484d5a | 2020-08-09 00:13:27 +0000 | [diff] [blame] | 29 | #if CONFIG_TUNE_VMAF |
| 30 | #include "av1/encoder/tune_vmaf.h" |
| 31 | #endif |
| 32 | |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 33 | #define MIN_BOOST_COMBINE_FACTOR 4.0 |
| 34 | #define MAX_BOOST_COMBINE_FACTOR 12.0 |
| 35 | |
| 36 | const int default_tx_type_probs[FRAME_UPDATE_TYPES][TX_SIZES_ALL][TX_TYPES] = { |
| 37 | { { 221, 189, 214, 292, 0, 0, 0, 0, 0, 2, 38, 68, 0, 0, 0, 0 }, |
| 38 | { 262, 203, 216, 239, 0, 0, 0, 0, 0, 1, 37, 66, 0, 0, 0, 0 }, |
| 39 | { 315, 231, 239, 226, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 }, |
| 40 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 41 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 42 | { 222, 188, 214, 287, 0, 0, 0, 0, 0, 2, 50, 61, 0, 0, 0, 0 }, |
| 43 | { 256, 182, 205, 282, 0, 0, 0, 0, 0, 2, 21, 76, 0, 0, 0, 0 }, |
| 44 | { 281, 214, 217, 222, 0, 0, 0, 0, 0, 1, 48, 41, 0, 0, 0, 0 }, |
| 45 | { 263, 194, 225, 225, 0, 0, 0, 0, 0, 2, 15, 100, 0, 0, 0, 0 }, |
| 46 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 47 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 48 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 49 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 50 | { 170, 192, 242, 293, 0, 0, 0, 0, 0, 1, 68, 58, 0, 0, 0, 0 }, |
| 51 | { 199, 210, 213, 291, 0, 0, 0, 0, 0, 1, 14, 96, 0, 0, 0, 0 }, |
| 52 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 53 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 54 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 55 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, |
| 56 | { { 106, 69, 107, 278, 9, 15, 20, 45, 49, 23, 23, 88, 36, 74, 25, 57 }, |
| 57 | { 105, 72, 81, 98, 45, 49, 47, 50, 56, 72, 30, 81, 33, 95, 27, 83 }, |
| 58 | { 211, 105, 109, 120, 57, 62, 43, 49, 52, 58, 42, 116, 0, 0, 0, 0 }, |
| 59 | { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 }, |
| 60 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 61 | { 131, 57, 98, 172, 19, 40, 37, 64, 69, 22, 41, 52, 51, 77, 35, 59 }, |
| 62 | { 176, 83, 93, 202, 22, 24, 28, 47, 50, 16, 12, 93, 26, 76, 17, 59 }, |
| 63 | { 136, 72, 89, 95, 46, 59, 47, 56, 61, 68, 35, 51, 32, 82, 26, 69 }, |
| 64 | { 122, 80, 87, 105, 49, 47, 46, 46, 57, 52, 13, 90, 19, 103, 15, 93 }, |
| 65 | { 1009, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0 }, |
| 66 | { 1011, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0 }, |
| 67 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 68 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 69 | { 202, 20, 84, 114, 14, 60, 41, 79, 99, 21, 41, 15, 50, 84, 34, 66 }, |
| 70 | { 196, 44, 23, 72, 30, 22, 28, 57, 67, 13, 4, 165, 15, 148, 9, 131 }, |
| 71 | { 882, 0, 0, 0, 0, 0, 0, 0, 0, 142, 0, 0, 0, 0, 0, 0 }, |
| 72 | { 840, 0, 0, 0, 0, 0, 0, 0, 0, 184, 0, 0, 0, 0, 0, 0 }, |
| 73 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 74 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, |
| 75 | { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 76 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 77 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 78 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 79 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 80 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 81 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 82 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 83 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 84 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 85 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 86 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 87 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 88 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 89 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 90 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 91 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 92 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 93 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } }, |
| 94 | { { 213, 110, 141, 269, 12, 16, 15, 19, 21, 11, 38, 68, 22, 29, 16, 24 }, |
| 95 | { 216, 119, 128, 143, 38, 41, 26, 30, 31, 30, 42, 70, 23, 36, 19, 32 }, |
| 96 | { 367, 149, 154, 154, 38, 35, 17, 21, 21, 10, 22, 36, 0, 0, 0, 0 }, |
| 97 | { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, |
| 98 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 99 | { 219, 96, 127, 191, 21, 40, 25, 32, 34, 18, 45, 45, 33, 39, 26, 33 }, |
| 100 | { 296, 99, 122, 198, 23, 21, 19, 24, 25, 13, 20, 64, 23, 32, 18, 27 }, |
| 101 | { 275, 128, 142, 143, 35, 48, 23, 30, 29, 18, 42, 36, 18, 23, 14, 20 }, |
| 102 | { 239, 132, 166, 175, 36, 27, 19, 21, 24, 14, 13, 85, 9, 31, 8, 25 }, |
| 103 | { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, |
| 104 | { 1022, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 }, |
| 105 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 106 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 107 | { 309, 25, 79, 59, 25, 80, 34, 53, 61, 25, 49, 23, 43, 64, 36, 59 }, |
| 108 | { 270, 57, 40, 54, 50, 42, 41, 53, 56, 28, 17, 81, 45, 86, 34, 70 }, |
| 109 | { 1005, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0 }, |
| 110 | { 992, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0 }, |
| 111 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 112 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, |
| 113 | { { 133, 63, 55, 83, 57, 87, 58, 72, 68, 16, 24, 35, 29, 105, 25, 114 }, |
| 114 | { 131, 75, 74, 60, 71, 77, 65, 66, 73, 33, 21, 79, 20, 83, 18, 78 }, |
| 115 | { 276, 95, 82, 58, 86, 93, 63, 60, 64, 17, 38, 92, 0, 0, 0, 0 }, |
| 116 | { 1006, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0 }, |
| 117 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 118 | { 147, 49, 75, 78, 50, 97, 60, 67, 76, 17, 42, 35, 31, 93, 27, 80 }, |
| 119 | { 157, 49, 58, 75, 61, 52, 56, 67, 69, 12, 15, 79, 24, 119, 11, 120 }, |
| 120 | { 178, 69, 83, 77, 69, 85, 72, 77, 77, 20, 35, 40, 25, 48, 23, 46 }, |
| 121 | { 174, 55, 64, 57, 73, 68, 62, 61, 75, 15, 12, 90, 17, 99, 16, 86 }, |
| 122 | { 1008, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0 }, |
| 123 | { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 }, |
| 124 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 125 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 126 | { 266, 31, 63, 64, 21, 52, 39, 54, 63, 30, 52, 31, 48, 89, 46, 75 }, |
| 127 | { 272, 26, 32, 44, 29, 31, 32, 53, 51, 13, 13, 88, 22, 153, 16, 149 }, |
| 128 | { 923, 0, 0, 0, 0, 0, 0, 0, 0, 101, 0, 0, 0, 0, 0, 0 }, |
| 129 | { 969, 0, 0, 0, 0, 0, 0, 0, 0, 55, 0, 0, 0, 0, 0, 0 }, |
| 130 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 131 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, |
| 132 | { { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 133 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 134 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 135 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 136 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 137 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 138 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 139 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 140 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 141 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 142 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 143 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 144 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 145 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 146 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 147 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 148 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 149 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 }, |
| 150 | { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 } }, |
| 151 | { { 158, 92, 125, 298, 12, 15, 20, 29, 31, 12, 29, 67, 34, 44, 23, 35 }, |
| 152 | { 147, 94, 103, 123, 45, 48, 38, 41, 46, 48, 37, 78, 33, 63, 27, 53 }, |
| 153 | { 268, 126, 125, 136, 54, 53, 31, 38, 38, 33, 35, 87, 0, 0, 0, 0 }, |
| 154 | { 1018, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0 }, |
| 155 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 156 | { 159, 72, 103, 194, 20, 35, 37, 50, 56, 21, 39, 40, 51, 61, 38, 48 }, |
| 157 | { 259, 86, 95, 188, 32, 20, 25, 34, 37, 13, 12, 85, 25, 53, 17, 43 }, |
| 158 | { 189, 99, 113, 123, 45, 59, 37, 46, 48, 44, 39, 41, 31, 47, 26, 37 }, |
| 159 | { 175, 110, 113, 128, 58, 38, 33, 33, 43, 29, 13, 100, 14, 68, 12, 57 }, |
| 160 | { 1017, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0 }, |
| 161 | { 1019, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0 }, |
| 162 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 163 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 164 | { 208, 22, 84, 101, 21, 59, 44, 70, 90, 25, 59, 13, 64, 67, 49, 48 }, |
| 165 | { 277, 52, 32, 63, 43, 26, 33, 48, 54, 11, 6, 130, 18, 119, 11, 101 }, |
| 166 | { 963, 0, 0, 0, 0, 0, 0, 0, 0, 61, 0, 0, 0, 0, 0, 0 }, |
| 167 | { 979, 0, 0, 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0 }, |
| 168 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 169 | { 1024, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } |
| 170 | }; |
| 171 | |
| 172 | const int default_obmc_probs[FRAME_UPDATE_TYPES][BLOCK_SIZES_ALL] = { |
| 173 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 174 | { 0, 0, 0, 106, 90, 90, 97, 67, 59, 70, 28, |
| 175 | 30, 38, 16, 16, 16, 0, 0, 44, 50, 26, 25 }, |
| 176 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 177 | { 0, 0, 0, 98, 93, 97, 68, 82, 85, 33, 30, |
| 178 | 33, 16, 16, 16, 16, 0, 0, 43, 37, 26, 16 }, |
| 179 | { 0, 0, 0, 91, 80, 76, 78, 55, 49, 24, 16, |
| 180 | 16, 16, 16, 16, 16, 0, 0, 29, 45, 16, 38 }, |
| 181 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 182 | { 0, 0, 0, 103, 89, 89, 89, 62, 63, 76, 34, |
| 183 | 35, 32, 19, 16, 16, 0, 0, 49, 55, 29, 19 } |
| 184 | }; |
| 185 | |
| 186 | const int default_warped_probs[FRAME_UPDATE_TYPES] = { 64, 64, 64, 64, |
| 187 | 64, 64, 64 }; |
| 188 | |
| 189 | // TODO(yunqing): the default probs can be trained later from better |
| 190 | // performance. |
| 191 | const int default_switchable_interp_probs[FRAME_UPDATE_TYPES] |
| 192 | [SWITCHABLE_FILTER_CONTEXTS] |
| 193 | [SWITCHABLE_FILTERS] = { |
| 194 | { { 512, 512, 512 }, |
| 195 | { 512, 512, 512 }, |
| 196 | { 512, 512, 512 }, |
| 197 | { 512, 512, 512 }, |
| 198 | { 512, 512, 512 }, |
| 199 | { 512, 512, 512 }, |
| 200 | { 512, 512, 512 }, |
| 201 | { 512, 512, 512 }, |
| 202 | { 512, 512, 512 }, |
| 203 | { 512, 512, 512 }, |
| 204 | { 512, 512, 512 }, |
| 205 | { 512, 512, 512 }, |
| 206 | { 512, 512, 512 }, |
| 207 | { 512, 512, 512 }, |
| 208 | { 512, 512, 512 }, |
| 209 | { 512, 512, 512 } }, |
| 210 | { { 512, 512, 512 }, |
| 211 | { 512, 512, 512 }, |
| 212 | { 512, 512, 512 }, |
| 213 | { 512, 512, 512 }, |
| 214 | { 512, 512, 512 }, |
| 215 | { 512, 512, 512 }, |
| 216 | { 512, 512, 512 }, |
| 217 | { 512, 512, 512 }, |
| 218 | { 512, 512, 512 }, |
| 219 | { 512, 512, 512 }, |
| 220 | { 512, 512, 512 }, |
| 221 | { 512, 512, 512 }, |
| 222 | { 512, 512, 512 }, |
| 223 | { 512, 512, 512 }, |
| 224 | { 512, 512, 512 }, |
| 225 | { 512, 512, 512 } }, |
| 226 | { { 512, 512, 512 }, |
| 227 | { 512, 512, 512 }, |
| 228 | { 512, 512, 512 }, |
| 229 | { 512, 512, 512 }, |
| 230 | { 512, 512, 512 }, |
| 231 | { 512, 512, 512 }, |
| 232 | { 512, 512, 512 }, |
| 233 | { 512, 512, 512 }, |
| 234 | { 512, 512, 512 }, |
| 235 | { 512, 512, 512 }, |
| 236 | { 512, 512, 512 }, |
| 237 | { 512, 512, 512 }, |
| 238 | { 512, 512, 512 }, |
| 239 | { 512, 512, 512 }, |
| 240 | { 512, 512, 512 }, |
| 241 | { 512, 512, 512 } }, |
| 242 | { { 512, 512, 512 }, |
| 243 | { 512, 512, 512 }, |
| 244 | { 512, 512, 512 }, |
| 245 | { 512, 512, 512 }, |
| 246 | { 512, 512, 512 }, |
| 247 | { 512, 512, 512 }, |
| 248 | { 512, 512, 512 }, |
| 249 | { 512, 512, 512 }, |
| 250 | { 512, 512, 512 }, |
| 251 | { 512, 512, 512 }, |
| 252 | { 512, 512, 512 }, |
| 253 | { 512, 512, 512 }, |
| 254 | { 512, 512, 512 }, |
| 255 | { 512, 512, 512 }, |
| 256 | { 512, 512, 512 }, |
| 257 | { 512, 512, 512 } }, |
| 258 | { { 512, 512, 512 }, |
| 259 | { 512, 512, 512 }, |
| 260 | { 512, 512, 512 }, |
| 261 | { 512, 512, 512 }, |
| 262 | { 512, 512, 512 }, |
| 263 | { 512, 512, 512 }, |
| 264 | { 512, 512, 512 }, |
| 265 | { 512, 512, 512 }, |
| 266 | { 512, 512, 512 }, |
| 267 | { 512, 512, 512 }, |
| 268 | { 512, 512, 512 }, |
| 269 | { 512, 512, 512 }, |
| 270 | { 512, 512, 512 }, |
| 271 | { 512, 512, 512 }, |
| 272 | { 512, 512, 512 }, |
| 273 | { 512, 512, 512 } }, |
| 274 | { { 512, 512, 512 }, |
| 275 | { 512, 512, 512 }, |
| 276 | { 512, 512, 512 }, |
| 277 | { 512, 512, 512 }, |
| 278 | { 512, 512, 512 }, |
| 279 | { 512, 512, 512 }, |
| 280 | { 512, 512, 512 }, |
| 281 | { 512, 512, 512 }, |
| 282 | { 512, 512, 512 }, |
| 283 | { 512, 512, 512 }, |
| 284 | { 512, 512, 512 }, |
| 285 | { 512, 512, 512 }, |
| 286 | { 512, 512, 512 }, |
| 287 | { 512, 512, 512 }, |
| 288 | { 512, 512, 512 }, |
| 289 | { 512, 512, 512 } }, |
| 290 | { { 512, 512, 512 }, |
| 291 | { 512, 512, 512 }, |
| 292 | { 512, 512, 512 }, |
| 293 | { 512, 512, 512 }, |
| 294 | { 512, 512, 512 }, |
| 295 | { 512, 512, 512 }, |
| 296 | { 512, 512, 512 }, |
| 297 | { 512, 512, 512 }, |
| 298 | { 512, 512, 512 }, |
| 299 | { 512, 512, 512 }, |
| 300 | { 512, 512, 512 }, |
| 301 | { 512, 512, 512 }, |
| 302 | { 512, 512, 512 }, |
| 303 | { 512, 512, 512 }, |
| 304 | { 512, 512, 512 }, |
| 305 | { 512, 512, 512 } } |
| 306 | }; |
| 307 | |
| 308 | static void configure_static_seg_features(AV1_COMP *cpi) { |
| 309 | AV1_COMMON *const cm = &cpi->common; |
| 310 | const RATE_CONTROL *const rc = &cpi->rc; |
| 311 | struct segmentation *const seg = &cm->seg; |
| 312 | |
Remya Prakasan | 6566bc8 | 2021-11-05 23:21:12 +0530 | [diff] [blame] | 313 | double avg_q; |
Remya Prakasan | ffeb497 | 2022-06-21 20:00:28 +0530 | [diff] [blame] | 314 | #if CONFIG_FPMT_TEST |
Remya Prakasan | 6566bc8 | 2021-11-05 23:21:12 +0530 | [diff] [blame] | 315 | avg_q = ((cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) && |
| 316 | (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE)) |
| 317 | ? cpi->ppi->p_rc.temp_avg_q |
| 318 | : cpi->ppi->p_rc.avg_q; |
| 319 | #else |
| 320 | avg_q = cpi->ppi->p_rc.avg_q; |
| 321 | #endif |
| 322 | |
Aasaipriya | cdfcf17 | 2021-06-05 16:40:57 +0530 | [diff] [blame] | 323 | int high_q = (int)(avg_q > 48.0); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 324 | int qi_delta; |
| 325 | |
| 326 | // Disable and clear down for KF |
| 327 | if (cm->current_frame.frame_type == KEY_FRAME) { |
| 328 | // Clear down the global segmentation map |
| 329 | memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols); |
| 330 | seg->update_map = 0; |
| 331 | seg->update_data = 0; |
| 332 | |
| 333 | // Disable segmentation |
| 334 | av1_disable_segmentation(seg); |
| 335 | |
| 336 | // Clear down the segment features. |
| 337 | av1_clearall_segfeatures(seg); |
| 338 | } else if (cpi->refresh_frame.alt_ref_frame) { |
| 339 | // If this is an alt ref frame |
| 340 | // Clear down the global segmentation map |
| 341 | memset(cpi->enc_seg.map, 0, cm->mi_params.mi_rows * cm->mi_params.mi_cols); |
| 342 | seg->update_map = 0; |
| 343 | seg->update_data = 0; |
| 344 | |
| 345 | // Disable segmentation and individual segment features by default |
| 346 | av1_disable_segmentation(seg); |
| 347 | av1_clearall_segfeatures(seg); |
| 348 | |
| 349 | // If segmentation was enabled set those features needed for the |
| 350 | // arf itself. |
| 351 | if (seg->enabled) { |
| 352 | seg->update_map = 1; |
| 353 | seg->update_data = 1; |
| 354 | |
Aasaipriya | cdfcf17 | 2021-06-05 16:40:57 +0530 | [diff] [blame] | 355 | qi_delta = av1_compute_qdelta(rc, avg_q, avg_q * 0.875, |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 356 | cm->seq_params->bit_depth); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 357 | av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2); |
| 358 | av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2); |
| 359 | av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2); |
| 360 | av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2); |
| 361 | av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2); |
| 362 | |
| 363 | av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H); |
| 364 | av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V); |
| 365 | av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U); |
| 366 | av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V); |
| 367 | |
| 368 | av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q); |
| 369 | } |
| 370 | } else if (seg->enabled) { |
| 371 | // All other frames if segmentation has been enabled |
| 372 | |
| 373 | // First normal frame in a valid gf or alt ref group |
| 374 | if (rc->frames_since_golden == 0) { |
| 375 | // Set up segment features for normal frames in an arf group |
Jingning Han | 98bcaef | 2020-09-27 14:57:39 -0700 | [diff] [blame] | 376 | // Disable segmentation and clear down features if alt ref |
| 377 | // is not active for this group |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 378 | |
Jingning Han | 98bcaef | 2020-09-27 14:57:39 -0700 | [diff] [blame] | 379 | av1_disable_segmentation(seg); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 380 | |
Jingning Han | 98bcaef | 2020-09-27 14:57:39 -0700 | [diff] [blame] | 381 | memset(cpi->enc_seg.map, 0, |
| 382 | cm->mi_params.mi_rows * cm->mi_params.mi_cols); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 383 | |
Jingning Han | 98bcaef | 2020-09-27 14:57:39 -0700 | [diff] [blame] | 384 | seg->update_map = 0; |
| 385 | seg->update_data = 0; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 386 | |
Jingning Han | 98bcaef | 2020-09-27 14:57:39 -0700 | [diff] [blame] | 387 | av1_clearall_segfeatures(seg); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 388 | } else if (rc->is_src_frame_alt_ref) { |
| 389 | // Special case where we are coding over the top of a previous |
| 390 | // alt ref frame. |
| 391 | // Segment coding disabled for compred testing |
| 392 | |
| 393 | // Enable ref frame features for segment 0 as well |
| 394 | av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME); |
| 395 | av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME); |
| 396 | |
| 397 | // All mbs should use ALTREF_FRAME |
| 398 | av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME); |
| 399 | av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME); |
| 400 | av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME); |
| 401 | av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME); |
| 402 | |
| 403 | // Skip all MBs if high Q (0,0 mv and skip coeffs) |
| 404 | if (high_q) { |
| 405 | av1_enable_segfeature(seg, 0, SEG_LVL_SKIP); |
| 406 | av1_enable_segfeature(seg, 1, SEG_LVL_SKIP); |
| 407 | } |
| 408 | // Enable data update |
| 409 | seg->update_data = 1; |
| 410 | } else { |
| 411 | // All other frames. |
| 412 | |
| 413 | // No updates.. leave things as they are. |
| 414 | seg->update_map = 0; |
| 415 | seg->update_data = 0; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void av1_apply_active_map(AV1_COMP *cpi) { |
| 421 | struct segmentation *const seg = &cpi->common.seg; |
| 422 | unsigned char *const seg_map = cpi->enc_seg.map; |
| 423 | const unsigned char *const active_map = cpi->active_map.map; |
| 424 | int i; |
| 425 | |
| 426 | assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE); |
| 427 | |
| 428 | if (frame_is_intra_only(&cpi->common)) { |
| 429 | cpi->active_map.enabled = 0; |
| 430 | cpi->active_map.update = 1; |
| 431 | } |
| 432 | |
| 433 | if (cpi->active_map.update) { |
| 434 | if (cpi->active_map.enabled) { |
chiyotsai | ae4339f | 2022-11-16 10:44:11 -0800 | [diff] [blame] | 435 | const int num_mis = |
| 436 | cpi->common.mi_params.mi_rows * cpi->common.mi_params.mi_cols; |
| 437 | for (i = 0; i < num_mis; ++i) |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 438 | if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i]; |
| 439 | av1_enable_segmentation(seg); |
| 440 | av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP); |
| 441 | av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H); |
| 442 | av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V); |
| 443 | av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U); |
| 444 | av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V); |
| 445 | |
| 446 | av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H, |
| 447 | -MAX_LOOP_FILTER); |
| 448 | av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V, |
| 449 | -MAX_LOOP_FILTER); |
| 450 | av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U, |
| 451 | -MAX_LOOP_FILTER); |
| 452 | av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V, |
| 453 | -MAX_LOOP_FILTER); |
| 454 | } else { |
| 455 | av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP); |
| 456 | av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H); |
| 457 | av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V); |
| 458 | av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U); |
| 459 | av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V); |
| 460 | if (seg->enabled) { |
| 461 | seg->update_data = 1; |
| 462 | seg->update_map = 1; |
| 463 | } |
| 464 | } |
| 465 | cpi->active_map.update = 0; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | #if !CONFIG_REALTIME_ONLY |
| 470 | static void process_tpl_stats_frame(AV1_COMP *cpi) { |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 471 | const GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 472 | AV1_COMMON *const cm = &cpi->common; |
| 473 | |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 474 | assert(IMPLIES(gf_group->size > 0, cpi->gf_frame_index < gf_group->size)); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 475 | |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 476 | const int tpl_idx = cpi->gf_frame_index; |
Mufaddal Chakera | 7260d14 | 2021-04-12 01:03:40 +0530 | [diff] [blame] | 477 | TplParams *const tpl_data = &cpi->ppi->tpl_data; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 478 | TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx]; |
| 479 | TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr; |
| 480 | |
| 481 | if (tpl_frame->is_valid) { |
| 482 | int tpl_stride = tpl_frame->stride; |
Jingning Han | 4a47940 | 2021-12-01 15:16:06 -0800 | [diff] [blame] | 483 | double intra_cost_base = 0; |
| 484 | double mc_dep_cost_base = 0; |
| 485 | double cbcmp_base = 1; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 486 | const int step = 1 << tpl_data->tpl_stats_block_mis_log2; |
Urvang Joshi | 68af528 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 487 | const int row_step = step; |
| 488 | const int col_step_sr = |
| 489 | coded_to_superres_mi(step, cm->superres_scale_denominator); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 490 | const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width); |
| 491 | |
Urvang Joshi | 68af528 | 2020-10-08 15:37:55 -0700 | [diff] [blame] | 492 | for (int row = 0; row < cm->mi_params.mi_rows; row += row_step) { |
| 493 | for (int col = 0; col < mi_cols_sr; col += col_step_sr) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 494 | TplDepStats *this_stats = &tpl_stats[av1_tpl_ptr_pos( |
| 495 | row, col, tpl_stride, tpl_data->tpl_stats_block_mis_log2)]; |
Jingning Han | fe2ec60 | 2021-12-31 07:26:21 -0800 | [diff] [blame] | 496 | double cbcmp = (double)(this_stats->srcrf_dist); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 497 | int64_t mc_dep_delta = |
| 498 | RDCOST(tpl_frame->base_rdmult, this_stats->mc_dep_rate, |
| 499 | this_stats->mc_dep_dist); |
Yaowu Xu | f436027 | 2021-12-30 09:14:28 -0800 | [diff] [blame] | 500 | double dist_scaled = (double)(this_stats->recrf_dist << RDDIV_BITS); |
| 501 | intra_cost_base += log(dist_scaled) * cbcmp; |
| 502 | mc_dep_cost_base += log(dist_scaled + mc_dep_delta) * cbcmp; |
Jingning Han | 4a47940 | 2021-12-01 15:16:06 -0800 | [diff] [blame] | 503 | cbcmp_base += cbcmp; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | if (mc_dep_cost_base == 0) { |
| 508 | tpl_frame->is_valid = 0; |
| 509 | } else { |
Jingning Han | 4a47940 | 2021-12-01 15:16:06 -0800 | [diff] [blame] | 510 | cpi->rd.r0 = exp((intra_cost_base - mc_dep_cost_base) / cbcmp_base); |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 511 | if (is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) { |
Tarundeep Singh | 5e5305a | 2021-03-16 13:04:04 +0530 | [diff] [blame] | 512 | if (cpi->ppi->lap_enabled) { |
Mufaddal Chakera | 94ee9bf | 2021-04-12 01:02:22 +0530 | [diff] [blame] | 513 | double min_boost_factor = sqrt(cpi->ppi->p_rc.baseline_gf_interval); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 514 | const int gfu_boost = get_gfu_boost_from_r0_lap( |
Jingning Han | 94b2854 | 2020-08-03 23:11:27 -0700 | [diff] [blame] | 515 | min_boost_factor, MAX_GFUBOOST_FACTOR, cpi->rd.r0, |
Mufaddal Chakera | 94ee9bf | 2021-04-12 01:02:22 +0530 | [diff] [blame] | 516 | cpi->ppi->p_rc.num_stats_required_for_gfu_boost); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 517 | // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost, |
| 518 | // gfu_boost); |
Mufaddal Chakera | 94ee9bf | 2021-04-12 01:02:22 +0530 | [diff] [blame] | 519 | cpi->ppi->p_rc.gfu_boost = combine_prior_with_tpl_boost( |
| 520 | min_boost_factor, MAX_BOOST_COMBINE_FACTOR, |
| 521 | cpi->ppi->p_rc.gfu_boost, gfu_boost, |
| 522 | cpi->ppi->p_rc.num_stats_used_for_gfu_boost); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 523 | } else { |
Deepa K G | ccca9fe | 2023-08-03 00:55:00 +0530 | [diff] [blame] | 524 | // TPL may only look at a subset of frame in the gf group when the |
| 525 | // speed feature 'reduce_num_frames' is on, which affects the r0 |
| 526 | // calcuation. Thus, to compensate for TPL not using all frames a |
| 527 | // factor to adjust r0 is used. |
| 528 | const int gfu_boost = |
| 529 | (int)(200.0 * cpi->ppi->tpl_data.r0_adjust_factor / cpi->rd.r0); |
Mufaddal Chakera | 94ee9bf | 2021-04-12 01:02:22 +0530 | [diff] [blame] | 530 | cpi->ppi->p_rc.gfu_boost = combine_prior_with_tpl_boost( |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 531 | MIN_BOOST_COMBINE_FACTOR, MAX_BOOST_COMBINE_FACTOR, |
Mufaddal Chakera | 94ee9bf | 2021-04-12 01:02:22 +0530 | [diff] [blame] | 532 | cpi->ppi->p_rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 533 | } |
| 534 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | } |
| 538 | #endif // !CONFIG_REALTIME_ONLY |
| 539 | |
| 540 | void av1_set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index, |
| 541 | int *top_index) { |
| 542 | AV1_COMMON *const cm = &cpi->common; |
| 543 | |
| 544 | // Setup variables that depend on the dimensions of the frame. |
| 545 | av1_set_speed_features_framesize_dependent(cpi, cpi->speed); |
| 546 | |
| 547 | #if !CONFIG_REALTIME_ONLY |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 548 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
James Zern | ce095f1 | 2022-04-08 15:55:52 -0700 | [diff] [blame] | 549 | if (cpi->oxcf.algo_cfg.enable_tpl_model && |
Jingning Han | 6850cc3 | 2021-12-25 00:52:32 -0800 | [diff] [blame] | 550 | av1_tpl_stats_ready(&cpi->ppi->tpl_data, cpi->gf_frame_index)) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 551 | process_tpl_stats_frame(cpi); |
| 552 | av1_tpl_rdmult_setup(cpi); |
| 553 | } |
| 554 | #endif |
| 555 | |
| 556 | // Decide q and q bounds. |
Mufaddal Chakera | 94ee9bf | 2021-04-12 01:02:22 +0530 | [diff] [blame] | 557 | *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, cpi->gf_frame_index, |
| 558 | bottom_index, top_index); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 559 | |
Jingning Han | 1657934 | 2021-05-12 00:02:01 -0700 | [diff] [blame] | 560 | #if !CONFIG_REALTIME_ONLY |
| 561 | if (cpi->oxcf.rc_cfg.mode == AOM_Q && |
James Zern | ce095f1 | 2022-04-08 15:55:52 -0700 | [diff] [blame] | 562 | cpi->ppi->tpl_data.tpl_frame[cpi->gf_frame_index].is_valid && |
Angie Chiang | b93d5b7 | 2021-10-06 12:17:09 -0700 | [diff] [blame] | 563 | !is_lossless_requested(&cpi->oxcf.rc_cfg)) { |
Angie Chiang | 6dabc84 | 2021-11-02 15:34:15 -0700 | [diff] [blame] | 564 | const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg; |
| 565 | const int tpl_q = av1_tpl_get_q_index( |
| 566 | &cpi->ppi->tpl_data, cpi->gf_frame_index, cpi->rc.active_worst_quality, |
| 567 | cm->seq_params->bit_depth); |
| 568 | *q = clamp(tpl_q, rc_cfg->best_allowed_q, rc_cfg->worst_allowed_q); |
Jingning Han | 1657934 | 2021-05-12 00:02:01 -0700 | [diff] [blame] | 569 | *top_index = *bottom_index = *q; |
| 570 | if (gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE) |
| 571 | cpi->ppi->p_rc.arf_q = *q; |
| 572 | } |
Jingning Han | e9187e3 | 2021-08-02 23:57:30 -0700 | [diff] [blame] | 573 | |
Jingning Han | 958fcc5 | 2021-09-01 00:45:11 -0700 | [diff] [blame] | 574 | if (cpi->oxcf.q_cfg.use_fixed_qp_offsets && cpi->oxcf.rc_cfg.mode == AOM_Q) { |
| 575 | if (is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) { |
Jingning Han | a605486 | 2021-09-08 15:53:44 -0700 | [diff] [blame] | 576 | const double qratio_grad = |
| 577 | cpi->ppi->p_rc.baseline_gf_interval > 20 ? 0.2 : 0.3; |
Jingning Han | 958fcc5 | 2021-09-01 00:45:11 -0700 | [diff] [blame] | 578 | const double qstep_ratio = |
Jingning Han | a605486 | 2021-09-08 15:53:44 -0700 | [diff] [blame] | 579 | 0.2 + |
| 580 | (1.0 - (double)cpi->rc.active_worst_quality / MAXQ) * qratio_grad; |
Jingning Han | 958fcc5 | 2021-09-01 00:45:11 -0700 | [diff] [blame] | 581 | *q = av1_get_q_index_from_qstep_ratio( |
| 582 | cpi->rc.active_worst_quality, qstep_ratio, cm->seq_params->bit_depth); |
| 583 | *top_index = *bottom_index = *q; |
| 584 | if (gf_group->update_type[cpi->gf_frame_index] == ARF_UPDATE || |
| 585 | gf_group->update_type[cpi->gf_frame_index] == KF_UPDATE || |
| 586 | gf_group->update_type[cpi->gf_frame_index] == GF_UPDATE) |
| 587 | cpi->ppi->p_rc.arf_q = *q; |
Jingning Han | af3cd5b | 2021-09-09 13:21:21 -0700 | [diff] [blame] | 588 | } else if (gf_group->layer_depth[cpi->gf_frame_index] < |
| 589 | gf_group->max_layer_depth) { |
Jingning Han | 958fcc5 | 2021-09-01 00:45:11 -0700 | [diff] [blame] | 590 | int this_height = gf_group->layer_depth[cpi->gf_frame_index]; |
| 591 | int arf_q = cpi->ppi->p_rc.arf_q; |
| 592 | while (this_height > 1) { |
| 593 | arf_q = (arf_q + cpi->oxcf.rc_cfg.cq_level + 1) / 2; |
| 594 | --this_height; |
| 595 | } |
| 596 | *top_index = *bottom_index = *q = arf_q; |
| 597 | } |
Jingning Han | e9187e3 | 2021-08-02 23:57:30 -0700 | [diff] [blame] | 598 | } |
Jingning Han | 1657934 | 2021-05-12 00:02:01 -0700 | [diff] [blame] | 599 | #endif |
| 600 | |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 601 | // Configure experimental use of segmentation for enhanced coding of |
| 602 | // static regions if indicated. |
| 603 | // Only allowed in the second pass of a two pass encode, as it requires |
| 604 | // lagged coding, and if the relevant speed feature flag is set. |
| 605 | if (is_stat_consumption_stage_twopass(cpi) && |
| 606 | cpi->sf.hl_sf.static_segmentation) |
| 607 | configure_static_seg_features(cpi); |
| 608 | } |
| 609 | |
| 610 | static void reset_film_grain_chroma_params(aom_film_grain_t *pars) { |
| 611 | pars->num_cr_points = 0; |
| 612 | pars->cr_mult = 0; |
| 613 | pars->cr_luma_mult = 0; |
| 614 | memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr)); |
| 615 | memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr)); |
| 616 | pars->num_cb_points = 0; |
| 617 | pars->cb_mult = 0; |
| 618 | pars->cb_luma_mult = 0; |
| 619 | pars->chroma_scaling_from_luma = 0; |
| 620 | memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb)); |
| 621 | memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb)); |
| 622 | } |
| 623 | |
Tarundeep Singh | 9e77b30 | 2021-03-19 16:07:48 +0530 | [diff] [blame] | 624 | void av1_update_film_grain_parameters_seq(struct AV1_PRIMARY *ppi, |
| 625 | const AV1EncoderConfig *oxcf) { |
| 626 | SequenceHeader *const seq_params = &ppi->seq_params; |
| 627 | const TuneCfg *const tune_cfg = &oxcf->tune_cfg; |
| 628 | |
| 629 | if (tune_cfg->film_grain_test_vector || tune_cfg->film_grain_table_filename || |
| 630 | tune_cfg->content == AOM_CONTENT_FILM) { |
| 631 | seq_params->film_grain_params_present = 1; |
| 632 | } else { |
| 633 | #if CONFIG_DENOISE |
| 634 | seq_params->film_grain_params_present = (oxcf->noise_level > 0); |
| 635 | #else |
| 636 | seq_params->film_grain_params_present = 0; |
| 637 | #endif |
| 638 | } |
| 639 | } |
| 640 | |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 641 | void av1_update_film_grain_parameters(struct AV1_COMP *cpi, |
| 642 | const AV1EncoderConfig *oxcf) { |
| 643 | AV1_COMMON *const cm = &cpi->common; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 644 | const TuneCfg *const tune_cfg = &oxcf->tune_cfg; |
| 645 | |
| 646 | if (cpi->film_grain_table) { |
| 647 | aom_film_grain_table_free(cpi->film_grain_table); |
| 648 | aom_free(cpi->film_grain_table); |
| 649 | cpi->film_grain_table = NULL; |
| 650 | } |
| 651 | |
| 652 | if (tune_cfg->film_grain_test_vector) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 653 | if (cm->current_frame.frame_type == KEY_FRAME) { |
| 654 | memcpy(&cm->film_grain_params, |
| 655 | film_grain_test_vectors + tune_cfg->film_grain_test_vector - 1, |
| 656 | sizeof(cm->film_grain_params)); |
| 657 | if (oxcf->tool_cfg.enable_monochrome) |
| 658 | reset_film_grain_chroma_params(&cm->film_grain_params); |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 659 | cm->film_grain_params.bit_depth = cm->seq_params->bit_depth; |
| 660 | if (cm->seq_params->color_range == AOM_CR_FULL_RANGE) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 661 | cm->film_grain_params.clip_to_restricted_range = 0; |
| 662 | } |
| 663 | } |
| 664 | } else if (tune_cfg->film_grain_table_filename) { |
James Zern | 8ff2856 | 2022-04-28 12:54:06 -0700 | [diff] [blame] | 665 | CHECK_MEM_ERROR(cm, cpi->film_grain_table, |
| 666 | aom_calloc(1, sizeof(*cpi->film_grain_table))); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 667 | |
| 668 | aom_film_grain_table_read(cpi->film_grain_table, |
Tarundeep Singh | cfa5fb4 | 2021-04-23 18:25:32 +0530 | [diff] [blame] | 669 | tune_cfg->film_grain_table_filename, cm->error); |
Jingning Han | 4aefbde | 2021-02-03 15:09:04 -0800 | [diff] [blame] | 670 | } else if (tune_cfg->content == AOM_CONTENT_FILM) { |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 671 | cm->film_grain_params.bit_depth = cm->seq_params->bit_depth; |
Jingning Han | 4aefbde | 2021-02-03 15:09:04 -0800 | [diff] [blame] | 672 | if (oxcf->tool_cfg.enable_monochrome) |
| 673 | reset_film_grain_chroma_params(&cm->film_grain_params); |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 674 | if (cm->seq_params->color_range == AOM_CR_FULL_RANGE) |
Jingning Han | 4aefbde | 2021-02-03 15:09:04 -0800 | [diff] [blame] | 675 | cm->film_grain_params.clip_to_restricted_range = 0; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 676 | } else { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 677 | memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params)); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | void av1_scale_references(AV1_COMP *cpi, const InterpFilter filter, |
| 682 | const int phase, const int use_optimized_scaler) { |
| 683 | AV1_COMMON *cm = &cpi->common; |
| 684 | const int num_planes = av1_num_planes(cm); |
| 685 | MV_REFERENCE_FRAME ref_frame; |
| 686 | |
| 687 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 688 | // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1). |
| 689 | if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) { |
| 690 | BufferPool *const pool = cm->buffer_pool; |
| 691 | const YV12_BUFFER_CONFIG *const ref = |
| 692 | get_ref_frame_yv12_buf(cm, ref_frame); |
| 693 | |
| 694 | if (ref == NULL) { |
| 695 | cpi->scaled_ref_buf[ref_frame - 1] = NULL; |
| 696 | continue; |
| 697 | } |
| 698 | |
Marco Paniconi | a7a3dce | 2023-07-26 15:36:30 -0700 | [diff] [blame] | 699 | // For RTC-SVC: if force_zero_mode_spatial_ref is enabled, check if the |
| 700 | // motion search can be skipped for the references: last, golden, altref. |
| 701 | // If so, we can skip scaling that reference. |
| 702 | if (cpi->ppi->use_svc && cpi->svc.force_zero_mode_spatial_ref && |
| 703 | cpi->ppi->rtc_ref.set_ref_frame_config) { |
| 704 | if (ref_frame == LAST_FRAME && cpi->svc.skip_mvsearch_last) continue; |
| 705 | if (ref_frame == GOLDEN_FRAME && cpi->svc.skip_mvsearch_gf) continue; |
| 706 | if (ref_frame == ALTREF_FRAME && cpi->svc.skip_mvsearch_altref) |
| 707 | continue; |
| 708 | } |
Marco Paniconi | 776fdd8 | 2023-11-06 15:47:04 -0800 | [diff] [blame^] | 709 | // For RTC with superres on: golden reference only needs to be scaled |
| 710 | // if it was refreshed in previous frame. |
| 711 | if (is_one_pass_rt_params(cpi) && |
| 712 | cpi->oxcf.superres_cfg.enable_superres && ref_frame == GOLDEN_FRAME && |
| 713 | cpi->rc.frame_num_last_gf_refresh < |
| 714 | (int)cm->current_frame.frame_number - 1) { |
| 715 | continue; |
| 716 | } |
Marco Paniconi | a7a3dce | 2023-07-26 15:36:30 -0700 | [diff] [blame] | 717 | |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 718 | if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) { |
| 719 | // Replace the reference buffer with a copy having a thicker border, |
| 720 | // if the reference buffer is higher resolution than the current |
| 721 | // frame, and the border is thin. |
| 722 | if ((ref->y_crop_width > cm->width || |
| 723 | ref->y_crop_height > cm->height) && |
| 724 | ref->border < AOM_BORDER_IN_PIXELS) { |
| 725 | RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame); |
| 726 | if (aom_yv12_realloc_with_new_border( |
| 727 | &ref_fb->buf, AOM_BORDER_IN_PIXELS, |
Rachel Barker | 674eaa0 | 2022-12-22 16:03:44 +0000 | [diff] [blame] | 728 | cm->features.byte_alignment, cpi->image_pyramid_levels, |
| 729 | num_planes) != 0) { |
Tarundeep Singh | cfa5fb4 | 2021-04-23 18:25:32 +0530 | [diff] [blame] | 730 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 731 | "Failed to allocate frame buffer"); |
| 732 | } |
| 733 | } |
| 734 | int force_scaling = 0; |
| 735 | RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1]; |
| 736 | if (new_fb == NULL) { |
| 737 | const int new_fb_idx = get_free_fb(cm); |
| 738 | if (new_fb_idx == INVALID_IDX) { |
Tarundeep Singh | cfa5fb4 | 2021-04-23 18:25:32 +0530 | [diff] [blame] | 739 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 740 | "Unable to find free frame buffer"); |
| 741 | } |
| 742 | force_scaling = 1; |
| 743 | new_fb = &pool->frame_bufs[new_fb_idx]; |
| 744 | } |
| 745 | |
| 746 | if (force_scaling || new_fb->buf.y_crop_width != cm->width || |
| 747 | new_fb->buf.y_crop_height != cm->height) { |
| 748 | if (aom_realloc_frame_buffer( |
| 749 | &new_fb->buf, cm->width, cm->height, |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 750 | cm->seq_params->subsampling_x, cm->seq_params->subsampling_y, |
| 751 | cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS, |
Cherma Rajan A | d3b1571 | 2022-04-11 17:02:19 +0530 | [diff] [blame] | 752 | cm->features.byte_alignment, NULL, NULL, NULL, 0, 0)) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 753 | if (force_scaling) { |
| 754 | // Release the reference acquired in the get_free_fb() call above. |
| 755 | --new_fb->ref_count; |
| 756 | } |
Tarundeep Singh | cfa5fb4 | 2021-04-23 18:25:32 +0530 | [diff] [blame] | 757 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 758 | "Failed to allocate frame buffer"); |
| 759 | } |
Yuan Tong | e1399f3 | 2022-11-15 19:47:27 +0800 | [diff] [blame] | 760 | bool has_optimized_scaler = av1_has_optimized_scaler( |
Wan-Teh Chang | cc7a0cc | 2022-11-23 16:01:26 -0800 | [diff] [blame] | 761 | ref->y_crop_width, ref->y_crop_height, new_fb->buf.y_crop_width, |
Jerome Jiang | ff7b753 | 2022-08-30 14:45:28 -0400 | [diff] [blame] | 762 | new_fb->buf.y_crop_height); |
Yuan Tong | e1399f3 | 2022-11-15 19:47:27 +0800 | [diff] [blame] | 763 | if (num_planes > 1) { |
| 764 | has_optimized_scaler = |
| 765 | has_optimized_scaler && |
| 766 | av1_has_optimized_scaler( |
| 767 | ref->uv_crop_width, ref->uv_crop_height, |
| 768 | new_fb->buf.uv_crop_width, new_fb->buf.uv_crop_height); |
| 769 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 770 | #if CONFIG_AV1_HIGHBITDEPTH |
Jerome Jiang | ff7b753 | 2022-08-30 14:45:28 -0400 | [diff] [blame] | 771 | if (use_optimized_scaler && has_optimized_scaler && |
Satheesh Kumar | 3176890 | 2023-08-30 10:16:10 +0530 | [diff] [blame] | 772 | cm->seq_params->bit_depth == AOM_BITS_8) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 773 | av1_resize_and_extend_frame(ref, &new_fb->buf, filter, phase, |
| 774 | num_planes); |
Satheesh Kumar | 3176890 | 2023-08-30 10:16:10 +0530 | [diff] [blame] | 775 | } else if (!av1_resize_and_extend_frame_nonnormative( |
| 776 | ref, &new_fb->buf, (int)cm->seq_params->bit_depth, |
| 777 | num_planes)) { |
| 778 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
| 779 | "Failed to allocate buffer during resize"); |
| 780 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 781 | #else |
Satheesh Kumar | 3176890 | 2023-08-30 10:16:10 +0530 | [diff] [blame] | 782 | if (use_optimized_scaler && has_optimized_scaler) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 783 | av1_resize_and_extend_frame(ref, &new_fb->buf, filter, phase, |
| 784 | num_planes); |
Satheesh Kumar | 3176890 | 2023-08-30 10:16:10 +0530 | [diff] [blame] | 785 | } else if (!av1_resize_and_extend_frame_nonnormative( |
| 786 | ref, &new_fb->buf, (int)cm->seq_params->bit_depth, |
| 787 | num_planes)) { |
| 788 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
| 789 | "Failed to allocate buffer during resize"); |
| 790 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 791 | #endif |
| 792 | cpi->scaled_ref_buf[ref_frame - 1] = new_fb; |
| 793 | alloc_frame_mvs(cm, new_fb); |
| 794 | } |
| 795 | } else { |
| 796 | RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame); |
| 797 | buf->buf.y_crop_width = ref->y_crop_width; |
| 798 | buf->buf.y_crop_height = ref->y_crop_height; |
| 799 | cpi->scaled_ref_buf[ref_frame - 1] = buf; |
| 800 | ++buf->ref_count; |
| 801 | } |
| 802 | } else { |
| 803 | if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
Tarundeep Singh | 4912869 | 2021-04-07 12:37:21 +0530 | [diff] [blame] | 808 | BLOCK_SIZE av1_select_sb_size(const AV1EncoderConfig *const oxcf, int width, |
| 809 | int height, int number_spatial_layers) { |
Sai Deng | 06a32d9 | 2021-12-13 14:49:17 -0800 | [diff] [blame] | 810 | if (oxcf->tool_cfg.superblock_size == AOM_SUPERBLOCK_SIZE_64X64) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 811 | return BLOCK_64X64; |
Sai Deng | 06a32d9 | 2021-12-13 14:49:17 -0800 | [diff] [blame] | 812 | } |
| 813 | if (oxcf->tool_cfg.superblock_size == AOM_SUPERBLOCK_SIZE_128X128) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 814 | return BLOCK_128X128; |
Sai Deng | 06a32d9 | 2021-12-13 14:49:17 -0800 | [diff] [blame] | 815 | } |
| 816 | #if CONFIG_TFLITE |
| 817 | if (oxcf->q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED) return BLOCK_64X64; |
| 818 | #endif |
Jingning Han | 2e92bd5 | 2021-06-28 11:20:37 -0700 | [diff] [blame] | 819 | // Force 64x64 superblock size to increase resolution in perceptual |
| 820 | // AQ mode. |
| 821 | if (oxcf->mode == ALLINTRA && |
Sai Deng | 039ef51 | 2021-08-03 10:13:43 -0700 | [diff] [blame] | 822 | (oxcf->q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL_AI || |
Sai Deng | 06a32d9 | 2021-12-13 14:49:17 -0800 | [diff] [blame] | 823 | oxcf->q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED)) { |
Jingning Han | 2e92bd5 | 2021-06-28 11:20:37 -0700 | [diff] [blame] | 824 | return BLOCK_64X64; |
Sai Deng | 06a32d9 | 2021-12-13 14:49:17 -0800 | [diff] [blame] | 825 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 826 | assert(oxcf->tool_cfg.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC); |
| 827 | |
Tarundeep Singh | 4912869 | 2021-04-07 12:37:21 +0530 | [diff] [blame] | 828 | if (number_spatial_layers > 1 || |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 829 | oxcf->resize_cfg.resize_mode != RESIZE_NONE) { |
| 830 | // Use the configured size (top resolution) for spatial layers or |
| 831 | // on resize. |
Marco Paniconi | a768475 | 2022-04-13 15:18:53 -0700 | [diff] [blame] | 832 | return AOMMIN(oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height) > 720 |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 833 | ? BLOCK_128X128 |
| 834 | : BLOCK_64X64; |
Marco Paniconi | a768475 | 2022-04-13 15:18:53 -0700 | [diff] [blame] | 835 | } else if (oxcf->mode == REALTIME) { |
Marco Paniconi | 5115747 | 2023-01-18 00:24:35 -0800 | [diff] [blame] | 836 | if (oxcf->tune_cfg.content == AOM_CONTENT_SCREEN) { |
| 837 | const TileConfig *const tile_cfg = &oxcf->tile_cfg; |
| 838 | const int num_tiles = |
| 839 | (1 << tile_cfg->tile_columns) * (1 << tile_cfg->tile_rows); |
Wan-Teh Chang | d74c862 | 2023-03-01 15:23:15 -0800 | [diff] [blame] | 840 | // For multi-thread encode: if the number of (128x128) superblocks |
Marco Paniconi | 5115747 | 2023-01-18 00:24:35 -0800 | [diff] [blame] | 841 | // per tile is low use 64X64 superblock. |
| 842 | if (oxcf->row_mt == 1 && oxcf->max_threads >= 4 && |
| 843 | oxcf->max_threads >= num_tiles && AOMMIN(width, height) > 720 && |
Marco Paniconi | b51e6ee | 2023-01-31 14:40:14 -0800 | [diff] [blame] | 844 | (width * height) / (128 * 128 * num_tiles) <= 38) |
Marco Paniconi | 5115747 | 2023-01-18 00:24:35 -0800 | [diff] [blame] | 845 | return BLOCK_64X64; |
| 846 | else |
| 847 | return AOMMIN(width, height) >= 720 ? BLOCK_128X128 : BLOCK_64X64; |
| 848 | } else { |
Yunqing Wang | e538bf0 | 2022-08-10 17:27:52 -0700 | [diff] [blame] | 849 | return AOMMIN(width, height) > 720 ? BLOCK_128X128 : BLOCK_64X64; |
Marco Paniconi | 5115747 | 2023-01-18 00:24:35 -0800 | [diff] [blame] | 850 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // TODO(any): Possibly could improve this with a heuristic. |
| 854 | // When superres / resize is on, 'cm->width / height' can change between |
| 855 | // calls, so we don't apply this heuristic there. |
| 856 | // Things break if superblock size changes between the first pass and second |
| 857 | // pass encoding, which is why this heuristic is not configured as a |
| 858 | // speed-feature. |
| 859 | if (oxcf->superres_cfg.superres_mode == AOM_SUPERRES_NONE && |
Remya Prakasan | 22e8492 | 2021-10-25 19:48:53 +0530 | [diff] [blame] | 860 | oxcf->resize_cfg.resize_mode == RESIZE_NONE) { |
| 861 | int is_480p_or_lesser = AOMMIN(width, height) <= 480; |
chiyotsai | b6daca7 | 2022-05-25 11:01:09 -0700 | [diff] [blame] | 862 | if (oxcf->speed >= 1 && is_480p_or_lesser) return BLOCK_64X64; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 863 | |
Remya Prakasan | 22e8492 | 2021-10-25 19:48:53 +0530 | [diff] [blame] | 864 | // For 1080p and lower resolutions, choose SB size adaptively based on |
| 865 | // resolution and speed level for multi-thread encode. |
| 866 | int is_1080p_or_lesser = AOMMIN(width, height) <= 1080; |
| 867 | if (!is_480p_or_lesser && is_1080p_or_lesser && oxcf->mode == GOOD && |
| 868 | oxcf->row_mt == 1 && oxcf->max_threads > 1 && oxcf->speed >= 5) |
| 869 | return BLOCK_64X64; |
Mudassir Galaganath | 4a7863b | 2023-01-04 19:03:44 +0530 | [diff] [blame] | 870 | |
| 871 | // For allintra encode, since the maximum partition size is set to 32X32 for |
| 872 | // speed>=6, superblock size is set to 64X64 instead of 128X128. This |
| 873 | // improves the multithread performance due to reduction in top right delay |
| 874 | // and thread sync wastage. Currently, this setting is selectively enabled |
| 875 | // only for speed>=9 and resolutions less than 4k since cost update |
| 876 | // frequency is set to INTERNAL_COST_UPD_OFF in these cases. |
| 877 | const int is_4k_or_larger = AOMMIN(width, height) >= 2160; |
| 878 | if (oxcf->mode == ALLINTRA && oxcf->speed >= 9 && !is_4k_or_larger) |
| 879 | return BLOCK_64X64; |
Remya Prakasan | 22e8492 | 2021-10-25 19:48:53 +0530 | [diff] [blame] | 880 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 881 | return BLOCK_128X128; |
| 882 | } |
| 883 | |
| 884 | void av1_setup_frame(AV1_COMP *cpi) { |
| 885 | AV1_COMMON *const cm = &cpi->common; |
| 886 | // Set up entropy context depending on frame type. The decoder mandates |
| 887 | // the use of the default context, index 0, for keyframes and inter |
| 888 | // frames where the error_resilient_mode or intra_only flag is set. For |
| 889 | // other inter-frames the encoder currently uses only two contexts; |
| 890 | // context 1 for ALTREF frames and context 0 for the others. |
| 891 | |
| 892 | if (frame_is_intra_only(cm) || cm->features.error_resilient_mode || |
| 893 | cpi->ext_flags.use_primary_ref_none) { |
| 894 | av1_setup_past_independence(cm); |
| 895 | } |
| 896 | |
| 897 | if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) || |
| 898 | frame_is_sframe(cm)) { |
Tarundeep Singh | 0209135 | 2021-03-12 01:22:08 +0530 | [diff] [blame] | 899 | if (!cpi->ppi->seq_params_locked) { |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 900 | set_sb_size(cm->seq_params, |
Tarundeep Singh | 4912869 | 2021-04-07 12:37:21 +0530 | [diff] [blame] | 901 | av1_select_sb_size(&cpi->oxcf, cm->width, cm->height, |
Yuan Tong | 0189e1a | 2023-02-09 14:06:42 +0800 | [diff] [blame] | 902 | cpi->ppi->number_spatial_layers)); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 903 | } |
| 904 | } else { |
| 905 | const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm); |
| 906 | if (primary_ref_buf == NULL) { |
| 907 | av1_setup_past_independence(cm); |
| 908 | cm->seg.update_map = 1; |
| 909 | cm->seg.update_data = 1; |
| 910 | } else { |
| 911 | *cm->fc = primary_ref_buf->frame_context; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | av1_zero(cm->cur_frame->interp_filter_selected); |
| 916 | cm->prev_frame = get_primary_ref_frame_buf(cm); |
| 917 | cpi->vaq_refresh = 0; |
| 918 | } |
| 919 | |
| 920 | #if !CONFIG_REALTIME_ONLY |
| 921 | static int get_interp_filter_selected(const AV1_COMMON *const cm, |
| 922 | MV_REFERENCE_FRAME ref, |
| 923 | InterpFilter ifilter) { |
| 924 | const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref); |
| 925 | if (buf == NULL) return 0; |
| 926 | return buf->interp_filter_selected[ifilter]; |
| 927 | } |
| 928 | |
| 929 | uint16_t av1_setup_interp_filter_search_mask(AV1_COMP *cpi) { |
| 930 | const AV1_COMMON *const cm = &cpi->common; |
| 931 | int ref_total[REF_FRAMES] = { 0 }; |
| 932 | uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK; |
| 933 | |
| 934 | if (cpi->last_frame_type == KEY_FRAME || cpi->refresh_frame.alt_ref_frame) |
| 935 | return mask; |
| 936 | |
| 937 | for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) { |
| 938 | for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP; |
| 939 | ++ifilter) { |
| 940 | ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter); |
| 941 | } |
| 942 | } |
| 943 | int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] + |
| 944 | ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] + |
| 945 | ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]); |
| 946 | |
| 947 | for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP; |
| 948 | ++ifilter) { |
| 949 | int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30; |
| 950 | if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) { |
| 951 | int filter_score = |
| 952 | get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 + |
| 953 | get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 + |
| 954 | get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 + |
| 955 | get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 + |
| 956 | get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 + |
| 957 | get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10; |
| 958 | if (filter_score < ref_total_total) { |
| 959 | DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter; |
| 960 | reset_interp_filter_allowed_mask(&mask, filt_type); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | return mask; |
| 965 | } |
| 966 | |
| 967 | #define STRICT_PSNR_DIFF_THRESH 0.9 |
| 968 | // Encode key frame with/without screen content tools to determine whether |
| 969 | // screen content tools should be enabled for this key frame group or not. |
| 970 | // The first encoding is without screen content tools. |
| 971 | // The second encoding is with screen content tools. |
| 972 | // We compare the psnr and frame size to make the decision. |
| 973 | static void screen_content_tools_determination( |
| 974 | AV1_COMP *cpi, const int allow_screen_content_tools_orig_decision, |
| 975 | const int allow_intrabc_orig_decision, |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 976 | const int use_screen_content_tools_orig_decision, |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 977 | const int is_screen_content_type_orig_decision, const int pass, |
| 978 | int *projected_size_pass, PSNR_STATS *psnr) { |
| 979 | AV1_COMMON *const cm = &cpi->common; |
| 980 | FeatureFlags *const features = &cm->features; |
Remya Prakasan | 6566bc8 | 2021-11-05 23:21:12 +0530 | [diff] [blame] | 981 | |
Remya Prakasan | ffeb497 | 2022-06-21 20:00:28 +0530 | [diff] [blame] | 982 | #if CONFIG_FPMT_TEST |
Remya Prakasan | 6566bc8 | 2021-11-05 23:21:12 +0530 | [diff] [blame] | 983 | projected_size_pass[pass] = |
| 984 | ((cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] > 0) && |
| 985 | (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE)) |
| 986 | ? cpi->ppi->p_rc.temp_projected_frame_size |
| 987 | : cpi->rc.projected_frame_size; |
| 988 | #else |
Aasaipriya | 65e5e9e | 2021-08-12 11:31:27 +0530 | [diff] [blame] | 989 | projected_size_pass[pass] = cpi->rc.projected_frame_size; |
Remya Prakasan | 6566bc8 | 2021-11-05 23:21:12 +0530 | [diff] [blame] | 990 | #endif |
| 991 | |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 992 | #if CONFIG_AV1_HIGHBITDEPTH |
| 993 | const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth; |
| 994 | const uint32_t bit_depth = cpi->td.mb.e_mbd.bd; |
| 995 | aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass], |
| 996 | bit_depth, in_bit_depth); |
| 997 | #else |
| 998 | aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr[pass]); |
| 999 | #endif |
| 1000 | if (pass != 1) return; |
| 1001 | |
| 1002 | const double psnr_diff = psnr[1].psnr[0] - psnr[0].psnr[0]; |
Lin Zheng | 4c3c2c1 | 2023-03-02 01:49:58 +0000 | [diff] [blame] | 1003 | // Calculate % of palette mode to be chosen in a frame from mode decision. |
| 1004 | const double palette_ratio = |
| 1005 | (double)cpi->palette_pixel_num / (double)(cm->height * cm->width); |
Lin Zheng | d32006b | 2023-03-22 13:28:44 -0700 | [diff] [blame] | 1006 | const int psnr_diff_is_large = (psnr_diff > STRICT_PSNR_DIFF_THRESH); |
| 1007 | const int ratio_is_large = |
| 1008 | ((palette_ratio >= 0.0001) && ((psnr_diff / palette_ratio) > 4)); |
| 1009 | const int is_sc_encoding_much_better = (psnr_diff_is_large || ratio_is_large); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1010 | if (is_sc_encoding_much_better) { |
| 1011 | // Use screen content tools, if we get coding gain. |
| 1012 | features->allow_screen_content_tools = 1; |
| 1013 | features->allow_intrabc = cpi->intrabc_used; |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1014 | cpi->use_screen_content_tools = 1; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1015 | cpi->is_screen_content_type = 1; |
| 1016 | } else { |
| 1017 | // Use original screen content decision. |
| 1018 | features->allow_screen_content_tools = |
| 1019 | allow_screen_content_tools_orig_decision; |
| 1020 | features->allow_intrabc = allow_intrabc_orig_decision; |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1021 | cpi->use_screen_content_tools = use_screen_content_tools_orig_decision; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1022 | cpi->is_screen_content_type = is_screen_content_type_orig_decision; |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | // Set some encoding parameters to make the encoding process fast. |
| 1027 | // A fixed block partition size, and a large q is used. |
| 1028 | static void set_encoding_params_for_screen_content(AV1_COMP *cpi, |
| 1029 | const int pass) { |
| 1030 | AV1_COMMON *const cm = &cpi->common; |
| 1031 | if (pass == 0) { |
| 1032 | // In the first pass, encode without screen content tools. |
| 1033 | // Use a high q, and a fixed block size for fast encoding. |
| 1034 | cm->features.allow_screen_content_tools = 0; |
| 1035 | cm->features.allow_intrabc = 0; |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1036 | cpi->use_screen_content_tools = 0; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1037 | cpi->sf.part_sf.partition_search_type = FIXED_PARTITION; |
| 1038 | cpi->sf.part_sf.fixed_partition_size = BLOCK_32X32; |
| 1039 | return; |
| 1040 | } |
| 1041 | assert(pass == 1); |
| 1042 | // In the second pass, encode with screen content tools. |
| 1043 | // Use a high q, and a fixed block size for fast encoding. |
| 1044 | cm->features.allow_screen_content_tools = 1; |
| 1045 | // TODO(chengchen): turn intrabc on could lead to data race issue. |
| 1046 | // cm->allow_intrabc = 1; |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1047 | cpi->use_screen_content_tools = 1; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1048 | cpi->sf.part_sf.partition_search_type = FIXED_PARTITION; |
| 1049 | cpi->sf.part_sf.fixed_partition_size = BLOCK_32X32; |
| 1050 | } |
| 1051 | |
| 1052 | // Determines whether to use screen content tools for the key frame group. |
| 1053 | // This function modifies "cm->features.allow_screen_content_tools", |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1054 | // "cm->features.allow_intrabc" and "cpi->use_screen_content_tools". |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1055 | void av1_determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig) { |
| 1056 | AV1_COMMON *const cm = &cpi->common; |
| 1057 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
| 1058 | const QuantizationCfg *const q_cfg = &oxcf->q_cfg; |
| 1059 | // Variables to help determine if we should allow screen content tools. |
| 1060 | int projected_size_pass[3] = { 0 }; |
| 1061 | PSNR_STATS psnr[3]; |
| 1062 | const int is_key_frame = cm->current_frame.frame_type == KEY_FRAME; |
| 1063 | const int allow_screen_content_tools_orig_decision = |
| 1064 | cm->features.allow_screen_content_tools; |
| 1065 | const int allow_intrabc_orig_decision = cm->features.allow_intrabc; |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1066 | const int use_screen_content_tools_orig_decision = |
| 1067 | cpi->use_screen_content_tools; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1068 | const int is_screen_content_type_orig_decision = cpi->is_screen_content_type; |
| 1069 | // Turn off the encoding trial for forward key frame and superres. |
| 1070 | if (cpi->sf.rt_sf.use_nonrd_pick_mode || oxcf->kf_cfg.fwd_kf_enabled || |
| 1071 | cpi->superres_mode != AOM_SUPERRES_NONE || oxcf->mode == REALTIME || |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1072 | use_screen_content_tools_orig_decision || !is_key_frame) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | // TODO(chengchen): multiple encoding for the lossless mode is time consuming. |
| 1077 | // Find a better way to determine whether screen content tools should be used |
| 1078 | // for lossless coding. |
| 1079 | // Use a high q and a fixed partition to do quick encoding. |
| 1080 | const int q_for_screen_content_quick_run = |
| 1081 | is_lossless_requested(&oxcf->rc_cfg) ? q_orig : AOMMAX(q_orig, 244); |
| 1082 | const int partition_search_type_orig = cpi->sf.part_sf.partition_search_type; |
| 1083 | const BLOCK_SIZE fixed_partition_block_size_orig = |
| 1084 | cpi->sf.part_sf.fixed_partition_size; |
| 1085 | |
| 1086 | // Setup necessary params for encoding, including frame source, etc. |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1087 | |
Jayasanker J | dba4f0f | 2021-07-27 22:45:35 +0530 | [diff] [blame] | 1088 | cpi->source = av1_realloc_and_scale_if_required( |
| 1089 | cm, cpi->unscaled_source, &cpi->scaled_source, cm->features.interp_filter, |
Rachel Barker | 674eaa0 | 2022-12-22 16:03:44 +0000 | [diff] [blame] | 1090 | 0, false, false, cpi->oxcf.border_in_pixels, cpi->image_pyramid_levels); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1091 | if (cpi->unscaled_last_source != NULL) { |
Jayasanker J | dba4f0f | 2021-07-27 22:45:35 +0530 | [diff] [blame] | 1092 | cpi->last_source = av1_realloc_and_scale_if_required( |
Urvang Joshi | 708a34f | 2020-10-06 16:05:26 -0700 | [diff] [blame] | 1093 | cm, cpi->unscaled_last_source, &cpi->scaled_last_source, |
Jayasanker J | dba4f0f | 2021-07-27 22:45:35 +0530 | [diff] [blame] | 1094 | cm->features.interp_filter, 0, false, false, cpi->oxcf.border_in_pixels, |
Rachel Barker | 674eaa0 | 2022-12-22 16:03:44 +0000 | [diff] [blame] | 1095 | cpi->image_pyramid_levels); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | av1_setup_frame(cpi); |
| 1099 | |
| 1100 | if (cm->seg.enabled) { |
| 1101 | if (!cm->seg.update_data && cm->prev_frame) { |
| 1102 | segfeatures_copy(&cm->seg, &cm->prev_frame->seg); |
| 1103 | cm->seg.enabled = cm->prev_frame->seg.enabled; |
| 1104 | } else { |
| 1105 | av1_calculate_segdata(&cm->seg); |
| 1106 | } |
| 1107 | } else { |
| 1108 | memset(&cm->seg, 0, sizeof(cm->seg)); |
| 1109 | } |
| 1110 | segfeatures_copy(&cm->cur_frame->seg, &cm->seg); |
| 1111 | cm->cur_frame->seg.enabled = cm->seg.enabled; |
| 1112 | |
| 1113 | // The two encoding passes aim to help determine whether to use screen |
| 1114 | // content tools, with a high q and fixed partition. |
| 1115 | for (int pass = 0; pass < 2; ++pass) { |
| 1116 | set_encoding_params_for_screen_content(cpi, pass); |
Sai Deng | 14a0294 | 2020-08-13 15:30:50 -0700 | [diff] [blame] | 1117 | av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, |
| 1118 | q_for_screen_content_quick_run, |
Ryan Lei | b89e8f8 | 2021-09-26 15:10:32 -0700 | [diff] [blame] | 1119 | q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1120 | av1_set_speed_features_qindex_dependent(cpi, oxcf->speed); |
Apurve Kumar Pandey | 917568c | 2022-12-16 16:27:34 +0530 | [diff] [blame] | 1121 | av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params, |
| 1122 | cm->seq_params->bit_depth); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1123 | |
| 1124 | av1_set_variance_partition_thresholds(cpi, q_for_screen_content_quick_run, |
| 1125 | 0); |
| 1126 | // transform / motion compensation build reconstruction frame |
| 1127 | av1_encode_frame(cpi); |
| 1128 | // Screen content decision |
| 1129 | screen_content_tools_determination( |
| 1130 | cpi, allow_screen_content_tools_orig_decision, |
Cheng Chen | fd883dc | 2020-08-14 15:38:16 -0700 | [diff] [blame] | 1131 | allow_intrabc_orig_decision, use_screen_content_tools_orig_decision, |
| 1132 | is_screen_content_type_orig_decision, pass, projected_size_pass, psnr); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | // Set partition speed feature back. |
| 1136 | cpi->sf.part_sf.partition_search_type = partition_search_type_orig; |
| 1137 | cpi->sf.part_sf.fixed_partition_size = fixed_partition_block_size_orig; |
Jayasanker J | 92cca7a | 2021-10-27 15:05:52 +0530 | [diff] [blame] | 1138 | |
| 1139 | // Free token related info if screen content coding tools are not enabled. |
| 1140 | if (!cm->features.allow_screen_content_tools) |
| 1141 | free_token_info(&cpi->token_info); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1142 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1143 | #endif // CONFIG_REALTIME_ONLY |
| 1144 | |
| 1145 | static void fix_interp_filter(InterpFilter *const interp_filter, |
| 1146 | const FRAME_COUNTS *const counts) { |
| 1147 | if (*interp_filter == SWITCHABLE) { |
| 1148 | // Check to see if only one of the filters is actually used |
| 1149 | int count[SWITCHABLE_FILTERS] = { 0 }; |
| 1150 | int num_filters_used = 0; |
| 1151 | for (int i = 0; i < SWITCHABLE_FILTERS; ++i) { |
| 1152 | for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) |
| 1153 | count[i] += counts->switchable_interp[j][i]; |
| 1154 | num_filters_used += (count[i] > 0); |
| 1155 | } |
| 1156 | if (num_filters_used == 1) { |
| 1157 | // Only one filter is used. So set the filter at frame level |
| 1158 | for (int i = 0; i < SWITCHABLE_FILTERS; ++i) { |
| 1159 | if (count[i]) { |
Yunqing Wang | ea72639 | 2023-01-18 15:41:16 -0800 | [diff] [blame] | 1160 | *interp_filter = i; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1161 | break; |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | void av1_finalize_encoded_frame(AV1_COMP *const cpi) { |
| 1169 | AV1_COMMON *const cm = &cpi->common; |
| 1170 | CurrentFrame *const current_frame = &cm->current_frame; |
| 1171 | |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1172 | if (!cm->seq_params->reduced_still_picture_hdr && |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1173 | encode_show_existing_frame(cm)) { |
| 1174 | RefCntBuffer *const frame_to_show = |
| 1175 | cm->ref_frame_map[cpi->existing_fb_idx_to_show]; |
| 1176 | |
| 1177 | if (frame_to_show == NULL) { |
Tarundeep Singh | cfa5fb4 | 2021-04-23 18:25:32 +0530 | [diff] [blame] | 1178 | aom_internal_error(cm->error, AOM_CODEC_UNSUP_BITSTREAM, |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1179 | "Buffer does not contain a reconstructed frame"); |
| 1180 | } |
| 1181 | assert(frame_to_show->ref_count > 0); |
| 1182 | assign_frame_buffer_p(&cm->cur_frame, frame_to_show); |
| 1183 | } |
| 1184 | |
| 1185 | if (!encode_show_existing_frame(cm) && |
Tarundeep Singh | 4243e62 | 2021-04-20 16:10:22 +0530 | [diff] [blame] | 1186 | cm->seq_params->film_grain_params_present && |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1187 | (cm->show_frame || cm->showable_frame)) { |
| 1188 | // Copy the current frame's film grain params to the its corresponding |
| 1189 | // RefCntBuffer slot. |
| 1190 | cm->cur_frame->film_grain_params = cm->film_grain_params; |
| 1191 | |
| 1192 | // We must update the parameters if this is not an INTER_FRAME |
| 1193 | if (current_frame->frame_type != INTER_FRAME) |
| 1194 | cm->cur_frame->film_grain_params.update_parameters = 1; |
| 1195 | |
| 1196 | // Iterate the random seed for the next frame. |
| 1197 | cm->film_grain_params.random_seed += 3381; |
| 1198 | if (cm->film_grain_params.random_seed == 0) |
| 1199 | cm->film_grain_params.random_seed = 7391; |
| 1200 | } |
| 1201 | |
| 1202 | // Initialise all tiles' contexts from the global frame context |
| 1203 | for (int tile_col = 0; tile_col < cm->tiles.cols; tile_col++) { |
| 1204 | for (int tile_row = 0; tile_row < cm->tiles.rows; tile_row++) { |
| 1205 | const int tile_idx = tile_row * cm->tiles.cols + tile_col; |
| 1206 | cpi->tile_data[tile_idx].tctx = *cm->fc; |
| 1207 | } |
| 1208 | } |
| 1209 | |
Yunqing Wang | ea72639 | 2023-01-18 15:41:16 -0800 | [diff] [blame] | 1210 | if (!frame_is_intra_only(cm)) |
| 1211 | fix_interp_filter(&cm->features.interp_filter, cpi->td.counts); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | int av1_is_integer_mv(const YV12_BUFFER_CONFIG *cur_picture, |
| 1215 | const YV12_BUFFER_CONFIG *last_picture, |
| 1216 | ForceIntegerMVInfo *const force_intpel_info) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1217 | // check use hash ME |
| 1218 | int k; |
| 1219 | |
| 1220 | const int block_size = FORCE_INT_MV_DECISION_BLOCK_SIZE; |
| 1221 | const double threshold_current = 0.8; |
| 1222 | const double threshold_average = 0.95; |
| 1223 | const int max_history_size = 32; |
| 1224 | int T = 0; // total block |
| 1225 | int C = 0; // match with collocated block |
| 1226 | int S = 0; // smooth region but not match with collocated block |
| 1227 | |
| 1228 | const int pic_width = cur_picture->y_width; |
| 1229 | const int pic_height = cur_picture->y_height; |
| 1230 | for (int i = 0; i + block_size <= pic_height; i += block_size) { |
| 1231 | for (int j = 0; j + block_size <= pic_width; j += block_size) { |
| 1232 | const int x_pos = j; |
| 1233 | const int y_pos = i; |
| 1234 | int match = 1; |
| 1235 | T++; |
| 1236 | |
| 1237 | // check whether collocated block match with current |
| 1238 | uint8_t *p_cur = cur_picture->y_buffer; |
| 1239 | uint8_t *p_ref = last_picture->y_buffer; |
| 1240 | int stride_cur = cur_picture->y_stride; |
| 1241 | int stride_ref = last_picture->y_stride; |
| 1242 | p_cur += (y_pos * stride_cur + x_pos); |
| 1243 | p_ref += (y_pos * stride_ref + x_pos); |
| 1244 | |
| 1245 | if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) { |
| 1246 | uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur); |
| 1247 | uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref); |
| 1248 | for (int tmpY = 0; tmpY < block_size && match; tmpY++) { |
| 1249 | for (int tmpX = 0; tmpX < block_size && match; tmpX++) { |
| 1250 | if (p16_cur[tmpX] != p16_ref[tmpX]) { |
| 1251 | match = 0; |
| 1252 | } |
| 1253 | } |
| 1254 | p16_cur += stride_cur; |
| 1255 | p16_ref += stride_ref; |
| 1256 | } |
| 1257 | } else { |
| 1258 | for (int tmpY = 0; tmpY < block_size && match; tmpY++) { |
| 1259 | for (int tmpX = 0; tmpX < block_size && match; tmpX++) { |
| 1260 | if (p_cur[tmpX] != p_ref[tmpX]) { |
| 1261 | match = 0; |
| 1262 | } |
| 1263 | } |
| 1264 | p_cur += stride_cur; |
| 1265 | p_ref += stride_ref; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | if (match) { |
| 1270 | C++; |
| 1271 | continue; |
| 1272 | } |
| 1273 | |
| 1274 | if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos, |
| 1275 | y_pos) || |
| 1276 | av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) { |
| 1277 | S++; |
| 1278 | continue; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | assert(T > 0); |
| 1284 | double cs_rate = ((double)(C + S)) / ((double)(T)); |
| 1285 | |
| 1286 | force_intpel_info->cs_rate_array[force_intpel_info->rate_index] = cs_rate; |
| 1287 | |
| 1288 | force_intpel_info->rate_index = |
| 1289 | (force_intpel_info->rate_index + 1) % max_history_size; |
| 1290 | force_intpel_info->rate_size++; |
| 1291 | force_intpel_info->rate_size = |
| 1292 | AOMMIN(force_intpel_info->rate_size, max_history_size); |
| 1293 | |
| 1294 | if (cs_rate < threshold_current) { |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | if (C == T) { |
| 1299 | return 1; |
| 1300 | } |
| 1301 | |
| 1302 | double cs_average = 0.0; |
| 1303 | |
| 1304 | for (k = 0; k < force_intpel_info->rate_size; k++) { |
| 1305 | cs_average += force_intpel_info->cs_rate_array[k]; |
| 1306 | } |
| 1307 | cs_average /= force_intpel_info->rate_size; |
| 1308 | |
| 1309 | if (cs_average < threshold_average) { |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | if ((T - C - S) < 0) { |
| 1314 | return 1; |
| 1315 | } |
| 1316 | |
| 1317 | if (cs_average > 1.01) { |
| 1318 | return 1; |
| 1319 | } |
| 1320 | |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | void av1_set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) { |
| 1325 | const CommonModeInfoParams *const mi_params = &cpi->common.mi_params; |
Mudassir Galagnath | cd33f5f | 2022-07-15 12:17:07 +0530 | [diff] [blame] | 1326 | const MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1327 | uint8_t *y_buffer = cpi->source->y_buffer; |
| 1328 | const int y_stride = cpi->source->y_stride; |
| 1329 | const int block_size = BLOCK_16X16; |
| 1330 | |
| 1331 | const int num_mi_w = mi_size_wide[block_size]; |
| 1332 | const int num_mi_h = mi_size_high[block_size]; |
| 1333 | const int num_cols = (mi_params->mi_cols + num_mi_w - 1) / num_mi_w; |
| 1334 | const int num_rows = (mi_params->mi_rows + num_mi_h - 1) / num_mi_h; |
| 1335 | double log_sum = 0.0; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1336 | |
| 1337 | // Loop through each 16x16 block. |
| 1338 | for (int row = 0; row < num_rows; ++row) { |
| 1339 | for (int col = 0; col < num_cols; ++col) { |
| 1340 | double var = 0.0, num_of_var = 0.0; |
| 1341 | const int index = row * num_cols + col; |
| 1342 | |
| 1343 | // Loop through each 8x8 block. |
| 1344 | for (int mi_row = row * num_mi_h; |
| 1345 | mi_row < mi_params->mi_rows && mi_row < (row + 1) * num_mi_h; |
| 1346 | mi_row += 2) { |
| 1347 | for (int mi_col = col * num_mi_w; |
| 1348 | mi_col < mi_params->mi_cols && mi_col < (col + 1) * num_mi_w; |
| 1349 | mi_col += 2) { |
| 1350 | struct buf_2d buf; |
| 1351 | const int row_offset_y = mi_row << 2; |
| 1352 | const int col_offset_y = mi_col << 2; |
| 1353 | |
| 1354 | buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y; |
| 1355 | buf.stride = y_stride; |
| 1356 | |
Mudassir Galagnath | 4673972 | 2022-07-22 00:35:17 +0530 | [diff] [blame] | 1357 | var += av1_get_perpixel_variance_facade(cpi, xd, &buf, BLOCK_8X8, |
| 1358 | AOM_PLANE_Y); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1359 | num_of_var += 1.0; |
| 1360 | } |
| 1361 | } |
| 1362 | var = var / num_of_var; |
| 1363 | |
| 1364 | // Curve fitting with an exponential model on all 16x16 blocks from the |
| 1365 | // midres dataset. |
| 1366 | var = 67.035434 * (1 - exp(-0.0021489 * var)) + 17.492222; |
Apurve Kumar Pandey | 175bb56 | 2023-01-23 10:32:41 +0530 | [diff] [blame] | 1367 | |
| 1368 | // As per the above computation, var will be in the range of |
| 1369 | // [17.492222, 84.527656], assuming the data type is of infinite |
| 1370 | // precision. The following assert conservatively checks if var is in the |
| 1371 | // range of [17.0, 85.0] to avoid any issues due to the precision of the |
| 1372 | // relevant data type. |
| 1373 | assert(var > 17.0 && var < 85.0); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1374 | cpi->ssim_rdmult_scaling_factors[index] = var; |
| 1375 | log_sum += log(var); |
| 1376 | } |
| 1377 | } |
Apurve Kumar Pandey | 175bb56 | 2023-01-23 10:32:41 +0530 | [diff] [blame] | 1378 | |
| 1379 | // As log_sum holds the geometric mean, it will be in the range |
| 1380 | // [17.492222, 84.527656]. Hence, in the below loop, the value of |
| 1381 | // cpi->ssim_rdmult_scaling_factors[index] would be in the range |
| 1382 | // [0.2069, 4.8323]. |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1383 | log_sum = exp(log_sum / (double)(num_rows * num_cols)); |
| 1384 | |
| 1385 | for (int row = 0; row < num_rows; ++row) { |
| 1386 | for (int col = 0; col < num_cols; ++col) { |
| 1387 | const int index = row * num_cols + col; |
| 1388 | cpi->ssim_rdmult_scaling_factors[index] /= log_sum; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1393 | // Coding context that only needs to be saved when recode loop includes |
| 1394 | // filtering (deblocking, CDEF, superres post-encode upscale and/or loop |
| 1395 | // restoraton). |
| 1396 | static void save_extra_coding_context(AV1_COMP *cpi) { |
| 1397 | CODING_CONTEXT *const cc = &cpi->coding_context; |
| 1398 | AV1_COMMON *cm = &cpi->common; |
| 1399 | |
| 1400 | cc->lf = cm->lf; |
| 1401 | cc->cdef_info = cm->cdef_info; |
| 1402 | cc->rc = cpi->rc; |
Aasaipriya Chandran | be8ccad | 2021-05-25 20:37:01 +0530 | [diff] [blame] | 1403 | cc->mv_stats = cpi->ppi->mv_stats; |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | void av1_save_all_coding_context(AV1_COMP *cpi) { |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1407 | save_extra_coding_context(cpi); |
| 1408 | if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi); |
| 1409 | } |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1410 | |
| 1411 | #if DUMP_RECON_FRAMES == 1 |
| 1412 | |
| 1413 | // NOTE(zoeliu): For debug - Output the filtered reconstructed video. |
| 1414 | void av1_dump_filtered_recon_frames(AV1_COMP *cpi) { |
| 1415 | AV1_COMMON *const cm = &cpi->common; |
| 1416 | const CurrentFrame *const current_frame = &cm->current_frame; |
| 1417 | const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf; |
| 1418 | |
| 1419 | if (recon_buf == NULL) { |
| 1420 | printf("Frame %d is not ready.\n", current_frame->frame_number); |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | static const int flag_list[REF_FRAMES] = { 0, |
| 1425 | AOM_LAST_FLAG, |
| 1426 | AOM_LAST2_FLAG, |
| 1427 | AOM_LAST3_FLAG, |
| 1428 | AOM_GOLD_FLAG, |
| 1429 | AOM_BWD_FLAG, |
| 1430 | AOM_ALT2_FLAG, |
| 1431 | AOM_ALT_FLAG }; |
| 1432 | printf( |
| 1433 | "\n***Frame=%d (frame_offset=%d, show_frame=%d, " |
| 1434 | "show_existing_frame=%d) " |
| 1435 | "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[", |
| 1436 | current_frame->frame_number, current_frame->order_hint, cm->show_frame, |
| 1437 | cm->show_existing_frame); |
| 1438 | for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
| 1439 | const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame); |
| 1440 | const int ref_offset = buf != NULL ? (int)buf->order_hint : -1; |
| 1441 | printf(" %d(%c)", ref_offset, |
| 1442 | (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N'); |
| 1443 | } |
| 1444 | printf(" ]\n"); |
| 1445 | |
| 1446 | if (!cm->show_frame) { |
| 1447 | printf("Frame %d is a no show frame, so no image dump.\n", |
| 1448 | current_frame->frame_number); |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | int h; |
| 1453 | char file_name[256] = "/tmp/enc_filtered_recon.yuv"; |
| 1454 | FILE *f_recon = NULL; |
| 1455 | |
| 1456 | if (current_frame->frame_number == 0) { |
| 1457 | if ((f_recon = fopen(file_name, "wb")) == NULL) { |
| 1458 | printf("Unable to open file %s to write.\n", file_name); |
| 1459 | return; |
| 1460 | } |
| 1461 | } else { |
| 1462 | if ((f_recon = fopen(file_name, "ab")) == NULL) { |
| 1463 | printf("Unable to open file %s to append.\n", file_name); |
| 1464 | return; |
| 1465 | } |
| 1466 | } |
| 1467 | printf( |
| 1468 | "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, " |
| 1469 | "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, " |
| 1470 | "refresh_alt_ref_frame=%d, " |
| 1471 | "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n", |
Mufaddal Chakera | ab20d37 | 2021-03-17 12:18:34 +0530 | [diff] [blame] | 1472 | current_frame->frame_number, cpi->gf_frame_index, |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 1473 | cpi->ppi->gf_group.update_type[cpi->gf_frame_index], |
| 1474 | current_frame->order_hint, cm->show_frame, cm->show_existing_frame, |
Mufaddal Chakera | 09b847f | 2021-04-20 07:11:24 +0530 | [diff] [blame] | 1475 | cpi->rc.source_alt_ref_active, cpi->refresh_frame.alt_ref_frame, |
Mufaddal Chakera | 8ee04fa | 2021-03-17 13:33:18 +0530 | [diff] [blame] | 1476 | recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height); |
Jayasanker J | dee179d | 2020-07-10 23:47:37 +0530 | [diff] [blame] | 1477 | #if 0 |
| 1478 | int ref_frame; |
| 1479 | printf("get_ref_frame_map_idx: ["); |
| 1480 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) |
| 1481 | printf(" %d", get_ref_frame_map_idx(cm, ref_frame)); |
| 1482 | printf(" ]\n"); |
| 1483 | #endif // 0 |
| 1484 | |
| 1485 | // --- Y --- |
| 1486 | for (h = 0; h < cm->height; ++h) { |
| 1487 | fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width, |
| 1488 | f_recon); |
| 1489 | } |
| 1490 | // --- U --- |
| 1491 | for (h = 0; h < (cm->height >> 1); ++h) { |
| 1492 | fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1), |
| 1493 | f_recon); |
| 1494 | } |
| 1495 | // --- V --- |
| 1496 | for (h = 0; h < (cm->height >> 1); ++h) { |
| 1497 | fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1), |
| 1498 | f_recon); |
| 1499 | } |
| 1500 | |
| 1501 | fclose(f_recon); |
| 1502 | } |
| 1503 | #endif // DUMP_RECON_FRAMES |