Add verified call profiles and defer bitrate replacement until IDR

This commit is contained in:
evgeny committed 2026-10-14 21:46:13 +02:00
1 parent 43a90ff23b
commit 3b3ba3c495
3 files changed
+190 -8

No files matched your search

+68 -8
View File
@@ -66,18 +66,62 @@ AVPixelFormat softwareFormat(const AVCodec* codec) {
}
return AV_PIX_FMT_NONE;
}
// Проверяем фактический bitstream: одного принятого encoder option недостаточно.
bool verifiedProfile(const LiveVideoConfig& config, const QList<LiveEncodedFrame>& output) {
if (output.size() != 1 || !output.front().key) return false;
const auto& data = output.front().data;
const uint8_t *header, *frame; size_t headerSize, frameSize;
if (stream_media_key_parse(reinterpret_cast<const uint8_t*>(data.constData()), data.size(),
&header, &headerSize, &frame, &frameSize) != STREAM_OK) return false;
QByteArray annex(reinterpret_cast<const char*>(header), headerSize); annex.append(reinterpret_cast<const char*>(frame), frameSize);
bool idr = false, mainTier = config.codec == STREAM_MEDIA_H264;
for (qsizetype i = 0; i + 4 < annex.size(); i++) {
const auto* p = reinterpret_cast<const uint8_t*>(annex.constData() + i);
int prefix = p[0] == 0 && p[1] == 0 ? (p[2] == 1 ? 3 : p[2] == 0 && p[3] == 1 ? 4 : 0) : 0;
if (!prefix || i + prefix >= annex.size()) continue;
int type = config.codec == STREAM_MEDIA_H264 ? p[prefix] & 31 : (p[prefix] >> 1) & 63;
idr |= config.codec == STREAM_MEDIA_H264 ? type == 5 : type == 19 || type == 20;
if (config.codec == STREAM_MEDIA_H265 && type == 33 && i + prefix + 3 < annex.size())
mainTier = (p[prefix + 3] & 0x20) == 0;
}
const AVCodec* codec = avcodec_find_decoder(config.codec == STREAM_MEDIA_H264 ? AV_CODEC_ID_H264 : AV_CODEC_ID_HEVC);
AVCodecContext* decoder = codec ? avcodec_alloc_context3(codec) : nullptr;
AVPacket* packet = av_packet_alloc(); AVFrame* decoded = av_frame_alloc();
int result = decoder && packet && decoded ? 0 : AVERROR(ENOMEM);
if (result >= 0) { decoder->thread_count = 1; result = avcodec_open2(decoder, codec, nullptr); }
if (result >= 0) result = av_new_packet(packet, annex.size());
if (result >= 0) { memcpy(packet->data, annex.constData(), annex.size()); result = avcodec_send_packet(decoder, packet); }
if (result >= 0) result = avcodec_receive_frame(decoder, decoded);
int profile = config.codec == STREAM_MEDIA_H264 ? AV_PROFILE_H264_CONSTRAINED_BASELINE : AV_PROFILE_HEVC_MAIN;
bool valid = result >= 0 && idr && mainTier && decoder->profile == profile && decoder->level == config.level
&& !decoder->has_b_frames && decoded->format == AV_PIX_FMT_YUV420P
&& decoded->width == config.width && decoded->height == config.height;
if (!valid) DEBUG_ERROR(DEBUG_CATEGORY_VIDEO,
"live profile verification failed codec=%d result=%d idr=%d main_tier=%d profile=%d/%d level=%d/%d pixel=%d B=%d",
config.codec, result, idr, mainTier, decoder ? decoder->profile : -1, profile,
decoder ? decoder->level : -1, config.level, decoded ? decoded->format : -1, decoder ? decoder->has_b_frames : -1);
av_frame_free(&decoded); av_packet_free(&packet); avcodec_free_context(&decoder); return valid;
}
}
#endif
bool LiveVideoEncoder::open(const LiveVideoConfig& config) {
close();
bool constrained = config.profile != LiveVideoProfile::Automatic;
if (config.width < 16 || config.height < 16 || config.width > 4096 || config.height > 4096
|| config.width % 2 || config.height % 2 || config.fps < 1 || config.fps > 60
|| config.bitrate < 64000 || config.bitrate > 16000000 || config.codec < 1 || config.codec > 5) {
|| config.bitrate < 64000 || config.bitrate > 16000000 || config.codec < 1 || config.codec > 5
|| config.vbvBits < 0 || config.vbvBits > 16000000 || config.references < 0 || config.references > 16
|| (constrained && (config.level <= 0 || config.level > 255 || config.references != 1 || !config.vbvBits
|| !((config.codec == 1 && config.profile == LiveVideoProfile::H264ConstrainedBaseline)
|| (config.codec == 2 && config.profile == LiveVideoProfile::H265Main))))) {
m->error = "Invalid video configuration"; DEBUG_ERROR(DEBUG_CATEGORY_VIDEO, "desktop encoder invalid configuration"); return false;
}
#ifdef HAVE_FFMPEG
for (const auto& name : encoders(config.codec)) {
if (!config.backend.isEmpty() && name != config.backend.toLatin1()) continue;
if (constrained && name != "libx264" && name != "libx265") continue;
const AVCodec* encoder = avcodec_find_encoder_by_name(name.constData());
if (!encoder) continue;
// Проба не оставляет references/PTS в live-контексте: после двух KEY encoder открывается заново.
@@ -87,12 +131,14 @@ bool LiveVideoEncoder::open(const LiveVideoConfig& config) {
if (!m->codec) { m->error = "Video encoder allocation failed"; break; }
auto* c = m->codec;
c->width = config.width; c->height = config.height; c->time_base = {1, 1000000}; c->framerate = {config.fps, 1};
c->bit_rate = config.bitrate; c->rc_max_rate = config.bitrate; c->rc_buffer_size = config.bitrate / 4;
c->bit_rate = config.bitrate; c->rc_max_rate = config.bitrate;
c->rc_buffer_size = config.vbvBits ? config.vbvBits : config.bitrate / 4;
c->gop_size = config.fps; c->max_b_frames = 0; c->thread_count = 2;
if (config.references) c->refs = config.references;
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER | AV_CODEC_FLAG_CLOSED_GOP;
c->color_range = AVCOL_RANGE_MPEG; c->colorspace = AVCOL_SPC_BT709;
c->color_primaries = AVCOL_PRI_BT709; c->color_trc = AVCOL_TRC_BT709;
AVPixelFormat sw = softwareFormat(encoder);
AVPixelFormat sw = constrained ? AV_PIX_FMT_YUV420P : softwareFormat(encoder);
int result = 0;
if (name.endsWith("_vaapi")) {
sw = AV_PIX_FMT_NV12; c->pix_fmt = AV_PIX_FMT_VAAPI;
@@ -112,8 +158,17 @@ bool LiveVideoEncoder::open(const LiveVideoConfig& config) {
if (name == "libx264" || name == "libx265") {
av_dict_set(&options, "preset", "ultrafast", 0); av_dict_set(&options, "tune", "zerolatency", 0);
av_dict_set(&options, "forced-idr", "1", 0);
if (name == "libx264") av_dict_set(&options, "x264-params", "scenecut=0:open-gop=0:repeat-headers=0", 0);
else av_dict_set(&options, "x265-params", "bframes=0:rc-lookahead=0:open-gop=0:pools=1:frame-threads=1:log-level=error", 0);
QByteArray params = name == "libx264" ? "scenecut=0:open-gop=0:repeat-headers=0"
: "bframes=0:rc-lookahead=0:open-gop=0:pools=1:frame-threads=1:log-level=error";
if (constrained) {
c->thread_count = 1;
av_dict_set(&options, "profile", name == "libx264" ? "baseline" : "main", 0);
QByteArray level = QByteArray::number(config.level / (name == "libx264" ? 10.0 : 30.0), 'f', 1);
params += ":ref=1:";
if (name == "libx264") params += "level=" + level;
else params += "level-idc=" + level + ":high-tier=0:hrd=0:info=0:scenecut=0";
}
av_dict_set(&options, name == "libx264" ? "x264-params" : "x265-params", params.constData(), 0);
} else if (name.endsWith("_nvenc")) {
av_dict_set(&options, "preset", "p1", 0); av_dict_set(&options, "tune", "ull", 0);
av_dict_set(&options, "zerolatency", "1", 0); av_dict_set(&options, "rc-lookahead", "0", 0);
@@ -162,10 +217,15 @@ bool LiveVideoEncoder::open(const LiveVideoConfig& config) {
DEBUG_WARN(DEBUG_CATEGORY_VIDEO, "desktop encoder cannot force an independent KEY name=%s error=%s", name.constData(), qPrintable(m->error));
break;
}
if (constrained && !verifiedProfile(config, output)) {
m->error = "Encoder output violates the requested profile"; break;
}
continue;
}
DEBUG_INFO(DEBUG_CATEGORY_VIDEO, "desktop encoder ready name=%s codec=%d size=%dx%d fps=%d bitrate=%d two_KEYs=verified",
name.constData(), config.codec, config.width, config.height, config.fps, config.bitrate);
DEBUG_INFO(DEBUG_CATEGORY_VIDEO,
"desktop encoder ready name=%s codec=%d size=%dx%d fps=%d bitrate=%d level=%d refs=%d vbv=%d profile_verified=%d",
name.constData(), config.codec, config.width, config.height, config.fps, config.bitrate,
config.level, config.references, c->rc_buffer_size, constrained);
return true;
}
}
@@ -264,7 +324,7 @@ bool LiveVideoEncoder::setBitrate(int bitrate) {
if (bitrate < 64000 || bitrate > 16000000) { DEBUG_ERROR(DEBUG_CATEGORY_VIDEO, "desktop live bitrate invalid value=%d", bitrate); return false; }
#ifdef HAVE_FFMPEG
// libx264 перечитывает эти три значения через reconfig_encoder при подаче следующего кадра.
if (m->codec && m->name == "libx264") {
if (m->codec && m->name == "libx264" && m->config.profile == LiveVideoProfile::Automatic) {
m->codec->bit_rate = m->codec->rc_max_rate = bitrate; m->codec->rc_buffer_size = bitrate / 4; m->config.bitrate = bitrate;
DEBUG_INFO(DEBUG_CATEGORY_VIDEO, "desktop encoder bitrate updated in place name=libx264 bitrate=%d", bitrate); return true;
}