Skip to content

Commit

Permalink
enhance H264EncoderNvCodec by adding a buffer threshold property
Browse files Browse the repository at this point in the history
  • Loading branch information
kushaljain-apra committed Jun 3, 2024
1 parent 221719b commit 5ab86a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
5 changes: 5 additions & 0 deletions base/include/H264EncoderNVCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ class H264EncoderNVCodecProps : public ModuleProps
H264EncoderNVCodecProps(apracucontext_sp& _cuContext) : bitRateKbps(0), cuContext(_cuContext)
{

}
H264EncoderNVCodecProps(const uint32_t &_bitRateKbps, const apracucontext_sp& _cuContext, const uint32_t &_gopLength,const uint32_t &_frameRate,H264CodecProfile _vProfile,bool _enableBFrames, uint32_t &_bufferThres)
: cuContext(_cuContext), gopLength(_gopLength), frameRate(_frameRate), bitRateKbps(_bitRateKbps), vProfile(_vProfile), enableBFrames(_enableBFrames), bufferThres(_bufferThres)
{
}
H264CodecProfile vProfile= H264EncoderNVCodecProps::BASELINE;
bool enableBFrames=false;
uint32_t gopLength = 30;
uint32_t bitRateKbps = 1000;
uint32_t frameRate = 30;
apracucontext_sp cuContext;
uint32_t bufferThres = 30;
};

class H264EncoderNVCodec : public Module
Expand Down
1 change: 1 addition & 0 deletions base/include/H264EncoderNVCodecHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class H264EncoderNVCodecHelper
{
public:
H264EncoderNVCodecHelper(uint32_t _bitRateKbps, apracucontext_sp& _cuContext, uint32_t _gopLength, uint32_t _frameRate,H264EncoderNVCodecProps::H264CodecProfile _profile, bool enableBFrames);
H264EncoderNVCodecHelper(uint32_t _bitRateKbps, apracucontext_sp& _cuContext, uint32_t _gopLength, uint32_t _frameRate,H264EncoderNVCodecProps::H264CodecProfile _profile, bool enableBFrames, uint32_t _bufferThres);
~H264EncoderNVCodecHelper();

bool init(uint32_t width, uint32_t height, uint32_t pitch, ImageMetadata::ImageType imageType, std::function<frame_sp(size_t)> makeFrame, std::function<void(frame_sp& ,frame_sp&)> send);
Expand Down
9 changes: 8 additions & 1 deletion base/src/H264EncoderNVCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ class H264EncoderNVCodec::Detail
public:
Detail(H264EncoderNVCodecProps &_props) : mProps(_props)
{
helper.reset(new H264EncoderNVCodecHelper(_props.bitRateKbps, _props.cuContext,_props.gopLength,_props.frameRate,_props.vProfile,_props.enableBFrames));
if(_props.bufferThres == 30)
{
helper.reset(new H264EncoderNVCodecHelper(_props.bitRateKbps, _props.cuContext,_props.gopLength,_props.frameRate,_props.vProfile,_props.enableBFrames));
}
else
{
helper.reset(new H264EncoderNVCodecHelper(_props.bitRateKbps, _props.cuContext,_props.gopLength,_props.frameRate,_props.vProfile,_props.enableBFrames,_props.bufferThres));
}
}

~Detail()
Expand Down
36 changes: 31 additions & 5 deletions base/src/H264EncoderNVCodecHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class H264EncoderNVCodecHelper::Detail
}
}
public:
Detail(uint32_t& bitRateKbps, apracucontext_sp& cuContext, uint32_t& gopLength, uint32_t& frameRate,H264EncoderNVCodecProps::H264CodecProfile profile,bool enableBFrames) :
Detail(uint32_t& bitRateKbps, apracucontext_sp& cuContext, uint32_t& gopLength, uint32_t& frameRate,H264EncoderNVCodecProps::H264CodecProfile profile,bool enableBFrames, uint32_t& bufferThres) :
m_nWidth(0),
m_nHeight(0),
m_eBufferFormat(NV_ENC_BUFFER_FORMAT_UNDEFINED),
Expand All @@ -214,7 +214,8 @@ class H264EncoderNVCodecHelper::Detail
m_nGopLength(gopLength),
m_nFrameRate(frameRate),
m_nProfile(profile),
m_bEnableBFrames(enableBFrames)
m_bEnableBFrames(enableBFrames),
m_nBufferThres(bufferThres)
{
m_nvcodecResources.reset(new NVCodecResources(cuContext));

Expand Down Expand Up @@ -472,7 +473,7 @@ class H264EncoderNVCodecHelper::Detail
{
NVENC_API_CALL(m_nvcodecResources->m_nvenc.nvEncInitializeEncoder(m_nvcodecResources->m_hEncoder, &m_initializeParams));

m_nEncoderBuffer = m_encodeConfig.frameIntervalP + m_encodeConfig.rcParams.lookaheadDepth + 20;
m_nEncoderBuffer = m_encodeConfig.frameIntervalP + m_encodeConfig.rcParams.lookaheadDepth + 30;
m_nvcodecResources->m_nFreeOutputBitstreams = m_nEncoderBuffer;

for (int i = 0; i < m_nEncoderBuffer; i++)
Expand Down Expand Up @@ -574,7 +575,15 @@ class H264EncoderNVCodecHelper::Detail

bool is_not_empty() const
{
if (!m_nvcodecResources->m_nFreeOutputBitstreams)
uint32_t busyStreams = m_nvcodecResources->m_nBusyOutputBitstreams;
if (!m_nvcodecResources->m_nFreeOutputBitstreams && busyStreams < m_nBufferThres)
{
uint32_t bufferLength = min(busyStreams, m_nBufferThres - busyStreams);
doubleOutputBuffers(bufferLength);
LOG_INFO << "Allocated <" << bufferLength << "> outputbitstreams to the encoder buffer.";
m_nvcodecResources->m_nFreeOutputBitstreams += bufferLength;
}
else
{
LOG_INFO << "waiting for free outputbitstream<> busy streams<" << m_nvcodecResources->m_nBusyOutputBitstreams << ">";
}
Expand All @@ -587,6 +596,16 @@ class H264EncoderNVCodecHelper::Detail
return m_nvcodecResources->m_nBusyOutputBitstreams > 0 || !m_bRunning;
}

void doubleOutputBuffers(uint32_t bufferLength) const
{
for (int i = 0; i < bufferLength; i++)
{
NV_ENC_CREATE_BITSTREAM_BUFFER createBitstreamBuffer = { NV_ENC_CREATE_BITSTREAM_BUFFER_VER };
NVENC_API_CALL(m_nvcodecResources->m_nvenc.nvEncCreateBitstreamBuffer(m_nvcodecResources->m_hEncoder, &createBitstreamBuffer));
m_nvcodecResources->m_qBitstreamOutputBitstream.push_back(createBitstreamBuffer.bitstreamBuffer);
}
}

std::thread m_thread;
bool m_bRunning;

Expand All @@ -600,6 +619,7 @@ class H264EncoderNVCodecHelper::Detail
uint32_t m_nFrameRate;
H264EncoderNVCodecProps::H264CodecProfile m_nProfile;
bool m_bEnableBFrames;
uint32_t m_nBufferThres;

NV_ENC_INITIALIZE_PARAMS m_initializeParams;
NV_ENC_CONFIG m_encodeConfig;
Expand All @@ -621,7 +641,13 @@ class H264EncoderNVCodecHelper::Detail

H264EncoderNVCodecHelper::H264EncoderNVCodecHelper(uint32_t _bitRateKbps, apracucontext_sp& _cuContext, uint32_t _gopLength, uint32_t _frameRate, H264EncoderNVCodecProps::H264CodecProfile _profile, bool enableBFrames)
{
mDetail.reset(new Detail(_bitRateKbps, _cuContext,_gopLength,_frameRate,_profile,enableBFrames));
uint32_t _bufferThres = 30;
mDetail.reset(new Detail(_bitRateKbps, _cuContext,_gopLength,_frameRate,_profile,enableBFrames,_bufferThres));
}

H264EncoderNVCodecHelper::H264EncoderNVCodecHelper(uint32_t _bitRateKbps, apracucontext_sp& _cuContext, uint32_t _gopLength, uint32_t _frameRate, H264EncoderNVCodecProps::H264CodecProfile _profile, bool enableBFrames, uint32_t _bufferThres)
{
mDetail.reset(new Detail(_bitRateKbps, _cuContext,_gopLength,_frameRate,_profile,enableBFrames,_bufferThres));
}

H264EncoderNVCodecHelper::~H264EncoderNVCodecHelper()
Expand Down

0 comments on commit 5ab86a0

Please sign in to comment.