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 | /**************************************************************************** |
| 14 | * |
| 15 | * Module Title : scale.c |
| 16 | * |
| 17 | * Description : Image scaling functions. |
| 18 | * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | /**************************************************************************** |
Johann | 123e8a6 | 2017-12-28 14:40:49 -0800 | [diff] [blame] | 22 | * Header Files |
| 23 | ****************************************************************************/ |
Tom Finegan | 44702c8 | 2018-05-22 13:00:39 -0700 | [diff] [blame] | 24 | #include "config/aom_scale_rtcd.h" |
| 25 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 26 | #include "aom_mem/aom_mem.h" |
| 27 | #include "aom_scale/aom_scale.h" |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 28 | #include "aom_scale/yv12config.h" |
| 29 | |
| 30 | typedef struct { |
| 31 | int expanded_frame_width; |
| 32 | int expanded_frame_height; |
| 33 | |
| 34 | int HScale; |
| 35 | int HRatio; |
| 36 | int VScale; |
| 37 | int VRatio; |
| 38 | |
| 39 | YV12_BUFFER_CONFIG *src_yuv_config; |
| 40 | YV12_BUFFER_CONFIG *dst_yuv_config; |
| 41 | |
| 42 | } SCALE_VARS; |
| 43 | |
| 44 | /**************************************************************************** |
| 45 | * |
| 46 | * ROUTINE : scale1d_2t1_i |
| 47 | * |
| 48 | * INPUTS : const unsigned char *source : Pointer to data to be scaled. |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 49 | * int source_step : Number of pixels to step on |
| 50 | * in source. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 51 | * unsigned int source_scale : Scale for source (UNUSED). |
| 52 | * unsigned int source_length : Length of source (UNUSED). |
| 53 | * unsigned char *dest : Pointer to output data array. |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 54 | * int dest_step : Number of pixels to step on |
| 55 | * in destination. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 56 | * unsigned int dest_scale : Scale for destination |
| 57 | * (UNUSED). |
| 58 | * unsigned int dest_length : Length of destination. |
| 59 | * |
| 60 | * OUTPUTS : None. |
| 61 | * |
| 62 | * RETURNS : void |
| 63 | * |
| 64 | * FUNCTION : Performs 2-to-1 interpolated scaling. |
| 65 | * |
| 66 | * SPECIAL NOTES : None. |
| 67 | * |
| 68 | ****************************************************************************/ |
| 69 | static void scale1d_2t1_i(const unsigned char *source, int source_step, |
| 70 | unsigned int source_scale, unsigned int source_length, |
| 71 | unsigned char *dest, int dest_step, |
| 72 | unsigned int dest_scale, unsigned int dest_length) { |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 73 | const unsigned char *const dest_end = dest + dest_length * dest_step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 74 | (void)source_length; |
| 75 | (void)source_scale; |
| 76 | (void)dest_scale; |
| 77 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 78 | source_step *= 2; // Every other row. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 79 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 80 | dest[0] = source[0]; // Special case: 1st pixel. |
| 81 | source += source_step; |
| 82 | dest += dest_step; |
| 83 | |
| 84 | while (dest < dest_end) { |
Angie Chiang | 10ab157 | 2016-10-24 09:34:53 -0700 | [diff] [blame] | 85 | const unsigned int a = 3 * source[-source_step]; |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 86 | const unsigned int b = 10 * source[0]; |
Angie Chiang | 10ab157 | 2016-10-24 09:34:53 -0700 | [diff] [blame] | 87 | const unsigned int c = 3 * source[source_step]; |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 88 | *dest = (unsigned char)((8 + a + b + c) >> 4); |
| 89 | source += source_step; |
| 90 | dest += dest_step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | /**************************************************************************** |
| 95 | * |
| 96 | * ROUTINE : scale1d_2t1_ps |
| 97 | * |
| 98 | * INPUTS : const unsigned char *source : Pointer to data to be scaled. |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 99 | * int source_step : Number of pixels to step on |
| 100 | * in source. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 101 | * unsigned int source_scale : Scale for source (UNUSED). |
| 102 | * unsigned int source_length : Length of source (UNUSED). |
| 103 | * unsigned char *dest : Pointer to output data array. |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 104 | * int dest_step : Number of pixels to step on |
| 105 | * in destination. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 106 | * unsigned int dest_scale : Scale for destination |
| 107 | * (UNUSED). |
| 108 | * unsigned int dest_length : Length of destination. |
| 109 | * |
| 110 | * OUTPUTS : None. |
| 111 | * |
| 112 | * RETURNS : void |
| 113 | * |
| 114 | * FUNCTION : Performs 2-to-1 point subsampled scaling. |
| 115 | * |
| 116 | * SPECIAL NOTES : None. |
| 117 | * |
| 118 | ****************************************************************************/ |
| 119 | static void scale1d_2t1_ps(const unsigned char *source, int source_step, |
| 120 | unsigned int source_scale, |
| 121 | unsigned int source_length, unsigned char *dest, |
| 122 | int dest_step, unsigned int dest_scale, |
| 123 | unsigned int dest_length) { |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 124 | const unsigned char *const dest_end = dest + dest_length * dest_step; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 125 | (void)source_length; |
| 126 | (void)source_scale; |
| 127 | (void)dest_scale; |
| 128 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 129 | source_step *= 2; // Every other row. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 130 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 131 | while (dest < dest_end) { |
| 132 | *dest = *source; |
| 133 | source += source_step; |
| 134 | dest += dest_step; |
| 135 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 136 | } |
| 137 | /**************************************************************************** |
| 138 | * |
| 139 | * ROUTINE : scale1d_c |
| 140 | * |
| 141 | * INPUTS : const unsigned char *source : Pointer to data to be scaled. |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 142 | * int source_step : Number of pixels to step on |
| 143 | * in source. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 144 | * unsigned int source_scale : Scale for source. |
| 145 | * unsigned int source_length : Length of source (UNUSED). |
| 146 | * unsigned char *dest : Pointer to output data array. |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 147 | * int dest_step : Number of pixels to step on |
| 148 | * in destination. |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 149 | * unsigned int dest_scale : Scale for destination. |
| 150 | * unsigned int dest_length : Length of destination. |
| 151 | * |
| 152 | * OUTPUTS : None. |
| 153 | * |
| 154 | * RETURNS : void |
| 155 | * |
| 156 | * FUNCTION : Performs linear interpolation in one dimension. |
| 157 | * |
| 158 | * SPECIAL NOTES : None. |
| 159 | * |
| 160 | ****************************************************************************/ |
| 161 | static void scale1d_c(const unsigned char *source, int source_step, |
| 162 | unsigned int source_scale, unsigned int source_length, |
| 163 | unsigned char *dest, int dest_step, |
| 164 | unsigned int dest_scale, unsigned int dest_length) { |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 165 | const unsigned char *const dest_end = dest + dest_length * dest_step; |
| 166 | const unsigned int round_value = dest_scale / 2; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 167 | unsigned int left_modifier = dest_scale; |
| 168 | unsigned int right_modifier = 0; |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 169 | unsigned char left_pixel = source[0]; |
| 170 | unsigned char right_pixel = source[source_step]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 171 | |
| 172 | (void)source_length; |
| 173 | |
| 174 | /* These asserts are needed if there are boundary issues... */ |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 175 | /* assert ( dest_scale > source_scale );*/ |
| 176 | /* assert ( (source_length - 1) * dest_scale >= (dest_length - 1) * |
| 177 | * source_scale);*/ |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 178 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 179 | while (dest < dest_end) { |
| 180 | *dest = (unsigned char)((left_modifier * left_pixel + |
| 181 | right_modifier * right_pixel + round_value) / |
| 182 | dest_scale); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 183 | |
| 184 | right_modifier += source_scale; |
| 185 | |
| 186 | while (right_modifier > dest_scale) { |
| 187 | right_modifier -= dest_scale; |
| 188 | source += source_step; |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 189 | left_pixel = source[0]; |
| 190 | right_pixel = source[source_step]; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | left_modifier = dest_scale - right_modifier; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /**************************************************************************** |
| 198 | * |
| 199 | * ROUTINE : Scale2D |
| 200 | * |
| 201 | * INPUTS : const unsigned char *source : Pointer to data to be |
| 202 | * scaled. |
| 203 | * int source_pitch : Stride of source image. |
| 204 | * unsigned int source_width : Width of input image. |
| 205 | * unsigned int source_height : Height of input image. |
| 206 | * unsigned char *dest : Pointer to output data |
| 207 | * array. |
| 208 | * int dest_pitch : Stride of destination |
| 209 | * image. |
| 210 | * unsigned int dest_width : Width of destination image. |
| 211 | * unsigned int dest_height : Height of destination |
| 212 | * image. |
| 213 | * unsigned char *temp_area : Pointer to temp work area. |
| 214 | * unsigned char temp_area_height : Height of temp work area. |
| 215 | * unsigned int hscale : Horizontal scale factor |
| 216 | * numerator. |
| 217 | * unsigned int hratio : Horizontal scale factor |
| 218 | * denominator. |
| 219 | * unsigned int vscale : Vertical scale factor |
| 220 | * numerator. |
| 221 | * unsigned int vratio : Vertical scale factor |
| 222 | * denominator. |
| 223 | * unsigned int interlaced : Interlace flag. |
| 224 | * |
| 225 | * OUTPUTS : None. |
| 226 | * |
| 227 | * RETURNS : void |
| 228 | * |
| 229 | * FUNCTION : Performs 2-tap linear interpolation in two dimensions. |
| 230 | * |
| 231 | * SPECIAL NOTES : Expansion is performed one band at a time to help with |
| 232 | * caching. |
| 233 | * |
| 234 | ****************************************************************************/ |
| 235 | static void Scale2D( |
| 236 | /*const*/ |
| 237 | unsigned char *source, int source_pitch, unsigned int source_width, |
| 238 | unsigned int source_height, unsigned char *dest, int dest_pitch, |
| 239 | unsigned int dest_width, unsigned int dest_height, unsigned char *temp_area, |
| 240 | unsigned char temp_area_height, unsigned int hscale, unsigned int hratio, |
| 241 | unsigned int vscale, unsigned int vratio, unsigned int interlaced) { |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 242 | unsigned int i, j, k; |
| 243 | unsigned int bands; |
| 244 | unsigned int dest_band_height; |
| 245 | unsigned int source_band_height; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 246 | |
| 247 | typedef void (*Scale1D)(const unsigned char *source, int source_step, |
| 248 | unsigned int source_scale, unsigned int source_length, |
| 249 | unsigned char *dest, int dest_step, |
| 250 | unsigned int dest_scale, unsigned int dest_length); |
| 251 | |
| 252 | Scale1D Scale1Dv = scale1d_c; |
| 253 | Scale1D Scale1Dh = scale1d_c; |
| 254 | |
| 255 | void (*horiz_line_scale)(const unsigned char *, unsigned int, unsigned char *, |
| 256 | unsigned int) = NULL; |
Urvang Joshi | d650f27 | 2016-10-25 13:00:22 -0700 | [diff] [blame] | 257 | void (*vert_band_scale)(unsigned char *, int, unsigned char *, int, |
| 258 | unsigned int) = NULL; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 259 | |
| 260 | int ratio_scalable = 1; |
| 261 | int interpolation = 0; |
| 262 | |
| 263 | unsigned char *source_base; |
| 264 | unsigned char *line_src; |
| 265 | |
| 266 | source_base = (unsigned char *)source; |
| 267 | |
| 268 | if (source_pitch < 0) { |
| 269 | int offset; |
| 270 | |
| 271 | offset = (source_height - 1); |
| 272 | offset *= source_pitch; |
| 273 | |
| 274 | source_base += offset; |
| 275 | } |
| 276 | |
| 277 | /* find out the ratio for each direction */ |
| 278 | switch (hratio * 10 / hscale) { |
| 279 | case 8: |
| 280 | /* 4-5 Scale in Width direction */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 281 | horiz_line_scale = aom_horizontal_line_5_4_scale; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 282 | break; |
| 283 | case 6: |
| 284 | /* 3-5 Scale in Width direction */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 285 | horiz_line_scale = aom_horizontal_line_5_3_scale; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 286 | break; |
| 287 | case 5: |
| 288 | /* 1-2 Scale in Width direction */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 289 | horiz_line_scale = aom_horizontal_line_2_1_scale; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 290 | break; |
| 291 | default: |
| 292 | /* The ratio is not acceptable now */ |
| 293 | /* throw("The ratio is not acceptable for now!"); */ |
| 294 | ratio_scalable = 0; |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | switch (vratio * 10 / vscale) { |
| 299 | case 8: |
| 300 | /* 4-5 Scale in vertical direction */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 301 | vert_band_scale = aom_vertical_band_5_4_scale; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 302 | source_band_height = 5; |
| 303 | dest_band_height = 4; |
| 304 | break; |
| 305 | case 6: |
| 306 | /* 3-5 Scale in vertical direction */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 307 | vert_band_scale = aom_vertical_band_5_3_scale; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 308 | source_band_height = 5; |
| 309 | dest_band_height = 3; |
| 310 | break; |
| 311 | case 5: |
| 312 | /* 1-2 Scale in vertical direction */ |
| 313 | |
| 314 | if (interlaced) { |
| 315 | /* if the content is interlaced, point sampling is used */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 316 | vert_band_scale = aom_vertical_band_2_1_scale; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 317 | } else { |
| 318 | interpolation = 1; |
| 319 | /* if the content is progressive, interplo */ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 320 | vert_band_scale = aom_vertical_band_2_1_scale_i; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | source_band_height = 2; |
| 324 | dest_band_height = 1; |
| 325 | break; |
| 326 | default: |
| 327 | /* The ratio is not acceptable now */ |
| 328 | /* throw("The ratio is not acceptable for now!"); */ |
| 329 | ratio_scalable = 0; |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | if (ratio_scalable) { |
| 334 | if (source_height == dest_height) { |
| 335 | /* for each band of the image */ |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 336 | for (k = 0; k < dest_height; ++k) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 337 | horiz_line_scale(source, source_width, dest, dest_width); |
| 338 | source += source_pitch; |
| 339 | dest += dest_pitch; |
| 340 | } |
| 341 | |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (interpolation) { |
| 346 | if (source < source_base) source = source_base; |
| 347 | |
| 348 | horiz_line_scale(source, source_width, temp_area, dest_width); |
| 349 | } |
| 350 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 351 | for (k = 0; k < (dest_height + dest_band_height - 1) / dest_band_height; |
| 352 | ++k) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 353 | /* scale one band horizontally */ |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 354 | for (i = 0; i < source_band_height; ++i) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 355 | /* Trap case where we could read off the base of the source buffer */ |
| 356 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 357 | line_src = source + i * source_pitch; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 358 | |
| 359 | if (line_src < source_base) line_src = source_base; |
| 360 | |
| 361 | horiz_line_scale(line_src, source_width, |
| 362 | temp_area + (i + 1) * dest_pitch, dest_width); |
| 363 | } |
| 364 | |
| 365 | /* Vertical scaling is in place */ |
| 366 | vert_band_scale(temp_area + dest_pitch, dest_pitch, dest, dest_pitch, |
| 367 | dest_width); |
| 368 | |
| 369 | if (interpolation) |
| 370 | memcpy(temp_area, temp_area + source_band_height * dest_pitch, |
| 371 | dest_width); |
| 372 | |
| 373 | /* Next band... */ |
| 374 | source += (unsigned long)source_band_height * source_pitch; |
| 375 | dest += (unsigned long)dest_band_height * dest_pitch; |
| 376 | } |
| 377 | |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | if (hscale == 2 && hratio == 1) Scale1Dh = scale1d_2t1_ps; |
| 382 | |
| 383 | if (vscale == 2 && vratio == 1) { |
| 384 | if (interlaced) |
| 385 | Scale1Dv = scale1d_2t1_ps; |
| 386 | else |
| 387 | Scale1Dv = scale1d_2t1_i; |
| 388 | } |
| 389 | |
| 390 | if (source_height == dest_height) { |
| 391 | /* for each band of the image */ |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 392 | for (k = 0; k < dest_height; ++k) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 393 | Scale1Dh(source, 1, hscale, source_width + 1, dest, 1, hratio, |
| 394 | dest_width); |
| 395 | source += source_pitch; |
| 396 | dest += dest_pitch; |
| 397 | } |
| 398 | |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | if (dest_height > source_height) { |
| 403 | dest_band_height = temp_area_height - 1; |
| 404 | source_band_height = dest_band_height * source_height / dest_height; |
| 405 | } else { |
| 406 | source_band_height = temp_area_height - 1; |
| 407 | dest_band_height = source_band_height * vratio / vscale; |
| 408 | } |
| 409 | |
| 410 | /* first row needs to be done so that we can stay one row ahead for vertical |
| 411 | * zoom */ |
| 412 | Scale1Dh(source, 1, hscale, source_width + 1, temp_area, 1, hratio, |
| 413 | dest_width); |
| 414 | |
| 415 | /* for each band of the image */ |
| 416 | bands = (dest_height + dest_band_height - 1) / dest_band_height; |
| 417 | |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 418 | for (k = 0; k < bands; ++k) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 419 | /* scale one band horizontally */ |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 420 | for (i = 1; i < source_band_height + 1; ++i) { |
| 421 | if (k * source_band_height + i < source_height) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 422 | Scale1Dh(source + i * source_pitch, 1, hscale, source_width + 1, |
| 423 | temp_area + i * dest_pitch, 1, hratio, dest_width); |
| 424 | } else { /* Duplicate the last row */ |
| 425 | /* copy temp_area row 0 over from last row in the past */ |
| 426 | memcpy(temp_area + i * dest_pitch, temp_area + (i - 1) * dest_pitch, |
| 427 | dest_pitch); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* scale one band vertically */ |
Urvang Joshi | b42827f | 2016-07-15 15:58:25 -0700 | [diff] [blame] | 432 | for (j = 0; j < dest_width; ++j) { |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 433 | Scale1Dv(&temp_area[j], dest_pitch, vscale, source_band_height + 1, |
| 434 | &dest[j], dest_pitch, vratio, dest_band_height); |
| 435 | } |
| 436 | |
| 437 | /* copy temp_area row 0 over from last row in the past */ |
| 438 | memcpy(temp_area, temp_area + source_band_height * dest_pitch, dest_pitch); |
| 439 | |
| 440 | /* move to the next band */ |
| 441 | source += source_band_height * source_pitch; |
| 442 | dest += dest_band_height * dest_pitch; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /**************************************************************************** |
| 447 | * |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 448 | * ROUTINE : aom_scale_frame |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 449 | * |
| 450 | * INPUTS : YV12_BUFFER_CONFIG *src : Pointer to frame to be |
| 451 | * scaled. |
| 452 | * YV12_BUFFER_CONFIG *dst : Pointer to buffer to hold |
| 453 | * scaled frame. |
| 454 | * unsigned char *temp_area : Pointer to temp work area. |
| 455 | * unsigned char temp_area_height : Height of temp work area. |
| 456 | * unsigned int hscale : Horizontal scale factor |
| 457 | * numerator. |
| 458 | * unsigned int hratio : Horizontal scale factor |
| 459 | * denominator. |
| 460 | * unsigned int vscale : Vertical scale factor |
| 461 | * numerator. |
| 462 | * unsigned int vratio : Vertical scale factor |
| 463 | * denominator. |
| 464 | * unsigned int interlaced : Interlace flag. |
| 465 | * |
| 466 | * OUTPUTS : None. |
| 467 | * |
| 468 | * RETURNS : void |
| 469 | * |
| 470 | * FUNCTION : Performs 2-tap linear interpolation in two dimensions. |
| 471 | * |
| 472 | * SPECIAL NOTES : Expansion is performed one band at a time to help with |
| 473 | * caching. |
| 474 | * |
| 475 | ****************************************************************************/ |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 476 | void aom_scale_frame(YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst, |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 477 | unsigned char *temp_area, unsigned char temp_height, |
| 478 | unsigned int hscale, unsigned int hratio, |
| 479 | unsigned int vscale, unsigned int vratio, |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 480 | unsigned int interlaced, const int num_planes) { |
Rupert Swarbrick | 82529d2 | 2017-09-20 13:36:39 +0100 | [diff] [blame] | 481 | const int dw = (hscale - 1 + src->y_width * hratio) / hscale; |
| 482 | const int dh = (vscale - 1 + src->y_height * vratio) / vscale; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 483 | |
Imdad Sardharwalla | af8e264 | 2018-01-19 11:46:34 +0000 | [diff] [blame] | 484 | for (int plane = 0; plane < num_planes; ++plane) { |
Rupert Swarbrick | 82529d2 | 2017-09-20 13:36:39 +0100 | [diff] [blame] | 485 | const int is_uv = plane > 0; |
| 486 | const int plane_dw = dw >> is_uv; |
| 487 | const int plane_dh = dh >> is_uv; |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 488 | |
Rupert Swarbrick | 82529d2 | 2017-09-20 13:36:39 +0100 | [diff] [blame] | 489 | Scale2D((unsigned char *)src->buffers[plane], src->strides[is_uv], |
| 490 | src->widths[is_uv], src->heights[is_uv], |
| 491 | (unsigned char *)dst->buffers[plane], dst->strides[is_uv], plane_dw, |
| 492 | plane_dh, temp_area, temp_height, hscale, hratio, vscale, vratio, |
| 493 | interlaced); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 494 | |
Rupert Swarbrick | 82529d2 | 2017-09-20 13:36:39 +0100 | [diff] [blame] | 495 | if (plane_dw < dst->widths[is_uv]) |
| 496 | for (int i = 0; i < plane_dh; ++i) |
| 497 | memset(dst->buffers[plane] + i * dst->strides[is_uv] + plane_dw - 1, |
| 498 | dst->buffers[plane][i * dst->strides[is_uv] + plane_dw - 2], |
| 499 | dst->widths[is_uv] - plane_dw + 1); |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 500 | |
Rupert Swarbrick | 82529d2 | 2017-09-20 13:36:39 +0100 | [diff] [blame] | 501 | if (plane_dh < dst->heights[is_uv]) |
| 502 | for (int i = plane_dh - 1; i < dst->heights[is_uv]; ++i) |
| 503 | memcpy(dst->buffers[plane] + i * dst->strides[is_uv], |
| 504 | dst->buffers[plane] + (plane_dh - 2) * dst->strides[is_uv], |
| 505 | dst->widths[is_uv] + 1); |
| 506 | } |
Yaowu Xu | c27fc14 | 2016-08-22 16:08:15 -0700 | [diff] [blame] | 507 | } |