1 /*****************************************************************************
2  * This file is part of Kvazaar HEVC encoder.
3  *
4  * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  *   list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this
14  *   list of conditions and the following disclaimer in the documentation and/or
15  *   other materials provided with the distribution.
16  *
17  * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
28  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
31  ****************************************************************************/
32 
33 #include "strategies/strategies-picture.h"
34 
35 #include "strategies/altivec/picture-altivec.h"
36 #include "strategies/avx2/picture-avx2.h"
37 #include "strategies/generic/picture-generic.h"
38 #include "strategies/sse2/picture-sse2.h"
39 #include "strategies/sse41/picture-sse41.h"
40 #include "strategies/x86_asm/picture-x86-asm.h"
41 #include "strategyselector.h"
42 
43 
44 // Define function pointers.
45 reg_sad_func * kvz_reg_sad = 0;
46 
47 cost_pixel_nxn_func * kvz_sad_4x4 = 0;
48 cost_pixel_nxn_func * kvz_sad_8x8 = 0;
49 cost_pixel_nxn_func * kvz_sad_16x16 = 0;
50 cost_pixel_nxn_func * kvz_sad_32x32 = 0;
51 cost_pixel_nxn_func * kvz_sad_64x64 = 0;
52 
53 cost_pixel_nxn_func * kvz_satd_4x4 = 0;
54 cost_pixel_nxn_func * kvz_satd_8x8 = 0;
55 cost_pixel_nxn_func * kvz_satd_16x16 = 0;
56 cost_pixel_nxn_func * kvz_satd_32x32 = 0;
57 cost_pixel_nxn_func * kvz_satd_64x64 = 0;
58 
59 cost_pixel_nxn_multi_func * kvz_sad_4x4_dual = 0;
60 cost_pixel_nxn_multi_func * kvz_sad_8x8_dual = 0;
61 cost_pixel_nxn_multi_func * kvz_sad_16x16_dual = 0;
62 cost_pixel_nxn_multi_func * kvz_sad_32x32_dual = 0;
63 cost_pixel_nxn_multi_func * kvz_sad_64x64_dual = 0;
64 
65 cost_pixel_nxn_multi_func * kvz_satd_4x4_dual = 0;
66 cost_pixel_nxn_multi_func * kvz_satd_8x8_dual = 0;
67 cost_pixel_nxn_multi_func * kvz_satd_16x16_dual = 0;
68 cost_pixel_nxn_multi_func * kvz_satd_32x32_dual = 0;
69 cost_pixel_nxn_multi_func * kvz_satd_64x64_dual = 0;
70 
71 cost_pixel_any_size_func * kvz_satd_any_size = 0;
72 cost_pixel_any_size_multi_func * kvz_satd_any_size_quad = 0;
73 
74 pixels_calc_ssd_func * kvz_pixels_calc_ssd = 0;
75 
76 inter_recon_bipred_func * kvz_inter_recon_bipred_blend = 0;
77 
78 get_optimized_sad_func *kvz_get_optimized_sad = 0;
79 ver_sad_func *kvz_ver_sad = 0;
80 hor_sad_func *kvz_hor_sad = 0;
81 
82 pixel_var_func *kvz_pixel_var = 0;
83 
84 
kvz_strategy_register_picture(void * opaque,uint8_t bitdepth)85 int kvz_strategy_register_picture(void* opaque, uint8_t bitdepth) {
86   bool success = true;
87 
88   success &= kvz_strategy_register_picture_generic(opaque, bitdepth);
89 
90   if (kvz_g_hardware_flags.intel_flags.sse2) {
91     success &= kvz_strategy_register_picture_sse2(opaque, bitdepth);
92   }
93   if (kvz_g_hardware_flags.intel_flags.sse41) {
94     success &= kvz_strategy_register_picture_sse41(opaque, bitdepth);
95   }
96   if (kvz_g_hardware_flags.intel_flags.avx) {
97     success &= kvz_strategy_register_picture_x86_asm_avx(opaque, bitdepth);
98   }
99   if (kvz_g_hardware_flags.intel_flags.avx2) {
100     success &= kvz_strategy_register_picture_avx2(opaque, bitdepth);
101   }
102   if (kvz_g_hardware_flags.powerpc_flags.altivec) {
103     success &= kvz_strategy_register_picture_altivec(opaque, bitdepth);
104   }
105 
106   return success;
107 }
108 
109 
110 /**
111 * \brief  Get a function that calculates SATD for NxN block.
112 *
113 * \param n  Width of the region for which SATD is calculated.
114 *
115 * \returns  Pointer to cost_16bit_nxn_func.
116 */
kvz_pixels_get_satd_func(unsigned n)117 cost_pixel_nxn_func * kvz_pixels_get_satd_func(unsigned n)
118 {
119   switch (n) {
120   case 4:
121     return kvz_satd_4x4;
122   case 8:
123     return kvz_satd_8x8;
124   case 16:
125     return kvz_satd_16x16;
126   case 32:
127     return kvz_satd_32x32;
128   case 64:
129     return kvz_satd_64x64;
130   default:
131     return NULL;
132   }
133 }
134 
135 
136 /**
137 * \brief  Get a function that calculates SAD for NxN block.
138 *
139 * \param n  Width of the region for which SAD is calculated.
140 *
141 * \returns  Pointer to cost_16bit_nxn_func.
142 */
kvz_pixels_get_sad_func(unsigned n)143 cost_pixel_nxn_func * kvz_pixels_get_sad_func(unsigned n)
144 {
145   switch (n) {
146   case 4:
147     return kvz_sad_4x4;
148   case 8:
149     return kvz_sad_8x8;
150   case 16:
151     return kvz_sad_16x16;
152   case 32:
153     return kvz_sad_32x32;
154   case 64:
155     return kvz_sad_64x64;
156   default:
157     return NULL;
158   }
159 }
160 
161 /**
162 * \brief  Get a function that calculates SATDs for 2 NxN blocks.
163 *
164 * \param n  Width of the region for which SATD is calculated.
165 *
166 * \returns  Pointer to cost_pixel_nxn_multi_func.
167 */
kvz_pixels_get_satd_dual_func(unsigned n)168 cost_pixel_nxn_multi_func * kvz_pixels_get_satd_dual_func(unsigned n)
169 {
170   switch (n) {
171   case 4:
172     return kvz_satd_4x4_dual;
173   case 8:
174     return kvz_satd_8x8_dual;
175   case 16:
176     return kvz_satd_16x16_dual;
177   case 32:
178     return kvz_satd_32x32_dual;
179   case 64:
180     return kvz_satd_64x64_dual;
181   default:
182     return NULL;
183   }
184 }
185 
186 
187 /**
188 * \brief  Get a function that calculates SADs for 2 NxN blocks.
189 *
190 * \param n  Width of the region for which SAD is calculated.
191 *
192 * \returns  Pointer to cost_pixel_nxn_multi_func.
193 */
kvz_pixels_get_sad_dual_func(unsigned n)194 cost_pixel_nxn_multi_func * kvz_pixels_get_sad_dual_func(unsigned n)
195 {
196   switch (n) {
197   case 4:
198     return kvz_sad_4x4_dual;
199   case 8:
200     return kvz_sad_8x8_dual;
201   case 16:
202     return kvz_sad_16x16_dual;
203   case 32:
204     return kvz_sad_32x32_dual;
205   case 64:
206     return kvz_sad_64x64_dual;
207   default:
208     return NULL;
209   }
210 }
211