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          h264bsdDecodeSliceData
270c1bc742181ded4930842b46e9507372f0b1b963James Dong          SetMbParams
280c1bc742181ded4930842b46e9507372f0b1b963James Dong          h264bsdMarkSliceCorrupted
290c1bc742181ded4930842b46e9507372f0b1b963James Dong
300c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
310c1bc742181ded4930842b46e9507372f0b1b963James Dong
320c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
330c1bc742181ded4930842b46e9507372f0b1b963James Dong    1. Include headers
340c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
350c1bc742181ded4930842b46e9507372f0b1b963James Dong
360c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_slice_data.h"
370c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_util.h"
380c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_vlc.h"
390c1bc742181ded4930842b46e9507372f0b1b963James Dong
400c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
410c1bc742181ded4930842b46e9507372f0b1b963James Dong    2. External compiler flags
420c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
430c1bc742181ded4930842b46e9507372f0b1b963James Dong
440c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
450c1bc742181ded4930842b46e9507372f0b1b963James Dong    3. Module defines
460c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
470c1bc742181ded4930842b46e9507372f0b1b963James Dong
480c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
490c1bc742181ded4930842b46e9507372f0b1b963James Dong    4. Local function prototypes
500c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
510c1bc742181ded4930842b46e9507372f0b1b963James Dong
520c1bc742181ded4930842b46e9507372f0b1b963James Dongstatic void SetMbParams(mbStorage_t *pMb, sliceHeader_t *pSlice, u32 sliceId,
530c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 chromaQpIndexOffset);
540c1bc742181ded4930842b46e9507372f0b1b963James Dong
550c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
560c1bc742181ded4930842b46e9507372f0b1b963James Dong
570c1bc742181ded4930842b46e9507372f0b1b963James Dong   5.1  Function name: h264bsdDecodeSliceData
580c1bc742181ded4930842b46e9507372f0b1b963James Dong
590c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
600c1bc742181ded4930842b46e9507372f0b1b963James Dong            Decode one slice. Function decodes stream data, i.e. macroblocks
610c1bc742181ded4930842b46e9507372f0b1b963James Dong            and possible skip_run fields. h264bsdDecodeMacroblock function is
620c1bc742181ded4930842b46e9507372f0b1b963James Dong            called to handle all other macroblock related processing.
630c1bc742181ded4930842b46e9507372f0b1b963James Dong            Macroblock to slice group mapping is considered when next
640c1bc742181ded4930842b46e9507372f0b1b963James Dong            macroblock to process is determined (h264bsdNextMbAddress function)
650c1bc742181ded4930842b46e9507372f0b1b963James Dong            map
660c1bc742181ded4930842b46e9507372f0b1b963James Dong
670c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
680c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStrmData       pointer to stream data structure
690c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage        pointer to storage structure
700c1bc742181ded4930842b46e9507372f0b1b963James Dong            currImage       pointer to current processed picture, needed for
710c1bc742181ded4930842b46e9507372f0b1b963James Dong                            intra prediction of the macroblocks
720c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSliceHeader    pointer to slice header of the current slice
730c1bc742181ded4930842b46e9507372f0b1b963James Dong
740c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
750c1bc742181ded4930842b46e9507372f0b1b963James Dong            currImage       processed macroblocks are written to current image
760c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage        mbStorage structure of each processed macroblock
770c1bc742181ded4930842b46e9507372f0b1b963James Dong                            is updated here
780c1bc742181ded4930842b46e9507372f0b1b963James Dong
790c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
800c1bc742181ded4930842b46e9507372f0b1b963James Dong            HANTRO_OK       success
810c1bc742181ded4930842b46e9507372f0b1b963James Dong            HANTRO_NOK      invalid stream data
820c1bc742181ded4930842b46e9507372f0b1b963James Dong
830c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
840c1bc742181ded4930842b46e9507372f0b1b963James Dong
850c1bc742181ded4930842b46e9507372f0b1b963James Dongu32 h264bsdDecodeSliceData(strmData_t *pStrmData, storage_t *pStorage,
860c1bc742181ded4930842b46e9507372f0b1b963James Dong    image_t *currImage, sliceHeader_t *pSliceHeader)
870c1bc742181ded4930842b46e9507372f0b1b963James Dong{
880c1bc742181ded4930842b46e9507372f0b1b963James Dong
890c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
900c1bc742181ded4930842b46e9507372f0b1b963James Dong
910c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 mbData[384 + 15 + 32];
920c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 *data;
930c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 tmp;
940c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 skipRun;
950c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 prevSkipped;
960c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 currMbAddr;
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 moreMbs;
980c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 mbCount;
990c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 qpY;
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong    macroblockLayer_t *mbLayer;
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pStrmData);
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pSliceHeader);
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pStorage);
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pSliceHeader->firstMbInSlice < pStorage->picSizeInMbs);
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* ensure 16-byte alignment */
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong    data = (u8*)ALIGN(mbData, 16);
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong    mbLayer = pStorage->mbLayer;
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong    currMbAddr = pSliceHeader->firstMbInSlice;
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong    skipRun = 0;
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong    prevSkipped = HANTRO_FALSE;
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* increment slice index, will be one for decoding of the first slice of
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong     * the picture */
1200c1bc742181ded4930842b46e9507372f0b1b963James Dong    pStorage->slice->sliceId++;
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* lastMbAddr stores address of the macroblock that was last successfully
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong     * decoded, needed for error handling */
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong    pStorage->slice->lastMbAddr = 0;
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong    mbCount = 0;
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* initial quantization parameter for the slice is obtained as the sum of
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong     * initial QP for the picture and sliceQpDelta for the current slice */
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong    qpY = (i32)pStorage->activePps->picInitQp + pSliceHeader->sliceQpDelta;
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong    do
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* primary picture and already decoded macroblock -> error */
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (!pSliceHeader->redundantPicCnt && pStorage->mb[currMbAddr].decoded)
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong            EPRINT("Primary and already decoded");
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(HANTRO_NOK);
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong        SetMbParams(pStorage->mb + currMbAddr, pSliceHeader,
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->slice->sliceId, pStorage->activePps->chromaQpIndexOffset);
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (!IS_I_SLICE(pSliceHeader->sliceType))
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (!prevSkipped)
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong                tmp = h264bsdDecodeExpGolombUnsigned(pStrmData, &skipRun);
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (tmp != HANTRO_OK)
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong                    return(tmp);
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong                /* skip_run shall be less than or equal to number of
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong                 * macroblocks left */
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (skipRun > (pStorage->picSizeInMbs - currMbAddr))
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong                    EPRINT("skip_run");
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong                    return(HANTRO_NOK);
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (skipRun)
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong                    prevSkipped = HANTRO_TRUE;
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong                    H264SwDecMemset(&mbLayer->mbPred, 0, sizeof(mbPred_t));
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong                    /* mark current macroblock skipped */
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong                    mbLayer->mbType = P_Skip;
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (skipRun)
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong            DEBUG(("Skipping macroblock %d\n", currMbAddr));
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong            skipRun--;
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong        else
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong            prevSkipped = HANTRO_FALSE;
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong            tmp = h264bsdDecodeMacroblockLayer(pStrmData, mbLayer,
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong                pStorage->mb + currMbAddr, pSliceHeader->sliceType,
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong                pSliceHeader->numRefIdxL0Active);
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (tmp != HANTRO_OK)
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong                EPRINT("macroblock_layer");
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong                return(tmp);
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = h264bsdDecodeMacroblock(pStorage->mb + currMbAddr, mbLayer,
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong            currImage, pStorage->dpb, &qpY, currMbAddr,
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->activePps->constrainedIntraPredFlag, data);
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (tmp != HANTRO_OK)
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong            EPRINT("MACRO_BLOCK");
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(tmp);
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* increment macroblock count only for macroblocks that were decoded
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong         * for the first time (redundant slices) */
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pStorage->mb[currMbAddr].decoded == 1)
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong            mbCount++;
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* keep on processing as long as there is stream data left or
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong         * processing of macroblocks to be skipped based on the last skipRun is
2000c1bc742181ded4930842b46e9507372f0b1b963James Dong         * not finished */
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong        moreMbs = (h264bsdMoreRbspData(pStrmData) || skipRun) ?
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong                                        HANTRO_TRUE : HANTRO_FALSE;
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* lastMbAddr is only updated for intra slices (all macroblocks of
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong         * inter slices will be lost in case of an error) */
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (IS_I_SLICE(pSliceHeader->sliceType))
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->slice->lastMbAddr = currMbAddr;
2080c1bc742181ded4930842b46e9507372f0b1b963James Dong
2090c1bc742181ded4930842b46e9507372f0b1b963James Dong        currMbAddr = h264bsdNextMbAddress(pStorage->sliceGroupMap,
2100c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->picSizeInMbs, currMbAddr);
2110c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* data left in the buffer but no more macroblocks for current slice
2120c1bc742181ded4930842b46e9507372f0b1b963James Dong         * group -> error */
2130c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (moreMbs && !currMbAddr)
2140c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
2150c1bc742181ded4930842b46e9507372f0b1b963James Dong            EPRINT("Next mb address");
2160c1bc742181ded4930842b46e9507372f0b1b963James Dong            return(HANTRO_NOK);
2170c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
2180c1bc742181ded4930842b46e9507372f0b1b963James Dong
2190c1bc742181ded4930842b46e9507372f0b1b963James Dong    } while (moreMbs);
2200c1bc742181ded4930842b46e9507372f0b1b963James Dong
2210c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((pStorage->slice->numDecodedMbs + mbCount) > pStorage->picSizeInMbs)
2220c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2230c1bc742181ded4930842b46e9507372f0b1b963James Dong        EPRINT("Num decoded mbs");
2240c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(HANTRO_NOK);
2250c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2260c1bc742181ded4930842b46e9507372f0b1b963James Dong
2270c1bc742181ded4930842b46e9507372f0b1b963James Dong    pStorage->slice->numDecodedMbs += mbCount;
2280c1bc742181ded4930842b46e9507372f0b1b963James Dong
2290c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(HANTRO_OK);
2300c1bc742181ded4930842b46e9507372f0b1b963James Dong
2310c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2320c1bc742181ded4930842b46e9507372f0b1b963James Dong
2330c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
2340c1bc742181ded4930842b46e9507372f0b1b963James Dong
2350c1bc742181ded4930842b46e9507372f0b1b963James Dong   5.2  Function: SetMbParams
2360c1bc742181ded4930842b46e9507372f0b1b963James Dong
2370c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
2380c1bc742181ded4930842b46e9507372f0b1b963James Dong            Set macroblock parameters that remain constant for this slice
2390c1bc742181ded4930842b46e9507372f0b1b963James Dong
2400c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
2410c1bc742181ded4930842b46e9507372f0b1b963James Dong            pSlice      pointer to current slice header
2420c1bc742181ded4930842b46e9507372f0b1b963James Dong            sliceId     id of the current slice
2430c1bc742181ded4930842b46e9507372f0b1b963James Dong            chromaQpIndexOffset
2440c1bc742181ded4930842b46e9507372f0b1b963James Dong
2450c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
2460c1bc742181ded4930842b46e9507372f0b1b963James Dong            pMb         pointer to macroblock structure which is updated
2470c1bc742181ded4930842b46e9507372f0b1b963James Dong
2480c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
2490c1bc742181ded4930842b46e9507372f0b1b963James Dong            none
2500c1bc742181ded4930842b46e9507372f0b1b963James Dong
2510c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
2520c1bc742181ded4930842b46e9507372f0b1b963James Dong
2530c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid SetMbParams(mbStorage_t *pMb, sliceHeader_t *pSlice, u32 sliceId,
2540c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 chromaQpIndexOffset)
2550c1bc742181ded4930842b46e9507372f0b1b963James Dong{
2560c1bc742181ded4930842b46e9507372f0b1b963James Dong
2570c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
2580c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 tmp1;
2590c1bc742181ded4930842b46e9507372f0b1b963James Dong    i32 tmp2, tmp3;
2600c1bc742181ded4930842b46e9507372f0b1b963James Dong
2610c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
2620c1bc742181ded4930842b46e9507372f0b1b963James Dong
2630c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp1 = pSlice->disableDeblockingFilterIdc;
2640c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp2 = pSlice->sliceAlphaC0Offset;
2650c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmp3 = pSlice->sliceBetaOffset;
2660c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->sliceId = sliceId;
2670c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->disableDeblockingFilterIdc = tmp1;
2680c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->filterOffsetA = tmp2;
2690c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->filterOffsetB = tmp3;
2700c1bc742181ded4930842b46e9507372f0b1b963James Dong    pMb->chromaQpIndexOffset = chromaQpIndexOffset;
2710c1bc742181ded4930842b46e9507372f0b1b963James Dong
2720c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2730c1bc742181ded4930842b46e9507372f0b1b963James Dong
2740c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
2750c1bc742181ded4930842b46e9507372f0b1b963James Dong
2760c1bc742181ded4930842b46e9507372f0b1b963James Dong   5.3  Function name: h264bsdMarkSliceCorrupted
2770c1bc742181ded4930842b46e9507372f0b1b963James Dong
2780c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
2790c1bc742181ded4930842b46e9507372f0b1b963James Dong            Mark macroblocks of the slice corrupted. If lastMbAddr in the slice
2800c1bc742181ded4930842b46e9507372f0b1b963James Dong            storage is set -> picWidhtInMbs (or at least 10) macroblocks back
2810c1bc742181ded4930842b46e9507372f0b1b963James Dong            from  the lastMbAddr are marked corrupted. However, if lastMbAddr
2820c1bc742181ded4930842b46e9507372f0b1b963James Dong            is not set -> all macroblocks of the slice are marked.
2830c1bc742181ded4930842b46e9507372f0b1b963James Dong
2840c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
2850c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage        pointer to storage structure
2860c1bc742181ded4930842b46e9507372f0b1b963James Dong            firstMbInSlice  address of the first macroblock in the slice, this
2870c1bc742181ded4930842b46e9507372f0b1b963James Dong                            identifies the slice to be marked corrupted
2880c1bc742181ded4930842b46e9507372f0b1b963James Dong
2890c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
2900c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage        mbStorage for the corrupted macroblocks updated
2910c1bc742181ded4930842b46e9507372f0b1b963James Dong
2920c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
2930c1bc742181ded4930842b46e9507372f0b1b963James Dong            none
2940c1bc742181ded4930842b46e9507372f0b1b963James Dong
2950c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
2960c1bc742181ded4930842b46e9507372f0b1b963James Dong
2970c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid h264bsdMarkSliceCorrupted(storage_t *pStorage, u32 firstMbInSlice)
2980c1bc742181ded4930842b46e9507372f0b1b963James Dong{
2990c1bc742181ded4930842b46e9507372f0b1b963James Dong
3000c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Variables */
3010c1bc742181ded4930842b46e9507372f0b1b963James Dong
3020c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 tmp, i;
3030c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 sliceId;
3040c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 currMbAddr;
3050c1bc742181ded4930842b46e9507372f0b1b963James Dong
3060c1bc742181ded4930842b46e9507372f0b1b963James Dong/* Code */
3070c1bc742181ded4930842b46e9507372f0b1b963James Dong
3080c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(pStorage);
3090c1bc742181ded4930842b46e9507372f0b1b963James Dong    ASSERT(firstMbInSlice < pStorage->picSizeInMbs);
3100c1bc742181ded4930842b46e9507372f0b1b963James Dong
3110c1bc742181ded4930842b46e9507372f0b1b963James Dong    currMbAddr = firstMbInSlice;
3120c1bc742181ded4930842b46e9507372f0b1b963James Dong
3130c1bc742181ded4930842b46e9507372f0b1b963James Dong    sliceId = pStorage->slice->sliceId;
3140c1bc742181ded4930842b46e9507372f0b1b963James Dong
3150c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* DecodeSliceData sets lastMbAddr for I slices -> if it was set, go back
3160c1bc742181ded4930842b46e9507372f0b1b963James Dong     * MAX(picWidthInMbs, 10) macroblocks and start marking from there */
3170c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pStorage->slice->lastMbAddr)
3180c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3190c1bc742181ded4930842b46e9507372f0b1b963James Dong        ASSERT(pStorage->mb[pStorage->slice->lastMbAddr].sliceId == sliceId);
3200c1bc742181ded4930842b46e9507372f0b1b963James Dong        i = pStorage->slice->lastMbAddr - 1;
3210c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmp = 0;
3220c1bc742181ded4930842b46e9507372f0b1b963James Dong        while (i > currMbAddr)
3230c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3240c1bc742181ded4930842b46e9507372f0b1b963James Dong            if (pStorage->mb[i].sliceId == sliceId)
3250c1bc742181ded4930842b46e9507372f0b1b963James Dong            {
3260c1bc742181ded4930842b46e9507372f0b1b963James Dong                tmp++;
3270c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (tmp >= MAX(pStorage->activeSps->picWidthInMbs, 10))
3280c1bc742181ded4930842b46e9507372f0b1b963James Dong                    break;
3290c1bc742181ded4930842b46e9507372f0b1b963James Dong            }
3300c1bc742181ded4930842b46e9507372f0b1b963James Dong            i--;
3310c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3320c1bc742181ded4930842b46e9507372f0b1b963James Dong        currMbAddr = i;
3330c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3340c1bc742181ded4930842b46e9507372f0b1b963James Dong
3350c1bc742181ded4930842b46e9507372f0b1b963James Dong    do
3360c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3370c1bc742181ded4930842b46e9507372f0b1b963James Dong
3380c1bc742181ded4930842b46e9507372f0b1b963James Dong        if ( (pStorage->mb[currMbAddr].sliceId == sliceId) &&
3390c1bc742181ded4930842b46e9507372f0b1b963James Dong             (pStorage->mb[currMbAddr].decoded) )
3400c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3410c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->mb[currMbAddr].decoded--;
3420c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3430c1bc742181ded4930842b46e9507372f0b1b963James Dong        else
3440c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3450c1bc742181ded4930842b46e9507372f0b1b963James Dong            break;
3460c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3470c1bc742181ded4930842b46e9507372f0b1b963James Dong
3480c1bc742181ded4930842b46e9507372f0b1b963James Dong        currMbAddr = h264bsdNextMbAddress(pStorage->sliceGroupMap,
3490c1bc742181ded4930842b46e9507372f0b1b963James Dong            pStorage->picSizeInMbs, currMbAddr);
3500c1bc742181ded4930842b46e9507372f0b1b963James Dong
3510c1bc742181ded4930842b46e9507372f0b1b963James Dong    } while (currMbAddr);
3520c1bc742181ded4930842b46e9507372f0b1b963James Dong
3530c1bc742181ded4930842b46e9507372f0b1b963James Dong}
3540c1bc742181ded4930842b46e9507372f0b1b963James Dong
355