---
 libavfilter/Makefile        |   2 +
 libavfilter/allfilters.c    |   2 +
 libavfilter/avfilter.c      |   1 +
 libavfilter/avfilter.h      |   9 ++
 libavfilter/vf_hwdownload.c | 178 ++++++++++++++++++++++++++++++++++
 libavfilter/vf_hwupload.c   | 227 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 419 insertions(+)
 create mode 100644 libavfilter/vf_hwdownload.c
 create mode 100644 libavfilter/vf_hwupload.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index f5dd2e9..fa6647b 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -56,6 +56,8 @@ OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
 OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
 OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
+OBJS-$(CONFIG_HWDOWNLOAD_FILTER)             += vf_hwdownload.o
+OBJS-$(CONFIG_HWUPLOAD_FILTER)               += vf_hwupload.o
 OBJS-$(CONFIG_HWUPLOAD_CUDA_FILTER)          += vf_hwupload_cuda.o
 OBJS-$(CONFIG_INTERLACE_FILTER)              += vf_interlace.o
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 4bdfaea..9461145 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -82,6 +82,8 @@ void avfilter_register_all(void)
     REGISTER_FILTER(GRADFUN,        gradfun,        vf);
     REGISTER_FILTER(HFLIP,          hflip,          vf);
     REGISTER_FILTER(HQDN3D,         hqdn3d,         vf);
+    REGISTER_FILTER(HWDOWNLOAD,     hwdownload,     vf);
+    REGISTER_FILTER(HWUPLOAD,       hwupload,       vf);
     REGISTER_FILTER(HWUPLOAD_CUDA,  hwupload_cuda,  vf);
     REGISTER_FILTER(INTERLACE,      interlace,      vf);
     REGISTER_FILTER(LUT,            lut,            vf);
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 8eefc51..bc7c343 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -532,6 +532,7 @@ void avfilter_free(AVFilterContext *filter)
     av_freep(&filter->outputs);
     av_freep(&filter->priv);
     av_freep(&filter->internal);
+    av_freep(&filter->hw_device_ctx);
     av_free(filter);
 }

diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index 0a0c415..65918fc 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -300,6 +300,15 @@ struct AVFilterContext {
      * An opaque struct for libavfilter internal use.
      */
     AVFilterInternal *internal;
+
+    /**
+     * For filters which will create hardware frames, sets the device the
+     * filter should create them in.  All other filters will ignore this field:
+     * in particular, a filter which consumes or processes hardware frames will
+     * instead use the hw_frames_ctx field in AVFilterLink to carry the
+     * hardware context information.
+     */
+    AVBufferRef *hw_device_ctx;
 };

 /**
diff --git a/libavfilter/vf_hwdownload.c b/libavfilter/vf_hwdownload.c
new file mode 100644
index 0000000..e9bd453
--- /dev/null
+++ b/libavfilter/vf_hwdownload.c
@@ -0,0 +1,178 @@
+/*
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/buffer.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/log.h"
+#include "libavutil/opt.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct HWDownloadContext {
+    const AVClass *class;
+
+    AVBufferRef       *hwframes_ref;
+    AVHWFramesContext *hwframes;
+} HWDownloadContext;
+
+static int hwdownload_query_formats(AVFilterContext *avctx)
+{
+    if (avctx->inputs[0]) {
+        ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
+                       &avctx->inputs[0]->out_formats);
+    }
+    if (avctx->outputs[0]) {
+        ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
+                       &avctx->outputs[0]->in_formats);
+    }
+
+    return 0;
+}
+
+static int hwdownload_config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *avctx = inlink->dst;
+    HWDownloadContext *ctx = avctx->priv;
+
+    av_buffer_unref(&ctx->hwframes_ref);
+
+    if (!inlink->hw_frames_ctx) {
+        // We allow success here in order to allow dynamic reconfiguration
+        // to do the right thing after one step.  filter_frame must not
+        // be called in this state, however.
+        return 0;
+    }
+
+    ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
+    if (!ctx->hwframes_ref)
+        return AVERROR(ENOMEM);
+
+    ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
+
+    return 0;
+}
+
+static int hwdownload_config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *avctx = outlink->src;
+    AVFilterLink *inlink   = avctx->inputs[0];
+    HWDownloadContext *ctx = avctx->priv;
+
+    if (ctx->hwframes_ref) {
+        outlink->format = ctx->hwframes->sw_format;
+        outlink->w      = ctx->hwframes->width;
+        outlink->h      = ctx->hwframes->height;
+    } else {
+        outlink->format = inlink->format;
+        outlink->w      = inlink->w;
+        outlink->h      = inlink->h;
+    }
+
+    return 0;
+}
+
+static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
+{
+    AVFilterContext *avctx = link->dst;
+    HWDownloadContext *ctx = avctx->priv;
+    AVFrame *output = NULL;
+    int err;
+
+    if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
+        av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware 
context.\n");
+        return AVERROR(EINVAL);
+    }
+    av_assert0((void*)ctx->hwframes == input->hw_frames_ctx->data);
+
+    output = av_frame_alloc();
+    if (!output) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    output->format = ctx->hwframes->sw_format;
+    output->width  = input->width;
+    output->height = input->height;
+
+    err = av_hwframe_transfer_data(output, input, 0);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
+        goto fail;
+    }
+
+    err = av_frame_copy_props(output, input);
+    if (err < 0)
+        goto fail;
+
+    av_frame_free(&input);
+
+    return ff_filter_frame(avctx->outputs[0], output);
+
+  fail:
+    av_frame_free(&input);
+    av_frame_free(&output);
+    return err;
+}
+
+static av_cold void hwdownload_uninit(AVFilterContext *avctx)
+{
+    HWDownloadContext *ctx = avctx->priv;
+
+    av_buffer_unref(&ctx->hwframes_ref);
+}
+
+static const AVClass hwdownload_class = {
+    .class_name = "hwdownload",
+    .item_name  = av_default_item_name,
+    .option     = NULL,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVFilterPad hwdownload_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = hwdownload_config_input,
+        .filter_frame = hwdownload_filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad hwdownload_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = hwdownload_config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_hwdownload = {
+    .name          = "hwdownload",
+    .description   = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware 
frame"),
+    .uninit        = hwdownload_uninit,
+    .query_formats = hwdownload_query_formats,
+    .priv_size     = sizeof(HWDownloadContext),
+    .priv_class    = &hwdownload_class,
+    .inputs        = hwdownload_inputs,
+    .outputs       = hwdownload_outputs,
+};
diff --git a/libavfilter/vf_hwupload.c b/libavfilter/vf_hwupload.c
new file mode 100644
index 0000000..d8eeb9d
--- /dev/null
+++ b/libavfilter/vf_hwupload.c
@@ -0,0 +1,227 @@
+/*
+ * This file is part of Libav.
+ *
+ * Libav 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.
+ *
+ * Libav 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 Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/buffer.h"
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_internal.h"
+#include "libavutil/log.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct HWUploadContext {
+    const AVClass *class;
+
+    AVBufferRef       *hwdevice_ref;
+    AVHWDeviceContext *hwdevice;
+
+    AVBufferRef       *hwframes_ref;
+    AVHWFramesContext *hwframes;
+} HWUploadContext;
+
+static int hwupload_late_init(AVFilterContext *avctx)
+{
+    HWUploadContext *ctx = avctx->priv;
+
+    if (ctx->hwdevice_ref)
+        return 0;
+
+    if (!avctx->hw_device_ctx) {
+        av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
+               "to upload frames to.\n");
+        return AVERROR(EINVAL);
+    }
+
+    ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
+    if (!ctx->hwdevice_ref)
+        return AVERROR(ENOMEM);
+
+    ctx->hwdevice = (AVHWDeviceContext*)ctx->hwdevice_ref->data;
+
+    return 0;
+}
+
+static int hwupload_query_formats(AVFilterContext *avctx)
+{
+    HWUploadContext *ctx = avctx->priv;
+    AVHWFramesConstraints *constraints = NULL;
+    const enum AVPixelFormat *output_pix_fmts;
+    int err;
+
+    err = hwupload_late_init(avctx);
+    if (err < 0)
+        return err;
+
+    // Naughty internal reference, but we need to get the hardware pixfmts
+    // associated with the device somehow.
+    output_pix_fmts =
+        ((AVHWDeviceInternal*)ctx->hwdevice->internal)->hw_type->pix_fmts;
+
+    constraints = av_hwframe_constraints_alloc();
+    if (!constraints) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    err = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL,
+                                              constraints);
+    if (err < 0)
+        goto fail;
+
+    if (avctx->inputs[0]) {
+        ff_formats_ref(ff_make_format_list(constraints->valid_sw_formats),
+                       &avctx->inputs[0]->out_formats);
+    }
+    if (avctx->outputs[0]) {
+        ff_formats_ref(ff_make_format_list(output_pix_fmts),
+                       &avctx->outputs[0]->in_formats);
+    }
+
+    err = 0;
+  fail:
+    av_hwframe_constraints_free(constraints);
+    return err;
+}
+
+static int hwupload_config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *avctx = outlink->src;
+    AVFilterLink *inlink   = avctx->inputs[0];
+    HWUploadContext *ctx   = avctx->priv;
+    int err;
+
+    err = hwupload_late_init(avctx);
+    if (err < 0)
+        return err;
+
+    av_buffer_unref(&ctx->hwframes_ref);
+
+    ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
+    if (!ctx->hwframes_ref)
+        return AVERROR(ENOMEM);
+
+    ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
+
+    av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
+           av_get_pix_fmt_name(inlink->format));
+
+    ctx->hwframes->format    = outlink->format;
+    ctx->hwframes->sw_format = inlink->format;
+    ctx->hwframes->width     = inlink->w;
+    ctx->hwframes->height    = inlink->h;
+
+    err = av_hwframe_ctx_init(ctx->hwframes_ref);
+    if (err < 0)
+        return err;
+
+    outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
+    if (!outlink->hw_frames_ctx)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
+{
+    AVFilterContext *avctx = link->dst;
+    HWUploadContext *ctx = avctx->priv;
+    AVFrame *output = NULL;
+    int err;
+
+    output = av_frame_alloc();
+    if (!output) {
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    err = av_hwframe_get_buffer(ctx->hwframes_ref, output, 0);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
+        goto fail;
+    }
+
+    output->width  = input->width;
+    output->height = input->height;
+
+    err = av_hwframe_transfer_data(output, input, 0);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
+        goto fail;
+    }
+
+    err = av_frame_copy_props(output, input);
+    if (err < 0)
+        goto fail;
+
+    av_frame_free(&input);
+
+    return ff_filter_frame(avctx->outputs[0], output);
+
+  fail:
+    av_frame_free(&input);
+    av_frame_free(&output);
+    return err;
+}
+
+static av_cold void hwupload_uninit(AVFilterContext *avctx)
+{
+    HWUploadContext *ctx = avctx->priv;
+
+    av_buffer_unref(&ctx->hwframes_ref);
+    av_buffer_unref(&ctx->hwdevice_ref);
+}
+
+static const AVClass hwupload_class = {
+    .class_name = "hwupload",
+    .item_name  = av_default_item_name,
+    .option     = NULL,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVFilterPad hwupload_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = hwupload_filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad hwupload_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = hwupload_config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_hwupload = {
+    .name          = "hwupload",
+    .description   = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware 
frame"),
+    .uninit        = hwupload_uninit,
+    .query_formats = hwupload_query_formats,
+    .priv_size     = sizeof(HWUploadContext),
+    .priv_class    = &hwupload_class,
+    .inputs        = hwupload_inputs,
+    .outputs       = hwupload_outputs,
+};
-- 
2.7.0

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to