refactor(video): 移除性能实验开关和逐帧统计代码

This commit is contained in:
mofeng-git
2026-10-01 23:05:24 +08:00
parent 9a1f4ff4b5
commit 8b4ca6d080
13 changed files with 25 additions and 563 deletions

View File

@@ -49,13 +49,9 @@ input to FFmpeg V4L2 M2M encoders. It holds a `Bytes` owner in a read-only
input into an intermediate AVFrame. Other encoders retain the existing copy
path. Converted or stride-compacted One-KVM input also retains that path.
`ONE_KVM_V4L2_OWNED_INPUT=0` disables this optimization when the encoder is
created, allowing comparisons with the same binary. `ONE_KVM_VIDEO_STATS=1`
enables preparation, send-frame, receive-packet, callback and output-copy
timings. One-KVM reports them in `[VideoInputPerf]` at five-second intervals;
`copied_bytes` counts only the intermediate raw-input copy, not FFmpeg's
remaining copy into V4L2 MMAP buffers. Encoded packet detachment remains in
place to avoid retaining scarce V4L2 capture buffers in downstream caches.
Encoded packet detachment remains in place to avoid retaining scarce V4L2
capture buffers in downstream caches. FFmpeg still copies raw input into its
V4L2 MMAP buffers.
## System requirements

View File

@@ -5,7 +5,6 @@ extern "C" {
#include <libavutil/imgutils.h>
#include <libavutil/log.h>
#include <libavutil/opt.h>
#include <libavutil/time.h>
}
#include <stdbool.h>
@@ -15,7 +14,6 @@ extern "C" {
#include <string>
#include "common.h"
#include "ffmpeg_ram_encode_stats.h"
#include "owned_nv12_frame.h"
#define LOG_MODULE "FFMPEG_RAM_ENC"
@@ -149,8 +147,6 @@ public:
RamEncodePacketCallback packet_callback_ = NULL;
int offset_[AV_NUM_DATA_POINTERS] = {0};
bool force_keyframe_ = false; // Force next frame to be a keyframe
bool measure_ = false;
RamEncodeStats stats_ = {};
AVHWDeviceType hw_device_type_ = AV_HWDEVICE_TYPE_NONE;
AVPixelFormat hw_pixfmt_ = AV_PIX_FMT_NONE;
@@ -175,8 +171,6 @@ public:
thread_count_ = thread_count;
gpu_ = gpu;
callback_ = callback;
const char *stats_setting = getenv("ONE_KVM_VIDEO_STATS");
measure_ = stats_setting && strcmp(stats_setting, "1") == 0;
if (name_.find("vaapi") != std::string::npos) {
hw_device_type_ = AV_HWDEVICE_TYPE_VAAPI;
hw_pixfmt_ = AV_PIX_FMT_VAAPI;
@@ -305,7 +299,6 @@ public:
int encode(const uint8_t *data, int length, const void *obj, uint64_t ms) {
int ret;
const int64_t prepare_start = timer_start();
if ((ret = av_frame_make_writable(frame_)) != 0) {
LOG_ERROR(std::string("av_frame_make_writable failed, ret = ") + av_err2str(ret));
@@ -324,15 +317,6 @@ public:
tmp_frame = frame_;
}
timer_add(stats_.prepare_us, prepare_start);
if (measure_) {
stats_.input_frames++;
stats_.copied_frames++;
const int bytes = av_image_get_buffer_size(pixfmt_, width_, height_, 1);
if (bytes > 0)
stats_.copied_bytes += bytes;
}
return do_encode(tmp_frame, obj, ms);
}
@@ -344,16 +328,10 @@ public:
release(owner, const_cast<uint8_t *>(data));
return AVERROR(ENOSYS);
}
const int64_t prepare_start = timer_start();
auto frame = make_owned_nv12_frame(data, length, width_, height_, owner,
release);
timer_add(stats_.prepare_us, prepare_start);
if (!frame)
return AVERROR(EINVAL);
if (measure_) {
stats_.input_frames++;
stats_.borrowed_frames++;
}
packet_callback_ = callback;
int ret = do_encode(frame.get(), obj, ms);
packet_callback_ = NULL;
@@ -386,17 +364,8 @@ public:
}
private:
int64_t timer_start() const { return measure_ ? av_gettime_relative() : 0; }
void timer_add(uint64_t &elapsed, int64_t start) const {
if (measure_)
elapsed += av_gettime_relative() - start;
}
int send_frame(AVFrame *frame) {
const int64_t start = timer_start();
const int ret = avcodec_send_frame(c_, frame);
timer_add(stats_.send_us, start);
return ret;
}
@@ -472,9 +441,7 @@ private:
auto start = util::now();
while (util::elapsed_ms(start) < DECODE_TIMEOUT_MS) {
const int64_t receive_start = timer_start();
ret = avcodec_receive_packet(c_, pkt_);
timer_add(stats_.receive_us, receive_start);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
}
@@ -489,7 +456,6 @@ private:
continue;
}
encoded = true;
const int64_t packet_start = timer_start();
if (packet_callback_) {
AVPacket *owned_pkt = av_packet_clone(pkt_);
if (!owned_pkt) {
@@ -505,7 +471,6 @@ private:
pkt_->flags & AV_PKT_FLAG_KEY, obj);
}
av_packet_unref(pkt_);
timer_add(stats_.packet_us, packet_start);
}
return 0;
@@ -782,12 +747,6 @@ extern "C" int ffmpeg_ram_encode_owned_packet(
}
}
extern "C" void ffmpeg_ram_encoder_stats(FFmpegRamEncoder *encoder,
RamEncodeStats *stats) {
if (encoder && stats)
*stats = encoder->stats_;
}
extern "C" int ffmpeg_ram_set_bitrate(FFmpegRamEncoder *encoder, int kbs) {
try {
return encoder->set_bitrate(kbs);

View File

@@ -1,17 +0,0 @@
#ifndef FFMPEG_RAM_ENCODE_STATS_H
#define FFMPEG_RAM_ENCODE_STATS_H
#include <stdint.h>
typedef struct RamEncodeStats {
uint64_t input_frames;
uint64_t borrowed_frames;
uint64_t copied_frames;
uint64_t copied_bytes;
uint64_t prepare_us;
uint64_t send_us;
uint64_t receive_us;
uint64_t packet_us;
} RamEncodeStats;
#endif

View File

@@ -2,7 +2,6 @@
#define FFMPEG_RAM_FFI_H
#include <stdint.h>
#include "ffmpeg_ram_encode_stats.h"
#define AV_NUM_DATA_POINTERS 8
@@ -30,7 +29,6 @@ int ffmpeg_ram_encode_owned_packet(void *encoder, const uint8_t *data,
void (*release)(void *, uint8_t *),
const void *obj, int64_t ms,
RamEncodePacketCallback callback);
void ffmpeg_ram_encoder_stats(void *encoder, RamEncodeStats *stats);
void ffmpeg_ram_free_encoder(void *encoder);
void ffmpeg_ram_free_packet(void *packet);
int ffmpeg_ram_get_linesize_offset_length(int pix_fmt, int width, int height,

View File

@@ -501,22 +501,6 @@ pub struct Encoder {
pub offset: Vec<i32>,
pub length: i32,
owned_nv12_input: bool,
measure: bool,
#[cfg(feature = "bytes")]
output_copy_us: u64,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct EncodeInputStats {
pub input_frames: u64,
pub borrowed_frames: u64,
pub copied_frames: u64,
pub copied_bytes: u64,
pub prepare_us: u64,
pub send_us: u64,
pub receive_us: u64,
pub packet_us: u64,
pub output_copy_us: u64,
}
impl Encoder {
@@ -565,8 +549,7 @@ impl Encoder {
}
let owned_nv12_input = ctx.name.ends_with("_v4l2m2m")
&& ctx.pixfmt == AVPixelFormat::AV_PIX_FMT_NV12 as i32
&& std::env::var("ONE_KVM_V4L2_OWNED_INPUT").as_deref() != Ok("0");
&& ctx.pixfmt == AVPixelFormat::AV_PIX_FMT_NV12 as i32;
Ok(Encoder {
codec,
frames: Box::into_raw(Box::new(Vec::<EncodeFrame>::new())),
@@ -575,9 +558,6 @@ impl Encoder {
offset,
length: length[0],
owned_nv12_input,
measure: std::env::var("ONE_KVM_VIDEO_STATS").as_deref() == Ok("1"),
#[cfg(feature = "bytes")]
output_copy_us: 0,
})
}
}
@@ -626,28 +606,6 @@ impl Encoder {
self.owned_nv12_input
}
pub fn input_stats(&self) -> Option<EncodeInputStats> {
if !self.measure {
return None;
}
let mut stats = unsafe { std::mem::zeroed() };
unsafe { crate::ffmpeg_ram::ffmpeg_ram_encoder_stats(self.codec, &mut stats) };
Some(EncodeInputStats {
input_frames: stats.input_frames,
borrowed_frames: stats.borrowed_frames,
copied_frames: stats.copied_frames,
copied_bytes: stats.copied_bytes,
prepare_us: stats.prepare_us,
send_us: stats.send_us,
receive_us: stats.receive_us,
packet_us: stats.packet_us,
#[cfg(feature = "bytes")]
output_copy_us: self.output_copy_us,
#[cfg(not(feature = "bytes"))]
output_copy_us: 0,
})
}
#[cfg(feature = "bytes")]
pub fn encode_owned_bytes(
&mut self,
@@ -688,7 +646,6 @@ impl Encoder {
if !self.ctx.name.contains("v4l2m2m") {
return Ok(frames);
}
let start = self.measure.then(std::time::Instant::now);
let detached = frames
.into_iter()
.map(|frame| EncodeBytesFrame {
@@ -697,9 +654,6 @@ impl Encoder {
key: frame.key,
})
.collect();
if let Some(start) = start {
self.output_copy_us += start.elapsed().as_micros() as u64;
}
Ok(detached)
}