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