SfxDecoder.cpp 3.59 KB
#include "SfxDecoder.h"

#include <thread>
#include <chrono>
#include <iostream>

#include "sfx_depend_inc.h"

#include "cuda_kernels.h"
#include <fstream>
#include <iostream>

using namespace std;

SfxDecoder::SfxDecoder( )
{
	memset( &m_video, 0, sizeof( DxVideoConfig ) );
	m_handler = sfx_null;

	return;
}


SfxDecoder::~SfxDecoder()
{
	LOG_INFO( "[{}]-free SfxDecoder..", m_dec_name);

	if ( sfx_null != m_handler )
	{
		SfxStreamHandlerInterface::SfxReleaseInstance( m_handler );
		m_handler = sfx_null;
	}

	return;
}

bool SfxDecoder::OpenDecoder(DxDecoderConfig& cfg)
{
	m_cfg = cfg;

	SfxStreamHandlerConfig* sfx_cfg = new SfxStreamHandlerConfig();
	sfx_cfg->userPtr = this;
	sfx_cfg->escb = SfxStreamCallback;
	sfx_cfg->log = SfxLogDefaultHandler;
	sfx_cfg->flags.outFrameIndex = 1;
	sfx_cfg->flags.preposeFrameCount = 1;
	sfx_cfg->flags.useFastDecode = 0;
	sfx_cfg->flags.accelId = m_cfg.devId;
	sfx_cfg->flags.frameNumber = 1;
	// sfx_cfg->flags.fmt = SFX_VIDEO_FORMAT_NV12U8;
	sfx_cfg->flags.fmt = SFX_VIDEO_FORMAT_C3BGRU8;

	m_handler = SfxStreamHandlerInterface::SfxGetInstance(sfx_cfg);

	m_dec_name = m_cfg.dec_name;

	cout << "open: " << m_cfg.uri << endl;

	int nRet = m_handler->SfxOpenStreamHandler( m_cfg.uri.c_str() );
	if (nRet != 0) {
		m_bRun = false;

		LOG_INFO( "[{}]-OpenDecoder failed!", m_dec_name);
		return false;
	}
	
	m_bRun = true;
	return true;
}

int SfxDecoder::CloseDecoder()
{
	if (sfx_null != m_handler) { // linux待同步
		m_handler->SfxCloseStreamHandler();
		while (m_bRun)
		{
			std::this_thread::sleep_for(std::chrono::milliseconds(250));
		}
	}

	return 0;
}

bool SfxDecoder::isFinished() {
	return !m_bRun;
}

unsigned int SfxDecoder::GetFrameCount() {
	return m_video.frame_count;
}

sfx_32 SfxDecoder::SfxStreamCallback( 
									 const sfx_void * userPtr,
									 SfxFrameType type,
									 const sfx_u8 * buf, 
									 sfx_u32 segLen, 
									 sfx_u32 len, 
									 sfx_u32 timestamp 
									 )
{
	SfxDecoder * pThis = sfx_null;

	pThis = ( SfxDecoder * )userPtr;
	if (!pThis->m_bRun)
	{
		return 0;
	}

	if ( SFX_FRAME_TYPE_CONFIG == type )
	{
		
		if (pThis->m_handler) {
			SfxStreamHandlerConfigInterface *sc = nullptr;
			pThis->m_handler->SfxQueryInterface(SFX_STREAM_HANDLER_CONFIG_INTERFACE, ( sfx_void ** )&sc);
			if (sc) {
				pThis->m_video.frame_count = sc->SfxGetFrameNumber();
			}
		}

		pThis->m_video.height = ( ( SfxMediaConfig * )buf )->v.height;
		pThis->m_video.width = ( ( SfxMediaConfig * )buf )->v.width;
		
		pThis->m_cfg.escbk( pThis->m_cfg.userPtr, &pThis->m_video, 0, pThis->m_video.width, pThis->m_video.height, timestamp);

		return 0;
	}
	
	pThis->m_cfg.escbk( pThis->m_cfg.userPtr, ( void * )buf, segLen, pThis->m_video.width, pThis->m_video.height, timestamp);

	return 0;
}
	
sfx_32 SfxDecoder::SfxLogDefaultHandler(
										const sfx_void * userPtr,
										SfxLogLevel level,
										const sfx_8 * log,
										sfx_u32 logLen
										)
{
	SfxDecoder * pThis = ( SfxDecoder * )userPtr;

	if ( SFX_LOG_LEVEL_IMPORTANT == level) //modify by zsh 220602
	{
		pThis->m_cfg.logcbk( pThis->m_cfg.userPtr, DX_LOG_LEVEL_IMPORTANT, log, 0 );
		return 0;	
	}

	if ( SFX_LOG_LEVEL_CLEANUP == level )
	{
		pThis->m_bRun = sfx_false;
		if ( pThis->m_cfg.logcbk )
		{
			pThis->m_cfg.logcbk( pThis->m_cfg.userPtr, DX_LOG_LEVEL_CLEANUP, "DxDecoder[CUDA] module exited..", 0 );
		}
	} 
	else if (pThis->m_cfg.log_all) 
	{
		pThis->m_cfg.log_all(pThis->m_cfg.log_user_ptr, (int)level, log, logLen);
	}
	
	return 0;
}