www.pudn.com > w_ipp-sample-media_p_5.0.017.zip > video_enc_utils.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) 2005 Intel Corporation. All Rights Reserved. 
// 
 
//ipp 
#include "ipp.h" 
 
//mpeg2 
#include "umc_mpeg2_video_encoder.h" 
//h264 
#include "umc_h264_video_encoder.h" 
//mpeg4 
#include "umc_mpeg4_video_encoder.h" 
//h263 
#include "umc_h263_video_encoder.h" 
//h261 
#include "umc_h261_video_encoder.h" 
 
using namespace UMC; 
 
#include  
 
/* implementation of UMCReadYUV class */ 
 
UMCReadYUV::UMCReadYUV(const vm_char *yuvFileName, int yuvWidth, int yuvHeight, ColorFormat yuvColorFormat, int yuvType) 
{ 
  mIsInit = false; 
  Init(yuvFileName, yuvWidth, yuvHeight, yuvColorFormat, yuvType); 
} 
 
int UMCReadYUV::Init(const vm_char *yuvFileName, int yuvWidth, int yuvHeight, ColorFormat yuvColorFormat, int yuvType) 
{ 
  Close(); 
  mType = yuvType; 
  mColorFormat = yuvColorFormat; 
  mWidth = yuvWidth; 
  mHeight = yuvHeight; 
  mFrameCount = 0; 
  mFile = vm_file_open(yuvFileName, VM_STRING("rb")); 
  if (!mFile) { 
    mError = YUV_ERROR_FILE_OPEN; 
    return  mError; 
  } 
  mIsInit = true; 
  mError = YUV_ERROR_NOERR; 
 
  switch (mColorFormat) { 
      default: 
      case YV12: 
        mWidthChroma = mWidth/2; 
        mHeightChroma = mHeight/2; 
        break; 
      case YUV422: 
        mWidthChroma = mWidth/2; 
        mHeightChroma = mHeight; 
        break; 
      case YUV444: 
        mWidthChroma = mWidth; 
        mHeightChroma = mHeight; 
        break; 
  } 
 
  mFrameSize = mWidth*mHeight + 2*mWidthChroma*mHeightChroma; 
 
  return YUV_ERROR_NOERR; 
} 
 
UMCReadYUV::~UMCReadYUV() 
{ 
  Close(); 
} 
 
void UMCReadYUV::Close() 
{ 
  if (mIsInit) { 
    fclose(mFile); 
  } 
  mIsInit = false; 
} 
 
int UMCReadYUV::LoadNextFrame(VideoData *data) 
{ 
  Ipp8u* pY = (Ipp8u*)data->m_lpDest[0]; 
  Ipp8u* pU = (Ipp8u*)data->m_lpDest[1]; 
  Ipp8u* pV = (Ipp8u*)data->m_lpDest[2]; 
  int    stepY = (int)data->m_lPitch[0]; 
  int    stepU = (int)data->m_lPitch[1]; 
  int    stepV = (int)data->m_lPitch[2]; 
  int    count, i; 
 
  if (!mIsInit) return YUV_ERROR_NOTINIT; 
 
  // simple yuv frame 
  if (mType == 0) { 
    for (i = 0; i < mHeight; i ++) { 
      count = (int)fread(pY + i * stepY, 1, mWidth, mFile); 
      if (count != mWidth) { 
        mError = YUV_ERROR_FILE_READ; 
        vm_string_printf(VM_STRING("Can't read Y (count=%d): mHeight = %d, mWidth =%d, step = %d \n"), 
          count,  mHeight , mWidth, stepY ); 
        return mError; 
      } 
    } 
    for (i = 0; i < mHeightChroma; i ++) { 
      count = (int)fread(pU + i * stepU, 1, mWidthChroma, mFile); 
      if (count != mWidthChroma) { 
        mError = YUV_ERROR_FILE_READ; 
        vm_string_printf(VM_STRING("Can't read U (count=%d): mHeight = %d, mWidth =%d, step = %d \n"), 
          count, mHeightChroma, mWidthChroma, stepU); 
        return mError; 
      } 
    } 
    for (i = 0; i < mHeightChroma; i ++) { 
      count = (int)fread(pV + i * stepV, 1, mWidthChroma, mFile); 
      if (count != mWidthChroma) { 
        mError = YUV_ERROR_FILE_READ; 
        vm_string_printf(VM_STRING("Can't read V (count=%d): mHeight = %d, mWidth =%d, step = %d \n"), 
          count, mHeightChroma, mWidthChroma, stepV); 
        return mError; 
      } 
    } 
  } 
 
  mFrameCount++; 
 
  data->SetDataSize(mHeight*stepY + mHeightChroma*stepU + mHeightChroma*stepV); 
 
  return YUV_ERROR_NOERR; 
} 
 
/* Implementation of ReadH264EncoderParams */ 
 
int ReadH264EncoderParams(vm_char *ParFileName, H264EncoderParams* pParams, vm_char *SrcFileName, int lenSrcFileName ) 
{ 
  if (( ParFileName==NULL ) || ( pParams==NULL ) || ( SrcFileName==NULL )) 
  { 
    vm_string_fprintf(stderr,VM_STRING("Error: null pointers!\n") ); 
    return 1; 
  } 
 
  if ( pParams->ReadParamFile(ParFileName)!= UMC_OK ) 
  { 
    vm_string_fprintf(stderr,VM_STRING("Error: failed reading parfile!\n") ); 
    return 1; 
  } 
  if ((int)vm_string_strlen(pParams->SrcFName) > lenSrcFileName ) 
  { 
    vm_string_fprintf(stderr,VM_STRING("Error: can't pass filename %s, len=%d, but we have =%d \n"), pParams->SrcFName, (int)vm_string_strlen(pParams->SrcFName),lenSrcFileName ); 
    return 1; 
  } 
 
  vm_string_strcat(SrcFileName,pParams->SrcFName); 
  return 0; 
} 
 
/* Implementation of ReadMPEG4EncoderParams */ 
 
int ReadMPEG4EncoderParams(vm_char *ParFileName, MPEG4EncoderParams* pParams, vm_char *SrcFileName, int lenSrcFileName ) 
{ 
  if (( !ParFileName ) || ( !pParams ) || ( !SrcFileName )){ 
    vm_string_fprintf(stderr, VM_STRING("Error: null pointers!\n") ); 
    return 1; 
  } 
 
  if ( pParams->ReadParamFile(ParFileName)!= UMC_OK ){ 
    vm_string_fprintf(stderr, VM_STRING("Error: failed reading parfile!\n") ); 
    return 1; 
  } 
 
  if ((int)vm_string_strlen(pParams->m_SourceFileName) > lenSrcFileName ){ 
    vm_string_fprintf(stderr, VM_STRING("Error: can't pass filename %s, len=%d, but we have =%d \n"), pParams->m_SourceFileName, (int)vm_string_strlen(pParams->m_SourceFileName),lenSrcFileName ); 
    return 1; 
  } 
 
  vm_string_strcat(SrcFileName,pParams->m_SourceFileName); 
 
  return 0; 
} 
 
/* Implementation of ReadMPEG2EncoderParams */ 
 
int ReadMPEG2EncoderParams(vm_char *ParFileName, MPEG2EncoderParams* pParams, vm_char *SrcFileName, int lenSrcFileName ) 
{ 
  vm_char SrcFName[512]; 
 
  if (( !ParFileName ) || ( !pParams ) || ( !SrcFileName )){ 
    vm_string_fprintf(stderr, VM_STRING("Error: null pointers!\n") ); 
    return 1; 
  } 
 
  if ( pParams->ReadParamFile(ParFileName, SrcFName) != UMC_OK ) { 
    vm_string_fprintf(stderr, VM_STRING("Error: failed reading parfile!\n") ); 
    return 1; 
  } 
 
  vm_string_strcat(SrcFileName, SrcFName); 
 
  return 0; 
} 
 
/* Implementation of ReadH263EncoderParams */ 
 
int ReadH263EncoderParams(vm_char *ParFileName, H263EncoderParams* pParams, vm_char *SrcFileName, int lenSrcFileName ) 
{ 
    if (( !ParFileName ) || ( !pParams ) || ( !SrcFileName )){ 
        vm_string_fprintf(stderr, VM_STRING("Error: null pointers!\n") ); 
        return 1; 
    } 
 
    if ( pParams->ReadParamFile(ParFileName)!= UMC_OK ){ 
        vm_string_fprintf(stderr, VM_STRING("Error: failed reading parfile!\n") ); 
        return 1; 
    } 
 
    if ((int)vm_string_strlen(pParams->m_SourceFileName) > lenSrcFileName ){ 
        vm_string_fprintf(stderr, VM_STRING("Error: can't pass filename %s, len=%d, but we have =%d \n"), pParams->m_SourceFileName, (int)vm_string_strlen(pParams->m_SourceFileName),lenSrcFileName ); 
        return 1; 
    } 
 
    vm_string_strcat(SrcFileName,pParams->m_SourceFileName); 
 
    return 0; 
} 
 
/* Implementation of ReadH261EncoderParams */ 
 
int ReadH261EncoderParams(vm_char *ParFileName, H261EncoderParams* pParams, vm_char *SrcFileName, int lenSrcFileName ) 
{ 
    if (( !ParFileName ) || ( !pParams ) || ( !SrcFileName )){ 
        vm_string_fprintf(stderr, VM_STRING("Error: null pointers!\n")); 
        return 1; 
    } 
 
    if ( pParams->ReadParamFile(ParFileName)!= UMC_OK ){ 
        vm_string_fprintf(stderr, VM_STRING("Error: failed reading parfile!\n")); 
        return 1; 
    } 
 
    if ((int)vm_string_strlen(pParams->m_SourceFileName) > lenSrcFileName ){ 
        vm_string_fprintf(stderr, VM_STRING("Error: can't pass filename %s, len=%d, but we have =%d \n"), pParams->m_SourceFileName, (int)vm_string_strlen(pParams->m_SourceFileName),lenSrcFileName ); 
        return 1; 
    } 
 
    vm_string_strcat(SrcFileName,pParams->m_SourceFileName); 
 
    return 0; 
} 
 
/*End of file*/