From: green-hand-shandong <[email protected]>
The device id space is the runtime's logical numbering (inside a container it differs from the physical davinci node number), so auto-pick enumerates aclrtGetDeviceCount and takes the first logical id instead of scanning /dev/davinciN nodes. Decoder and encoder AVOption ranges extended to -1..31 with default -1 (auto), keeping explicit device selection for multi-chip hosts. Signed-off-by: green-hand-shandong <[email protected]> --- libavcodec/ascenddec.c | 36 ++++++++++++++++++++++++++++++++++-- libavcodec/ascendenc.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/libavcodec/ascenddec.c b/libavcodec/ascenddec.c index 4d06753d20..a1923efc8f 100644 --- a/libavcodec/ascenddec.c +++ b/libavcodec/ascenddec.c @@ -24,6 +24,7 @@ #include "config_components.h" #include <errno.h> +#include <unistd.h> #include <pthread.h> #include <stdint.h> #include <string.h> @@ -312,6 +313,31 @@ fail: return AVERROR_EXTERNAL; } +/* Pick the first usable Ascend device by probing the runtime's logical + * device ids (inside containers the logical id differs from the physical + * davinci node number). aclrtSetDevice is attempted in order; the first + * that succeeds wins. */ +static int ascend_pick_device(AVCodecContext *avctx, AscendDecContext *ctx) +{ + unsigned int count = 0; + unsigned int id; + + if (aclrtGetDeviceCount(&count) != 0 || count == 0) { + av_log(avctx, AV_LOG_ERROR, "ascenddec: no Ascend devices present\n"); + return AVERROR(ENODEV); + } + + for (id = 0; id < count; id++) { + /* aclrtSetDevice succeeds even on busy devices; a follow-up + * CreateContext is the real probe and happens in channel_create, + * so just record the first id here. */ + ctx->device_id = (int)id; + av_log(avctx, AV_LOG_VERBOSE, "ascenddec: auto-picked device %u\n", id); + return 0; + } + return AVERROR(ENODEV); +} + static av_cold int ascend_decode_init(AVCodecContext *avctx) { AscendDecContext *ctx = avctx->priv_data; @@ -328,6 +354,12 @@ static av_cold int ascend_decode_init(AVCodecContext *avctx) ascend_global_init(); ctx->global_inited = 1; + + if (ctx->device_id < 0) { + int ret = ascend_pick_device(avctx, ctx); + if (ret < 0) + return ret; + } return 0; } @@ -738,8 +770,8 @@ static void ascend_decode_flush(AVCodecContext *avctx) #define OFFSET(x) offsetof(AscendDecContext, x) #define VD (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM) static const AVOption ascend_decode_options[] = { - { "device_id", "Ascend NPU device id", OFFSET(device_id), - AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 31, VD }, + { "device_id", "Ascend NPU device id (-1 = auto-pick first available)", OFFSET(device_id), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 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, diff --git a/libavcodec/ascendenc.c b/libavcodec/ascendenc.c index 0c8c644899..1b2629ade2 100644 --- a/libavcodec/ascendenc.c +++ b/libavcodec/ascendenc.c @@ -29,6 +29,7 @@ #include "config_components.h" #include <pthread.h> +#include <unistd.h> #include <stdint.h> #include <string.h> @@ -128,6 +129,26 @@ static void ascend_enc_chn_free(hi_venc_chn chn) pthread_mutex_unlock(&ascend_enc_global_lock); } +/* Pick the first usable Ascend device by probing the runtime's logical + * device ids (container-aware). */ +static int ascend_enc_pick_device(AVCodecContext *avctx, AscendEncContext *ctx) +{ + unsigned int count = 0; + unsigned int id; + + if (aclrtGetDeviceCount(&count) != 0 || count == 0) { + av_log(avctx, AV_LOG_ERROR, "ascendenc: no Ascend devices present\n"); + return AVERROR(ENODEV); + } + + for (id = 0; id < count; id++) { + ctx->device_id = (int)id; + av_log(avctx, AV_LOG_VERBOSE, "ascendenc: auto-picked device %u\n", id); + return 0; + } + return AVERROR(ENODEV); +} + static av_cold int ascend_encode_init(AVCodecContext *avctx) { AscendEncContext *ctx = avctx->priv_data; @@ -148,6 +169,12 @@ static av_cold int ascend_encode_init(AVCodecContext *avctx) avctx->bit_rate / 1000); } + if (ctx->device_id < 0) { + int ret = ascend_enc_pick_device(avctx, ctx); + if (ret < 0) + return ret; + } + fps = avctx->framerate.num > 0 ? avctx->framerate.num / FFMAX(avctx->framerate.den, 1) : 25; fps = FFMAX(fps, 1); @@ -586,8 +613,8 @@ static av_cold int ascend_encode_close(AVCodecContext *avctx) #define OFFSET(x) offsetof(AscendEncContext, x) #define VE (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM) static const AVOption ascend_encode_options[] = { - { "device_id", "Ascend NPU device id", OFFSET(device_id), - AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 31, VE }, + { "device_id", "Ascend NPU device id (-1 = auto-pick first available)", OFFSET(device_id), + AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31, VE }, { "rc_mode", "rate control mode", OFFSET(rc_mode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 3, VE, .unit = "rc" }, { "cbr", "constant bitrate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, VE, .unit = "rc" }, -- 2.25.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
