static Void VcapVencVdecVdis_ipcBitsWriteBitsToFile (FILE  * fpHdr[MCFW_IPC_BITS_MAX_NUM_CHANNELS],
        FILE  * fpBuf[MCFW_IPC_BITS_MAX_NUM_CHANNELS],
        FILE  *fpMvBuf[MCFW_IPC_BITS_MAX_NUM_CHANNELS],
        VCODEC_BITSBUF_LIST_S *bufList,
        UInt32 maxFileSize,
        UInt32 fwriteEnableChannelBitmask,
        Bool wrapOccuredHdr[],
        Bool wrapOccuredBuf[])
{
    Int i;
    VCODEC_BITSBUF_S *pBuf;
    size_t write_cnt;
    static UInt32 maxMvFrameCnt;

    for (i = 0; i < bufList->numBufs; i++)
    {
        UInt32 fileIdx;

        pBuf = &bufList->bitsBuf[i];
        OSA_assert(pBuf->chnId < MCFW_IPC_BITS_MAX_NUM_CHANNELS);
        fileIdx = pBuf->chnId;
        if (fwriteEnableChannelBitmask & (1 << fileIdx) || (fileIdx > 31 && (fwriteEnableChannelBitmask & (1 << (fileIdx-32)))))
        {
            if (FALSE == wrapOccuredBuf[fileIdx])
            {
                wrapOccuredBuf[fileIdx] =
                    VcapVencVdecVdis_ipcBitsWriteWrap(fpBuf[fileIdx],pBuf->filledBufSize, maxFileSize);
            }

            if (FALSE == wrapOccuredBuf[fileIdx])
            {
                write_cnt = fwrite(pBuf->bufVirtAddr,sizeof(char),pBuf->filledBufSize,fpBuf[fileIdx]);
                OSA_assert(write_cnt == pBuf->filledBufSize);
                /*The below check for max frames as 500, means write mv data only for first n frame*/
                if(pBuf->mvDataFilledSize != 0 && maxMvFrameCnt <= 500)
                {
                    maxMvFrameCnt++;
                    write_cnt = fwrite(pBuf->bufVirtAddr + pBuf->mvDataOffset,
                                       sizeof(char), pBuf->mvDataFilledSize,fpMvBuf[fileIdx]);

                    OSA_assert(write_cnt == pBuf->mvDataFilledSize);
                }
            }
            if (FALSE == wrapOccuredHdr[fileIdx])
            {
                wrapOccuredHdr[fileIdx] =
                    VcapVencVdecVdis_ipcBitsWriteWrap(fpHdr[fileIdx],sizeof(*pBuf),maxFileSize);
            }
            if (FALSE == wrapOccuredHdr[fileIdx])
            {
                write_cnt = fwrite(pBuf,sizeof(*pBuf),1,fpHdr[fileIdx]);
                OSA_assert(write_cnt == 1);
            }
        }
    }
}
/*
* When numTemporalLayer is set to VENC_TEMPORAL_LAYERS_1 & enableSVCExtensionFlag is
* set to enabled, then the stream comes as follows IPPPPP.....PPPPPPI 
* and the file dump with complete stream will be VBITS_DATA_0_LAYER_0.h264
* ****************************************************************************
*  When numTemporalLayer is set to VENC_TEMPORAL_LAYERS_2 & enableSVCExtensionFlag is
*  set to enabled, then the stream comes as follows 
*
*     P1    P1    P1    P1    P1
*   I    P0    P0    P0    P0     ......
*
*  and the file dump with complete stream will be VBITS_DATA_0_LAYER_1.h264 while the 
*  file VBITS_DATA_0_LAYER_0.h264 will be the subset stream of lesser frame rate.
*  There for, for any numTemporalLayer set, the complete stream file will be
*  (numTemporalLayer-1) index file
*****************************************************************************
*
*  When numTemporalLayer is set to VENC_TEMPORAL_LAYERS_4 & enableSVCExtensionFlag is
*  set to enabled, then the stream comes as follows 
*
* P3  P3    P3  P3    P3  P3       P3 P3     ....
*    P2         P2          P2           P2        ....
*           P1                       P1               P1    P1   .....
*I                       P0                               P0         ....
*
*
*  and the file dump with complete stream will be 
        VBITS_DATA_0_LAYER_3.h264 -> IP3P2P3P1P3P2P3P0P3P2P3P1....
*  while the  files 
*      VBITS_DATA_0_LAYER_2.h264  -> IP2P1P2P0P2P1P2P0....
*      VBITS_DATA_0_LAYER_1.h264  -> IP1P0P1P0P1P0...
*      VBITS_DATA_0_LAYER_0.h264  -> IP0P0P0P0IP0....
*        will be the subset streams with decreasing order of frame rate.
*
*/
static Void VcapVencVdecVdis_ipcBitsWriteBitsToFile (FILE  * fpHdr[MCFW_IPC_BITS_MAX_NUM_CHANNELS],
                                           FILE  * fpBuf[MCFW_IPC_BITS_MAX_NUM_CHANNELS][MCFW_IPC_BITS_MAX_NUM_CHANNELS_LAYERS],
                                           FILE  *fpMvBuf[MCFW_IPC_BITS_MAX_NUM_CHANNELS],
                                           VCODEC_BITSBUF_LIST_S *bufList,
                                           UInt32 maxFileSize,
                                           UInt32 fwriteEnableChannelBitmask,
                                           Bool enableLayerWrite,
                                           Bool wrapOccuredHdr[],
                                           Bool wrapOccuredBuf[MCFW_IPC_BITS_MAX_NUM_CHANNELS][MCFW_IPC_BITS_MAX_NUM_CHANNELS_LAYERS])
{
    Int i, j;
    VCODEC_BITSBUF_S *pBuf;
    size_t write_cnt;
    static UInt32 maxMvFrameCnt;

    for (i = 0; i < bufList->numBufs;i++)
    {
        UInt32 fileIdx;

        pBuf = &bufList->bitsBuf[i];
        OSA_assert(pBuf->chnId < MCFW_IPC_BITS_MAX_NUM_CHANNELS);
        fileIdx = pBuf->chnId;
    if ((fwriteEnableChannelBitmask & (1 << fileIdx)) || (fileIdx > 31 && (fwriteEnableChannelBitmask & (1 << (fileIdx-32)))))
        {
            /* This file write will be the default case valid for all codecs*/
            if(enableLayerWrite == FALSE || 
              ((enableLayerWrite == TRUE) && (pBuf->codecType != VCODEC_TYPE_H264)))
            {
                if (FALSE == wrapOccuredBuf[fileIdx][MCFW_IPC_BITS_DATA_FILE_DEFAULTLAYER_INDEX])
                {
                     wrapOccuredBuf[fileIdx][MCFW_IPC_BITS_DATA_FILE_DEFAULTLAYER_INDEX] =
                        VcapVencVdecVdis_ipcBitsWriteWrap(fpBuf[fileIdx][MCFW_IPC_BITS_DATA_FILE_DEFAULTLAYER_INDEX],
                                                          pBuf->filledBufSize, 
                                                          maxFileSize);
                }

                if (FALSE == wrapOccuredBuf[fileIdx][MCFW_IPC_BITS_DATA_FILE_DEFAULTLAYER_INDEX])
                {
                    write_cnt = fwrite(pBuf->bufVirtAddr,sizeof(char),
                                       pBuf->filledBufSize,
                                       fpBuf[fileIdx][MCFW_IPC_BITS_DATA_FILE_DEFAULTLAYER_INDEX]);
                    OSA_assert(write_cnt == pBuf->filledBufSize);

                    /*The below check for max frames as 500, means write mv data only for first n frame*/
                    if(pBuf->mvDataFilledSize != 0 && maxMvFrameCnt <= 500)
                    {
                        maxMvFrameCnt++;
                        write_cnt = fwrite(pBuf->bufVirtAddr + pBuf->mvDataOffset,
                                  sizeof(char), pBuf->mvDataFilledSize,fpMvBuf[fileIdx]);

                        OSA_assert(write_cnt == pBuf->mvDataFilledSize);
                    }
                }
            }
            else if(enableLayerWrite == TRUE)
            {
                /*I-Frames should be present in all the h264 temporal layers*/
                if( pBuf->temporalId == 0 && 
                    pBuf->frameType == VCODEC_FRAME_TYPE_I_FRAME )
                {
                    for(j = 0; j < pBuf->numTemporalLayerSetInCodec ; j++)
                    {
                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            wrapOccuredBuf[fileIdx][j] =
                            VcapVencVdecVdis_ipcBitsWriteWrap(fpBuf[fileIdx][j],pBuf->filledBufSize, maxFileSize);
                        }

                    if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            write_cnt = fwrite(pBuf->bufVirtAddr,sizeof(char),pBuf->filledBufSize,fpBuf[fileIdx][j]);
                            OSA_assert(write_cnt == pBuf->filledBufSize);
                        }                 
                    }
                }

                /*P-Frames should be present in specific layers based on their temporal Id's
                               ***/
                if( pBuf->temporalId == 0 && 
                     pBuf->frameType == VCODEC_FRAME_TYPE_P_FRAME )
                {
                    for(j = 0; j < pBuf->numTemporalLayerSetInCodec ; j++)
                    {
                           
                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            wrapOccuredBuf[fileIdx][j] =
                                VcapVencVdecVdis_ipcBitsWriteWrap(fpBuf[fileIdx][j],pBuf->filledBufSize, maxFileSize);
                        }

                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            write_cnt = fwrite(pBuf->bufVirtAddr,sizeof(char),pBuf->filledBufSize,fpBuf[fileIdx][j]);
                            OSA_assert(write_cnt == pBuf->filledBufSize);
                        }
                    }
                }

                if( pBuf->temporalId == 1 && 
                     pBuf->frameType == VCODEC_FRAME_TYPE_P_FRAME )
                {
                    for(j = 1; j < pBuf->numTemporalLayerSetInCodec ; j++)
                    {
                            
                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            wrapOccuredBuf[fileIdx][j] =
                                VcapVencVdecVdis_ipcBitsWriteWrap(fpBuf[fileIdx][j],pBuf->filledBufSize, maxFileSize);
                        }

                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            write_cnt = fwrite(pBuf->bufVirtAddr,sizeof(char),pBuf->filledBufSize,fpBuf[fileIdx][j]);
                            OSA_assert(write_cnt == pBuf->filledBufSize);
                        }
                    }
                }

                if( pBuf->temporalId == 2 && 
                     pBuf->frameType == VCODEC_FRAME_TYPE_P_FRAME )
                {
                    for(j = 2; j < pBuf->numTemporalLayerSetInCodec ; j++)
                    {
                             
                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            wrapOccuredBuf[fileIdx][j] =
                                VcapVencVdecVdis_ipcBitsWriteWrap(fpBuf[fileIdx][j],pBuf->filledBufSize, maxFileSize);
                        }

                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            write_cnt = fwrite(pBuf->bufVirtAddr,sizeof(char),pBuf->filledBufSize,fpBuf[fileIdx][j]);
                            OSA_assert(write_cnt == pBuf->filledBufSize);
                        }
                    }
                }
                if( pBuf->temporalId == 3 && 
                     pBuf->frameType == VCODEC_FRAME_TYPE_P_FRAME )
                {
                    for(j = 3; j < pBuf->numTemporalLayerSetInCodec ; j++)
                    {
                           
                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            wrapOccuredBuf[fileIdx][j] =
                                VcapVencVdecVdis_ipcBitsWriteWrap(fpBuf[fileIdx][j],pBuf->filledBufSize, maxFileSize);
                        }

                        if (FALSE == wrapOccuredBuf[fileIdx][j])
                        {
                            write_cnt = fwrite(pBuf->bufVirtAddr,sizeof(char),pBuf->filledBufSize,fpBuf[fileIdx][j]);
                            OSA_assert(write_cnt == pBuf->filledBufSize);
                        }                 
                    }
                }                   

                /*The below check for max frames as 500, means write mv data only for first n frame*/
                if(pBuf->mvDataFilledSize != 0 && maxMvFrameCnt <= 500)
                {
                    maxMvFrameCnt++;
                    write_cnt = fwrite(pBuf->bufVirtAddr + pBuf->mvDataOffset,
                              sizeof(char), pBuf->mvDataFilledSize,fpMvBuf[fileIdx]);

                    OSA_assert(write_cnt == pBuf->mvDataFilledSize);
                }
            }

            if (FALSE == wrapOccuredHdr[fileIdx])
            {
                wrapOccuredHdr[fileIdx] =
                VcapVencVdecVdis_ipcBitsWriteWrap(fpHdr[fileIdx],sizeof(*pBuf),maxFileSize);
            }
            if (FALSE == wrapOccuredHdr[fileIdx])
            {
                write_cnt = fwrite(pBuf,sizeof(*pBuf),1,fpHdr[fileIdx]);
                OSA_assert(write_cnt == 1);
            }
        }
    }
}