Coverage Report

Created: 2024-07-27 06:35

/src/libavc/encoder/ime.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
/**
21
 *******************************************************************************
22
 * @file
23
 *  ime.c
24
 *
25
 * @brief
26
 *  This file contains functions needed for computing motion vectors of a
27
 *  16x16 block
28
 *
29
 * @author
30
 *  Ittiam
31
 *
32
 * @par List of Functions:
33
 *  - ime_diamond_search_16x16
34
 *  - ime_evaluate_init_srchposn_16x16
35
 *  - ime_full_pel_motion_estimation_16x16
36
 *  - ime_sub_pel_motion_estimation_16x16
37
 *  - ime_compute_skip_cost
38
 *
39
 * @remarks
40
 *  None
41
 *
42
 *******************************************************************************
43
 */
44
45
/*****************************************************************************/
46
/* File Includes                                                             */
47
/*****************************************************************************/
48
49
/* System include files */
50
#include <stdio.h>
51
#include <assert.h>
52
#include <limits.h>
53
#include <string.h>
54
55
/* User include files */
56
#include "ime_typedefs.h"
57
#include "ime_distortion_metrics.h"
58
#include "ime_defs.h"
59
#include "ime_structs.h"
60
#include "ime.h"
61
#include "ime_macros.h"
62
#include "ime_statistics.h"
63
64
/**
65
*******************************************************************************
66
*
67
* @brief Diamond Search
68
*
69
* @par Description:
70
*  This function computes the sad at vertices of several layers of diamond grid
71
*  at a time. The number of layers of diamond grid that would be evaluated is
72
*  configurable.The function computes the sad at vertices of a diamond grid. If
73
*  the sad at the center of the diamond grid is lesser than the sad at any other
74
*  point of the diamond grid, the function marks the candidate Mb partition as
75
*  mv.
76
*
77
* @param[in] ps_me_ctxt
78
*  pointer to me context
79
*
80
* @param[in] i4_reflist
81
*  ref list
82
*
83
* @returns  mv pair & corresponding distortion and cost
84
*
85
* @remarks Diamond Srch, radius is 1
86
*
87
*******************************************************************************
88
*/
89
void ime_diamond_search_16x16(me_ctxt_t *ps_me_ctxt, WORD32 i4_reflist)
90
249k
{
91
    /* MB partition info */
92
249k
    mb_part_ctxt *ps_mb_part = &ps_me_ctxt->as_mb_part[i4_reflist];
93
94
    /* lagrange parameter */
95
249k
    UWORD32 u4_lambda_motion = ps_me_ctxt->u4_lambda_motion;
96
97
    /* srch range*/
98
249k
    WORD32 i4_srch_range_n = ps_me_ctxt->i4_srch_range_n;
99
249k
    WORD32 i4_srch_range_s = ps_me_ctxt->i4_srch_range_s;
100
249k
    WORD32 i4_srch_range_e = ps_me_ctxt->i4_srch_range_e;
101
249k
    WORD32 i4_srch_range_w = ps_me_ctxt->i4_srch_range_w;
102
103
    /* enabled fast sad computation */
104
//    UWORD32 u4_enable_fast_sad = ps_me_ctxt->u4_enable_fast_sad;
105
106
    /* pointer to src macro block */
107
249k
    UWORD8 *pu1_curr_mb = ps_me_ctxt->pu1_src_buf_luma;
108
249k
    UWORD8 *pu1_ref_mb = ps_me_ctxt->apu1_ref_buf_luma[i4_reflist];
109
110
    /* strides */
111
249k
    WORD32 i4_src_strd = ps_me_ctxt->i4_src_strd;
112
249k
    WORD32 i4_ref_strd = ps_me_ctxt->i4_rec_strd;
113
114
    /* least cost */
115
249k
    WORD32 i4_cost_least = ps_mb_part->i4_mb_cost;
116
117
    /* least sad */
118
249k
    WORD32 i4_distortion_least = ps_mb_part->i4_mb_distortion;
119
120
    /* mv pair */
121
249k
    WORD16 i2_mvx, i2_mvy;
122
123
    /* mv bits */
124
249k
    UWORD8 *pu1_mv_bits = ps_me_ctxt->pu1_mv_bits;
125
126
    /* temp var */
127
249k
    WORD32 i4_cost[4];
128
249k
    WORD32 i4_sad[4];
129
249k
    UWORD8 *pu1_ref;
130
249k
    WORD16 i2_mv_u_x, i2_mv_u_y;
131
132
    /* Diamond search Iteration Max Cnt */
133
249k
    UWORD32 u4_num_layers = ps_me_ctxt->u4_num_layers;
134
135
    /* temp var */
136
//    UWORD8 u1_prev_jump = NONE;
137
//    UWORD8 u1_curr_jump = NONE;
138
//    UWORD8 u1_next_jump;
139
//    WORD32 mask_arr[5] = {15, 13, 14, 7, 11};
140
//    WORD32 mask;
141
//    UWORD8 *apu1_ref[4];
142
//    WORD32 i, cnt;
143
//    WORD32 dia[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
144
145
    /* mv with best sad during initial evaluation */
146
249k
    i2_mvx = ps_mb_part->s_mv_curr.i2_mvx;
147
249k
    i2_mvy = ps_mb_part->s_mv_curr.i2_mvy;
148
149
249k
    i2_mv_u_x = i2_mvx;
150
249k
    i2_mv_u_y = i2_mvy;
151
152
836k
    while (u4_num_layers)
153
822k
    {
154
        /* FIXME : is this the write way to check for out of bounds ? */
155
822k
        if ( (i2_mvx - 1 < i4_srch_range_w) ||
156
822k
                        (i2_mvx + 1 > i4_srch_range_e) ||
157
822k
                        (i2_mvy - 1 < i4_srch_range_n) ||
158
822k
                        (i2_mvy + 1 > i4_srch_range_s) )
159
50.7k
        {
160
50.7k
            break;
161
50.7k
        }
162
163
771k
        pu1_ref = pu1_ref_mb + i2_mvx + (i2_mvy * i4_ref_strd);
164
165
771k
        ps_me_ctxt->pf_ime_compute_sad4_diamond(pu1_ref,
166
771k
                                                pu1_curr_mb,
167
771k
                                                i4_ref_strd,
168
771k
                                                i4_src_strd,
169
771k
                                                i4_sad);
170
171
771k
        DEBUG_SAD_HISTOGRAM_ADD(i4_sad[0], 2);
172
771k
        DEBUG_SAD_HISTOGRAM_ADD(i4_sad[1], 2);
173
771k
        DEBUG_SAD_HISTOGRAM_ADD(i4_sad[2], 2);
174
771k
        DEBUG_SAD_HISTOGRAM_ADD(i4_sad[3], 2);
175
176
        /* compute cost */
177
771k
        i4_cost[0] = i4_sad[0] + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ ((i2_mvx - 1) << 2) - ps_mb_part->s_mv_pred.i2_mvx]
178
771k
                                                                   + pu1_mv_bits[(i2_mvy << 2) - ps_mb_part->s_mv_pred.i2_mvy] ));
179
771k
        i4_cost[1] = i4_sad[1] + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ ((i2_mvx + 1) << 2) - ps_mb_part->s_mv_pred.i2_mvx]
180
771k
                                                                   + pu1_mv_bits[(i2_mvy << 2) - ps_mb_part->s_mv_pred.i2_mvy] ));
181
771k
        i4_cost[2] = i4_sad[2] + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ (i2_mvx << 2) - ps_mb_part->s_mv_pred.i2_mvx]
182
771k
                                                                   + pu1_mv_bits[((i2_mvy - 1) << 2) - ps_mb_part->s_mv_pred.i2_mvy] ));
183
771k
        i4_cost[3] = i4_sad[3] + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ (i2_mvx << 2) - ps_mb_part->s_mv_pred.i2_mvx]
184
771k
                                                                   + pu1_mv_bits[((i2_mvy + 1) << 2) - ps_mb_part->s_mv_pred.i2_mvy] ));
185
186
187
771k
        if (i4_cost_least > i4_cost[0])
188
238k
        {
189
238k
            i4_cost_least = i4_cost[0];
190
238k
            i4_distortion_least = i4_sad[0];
191
192
238k
            i2_mv_u_x = (i2_mvx - 1);
193
238k
            i2_mv_u_y = i2_mvy;
194
238k
        }
195
196
771k
        if (i4_cost_least > i4_cost[1])
197
191k
        {
198
191k
            i4_cost_least = i4_cost[1];
199
191k
            i4_distortion_least = i4_sad[1];
200
201
191k
            i2_mv_u_x = (i2_mvx + 1);
202
191k
            i2_mv_u_y = i2_mvy;
203
191k
        }
204
205
771k
        if (i4_cost_least > i4_cost[2])
206
187k
        {
207
187k
            i4_cost_least = i4_cost[2];
208
187k
            i4_distortion_least = i4_sad[2];
209
210
187k
            i2_mv_u_x = i2_mvx;
211
187k
            i2_mv_u_y = i2_mvy - 1;
212
187k
        }
213
214
771k
        if (i4_cost_least > i4_cost[3])
215
111k
        {
216
111k
            i4_cost_least = i4_cost[3];
217
111k
            i4_distortion_least = i4_sad[3];
218
219
111k
            i2_mv_u_x = i2_mvx;
220
111k
            i2_mv_u_y = i2_mvy + 1;
221
111k
        }
222
223
771k
        if( (i2_mv_u_x == i2_mvx) && (i2_mv_u_y == i2_mvy))
224
184k
        {
225
184k
            ps_mb_part->u4_exit = 1;
226
184k
            break;
227
184k
        }
228
587k
        else
229
587k
        {
230
587k
            i2_mvx = i2_mv_u_x;
231
587k
            i2_mvy = i2_mv_u_y;
232
587k
        }
233
587k
        u4_num_layers--;
234
587k
    }
235
236
249k
    if (i4_cost_least < ps_mb_part->i4_mb_cost)
237
110k
    {
238
110k
        ps_mb_part->i4_mb_cost = i4_cost_least;
239
110k
        ps_mb_part->i4_mb_distortion = i4_distortion_least;
240
110k
        ps_mb_part->s_mv_curr.i2_mvx = i2_mvx;
241
110k
        ps_mb_part->s_mv_curr.i2_mvy = i2_mvy;
242
110k
    }
243
244
249k
}
245
246
247
/**
248
*******************************************************************************
249
*
250
* @brief This function computes the best motion vector among the tentative mv
251
* candidates chosen.
252
*
253
* @par Description:
254
*  This function determines the position in the search window at which the motion
255
*  estimation should begin in order to minimise the number of search iterations.
256
*
257
* @param[in] ps_me_ctxt
258
*  pointer to me context
259
*
260
* @param[in] i4_reflist
261
*  ref list
262
*
263
* @returns  mv pair & corresponding distortion and cost
264
*
265
* @remarks none
266
*
267
*******************************************************************************
268
*/
269
270
void ime_evaluate_init_srchposn_16x16
271
        (
272
            me_ctxt_t *ps_me_ctxt,
273
            WORD32 i4_reflist
274
        )
275
249k
{
276
249k
    UWORD32 u4_lambda_motion = ps_me_ctxt->u4_lambda_motion;
277
278
    /* candidate mv cnt */
279
249k
    UWORD32 u4_num_candidates = ps_me_ctxt->u4_num_candidates[i4_reflist];
280
281
    /* list of candidate mvs */
282
249k
    ime_mv_t *ps_mv_list = ps_me_ctxt->as_mv_init_search[i4_reflist];
283
284
    /* pointer to src macro block */
285
249k
    UWORD8 *pu1_curr_mb = ps_me_ctxt->pu1_src_buf_luma;
286
249k
    UWORD8 *pu1_ref_mb = ps_me_ctxt->apu1_ref_buf_luma[i4_reflist];
287
288
    /* strides */
289
249k
    WORD32 i4_src_strd = ps_me_ctxt->i4_src_strd;
290
249k
    WORD32 i4_ref_strd = ps_me_ctxt->i4_rec_strd;
291
292
    /* enabled fast sad computation */
293
249k
    UWORD32 u4_enable_fast_sad = ps_me_ctxt->u4_enable_fast_sad;
294
295
    /* SAD(distortion metric) of an 8x8 block */
296
249k
    WORD32 i4_mb_distortion;
297
298
    /* cost = distortion + u4_lambda_motion * rate */
299
249k
    WORD32 i4_mb_cost, i4_mb_cost_least = INT_MAX, i4_distortion_least = INT_MAX;
300
301
    /* mb partitions info */
302
249k
    mb_part_ctxt *ps_mb_part = &(ps_me_ctxt->as_mb_part[i4_reflist]);
303
304
    /* mv bits */
305
249k
    UWORD8 *pu1_mv_bits = ps_me_ctxt->pu1_mv_bits;
306
307
    /* temp var */
308
249k
    UWORD32  i, j;
309
249k
    WORD32 i4_srch_pos_idx = 0;
310
249k
    UWORD8 *pu1_ref = NULL;
311
312
    /* Carry out a search using each of the motion vector pairs identified above as predictors. */
313
    /* TODO : Just like Skip, Do we need to add any bias to zero mv as well */
314
1.21M
    for(i = 0; i < u4_num_candidates; i++)
315
965k
    {
316
        /* compute sad */
317
965k
        WORD32 c_sad = 1;
318
319
1.50M
        for(j = 0; j < i; j++ )
320
1.02M
        {
321
1.02M
            if ( (ps_mv_list[i].i2_mvx == ps_mv_list[j].i2_mvx) &&
322
1.02M
                            (ps_mv_list[i].i2_mvy == ps_mv_list[j].i2_mvy) )
323
488k
            {
324
488k
                c_sad = 0;
325
488k
                break;
326
488k
            }
327
1.02M
        }
328
965k
        if(c_sad)
329
478k
        {
330
            /* adjust ref pointer */
331
478k
            pu1_ref = pu1_ref_mb + ps_mv_list[i].i2_mvx + (ps_mv_list[i].i2_mvy * i4_ref_strd);
332
333
            /* compute distortion */
334
478k
            ps_me_ctxt->pf_ime_compute_sad_16x16[u4_enable_fast_sad](pu1_curr_mb, pu1_ref, i4_src_strd, i4_ref_strd, i4_mb_cost_least, &i4_mb_distortion);
335
336
478k
            DEBUG_SAD_HISTOGRAM_ADD(i4_mb_distortion, 3);
337
338
            /* compute cost */
339
478k
            i4_mb_cost = i4_mb_distortion + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ (ps_mv_list[i].i2_mvx << 2) - ps_mb_part->s_mv_pred.i2_mvx]
340
478k
                            + pu1_mv_bits[(ps_mv_list[i].i2_mvy << 2) - ps_mb_part->s_mv_pred.i2_mvy] ));
341
342
478k
            if (i4_mb_cost < i4_mb_cost_least)
343
360k
            {
344
360k
                i4_mb_cost_least = i4_mb_cost;
345
346
360k
                i4_distortion_least = i4_mb_distortion;
347
348
360k
                i4_srch_pos_idx = i;
349
360k
            }
350
478k
        }
351
965k
    }
352
353
249k
    if (i4_mb_cost_least < ps_mb_part->i4_mb_cost)
354
249k
    {
355
249k
        ps_mb_part->i4_srch_pos_idx = i4_srch_pos_idx;
356
249k
        ps_mb_part->i4_mb_cost = i4_mb_cost_least;
357
249k
        ps_mb_part->i4_mb_distortion = i4_distortion_least;
358
249k
        ps_mb_part->s_mv_curr.i2_mvx = ps_mv_list[i4_srch_pos_idx].i2_mvx;
359
249k
        ps_mb_part->s_mv_curr.i2_mvy = ps_mv_list[i4_srch_pos_idx].i2_mvy;
360
249k
    }
361
249k
}
362
363
/**
364
*******************************************************************************
365
*
366
* @brief Searches for the best matching full pixel predictor within the search
367
* range
368
*
369
* @par Description:
370
*  For a given algorithm (diamond, Hex, nStep, ...) chosen, it searches for the
371
*  best matching full pixel predictor within the search range
372
*
373
* @param[in] ps_me_ctxt
374
*  pointer to me context
375
*
376
* @param[in] i4_reflist
377
*  ref list
378
*
379
* @returns  mv pair & corresponding distortion and cost
380
*
381
* @remarks none
382
*
383
*******************************************************************************
384
*/
385
void ime_full_pel_motion_estimation_16x16
386
    (
387
        me_ctxt_t *ps_me_ctxt,
388
        WORD32 i4_ref_list
389
    )
390
249k
{
391
    /* mb part info */
392
249k
    mb_part_ctxt *ps_mb_part = &ps_me_ctxt->as_mb_part[i4_ref_list];
393
394
    /******************************************************************/
395
    /* Modify Search range about initial candidate instead of zero mv */
396
    /******************************************************************/
397
    /*
398
     * FIXME: The motion vectors in a way can become unbounded. It may so happen that
399
     * MV might exceed the limit of the profile configured.
400
     */
401
249k
    ps_me_ctxt->i4_srch_range_w = MAX(ps_me_ctxt->i4_srch_range_w,
402
249k
                                      -ps_me_ctxt->ai2_srch_boundaries[0] + ps_mb_part->s_mv_curr.i2_mvx);
403
249k
    ps_me_ctxt->i4_srch_range_e = MIN(ps_me_ctxt->i4_srch_range_e,
404
249k
                                       ps_me_ctxt->ai2_srch_boundaries[0] + ps_mb_part->s_mv_curr.i2_mvx);
405
249k
    ps_me_ctxt->i4_srch_range_n = MAX(ps_me_ctxt->i4_srch_range_n,
406
249k
                                      -ps_me_ctxt->ai2_srch_boundaries[1] + ps_mb_part->s_mv_curr.i2_mvy);
407
249k
    ps_me_ctxt->i4_srch_range_s = MIN(ps_me_ctxt->i4_srch_range_s,
408
249k
                                       ps_me_ctxt->ai2_srch_boundaries[1] + ps_mb_part->s_mv_curr.i2_mvy);
409
410
    /************************************************************/
411
    /* Traverse about best initial candidate for mv             */
412
    /************************************************************/
413
414
249k
    switch (ps_me_ctxt->u4_me_speed_preset)
415
249k
    {
416
249k
        case DMND_SRCH:
417
249k
            ime_diamond_search_16x16(ps_me_ctxt, i4_ref_list);
418
249k
            break;
419
0
        default:
420
0
            assert(0);
421
0
            break;
422
249k
    }
423
249k
}
424
425
/**
426
*******************************************************************************
427
*
428
* @brief Searches for the best matching sub pixel predictor within the search
429
* range
430
*
431
* @par Description:
432
*  This function begins by searching across all sub pixel sample points
433
*  around the full pel motion vector. The vector with least cost is chosen as
434
*  the mv for the current mb.
435
*
436
* @param[in] ps_me_ctxt
437
*  pointer to me context
438
*
439
* @param[in] i4_reflist
440
*  ref list
441
*
442
* @returns mv pair & corresponding distortion and cost
443
*
444
* @remarks none
445
*
446
*******************************************************************************
447
*/
448
void ime_sub_pel_motion_estimation_16x16
449
    (
450
        me_ctxt_t *ps_me_ctxt,
451
        WORD32 i4_reflist
452
    )
453
163k
{
454
    /* pointers to src & ref macro block */
455
163k
    UWORD8 *pu1_curr_mb = ps_me_ctxt->pu1_src_buf_luma;
456
457
    /* pointers to ref. half pel planes */
458
163k
    UWORD8 *pu1_ref_mb_half_x;
459
163k
    UWORD8 *pu1_ref_mb_half_y;
460
163k
    UWORD8 *pu1_ref_mb_half_xy;
461
462
    /* pointers to ref. half pel planes */
463
163k
    UWORD8 *pu1_ref_mb_half_x_temp;
464
163k
    UWORD8 *pu1_ref_mb_half_y_temp;
465
163k
    UWORD8 *pu1_ref_mb_half_xy_temp;
466
467
    /* strides */
468
163k
    WORD32 i4_src_strd = ps_me_ctxt->i4_src_strd;
469
470
163k
    WORD32 i4_ref_strd = ps_me_ctxt->u4_subpel_buf_strd;
471
472
    /* mb partitions info */
473
163k
    mb_part_ctxt *ps_mb_part = &ps_me_ctxt->as_mb_part[i4_reflist];
474
475
    /* SAD(distortion metric) of an mb */
476
163k
    WORD32 i4_mb_distortion;
477
163k
    WORD32 i4_distortion_least = ps_mb_part->i4_mb_distortion;
478
479
    /* cost = distortion + u4_lambda_motion * rate */
480
163k
    WORD32 i4_mb_cost;
481
163k
    WORD32 i4_mb_cost_least = ps_mb_part->i4_mb_cost;
482
483
    /*Best half pel buffer*/
484
163k
    UWORD8 *pu1_best_hpel_buf = NULL;
485
486
    /* mv bits */
487
163k
    UWORD8 *pu1_mv_bits = ps_me_ctxt->pu1_mv_bits;
488
489
    /* Motion vectors in full-pel units */
490
163k
    WORD16 mv_x, mv_y;
491
492
    /* lambda - lagrange constant */
493
163k
    UWORD32 u4_lambda_motion = ps_me_ctxt->u4_lambda_motion;
494
495
    /* Flags to check if half pel points needs to be evaluated */
496
    /**************************************/
497
    /* 1 bit for each half pel candidate  */
498
    /* bit 0 - half x = 1, half y = 0     */
499
    /* bit 1 - half x = -1, half y = 0    */
500
    /* bit 2 - half x = 0, half y = 1     */
501
    /* bit 3 - half x = 0, half y = -1    */
502
    /* bit 4 - half x = 1, half y = 1     */
503
    /* bit 5 - half x = -1, half y = 1    */
504
    /* bit 6 - half x = 1, half y = -1    */
505
    /* bit 7 - half x = -1, half y = -1   */
506
    /**************************************/
507
    /* temp var */
508
163k
    WORD16 i2_mv_u_x, i2_mv_u_y;
509
163k
    WORD32 i, j;
510
163k
    WORD32 ai4_sad[8];
511
512
163k
    WORD32 i4_srch_pos_idx = ps_mb_part->i4_srch_pos_idx;
513
514
163k
    i2_mv_u_x = ps_mb_part->s_mv_curr.i2_mvx;
515
163k
    i2_mv_u_y = ps_mb_part->s_mv_curr.i2_mvy;
516
517
    /************************************************************/
518
    /* Evaluate half pel                                        */
519
    /************************************************************/
520
163k
    mv_x = ps_mb_part->s_mv_curr.i2_mvx >> 2;
521
163k
    mv_y = ps_mb_part->s_mv_curr.i2_mvy >> 2;
522
523
524
    /**************************************************************/
525
    /* ps_me_ctxt->pu1_half_x points to the half pel pixel on the */
526
    /* left side of full pel                                      */
527
    /* ps_me_ctxt->pu1_half_y points to the half pel pixel on the */
528
    /* top  side of full pel                                      */
529
    /* ps_me_ctxt->pu1_half_xy points to the half pel pixel       */
530
    /* on the top left side of full pel                           */
531
    /* for the function pf_ime_sub_pel_compute_sad_16x16 the      */
532
    /* default postions are                                       */
533
    /* ps_me_ctxt->pu1_half_x = right halp_pel                    */
534
    /*  ps_me_ctxt->pu1_half_y = bottom halp_pel                  */
535
    /*  ps_me_ctxt->pu1_half_xy = bottom right halp_pel           */
536
    /* Hence corresponding adjustments made here                  */
537
    /**************************************************************/
538
539
163k
    pu1_ref_mb_half_x_temp = pu1_ref_mb_half_x = ps_me_ctxt->apu1_subpel_buffs[0] + 1;
540
163k
    pu1_ref_mb_half_y_temp = pu1_ref_mb_half_y = ps_me_ctxt->apu1_subpel_buffs[1] + 1 + i4_ref_strd;
541
163k
    pu1_ref_mb_half_xy_temp = pu1_ref_mb_half_xy = ps_me_ctxt->apu1_subpel_buffs[2] + 1 + i4_ref_strd;
542
543
163k
    ps_me_ctxt->pf_ime_sub_pel_compute_sad_16x16(pu1_curr_mb, pu1_ref_mb_half_x,
544
163k
                                                 pu1_ref_mb_half_y,
545
163k
                                                 pu1_ref_mb_half_xy,
546
163k
                                                 i4_src_strd, i4_ref_strd,
547
163k
                                                 ai4_sad);
548
549
    /* Half x plane */
550
491k
    for(i = 0; i < 2; i++)
551
327k
    {
552
327k
        WORD32 mv_x_tmp = (mv_x << 2) + 2;
553
327k
        WORD32 mv_y_tmp = (mv_y << 2);
554
555
327k
        mv_x_tmp -= (i * 4);
556
557
327k
        i4_mb_distortion = ai4_sad[i];
558
559
        /* compute cost */
560
327k
        i4_mb_cost = i4_mb_distortion + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ mv_x_tmp - ps_mb_part->s_mv_pred.i2_mvx]
561
327k
                        + pu1_mv_bits[mv_y_tmp - ps_mb_part->s_mv_pred.i2_mvy] ));
562
563
327k
        if (i4_mb_cost < i4_mb_cost_least)
564
60.8k
        {
565
60.8k
            i4_mb_cost_least = i4_mb_cost;
566
567
60.8k
            i4_distortion_least = i4_mb_distortion;
568
569
60.8k
            i2_mv_u_x = mv_x_tmp;
570
571
60.8k
            i2_mv_u_y = mv_y_tmp;
572
573
60.8k
#ifndef HP_PL /*choosing whether left or right half_x*/
574
60.8k
            ps_me_ctxt->apu1_subpel_buffs[0] = pu1_ref_mb_half_x_temp - i;
575
60.8k
            pu1_best_hpel_buf = pu1_ref_mb_half_x_temp - i;
576
577
60.8k
            i4_srch_pos_idx = 0;
578
60.8k
#endif
579
60.8k
        }
580
581
327k
    }
582
583
    /* Half y plane */
584
491k
    for(i = 0; i < 2; i++)
585
327k
    {
586
327k
        WORD32 mv_x_tmp = (mv_x << 2);
587
327k
        WORD32 mv_y_tmp = (mv_y << 2) + 2;
588
589
327k
        mv_y_tmp -= (i * 4);
590
591
327k
        i4_mb_distortion = ai4_sad[2 + i];
592
593
        /* compute cost */
594
327k
        i4_mb_cost = i4_mb_distortion + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ mv_x_tmp - ps_mb_part->s_mv_pred.i2_mvx]
595
327k
                        + pu1_mv_bits[mv_y_tmp - ps_mb_part->s_mv_pred.i2_mvy] ));
596
597
327k
        if (i4_mb_cost < i4_mb_cost_least)
598
44.3k
        {
599
44.3k
            i4_mb_cost_least = i4_mb_cost;
600
601
44.3k
            i4_distortion_least = i4_mb_distortion;
602
603
44.3k
            i2_mv_u_x = mv_x_tmp;
604
605
44.3k
            i2_mv_u_y = mv_y_tmp;
606
607
44.3k
#ifndef HP_PL/*choosing whether top or bottom half_y*/
608
44.3k
            ps_me_ctxt->apu1_subpel_buffs[1] = pu1_ref_mb_half_y_temp  - i*(i4_ref_strd);
609
44.3k
            pu1_best_hpel_buf = pu1_ref_mb_half_y_temp  - i*(i4_ref_strd);
610
611
44.3k
            i4_srch_pos_idx = 1;
612
44.3k
#endif
613
44.3k
        }
614
615
327k
    }
616
617
    /* Half xy plane */
618
491k
    for(j = 0; j < 2; j++)
619
327k
    {
620
982k
        for(i = 0; i < 2; i++)
621
655k
        {
622
655k
            WORD32 mv_x_tmp = (mv_x << 2) + 2;
623
655k
            WORD32 mv_y_tmp = (mv_y << 2) + 2;
624
625
655k
            mv_x_tmp -= (i * 4);
626
655k
            mv_y_tmp -= (j * 4);
627
628
655k
            i4_mb_distortion = ai4_sad[4 + i + 2 * j];
629
630
            /* compute cost */
631
655k
            i4_mb_cost = i4_mb_distortion + (WORD32)(u4_lambda_motion * ( pu1_mv_bits[ mv_x_tmp - ps_mb_part->s_mv_pred.i2_mvx]
632
655k
                            + pu1_mv_bits[mv_y_tmp - ps_mb_part->s_mv_pred.i2_mvy] ));
633
634
655k
            if (i4_mb_cost < i4_mb_cost_least)
635
35.1k
            {
636
35.1k
                i4_mb_cost_least = i4_mb_cost;
637
638
35.1k
                i4_distortion_least = i4_mb_distortion;
639
640
35.1k
                i2_mv_u_x = mv_x_tmp;
641
642
35.1k
                i2_mv_u_y = mv_y_tmp;
643
644
35.1k
#ifndef HP_PL /*choosing between four half_xy */
645
35.1k
                ps_me_ctxt->apu1_subpel_buffs[2] = pu1_ref_mb_half_xy_temp  - j*(i4_ref_strd) - i;
646
35.1k
                pu1_best_hpel_buf =  pu1_ref_mb_half_xy_temp  - j*(i4_ref_strd) - i;
647
648
35.1k
                i4_srch_pos_idx = 2;
649
35.1k
#endif
650
35.1k
            }
651
652
655k
        }
653
327k
    }
654
655
163k
    if (i4_mb_cost_least < ps_mb_part->i4_mb_cost)
656
77.3k
    {
657
77.3k
        ps_mb_part->i4_mb_cost = i4_mb_cost_least;
658
77.3k
        ps_mb_part->i4_mb_distortion = i4_distortion_least;
659
77.3k
        ps_mb_part->s_mv_curr.i2_mvx = i2_mv_u_x;
660
77.3k
        ps_mb_part->s_mv_curr.i2_mvy = i2_mv_u_y;
661
77.3k
        ps_mb_part->pu1_best_hpel_buf = pu1_best_hpel_buf;
662
77.3k
        ps_mb_part->i4_srch_pos_idx = i4_srch_pos_idx;
663
77.3k
    }
664
163k
}
665
666
/**
667
*******************************************************************************
668
*
669
* @brief This function computes cost of skip macroblocks
670
*
671
* @par Description:
672
*
673
* @param[in] ps_me_ctxt
674
*  pointer to me ctxt
675
*
676
*
677
* @returns  none
678
*
679
* @remarks
680
* NOTE: while computing the skip cost, do not enable early exit from compute
681
* sad function because, a negative bias gets added later
682
* Note that the last ME candidate in me ctxt is taken as skip motion vector
683
*
684
*******************************************************************************
685
*/
686
void ime_compute_skip_cost
687
    (
688
         me_ctxt_t *ps_me_ctxt,
689
         ime_mv_t *ps_skip_mv,
690
         mb_part_ctxt *ps_smb_part_info,
691
         UWORD32 u4_use_stat_sad,
692
         WORD32 i4_reflist,
693
         WORD32 i4_is_slice_type_b
694
    )
695
137k
{
696
697
    /* SAD(distortion metric) of an mb */
698
137k
    WORD32 i4_mb_distortion;
699
700
    /* cost = distortion + u4_lambda_motion * rate */
701
137k
    WORD32 i4_mb_cost;
702
703
    /* temp var */
704
137k
    UWORD8 *pu1_ref = NULL;
705
706
137k
    ime_mv_t s_skip_mv;
707
708
137k
    s_skip_mv.i2_mvx = (ps_skip_mv->i2_mvx +2)>>2;
709
137k
    s_skip_mv.i2_mvy = (ps_skip_mv->i2_mvy +2)>>2;
710
711
    /* Check if the skip mv is out of bounds or subpel */
712
137k
    {
713
        /* skip mv */
714
137k
        ime_mv_t s_clip_skip_mv;
715
716
137k
        s_clip_skip_mv.i2_mvx = CLIP3(ps_me_ctxt->i4_srch_range_w, ps_me_ctxt->i4_srch_range_e, s_skip_mv.i2_mvx);
717
137k
        s_clip_skip_mv.i2_mvy = CLIP3(ps_me_ctxt->i4_srch_range_n, ps_me_ctxt->i4_srch_range_s, s_skip_mv.i2_mvy);
718
719
137k
        if ((s_clip_skip_mv.i2_mvx != s_skip_mv.i2_mvx) ||
720
137k
           (s_clip_skip_mv.i2_mvy != s_skip_mv.i2_mvy) ||
721
137k
           (ps_skip_mv->i2_mvx & 0x3) ||
722
137k
           (ps_skip_mv->i2_mvy & 0x3))
723
16.6k
        {
724
16.6k
            return ;
725
16.6k
        }
726
137k
    }
727
728
729
    /* adjust ref pointer */
730
120k
    pu1_ref = ps_me_ctxt->apu1_ref_buf_luma[i4_reflist] + s_skip_mv.i2_mvx
731
120k
                    + (s_skip_mv.i2_mvy * ps_me_ctxt->i4_rec_strd);
732
733
120k
    if(u4_use_stat_sad == 1)
734
121k
    {
735
121k
        UWORD32 u4_is_nonzero;
736
737
121k
        ps_me_ctxt->pf_ime_compute_sad_stat_luma_16x16(
738
121k
                        ps_me_ctxt->pu1_src_buf_luma, pu1_ref, ps_me_ctxt->i4_src_strd,
739
121k
                        ps_me_ctxt->i4_rec_strd, ps_me_ctxt->pu2_sad_thrsh,
740
121k
                        &i4_mb_distortion, &u4_is_nonzero);
741
742
121k
        if (u4_is_nonzero == 0 || i4_mb_distortion <= ps_me_ctxt->i4_min_sad)
743
19.8k
        {
744
19.8k
            ps_me_ctxt->u4_min_sad_reached = 1; /* found min sad */
745
19.8k
            ps_me_ctxt->i4_min_sad = (u4_is_nonzero == 0) ? 0 : i4_mb_distortion;
746
19.8k
        }
747
121k
    }
748
18.4E
    else
749
18.4E
    {
750
18.4E
        ps_me_ctxt->pf_ime_compute_sad_16x16[ps_me_ctxt->u4_enable_fast_sad](
751
18.4E
                        ps_me_ctxt->pu1_src_buf_luma, pu1_ref, ps_me_ctxt->i4_src_strd,
752
18.4E
                        ps_me_ctxt->i4_rec_strd, INT_MAX, &i4_mb_distortion);
753
754
18.4E
        if(i4_mb_distortion <= ps_me_ctxt->i4_min_sad)
755
0
        {
756
0
            ps_me_ctxt->i4_min_sad = i4_mb_distortion;
757
0
            ps_me_ctxt->u4_min_sad_reached = 1; /* found min sad */
758
0
        }
759
18.4E
    }
760
761
762
    /* for skip mode cost & distortion are identical
763
     * But we shall add a bias to favor skip mode.
764
     * Doc. JVT B118 Suggests SKIP_BIAS as 16.
765
     * TODO : Empirical analysis of SKIP_BIAS is necessary */
766
767
120k
    i4_mb_cost = i4_mb_distortion - (ps_me_ctxt->u4_lambda_motion * (ps_me_ctxt->i4_skip_bias[0] + ps_me_ctxt->i4_skip_bias[1]  * i4_is_slice_type_b));
768
769
120k
    if (i4_mb_cost <= ps_smb_part_info->i4_mb_cost)
770
121k
    {
771
121k
        ps_smb_part_info->i4_mb_cost = i4_mb_cost;
772
121k
        ps_smb_part_info->i4_mb_distortion = i4_mb_distortion;
773
121k
        ps_smb_part_info->s_mv_curr.i2_mvx = s_skip_mv.i2_mvx;
774
121k
        ps_smb_part_info->s_mv_curr.i2_mvy = s_skip_mv.i2_mvy;
775
121k
    }
776
120k
}
777