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

📄 umc_dv_dec_con.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) 2007 Intel Corporation. All Rights Reserved.
//
*/

#include <stdio.h>
#include <ipp.h>
#include "umc_structures.h"
#include "umc_media_data.h"
#include "umc_video_data.h"
#include "umc_color_space_conversion.h"
#include "umc_dv100_decoder.h"

#define MAX_FILELEN 1024

#pragma warning(disable : 4996)

using namespace UMC;

int PrintHelp(vm_char *prog_name, vm_char *err_message)
{
    vm_string_printf(VM_STRING("Attention: %s\n"), err_message);
    vm_string_printf(VM_STRING("Usage: %s dv_hd -n Num -w Width -h Height -f Framerate -c ColorFormat -i InputPureDVFile -o OutputDecodedYUVFile\n"), prog_name);
    vm_string_printf(VM_STRING(" dv_hd - Digital Video High Definition, DV100\n"));
    vm_string_printf(VM_STRING(" -n Num - Num Number frame to decode\n"));
    vm_string_printf(VM_STRING(" -w Width - Width is a width of yuv frame\n"));
    vm_string_printf(VM_STRING(" -h Height - Height is a height of yuv frame\n"));
    vm_string_printf(VM_STRING(" -f FrameRate - FrameRate\n"));
    vm_string_printf(VM_STRING(" -c ColorFormat - output Color Format, only YUV420, YUY2 and UYVY are possible \n"));
    vm_string_printf(VM_STRING("Example: >umc_dv_dec_con dv_hd -n 10 -w 1280 -h 720 -f 60 -c yuv420 -i dv100_stream.dv100 -o decoded.yuv\n"));
    vm_string_printf(VM_STRING("\n"));
    return 1;
}

int main(int argc, vm_char* argv[])
{
    vm_char inputFileName[MAX_FILELEN];
    vm_char outputFileName[MAX_FILELEN];
    FILE* input_file;
    FILE* output_file;

    int frameRate = 0;
    int numReadBytes = 0, frameReaded = 0, frameWrote = 0;
    int sizeOfYUVFrame = 0, sizeOfYPlane = 0, sizeOfUVPlane = 0;
    int frameSize = 0, numOfFramesToDecode = 0;
    int m_Height = 0, m_Width = 0;

    Ipp8u *tempBuffer;
    MediaData dataIn;
    VideoData dataOut, dataOutUYVY;
    ColorFormat targetColorFormat = YUV420;
    Status umcRes = UMC_OK;
    VideoDecoderParams DVDecParams;
    DV100VideoDecoder DV100Dec;
    DV100_SystemType system_dv100;
    ColorSpaceConversion cc;
    //Initialization of static ipp libraries
    if (ippStaticInit() < ippStsNoErr)
    {
        vm_string_fprintf(vm_stderr, VM_STRING("Error: Can't initialize ipp libs!\n"));
        return 1;
    }

    //Check & parse arguments
    if (NULL == argv[1] )
        return PrintHelp(argv[0], VM_STRING("Unknown options"));
    //if ( (NULL != argv[1]) && (argc < 12))
    //    return PrintHelp(argv[0], VM_STRING("Wrong options"));
    if (0 != vm_string_strcmp("dv_hd", argv[1]))
        return PrintHelp(argv[0], VM_STRING("Wrong options: dv_hd identification missed"));

    for (Ipp32s i = 2; i < argc; i+=2)
    {
        if ('-' != argv[i][0])
            return PrintHelp(argv[0], VM_STRING("Wrong options:"));
        else
        {
            switch (argv[i][1])
            {
            case 'w':
              m_Width = vm_string_atol(argv[i+1]);
              break;
            case 'h':
              m_Height = vm_string_atol(argv[i+1]);
              break;
            case 'f':
              frameRate = vm_string_atol(argv[i+1]);
              break;
            case 'n':
              numOfFramesToDecode = vm_string_atol(argv[i+1]);
              break;
            case 'i':
              vm_string_strcpy(inputFileName, argv[i+1]);
              break;
            case 'o':
              vm_string_strcpy(outputFileName, argv[i+1]);
              break;
            case 'c':
                {
                if ((0 == vm_string_strcmp("yuv420", argv[i+1])) || (0 == vm_string_strcmp("YUV420", argv[i+1])))
                    targetColorFormat = YUV420;
                else if ((0 == vm_string_strcmp("yuy2", argv[i+1])) || (0 == vm_string_strcmp("YUY2", argv[i+1])))
                    targetColorFormat = YUY2;
                else if ((0 == vm_string_strcmp("uyvy", argv[i+1])) || (0 == vm_string_strcmp("UYVY", argv[i+1])))
                    targetColorFormat = UYVY;
                }
              break;
            default:
              return PrintHelp(argv[0], VM_STRING("Wrong options:"));
            }
        }
    }

    //Default initialization: DV100 720/60p mode
    system_dv100 = System720_60p;
    frameSize = 4*120000;
    DVDecParams.info.framerate = 59.94;
    DVDecParams.info.clip_info.height = m_Height;
    DVDecParams.info.clip_info.width = m_Width;

    //Set DV100 systems
    //And Initialization of DV DVDecParams
    if (((960 == m_Width) && (720 == m_Height)) || ((1280 == m_Width) && (720 == m_Height)))
    {
        if (50 == frameRate)
        {
            system_dv100 = System720_50p;
            frameSize = 4*144000;
            DVDecParams.info.framerate = 50;
            DVDecParams.info.clip_info.height = m_Height;
            DVDecParams.info.clip_info.width = m_Width;
        }
        else if ((60 == frameRate) || (59 == frameRate) || (59.94 == frameRate))
        {
            system_dv100 = System720_60p;
            frameSize = 4*120000;
            DVDecParams.info.framerate = 59.94;
            DVDecParams.info.clip_info.height = m_Height;
            DVDecParams.info.clip_info.width = m_Width;
        }
    }

    if (((1440 == m_Width) && (1080 == m_Height)) || ((1920 == m_Width) && (1080 == m_Height)))
    {
        if (25 == frameRate)
        {
            system_dv100 = System1080_50i;
            frameSize = 4*144000;
            DVDecParams.info.framerate = 25;
            DVDecParams.info.clip_info.height = m_Height;
            DVDecParams.info.clip_info.width = m_Width;
        }
    }
    if (((1280 == m_Width) && (1080 == m_Height)) || ((1920 == m_Width) && (1080 == m_Height)))
    {
        if ((30 == frameRate) || (29 == frameRate)||(29.97 == frameRate))
        {
            system_dv100 = System1080_60i;
            frameSize = 4*120000;
            DVDecParams.info.framerate = 29.94;
            DVDecParams.info.clip_info.height = m_Height;
            DVDecParams.info.clip_info.width = m_Width;
        }
    }
    //Number of threads
    DVDecParams.numThreads = 1;
    //Flags
    DVDecParams.lFlags = 0;
    //Color format
    DVDecParams.info.color_format = YUV422;
    //Stream type
    DVDecParams.info.stream_type = DIGITAL_VIDEO_HD;
    //Decoder initialization
    umcRes = DV100Dec.Init(&DVDecParams);
    if (UMC_OK != umcRes)
    {
        vm_string_printf(VM_STRING("Can't Init DV100 decoder\n"));
        return 0;
    }
    vm_string_printf(VM_STRING("Init of DV100 decoder is successful \n"));
    //Init output buffer
    dataOut.Init(m_Width, m_Height, 3, 8);
    umcRes = dataOut.SetColorFormat(targetColorFormat);
    dataOut.Alloc();
    if (YUV420 == targetColorFormat)
    {
        sizeOfYUVFrame = 3*(m_Height * m_Width)/2;
        sizeOfYPlane = m_Height * m_Width;
        sizeOfUVPlane = (m_Height * m_Width)/4;
    }
    if ((YUY2 == targetColorFormat)||(UYVY == targetColorFormat))
    {
        sizeOfYUVFrame = 2*(m_Height * m_Width);
        sizeOfYPlane = 0;
        sizeOfUVPlane = 0;
    }
    //Init & Alloc UYVY buffer
    //dataOutUYVY.Init(m_Width, m_Height, UYVY);
    //dataOutUYVY.Init(m_Width, m_Height, 3,8);
    //dataOutUYVY.SetColorFormat(UYVY);
    //dataOutUYVY.Alloc();

    //Open files
    input_file = fopen(inputFileName,"rb");
    if (NULL == input_file)
    {
        vm_string_printf(VM_STRING("Can't open input_file: %s\n"), inputFileName);
        return 0;
    }
    output_file = fopen(outputFileName,"wb");
    if (NULL == output_file)
    {
        vm_string_printf(VM_STRING("Can't create output_file: %s\n"), outputFileName);
        return 0;
    }

    vm_string_printf(VM_STRING("Start decoding...\n"));
    //Alloc buffer for 1 frame
    tempBuffer = ippsMalloc_8u(frameSize);
    //main decoding loop
    frameReaded = 0;
    while (frameWrote < numOfFramesToDecode)
    {
        if (0 == dataIn.GetDataSize())
        {
            fseek(input_file, frameSize *frameReaded ,SEEK_SET);
            numReadBytes = (int)fread(tempBuffer, sizeof(char), frameSize,  input_file);
            if (numReadBytes < frameSize)
            {
                vm_string_printf(VM_STRING("Can't read frame number %d from %s\n"), (frameWrote + 1), inputFileName);
                break;
            }
            frameReaded++;
            //lets check what was readed
            {
                Ipp8u vaux_framerate = 0;
                Ipp8u vaux_system = 0;
                Ipp8u byte_PC0_of_VAUX  = 0;
                Ipp8u byte_PC3_of_VAUX = 0;
                byte_PC0_of_VAUX = (Ipp8u) tempBuffer[240 + 48 + 0 + 2*80];
                byte_PC3_of_VAUX = (Ipp8u) tempBuffer[240 + 48 + 3 + 2*80];

                //check VAUX section
                if (0x60 != byte_PC0_of_VAUX)
                {
                    vm_string_printf(VM_STRING("Wrong input frame.\n"));
                    vm_string_printf(VM_STRING("Please, check parameters \n"));
                    break;
                }
                else
                {
                    vaux_framerate = (byte_PC3_of_VAUX>>5) &0x1;
                    vaux_system = byte_PC3_of_VAUX &0x1f;
                    if ((0 == vaux_framerate) &&( (0x14 == vaux_system) || (0x15 == vaux_system)) )
                    {
                        if (System1080_60i != system_dv100)
                        {
                            vm_string_printf(VM_STRING("Wrong initialization of DV100 decoder or input frame has another dv100 type.\n"));
                            vm_string_printf(VM_STRING("Please, check parameters \n"));
                            break;
                        }
                    }
                    if ((0 == vaux_framerate) && (0x18 == vaux_system) )
                    {
                        if (System720_60p != system_dv100)
                        {
                            vm_string_printf(VM_STRING("Wrong initialization of DV100 decoder or input frame has another dv100 type.\n"));
                            vm_string_printf(VM_STRING("Please, check parameters \n"));
                            break;
                        }
                    }
                    if ((1 == vaux_framerate) && (0x14 == vaux_system) )
                    {
                        if (System1080_50i != system_dv100)
                        {
                            vm_string_printf(VM_STRING("Wrong initialization of DV100 decoder or input frame has another dv100 type.\n"));
                            vm_string_printf(VM_STRING("Please, check parameters \n"));
                            break;
                        }
                    }
                    if ((1 == vaux_framerate) && (0x18 == vaux_system) )
                    {
                        if (System720_50p != system_dv100)
                        {
                            vm_string_printf(VM_STRING("Wrong initialization of DV100 decoder or input frame has another dv100 type.\n"));
                            vm_string_printf(VM_STRING("Please, check parameters \n"));
                            break;
                        }
                    }
                }
            }
            dataIn.SetBufferPointer((Ipp8u *)tempBuffer, frameSize);
            dataIn.SetDataSize(frameSize);
        }
        umcRes = DV100Dec.GetFrame(&dataIn, &dataOut);
        if (UMC_OK != umcRes)
        {
            vm_string_printf(VM_STRING("Decoder can't decode frame number %d from %s\n"), (frameWrote + 1), inputFileName);
            break;
        }

        //saving decoded data
        if ((YUY2 == targetColorFormat)||(UYVY == targetColorFormat))
        {
            fwrite(dataOut.GetPlanePointer(0), sizeof(char), sizeOfYUVFrame, output_file);
            //fwrite(dataOutUYVY.GetPlanePointer(0), sizeof(char), sizeOfYUVFrame, output_file);
        }
        else // ((YUV420 == targetColorFormat) )
        {
            fwrite(dataOut.GetPlanePointer(0), sizeof(char), sizeOfYPlane, output_file);
            fwrite(dataOut.GetPlanePointer(1), sizeof(char), sizeOfUVPlane, output_file);
            fwrite(dataOut.GetPlanePointer(2), sizeof(char), sizeOfUVPlane, output_file);
        }
        frameWrote++;
    }

    vm_string_printf(VM_STRING("%d numbers of frames were decoded and written to %s\n"), frameWrote, outputFileName);
    //The End
    //Close files and free memory
    fclose(input_file);
    fclose(output_file);
    ippsFree(tempBuffer);
    return 0;
}

⌨️ 快捷键说明

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