Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 1 | /* |
Krishna Rapaka | 7319db5 | 2021-09-28 20:35:29 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 3 | * |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +0000 | [diff] [blame] | 4 | * This source code is subject to the terms of the BSD 3-Clause Clear License |
| 5 | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
| 6 | * License was not distributed with this source code in the LICENSE file, you |
| 7 | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
| 8 | * Alliance for Open Media Patent License 1.0 was not distributed with this |
| 9 | * source code in the PATENTS file, you can obtain it at |
| 10 | * aomedia.org/license/patent-license/. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <limits.h> |
| 14 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | #include "aom_mem/aom_mem.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 16 | |
| 17 | #include "av1/common/pred_common.h" |
| 18 | #include "av1/common/tile_common.h" |
| 19 | |
| 20 | #include "av1/encoder/cost.h" |
| 21 | #include "av1/encoder/segmentation.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 22 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 23 | void av1_enable_segmentation(struct segmentation *seg) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 24 | seg->enabled = 1; |
| 25 | seg->update_map = 1; |
| 26 | seg->update_data = 1; |
Yushin Cho | b42e98d | 2018-01-25 12:06:05 -0800 | [diff] [blame] | 27 | seg->temporal_update = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 30 | void av1_disable_segmentation(struct segmentation *seg) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 31 | seg->enabled = 0; |
| 32 | seg->update_map = 0; |
| 33 | seg->update_data = 0; |
Yushin Cho | b42e98d | 2018-01-25 12:06:05 -0800 | [diff] [blame] | 34 | seg->temporal_update = 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 37 | void av1_disable_segfeature(struct segmentation *seg, int segment_id, |
| 38 | SEG_LVL_FEATURES feature_id) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 39 | seg->feature_mask[segment_id] &= ~(1 << feature_id); |
| 40 | } |
| 41 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 42 | void av1_clear_segdata(struct segmentation *seg, int segment_id, |
| 43 | SEG_LVL_FEATURES feature_id) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 44 | seg->feature_data[segment_id][feature_id] = 0; |
| 45 | } |
| 46 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 47 | static void count_segs(const AV1_COMMON *cm, MACROBLOCKD *xd, |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 48 | const TileInfo *tile, MB_MODE_INFO **mi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 49 | unsigned *no_pred_segcounts, |
| 50 | unsigned (*temporal_predictor_count)[2], |
| 51 | unsigned *t_unpred_seg_counts, int bw, int bh, |
| 52 | int mi_row, int mi_col) { |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 53 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
| 54 | if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 55 | |
| 56 | xd->mi = mi; |
Chi Yo Tsai | e05bc1d | 2023-01-12 20:23:30 +0000 | [diff] [blame] | 57 | assert(xd->mi && xd->mi[0]); |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 58 | set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, mi_params->mi_rows, |
Chi Yo Tsai | e05bc1d | 2023-01-12 20:23:30 +0000 | [diff] [blame] | 59 | mi_params->mi_cols, &xd->mi[0]->chroma_ref_info); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 60 | |
| 61 | // Count the number of hits on each segment with no prediction |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 62 | const int segment_id = xd->mi[0]->segment_id; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 63 | no_pred_segcounts[segment_id]++; |
| 64 | |
| 65 | // Temporal prediction not allowed on key frames |
David Turner | d2a592e | 2018-11-16 14:59:31 +0000 | [diff] [blame] | 66 | if (cm->current_frame.frame_type != KEY_FRAME) { |
liang zhao | c6f775a | 2020-12-17 11:54:58 -0800 | [diff] [blame] | 67 | const BLOCK_SIZE bsize = xd->mi[0]->sb_type[xd->tree_type == CHROMA_PART]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 68 | // Test to see if the segment id matches the predicted value. |
| 69 | const int pred_segment_id = |
Yue Chen | d90d343 | 2018-03-16 11:28:42 -0700 | [diff] [blame] | 70 | cm->last_frame_seg_map |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 71 | ? get_segment_id(mi_params, cm->last_frame_seg_map, bsize, mi_row, |
| 72 | mi_col) |
Yue Chen | d90d343 | 2018-03-16 11:28:42 -0700 | [diff] [blame] | 73 | : 0; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 74 | const int pred_flag = pred_segment_id == segment_id; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 75 | const int pred_context = av1_get_pred_context_seg_id(xd); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 76 | |
| 77 | // Store the prediction status for this mb and update counts |
| 78 | // as appropriate |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 79 | xd->mi[0]->seg_id_predicted = pred_flag; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 80 | temporal_predictor_count[pred_context][pred_flag]++; |
| 81 | |
| 82 | // Update the "unpredicted" segment count |
| 83 | if (!pred_flag) t_unpred_seg_counts[segment_id]++; |
| 84 | } |
| 85 | } |
| 86 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 87 | static void count_segs_sb(const AV1_COMMON *cm, MACROBLOCKD *xd, |
Yue Chen | 53b53f0 | 2018-03-29 14:31:23 -0700 | [diff] [blame] | 88 | const TileInfo *tile, MB_MODE_INFO **mi, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 89 | unsigned *no_pred_segcounts, |
| 90 | unsigned (*temporal_predictor_count)[2], |
| 91 | unsigned *t_unpred_seg_counts, int mi_row, int mi_col, |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 92 | #if !CONFIG_EXT_RECUR_PARTITIONS |
| 93 | BLOCK_SIZE bsize, |
| 94 | #endif // !CONFIG_EXT_RECUR_PARTITIONS |
| 95 | const PARTITION_TREE *ptree) { |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 96 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
| 97 | const int mis = mi_params->mi_stride; |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 98 | #if CONFIG_EXT_RECUR_PARTITIONS |
| 99 | BLOCK_SIZE bsize = ptree->bsize; |
| 100 | const int bw = mi_size_wide[bsize], bh = mi_size_high[bsize]; |
| 101 | const int hbw = bw / 2, hbh = bh / 2; |
| 102 | const int qbw = bw / 4, qbh = bh / 4; |
Urvang Joshi | e2d0566 | 2023-06-21 21:40:27 +0000 | [diff] [blame] | 103 | const int ebw = bw / 8, ebh = bh / 8; |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 104 | #else |
Jingning Han | c709e1f | 2016-12-06 14:48:09 -0800 | [diff] [blame] | 105 | const int bs = mi_size_wide[bsize], hbs = bs / 2; |
Rupert Swarbrick | 3dd3391 | 2017-09-12 14:24:11 +0100 | [diff] [blame] | 106 | const int qbs = bs / 4; |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 107 | #endif // CONFIG_EXT_RECUR_PARTITIONS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 108 | |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 109 | if (mi_row >= mi_params->mi_rows || mi_col >= mi_params->mi_cols) return; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 110 | |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 111 | #define CSEGS(cs_bw, cs_bh, cs_rowoff, cs_coloff) \ |
| 112 | count_segs(cm, xd, tile, mi + mis * (cs_rowoff) + (cs_coloff), \ |
| 113 | no_pred_segcounts, temporal_predictor_count, t_unpred_seg_counts, \ |
| 114 | (cs_bw), (cs_bh), mi_row + (cs_rowoff), mi_col + (cs_coloff)); |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 115 | #if CONFIG_EXT_RECUR_PARTITIONS |
| 116 | #define CSEGS_RECURSIVE(cs_rowoff, cs_coloff, subtree) \ |
| 117 | count_segs_sb(cm, xd, tile, mi + mis * (cs_rowoff) + (cs_coloff), \ |
| 118 | no_pred_segcounts, temporal_predictor_count, \ |
| 119 | t_unpred_seg_counts, mi_row + (cs_rowoff), \ |
| 120 | mi_col + (cs_coloff), subtree); |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 121 | |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 122 | int tree_idx = 0; |
| 123 | const PARTITION_TYPE partition = ptree->partition; |
| 124 | #else |
| 125 | PARTITION_TYPE partition; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 126 | if (bsize == BLOCK_8X8) |
| 127 | partition = PARTITION_NONE; |
| 128 | else |
leolzhao | 3db7cca | 2021-01-26 16:53:07 -0800 | [diff] [blame] | 129 | partition = |
| 130 | get_partition(cm, xd->tree_type == CHROMA_PART, mi_row, mi_col, bsize); |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 131 | #endif // CONFIG_EXT_RECUR_PARTITIONS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 132 | switch (partition) { |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 133 | #if CONFIG_EXT_RECUR_PARTITIONS |
| 134 | case PARTITION_NONE: CSEGS(bw, bh, 0, 0); break; |
| 135 | case PARTITION_HORZ: |
| 136 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 137 | CSEGS_RECURSIVE(hbh, 0, ptree->sub_tree[tree_idx++]); |
| 138 | break; |
| 139 | case PARTITION_VERT: |
| 140 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 141 | CSEGS_RECURSIVE(0, hbw, ptree->sub_tree[tree_idx++]); |
| 142 | break; |
| 143 | case PARTITION_HORZ_3: |
| 144 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 145 | CSEGS_RECURSIVE(qbh, 0, ptree->sub_tree[tree_idx++]); |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 146 | CSEGS_RECURSIVE(qbh, hbw, ptree->sub_tree[tree_idx++]); |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 147 | if (mi_row + 3 * qbh < mi_params->mi_rows) |
| 148 | CSEGS_RECURSIVE(3 * qbh, 0, ptree->sub_tree[tree_idx++]); |
| 149 | break; |
| 150 | case PARTITION_VERT_3: |
| 151 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 152 | CSEGS_RECURSIVE(0, qbw, ptree->sub_tree[tree_idx++]); |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 153 | CSEGS_RECURSIVE(hbh, qbw, ptree->sub_tree[tree_idx++]); |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 154 | if (mi_col + 3 * qbw < mi_params->mi_cols) |
| 155 | CSEGS_RECURSIVE(0, 3 * qbw, ptree->sub_tree[tree_idx++]); |
| 156 | break; |
Urvang Joshi | e2d0566 | 2023-06-21 21:40:27 +0000 | [diff] [blame] | 157 | case PARTITION_HORZ_4A: |
| 158 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 159 | if (mi_row + ebh < mi_params->mi_rows) |
| 160 | CSEGS_RECURSIVE(ebh, 0, ptree->sub_tree[tree_idx++]); |
| 161 | if (mi_row + 3 * ebh < mi_params->mi_rows) |
| 162 | CSEGS_RECURSIVE(3 * ebh, 0, ptree->sub_tree[tree_idx++]); |
| 163 | if (mi_row + 7 * ebh < mi_params->mi_rows) |
| 164 | CSEGS_RECURSIVE(7 * ebh, 0, ptree->sub_tree[tree_idx++]); |
| 165 | break; |
| 166 | case PARTITION_HORZ_4B: |
| 167 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 168 | if (mi_row + ebh < mi_params->mi_rows) |
| 169 | CSEGS_RECURSIVE(ebh, 0, ptree->sub_tree[tree_idx++]); |
| 170 | if (mi_row + 5 * ebh < mi_params->mi_rows) |
| 171 | CSEGS_RECURSIVE(5 * ebh, 0, ptree->sub_tree[tree_idx++]); |
| 172 | if (mi_row + 7 * ebh < mi_params->mi_rows) |
| 173 | CSEGS_RECURSIVE(7 * ebh, 0, ptree->sub_tree[tree_idx++]); |
| 174 | break; |
| 175 | case PARTITION_VERT_4A: |
| 176 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 177 | if (mi_col + ebw < mi_params->mi_cols) |
| 178 | CSEGS_RECURSIVE(0, ebw, ptree->sub_tree[tree_idx++]); |
| 179 | if (mi_col + 3 * ebw < mi_params->mi_cols) |
| 180 | CSEGS_RECURSIVE(0, 3 * ebw, ptree->sub_tree[tree_idx++]); |
| 181 | if (mi_col + 7 * ebw < mi_params->mi_cols) |
| 182 | CSEGS_RECURSIVE(0, 7 * ebw, ptree->sub_tree[tree_idx++]); |
| 183 | break; |
| 184 | case PARTITION_VERT_4B: |
| 185 | CSEGS_RECURSIVE(0, 0, ptree->sub_tree[tree_idx++]); |
| 186 | if (mi_col + ebw < mi_params->mi_cols) |
| 187 | CSEGS_RECURSIVE(0, ebw, ptree->sub_tree[tree_idx++]); |
| 188 | if (mi_col + 5 * ebw < mi_params->mi_cols) |
| 189 | CSEGS_RECURSIVE(0, 5 * ebw, ptree->sub_tree[tree_idx++]); |
| 190 | if (mi_col + 7 * ebw < mi_params->mi_cols) |
| 191 | CSEGS_RECURSIVE(0, 7 * ebw, ptree->sub_tree[tree_idx++]); |
| 192 | break; |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 193 | #else // CONFIG_EXT_RECUR_PARTITIONS |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 194 | case PARTITION_NONE: CSEGS(bs, bs, 0, 0); break; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 195 | case PARTITION_HORZ: |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 196 | CSEGS(bs, hbs, 0, 0); |
| 197 | CSEGS(bs, hbs, hbs, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 198 | break; |
| 199 | case PARTITION_VERT: |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 200 | CSEGS(hbs, bs, 0, 0); |
| 201 | CSEGS(hbs, bs, 0, hbs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 202 | break; |
| 203 | case PARTITION_HORZ_A: |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 204 | CSEGS(hbs, hbs, 0, 0); |
| 205 | CSEGS(hbs, hbs, 0, hbs); |
| 206 | CSEGS(bs, hbs, hbs, 0); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 207 | break; |
| 208 | case PARTITION_HORZ_B: |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 209 | CSEGS(bs, hbs, 0, 0); |
| 210 | CSEGS(hbs, hbs, hbs, 0); |
| 211 | CSEGS(hbs, hbs, hbs, hbs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 212 | break; |
| 213 | case PARTITION_VERT_A: |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 214 | CSEGS(hbs, hbs, 0, 0); |
| 215 | CSEGS(hbs, hbs, hbs, 0); |
| 216 | CSEGS(hbs, bs, 0, hbs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 217 | break; |
| 218 | case PARTITION_VERT_B: |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 219 | CSEGS(hbs, bs, 0, 0); |
| 220 | CSEGS(hbs, hbs, 0, hbs); |
| 221 | CSEGS(hbs, hbs, hbs, hbs); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 222 | break; |
Rupert Swarbrick | 2725693 | 2017-10-11 15:54:36 +0100 | [diff] [blame] | 223 | case PARTITION_HORZ_4: |
| 224 | CSEGS(bs, qbs, 0, 0); |
| 225 | CSEGS(bs, qbs, qbs, 0); |
| 226 | CSEGS(bs, qbs, 2 * qbs, 0); |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 227 | if (mi_row + 3 * qbs < mi_params->mi_rows) CSEGS(bs, qbs, 3 * qbs, 0); |
Rupert Swarbrick | 2725693 | 2017-10-11 15:54:36 +0100 | [diff] [blame] | 228 | break; |
Rupert Swarbrick | 2725693 | 2017-10-11 15:54:36 +0100 | [diff] [blame] | 229 | case PARTITION_VERT_4: |
| 230 | CSEGS(qbs, bs, 0, 0); |
| 231 | CSEGS(qbs, bs, 0, qbs); |
| 232 | CSEGS(qbs, bs, 0, 2 * qbs); |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 233 | if (mi_col + 3 * qbs < mi_params->mi_cols) CSEGS(qbs, bs, 0, 3 * qbs); |
Rupert Swarbrick | 2725693 | 2017-10-11 15:54:36 +0100 | [diff] [blame] | 234 | break; |
Chi Yo Tsai | e05bc1d | 2023-01-12 20:23:30 +0000 | [diff] [blame] | 235 | #endif // CONFIG_EXT_RECUR_PARTITIONS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 236 | case PARTITION_SPLIT: { |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 237 | #if !CONFIG_EXT_RECUR_PARTITIONS |
Cheng Chen | 82b4fa1 | 2018-05-02 18:43:17 -0700 | [diff] [blame] | 238 | const BLOCK_SIZE subsize = get_partition_subsize(bsize, PARTITION_SPLIT); |
kyslov | 5859dca | 2019-04-08 12:13:11 -0700 | [diff] [blame] | 239 | assert(subsize < BLOCK_SIZES_ALL); |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 240 | #endif // !CONFIG_EXT_RECUR_PARTITIONS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 241 | |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 242 | for (int n = 0; n < 4; n++) { |
| 243 | #if CONFIG_EXT_RECUR_PARTITIONS |
| 244 | const int mi_dc = hbw * (n & 1); |
| 245 | const int mi_dr = hbh * (n >> 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 246 | count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts, |
| 247 | temporal_predictor_count, t_unpred_seg_counts, |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 248 | mi_row + mi_dr, mi_col + mi_dc, ptree->sub_tree[n]); |
| 249 | #else |
| 250 | const int mi_dc = hbs * (n & 1); |
| 251 | const int mi_dr = hbs * (n >> 1); |
| 252 | count_segs_sb(cm, xd, tile, &mi[mi_dr * mis + mi_dc], no_pred_segcounts, |
| 253 | temporal_predictor_count, t_unpred_seg_counts, |
| 254 | mi_row + mi_dr, mi_col + mi_dc, subsize, |
| 255 | ptree->sub_tree[n]); |
| 256 | #endif // CONFIG_EXT_RECUR_PARTITIONS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 257 | } |
| 258 | } break; |
| 259 | default: assert(0); |
| 260 | } |
Rupert Swarbrick | 114e81a | 2017-09-11 15:08:25 +0100 | [diff] [blame] | 261 | |
| 262 | #undef CSEGS |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Jerome Jiang | f966eb1 | 2019-10-17 14:58:50 -0700 | [diff] [blame] | 265 | void av1_choose_segmap_coding_method(AV1_COMMON *cm, MACROBLOCKD *xd) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 266 | struct segmentation *seg = &cm->seg; |
| 267 | struct segmentation_probs *segp = &cm->fc->seg; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 268 | int no_pred_cost; |
| 269 | int t_pred_cost = INT_MAX; |
Thomas Davies | 0002135 | 2017-07-11 16:07:55 +0100 | [diff] [blame] | 270 | int tile_col, tile_row, mi_row, mi_col; |
Hui Su | 52b1ba2 | 2017-12-27 14:25:25 -0800 | [diff] [blame] | 271 | unsigned temporal_predictor_count[SEG_TEMPORAL_PRED_CTXS][2] = { { 0 } }; |
| 272 | unsigned no_pred_segcounts[MAX_SEGMENTS] = { 0 }; |
| 273 | unsigned t_unpred_seg_counts[MAX_SEGMENTS] = { 0 }; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 274 | (void)xd; |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 275 | int scale_up = cm->prev_frame && (cm->width > cm->prev_frame->width || |
| 276 | cm->height > cm->prev_frame->height); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 277 | // First of all generate stats regarding how well the last segment map |
| 278 | // predicts this one |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 279 | if (!scale_up) { |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 280 | for (tile_row = 0; tile_row < cm->tiles.rows; tile_row++) { |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 281 | TileInfo tile_info; |
| 282 | av1_tile_set_row(&tile_info, cm, tile_row); |
Urvang Joshi | 54ffae7 | 2020-03-23 13:37:10 -0700 | [diff] [blame] | 283 | for (tile_col = 0; tile_col < cm->tiles.cols; tile_col++) { |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 284 | MB_MODE_INFO **mi_ptr; |
| 285 | av1_tile_set_col(&tile_info, cm, tile_col); |
Urvang Joshi | 9dc909d | 2020-03-23 16:07:02 -0700 | [diff] [blame] | 286 | mi_ptr = cm->mi_params.mi_grid_base + |
| 287 | tile_info.mi_row_start * cm->mi_params.mi_stride + |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 288 | tile_info.mi_col_start; |
| 289 | for (mi_row = tile_info.mi_row_start; mi_row < tile_info.mi_row_end; |
Chi Yo Tsai | ac6a5c8 | 2023-10-16 21:56:18 +0000 | [diff] [blame] | 290 | mi_row += cm->mib_size, |
| 291 | mi_ptr += cm->mib_size * cm->mi_params.mi_stride) { |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 292 | MB_MODE_INFO **mi = mi_ptr; |
| 293 | for (mi_col = tile_info.mi_col_start; mi_col < tile_info.mi_col_end; |
Chi Yo Tsai | ac6a5c8 | 2023-10-16 21:56:18 +0000 | [diff] [blame] | 294 | mi_col += cm->mib_size, mi += cm->mib_size) { |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 295 | const SB_INFO *sbi = av1_get_sb_info(cm, mi_row, mi_col); |
| 296 | const PARTITION_TREE *ptree = sbi->ptree_root[AOM_PLANE_Y]; |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 297 | count_segs_sb(cm, xd, &tile_info, mi, no_pred_segcounts, |
| 298 | temporal_predictor_count, t_unpred_seg_counts, mi_row, |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 299 | mi_col, |
| 300 | #if !CONFIG_EXT_RECUR_PARTITIONS |
Chi Yo Tsai | ac6a5c8 | 2023-10-16 21:56:18 +0000 | [diff] [blame] | 301 | cm->sb_size, |
chiyotsai | 16eb685 | 2023-03-14 23:24:25 -0700 | [diff] [blame] | 302 | #endif // !CONFIG_EXT_RECUR_PARTITIONS |
| 303 | ptree); |
Jerome Jiang | d4e4e58 | 2019-05-14 10:29:28 -0700 | [diff] [blame] | 304 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Hui Su | 52b1ba2 | 2017-12-27 14:25:25 -0800 | [diff] [blame] | 310 | int seg_id_cost[MAX_SEGMENTS]; |
| 311 | av1_cost_tokens_from_cdf(seg_id_cost, segp->tree_cdf, NULL); |
| 312 | no_pred_cost = 0; |
| 313 | for (int i = 0; i < MAX_SEGMENTS; ++i) |
| 314 | no_pred_cost += no_pred_segcounts[i] * seg_id_cost[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 315 | |
David Barker | cc615a8 | 2018-03-19 14:38:51 +0000 | [diff] [blame] | 316 | // Frames without past dependency cannot use temporal prediction |
Urvang Joshi | 6237b88 | 2020-03-26 15:02:26 -0700 | [diff] [blame] | 317 | if (cm->features.primary_ref_frame != PRIMARY_REF_NONE) { |
Hui Su | 52b1ba2 | 2017-12-27 14:25:25 -0800 | [diff] [blame] | 318 | int pred_flag_cost[SEG_TEMPORAL_PRED_CTXS][2]; |
| 319 | for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) |
| 320 | av1_cost_tokens_from_cdf(pred_flag_cost[i], segp->pred_cdf[i], NULL); |
| 321 | t_pred_cost = 0; |
| 322 | // Cost for signaling the prediction flag. |
| 323 | for (int i = 0; i < SEG_TEMPORAL_PRED_CTXS; ++i) { |
| 324 | for (int j = 0; j < 2; ++j) |
| 325 | t_pred_cost += temporal_predictor_count[i][j] * pred_flag_cost[i][j]; |
| 326 | } |
| 327 | // Cost for signaling the unpredicted segment id. |
| 328 | for (int i = 0; i < MAX_SEGMENTS; ++i) |
| 329 | t_pred_cost += t_unpred_seg_counts[i] * seg_id_cost[i]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Now choose which coding method to use. |
| 333 | if (t_pred_cost < no_pred_cost) { |
Urvang Joshi | b6409e9 | 2020-03-23 11:23:27 -0700 | [diff] [blame] | 334 | assert(!cm->features.error_resilient_mode); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 335 | seg->temporal_update = 1; |
| 336 | } else { |
| 337 | seg->temporal_update = 0; |
| 338 | } |
| 339 | } |
| 340 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 341 | void av1_reset_segment_features(AV1_COMMON *cm) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 342 | struct segmentation *seg = &cm->seg; |
| 343 | |
| 344 | // Set up default state for MB feature flags |
| 345 | seg->enabled = 0; |
| 346 | seg->update_map = 0; |
| 347 | seg->update_data = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 348 | av1_clearall_segfeatures(seg); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 349 | } |