static Void VcapVencVdecVdis_ipcBitsProcessFullBufs(VcapVencVdecVdis_IpcBitsCtrlThrObj *thrObj,
        VcapVencVdecVdis_IpcBitsCtrlFileObj *fObj)
{
    VCODEC_BITSBUF_LIST_S fullBufList;
    VCODEC_BITSBUF_S *pFullBuf;
    VCODEC_BITSBUF_S *pEmptyBuf;
    Int i,status;
    static Int printStatsInterval = 0;

#ifdef IPC_BITS_DEBUG
    if ((printStatsInterval % MCFW_IPCBITS_INFO_PRINT_INTERVAL) == 0)
    {
        OSA_printf("MCFW_IPCBITS:%s:INFO: periodic print..",__func__);
    }
#endif
    printStatsInterval++;

    Venc_getBitstreamBuffer(&fullBufList, 0);

    for (i = 0; i < fullBufList.numBufs; i++)
    {
        VcapVencVdecVdis_updateStatistics(&fullBufList.bitsBuf[i]);

        if (fullBufList.bitsBuf[i].chnId < Venc_getPrimaryChannels())
        {
            status = OSA_queGet(&thrObj->bufQFreeBufs,(Int32 *)(&pEmptyBuf),
                                OSA_TIMEOUT_FOREVER);
            OSA_assert(status == 0);
            pFullBuf = &fullBufList.bitsBuf[i];
            VcapVencVdecVdis_ipcBitsCopyBitBufInfo(pEmptyBuf,pFullBuf);
            VcapVencVdecVdis_ipcBitsCopyBitBufDataMem2Mem(pEmptyBuf,pFullBuf);
            status = OSA_quePut(&thrObj->bufQFullBufs,
                                (Int32)pEmptyBuf,OSA_TIMEOUT_NONE);
            OSA_assert(status == 0);
        }
    }
    if (fObj->enableFWrite)
    {
        VcapVencVdecVdis_ipcBitsWriteBitsToFile(fObj->fpWrHdr,
                                                fObj->fpWrData,
                                                fObj->fpWrMvData,
                                                &fullBufList,
                                                fObj->maxFileSize,
                                                fObj->fwriteEnableBitMask,
                                                fObj->wrapOccuredHdr,
                                                fObj->wrapOccuredBuf);

    }
    Venc_releaseBitstreamBuffer(&fullBufList);
}
void *VcapVenc_bitsWriteMain(void *pPrm)
{
    Int32 status, frameId;
    VCODEC_BITSBUF_LIST_S bitsBuf;
    VCODEC_BITSBUF_S *pBuf;
    VcapVenc_ChInfo *pChInfo;
    UInt32 latency;

    FILE *fp = NULL;
    UInt32 fileWriteState = FILE_WRITE_STOPPED, writeDataSize;

    if(gVcapVenc_ctrl.fileWriteEnable)
    {
        fp = fopen(gVcapVenc_ctrl.fileWriteName, "wb");
        if(fp!=NULL)
        {
            fileWriteState = FILE_WRITE_RUNNING;
            printf(" Opened file [%s] for writing CH%d\n", gVcapVenc_ctrl.fileWriteName, gVcapVenc_ctrl.fileWriteChn);
        }
        else
        {
            printf(" ERROR: File open [%s] for writing CH%d FAILED !!!!\n", gVcapVenc_ctrl.fileWriteName, gVcapVenc_ctrl.fileWriteChn);
        }
    }

    while(!gVcapVenc_ctrl.exitWrThr)
    {
        status = OSA_semWait(&gVcapVenc_ctrl.wrSem, OSA_TIMEOUT_FOREVER);
        if(status!=OSA_SOK)
            break;

        status = Venc_getBitstreamBuffer(&bitsBuf, TIMEOUT_NO_WAIT);

        if(status==ERROR_NONE && bitsBuf.numBufs)
        {
            for(frameId=0; frameId<bitsBuf.numBufs; frameId++)
            {
                pBuf = &bitsBuf.bitsBuf[frameId];
                if(pBuf->chnId<VENC_CHN_MAX)
                {
                    pChInfo = &gVcapVenc_ctrl.chInfo[pBuf->chnId];

                    pChInfo->totalDataSize += pBuf->filledBufSize;
                    pChInfo->numFrames++;
                    if(pBuf->frameType==VCODEC_FRAME_TYPE_I_FRAME)
                    {
                        pChInfo->numKeyFrames++;
                    }

                    latency = pBuf->encodeTimestamp - pBuf->timestamp;

                    if(latency > pChInfo->maxLatency)
                        pChInfo->maxLatency = latency;

                    if(latency < pChInfo->minLatency)
                        pChInfo->minLatency = latency;

                    if(pBuf->frameWidth > pChInfo->maxWidth)
                        pChInfo->maxWidth = pBuf->frameWidth;

                    if(pBuf->frameWidth < pChInfo->minWidth)
                        pChInfo->minWidth = pBuf->frameWidth;

                    if(pBuf->frameHeight > pChInfo->maxHeight)
                        pChInfo->maxHeight = pBuf->frameHeight;

                    if(pBuf->frameHeight < pChInfo->minHeight)
                        pChInfo->minHeight = pBuf->frameHeight;

                }
                if(gVcapVenc_ctrl.fileWriteEnable)
                {
                    if(pBuf->chnId== gVcapVenc_ctrl.fileWriteChn && fileWriteState == FILE_WRITE_RUNNING)
                    {
                        writeDataSize = fwrite(pBuf->bufVirtAddr, 1, pBuf->filledBufSize, fp);
                        if(writeDataSize!=pBuf->filledBufSize)
                        {
                            fileWriteState = FILE_WRITE_STOPPED;
                            fclose(fp);
                            printf(" Closing file [%s] for CH%d\n", gVcapVenc_ctrl.fileWriteName, gVcapVenc_ctrl.fileWriteChn);
                        }
                    }
                }
            }

            Venc_releaseBitstreamBuffer(&bitsBuf);
        }
    }

    gVcapVenc_ctrl.isWrThrStopDone = TRUE;

    if(gVcapVenc_ctrl.fileWriteEnable)
    {
        if(fileWriteState==FILE_WRITE_RUNNING)
        {
            fclose(fp);
            printf(" Closing file [%s] for CH%d\n", gVcapVenc_ctrl.fileWriteName, gVcapVenc_ctrl.fileWriteChn);
        }
    }

    return NULL;
}
static Void VcapVencVdecVdis_ipcBitsProcessFullBufs(VcapVencVdecVdis_IpcBitsCtrlThrObj *thrObj,
                                       VcapVencVdecVdis_IpcBitsCtrlFileObj *fObj,
                                       VcapVencVdecVdis_ChInfo decChInfo[])
{
    VCODEC_BITSBUF_LIST_S fullBufList;
    VCODEC_BITSBUF_S *pFullBuf;
    VCODEC_BITSBUF_S *pEmptyBuf;
    Int i,status;
    static Int printStatsInterval = 0;

    #ifdef IPC_BITS_DEBUG
    if ((printStatsInterval % MCFW_IPCBITS_INFO_PRINT_INTERVAL) == 0)
    {
        OSA_printf("MCFW_IPCBITS:%s:INFO: periodic print..",__func__);
    }
    #endif
    printStatsInterval++;

    Venc_getBitstreamBuffer(&fullBufList, 0);

    for (i = 0; i < fullBufList.numBufs; i++)
    {
        VcapVencVdecVdis_updateStatistics(&fullBufList.bitsBuf[i]);

        if (fullBufList.bitsBuf[i].chnId < Venc_getPrimaryChannels())
        {
            pFullBuf = &fullBufList.bitsBuf[i];
GET_BUF:
            status = OSA_queGet(&thrObj->bufQFreeBufs,(Int32 *)(&pEmptyBuf),
                                OSA_TIMEOUT_FOREVER);
            OSA_assert(status == 0);

            /* If empty buf size not enough to hold rcvd buf <e.g, in multiple resolution scenarios>,
                        * put back the buffer into freeBufs Q
                        */
            if (pFullBuf->filledBufSize > pEmptyBuf->bufSize)
            {
                status = OSA_quePut(&thrObj->bufQFreeBufs,(Int32)pEmptyBuf,
                                    OSA_TIMEOUT_FOREVER);
                OSA_assert(status == 0);
                goto GET_BUF;
            }
            VcapVencVdecVdis_ipcBitsCopyBitBufInfo(pEmptyBuf,pFullBuf);
            VcapVencVdecVdis_ipcBitsCopyBitBufDataMem2Mem(pEmptyBuf,pFullBuf);
            VcapVencVdecVdis_setFrameTimeStamp(&(decChInfo[pEmptyBuf->chnId]),pEmptyBuf);
            status = OSA_quePut(&thrObj->bufQFullBufs,
                                (Int32)pEmptyBuf,OSA_TIMEOUT_NONE);
            OSA_assert(status == 0);
        }
    }
    if (fObj->enableFWrite)
    {
        VcapVencVdecVdis_ipcBitsWriteBitsToFile(fObj->fpWrHdr,
                                      fObj->fpWrData,
                                      fObj->fpWrMvData,
                                      &fullBufList,
                                      fObj->maxFileSize,
                                      fObj->fwriteEnableBitMask,
                                      fObj->enableLayerWrite,
                                      fObj->wrapOccuredHdr,
                                      fObj->wrapOccuredBuf);

    }
    Venc_releaseBitstreamBuffer(&fullBufList);
}