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