1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_COMMON_RESIZE_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_COMMON_RESIZE_H_
14*77c1e3ccSAndroid Build Coastguard Worker
15*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/av1_common_int.h"
18*77c1e3ccSAndroid Build Coastguard Worker
19*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
20*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
21*77c1e3ccSAndroid Build Coastguard Worker #endif
22*77c1e3ccSAndroid Build Coastguard Worker
23*77c1e3ccSAndroid Build Coastguard Worker // Filters for factor of 2 downsampling.
24*77c1e3ccSAndroid Build Coastguard Worker static const int16_t av1_down2_symeven_half_filter[] = { 56, 12, -3, -1 };
25*77c1e3ccSAndroid Build Coastguard Worker static const int16_t av1_down2_symodd_half_filter[] = { 64, 35, 0, -3 };
26*77c1e3ccSAndroid Build Coastguard Worker
27*77c1e3ccSAndroid Build Coastguard Worker bool av1_resize_plane(const uint8_t *input, int height, int width,
28*77c1e3ccSAndroid Build Coastguard Worker int in_stride, uint8_t *output, int height2, int width2,
29*77c1e3ccSAndroid Build Coastguard Worker int out_stride);
30*77c1e3ccSAndroid Build Coastguard Worker
31*77c1e3ccSAndroid Build Coastguard Worker void av1_highbd_resize_plane(const uint8_t *input, int height, int width,
32*77c1e3ccSAndroid Build Coastguard Worker int in_stride, uint8_t *output, int height2,
33*77c1e3ccSAndroid Build Coastguard Worker int width2, int out_stride, int bd);
34*77c1e3ccSAndroid Build Coastguard Worker
35*77c1e3ccSAndroid Build Coastguard Worker void av1_upscale_normative_rows(const AV1_COMMON *cm, const uint8_t *src,
36*77c1e3ccSAndroid Build Coastguard Worker int src_stride, uint8_t *dst, int dst_stride,
37*77c1e3ccSAndroid Build Coastguard Worker int plane, int rows);
38*77c1e3ccSAndroid Build Coastguard Worker
39*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG *av1_realloc_and_scale_if_required(
40*77c1e3ccSAndroid Build Coastguard Worker AV1_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
41*77c1e3ccSAndroid Build Coastguard Worker const InterpFilter filter, const int phase, const bool use_optimized_scaler,
42*77c1e3ccSAndroid Build Coastguard Worker const bool for_psnr, const int border_in_pixels, const bool alloc_pyramid);
43*77c1e3ccSAndroid Build Coastguard Worker
44*77c1e3ccSAndroid Build Coastguard Worker bool av1_resize_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
45*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG *dst, int bd,
46*77c1e3ccSAndroid Build Coastguard Worker int num_planes);
47*77c1e3ccSAndroid Build Coastguard Worker
48*77c1e3ccSAndroid Build Coastguard Worker // Calculates the scaled dimensions from the given original dimensions and the
49*77c1e3ccSAndroid Build Coastguard Worker // resize scale denominator.
50*77c1e3ccSAndroid Build Coastguard Worker void av1_calculate_scaled_size(int *width, int *height, int resize_denom);
51*77c1e3ccSAndroid Build Coastguard Worker
52*77c1e3ccSAndroid Build Coastguard Worker // Similar to above, but calculates scaled dimensions after superres from the
53*77c1e3ccSAndroid Build Coastguard Worker // given original dimensions and superres scale denominator.
54*77c1e3ccSAndroid Build Coastguard Worker void av1_calculate_scaled_superres_size(int *width, int *height,
55*77c1e3ccSAndroid Build Coastguard Worker int superres_denom);
56*77c1e3ccSAndroid Build Coastguard Worker
57*77c1e3ccSAndroid Build Coastguard Worker // Inverse of av1_calculate_scaled_superres_size() above: calculates the
58*77c1e3ccSAndroid Build Coastguard Worker // original dimensions from the given scaled dimensions and the scale
59*77c1e3ccSAndroid Build Coastguard Worker // denominator.
60*77c1e3ccSAndroid Build Coastguard Worker void av1_calculate_unscaled_superres_size(int *width, int *height, int denom);
61*77c1e3ccSAndroid Build Coastguard Worker
62*77c1e3ccSAndroid Build Coastguard Worker void av1_superres_upscale(AV1_COMMON *cm, BufferPool *const pool,
63*77c1e3ccSAndroid Build Coastguard Worker bool alloc_pyramid);
64*77c1e3ccSAndroid Build Coastguard Worker
65*77c1e3ccSAndroid Build Coastguard Worker bool av1_resize_plane_to_half(const uint8_t *const input, int height, int width,
66*77c1e3ccSAndroid Build Coastguard Worker int in_stride, uint8_t *output, int height2,
67*77c1e3ccSAndroid Build Coastguard Worker int width2, int out_stride);
68*77c1e3ccSAndroid Build Coastguard Worker
69*77c1e3ccSAndroid Build Coastguard Worker void down2_symeven(const uint8_t *const input, int length, uint8_t *output,
70*77c1e3ccSAndroid Build Coastguard Worker int start_offset);
71*77c1e3ccSAndroid Build Coastguard Worker
72*77c1e3ccSAndroid Build Coastguard Worker bool should_resize_by_half(int height, int width, int height2, int width2);
73*77c1e3ccSAndroid Build Coastguard Worker
74*77c1e3ccSAndroid Build Coastguard Worker // Returns 1 if a superres upscaled frame is scaled and 0 otherwise.
av1_superres_scaled(const AV1_COMMON * cm)75*77c1e3ccSAndroid Build Coastguard Worker static inline int av1_superres_scaled(const AV1_COMMON *cm) {
76*77c1e3ccSAndroid Build Coastguard Worker // Note: for some corner cases (e.g. cm->width of 1), there may be no scaling
77*77c1e3ccSAndroid Build Coastguard Worker // required even though cm->superres_scale_denominator != SCALE_NUMERATOR.
78*77c1e3ccSAndroid Build Coastguard Worker // So, the following check is more accurate.
79*77c1e3ccSAndroid Build Coastguard Worker return (cm->width != cm->superres_upscaled_width);
80*77c1e3ccSAndroid Build Coastguard Worker }
81*77c1e3ccSAndroid Build Coastguard Worker
82*77c1e3ccSAndroid Build Coastguard Worker // The optimized scaler av1_resize_and_extend_frame() can only handle scaling
83*77c1e3ccSAndroid Build Coastguard Worker // ratios >= 1/4 and <= 16. See comment in aom_scaled_2d_c() for detail.
84*77c1e3ccSAndroid Build Coastguard Worker // Visual assessment shows that if the scaling ratio or its reciprocal is not a
85*77c1e3ccSAndroid Build Coastguard Worker // multiple of 1/16, there are some artifacts in the output of the optimized
86*77c1e3ccSAndroid Build Coastguard Worker // scaler, especially on lines, due to non-exact ratio representation. SSSE3
87*77c1e3ccSAndroid Build Coastguard Worker // and NEON have a specialized 3/4 version of av1_resize_and_extend_frame()
88*77c1e3ccSAndroid Build Coastguard Worker // that does not have this issue.
89*77c1e3ccSAndroid Build Coastguard Worker //
90*77c1e3ccSAndroid Build Coastguard Worker // Use the non-normative scaler av1_resize_and_extend_frame_nonnormative()
91*77c1e3ccSAndroid Build Coastguard Worker // for other scaling ratios.
av1_has_optimized_scaler(const int src_width,const int src_height,const int dst_width,const int dst_height)92*77c1e3ccSAndroid Build Coastguard Worker static inline bool av1_has_optimized_scaler(const int src_width,
93*77c1e3ccSAndroid Build Coastguard Worker const int src_height,
94*77c1e3ccSAndroid Build Coastguard Worker const int dst_width,
95*77c1e3ccSAndroid Build Coastguard Worker const int dst_height) {
96*77c1e3ccSAndroid Build Coastguard Worker bool has_optimized_scaler =
97*77c1e3ccSAndroid Build Coastguard Worker (dst_width * 4 >= src_width && dst_height * 4 >= src_height) &&
98*77c1e3ccSAndroid Build Coastguard Worker (dst_width <= src_width * 16 && dst_height <= src_height * 16) &&
99*77c1e3ccSAndroid Build Coastguard Worker (16 * dst_width % src_width == 0) && (16 * src_width % dst_width == 0) &&
100*77c1e3ccSAndroid Build Coastguard Worker (16 * dst_height % src_height == 0) &&
101*77c1e3ccSAndroid Build Coastguard Worker (16 * src_height % dst_height == 0);
102*77c1e3ccSAndroid Build Coastguard Worker #if HAVE_SSSE3 || HAVE_NEON
103*77c1e3ccSAndroid Build Coastguard Worker has_optimized_scaler =
104*77c1e3ccSAndroid Build Coastguard Worker has_optimized_scaler ||
105*77c1e3ccSAndroid Build Coastguard Worker (4 * dst_width == 3 * src_width && 4 * dst_height == 3 * src_height);
106*77c1e3ccSAndroid Build Coastguard Worker #endif
107*77c1e3ccSAndroid Build Coastguard Worker return has_optimized_scaler;
108*77c1e3ccSAndroid Build Coastguard Worker }
109*77c1e3ccSAndroid Build Coastguard Worker
110*77c1e3ccSAndroid Build Coastguard Worker #define UPSCALE_NORMATIVE_TAPS 8
111*77c1e3ccSAndroid Build Coastguard Worker extern const int16_t av1_resize_filter_normative[1 << RS_SUBPEL_BITS]
112*77c1e3ccSAndroid Build Coastguard Worker [UPSCALE_NORMATIVE_TAPS];
113*77c1e3ccSAndroid Build Coastguard Worker
114*77c1e3ccSAndroid Build Coastguard Worker int32_t av1_get_upscale_convolve_step(int in_length, int out_length);
115*77c1e3ccSAndroid Build Coastguard Worker
116*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
117*77c1e3ccSAndroid Build Coastguard Worker } // extern "C"
118*77c1e3ccSAndroid Build Coastguard Worker #endif
119*77c1e3ccSAndroid Build Coastguard Worker
120*77c1e3ccSAndroid Build Coastguard Worker #endif // AOM_AV1_COMMON_RESIZE_H_
121