From f15b20628675c72e3240352ff466e3ebc55a0702 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 19 May 2023 09:20:37 +0200 Subject: [PATCH] fftools/ffmpeg_hw: move hw_device_setup_for_encode() to ffmpeg_enc This function is entangled with encoder setup, so it is more encoding code rather than ffmpeg_hw code. This will allow making more encoder state private in the future. --- fftools/ffmpeg.h | 1 - fftools/ffmpeg_enc.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ fftools/ffmpeg_hw.c | 55 -------------------------------------------- 3 files changed, 55 insertions(+), 56 deletions(-) diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 895edbd6d6..927e402f7c 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -802,7 +802,6 @@ int hw_device_init_from_type(enum AVHWDeviceType type, HWDevice **dev_out); void hw_device_free_all(void); -int hw_device_setup_for_encode(OutputStream *ost); /** * Get a hardware device to be used with this filtergraph. * The returned reference is owned by the callee, the caller diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index 9b81d14922..7d99d9270b 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -103,6 +103,61 @@ fail: return AVERROR(ENOMEM); } +static int hw_device_setup_for_encode(OutputStream *ost) +{ + const AVCodecHWConfig *config; + HWDevice *dev = NULL; + AVBufferRef *frames_ref = NULL; + int i; + + if (ost->filter) { + frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter); + if (frames_ref && + ((AVHWFramesContext*)frames_ref->data)->format == + ost->enc_ctx->pix_fmt) { + // Matching format, will try to use hw_frames_ctx. + } else { + frames_ref = NULL; + } + } + + for (i = 0;; i++) { + config = avcodec_get_hw_config(ost->enc_ctx->codec, i); + if (!config) + break; + + if (frames_ref && + config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX && + (config->pix_fmt == AV_PIX_FMT_NONE || + config->pix_fmt == ost->enc_ctx->pix_fmt)) { + av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input " + "frames context (format %s) with %s encoder.\n", + av_get_pix_fmt_name(ost->enc_ctx->pix_fmt), + ost->enc_ctx->codec->name); + ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref); + if (!ost->enc_ctx->hw_frames_ctx) + return AVERROR(ENOMEM); + return 0; + } + + if (!dev && + config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) + dev = hw_device_get_by_type(config->device_type); + } + + if (dev) { + av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s " + "(type %s) with %s encoder.\n", dev->name, + av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name); + ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); + if (!ost->enc_ctx->hw_device_ctx) + return AVERROR(ENOMEM); + } else { + // No device required, or no device available. + } + return 0; +} + static void set_encoder_id(OutputFile *of, OutputStream *ost) { const char *cname = ost->enc_ctx->codec->name; diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c index d28257a1d6..4a0b346fe1 100644 --- a/fftools/ffmpeg_hw.c +++ b/fftools/ffmpeg_hw.c @@ -297,61 +297,6 @@ void hw_device_free_all(void) nb_hw_devices = 0; } -int hw_device_setup_for_encode(OutputStream *ost) -{ - const AVCodecHWConfig *config; - HWDevice *dev = NULL; - AVBufferRef *frames_ref = NULL; - int i; - - if (ost->filter) { - frames_ref = av_buffersink_get_hw_frames_ctx(ost->filter->filter); - if (frames_ref && - ((AVHWFramesContext*)frames_ref->data)->format == - ost->enc_ctx->pix_fmt) { - // Matching format, will try to use hw_frames_ctx. - } else { - frames_ref = NULL; - } - } - - for (i = 0;; i++) { - config = avcodec_get_hw_config(ost->enc_ctx->codec, i); - if (!config) - break; - - if (frames_ref && - config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX && - (config->pix_fmt == AV_PIX_FMT_NONE || - config->pix_fmt == ost->enc_ctx->pix_fmt)) { - av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input " - "frames context (format %s) with %s encoder.\n", - av_get_pix_fmt_name(ost->enc_ctx->pix_fmt), - ost->enc_ctx->codec->name); - ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref); - if (!ost->enc_ctx->hw_frames_ctx) - return AVERROR(ENOMEM); - return 0; - } - - if (!dev && - config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) - dev = hw_device_get_by_type(config->device_type); - } - - if (dev) { - av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s " - "(type %s) with %s encoder.\n", dev->name, - av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name); - ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); - if (!ost->enc_ctx->hw_device_ctx) - return AVERROR(ENOMEM); - } else { - // No device required, or no device available. - } - return 0; -} - static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input) { InputStream *ist = avctx->opaque;