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