⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 umc_file_reader.cpp

📁 audio-video-codecs.rar语音编解码器
💻 CPP
字号:
/*
//
//                  INTEL CORPORATION PROPRIETARY INFORMATION
//     This software is supplied under the terms of a license agreement or
//     nondisclosure agreement with Intel Corporation and may not be copied
//     or disclosed except in accordance with the terms of that agreement.
//          Copyright(c) 2003-2007 Intel Corporation. All Rights Reserved.
//
*/

#include "umc_defs.h"
#include "umc_file_reader.h"

#if defined (UMC_ENABLE_FILE_READER) || defined (UMC_ENABLE_FIO_READER)

#include <string.h>
#include "vm_debug.h"
#include "umc_dynamic_cast.h"

UMC::FileReader::FileReader():
    m_pbBuffer(NULL),
    m_stCurPortionSize(0),
    m_uiPortionSize(0),
    m_stDoneSize(0),
    m_stFileSize(0),
    m_uiPageSize(0),
    m_bBufferInit(false),
    m_bLastPortion(true)
{
    vm_mmap_set_invalid(&m_mmap);
}

UMC::FileReader::~FileReader()
{
    Close();
}

UMC::Status
UMC::FileReader::Init(DataReaderParams *pInit)
{
    UMC::Status umcRes = UMC_OK;

    FileReaderParams* initFileExt = DynamicCast<FileReaderParams, DataReaderParams>(pInit);

    if(!initFileExt)
        return UMC_ERR_NULL_PTR;

//    VM_ASSERT(NULL != init->InitExt);
//    sFileRdrInitExt* initFileExt = (sFileRdrInitExt*)init->InitExt;

    m_uiPageSize = vm_mmap_get_alloc_granularity();
    VM_ASSERT(0 != m_uiPageSize);

    if (UMC_OK == umcRes) {
        m_stFileSize = vm_mmap_create(&m_mmap, initFileExt->m_file_name, FLAG_ATTRIBUTE_READ);
        if (0 == m_stFileSize) {
            vm_debug_trace1(VM_DEBUG_INFO, VM_STRING("File %s open error\n"),
                            initFileExt->m_file_name);
            umcRes = UMC_ERR_OPEN_FAILED;
        }
    }

    if (UMC_OK == umcRes) {
        if (initFileExt->m_portion_size > 0 &&
            initFileExt->m_portion_size / m_uiPageSize > 0)
        {
            m_uiPortionSize =
                (initFileExt->m_portion_size/m_uiPageSize)*m_uiPageSize;
        } else
        {   m_uiPortionSize = m_uiPageSize * 188;   }

        Ipp64u csize = m_uiPortionSize;
        m_bLastPortion = false;
        if (m_stFileSize < csize)
        {   csize = m_stFileSize;   }

        Ipp64u offset = 0;
        m_bBufferInit = false;
        m_pbBuffer = (Ipp8u *)vm_mmap_set_view(&m_mmap, &offset, &csize);
        m_stCurPortionSize  = csize;

        if (!m_pbBuffer) {
            umcRes = UMC_ERR_ALLOC;
            vm_debug_trace1(VM_DEBUG_INFO, VM_STRING("File %s map error\n"),
                            initFileExt->m_file_name);
        } else {
            m_pDataPointer = m_pbBuffer;
            m_pEODPointer  = m_pDataPointer + csize;
        }
    }

    if (UMC_OK == umcRes) {
        m_bBufferInit   = true;
        m_stDoneSize    = 0;
    }
    else
    {   Close();    }

    return umcRes;
}

UMC::Status UMC::FileReader::Close()
{
    m_bBufferInit = false;
    vm_mmap_close(&m_mmap);
    m_pbBuffer          = 0;
    m_pDataPointer      = 0;
    m_pEODPointer       = 0;
    m_stCurPortionSize  = 0;
    m_stDoneSize        = 0;
    m_uiPortionSize     = 0;

    return UMC_OK;
}

UMC::Status UMC::FileReader::Reset()
{
    UMC::Status umcRes = UMC_OK;
    m_bLastPortion  = false;

    if (!vm_mmap_is_valid(&m_mmap)) {   umcRes = UMC_ERR_NOT_INITIALIZED;   }

    if(UMC_OK == umcRes && 0 != m_stDoneSize)
    {
        Ipp64u csize, offset;

        m_bBufferInit = false;
        vm_mmap_unmap(&m_mmap);
        m_pbBuffer = 0;

        csize =
            (m_stFileSize < m_uiPortionSize) ? m_stFileSize : m_uiPortionSize;
        offset = 0;
        m_pbBuffer = (Ipp8u *)vm_mmap_set_view(&m_mmap, &offset, &csize);
        m_stCurPortionSize  = m_uiPortionSize;

        if (!m_pbBuffer)
            umcRes = UMC_ERR_ALLOC;
        else {
            m_bBufferInit = true;
            m_pDataPointer = m_pbBuffer;
            m_pEODPointer  = m_pDataPointer + csize;
        }
    }
    m_stDoneSize    = 0;

    return umcRes;
}

UMC::Status UMC::FileReader::MapCSize(Ipp64u csize)
{
    Ipp64u tmp_size;
    Ipp32u stPos = m_pDataPointer - m_pbBuffer;

    if (!vm_mmap_is_valid(&m_mmap)) {   return UMC_ERR_NOT_INITIALIZED;   }

    if(m_stDoneSize + m_stCurPortionSize >= m_stFileSize)
        return UMC_ERR_END_OF_STREAM;
    tmp_size = (m_stDoneSize + stPos) % m_uiPageSize;
    m_stDoneSize = (m_stDoneSize + stPos) - tmp_size;
    if(m_bLastPortion)
        return UMC_ERR_END_OF_STREAM;
    if(m_stDoneSize + m_uiPortionSize >= m_stFileSize)
    {
        m_stCurPortionSize  = m_stFileSize - m_stDoneSize;
        m_bLastPortion      = true;
    }
    else
        m_stCurPortionSize  = m_uiPortionSize;
    m_bBufferInit = false;
    m_pbBuffer = (Ipp8u *)vm_mmap_set_view(&m_mmap, &m_stDoneSize, &m_stCurPortionSize);
    if (!m_pbBuffer)
    {
        if(m_stDoneSize + csize> m_stFileSize)
            return UMC_ERR_END_OF_STREAM;
        else
            return UMC_ERR_ALLOC;
    } else {
        m_pDataPointer = m_pbBuffer + tmp_size;
        m_pEODPointer  = m_pbBuffer + m_stCurPortionSize;
    }
    m_bBufferInit = true;

    return UMC_OK;
}

UMC::Status UMC::FileReader::ReadData(void *data, Ipp32u *nsize)
{
    UMC::Status ret = UMC_OK;
    Ipp32u tmp_size = 0;
    Ipp32u stPos = m_pDataPointer - m_pbBuffer;

    if (!vm_mmap_is_valid(&m_mmap)) {   return UMC_ERR_NOT_INITIALIZED;   }

    if ((stPos + *nsize > m_stCurPortionSize || !m_bBufferInit))
    {
        ret = MapCSize(*nsize);
        if (ret == UMC_OK  || ret == UMC_ERR_END_OF_STREAM)
        {
            tmp_size = (Ipp32u)(m_stFileSize - m_stDoneSize - (stPos));
            if(tmp_size < *nsize && tmp_size != 0)
            {
                memcpy(data, m_pDataPointer, tmp_size);
                m_pDataPointer += tmp_size;
                m_bLastPortion = true;
                ret = UMC_ERR_END_OF_STREAM;
            }
            else
                stPos = m_pDataPointer - m_pbBuffer;
        }
    }

    if (UMC_OK == ret) {
        Ipp32u get_size = *nsize;
        if(stPos + *nsize > m_stCurPortionSize)
        {
            get_size = static_cast<Ipp32u>(m_stCurPortionSize - (stPos));
        }
        memcpy(data, m_pDataPointer, get_size);
        m_pDataPointer += get_size;
        *nsize = get_size;
    }
    else
    {
        *nsize = tmp_size;
    }
    return ret;
}

UMC::Status UMC::FileReader::CacheData(void *data, Ipp32u *nsize, Ipp32s how_far)
{
    UMC::Status  ret = UMC_OK;
    Ipp32u     stPos = m_pDataPointer - m_pbBuffer;

    if (!vm_mmap_is_valid(&m_mmap)) {   ret = UMC_ERR_NOT_INITIALIZED;   }

    if (UMC_OK == ret &&
        (stPos + how_far + *nsize > m_stCurPortionSize || !m_bBufferInit))
    {   ret = MapCSize(*nsize); }

    if (UMC_OK == ret)
    {   memcpy(data, m_pDataPointer + how_far, *nsize);   }

    return ret;
}

UMC::Status UMC::FileReader::MovePosition(Ipp64u npos)
{
    UMC::Status ret = UMC_OK;
    Ipp32u    stPos = m_pDataPointer - m_pbBuffer;

    if (!vm_mmap_is_valid(&m_mmap)) {   ret = UMC_ERR_NOT_INITIALIZED;   }

    if (UMC_OK == ret) {
        if( (stPos + npos <= m_stCurPortionSize) || npos <= 0 || !m_bBufferInit)
        {   m_pDataPointer += npos; }
        else
        {
            ret = MapCSize(npos);
            if (UMC_OK == ret) {
              m_pDataPointer += npos;
              if ((m_pEODPointer - m_pbBuffer) < (m_pDataPointer - m_pbBuffer))
                m_pEODPointer = m_pDataPointer;
            }
            else if (UMC_ERR_END_OF_STREAM == ret)
            {
                m_stDoneSize = m_stFileSize;
                m_stCurPortionSize = 0;
                m_bBufferInit = false;
                m_bLastPortion = true;
            }
        }
    }
    return ret;
}

Ipp64u UMC::FileReader::GetPosition()
{
    Ipp32u stPos = m_pDataPointer - m_pbBuffer;

    return (m_stDoneSize + stPos);
}

UMC::Status UMC::FileReader::SetPosition(Ipp64f position)
{
    UMC::Status ret = UMC_OK;
    Ipp32u    stPos = m_pDataPointer - m_pbBuffer;

    m_bLastPortion  = false;

    if ((!vm_mmap_is_valid(&m_mmap)) || (!m_uiPortionSize)) {   return UMC_ERR_NOT_INITIALIZED;   }

    Ipp64u    new_pos = (Ipp64u)(position * (Ipp64f)(Ipp64s)m_stFileSize + 0.5);
    Ipp32u    new_portion = (Ipp32u)(new_pos/m_uiPortionSize + 1);
    Ipp32u    cur_portion = (Ipp32u)((m_stDoneSize + stPos)/m_uiPortionSize + 1);
    //if(new_portion != portion ){

    if(new_portion != cur_portion)
    {
        m_pDataPointer = m_pbBuffer;
        m_stCurPortionSize = 0;
        m_stDoneSize = (Ipp64u)(new_portion - 1) * m_uiPortionSize;
        ret = MapCSize(0);
    }
    else
    {
        m_stDoneSize = (Ipp64u)(new_portion - 1) * m_uiPortionSize;
    }

    if(ret == UMC_OK)
        m_pDataPointer = m_pbBuffer + (new_pos-m_stDoneSize);
    return ret;
}

Ipp64u UMC::FileReader::GetSize()
{
    return m_stFileSize;
}

#endif // (UMC_ENABLE_FILE_READER)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -