blob: 09960a34d2e0bb55e7ba66588e287193457d6be2 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Vibhoothi41c6dd72021-10-12 18:48:26 +00004 * 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 Xuc27fc142016-08-22 16:08:15 -070011 */
12
13/****************************************************************************
14 *
15 * Module Title : scale.c
16 *
17 * Description : Image scaling functions.
18 *
19 ***************************************************************************/
20
21/****************************************************************************
Johann123e8a62017-12-28 14:40:49 -080022 * Header Files
23 ****************************************************************************/
Tom Finegan44702c82018-05-22 13:00:39 -070024#include "config/aom_scale_rtcd.h"
25
Yaowu Xuf883b422016-08-30 14:01:10 -070026#include "aom_mem/aom_mem.h"
27#include "aom_scale/aom_scale.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070028#include "aom_scale/yv12config.h"
29
30typedef 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 Xu9c01aa12016-09-01 14:32:49 -070049 * int source_step : Number of pixels to step on
50 * in source.
Yaowu Xuc27fc142016-08-22 16:08:15 -070051 * 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 Xu9c01aa12016-09-01 14:32:49 -070054 * int dest_step : Number of pixels to step on
55 * in destination.
Yaowu Xuc27fc142016-08-22 16:08:15 -070056 * 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 ****************************************************************************/
69static 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 Joshib42827f2016-07-15 15:58:25 -070073 const unsigned char *const dest_end = dest + dest_length * dest_step;
Yaowu Xuc27fc142016-08-22 16:08:15 -070074 (void)source_length;
75 (void)source_scale;
76 (void)dest_scale;
77
Urvang Joshib42827f2016-07-15 15:58:25 -070078 source_step *= 2; // Every other row.
Yaowu Xuc27fc142016-08-22 16:08:15 -070079
Urvang Joshib42827f2016-07-15 15:58:25 -070080 dest[0] = source[0]; // Special case: 1st pixel.
81 source += source_step;
82 dest += dest_step;
83
84 while (dest < dest_end) {
Angie Chiang10ab1572016-10-24 09:34:53 -070085 const unsigned int a = 3 * source[-source_step];
Urvang Joshib42827f2016-07-15 15:58:25 -070086 const unsigned int b = 10 * source[0];
Angie Chiang10ab1572016-10-24 09:34:53 -070087 const unsigned int c = 3 * source[source_step];
Urvang Joshib42827f2016-07-15 15:58:25 -070088 *dest = (unsigned char)((8 + a + b + c) >> 4);
89 source += source_step;
90 dest += dest_step;
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 }
92}
93
94/****************************************************************************
95 *
96 * ROUTINE : scale1d_2t1_ps
97 *
98 * INPUTS : const unsigned char *source : Pointer to data to be scaled.
Yaowu Xu9c01aa12016-09-01 14:32:49 -070099 * int source_step : Number of pixels to step on
100 * in source.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101 * 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 Xu9c01aa12016-09-01 14:32:49 -0700104 * int dest_step : Number of pixels to step on
105 * in destination.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700106 * 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 ****************************************************************************/
119static 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 Joshib42827f2016-07-15 15:58:25 -0700124 const unsigned char *const dest_end = dest + dest_length * dest_step;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 (void)source_length;
126 (void)source_scale;
127 (void)dest_scale;
128
Urvang Joshib42827f2016-07-15 15:58:25 -0700129 source_step *= 2; // Every other row.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130
Urvang Joshib42827f2016-07-15 15:58:25 -0700131 while (dest < dest_end) {
132 *dest = *source;
133 source += source_step;
134 dest += dest_step;
135 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136}
137/****************************************************************************
138 *
139 * ROUTINE : scale1d_c
140 *
141 * INPUTS : const unsigned char *source : Pointer to data to be scaled.
Yaowu Xu9c01aa12016-09-01 14:32:49 -0700142 * int source_step : Number of pixels to step on
143 * in source.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700144 * 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 Xu9c01aa12016-09-01 14:32:49 -0700147 * int dest_step : Number of pixels to step on
148 * in destination.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149 * 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 ****************************************************************************/
161static 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 Joshib42827f2016-07-15 15:58:25 -0700165 const unsigned char *const dest_end = dest + dest_length * dest_step;
166 const unsigned int round_value = dest_scale / 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700167 unsigned int left_modifier = dest_scale;
168 unsigned int right_modifier = 0;
Urvang Joshib42827f2016-07-15 15:58:25 -0700169 unsigned char left_pixel = source[0];
170 unsigned char right_pixel = source[source_step];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171
172 (void)source_length;
173
174 /* These asserts are needed if there are boundary issues... */
Yaowu Xu9c01aa12016-09-01 14:32:49 -0700175 /* assert ( dest_scale > source_scale );*/
176 /* assert ( (source_length - 1) * dest_scale >= (dest_length - 1) *
177 * source_scale);*/
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178
Urvang Joshib42827f2016-07-15 15:58:25 -0700179 while (dest < dest_end) {
180 *dest = (unsigned char)((left_modifier * left_pixel +
181 right_modifier * right_pixel + round_value) /
182 dest_scale);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183
184 right_modifier += source_scale;
185
186 while (right_modifier > dest_scale) {
187 right_modifier -= dest_scale;
188 source += source_step;
Urvang Joshib42827f2016-07-15 15:58:25 -0700189 left_pixel = source[0];
190 right_pixel = source[source_step];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191 }
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 ****************************************************************************/
235static 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 Joshib42827f2016-07-15 15:58:25 -0700242 unsigned int i, j, k;
243 unsigned int bands;
244 unsigned int dest_band_height;
245 unsigned int source_band_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246
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 Joshid650f272016-10-25 13:00:22 -0700257 void (*vert_band_scale)(unsigned char *, int, unsigned char *, int,
258 unsigned int) = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259
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 Xuf883b422016-08-30 14:01:10 -0700281 horiz_line_scale = aom_horizontal_line_5_4_scale;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700282 break;
283 case 6:
284 /* 3-5 Scale in Width direction */
Yaowu Xuf883b422016-08-30 14:01:10 -0700285 horiz_line_scale = aom_horizontal_line_5_3_scale;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700286 break;
287 case 5:
288 /* 1-2 Scale in Width direction */
Yaowu Xuf883b422016-08-30 14:01:10 -0700289 horiz_line_scale = aom_horizontal_line_2_1_scale;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 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 Xuf883b422016-08-30 14:01:10 -0700301 vert_band_scale = aom_vertical_band_5_4_scale;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700302 source_band_height = 5;
303 dest_band_height = 4;
304 break;
305 case 6:
306 /* 3-5 Scale in vertical direction */
Yaowu Xuf883b422016-08-30 14:01:10 -0700307 vert_band_scale = aom_vertical_band_5_3_scale;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308 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 Xuf883b422016-08-30 14:01:10 -0700316 vert_band_scale = aom_vertical_band_2_1_scale;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700317 } else {
318 interpolation = 1;
319 /* if the content is progressive, interplo */
Yaowu Xuf883b422016-08-30 14:01:10 -0700320 vert_band_scale = aom_vertical_band_2_1_scale_i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321 }
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 Joshib42827f2016-07-15 15:58:25 -0700336 for (k = 0; k < dest_height; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700337 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 Joshib42827f2016-07-15 15:58:25 -0700351 for (k = 0; k < (dest_height + dest_band_height - 1) / dest_band_height;
352 ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700353 /* scale one band horizontally */
Urvang Joshib42827f2016-07-15 15:58:25 -0700354 for (i = 0; i < source_band_height; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700355 /* Trap case where we could read off the base of the source buffer */
356
Urvang Joshib42827f2016-07-15 15:58:25 -0700357 line_src = source + i * source_pitch;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700358
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 Joshib42827f2016-07-15 15:58:25 -0700392 for (k = 0; k < dest_height; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393 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 Joshib42827f2016-07-15 15:58:25 -0700418 for (k = 0; k < bands; ++k) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 /* scale one band horizontally */
Urvang Joshib42827f2016-07-15 15:58:25 -0700420 for (i = 1; i < source_band_height + 1; ++i) {
421 if (k * source_band_height + i < source_height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422 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 Joshib42827f2016-07-15 15:58:25 -0700432 for (j = 0; j < dest_width; ++j) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433 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 Xuf883b422016-08-30 14:01:10 -0700448 * ROUTINE : aom_scale_frame
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449 *
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 Xuf883b422016-08-30 14:01:10 -0700476void aom_scale_frame(YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 unsigned char *temp_area, unsigned char temp_height,
478 unsigned int hscale, unsigned int hratio,
479 unsigned int vscale, unsigned int vratio,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000480 unsigned int interlaced, const int num_planes) {
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100481 const int dw = (hscale - 1 + src->y_width * hratio) / hscale;
482 const int dh = (vscale - 1 + src->y_height * vratio) / vscale;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000484 for (int plane = 0; plane < num_planes; ++plane) {
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100485 const int is_uv = plane > 0;
486 const int plane_dw = dw >> is_uv;
487 const int plane_dh = dh >> is_uv;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100489 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 Xuc27fc142016-08-22 16:08:15 -0700494
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100495 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 Xuc27fc142016-08-22 16:08:15 -0700500
Rupert Swarbrick82529d22017-09-20 13:36:39 +0100501 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 Xuc27fc142016-08-22 16:08:15 -0700507}