Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Steven Xiao via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Steven Xiao <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PATCH] avcodec/d3d12va_encode: support motion estimation precision mode (PR #20973)
Date: Wed, 19 Nov 2025 18:40:00 -0000
Message-ID: <176357760064.59.9110328799271576369@2cb04c0e5124> (raw)

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 <steven.xiao@amd.com>
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 -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

                 reply	other threads:[~2025-11-19 18:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=176357760064.59.9110328799271576369@2cb04c0e5124 \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=code@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git