fix(calling): no crash in AMD's H.264 encoder while sharing
- MF encoder input buffer allocated for the frame rounded up to 16x16, tail zeroed: amdh264enc64 read past the exact-size NV12 buffer (access violation on the encoder thread, 868 px wide share) - odd frame sizes are cut to even ones - diagnostics.log keeps the MFH264 lines (frame size, re-init) - vendor change listed in vendor/webrtc-sys/PATCH.md Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -21,7 +21,8 @@ const OWN_CRATE_PREFIXES: [&str; 10] = [
21
21
"teams",
22
22
];
23
23
24
const WEBRTC_AUDIO_MARKERS: [&str; 5] = [
24
const WEBRTC_KEPT_MARKERS: [&str; 6] = [
25
"MFH264",
25
26
"AdmProxy",
26
27
"audio_device",
27
28
"core_audio",
@@ -265,7 +266,7 @@ fn keeps_webrtc_line(level: log::Level, text: &str) -> bool {
265
266
{
266
267
return true;
267
268
}
268
WEBRTC_AUDIO_MARKERS
269
WEBRTC_KEPT_MARKERS
269
270
.iter()
270
271
.any(|marker| text.contains(marker))
271
272
&& !WEBRTC_SKIPPED.iter().any(|skipped| text.contains(skipped))
@@ -284,6 +285,14 @@ mod tests {
284
285
use super::*;
285
286
use tempfile::tempdir;
286
287
288
#[test]
289
fn hardware_encoder_lines_are_kept() {
290
assert!(keeps_webrtc_line(
291
log::Level::Info,
292
"(mf_h264_encoder_impl.cc:250): MFH264: initialized 868x488 @30fps"
293
));
294
}
295
287
296
#[test]
288
297
fn audio_lines_are_kept_and_endpoint_dumps_and_noise_are_skipped() {
289
298
assert!(keeps_webrtc_line(
@@ -21,7 +21,8 @@ const OWN_CRATE_PREFIXES: [&str; 10] = [21
"teams",21
"teams",22
];22
];23
23
24
const WEBRTC_AUDIO_MARKERS: [&str; 5] = [24
const WEBRTC_KEPT_MARKERS: [&str; 6] = [25
"MFH264",25
"AdmProxy",26
"AdmProxy",26
"audio_device",27
"audio_device",27
"core_audio",28
"core_audio",@@ -265,7 +266,7 @@ fn keeps_webrtc_line(level: log::Level, text: &str) -> bool {265
{266
{266
return true;267
return true;267
}268
}268
WEBRTC_AUDIO_MARKERS269
WEBRTC_KEPT_MARKERS269
.iter()270
.iter()270
.any(|marker| text.contains(marker))271
.any(|marker| text.contains(marker))271
&& !WEBRTC_SKIPPED.iter().any(|skipped| text.contains(skipped))272
&& !WEBRTC_SKIPPED.iter().any(|skipped| text.contains(skipped))@@ -284,6 +285,14 @@ mod tests {284
use super::*;285
use super::*;285
use tempfile::tempdir;286
use tempfile::tempdir;286
287
288
#[test]289
fn hardware_encoder_lines_are_kept() {290
assert!(keeps_webrtc_line(291
log::Level::Info,292
"(mf_h264_encoder_impl.cc:250): MFH264: initialized 868x488 @30fps"293
));294
}295
287
#[test]296
#[test]288
fn audio_lines_are_kept_and_endpoint_dumps_and_noise_are_skipped() {297
fn audio_lines_are_kept_and_endpoint_dumps_and_noise_are_skipped() {289
assert!(keeps_webrtc_line(298
assert!(keeps_webrtc_line(@@ -11,3 +11,4 @@ Upstream: crates.io `webrtc-sys 0.3.48` (livekit/rust-sdks). Wired in via `[patc
11
11
| `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `SetPlayoutDevice` / `SetRecordingDevice(WindowsDeviceType)` turn `kDefaultCommunicationDevice` into `kDefaultDevice` | `WebRtcVoiceEngine::Init` selects the communications default at every call start; the app's "Default device" means the Windows default device (seen 2026-10-14: speaker came up on the communications default, user picked the default device by hand every call) |
12
12
| `src/noise_hook.rs`, `src/noise_hook.cpp`, `include/livekit/noise_hook.h`, `build.rs`, `src/lib.rs`, `src/peer_connection_factory.cpp` | The factory's APM gets a capture post processor that calls a Rust hook (`noise_hook::set_capture_hook`) on the full-band capture buffer | ML noise suppression (RNNoise) on the device track, keeping echo cancellation on the real playout reference |
13
13
| `src/vaapi/vaapi_h264_encoder_wrapper.cpp` | `bitstream_put_ui()` no longer shifts a 32-bit word by 32 when a value fills a fresh word | That shift is undefined; built with clang -O2 it wrote the NAL start code of the packed slice header as `00 00 00 00`, and the Intel iHD driver (26.3.5) read past the buffer and segfaulted in `vaRenderPicture` on the first camera frame (2026-11-24, Core Ultra 7 165U). gcc -O0 happened to produce `00 00 00 01`, which hid it |
14
| `src/mf/mf_h264_encoder_impl.cpp` | The NV12 input buffer is allocated for a frame rounded up to 16x16, the tail zeroed; frame sizes are made even | AMD's H.264 MFT (`amdh264enc64.dll`) copies the input in 16-row/16-column blocks and read past the exact-size buffer: access violation on the encoder thread in a call with a 868 px wide frame (2026-11-26) |
@@ -11,3 +11,4 @@ Upstream: crates.io `webrtc-sys 0.3.48` (livekit/rust-sdks). Wired in via `[patc11
| `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `SetPlayoutDevice` / `SetRecordingDevice(WindowsDeviceType)` turn `kDefaultCommunicationDevice` into `kDefaultDevice` | `WebRtcVoiceEngine::Init` selects the communications default at every call start; the app's "Default device" means the Windows default device (seen 2026-10-14: speaker came up on the communications default, user picked the default device by hand every call) |11
| `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `SetPlayoutDevice` / `SetRecordingDevice(WindowsDeviceType)` turn `kDefaultCommunicationDevice` into `kDefaultDevice` | `WebRtcVoiceEngine::Init` selects the communications default at every call start; the app's "Default device" means the Windows default device (seen 2026-10-14: speaker came up on the communications default, user picked the default device by hand every call) |12
| `src/noise_hook.rs`, `src/noise_hook.cpp`, `include/livekit/noise_hook.h`, `build.rs`, `src/lib.rs`, `src/peer_connection_factory.cpp` | The factory's APM gets a capture post processor that calls a Rust hook (`noise_hook::set_capture_hook`) on the full-band capture buffer | ML noise suppression (RNNoise) on the device track, keeping echo cancellation on the real playout reference |12
| `src/noise_hook.rs`, `src/noise_hook.cpp`, `include/livekit/noise_hook.h`, `build.rs`, `src/lib.rs`, `src/peer_connection_factory.cpp` | The factory's APM gets a capture post processor that calls a Rust hook (`noise_hook::set_capture_hook`) on the full-band capture buffer | ML noise suppression (RNNoise) on the device track, keeping echo cancellation on the real playout reference |13
| `src/vaapi/vaapi_h264_encoder_wrapper.cpp` | `bitstream_put_ui()` no longer shifts a 32-bit word by 32 when a value fills a fresh word | That shift is undefined; built with clang -O2 it wrote the NAL start code of the packed slice header as `00 00 00 00`, and the Intel iHD driver (26.3.5) read past the buffer and segfaulted in `vaRenderPicture` on the first camera frame (2026-11-24, Core Ultra 7 165U). gcc -O0 happened to produce `00 00 00 01`, which hid it |13
| `src/vaapi/vaapi_h264_encoder_wrapper.cpp` | `bitstream_put_ui()` no longer shifts a 32-bit word by 32 when a value fills a fresh word | That shift is undefined; built with clang -O2 it wrote the NAL start code of the packed slice header as `00 00 00 00`, and the Intel iHD driver (26.3.5) read past the buffer and segfaulted in `vaRenderPicture` on the first camera frame (2026-11-24, Core Ultra 7 165U). gcc -O0 happened to produce `00 00 00 01`, which hid it |14
| `src/mf/mf_h264_encoder_impl.cpp` | The NV12 input buffer is allocated for a frame rounded up to 16x16, the tail zeroed; frame sizes are made even | AMD's H.264 MFT (`amdh264enc64.dll`) copies the input in 16-row/16-column blocks and read past the exact-size buffer: access violation on the encoder thread in a call with a 868 px wide frame (2026-11-26) |@@ -40,6 +40,18 @@ bool ContainsIdrNal(const uint8_t* data, size_t size) {
40
40
return false;
41
41
}
42
42
43
// Hardware encoders (AMD's amdh264enc64) copy the NV12 input in 16-row and
44
// 16-column blocks and read past a buffer sized to the exact frame.
45
constexpr uint32_t kEncoderBlock = 16;
46
47
uint32_t EvenSize(int size) {
48
return size > 0 ? static_cast<uint32_t>(size) & ~1u : 0;
49
}
50
51
uint32_t AlignedSize(uint32_t size) {
52
return (size + kEncoderBlock - 1) / kEncoderBlock * kEncoderBlock;
53
}
54
43
55
} // namespace
44
56
45
57
MFH264EncoderImpl::MFH264EncoderImpl(const Environment& env,
@@ -257,8 +269,8 @@ int32_t MFH264EncoderImpl::InitEncode(const VideoCodec* inst,
257
269
}
258
270
mf_started_ = true;
259
271
260
width_ = inst->width;
261
height_ = inst->height;
272
width_ = EvenSize(inst->width);
273
height_ = EvenSize(inst->height);
262
274
max_fps_ = std::max<uint32_t>(inst->maxFramerate, 1);
263
275
target_bps_ = std::max<uint32_t>(inst->startBitrate * 1000, 150000);
264
276
frame_index_ = 0;
@@ -556,11 +568,17 @@ int32_t MFH264EncoderImpl::Encode(
556
568
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
557
569
}
558
570
559
if (static_cast<uint32_t>(i420->width()) != width_ ||
560
static_cast<uint32_t>(i420->height()) != height_) {
571
const uint32_t frame_width = EvenSize(i420->width());
572
const uint32_t frame_height = EvenSize(i420->height());
573
if (frame_width < 2 || frame_height < 2) {
574
return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
575
}
576
if (frame_width != width_ || frame_height != height_) {
561
577
// Resolution changed dynamically: re-init MFT or fall back cleanly.
562
width_ = static_cast<uint32_t>(i420->width());
563
height_ = static_cast<uint32_t>(i420->height());
578
width_ = frame_width;
579
height_ = frame_height;
580
RTC_LOG(LS_INFO) << "MFH264: input size changed to " << width_ << "x"
581
<< height_;
564
582
mft_.Reset();
565
583
codec_api_.Reset();
566
584
event_gen_.Reset();
@@ -583,9 +601,11 @@ int32_t MFH264EncoderImpl::Encode(
583
601
}
584
602
585
603
const uint32_t nv12_size = width_ * height_ * 3 / 2;
604
const uint32_t padded_size =
605
AlignedSize(width_) * AlignedSize(height_) * 3 / 2;
586
606
ComPtr<IMFMediaBuffer> in_buffer;
587
607
ComPtr<IMFSample> in_sample;
588
if (FAILED(MFCreateMemoryBuffer(nv12_size, &in_buffer)) ||
608
if (FAILED(MFCreateMemoryBuffer(padded_size, &in_buffer)) ||
589
609
FAILED(MFCreateSample(&in_sample))) {
590
610
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
591
611
}
@@ -594,6 +614,7 @@ int32_t MFH264EncoderImpl::Encode(
594
614
if (FAILED(in_buffer->Lock(&dst, nullptr, nullptr)) || !dst) {
595
615
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
596
616
}
617
std::memset(dst + nv12_size, 0, padded_size - nv12_size);
597
618
uint8_t* dst_y = dst;
598
619
uint8_t* dst_uv = dst + width_ * height_;
599
620
libyuv::I420ToNV12(i420->DataY(), i420->StrideY(), i420->DataU(),
@@ -40,6 +40,18 @@ bool ContainsIdrNal(const uint8_t* data, size_t size) {40
return false;40
return false;41
}41
}42
42
43
// Hardware encoders (AMD's amdh264enc64) copy the NV12 input in 16-row and44
// 16-column blocks and read past a buffer sized to the exact frame.45
constexpr uint32_t kEncoderBlock = 16;46
47
uint32_t EvenSize(int size) {48
return size > 0 ? static_cast<uint32_t>(size) & ~1u : 0;49
}50
51
uint32_t AlignedSize(uint32_t size) {52
return (size + kEncoderBlock - 1) / kEncoderBlock * kEncoderBlock;53
}54
43
} // namespace55
} // namespace44
56
45
MFH264EncoderImpl::MFH264EncoderImpl(const Environment& env,57
MFH264EncoderImpl::MFH264EncoderImpl(const Environment& env,@@ -257,8 +269,8 @@ int32_t MFH264EncoderImpl::InitEncode(const VideoCodec* inst,257
}269
}258
mf_started_ = true;270
mf_started_ = true;259
271
260
width_ = inst->width;272
width_ = EvenSize(inst->width);261
height_ = inst->height;273
height_ = EvenSize(inst->height);262
max_fps_ = std::max<uint32_t>(inst->maxFramerate, 1);274
max_fps_ = std::max<uint32_t>(inst->maxFramerate, 1);263
target_bps_ = std::max<uint32_t>(inst->startBitrate * 1000, 150000);275
target_bps_ = std::max<uint32_t>(inst->startBitrate * 1000, 150000);264
frame_index_ = 0;276
frame_index_ = 0;@@ -556,11 +568,17 @@ int32_t MFH264EncoderImpl::Encode(556
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;568
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;557
}569
}558
570
559
if (static_cast<uint32_t>(i420->width()) != width_ ||571
const uint32_t frame_width = EvenSize(i420->width());560
static_cast<uint32_t>(i420->height()) != height_) {572
const uint32_t frame_height = EvenSize(i420->height());573
if (frame_width < 2 || frame_height < 2) {574
return WEBRTC_VIDEO_CODEC_NO_OUTPUT;575
}576
if (frame_width != width_ || frame_height != height_) {561
// Resolution changed dynamically: re-init MFT or fall back cleanly.577
// Resolution changed dynamically: re-init MFT or fall back cleanly.562
width_ = static_cast<uint32_t>(i420->width());578
width_ = frame_width;563
height_ = static_cast<uint32_t>(i420->height());579
height_ = frame_height;580
RTC_LOG(LS_INFO) << "MFH264: input size changed to " << width_ << "x"581
<< height_;564
mft_.Reset();582
mft_.Reset();565
codec_api_.Reset();583
codec_api_.Reset();566
event_gen_.Reset();584
event_gen_.Reset();@@ -583,9 +601,11 @@ int32_t MFH264EncoderImpl::Encode(583
}601
}584
602
585
const uint32_t nv12_size = width_ * height_ * 3 / 2;603
const uint32_t nv12_size = width_ * height_ * 3 / 2;604
const uint32_t padded_size =605
AlignedSize(width_) * AlignedSize(height_) * 3 / 2;586
ComPtr<IMFMediaBuffer> in_buffer;606
ComPtr<IMFMediaBuffer> in_buffer;587
ComPtr<IMFSample> in_sample;607
ComPtr<IMFSample> in_sample;588
if (FAILED(MFCreateMemoryBuffer(nv12_size, &in_buffer)) ||608
if (FAILED(MFCreateMemoryBuffer(padded_size, &in_buffer)) ||589
FAILED(MFCreateSample(&in_sample))) {609
FAILED(MFCreateSample(&in_sample))) {590
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;610
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;591
}611
}@@ -594,6 +614,7 @@ int32_t MFH264EncoderImpl::Encode(594
if (FAILED(in_buffer->Lock(&dst, nullptr, nullptr)) || !dst) {614
if (FAILED(in_buffer->Lock(&dst, nullptr, nullptr)) || !dst) {595
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;615
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;596
}616
}617
std::memset(dst + nv12_size, 0, padded_size - nv12_size);597
uint8_t* dst_y = dst;618
uint8_t* dst_y = dst;598
uint8_t* dst_uv = dst + width_ * height_;619
uint8_t* dst_uv = dst + width_ * height_;599
libyuv::I420ToNV12(i420->DataY(), i420->StrideY(), i420->DataU(),620
libyuv::I420ToNV12(i420->DataY(), i420->StrideY(), i420->DataU(),