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          H264SwDecInit
270c1bc742181ded4930842b46e9507372f0b1b963James Dong          H264SwDecGetInfo
280c1bc742181ded4930842b46e9507372f0b1b963James Dong          H264SwDecRelease
290c1bc742181ded4930842b46e9507372f0b1b963James Dong          H264SwDecDecode
300c1bc742181ded4930842b46e9507372f0b1b963James Dong          H264SwDecGetAPIVersion
310c1bc742181ded4930842b46e9507372f0b1b963James Dong          H264SwDecNextPicture
320c1bc742181ded4930842b46e9507372f0b1b963James Dong
330c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
340c1bc742181ded4930842b46e9507372f0b1b963James Dong
350c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
360c1bc742181ded4930842b46e9507372f0b1b963James Dong    1. Include headers
370c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
380c1bc742181ded4930842b46e9507372f0b1b963James Dong#include <stdlib.h>
390c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "basetype.h"
400c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_container.h"
410c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "H264SwDecApi.h"
420c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_decoder.h"
430c1bc742181ded4930842b46e9507372f0b1b963James Dong#include "h264bsd_util.h"
440c1bc742181ded4930842b46e9507372f0b1b963James Dong
450c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
460c1bc742181ded4930842b46e9507372f0b1b963James Dong       Version Information
470c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
480c1bc742181ded4930842b46e9507372f0b1b963James Dong
490c1bc742181ded4930842b46e9507372f0b1b963James Dong#define H264SWDEC_MAJOR_VERSION 2
500c1bc742181ded4930842b46e9507372f0b1b963James Dong#define H264SWDEC_MINOR_VERSION 3
510c1bc742181ded4930842b46e9507372f0b1b963James Dong
520c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
530c1bc742181ded4930842b46e9507372f0b1b963James Dong    2. External compiler flags
540c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
550c1bc742181ded4930842b46e9507372f0b1b963James Dong
560c1bc742181ded4930842b46e9507372f0b1b963James DongH264DEC_TRACE           Trace H264 Decoder API function calls.
570c1bc742181ded4930842b46e9507372f0b1b963James DongH264DEC_EVALUATION      Compile evaluation version, restricts number of frames
580c1bc742181ded4930842b46e9507372f0b1b963James Dong                        that can be decoded
590c1bc742181ded4930842b46e9507372f0b1b963James Dong
600c1bc742181ded4930842b46e9507372f0b1b963James Dong--------------------------------------------------------------------------------
610c1bc742181ded4930842b46e9507372f0b1b963James Dong    3. Module defines
620c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
630c1bc742181ded4930842b46e9507372f0b1b963James Dong
640c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
650c1bc742181ded4930842b46e9507372f0b1b963James Dong#include <stdio.h>
660c1bc742181ded4930842b46e9507372f0b1b963James Dong#define DEC_API_TRC(str)    H264SwDecTrace(str)
670c1bc742181ded4930842b46e9507372f0b1b963James Dong#else
680c1bc742181ded4930842b46e9507372f0b1b963James Dong#define DEC_API_TRC(str)
690c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
700c1bc742181ded4930842b46e9507372f0b1b963James Dong
710c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_EVALUATION
720c1bc742181ded4930842b46e9507372f0b1b963James Dong#define H264DEC_EVALUATION_LIMIT   500
730c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
740c1bc742181ded4930842b46e9507372f0b1b963James Dong
750c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid H264SwDecTrace(char *string) {
760c1bc742181ded4930842b46e9507372f0b1b963James Dong}
770c1bc742181ded4930842b46e9507372f0b1b963James Dong
780c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid* H264SwDecMalloc(u32 size) {
790c1bc742181ded4930842b46e9507372f0b1b963James Dong    return malloc(size);
800c1bc742181ded4930842b46e9507372f0b1b963James Dong}
810c1bc742181ded4930842b46e9507372f0b1b963James Dong
820c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid H264SwDecFree(void *ptr) {
830c1bc742181ded4930842b46e9507372f0b1b963James Dong    free(ptr);
840c1bc742181ded4930842b46e9507372f0b1b963James Dong}
850c1bc742181ded4930842b46e9507372f0b1b963James Dong
860c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid H264SwDecMemcpy(void *dest, void *src, u32 count) {
870c1bc742181ded4930842b46e9507372f0b1b963James Dong    memcpy(dest, src, count);
880c1bc742181ded4930842b46e9507372f0b1b963James Dong}
890c1bc742181ded4930842b46e9507372f0b1b963James Dong
900c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid H264SwDecMemset(void *ptr, i32 value, u32 count) {
910c1bc742181ded4930842b46e9507372f0b1b963James Dong    memset(ptr, value, count);
920c1bc742181ded4930842b46e9507372f0b1b963James Dong}
930c1bc742181ded4930842b46e9507372f0b1b963James Dong
940c1bc742181ded4930842b46e9507372f0b1b963James Dong
950c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
960c1bc742181ded4930842b46e9507372f0b1b963James Dong
970c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function: H264SwDecInit()
980c1bc742181ded4930842b46e9507372f0b1b963James Dong
990c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
1000c1bc742181ded4930842b46e9507372f0b1b963James Dong            Initialize decoder software. Function reserves memory for the
1010c1bc742181ded4930842b46e9507372f0b1b963James Dong            decoder instance and calls h264bsdInit to initialize the
1020c1bc742181ded4930842b46e9507372f0b1b963James Dong            instance data.
1030c1bc742181ded4930842b46e9507372f0b1b963James Dong
1040c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
1050c1bc742181ded4930842b46e9507372f0b1b963James Dong            noOutputReordering  flag to indicate decoder that it doesn't have
1060c1bc742181ded4930842b46e9507372f0b1b963James Dong                                to try to provide output pictures in display
1070c1bc742181ded4930842b46e9507372f0b1b963James Dong                                order, saves memory
1080c1bc742181ded4930842b46e9507372f0b1b963James Dong
1090c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
1100c1bc742181ded4930842b46e9507372f0b1b963James Dong            decInst             pointer to initialized instance is stored here
1110c1bc742181ded4930842b46e9507372f0b1b963James Dong
1120c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
1130c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_OK        successfully initialized the instance
1140c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_INITFAIL  initialization failed
1150c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_PARAM_ERR invalid parameters
1160c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_MEM_FAIL  memory allocation failed
1170c1bc742181ded4930842b46e9507372f0b1b963James Dong
1180c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
1190c1bc742181ded4930842b46e9507372f0b1b963James Dong
1200c1bc742181ded4930842b46e9507372f0b1b963James DongH264SwDecRet H264SwDecInit(H264SwDecInst *decInst, u32 noOutputReordering)
1210c1bc742181ded4930842b46e9507372f0b1b963James Dong{
1220c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 rv = 0;
1230c1bc742181ded4930842b46e9507372f0b1b963James Dong
1240c1bc742181ded4930842b46e9507372f0b1b963James Dong    decContainer_t *pDecCont;
1250c1bc742181ded4930842b46e9507372f0b1b963James Dong
1260c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC("H264SwDecInit#");
1270c1bc742181ded4930842b46e9507372f0b1b963James Dong
1280c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* check that right shift on negative numbers is performed signed */
1290c1bc742181ded4930842b46e9507372f0b1b963James Dong    /*lint -save -e* following check causes multiple lint messages */
1300c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ( ((-1)>>1) != (-1) )
1310c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1320c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecInit# ERROR: Right shift is not signed");
1330c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_INITFAIL);
1340c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1350c1bc742181ded4930842b46e9507372f0b1b963James Dong    /*lint -restore */
1360c1bc742181ded4930842b46e9507372f0b1b963James Dong
1370c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (decInst == NULL)
1380c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1390c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecInit# ERROR: decInst == NULL");
1400c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_PARAM_ERR);
1410c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1420c1bc742181ded4930842b46e9507372f0b1b963James Dong
1430c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecCont = (decContainer_t *)H264SwDecMalloc(sizeof(decContainer_t));
1440c1bc742181ded4930842b46e9507372f0b1b963James Dong
1450c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pDecCont == NULL)
1460c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1470c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecInit# ERROR: Memory allocation failed");
1480c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_MEMFAIL);
1490c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1500c1bc742181ded4930842b46e9507372f0b1b963James Dong
1510c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
1520c1bc742181ded4930842b46e9507372f0b1b963James Dong    sprintf(pDecCont->str, "H264SwDecInit# decInst %p noOutputReordering %d",
1530c1bc742181ded4930842b46e9507372f0b1b963James Dong            (void*)decInst, noOutputReordering);
1540c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC(pDecCont->str);
1550c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1560c1bc742181ded4930842b46e9507372f0b1b963James Dong
1570c1bc742181ded4930842b46e9507372f0b1b963James Dong    rv = h264bsdInit(&pDecCont->storage, noOutputReordering);
1580c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (rv != HANTRO_OK)
1590c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
1600c1bc742181ded4930842b46e9507372f0b1b963James Dong        H264SwDecRelease(pDecCont);
1610c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_MEMFAIL);
1620c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
1630c1bc742181ded4930842b46e9507372f0b1b963James Dong
1640c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecCont->decStat  = INITIALIZED;
1650c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecCont->picNumber = 0;
1660c1bc742181ded4930842b46e9507372f0b1b963James Dong
1670c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
1680c1bc742181ded4930842b46e9507372f0b1b963James Dong    sprintf(pDecCont->str, "H264SwDecInit# OK: return %p", (void*)pDecCont);
1690c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC(pDecCont->str);
1700c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
1710c1bc742181ded4930842b46e9507372f0b1b963James Dong
1720c1bc742181ded4930842b46e9507372f0b1b963James Dong    *decInst = (decContainer_t *)pDecCont;
1730c1bc742181ded4930842b46e9507372f0b1b963James Dong
1740c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(H264SWDEC_OK);
1750c1bc742181ded4930842b46e9507372f0b1b963James Dong
1760c1bc742181ded4930842b46e9507372f0b1b963James Dong}
1770c1bc742181ded4930842b46e9507372f0b1b963James Dong
1780c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
1790c1bc742181ded4930842b46e9507372f0b1b963James Dong
1800c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function: H264SwDecGetInfo()
1810c1bc742181ded4930842b46e9507372f0b1b963James Dong
1820c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
1830c1bc742181ded4930842b46e9507372f0b1b963James Dong            This function provides read access to decoder information. This
1840c1bc742181ded4930842b46e9507372f0b1b963James Dong            function should not be called before H264SwDecDecode function has
1850c1bc742181ded4930842b46e9507372f0b1b963James Dong            indicated that headers are ready.
1860c1bc742181ded4930842b46e9507372f0b1b963James Dong
1870c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
1880c1bc742181ded4930842b46e9507372f0b1b963James Dong            decInst     decoder instance
1890c1bc742181ded4930842b46e9507372f0b1b963James Dong
1900c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
1910c1bc742181ded4930842b46e9507372f0b1b963James Dong            pDecInfo    pointer to info struct where data is written
1920c1bc742181ded4930842b46e9507372f0b1b963James Dong
1930c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
1940c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_OK            success
1950c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_PARAM_ERR     invalid parameters
1960c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_HDRS_NOT_RDY  information not available yet
1970c1bc742181ded4930842b46e9507372f0b1b963James Dong
1980c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
1990c1bc742181ded4930842b46e9507372f0b1b963James Dong
2000c1bc742181ded4930842b46e9507372f0b1b963James DongH264SwDecRet H264SwDecGetInfo(H264SwDecInst decInst, H264SwDecInfo *pDecInfo)
2010c1bc742181ded4930842b46e9507372f0b1b963James Dong{
2020c1bc742181ded4930842b46e9507372f0b1b963James Dong
2030c1bc742181ded4930842b46e9507372f0b1b963James Dong    storage_t *pStorage;
2040c1bc742181ded4930842b46e9507372f0b1b963James Dong
2050c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC("H264SwDecGetInfo#");
2060c1bc742181ded4930842b46e9507372f0b1b963James Dong
2070c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (decInst == NULL || pDecInfo == NULL)
2080c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2090c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecGetInfo# ERROR: decInst or pDecInfo is NULL");
2100c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_PARAM_ERR);
2110c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2120c1bc742181ded4930842b46e9507372f0b1b963James Dong
2130c1bc742181ded4930842b46e9507372f0b1b963James Dong    pStorage = &(((decContainer_t *)decInst)->storage);
2140c1bc742181ded4930842b46e9507372f0b1b963James Dong
2150c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pStorage->activeSps == NULL || pStorage->activePps == NULL)
2160c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2170c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecGetInfo# ERROR: Headers not decoded yet");
2180c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_HDRS_NOT_RDY);
2190c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2200c1bc742181ded4930842b46e9507372f0b1b963James Dong
2210c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
2220c1bc742181ded4930842b46e9507372f0b1b963James Dong    sprintf(((decContainer_t*)decInst)->str,
2230c1bc742181ded4930842b46e9507372f0b1b963James Dong        "H264SwDecGetInfo# decInst %p  pDecInfo %p", decInst, (void*)pDecInfo);
2240c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC(((decContainer_t*)decInst)->str);
2250c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
2260c1bc742181ded4930842b46e9507372f0b1b963James Dong
2270c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* h264bsdPicWidth and -Height return dimensions in macroblock units,
2280c1bc742181ded4930842b46e9507372f0b1b963James Dong     * picWidth and -Height in pixels */
2290c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecInfo->picWidth        = h264bsdPicWidth(pStorage) << 4;
2300c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecInfo->picHeight       = h264bsdPicHeight(pStorage) << 4;
2310c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecInfo->videoRange      = h264bsdVideoRange(pStorage);
2320c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecInfo->matrixCoefficients = h264bsdMatrixCoefficients(pStorage);
2330c1bc742181ded4930842b46e9507372f0b1b963James Dong
2340c1bc742181ded4930842b46e9507372f0b1b963James Dong    h264bsdCroppingParams(pStorage,
2350c1bc742181ded4930842b46e9507372f0b1b963James Dong        &pDecInfo->croppingFlag,
2360c1bc742181ded4930842b46e9507372f0b1b963James Dong        &pDecInfo->cropParams.cropLeftOffset,
2370c1bc742181ded4930842b46e9507372f0b1b963James Dong        &pDecInfo->cropParams.cropOutWidth,
2380c1bc742181ded4930842b46e9507372f0b1b963James Dong        &pDecInfo->cropParams.cropTopOffset,
2390c1bc742181ded4930842b46e9507372f0b1b963James Dong        &pDecInfo->cropParams.cropOutHeight);
2400c1bc742181ded4930842b46e9507372f0b1b963James Dong
2410c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* sample aspect ratio */
2420c1bc742181ded4930842b46e9507372f0b1b963James Dong    h264bsdSampleAspectRatio(pStorage,
2430c1bc742181ded4930842b46e9507372f0b1b963James Dong                             &pDecInfo->parWidth,
2440c1bc742181ded4930842b46e9507372f0b1b963James Dong                             &pDecInfo->parHeight);
2450c1bc742181ded4930842b46e9507372f0b1b963James Dong
2460c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* profile */
2470c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecInfo->profile = h264bsdProfile(pStorage);
2480c1bc742181ded4930842b46e9507372f0b1b963James Dong
2490c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC("H264SwDecGetInfo# OK");
2500c1bc742181ded4930842b46e9507372f0b1b963James Dong
2510c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(H264SWDEC_OK);
2520c1bc742181ded4930842b46e9507372f0b1b963James Dong
2530c1bc742181ded4930842b46e9507372f0b1b963James Dong}
2540c1bc742181ded4930842b46e9507372f0b1b963James Dong
2550c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
2560c1bc742181ded4930842b46e9507372f0b1b963James Dong
2570c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function: H264SwDecRelease()
2580c1bc742181ded4930842b46e9507372f0b1b963James Dong
2590c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
2600c1bc742181ded4930842b46e9507372f0b1b963James Dong            Release the decoder instance. Function calls h264bsdShutDown to
2610c1bc742181ded4930842b46e9507372f0b1b963James Dong            release instance data and frees the memory allocated for the
2620c1bc742181ded4930842b46e9507372f0b1b963James Dong            instance.
2630c1bc742181ded4930842b46e9507372f0b1b963James Dong
2640c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
2650c1bc742181ded4930842b46e9507372f0b1b963James Dong            decInst     Decoder instance
2660c1bc742181ded4930842b46e9507372f0b1b963James Dong
2670c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
2680c1bc742181ded4930842b46e9507372f0b1b963James Dong            none
2690c1bc742181ded4930842b46e9507372f0b1b963James Dong
2700c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
2710c1bc742181ded4930842b46e9507372f0b1b963James Dong            none
2720c1bc742181ded4930842b46e9507372f0b1b963James Dong
2730c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
2740c1bc742181ded4930842b46e9507372f0b1b963James Dong
2750c1bc742181ded4930842b46e9507372f0b1b963James Dongvoid H264SwDecRelease(H264SwDecInst decInst)
2760c1bc742181ded4930842b46e9507372f0b1b963James Dong{
2770c1bc742181ded4930842b46e9507372f0b1b963James Dong
2780c1bc742181ded4930842b46e9507372f0b1b963James Dong    decContainer_t *pDecCont;
2790c1bc742181ded4930842b46e9507372f0b1b963James Dong
2800c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC("H264SwDecRelease#");
2810c1bc742181ded4930842b46e9507372f0b1b963James Dong
2820c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (decInst == NULL)
2830c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
2840c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecRelease# ERROR: decInst == NULL");
2850c1bc742181ded4930842b46e9507372f0b1b963James Dong        return;
2860c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
2870c1bc742181ded4930842b46e9507372f0b1b963James Dong
2880c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecCont = (decContainer_t*)decInst;
2890c1bc742181ded4930842b46e9507372f0b1b963James Dong
2900c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
2910c1bc742181ded4930842b46e9507372f0b1b963James Dong    sprintf(pDecCont->str, "H264SwDecRelease# decInst %p",decInst);
2920c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC(pDecCont->str);
2930c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
2940c1bc742181ded4930842b46e9507372f0b1b963James Dong
2950c1bc742181ded4930842b46e9507372f0b1b963James Dong    h264bsdShutdown(&pDecCont->storage);
2960c1bc742181ded4930842b46e9507372f0b1b963James Dong
2970c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecFree(pDecCont);
2980c1bc742181ded4930842b46e9507372f0b1b963James Dong
2990c1bc742181ded4930842b46e9507372f0b1b963James Dong}
3000c1bc742181ded4930842b46e9507372f0b1b963James Dong
3010c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
3020c1bc742181ded4930842b46e9507372f0b1b963James Dong
3030c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function: H264SwDecDecode
3040c1bc742181ded4930842b46e9507372f0b1b963James Dong
3050c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
3060c1bc742181ded4930842b46e9507372f0b1b963James Dong            Decode stream data. Calls h264bsdDecode to do the actual decoding.
3070c1bc742181ded4930842b46e9507372f0b1b963James Dong
3080c1bc742181ded4930842b46e9507372f0b1b963James Dong        Input:
3090c1bc742181ded4930842b46e9507372f0b1b963James Dong            decInst     decoder instance
3100c1bc742181ded4930842b46e9507372f0b1b963James Dong            pInput      pointer to input struct
3110c1bc742181ded4930842b46e9507372f0b1b963James Dong
3120c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
3130c1bc742181ded4930842b46e9507372f0b1b963James Dong            pOutput     pointer to output struct
3140c1bc742181ded4930842b46e9507372f0b1b963James Dong
3150c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
3160c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_NOT_INITIALIZED   decoder instance not initialized yet
3170c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_PARAM_ERR         invalid parameters
3180c1bc742181ded4930842b46e9507372f0b1b963James Dong
3190c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_STRM_PROCESSED    stream buffer decoded
3200c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY   headers decoded,
3210c1bc742181ded4930842b46e9507372f0b1b963James Dong                                                stream buffer not finished
3220c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_PIC_RDY                   decoding of a picture finished
3230c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY    decoding of a picture finished,
3240c1bc742181ded4930842b46e9507372f0b1b963James Dong                                                stream buffer not finished
3250c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_STRM_ERR                  serious error in decoding, no
3260c1bc742181ded4930842b46e9507372f0b1b963James Dong                                                valid parameter sets available
3270c1bc742181ded4930842b46e9507372f0b1b963James Dong                                                to decode picture data
3280c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_EVALUATION_LIMIT_EXCEEDED this can only occur when
3290c1bc742181ded4930842b46e9507372f0b1b963James Dong                                                evaluation version is used,
3300c1bc742181ded4930842b46e9507372f0b1b963James Dong                                                max number of frames reached
3310c1bc742181ded4930842b46e9507372f0b1b963James Dong
3320c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
3330c1bc742181ded4930842b46e9507372f0b1b963James Dong
3340c1bc742181ded4930842b46e9507372f0b1b963James DongH264SwDecRet H264SwDecDecode(H264SwDecInst decInst, H264SwDecInput *pInput,
3350c1bc742181ded4930842b46e9507372f0b1b963James Dong                  H264SwDecOutput *pOutput)
3360c1bc742181ded4930842b46e9507372f0b1b963James Dong{
3370c1bc742181ded4930842b46e9507372f0b1b963James Dong
3380c1bc742181ded4930842b46e9507372f0b1b963James Dong    decContainer_t *pDecCont;
3390c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 strmLen;
3400c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 numReadBytes;
3410c1bc742181ded4930842b46e9507372f0b1b963James Dong    u8 *tmpStream;
3420c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 decResult = 0;
3430c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecRet returnValue = H264SWDEC_STRM_PROCESSED;
3440c1bc742181ded4930842b46e9507372f0b1b963James Dong
3450c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC("H264SwDecDecode#");
3460c1bc742181ded4930842b46e9507372f0b1b963James Dong
3470c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Check that function input parameters are valid */
3480c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pInput == NULL || pOutput == NULL)
3490c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3500c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecDecode# ERROR: pInput or pOutput is NULL");
3510c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_PARAM_ERR);
3520c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3530c1bc742181ded4930842b46e9507372f0b1b963James Dong
3540c1bc742181ded4930842b46e9507372f0b1b963James Dong    if ((pInput->pStream == NULL) || (pInput->dataLen == 0))
3550c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3560c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecDecode# ERROR: Invalid input parameters");
3570c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_PARAM_ERR);
3580c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3590c1bc742181ded4930842b46e9507372f0b1b963James Dong
3600c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecCont = (decContainer_t *)decInst;
3610c1bc742181ded4930842b46e9507372f0b1b963James Dong
3620c1bc742181ded4930842b46e9507372f0b1b963James Dong    /* Check if decoder is in an incorrect mode */
3630c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (decInst == NULL || pDecCont->decStat == UNINITIALIZED)
3640c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3650c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecDecode# ERROR: Decoder not initialized");
3660c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_NOT_INITIALIZED);
3670c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
3680c1bc742181ded4930842b46e9507372f0b1b963James Dong
3690c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_EVALUATION
3700c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pDecCont->picNumber >= H264DEC_EVALUATION_LIMIT)
3710c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_EVALUATION_LIMIT_EXCEEDED);
3720c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
3730c1bc742181ded4930842b46e9507372f0b1b963James Dong
3740c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
3750c1bc742181ded4930842b46e9507372f0b1b963James Dong    sprintf(pDecCont->str, "H264SwDecDecode# decInst %p  pInput %p  pOutput %p",
3760c1bc742181ded4930842b46e9507372f0b1b963James Dong            decInst, (void*)pInput, (void*)pOutput);
3770c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC(pDecCont->str);
3780c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
3790c1bc742181ded4930842b46e9507372f0b1b963James Dong
3800c1bc742181ded4930842b46e9507372f0b1b963James Dong    pOutput->pStrmCurrPos   = NULL;
3810c1bc742181ded4930842b46e9507372f0b1b963James Dong
3820c1bc742181ded4930842b46e9507372f0b1b963James Dong    numReadBytes = 0;
3830c1bc742181ded4930842b46e9507372f0b1b963James Dong    strmLen = pInput->dataLen;
3840c1bc742181ded4930842b46e9507372f0b1b963James Dong    tmpStream = pInput->pStream;
3850c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecCont->storage.intraConcealmentFlag = pInput->intraConcealmentMethod;
3860c1bc742181ded4930842b46e9507372f0b1b963James Dong
3870c1bc742181ded4930842b46e9507372f0b1b963James Dong    do
3880c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
3890c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* Return HDRS_RDY after DPB flush caused by new SPS */
3900c1bc742181ded4930842b46e9507372f0b1b963James Dong        if (pDecCont->decStat == NEW_HEADERS)
3910c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3920c1bc742181ded4930842b46e9507372f0b1b963James Dong            decResult = H264BSD_HDRS_RDY;
3930c1bc742181ded4930842b46e9507372f0b1b963James Dong            pDecCont->decStat = INITIALIZED;
3940c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
3950c1bc742181ded4930842b46e9507372f0b1b963James Dong        else /* Continue decoding normally */
3960c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
3970c1bc742181ded4930842b46e9507372f0b1b963James Dong            decResult = h264bsdDecode(&pDecCont->storage, tmpStream, strmLen,
3980c1bc742181ded4930842b46e9507372f0b1b963James Dong                pInput->picId, &numReadBytes);
3990c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
4000c1bc742181ded4930842b46e9507372f0b1b963James Dong        tmpStream += numReadBytes;
4010c1bc742181ded4930842b46e9507372f0b1b963James Dong        /* check if too many bytes are read from stream */
4020c1bc742181ded4930842b46e9507372f0b1b963James Dong        if ( (i32)(strmLen - numReadBytes) >= 0 )
4030c1bc742181ded4930842b46e9507372f0b1b963James Dong            strmLen -= numReadBytes;
4040c1bc742181ded4930842b46e9507372f0b1b963James Dong        else
4050c1bc742181ded4930842b46e9507372f0b1b963James Dong            strmLen = 0;
4060c1bc742181ded4930842b46e9507372f0b1b963James Dong
4070c1bc742181ded4930842b46e9507372f0b1b963James Dong        pOutput->pStrmCurrPos = tmpStream;
4080c1bc742181ded4930842b46e9507372f0b1b963James Dong
4090c1bc742181ded4930842b46e9507372f0b1b963James Dong        switch (decResult)
4100c1bc742181ded4930842b46e9507372f0b1b963James Dong        {
4110c1bc742181ded4930842b46e9507372f0b1b963James Dong            case H264BSD_HDRS_RDY:
4120c1bc742181ded4930842b46e9507372f0b1b963James Dong
4130c1bc742181ded4930842b46e9507372f0b1b963James Dong                if(pDecCont->storage.dpb->flushed &&
4140c1bc742181ded4930842b46e9507372f0b1b963James Dong                   pDecCont->storage.dpb->numOut !=
4150c1bc742181ded4930842b46e9507372f0b1b963James Dong                   pDecCont->storage.dpb->outIndex)
4160c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
4170c1bc742181ded4930842b46e9507372f0b1b963James Dong                    /* output first all DPB stored pictures
4180c1bc742181ded4930842b46e9507372f0b1b963James Dong                     * DPB flush caused by new SPS */
4190c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pDecCont->storage.dpb->flushed = 0;
4200c1bc742181ded4930842b46e9507372f0b1b963James Dong                    pDecCont->decStat = NEW_HEADERS;
4210c1bc742181ded4930842b46e9507372f0b1b963James Dong                    returnValue = H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY;
4220c1bc742181ded4930842b46e9507372f0b1b963James Dong                    strmLen = 0;
4230c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
4240c1bc742181ded4930842b46e9507372f0b1b963James Dong                else
4250c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
4260c1bc742181ded4930842b46e9507372f0b1b963James Dong                    returnValue = H264SWDEC_HDRS_RDY_BUFF_NOT_EMPTY;
4270c1bc742181ded4930842b46e9507372f0b1b963James Dong                    strmLen = 0;
4280c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
4290c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
4300c1bc742181ded4930842b46e9507372f0b1b963James Dong
4310c1bc742181ded4930842b46e9507372f0b1b963James Dong            case H264BSD_PIC_RDY:
4320c1bc742181ded4930842b46e9507372f0b1b963James Dong                pDecCont->picNumber++;
4330c1bc742181ded4930842b46e9507372f0b1b963James Dong
4340c1bc742181ded4930842b46e9507372f0b1b963James Dong                if (strmLen == 0)
4350c1bc742181ded4930842b46e9507372f0b1b963James Dong                    returnValue = H264SWDEC_PIC_RDY;
4360c1bc742181ded4930842b46e9507372f0b1b963James Dong                else
4370c1bc742181ded4930842b46e9507372f0b1b963James Dong                    returnValue = H264SWDEC_PIC_RDY_BUFF_NOT_EMPTY;
4380c1bc742181ded4930842b46e9507372f0b1b963James Dong
4390c1bc742181ded4930842b46e9507372f0b1b963James Dong                strmLen = 0;
4400c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
4410c1bc742181ded4930842b46e9507372f0b1b963James Dong
4420c1bc742181ded4930842b46e9507372f0b1b963James Dong            case H264BSD_PARAM_SET_ERROR:
4430c1bc742181ded4930842b46e9507372f0b1b963James Dong                if ( !h264bsdCheckValidParamSets(&pDecCont->storage) &&
4440c1bc742181ded4930842b46e9507372f0b1b963James Dong                     strmLen == 0 )
4450c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
4460c1bc742181ded4930842b46e9507372f0b1b963James Dong                    returnValue = H264SWDEC_STRM_ERR;
4470c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
4480c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
4490c1bc742181ded4930842b46e9507372f0b1b963James Dong            case H264BSD_MEMALLOC_ERROR:
4500c1bc742181ded4930842b46e9507372f0b1b963James Dong                {
4510c1bc742181ded4930842b46e9507372f0b1b963James Dong                    returnValue = H264SWDEC_MEMFAIL;
4520c1bc742181ded4930842b46e9507372f0b1b963James Dong                    strmLen = 0;
4530c1bc742181ded4930842b46e9507372f0b1b963James Dong                }
4540c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
4550c1bc742181ded4930842b46e9507372f0b1b963James Dong            default:
4560c1bc742181ded4930842b46e9507372f0b1b963James Dong                break;
4570c1bc742181ded4930842b46e9507372f0b1b963James Dong        }
4580c1bc742181ded4930842b46e9507372f0b1b963James Dong
4590c1bc742181ded4930842b46e9507372f0b1b963James Dong    } while (strmLen);
4600c1bc742181ded4930842b46e9507372f0b1b963James Dong
4610c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
4620c1bc742181ded4930842b46e9507372f0b1b963James Dong    sprintf(pDecCont->str, "H264SwDecDecode# OK: DecResult %d",
4630c1bc742181ded4930842b46e9507372f0b1b963James Dong            returnValue);
4640c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC(pDecCont->str);
4650c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
4660c1bc742181ded4930842b46e9507372f0b1b963James Dong
4670c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(returnValue);
4680c1bc742181ded4930842b46e9507372f0b1b963James Dong
4690c1bc742181ded4930842b46e9507372f0b1b963James Dong}
4700c1bc742181ded4930842b46e9507372f0b1b963James Dong
4710c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
4720c1bc742181ded4930842b46e9507372f0b1b963James Dong
4730c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function: H264SwDecGetAPIVersion
4740c1bc742181ded4930842b46e9507372f0b1b963James Dong
4750c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
4760c1bc742181ded4930842b46e9507372f0b1b963James Dong            Return version information of the API
4770c1bc742181ded4930842b46e9507372f0b1b963James Dong
4780c1bc742181ded4930842b46e9507372f0b1b963James Dong        Inputs:
4790c1bc742181ded4930842b46e9507372f0b1b963James Dong            none
4800c1bc742181ded4930842b46e9507372f0b1b963James Dong
4810c1bc742181ded4930842b46e9507372f0b1b963James Dong        Outputs:
4820c1bc742181ded4930842b46e9507372f0b1b963James Dong            none
4830c1bc742181ded4930842b46e9507372f0b1b963James Dong
4840c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
4850c1bc742181ded4930842b46e9507372f0b1b963James Dong            API version
4860c1bc742181ded4930842b46e9507372f0b1b963James Dong
4870c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
4880c1bc742181ded4930842b46e9507372f0b1b963James Dong
4890c1bc742181ded4930842b46e9507372f0b1b963James DongH264SwDecApiVersion H264SwDecGetAPIVersion()
4900c1bc742181ded4930842b46e9507372f0b1b963James Dong{
4910c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecApiVersion ver;
4920c1bc742181ded4930842b46e9507372f0b1b963James Dong
4930c1bc742181ded4930842b46e9507372f0b1b963James Dong    ver.major = H264SWDEC_MAJOR_VERSION;
4940c1bc742181ded4930842b46e9507372f0b1b963James Dong    ver.minor = H264SWDEC_MINOR_VERSION;
4950c1bc742181ded4930842b46e9507372f0b1b963James Dong
4960c1bc742181ded4930842b46e9507372f0b1b963James Dong    return(ver);
4970c1bc742181ded4930842b46e9507372f0b1b963James Dong}
4980c1bc742181ded4930842b46e9507372f0b1b963James Dong
4990c1bc742181ded4930842b46e9507372f0b1b963James Dong/*------------------------------------------------------------------------------
5000c1bc742181ded4930842b46e9507372f0b1b963James Dong
5010c1bc742181ded4930842b46e9507372f0b1b963James Dong    Function: H264SwDecNextPicture
5020c1bc742181ded4930842b46e9507372f0b1b963James Dong
5030c1bc742181ded4930842b46e9507372f0b1b963James Dong        Functional description:
5040c1bc742181ded4930842b46e9507372f0b1b963James Dong            Get next picture in display order if any available.
5050c1bc742181ded4930842b46e9507372f0b1b963James Dong
5060c1bc742181ded4930842b46e9507372f0b1b963James Dong        Input:
5070c1bc742181ded4930842b46e9507372f0b1b963James Dong            decInst     decoder instance.
5080c1bc742181ded4930842b46e9507372f0b1b963James Dong            flushBuffer force output of all buffered pictures
5090c1bc742181ded4930842b46e9507372f0b1b963James Dong
5100c1bc742181ded4930842b46e9507372f0b1b963James Dong        Output:
5110c1bc742181ded4930842b46e9507372f0b1b963James Dong            pOutput     pointer to output structure
5120c1bc742181ded4930842b46e9507372f0b1b963James Dong
5130c1bc742181ded4930842b46e9507372f0b1b963James Dong        Returns:
5140c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_OK            no pictures available for display
5150c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_PIC_RDY       picture available for display
5160c1bc742181ded4930842b46e9507372f0b1b963James Dong            H264SWDEC_PARAM_ERR     invalid parameters
5170c1bc742181ded4930842b46e9507372f0b1b963James Dong
5180c1bc742181ded4930842b46e9507372f0b1b963James Dong------------------------------------------------------------------------------*/
5190c1bc742181ded4930842b46e9507372f0b1b963James Dong
5200c1bc742181ded4930842b46e9507372f0b1b963James DongH264SwDecRet H264SwDecNextPicture(H264SwDecInst decInst,
5210c1bc742181ded4930842b46e9507372f0b1b963James Dong    H264SwDecPicture *pOutput, u32 flushBuffer)
5220c1bc742181ded4930842b46e9507372f0b1b963James Dong{
5230c1bc742181ded4930842b46e9507372f0b1b963James Dong
5240c1bc742181ded4930842b46e9507372f0b1b963James Dong    decContainer_t *pDecCont;
5250c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 numErrMbs, isIdrPic, picId;
5260c1bc742181ded4930842b46e9507372f0b1b963James Dong    u32 *pOutPic;
5270c1bc742181ded4930842b46e9507372f0b1b963James Dong
5280c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC("H264SwDecNextPicture#");
5290c1bc742181ded4930842b46e9507372f0b1b963James Dong
5300c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (decInst == NULL || pOutput == NULL)
5310c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5320c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecNextPicture# ERROR: decInst or pOutput is NULL");
5330c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_PARAM_ERR);
5340c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5350c1bc742181ded4930842b46e9507372f0b1b963James Dong
5360c1bc742181ded4930842b46e9507372f0b1b963James Dong    pDecCont = (decContainer_t*)decInst;
5370c1bc742181ded4930842b46e9507372f0b1b963James Dong
5380c1bc742181ded4930842b46e9507372f0b1b963James Dong#ifdef H264DEC_TRACE
5390c1bc742181ded4930842b46e9507372f0b1b963James Dong    sprintf(pDecCont->str, "H264SwDecNextPicture# decInst %p pOutput %p %s %d",
5400c1bc742181ded4930842b46e9507372f0b1b963James Dong            decInst, (void*)pOutput, "flushBuffer", flushBuffer);
5410c1bc742181ded4930842b46e9507372f0b1b963James Dong    DEC_API_TRC(pDecCont->str);
5420c1bc742181ded4930842b46e9507372f0b1b963James Dong#endif
5430c1bc742181ded4930842b46e9507372f0b1b963James Dong
5440c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (flushBuffer)
5450c1bc742181ded4930842b46e9507372f0b1b963James Dong        h264bsdFlushBuffer(&pDecCont->storage);
5460c1bc742181ded4930842b46e9507372f0b1b963James Dong
5470c1bc742181ded4930842b46e9507372f0b1b963James Dong    pOutPic = (u32*)h264bsdNextOutputPicture(&pDecCont->storage, &picId,
5480c1bc742181ded4930842b46e9507372f0b1b963James Dong                                             &isIdrPic, &numErrMbs);
5490c1bc742181ded4930842b46e9507372f0b1b963James Dong
5500c1bc742181ded4930842b46e9507372f0b1b963James Dong    if (pOutPic == NULL)
5510c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5520c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecNextPicture# OK: return H264SWDEC_OK");
5530c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_OK);
5540c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5550c1bc742181ded4930842b46e9507372f0b1b963James Dong    else
5560c1bc742181ded4930842b46e9507372f0b1b963James Dong    {
5570c1bc742181ded4930842b46e9507372f0b1b963James Dong        pOutput->pOutputPicture = pOutPic;
5580c1bc742181ded4930842b46e9507372f0b1b963James Dong        pOutput->picId          = picId;
5590c1bc742181ded4930842b46e9507372f0b1b963James Dong        pOutput->isIdrPicture   = isIdrPic;
5600c1bc742181ded4930842b46e9507372f0b1b963James Dong        pOutput->nbrOfErrMBs    = numErrMbs;
5610c1bc742181ded4930842b46e9507372f0b1b963James Dong        DEC_API_TRC("H264SwDecNextPicture# OK: return H264SWDEC_PIC_RDY");
5620c1bc742181ded4930842b46e9507372f0b1b963James Dong        return(H264SWDEC_PIC_RDY);
5630c1bc742181ded4930842b46e9507372f0b1b963James Dong    }
5640c1bc742181ded4930842b46e9507372f0b1b963James Dong
5650c1bc742181ded4930842b46e9507372f0b1b963James Dong}
5660c1bc742181ded4930842b46e9507372f0b1b963James Dong
5670c1bc742181ded4930842b46e9507372f0b1b963James Dong
568