📄 umc_mpeg4_video_encoder.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-2005 Intel Corporation. All Rights Reserved.//*/#include "vm_debug.h"#include "vm_time.h"#include "umc_mpeg4_video_encoder.h"#include "umc_video_data.h"namespace UMC{MPEG4VideoEncoder::MPEG4VideoEncoder(){ m_IsInit = false;}MPEG4VideoEncoder::~MPEG4VideoEncoder(){ Close();}Status MPEG4VideoEncoder::Reset(){ return UMC_NOT_IMPLEMENTED;}Status MPEG4VideoEncoder::SetParams(BaseCodecParams* params){ return UMC_NOT_IMPLEMENTED;}Status MPEG4VideoEncoder::Init(BaseCodecParams* init){ MPEG4EncoderParams *pParam = DynamicCast<MPEG4EncoderParams> (init); if (NULL == pParam) return UMC_NULL_PTR; if (m_IsInit) Close(); m_Param = *pParam; int mp4status = mp4enc.Init(&pParam->m_Param); if (mp4status == MP4_STS_ERR_PARAM) return UMC_FAILED_TO_INITIALIZE; if (mp4status == MP4_STS_ERR_NOMEM) return UMC_ALLOC; m_FrameCount = 0; if (pParam->m_Param.BVOPdist) { bTime = new double [pParam->m_Param.BVOPdist]; if (!bTime) return UMC_ALLOC; } bTimePos = 0; m_IsInit = true; return UMC_OK;}Status MPEG4VideoEncoder::GetInfo(BaseCodecParams* info){ MPEG4EncoderParams* pParam = DynamicCast<MPEG4EncoderParams>(info); if (!m_IsInit) return UMC_NOT_INITIALIZED; if (!pParam) return UMC_NULL_PTR; *pParam = m_Param; return UMC_OK;}Status MPEG4VideoEncoder::Close(){ if (!m_IsInit) return UMC_NOT_INITIALIZED; if (m_Param.m_Param.BVOPdist) delete [] bTime; mp4enc.Close(); m_IsInit = false; return UMC_OK;}Status MPEG4VideoEncoder::GetFrame(MediaData* pIn, MediaData* pOut){ if (!m_IsInit) return UMC_NOT_INITIALIZED; VideoData* pVideoDataIn = DynamicCast<VideoData> (pIn); if (!pOut) return UMC_NULL_PTR; mp4enc.InitBuffer((Ipp8u*)pOut->GetDataPointer(), pOut->GetBufferSize()); if (m_FrameCount == 0) mp4enc.EncodeHeader(); if (pIn && ((m_Param.m_Param.sprite_enable != MP4_SPRITE_STATIC) || (m_FrameCount == 0))) { // copy YUV to internal frame IppiSize roi; Ipp8u *pY, *pU, *pV; int stepL, stepC; mp4enc.GetFrameWriteInfo(&pY, &pU, &pV, &stepL, &stepC); roi.width = m_Param.src_width; roi.height = m_Param.src_height; ippiCopy_8u_C1R(pVideoDataIn->m_lpDest[0], pVideoDataIn->m_lPitch[0], pY, stepL, roi); roi.width >>= 1; roi.height >>= 1; ippiCopy_8u_C1R(pVideoDataIn->m_lpDest[1], pVideoDataIn->m_lPitch[1], pU, stepC, roi); ippiCopy_8u_C1R(pVideoDataIn->m_lpDest[2], pVideoDataIn->m_lPitch[2], pV, stepC, roi); } int sts = mp4enc.EncodeFrame(pIn == NULL); if (pIn) pIn->SetDataSize(0); if (sts == MP4_STS_BUFFERED) { pOut->SetDataSize(0); pOut->SetTime(-1.0); if (pIn) { bTime[bTimePos] = pIn->GetTime(); bTimePos ++; if (bTimePos >= m_Param.m_Param.BVOPdist) bTimePos = 0; } } else { m_FrameCount ++; pOut->SetDataSize(mp4enc.GetBufferFullness()); if (m_Param.m_Param.BVOPdist == 0) { pOut->SetTime(pIn ? pIn->GetTime() : -1.0); } else { if ((mp4enc.GetFrameType() != MP4_VOP_TYPE_B) && pIn) { pOut->SetTime(pIn->GetTime()); } else { pOut->SetTime(bTime[bTimePos]); bTime[bTimePos] = pIn ? pIn->GetTime() : -1.0; bTimePos ++; if (bTimePos >= m_Param.m_Param.BVOPdist) bTimePos = 0; } } } //return (sts == MP4_STS_BUFFERED) ? UMC_NOT_ENOUGH_DATA : (sts == MP4_STS_NODATA) ? UMC_END_OF_STREAM : UMC_OK; return UMC_OK;}VideoEncoder* createMPEG4VideoEncoder(){ MPEG4VideoEncoder* ptr = new MPEG4VideoEncoder; return ptr;}Status MPEG4EncoderParams::ReadParamFile(const vm_char *FileName){ FILE *InputFile; vm_char str[STR_LEN+1], IntraQMatrixFileName[STR_LEN+1], NonIntraQMatrixFileName[STR_LEN+1]; int i, j, k; InputFile = vm_file_open(FileName, VM_STRING("rt")); if (!InputFile) { vm_debug_trace(4,__VM_STRING("Error: Couldn't open file '%s'\n"), FileName); return UMC_OPERATION_FAILED; } vm_file_gets(str, STR_LEN, InputFile); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%s"), m_SourceFileName); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.Width); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.Height); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &numFramesToEncode); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.TimeResolution); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.TimeIncrement); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.quant_type); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.quantIVOP); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.quantPVOP); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.quantBVOP); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%s"), IntraQMatrixFileName); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%s"), NonIntraQMatrixFileName); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.short_video_header); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.IVOPdist); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.BVOPdist); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.PVOPsearchWidth); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.PVOPsearchHeight); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.BVOPsearchWidthForw); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.BVOPsearchHeightForw); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.BVOPsearchWidthBack); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.BVOPsearchHeightBack); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.MEalgorithm); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.MEaccuracy); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.ME4mv); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.obmc_disable); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.RoundingControl); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.calcPSNR); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.RateControl); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.BitRate); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.SceneChangeThreshold); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.insertGOV); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.repeatHeaders); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.resync); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.VideoPacketLenght); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.data_partitioned); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.reversible_vlc); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.interlaced); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.top_field_first); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.alternate_vertical_scan_flag); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.interlacedME); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.sprite_enable); if (m_Param.sprite_enable) { vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.no_of_sprite_warping_points); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.sprite_warping_accuracy); } if (m_Param.sprite_enable == IPPVC_SPRITE_STATIC) { vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d"), &m_Param.sprite_brightness_change); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d %d"), &m_Param.sprite_left_coordinate, &m_Param.sprite_top_coordinate); vm_file_gets(str, STR_LEN, InputFile); vm_string_sscanf(str, VM_STRING("%d %d"), &m_Param.sprite_width, &m_Param.sprite_height); m_Param.warping_mv_code_du = new int [m_Param.no_of_sprite_warping_points * numFramesToEncode]; m_Param.warping_mv_code_dv = new int [m_Param.no_of_sprite_warping_points * numFramesToEncode]; m_Param.brightness_change_factor = new int [numFramesToEncode]; for (i = 0; i < (int)numFramesToEncode; i ++) { for (j = 0; j < m_Param.no_of_sprite_warping_points; j ++) { vm_file_scanf(InputFile, VM_STRING("%d"), &m_Param.warping_mv_code_du[i*m_Param.no_of_sprite_warping_points+j]); vm_file_scanf(InputFile, VM_STRING("%d"), &m_Param.warping_mv_code_dv[i*m_Param.no_of_sprite_warping_points+j]); } if (m_Param.sprite_brightness_change) vm_file_scanf(InputFile, VM_STRING("%d"), &m_Param.brightness_change_factor[i]); } } fclose(InputFile); // read quant matrix m_Param.load_intra_quant_mat = 0; m_Param.load_intra_quant_mat_len = 0; if (IntraQMatrixFileName[0] != '-' ) { InputFile = vm_file_open(IntraQMatrixFileName, VM_STRING("rt")); if (!InputFile) { vm_debug_trace(4,__VM_STRING("Error: Couldn't open quant matrix file '%s'\n"), IntraQMatrixFileName); return UMC_OPERATION_FAILED; } else { m_Param.load_intra_quant_mat = 1; for (i = 0; i < 64; i++) { k = vm_file_scanf(InputFile, VM_STRING("%d"), &j); if (k <= 0 || j < 1 || j > 255 ) break; m_Param.intra_quant_mat[i] = (Ipp8u)j; } m_Param.load_intra_quant_mat_len = i; if (m_Param.load_intra_quant_mat_len < 2) { m_Param.load_intra_quant_mat = 0; m_Param.load_intra_quant_mat_len = 0; } } fclose(InputFile); m_Param.quant_type = 1; } m_Param.load_nonintra_quant_mat = 0; m_Param.load_nonintra_quant_mat_len = 0; if (NonIntraQMatrixFileName[0] != '-' ) { InputFile = vm_file_open(NonIntraQMatrixFileName, VM_STRING("rt")); if (!InputFile) { vm_debug_trace(4,__VM_STRING("Error: Couldn't open quant matrix file '%s'\n"), NonIntraQMatrixFileName); return UMC_OPERATION_FAILED; } else { m_Param.load_nonintra_quant_mat = 1; for (i = 0; i < 64; i++) { k = vm_file_scanf(InputFile, VM_STRING("%d"), &j); if (k <= 0 || j < 1 || j > 255 ) break; m_Param.nonintra_quant_mat[i] = (Ipp8u)j; } m_Param.load_nonintra_quant_mat_len = i; if (m_Param.load_nonintra_quant_mat_len < 2) { m_Param.load_nonintra_quant_mat = 0; m_Param.load_nonintra_quant_mat_len = 0; } } fclose(InputFile); m_Param.quant_type = 1; } m_Param.bsBuffer = (Ipp8u*)1; m_Param.bsBuffSize = 1; // encoder will not allocate buffer m_Param.padType = 0; // set 1 for QuickTime(tm) and 2 for DivX (tm) v. >= 5 src_width = m_Param.Width; src_height = m_Param.Height; if (m_Param.sprite_enable == IPPVC_SPRITE_STATIC) { src_width = m_Param.sprite_width; src_height = m_Param.sprite_height; } return UMC_OK;}}; //namespace UMC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -