From: green-hand-shandong 
<[email protected]>

Add AV_PIX_FMT_ASCEND, AV_HWDEVICE_TYPE_ASCEND and the hwcontext_ascend
backend implementing device init (aclrt context + DVPP sys), a DVPP
buffer pool, frame allocation and hw<->sw transfers (hwupload/
hwdownload). Pool growth and transfers may run on foreign threads, so
the device context is bound before DVPP/ACL calls.

Also fix ascenddec bit-depth detection lost in refactoring: 10-bit
streams are now properly detected (HEVC Main 10/Rext, H.264 High 10)
and decoded with the 8-bit NV12 output path, since the 310P VDEC
rejects 10-bit semi-planar output formats with ILLEGAL_PARAM.

CLI validation on Atlas 300I-Duo (CANN 9.1.0):
- h264_ascend -output_mode hw ... hwdownload,format=nv12: MD5 identical
  to software decode
- sw -> hwupload -> hwdownload roundtrip: MD5 identical
- 10-bit HEVC Main10 -> NV12: PSNR 59.7dB vs software conversion
- full decode matrix: 10/10 PASS

Signed-off-by: green-hand-shandong 
<[email protected]>
---
 configure                      |   4 +-
 libavcodec/ascenddec.c         | 121 +++++++++-
 libavutil/Makefile             |   2 +
 libavutil/hwcontext.c          |   4 +
 libavutil/hwcontext.h          |   2 +
 libavutil/hwcontext_ascend.c   | 423 +++++++++++++++++++++++++++++++++
 libavutil/hwcontext_ascend.h   |  55 +++++
 libavutil/hwcontext_internal.h |   1 +
 libavutil/pixdesc.c            |   4 +
 libavutil/pixfmt.h             |   7 +
 10 files changed, 619 insertions(+), 4 deletions(-)
 create mode 100644 libavutil/hwcontext_ascend.c
 create mode 100644 libavutil/hwcontext_ascend.h

diff --git a/configure b/configure
index 6cc274d407..9b059d92f7 100755
--- a/configure
+++ b/configure
@@ -2130,6 +2130,7 @@ EXTERNAL_LIBRARY_LIST="
     libzimg
     libzmq
     libzvbi
+    ascend
     lv2
     mediacodec
     ohcodec
@@ -7225,7 +7226,8 @@ enabled jni               && { [ $target_os = "android" ] 
&& check_headers jni.h
 enabled ladspa            && require_headers "ladspa.h dlfcn.h"
 enabled lcms2             && require_pkg_config lcms2 "lcms2 >= 2.13" lcms2.h 
cmsCreateContext
 enabled libaom            && require_pkg_config libaom "aom >= 2.0.0" 
aom/aom_codec.h aom_codec_version
-enabled libascendcl       && require libascendcl "acl/acl.h 
acl/dvpp/hi_dvpp.h" hi_mpi_sys_init "-lascendcl -lacl_dvpp_mpi"
+enabled libascendcl       && require libascendcl "acl/acl.h 
acl/dvpp/hi_dvpp.h" hi_mpi_sys_init "-lascendcl -lacl_dvpp_mpi" &&
+                             enable ascend
 enabled liboapv           && require_pkg_config liboapv "oapv >= 0.2.0.0" 
"oapv/oapv.h" oapve_encode
 enabled libaribb24        && { check_pkg_config libaribb24 "aribb24 > 1.0.3" 
"aribb24/aribb24.h" arib_instance_new ||
                                { enabled gpl && require_pkg_config libaribb24 
aribb24 "aribb24/aribb24.h" arib_instance_new; } ||
diff --git a/libavcodec/ascenddec.c b/libavcodec/ascenddec.c
index 425844e223..4b56513ef9 100644
--- a/libavcodec/ascenddec.c
+++ b/libavcodec/ascenddec.c
@@ -34,6 +34,9 @@
 #include "avcodec.h"
 #include "codec_internal.h"
 #include "decode.h"
+#include "hwaccel_internal.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_ascend.h"
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/error.h"
@@ -93,6 +96,12 @@ typedef struct AscendDecContext {
     int eos_sent;
     int drain_fail_cnt;
     AVPacket *pkt;
+
+    /* output_mode AVOption: 0 = sw copy (default), 1 = hw frames */
+    int output_mode;
+    AVBufferRef *hw_device_ref;
+    AVBufferRef *hwframe_ref;
+    AVHWFramesContext *hw_frames;
 } AscendDecContext;
 
 static int ascend_global_init(void)
@@ -179,9 +188,14 @@ static av_cold int ascend_channel_create(AVCodecContext 
*avctx)
     ctx->coded_height      = avctx->coded_height;
     /* 10-bit streams (H.264 High 10 / HEVC Main 10) decode to 2-byte-per-
      * sample semi-planar output (P010 layout); strides are in bytes. */
-    ctx->bytes_per_sample  = ctx->bit_depth > 8 ? 2 : 1;
-    ctx->out_width_stride  = FFALIGN(ctx->coded_width * ctx->bytes_per_sample,
-                                     ctx->bytes_per_sample > 1 ? 32 : 16);
+    /* 10-bit input streams (HEVC Main 10 / H.264 High 10) are decoded by
+     * the 310P VDEC to 8-bit NV12: the 10-bit semi-planar output formats
+     * are rejected by hi_mpi_vdec_send_stream with ILLEGAL_PARAM on this
+     * hardware/CANN combination. 10-bit passthrough output would need a
+     * different engine path; for now always output NV12. */
+    ctx->bit_depth         = 8;
+    ctx->bytes_per_sample  = 1;
+    ctx->out_width_stride  = FFALIGN(ctx->coded_width, 16);
     ctx->out_height_stride = FFALIGN(ctx->coded_height, 16);
     ctx->out_buffer_size   = (size_t)ctx->out_width_stride *
                              ctx->out_height_stride * 3 / 2;
@@ -385,6 +399,60 @@ static int ascend_send_stream(AscendDecContext *ctx, 
uint8_t *buf, int len,
 
 /* Fetch one decoded frame into AVFrame.
  * Returns 0 on success, AVERROR(EAGAIN) if nothing is ready. */
+/* AVBufferRef free callback for exported DVPP output buffers. */
+static void ascend_hw_frame_free(void *opaque, uint8_t *data)
+{
+    AVCodecContext *avctx = opaque;
+    AscendDecContext *ctx = avctx->priv_data;
+
+    ascend_return_out_buffer(ctx, data);
+}
+
+/* Set up (or reuse) an ASCEND hwframes context for hw output mode. */
+static int ascend_hw_frames_setup(AVCodecContext *avctx)
+{
+    AscendDecContext *ctx = avctx->priv_data;
+    AVHWFramesContext *fctx;
+    int ret;
+
+    if (ctx->hw_frames)
+        return 0;
+
+    if (!ctx->hw_device_ref) {
+        if (avctx->hw_device_ctx) {
+            ctx->hw_device_ref = av_buffer_ref(avctx->hw_device_ctx);
+        } else {
+            ret = av_hwdevice_ctx_create(&ctx->hw_device_ref,
+                                         AV_HWDEVICE_TYPE_ASCEND,
+                                         NULL, NULL, 0);
+            if (ret < 0) {
+                av_log(avctx, AV_LOG_ERROR, "ascenddec: hwdevice create 
failed\n");
+                return ret;
+            }
+        }
+        if (!ctx->hw_device_ref)
+            return AVERROR(ENOMEM);
+    }
+
+    ctx->hwframe_ref = av_hwframe_ctx_alloc(ctx->hw_device_ref);
+    if (!ctx->hwframe_ref)
+        return AVERROR(ENOMEM);
+    fctx = (AVHWFramesContext *)ctx->hwframe_ref->data;
+    fctx->format    = AV_PIX_FMT_ASCEND;
+    fctx->sw_format = ctx->bit_depth > 8 ? AV_PIX_FMT_P010LE : AV_PIX_FMT_NV12;
+    fctx->width     = FFALIGN(avctx->coded_width, 16);
+    fctx->height    = FFALIGN(avctx->coded_height, 16);
+    fctx->initial_pool_size = 8;
+    ret = av_hwframe_ctx_init(ctx->hwframe_ref);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "ascenddec: hwframes init failed: %d\n", 
ret);
+        av_buffer_unref(&ctx->hwframe_ref);
+        return ret;
+    }
+    ctx->hw_frames = fctx;
+    return 0;
+}
+
 static int ascend_get_frame(AVCodecContext *avctx, AVFrame *frame, int 
timeout_ms)
 {
     AscendDecContext *ctx = avctx->priv_data;
@@ -433,6 +501,40 @@ static int ascend_get_frame(AVCodecContext *avctx, AVFrame 
*frame, int timeout_m
         return ret;
     }
 
+    if (ctx->output_mode == 1) {
+        /* Export the DVPP buffer as a hardware frame; the buffer ref owns
+         * it until release, at which point it returns to the pool. */
+        frame->buf[0] = av_buffer_create((uint8_t 
*)(uintptr_t)fi.v_frame.virt_addr[0],
+                                         frame_size, ascend_hw_frame_free, 
avctx,
+                                         0);
+        if (!frame->buf[0]) {
+            ascend_return_out_buffer(ctx, fi.v_frame.virt_addr[0]);
+            hi_mpi_vdec_release_frame(ctx->chn, &fi);
+            return AVERROR(ENOMEM);
+        }
+        /* Prevent the pool return below; ownership moved to the AVFrame. */
+        fi.v_frame.virt_addr[0] = NULL;
+
+        frame->data[0]     = frame->buf[0]->data;
+        frame->data[1]     = frame->data[0] + (size_t)stride *
+                             ctx->out_height_stride;
+        frame->data[3]     = (uint8_t *)ctx->hwframe_ref;
+        frame->linesize[0] = stride;
+        frame->linesize[1] = stride;
+        frame->width       = fi.v_frame.width;
+        frame->height      = fi.v_frame.height;
+        frame->format        = AV_PIX_FMT_ASCEND;
+        frame->hw_frames_ctx = av_buffer_ref(ctx->hwframe_ref);
+        if (!frame->hw_frames_ctx) {
+            av_buffer_unref(&frame->buf[0]);
+            hi_mpi_vdec_release_frame(ctx->chn, &fi);
+            return AVERROR(ENOMEM);
+        }
+        frame->pts         = pts;
+        hi_mpi_vdec_release_frame(ctx->chn, &fi);
+        return 0;
+    }
+
     frame->buf[0] = av_buffer_alloc(frame_size);
     if (!frame->buf[0]) {
         ascend_return_out_buffer(ctx, fi.v_frame.virt_addr[0]);
@@ -484,6 +586,11 @@ static int ascend_receive_frame(AVCodecContext *avctx, 
AVFrame *frame)
         ret = ascend_channel_create(avctx);
         if (ret < 0)
             return ret;
+        if (ctx->output_mode == 1) {
+            ret = ascend_hw_frames_setup(avctx);
+            if (ret < 0)
+                return ret;
+        }
     }
 
     for (;;) {
@@ -585,6 +692,8 @@ static av_cold int ascend_decode_close(AVCodecContext 
*avctx)
         ctx->global_inited = 0;
     }
 
+    av_buffer_unref(&ctx->hwframe_ref);
+    av_buffer_unref(&ctx->hw_device_ref);
     av_packet_free(&ctx->pkt);
     return 0;
 }
@@ -612,6 +721,12 @@ static void ascend_decode_flush(AVCodecContext *avctx)
 static const AVOption ascend_decode_options[] = {
     { "device_id", "Ascend NPU device id", OFFSET(device_id),
       AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 31, VD },
+    { "output_mode", "output frame location", OFFSET(output_mode),
+      AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD, .unit = "output_mode" },
+    { "sw", "copy decoded frames to host memory", 0,
+      AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, VD, .unit = "output_mode" },
+    { "hw", "keep decoded frames in NPU device memory", 0,
+      AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, VD, .unit = "output_mode" },
     { NULL }
 };
 
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 1f91fd5d17..7b9b5c0a03 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -51,6 +51,7 @@ HEADERS = adler32.h                                           
          \
           hwcontext_amf.h                                               \
           hwcontext_qsv.h                                               \
           hwcontext_mediacodec.h                                        \
+          hwcontext_ascend.h                                            \
           hwcontext_opencl.h                                            \
           hwcontext_oh.h                                                \
           hwcontext_vaapi.h                                             \
@@ -215,6 +216,7 @@ OBJS-$(CONFIG_AMF)                      += hwcontext_amf.o
 OBJS-$(CONFIG_LIBDRM)                   += hwcontext_drm.o
 OBJS-$(CONFIG_MACOS_KPERF)              += macos_kperf.o
 OBJS-$(CONFIG_MEDIACODEC)               += hwcontext_mediacodec.o
+OBJS-$(CONFIG_ASCEND)                   += hwcontext_ascend.o
 OBJS-$(CONFIG_OHCODEC)                  += hwcontext_oh.o
 OBJS-$(CONFIG_OPENCL)                   += hwcontext_opencl.o
 OBJS-$(CONFIG_QSV)                      += hwcontext_qsv.o
diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c
index 297cc5dd85..080388769b 100644
--- a/libavutil/hwcontext.c
+++ b/libavutil/hwcontext.c
@@ -31,6 +31,9 @@
 #include "pixfmt.h"
 
 static const HWContextType * const hw_table[] = {
+#if CONFIG_ASCEND
+    &ff_hwcontext_type_ascend,
+#endif
 #if CONFIG_CUDA
     &ff_hwcontext_type_cuda,
 #endif
@@ -91,6 +94,7 @@ static const char *const hw_type_names[] = {
     [AV_HWDEVICE_TYPE_VULKAN] = "vulkan",
     [AV_HWDEVICE_TYPE_AMF] = "amf",
     [AV_HWDEVICE_TYPE_OHCODEC] = "ohcodec",
+    [AV_HWDEVICE_TYPE_ASCEND] = "ascend",
 };
 
 typedef struct FFHWDeviceContext {
diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h
index 8e414f4863..4a044a1f09 100644
--- a/libavutil/hwcontext.h
+++ b/libavutil/hwcontext.h
@@ -41,6 +41,8 @@ enum AVHWDeviceType {
     AV_HWDEVICE_TYPE_AMF,
     /* OpenHarmony Codec device */
     AV_HWDEVICE_TYPE_OHCODEC,
+    /* Ascend NPU (CANN DVPP) device */
+    AV_HWDEVICE_TYPE_ASCEND,
 };
 
 /**
diff --git a/libavutil/hwcontext_ascend.c b/libavutil/hwcontext_ascend.c
new file mode 100644
index 0000000000..0ef844b9c5
--- /dev/null
+++ b/libavutil/hwcontext_ascend.c
@@ -0,0 +1,423 @@
+/*
+ * Ascend NPU (CANN DVPP) hardware device context.
+ *
+ * Copyright (c) 2026 green-hand-shandong
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "config.h"
+
+#include <pthread.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <acl/acl.h>
+#include <acl/dvpp/hi_dvpp.h>
+
+#include "buffer.h"
+#include "common.h"
+#include "hwcontext.h"
+#include "hwcontext_ascend.h"
+#include "hwcontext_internal.h"
+#include "imgutils.h"
+#include "log.h"
+#include "mem.h"
+#include "pixdesc.h"
+#include "pixfmt.h"
+
+/* aclInit is a process singleton shared with libavcodec. */
+static pthread_mutex_t ascend_ref_lock = PTHREAD_MUTEX_INITIALIZER;
+static int ascend_ref_count;
+
+static void ascend_global_ref(void)
+{
+    pthread_mutex_lock(&ascend_ref_lock);
+    if (ascend_ref_count++ == 0)
+        /* Fails benignly when ACL is already initialized (e.g. torch_npu);
+         * subsequent ACL calls surface real errors. */
+        aclInit(NULL);
+    pthread_mutex_unlock(&ascend_ref_lock);
+}
+
+static void ascend_global_unref(void)
+{
+    pthread_mutex_lock(&ascend_ref_lock);
+    if (--ascend_ref_count == 0)
+        aclFinalize();
+    pthread_mutex_unlock(&ascend_ref_lock);
+}
+
+static int ascend_bytes_per_sample(enum AVPixelFormat sw_fmt)
+{
+    return sw_fmt == AV_PIX_FMT_P010LE ? 2 : 1;
+}
+
+/* Stride rules of the 310P DVPP: the width stride counts bytes and is
+ * 16-aligned for 8-bit, 32-aligned for 10-bit; height is 16-aligned. */
+static int ascend_width_stride(enum AVPixelFormat sw_fmt, int width)
+{
+    const int bps = ascend_bytes_per_sample(sw_fmt);
+
+    return FFALIGN(width * bps, bps > 1 ? 32 : 16);
+}
+
+static int ascend_height_stride(enum AVPixelFormat sw_fmt, int height)
+{
+    return FFALIGN(height, 16);
+}
+
+static size_t ascend_frame_size(enum AVPixelFormat sw_fmt, int width, int 
height)
+{
+    return (size_t)ascend_width_stride(sw_fmt, width) *
+           ascend_height_stride(sw_fmt, height) * 3 / 2;
+}
+
+/* Per-device state; device_init runs once per AVHWDeviceContext. */
+typedef struct AscendDeviceContext {
+    AVAscendDeviceContext p;
+
+    aclrtContext context;
+    int context_created;
+    int dvpp_inited;
+    int global_inited;
+} AscendDeviceContext;
+
+static void ascend_device_uninit(AVHWDeviceContext *hwdev)
+{
+    AscendDeviceContext *dev = hwdev->hwctx;
+
+    if (dev->dvpp_inited) {
+        hi_mpi_sys_exit();
+        dev->dvpp_inited = 0;
+    }
+    if (dev->context_created) {
+        aclrtDestroyContext(dev->context);
+        dev->context_created = 0;
+    }
+    aclrtResetDevice(dev->p.device_id >= 0 ? dev->p.device_id : 0);
+    if (dev->global_inited) {
+        ascend_global_unref();
+        dev->global_inited = 0;
+    }
+}
+
+static int ascend_device_init(AVHWDeviceContext *hwdev)
+{
+    AscendDeviceContext *dev = hwdev->hwctx;
+    int ret;
+
+    if (dev->global_inited)
+        return 0;
+    if (dev->p.device_id < 0)
+        dev->p.device_id = 0;
+
+    ascend_global_ref();
+    dev->global_inited = 1;
+
+    ret = aclrtSetDevice(dev->p.device_id);
+    if (ret != 0) {
+        av_log(hwdev, AV_LOG_ERROR, "ascend: aclrtSetDevice(%d) failed: %d\n",
+               dev->p.device_id, ret);
+        goto fail;
+    }
+    ret = aclrtCreateContext(&dev->context, dev->p.device_id);
+    if (ret != 0) {
+        av_log(hwdev, AV_LOG_ERROR, "ascend: aclrtCreateContext failed: %d\n", 
ret);
+        aclrtResetDevice(dev->p.device_id);
+        goto fail;
+    }
+    dev->context_created = 1;
+    aclrtSetCurrentContext(dev->context);
+
+    ret = hi_mpi_sys_init();
+    if (ret != 0) {
+        av_log(hwdev, AV_LOG_ERROR, "ascend: hi_mpi_sys_init failed: 0x%x\n", 
ret);
+        goto fail;
+    }
+    dev->dvpp_inited = 1;
+
+    av_log(hwdev, AV_LOG_VERBOSE, "ascend: device %d initialized\n",
+           dev->p.device_id);
+    return 0;
+
+fail:
+    ascend_device_uninit(hwdev);
+    return AVERROR_EXTERNAL;
+}
+
+static int ascend_device_create(AVHWDeviceContext *hwdev, const char *device,
+                                AVDictionary *opts, int flags)
+{
+    AscendDeviceContext *dev = hwdev->hwctx;
+
+    dev->p.device_id = 0;
+    if (device && device[0]) {
+        char *end;
+        long id = strtol(device, &end, 10);
+        if (*end != '\0' || id < 0 || id > 31) {
+            av_log(hwdev, AV_LOG_ERROR, "ascend: invalid device \"%s\"\n", 
device);
+            return AVERROR(EINVAL);
+        }
+        dev->p.device_id = id;
+    }
+    return 0;
+}
+
+static int ascend_frames_get_constraints(AVHWDeviceContext *hwdev,
+                                         const void *hwconfig,
+                                         AVHWFramesConstraints *constraints)
+{
+    constraints->valid_sw_formats = av_malloc_array(3, sizeof(enum 
AVPixelFormat));
+    if (!constraints->valid_sw_formats)
+        return AVERROR(ENOMEM);
+    constraints->valid_sw_formats[0] = AV_PIX_FMT_NV12;
+    constraints->valid_sw_formats[1] = AV_PIX_FMT_P010LE;
+    constraints->valid_sw_formats[2] = AV_PIX_FMT_NONE;
+
+    constraints->valid_hw_formats = av_malloc_array(2, sizeof(enum 
AVPixelFormat));
+    if (!constraints->valid_hw_formats) {
+        av_freep(&constraints->valid_sw_formats);
+        return AVERROR(ENOMEM);
+    }
+    constraints->valid_hw_formats[0] = AV_PIX_FMT_ASCEND;
+    constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
+
+    constraints->min_width  = 128;
+    constraints->min_height = 128;
+    constraints->max_width  = 4096;
+    constraints->max_height = 4096;
+    return 0;
+}
+
+/* A pool buffer carries the DVPP device pointer in a small host wrapper
+ * so the free callback can hand it back to the DVPP allocator. */
+typedef struct AscendSurface {
+    void *dev_ptr;
+} AscendSurface;
+
+
+static void ascend_surface_free(void *opaque, uint8_t *data)
+{
+    AscendSurface *surf = (AscendSurface *)data;
+
+    if (surf->dev_ptr)
+        hi_mpi_dvpp_free(surf->dev_ptr);
+    av_free(surf);
+}
+
+static AVBufferRef *ascend_pool_alloc(void *opaque, size_t size)
+{
+    AVHWFramesContext *hwfc = opaque;
+    AscendDeviceContext *dev = hwfc->device_ctx->hwctx;
+    AscendSurface *surf;
+    AVBufferRef *ref;
+
+    /* Pool growth can happen on foreign threads; bind the device context
+     * for the DVPP allocator. */
+    aclrtSetCurrentContext(dev->context);
+
+    surf = av_mallocz(sizeof(*surf));
+    if (!surf)
+        return NULL;
+
+    if (hi_mpi_dvpp_malloc(dev->p.device_id, &surf->dev_ptr, size) != 0) {
+        av_log(hwfc, AV_LOG_ERROR, "ascend: DVPP buffer alloc (%zu) failed\n", 
size);
+        av_free(surf);
+        return NULL;
+    }
+
+    ref = av_buffer_create((uint8_t *)surf, sizeof(*surf),
+                           ascend_surface_free, NULL, 0);
+    if (!ref) {
+        hi_mpi_dvpp_free(surf->dev_ptr);
+        av_free(surf);
+        return NULL;
+    }
+    return ref;
+}
+
+static int ascend_frames_init(AVHWFramesContext *hwfc)
+{
+    FFHWFramesContext *ctxi = ffhwframesctx(hwfc);
+    AscendDeviceContext *dev = hwfc->device_ctx->hwctx;
+    AVAscendFramesContext *fctx = hwfc->hwctx;
+
+    switch (hwfc->sw_format) {
+    case AV_PIX_FMT_NV12:
+    case AV_PIX_FMT_P010LE:
+        break;
+    default:
+        av_log(hwfc, AV_LOG_ERROR, "ascend: unsupported sw format %s\n",
+               av_get_pix_fmt_name(hwfc->sw_format));
+        return AVERROR(ENOSYS);
+    }
+
+    if (fctx)
+        fctx->device_id = dev->p.device_id;
+
+    ctxi->pool_internal = av_buffer_pool_init2(
+        ascend_frame_size(hwfc->sw_format, hwfc->width, hwfc->height),
+        hwfc, ascend_pool_alloc, NULL);
+    if (!ctxi->pool_internal)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static void ascend_frames_uninit(AVHWFramesContext *hwfc)
+{
+    FFHWFramesContext *ctxi = ffhwframesctx(hwfc);
+
+    av_buffer_pool_uninit(&ctxi->pool_internal);
+}
+
+static int ascend_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
+{
+    AscendSurface *surf;
+    int wstride = ascend_width_stride(hwfc->sw_format, hwfc->width);
+    int hstride = ascend_height_stride(hwfc->sw_format, hwfc->height);
+
+    frame->buf[0] = av_buffer_pool_get(hwfc->pool);
+    if (!frame->buf[0])
+        return AVERROR(ENOMEM);
+
+    surf          = (AscendSurface *)frame->buf[0]->data;
+    frame->data[0] = surf->dev_ptr;
+    frame->data[1] = (uint8_t *)surf->dev_ptr + (size_t)wstride * hstride;
+    frame->data[3] = (uint8_t *)surf;
+    frame->format  = AV_PIX_FMT_ASCEND;
+    frame->width   = hwfc->width;
+    frame->height  = hwfc->height;
+    frame->linesize[0] = wstride;
+    frame->linesize[1] = wstride;
+
+    return 0;
+}
+
+static int ascend_transfer_get_formats(AVHWFramesContext *hwfc,
+                                       enum AVHWFrameTransferDirection dir,
+                                       enum AVPixelFormat **formats)
+{
+    enum AVPixelFormat *fmts;
+
+    fmts = av_malloc_array(2, sizeof(*fmts));
+    if (!fmts)
+        return AVERROR(ENOMEM);
+    fmts[0] = hwfc->sw_format;
+    fmts[1] = AV_PIX_FMT_NONE;
+    *formats = fmts;
+    return 0;
+}
+
+static int ascend_transfer_data_from(AVHWFramesContext *hwfc,
+                                     AVFrame *dst, const AVFrame *src)
+{
+    const int bps = ascend_bytes_per_sample(hwfc->sw_format);
+    const int wstride = ascend_width_stride(hwfc->sw_format, src->width);
+    const int hstride = ascend_height_stride(hwfc->sw_format, src->height);
+    const uint8_t *dev_base = (const uint8_t *)src->data[0];
+    const int rows[2] = { src->height, (src->height + 1) / 2 };
+    const int bw = src->width * bps;
+    int ret = 0;
+
+    if (!dev_base)
+        return AVERROR(EINVAL);
+
+    /* aclrtMemcpy requires the device context of the calling thread. */
+    {
+        AscendDeviceContext *dev = hwfc->device_ctx->hwctx;
+        int arc = aclrtSetCurrentContext(dev->context);
+        if (arc != 0)
+            av_log(hwfc, AV_LOG_WARNING, "ascend: SetCurrentContext %d\n", 
arc);
+    }
+
+    for (int p = 0; p < 2 && !ret; p++) {
+        const uint8_t *src_p = dev_base + (p == 0 ? 0 : (size_t)wstride * 
hstride);
+        uint8_t *dst_p = p == 0 ? dst->data[0] : dst->data[1];
+        const int dl = p == 0 ? dst->linesize[0] : dst->linesize[1];
+
+        for (int y = 0; y < rows[p]; y++)
+            if (aclrtMemcpy(dst_p + (size_t)y * dl, bw,
+                            src_p + (size_t)y * wstride, bw,
+                            ACL_MEMCPY_DEVICE_TO_HOST) != 0) {
+                ret = AVERROR_EXTERNAL;
+                break;
+            }
+    }
+    return ret;
+}
+
+static int ascend_transfer_data_to(AVHWFramesContext *hwfc,
+                                   AVFrame *dst, const AVFrame *src)
+{
+    const int bps = ascend_bytes_per_sample(hwfc->sw_format);
+    const int wstride = ascend_width_stride(hwfc->sw_format, dst->width);
+    const int hstride = ascend_height_stride(hwfc->sw_format, dst->height);
+    uint8_t *dev_base = (uint8_t *)dst->data[0];
+    const int rows[2] = { dst->height, (dst->height + 1) / 2 };
+    const int bw = dst->width * bps;
+    int ret = 0;
+
+    if (!dev_base)
+        return AVERROR(EINVAL);
+
+    {
+        AscendDeviceContext *dev = hwfc->device_ctx->hwctx;
+        int arc = aclrtSetCurrentContext(dev->context);
+        if (arc != 0)
+            av_log(hwfc, AV_LOG_WARNING, "ascend: SetCurrentContext %d\n", 
arc);
+    }
+    for (int p = 0; p < 2 && !ret; p++) {
+        uint8_t *dst_p = dev_base + (p == 0 ? 0 : (size_t)wstride * hstride);
+        const uint8_t *src_p = p == 0 ? src->data[0] : src->data[1];
+        const int sl = p == 0 ? src->linesize[0] : src->linesize[1];
+
+        for (int y = 0; y < rows[p]; y++)
+            if (aclrtMemcpy(dst_p + (size_t)y * wstride, bw,
+                            src_p + (size_t)y * sl, bw,
+                            ACL_MEMCPY_HOST_TO_DEVICE) != 0) {
+                ret = AVERROR_EXTERNAL;
+                break;
+            }
+    }
+    return ret;
+}
+
+const HWContextType ff_hwcontext_type_ascend = {
+    .type                   = AV_HWDEVICE_TYPE_ASCEND,
+    .name                   = "ascend",
+
+    .device_hwctx_size      = sizeof(AscendDeviceContext),
+
+    .device_create          = &ascend_device_create,
+    .device_init            = &ascend_device_init,
+    .device_uninit          = &ascend_device_uninit,
+    .frames_get_constraints = &ascend_frames_get_constraints,
+    .frames_init            = &ascend_frames_init,
+    .frames_uninit          = &ascend_frames_uninit,
+    .frames_get_buffer      = &ascend_get_buffer,
+    .transfer_get_formats   = &ascend_transfer_get_formats,
+    .transfer_data_from     = &ascend_transfer_data_from,
+    .transfer_data_to       = &ascend_transfer_data_to,
+
+    .pix_fmts = (const enum AVPixelFormat[]) {
+        AV_PIX_FMT_ASCEND,
+        AV_PIX_FMT_NONE
+    },
+};
diff --git a/libavutil/hwcontext_ascend.h b/libavutil/hwcontext_ascend.h
new file mode 100644
index 0000000000..0fb6c2e53e
--- /dev/null
+++ b/libavutil/hwcontext_ascend.h
@@ -0,0 +1,55 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_HWCONTEXT_ASCEND_H
+#define AVUTIL_HWCONTEXT_ASCEND_H
+
+#include "pixfmt.h"
+
+/**
+ * @file
+ * An AVHWDeviceContext for Ascend NPU DVPP (CANN) device memory.
+ */
+
+/**
+ * Ascend-specific device context, allocated as AVHWDeviceContext.hwctx.
+ * The device id selects the NPU chip (as in npu-smi); contexts and the
+ * DVPP subsystem are initialized per process on first use.
+ */
+typedef struct AVAscendDeviceContext {
+    /**
+     * Ascend NPU device id (-1 = unspecified; defaults to 0 on use).
+     */
+    int device_id;
+} AVAscendDeviceContext;
+
+/**
+ * Ascend-specific frame pool context, allocated as AVHWFramesContext.hwctx.
+ * Surfaces are plain DVPP device buffers of frames->width x frames->height
+ * in the pool's sw_format; stride metadata lives in AVHWFramesContext
+ * (line sizes are derived from the width with CANN alignment rules).
+ */
+typedef struct AVAscendFramesContext {
+    /**
+     * Device the pool buffers were allocated on; inherited from the
+     * device context at pool init time.
+     */
+    int device_id;
+} AVAscendFramesContext;
+
+#endif /* AVUTIL_HWCONTEXT_ASCEND_H */
diff --git a/libavutil/hwcontext_internal.h b/libavutil/hwcontext_internal.h
index e100233e35..30f8acc83a 100644
--- a/libavutil/hwcontext_internal.h
+++ b/libavutil/hwcontext_internal.h
@@ -182,5 +182,6 @@ extern const HWContextType ff_hwcontext_type_mediacodec;
 extern const HWContextType ff_hwcontext_type_vulkan;
 extern const HWContextType ff_hwcontext_type_amf;
 extern const HWContextType ff_hwcontext_type_oh;
+extern const HWContextType ff_hwcontext_type_ascend;
 
 #endif /* AVUTIL_HWCONTEXT_INTERNAL_H */
diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
index c7cbfa8658..712c805b95 100644
--- a/libavutil/pixdesc.c
+++ b/libavutil/pixdesc.c
@@ -2294,6 +2294,10 @@ static const AVPixFmtDescriptor 
av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
         .name = "mediacodec",
         .flags = AV_PIX_FMT_FLAG_HWACCEL,
     },
+    [AV_PIX_FMT_ASCEND] = {
+        .name = "ascend",
+        .flags = AV_PIX_FMT_FLAG_HWACCEL,
+    },
     [AV_PIX_FMT_MMAL] = {
         .name = "mmal",
         .flags = AV_PIX_FMT_FLAG_HWACCEL,
diff --git a/libavutil/pixfmt.h b/libavutil/pixfmt.h
index b0bf9524c9..f883e57c12 100644
--- a/libavutil/pixfmt.h
+++ b/libavutil/pixfmt.h
@@ -315,6 +315,13 @@ enum AVPixelFormat {
 
     AV_PIX_FMT_MEDIACODEC, ///< hardware decoding through MediaCodec
 
+    /**
+     * Hardware surfaces for Ascend NPU DVPP (CANN).
+     * data[0] points to a DVPP device buffer (allocated via
+     * hi_mpi_dvpp_malloc), data[3] carries the pool AVBufferRef.
+     */
+    AV_PIX_FMT_ASCEND,
+
     AV_PIX_FMT_GRAY12BE,   ///<        Y        , 12bpp, big-endian
     AV_PIX_FMT_GRAY12LE,   ///<        Y        , 12bpp, little-endian
     AV_PIX_FMT_GRAY10BE,   ///<        Y        , 10bpp, big-endian
-- 
2.25.1


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

Reply via email to