Improve checks for video frame sizes.

This commit is contained in:
John Preston
2026-04-30 23:52:45 +07:00
parent b1f8021847
commit 9eda44ca31
13 changed files with 184 additions and 79 deletions
@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ffmpeg/ffmpeg_frame_generator.h"
#include "ffmpeg/ffmpeg_utility.h"
#include "media/media_common.h"
#include "base/debug_log.h"
namespace FFmpeg {
@@ -15,6 +16,8 @@ namespace {
constexpr auto kMaxArea = 1920 * 1080 * 4;
using ::Media::ValidFrameSize;
} // namespace
class FrameGenerator::Impl final {
@@ -103,7 +106,11 @@ FrameGenerator::Impl::Impl(const QByteArray &bytes)
const auto info = _format->streams[_streamId];
_rotation = ReadRotationFromMetadata(info);
//_aspect = ValidateAspectRatio(info->sample_aspect_ratio);
_codec = MakeCodecPointer({ .stream = info });
_codec = MakeCodecPointer({
.stream = info,
.hwAllowed = false,
.videoMaxArea = kMaxArea,
});
}
int FrameGenerator::Impl::Read(void *opaque, uint8_t *buf, int buf_size) {
@@ -157,7 +164,7 @@ FrameGenerator::Frame FrameGenerator::Impl::renderCurrent(
const auto width = frame->width;
const auto height = frame->height;
if (!width || !height) {
LOG(("Webm Error: Bad frame size: %1x%2 ").arg(width).arg(height));
LOG(("Webm Error: Bad frame size %1x%2").arg(width).arg(height));
return {};
}
@@ -167,6 +174,10 @@ FrameGenerator::Frame FrameGenerator::Impl::renderCurrent(
}
if (!GoodStorageForFrame(storage, size)) {
storage = CreateFrameStorage(size);
if (storage.isNull()) {
LOG(("Webm Error: Bad frame size %1x%2").arg(width).arg(height));
return {};
}
}
const auto dx = (size.width() - scaled.width()) / 2;
const auto dy = (size.height() - scaled.height()) / 2;
@@ -317,7 +328,7 @@ void FrameGenerator::Impl::readNextFrame() {
while (true) {
auto result = avcodec_receive_frame(_codec.get(), frame.get());
if (result >= 0) {
if (frame->width * frame->height > kMaxArea) {
if (!ValidFrameSize(frame->width, frame->height, kMaxArea)) {
return;
}
_next.frame = std::move(frame);