1 /*
2 * Copyright (c) 2016, 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
12 #include "av1/common/tile_common.h"
13 #include "av1/common/onyxc_int.h"
14 #include "av1/common/resize.h"
15 #include "aom_dsp/aom_dsp_common.h"
16
av1_tile_init(TileInfo * tile,const AV1_COMMON * cm,int row,int col)17 void av1_tile_init(TileInfo *tile, const AV1_COMMON *cm, int row, int col) {
18 av1_tile_set_row(tile, cm, row);
19 av1_tile_set_col(tile, cm, col);
20 }
21
22 // Find smallest k>=0 such that (blk_size << k) >= target
tile_log2(int blk_size,int target)23 static int tile_log2(int blk_size, int target) {
24 int k;
25 for (k = 0; (blk_size << k) < target; k++) {
26 }
27 return k;
28 }
29
av1_get_tile_limits(AV1_COMMON * const cm)30 void av1_get_tile_limits(AV1_COMMON *const cm) {
31 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
32 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
33 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
34 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
35
36 int sb_size_log2 = cm->seq_params.mib_size_log2 + MI_SIZE_LOG2;
37 cm->max_tile_width_sb = MAX_TILE_WIDTH >> sb_size_log2;
38 int max_tile_area_sb = MAX_TILE_AREA >> (2 * sb_size_log2);
39
40 cm->min_log2_tile_cols = tile_log2(cm->max_tile_width_sb, sb_cols);
41 cm->max_log2_tile_cols = tile_log2(1, AOMMIN(sb_cols, MAX_TILE_COLS));
42 cm->max_log2_tile_rows = tile_log2(1, AOMMIN(sb_rows, MAX_TILE_ROWS));
43 cm->min_log2_tiles = tile_log2(max_tile_area_sb, sb_cols * sb_rows);
44 cm->min_log2_tiles = AOMMAX(cm->min_log2_tiles, cm->min_log2_tile_cols);
45 }
46
av1_calculate_tile_cols(AV1_COMMON * const cm)47 void av1_calculate_tile_cols(AV1_COMMON *const cm) {
48 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
49 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
50 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
51 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
52 int i;
53
54 // This will be overridden if there is at least two columns of tiles
55 // (otherwise there is no inner tile width)
56 cm->min_inner_tile_width = -1;
57
58 if (cm->uniform_tile_spacing_flag) {
59 int start_sb;
60 int size_sb = ALIGN_POWER_OF_TWO(sb_cols, cm->log2_tile_cols);
61 size_sb >>= cm->log2_tile_cols;
62 assert(size_sb > 0);
63 for (i = 0, start_sb = 0; start_sb < sb_cols; i++) {
64 cm->tile_col_start_sb[i] = start_sb;
65 start_sb += size_sb;
66 }
67 cm->tile_cols = i;
68 cm->tile_col_start_sb[i] = sb_cols;
69 cm->min_log2_tile_rows = AOMMAX(cm->min_log2_tiles - cm->log2_tile_cols, 0);
70 cm->max_tile_height_sb = sb_rows >> cm->min_log2_tile_rows;
71
72 cm->tile_width = size_sb << cm->seq_params.mib_size_log2;
73 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
74 if (cm->tile_cols > 1) {
75 cm->min_inner_tile_width = cm->tile_width;
76 }
77 } else {
78 int max_tile_area_sb = (sb_rows * sb_cols);
79 int widest_tile_sb = 1;
80 int narrowest_inner_tile_sb = 65536;
81 cm->log2_tile_cols = tile_log2(1, cm->tile_cols);
82 for (i = 0; i < cm->tile_cols; i++) {
83 int size_sb = cm->tile_col_start_sb[i + 1] - cm->tile_col_start_sb[i];
84 widest_tile_sb = AOMMAX(widest_tile_sb, size_sb);
85 // ignore the rightmost tile in frame for determining the narrowest
86 if (i < cm->tile_cols - 1)
87 narrowest_inner_tile_sb = AOMMIN(narrowest_inner_tile_sb, size_sb);
88 }
89 if (cm->min_log2_tiles) {
90 max_tile_area_sb >>= (cm->min_log2_tiles + 1);
91 }
92 cm->max_tile_height_sb = AOMMAX(max_tile_area_sb / widest_tile_sb, 1);
93 if (cm->tile_cols > 1) {
94 cm->min_inner_tile_width = narrowest_inner_tile_sb
95 << cm->seq_params.mib_size_log2;
96 }
97 }
98 }
99
av1_calculate_tile_rows(AV1_COMMON * const cm)100 void av1_calculate_tile_rows(AV1_COMMON *const cm) {
101 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
102 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
103 int start_sb, size_sb, i;
104
105 if (cm->uniform_tile_spacing_flag) {
106 size_sb = ALIGN_POWER_OF_TWO(sb_rows, cm->log2_tile_rows);
107 size_sb >>= cm->log2_tile_rows;
108 assert(size_sb > 0);
109 for (i = 0, start_sb = 0; start_sb < sb_rows; i++) {
110 cm->tile_row_start_sb[i] = start_sb;
111 start_sb += size_sb;
112 }
113 cm->tile_rows = i;
114 cm->tile_row_start_sb[i] = sb_rows;
115
116 cm->tile_height = size_sb << cm->seq_params.mib_size_log2;
117 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
118 } else {
119 cm->log2_tile_rows = tile_log2(1, cm->tile_rows);
120 }
121 }
122
av1_tile_set_row(TileInfo * tile,const AV1_COMMON * cm,int row)123 void av1_tile_set_row(TileInfo *tile, const AV1_COMMON *cm, int row) {
124 assert(row < cm->tile_rows);
125 int mi_row_start = cm->tile_row_start_sb[row] << cm->seq_params.mib_size_log2;
126 int mi_row_end = cm->tile_row_start_sb[row + 1]
127 << cm->seq_params.mib_size_log2;
128 tile->tile_row = row;
129 tile->mi_row_start = mi_row_start;
130 tile->mi_row_end = AOMMIN(mi_row_end, cm->mi_rows);
131 assert(tile->mi_row_end > tile->mi_row_start);
132 }
133
av1_tile_set_col(TileInfo * tile,const AV1_COMMON * cm,int col)134 void av1_tile_set_col(TileInfo *tile, const AV1_COMMON *cm, int col) {
135 assert(col < cm->tile_cols);
136 int mi_col_start = cm->tile_col_start_sb[col] << cm->seq_params.mib_size_log2;
137 int mi_col_end = cm->tile_col_start_sb[col + 1]
138 << cm->seq_params.mib_size_log2;
139 tile->tile_col = col;
140 tile->mi_col_start = mi_col_start;
141 tile->mi_col_end = AOMMIN(mi_col_end, cm->mi_cols);
142 assert(tile->mi_col_end > tile->mi_col_start);
143 }
144
av1_get_sb_rows_in_tile(AV1_COMMON * cm,TileInfo tile)145 int av1_get_sb_rows_in_tile(AV1_COMMON *cm, TileInfo tile) {
146 int mi_rows_aligned_to_sb = ALIGN_POWER_OF_TWO(
147 tile.mi_row_end - tile.mi_row_start, cm->seq_params.mib_size_log2);
148 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
149
150 return sb_rows;
151 }
152
av1_get_sb_cols_in_tile(AV1_COMMON * cm,TileInfo tile)153 int av1_get_sb_cols_in_tile(AV1_COMMON *cm, TileInfo tile) {
154 int mi_cols_aligned_to_sb = ALIGN_POWER_OF_TWO(
155 tile.mi_col_end - tile.mi_col_start, cm->seq_params.mib_size_log2);
156 int sb_cols = mi_cols_aligned_to_sb >> cm->seq_params.mib_size_log2;
157
158 return sb_cols;
159 }
160
av1_get_tile_rect(const TileInfo * tile_info,const AV1_COMMON * cm,int is_uv)161 AV1PixelRect av1_get_tile_rect(const TileInfo *tile_info, const AV1_COMMON *cm,
162 int is_uv) {
163 AV1PixelRect r;
164
165 // Calculate position in the Y plane
166 r.left = tile_info->mi_col_start * MI_SIZE;
167 r.right = tile_info->mi_col_end * MI_SIZE;
168 r.top = tile_info->mi_row_start * MI_SIZE;
169 r.bottom = tile_info->mi_row_end * MI_SIZE;
170
171 // If upscaling is enabled, the tile limits need scaling to match the
172 // upscaled frame where the restoration units live. To do this, scale up the
173 // top-left and bottom-right of the tile.
174 if (av1_superres_scaled(cm)) {
175 av1_calculate_unscaled_superres_size(&r.left, &r.top,
176 cm->superres_scale_denominator);
177 av1_calculate_unscaled_superres_size(&r.right, &r.bottom,
178 cm->superres_scale_denominator);
179 }
180
181 const int frame_w = cm->superres_upscaled_width;
182 const int frame_h = cm->superres_upscaled_height;
183
184 // Make sure we don't fall off the bottom-right of the frame.
185 r.right = AOMMIN(r.right, frame_w);
186 r.bottom = AOMMIN(r.bottom, frame_h);
187
188 // Convert to coordinates in the appropriate plane
189 const int ss_x = is_uv && cm->seq_params.subsampling_x;
190 const int ss_y = is_uv && cm->seq_params.subsampling_y;
191
192 r.left = ROUND_POWER_OF_TWO(r.left, ss_x);
193 r.right = ROUND_POWER_OF_TWO(r.right, ss_x);
194 r.top = ROUND_POWER_OF_TWO(r.top, ss_y);
195 r.bottom = ROUND_POWER_OF_TWO(r.bottom, ss_y);
196
197 return r;
198 }
199
av1_get_uniform_tile_size(const AV1_COMMON * cm,int * w,int * h)200 void av1_get_uniform_tile_size(const AV1_COMMON *cm, int *w, int *h) {
201 if (cm->uniform_tile_spacing_flag) {
202 *w = cm->tile_width;
203 *h = cm->tile_height;
204 } else {
205 for (int i = 0; i < cm->tile_cols; ++i) {
206 const int tile_width_sb =
207 cm->tile_col_start_sb[i + 1] - cm->tile_col_start_sb[i];
208 const int tile_w = tile_width_sb * cm->seq_params.mib_size;
209 assert(i == 0 || tile_w == *w); // ensure all tiles have same dimension
210 *w = tile_w;
211 }
212
213 for (int i = 0; i < cm->tile_rows; ++i) {
214 const int tile_height_sb =
215 cm->tile_row_start_sb[i + 1] - cm->tile_row_start_sb[i];
216 const int tile_h = tile_height_sb * cm->seq_params.mib_size;
217 assert(i == 0 || tile_h == *h); // ensure all tiles have same dimension
218 *h = tile_h;
219 }
220 }
221 }
222
is_min_tile_width_satisfied(const AV1_COMMON * cm)223 int is_min_tile_width_satisfied(const AV1_COMMON *cm) {
224 // Disable check if there is a single tile col in the frame
225 if (cm->tile_cols == 1) return 1;
226
227 return ((cm->min_inner_tile_width << MI_SIZE_LOG2) >=
228 (64 << av1_superres_scaled(cm)));
229 }
230