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_CONVOLVE_H_
13 #define AOM_AV1_COMMON_CONVOLVE_H_
14 #include "av1/common/filter.h"
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 typedef uint16_t CONV_BUF_TYPE;
21 typedef struct ConvolveParams {
22 int do_average;
23 CONV_BUF_TYPE *dst;
24 int dst_stride;
25 int round_0;
26 int round_1;
27 int plane;
28 int is_compound;
29 int use_dist_wtd_comp_avg;
30 int fwd_offset;
31 int bck_offset;
32 } ConvolveParams;
33
34 #define ROUND0_BITS 3
35 #define COMPOUND_ROUND1_BITS 7
36 #define WIENER_ROUND0_BITS 3
37
38 #define WIENER_CLAMP_LIMIT(r0, bd) (1 << ((bd) + 1 + FILTER_BITS - r0))
39
40 typedef void (*aom_convolve_fn_t)(const uint8_t *src, int src_stride,
41 uint8_t *dst, int dst_stride, int w, int h,
42 const InterpFilterParams *filter_params_x,
43 const InterpFilterParams *filter_params_y,
44 const int subpel_x_qn, const int subpel_y_qn,
45 ConvolveParams *conv_params);
46
47 typedef void (*aom_highbd_convolve_fn_t)(
48 const uint16_t *src, int src_stride, uint16_t *dst, int dst_stride, int w,
49 int h, const InterpFilterParams *filter_params_x,
50 const InterpFilterParams *filter_params_y, const int subpel_x_qn,
51 const int subpel_y_qn, ConvolveParams *conv_params, int bd);
52
53 struct AV1Common;
54 struct scale_factors;
55
56 void av1_convolve_2d_facade(const uint8_t *src, int src_stride, uint8_t *dst,
57 int dst_stride, int w, int h,
58 const InterpFilterParams *interp_filters[2],
59 const int subpel_x_qn, int x_step_q4,
60 const int subpel_y_qn, int y_step_q4, int scaled,
61 ConvolveParams *conv_params);
62
get_conv_params_no_round(int cmp_index,int plane,CONV_BUF_TYPE * dst,int dst_stride,int is_compound,int bd)63 static INLINE ConvolveParams get_conv_params_no_round(int cmp_index, int plane,
64 CONV_BUF_TYPE *dst,
65 int dst_stride,
66 int is_compound, int bd) {
67 ConvolveParams conv_params;
68 assert(IMPLIES(cmp_index, is_compound));
69
70 conv_params.is_compound = is_compound;
71 conv_params.use_dist_wtd_comp_avg = 0;
72 conv_params.round_0 = ROUND0_BITS;
73 conv_params.round_1 = is_compound ? COMPOUND_ROUND1_BITS
74 : 2 * FILTER_BITS - conv_params.round_0;
75 #if CONFIG_AV1_HIGHBITDEPTH
76 const int intbufrange = bd + FILTER_BITS - conv_params.round_0 + 2;
77 assert(IMPLIES(bd < 12, intbufrange <= 16));
78 if (intbufrange > 16) {
79 conv_params.round_0 += intbufrange - 16;
80 if (!is_compound) conv_params.round_1 -= intbufrange - 16;
81 }
82 #else
83 (void)bd;
84 #endif // CONFIG_AV1_HIGHBITDEPTH
85 // TODO(yunqing): The following dst should only be valid while
86 // is_compound = 1;
87 conv_params.dst = dst;
88 conv_params.dst_stride = dst_stride;
89 conv_params.plane = plane;
90
91 // By default, set do average to 1 if this is the second single prediction
92 // in a compound mode.
93 conv_params.do_average = cmp_index;
94 return conv_params;
95 }
96
get_conv_params(int do_average,int plane,int bd)97 static INLINE ConvolveParams get_conv_params(int do_average, int plane,
98 int bd) {
99 return get_conv_params_no_round(do_average, plane, NULL, 0, 0, bd);
100 }
101
get_conv_params_wiener(int bd)102 static INLINE ConvolveParams get_conv_params_wiener(int bd) {
103 ConvolveParams conv_params;
104 (void)bd;
105 conv_params.do_average = 0;
106 conv_params.is_compound = 0;
107 conv_params.round_0 = WIENER_ROUND0_BITS;
108 conv_params.round_1 = 2 * FILTER_BITS - conv_params.round_0;
109 const int intbufrange = bd + FILTER_BITS - conv_params.round_0 + 2;
110 assert(IMPLIES(bd < 12, intbufrange <= 16));
111 if (intbufrange > 16) {
112 conv_params.round_0 += intbufrange - 16;
113 conv_params.round_1 -= intbufrange - 16;
114 }
115 conv_params.dst = NULL;
116 conv_params.dst_stride = 0;
117 conv_params.plane = 0;
118 return conv_params;
119 }
120
121 void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
122 uint8_t *dst, int dst_stride, int w, int h,
123 const InterpFilterParams *interp_filters[2],
124 const int subpel_x_qn, int x_step_q4,
125 const int subpel_y_qn, int y_step_q4,
126 int scaled, ConvolveParams *conv_params,
127 int bd);
128
129 #ifdef __cplusplus
130 } // extern "C"
131 #endif
132
133 #endif // AOM_AV1_COMMON_CONVOLVE_H_
134