Handle H.264 SPS read/write failure in fixup path

This commit is contained in:
Cameron Gutman committed 2026-10-24 21:26:32 -05:00
1 parent 9fee675c91
commit 6fc7e81683
1 file changed
+28 -7
+28 -7
View File
@@ -1765,11 +1765,21 @@ void FFmpegVideoDecoder::writeBuffer(PLENTRY entry, int& offset)
int nalStart, nalEnd; int nalStart, nalEnd;
bool needsFixup; bool needsFixup;
// Read the old NALU // Find the old SPS NALU
find_nal_unit((uint8_t*)entry->data, entry->length, &nalStart, &nalEnd); if (find_nal_unit((uint8_t*)entry->data, entry->length, &nalStart, &nalEnd) <= 0) {
read_nal_unit(stream, SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
(unsigned char *)&entry->data[nalStart], "Failed to find SPS");
nalEnd - nalStart); h264_free(stream);
goto WriteUnmodifiedBuffer;
}
// Read the SPS. Pass it through unmodified if we fail to parse it.
if (read_nal_unit(stream, (uint8_t*)&entry->data[nalStart], nalEnd - nalStart) < 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Failed to read SPS");
h264_free(stream);
goto WriteUnmodifiedBuffer;
}
SDL_assert(nalStart == 3 || nalStart == 4); // 3 or 4 byte Annex B start sequence SDL_assert(nalStart == 3 || nalStart == 4); // 3 or 4 byte Annex B start sequence
SDL_assert(nalEnd == entry->length); SDL_assert(nalEnd == entry->length);
@@ -1802,8 +1812,18 @@ void FFmpegVideoDecoder::writeBuffer(PLENTRY entry, int& offset)
// Copy the modified NALU data. This clobbers byte 0 and starts NALU data at byte 1. // Copy the modified NALU data. This clobbers byte 0 and starts NALU data at byte 1.
// Since it prepended one extra byte, subtract one from the returned length. // Since it prepended one extra byte, subtract one from the returned length.
offset += write_nal_unit(stream, (uint8_t*)&m_DecodeBuffer.data()[initialOffset + nalStart - 1], //
MAX_SPS_EXTRA_SIZE + entry->length - nalStart) - 1; // Pass the SPS through unmodified if the write fails.
int writeLength = write_nal_unit(stream, (uint8_t*)&m_DecodeBuffer.data()[initialOffset + nalStart - 1],
MAX_SPS_EXTRA_SIZE + entry->length - nalStart);
if (writeLength < 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Failed to write modified SPS");
h264_free(stream);
goto WriteUnmodifiedBuffer;
}
offset += writeLength - 1;
// Copy the NALU prefix over from the original SPS // Copy the NALU prefix over from the original SPS
memcpy(&m_DecodeBuffer.data()[initialOffset], entry->data, nalStart); memcpy(&m_DecodeBuffer.data()[initialOffset], entry->data, nalStart);
@@ -1834,6 +1854,7 @@ void FFmpegVideoDecoder::writeBuffer(PLENTRY entry, int& offset)
h264_free(stream); h264_free(stream);
} }
else { else {
WriteUnmodifiedBuffer:
// Write the buffer as-is // Write the buffer as-is
memcpy(&m_DecodeBuffer.data()[offset], memcpy(&m_DecodeBuffer.data()[offset],
entry->data, entry->data,