perf: 降低串流延迟(软编降档、编码器实时参数、NV12 转换与绘制优化)
- VideoEncoder 按实时流配置编码器:实时优先、不攒帧(KEY_LATENCY)、 禁 B 帧、CBR;个别 ROM 拒绝这些参数时自动退回默认配置 - 新增 VideoEncoder.isSoftwareEncoder:全部候选编码器都是 c2.android./OMX.google. 时判定为软编,ReceiverSession 把长边上限从 1920 降到 1280 (模拟器实测 864x1920 → 576x1280,滑动时帧率明显回升) - ConvertNv12 改为每 2 个像素复用一次 U/V 计算,裁剪改成无分支写法 - 串流窗口画面铺满客户区时跳过整屏刷黑,省一次全屏填充 - README 3.4 补充编码器实时配置与软编降档说明
This commit is contained in:
1 parent
04f2b800b2
commit
cbbc2a642d
5 files changed
+135
-30
No files matched your search
@@ -17,6 +17,7 @@
|
||||
#include <mfidl.h>
|
||||
#include <mftransform.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -297,14 +298,9 @@ class FrameStore {
|
||||
uint64_t frames_ = 0;
|
||||
};
|
||||
|
||||
void ClampToByte(int value, uint8_t* out) {
|
||||
if (value < 0) {
|
||||
*out = 0;
|
||||
} else if (value > 255) {
|
||||
*out = 255;
|
||||
} else {
|
||||
*out = static_cast<uint8_t>(value);
|
||||
}
|
||||
// 单通道裁剪到 0..255,展开成无分支写法:每帧要算百万级像素,if 链是热点。
|
||||
inline uint8_t ClampByte(int value) {
|
||||
return static_cast<uint8_t>((std::max)(0, (std::min)(255, value)));
|
||||
}
|
||||
|
||||
// BT.601 有限范围 NV12 -> BGRA(按显示区域裁剪掉解码补齐的行)。
|
||||
@@ -341,16 +337,24 @@ void ConvertNv12(const uint8_t* data,
|
||||
uv_plane + static_cast<size_t>(source_row / 2) * chroma_stride + crop_x;
|
||||
uint8_t* dst =
|
||||
out->data() + static_cast<size_t>(row) * visible_width * 4;
|
||||
for (int column = 0; column < visible_width; ++column) {
|
||||
const int c = static_cast<int>(y_row[column]) - 16;
|
||||
const int u = static_cast<int>(uv_row[(column & ~1)]) - 128;
|
||||
const int v = static_cast<int>(uv_row[(column & ~1) + 1]) - 128;
|
||||
const int scaled = c < 0 ? 0 : c;
|
||||
ClampToByte((298 * scaled + 516 * u + 128) >> 8, dst);
|
||||
ClampToByte((298 * scaled - 100 * u - 208 * v + 128) >> 8, dst + 1);
|
||||
ClampToByte((298 * scaled + 409 * v + 128) >> 8, dst + 2);
|
||||
dst[3] = 0xff;
|
||||
dst += 4;
|
||||
// NV12 的色度是 2x2 采样:一次算好两个像素共用的 U/V 项,省一半查表和乘法。
|
||||
for (int column = 0; column < visible_width; column += 2) {
|
||||
const int uv_index = column & ~1;
|
||||
const int u = static_cast<int>(uv_row[uv_index]) - 128;
|
||||
const int v = static_cast<int>(uv_row[uv_index + 1]) - 128;
|
||||
const int blue_term = 516 * u + 128;
|
||||
const int green_term = -100 * u - 208 * v + 128;
|
||||
const int red_term = 409 * v + 128;
|
||||
const int pair = visible_width - column >= 2 ? 2 : 1;
|
||||
for (int k = 0; k < pair; ++k) {
|
||||
const int c = static_cast<int>(y_row[column + k]) - 16;
|
||||
const int luma = 298 * (c < 0 ? 0 : c);
|
||||
dst[0] = ClampByte((luma + blue_term) >> 8);
|
||||
dst[1] = ClampByte((luma + green_term) >> 8);
|
||||
dst[2] = ClampByte((luma + red_term) >> 8);
|
||||
dst[3] = 0xff;
|
||||
dst += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,7 +307,6 @@ void VideoWindow::Paint() {
|
||||
target = back_dc_;
|
||||
}
|
||||
}
|
||||
FillRect(target, &client, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
@@ -333,6 +332,13 @@ void VideoWindow::Paint() {
|
||||
const int offset_x = client.left + (client_width - draw_width) / 2;
|
||||
const int offset_y = client.top + (client_height - draw_height) / 2;
|
||||
|
||||
// 画面正好铺满客户区时不用先刷黑:StretchDIBits 会把整块覆盖掉,
|
||||
// 省掉每秒几十次的全屏填充(1080p 一帧就是 8MB 内存带宽)。
|
||||
if (draw_width < client_width || draw_height < client_height) {
|
||||
FillRect(target, &client,
|
||||
static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
}
|
||||
|
||||
// 记住这一帧的尺寸和实际绘制矩形,鼠标点击时据此换算成画面像素。
|
||||
frame_width_ = width;
|
||||
frame_height_ = height;
|
||||
@@ -352,6 +358,8 @@ void VideoWindow::Paint() {
|
||||
StretchDIBits(target, offset_x, offset_y, draw_width, draw_height, 0, 0,
|
||||
width, height, frame->data(), &info, DIB_RGB_COLORS, SRCCOPY);
|
||||
} else {
|
||||
FillRect(target, &client,
|
||||
static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
// 还没有画面:清掉坐标映射,避免拿上一路的尺寸误点。
|
||||
frame_width_ = 0;
|
||||
frame_height_ = 0;
|
||||
|
||||
Reference in new issue
Block a user