PR #20973 opened by Steven Xiao (younengxiao)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20973
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20973.patch

This code submission is purposed to support the **motion estimation precision 
mode** for the D3D12 video encoder.  
By default, the D3D12 video encoder uses **MAXIMUM**, which means no 
restriction—it uses the highest precision supported by the driver.

Maximum precision increases computational load and latency. In certain 
scenarios (such as real-time encoding or low-power devices), applications may 
want to reduce precision to improve speed or reduce power consumption. This 
requires the encoder to support user-defined motion estimation precision modes.

***

**`D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE`** defines several 
precision modes:

*   **MAXIMUM**: No restriction, uses the maximum precision supported by the 
driver.
*   **FULL\_PIXEL**: Allows only full-pixel precision.
*   **HALF\_PIXEL**: Allows half-pixel precision.
*   **QUARTER\_PIXEL**: Allows quarter-pixel precision.
*   **EIGHTH\_PIXEL**: Allows eighth-pixel precision (introduced in Windows 11).

The flag 
**`D3D12_VIDEO_ENCODER_SUPPORT_FLAG_MOTION_ESTIMATION_PRECISION_MODE_LIMIT_AVAILABLE`**
 in **`D3D12_VIDEO_ENCODER_SUPPORT_FLAGS`** indicates whether the video encoder 
supports limiting the motion estimation precision mode under the current 
configuration.




From 7589b78b1e08f03bf9746fb6482f54345f274b6e Mon Sep 17 00:00:00 2001
From: stevxiao <[email protected]>
Date: Wed, 19 Nov 2025 13:14:17 -0500
Subject: [PATCH] support motion estimation precision mode source version 1

---
 libavcodec/d3d12va_encode.c      |  2 +-
 libavcodec/d3d12va_encode.h      | 21 ++++++++++++++++++++-
 libavcodec/d3d12va_encode_h264.c | 11 +++++++++++
 libavcodec/d3d12va_encode_hevc.c | 11 +++++++++++
 4 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/libavcodec/d3d12va_encode.c b/libavcodec/d3d12va_encode.c
index aa8a5982be..0e1e2de2f0 100644
--- a/libavcodec/d3d12va_encode.c
+++ b/libavcodec/d3d12va_encode.c
@@ -1205,7 +1205,7 @@ static int d3d12va_create_encoder(AVCodecContext *avctx)
         .EncodeProfile                = ctx->profile->d3d12_profile,
         .InputFormat                  = frames_hwctx->format,
         .CodecConfiguration           = ctx->codec_conf,
-        .MaxMotionEstimationPrecision = 
D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE_MAXIMUM,
+        .MaxMotionEstimationPrecision = 
(D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE)ctx->me_precision,
     };
 
     hr = ID3D12VideoDevice3_CreateVideoEncoder(ctx->video_device3, &desc, 
&IID_ID3D12VideoEncoder,
diff --git a/libavcodec/d3d12va_encode.h b/libavcodec/d3d12va_encode.h
index 5bd1eedb7f..24fc3f9435 100644
--- a/libavcodec/d3d12va_encode.h
+++ b/libavcodec/d3d12va_encode.h
@@ -156,6 +156,11 @@ typedef struct D3D12VAEncodeContext {
      */
     int max_frame_size;
 
+    /**
+     * Motion estimation precision mode
+     */
+    int me_precision;
+
     /**
      * Explicitly set RC mode (otherwise attempt to pick from
      * available modes).
@@ -338,7 +343,21 @@ int ff_d3d12va_encode_close(AVCodecContext *avctx);
     { "max_frame_size", \
       "Maximum frame size (in bytes)",\
       OFFSET(common.max_frame_size), AV_OPT_TYPE_INT, \
-      { .i64 = 0 }, 0, INT_MAX / 8, FLAGS }
+      { .i64 = 0 }, 0, INT_MAX / 8, FLAGS }, \
+    { "me_precision", \
+      "Motion estimation precision mode",\
+      OFFSET(common.me_precision), AV_OPT_TYPE_INT, \
+      { .i64 = 0 }, 0, 4, FLAGS, .unit = "me_precision" }, \
+    { "maximum", "Maximum (best quality, slowest)", 0, AV_OPT_TYPE_CONST, \
+      { .i64 = 0 }, 0, 0, FLAGS, .unit = "me_precision" }, \
+    { "full_pixel", "Full pixel precision", 0, AV_OPT_TYPE_CONST, \
+      { .i64 = 1 }, 0, 0, FLAGS, .unit = "me_precision" }, \
+    { "half_pixel", "Half pixel precision", 0, AV_OPT_TYPE_CONST, \
+      { .i64 = 2 }, 0, 0, FLAGS, .unit = "me_precision" }, \
+    { "quarter_pixel", "Quarter pixel precision", 0, AV_OPT_TYPE_CONST, \
+      { .i64 = 3 }, 0, 0, FLAGS, .unit = "me_precision" }, \
+    { "eighth_pixel", "Eighth pixel precision", 0, AV_OPT_TYPE_CONST, \
+      { .i64 = 4 }, 0, 0, FLAGS, .unit = "me_precision" }
 
 #define D3D12VA_ENCODE_RC_MODE(name, desc) \
     { #name, desc, 0, AV_OPT_TYPE_CONST, { .i64 = RC_MODE_ ## name }, \
diff --git a/libavcodec/d3d12va_encode_h264.c b/libavcodec/d3d12va_encode_h264.c
index 967544ea24..7c815b4fe1 100644
--- a/libavcodec/d3d12va_encode_h264.c
+++ b/libavcodec/d3d12va_encode_h264.c
@@ -211,6 +211,17 @@ static int 
d3d12va_encode_h264_init_sequence_params(AVCodecContext *avctx)
         av_log(avctx, AV_LOG_DEBUG, "D3D12 video encode on this device uses 
texture array mode.\n");
     }
 
+    if (ctx->me_precision != 0) {
+        if (support.SupportFlags & 
D3D12_VIDEO_ENCODER_SUPPORT_FLAG_MOTION_ESTIMATION_PRECISION_MODE_LIMIT_AVAILABLE)
 {
+            av_log(avctx, AV_LOG_VERBOSE, "Hardware supports motion estimation 
"
+                "precision mode limits.\n");
+        } else {
+            ctx->me_precision = 0;
+            av_log(avctx, AV_LOG_WARNING, "Hardware does not support motion 
estimation "
+                "precision mode limits. The me_precision parameter may be 
ignored.\n");
+        }
+    }
+
     desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format);
     av_assert0(desc);
 
diff --git a/libavcodec/d3d12va_encode_hevc.c b/libavcodec/d3d12va_encode_hevc.c
index 01e5b4cb4c..9ca733fbd5 100644
--- a/libavcodec/d3d12va_encode_hevc.c
+++ b/libavcodec/d3d12va_encode_hevc.c
@@ -283,6 +283,17 @@ static int 
d3d12va_encode_hevc_init_sequence_params(AVCodecContext *avctx)
         av_log(avctx, AV_LOG_DEBUG, "D3D12 video encode on this device uses 
texture array mode.\n");
     }
 
+    if (ctx->me_precision != 0) {
+        if (support.SupportFlags & 
D3D12_VIDEO_ENCODER_SUPPORT_FLAG_MOTION_ESTIMATION_PRECISION_MODE_LIMIT_AVAILABLE)
 {
+            av_log(avctx, AV_LOG_VERBOSE, "Hardware supports motion estimation 
"
+                "precision mode limits.\n");
+        } else {
+            ctx->me_precision = 0;
+            av_log(avctx, AV_LOG_WARNING, "Hardware does not support motion 
estimation "
+                "precision mode limits. The me_precision parameter may be 
ignored.\n");
+        }
+    }
+
     desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format);
     av_assert0(desc);
 
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to