Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ apply_managed_component_patch(patches/esp_h264-1.4.0-external-db.patch)
# An access unit that overflows its output buffer raises the minimum QP, so
# the next IDR fits instead of overflowing at the same QP every second.
apply_managed_component_patch(patches/esp_h264-1.4.1-overflow-qp.patch)
# The average QP of each frame for /webrtc/stats, and the QP range that the
# bench-only encoder toggles set (CONFIG_JETKVM_VIDEO_TUNING).
apply_managed_component_patch(patches/esp_h264-1.4.1-frame-qp.patch)
# These three libSRTP patches stack on the same CMakeLists, so an earlier one
# cannot reverse-check once a later one is applied. Detect the final state.
set(_srtp_component_cmake
Expand Down
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
# docker buildx build --build-arg VERSION=<PROJECT_VER> --build-arg BOARD=<ethernet|wifi> \
# [--build-arg MOCK_CLOUD_JWKS_URL=<url>] [--build-arg WIFI_SETUP_LOG_CODE=1] \
# [--build-arg USB_DEBUGGING=1] [--build-arg MEMORY_OWNERS=1] \
# --output type=local,dest=<dir> .
# [--build-arg VIDEO_TUNING=1] --output type=local,dest=<dir> .
#
# USB_DEBUGGING=1 keeps the USB Serial/JTAG port on by default.
# MEMORY_OWNERS=1 counts heap bytes per task (components/jetkvm_memory_owners).
# VIDEO_TUNING=1 lets /webrtc/stats set encoder toggles (tools/sprites).
#
# <dir> gets jetkvm-mini.bin (the update image) plus what
# tools/e2e/serial_flash.sh writes over USB: bootloader.bin,
Expand Down Expand Up @@ -47,6 +48,7 @@ ARG MOCK_CLOUD_JWKS_URL=
ARG WIFI_SETUP_LOG_CODE=0
ARG USB_DEBUGGING=0
ARG MEMORY_OWNERS=0
ARG VIDEO_TUNING=0
# A fresh tree's first configure stops once and asks for another run
# (CMakeLists.txt). The Component Manager does not lock its cache, so only one
# configure at a time may hold it.
Expand All @@ -64,6 +66,11 @@ RUN --mount=type=cache,target=/component-cache,sharing=locked \
1) echo CONFIG_JETKVM_MEMORY_OWNERS=y >> /tmp/bench.defaults ;; \
*) echo "MEMORY_OWNERS must be 0 or 1" >&2; exit 1 ;; \
esac && \
case "$VIDEO_TUNING" in \
0) ;; \
1) echo CONFIG_JETKVM_VIDEO_TUNING=y >> /tmp/bench.defaults ;; \
*) echo "VIDEO_TUNING must be 0 or 1" >&2; exit 1 ;; \
esac && \
if [ -s /tmp/bench.defaults ]; then \
defaults=("-DSDKCONFIG_DEFAULTS=/src/sdkconfig.defaults;/tmp/bench.defaults"); \
fi && \
Expand Down
49 changes: 45 additions & 4 deletions components/jetkvm_video/jetkvm_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "jetkvm_video.h"
#include "jetkvm_video_helpers.h"
#include "jetkvm_video_quality.h"
#include "jetkvm_video_tuning.h"
#include "video_encode_totals.h"
#include "hdmi_hpd_retry.h"
#include "hdmi_bridge_fault.h"
Expand Down Expand Up @@ -405,6 +406,9 @@ typedef struct {
uint8_t h264_fps;
uint8_t h264_gop;
uint32_t h264_bitrate;
#if CONFIG_JETKVM_VIDEO_TUNING
uint8_t h264_qp_max;
#endif
/* The transport refused the encoder's mode; cleared by csi_destroy(). */
bool h264_mode_refused;
esp_h264_raw_format_t h264_pic_type;
Expand Down Expand Up @@ -1538,7 +1542,8 @@ static void video_metrics_record_frame(app_ctx_t *ctx,
bool keyframe,
bool sink_accepted,
int64_t csi_dma_eof_timestamp_us,
int64_t h264_encode_start_timestamp_us)
int64_t h264_encode_start_timestamp_us,
uint32_t qp_x100)
{
uint32_t handoff_us = 0;
if (h264_encode_start_timestamp_us >= csi_dma_eof_timestamp_us) {
Expand Down Expand Up @@ -1583,7 +1588,7 @@ static void video_metrics_record_frame(app_ctx_t *ctx,
}
video_encode_totals_add(&s_video_encode_totals,
h264_encode_start_timestamp_us + encode_time_us,
keyframe, encode_time_us, handoff_us);
keyframe, encode_time_us, handoff_us, qp_x100);
portEXIT_CRITICAL(&s_video_metrics_mux);
}

Expand All @@ -1603,6 +1608,8 @@ void jetkvm_video_get_encode_stats(webrtc_transport_video_encode_t *encode)
.idr_encode_max_us = longest.idr_encode_us,
.handoff_us = totals.handoff_us,
.handoff_max_us = longest.handoff_us,
.qp_x100_sum = totals.qp_x100_sum,
.coarse_frames = totals.coarse_frames,
};
}

Expand Down Expand Up @@ -1925,11 +1932,40 @@ static uint32_t h264_target_bitrate(uint16_t width, uint16_t height,
return bitrate;
}

#if CONFIG_JETKVM_VIDEO_TUNING
static void h264_hw_apply_qp_max(app_ctx_t *ctx)
{
const uint32_t tuned = jetkvm_video_tuning_qp_max();
const uint8_t qp_max = tuned ? (uint8_t)tuned : ESP_H264_QP_MAX;
if (qp_max == ctx->h264_qp_max) {
return;
}
const uint8_t qp_min = qp_max < H264_QUALITY_QP_MIN ?
qp_max : H264_QUALITY_QP_MIN;
if (esp_h264_enc_hw_set_qp_range(ctx->h264_enc, qp_min, qp_max) !=
ESP_H264_ERR_OK) {
ESP_LOGE(TAG, "h264hw: QP range %u-%u failed", (unsigned)qp_min,
(unsigned)qp_max);
return;
}
ESP_LOGI(TAG, "h264hw: maximum QP %u -> %u",
(unsigned)ctx->h264_qp_max, (unsigned)qp_max);
ctx->h264_qp_max = qp_max;
}
#endif

static bool h264_hw_update_quality(app_ctx_t *ctx)
{
const uint32_t quality_permille = jetkvm_video_quality_get_permille();
const uint32_t ceiling = h264_target_bitrate(
uint32_t ceiling = h264_target_bitrate(
ctx->h264_width, ctx->h264_height, ctx->h264_fps, quality_permille);
#if CONFIG_JETKVM_VIDEO_TUNING
h264_hw_apply_qp_max(ctx);
const uint32_t tuned_rate_kbps = jetkvm_video_tuning_rate_kbps();
if (tuned_rate_kbps) {
ceiling = tuned_rate_kbps * 1000U;
}
#endif
/* Only Auto follows receiver feedback; a manual preset is a fixed rate. */
const bool adaptive =
quality_permille == JETKVM_VIDEO_QUALITY_AUTO_PERMILLE;
Expand Down Expand Up @@ -2073,6 +2109,9 @@ static esp_err_t h264_hw_ensure(app_ctx_t *ctx, uint16_t width, uint16_t height,
ctx->h264_fps = configured_fps;
ctx->h264_gop = configured_gop;
ctx->h264_bitrate = bitrate;
#if CONFIG_JETKVM_VIDEO_TUNING
ctx->h264_qp_max = ESP_H264_QP_MAX;
#endif
ctx->h264_pic_type = pic_type;
ctx->h264_serial_dumped = false;
ESP_LOGI(TAG, "h264hw: ready %ux%u@%u %s gop=%u bitrate=%" PRIu32 " out=%" PRIu32,
Expand Down Expand Up @@ -2381,6 +2420,8 @@ static void h264_pipeline_encode_one(app_ctx_t *ctx,
goto cancel_output;
}

uint32_t qp_x100 = 0;
esp_h264_enc_hw_get_frame_qp_x100(ctx->h264_enc, &qp_x100);
bool keyframe = out_frame.frame_type == ESP_H264_FRAME_TYPE_IDR;
/* Only this task writes encode_frames; the frame is counted below. */
app_h264_frame_meta_t meta = {
Expand All @@ -2405,7 +2446,7 @@ static void h264_pipeline_encode_one(app_ctx_t *ctx,
rtc_reserved = false; /* Commit consumes the lease even when rejected. */
video_metrics_record_frame(ctx, encode_time_us, capture_sequence,
out_frame.length, keyframe, sink_accepted,
capture_timestamp_us, start_us);
capture_timestamp_us, start_us, qp_x100);
if (!sink_accepted) {
goto cancel_output;
}
Expand Down
24 changes: 15 additions & 9 deletions components/jetkvm_video/tests/test_encode_totals.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@
/* The first frames come long after boot. Each type has its count, its
* sum and its longest time; the handoff is over both types. */
const int64_t start = 5 * W + 123;
video_encode_totals_add(&totals, start, false, 33000, 100);
video_encode_totals_add(&totals, start + 33000, true, 27000, 300);
video_encode_totals_add(&totals, start + 66000, false, 32000, 200);
video_encode_totals_add(&totals, start, false, 33000, 100, 2800);
video_encode_totals_add(&totals, start + 33000, true, 27000, 300, 2800);
video_encode_totals_add(&totals, start + 66000, false, 32000, 200, 2800);
assert(totals.p_frames == 2 && totals.p_encode_us == 65000);
assert(totals.idr_frames == 1 && totals.idr_encode_us == 27000);
assert(totals.handoff_us == 600);
assert_longest(start + 66000, 33000, 27000, 300);

/* The next window follows at once. Its shorter frame does not hide the
* longest times of the window before, up to the window's last moment. */
video_encode_totals_add(&totals, start + W, false, 31000, 50);
video_encode_totals_add(&totals, start + W, false, 31000, 50, 2800);
assert_longest(start + W, 33000, 27000, 300);
assert_longest(start + 2 * W - 1, 33000, 27000, 300);

/* A frame of the third window ends the first: its times are gone, and
* no keyframe is left in the two windows. */
video_encode_totals_add(&totals, start + 2 * W, false, 30000, 40);
video_encode_totals_add(&totals, start + 2 * W, false, 30000, 40, 2800);
assert_longest(start + 2 * W, 31000, 0, 50);

/* Encoding stops. The last window's times stay for one more window
Expand All @@ -66,21 +66,27 @@
assert(totals.idr_frames == 1 && totals.handoff_us == 690);

/* Encoding starts again later: no time of the old windows comes back. */
video_encode_totals_add(&totals, start + 20 * W + 7, true, 28000, 10);
video_encode_totals_add(&totals, start + 20 * W + 7, true, 28000, 10, 2800);
assert_longest(start + 20 * W + 7, 0, 28000, 10);
assert_longest(start + 21 * W + 6, 0, 28000, 10);
video_encode_totals_add(&totals, start + 21 * W + 7, false, 29000, 20);
video_encode_totals_add(&totals, start + 21 * W + 7, false, 29000, 20, 2800);
assert_longest(start + 21 * W + 7, 29000, 28000, 20);

/* The sums do not wrap at 32 bits (about 71 minutes of encode time). */
video_encode_totals_t wide = {0};
for (int frame = 0; frame < 3; ++frame) {
video_encode_totals_add(&wide, frame, false, UINT32_MAX, UINT32_MAX);
video_encode_totals_add(&wide, frame, false, UINT32_MAX, UINT32_MAX, 0);
}
assert(wide.p_encode_us == 3 * (uint64_t)UINT32_MAX);
assert(wide.handoff_us == 3 * (uint64_t)UINT32_MAX);

puts("encoder totals since boot: sums per type, longest of two windows: PASS");
/* A frame at an average QP of 50 is coarse; one just below is not. */
video_encode_totals_t qp = {0};
video_encode_totals_add(&qp, 0, false, 0, 0, 4999);
video_encode_totals_add(&qp, 1, true, 0, 0, 5000);
assert(qp.qp_x100_sum == 9999 && qp.coarse_frames == 1);

puts("encoder totals since boot: sums per type, longest of two windows, QP: PASS");
return 0;
}
"""
Expand Down
22 changes: 18 additions & 4 deletions components/jetkvm_video/tests/test_output_reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def structure(name):
uint32_t pending_sync, last_completed_sync;
bool last_completed_success;
int encode_result;
uint32_t encoded_length;
uint32_t encoded_length, frame_qp_x100;
int64_t now_us;
app_h264_frame_meta_t committed_meta;
} state;
Expand Down Expand Up @@ -252,6 +252,12 @@ def structure(name):
state.applied_bitrate = bitrate;
return ESP_H264_ERR_OK;
}
static esp_h264_err_t esp_h264_enc_hw_get_frame_qp_x100(void *encoder, uint32_t *qp_x100)
{
assert(encoder == &encoder_param && !lock_depth && state.encodes > 0);
*qp_x100 = state.frame_qp_x100;
return ESP_H264_ERR_OK;
}
static esp_h264_err_t esp_h264_enc_force_idr(int *base)
{
assert(base == &encoder_param.base);
Expand Down Expand Up @@ -470,6 +476,7 @@ def structure(name):
/* A successful IDR and P frame both use the lease throughout encoding. */
ctx = reset();
state.pending_sync = 41;
state.frame_qp_x100 = 5000;
encode(&ctx, 2);
assert(state.encodes == 1 && state.commits == 1 && state.cancels == 0);
assert(ctx.metrics.encode_frames == 1 && ctx.metrics.sink_frames == 1);
Expand All @@ -482,12 +489,14 @@ def structure(name):
(uint32_t)(state.committed_meta.encode_start_timestamp_us - 90000));
assert(ctx.timing_window.idr_encode_count == 1 && ctx.timing_window.idr_encode_us[0] == 1000);
assert(ctx.timing_window.p_encode_count == 0);
/* The totals since boot take the same frame: its type, its encode time
* and its handoff. */
/* The totals since boot take the same frame: its type, its encode time,
* its handoff and its QP. */
assert(idr_frames_since_reset() == 1 && p_frames_since_reset() == 0);
assert(s_video_encode_totals.idr_encode_us - totals_at_reset.idr_encode_us == 1000);
assert(s_video_encode_totals.p_encode_us == totals_at_reset.p_encode_us);
assert(s_video_encode_totals.handoff_us - totals_at_reset.handoff_us == 10000);
assert(s_video_encode_totals.qp_x100_sum - totals_at_reset.qp_x100_sum == 5000);
assert(s_video_encode_totals.coarse_frames - totals_at_reset.coarse_frames == 1);
assert(state.complete_sync == 1 && state.last_completed_sync == 41);
assert(state.last_completed_success && ctx.h264_sync_idr_generation == 0);
assert(state.committed_meta.sequence == 1 && state.committed_meta.capture_sequence == 2);
Expand All @@ -499,6 +508,7 @@ def structure(name):
assert(state.committed_meta.width == 1920 && state.committed_meta.height == 1080);
assert(state.committed_meta.fps == 30 && state.committed_meta.keyframe);
assert(!state.committed_meta.encode_overlapped_capture);
state.frame_qp_x100 = 2850;
encode(&ctx, 4);
assert(state.commits == 2 && state.cancels == 0 && state.force_idr == 1);
assert(!state.committed_meta.keyframe && state.committed_meta.sync_idr_generation == 0);
Expand All @@ -507,6 +517,8 @@ def structure(name):
assert(s_video_encode_totals.p_encode_us - totals_at_reset.p_encode_us ==
(uint64_t)(state.committed_meta.encode_done_timestamp_us -
state.committed_meta.encode_start_timestamp_us));
assert(s_video_encode_totals.qp_x100_sum - totals_at_reset.qp_x100_sum == 7850);
assert(s_video_encode_totals.coarse_frames - totals_at_reset.coarse_frames == 1);

/* Encoder errors and empty output retire/requeue sync and restart the
* reference chain. Failed output never reaches the consuming commit. */
Expand All @@ -515,9 +527,11 @@ def structure(name):
ctx = reset();
state.pending_sync = 41;
state.encode_result = failures[i];
state.frame_qp_x100 = 5100;
if (state.encode_result == ESP_H264_ERR_OK) state.encoded_length = 0;
encode(&ctx, 2);
assert(ctx.metrics.encode_overflows == (i < 2));
assert(s_video_encode_totals.qp_x100_sum == totals_at_reset.qp_x100_sum);
assert_recoverable_failure(&ctx);
}

Expand Down Expand Up @@ -560,7 +574,7 @@ def structure(name):
assert(state.commits == 2 && state.cancels == 0 && !state.force_next_idr);
if (pending) assert(state.last_completed_success && state.pending_sync == 0);
}
puts("Encoder output leases: bitrate retune/retry, skip before IDR, DMA ownership, cancellation, reference recovery: PASS");
puts("Encoder output leases: bitrate retune/retry, skip before IDR, DMA ownership, cancellation, reference recovery, frame QP: PASS");
}
"""

Expand Down
16 changes: 13 additions & 3 deletions components/jetkvm_video/video_encode_totals.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Encode times of the hardware H.264 encoder since boot, for /webrtc/stats.
/* Encode times and QP of the hardware H.264 encoder since boot, for /webrtc/stats.
* No lock and no clock of its own: the caller holds the lock of the video
* metrics and gives the time. */
#pragma once
Expand All @@ -13,6 +13,8 @@
* a full window without a frame, the next frame starts a new one. */
#define VIDEO_ENCODE_LONGEST_WINDOW_US (2 * 1000 * 1000)

#define VIDEO_ENCODE_COARSE_QP_X100 5000U

typedef struct {
uint32_t p_encode_us;
uint32_t idr_encode_us;
Expand All @@ -26,16 +28,20 @@ typedef struct {
uint64_t p_encode_us;
uint64_t idr_encode_us;
uint64_t handoff_us;
uint64_t qp_x100_sum;
uint32_t coarse_frames;
int64_t window_end_us;
video_encode_longest_t current;
video_encode_longest_t previous;
} video_encode_totals_t;

/* One encoded frame: now_us is the end of its encode. */
/* One encoded frame: now_us is the end of its encode, qp_x100 its average
* QP in hundredths. */
static inline void video_encode_totals_add(video_encode_totals_t *totals,
int64_t now_us, bool keyframe,
uint32_t encode_us,
uint32_t handoff_us)
uint32_t handoff_us,
uint32_t qp_x100)
{
if (now_us >= totals->window_end_us) {
if (now_us - totals->window_end_us < VIDEO_ENCODE_LONGEST_WINDOW_US) {
Expand Down Expand Up @@ -64,6 +70,10 @@ static inline void video_encode_totals_add(video_encode_totals_t *totals,
if (handoff_us > totals->current.handoff_us) {
totals->current.handoff_us = handoff_us;
}
totals->qp_x100_sum += qp_x100;
if (qp_x100 >= VIDEO_ENCODE_COARSE_QP_X100) {
totals->coarse_frames++;
}
}

/* The longest times a report at now_us has; zero for a kind of frame that
Expand Down
7 changes: 6 additions & 1 deletion components/jetkvm_video_quality/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
set(srcs "jetkvm_video_quality.c")
if(CONFIG_JETKVM_VIDEO_TUNING)
list(APPEND srcs "jetkvm_video_tuning.c")
endif()

idf_component_register(
SRCS "jetkvm_video_quality.c"
SRCS ${srcs}
INCLUDE_DIRS "include")

target_compile_options(${COMPONENT_LIB} PRIVATE -Werror)
17 changes: 17 additions & 0 deletions components/jetkvm_video_quality/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
menu "JetKVM video tuning"

config JETKVM_VIDEO_TUNING
bool "Encoder toggles in /webrtc/stats (bench builds only)"
default n
help
Let GET /webrtc/stats?bitrate=1&qpMax=<n>&rateKbps=<n> set the
maximum QP and the rate target of the H.264 encoder, and give the
values in force in that answer (videoTuning). 0 is the firmware's
value. tools/sprites uses them to measure the picture.

Bench builds set this option (the Dockerfile, with the
VIDEO_TUNING build argument that
tools/combined_load/runner/build.mjs gives). A release leaves it
off, and tools/release_config.py refuses a release build with it.

endmenu
Loading
Loading