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   const int intbufrange = bd + FILTER_BITS - conv_params.round_0 + 2;
76   assert(IMPLIES(bd < 12, intbufrange <= 16));
77   if (intbufrange > 16) {
78     conv_params.round_0 += intbufrange - 16;
79     if (!is_compound) conv_params.round_1 -= intbufrange - 16;
80   }
81   // TODO(yunqing): The following dst should only be valid while
82   // is_compound = 1;
83   conv_params.dst = dst;
84   conv_params.dst_stride = dst_stride;
85   conv_params.plane = plane;
86 
87   // By default, set do average to 1 if this is the second single prediction
88   // in a compound mode.
89   conv_params.do_average = cmp_index;
90   return conv_params;
91 }
92 
get_conv_params(int do_average,int plane,int bd)93 static INLINE ConvolveParams get_conv_params(int do_average, int plane,
94                                              int bd) {
95   return get_conv_params_no_round(do_average, plane, NULL, 0, 0, bd);
96 }
97 
get_conv_params_wiener(int bd)98 static INLINE ConvolveParams get_conv_params_wiener(int bd) {
99   ConvolveParams conv_params;
100   (void)bd;
101   conv_params.do_average = 0;
102   conv_params.is_compound = 0;
103   conv_params.round_0 = WIENER_ROUND0_BITS;
104   conv_params.round_1 = 2 * FILTER_BITS - conv_params.round_0;
105   const int intbufrange = bd + FILTER_BITS - conv_params.round_0 + 2;
106   assert(IMPLIES(bd < 12, intbufrange <= 16));
107   if (intbufrange > 16) {
108     conv_params.round_0 += intbufrange - 16;
109     conv_params.round_1 -= intbufrange - 16;
110   }
111   conv_params.dst = NULL;
112   conv_params.dst_stride = 0;
113   conv_params.plane = 0;
114   return conv_params;
115 }
116 
117 void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
118                                    uint8_t *dst, int dst_stride, int w, int h,
119                                    const InterpFilterParams *interp_filters[2],
120                                    const int subpel_x_qn, int x_step_q4,
121                                    const int subpel_y_qn, int y_step_q4,
122                                    int scaled, ConvolveParams *conv_params,
123                                    int bd);
124 
125 // TODO(sarahparker) This will need to be integerized and optimized
126 void av1_convolve_2d_sobel_y_c(const uint8_t *src, int src_stride, double *dst,
127                                int dst_stride, int w, int h, int dir,
128                                double norm);
129 
130 #ifdef __cplusplus
131 }  // extern "C"
132 #endif
133 
134 #endif  // AOM_AV1_COMMON_CONVOLVE_H_
135