1 /*
2 * Copyright(c) 2019 Intel Corporation
3 * SPDX - License - Identifier: BSD - 2 - Clause - Patent
4 */
5 
6 #ifndef EbMotionEstimationContext_h
7 #define EbMotionEstimationContext_h
8 
9 #include "EbDefinitions.h"
10 #include "EbCodingUnit.h"
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 // Max Search Area
16 #define MAX_SEARCH_AREA_WIDTH   1350 // This should be a function for the MAX HME L0 * the multiplications per layers and per Hierarchichal structures
17 #define MAX_SEARCH_AREA_HEIGHT  675 // This should be a function for the MAX HME L0 * the multiplications per layers and per Hierarchichal structures
18 
19 // 1-D interpolation shift value
20 #define if_shift 6
21 
22 #define VARIANCE_PRECISION      16
23 
24 #define MEAN_PRECISION      (VARIANCE_PRECISION >> 1)
25 
26 #define HME_RECTANGULAR    0
27 #define HME_SPARSE         1
28 
29 // Quater pel refinement methods
30 typedef enum EbQuarterPelRefinementMethod
31 {
32     EB_QUARTER_IN_FULL,
33     EB_QUARTER_IN_HALF_HORIZONTAL,
34     EB_QUARTER_IN_HALF_VERTICAL,
35     EB_QUARTER_IN_HALF_DIAGONAL
36 } EbQuarterPelInterpolationMethod;
37 
38 // HME parameters
39 
40 #define HME_DECIM_FILTER_TAP     9
41 
42 typedef enum EbMeTierZeroPu
43 {
44 
45     // 2Nx2N [85 partitions]
46     ME_TIER_ZERO_PU_64x64       =    0,
47     ME_TIER_ZERO_PU_32x32_0     =    1,
48     ME_TIER_ZERO_PU_32x32_1     =    2,
49     ME_TIER_ZERO_PU_32x32_2     =    3,
50     ME_TIER_ZERO_PU_32x32_3     =    4,
51     ME_TIER_ZERO_PU_16x16_0     =    5,
52     ME_TIER_ZERO_PU_16x16_1     =    6,
53     ME_TIER_ZERO_PU_16x16_2     =    7,
54     ME_TIER_ZERO_PU_16x16_3     =    8,
55     ME_TIER_ZERO_PU_16x16_4     =    9,
56     ME_TIER_ZERO_PU_16x16_5     =    10,
57     ME_TIER_ZERO_PU_16x16_6     =    11,
58     ME_TIER_ZERO_PU_16x16_7     =    12,
59     ME_TIER_ZERO_PU_16x16_8     =    13,
60     ME_TIER_ZERO_PU_16x16_9     =    14,
61     ME_TIER_ZERO_PU_16x16_10    =    15,
62     ME_TIER_ZERO_PU_16x16_11    =    16,
63     ME_TIER_ZERO_PU_16x16_12    =    17,
64     ME_TIER_ZERO_PU_16x16_13    =    18,
65     ME_TIER_ZERO_PU_16x16_14    =    19,
66     ME_TIER_ZERO_PU_16x16_15    =    20,
67     ME_TIER_ZERO_PU_8x8_0       =    21,
68     ME_TIER_ZERO_PU_8x8_1       =    22,
69     ME_TIER_ZERO_PU_8x8_2       =    23,
70     ME_TIER_ZERO_PU_8x8_3       =    24,
71     ME_TIER_ZERO_PU_8x8_4       =    25,
72     ME_TIER_ZERO_PU_8x8_5       =    26,
73     ME_TIER_ZERO_PU_8x8_6       =    27,
74     ME_TIER_ZERO_PU_8x8_7       =    28,
75     ME_TIER_ZERO_PU_8x8_8       =    29,
76     ME_TIER_ZERO_PU_8x8_9       =    30,
77     ME_TIER_ZERO_PU_8x8_10      =    31,
78     ME_TIER_ZERO_PU_8x8_11      =    32,
79     ME_TIER_ZERO_PU_8x8_12      =    33,
80     ME_TIER_ZERO_PU_8x8_13      =    34,
81     ME_TIER_ZERO_PU_8x8_14      =    35,
82     ME_TIER_ZERO_PU_8x8_15      =    36,
83     ME_TIER_ZERO_PU_8x8_16      =    37,
84     ME_TIER_ZERO_PU_8x8_17      =    38,
85     ME_TIER_ZERO_PU_8x8_18      =    39,
86     ME_TIER_ZERO_PU_8x8_19      =    40,
87     ME_TIER_ZERO_PU_8x8_20      =    41,
88     ME_TIER_ZERO_PU_8x8_21      =    42,
89     ME_TIER_ZERO_PU_8x8_22      =    43,
90     ME_TIER_ZERO_PU_8x8_23      =    44,
91     ME_TIER_ZERO_PU_8x8_24      =    45,
92     ME_TIER_ZERO_PU_8x8_25      =    46,
93     ME_TIER_ZERO_PU_8x8_26      =    47,
94     ME_TIER_ZERO_PU_8x8_27      =    48,
95     ME_TIER_ZERO_PU_8x8_28      =    49,
96     ME_TIER_ZERO_PU_8x8_29      =    50,
97     ME_TIER_ZERO_PU_8x8_30      =    51,
98     ME_TIER_ZERO_PU_8x8_31      =    52,
99     ME_TIER_ZERO_PU_8x8_32      =    53,
100     ME_TIER_ZERO_PU_8x8_33      =    54,
101     ME_TIER_ZERO_PU_8x8_34      =    55,
102     ME_TIER_ZERO_PU_8x8_35      =    56,
103     ME_TIER_ZERO_PU_8x8_36      =    57,
104     ME_TIER_ZERO_PU_8x8_37      =    58,
105     ME_TIER_ZERO_PU_8x8_38      =    59,
106     ME_TIER_ZERO_PU_8x8_39      =    60,
107     ME_TIER_ZERO_PU_8x8_40      =    61,
108     ME_TIER_ZERO_PU_8x8_41      =    62,
109     ME_TIER_ZERO_PU_8x8_42      =    63,
110     ME_TIER_ZERO_PU_8x8_43      =    64,
111     ME_TIER_ZERO_PU_8x8_44      =    65,
112     ME_TIER_ZERO_PU_8x8_45      =    66,
113     ME_TIER_ZERO_PU_8x8_46      =    67,
114     ME_TIER_ZERO_PU_8x8_47      =    68,
115     ME_TIER_ZERO_PU_8x8_48      =    69,
116     ME_TIER_ZERO_PU_8x8_49      =    70,
117     ME_TIER_ZERO_PU_8x8_50      =    71,
118     ME_TIER_ZERO_PU_8x8_51      =    72,
119     ME_TIER_ZERO_PU_8x8_52      =    73,
120     ME_TIER_ZERO_PU_8x8_53      =    74,
121     ME_TIER_ZERO_PU_8x8_54      =    75,
122     ME_TIER_ZERO_PU_8x8_55      =    76,
123     ME_TIER_ZERO_PU_8x8_56      =    77,
124     ME_TIER_ZERO_PU_8x8_57      =    78,
125     ME_TIER_ZERO_PU_8x8_58      =    79,
126     ME_TIER_ZERO_PU_8x8_59      =    80,
127     ME_TIER_ZERO_PU_8x8_60      =    81,
128     ME_TIER_ZERO_PU_8x8_61      =    82,
129     ME_TIER_ZERO_PU_8x8_62      =    83,
130     ME_TIER_ZERO_PU_8x8_63      =    84,
131     // 2NxN  [42 partitions]
132     ME_TIER_ZERO_PU_64x32_0     =    85,
133     ME_TIER_ZERO_PU_64x32_1     =    86,
134     ME_TIER_ZERO_PU_32x16_0     =    87,
135     ME_TIER_ZERO_PU_32x16_1     =    88,
136     ME_TIER_ZERO_PU_32x16_2     =    89,
137     ME_TIER_ZERO_PU_32x16_3     =    90,
138     ME_TIER_ZERO_PU_32x16_4     =    91,
139     ME_TIER_ZERO_PU_32x16_5     =    92,
140     ME_TIER_ZERO_PU_32x16_6     =    93,
141     ME_TIER_ZERO_PU_32x16_7     =    94,
142     ME_TIER_ZERO_PU_16x8_0      =    95,
143     ME_TIER_ZERO_PU_16x8_1      =    96,
144     ME_TIER_ZERO_PU_16x8_2      =    97,
145     ME_TIER_ZERO_PU_16x8_3      =    98,
146     ME_TIER_ZERO_PU_16x8_4      =    99,
147     ME_TIER_ZERO_PU_16x8_5      =    100,
148     ME_TIER_ZERO_PU_16x8_6      =    101,
149     ME_TIER_ZERO_PU_16x8_7      =    102,
150     ME_TIER_ZERO_PU_16x8_8      =    103,
151     ME_TIER_ZERO_PU_16x8_9      =    104,
152     ME_TIER_ZERO_PU_16x8_10     =    105,
153     ME_TIER_ZERO_PU_16x8_11     =    106,
154     ME_TIER_ZERO_PU_16x8_12     =    107,
155     ME_TIER_ZERO_PU_16x8_13     =    108,
156     ME_TIER_ZERO_PU_16x8_14     =    109,
157     ME_TIER_ZERO_PU_16x8_15     =    110,
158     ME_TIER_ZERO_PU_16x8_16     =    111,
159     ME_TIER_ZERO_PU_16x8_17     =    112,
160     ME_TIER_ZERO_PU_16x8_18     =    113,
161     ME_TIER_ZERO_PU_16x8_19     =    114,
162     ME_TIER_ZERO_PU_16x8_20     =    115,
163     ME_TIER_ZERO_PU_16x8_21     =    116,
164     ME_TIER_ZERO_PU_16x8_22     =    117,
165     ME_TIER_ZERO_PU_16x8_23     =    118,
166     ME_TIER_ZERO_PU_16x8_24     =    119,
167     ME_TIER_ZERO_PU_16x8_25     =    120,
168     ME_TIER_ZERO_PU_16x8_26     =    121,
169     ME_TIER_ZERO_PU_16x8_27     =    122,
170     ME_TIER_ZERO_PU_16x8_28     =    123,
171     ME_TIER_ZERO_PU_16x8_29     =    124,
172     ME_TIER_ZERO_PU_16x8_30     =    125,
173     ME_TIER_ZERO_PU_16x8_31     =    126,
174     // Nx2N  [42 partitions]
175     ME_TIER_ZERO_PU_32x64_0     =    127,
176     ME_TIER_ZERO_PU_32x64_1     =    128,
177     ME_TIER_ZERO_PU_16x32_0     =    129,
178     ME_TIER_ZERO_PU_16x32_1     =    130,
179     ME_TIER_ZERO_PU_16x32_2     =    131,
180     ME_TIER_ZERO_PU_16x32_3     =    132,
181     ME_TIER_ZERO_PU_16x32_4     =    133,
182     ME_TIER_ZERO_PU_16x32_5     =    134,
183     ME_TIER_ZERO_PU_16x32_6     =    135,
184     ME_TIER_ZERO_PU_16x32_7     =    136,
185     ME_TIER_ZERO_PU_8x16_0      =    137,
186     ME_TIER_ZERO_PU_8x16_1      =    138,
187     ME_TIER_ZERO_PU_8x16_2      =    139,
188     ME_TIER_ZERO_PU_8x16_3      =    140,
189     ME_TIER_ZERO_PU_8x16_4      =    141,
190     ME_TIER_ZERO_PU_8x16_5      =    142,
191     ME_TIER_ZERO_PU_8x16_6      =    143,
192     ME_TIER_ZERO_PU_8x16_7      =    144,
193     ME_TIER_ZERO_PU_8x16_8      =    145,
194     ME_TIER_ZERO_PU_8x16_9      =    146,
195     ME_TIER_ZERO_PU_8x16_10     =    147,
196     ME_TIER_ZERO_PU_8x16_11     =    148,
197     ME_TIER_ZERO_PU_8x16_12     =    149,
198     ME_TIER_ZERO_PU_8x16_13     =    150,
199     ME_TIER_ZERO_PU_8x16_14     =    151,
200     ME_TIER_ZERO_PU_8x16_15     =    152,
201     ME_TIER_ZERO_PU_8x16_16     =    153,
202     ME_TIER_ZERO_PU_8x16_17     =    154,
203     ME_TIER_ZERO_PU_8x16_18     =    155,
204     ME_TIER_ZERO_PU_8x16_19     =    156,
205     ME_TIER_ZERO_PU_8x16_20     =    157,
206     ME_TIER_ZERO_PU_8x16_21     =    158,
207     ME_TIER_ZERO_PU_8x16_22     =    159,
208     ME_TIER_ZERO_PU_8x16_23     =    160,
209     ME_TIER_ZERO_PU_8x16_24     =    161,
210     ME_TIER_ZERO_PU_8x16_25     =    162,
211     ME_TIER_ZERO_PU_8x16_26     =    163,
212     ME_TIER_ZERO_PU_8x16_27     =    164,
213     ME_TIER_ZERO_PU_8x16_28     =    165,
214     ME_TIER_ZERO_PU_8x16_29     =    166,
215     ME_TIER_ZERO_PU_8x16_30     =    167,
216     ME_TIER_ZERO_PU_8x16_31     =    168,
217 
218     // 2NxnU [10 partitions]
219     ME_TIER_ZERO_PU_64x16_0     =    169,
220     ME_TIER_ZERO_PU_64x16_1     =    170,
221     ME_TIER_ZERO_PU_32x8_0      =    171,
222     ME_TIER_ZERO_PU_32x8_1      =    172,
223     ME_TIER_ZERO_PU_32x8_2      =    173,
224     ME_TIER_ZERO_PU_32x8_3      =    174,
225     ME_TIER_ZERO_PU_32x8_4      =    175,
226     ME_TIER_ZERO_PU_32x8_5      =    176,
227     ME_TIER_ZERO_PU_32x8_6      =    177,
228     ME_TIER_ZERO_PU_32x8_7      =    178,
229     // 2NxnD [10 partitions]
230     ME_TIER_ZERO_PU_64x48_0     =    179,
231     ME_TIER_ZERO_PU_64x48_1     =    180,
232     ME_TIER_ZERO_PU_32x24_0     =    181,
233     ME_TIER_ZERO_PU_32x24_1     =    182,
234     ME_TIER_ZERO_PU_32x24_2     =    183,
235     ME_TIER_ZERO_PU_32x24_3     =    184,
236     ME_TIER_ZERO_PU_32x24_4     =    185,
237     ME_TIER_ZERO_PU_32x24_5     =    186,
238     ME_TIER_ZERO_PU_32x24_6     =    187,
239     ME_TIER_ZERO_PU_32x24_7     =    188,
240     // nLx2N [10 partitions]
241     ME_TIER_ZERO_PU_16x64_0     =    189,
242     ME_TIER_ZERO_PU_16x64_1     =    190,
243     ME_TIER_ZERO_PU_8x32_0      =    191,
244     ME_TIER_ZERO_PU_8x32_1      =    192,
245     ME_TIER_ZERO_PU_8x32_2      =    193,
246     ME_TIER_ZERO_PU_8x32_3      =    194,
247     ME_TIER_ZERO_PU_8x32_4      =    195,
248     ME_TIER_ZERO_PU_8x32_5      =    196,
249     ME_TIER_ZERO_PU_8x32_6      =    197,
250     ME_TIER_ZERO_PU_8x32_7      =    198,
251     // nRx2N [10 partitions]
252     ME_TIER_ZERO_PU_48x64_0     =    199,
253     ME_TIER_ZERO_PU_48x64_1     =    200,
254     ME_TIER_ZERO_PU_24x32_0     =    201,
255     ME_TIER_ZERO_PU_24x32_1     =    202,
256     ME_TIER_ZERO_PU_24x32_2     =    203,
257     ME_TIER_ZERO_PU_24x32_3     =    204,
258     ME_TIER_ZERO_PU_24x32_4     =    205,
259     ME_TIER_ZERO_PU_24x32_5     =    206,
260     ME_TIER_ZERO_PU_24x32_6     =    207,
261     ME_TIER_ZERO_PU_24x32_7     =    208
262 } EbMeTierZeroPu;
263 
264 typedef struct MePredUnit
265 {
266     uint32_t         distortion;
267     EB_PREDDIRECTION prediction_direction;
268     uint32_t         Mv[MAX_NUM_OF_REF_PIC_LIST];
269 
270 } MePredUnit;
271 
272 typedef struct MotionEstimationTierZero {
273     MePredUnit pu[MAX_ME_PU_COUNT];
274 } MotionEstimationTierZero;
275 
276 typedef struct MeContext
277 {\
278     // MV offset (search center)
279     int16_t   x_mv_offset;
280     int16_t   y_mv_offset;
281 
282     // Search region stride
283     uint32_t  interpolated_stride;
284     uint32_t  interpolated_full_stride[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
285 
286     MotionEstimationTierZero  me_candidate[MAX_ME_CANDIDATE_PER_PU];
287 
288     uint8_t *hme_buffer;
289     uint32_t hme_buffer_stride;
290 
291     // Intermediate LCU-sized buffer to retain the input samples
292     uint8_t *sb_buffer;
293     uint8_t *sb_buffer_ptr;
294     uint32_t sb_buffer_stride;
295     uint8_t *hme_sb_buffer;
296     uint32_t hme_sb_buffer_stride;
297     uint8_t *sb_src_ptr;
298     uint32_t sb_src_stride;
299     uint8_t *quarter_sb_buffer;
300     uint32_t quarter_sb_buffer_stride;
301     uint8_t *sixteenth_sb_buffer;
302     uint32_t sixteenth_sb_buffer_stride;
303 
304     uint8_t *integer_buffer[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
305     uint8_t *integer_buffer_ptr[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
306     uint8_t *posb_buffer[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
307     uint8_t *posh_buffer[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
308     uint8_t *posj_buffer[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
309 
310     uint8_t  *one_d_intermediate_results_buf0;
311     uint8_t  *one_d_intermediate_results_buf1;
312 
313     int16_t x_search_area_origin[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
314     int16_t y_search_area_origin[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX];
315 
316     uint8_t *avctemp_buffer;
317 
318     uint32_t *p_best_sad8x8;
319     uint32_t *p_best_sad16x16;
320     uint32_t *p_best_sad32x32;
321     uint32_t *p_best_sad64x64;
322 
323     uint32_t *p_best_mv8x8;
324     uint32_t *p_best_mv16x16;
325     uint32_t *p_best_mv32x32;
326     uint32_t *p_best_mv64x64;
327 
328     uint32_t *p_best_sad64x32;
329     uint32_t *p_best_sad32x16;
330     uint32_t *p_best_sad16x8;
331     uint32_t *p_best_sad32x64;
332     uint32_t *p_best_sad16x32;
333     uint32_t *p_best_sad8x16;
334     uint32_t *p_best_sad64x16;
335     uint32_t *p_best_sad32x8;
336     uint32_t *p_best_sad64x48;
337     uint32_t *p_best_sad32x24;
338     uint32_t *p_best_sad16x64;
339     uint32_t *p_best_sad8x32;
340     uint32_t *p_best_sad48x64;
341     uint32_t *p_best_sad24x32;
342 
343     uint32_t *p_best_mv64x32;
344     uint32_t *p_best_mv32x16;
345     uint32_t *p_best_mv16x8;
346     uint32_t *p_best_mv32x64;
347     uint32_t *p_best_mv16x32;
348     uint32_t *p_best_mv8x16;
349     uint32_t *p_best_mv64x16;
350     uint32_t *p_best_mv32x8;
351     uint32_t *p_best_mv64x48;
352     uint32_t *p_best_mv32x24;
353     uint32_t *p_best_mv16x64;
354     uint32_t *p_best_mv8x32;
355     uint32_t *p_best_mv48x64;
356     uint32_t *p_best_mv24x32;
357 
358     uint32_t p_sad32x32[4];
359     uint32_t p_sad16x16[16];
360     uint32_t p_sad8x8[64];
361 
362     uint8_t  psub_pel_direction64x64;
363 
364     uint8_t  psub_pel_direction32x32[4];
365     uint8_t  psub_pel_direction16x16[16];
366     uint8_t  psub_pel_direction8x8[64];
367 
368     uint32_t p_sb_best_sad[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX][MAX_ME_PU_COUNT];
369     uint32_t p_sb_best_mv[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX][MAX_ME_PU_COUNT];
370     uint32_t p_sb_bipred_sad[MAX_ME_PU_COUNT];//needs to be upgraded to 209 pus
371 
372     uint32_t p_sb_best_ssd[MAX_NUM_OF_REF_PIC_LIST][MAX_REF_IDX][MAX_ME_PU_COUNT];
373     uint32_t *p_best_ssd8x8;
374     uint32_t *p_best_ssd16x16;
375     uint32_t *p_best_ssd32x32;
376     uint32_t *p_best_ssd64x64;
377 
378     uint16_t *p_eight_pos_sad16x16;
379 
380     uint8_t  hme_search_type;
381 
382     // Multi-Mode signal(s)
383     uint8_t  fractional_search_method;
384     uint8_t  fractional_search_model;
385     EB_BOOL  fractional_search64x64;
386     EB_BOOL  single_hme_quadrant;
387 
388     // ME
389     uint8_t  search_area_width;
390     uint8_t  search_area_height;
391     // HME
392     uint16_t number_hme_search_region_in_width;
393     uint16_t number_hme_search_region_in_height;
394     uint16_t hme_level0_total_search_area_width;
395     uint16_t hme_level0_total_search_area_height;
396     uint16_t hme_level0_search_area_in_width_array[EB_HME_SEARCH_AREA_COLUMN_MAX_COUNT];
397     uint16_t hme_level0_search_area_in_height_array[EB_HME_SEARCH_AREA_ROW_MAX_COUNT];
398     uint16_t hme_level1_search_area_in_width_array[EB_HME_SEARCH_AREA_COLUMN_MAX_COUNT];
399     uint16_t hme_level1_search_area_in_height_array[EB_HME_SEARCH_AREA_ROW_MAX_COUNT];
400     uint16_t hme_level2_search_area_in_width_array[EB_HME_SEARCH_AREA_COLUMN_MAX_COUNT];
401     uint16_t hme_level2_search_area_in_height_array[EB_HME_SEARCH_AREA_ROW_MAX_COUNT];
402 
403 } MeContext;
404 
405 extern EbErrorType eb_vp9_me_context_ctor(
406     MeContext **object_dbl_ptr);
407 
408 #ifdef __cplusplus
409 }
410 #endif
411 #endif // EbMotionEstimationContext_h
412