Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance H264EncoderNvCodec by adding a buffer threshold property #360

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some basic documentation

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add it to #define instead of hard coding

{
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
34 changes: 30 additions & 4 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) :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we Have Limit on Max Buffer Threshold, or it can be any

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 @@ -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
Loading