summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/avc/enc/src/findhalfpel.cpp
blob: d0bbee291358335a1391c0428b83da78e44e58ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
#include "avcenc_lib.h"
/* 3/29/01 fast half-pel search based on neighboring guess */
/* value ranging from 0 to 4, high complexity (more accurate) to
   low complexity (less accurate) */
#define HP_DISTANCE_TH      5 // 2  /* half-pel distance threshold */

#define PREF_16_VEC 129     /* 1MV bias versus 4MVs*/

#define CLIP_RESULT(x)      if((uint)x > 0xFF){ \
                 x = 0xFF & (~(x>>31));}

#define CLIP_UPPER16(x)     if((uint)x >= 0x20000000){ \
        x = 0xFF0000 & (~(x>>31));} \
        else { \
        x = (x>>5)&0xFF0000; \
        }

/*=====================================================================
    Function:   AVCFindHalfPelMB
    Date:       10/31/2007
    Purpose:    Find half pel resolution MV surrounding the full-pel MV
=====================================================================*/

int AVCFindHalfPelMB(AVCEncObject *encvid, uint8 *cur, AVCMV *mot, uint8 *ncand,
                     int xpos, int ypos, int hp_guess, int cmvx, int cmvy)
{
    AVCPictureData *currPic = encvid->common->currPic;
    int lx = currPic->pitch;
    int d, dmin, satd_min;
    uint8* cand;
    int lambda_motion = encvid->lambda_motion;
    uint8 *mvbits = encvid->mvbits;
    int mvcost;
    /* list of candidate to go through for half-pel search*/
    uint8 *subpel_pred = (uint8*) encvid->subpel_pred; // all 16 sub-pel positions
    uint8 **hpel_cand = (uint8**) encvid->hpel_cand; /* half-pel position */

    int xh[9] = {0, 0, 2, 2, 2, 0, -2, -2, -2};
    int yh[9] = {0, -2, -2, 0, 2, 2, 2, 0, -2};
    int xq[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    int yq[8] = { -1, -1, 0, 1, 1, 1, 0, -1};
    int h, hmin, q, qmin;

    OSCL_UNUSED_ARG(xpos);
    OSCL_UNUSED_ARG(ypos);
    OSCL_UNUSED_ARG(hp_guess);

    GenerateHalfPelPred(subpel_pred, ncand, lx);

    cur = encvid->currYMB; // pre-load current original MB

    cand = hpel_cand[0];

    // find cost for the current full-pel position
    dmin = SATD_MB(cand, cur, 65535); // get Hadamaard transform SAD
    mvcost = MV_COST_S(lambda_motion, mot->x, mot->y, cmvx, cmvy);
    satd_min = dmin;
    dmin += mvcost;
    hmin = 0;

    /* find half-pel */
    for (h = 1; h < 9; h++)
    {
        d = SATD_MB(hpel_cand[h], cur, dmin);
        mvcost = MV_COST_S(lambda_motion, mot->x + xh[h], mot->y + yh[h], cmvx, cmvy);
        d += mvcost;

        if (d < dmin)
        {
            dmin = d;
            hmin = h;
            satd_min = d - mvcost;
        }
    }

    mot->sad = dmin;
    mot->x += xh[hmin];
    mot->y += yh[hmin];
    encvid->best_hpel_pos = hmin;

    /*** search for quarter-pel ****/
    GenerateQuartPelPred(encvid->bilin_base[hmin], &(encvid->qpel_cand[0][0]), hmin);

    encvid->best_qpel_pos = qmin = -1;

    for (q = 0; q < 8; q++)
    {
        d = SATD_MB(encvid->qpel_cand[q], cur, dmin);
        mvcost = MV_COST_S(lambda_motion, mot->x + xq[q], mot->y + yq[q], cmvx, cmvy);
        d += mvcost;
        if (d < dmin)
        {
            dmin = d;
            qmin = q;
            satd_min = d - mvcost;
        }
    }

    if (qmin != -1)
    {
        mot->sad = dmin;
        mot->x += xq[qmin];
        mot->y += yq[qmin];
        encvid->best_qpel_pos = qmin;
    }

    return satd_min;
}



/** This function generates sub-pel prediction around the full-pel candidate.
Each sub-pel position array is 20 pixel wide (for word-alignment) and 17 pixel tall. */
/** The sub-pel position is labeled in spiral manner from the center. */

void GenerateHalfPelPred(uint8* subpel_pred, uint8 *ncand, int lx)
{
    /* let's do straightforward way first */
    uint8 *ref;
    uint8 *dst;
    uint8 tmp8;
    int32 tmp32;
    int16 tmp_horz[18*22], *dst_16, *src_16;
    int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0; // temp
    int i, j;

    /* first copy full-pel to the first array */
    /* to be optimized later based on byte-offset load */
    ref = ncand - 3 - lx - (lx << 1); /* move back (-3,-3) */
    dst = subpel_pred;

    dst -= 4; /* offset */
    for (j = 0; j < 22; j++) /* 24x22 */
    {
        i = 6;
        while (i > 0)
        {
            tmp32 = *ref++;
            tmp8 = *ref++;
            tmp32 |= (tmp8 << 8);
            tmp8 = *ref++;
            tmp32 |= (tmp8 << 16);
            tmp8 = *ref++;
            tmp32 |= (tmp8 << 24);
            *((uint32*)(dst += 4)) = tmp32;
            i--;
        }
        ref += (lx - 24);
    }

    /* from the first array, we do horizontal interp */
    ref = subpel_pred + 2;
    dst_16 = tmp_horz; /* 17 x 22 */

    for (j = 4; j > 0; j--)
    {
        for (i = 16; i > 0; i -= 4)
        {
            a = ref[-2];
            b = ref[-1];
            c = ref[0];
            d = ref[1];
            e = ref[2];
            f = ref[3];
            *dst_16++ = a + f - 5 * (b + e) + 20 * (c + d);
            a = ref[4];
            *dst_16++ = b + a - 5 * (c + f) + 20 * (d + e);
            b = ref[5];
            *dst_16++ = c + b - 5 * (d + a) + 20 * (e + f);
            c = ref[6];
            *dst_16++ = d + c - 5 * (e + b) + 20 * (f + a);

            ref += 4;
        }
        /* do the 17th column here */
        d = ref[3];
        *dst_16 =  e + d - 5 * (f + c) + 20 * (a + b);
        dst_16 += 2; /* stride for tmp_horz is 18 */
        ref += 8;  /* stride for ref is 24 */
        if (j == 3)  // move 18 lines down
        {
            dst_16 += 324;//18*18;
            ref += 432;//18*24;
        }
    }

    ref -= 480;//20*24;
    dst_16 -= 360;//20*18;
    dst = subpel_pred + V0Q_H2Q * SUBPEL_PRED_BLK_SIZE; /* go to the 14th array 17x18*/

    for (j = 18; j > 0; j--)
    {
        for (i = 16; i > 0; i -= 4)
        {
            a = ref[-2];
            b = ref[-1];
            c = ref[0];
            d = ref[1];
            e = ref[2];
            f = ref[3];
            tmp32 = a + f - 5 * (b + e) + 20 * (c + d);
            *dst_16++ = tmp32;
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *dst++ = tmp32;

            a = ref[4];
            tmp32 = b + a - 5 * (c + f) + 20 * (d + e);
            *dst_16++ = tmp32;
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *dst++ = tmp32;

            b = ref[5];
            tmp32 = c + b - 5 * (d + a) + 20 * (e + f);
            *dst_16++ = tmp32;
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *dst++ = tmp32;

            c = ref[6];
            tmp32 = d + c - 5 * (e + b) + 20 * (f + a);
            *dst_16++ = tmp32;
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *dst++ = tmp32;

            ref += 4;
        }
        /* do the 17th column here */
        d = ref[3];
        tmp32 =  e + d - 5 * (f + c) + 20 * (a + b);
        *dst_16 = tmp32;
        tmp32 = (tmp32 + 16) >> 5;
        CLIP_RESULT(tmp32)
        *dst = tmp32;

        dst += 8;  /* stride for dst is 24 */
        dst_16 += 2; /* stride for tmp_horz is 18 */
        ref += 8;  /* stride for ref is 24 */
    }


    /* Do middle point filtering*/
    src_16 = tmp_horz; /* 17 x 22 */
    dst = subpel_pred + V2Q_H2Q * SUBPEL_PRED_BLK_SIZE; /* 12th array 17x17*/
    dst -= 24; // offset
    for (i = 0; i < 17; i++)
    {
        for (j = 16; j > 0; j -= 4)
        {
            a = *src_16;
            b = *(src_16 += 18);
            c = *(src_16 += 18);
            d = *(src_16 += 18);
            e = *(src_16 += 18);
            f = *(src_16 += 18);

            tmp32 = a + f - 5 * (b + e) + 20 * (c + d);
            tmp32 = (tmp32 + 512) >> 10;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;

            a = *(src_16 += 18);
            tmp32 = b + a - 5 * (c + f) + 20 * (d + e);
            tmp32 = (tmp32 + 512) >> 10;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;

            b = *(src_16 += 18);
            tmp32 = c + b - 5 * (d + a) + 20 * (e + f);
            tmp32 = (tmp32 + 512) >> 10;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;

            c = *(src_16 += 18);
            tmp32 = d + c - 5 * (e + b) + 20 * (f + a);
            tmp32 = (tmp32 + 512) >> 10;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;

            src_16 -= (18 << 2);
        }

        d = src_16[90]; // 18*5
        tmp32 = e + d - 5 * (f + c) + 20 * (a + b);
        tmp32 = (tmp32 + 512) >> 10;
        CLIP_RESULT(tmp32)
        dst[24] = tmp32;

        src_16 -= ((18 << 4) - 1);
        dst -= ((24 << 4) - 1);
    }

    /* do vertical interpolation */
    ref = subpel_pred + 2;
    dst = subpel_pred + V2Q_H0Q * SUBPEL_PRED_BLK_SIZE; /* 10th array 18x17 */
    dst -= 24; // offset

    for (i = 2; i > 0; i--)
    {
        for (j = 16; j > 0; j -= 4)
        {
            a = *ref;
            b = *(ref += 24);
            c = *(ref += 24);
            d = *(ref += 24);
            e = *(ref += 24);
            f = *(ref += 24);

            tmp32 = a + f - 5 * (b + e) + 20 * (c + d);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            a = *(ref += 24);
            tmp32 = b + a - 5 * (c + f) + 20 * (d + e);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            b = *(ref += 24);
            tmp32 = c + b - 5 * (d + a) + 20 * (e + f);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            c = *(ref += 24);
            tmp32 = d + c - 5 * (e + b) + 20 * (f + a);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            ref -= (24 << 2);
        }

        d = ref[120]; // 24*5
        tmp32 = e + d - 5 * (f + c) + 20 * (a + b);
        tmp32 = (tmp32 + 16) >> 5;
        CLIP_RESULT(tmp32)
        dst[24] = tmp32;  // 10th

        dst -= ((24 << 4) - 1);
        ref -= ((24 << 4) - 1);
    }

    // note that using SIMD here doesn't help much, the cycle almost stays the same
    // one can just use the above code and change the for(i=2 to for(i=18
    for (i = 16; i > 0; i -= 4)
    {
        for (j = 17; j > 0; j--)
        {
            a = *((uint32*)ref); /* load 4 bytes */
            b = (a >> 8) & 0xFF00FF; /* second and fourth byte */
            a &= 0xFF00FF;

            c = *((uint32*)(ref + 120));
            d = (c >> 8) & 0xFF00FF;
            c &= 0xFF00FF;

            a += c;
            b += d;

            e = *((uint32*)(ref + 72)); /* e, f */
            f = (e >> 8) & 0xFF00FF;
            e &= 0xFF00FF;

            c = *((uint32*)(ref + 48)); /* c, d */
            d = (c >> 8) & 0xFF00FF;
            c &= 0xFF00FF;

            c += e;
            d += f;

            a += 20 * c;
            b += 20 * d;
            a += 0x100010;
            b += 0x100010;

            e = *((uint32*)(ref += 24)); /* e, f */
            f = (e >> 8) & 0xFF00FF;
            e &= 0xFF00FF;

            c = *((uint32*)(ref + 72)); /* c, d */
            d = (c >> 8) & 0xFF00FF;
            c &= 0xFF00FF;

            c += e;
            d += f;

            a -= 5 * c;
            b -= 5 * d;

            c = a << 16;
            d = b << 16;
            CLIP_UPPER16(a)
            CLIP_UPPER16(c)
            CLIP_UPPER16(b)
            CLIP_UPPER16(d)

            a |= (c >> 16);
            b |= (d >> 16);
            //  a>>=5;
            //  b>>=5;
            /* clip */
            //  msk |= b;  msk|=a;
            //  a &= 0xFF00FF;
            //  b &= 0xFF00FF;
            a |= (b << 8);  /* pack it back */

            *((uint16*)(dst += 24)) = a & 0xFFFF; //dst is not word-aligned.
            *((uint16*)(dst + 2)) = a >> 16;

        }
        dst -= 404; // 24*17-4
        ref -= 404;
        /*      if(msk & 0xFF00FF00) // need clipping
                {
                    VertInterpWClip(dst,ref); // re-do 4 column with clip
                }*/
    }

    return ;
}

void VertInterpWClip(uint8 *dst, uint8 *ref)
{
    int i, j;
    int a, b, c, d, e, f;
    int32 tmp32;

    dst -= 4;
    ref -= 4;

    for (i = 4; i > 0; i--)
    {
        for (j = 16; j > 0; j -= 4)
        {
            a = *ref;
            b = *(ref += 24);
            c = *(ref += 24);
            d = *(ref += 24);
            e = *(ref += 24);
            f = *(ref += 24);

            tmp32 = a + f - 5 * (b + e) + 20 * (c + d);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            a = *(ref += 24);
            tmp32 = b + a - 5 * (c + f) + 20 * (d + e);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            b = *(ref += 24);
            tmp32 = c + b - 5 * (d + a) + 20 * (e + f);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            c = *(ref += 24);
            tmp32 = d + c - 5 * (e + b) + 20 * (f + a);
            tmp32 = (tmp32 + 16) >> 5;
            CLIP_RESULT(tmp32)
            *(dst += 24) = tmp32;  // 10th

            ref -= (24 << 2);
        }

        d = ref[120]; // 24*5
        tmp32 = e + d - 5 * (f + c) + 20 * (a + b);
        tmp32 = (tmp32 + 16) >> 5;
        CLIP_RESULT(tmp32)
        dst[24] = tmp32;  // 10th

        dst -= ((24 << 4) - 1);
        ref -= ((24 << 4) - 1);
    }

    return ;
}


void GenerateQuartPelPred(uint8 **bilin_base, uint8 *qpel_cand, int hpel_pos)
{
    // for even value of hpel_pos, start with pattern 1, otherwise, start with pattern 2
    int i, j;

    uint8 *c1 = qpel_cand;
    uint8 *tl = bilin_base[0];
    uint8 *tr = bilin_base[1];
    uint8 *bl = bilin_base[2];
    uint8 *br = bilin_base[3];
    int a, b, c, d;
    int offset = 1 - (384 * 7);

    if (!(hpel_pos&1)) // diamond pattern
    {
        j = 16;
        while (j--)
        {
            i = 16;
            while (i--)
            {
                d = tr[24];
                a = *tr++;
                b = bl[1];
                c = *br++;

                *c1 = (c + a + 1) >> 1;
                *(c1 += 384) = (b + a + 1) >> 1; /* c2 */
                *(c1 += 384) = (b + c + 1) >> 1; /* c3 */
                *(c1 += 384) = (b + d + 1) >> 1; /* c4 */

                b = *bl++;

                *(c1 += 384) = (c + d + 1) >> 1;  /* c5 */
                *(c1 += 384) = (b + d + 1) >> 1;  /* c6 */
                *(c1 += 384) = (b + c + 1) >> 1;  /* c7 */
                *(c1 += 384) = (b + a + 1) >> 1;  /* c8 */

                c1 += offset;
            }
            // advance to the next line, pitch is 24
            tl += 8;
            tr += 8;
            bl += 8;
            br += 8;
            c1 += 8;
        }
    }
    else // star pattern
    {
        j = 16;
        while (j--)
        {
            i = 16;
            while (i--)
            {
                a = *br++;
                b = *tr++;
                c = tl[1];
                *c1 = (a + b + 1) >> 1;
                b = bl[1];
                *(c1 += 384) = (a + c + 1) >> 1; /* c2 */
                c = tl[25];
                *(c1 += 384) = (a + b + 1) >> 1; /* c3 */
                b = tr[23];
                *(c1 += 384) = (a + c + 1) >> 1; /* c4 */
                c = tl[24];
                *(c1 += 384) = (a + b + 1) >> 1; /* c5 */
                b = *bl++;
                *(c1 += 384) = (a + c + 1) >> 1; /* c6 */
                c = *tl++;
                *(c1 += 384) = (a + b + 1) >> 1; /* c7 */
                *(c1 += 384) = (a + c + 1) >> 1; /* c8 */

                c1 += offset;
            }
            // advance to the next line, pitch is 24
            tl += 8;
            tr += 8;
            bl += 8;
            br += 8;
            c1 += 8;
        }
    }

    return ;
}


/* assuming cand always has a pitch of 24 */
int SATD_MB(uint8 *cand, uint8 *cur, int dmin)
{
    int cost;


    dmin = (dmin << 16) | 24;
    cost = AVCSAD_Macroblock_C(cand, cur, dmin, NULL);

    return cost;
}