From 043faeacca817c971138b33f81c3b0d9fd4da747 Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 14 Oct 2026 23:08:47 +0200 Subject: [PATCH] Validate desktop call IDRs with the common bitstream contract --- tools/chatgui/src/call_video_decoder.cpp | 5 ++++- tools/chatgui/src/call_video_encoder.cpp | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/chatgui/src/call_video_decoder.cpp b/tools/chatgui/src/call_video_decoder.cpp index 467ecd29e..c90823080 100644 --- a/tools/chatgui/src/call_video_decoder.cpp +++ b/tools/chatgui/src/call_video_decoder.cpp @@ -1,5 +1,6 @@ #include "call_video_decoder.h" #include "debug_config.h" +#include "../../../src/call/call_video_bitstream.h" #include #ifdef HAVE_FFMPEG extern "C" { @@ -34,6 +35,7 @@ bool CallVideoDecoder::decode(const call_video_frame& f, QImage& image) { bool changed = !call_video_same_format(&m->format, &f.format); const uint8_t* data = f.data; unsigned size = f.size; if (f.flags & CALL_VIDEO_KEY) { + if (call_video_check_key(&f.format, f.data, f.size)) return failed("bitstream contract"); if (size < 5) return failed("KEY envelope"); uint32_t config = (uint32_t)data[0] << 24 | (uint32_t)data[1] << 16 | (uint32_t)data[2] << 8 | data[3]; if (config > 65536 || config >= size - 4) return failed("KEY config length"); @@ -60,7 +62,8 @@ bool CallVideoDecoder::decode(const call_video_frame& f, QImage& image) { bool valid = result >= 0 && decoded->pts == f.pts_us && decoded->width == f.format.width && decoded->height == f.format.height && decoded->format == AV_PIX_FMT_YUV420P && !m->codec->has_b_frames && m->codec->level <= f.format.level; int profile = f.format.codec == CALL_VIDEO_H265 ? AV_PROFILE_HEVC_MAIN : AV_PROFILE_H264_CONSTRAINED_BASELINE; - valid = valid && m->codec->profile == profile; + valid = valid && (m->codec->profile == profile || (f.format.codec == CALL_VIDEO_H264 + && f.format.profile == CALL_VIDEO_BASELINE && m->codec->profile == AV_PROFILE_H264_BASELINE)); if (valid) { QImage output(decoded->width, decoded->height, QImage::Format_RGB888); m->scale = sws_getCachedContext(m->scale, decoded->width, decoded->height, AV_PIX_FMT_YUV420P, diff --git a/tools/chatgui/src/call_video_encoder.cpp b/tools/chatgui/src/call_video_encoder.cpp index 8805aa5f9..5b0a0e780 100644 --- a/tools/chatgui/src/call_video_encoder.cpp +++ b/tools/chatgui/src/call_video_encoder.cpp @@ -1,18 +1,19 @@ #include "call_video_encoder.h" #include "debug_config.h" #include "../../../src/call/call_video_rate.h" +#include "../../../src/call/call_video_bitstream.h" #include // Профиль и envelope задаются явно: автоматический выбор level при смене bitrate запрещён. bool CallVideoEncoder::open(const call_video_format& format, uint32_t bitrate, const QString& backend) { if (!call_video_format_valid(&format) || bitrate < CALL_VIDEO_RATE_MIN_BPS || bitrate > format.maximum_bps) { m_error = "Invalid call video encoder configuration"; - DEBUG_ERROR(DEBUG_CATEGORY_CALL, "call video open invalid generation=%llu codec=%u level=%u bitrate=%u cap=%u vbv=%u", - (unsigned long long)format.generation, format.codec, format.level, bitrate, format.maximum_bps, format.vbv_bits); return false; + DEBUG_ERROR(DEBUG_CATEGORY_CALL, "call video open invalid generation=%llu codec=%u level=%u bitrate=%u cap=%u profile=%u", + (unsigned long long)format.generation, format.codec, format.level, bitrate, format.maximum_bps, format.profile); return false; } LiveVideoConfig config; config.width = format.width; config.height = format.height; config.fps = format.fps; config.rotation = format.rotation; - config.codec = format.codec; config.level = format.level; config.vbvBits = format.vbv_bits; + config.codec = format.codec; config.level = format.level; config.vbvBits = 1000000; config.bitrate = bitrate; config.references = 1; config.backend = backend; config.profile = format.codec == CALL_VIDEO_H264 ? LiveVideoProfile::H264ConstrainedBaseline : LiveVideoProfile::H265Main; auto candidate = std::make_unique(); @@ -92,6 +93,12 @@ bool CallVideoEncoder::encode(const QImage& image, uint64_t pts, bool forceKey, DEBUG_ERROR(DEBUG_CATEGORY_CALL, "call video output contract failed generation=%llu pts=%llu frames=%lld key_requested=%d", (unsigned long long)m_format.generation, (unsigned long long)pts, (long long)frames.size(), key); return false; } + if (frames.front().key && call_video_check_key(&m_format, + reinterpret_cast(frames.front().data.constData()), frames.front().data.size())) { + m_error = "Encoder produced an incompatible IDR"; + if (completion.request) completion.bitrate = 0; + return false; + } m_haveFrame = true; m_lastPts = pts; if (frames.front().key) m_lastKey = pts; output = std::move(frames.front()); return true; }