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

📄 umc_vob_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_vob_reader.h"
#include "ippdefs.h"
#include "ipps.h"
#include "vm_debug.h"
#include "vm_strings.h"
#include "umc_dynamic_cast.h"

UMC::VobReader::VobReader()
{
    m_vob_size = NULL;
    m_vob_dig = NULL;
    m_vob_list = NULL;
}

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

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

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

    if(!initVobExt)
        return UMC_ERR_NULL_PTR;

    m_vob_num = 0;
    m_vob_current = 0;
    m_vob_size_sum = 0;

    umcRes = FindVobFiles(initVobExt->m_file_name);
    if (UMC_OK != umcRes)
        return umcRes;

    UMC::FileReaderParams FileParams;
    FileParams.m_portion_size = 0;

    vm_string_strcpy(FileParams.m_file_name, m_vob_list[m_vob_current]);
    vm_debug_trace1(VM_DEBUG_ALL, VM_STRING("Init.Open %d\n"), m_vob_current);
    umcRes = FileReaderCur.Init(&FileParams);

    return umcRes;
}

UMC::Status UMC::VobReader::Close()
{
    UMC::Status umcRes = UMC_OK;

    umcRes = FileReaderCur.Close();
//FileReaderNxt.Close();

    if(m_vob_size)
    {
    free(m_vob_size);
    }
    if(m_vob_dig)
    {
    free(m_vob_dig);
    }
    if(m_vob_list)
    {
        for(Ipp32s j=0; j<m_vob_num; j++)
        {
            if(m_vob_list[j])
            {
                free(m_vob_list[j]);
            }
        }
        free(m_vob_list);
    }
    return umcRes;
}

UMC::Status UMC::VobReader::Reset()
{
    UMC::Status umcRes = UMC_OK;
    UMC::FileReaderParams FileParams;

    if(m_vob_current == 0) {
        umcRes = FileReaderCur.Reset();
        return umcRes;
    }

    umcRes = FileReaderCur.Close();

    if(umcRes == UMC_OK) {
        m_vob_current = 0;
        vm_string_strcpy(FileParams.m_file_name, m_vob_list[m_vob_current]);
        umcRes = FileReaderCur.Init(&FileParams);
    }
    return umcRes;
}

UMC::Status UMC::VobReader::ReadData(void *data, Ipp32u *nsize)
{
    UMC::Status umcRes;

    if((m_vob_size[m_vob_current]-FileReaderCur.GetPosition()) < *nsize)
    {
        umcRes = FileReaderCur.GetData(data,nsize);

        if(m_vob_current < m_vob_num){

            vm_debug_trace1(VM_DEBUG_ALL, VM_STRING("GetData.Close %d\n"), m_vob_current);
            umcRes = FileReaderCur.Close();
            if (UMC_OK != umcRes)
                return umcRes;

            UMC::FileReaderParams FileParams;
            FileParams.m_portion_size = 0;

            vm_string_strcpy(FileParams.m_file_name, m_vob_list[++m_vob_current]);
            vm_debug_trace1(VM_DEBUG_ALL, VM_STRING("GetData.Open %d\n"), m_vob_current);
            umcRes = FileReaderCur.Init(&FileParams);
            if (UMC_OK != umcRes)
                return umcRes;
        } else {
            return UMC_ERR_END_OF_STREAM;
        }
    } else {
        umcRes = FileReaderCur.GetData(data, nsize);
        if (UMC_ERR_END_OF_STREAM == umcRes) {
            umcRes = UMC_OK;
        }
    }
    return umcRes;
}

UMC::Status UMC::VobReader::CacheData(void *data, Ipp32u *nsize, Ipp32s how_far)
{
    return FileReaderCur.CheckData(data, nsize, how_far);
}

UMC::Status UMC::VobReader::MovePosition(Ipp64u npos)
{
    UMC::Status umcRes = UMC_OK;

    Ipp64u curr_pos  = FileReaderCur.GetPosition();
    Ipp64u curr_sum  = 0;
    Ipp64u shift_pos = npos;
    Ipp32s curr_vob  = m_vob_current;

    if((curr_pos + npos) >= (m_vob_size[m_vob_current]-4)){
        if(m_vob_current < m_vob_num){

            vm_debug_trace1(VM_DEBUG_ALL, VM_STRING("MovePosition.Close %d\n"), curr_vob);
            FileReaderCur.Close();
            if (UMC_OK != umcRes)
                return umcRes;

            UMC::FileReaderParams FileParams;
            FileParams.m_portion_size = 0;

            for (;;) {
                curr_sum += m_vob_size[curr_vob];
                if ((curr_pos + shift_pos) < (curr_sum-4))
                    break;
                if(npos >= m_vob_size[curr_vob])
                npos -= m_vob_size[curr_vob];
                curr_vob++;
            }
            if(curr_vob >= m_vob_num)
                return UMC_ERR_END_OF_STREAM;

            m_vob_current = curr_vob;

            vm_string_strcpy(FileParams.m_file_name, m_vob_list[m_vob_current]);

            vm_debug_trace1(VM_DEBUG_ALL, VM_STRING("MovePosition.Open %d\n"), m_vob_current);
            umcRes = FileReaderCur.Init(&FileParams);
        } else {
            return UMC_ERR_END_OF_STREAM;
        }
    }

    umcRes = FileReaderCur.MovePosition(npos);

    FileReaderCur.StartReadingAfterReposition();

    if (UMC_OK != umcRes)
        return umcRes;

    return umcRes;
}

Ipp64u UMC::VobReader::GetPosition()
{
    Ipp64u pos = 0;
    for(Ipp32s i = 0;i<m_vob_current;i++){
        pos = pos + m_vob_size[i];
    }
    pos = pos + FileReaderCur.GetPosition();
    return pos;
}

UMC::Status UMC::VobReader::SetPosition(Ipp64f position)
{
    Ipp64s pos_1 = (Ipp64s)(((Ipp64f)((Ipp64s)m_vob_size_sum))*position);
    Ipp64s pos_2 = 0;
    Ipp32s num = 0;
    Ipp32s curr_num = 0;
    Ipp64f set_pos = 0.;

    for(Ipp32s i = 0;i<m_vob_num;i++){
        pos_2 += m_vob_size[i];
        num = i;
        if (pos_2 > pos_1) {
            break;
        }
        curr_num = i;
    }

    pos_2 = pos_2 - m_vob_size[num];
    pos_1 = pos_1 - pos_2;
    set_pos = (Ipp64f)pos_1/((Ipp64f)((Ipp64s)m_vob_size[curr_num]));

    if (m_vob_current != curr_num) {
        Status umcRes = UMC_OK;

        vm_debug_trace1(VM_DEBUG_ALL, VM_STRING("SetPosition(Ipp64f).Close %d\n"), m_vob_current);
        FileReaderCur.Close();
        if (UMC_OK != umcRes)
            return umcRes;

        UMC::FileReaderParams FileParams;
        FileParams.m_portion_size = 0;

        m_vob_current = curr_num;
        vm_string_strcpy(FileParams.m_file_name, m_vob_list[m_vob_current]);
        vm_debug_trace1(VM_DEBUG_ALL, VM_STRING("SetPosition.Open %d\n"), m_vob_current);
        umcRes = FileReaderCur.Init(&FileParams);
    }

    FileReaderCur.SetPosition(set_pos);
    return FileReaderCur.StartReadingAfterReposition();
}

Ipp64u UMC::VobReader::GetSize()
{
    return (Ipp64u)m_vob_size_sum;
}

UMC::Status UMC::VobReader::FindVobFiles(vm_char *file_name)
{
    UMC::Status umcRes = UMC_OK;
    vm_char     fname[MAXIMUM_PATH];
    vm_mmap     m_mmap;
    vm_char*    tmp_ptr;
    VobStruct   *vobPrevPtr, *vobCurrentPtr;

    Ipp32s      skipped_zero;

    vobPrevPtr     = vobCurrentPtr = NULL;
    m_vob_num      = 0;
    m_vob_size_sum = 0;
    skipped_zero   = 0;

    vm_string_strcpy(fname, file_name);

    tmp_ptr = (vm_char*)fname;
    // find last "_"
    while (vm_string_strstr(tmp_ptr, VM_STRING("_"))) {
        tmp_ptr = vm_string_strstr(tmp_ptr, VM_STRING("_")) + 1;
    }

    // skip "0"
    while (vm_string_strstr(tmp_ptr, VM_STRING("0"))) {
        tmp_ptr = vm_string_strstr(tmp_ptr, VM_STRING("0")) + 1;
        skipped_zero++;
    }
    // detect first number
    Ipp32s start_number = 0;
    vm_string_sscanf(tmp_ptr, VM_STRING("%d"), &start_number);

    vobCurrentPtr = (VobStruct*)malloc(sizeof(VobStruct));
    if (NULL == vobCurrentPtr)
        return UMC_ERR_ALLOC;
    vobCurrentPtr->prevPtr = NULL;

    // try to open file by file
    for(m_vob_num = 0;;m_vob_num++,start_number++) {
        if ( ( 10 == start_number && skipped_zero) ||
             (100 == start_number && skipped_zero))
        {
            tmp_ptr -= 1;
            skipped_zero--;
        }

        vm_string_sprintf(tmp_ptr, VM_STRING("%d.vob"), start_number);

        vm_string_strcpy(vobCurrentPtr->vob_name, fname);
        vobCurrentPtr->vob_size = vm_mmap_create(&m_mmap, vobCurrentPtr->vob_name, FLAG_ATTRIBUTE_READ);
        vobCurrentPtr->vob_dig  = start_number;
        vm_mmap_close(&m_mmap);
        m_vob_size_sum += vobCurrentPtr->vob_size;

        if (vobCurrentPtr->vob_size) {
            vobPrevPtr = vobCurrentPtr;
            vobCurrentPtr = (VobStruct*)malloc(sizeof(VobStruct));
            if (NULL == vobCurrentPtr)
                break;
            vobCurrentPtr->prevPtr = vobPrevPtr;
        } else {
            if (vobCurrentPtr)
                free(vobCurrentPtr);
            vobCurrentPtr = vobPrevPtr;
            break;
        }
    }

    if(m_vob_num > 0)
    {
        m_vob_size = (Ipp64u*)malloc(sizeof(Ipp64u)*m_vob_num);
        m_vob_dig  = (Ipp32s*)malloc(sizeof(Ipp32s)*m_vob_num);
        m_vob_list = (vm_char**)malloc(sizeof(vm_char*)*m_vob_num);

        if (m_vob_size && m_vob_dig && m_vob_list) {
            for(Ipp32s j=m_vob_num-1; j>=0; j--) {
                m_vob_list[j] = (vm_char*)malloc(sizeof(vm_char)*MAXIMUM_PATH);
                vm_string_strcpy(m_vob_list[j], vobCurrentPtr->vob_name);
                m_vob_dig[j] = vobCurrentPtr->vob_dig;
                m_vob_size[j] = vobCurrentPtr->vob_size;
                vobPrevPtr = vobCurrentPtr;
                vobCurrentPtr = vobCurrentPtr->prevPtr;
                if(vobPrevPtr!=NULL)
                    free(vobPrevPtr);
            }
        } else {
            for(Ipp32s j=m_vob_num-1; j>=0; j--){
                vobPrevPtr = vobCurrentPtr;
                vobCurrentPtr = vobCurrentPtr->prevPtr;
                if(vobPrevPtr!=NULL)
                    free(vobPrevPtr);
            }
            return UMC_ERR_ALLOC;
        }
    }
    else
    {
        if (vobCurrentPtr)
            free(vobCurrentPtr);
        // No VOB files found
        // NOTE: this is an expected case if the vob_reader is used
        // in an attempt to parse a media file that is not a VOB
        return UMC_ERR_INIT;
    }
    return umcRes;
}

⌨️ 快捷键说明

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