Skip to content

Commit 84b2703

Browse files
krajunvzlatinski
authored andcommitted
Set the bitstream buffer size to match the input image size.
For higher image resolutions and bitrates, the fixed 2MB buffer size is inadequate. To handle the worst-case scenario, ensure the bitstream size is the same as the input image size. Signed-off-by: Raju Konda <kraju@nvidia.com>
1 parent b5d0d26 commit 84b2703

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

vk_video_encoder/libs/VkVideoEncoder/VkVideoEncoder.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,20 @@ VkResult VkVideoEncoder::AssembleBitstreamData(VkSharedBaseObj<VkVideoEncodeFram
539539
VkDeviceSize maxSize;
540540
uint8_t* data = encodeFrameInfo->outputBitstreamBuffer->GetDataPtr(0, maxSize);
541541

542-
size_t vcl = fwrite(data + encodeResult.bitstreamStartOffset, 1, encodeResult.bitstreamSize,
543-
m_encoderConfig->outputFileHandler.GetFileHandle());
542+
size_t totalBytesWritten = 0;
543+
while (totalBytesWritten < encodeResult.bitstreamSize) { // handle partial writes
544+
size_t remainingBytes = encodeResult.bitstreamSize - totalBytesWritten;
545+
size_t bytesWritten = fwrite(data + encodeResult.bitstreamStartOffset + totalBytesWritten, 1, remainingBytes,
546+
m_encoderConfig->outputFileHandler.GetFileHandle());
547+
if (bytesWritten == 0) {
548+
std::cerr << "Error writing VCL data" << std::endl;
549+
return VK_ERROR_OUT_OF_HOST_MEMORY;
550+
}
551+
totalBytesWritten += bytesWritten;
552+
}
544553

545554
if (m_encoderConfig->verboseFrameStruct) {
546-
std::cout << " == Output VCL data " << (vcl ? "SUCCESS" : "FAIL") << " with size: " << encodeResult.bitstreamSize
555+
std::cout << " == Output VCL data " << ((totalBytesWritten == encodeResult.bitstreamSize) ? "SUCCESS" : "FAIL") << " with size: " << encodeResult.bitstreamSize
547556
<< " and offset: " << encodeResult.bitstreamStartOffset
548557
<< ", Input Order: " << encodeFrameInfo->gopPosition.inputOrder
549558
<< ", Encode Order: " << encodeFrameInfo->gopPosition.encodeOrder << std::endl << std::flush;
@@ -687,6 +696,7 @@ VkResult VkVideoEncoder::InitEncoder(VkSharedBaseObj<EncoderConfig>& encoderConf
687696
}
688697

689698
m_maxCodedExtent = { encoderConfig->encodeMaxWidth, encoderConfig->encodeMaxHeight }; // max coded size
699+
m_streamBufferSize = std::max(m_minStreamBufferSize, (size_t)encoderConfig->input.fullImageSize); // use worst case size
690700

691701
const uint32_t maxActiveReferencePicturesCount = encoderConfig->videoCapabilities.maxActiveReferencePictures;
692702
const uint32_t maxDpbPicturesCount = std::min<uint32_t>(m_maxDpbPicturesCount, encoderConfig->videoCapabilities.maxDpbSlots);

vk_video_encoder/libs/VkVideoEncoder/VkVideoEncoderAV1.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,29 @@ VkResult VkVideoEncoderAV1::AssembleBitstreamData(VkSharedBaseObj<VkVideoEncodeF
955955
}
956956

957957
for (const auto& curIndex : m_batchFramesIndxSetToAssemble) {
958-
if (frameIdx == curIndex) {
959-
fwrite(data + encodeResult.bitstreamStartOffset, 1, encodeResult.bitstreamSize,
960-
m_encoderConfig->outputFileHandler.GetFileHandle());
961-
} else {
962-
fwrite(m_bitstream[curIndex].data(), 1, m_bitstream[curIndex].size(),
963-
m_encoderConfig->outputFileHandler.GetFileHandle());
958+
const uint8_t* writeData = (frameIdx == curIndex) ? (data + encodeResult.bitstreamStartOffset) : m_bitstream[curIndex].data();
959+
const size_t bytesToWrite = (frameIdx == curIndex) ? encodeResult.bitstreamSize : m_bitstream[curIndex].size();
960+
961+
// Write data in chunks to handle partial writes
962+
size_t totalBytesWritten = 0;
963+
while (totalBytesWritten < bytesToWrite) {
964+
const size_t remainingBytes = bytesToWrite - totalBytesWritten;
965+
const size_t bytesWritten = fwrite(writeData + totalBytesWritten, 1,
966+
remainingBytes,
967+
m_encoderConfig->outputFileHandler.GetFileHandle());
968+
969+
if (bytesWritten == 0) {
970+
std::cerr << "Failed to write bitstream data" << std::endl;
971+
return VK_ERROR_OUT_OF_HOST_MEMORY;
972+
}
973+
974+
totalBytesWritten += bytesWritten;
975+
}
976+
977+
// Verify complete write
978+
if (totalBytesWritten != bytesToWrite) {
979+
std::cerr << "Warning: Incomplete write - expected " << bytesToWrite << " bytes but wrote " << totalBytesWritten << " bytes\n";
980+
return VK_ERROR_OUT_OF_HOST_MEMORY;
964981
}
965982
}
966983
// reset the batch frames to assemble counter

0 commit comments

Comments
 (0)