159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ------------------------------------------------------------------
259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * Copyright (C) 1998-2009 PacketVideo
359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *
459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * Licensed under the Apache License, Version 2.0 (the "License");
559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * you may not use this file except in compliance with the License.
659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * You may obtain a copy of the License at
759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *
859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *      http://www.apache.org/licenses/LICENSE-2.0
959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *
1059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * Unless required by applicable law or agreed to in writing, software
1159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * distributed under the License is distributed on an "AS IS" BASIS,
1259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * express or implied.
1459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * See the License for the specific language governing permissions
1559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * and limitations under the License.
1659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * -------------------------------------------------------------------
1759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong */
1859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "mp4def.h"
1959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "mp4lib_int.h"
2059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
2159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "sad_inline.h"
2259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
2359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define Cached_lx 176
2459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
2559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef _SAD_STAT
2659f566c4ec3dfc097ad8163523e522280b27e5c3James DongULong num_sad_MB = 0;
2759f566c4ec3dfc097ad8163523e522280b27e5c3James DongULong num_sad_Blk = 0;
2859f566c4ec3dfc097ad8163523e522280b27e5c3James DongULong num_sad_MB_call = 0;
2959f566c4ec3dfc097ad8163523e522280b27e5c3James DongULong num_sad_Blk_call = 0;
3059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
3159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_MB_CALL()       num_sad_MB_call++
3259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_MB()            num_sad_MB++
3359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_BLK_CALL()      num_sad_Blk_call++
3459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_BLK()           num_sad_Blk++
3559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
3659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#else
3759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
3859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_MB_CALL()
3959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_MB()
4059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_BLK_CALL()
4159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define NUM_SAD_BLK()
4259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
4359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
4459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
4559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
4659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* consist of
4759f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_Macroblock_C(UChar *ref,UChar *blk,Int dmin,Int lx,void *extra_info)
4859f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_MB_HTFM_Collect(UChar *ref,UChar *blk,Int dmin,Int lx,void *extra_info)
4959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_MB_HTFM(UChar *ref,UChar *blk,Int dmin,Int lx,void *extra_info)
5059f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_Block_C(UChar *ref,UChar *blk,Int dmin,Int lx,void *extra_info)
5159f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_Blk_PADDING(UChar *ref,UChar *cur,Int dmin,Int lx,void *extra_info)
5259f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_MB_PADDING(UChar *ref,UChar *cur,Int dmin,Int lx,void *extra_info)
5359f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_MB_PAD1(UChar *ref,UChar *cur,Int dmin,Int lx,Int *rep);
5459f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_MB_PADDING_HTFM_Collect(UChar *ref,UChar *cur,Int dmin,Int lx,void *extra_info)
5559f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt SAD_MB_PADDING_HTFM(UChar *ref,UChar *cur,Int dmin,Int lx,void *vptr)
5659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*/
5759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
5859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
5959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef __cplusplus
6059f566c4ec3dfc097ad8163523e522280b27e5c3James Dongextern "C"
6159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
6259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
6359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
6459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int SAD_MB_PAD1(UChar *ref, UChar *cur, Int dmin, Int lx, Int *rep);
6559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
6659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
6759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*==================================================================
6859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Function:   SAD_Macroblock
6959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Date:       09/07/2000
7059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Purpose:    Compute SAD 16x16 between blk and ref.
7159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        To do:      Uniform subsampling will be inserted later!
7259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    Hypothesis Testing Fast Matching to be used later!
7359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Changes:
7459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    11/7/00:     implemented MMX
7559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    1/24/01:     implemented SSE
7659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    ==================================================================*/
7759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /********** C ************/
7859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int SAD_Macroblock_C(UChar *ref, UChar *blk, Int dmin_lx, void *extra_info)
7959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
8059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        int32 x10;
8159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int dmin = (ULong)dmin_lx >> 16;
8259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int lx = dmin_lx & 0xFFFF;
8359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
8459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        OSCL_UNUSED_ARG(extra_info);
8559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
8659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        NUM_SAD_MB_CALL();
8759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
8859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        x10 = simd_sad_mb(ref, blk, dmin, lx);
8959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
9059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return x10;
9159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
9259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
9359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef HTFM   /* HTFM with uniform subsampling implementation, 2/28/01 */
9459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*===============================================================
9559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Function:   SAD_MB_HTFM_Collect and SAD_MB_HTFM
9659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Date:       3/2/1
9759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Purpose:    Compute the SAD on a 16x16 block using
9859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    uniform subsampling and hypothesis testing fast matching
9959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    for early dropout. SAD_MB_HP_HTFM_Collect is to collect
10059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    the statistics to compute the thresholds to be used in
10159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    SAD_MB_HP_HTFM.
10259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Input/Output:
10359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Changes:
10459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong      ===============================================================*/
10559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
10659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int SAD_MB_HTFM_Collect(UChar *ref, UChar *blk, Int dmin_lx, void *extra_info)
10759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
10859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int i;
10959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int sad = 0;
11059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        UChar *p1;
11159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int lx4 = (dmin_lx << 2) & 0x3FFFC;
11259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        ULong cur_word;
11359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int saddata[16], tmp, tmp2;    /* used when collecting flag (global) is on */
11459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int difmad;
11559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        HTFM_Stat *htfm_stat = (HTFM_Stat*) extra_info;
11659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int *abs_dif_mad_avg = &(htfm_stat->abs_dif_mad_avg);
11759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        UInt *countbreak = &(htfm_stat->countbreak);
11859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int *offsetRef = htfm_stat->offsetRef;
11959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
12059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        NUM_SAD_MB_CALL();
12159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
12259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        blk -= 4;
12359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 16; i++)
12459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
12559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 = ref + offsetRef[i];
12659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
12759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
12859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
12959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
13059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
13159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
13259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
13359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
13459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
13559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
13659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
13759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
13859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
13959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
14059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
14159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
14259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
14359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
14459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
14559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
14659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
14759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
14859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
14959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
15059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
15159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
15259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
15359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
15459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
15559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
15659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
15759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
15859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
15959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
16059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
16159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
16259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
16359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
16459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
16559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
16659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
16759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
16859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
16959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
17059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
17159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
17259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
17359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
17459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
17559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
17659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
17759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
17859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
17959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
18059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
18159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
18259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
18359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
18459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
18559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
18659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            NUM_SAD_MB();
18759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
18859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            saddata[i] = sad;
18959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
19059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (i > 0)
19159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
19259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if ((ULong)sad > ((ULong)dmin_lx >> 16))
19359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
19459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    difmad = saddata[0] - ((saddata[1] + 1) >> 1);
19559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*abs_dif_mad_avg) += ((difmad > 0) ? difmad : -difmad);
19659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*countbreak)++;
19759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    return sad;
19859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
19959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
20059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
20159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
20259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        difmad = saddata[0] - ((saddata[1] + 1) >> 1);
20359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        (*abs_dif_mad_avg) += ((difmad > 0) ? difmad : -difmad);
20459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        (*countbreak)++;
20559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return sad;
20659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
20759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
20859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int SAD_MB_HTFM(UChar *ref, UChar *blk, Int dmin_lx, void *extra_info)
20959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
21059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int sad = 0;
21159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        UChar *p1;
21259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
21359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int i;
21459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int tmp, tmp2;
21559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int lx4 = (dmin_lx << 2) & 0x3FFFC;
21659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int sadstar = 0, madstar;
21759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int *nrmlz_th = (Int*) extra_info;
21859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int *offsetRef = (Int*) extra_info + 32;
21959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        ULong cur_word;
22059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
22159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        madstar = (ULong)dmin_lx >> 20;
22259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
22359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        NUM_SAD_MB_CALL();
22459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
22559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        blk -= 4;
22659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 16; i++)
22759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
22859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 = ref + offsetRef[i];
22959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
23059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
23159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
23259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
23359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
23459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
23559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
23659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
23759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
23859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
23959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
24059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
24159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
24259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
24359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
24459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
24559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
24659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
24759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
24859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
24959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
25059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
25159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
25259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
25359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
25459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
25559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
25659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
25759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
25859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
25959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
26059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
26159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
26259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
26359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
26459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
26559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
26659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
26759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
26859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
26959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
27059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
27159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
27259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
27359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
27459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            cur_word = *((ULong*)(blk += 4));
27559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[12];
27659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 24) & 0xFF;
27759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
27859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[8];
27959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 16) & 0xFF;
28059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
28159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[4];
28259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word >> 8) & 0xFF;
28359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
28459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = p1[0];
28559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            p1 += lx4;
28659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (cur_word & 0xFF);
28759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
28859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
28959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            NUM_SAD_MB();
29059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
29159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sadstar += madstar;
29259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (((ULong)sad <= ((ULong)dmin_lx >> 16)) && (sad <= (sadstar - *nrmlz_th++)))
29359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                ;
29459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
29559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                return 65536;
29659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
29759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
29859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return sad;
29959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
30059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif /* HTFM */
30159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
30259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef NO_INTER4V
30359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*==================================================================
30459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Function:   SAD_Block
30559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Date:       09/07/2000
30659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Purpose:    Compute SAD 16x16 between blk and ref.
30759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        To do:      Uniform subsampling will be inserted later!
30859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    Hypothesis Testing Fast Matching to be used later!
30959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Changes:
31059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    11/7/00:     implemented MMX
31159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    1/24/01:     implemented SSE
31259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong      ==================================================================*/
31359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /********** C ************/
31459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int SAD_Block_C(UChar *ref, UChar *blk, Int dmin, Int lx, void *)
31559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
31659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int sad = 0;
31759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
31859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int i;
31959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        UChar *ii;
32059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int *kk;
32159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int tmp, tmp2, tmp3, mask = 0xFF;
32259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int width = (lx - 32);
32359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
32459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        NUM_SAD_BLK_CALL();
32559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
32659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        ii = ref;
32759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        kk  = (Int*)blk; /* assuming word-align for blk */
32859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 8; i++)
32959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
33059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp3 = kk[1];
33159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = ii[7];
33259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (UInt)tmp3 >> 24;
33359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
33459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = ii[6];
33559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (tmp3 >> 16) & mask;
33659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
33759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = ii[5];
33859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (tmp3 >> 8) & mask;
33959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
34059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = ii[4];
34159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = tmp3 & mask;
34259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
34359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp3 = *kk;
34459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            kk += (width >> 2);
34559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = ii[3];
34659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (UInt)tmp3 >> 24;
34759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
34859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = ii[2];
34959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (tmp3 >> 16) & mask;
35059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
35159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = ii[1];
35259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = (tmp3 >> 8) & mask;
35359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
35459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp = *ii;
35559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            ii += lx;
35659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            tmp2 = tmp3 & mask;
35759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sad = SUB_SAD(sad, tmp, tmp2);
35859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
35959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            NUM_SAD_BLK();
36059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
36159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (sad > dmin)
36259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                return sad;
36359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
36459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
36559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return sad;
36659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
36759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
36859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif /* NO_INTER4V */
36959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
37059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef __cplusplus
37159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
37259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
37359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
37459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
37559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
376