intra_est.cpp revision 29a84457aed4c45bc900998b5e11c03023264208
129a84457aed4c45bc900998b5e11c03023264208James Dong/* ------------------------------------------------------------------
229a84457aed4c45bc900998b5e11c03023264208James Dong * Copyright (C) 1998-2009 PacketVideo
329a84457aed4c45bc900998b5e11c03023264208James Dong *
429a84457aed4c45bc900998b5e11c03023264208James Dong * Licensed under the Apache License, Version 2.0 (the "License");
529a84457aed4c45bc900998b5e11c03023264208James Dong * you may not use this file except in compliance with the License.
629a84457aed4c45bc900998b5e11c03023264208James Dong * You may obtain a copy of the License at
729a84457aed4c45bc900998b5e11c03023264208James Dong *
829a84457aed4c45bc900998b5e11c03023264208James Dong *      http://www.apache.org/licenses/LICENSE-2.0
929a84457aed4c45bc900998b5e11c03023264208James Dong *
1029a84457aed4c45bc900998b5e11c03023264208James Dong * Unless required by applicable law or agreed to in writing, software
1129a84457aed4c45bc900998b5e11c03023264208James Dong * distributed under the License is distributed on an "AS IS" BASIS,
1229a84457aed4c45bc900998b5e11c03023264208James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1329a84457aed4c45bc900998b5e11c03023264208James Dong * express or implied.
1429a84457aed4c45bc900998b5e11c03023264208James Dong * See the License for the specific language governing permissions
1529a84457aed4c45bc900998b5e11c03023264208James Dong * and limitations under the License.
1629a84457aed4c45bc900998b5e11c03023264208James Dong * -------------------------------------------------------------------
1729a84457aed4c45bc900998b5e11c03023264208James Dong */
1829a84457aed4c45bc900998b5e11c03023264208James Dong#include "avcenc_lib.h"
1929a84457aed4c45bc900998b5e11c03023264208James Dong
2029a84457aed4c45bc900998b5e11c03023264208James Dong#define TH_I4  0  /* threshold biasing toward I16 mode instead of I4 mode */
2129a84457aed4c45bc900998b5e11c03023264208James Dong#define TH_Intra  0 /* threshold biasing toward INTER mode instead of intra mode */
2229a84457aed4c45bc900998b5e11c03023264208James Dong
2329a84457aed4c45bc900998b5e11c03023264208James Dong#define FIXED_INTRAPRED_MODE  AVC_I16
2429a84457aed4c45bc900998b5e11c03023264208James Dong#define FIXED_I16_MODE  AVC_I16_DC
2529a84457aed4c45bc900998b5e11c03023264208James Dong#define FIXED_I4_MODE   AVC_I4_Diagonal_Down_Left
2629a84457aed4c45bc900998b5e11c03023264208James Dong#define FIXED_INTRA_CHROMA_MODE AVC_IC_DC
2729a84457aed4c45bc900998b5e11c03023264208James Dong
2829a84457aed4c45bc900998b5e11c03023264208James Dong#define CLIP_RESULT(x)      if((uint)x > 0xFF){ \
2929a84457aed4c45bc900998b5e11c03023264208James Dong                 x = 0xFF & (~(x>>31));}
3029a84457aed4c45bc900998b5e11c03023264208James Dong
3129a84457aed4c45bc900998b5e11c03023264208James Dong
3229a84457aed4c45bc900998b5e11c03023264208James Dongbool IntraDecisionABE(AVCEncObject *encvid, int min_cost, uint8 *curL, int picPitch)
3329a84457aed4c45bc900998b5e11c03023264208James Dong{
3429a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
3529a84457aed4c45bc900998b5e11c03023264208James Dong    AVCFrameIO *currInput = encvid->currInput;
3629a84457aed4c45bc900998b5e11c03023264208James Dong    int orgPitch = currInput->pitch;
3729a84457aed4c45bc900998b5e11c03023264208James Dong    int x_pos = (video->mb_x) << 4;
3829a84457aed4c45bc900998b5e11c03023264208James Dong    int y_pos = (video->mb_y) << 4;
3929a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *orgY = currInput->YCbCr[0] + y_pos * orgPitch + x_pos;
4029a84457aed4c45bc900998b5e11c03023264208James Dong    int j;
4129a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *topL, *leftL, *orgY_2, *orgY_3;
4229a84457aed4c45bc900998b5e11c03023264208James Dong    int temp, SBE, offset;
4329a84457aed4c45bc900998b5e11c03023264208James Dong    OsclFloat ABE;
4429a84457aed4c45bc900998b5e11c03023264208James Dong    bool intra = true;
4529a84457aed4c45bc900998b5e11c03023264208James Dong
4629a84457aed4c45bc900998b5e11c03023264208James Dong    if (((x_pos >> 4) != (int)video->PicWidthInMbs - 1) &&
4729a84457aed4c45bc900998b5e11c03023264208James Dong            ((y_pos >> 4) != (int)video->PicHeightInMbs - 1) &&
4829a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailA &&
4929a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailB)
5029a84457aed4c45bc900998b5e11c03023264208James Dong    {
5129a84457aed4c45bc900998b5e11c03023264208James Dong        SBE = 0;
5229a84457aed4c45bc900998b5e11c03023264208James Dong        /* top neighbor */
5329a84457aed4c45bc900998b5e11c03023264208James Dong        topL = curL - picPitch;
5429a84457aed4c45bc900998b5e11c03023264208James Dong        /* left neighbor */
5529a84457aed4c45bc900998b5e11c03023264208James Dong        leftL = curL - 1;
5629a84457aed4c45bc900998b5e11c03023264208James Dong        orgY_2 = orgY - orgPitch;
5729a84457aed4c45bc900998b5e11c03023264208James Dong
5829a84457aed4c45bc900998b5e11c03023264208James Dong        for (j = 0; j < 16; j++)
5929a84457aed4c45bc900998b5e11c03023264208James Dong        {
6029a84457aed4c45bc900998b5e11c03023264208James Dong            temp = *topL++ - orgY[j];
6129a84457aed4c45bc900998b5e11c03023264208James Dong            SBE += ((temp >= 0) ? temp : -temp);
6229a84457aed4c45bc900998b5e11c03023264208James Dong            temp = *(leftL += picPitch) - *(orgY_2 += orgPitch);
6329a84457aed4c45bc900998b5e11c03023264208James Dong            SBE += ((temp >= 0) ? temp : -temp);
6429a84457aed4c45bc900998b5e11c03023264208James Dong        }
6529a84457aed4c45bc900998b5e11c03023264208James Dong
6629a84457aed4c45bc900998b5e11c03023264208James Dong        /* calculate chroma */
6729a84457aed4c45bc900998b5e11c03023264208James Dong        offset = (y_pos >> 2) * picPitch + (x_pos >> 1);
6829a84457aed4c45bc900998b5e11c03023264208James Dong        topL = video->currPic->Scb + offset;
6929a84457aed4c45bc900998b5e11c03023264208James Dong        orgY_2 = currInput->YCbCr[1] + offset + (y_pos >> 2) * (orgPitch - picPitch);
7029a84457aed4c45bc900998b5e11c03023264208James Dong
7129a84457aed4c45bc900998b5e11c03023264208James Dong        leftL = topL - 1;
7229a84457aed4c45bc900998b5e11c03023264208James Dong        topL -= (picPitch >> 1);
7329a84457aed4c45bc900998b5e11c03023264208James Dong        orgY_3 = orgY_2 - (orgPitch >> 1);
7429a84457aed4c45bc900998b5e11c03023264208James Dong        for (j = 0; j < 8; j++)
7529a84457aed4c45bc900998b5e11c03023264208James Dong        {
7629a84457aed4c45bc900998b5e11c03023264208James Dong            temp = *topL++ - orgY_2[j];
7729a84457aed4c45bc900998b5e11c03023264208James Dong            SBE += ((temp >= 0) ? temp : -temp);
7829a84457aed4c45bc900998b5e11c03023264208James Dong            temp = *(leftL += (picPitch >> 1)) - *(orgY_3 += (orgPitch >> 1));
7929a84457aed4c45bc900998b5e11c03023264208James Dong            SBE += ((temp >= 0) ? temp : -temp);
8029a84457aed4c45bc900998b5e11c03023264208James Dong        }
8129a84457aed4c45bc900998b5e11c03023264208James Dong
8229a84457aed4c45bc900998b5e11c03023264208James Dong        topL = video->currPic->Scr + offset;
8329a84457aed4c45bc900998b5e11c03023264208James Dong        orgY_2 = currInput->YCbCr[2] + offset + (y_pos >> 2) * (orgPitch - picPitch);
8429a84457aed4c45bc900998b5e11c03023264208James Dong
8529a84457aed4c45bc900998b5e11c03023264208James Dong        leftL = topL - 1;
8629a84457aed4c45bc900998b5e11c03023264208James Dong        topL -= (picPitch >> 1);
8729a84457aed4c45bc900998b5e11c03023264208James Dong        orgY_3 = orgY_2 - (orgPitch >> 1);
8829a84457aed4c45bc900998b5e11c03023264208James Dong        for (j = 0; j < 8; j++)
8929a84457aed4c45bc900998b5e11c03023264208James Dong        {
9029a84457aed4c45bc900998b5e11c03023264208James Dong            temp = *topL++ - orgY_2[j];
9129a84457aed4c45bc900998b5e11c03023264208James Dong            SBE += ((temp >= 0) ? temp : -temp);
9229a84457aed4c45bc900998b5e11c03023264208James Dong            temp = *(leftL += (picPitch >> 1)) - *(orgY_3 += (orgPitch >> 1));
9329a84457aed4c45bc900998b5e11c03023264208James Dong            SBE += ((temp >= 0) ? temp : -temp);
9429a84457aed4c45bc900998b5e11c03023264208James Dong        }
9529a84457aed4c45bc900998b5e11c03023264208James Dong
9629a84457aed4c45bc900998b5e11c03023264208James Dong        /* compare mincost/384 and SBE/64 */
9729a84457aed4c45bc900998b5e11c03023264208James Dong        ABE = SBE / 64.0;
9829a84457aed4c45bc900998b5e11c03023264208James Dong        if (ABE*0.8 >= min_cost / 384.0)
9929a84457aed4c45bc900998b5e11c03023264208James Dong        {
10029a84457aed4c45bc900998b5e11c03023264208James Dong            intra = false;
10129a84457aed4c45bc900998b5e11c03023264208James Dong        }
10229a84457aed4c45bc900998b5e11c03023264208James Dong    }
10329a84457aed4c45bc900998b5e11c03023264208James Dong
10429a84457aed4c45bc900998b5e11c03023264208James Dong    return intra;
10529a84457aed4c45bc900998b5e11c03023264208James Dong}
10629a84457aed4c45bc900998b5e11c03023264208James Dong
10729a84457aed4c45bc900998b5e11c03023264208James Dong/* perform searching for MB mode */
10829a84457aed4c45bc900998b5e11c03023264208James Dong/* assuming that this is done inside the encoding loop,
10929a84457aed4c45bc900998b5e11c03023264208James Dongno need to call InitNeighborAvailability */
11029a84457aed4c45bc900998b5e11c03023264208James Dong
11129a84457aed4c45bc900998b5e11c03023264208James Dongvoid MBIntraSearch(AVCEncObject *encvid, int mbnum, uint8 *curL, int picPitch)
11229a84457aed4c45bc900998b5e11c03023264208James Dong{
11329a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
11429a84457aed4c45bc900998b5e11c03023264208James Dong    AVCFrameIO *currInput = encvid->currInput;
11529a84457aed4c45bc900998b5e11c03023264208James Dong    AVCMacroblock *currMB = video->currMB;
11629a84457aed4c45bc900998b5e11c03023264208James Dong    int min_cost;
11729a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *orgY;
11829a84457aed4c45bc900998b5e11c03023264208James Dong    int x_pos = (video->mb_x) << 4;
11929a84457aed4c45bc900998b5e11c03023264208James Dong    int y_pos = (video->mb_y) << 4;
12029a84457aed4c45bc900998b5e11c03023264208James Dong    uint32 *saved_inter;
12129a84457aed4c45bc900998b5e11c03023264208James Dong    int j;
12229a84457aed4c45bc900998b5e11c03023264208James Dong    int orgPitch = currInput->pitch;
12329a84457aed4c45bc900998b5e11c03023264208James Dong    bool intra = true;
12429a84457aed4c45bc900998b5e11c03023264208James Dong
12529a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->CBP = 0;
12629a84457aed4c45bc900998b5e11c03023264208James Dong
12729a84457aed4c45bc900998b5e11c03023264208James Dong    /* first do motion vector and variable block size search */
12829a84457aed4c45bc900998b5e11c03023264208James Dong    min_cost = encvid->min_cost[mbnum];
12929a84457aed4c45bc900998b5e11c03023264208James Dong
13029a84457aed4c45bc900998b5e11c03023264208James Dong    /* now perform intra prediction search */
13129a84457aed4c45bc900998b5e11c03023264208James Dong    /* need to add the check for encvid->intraSearch[video->mbNum] to skip intra
13229a84457aed4c45bc900998b5e11c03023264208James Dong       if it's not worth checking. */
13329a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->slice_type == AVC_P_SLICE)
13429a84457aed4c45bc900998b5e11c03023264208James Dong    {
13529a84457aed4c45bc900998b5e11c03023264208James Dong        /* Decide whether intra search is necessary or not */
13629a84457aed4c45bc900998b5e11c03023264208James Dong        /* This one, we do it in the encoding loop so the neighboring pixel are the
13729a84457aed4c45bc900998b5e11c03023264208James Dong        actual reconstructed pixels. */
13829a84457aed4c45bc900998b5e11c03023264208James Dong        intra = IntraDecisionABE(encvid, min_cost, curL, picPitch);
13929a84457aed4c45bc900998b5e11c03023264208James Dong    }
14029a84457aed4c45bc900998b5e11c03023264208James Dong
14129a84457aed4c45bc900998b5e11c03023264208James Dong    if (intra == true || video->slice_type == AVC_I_SLICE)
14229a84457aed4c45bc900998b5e11c03023264208James Dong    {
14329a84457aed4c45bc900998b5e11c03023264208James Dong        orgY = currInput->YCbCr[0] + y_pos * orgPitch + x_pos;
14429a84457aed4c45bc900998b5e11c03023264208James Dong
14529a84457aed4c45bc900998b5e11c03023264208James Dong        /* i16 mode search */
14629a84457aed4c45bc900998b5e11c03023264208James Dong        /* generate all the predictions */
14729a84457aed4c45bc900998b5e11c03023264208James Dong        intrapred_luma_16x16(encvid);
14829a84457aed4c45bc900998b5e11c03023264208James Dong
14929a84457aed4c45bc900998b5e11c03023264208James Dong        /* evaluate them one by one */
15029a84457aed4c45bc900998b5e11c03023264208James Dong        find_cost_16x16(encvid, orgY, &min_cost);
15129a84457aed4c45bc900998b5e11c03023264208James Dong
15229a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->slice_type == AVC_P_SLICE)
15329a84457aed4c45bc900998b5e11c03023264208James Dong        {
15429a84457aed4c45bc900998b5e11c03023264208James Dong            /* save current inter prediction */
15529a84457aed4c45bc900998b5e11c03023264208James Dong            saved_inter = encvid->subpel_pred; /* reuse existing buffer */
15629a84457aed4c45bc900998b5e11c03023264208James Dong            j = 16;
15729a84457aed4c45bc900998b5e11c03023264208James Dong            curL -= 4;
15829a84457aed4c45bc900998b5e11c03023264208James Dong            picPitch -= 16;
15929a84457aed4c45bc900998b5e11c03023264208James Dong            while (j--)
16029a84457aed4c45bc900998b5e11c03023264208James Dong            {
16129a84457aed4c45bc900998b5e11c03023264208James Dong                *saved_inter++ = *((uint32*)(curL += 4));
16229a84457aed4c45bc900998b5e11c03023264208James Dong                *saved_inter++ = *((uint32*)(curL += 4));
16329a84457aed4c45bc900998b5e11c03023264208James Dong                *saved_inter++ = *((uint32*)(curL += 4));
16429a84457aed4c45bc900998b5e11c03023264208James Dong                *saved_inter++ = *((uint32*)(curL += 4));
16529a84457aed4c45bc900998b5e11c03023264208James Dong                curL += picPitch;
16629a84457aed4c45bc900998b5e11c03023264208James Dong            }
16729a84457aed4c45bc900998b5e11c03023264208James Dong
16829a84457aed4c45bc900998b5e11c03023264208James Dong        }
16929a84457aed4c45bc900998b5e11c03023264208James Dong
17029a84457aed4c45bc900998b5e11c03023264208James Dong        /* i4 mode search */
17129a84457aed4c45bc900998b5e11c03023264208James Dong        mb_intra4x4_search(encvid, &min_cost);
17229a84457aed4c45bc900998b5e11c03023264208James Dong
17329a84457aed4c45bc900998b5e11c03023264208James Dong        encvid->min_cost[mbnum] = min_cost; /* update min_cost */
17429a84457aed4c45bc900998b5e11c03023264208James Dong    }
17529a84457aed4c45bc900998b5e11c03023264208James Dong
17629a84457aed4c45bc900998b5e11c03023264208James Dong
17729a84457aed4c45bc900998b5e11c03023264208James Dong    if (currMB->mb_intra)
17829a84457aed4c45bc900998b5e11c03023264208James Dong    {
17929a84457aed4c45bc900998b5e11c03023264208James Dong        chroma_intra_search(encvid);
18029a84457aed4c45bc900998b5e11c03023264208James Dong
18129a84457aed4c45bc900998b5e11c03023264208James Dong        /* need to set this in order for the MBInterPrediction to work!! */
18229a84457aed4c45bc900998b5e11c03023264208James Dong        memset(currMB->mvL0, 0, sizeof(int32)*16);
18329a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->ref_idx_L0[0] = currMB->ref_idx_L0[1] =
18429a84457aed4c45bc900998b5e11c03023264208James Dong                                    currMB->ref_idx_L0[2] = currMB->ref_idx_L0[3] = -1;
18529a84457aed4c45bc900998b5e11c03023264208James Dong    }
18629a84457aed4c45bc900998b5e11c03023264208James Dong    else if (video->slice_type == AVC_P_SLICE && intra == true)
18729a84457aed4c45bc900998b5e11c03023264208James Dong    {
18829a84457aed4c45bc900998b5e11c03023264208James Dong        /* restore current inter prediction */
18929a84457aed4c45bc900998b5e11c03023264208James Dong        saved_inter = encvid->subpel_pred; /* reuse existing buffer */
19029a84457aed4c45bc900998b5e11c03023264208James Dong        j = 16;
19129a84457aed4c45bc900998b5e11c03023264208James Dong        curL -= ((picPitch + 16) << 4);
19229a84457aed4c45bc900998b5e11c03023264208James Dong        while (j--)
19329a84457aed4c45bc900998b5e11c03023264208James Dong        {
19429a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(curL += 4)) = *saved_inter++;
19529a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(curL += 4)) = *saved_inter++;
19629a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(curL += 4)) = *saved_inter++;
19729a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(curL += 4)) = *saved_inter++;
19829a84457aed4c45bc900998b5e11c03023264208James Dong            curL += picPitch;
19929a84457aed4c45bc900998b5e11c03023264208James Dong        }
20029a84457aed4c45bc900998b5e11c03023264208James Dong    }
20129a84457aed4c45bc900998b5e11c03023264208James Dong
20229a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
20329a84457aed4c45bc900998b5e11c03023264208James Dong}
20429a84457aed4c45bc900998b5e11c03023264208James Dong
20529a84457aed4c45bc900998b5e11c03023264208James Dong/* generate all the prediction values */
20629a84457aed4c45bc900998b5e11c03023264208James Dongvoid intrapred_luma_16x16(AVCEncObject *encvid)
20729a84457aed4c45bc900998b5e11c03023264208James Dong{
20829a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
20929a84457aed4c45bc900998b5e11c03023264208James Dong    AVCPictureData *currPic = video->currPic;
21029a84457aed4c45bc900998b5e11c03023264208James Dong
21129a84457aed4c45bc900998b5e11c03023264208James Dong    int x_pos = (video->mb_x) << 4;
21229a84457aed4c45bc900998b5e11c03023264208James Dong    int y_pos = (video->mb_y) << 4;
21329a84457aed4c45bc900998b5e11c03023264208James Dong    int pitch = currPic->pitch;
21429a84457aed4c45bc900998b5e11c03023264208James Dong
21529a84457aed4c45bc900998b5e11c03023264208James Dong    int offset = y_pos * pitch + x_pos;
21629a84457aed4c45bc900998b5e11c03023264208James Dong
21729a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *pred, *top, *left;
21829a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *curL = currPic->Sl + offset; /* point to reconstructed frame */
21929a84457aed4c45bc900998b5e11c03023264208James Dong    uint32 word1, word2, word3, word4;
22029a84457aed4c45bc900998b5e11c03023264208James Dong    uint32 sum = 0;
22129a84457aed4c45bc900998b5e11c03023264208James Dong
22229a84457aed4c45bc900998b5e11c03023264208James Dong    int a_16, b, c, factor_c;
22329a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *comp_ref_x0, *comp_ref_x1, *comp_ref_y0, *comp_ref_y1;
22429a84457aed4c45bc900998b5e11c03023264208James Dong    int H = 0, V = 0, tmp, value;
22529a84457aed4c45bc900998b5e11c03023264208James Dong    int i;
22629a84457aed4c45bc900998b5e11c03023264208James Dong
22729a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailB)
22829a84457aed4c45bc900998b5e11c03023264208James Dong    {
22929a84457aed4c45bc900998b5e11c03023264208James Dong        //get vertical prediction mode
23029a84457aed4c45bc900998b5e11c03023264208James Dong        top = curL - pitch;
23129a84457aed4c45bc900998b5e11c03023264208James Dong
23229a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i16[AVC_I16_Vertical] - 16;
23329a84457aed4c45bc900998b5e11c03023264208James Dong
23429a84457aed4c45bc900998b5e11c03023264208James Dong        word1 = *((uint32*)(top));  /* read 4 bytes from top */
23529a84457aed4c45bc900998b5e11c03023264208James Dong        word2 = *((uint32*)(top + 4)); /* read 4 bytes from top */
23629a84457aed4c45bc900998b5e11c03023264208James Dong        word3 = *((uint32*)(top + 8)); /* read 4 bytes from top */
23729a84457aed4c45bc900998b5e11c03023264208James Dong        word4 = *((uint32*)(top + 12)); /* read 4 bytes from top */
23829a84457aed4c45bc900998b5e11c03023264208James Dong
23929a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 0; i < 16; i++)
24029a84457aed4c45bc900998b5e11c03023264208James Dong        {
24129a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 16)) = word1;
24229a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 4)) = word2;
24329a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 8)) = word3;
24429a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 12)) = word4;
24529a84457aed4c45bc900998b5e11c03023264208James Dong
24629a84457aed4c45bc900998b5e11c03023264208James Dong        }
24729a84457aed4c45bc900998b5e11c03023264208James Dong
24829a84457aed4c45bc900998b5e11c03023264208James Dong        sum = word1 & 0xFF00FF;
24929a84457aed4c45bc900998b5e11c03023264208James Dong        word1 = (word1 >> 8) & 0xFF00FF;
25029a84457aed4c45bc900998b5e11c03023264208James Dong        sum += word1;
25129a84457aed4c45bc900998b5e11c03023264208James Dong        word1 = (word2 & 0xFF00FF);
25229a84457aed4c45bc900998b5e11c03023264208James Dong        sum += word1;
25329a84457aed4c45bc900998b5e11c03023264208James Dong        word2 = (word2 >> 8) & 0xFF00FF;
25429a84457aed4c45bc900998b5e11c03023264208James Dong        sum += word2;
25529a84457aed4c45bc900998b5e11c03023264208James Dong        word1 = (word3 & 0xFF00FF);
25629a84457aed4c45bc900998b5e11c03023264208James Dong        sum += word1;
25729a84457aed4c45bc900998b5e11c03023264208James Dong        word3 = (word3 >> 8) & 0xFF00FF;
25829a84457aed4c45bc900998b5e11c03023264208James Dong        sum += word3;
25929a84457aed4c45bc900998b5e11c03023264208James Dong        word1 = (word4 & 0xFF00FF);
26029a84457aed4c45bc900998b5e11c03023264208James Dong        sum += word1;
26129a84457aed4c45bc900998b5e11c03023264208James Dong        word4 = (word4 >> 8) & 0xFF00FF;
26229a84457aed4c45bc900998b5e11c03023264208James Dong        sum += word4;
26329a84457aed4c45bc900998b5e11c03023264208James Dong
26429a84457aed4c45bc900998b5e11c03023264208James Dong        sum += (sum >> 16);
26529a84457aed4c45bc900998b5e11c03023264208James Dong        sum &= 0xFFFF;
26629a84457aed4c45bc900998b5e11c03023264208James Dong
26729a84457aed4c45bc900998b5e11c03023264208James Dong        if (!video->intraAvailA)
26829a84457aed4c45bc900998b5e11c03023264208James Dong        {
26929a84457aed4c45bc900998b5e11c03023264208James Dong            sum = (sum + 8) >> 4;
27029a84457aed4c45bc900998b5e11c03023264208James Dong        }
27129a84457aed4c45bc900998b5e11c03023264208James Dong    }
27229a84457aed4c45bc900998b5e11c03023264208James Dong
27329a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA)
27429a84457aed4c45bc900998b5e11c03023264208James Dong    {
27529a84457aed4c45bc900998b5e11c03023264208James Dong        // get horizontal mode
27629a84457aed4c45bc900998b5e11c03023264208James Dong        left = curL - 1 - pitch;
27729a84457aed4c45bc900998b5e11c03023264208James Dong
27829a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i16[AVC_I16_Horizontal] - 16;
27929a84457aed4c45bc900998b5e11c03023264208James Dong
28029a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 0; i < 16; i++)
28129a84457aed4c45bc900998b5e11c03023264208James Dong        {
28229a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = *(left += pitch);
28329a84457aed4c45bc900998b5e11c03023264208James Dong            sum += word1;
28429a84457aed4c45bc900998b5e11c03023264208James Dong
28529a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1 << 8) | word1;
28629a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1 << 16) | word1; /* make it 4 */
28729a84457aed4c45bc900998b5e11c03023264208James Dong
28829a84457aed4c45bc900998b5e11c03023264208James Dong            *(uint32*)(pred += 16) = word1;
28929a84457aed4c45bc900998b5e11c03023264208James Dong            *(uint32*)(pred + 4) = word1;
29029a84457aed4c45bc900998b5e11c03023264208James Dong            *(uint32*)(pred + 8) = word1;
29129a84457aed4c45bc900998b5e11c03023264208James Dong            *(uint32*)(pred + 12) = word1;
29229a84457aed4c45bc900998b5e11c03023264208James Dong        }
29329a84457aed4c45bc900998b5e11c03023264208James Dong
29429a84457aed4c45bc900998b5e11c03023264208James Dong        if (!video->intraAvailB)
29529a84457aed4c45bc900998b5e11c03023264208James Dong        {
29629a84457aed4c45bc900998b5e11c03023264208James Dong            sum = (sum + 8) >> 4;
29729a84457aed4c45bc900998b5e11c03023264208James Dong        }
29829a84457aed4c45bc900998b5e11c03023264208James Dong        else
29929a84457aed4c45bc900998b5e11c03023264208James Dong        {
30029a84457aed4c45bc900998b5e11c03023264208James Dong            sum = (sum + 16) >> 5;
30129a84457aed4c45bc900998b5e11c03023264208James Dong        }
30229a84457aed4c45bc900998b5e11c03023264208James Dong    }
30329a84457aed4c45bc900998b5e11c03023264208James Dong
30429a84457aed4c45bc900998b5e11c03023264208James Dong    // get DC mode
30529a84457aed4c45bc900998b5e11c03023264208James Dong    if (!video->intraAvailA && !video->intraAvailB)
30629a84457aed4c45bc900998b5e11c03023264208James Dong    {
30729a84457aed4c45bc900998b5e11c03023264208James Dong        sum = 0x80808080;
30829a84457aed4c45bc900998b5e11c03023264208James Dong    }
30929a84457aed4c45bc900998b5e11c03023264208James Dong    else
31029a84457aed4c45bc900998b5e11c03023264208James Dong    {
31129a84457aed4c45bc900998b5e11c03023264208James Dong        sum = (sum << 8) | sum;
31229a84457aed4c45bc900998b5e11c03023264208James Dong        sum = (sum << 16) | sum;
31329a84457aed4c45bc900998b5e11c03023264208James Dong    }
31429a84457aed4c45bc900998b5e11c03023264208James Dong
31529a84457aed4c45bc900998b5e11c03023264208James Dong    pred = encvid->pred_i16[AVC_I16_DC] - 16;
31629a84457aed4c45bc900998b5e11c03023264208James Dong    for (i = 0; i < 16; i++)
31729a84457aed4c45bc900998b5e11c03023264208James Dong    {
31829a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 16)) = sum;
31929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 4)) = sum;
32029a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 8)) = sum;
32129a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 12)) = sum;
32229a84457aed4c45bc900998b5e11c03023264208James Dong    }
32329a84457aed4c45bc900998b5e11c03023264208James Dong
32429a84457aed4c45bc900998b5e11c03023264208James Dong    // get plane mode
32529a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA && video->intraAvailB && video->intraAvailD)
32629a84457aed4c45bc900998b5e11c03023264208James Dong    {
32729a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i16[AVC_I16_Plane] - 16;
32829a84457aed4c45bc900998b5e11c03023264208James Dong
32929a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_x0 = curL - pitch + 8;
33029a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_x1 = curL - pitch + 6;
33129a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_y0 = curL - 1 + (pitch << 3);
33229a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_y1 = curL - 1 + 6 * pitch;
33329a84457aed4c45bc900998b5e11c03023264208James Dong
33429a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 1; i < 8; i++)
33529a84457aed4c45bc900998b5e11c03023264208James Dong        {
33629a84457aed4c45bc900998b5e11c03023264208James Dong            H += i * (*comp_ref_x0++ - *comp_ref_x1--);
33729a84457aed4c45bc900998b5e11c03023264208James Dong            V += i * (*comp_ref_y0 - *comp_ref_y1);
33829a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_y0 += pitch;
33929a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_y1 -= pitch;
34029a84457aed4c45bc900998b5e11c03023264208James Dong        }
34129a84457aed4c45bc900998b5e11c03023264208James Dong
34229a84457aed4c45bc900998b5e11c03023264208James Dong        H += i * (*comp_ref_x0++ - curL[-pitch-1]);
34329a84457aed4c45bc900998b5e11c03023264208James Dong        V += i * (*comp_ref_y0 - *comp_ref_y1);
34429a84457aed4c45bc900998b5e11c03023264208James Dong
34529a84457aed4c45bc900998b5e11c03023264208James Dong
34629a84457aed4c45bc900998b5e11c03023264208James Dong        a_16 = ((*(curL - pitch + 15) + *(curL - 1 + 15 * pitch)) << 4) + 16;;
34729a84457aed4c45bc900998b5e11c03023264208James Dong        b = (5 * H + 32) >> 6;
34829a84457aed4c45bc900998b5e11c03023264208James Dong        c = (5 * V + 32) >> 6;
34929a84457aed4c45bc900998b5e11c03023264208James Dong
35029a84457aed4c45bc900998b5e11c03023264208James Dong        tmp = 0;
35129a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 0; i < 16; i++)
35229a84457aed4c45bc900998b5e11c03023264208James Dong        {
35329a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c = a_16 + c * (tmp++ - 7);
35429a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c -= 7 * b;
35529a84457aed4c45bc900998b5e11c03023264208James Dong
35629a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
35729a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
35829a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
35929a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = value;
36029a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
36129a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
36229a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
36329a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 8);
36429a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
36529a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
36629a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
36729a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 16);
36829a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
36929a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
37029a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
37129a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 24);
37229a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 16)) = word1;
37329a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
37429a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
37529a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
37629a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = value;
37729a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
37829a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
37929a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
38029a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 8);
38129a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
38229a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
38329a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
38429a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 16);
38529a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
38629a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
38729a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
38829a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 24);
38929a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 4)) = word1;
39029a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
39129a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
39229a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
39329a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = value;
39429a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
39529a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
39629a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
39729a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 8);
39829a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
39929a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
40029a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
40129a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 16);
40229a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
40329a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
40429a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
40529a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 24);
40629a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 8)) = word1;
40729a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
40829a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
40929a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
41029a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = value;
41129a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
41229a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
41329a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
41429a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 8);
41529a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
41629a84457aed4c45bc900998b5e11c03023264208James Dong            factor_c += b;
41729a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
41829a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 16);
41929a84457aed4c45bc900998b5e11c03023264208James Dong            value = factor_c >> 5;
42029a84457aed4c45bc900998b5e11c03023264208James Dong            CLIP_RESULT(value)
42129a84457aed4c45bc900998b5e11c03023264208James Dong            word1 = (word1) | (value << 24);
42229a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 12)) = word1;
42329a84457aed4c45bc900998b5e11c03023264208James Dong        }
42429a84457aed4c45bc900998b5e11c03023264208James Dong    }
42529a84457aed4c45bc900998b5e11c03023264208James Dong
42629a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
42729a84457aed4c45bc900998b5e11c03023264208James Dong}
42829a84457aed4c45bc900998b5e11c03023264208James Dong
42929a84457aed4c45bc900998b5e11c03023264208James Dong
43029a84457aed4c45bc900998b5e11c03023264208James Dong/* evaluate each prediction mode of I16 */
43129a84457aed4c45bc900998b5e11c03023264208James Dongvoid find_cost_16x16(AVCEncObject *encvid, uint8 *orgY, int *min_cost)
43229a84457aed4c45bc900998b5e11c03023264208James Dong{
43329a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
43429a84457aed4c45bc900998b5e11c03023264208James Dong    AVCMacroblock *currMB = video->currMB;
43529a84457aed4c45bc900998b5e11c03023264208James Dong    int cost;
43629a84457aed4c45bc900998b5e11c03023264208James Dong    int org_pitch = encvid->currInput->pitch;
43729a84457aed4c45bc900998b5e11c03023264208James Dong
43829a84457aed4c45bc900998b5e11c03023264208James Dong    /* evaluate vertical mode */
43929a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailB)
44029a84457aed4c45bc900998b5e11c03023264208James Dong    {
44129a84457aed4c45bc900998b5e11c03023264208James Dong        cost = cost_i16(orgY, org_pitch, encvid->pred_i16[AVC_I16_Vertical], *min_cost);
44229a84457aed4c45bc900998b5e11c03023264208James Dong        if (cost < *min_cost)
44329a84457aed4c45bc900998b5e11c03023264208James Dong        {
44429a84457aed4c45bc900998b5e11c03023264208James Dong            *min_cost = cost;
44529a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->mbMode = AVC_I16;
44629a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->mb_intra = 1;
44729a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_Vertical;
44829a84457aed4c45bc900998b5e11c03023264208James Dong        }
44929a84457aed4c45bc900998b5e11c03023264208James Dong    }
45029a84457aed4c45bc900998b5e11c03023264208James Dong
45129a84457aed4c45bc900998b5e11c03023264208James Dong
45229a84457aed4c45bc900998b5e11c03023264208James Dong    /* evaluate horizontal mode */
45329a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA)
45429a84457aed4c45bc900998b5e11c03023264208James Dong    {
45529a84457aed4c45bc900998b5e11c03023264208James Dong        cost = cost_i16(orgY, org_pitch, encvid->pred_i16[AVC_I16_Horizontal], *min_cost);
45629a84457aed4c45bc900998b5e11c03023264208James Dong        if (cost < *min_cost)
45729a84457aed4c45bc900998b5e11c03023264208James Dong        {
45829a84457aed4c45bc900998b5e11c03023264208James Dong            *min_cost = cost;
45929a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->mbMode = AVC_I16;
46029a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->mb_intra = 1;
46129a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_Horizontal;
46229a84457aed4c45bc900998b5e11c03023264208James Dong        }
46329a84457aed4c45bc900998b5e11c03023264208James Dong    }
46429a84457aed4c45bc900998b5e11c03023264208James Dong
46529a84457aed4c45bc900998b5e11c03023264208James Dong    /* evaluate DC mode */
46629a84457aed4c45bc900998b5e11c03023264208James Dong    cost = cost_i16(orgY, org_pitch, encvid->pred_i16[AVC_I16_DC], *min_cost);
46729a84457aed4c45bc900998b5e11c03023264208James Dong    if (cost < *min_cost)
46829a84457aed4c45bc900998b5e11c03023264208James Dong    {
46929a84457aed4c45bc900998b5e11c03023264208James Dong        *min_cost = cost;
47029a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->mbMode = AVC_I16;
47129a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->mb_intra = 1;
47229a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->i16Mode = AVC_I16_DC;
47329a84457aed4c45bc900998b5e11c03023264208James Dong    }
47429a84457aed4c45bc900998b5e11c03023264208James Dong
47529a84457aed4c45bc900998b5e11c03023264208James Dong    /* evaluate plane mode */
47629a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA && video->intraAvailB && video->intraAvailD)
47729a84457aed4c45bc900998b5e11c03023264208James Dong    {
47829a84457aed4c45bc900998b5e11c03023264208James Dong        cost = cost_i16(orgY, org_pitch, encvid->pred_i16[AVC_I16_Plane], *min_cost);
47929a84457aed4c45bc900998b5e11c03023264208James Dong        if (cost < *min_cost)
48029a84457aed4c45bc900998b5e11c03023264208James Dong        {
48129a84457aed4c45bc900998b5e11c03023264208James Dong            *min_cost = cost;
48229a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->mbMode = AVC_I16;
48329a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->mb_intra = 1;
48429a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_Plane;
48529a84457aed4c45bc900998b5e11c03023264208James Dong        }
48629a84457aed4c45bc900998b5e11c03023264208James Dong    }
48729a84457aed4c45bc900998b5e11c03023264208James Dong
48829a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
48929a84457aed4c45bc900998b5e11c03023264208James Dong}
49029a84457aed4c45bc900998b5e11c03023264208James Dong
49129a84457aed4c45bc900998b5e11c03023264208James Dong
49229a84457aed4c45bc900998b5e11c03023264208James Dongint cost_i16(uint8 *org, int org_pitch, uint8 *pred, int min_cost)
49329a84457aed4c45bc900998b5e11c03023264208James Dong{
49429a84457aed4c45bc900998b5e11c03023264208James Dong
49529a84457aed4c45bc900998b5e11c03023264208James Dong    int cost;
49629a84457aed4c45bc900998b5e11c03023264208James Dong    int j, k;
49729a84457aed4c45bc900998b5e11c03023264208James Dong    int16 res[256], *pres; // residue
49829a84457aed4c45bc900998b5e11c03023264208James Dong    int m0, m1, m2, m3;
49929a84457aed4c45bc900998b5e11c03023264208James Dong
50029a84457aed4c45bc900998b5e11c03023264208James Dong    // calculate SATD
50129a84457aed4c45bc900998b5e11c03023264208James Dong    org_pitch -= 16;
50229a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
50329a84457aed4c45bc900998b5e11c03023264208James Dong    // horizontal transform
50429a84457aed4c45bc900998b5e11c03023264208James Dong    for (j = 0; j < 16; j++)
50529a84457aed4c45bc900998b5e11c03023264208James Dong    {
50629a84457aed4c45bc900998b5e11c03023264208James Dong        k = 4;
50729a84457aed4c45bc900998b5e11c03023264208James Dong        while (k > 0)
50829a84457aed4c45bc900998b5e11c03023264208James Dong        {
50929a84457aed4c45bc900998b5e11c03023264208James Dong            m0 = org[0] - pred[0];
51029a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = org[3] - pred[3];
51129a84457aed4c45bc900998b5e11c03023264208James Dong            m0 += m3;
51229a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = m0 - (m3 << 1);
51329a84457aed4c45bc900998b5e11c03023264208James Dong            m1 = org[1] - pred[1];
51429a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = org[2] - pred[2];
51529a84457aed4c45bc900998b5e11c03023264208James Dong            m1 += m2;
51629a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = m1 - (m2 << 1);
51729a84457aed4c45bc900998b5e11c03023264208James Dong            pres[0] = m0 + m1;
51829a84457aed4c45bc900998b5e11c03023264208James Dong            pres[2] = m0 - m1;
51929a84457aed4c45bc900998b5e11c03023264208James Dong            pres[1] = m2 + m3;
52029a84457aed4c45bc900998b5e11c03023264208James Dong            pres[3] = m3 - m2;
52129a84457aed4c45bc900998b5e11c03023264208James Dong
52229a84457aed4c45bc900998b5e11c03023264208James Dong            org += 4;
52329a84457aed4c45bc900998b5e11c03023264208James Dong            pres += 4;
52429a84457aed4c45bc900998b5e11c03023264208James Dong            pred += 4;
52529a84457aed4c45bc900998b5e11c03023264208James Dong            k--;
52629a84457aed4c45bc900998b5e11c03023264208James Dong        }
52729a84457aed4c45bc900998b5e11c03023264208James Dong        org += org_pitch;
52829a84457aed4c45bc900998b5e11c03023264208James Dong    }
52929a84457aed4c45bc900998b5e11c03023264208James Dong    /* vertical transform */
53029a84457aed4c45bc900998b5e11c03023264208James Dong    cost = 0;
53129a84457aed4c45bc900998b5e11c03023264208James Dong    for (j = 0; j < 4; j++)
53229a84457aed4c45bc900998b5e11c03023264208James Dong    {
53329a84457aed4c45bc900998b5e11c03023264208James Dong        pres = res + (j << 6);
53429a84457aed4c45bc900998b5e11c03023264208James Dong        k = 16;
53529a84457aed4c45bc900998b5e11c03023264208James Dong        while (k > 0)
53629a84457aed4c45bc900998b5e11c03023264208James Dong        {
53729a84457aed4c45bc900998b5e11c03023264208James Dong            m0 = pres[0];
53829a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = pres[3<<4];
53929a84457aed4c45bc900998b5e11c03023264208James Dong            m0 += m3;
54029a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = m0 - (m3 << 1);
54129a84457aed4c45bc900998b5e11c03023264208James Dong            m1 = pres[1<<4];
54229a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = pres[2<<4];
54329a84457aed4c45bc900998b5e11c03023264208James Dong            m1 += m2;
54429a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = m1 - (m2 << 1);
54529a84457aed4c45bc900998b5e11c03023264208James Dong            pres[0] = m0 = m0 + m1;
54629a84457aed4c45bc900998b5e11c03023264208James Dong
54729a84457aed4c45bc900998b5e11c03023264208James Dong            if (k&0x3)  // only sum up non DC values.
54829a84457aed4c45bc900998b5e11c03023264208James Dong            {
54929a84457aed4c45bc900998b5e11c03023264208James Dong                cost += ((m0 > 0) ? m0 : -m0);
55029a84457aed4c45bc900998b5e11c03023264208James Dong            }
55129a84457aed4c45bc900998b5e11c03023264208James Dong
55229a84457aed4c45bc900998b5e11c03023264208James Dong            m1 = m0 - (m1 << 1);
55329a84457aed4c45bc900998b5e11c03023264208James Dong            cost += ((m1 > 0) ? m1 : -m1);
55429a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = m2 + m3;
55529a84457aed4c45bc900998b5e11c03023264208James Dong            cost += ((m3 > 0) ? m3 : -m3);
55629a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = m3 - (m2 << 1);
55729a84457aed4c45bc900998b5e11c03023264208James Dong            cost += ((m2 > 0) ? m2 : -m2);
55829a84457aed4c45bc900998b5e11c03023264208James Dong
55929a84457aed4c45bc900998b5e11c03023264208James Dong            pres++;
56029a84457aed4c45bc900998b5e11c03023264208James Dong            k--;
56129a84457aed4c45bc900998b5e11c03023264208James Dong        }
56229a84457aed4c45bc900998b5e11c03023264208James Dong        if ((cost >> 1) > min_cost) /* early drop out */
56329a84457aed4c45bc900998b5e11c03023264208James Dong        {
56429a84457aed4c45bc900998b5e11c03023264208James Dong            return (cost >> 1);
56529a84457aed4c45bc900998b5e11c03023264208James Dong        }
56629a84457aed4c45bc900998b5e11c03023264208James Dong    }
56729a84457aed4c45bc900998b5e11c03023264208James Dong
56829a84457aed4c45bc900998b5e11c03023264208James Dong    /* Hadamard of the DC coefficient */
56929a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
57029a84457aed4c45bc900998b5e11c03023264208James Dong    k = 4;
57129a84457aed4c45bc900998b5e11c03023264208James Dong    while (k > 0)
57229a84457aed4c45bc900998b5e11c03023264208James Dong    {
57329a84457aed4c45bc900998b5e11c03023264208James Dong        m0 = pres[0];
57429a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = pres[3<<2];
57529a84457aed4c45bc900998b5e11c03023264208James Dong        m0 >>= 2;
57629a84457aed4c45bc900998b5e11c03023264208James Dong        m0 += (m3 >> 2);
57729a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = m0 - (m3 >> 1);
57829a84457aed4c45bc900998b5e11c03023264208James Dong        m1 = pres[1<<2];
57929a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = pres[2<<2];
58029a84457aed4c45bc900998b5e11c03023264208James Dong        m1 >>= 2;
58129a84457aed4c45bc900998b5e11c03023264208James Dong        m1 += (m2 >> 2);
58229a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = m1 - (m2 >> 1);
58329a84457aed4c45bc900998b5e11c03023264208James Dong        pres[0] = (m0 + m1);
58429a84457aed4c45bc900998b5e11c03023264208James Dong        pres[2<<2] = (m0 - m1);
58529a84457aed4c45bc900998b5e11c03023264208James Dong        pres[1<<2] = (m2 + m3);
58629a84457aed4c45bc900998b5e11c03023264208James Dong        pres[3<<2] = (m3 - m2);
58729a84457aed4c45bc900998b5e11c03023264208James Dong        pres += (4 << 4);
58829a84457aed4c45bc900998b5e11c03023264208James Dong        k--;
58929a84457aed4c45bc900998b5e11c03023264208James Dong    }
59029a84457aed4c45bc900998b5e11c03023264208James Dong
59129a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
59229a84457aed4c45bc900998b5e11c03023264208James Dong    k = 4;
59329a84457aed4c45bc900998b5e11c03023264208James Dong    while (k > 0)
59429a84457aed4c45bc900998b5e11c03023264208James Dong    {
59529a84457aed4c45bc900998b5e11c03023264208James Dong        m0 = pres[0];
59629a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = pres[3<<6];
59729a84457aed4c45bc900998b5e11c03023264208James Dong        m0 += m3;
59829a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = m0 - (m3 << 1);
59929a84457aed4c45bc900998b5e11c03023264208James Dong        m1 = pres[1<<6];
60029a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = pres[2<<6];
60129a84457aed4c45bc900998b5e11c03023264208James Dong        m1 += m2;
60229a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = m1 - (m2 << 1);
60329a84457aed4c45bc900998b5e11c03023264208James Dong        m0 = m0 + m1;
60429a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((m0 >= 0) ? m0 : -m0);
60529a84457aed4c45bc900998b5e11c03023264208James Dong        m1 = m0 - (m1 << 1);
60629a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((m1 >= 0) ? m1 : -m1);
60729a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = m2 + m3;
60829a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((m3 >= 0) ? m3 : -m3);
60929a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = m3 - (m2 << 1);
61029a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((m2 >= 0) ? m2 : -m2);
61129a84457aed4c45bc900998b5e11c03023264208James Dong        pres += 4;
61229a84457aed4c45bc900998b5e11c03023264208James Dong
61329a84457aed4c45bc900998b5e11c03023264208James Dong        if ((cost >> 1) > min_cost) /* early drop out */
61429a84457aed4c45bc900998b5e11c03023264208James Dong        {
61529a84457aed4c45bc900998b5e11c03023264208James Dong            return (cost >> 1);
61629a84457aed4c45bc900998b5e11c03023264208James Dong        }
61729a84457aed4c45bc900998b5e11c03023264208James Dong
61829a84457aed4c45bc900998b5e11c03023264208James Dong        k--;
61929a84457aed4c45bc900998b5e11c03023264208James Dong    }
62029a84457aed4c45bc900998b5e11c03023264208James Dong
62129a84457aed4c45bc900998b5e11c03023264208James Dong    return (cost >> 1);
62229a84457aed4c45bc900998b5e11c03023264208James Dong}
62329a84457aed4c45bc900998b5e11c03023264208James Dong
62429a84457aed4c45bc900998b5e11c03023264208James Dong
62529a84457aed4c45bc900998b5e11c03023264208James Dongvoid mb_intra4x4_search(AVCEncObject *encvid, int *min_cost)
62629a84457aed4c45bc900998b5e11c03023264208James Dong{
62729a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
62829a84457aed4c45bc900998b5e11c03023264208James Dong    AVCMacroblock *currMB = video->currMB;
62929a84457aed4c45bc900998b5e11c03023264208James Dong    AVCPictureData *currPic = video->currPic;
63029a84457aed4c45bc900998b5e11c03023264208James Dong    AVCFrameIO *currInput = encvid->currInput;
63129a84457aed4c45bc900998b5e11c03023264208James Dong    int pitch = currPic->pitch;
63229a84457aed4c45bc900998b5e11c03023264208James Dong    int org_pitch = currInput->pitch;
63329a84457aed4c45bc900998b5e11c03023264208James Dong    int offset;
63429a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *curL, *comp, *org4, *org8;
63529a84457aed4c45bc900998b5e11c03023264208James Dong    int y = video->mb_y << 4;
63629a84457aed4c45bc900998b5e11c03023264208James Dong    int x = video->mb_x << 4;
63729a84457aed4c45bc900998b5e11c03023264208James Dong
63829a84457aed4c45bc900998b5e11c03023264208James Dong    int b8, b4, cost4x4, blkidx;
63929a84457aed4c45bc900998b5e11c03023264208James Dong    int cost = 0;
64029a84457aed4c45bc900998b5e11c03023264208James Dong    int numcoef;
64129a84457aed4c45bc900998b5e11c03023264208James Dong    int dummy = 0;
64229a84457aed4c45bc900998b5e11c03023264208James Dong    int mb_intra = currMB->mb_intra; // save the original value
64329a84457aed4c45bc900998b5e11c03023264208James Dong
64429a84457aed4c45bc900998b5e11c03023264208James Dong    offset = y * pitch + x;
64529a84457aed4c45bc900998b5e11c03023264208James Dong
64629a84457aed4c45bc900998b5e11c03023264208James Dong    curL = currPic->Sl + offset;
64729a84457aed4c45bc900998b5e11c03023264208James Dong    org8 = currInput->YCbCr[0] + y * org_pitch + x;
64829a84457aed4c45bc900998b5e11c03023264208James Dong    video->pred_pitch = 4;
64929a84457aed4c45bc900998b5e11c03023264208James Dong
65029a84457aed4c45bc900998b5e11c03023264208James Dong    cost = (int)(6.0 * encvid->lambda_mode + 0.4999);
65129a84457aed4c45bc900998b5e11c03023264208James Dong    cost <<= 2;
65229a84457aed4c45bc900998b5e11c03023264208James Dong
65329a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->mb_intra = 1;  // temporary set this to one to enable the IDCT
65429a84457aed4c45bc900998b5e11c03023264208James Dong    // operation inside dct_luma
65529a84457aed4c45bc900998b5e11c03023264208James Dong
65629a84457aed4c45bc900998b5e11c03023264208James Dong    for (b8 = 0; b8 < 4; b8++)
65729a84457aed4c45bc900998b5e11c03023264208James Dong    {
65829a84457aed4c45bc900998b5e11c03023264208James Dong        comp = curL;
65929a84457aed4c45bc900998b5e11c03023264208James Dong        org4 = org8;
66029a84457aed4c45bc900998b5e11c03023264208James Dong
66129a84457aed4c45bc900998b5e11c03023264208James Dong        for (b4 = 0; b4 < 4; b4++)
66229a84457aed4c45bc900998b5e11c03023264208James Dong        {
66329a84457aed4c45bc900998b5e11c03023264208James Dong            blkidx = blkIdx2blkXY[b8][b4];
66429a84457aed4c45bc900998b5e11c03023264208James Dong            cost4x4 = blk_intra4x4_search(encvid, blkidx, comp, org4);
66529a84457aed4c45bc900998b5e11c03023264208James Dong            cost += cost4x4;
66629a84457aed4c45bc900998b5e11c03023264208James Dong            if (cost > *min_cost)
66729a84457aed4c45bc900998b5e11c03023264208James Dong            {
66829a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->mb_intra = mb_intra; // restore the value
66929a84457aed4c45bc900998b5e11c03023264208James Dong                return ;
67029a84457aed4c45bc900998b5e11c03023264208James Dong            }
67129a84457aed4c45bc900998b5e11c03023264208James Dong
67229a84457aed4c45bc900998b5e11c03023264208James Dong            /* do residue, Xfrm, Q, invQ, invXfrm, recon and save the DCT coefs.*/
67329a84457aed4c45bc900998b5e11c03023264208James Dong            video->pred_block = encvid->pred_i4[currMB->i4Mode[blkidx]];
67429a84457aed4c45bc900998b5e11c03023264208James Dong            numcoef = dct_luma(encvid, blkidx, comp, org4, &dummy);
67529a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->nz_coeff[blkidx] = numcoef;
67629a84457aed4c45bc900998b5e11c03023264208James Dong            if (numcoef)
67729a84457aed4c45bc900998b5e11c03023264208James Dong            {
67829a84457aed4c45bc900998b5e11c03023264208James Dong                video->cbp4x4 |= (1 << blkidx);
67929a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->CBP |= (1 << b8);
68029a84457aed4c45bc900998b5e11c03023264208James Dong            }
68129a84457aed4c45bc900998b5e11c03023264208James Dong
68229a84457aed4c45bc900998b5e11c03023264208James Dong            if (b4&1)
68329a84457aed4c45bc900998b5e11c03023264208James Dong            {
68429a84457aed4c45bc900998b5e11c03023264208James Dong                comp += ((pitch << 2) - 4);
68529a84457aed4c45bc900998b5e11c03023264208James Dong                org4 += ((org_pitch << 2) - 4);
68629a84457aed4c45bc900998b5e11c03023264208James Dong            }
68729a84457aed4c45bc900998b5e11c03023264208James Dong            else
68829a84457aed4c45bc900998b5e11c03023264208James Dong            {
68929a84457aed4c45bc900998b5e11c03023264208James Dong                comp += 4;
69029a84457aed4c45bc900998b5e11c03023264208James Dong                org4 += 4;
69129a84457aed4c45bc900998b5e11c03023264208James Dong            }
69229a84457aed4c45bc900998b5e11c03023264208James Dong        }
69329a84457aed4c45bc900998b5e11c03023264208James Dong
69429a84457aed4c45bc900998b5e11c03023264208James Dong        if (b8&1)
69529a84457aed4c45bc900998b5e11c03023264208James Dong        {
69629a84457aed4c45bc900998b5e11c03023264208James Dong            curL += ((pitch << 3) - 8);
69729a84457aed4c45bc900998b5e11c03023264208James Dong            org8 += ((org_pitch << 3) - 8);
69829a84457aed4c45bc900998b5e11c03023264208James Dong        }
69929a84457aed4c45bc900998b5e11c03023264208James Dong        else
70029a84457aed4c45bc900998b5e11c03023264208James Dong        {
70129a84457aed4c45bc900998b5e11c03023264208James Dong            curL += 8;
70229a84457aed4c45bc900998b5e11c03023264208James Dong            org8 += 8;
70329a84457aed4c45bc900998b5e11c03023264208James Dong        }
70429a84457aed4c45bc900998b5e11c03023264208James Dong    }
70529a84457aed4c45bc900998b5e11c03023264208James Dong
70629a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->mb_intra = mb_intra; // restore the value
70729a84457aed4c45bc900998b5e11c03023264208James Dong
70829a84457aed4c45bc900998b5e11c03023264208James Dong    if (cost < *min_cost)
70929a84457aed4c45bc900998b5e11c03023264208James Dong    {
71029a84457aed4c45bc900998b5e11c03023264208James Dong        *min_cost = cost;
71129a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->mbMode = AVC_I4;
71229a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->mb_intra = 1;
71329a84457aed4c45bc900998b5e11c03023264208James Dong    }
71429a84457aed4c45bc900998b5e11c03023264208James Dong
71529a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
71629a84457aed4c45bc900998b5e11c03023264208James Dong}
71729a84457aed4c45bc900998b5e11c03023264208James Dong
71829a84457aed4c45bc900998b5e11c03023264208James Dong
71929a84457aed4c45bc900998b5e11c03023264208James Dong/* search for i4 mode for a 4x4 block */
72029a84457aed4c45bc900998b5e11c03023264208James Dongint blk_intra4x4_search(AVCEncObject *encvid, int blkidx, uint8 *cur, uint8 *org)
72129a84457aed4c45bc900998b5e11c03023264208James Dong{
72229a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
72329a84457aed4c45bc900998b5e11c03023264208James Dong    AVCNeighborAvailability availability;
72429a84457aed4c45bc900998b5e11c03023264208James Dong    AVCMacroblock *currMB = video->currMB;
72529a84457aed4c45bc900998b5e11c03023264208James Dong    bool top_left = FALSE;
72629a84457aed4c45bc900998b5e11c03023264208James Dong    int pitch = video->currPic->pitch;
72729a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 mode_avail[AVCNumI4PredMode];
72829a84457aed4c45bc900998b5e11c03023264208James Dong    uint32 temp, DC;
72929a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *pred;
73029a84457aed4c45bc900998b5e11c03023264208James Dong    int org_pitch = encvid->currInput->pitch;
73129a84457aed4c45bc900998b5e11c03023264208James Dong    uint16 min_cost, cost;
73229a84457aed4c45bc900998b5e11c03023264208James Dong
73329a84457aed4c45bc900998b5e11c03023264208James Dong    int P_x, Q_x, R_x, P_y, Q_y, R_y, D, D0, D1;
73429a84457aed4c45bc900998b5e11c03023264208James Dong    int P0, Q0, R0, S0, P1, Q1, R1, P2, Q2;
73529a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 P_A, P_B, P_C, P_D, P_E, P_F, P_G, P_H, P_I, P_J, P_K, P_L, P_X;
73629a84457aed4c45bc900998b5e11c03023264208James Dong    int r0, r1, r2, r3, r4, r5, r6, r7;
73729a84457aed4c45bc900998b5e11c03023264208James Dong    int x0, x1, x2, x3, x4, x5;
73829a84457aed4c45bc900998b5e11c03023264208James Dong    uint32 temp1, temp2;
73929a84457aed4c45bc900998b5e11c03023264208James Dong
74029a84457aed4c45bc900998b5e11c03023264208James Dong    int ipmode, mostProbableMode;
74129a84457aed4c45bc900998b5e11c03023264208James Dong    int fixedcost = 4 * encvid->lambda_mode;
74229a84457aed4c45bc900998b5e11c03023264208James Dong    int min_sad = 0x7FFF;
74329a84457aed4c45bc900998b5e11c03023264208James Dong
74429a84457aed4c45bc900998b5e11c03023264208James Dong    availability.left = TRUE;
74529a84457aed4c45bc900998b5e11c03023264208James Dong    availability.top = TRUE;
74629a84457aed4c45bc900998b5e11c03023264208James Dong    if (blkidx <= 3) /* top row block  (!block_y) */
74729a84457aed4c45bc900998b5e11c03023264208James Dong    { /* check availability up */
74829a84457aed4c45bc900998b5e11c03023264208James Dong        availability.top = video->intraAvailB ;
74929a84457aed4c45bc900998b5e11c03023264208James Dong    }
75029a84457aed4c45bc900998b5e11c03023264208James Dong    if (!(blkidx&0x3)) /* left column block (!block_x)*/
75129a84457aed4c45bc900998b5e11c03023264208James Dong    { /* check availability left */
75229a84457aed4c45bc900998b5e11c03023264208James Dong        availability.left = video->intraAvailA ;
75329a84457aed4c45bc900998b5e11c03023264208James Dong    }
75429a84457aed4c45bc900998b5e11c03023264208James Dong    availability.top_right = BlkTopRight[blkidx];
75529a84457aed4c45bc900998b5e11c03023264208James Dong
75629a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.top_right == 2)
75729a84457aed4c45bc900998b5e11c03023264208James Dong    {
75829a84457aed4c45bc900998b5e11c03023264208James Dong        availability.top_right = video->intraAvailB;
75929a84457aed4c45bc900998b5e11c03023264208James Dong    }
76029a84457aed4c45bc900998b5e11c03023264208James Dong    else if (availability.top_right == 3)
76129a84457aed4c45bc900998b5e11c03023264208James Dong    {
76229a84457aed4c45bc900998b5e11c03023264208James Dong        availability.top_right = video->intraAvailC;
76329a84457aed4c45bc900998b5e11c03023264208James Dong    }
76429a84457aed4c45bc900998b5e11c03023264208James Dong
76529a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.top == TRUE)
76629a84457aed4c45bc900998b5e11c03023264208James Dong    {
76729a84457aed4c45bc900998b5e11c03023264208James Dong        temp = *(uint32*)(cur - pitch);
76829a84457aed4c45bc900998b5e11c03023264208James Dong        P_A = temp & 0xFF;
76929a84457aed4c45bc900998b5e11c03023264208James Dong        P_B = (temp >> 8) & 0xFF;
77029a84457aed4c45bc900998b5e11c03023264208James Dong        P_C = (temp >> 16) & 0xFF;
77129a84457aed4c45bc900998b5e11c03023264208James Dong        P_D = (temp >> 24) & 0xFF;
77229a84457aed4c45bc900998b5e11c03023264208James Dong    }
77329a84457aed4c45bc900998b5e11c03023264208James Dong    else
77429a84457aed4c45bc900998b5e11c03023264208James Dong    {
77529a84457aed4c45bc900998b5e11c03023264208James Dong        P_A = P_B = P_C = P_D = 128;
77629a84457aed4c45bc900998b5e11c03023264208James Dong    }
77729a84457aed4c45bc900998b5e11c03023264208James Dong
77829a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.top_right == TRUE)
77929a84457aed4c45bc900998b5e11c03023264208James Dong    {
78029a84457aed4c45bc900998b5e11c03023264208James Dong        temp = *(uint32*)(cur - pitch + 4);
78129a84457aed4c45bc900998b5e11c03023264208James Dong        P_E = temp & 0xFF;
78229a84457aed4c45bc900998b5e11c03023264208James Dong        P_F = (temp >> 8) & 0xFF;
78329a84457aed4c45bc900998b5e11c03023264208James Dong        P_G = (temp >> 16) & 0xFF;
78429a84457aed4c45bc900998b5e11c03023264208James Dong        P_H = (temp >> 24) & 0xFF;
78529a84457aed4c45bc900998b5e11c03023264208James Dong    }
78629a84457aed4c45bc900998b5e11c03023264208James Dong    else
78729a84457aed4c45bc900998b5e11c03023264208James Dong    {
78829a84457aed4c45bc900998b5e11c03023264208James Dong        P_E = P_F = P_G = P_H = 128;
78929a84457aed4c45bc900998b5e11c03023264208James Dong    }
79029a84457aed4c45bc900998b5e11c03023264208James Dong
79129a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.left == TRUE)
79229a84457aed4c45bc900998b5e11c03023264208James Dong    {
79329a84457aed4c45bc900998b5e11c03023264208James Dong        cur--;
79429a84457aed4c45bc900998b5e11c03023264208James Dong        P_I = *cur;
79529a84457aed4c45bc900998b5e11c03023264208James Dong        P_J = *(cur += pitch);
79629a84457aed4c45bc900998b5e11c03023264208James Dong        P_K = *(cur += pitch);
79729a84457aed4c45bc900998b5e11c03023264208James Dong        P_L = *(cur + pitch);
79829a84457aed4c45bc900998b5e11c03023264208James Dong        cur -= (pitch << 1);
79929a84457aed4c45bc900998b5e11c03023264208James Dong        cur++;
80029a84457aed4c45bc900998b5e11c03023264208James Dong    }
80129a84457aed4c45bc900998b5e11c03023264208James Dong    else
80229a84457aed4c45bc900998b5e11c03023264208James Dong    {
80329a84457aed4c45bc900998b5e11c03023264208James Dong        P_I = P_J = P_K = P_L = 128;
80429a84457aed4c45bc900998b5e11c03023264208James Dong    }
80529a84457aed4c45bc900998b5e11c03023264208James Dong
80629a84457aed4c45bc900998b5e11c03023264208James Dong    /* check if top-left pixel is available */
80729a84457aed4c45bc900998b5e11c03023264208James Dong    if (((blkidx > 3) && (blkidx&0x3)) || ((blkidx > 3) && video->intraAvailA)
80829a84457aed4c45bc900998b5e11c03023264208James Dong            || ((blkidx&0x3) && video->intraAvailB)
80929a84457aed4c45bc900998b5e11c03023264208James Dong            || (video->intraAvailA && video->intraAvailD && video->intraAvailB))
81029a84457aed4c45bc900998b5e11c03023264208James Dong    {
81129a84457aed4c45bc900998b5e11c03023264208James Dong        top_left = TRUE;
81229a84457aed4c45bc900998b5e11c03023264208James Dong        P_X = *(cur - pitch - 1);
81329a84457aed4c45bc900998b5e11c03023264208James Dong    }
81429a84457aed4c45bc900998b5e11c03023264208James Dong    else
81529a84457aed4c45bc900998b5e11c03023264208James Dong    {
81629a84457aed4c45bc900998b5e11c03023264208James Dong        P_X = 128;
81729a84457aed4c45bc900998b5e11c03023264208James Dong    }
81829a84457aed4c45bc900998b5e11c03023264208James Dong
81929a84457aed4c45bc900998b5e11c03023264208James Dong    //===== INTRA PREDICTION FOR 4x4 BLOCK =====
82029a84457aed4c45bc900998b5e11c03023264208James Dong    /* vertical */
82129a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Vertical] = 0;
82229a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.top)
82329a84457aed4c45bc900998b5e11c03023264208James Dong    {
82429a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Vertical] = 1;
82529a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Vertical];
82629a84457aed4c45bc900998b5e11c03023264208James Dong
82729a84457aed4c45bc900998b5e11c03023264208James Dong        temp = (P_D << 24) | (P_C << 16) | (P_B << 8) | P_A ;
82829a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) =  temp; /* write 4 at a time */
82929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) =  temp;
83029a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) =  temp;
83129a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) =  temp;
83229a84457aed4c45bc900998b5e11c03023264208James Dong    }
83329a84457aed4c45bc900998b5e11c03023264208James Dong    /* horizontal */
83429a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Horizontal] = 0;
83529a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Horizontal_Up] = 0;
83629a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.left)
83729a84457aed4c45bc900998b5e11c03023264208James Dong    {
83829a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Horizontal] = 1;
83929a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Horizontal];
84029a84457aed4c45bc900998b5e11c03023264208James Dong
84129a84457aed4c45bc900998b5e11c03023264208James Dong        temp = P_I | (P_I << 8);
84229a84457aed4c45bc900998b5e11c03023264208James Dong        temp = temp | (temp << 16);
84329a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) = temp;
84429a84457aed4c45bc900998b5e11c03023264208James Dong        temp = P_J | (P_J << 8);
84529a84457aed4c45bc900998b5e11c03023264208James Dong        temp = temp | (temp << 16);
84629a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
84729a84457aed4c45bc900998b5e11c03023264208James Dong        temp = P_K | (P_K << 8);
84829a84457aed4c45bc900998b5e11c03023264208James Dong        temp = temp | (temp << 16);
84929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
85029a84457aed4c45bc900998b5e11c03023264208James Dong        temp = P_L | (P_L << 8);
85129a84457aed4c45bc900998b5e11c03023264208James Dong        temp = temp | (temp << 16);
85229a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
85329a84457aed4c45bc900998b5e11c03023264208James Dong
85429a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Horizontal_Up] = 1;
85529a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Horizontal_Up];
85629a84457aed4c45bc900998b5e11c03023264208James Dong
85729a84457aed4c45bc900998b5e11c03023264208James Dong        Q0 = (P_J + P_K + 1) >> 1;
85829a84457aed4c45bc900998b5e11c03023264208James Dong        Q1 = (P_J + (P_K << 1) + P_L + 2) >> 2;
85929a84457aed4c45bc900998b5e11c03023264208James Dong        P0 = ((P_I + P_J + 1) >> 1);
86029a84457aed4c45bc900998b5e11c03023264208James Dong        P1 = ((P_I + (P_J << 1) + P_K + 2) >> 2);
86129a84457aed4c45bc900998b5e11c03023264208James Dong
86229a84457aed4c45bc900998b5e11c03023264208James Dong        temp = P0 | (P1 << 8);      // [P0 P1 Q0 Q1]
86329a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q0 << 16);     // [Q0 Q1 R0 DO]
86429a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q1 << 24);     // [R0 D0 D1 D1]
86529a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) = temp;      // [D1 D1 D1 D1]
86629a84457aed4c45bc900998b5e11c03023264208James Dong
86729a84457aed4c45bc900998b5e11c03023264208James Dong        D0 = (P_K + 3 * P_L + 2) >> 2;
86829a84457aed4c45bc900998b5e11c03023264208James Dong        R0 = (P_K + P_L + 1) >> 1;
86929a84457aed4c45bc900998b5e11c03023264208James Dong
87029a84457aed4c45bc900998b5e11c03023264208James Dong        temp = Q0 | (Q1 << 8);
87129a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (R0 << 16);
87229a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (D0 << 24);
87329a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
87429a84457aed4c45bc900998b5e11c03023264208James Dong
87529a84457aed4c45bc900998b5e11c03023264208James Dong        D1 = P_L;
87629a84457aed4c45bc900998b5e11c03023264208James Dong
87729a84457aed4c45bc900998b5e11c03023264208James Dong        temp = R0 | (D0 << 8);
87829a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (D1 << 16);
87929a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (D1 << 24);
88029a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
88129a84457aed4c45bc900998b5e11c03023264208James Dong
88229a84457aed4c45bc900998b5e11c03023264208James Dong        temp = D1 | (D1 << 8);
88329a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (temp << 16);
88429a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
88529a84457aed4c45bc900998b5e11c03023264208James Dong    }
88629a84457aed4c45bc900998b5e11c03023264208James Dong    /* DC */
88729a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_DC] = 1;
88829a84457aed4c45bc900998b5e11c03023264208James Dong    pred = encvid->pred_i4[AVC_I4_DC];
88929a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.left)
89029a84457aed4c45bc900998b5e11c03023264208James Dong    {
89129a84457aed4c45bc900998b5e11c03023264208James Dong        DC = P_I + P_J + P_K + P_L;
89229a84457aed4c45bc900998b5e11c03023264208James Dong
89329a84457aed4c45bc900998b5e11c03023264208James Dong        if (availability.top)
89429a84457aed4c45bc900998b5e11c03023264208James Dong        {
89529a84457aed4c45bc900998b5e11c03023264208James Dong            DC = (P_A + P_B + P_C + P_D + DC + 4) >> 3;
89629a84457aed4c45bc900998b5e11c03023264208James Dong        }
89729a84457aed4c45bc900998b5e11c03023264208James Dong        else
89829a84457aed4c45bc900998b5e11c03023264208James Dong        {
89929a84457aed4c45bc900998b5e11c03023264208James Dong            DC = (DC + 2) >> 2;
90029a84457aed4c45bc900998b5e11c03023264208James Dong
90129a84457aed4c45bc900998b5e11c03023264208James Dong        }
90229a84457aed4c45bc900998b5e11c03023264208James Dong    }
90329a84457aed4c45bc900998b5e11c03023264208James Dong    else if (availability.top)
90429a84457aed4c45bc900998b5e11c03023264208James Dong    {
90529a84457aed4c45bc900998b5e11c03023264208James Dong        DC = (P_A + P_B + P_C + P_D + 2) >> 2;
90629a84457aed4c45bc900998b5e11c03023264208James Dong
90729a84457aed4c45bc900998b5e11c03023264208James Dong    }
90829a84457aed4c45bc900998b5e11c03023264208James Dong    else
90929a84457aed4c45bc900998b5e11c03023264208James Dong    {
91029a84457aed4c45bc900998b5e11c03023264208James Dong        DC = 128;
91129a84457aed4c45bc900998b5e11c03023264208James Dong    }
91229a84457aed4c45bc900998b5e11c03023264208James Dong
91329a84457aed4c45bc900998b5e11c03023264208James Dong    temp = DC | (DC << 8);
91429a84457aed4c45bc900998b5e11c03023264208James Dong    temp = temp | (temp << 16);
91529a84457aed4c45bc900998b5e11c03023264208James Dong    *((uint32*)pred) = temp;
91629a84457aed4c45bc900998b5e11c03023264208James Dong    *((uint32*)(pred += 4)) = temp;
91729a84457aed4c45bc900998b5e11c03023264208James Dong    *((uint32*)(pred += 4)) = temp;
91829a84457aed4c45bc900998b5e11c03023264208James Dong    *((uint32*)(pred += 4)) = temp;
91929a84457aed4c45bc900998b5e11c03023264208James Dong
92029a84457aed4c45bc900998b5e11c03023264208James Dong    /* Down-left */
92129a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Diagonal_Down_Left] = 0;
92229a84457aed4c45bc900998b5e11c03023264208James Dong
92329a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.top)
92429a84457aed4c45bc900998b5e11c03023264208James Dong    {
92529a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Diagonal_Down_Left] = 1;
92629a84457aed4c45bc900998b5e11c03023264208James Dong
92729a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Diagonal_Down_Left];
92829a84457aed4c45bc900998b5e11c03023264208James Dong
92929a84457aed4c45bc900998b5e11c03023264208James Dong        r0 = P_A;
93029a84457aed4c45bc900998b5e11c03023264208James Dong        r1 = P_B;
93129a84457aed4c45bc900998b5e11c03023264208James Dong        r2 = P_C;
93229a84457aed4c45bc900998b5e11c03023264208James Dong        r3 = P_D;
93329a84457aed4c45bc900998b5e11c03023264208James Dong
93429a84457aed4c45bc900998b5e11c03023264208James Dong        r0 += (r1 << 1);
93529a84457aed4c45bc900998b5e11c03023264208James Dong        r0 += r2;
93629a84457aed4c45bc900998b5e11c03023264208James Dong        r0 += 2;
93729a84457aed4c45bc900998b5e11c03023264208James Dong        r0 >>= 2;
93829a84457aed4c45bc900998b5e11c03023264208James Dong        r1 += (r2 << 1);
93929a84457aed4c45bc900998b5e11c03023264208James Dong        r1 += r3;
94029a84457aed4c45bc900998b5e11c03023264208James Dong        r1 += 2;
94129a84457aed4c45bc900998b5e11c03023264208James Dong        r1 >>= 2;
94229a84457aed4c45bc900998b5e11c03023264208James Dong
94329a84457aed4c45bc900998b5e11c03023264208James Dong        if (availability.top_right)
94429a84457aed4c45bc900998b5e11c03023264208James Dong        {
94529a84457aed4c45bc900998b5e11c03023264208James Dong            r4 = P_E;
94629a84457aed4c45bc900998b5e11c03023264208James Dong            r5 = P_F;
94729a84457aed4c45bc900998b5e11c03023264208James Dong            r6 = P_G;
94829a84457aed4c45bc900998b5e11c03023264208James Dong            r7 = P_H;
94929a84457aed4c45bc900998b5e11c03023264208James Dong
95029a84457aed4c45bc900998b5e11c03023264208James Dong            r2 += (r3 << 1);
95129a84457aed4c45bc900998b5e11c03023264208James Dong            r2 += r4;
95229a84457aed4c45bc900998b5e11c03023264208James Dong            r2 += 2;
95329a84457aed4c45bc900998b5e11c03023264208James Dong            r2 >>= 2;
95429a84457aed4c45bc900998b5e11c03023264208James Dong            r3 += (r4 << 1);
95529a84457aed4c45bc900998b5e11c03023264208James Dong            r3 += r5;
95629a84457aed4c45bc900998b5e11c03023264208James Dong            r3 += 2;
95729a84457aed4c45bc900998b5e11c03023264208James Dong            r3 >>= 2;
95829a84457aed4c45bc900998b5e11c03023264208James Dong            r4 += (r5 << 1);
95929a84457aed4c45bc900998b5e11c03023264208James Dong            r4 += r6;
96029a84457aed4c45bc900998b5e11c03023264208James Dong            r4 += 2;
96129a84457aed4c45bc900998b5e11c03023264208James Dong            r4 >>= 2;
96229a84457aed4c45bc900998b5e11c03023264208James Dong            r5 += (r6 << 1);
96329a84457aed4c45bc900998b5e11c03023264208James Dong            r5 += r7;
96429a84457aed4c45bc900998b5e11c03023264208James Dong            r5 += 2;
96529a84457aed4c45bc900998b5e11c03023264208James Dong            r5 >>= 2;
96629a84457aed4c45bc900998b5e11c03023264208James Dong            r6 += (3 * r7);
96729a84457aed4c45bc900998b5e11c03023264208James Dong            r6 += 2;
96829a84457aed4c45bc900998b5e11c03023264208James Dong            r6 >>= 2;
96929a84457aed4c45bc900998b5e11c03023264208James Dong            temp = r0 | (r1 << 8);
97029a84457aed4c45bc900998b5e11c03023264208James Dong            temp |= (r2 << 16);
97129a84457aed4c45bc900998b5e11c03023264208James Dong            temp |= (r3 << 24);
97229a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)pred) = temp;
97329a84457aed4c45bc900998b5e11c03023264208James Dong
97429a84457aed4c45bc900998b5e11c03023264208James Dong            temp = (temp >> 8) | (r4 << 24);
97529a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 4)) = temp;
97629a84457aed4c45bc900998b5e11c03023264208James Dong
97729a84457aed4c45bc900998b5e11c03023264208James Dong            temp = (temp >> 8) | (r5 << 24);
97829a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 4)) = temp;
97929a84457aed4c45bc900998b5e11c03023264208James Dong
98029a84457aed4c45bc900998b5e11c03023264208James Dong            temp = (temp >> 8) | (r6 << 24);
98129a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 4)) = temp;
98229a84457aed4c45bc900998b5e11c03023264208James Dong        }
98329a84457aed4c45bc900998b5e11c03023264208James Dong        else
98429a84457aed4c45bc900998b5e11c03023264208James Dong        {
98529a84457aed4c45bc900998b5e11c03023264208James Dong            r2 += (r3 * 3);
98629a84457aed4c45bc900998b5e11c03023264208James Dong            r2 += 2;
98729a84457aed4c45bc900998b5e11c03023264208James Dong            r2 >>= 2;
98829a84457aed4c45bc900998b5e11c03023264208James Dong            r3 = ((r3 << 2) + 2);
98929a84457aed4c45bc900998b5e11c03023264208James Dong            r3 >>= 2;
99029a84457aed4c45bc900998b5e11c03023264208James Dong
99129a84457aed4c45bc900998b5e11c03023264208James Dong            temp = r0 | (r1 << 8);
99229a84457aed4c45bc900998b5e11c03023264208James Dong            temp |= (r2 << 16);
99329a84457aed4c45bc900998b5e11c03023264208James Dong            temp |= (r3 << 24);
99429a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)pred) = temp;
99529a84457aed4c45bc900998b5e11c03023264208James Dong
99629a84457aed4c45bc900998b5e11c03023264208James Dong            temp = (temp >> 8) | (r3 << 24);
99729a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 4)) = temp;
99829a84457aed4c45bc900998b5e11c03023264208James Dong
99929a84457aed4c45bc900998b5e11c03023264208James Dong            temp = (temp >> 8) | (r3 << 24);
100029a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 4)) = temp;
100129a84457aed4c45bc900998b5e11c03023264208James Dong
100229a84457aed4c45bc900998b5e11c03023264208James Dong            temp = (temp >> 8) | (r3 << 24);
100329a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred += 4)) = temp;
100429a84457aed4c45bc900998b5e11c03023264208James Dong
100529a84457aed4c45bc900998b5e11c03023264208James Dong        }
100629a84457aed4c45bc900998b5e11c03023264208James Dong    }
100729a84457aed4c45bc900998b5e11c03023264208James Dong
100829a84457aed4c45bc900998b5e11c03023264208James Dong    /* Down Right */
100929a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Diagonal_Down_Right] = 0;
101029a84457aed4c45bc900998b5e11c03023264208James Dong    /* Diagonal Vertical Right */
101129a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Vertical_Right] = 0;
101229a84457aed4c45bc900998b5e11c03023264208James Dong    /* Horizontal Down */
101329a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Horizontal_Down] = 0;
101429a84457aed4c45bc900998b5e11c03023264208James Dong
101529a84457aed4c45bc900998b5e11c03023264208James Dong    if (top_left == TRUE)
101629a84457aed4c45bc900998b5e11c03023264208James Dong    {
101729a84457aed4c45bc900998b5e11c03023264208James Dong        /* Down Right */
101829a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Diagonal_Down_Right] = 1;
101929a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Diagonal_Down_Right];
102029a84457aed4c45bc900998b5e11c03023264208James Dong
102129a84457aed4c45bc900998b5e11c03023264208James Dong        Q_x = (P_A + 2 * P_B + P_C + 2) >> 2;
102229a84457aed4c45bc900998b5e11c03023264208James Dong        R_x = (P_B + 2 * P_C + P_D + 2) >> 2;
102329a84457aed4c45bc900998b5e11c03023264208James Dong        P_x = (P_X + 2 * P_A + P_B + 2) >> 2;
102429a84457aed4c45bc900998b5e11c03023264208James Dong        D   = (P_A + 2 * P_X + P_I + 2) >> 2;
102529a84457aed4c45bc900998b5e11c03023264208James Dong        P_y = (P_X + 2 * P_I + P_J + 2) >> 2;
102629a84457aed4c45bc900998b5e11c03023264208James Dong        Q_y = (P_I + 2 * P_J + P_K + 2) >> 2;
102729a84457aed4c45bc900998b5e11c03023264208James Dong        R_y = (P_J + 2 * P_K + P_L + 2) >> 2;
102829a84457aed4c45bc900998b5e11c03023264208James Dong
102929a84457aed4c45bc900998b5e11c03023264208James Dong        /* we can pack these */
103029a84457aed4c45bc900998b5e11c03023264208James Dong        temp =  D | (P_x << 8);   //[D   P_x Q_x R_x]
103129a84457aed4c45bc900998b5e11c03023264208James Dong        //[P_y D   P_x Q_x]
103229a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q_x << 16); //[Q_y P_y D   P_x]
103329a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (R_x << 24);  //[R_y Q_y P_y D  ]
103429a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) = temp;
103529a84457aed4c45bc900998b5e11c03023264208James Dong
103629a84457aed4c45bc900998b5e11c03023264208James Dong        temp =  P_y | (D << 8);
103729a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (P_x << 16);
103829a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q_x << 24);
103929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
104029a84457aed4c45bc900998b5e11c03023264208James Dong
104129a84457aed4c45bc900998b5e11c03023264208James Dong        temp =  Q_y | (P_y << 8);
104229a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (D << 16);
104329a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (P_x << 24);
104429a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
104529a84457aed4c45bc900998b5e11c03023264208James Dong
104629a84457aed4c45bc900998b5e11c03023264208James Dong        temp = R_y | (Q_y << 8);
104729a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (P_y << 16);
104829a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (D << 24);
104929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
105029a84457aed4c45bc900998b5e11c03023264208James Dong
105129a84457aed4c45bc900998b5e11c03023264208James Dong
105229a84457aed4c45bc900998b5e11c03023264208James Dong        /* Diagonal Vertical Right */
105329a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Vertical_Right] = 1;
105429a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Vertical_Right];
105529a84457aed4c45bc900998b5e11c03023264208James Dong
105629a84457aed4c45bc900998b5e11c03023264208James Dong        Q0 = P_A + P_B + 1;
105729a84457aed4c45bc900998b5e11c03023264208James Dong        R0 = P_B + P_C + 1;
105829a84457aed4c45bc900998b5e11c03023264208James Dong        S0 = P_C + P_D + 1;
105929a84457aed4c45bc900998b5e11c03023264208James Dong        P0 = P_X + P_A + 1;
106029a84457aed4c45bc900998b5e11c03023264208James Dong        D = (P_I + 2 * P_X + P_A + 2) >> 2;
106129a84457aed4c45bc900998b5e11c03023264208James Dong
106229a84457aed4c45bc900998b5e11c03023264208James Dong        P1 = (P0 + Q0) >> 2;
106329a84457aed4c45bc900998b5e11c03023264208James Dong        Q1 = (Q0 + R0) >> 2;
106429a84457aed4c45bc900998b5e11c03023264208James Dong        R1 = (R0 + S0) >> 2;
106529a84457aed4c45bc900998b5e11c03023264208James Dong
106629a84457aed4c45bc900998b5e11c03023264208James Dong        P0 >>= 1;
106729a84457aed4c45bc900998b5e11c03023264208James Dong        Q0 >>= 1;
106829a84457aed4c45bc900998b5e11c03023264208James Dong        R0 >>= 1;
106929a84457aed4c45bc900998b5e11c03023264208James Dong        S0 >>= 1;
107029a84457aed4c45bc900998b5e11c03023264208James Dong
107129a84457aed4c45bc900998b5e11c03023264208James Dong        P2 = (P_X + 2 * P_I + P_J + 2) >> 2;
107229a84457aed4c45bc900998b5e11c03023264208James Dong        Q2 = (P_I + 2 * P_J + P_K + 2) >> 2;
107329a84457aed4c45bc900998b5e11c03023264208James Dong
107429a84457aed4c45bc900998b5e11c03023264208James Dong        temp =  P0 | (Q0 << 8);  //[P0 Q0 R0 S0]
107529a84457aed4c45bc900998b5e11c03023264208James Dong        //[D  P1 Q1 R1]
107629a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (R0 << 16); //[P2 P0 Q0 R0]
107729a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (S0 << 24); //[Q2 D  P1 Q1]
107829a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) =  temp;
107929a84457aed4c45bc900998b5e11c03023264208James Dong
108029a84457aed4c45bc900998b5e11c03023264208James Dong        temp =  D | (P1 << 8);
108129a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q1 << 16);
108229a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (R1 << 24);
108329a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) =  temp;
108429a84457aed4c45bc900998b5e11c03023264208James Dong
108529a84457aed4c45bc900998b5e11c03023264208James Dong        temp = P2 | (P0 << 8);
108629a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q0 << 16);
108729a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (R0 << 24);
108829a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) =  temp;
108929a84457aed4c45bc900998b5e11c03023264208James Dong
109029a84457aed4c45bc900998b5e11c03023264208James Dong        temp = Q2 | (D << 8);
109129a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (P1 << 16);
109229a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q1 << 24);
109329a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) =  temp;
109429a84457aed4c45bc900998b5e11c03023264208James Dong
109529a84457aed4c45bc900998b5e11c03023264208James Dong
109629a84457aed4c45bc900998b5e11c03023264208James Dong        /* Horizontal Down */
109729a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Horizontal_Down] = 1;
109829a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Horizontal_Down];
109929a84457aed4c45bc900998b5e11c03023264208James Dong
110029a84457aed4c45bc900998b5e11c03023264208James Dong
110129a84457aed4c45bc900998b5e11c03023264208James Dong        Q2 = (P_A + 2 * P_B + P_C + 2) >> 2;
110229a84457aed4c45bc900998b5e11c03023264208James Dong        P2 = (P_X + 2 * P_A + P_B + 2) >> 2;
110329a84457aed4c45bc900998b5e11c03023264208James Dong        D = (P_I + 2 * P_X + P_A + 2) >> 2;
110429a84457aed4c45bc900998b5e11c03023264208James Dong        P0 = P_X + P_I + 1;
110529a84457aed4c45bc900998b5e11c03023264208James Dong        Q0 = P_I + P_J + 1;
110629a84457aed4c45bc900998b5e11c03023264208James Dong        R0 = P_J + P_K + 1;
110729a84457aed4c45bc900998b5e11c03023264208James Dong        S0 = P_K + P_L + 1;
110829a84457aed4c45bc900998b5e11c03023264208James Dong
110929a84457aed4c45bc900998b5e11c03023264208James Dong        P1 = (P0 + Q0) >> 2;
111029a84457aed4c45bc900998b5e11c03023264208James Dong        Q1 = (Q0 + R0) >> 2;
111129a84457aed4c45bc900998b5e11c03023264208James Dong        R1 = (R0 + S0) >> 2;
111229a84457aed4c45bc900998b5e11c03023264208James Dong
111329a84457aed4c45bc900998b5e11c03023264208James Dong        P0 >>= 1;
111429a84457aed4c45bc900998b5e11c03023264208James Dong        Q0 >>= 1;
111529a84457aed4c45bc900998b5e11c03023264208James Dong        R0 >>= 1;
111629a84457aed4c45bc900998b5e11c03023264208James Dong        S0 >>= 1;
111729a84457aed4c45bc900998b5e11c03023264208James Dong
111829a84457aed4c45bc900998b5e11c03023264208James Dong
111929a84457aed4c45bc900998b5e11c03023264208James Dong        /* we can pack these */
112029a84457aed4c45bc900998b5e11c03023264208James Dong        temp = P0 | (D << 8);   //[P0 D  P2 Q2]
112129a84457aed4c45bc900998b5e11c03023264208James Dong        //[Q0 P1 P0 D ]
112229a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (P2 << 16);  //[R0 Q1 Q0 P1]
112329a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q2 << 24); //[S0 R1 R0 Q1]
112429a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) = temp;
112529a84457aed4c45bc900998b5e11c03023264208James Dong
112629a84457aed4c45bc900998b5e11c03023264208James Dong        temp = Q0 | (P1 << 8);
112729a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (P0 << 16);
112829a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (D << 24);
112929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
113029a84457aed4c45bc900998b5e11c03023264208James Dong
113129a84457aed4c45bc900998b5e11c03023264208James Dong        temp = R0 | (Q1 << 8);
113229a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q0 << 16);
113329a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (P1 << 24);
113429a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
113529a84457aed4c45bc900998b5e11c03023264208James Dong
113629a84457aed4c45bc900998b5e11c03023264208James Dong        temp = S0 | (R1 << 8);
113729a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (R0 << 16);
113829a84457aed4c45bc900998b5e11c03023264208James Dong        temp |= (Q1 << 24);
113929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp;
114029a84457aed4c45bc900998b5e11c03023264208James Dong
114129a84457aed4c45bc900998b5e11c03023264208James Dong    }
114229a84457aed4c45bc900998b5e11c03023264208James Dong
114329a84457aed4c45bc900998b5e11c03023264208James Dong    /* vertical left */
114429a84457aed4c45bc900998b5e11c03023264208James Dong    mode_avail[AVC_I4_Vertical_Left] = 0;
114529a84457aed4c45bc900998b5e11c03023264208James Dong    if (availability.top)
114629a84457aed4c45bc900998b5e11c03023264208James Dong    {
114729a84457aed4c45bc900998b5e11c03023264208James Dong        mode_avail[AVC_I4_Vertical_Left] = 1;
114829a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_i4[AVC_I4_Vertical_Left];
114929a84457aed4c45bc900998b5e11c03023264208James Dong
115029a84457aed4c45bc900998b5e11c03023264208James Dong        x0 = P_A + P_B + 1;
115129a84457aed4c45bc900998b5e11c03023264208James Dong        x1 = P_B + P_C + 1;
115229a84457aed4c45bc900998b5e11c03023264208James Dong        x2 = P_C + P_D + 1;
115329a84457aed4c45bc900998b5e11c03023264208James Dong        if (availability.top_right)
115429a84457aed4c45bc900998b5e11c03023264208James Dong        {
115529a84457aed4c45bc900998b5e11c03023264208James Dong            x3 = P_D + P_E + 1;
115629a84457aed4c45bc900998b5e11c03023264208James Dong            x4 = P_E + P_F + 1;
115729a84457aed4c45bc900998b5e11c03023264208James Dong            x5 = P_F + P_G + 1;
115829a84457aed4c45bc900998b5e11c03023264208James Dong        }
115929a84457aed4c45bc900998b5e11c03023264208James Dong        else
116029a84457aed4c45bc900998b5e11c03023264208James Dong        {
116129a84457aed4c45bc900998b5e11c03023264208James Dong            x3 = x4 = x5 = (P_D << 1) + 1;
116229a84457aed4c45bc900998b5e11c03023264208James Dong        }
116329a84457aed4c45bc900998b5e11c03023264208James Dong
116429a84457aed4c45bc900998b5e11c03023264208James Dong        temp1 = (x0 >> 1);
116529a84457aed4c45bc900998b5e11c03023264208James Dong        temp1 |= ((x1 >> 1) << 8);
116629a84457aed4c45bc900998b5e11c03023264208James Dong        temp1 |= ((x2 >> 1) << 16);
116729a84457aed4c45bc900998b5e11c03023264208James Dong        temp1 |= ((x3 >> 1) << 24);
116829a84457aed4c45bc900998b5e11c03023264208James Dong
116929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) = temp1;
117029a84457aed4c45bc900998b5e11c03023264208James Dong
117129a84457aed4c45bc900998b5e11c03023264208James Dong        temp2 = ((x0 + x1) >> 2);
117229a84457aed4c45bc900998b5e11c03023264208James Dong        temp2 |= (((x1 + x2) >> 2) << 8);
117329a84457aed4c45bc900998b5e11c03023264208James Dong        temp2 |= (((x2 + x3) >> 2) << 16);
117429a84457aed4c45bc900998b5e11c03023264208James Dong        temp2 |= (((x3 + x4) >> 2) << 24);
117529a84457aed4c45bc900998b5e11c03023264208James Dong
117629a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp2;
117729a84457aed4c45bc900998b5e11c03023264208James Dong
117829a84457aed4c45bc900998b5e11c03023264208James Dong        temp1 = (temp1 >> 8) | ((x4 >> 1) << 24);   /* rotate out old value */
117929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp1;
118029a84457aed4c45bc900998b5e11c03023264208James Dong
118129a84457aed4c45bc900998b5e11c03023264208James Dong        temp2 = (temp2 >> 8) | (((x4 + x5) >> 2) << 24); /* rotate out old value */
118229a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred += 4)) = temp2;
118329a84457aed4c45bc900998b5e11c03023264208James Dong    }
118429a84457aed4c45bc900998b5e11c03023264208James Dong
118529a84457aed4c45bc900998b5e11c03023264208James Dong    //===== LOOP OVER ALL 4x4 INTRA PREDICTION MODES =====
118629a84457aed4c45bc900998b5e11c03023264208James Dong    // can re-order the search here instead of going in order
118729a84457aed4c45bc900998b5e11c03023264208James Dong
118829a84457aed4c45bc900998b5e11c03023264208James Dong    // find most probable mode
118929a84457aed4c45bc900998b5e11c03023264208James Dong    encvid->mostProbableI4Mode[blkidx] = mostProbableMode = FindMostProbableI4Mode(video, blkidx);
119029a84457aed4c45bc900998b5e11c03023264208James Dong
119129a84457aed4c45bc900998b5e11c03023264208James Dong    min_cost = 0xFFFF;
119229a84457aed4c45bc900998b5e11c03023264208James Dong
119329a84457aed4c45bc900998b5e11c03023264208James Dong    for (ipmode = 0; ipmode < AVCNumI4PredMode; ipmode++)
119429a84457aed4c45bc900998b5e11c03023264208James Dong    {
119529a84457aed4c45bc900998b5e11c03023264208James Dong        if (mode_avail[ipmode] == TRUE)
119629a84457aed4c45bc900998b5e11c03023264208James Dong        {
119729a84457aed4c45bc900998b5e11c03023264208James Dong            cost  = (ipmode == mostProbableMode) ? 0 : fixedcost;
119829a84457aed4c45bc900998b5e11c03023264208James Dong            pred = encvid->pred_i4[ipmode];
119929a84457aed4c45bc900998b5e11c03023264208James Dong
120029a84457aed4c45bc900998b5e11c03023264208James Dong            cost_i4(org, org_pitch, pred, &cost);
120129a84457aed4c45bc900998b5e11c03023264208James Dong
120229a84457aed4c45bc900998b5e11c03023264208James Dong            if (cost < min_cost)
120329a84457aed4c45bc900998b5e11c03023264208James Dong            {
120429a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[blkidx] = (AVCIntra4x4PredMode)ipmode;
120529a84457aed4c45bc900998b5e11c03023264208James Dong                min_cost   = cost;
120629a84457aed4c45bc900998b5e11c03023264208James Dong                min_sad = cost - ((ipmode == mostProbableMode) ? 0 : fixedcost);
120729a84457aed4c45bc900998b5e11c03023264208James Dong            }
120829a84457aed4c45bc900998b5e11c03023264208James Dong        }
120929a84457aed4c45bc900998b5e11c03023264208James Dong    }
121029a84457aed4c45bc900998b5e11c03023264208James Dong
121129a84457aed4c45bc900998b5e11c03023264208James Dong    if (blkidx == 0)
121229a84457aed4c45bc900998b5e11c03023264208James Dong    {
121329a84457aed4c45bc900998b5e11c03023264208James Dong        encvid->i4_sad = min_sad;
121429a84457aed4c45bc900998b5e11c03023264208James Dong    }
121529a84457aed4c45bc900998b5e11c03023264208James Dong    else
121629a84457aed4c45bc900998b5e11c03023264208James Dong    {
121729a84457aed4c45bc900998b5e11c03023264208James Dong        encvid->i4_sad += min_sad;
121829a84457aed4c45bc900998b5e11c03023264208James Dong    }
121929a84457aed4c45bc900998b5e11c03023264208James Dong
122029a84457aed4c45bc900998b5e11c03023264208James Dong    return min_cost;
122129a84457aed4c45bc900998b5e11c03023264208James Dong}
122229a84457aed4c45bc900998b5e11c03023264208James Dong
122329a84457aed4c45bc900998b5e11c03023264208James Dongint FindMostProbableI4Mode(AVCCommonObj *video, int blkidx)
122429a84457aed4c45bc900998b5e11c03023264208James Dong{
122529a84457aed4c45bc900998b5e11c03023264208James Dong    int dcOnlyPredictionFlag;
122629a84457aed4c45bc900998b5e11c03023264208James Dong    AVCMacroblock *currMB = video->currMB;
122729a84457aed4c45bc900998b5e11c03023264208James Dong    int intra4x4PredModeA, intra4x4PredModeB, predIntra4x4PredMode;
122829a84457aed4c45bc900998b5e11c03023264208James Dong
122929a84457aed4c45bc900998b5e11c03023264208James Dong
123029a84457aed4c45bc900998b5e11c03023264208James Dong    dcOnlyPredictionFlag = 0;
123129a84457aed4c45bc900998b5e11c03023264208James Dong    if (blkidx&0x3)
123229a84457aed4c45bc900998b5e11c03023264208James Dong    {
123329a84457aed4c45bc900998b5e11c03023264208James Dong        intra4x4PredModeA = currMB->i4Mode[blkidx-1]; // block to the left
123429a84457aed4c45bc900998b5e11c03023264208James Dong    }
123529a84457aed4c45bc900998b5e11c03023264208James Dong    else /* for blk 0, 4, 8, 12 */
123629a84457aed4c45bc900998b5e11c03023264208James Dong    {
123729a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->intraAvailA)
123829a84457aed4c45bc900998b5e11c03023264208James Dong        {
123929a84457aed4c45bc900998b5e11c03023264208James Dong            if (video->mblock[video->mbAddrA].mbMode == AVC_I4)
124029a84457aed4c45bc900998b5e11c03023264208James Dong            {
124129a84457aed4c45bc900998b5e11c03023264208James Dong                intra4x4PredModeA = video->mblock[video->mbAddrA].i4Mode[blkidx + 3];
124229a84457aed4c45bc900998b5e11c03023264208James Dong            }
124329a84457aed4c45bc900998b5e11c03023264208James Dong            else
124429a84457aed4c45bc900998b5e11c03023264208James Dong            {
124529a84457aed4c45bc900998b5e11c03023264208James Dong                intra4x4PredModeA = AVC_I4_DC;
124629a84457aed4c45bc900998b5e11c03023264208James Dong            }
124729a84457aed4c45bc900998b5e11c03023264208James Dong        }
124829a84457aed4c45bc900998b5e11c03023264208James Dong        else
124929a84457aed4c45bc900998b5e11c03023264208James Dong        {
125029a84457aed4c45bc900998b5e11c03023264208James Dong            dcOnlyPredictionFlag = 1;
125129a84457aed4c45bc900998b5e11c03023264208James Dong            goto PRED_RESULT_READY;  // skip below
125229a84457aed4c45bc900998b5e11c03023264208James Dong        }
125329a84457aed4c45bc900998b5e11c03023264208James Dong    }
125429a84457aed4c45bc900998b5e11c03023264208James Dong
125529a84457aed4c45bc900998b5e11c03023264208James Dong    if (blkidx >> 2)
125629a84457aed4c45bc900998b5e11c03023264208James Dong    {
125729a84457aed4c45bc900998b5e11c03023264208James Dong        intra4x4PredModeB = currMB->i4Mode[blkidx-4]; // block above
125829a84457aed4c45bc900998b5e11c03023264208James Dong    }
125929a84457aed4c45bc900998b5e11c03023264208James Dong    else /* block 0, 1, 2, 3 */
126029a84457aed4c45bc900998b5e11c03023264208James Dong    {
126129a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->intraAvailB)
126229a84457aed4c45bc900998b5e11c03023264208James Dong        {
126329a84457aed4c45bc900998b5e11c03023264208James Dong            if (video->mblock[video->mbAddrB].mbMode == AVC_I4)
126429a84457aed4c45bc900998b5e11c03023264208James Dong            {
126529a84457aed4c45bc900998b5e11c03023264208James Dong                intra4x4PredModeB = video->mblock[video->mbAddrB].i4Mode[blkidx+12];
126629a84457aed4c45bc900998b5e11c03023264208James Dong            }
126729a84457aed4c45bc900998b5e11c03023264208James Dong            else
126829a84457aed4c45bc900998b5e11c03023264208James Dong            {
126929a84457aed4c45bc900998b5e11c03023264208James Dong                intra4x4PredModeB = AVC_I4_DC;
127029a84457aed4c45bc900998b5e11c03023264208James Dong            }
127129a84457aed4c45bc900998b5e11c03023264208James Dong        }
127229a84457aed4c45bc900998b5e11c03023264208James Dong        else
127329a84457aed4c45bc900998b5e11c03023264208James Dong        {
127429a84457aed4c45bc900998b5e11c03023264208James Dong            dcOnlyPredictionFlag = 1;
127529a84457aed4c45bc900998b5e11c03023264208James Dong        }
127629a84457aed4c45bc900998b5e11c03023264208James Dong    }
127729a84457aed4c45bc900998b5e11c03023264208James Dong
127829a84457aed4c45bc900998b5e11c03023264208James DongPRED_RESULT_READY:
127929a84457aed4c45bc900998b5e11c03023264208James Dong    if (dcOnlyPredictionFlag)
128029a84457aed4c45bc900998b5e11c03023264208James Dong    {
128129a84457aed4c45bc900998b5e11c03023264208James Dong        intra4x4PredModeA = intra4x4PredModeB = AVC_I4_DC;
128229a84457aed4c45bc900998b5e11c03023264208James Dong    }
128329a84457aed4c45bc900998b5e11c03023264208James Dong
128429a84457aed4c45bc900998b5e11c03023264208James Dong    predIntra4x4PredMode = AVC_MIN(intra4x4PredModeA, intra4x4PredModeB);
128529a84457aed4c45bc900998b5e11c03023264208James Dong
128629a84457aed4c45bc900998b5e11c03023264208James Dong    return predIntra4x4PredMode;
128729a84457aed4c45bc900998b5e11c03023264208James Dong}
128829a84457aed4c45bc900998b5e11c03023264208James Dong
128929a84457aed4c45bc900998b5e11c03023264208James Dongvoid cost_i4(uint8 *org, int org_pitch, uint8 *pred, uint16 *cost)
129029a84457aed4c45bc900998b5e11c03023264208James Dong{
129129a84457aed4c45bc900998b5e11c03023264208James Dong    int k;
129229a84457aed4c45bc900998b5e11c03023264208James Dong    int16 res[16], *pres;
129329a84457aed4c45bc900998b5e11c03023264208James Dong    int m0, m1, m2, m3, tmp1;
129429a84457aed4c45bc900998b5e11c03023264208James Dong    int satd = 0;
129529a84457aed4c45bc900998b5e11c03023264208James Dong
129629a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
129729a84457aed4c45bc900998b5e11c03023264208James Dong    // horizontal transform
129829a84457aed4c45bc900998b5e11c03023264208James Dong    k = 4;
129929a84457aed4c45bc900998b5e11c03023264208James Dong    while (k > 0)
130029a84457aed4c45bc900998b5e11c03023264208James Dong    {
130129a84457aed4c45bc900998b5e11c03023264208James Dong        m0 = org[0] - pred[0];
130229a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = org[3] - pred[3];
130329a84457aed4c45bc900998b5e11c03023264208James Dong        m0 += m3;
130429a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = m0 - (m3 << 1);
130529a84457aed4c45bc900998b5e11c03023264208James Dong        m1 = org[1] - pred[1];
130629a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = org[2] - pred[2];
130729a84457aed4c45bc900998b5e11c03023264208James Dong        m1 += m2;
130829a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = m1 - (m2 << 1);
130929a84457aed4c45bc900998b5e11c03023264208James Dong        pres[0] = m0 + m1;
131029a84457aed4c45bc900998b5e11c03023264208James Dong        pres[2] = m0 - m1;
131129a84457aed4c45bc900998b5e11c03023264208James Dong        pres[1] = m2 + m3;
131229a84457aed4c45bc900998b5e11c03023264208James Dong        pres[3] = m3 - m2;
131329a84457aed4c45bc900998b5e11c03023264208James Dong
131429a84457aed4c45bc900998b5e11c03023264208James Dong        org += org_pitch;
131529a84457aed4c45bc900998b5e11c03023264208James Dong        pres += 4;
131629a84457aed4c45bc900998b5e11c03023264208James Dong        pred += 4;
131729a84457aed4c45bc900998b5e11c03023264208James Dong        k--;
131829a84457aed4c45bc900998b5e11c03023264208James Dong    }
131929a84457aed4c45bc900998b5e11c03023264208James Dong    /* vertical transform */
132029a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
132129a84457aed4c45bc900998b5e11c03023264208James Dong    k = 4;
132229a84457aed4c45bc900998b5e11c03023264208James Dong    while (k > 0)
132329a84457aed4c45bc900998b5e11c03023264208James Dong    {
132429a84457aed4c45bc900998b5e11c03023264208James Dong        m0 = pres[0];
132529a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = pres[12];
132629a84457aed4c45bc900998b5e11c03023264208James Dong        m0 += m3;
132729a84457aed4c45bc900998b5e11c03023264208James Dong        m3 = m0 - (m3 << 1);
132829a84457aed4c45bc900998b5e11c03023264208James Dong        m1 = pres[4];
132929a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = pres[8];
133029a84457aed4c45bc900998b5e11c03023264208James Dong        m1 += m2;
133129a84457aed4c45bc900998b5e11c03023264208James Dong        m2 = m1 - (m2 << 1);
133229a84457aed4c45bc900998b5e11c03023264208James Dong        pres[0] = m0 + m1;
133329a84457aed4c45bc900998b5e11c03023264208James Dong        pres[8] = m0 - m1;
133429a84457aed4c45bc900998b5e11c03023264208James Dong        pres[4] = m2 + m3;
133529a84457aed4c45bc900998b5e11c03023264208James Dong        pres[12] = m3 - m2;
133629a84457aed4c45bc900998b5e11c03023264208James Dong
133729a84457aed4c45bc900998b5e11c03023264208James Dong        pres++;
133829a84457aed4c45bc900998b5e11c03023264208James Dong        k--;
133929a84457aed4c45bc900998b5e11c03023264208James Dong
134029a84457aed4c45bc900998b5e11c03023264208James Dong    }
134129a84457aed4c45bc900998b5e11c03023264208James Dong
134229a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
134329a84457aed4c45bc900998b5e11c03023264208James Dong    k = 4;
134429a84457aed4c45bc900998b5e11c03023264208James Dong    while (k > 0)
134529a84457aed4c45bc900998b5e11c03023264208James Dong    {
134629a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
134729a84457aed4c45bc900998b5e11c03023264208James Dong        satd += ((tmp1 >= 0) ? tmp1 : -tmp1);
134829a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
134929a84457aed4c45bc900998b5e11c03023264208James Dong        satd += ((tmp1 >= 0) ? tmp1 : -tmp1);
135029a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
135129a84457aed4c45bc900998b5e11c03023264208James Dong        satd += ((tmp1 >= 0) ? tmp1 : -tmp1);
135229a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
135329a84457aed4c45bc900998b5e11c03023264208James Dong        satd += ((tmp1 >= 0) ? tmp1 : -tmp1);
135429a84457aed4c45bc900998b5e11c03023264208James Dong        k--;
135529a84457aed4c45bc900998b5e11c03023264208James Dong    }
135629a84457aed4c45bc900998b5e11c03023264208James Dong
135729a84457aed4c45bc900998b5e11c03023264208James Dong    satd = (satd + 1) >> 1;
135829a84457aed4c45bc900998b5e11c03023264208James Dong    *cost += satd;
135929a84457aed4c45bc900998b5e11c03023264208James Dong
136029a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
136129a84457aed4c45bc900998b5e11c03023264208James Dong}
136229a84457aed4c45bc900998b5e11c03023264208James Dong
136329a84457aed4c45bc900998b5e11c03023264208James Dongvoid chroma_intra_search(AVCEncObject *encvid)
136429a84457aed4c45bc900998b5e11c03023264208James Dong{
136529a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
136629a84457aed4c45bc900998b5e11c03023264208James Dong    AVCPictureData *currPic = video->currPic;
136729a84457aed4c45bc900998b5e11c03023264208James Dong
136829a84457aed4c45bc900998b5e11c03023264208James Dong    int x_pos = video->mb_x << 3;
136929a84457aed4c45bc900998b5e11c03023264208James Dong    int y_pos = video->mb_y << 3;
137029a84457aed4c45bc900998b5e11c03023264208James Dong    int pitch = currPic->pitch >> 1;
137129a84457aed4c45bc900998b5e11c03023264208James Dong    int offset = y_pos * pitch + x_pos;
137229a84457aed4c45bc900998b5e11c03023264208James Dong
137329a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *comp_ref_x, *comp_ref_y, *pred;
137429a84457aed4c45bc900998b5e11c03023264208James Dong    int  sum_x0, sum_x1, sum_y0, sum_y1;
137529a84457aed4c45bc900998b5e11c03023264208James Dong    int pred_0[2], pred_1[2], pred_2[2], pred_3[2];
137629a84457aed4c45bc900998b5e11c03023264208James Dong    uint32 pred_a, pred_b, pred_c, pred_d;
137729a84457aed4c45bc900998b5e11c03023264208James Dong    int i, j, component;
137829a84457aed4c45bc900998b5e11c03023264208James Dong    int a_16, b, c, factor_c, topleft;
137929a84457aed4c45bc900998b5e11c03023264208James Dong    int H, V, value;
138029a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *comp_ref_x0, *comp_ref_x1,  *comp_ref_y0, *comp_ref_y1;
138129a84457aed4c45bc900998b5e11c03023264208James Dong
138229a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *curCb = currPic->Scb + offset;
138329a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *curCr = currPic->Scr + offset;
138429a84457aed4c45bc900998b5e11c03023264208James Dong
138529a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *orgCb, *orgCr;
138629a84457aed4c45bc900998b5e11c03023264208James Dong    AVCFrameIO *currInput = encvid->currInput;
138729a84457aed4c45bc900998b5e11c03023264208James Dong    AVCMacroblock *currMB = video->currMB;
138829a84457aed4c45bc900998b5e11c03023264208James Dong    int org_pitch;
138929a84457aed4c45bc900998b5e11c03023264208James Dong    int cost, mincost;
139029a84457aed4c45bc900998b5e11c03023264208James Dong
139129a84457aed4c45bc900998b5e11c03023264208James Dong    /* evaluate DC mode */
139229a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailB & video->intraAvailA)
139329a84457aed4c45bc900998b5e11c03023264208James Dong    {
139429a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_x = curCb - pitch;
139529a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_y = curCb - 1;
139629a84457aed4c45bc900998b5e11c03023264208James Dong
139729a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 0; i < 2; i++)
139829a84457aed4c45bc900998b5e11c03023264208James Dong        {
139929a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a = *((uint32*)comp_ref_x);
140029a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_x += 4;
140129a84457aed4c45bc900998b5e11c03023264208James Dong            pred_b = (pred_a >> 8) & 0xFF00FF;
140229a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a &= 0xFF00FF;
140329a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += pred_b;
140429a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += (pred_a >> 16);
140529a84457aed4c45bc900998b5e11c03023264208James Dong            sum_x0 = pred_a & 0xFFFF;
140629a84457aed4c45bc900998b5e11c03023264208James Dong
140729a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a = *((uint32*)comp_ref_x);
140829a84457aed4c45bc900998b5e11c03023264208James Dong            pred_b = (pred_a >> 8) & 0xFF00FF;
140929a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a &= 0xFF00FF;
141029a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += pred_b;
141129a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += (pred_a >> 16);
141229a84457aed4c45bc900998b5e11c03023264208James Dong            sum_x1 = pred_a & 0xFFFF;
141329a84457aed4c45bc900998b5e11c03023264208James Dong
141429a84457aed4c45bc900998b5e11c03023264208James Dong            pred_1[i] = (sum_x1 + 2) >> 2;
141529a84457aed4c45bc900998b5e11c03023264208James Dong
141629a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 = *comp_ref_y;
141729a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 += *(comp_ref_y += pitch);
141829a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 += *(comp_ref_y += pitch);
141929a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 += *(comp_ref_y += pitch);
142029a84457aed4c45bc900998b5e11c03023264208James Dong
142129a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 = *(comp_ref_y += pitch);
142229a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 += *(comp_ref_y += pitch);
142329a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 += *(comp_ref_y += pitch);
142429a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 += *(comp_ref_y += pitch);
142529a84457aed4c45bc900998b5e11c03023264208James Dong
142629a84457aed4c45bc900998b5e11c03023264208James Dong            pred_2[i] = (sum_y1 + 2) >> 2;
142729a84457aed4c45bc900998b5e11c03023264208James Dong
142829a84457aed4c45bc900998b5e11c03023264208James Dong            pred_0[i] = (sum_y0 + sum_x0 + 4) >> 3;
142929a84457aed4c45bc900998b5e11c03023264208James Dong            pred_3[i] = (sum_y1 + sum_x1 + 4) >> 3;
143029a84457aed4c45bc900998b5e11c03023264208James Dong
143129a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_x = curCr - pitch;
143229a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_y = curCr - 1;
143329a84457aed4c45bc900998b5e11c03023264208James Dong        }
143429a84457aed4c45bc900998b5e11c03023264208James Dong    }
143529a84457aed4c45bc900998b5e11c03023264208James Dong
143629a84457aed4c45bc900998b5e11c03023264208James Dong    else if (video->intraAvailA)
143729a84457aed4c45bc900998b5e11c03023264208James Dong    {
143829a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_y = curCb - 1;
143929a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 0; i < 2; i++)
144029a84457aed4c45bc900998b5e11c03023264208James Dong        {
144129a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 = *comp_ref_y;
144229a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 += *(comp_ref_y += pitch);
144329a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 += *(comp_ref_y += pitch);
144429a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y0 += *(comp_ref_y += pitch);
144529a84457aed4c45bc900998b5e11c03023264208James Dong
144629a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 = *(comp_ref_y += pitch);
144729a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 += *(comp_ref_y += pitch);
144829a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 += *(comp_ref_y += pitch);
144929a84457aed4c45bc900998b5e11c03023264208James Dong            sum_y1 += *(comp_ref_y += pitch);
145029a84457aed4c45bc900998b5e11c03023264208James Dong
145129a84457aed4c45bc900998b5e11c03023264208James Dong            pred_0[i] = pred_1[i] = (sum_y0 + 2) >> 2;
145229a84457aed4c45bc900998b5e11c03023264208James Dong            pred_2[i] = pred_3[i] = (sum_y1 + 2) >> 2;
145329a84457aed4c45bc900998b5e11c03023264208James Dong
145429a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_y = curCr - 1;
145529a84457aed4c45bc900998b5e11c03023264208James Dong        }
145629a84457aed4c45bc900998b5e11c03023264208James Dong    }
145729a84457aed4c45bc900998b5e11c03023264208James Dong    else if (video->intraAvailB)
145829a84457aed4c45bc900998b5e11c03023264208James Dong    {
145929a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_x = curCb - pitch;
146029a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 0; i < 2; i++)
146129a84457aed4c45bc900998b5e11c03023264208James Dong        {
146229a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a = *((uint32*)comp_ref_x);
146329a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_x += 4;
146429a84457aed4c45bc900998b5e11c03023264208James Dong            pred_b = (pred_a >> 8) & 0xFF00FF;
146529a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a &= 0xFF00FF;
146629a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += pred_b;
146729a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += (pred_a >> 16);
146829a84457aed4c45bc900998b5e11c03023264208James Dong            sum_x0 = pred_a & 0xFFFF;
146929a84457aed4c45bc900998b5e11c03023264208James Dong
147029a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a = *((uint32*)comp_ref_x);
147129a84457aed4c45bc900998b5e11c03023264208James Dong            pred_b = (pred_a >> 8) & 0xFF00FF;
147229a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a &= 0xFF00FF;
147329a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += pred_b;
147429a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a += (pred_a >> 16);
147529a84457aed4c45bc900998b5e11c03023264208James Dong            sum_x1 = pred_a & 0xFFFF;
147629a84457aed4c45bc900998b5e11c03023264208James Dong
147729a84457aed4c45bc900998b5e11c03023264208James Dong            pred_0[i] = pred_2[i] = (sum_x0 + 2) >> 2;
147829a84457aed4c45bc900998b5e11c03023264208James Dong            pred_1[i] = pred_3[i] = (sum_x1 + 2) >> 2;
147929a84457aed4c45bc900998b5e11c03023264208James Dong
148029a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_x = curCr - pitch;
148129a84457aed4c45bc900998b5e11c03023264208James Dong        }
148229a84457aed4c45bc900998b5e11c03023264208James Dong    }
148329a84457aed4c45bc900998b5e11c03023264208James Dong    else
148429a84457aed4c45bc900998b5e11c03023264208James Dong    {
148529a84457aed4c45bc900998b5e11c03023264208James Dong        pred_0[0] = pred_0[1] = pred_1[0] = pred_1[1] =
148629a84457aed4c45bc900998b5e11c03023264208James Dong                                                pred_2[0] = pred_2[1] = pred_3[0] = pred_3[1] = 128;
148729a84457aed4c45bc900998b5e11c03023264208James Dong    }
148829a84457aed4c45bc900998b5e11c03023264208James Dong
148929a84457aed4c45bc900998b5e11c03023264208James Dong    pred = encvid->pred_ic[AVC_IC_DC];
149029a84457aed4c45bc900998b5e11c03023264208James Dong
149129a84457aed4c45bc900998b5e11c03023264208James Dong    pred_a = pred_0[0];
149229a84457aed4c45bc900998b5e11c03023264208James Dong    pred_b = pred_1[0];
149329a84457aed4c45bc900998b5e11c03023264208James Dong    pred_a |= (pred_a << 8);
149429a84457aed4c45bc900998b5e11c03023264208James Dong    pred_a |= (pred_a << 16);
149529a84457aed4c45bc900998b5e11c03023264208James Dong    pred_b |= (pred_b << 8);
149629a84457aed4c45bc900998b5e11c03023264208James Dong    pred_b |= (pred_b << 16);
149729a84457aed4c45bc900998b5e11c03023264208James Dong
149829a84457aed4c45bc900998b5e11c03023264208James Dong    pred_c = pred_0[1];
149929a84457aed4c45bc900998b5e11c03023264208James Dong    pred_d = pred_1[1];
150029a84457aed4c45bc900998b5e11c03023264208James Dong    pred_c |= (pred_c << 8);
150129a84457aed4c45bc900998b5e11c03023264208James Dong    pred_c |= (pred_c << 16);
150229a84457aed4c45bc900998b5e11c03023264208James Dong    pred_d |= (pred_d << 8);
150329a84457aed4c45bc900998b5e11c03023264208James Dong    pred_d |= (pred_d << 16);
150429a84457aed4c45bc900998b5e11c03023264208James Dong
150529a84457aed4c45bc900998b5e11c03023264208James Dong
150629a84457aed4c45bc900998b5e11c03023264208James Dong    for (j = 0; j < 4; j++) /* 4 lines */
150729a84457aed4c45bc900998b5e11c03023264208James Dong    {
150829a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) = pred_a;
150929a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 4)) = pred_b;
151029a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 8)) = pred_c;
151129a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 12)) = pred_d;
151229a84457aed4c45bc900998b5e11c03023264208James Dong        pred += 16; /* move to the next line */
151329a84457aed4c45bc900998b5e11c03023264208James Dong    }
151429a84457aed4c45bc900998b5e11c03023264208James Dong
151529a84457aed4c45bc900998b5e11c03023264208James Dong    pred_a = pred_2[0];
151629a84457aed4c45bc900998b5e11c03023264208James Dong    pred_b = pred_3[0];
151729a84457aed4c45bc900998b5e11c03023264208James Dong    pred_a |= (pred_a << 8);
151829a84457aed4c45bc900998b5e11c03023264208James Dong    pred_a |= (pred_a << 16);
151929a84457aed4c45bc900998b5e11c03023264208James Dong    pred_b |= (pred_b << 8);
152029a84457aed4c45bc900998b5e11c03023264208James Dong    pred_b |= (pred_b << 16);
152129a84457aed4c45bc900998b5e11c03023264208James Dong
152229a84457aed4c45bc900998b5e11c03023264208James Dong    pred_c = pred_2[1];
152329a84457aed4c45bc900998b5e11c03023264208James Dong    pred_d = pred_3[1];
152429a84457aed4c45bc900998b5e11c03023264208James Dong    pred_c |= (pred_c << 8);
152529a84457aed4c45bc900998b5e11c03023264208James Dong    pred_c |= (pred_c << 16);
152629a84457aed4c45bc900998b5e11c03023264208James Dong    pred_d |= (pred_d << 8);
152729a84457aed4c45bc900998b5e11c03023264208James Dong    pred_d |= (pred_d << 16);
152829a84457aed4c45bc900998b5e11c03023264208James Dong
152929a84457aed4c45bc900998b5e11c03023264208James Dong    for (j = 0; j < 4; j++) /* 4 lines */
153029a84457aed4c45bc900998b5e11c03023264208James Dong    {
153129a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)pred) = pred_a;
153229a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 4)) = pred_b;
153329a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 8)) = pred_c;
153429a84457aed4c45bc900998b5e11c03023264208James Dong        *((uint32*)(pred + 12)) = pred_d;
153529a84457aed4c45bc900998b5e11c03023264208James Dong        pred += 16; /* move to the next line */
153629a84457aed4c45bc900998b5e11c03023264208James Dong    }
153729a84457aed4c45bc900998b5e11c03023264208James Dong
153829a84457aed4c45bc900998b5e11c03023264208James Dong    /* predict horizontal mode */
153929a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA)
154029a84457aed4c45bc900998b5e11c03023264208James Dong    {
154129a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_y = curCb - 1;
154229a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_x = curCr - 1;
154329a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_ic[AVC_IC_Horizontal];
154429a84457aed4c45bc900998b5e11c03023264208James Dong
154529a84457aed4c45bc900998b5e11c03023264208James Dong        for (i = 4; i < 6; i++)
154629a84457aed4c45bc900998b5e11c03023264208James Dong        {
154729a84457aed4c45bc900998b5e11c03023264208James Dong            for (j = 0; j < 4; j++)
154829a84457aed4c45bc900998b5e11c03023264208James Dong            {
154929a84457aed4c45bc900998b5e11c03023264208James Dong                pred_a = *comp_ref_y;
155029a84457aed4c45bc900998b5e11c03023264208James Dong                comp_ref_y += pitch;
155129a84457aed4c45bc900998b5e11c03023264208James Dong                pred_a |= (pred_a << 8);
155229a84457aed4c45bc900998b5e11c03023264208James Dong                pred_a |= (pred_a << 16);
155329a84457aed4c45bc900998b5e11c03023264208James Dong                *((uint32*)pred) = pred_a;
155429a84457aed4c45bc900998b5e11c03023264208James Dong                *((uint32*)(pred + 4)) = pred_a;
155529a84457aed4c45bc900998b5e11c03023264208James Dong
155629a84457aed4c45bc900998b5e11c03023264208James Dong                pred_a = *comp_ref_x;
155729a84457aed4c45bc900998b5e11c03023264208James Dong                comp_ref_x += pitch;
155829a84457aed4c45bc900998b5e11c03023264208James Dong                pred_a |= (pred_a << 8);
155929a84457aed4c45bc900998b5e11c03023264208James Dong                pred_a |= (pred_a << 16);
156029a84457aed4c45bc900998b5e11c03023264208James Dong                *((uint32*)(pred + 8)) = pred_a;
156129a84457aed4c45bc900998b5e11c03023264208James Dong                *((uint32*)(pred + 12)) = pred_a;
156229a84457aed4c45bc900998b5e11c03023264208James Dong
156329a84457aed4c45bc900998b5e11c03023264208James Dong                pred += 16;
156429a84457aed4c45bc900998b5e11c03023264208James Dong            }
156529a84457aed4c45bc900998b5e11c03023264208James Dong        }
156629a84457aed4c45bc900998b5e11c03023264208James Dong    }
156729a84457aed4c45bc900998b5e11c03023264208James Dong
156829a84457aed4c45bc900998b5e11c03023264208James Dong    /* vertical mode */
156929a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailB)
157029a84457aed4c45bc900998b5e11c03023264208James Dong    {
157129a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_x = curCb - pitch;
157229a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_y = curCr - pitch;
157329a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_ic[AVC_IC_Vertical];
157429a84457aed4c45bc900998b5e11c03023264208James Dong
157529a84457aed4c45bc900998b5e11c03023264208James Dong        pred_a = *((uint32*)comp_ref_x);
157629a84457aed4c45bc900998b5e11c03023264208James Dong        pred_b = *((uint32*)(comp_ref_x + 4));
157729a84457aed4c45bc900998b5e11c03023264208James Dong        pred_c = *((uint32*)comp_ref_y);
157829a84457aed4c45bc900998b5e11c03023264208James Dong        pred_d = *((uint32*)(comp_ref_y + 4));
157929a84457aed4c45bc900998b5e11c03023264208James Dong
158029a84457aed4c45bc900998b5e11c03023264208James Dong        for (j = 0; j < 8; j++)
158129a84457aed4c45bc900998b5e11c03023264208James Dong        {
158229a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)pred) = pred_a;
158329a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 4)) = pred_b;
158429a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 8)) = pred_c;
158529a84457aed4c45bc900998b5e11c03023264208James Dong            *((uint32*)(pred + 12)) = pred_d;
158629a84457aed4c45bc900998b5e11c03023264208James Dong            pred += 16;
158729a84457aed4c45bc900998b5e11c03023264208James Dong        }
158829a84457aed4c45bc900998b5e11c03023264208James Dong    }
158929a84457aed4c45bc900998b5e11c03023264208James Dong
159029a84457aed4c45bc900998b5e11c03023264208James Dong    /* Intra_Chroma_Plane */
159129a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA && video->intraAvailB && video->intraAvailD)
159229a84457aed4c45bc900998b5e11c03023264208James Dong    {
159329a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_x = curCb - pitch;
159429a84457aed4c45bc900998b5e11c03023264208James Dong        comp_ref_y = curCb - 1;
159529a84457aed4c45bc900998b5e11c03023264208James Dong        topleft = curCb[-pitch-1];
159629a84457aed4c45bc900998b5e11c03023264208James Dong
159729a84457aed4c45bc900998b5e11c03023264208James Dong        pred = encvid->pred_ic[AVC_IC_Plane];
159829a84457aed4c45bc900998b5e11c03023264208James Dong        for (component = 0; component < 2; component++)
159929a84457aed4c45bc900998b5e11c03023264208James Dong        {
160029a84457aed4c45bc900998b5e11c03023264208James Dong            H = V = 0;
160129a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_x0 = comp_ref_x + 4;
160229a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_x1 = comp_ref_x + 2;
160329a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_y0 = comp_ref_y + (pitch << 2);
160429a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_y1 = comp_ref_y + (pitch << 1);
160529a84457aed4c45bc900998b5e11c03023264208James Dong            for (i = 1; i < 4; i++)
160629a84457aed4c45bc900998b5e11c03023264208James Dong            {
160729a84457aed4c45bc900998b5e11c03023264208James Dong                H += i * (*comp_ref_x0++ - *comp_ref_x1--);
160829a84457aed4c45bc900998b5e11c03023264208James Dong                V += i * (*comp_ref_y0 - *comp_ref_y1);
160929a84457aed4c45bc900998b5e11c03023264208James Dong                comp_ref_y0 += pitch;
161029a84457aed4c45bc900998b5e11c03023264208James Dong                comp_ref_y1 -= pitch;
161129a84457aed4c45bc900998b5e11c03023264208James Dong            }
161229a84457aed4c45bc900998b5e11c03023264208James Dong            H += i * (*comp_ref_x0++ - topleft);
161329a84457aed4c45bc900998b5e11c03023264208James Dong            V += i * (*comp_ref_y0 - *comp_ref_y1);
161429a84457aed4c45bc900998b5e11c03023264208James Dong
161529a84457aed4c45bc900998b5e11c03023264208James Dong            a_16 = ((*(comp_ref_x + 7) + *(comp_ref_y + 7 * pitch)) << 4) + 16;
161629a84457aed4c45bc900998b5e11c03023264208James Dong            b = (17 * H + 16) >> 5;
161729a84457aed4c45bc900998b5e11c03023264208James Dong            c = (17 * V + 16) >> 5;
161829a84457aed4c45bc900998b5e11c03023264208James Dong
161929a84457aed4c45bc900998b5e11c03023264208James Dong            pred_a = 0;
162029a84457aed4c45bc900998b5e11c03023264208James Dong            for (i = 4; i < 6; i++)
162129a84457aed4c45bc900998b5e11c03023264208James Dong            {
162229a84457aed4c45bc900998b5e11c03023264208James Dong                for (j = 0; j < 4; j++)
162329a84457aed4c45bc900998b5e11c03023264208James Dong                {
162429a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c = a_16 + c * (pred_a++ - 3);
162529a84457aed4c45bc900998b5e11c03023264208James Dong
162629a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c -= 3 * b;
162729a84457aed4c45bc900998b5e11c03023264208James Dong
162829a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
162929a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
163029a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
163129a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b = value;
163229a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
163329a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
163429a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
163529a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b |= (value << 8);
163629a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
163729a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
163829a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
163929a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b |= (value << 16);
164029a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
164129a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
164229a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
164329a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b |= (value << 24);
164429a84457aed4c45bc900998b5e11c03023264208James Dong                    *((uint32*)pred) = pred_b;
164529a84457aed4c45bc900998b5e11c03023264208James Dong
164629a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
164729a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
164829a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
164929a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b = value;
165029a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
165129a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
165229a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
165329a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b |= (value << 8);
165429a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
165529a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
165629a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
165729a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b |= (value << 16);
165829a84457aed4c45bc900998b5e11c03023264208James Dong                    value = factor_c >> 5;
165929a84457aed4c45bc900998b5e11c03023264208James Dong                    factor_c += b;
166029a84457aed4c45bc900998b5e11c03023264208James Dong                    CLIP_RESULT(value)
166129a84457aed4c45bc900998b5e11c03023264208James Dong                    pred_b |= (value << 24);
166229a84457aed4c45bc900998b5e11c03023264208James Dong                    *((uint32*)(pred + 4)) = pred_b;
166329a84457aed4c45bc900998b5e11c03023264208James Dong                    pred += 16;
166429a84457aed4c45bc900998b5e11c03023264208James Dong                }
166529a84457aed4c45bc900998b5e11c03023264208James Dong            }
166629a84457aed4c45bc900998b5e11c03023264208James Dong
166729a84457aed4c45bc900998b5e11c03023264208James Dong            pred -= 120; /* point to cr */
166829a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_x = curCr - pitch;
166929a84457aed4c45bc900998b5e11c03023264208James Dong            comp_ref_y = curCr - 1;
167029a84457aed4c45bc900998b5e11c03023264208James Dong            topleft = curCr[-pitch-1];
167129a84457aed4c45bc900998b5e11c03023264208James Dong        }
167229a84457aed4c45bc900998b5e11c03023264208James Dong    }
167329a84457aed4c45bc900998b5e11c03023264208James Dong
167429a84457aed4c45bc900998b5e11c03023264208James Dong    /* now evaluate it */
167529a84457aed4c45bc900998b5e11c03023264208James Dong
167629a84457aed4c45bc900998b5e11c03023264208James Dong    org_pitch = (currInput->pitch) >> 1;
167729a84457aed4c45bc900998b5e11c03023264208James Dong    offset = x_pos + y_pos * org_pitch;
167829a84457aed4c45bc900998b5e11c03023264208James Dong
167929a84457aed4c45bc900998b5e11c03023264208James Dong    orgCb = currInput->YCbCr[1] + offset;
168029a84457aed4c45bc900998b5e11c03023264208James Dong    orgCr = currInput->YCbCr[2] + offset;
168129a84457aed4c45bc900998b5e11c03023264208James Dong
168229a84457aed4c45bc900998b5e11c03023264208James Dong    mincost = 0x7fffffff;
168329a84457aed4c45bc900998b5e11c03023264208James Dong    cost = SATDChroma(orgCb, orgCr, org_pitch, encvid->pred_ic[AVC_IC_DC], mincost);
168429a84457aed4c45bc900998b5e11c03023264208James Dong    if (cost < mincost)
168529a84457aed4c45bc900998b5e11c03023264208James Dong    {
168629a84457aed4c45bc900998b5e11c03023264208James Dong        mincost = cost;
168729a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->intra_chroma_pred_mode = AVC_IC_DC;
168829a84457aed4c45bc900998b5e11c03023264208James Dong    }
168929a84457aed4c45bc900998b5e11c03023264208James Dong
169029a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA)
169129a84457aed4c45bc900998b5e11c03023264208James Dong    {
169229a84457aed4c45bc900998b5e11c03023264208James Dong        cost = SATDChroma(orgCb, orgCr, org_pitch, encvid->pred_ic[AVC_IC_Horizontal], mincost);
169329a84457aed4c45bc900998b5e11c03023264208James Dong        if (cost < mincost)
169429a84457aed4c45bc900998b5e11c03023264208James Dong        {
169529a84457aed4c45bc900998b5e11c03023264208James Dong            mincost = cost;
169629a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->intra_chroma_pred_mode = AVC_IC_Horizontal;
169729a84457aed4c45bc900998b5e11c03023264208James Dong        }
169829a84457aed4c45bc900998b5e11c03023264208James Dong    }
169929a84457aed4c45bc900998b5e11c03023264208James Dong
170029a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailB)
170129a84457aed4c45bc900998b5e11c03023264208James Dong    {
170229a84457aed4c45bc900998b5e11c03023264208James Dong        cost = SATDChroma(orgCb, orgCr, org_pitch, encvid->pred_ic[AVC_IC_Vertical], mincost);
170329a84457aed4c45bc900998b5e11c03023264208James Dong        if (cost < mincost)
170429a84457aed4c45bc900998b5e11c03023264208James Dong        {
170529a84457aed4c45bc900998b5e11c03023264208James Dong            mincost = cost;
170629a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->intra_chroma_pred_mode = AVC_IC_Vertical;
170729a84457aed4c45bc900998b5e11c03023264208James Dong        }
170829a84457aed4c45bc900998b5e11c03023264208James Dong    }
170929a84457aed4c45bc900998b5e11c03023264208James Dong
171029a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA && video->intraAvailB && video->intraAvailD)
171129a84457aed4c45bc900998b5e11c03023264208James Dong    {
171229a84457aed4c45bc900998b5e11c03023264208James Dong        cost = SATDChroma(orgCb, orgCr, org_pitch, encvid->pred_ic[AVC_IC_Plane], mincost);
171329a84457aed4c45bc900998b5e11c03023264208James Dong        if (cost < mincost)
171429a84457aed4c45bc900998b5e11c03023264208James Dong        {
171529a84457aed4c45bc900998b5e11c03023264208James Dong            mincost = cost;
171629a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->intra_chroma_pred_mode = AVC_IC_Plane;
171729a84457aed4c45bc900998b5e11c03023264208James Dong        }
171829a84457aed4c45bc900998b5e11c03023264208James Dong    }
171929a84457aed4c45bc900998b5e11c03023264208James Dong
172029a84457aed4c45bc900998b5e11c03023264208James Dong
172129a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
172229a84457aed4c45bc900998b5e11c03023264208James Dong}
172329a84457aed4c45bc900998b5e11c03023264208James Dong
172429a84457aed4c45bc900998b5e11c03023264208James Dong
172529a84457aed4c45bc900998b5e11c03023264208James Dongint SATDChroma(uint8 *orgCb, uint8 *orgCr, int org_pitch, uint8 *pred, int min_cost)
172629a84457aed4c45bc900998b5e11c03023264208James Dong{
172729a84457aed4c45bc900998b5e11c03023264208James Dong    int cost;
172829a84457aed4c45bc900998b5e11c03023264208James Dong    /* first take difference between orgCb, orgCr and pred */
172929a84457aed4c45bc900998b5e11c03023264208James Dong    int16 res[128], *pres; // residue
173029a84457aed4c45bc900998b5e11c03023264208James Dong    int m0, m1, m2, m3, tmp1;
173129a84457aed4c45bc900998b5e11c03023264208James Dong    int j, k;
173229a84457aed4c45bc900998b5e11c03023264208James Dong
173329a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
173429a84457aed4c45bc900998b5e11c03023264208James Dong    org_pitch -= 8;
173529a84457aed4c45bc900998b5e11c03023264208James Dong    // horizontal transform
173629a84457aed4c45bc900998b5e11c03023264208James Dong    for (j = 0; j < 8; j++)
173729a84457aed4c45bc900998b5e11c03023264208James Dong    {
173829a84457aed4c45bc900998b5e11c03023264208James Dong        k = 2;
173929a84457aed4c45bc900998b5e11c03023264208James Dong        while (k > 0)
174029a84457aed4c45bc900998b5e11c03023264208James Dong        {
174129a84457aed4c45bc900998b5e11c03023264208James Dong            m0 = orgCb[0] - pred[0];
174229a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = orgCb[3] - pred[3];
174329a84457aed4c45bc900998b5e11c03023264208James Dong            m0 += m3;
174429a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = m0 - (m3 << 1);
174529a84457aed4c45bc900998b5e11c03023264208James Dong            m1 = orgCb[1] - pred[1];
174629a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = orgCb[2] - pred[2];
174729a84457aed4c45bc900998b5e11c03023264208James Dong            m1 += m2;
174829a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = m1 - (m2 << 1);
174929a84457aed4c45bc900998b5e11c03023264208James Dong            pres[0] = m0 + m1;
175029a84457aed4c45bc900998b5e11c03023264208James Dong            pres[2] = m0 - m1;
175129a84457aed4c45bc900998b5e11c03023264208James Dong            pres[1] = m2 + m3;
175229a84457aed4c45bc900998b5e11c03023264208James Dong            pres[3] = m3 - m2;
175329a84457aed4c45bc900998b5e11c03023264208James Dong
175429a84457aed4c45bc900998b5e11c03023264208James Dong            orgCb += 4;
175529a84457aed4c45bc900998b5e11c03023264208James Dong            pres += 4;
175629a84457aed4c45bc900998b5e11c03023264208James Dong            pred += 4;
175729a84457aed4c45bc900998b5e11c03023264208James Dong            k--;
175829a84457aed4c45bc900998b5e11c03023264208James Dong        }
175929a84457aed4c45bc900998b5e11c03023264208James Dong        orgCb += org_pitch;
176029a84457aed4c45bc900998b5e11c03023264208James Dong        k = 2;
176129a84457aed4c45bc900998b5e11c03023264208James Dong        while (k > 0)
176229a84457aed4c45bc900998b5e11c03023264208James Dong        {
176329a84457aed4c45bc900998b5e11c03023264208James Dong            m0 = orgCr[0] - pred[0];
176429a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = orgCr[3] - pred[3];
176529a84457aed4c45bc900998b5e11c03023264208James Dong            m0 += m3;
176629a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = m0 - (m3 << 1);
176729a84457aed4c45bc900998b5e11c03023264208James Dong            m1 = orgCr[1] - pred[1];
176829a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = orgCr[2] - pred[2];
176929a84457aed4c45bc900998b5e11c03023264208James Dong            m1 += m2;
177029a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = m1 - (m2 << 1);
177129a84457aed4c45bc900998b5e11c03023264208James Dong            pres[0] = m0 + m1;
177229a84457aed4c45bc900998b5e11c03023264208James Dong            pres[2] = m0 - m1;
177329a84457aed4c45bc900998b5e11c03023264208James Dong            pres[1] = m2 + m3;
177429a84457aed4c45bc900998b5e11c03023264208James Dong            pres[3] = m3 - m2;
177529a84457aed4c45bc900998b5e11c03023264208James Dong
177629a84457aed4c45bc900998b5e11c03023264208James Dong            orgCr += 4;
177729a84457aed4c45bc900998b5e11c03023264208James Dong            pres += 4;
177829a84457aed4c45bc900998b5e11c03023264208James Dong            pred += 4;
177929a84457aed4c45bc900998b5e11c03023264208James Dong            k--;
178029a84457aed4c45bc900998b5e11c03023264208James Dong        }
178129a84457aed4c45bc900998b5e11c03023264208James Dong        orgCr += org_pitch;
178229a84457aed4c45bc900998b5e11c03023264208James Dong    }
178329a84457aed4c45bc900998b5e11c03023264208James Dong
178429a84457aed4c45bc900998b5e11c03023264208James Dong    /* vertical transform */
178529a84457aed4c45bc900998b5e11c03023264208James Dong    for (j = 0; j < 2; j++)
178629a84457aed4c45bc900998b5e11c03023264208James Dong    {
178729a84457aed4c45bc900998b5e11c03023264208James Dong        pres = res + (j << 6);
178829a84457aed4c45bc900998b5e11c03023264208James Dong        k = 16;
178929a84457aed4c45bc900998b5e11c03023264208James Dong        while (k > 0)
179029a84457aed4c45bc900998b5e11c03023264208James Dong        {
179129a84457aed4c45bc900998b5e11c03023264208James Dong            m0 = pres[0];
179229a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = pres[3<<4];
179329a84457aed4c45bc900998b5e11c03023264208James Dong            m0 += m3;
179429a84457aed4c45bc900998b5e11c03023264208James Dong            m3 = m0 - (m3 << 1);
179529a84457aed4c45bc900998b5e11c03023264208James Dong            m1 = pres[1<<4];
179629a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = pres[2<<4];
179729a84457aed4c45bc900998b5e11c03023264208James Dong            m1 += m2;
179829a84457aed4c45bc900998b5e11c03023264208James Dong            m2 = m1 - (m2 << 1);
179929a84457aed4c45bc900998b5e11c03023264208James Dong            pres[0] = m0 + m1;
180029a84457aed4c45bc900998b5e11c03023264208James Dong            pres[2<<4] = m0 - m1;
180129a84457aed4c45bc900998b5e11c03023264208James Dong            pres[1<<4] = m2 + m3;
180229a84457aed4c45bc900998b5e11c03023264208James Dong            pres[3<<4] = m3 - m2;
180329a84457aed4c45bc900998b5e11c03023264208James Dong
180429a84457aed4c45bc900998b5e11c03023264208James Dong            pres++;
180529a84457aed4c45bc900998b5e11c03023264208James Dong            k--;
180629a84457aed4c45bc900998b5e11c03023264208James Dong        }
180729a84457aed4c45bc900998b5e11c03023264208James Dong    }
180829a84457aed4c45bc900998b5e11c03023264208James Dong
180929a84457aed4c45bc900998b5e11c03023264208James Dong    /* now sum of absolute value */
181029a84457aed4c45bc900998b5e11c03023264208James Dong    pres = res;
181129a84457aed4c45bc900998b5e11c03023264208James Dong    cost = 0;
181229a84457aed4c45bc900998b5e11c03023264208James Dong    k = 128;
181329a84457aed4c45bc900998b5e11c03023264208James Dong    while (k > 0)
181429a84457aed4c45bc900998b5e11c03023264208James Dong    {
181529a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
181629a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
181729a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
181829a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
181929a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
182029a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
182129a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
182229a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
182329a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
182429a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
182529a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
182629a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
182729a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
182829a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
182929a84457aed4c45bc900998b5e11c03023264208James Dong        tmp1 = *pres++;
183029a84457aed4c45bc900998b5e11c03023264208James Dong        cost += ((tmp1 >= 0) ? tmp1 : -tmp1);
183129a84457aed4c45bc900998b5e11c03023264208James Dong        k -= 8;
183229a84457aed4c45bc900998b5e11c03023264208James Dong        if (cost > min_cost) /* early drop out */
183329a84457aed4c45bc900998b5e11c03023264208James Dong        {
183429a84457aed4c45bc900998b5e11c03023264208James Dong            return cost;
183529a84457aed4c45bc900998b5e11c03023264208James Dong        }
183629a84457aed4c45bc900998b5e11c03023264208James Dong    }
183729a84457aed4c45bc900998b5e11c03023264208James Dong
183829a84457aed4c45bc900998b5e11c03023264208James Dong    return cost;
183929a84457aed4c45bc900998b5e11c03023264208James Dong}
184029a84457aed4c45bc900998b5e11c03023264208James Dong
184129a84457aed4c45bc900998b5e11c03023264208James Dong
184229a84457aed4c45bc900998b5e11c03023264208James Dong
184329a84457aed4c45bc900998b5e11c03023264208James Dong///////////////////////////////// old code, unused
184429a84457aed4c45bc900998b5e11c03023264208James Dong/* find the best intra mode based on original (unencoded) frame */
184529a84457aed4c45bc900998b5e11c03023264208James Dong/* output is
184629a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->mb_intra, currMB->mbMode,
184729a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->i16Mode  (if currMB->mbMode == AVC_I16)
184829a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->i4Mode[..] (if currMB->mbMode == AVC_I4) */
184929a84457aed4c45bc900998b5e11c03023264208James Dong
185029a84457aed4c45bc900998b5e11c03023264208James Dong#ifdef FIXED_INTRAPRED_MODE
185129a84457aed4c45bc900998b5e11c03023264208James Dongvoid MBIntraSearch(AVCEncObject *encvid, AVCMacroblock *currMB, int mbNum)
185229a84457aed4c45bc900998b5e11c03023264208James Dong{
185329a84457aed4c45bc900998b5e11c03023264208James Dong    (void)(mbNum);
185429a84457aed4c45bc900998b5e11c03023264208James Dong
185529a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
185629a84457aed4c45bc900998b5e11c03023264208James Dong    int indx, block_x, block_y;
185729a84457aed4c45bc900998b5e11c03023264208James Dong
185829a84457aed4c45bc900998b5e11c03023264208James Dong    video->intraAvailA = video->intraAvailB = video->intraAvailC = video->intraAvailD = 0;
185929a84457aed4c45bc900998b5e11c03023264208James Dong
186029a84457aed4c45bc900998b5e11c03023264208James Dong    if (!video->currPicParams->constrained_intra_pred_flag)
186129a84457aed4c45bc900998b5e11c03023264208James Dong    {
186229a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailA = video->mbAvailA;
186329a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailB = video->mbAvailB;
186429a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailC = video->mbAvailC;
186529a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailD = video->mbAvailD;
186629a84457aed4c45bc900998b5e11c03023264208James Dong    }
186729a84457aed4c45bc900998b5e11c03023264208James Dong    else
186829a84457aed4c45bc900998b5e11c03023264208James Dong    {
186929a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailA)
187029a84457aed4c45bc900998b5e11c03023264208James Dong        {
187129a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailA = video->mblock[video->mbAddrA].mb_intra;
187229a84457aed4c45bc900998b5e11c03023264208James Dong        }
187329a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailB)
187429a84457aed4c45bc900998b5e11c03023264208James Dong        {
187529a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailB = video->mblock[video->mbAddrB].mb_intra ;
187629a84457aed4c45bc900998b5e11c03023264208James Dong        }
187729a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailC)
187829a84457aed4c45bc900998b5e11c03023264208James Dong        {
187929a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailC = video->mblock[video->mbAddrC].mb_intra;
188029a84457aed4c45bc900998b5e11c03023264208James Dong        }
188129a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailD)
188229a84457aed4c45bc900998b5e11c03023264208James Dong        {
188329a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailD = video->mblock[video->mbAddrD].mb_intra;
188429a84457aed4c45bc900998b5e11c03023264208James Dong        }
188529a84457aed4c45bc900998b5e11c03023264208James Dong    }
188629a84457aed4c45bc900998b5e11c03023264208James Dong
188729a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->mb_intra = TRUE;
188829a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->mbMode = FIXED_INTRAPRED_MODE;
188929a84457aed4c45bc900998b5e11c03023264208James Dong
189029a84457aed4c45bc900998b5e11c03023264208James Dong    if (currMB->mbMode == AVC_I16)
189129a84457aed4c45bc900998b5e11c03023264208James Dong    {
189229a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->i16Mode = FIXED_I16_MODE;
189329a84457aed4c45bc900998b5e11c03023264208James Dong
189429a84457aed4c45bc900998b5e11c03023264208James Dong        if (FIXED_I16_MODE == AVC_I16_Vertical && !video->intraAvailB)
189529a84457aed4c45bc900998b5e11c03023264208James Dong        {
189629a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_DC;
189729a84457aed4c45bc900998b5e11c03023264208James Dong        }
189829a84457aed4c45bc900998b5e11c03023264208James Dong
189929a84457aed4c45bc900998b5e11c03023264208James Dong        if (FIXED_I16_MODE == AVC_I16_Horizontal && !video->intraAvailA)
190029a84457aed4c45bc900998b5e11c03023264208James Dong        {
190129a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_DC;
190229a84457aed4c45bc900998b5e11c03023264208James Dong        }
190329a84457aed4c45bc900998b5e11c03023264208James Dong
190429a84457aed4c45bc900998b5e11c03023264208James Dong        if (FIXED_I16_MODE == AVC_I16_Plane && !(video->intraAvailA && video->intraAvailB && video->intraAvailD))
190529a84457aed4c45bc900998b5e11c03023264208James Dong        {
190629a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_DC;
190729a84457aed4c45bc900998b5e11c03023264208James Dong        }
190829a84457aed4c45bc900998b5e11c03023264208James Dong    }
190929a84457aed4c45bc900998b5e11c03023264208James Dong    else //if(currMB->mbMode == AVC_I4)
191029a84457aed4c45bc900998b5e11c03023264208James Dong    {
191129a84457aed4c45bc900998b5e11c03023264208James Dong        for (indx = 0; indx < 16; indx++)
191229a84457aed4c45bc900998b5e11c03023264208James Dong        {
191329a84457aed4c45bc900998b5e11c03023264208James Dong            block_x = blkIdx2blkX[indx];
191429a84457aed4c45bc900998b5e11c03023264208James Dong            block_y = blkIdx2blkY[indx];
191529a84457aed4c45bc900998b5e11c03023264208James Dong
191629a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i4Mode[(block_y<<2)+block_x] = FIXED_I4_MODE;
191729a84457aed4c45bc900998b5e11c03023264208James Dong
191829a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Vertical && !(block_y > 0 || video->intraAvailB))
191929a84457aed4c45bc900998b5e11c03023264208James Dong            {
192029a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
192129a84457aed4c45bc900998b5e11c03023264208James Dong            }
192229a84457aed4c45bc900998b5e11c03023264208James Dong
192329a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Horizontal && !(block_x || video->intraAvailA))
192429a84457aed4c45bc900998b5e11c03023264208James Dong            {
192529a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
192629a84457aed4c45bc900998b5e11c03023264208James Dong            }
192729a84457aed4c45bc900998b5e11c03023264208James Dong
192829a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Diagonal_Down_Left &&
192929a84457aed4c45bc900998b5e11c03023264208James Dong                    (block_y == 0 && !video->intraAvailB))
193029a84457aed4c45bc900998b5e11c03023264208James Dong            {
193129a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
193229a84457aed4c45bc900998b5e11c03023264208James Dong            }
193329a84457aed4c45bc900998b5e11c03023264208James Dong
193429a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Diagonal_Down_Right &&
193529a84457aed4c45bc900998b5e11c03023264208James Dong                    !((block_y && block_x)
193629a84457aed4c45bc900998b5e11c03023264208James Dong                      || (block_y && video->intraAvailA)
193729a84457aed4c45bc900998b5e11c03023264208James Dong                      || (block_x && video->intraAvailB)
193829a84457aed4c45bc900998b5e11c03023264208James Dong                      || (video->intraAvailA && video->intraAvailD && video->intraAvailB)))
193929a84457aed4c45bc900998b5e11c03023264208James Dong            {
194029a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
194129a84457aed4c45bc900998b5e11c03023264208James Dong            }
194229a84457aed4c45bc900998b5e11c03023264208James Dong
194329a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Vertical_Right &&
194429a84457aed4c45bc900998b5e11c03023264208James Dong                    !((block_y && block_x)
194529a84457aed4c45bc900998b5e11c03023264208James Dong                      || (block_y && video->intraAvailA)
194629a84457aed4c45bc900998b5e11c03023264208James Dong                      || (block_x && video->intraAvailB)
194729a84457aed4c45bc900998b5e11c03023264208James Dong                      || (video->intraAvailA && video->intraAvailD && video->intraAvailB)))
194829a84457aed4c45bc900998b5e11c03023264208James Dong            {
194929a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
195029a84457aed4c45bc900998b5e11c03023264208James Dong            }
195129a84457aed4c45bc900998b5e11c03023264208James Dong
195229a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Horizontal_Down &&
195329a84457aed4c45bc900998b5e11c03023264208James Dong                    !((block_y && block_x)
195429a84457aed4c45bc900998b5e11c03023264208James Dong                      || (block_y && video->intraAvailA)
195529a84457aed4c45bc900998b5e11c03023264208James Dong                      || (block_x && video->intraAvailB)
195629a84457aed4c45bc900998b5e11c03023264208James Dong                      || (video->intraAvailA && video->intraAvailD && video->intraAvailB)))
195729a84457aed4c45bc900998b5e11c03023264208James Dong            {
195829a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
195929a84457aed4c45bc900998b5e11c03023264208James Dong            }
196029a84457aed4c45bc900998b5e11c03023264208James Dong
196129a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Vertical_Left &&
196229a84457aed4c45bc900998b5e11c03023264208James Dong                    (block_y == 0 && !video->intraAvailB))
196329a84457aed4c45bc900998b5e11c03023264208James Dong            {
196429a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
196529a84457aed4c45bc900998b5e11c03023264208James Dong            }
196629a84457aed4c45bc900998b5e11c03023264208James Dong
196729a84457aed4c45bc900998b5e11c03023264208James Dong            if (FIXED_I4_MODE == AVC_I4_Horizontal_Up && !(block_x || video->intraAvailA))
196829a84457aed4c45bc900998b5e11c03023264208James Dong            {
196929a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->i4Mode[(block_y<<2)+block_x] = AVC_I4_DC;
197029a84457aed4c45bc900998b5e11c03023264208James Dong            }
197129a84457aed4c45bc900998b5e11c03023264208James Dong        }
197229a84457aed4c45bc900998b5e11c03023264208James Dong    }
197329a84457aed4c45bc900998b5e11c03023264208James Dong
197429a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->intra_chroma_pred_mode = FIXED_INTRA_CHROMA_MODE;
197529a84457aed4c45bc900998b5e11c03023264208James Dong
197629a84457aed4c45bc900998b5e11c03023264208James Dong    if (FIXED_INTRA_CHROMA_MODE == AVC_IC_Horizontal && !(video->intraAvailA))
197729a84457aed4c45bc900998b5e11c03023264208James Dong    {
197829a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->intra_chroma_pred_mode = AVC_IC_DC;
197929a84457aed4c45bc900998b5e11c03023264208James Dong    }
198029a84457aed4c45bc900998b5e11c03023264208James Dong
198129a84457aed4c45bc900998b5e11c03023264208James Dong    if (FIXED_INTRA_CHROMA_MODE == AVC_IC_Vertical && !(video->intraAvailB))
198229a84457aed4c45bc900998b5e11c03023264208James Dong    {
198329a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->intra_chroma_pred_mode = AVC_IC_DC;
198429a84457aed4c45bc900998b5e11c03023264208James Dong    }
198529a84457aed4c45bc900998b5e11c03023264208James Dong
198629a84457aed4c45bc900998b5e11c03023264208James Dong    if (FIXED_INTRA_CHROMA_MODE == AVC_IC_Plane && !(video->intraAvailA && video->intraAvailB && video->intraAvailD))
198729a84457aed4c45bc900998b5e11c03023264208James Dong    {
198829a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->intra_chroma_pred_mode = AVC_IC_DC;
198929a84457aed4c45bc900998b5e11c03023264208James Dong    }
199029a84457aed4c45bc900998b5e11c03023264208James Dong
199129a84457aed4c45bc900998b5e11c03023264208James Dong    /* also reset the motion vectors */
199229a84457aed4c45bc900998b5e11c03023264208James Dong    /* set MV and Ref_Idx codes of Intra blocks in P-slices */
199329a84457aed4c45bc900998b5e11c03023264208James Dong    memset(currMB->mvL0, 0, sizeof(int32)*16);
199429a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->ref_idx_L0[0] = -1;
199529a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->ref_idx_L0[1] = -1;
199629a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->ref_idx_L0[2] = -1;
199729a84457aed4c45bc900998b5e11c03023264208James Dong    currMB->ref_idx_L0[3] = -1;
199829a84457aed4c45bc900998b5e11c03023264208James Dong
199929a84457aed4c45bc900998b5e11c03023264208James Dong    // output from this function, currMB->mbMode should be set to either
200029a84457aed4c45bc900998b5e11c03023264208James Dong    // AVC_I4, AVC_I16, or else in AVCMBMode enum, mbType, mb_intra, intra_chroma_pred_mode */
200129a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
200229a84457aed4c45bc900998b5e11c03023264208James Dong}
200329a84457aed4c45bc900998b5e11c03023264208James Dong#else // faster combined prediction+SAD calculation
200429a84457aed4c45bc900998b5e11c03023264208James Dongvoid MBIntraSearch(AVCEncObject *encvid, AVCMacroblock *currMB, int mbNum)
200529a84457aed4c45bc900998b5e11c03023264208James Dong{
200629a84457aed4c45bc900998b5e11c03023264208James Dong    AVCCommonObj *video = encvid->common;
200729a84457aed4c45bc900998b5e11c03023264208James Dong    AVCFrameIO *currInput = encvid->currInput;
200829a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *curL, *curCb, *curCr;
200929a84457aed4c45bc900998b5e11c03023264208James Dong    uint8 *comp, *pred_block;
201029a84457aed4c45bc900998b5e11c03023264208James Dong    int block_x, block_y, offset;
201129a84457aed4c45bc900998b5e11c03023264208James Dong    uint sad, sad4, sadI4, sadI16;
201229a84457aed4c45bc900998b5e11c03023264208James Dong    int component, SubBlock_indx, temp;
201329a84457aed4c45bc900998b5e11c03023264208James Dong    int pitch = video->currPic->pitch;
201429a84457aed4c45bc900998b5e11c03023264208James Dong
201529a84457aed4c45bc900998b5e11c03023264208James Dong    /* calculate the cost of each intra prediction mode  and compare to the
201629a84457aed4c45bc900998b5e11c03023264208James Dong    inter mode */
201729a84457aed4c45bc900998b5e11c03023264208James Dong    /* full search for all intra prediction */
201829a84457aed4c45bc900998b5e11c03023264208James Dong    offset = (video->mb_y << 4) * pitch + (video->mb_x << 4);
201929a84457aed4c45bc900998b5e11c03023264208James Dong    curL = currInput->YCbCr[0] + offset;
202029a84457aed4c45bc900998b5e11c03023264208James Dong    pred_block = video->pred_block + 84;
202129a84457aed4c45bc900998b5e11c03023264208James Dong
202229a84457aed4c45bc900998b5e11c03023264208James Dong    /* Assuming that InitNeighborAvailability has been called prior to this function */
202329a84457aed4c45bc900998b5e11c03023264208James Dong    video->intraAvailA = video->intraAvailB = video->intraAvailC = video->intraAvailD = 0;
202429a84457aed4c45bc900998b5e11c03023264208James Dong
202529a84457aed4c45bc900998b5e11c03023264208James Dong    if (!video->currPicParams->constrained_intra_pred_flag)
202629a84457aed4c45bc900998b5e11c03023264208James Dong    {
202729a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailA = video->mbAvailA;
202829a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailB = video->mbAvailB;
202929a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailC = video->mbAvailC;
203029a84457aed4c45bc900998b5e11c03023264208James Dong        video->intraAvailD = video->mbAvailD;
203129a84457aed4c45bc900998b5e11c03023264208James Dong    }
203229a84457aed4c45bc900998b5e11c03023264208James Dong    else
203329a84457aed4c45bc900998b5e11c03023264208James Dong    {
203429a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailA)
203529a84457aed4c45bc900998b5e11c03023264208James Dong        {
203629a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailA = video->mblock[video->mbAddrA].mb_intra;
203729a84457aed4c45bc900998b5e11c03023264208James Dong        }
203829a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailB)
203929a84457aed4c45bc900998b5e11c03023264208James Dong        {
204029a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailB = video->mblock[video->mbAddrB].mb_intra ;
204129a84457aed4c45bc900998b5e11c03023264208James Dong        }
204229a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailC)
204329a84457aed4c45bc900998b5e11c03023264208James Dong        {
204429a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailC = video->mblock[video->mbAddrC].mb_intra;
204529a84457aed4c45bc900998b5e11c03023264208James Dong        }
204629a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mbAvailD)
204729a84457aed4c45bc900998b5e11c03023264208James Dong        {
204829a84457aed4c45bc900998b5e11c03023264208James Dong            video->intraAvailD = video->mblock[video->mbAddrD].mb_intra;
204929a84457aed4c45bc900998b5e11c03023264208James Dong        }
205029a84457aed4c45bc900998b5e11c03023264208James Dong    }
205129a84457aed4c45bc900998b5e11c03023264208James Dong
205229a84457aed4c45bc900998b5e11c03023264208James Dong    /* currently we're doing exhaustive search. Smart search will be used later */
205329a84457aed4c45bc900998b5e11c03023264208James Dong
205429a84457aed4c45bc900998b5e11c03023264208James Dong    /* I16 modes */
205529a84457aed4c45bc900998b5e11c03023264208James Dong    curL = currInput->YCbCr[0] + offset;
205629a84457aed4c45bc900998b5e11c03023264208James Dong    video->pintra_pred_top = curL - pitch;
205729a84457aed4c45bc900998b5e11c03023264208James Dong    video->pintra_pred_left = curL - 1;
205829a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->mb_y)
205929a84457aed4c45bc900998b5e11c03023264208James Dong    {
206029a84457aed4c45bc900998b5e11c03023264208James Dong        video->intra_pred_topleft = *(curL - pitch - 1);
206129a84457aed4c45bc900998b5e11c03023264208James Dong    }
206229a84457aed4c45bc900998b5e11c03023264208James Dong
206329a84457aed4c45bc900998b5e11c03023264208James Dong    /* Intra_16x16_Vertical */
206429a84457aed4c45bc900998b5e11c03023264208James Dong    sadI16 = 65536;
206529a84457aed4c45bc900998b5e11c03023264208James Dong    /* check availability of top */
206629a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailB)
206729a84457aed4c45bc900998b5e11c03023264208James Dong    {
206829a84457aed4c45bc900998b5e11c03023264208James Dong        sad = SAD_I16_Vert(video, curL, sadI16);
206929a84457aed4c45bc900998b5e11c03023264208James Dong
207029a84457aed4c45bc900998b5e11c03023264208James Dong        if (sad < sadI16)
207129a84457aed4c45bc900998b5e11c03023264208James Dong        {
207229a84457aed4c45bc900998b5e11c03023264208James Dong            sadI16 = sad;
207329a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_Vertical;
207429a84457aed4c45bc900998b5e11c03023264208James Dong        }
207529a84457aed4c45bc900998b5e11c03023264208James Dong    }
207629a84457aed4c45bc900998b5e11c03023264208James Dong    /* Intra_16x16_Horizontal */
207729a84457aed4c45bc900998b5e11c03023264208James Dong    /* check availability of left */
207829a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA)
207929a84457aed4c45bc900998b5e11c03023264208James Dong    {
208029a84457aed4c45bc900998b5e11c03023264208James Dong        sad = SAD_I16_HorzDC(video, curL, AVC_I16_Horizontal, sadI16);
208129a84457aed4c45bc900998b5e11c03023264208James Dong
208229a84457aed4c45bc900998b5e11c03023264208James Dong        if (sad < sadI16)
208329a84457aed4c45bc900998b5e11c03023264208James Dong        {
208429a84457aed4c45bc900998b5e11c03023264208James Dong            sadI16 = sad;
208529a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_Horizontal;
208629a84457aed4c45bc900998b5e11c03023264208James Dong        }
208729a84457aed4c45bc900998b5e11c03023264208James Dong    }
208829a84457aed4c45bc900998b5e11c03023264208James Dong
208929a84457aed4c45bc900998b5e11c03023264208James Dong    /* Intra_16x16_DC, default mode */
209029a84457aed4c45bc900998b5e11c03023264208James Dong    sad = SAD_I16_HorzDC(video, curL, AVC_I16_DC, sadI16);
209129a84457aed4c45bc900998b5e11c03023264208James Dong    if (sad < sadI16)
209229a84457aed4c45bc900998b5e11c03023264208James Dong    {
209329a84457aed4c45bc900998b5e11c03023264208James Dong        sadI16 = sad;
209429a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->i16Mode = AVC_I16_DC;
209529a84457aed4c45bc900998b5e11c03023264208James Dong    }
209629a84457aed4c45bc900998b5e11c03023264208James Dong
209729a84457aed4c45bc900998b5e11c03023264208James Dong    /* Intra_16x16_Plane */
209829a84457aed4c45bc900998b5e11c03023264208James Dong    if (video->intraAvailA && video->intraAvailB && video->intraAvailD)
209929a84457aed4c45bc900998b5e11c03023264208James Dong    {
210029a84457aed4c45bc900998b5e11c03023264208James Dong        sad = SAD_I16_Plane(video, curL, sadI16);
210129a84457aed4c45bc900998b5e11c03023264208James Dong
210229a84457aed4c45bc900998b5e11c03023264208James Dong        if (sad < sadI16)
210329a84457aed4c45bc900998b5e11c03023264208James Dong        {
210429a84457aed4c45bc900998b5e11c03023264208James Dong            sadI16 = sad;
210529a84457aed4c45bc900998b5e11c03023264208James Dong            currMB->i16Mode = AVC_I16_Plane;
210629a84457aed4c45bc900998b5e11c03023264208James Dong        }
210729a84457aed4c45bc900998b5e11c03023264208James Dong    }
210829a84457aed4c45bc900998b5e11c03023264208James Dong
210929a84457aed4c45bc900998b5e11c03023264208James Dong    sadI16 >>= 1;  /* before comparison */
211029a84457aed4c45bc900998b5e11c03023264208James Dong
211129a84457aed4c45bc900998b5e11c03023264208James Dong    /* selection between intra4, intra16 or inter mode */
211229a84457aed4c45bc900998b5e11c03023264208James Dong    if (sadI16 < encvid->min_cost)
211329a84457aed4c45bc900998b5e11c03023264208James Dong    {
211429a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->mb_intra = TRUE;
211529a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->mbMode = AVC_I16;
211629a84457aed4c45bc900998b5e11c03023264208James Dong        encvid->min_cost = sadI16;
211729a84457aed4c45bc900998b5e11c03023264208James Dong    }
211829a84457aed4c45bc900998b5e11c03023264208James Dong
211929a84457aed4c45bc900998b5e11c03023264208James Dong    if (currMB->mb_intra) /* only do the chrominance search when intra is decided */
212029a84457aed4c45bc900998b5e11c03023264208James Dong    {
212129a84457aed4c45bc900998b5e11c03023264208James Dong        /* Note that we might be able to guess the type of prediction from
212229a84457aed4c45bc900998b5e11c03023264208James Dong        the luma prediction type */
212329a84457aed4c45bc900998b5e11c03023264208James Dong
212429a84457aed4c45bc900998b5e11c03023264208James Dong        /* now search for the best chroma intra prediction */
212529a84457aed4c45bc900998b5e11c03023264208James Dong        offset = (offset >> 2) + (video->mb_x << 2);
212629a84457aed4c45bc900998b5e11c03023264208James Dong        curCb = currInput->YCbCr[1] + offset;
212729a84457aed4c45bc900998b5e11c03023264208James Dong        curCr = currInput->YCbCr[2] + offset;
212829a84457aed4c45bc900998b5e11c03023264208James Dong
212929a84457aed4c45bc900998b5e11c03023264208James Dong        pitch >>= 1;
213029a84457aed4c45bc900998b5e11c03023264208James Dong        video->pintra_pred_top_cb = curCb - pitch;
213129a84457aed4c45bc900998b5e11c03023264208James Dong        video->pintra_pred_left_cb = curCb - 1;
213229a84457aed4c45bc900998b5e11c03023264208James Dong        video->pintra_pred_top_cr = curCr - pitch;
213329a84457aed4c45bc900998b5e11c03023264208James Dong        video->pintra_pred_left_cr = curCr - 1;
213429a84457aed4c45bc900998b5e11c03023264208James Dong
213529a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->mb_y)
213629a84457aed4c45bc900998b5e11c03023264208James Dong        {
213729a84457aed4c45bc900998b5e11c03023264208James Dong            video->intra_pred_topleft_cb = *(curCb - pitch - 1);
213829a84457aed4c45bc900998b5e11c03023264208James Dong            video->intra_pred_topleft_cr = *(curCr - pitch - 1);
213929a84457aed4c45bc900998b5e11c03023264208James Dong        }
214029a84457aed4c45bc900998b5e11c03023264208James Dong
214129a84457aed4c45bc900998b5e11c03023264208James Dong        /* Intra_Chroma_DC */
214229a84457aed4c45bc900998b5e11c03023264208James Dong        sad4 = SAD_Chroma_DC(video, curCb, curCr, 65536);
214329a84457aed4c45bc900998b5e11c03023264208James Dong        currMB->intra_chroma_pred_mode = AVC_IC_DC;
214429a84457aed4c45bc900998b5e11c03023264208James Dong
214529a84457aed4c45bc900998b5e11c03023264208James Dong        /* Intra_Chroma_Horizontal */
214629a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->intraAvailA)
214729a84457aed4c45bc900998b5e11c03023264208James Dong        {
214829a84457aed4c45bc900998b5e11c03023264208James Dong            /* check availability of left */
214929a84457aed4c45bc900998b5e11c03023264208James Dong            sad = SAD_Chroma_Horz(video, curCb, curCr, sad4);
215029a84457aed4c45bc900998b5e11c03023264208James Dong            if (sad < sad4)
215129a84457aed4c45bc900998b5e11c03023264208James Dong            {
215229a84457aed4c45bc900998b5e11c03023264208James Dong                sad4 = sad;
215329a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->intra_chroma_pred_mode = AVC_IC_Horizontal;
215429a84457aed4c45bc900998b5e11c03023264208James Dong            }
215529a84457aed4c45bc900998b5e11c03023264208James Dong        }
215629a84457aed4c45bc900998b5e11c03023264208James Dong
215729a84457aed4c45bc900998b5e11c03023264208James Dong        /* Intra_Chroma_Vertical */
215829a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->intraAvailB)
215929a84457aed4c45bc900998b5e11c03023264208James Dong        {
216029a84457aed4c45bc900998b5e11c03023264208James Dong            /* check availability of top */
216129a84457aed4c45bc900998b5e11c03023264208James Dong            sad = SAD_Chroma_Vert(video, curCb, curCr, sad4);
216229a84457aed4c45bc900998b5e11c03023264208James Dong
216329a84457aed4c45bc900998b5e11c03023264208James Dong            if (sad < sad4)
216429a84457aed4c45bc900998b5e11c03023264208James Dong            {
216529a84457aed4c45bc900998b5e11c03023264208James Dong                sad4 = sad;
216629a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->intra_chroma_pred_mode = AVC_IC_Vertical;
216729a84457aed4c45bc900998b5e11c03023264208James Dong            }
216829a84457aed4c45bc900998b5e11c03023264208James Dong        }
216929a84457aed4c45bc900998b5e11c03023264208James Dong
217029a84457aed4c45bc900998b5e11c03023264208James Dong        /* Intra_Chroma_Plane */
217129a84457aed4c45bc900998b5e11c03023264208James Dong        if (video->intraAvailA && video->intraAvailB && video->intraAvailD)
217229a84457aed4c45bc900998b5e11c03023264208James Dong        {
217329a84457aed4c45bc900998b5e11c03023264208James Dong            /* check availability of top and left */
217429a84457aed4c45bc900998b5e11c03023264208James Dong            Intra_Chroma_Plane(video, pitch);
217529a84457aed4c45bc900998b5e11c03023264208James Dong
217629a84457aed4c45bc900998b5e11c03023264208James Dong            sad = SADChroma(pred_block + 452, curCb, curCr, pitch);
217729a84457aed4c45bc900998b5e11c03023264208James Dong
217829a84457aed4c45bc900998b5e11c03023264208James Dong            if (sad < sad4)
217929a84457aed4c45bc900998b5e11c03023264208James Dong            {
218029a84457aed4c45bc900998b5e11c03023264208James Dong                sad4 = sad;
218129a84457aed4c45bc900998b5e11c03023264208James Dong                currMB->intra_chroma_pred_mode = AVC_IC_Plane;
218229a84457aed4c45bc900998b5e11c03023264208James Dong            }
218329a84457aed4c45bc900998b5e11c03023264208James Dong        }
218429a84457aed4c45bc900998b5e11c03023264208James Dong
218529a84457aed4c45bc900998b5e11c03023264208James Dong        /* also reset the motion vectors */
218629a84457aed4c45bc900998b5e11c03023264208James Dong        /* set MV and Ref_Idx codes of Intra blocks in P-slices */
218729a84457aed4c45bc900998b5e11c03023264208James Dong        memset(currMB->mvL0, 0, sizeof(int32)*16);
218829a84457aed4c45bc900998b5e11c03023264208James Dong        memset(currMB->ref_idx_L0, -1, sizeof(int16)*4);
218929a84457aed4c45bc900998b5e11c03023264208James Dong
219029a84457aed4c45bc900998b5e11c03023264208James Dong    }
219129a84457aed4c45bc900998b5e11c03023264208James Dong
219229a84457aed4c45bc900998b5e11c03023264208James Dong    // output from this function, currMB->mbMode should be set to either
219329a84457aed4c45bc900998b5e11c03023264208James Dong    // AVC_I4, AVC_I16, or else in AVCMBMode enum, mbType, mb_intra, intra_chroma_pred_mode */
219429a84457aed4c45bc900998b5e11c03023264208James Dong
219529a84457aed4c45bc900998b5e11c03023264208James Dong    return ;
219629a84457aed4c45bc900998b5e11c03023264208James Dong}
219729a84457aed4c45bc900998b5e11c03023264208James Dong#endif
219829a84457aed4c45bc900998b5e11c03023264208James Dong
219929a84457aed4c45bc900998b5e11c03023264208James Dong
2200