10c1bc742181ded4930842b46e9507372f0b1b963James Dong/*
20c1bc742181ded4930842b46e9507372f0b1b963James Dong * Copyright (C) 2009 The Android Open Source Project
30c1bc742181ded4930842b46e9507372f0b1b963James Dong *
40c1bc742181ded4930842b46e9507372f0b1b963James Dong * Licensed under the Apache License, Version 2.0 (the "License");
50c1bc742181ded4930842b46e9507372f0b1b963James Dong * you may not use this file except in compliance with the License.
60c1bc742181ded4930842b46e9507372f0b1b963James Dong * You may obtain a copy of the License at
70c1bc742181ded4930842b46e9507372f0b1b963James Dong *
80c1bc742181ded4930842b46e9507372f0b1b963James Dong *      http://www.apache.org/licenses/LICENSE-2.0
90c1bc742181ded4930842b46e9507372f0b1b963James Dong *
100c1bc742181ded4930842b46e9507372f0b1b963James Dong * Unless required by applicable law or agreed to in writing, software
110c1bc742181ded4930842b46e9507372f0b1b963James Dong * distributed under the License is distributed on an "AS IS" BASIS,
120c1bc742181ded4930842b46e9507372f0b1b963James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130c1bc742181ded4930842b46e9507372f0b1b963James Dong * See the License for the specific language governing permissions and
140c1bc742181ded4930842b46e9507372f0b1b963James Dong * limitations under the License.
150c1bc742181ded4930842b46e9507372f0b1b963James Dong */
160c1bc742181ded4930842b46e9507372f0b1b963James Dong
170c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
180c1bc742181ded4930842b46e9507372f0b1b963James Dong
190c1bc742181ded4930842b46e9507372f0b1b963James Dong    Table of contents
200c1bc742181ded4930842b46e9507372f0b1b963James Dong
210c1bc742181ded4930842b46e9507372f0b1b963James Dong     1. Include headers
220c1bc742181ded4930842b46e9507372f0b1b963James Dong     2. External compiler flags
230c1bc742181ded4930842b46e9507372f0b1b963James Dong     3. Module defines
240c1bc742181ded4930842b46e9507372f0b1b963James Dong     4. Local function prototypes
250c1bc742181ded4930842b46e9507372f0b1b963James Dong     5. Functions
260c1bc742181ded4930842b46e9507372f0b1b963James Dong          h264bsdConceal
270c1bc742181ded4930842b46e9507372f0b1b963James Dong          ConcealMb
280c1bc742181ded4930842b46e9507372f0b1b963James Dong          Transform
290c1bc742181ded4930842b46e9507372f0b1b963James Dong
300c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
310c1bc742181ded4930842b46e9507372f0b1b963James Dong
320c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
330c1bc742181ded4930842b46e9507372f0b1b963James Dong    1. Include headers
340c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
350c1bc742181ded4930842b46e9507372f0b1b963James Dong
360c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_conceal.h"
370c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_util.h"
380c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_reconstruct.h"
390c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_dpb.h"
400c1bc742181ded4930842b46e9507372f0b1b963James Dong
410c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
420c1bc742181ded4930842b46e9507372f0b1b963James Dong    2. External compiler flags
430c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
440c1bc742181ded4930842b46e9507372f0b1b963James Dong
450c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
460c1bc742181ded4930842b46e9507372f0b1b963James Dong    3. Module defines
470c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
480c1bc742181ded4930842b46e9507372f0b1b963James Dong
490c1bc742181ded4930842b46e9507372f0b1b963James Dong/*lint -e702 disable lint warning on right shift of signed quantity */
500c1bc742181ded4930842b46e9507372f0b1b963James Dong
510c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
520c1bc742181ded4930842b46e9507372f0b1b963James Dong    4. Local function prototypes
530c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
540c1bc742181ded4930842b46e9507372f0b1b963James Dong
550c1bc742181ded4930842b46e9507372f0b1b963James Dongstatic u32 ConcealMb(mbStorage_t *pMb, image_t *currImage, u32 row, u32 col,
560c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 sliceType, u8 *data);
570c1bc742181ded4930842b46e9507372f0b1b963James Dong
580c1bc742181ded4930842b46e9507372f0b1b963James Dongstatic void Transform(i32 *data);
590c1bc742181ded4930842b46e9507372f0b1b963James Dong
600c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
610c1bc742181ded4930842b46e9507372f0b1b963James Dong
620c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function name: h264bsdConceal
630c1bc742181ded4930842b46e9507372f0b1b963James Dong
640c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
650c1bc742181ded4930842b46e9507372f0b1b963James Dong            Perform error concealment for a picture. Two types of concealment
660c1bc742181ded4930842b46e9507372f0b1b963James Dong            is performed based on sliceType:
670c1bc742181ded4930842b46e9507372f0b1b963James Dong                1) copy from previous picture for P-slices.
680c1bc742181ded4930842b46e9507372f0b1b963James Dong                2) concealment from neighbour pixels for I-slices
690c1bc742181ded4930842b46e9507372f0b1b963James Dong
700c1bc742181ded4930842b46e9507372f0b1b963James Dong            I-type concealment is based on ideas presented by Jarno Tulkki.
710c1bc742181ded4930842b46e9507372f0b1b963James Dong            The concealment algorithm determines frequency domain coefficients
720c1bc742181ded4930842b46e9507372f0b1b963James Dong            from the neighbour pixels, applies integer transform (the same
730c1bc742181ded4930842b46e9507372f0b1b963James Dong            transform used in the residual processing) and uses the results as
740c1bc742181ded4930842b46e9507372f0b1b963James Dong            pixel values for concealed macroblocks. Transform produces 4x4
750c1bc742181ded4930842b46e9507372f0b1b963James Dong            array and one pixel value has to be used for 4x4 luma blocks and
760c1bc742181ded4930842b46e9507372f0b1b963James Dong            2x2 chroma blocks.
770c1bc742181ded4930842b46e9507372f0b1b963James Dong
780c1bc742181ded4930842b46e9507372f0b1b963James Dong            Similar concealment is performed for whole picture (the choise
790c1bc742181ded4930842b46e9507372f0b1b963James Dong            of the type is based on last successfully decoded slice header of
800c1bc742181ded4930842b46e9507372f0b1b963James Dong            the picture but it is handled by the calling function). It is
810c1bc742181ded4930842b46e9507372f0b1b963James Dong            acknowledged that this may result in wrong type of concealment
820c1bc742181ded4930842b46e9507372f0b1b963James Dong            when a picture contains both types of slices. However,
830c1bc742181ded4930842b46e9507372f0b1b963James Dong            determination of slice type macroblock-by-macroblock cannot
840c1bc742181ded4930842b46e9507372f0b1b963James Dong            be done due to the fact that it is impossible to know to which
850c1bc742181ded4930842b46e9507372f0b1b963James Dong            slice each corrupted (not successfully decoded) macroblock
860c1bc742181ded4930842b46e9507372f0b1b963James Dong            belongs.
870c1bc742181ded4930842b46e9507372f0b1b963James Dong
880c1bc742181ded4930842b46e9507372f0b1b963James Dong            The error concealment is started by searching the first propoerly
890c1bc742181ded4930842b46e9507372f0b1b963James Dong            decoded macroblock and concealing the row containing the macroblock
900c1bc742181ded4930842b46e9507372f0b1b963James Dong            in question. After that all macroblocks above the row in question
910c1bc742181ded4930842b46e9507372f0b1b963James Dong            are concealed. Finally concealment of rows below is performed.
920c1bc742181ded4930842b46e9507372f0b1b963James Dong            The order of concealment for 4x4 picture where macroblock 9 is the
930c1bc742181ded4930842b46e9507372f0b1b963James Dong            first properly decoded one is as follows (properly decoded
940c1bc742181ded4930842b46e9507372f0b1b963James Dong            macroblocks marked with 'x', numbers indicating the order of
950c1bc742181ded4930842b46e9507372f0b1b963James Dong            concealment):
960c1bc742181ded4930842b46e9507372f0b1b963James Dong
970c1bc742181ded4930842b46e9507372f0b1b963James Dong               4  6  8 10
980c1bc742181ded4930842b46e9507372f0b1b963James Dong               3  5  7  9
990c1bc742181ded4930842b46e9507372f0b1b963James Dong               1  x  x  2
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong              11 12 13 14
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong            If all macroblocks of the picture are lost, the concealment is
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong            copy of previous picture for P-type and setting the image to
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong            constant gray (pixel value 128) for I-type.
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong            Concealment sets quantization parameter of the concealed
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong            macroblocks to value 40 and macroblock type to intra to enable
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong            deblocking filter to smooth the edges of the concealed areas.
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage        pointer to storage structure
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong            currImage       pointer to current image structure
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong            sliceType       type of the slice
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong            currImage       concealed macroblocks will be written here
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong            HANTRO_OK
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong
1230c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdConceal(storage_t *pStorage, image_t *currImage, u32 sliceType)
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong{
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 i, j;
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 row, col;
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 width, height;
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 *refData;
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong    mbStorage_t *mb;
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pStorage);
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(currImage);
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEBUG(("Concealing %s slice\n", IS_I_SLICE(sliceType) ?
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong            "intra" : "inter"));
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong    width = currImage->width;
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong    height = currImage->height;
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong    refData = NULL;
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* use reference picture with smallest available index */
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (IS_P_SLICE(sliceType) || (pStorage->intraConcealmentFlag != 0))
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong        i = 0;
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong        do
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong            refData = h264bsdGetRefPicData(pStorage->dpb, i);
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong            i++;
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (i >= 16)
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong        } while (refData == NULL);
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong    i = row = col = 0;
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* find first properly decoded macroblock -> start point for concealment */
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong    while (i < pStorage->picSizeInMbs && !pStorage->mb[i].decoded)
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong        i++;
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong        col++;
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (col == width)
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong            row++;
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong            col = 0;
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* whole picture lost -> copy previous or set grey */
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (i == pStorage->picSizeInMbs)
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong        if ( (IS_I_SLICE(sliceType) && (pStorage->intraConcealmentFlag == 0)) ||
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong             refData == NULL)
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SwDecMemset(currImage->data, 128, width*height*384);
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong        else
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SwDecMemcpy(currImage->data, refData, width*height*384);
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong        pStorage->numConcealedMbs = pStorage->picSizeInMbs;
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* no filtering if whole picture concealed */
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (i = 0; i < pStorage->picSizeInMbs; i++)
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->mb[i].disableDeblockingFilterIdc = 1;
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_OK);
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* start from the row containing the first correct macroblock, conceal the
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong     * row in question, all rows above that row and then continue downwards */
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong    mb = pStorage->mb + row * width;
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (j = col; j--;)
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong        ConcealMb(mb+j, currImage, row, j, sliceType, refData);
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong        mb[j].decoded = 1;
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong        pStorage->numConcealedMbs++;
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (j = col + 1; j < width; j++)
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (!mb[j].decoded)
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong            ConcealMb(mb+j, currImage, row, j, sliceType, refData);
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong            mb[j].decoded = 1;
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->numConcealedMbs++;
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* if previous row(s) could not be concealed -> conceal them now */
2080c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (row)
2090c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2100c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (j = 0; j < width; j++)
2110c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
2120c1bc742181ded4930842b46e9507372f0b1b963James Dong            i = row - 1;
2130c1bc742181ded4930842b46e9507372f0b1b963James Dong            mb = pStorage->mb + i*width + j;
2140c1bc742181ded4930842b46e9507372f0b1b963James Dong            do
2150c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
2160c1bc742181ded4930842b46e9507372f0b1b963James Dong                ConcealMb(mb, currImage, i, j, sliceType, refData);
2170c1bc742181ded4930842b46e9507372f0b1b963James Dong                mb->decoded = 1;
2180c1bc742181ded4930842b46e9507372f0b1b963James Dong                pStorage->numConcealedMbs++;
2190c1bc742181ded4930842b46e9507372f0b1b963James Dong                mb -= width;
2200c1bc742181ded4930842b46e9507372f0b1b963James Dong            } while(i--);
2210c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
2220c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2230c1bc742181ded4930842b46e9507372f0b1b963James Dong
2240c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* process rows below the one containing the first correct macroblock */
2250c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (i = row + 1; i < height; i++)
2260c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2270c1bc742181ded4930842b46e9507372f0b1b963James Dong        mb = pStorage->mb + i * width;
2280c1bc742181ded4930842b46e9507372f0b1b963James Dong
2290c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (j = 0; j < width; j++)
2300c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
2310c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (!mb[j].decoded)
2320c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
2330c1bc742181ded4930842b46e9507372f0b1b963James Dong                ConcealMb(mb+j, currImage, i, j, sliceType, refData);
2340c1bc742181ded4930842b46e9507372f0b1b963James Dong                mb[j].decoded = 1;
2350c1bc742181ded4930842b46e9507372f0b1b963James Dong                pStorage->numConcealedMbs++;
2360c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
2370c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
2380c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2390c1bc742181ded4930842b46e9507372f0b1b963James Dong
2400c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(HANTRO_OK);
2410c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2420c1bc742181ded4930842b46e9507372f0b1b963James Dong
2430c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
2440c1bc742181ded4930842b46e9507372f0b1b963James Dong
2450c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function name: ConcealMb
2460c1bc742181ded4930842b46e9507372f0b1b963James Dong
2470c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
2480c1bc742181ded4930842b46e9507372f0b1b963James Dong            Perform error concealment for one macroblock, location of the
2490c1bc742181ded4930842b46e9507372f0b1b963James Dong            macroblock in the picture indicated by row and col
2500c1bc742181ded4930842b46e9507372f0b1b963James Dong
2510c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
2520c1bc742181ded4930842b46e9507372f0b1b963James Dong
2530c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 ConcealMb(mbStorage_t *pMb, image_t *currImage, u32 row, u32 col,
2540c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 sliceType, u8 *refData)
2550c1bc742181ded4930842b46e9507372f0b1b963James Dong{
2560c1bc742181ded4930842b46e9507372f0b1b963James Dong
2570c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
2580c1bc742181ded4930842b46e9507372f0b1b963James Dong
2590c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 i, j, comp;
2600c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 hor, ver;
2610c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 mbNum;
2620c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 width, height;
2630c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 *mbPos;
2640c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 data[384];
2650c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 *pData;
2660c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 tmp;
2670c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 firstPhase[16];
2680c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 *pTmp;
2690c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* neighbours above, below, left and right */
27084333e0475bc911adc16417f4ca327c975cf6c36Andreas Huber    i32 a[4] = { 0,0,0,0 }, b[4], l[4] = { 0,0,0,0 }, r[4];
2710c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 A, B, L, R;
2720c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_OMXDL
2730c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 fillBuff[32*21 + 15 + 32];
2740c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 *pFill;
2750c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
2760c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
2770c1bc742181ded4930842b46e9507372f0b1b963James Dong
2780c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pMb);
2790c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(!pMb->decoded);
2800c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(currImage);
2810c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(col < currImage->width);
2820c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(row < currImage->height);
2830c1bc742181ded4930842b46e9507372f0b1b963James Dong
2840c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_OMXDL
2850c1bc742181ded4930842b46e9507372f0b1b963James Dong    pFill = ALIGN(fillBuff, 16);
2860c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
2870c1bc742181ded4930842b46e9507372f0b1b963James Dong    width = currImage->width;
2880c1bc742181ded4930842b46e9507372f0b1b963James Dong    height = currImage->height;
2890c1bc742181ded4930842b46e9507372f0b1b963James Dong    mbNum = row * width + col;
2900c1bc742181ded4930842b46e9507372f0b1b963James Dong
2910c1bc742181ded4930842b46e9507372f0b1b963James Dong    h264bsdSetCurrImageMbPointers(currImage, mbNum);
2920c1bc742181ded4930842b46e9507372f0b1b963James Dong
2930c1bc742181ded4930842b46e9507372f0b1b963James Dong    mbPos = currImage->data + row * 16 * width * 16 + col * 16;
2940c1bc742181ded4930842b46e9507372f0b1b963James Dong    A = B = L = R = HANTRO_FALSE;
2950c1bc742181ded4930842b46e9507372f0b1b963James Dong
2960c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* set qpY to 40 to enable some filtering in deblocking (stetson value) */
2970c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->qpY = 40;
2980c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->disableDeblockingFilterIdc = 0;
2990c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* mbType set to intra to perform filtering despite the values of other
3000c1bc742181ded4930842b46e9507372f0b1b963James Dong     * boundary strength determination fields */
3010c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->mbType = I_4x4;
3020c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->filterOffsetA = 0;
3030c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->filterOffsetB = 0;
3040c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->chromaQpIndexOffset = 0;
3050c1bc742181ded4930842b46e9507372f0b1b963James Dong
3060c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (IS_I_SLICE(sliceType))
3070c1bc742181ded4930842b46e9507372f0b1b963James Dong        H264SwDecMemset(data, 0, sizeof(data));
3080c1bc742181ded4930842b46e9507372f0b1b963James Dong    else
3090c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3100c1bc742181ded4930842b46e9507372f0b1b963James Dong        mv_t mv = {0,0};
3110c1bc742181ded4930842b46e9507372f0b1b963James Dong        image_t refImage;
3120c1bc742181ded4930842b46e9507372f0b1b963James Dong        refImage.width = width;
3130c1bc742181ded4930842b46e9507372f0b1b963James Dong        refImage.height = height;
3140c1bc742181ded4930842b46e9507372f0b1b963James Dong        refImage.data = refData;
3150c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (refImage.data)
3160c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3170c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifndef H264DEC_OMXDL
3180c1bc742181ded4930842b46e9507372f0b1b963James Dong            h264bsdPredictSamples(data, &mv, &refImage, col*16, row*16,
3190c1bc742181ded4930842b46e9507372f0b1b963James Dong                0, 0, 16, 16);
3200c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
3210c1bc742181ded4930842b46e9507372f0b1b963James Dong            h264bsdPredictSamples(data, &mv, &refImage,
3220c1bc742181ded4930842b46e9507372f0b1b963James Dong                    ((row*16) + ((col*16)<<16)),
3230c1bc742181ded4930842b46e9507372f0b1b963James Dong                    0x00001010, pFill);
3240c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
3250c1bc742181ded4930842b46e9507372f0b1b963James Dong            h264bsdWriteMacroblock(currImage, data);
3260c1bc742181ded4930842b46e9507372f0b1b963James Dong
3270c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(HANTRO_OK);
3280c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3290c1bc742181ded4930842b46e9507372f0b1b963James Dong        else
3300c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SwDecMemset(data, 0, sizeof(data));
3310c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3320c1bc742181ded4930842b46e9507372f0b1b963James Dong
3330c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecMemset(firstPhase, 0, sizeof(firstPhase));
3340c1bc742181ded4930842b46e9507372f0b1b963James Dong
3350c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* counter for number of neighbours used */
3360c1bc742181ded4930842b46e9507372f0b1b963James Dong    j = 0;
3370c1bc742181ded4930842b46e9507372f0b1b963James Dong    hor = ver = 0;
3380c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (row && (pMb-width)->decoded)
3390c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3400c1bc742181ded4930842b46e9507372f0b1b963James Dong        A = HANTRO_TRUE;
3410c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData = mbPos - width*16;
3420c1bc742181ded4930842b46e9507372f0b1b963James Dong        a[0] = *pData++; a[0] += *pData++; a[0] += *pData++; a[0] += *pData++;
3430c1bc742181ded4930842b46e9507372f0b1b963James Dong        a[1] = *pData++; a[1] += *pData++; a[1] += *pData++; a[1] += *pData++;
3440c1bc742181ded4930842b46e9507372f0b1b963James Dong        a[2] = *pData++; a[2] += *pData++; a[2] += *pData++; a[2] += *pData++;
3450c1bc742181ded4930842b46e9507372f0b1b963James Dong        a[3] = *pData++; a[3] += *pData++; a[3] += *pData++; a[3] += *pData++;
3460c1bc742181ded4930842b46e9507372f0b1b963James Dong        j++;
3470c1bc742181ded4930842b46e9507372f0b1b963James Dong        hor++;
3480c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[0] += a[0] + a[1] + a[2] + a[3];
3490c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[1] += a[0] + a[1] - a[2] - a[3];
3500c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3510c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((row != height - 1) && (pMb+width)->decoded)
3520c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3530c1bc742181ded4930842b46e9507372f0b1b963James Dong        B = HANTRO_TRUE;
3540c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData = mbPos + 16*width*16;
3550c1bc742181ded4930842b46e9507372f0b1b963James Dong        b[0] = *pData++; b[0] += *pData++; b[0] += *pData++; b[0] += *pData++;
3560c1bc742181ded4930842b46e9507372f0b1b963James Dong        b[1] = *pData++; b[1] += *pData++; b[1] += *pData++; b[1] += *pData++;
3570c1bc742181ded4930842b46e9507372f0b1b963James Dong        b[2] = *pData++; b[2] += *pData++; b[2] += *pData++; b[2] += *pData++;
3580c1bc742181ded4930842b46e9507372f0b1b963James Dong        b[3] = *pData++; b[3] += *pData++; b[3] += *pData++; b[3] += *pData++;
3590c1bc742181ded4930842b46e9507372f0b1b963James Dong        j++;
3600c1bc742181ded4930842b46e9507372f0b1b963James Dong        hor++;
3610c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[0] += b[0] + b[1] + b[2] + b[3];
3620c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[1] += b[0] + b[1] - b[2] - b[3];
3630c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3640c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (col && (pMb-1)->decoded)
3650c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3660c1bc742181ded4930842b46e9507372f0b1b963James Dong        L = HANTRO_TRUE;
3670c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData = mbPos - 1;
3680c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[0] = pData[0]; l[0] += pData[16*width];
3690c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[0] += pData[32*width]; l[0] += pData[48*width];
3700c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData += 64*width;
3710c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[1] = pData[0]; l[1] += pData[16*width];
3720c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[1] += pData[32*width]; l[1] += pData[48*width];
3730c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData += 64*width;
3740c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[2] = pData[0]; l[2] += pData[16*width];
3750c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[2] += pData[32*width]; l[2] += pData[48*width];
3760c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData += 64*width;
3770c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[3] = pData[0]; l[3] += pData[16*width];
3780c1bc742181ded4930842b46e9507372f0b1b963James Dong        l[3] += pData[32*width]; l[3] += pData[48*width];
3790c1bc742181ded4930842b46e9507372f0b1b963James Dong        j++;
3800c1bc742181ded4930842b46e9507372f0b1b963James Dong        ver++;
3810c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[0] += l[0] + l[1] + l[2] + l[3];
3820c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[4] += l[0] + l[1] - l[2] - l[3];
3830c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3840c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((col != width - 1) && (pMb+1)->decoded)
3850c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3860c1bc742181ded4930842b46e9507372f0b1b963James Dong        R = HANTRO_TRUE;
3870c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData = mbPos + 16;
3880c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[0] = pData[0]; r[0] += pData[16*width];
3890c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[0] += pData[32*width]; r[0] += pData[48*width];
3900c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData += 64*width;
3910c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[1] = pData[0]; r[1] += pData[16*width];
3920c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[1] += pData[32*width]; r[1] += pData[48*width];
3930c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData += 64*width;
3940c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[2] = pData[0]; r[2] += pData[16*width];
3950c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[2] += pData[32*width]; r[2] += pData[48*width];
3960c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData += 64*width;
3970c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[3] = pData[0]; r[3] += pData[16*width];
3980c1bc742181ded4930842b46e9507372f0b1b963James Dong        r[3] += pData[32*width]; r[3] += pData[48*width];
3990c1bc742181ded4930842b46e9507372f0b1b963James Dong        j++;
4000c1bc742181ded4930842b46e9507372f0b1b963James Dong        ver++;
4010c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[0] += r[0] + r[1] + r[2] + r[3];
4020c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[4] += r[0] + r[1] - r[2] - r[3];
4030c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
4040c1bc742181ded4930842b46e9507372f0b1b963James Dong
4050c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* at least one properly decoded neighbour available */
4060c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(j);
4070c1bc742181ded4930842b46e9507372f0b1b963James Dong
4080c1bc742181ded4930842b46e9507372f0b1b963James Dong    /*lint -esym(644,l,r,a,b) variable initialized above */
4090c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (!hor && L && R)
4100c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[1] = (l[0]+l[1]+l[2]+l[3]-r[0]-r[1]-r[2]-r[3]) >> 5;
4110c1bc742181ded4930842b46e9507372f0b1b963James Dong    else if (hor)
4120c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[1] >>= (3+hor);
4130c1bc742181ded4930842b46e9507372f0b1b963James Dong
4140c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (!ver && A && B)
4150c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[4] = (a[0]+a[1]+a[2]+a[3]-b[0]-b[1]-b[2]-b[3]) >> 5;
4160c1bc742181ded4930842b46e9507372f0b1b963James Dong    else if (ver)
4170c1bc742181ded4930842b46e9507372f0b1b963James Dong        firstPhase[4] >>= (3+ver);
4180c1bc742181ded4930842b46e9507372f0b1b963James Dong
4190c1bc742181ded4930842b46e9507372f0b1b963James Dong    switch (j)
4200c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
4210c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 1:
4220c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] >>= 4;
4230c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4240c1bc742181ded4930842b46e9507372f0b1b963James Dong
4250c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 2:
4260c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] >>= 5;
4270c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4280c1bc742181ded4930842b46e9507372f0b1b963James Dong
4290c1bc742181ded4930842b46e9507372f0b1b963James Dong        case 3:
4300c1bc742181ded4930842b46e9507372f0b1b963James Dong            /* approximate (firstPhase[0]*4/3)>>6 */
4310c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] = (21 * firstPhase[0]) >> 10;
4320c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4330c1bc742181ded4930842b46e9507372f0b1b963James Dong
4340c1bc742181ded4930842b46e9507372f0b1b963James Dong        default: /* 4 */
4350c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] >>= 6;
4360c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
4370c1bc742181ded4930842b46e9507372f0b1b963James Dong
4380c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
4390c1bc742181ded4930842b46e9507372f0b1b963James Dong
4400c1bc742181ded4930842b46e9507372f0b1b963James Dong
4410c1bc742181ded4930842b46e9507372f0b1b963James Dong    Transform(firstPhase);
4420c1bc742181ded4930842b46e9507372f0b1b963James Dong
4430c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (i = 0, pData = data, pTmp = firstPhase; i < 256;)
4440c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
4450c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = pTmp[(i & 0xF)>>2];
4460c1bc742181ded4930842b46e9507372f0b1b963James Dong        /*lint -e734 CLIP1 macro results in value that fits into 8 bits */
4470c1bc742181ded4930842b46e9507372f0b1b963James Dong        *pData++ = CLIP1(tmp);
4480c1bc742181ded4930842b46e9507372f0b1b963James Dong        /*lint +e734 */
4490c1bc742181ded4930842b46e9507372f0b1b963James Dong
4500c1bc742181ded4930842b46e9507372f0b1b963James Dong        i++;
4510c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (!(i & 0x3F))
4520c1bc742181ded4930842b46e9507372f0b1b963James Dong            pTmp += 4;
4530c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
4540c1bc742181ded4930842b46e9507372f0b1b963James Dong
4550c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* chroma components */
4560c1bc742181ded4930842b46e9507372f0b1b963James Dong    mbPos = currImage->data + width * height * 256 +
4570c1bc742181ded4930842b46e9507372f0b1b963James Dong       row * 8 * width * 8 + col * 8;
4580c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (comp = 0; comp < 2; comp++)
4590c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
4600c1bc742181ded4930842b46e9507372f0b1b963James Dong
4610c1bc742181ded4930842b46e9507372f0b1b963James Dong        H264SwDecMemset(firstPhase, 0, sizeof(firstPhase));
4620c1bc742181ded4930842b46e9507372f0b1b963James Dong
4630c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* counter for number of neighbours used */
4640c1bc742181ded4930842b46e9507372f0b1b963James Dong        j = 0;
4650c1bc742181ded4930842b46e9507372f0b1b963James Dong        hor = ver = 0;
4660c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (A)
4670c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
4680c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData = mbPos - width*8;
4690c1bc742181ded4930842b46e9507372f0b1b963James Dong            a[0] = *pData++; a[0] += *pData++;
4700c1bc742181ded4930842b46e9507372f0b1b963James Dong            a[1] = *pData++; a[1] += *pData++;
4710c1bc742181ded4930842b46e9507372f0b1b963James Dong            a[2] = *pData++; a[2] += *pData++;
4720c1bc742181ded4930842b46e9507372f0b1b963James Dong            a[3] = *pData++; a[3] += *pData++;
4730c1bc742181ded4930842b46e9507372f0b1b963James Dong            j++;
4740c1bc742181ded4930842b46e9507372f0b1b963James Dong            hor++;
4750c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] += a[0] + a[1] + a[2] + a[3];
4760c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[1] += a[0] + a[1] - a[2] - a[3];
4770c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
4780c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (B)
4790c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
4800c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData = mbPos + 8*width*8;
4810c1bc742181ded4930842b46e9507372f0b1b963James Dong            b[0] = *pData++; b[0] += *pData++;
4820c1bc742181ded4930842b46e9507372f0b1b963James Dong            b[1] = *pData++; b[1] += *pData++;
4830c1bc742181ded4930842b46e9507372f0b1b963James Dong            b[2] = *pData++; b[2] += *pData++;
4840c1bc742181ded4930842b46e9507372f0b1b963James Dong            b[3] = *pData++; b[3] += *pData++;
4850c1bc742181ded4930842b46e9507372f0b1b963James Dong            j++;
4860c1bc742181ded4930842b46e9507372f0b1b963James Dong            hor++;
4870c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] += b[0] + b[1] + b[2] + b[3];
4880c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[1] += b[0] + b[1] - b[2] - b[3];
4890c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
4900c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (L)
4910c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
4920c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData = mbPos - 1;
4930c1bc742181ded4930842b46e9507372f0b1b963James Dong            l[0] = pData[0]; l[0] += pData[8*width];
4940c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData += 16*width;
4950c1bc742181ded4930842b46e9507372f0b1b963James Dong            l[1] = pData[0]; l[1] += pData[8*width];
4960c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData += 16*width;
4970c1bc742181ded4930842b46e9507372f0b1b963James Dong            l[2] = pData[0]; l[2] += pData[8*width];
4980c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData += 16*width;
4990c1bc742181ded4930842b46e9507372f0b1b963James Dong            l[3] = pData[0]; l[3] += pData[8*width];
5000c1bc742181ded4930842b46e9507372f0b1b963James Dong            j++;
5010c1bc742181ded4930842b46e9507372f0b1b963James Dong            ver++;
5020c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] += l[0] + l[1] + l[2] + l[3];
5030c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[4] += l[0] + l[1] - l[2] - l[3];
5040c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
5050c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (R)
5060c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
5070c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData = mbPos + 8;
5080c1bc742181ded4930842b46e9507372f0b1b963James Dong            r[0] = pData[0]; r[0] += pData[8*width];
5090c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData += 16*width;
5100c1bc742181ded4930842b46e9507372f0b1b963James Dong            r[1] = pData[0]; r[1] += pData[8*width];
5110c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData += 16*width;
5120c1bc742181ded4930842b46e9507372f0b1b963James Dong            r[2] = pData[0]; r[2] += pData[8*width];
5130c1bc742181ded4930842b46e9507372f0b1b963James Dong            pData += 16*width;
5140c1bc742181ded4930842b46e9507372f0b1b963James Dong            r[3] = pData[0]; r[3] += pData[8*width];
5150c1bc742181ded4930842b46e9507372f0b1b963James Dong            j++;
5160c1bc742181ded4930842b46e9507372f0b1b963James Dong            ver++;
5170c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[0] += r[0] + r[1] + r[2] + r[3];
5180c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[4] += r[0] + r[1] - r[2] - r[3];
5190c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
5200c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (!hor && L && R)
5210c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[1] = (l[0]+l[1]+l[2]+l[3]-r[0]-r[1]-r[2]-r[3]) >> 4;
5220c1bc742181ded4930842b46e9507372f0b1b963James Dong        else if (hor)
5230c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[1] >>= (2+hor);
5240c1bc742181ded4930842b46e9507372f0b1b963James Dong
5250c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (!ver && A && B)
5260c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[4] = (a[0]+a[1]+a[2]+a[3]-b[0]-b[1]-b[2]-b[3]) >> 4;
5270c1bc742181ded4930842b46e9507372f0b1b963James Dong        else if (ver)
5280c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstPhase[4] >>= (2+ver);
5290c1bc742181ded4930842b46e9507372f0b1b963James Dong
5300c1bc742181ded4930842b46e9507372f0b1b963James Dong        switch (j)
5310c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
5320c1bc742181ded4930842b46e9507372f0b1b963James Dong            case 1:
5330c1bc742181ded4930842b46e9507372f0b1b963James Dong                firstPhase[0] >>= 3;
5340c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
5350c1bc742181ded4930842b46e9507372f0b1b963James Dong
5360c1bc742181ded4930842b46e9507372f0b1b963James Dong            case 2:
5370c1bc742181ded4930842b46e9507372f0b1b963James Dong                firstPhase[0] >>= 4;
5380c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
5390c1bc742181ded4930842b46e9507372f0b1b963James Dong
5400c1bc742181ded4930842b46e9507372f0b1b963James Dong            case 3:
5410c1bc742181ded4930842b46e9507372f0b1b963James Dong                /* approximate (firstPhase[0]*4/3)>>5 */
5420c1bc742181ded4930842b46e9507372f0b1b963James Dong                firstPhase[0] = (21 * firstPhase[0]) >> 9;
5430c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
5440c1bc742181ded4930842b46e9507372f0b1b963James Dong
5450c1bc742181ded4930842b46e9507372f0b1b963James Dong            default: /* 4 */
5460c1bc742181ded4930842b46e9507372f0b1b963James Dong                firstPhase[0] >>= 5;
5470c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
5480c1bc742181ded4930842b46e9507372f0b1b963James Dong
5490c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
5500c1bc742181ded4930842b46e9507372f0b1b963James Dong
5510c1bc742181ded4930842b46e9507372f0b1b963James Dong        Transform(firstPhase);
5520c1bc742181ded4930842b46e9507372f0b1b963James Dong
5530c1bc742181ded4930842b46e9507372f0b1b963James Dong        pData = data + 256 + comp*64;
5540c1bc742181ded4930842b46e9507372f0b1b963James Dong        for (i = 0, pTmp = firstPhase; i < 64;)
5550c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
5560c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = pTmp[(i & 0x7)>>1];
5570c1bc742181ded4930842b46e9507372f0b1b963James Dong            /*lint -e734 CLIP1 macro results in value that fits into 8 bits */
5580c1bc742181ded4930842b46e9507372f0b1b963James Dong            *pData++ = CLIP1(tmp);
5590c1bc742181ded4930842b46e9507372f0b1b963James Dong            /*lint +e734 */
5600c1bc742181ded4930842b46e9507372f0b1b963James Dong
5610c1bc742181ded4930842b46e9507372f0b1b963James Dong            i++;
5620c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (!(i & 0xF))
5630c1bc742181ded4930842b46e9507372f0b1b963James Dong                pTmp += 4;
5640c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
5650c1bc742181ded4930842b46e9507372f0b1b963James Dong
5660c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* increment pointers for cr */
5670c1bc742181ded4930842b46e9507372f0b1b963James Dong        mbPos += width * height * 64;
5680c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5690c1bc742181ded4930842b46e9507372f0b1b963James Dong
5700c1bc742181ded4930842b46e9507372f0b1b963James Dong    h264bsdWriteMacroblock(currImage, data);
5710c1bc742181ded4930842b46e9507372f0b1b963James Dong
5720c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(HANTRO_OK);
5730c1bc742181ded4930842b46e9507372f0b1b963James Dong
5740c1bc742181ded4930842b46e9507372f0b1b963James Dong}
5750c1bc742181ded4930842b46e9507372f0b1b963James Dong
5760c1bc742181ded4930842b46e9507372f0b1b963James Dong
5770c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
5780c1bc742181ded4930842b46e9507372f0b1b963James Dong
5790c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function name: Transform
5800c1bc742181ded4930842b46e9507372f0b1b963James Dong
5810c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
5820c1bc742181ded4930842b46e9507372f0b1b963James Dong            Simplified transform, assuming that only dc component and lowest
5830c1bc742181ded4930842b46e9507372f0b1b963James Dong            horizontal and lowest vertical component may be non-zero
5840c1bc742181ded4930842b46e9507372f0b1b963James Dong
5850c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
5860c1bc742181ded4930842b46e9507372f0b1b963James Dong
5870c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid Transform(i32 *data)
5880c1bc742181ded4930842b46e9507372f0b1b963James Dong{
5890c1bc742181ded4930842b46e9507372f0b1b963James Dong
5900c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 col;
5910c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 tmp0, tmp1;
5920c1bc742181ded4930842b46e9507372f0b1b963James Dong
5930c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (!data[1] && !data[4])
5940c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5950c1bc742181ded4930842b46e9507372f0b1b963James Dong        data[1]  = data[2]  = data[3]  = data[4]  = data[5]  =
5960c1bc742181ded4930842b46e9507372f0b1b963James Dong        data[6]  = data[7]  = data[8]  = data[9]  = data[10] =
5970c1bc742181ded4930842b46e9507372f0b1b963James Dong        data[11] = data[12] = data[13] = data[14] = data[15] = data[0];
5980c1bc742181ded4930842b46e9507372f0b1b963James Dong        return;
5990c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
6000c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* first horizontal transform for rows 0 and 1 */
6010c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp0 = data[0];
6020c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp1 = data[1];
6030c1bc742181ded4930842b46e9507372f0b1b963James Dong    data[0] = tmp0 + tmp1;
6040c1bc742181ded4930842b46e9507372f0b1b963James Dong    data[1] = tmp0 + (tmp1>>1);
6050c1bc742181ded4930842b46e9507372f0b1b963James Dong    data[2] = tmp0 - (tmp1>>1);
6060c1bc742181ded4930842b46e9507372f0b1b963James Dong    data[3] = tmp0 - tmp1;
6070c1bc742181ded4930842b46e9507372f0b1b963James Dong
6080c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp0 = data[4];
6090c1bc742181ded4930842b46e9507372f0b1b963James Dong    data[5] = tmp0;
6100c1bc742181ded4930842b46e9507372f0b1b963James Dong    data[6] = tmp0;
6110c1bc742181ded4930842b46e9507372f0b1b963James Dong    data[7] = tmp0;
6120c1bc742181ded4930842b46e9507372f0b1b963James Dong
6130c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* then vertical transform */
6140c1bc742181ded4930842b46e9507372f0b1b963James Dong    for (col = 4; col--; data++)
6150c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
6160c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp0 = data[0];
6170c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp1 = data[4];
6180c1bc742181ded4930842b46e9507372f0b1b963James Dong        data[0] = tmp0 + tmp1;
6190c1bc742181ded4930842b46e9507372f0b1b963James Dong        data[4] = tmp0 + (tmp1>>1);
6200c1bc742181ded4930842b46e9507372f0b1b963James Dong        data[8] = tmp0 - (tmp1>>1);
6210c1bc742181ded4930842b46e9507372f0b1b963James Dong        data[12] = tmp0 - tmp1;
6220c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
6230c1bc742181ded4930842b46e9507372f0b1b963James Dong
6240c1bc742181ded4930842b46e9507372f0b1b963James Dong}
6250c1bc742181ded4930842b46e9507372f0b1b963James Dong/*lint +e702 */
6260c1bc742181ded4930842b46e9507372f0b1b963James Dong
627