Merge commit '124e26971e69bb25f38c6c7cb3fa20c77cf10966'
authorClément Bœsch <cboesch@gopro.com>
Thu, 30 Mar 2017 08:41:24 +0000 (10:41 +0200)
committerClément Bœsch <cboesch@gopro.com>
Thu, 30 Mar 2017 08:51:32 +0000 (10:51 +0200)
* commit '124e26971e69bb25f38c6c7cb3fa20c77cf10966':
  lavfi: Hardware map filter

Merged-by: Clément Bœsch <cboesch@gopro.com>
1  2 
libavfilter/Makefile
libavfilter/allfilters.c
libavfilter/version.h
libavfilter/vf_hwmap.c

@@@ -182,94 -57,31 +182,95 @@@ OBJS-$(CONFIG_FIND_RECT_FILTER
  OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
  OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
  OBJS-$(CONFIG_FRAMEPACK_FILTER)              += vf_framepack.o
 +OBJS-$(CONFIG_FRAMERATE_FILTER)              += vf_framerate.o
 +OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
  OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
 +OBJS-$(CONFIG_FSPP_FILTER)                   += vf_fspp.o
 +OBJS-$(CONFIG_GBLUR_FILTER)                  += vf_gblur.o
 +OBJS-$(CONFIG_GEQ_FILTER)                    += vf_geq.o
  OBJS-$(CONFIG_GRADFUN_FILTER)                += vf_gradfun.o
 +OBJS-$(CONFIG_HALDCLUT_FILTER)               += vf_lut3d.o dualinput.o framesync.o
  OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
 +OBJS-$(CONFIG_HISTEQ_FILTER)                 += vf_histeq.o
 +OBJS-$(CONFIG_HISTOGRAM_FILTER)              += vf_histogram.o
  OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
 +OBJS-$(CONFIG_HQX_FILTER)                    += vf_hqx.o
 +OBJS-$(CONFIG_HSTACK_FILTER)                 += vf_stack.o framesync.o
 +OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
  OBJS-$(CONFIG_HWDOWNLOAD_FILTER)             += vf_hwdownload.o
+ OBJS-$(CONFIG_HWMAP_FILTER)                  += vf_hwmap.o
  OBJS-$(CONFIG_HWUPLOAD_CUDA_FILTER)          += vf_hwupload_cuda.o
  OBJS-$(CONFIG_HWUPLOAD_FILTER)               += vf_hwupload.o
 +OBJS-$(CONFIG_HYSTERESIS_FILTER)             += vf_hysteresis.o framesync.o
 +OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
 +OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
 +OBJS-$(CONFIG_INFLATE_FILTER)                += vf_neighbor.o
  OBJS-$(CONFIG_INTERLACE_FILTER)              += vf_interlace.o
 +OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
 +OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
 +OBJS-$(CONFIG_LENSCORRECTION_FILTER)         += vf_lenscorrection.o
 +OBJS-$(CONFIG_LOOP_FILTER)                   += f_loop.o
  OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
 +OBJS-$(CONFIG_LUT2_FILTER)                   += vf_lut2.o framesync.o
 +OBJS-$(CONFIG_LUT3D_FILTER)                  += vf_lut3d.o
  OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
  OBJS-$(CONFIG_LUTYUV_FILTER)                 += vf_lut.o
 +OBJS-$(CONFIG_MASKEDCLAMP_FILTER)            += vf_maskedclamp.o framesync.o
 +OBJS-$(CONFIG_MASKEDMERGE_FILTER)            += vf_maskedmerge.o framesync.o
 +OBJS-$(CONFIG_MCDEINT_FILTER)                += vf_mcdeint.o
 +OBJS-$(CONFIG_MERGEPLANES_FILTER)            += vf_mergeplanes.o framesync.o
 +OBJS-$(CONFIG_MESTIMATE_FILTER)              += vf_mestimate.o motion_estimation.o
 +OBJS-$(CONFIG_METADATA_FILTER)               += f_metadata.o
 +OBJS-$(CONFIG_MIDEQUALIZER_FILTER)           += vf_midequalizer.o framesync.o
 +OBJS-$(CONFIG_MINTERPOLATE_FILTER)           += vf_minterpolate.o motion_estimation.o
 +OBJS-$(CONFIG_MPDECIMATE_FILTER)             += vf_mpdecimate.o
  OBJS-$(CONFIG_NEGATE_FILTER)                 += vf_lut.o
 +OBJS-$(CONFIG_NLMEANS_FILTER)                += vf_nlmeans.o
 +OBJS-$(CONFIG_NNEDI_FILTER)                  += vf_nnedi.o
  OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
 +OBJS-$(CONFIG_NOISE_FILTER)                  += vf_noise.o
  OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
 +OBJS-$(CONFIG_OCR_FILTER)                    += vf_ocr.o
  OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
 -OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
 +OBJS-$(CONFIG_OPENCL)                        += deshake_opencl.o unsharp_opencl.o
 +OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o dualinput.o framesync.o
 +OBJS-$(CONFIG_OWDENOISE_FILTER)              += vf_owdenoise.o
  OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 +OBJS-$(CONFIG_PALETTEGEN_FILTER)             += vf_palettegen.o
 +OBJS-$(CONFIG_PALETTEUSE_FILTER)             += vf_paletteuse.o dualinput.o framesync.o
 +OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
 +OBJS-$(CONFIG_PERSPECTIVE_FILTER)            += vf_perspective.o
 +OBJS-$(CONFIG_PHASE_FILTER)                  += vf_phase.o
  OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 -OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 -OBJS-$(CONFIG_SCALE_NPP_FILTER)              += vf_scale_npp.o
 +OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
 +OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
 +OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync.o
 +OBJS-$(CONFIG_PREWITT_FILTER)                += vf_convolution.o
 +OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o framesync.o
 +OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
 +OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
 +OBJS-$(CONFIG_RANDOM_FILTER)                 += vf_random.o
 +OBJS-$(CONFIG_READEIA608_FILTER)             += vf_readeia608.o
 +OBJS-$(CONFIG_READVITC_FILTER)               += vf_readvitc.o
 +OBJS-$(CONFIG_REALTIME_FILTER)               += f_realtime.o
 +OBJS-$(CONFIG_REMAP_FILTER)                  += vf_remap.o framesync.o
 +OBJS-$(CONFIG_REMOVEGRAIN_FILTER)            += vf_removegrain.o
 +OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
 +OBJS-$(CONFIG_REPEATFIELDS_FILTER)           += vf_repeatfields.o
 +OBJS-$(CONFIG_REVERSE_FILTER)                += f_reverse.o
 +OBJS-$(CONFIG_ROTATE_FILTER)                 += vf_rotate.o
 +OBJS-$(CONFIG_SAB_FILTER)                    += vf_sab.o
 +OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o scale.o
 +OBJS-$(CONFIG_SCALE_NPP_FILTER)              += vf_scale_npp.o scale.o
  OBJS-$(CONFIG_SCALE_QSV_FILTER)              += vf_scale_qsv.o
 -OBJS-$(CONFIG_SCALE_VAAPI_FILTER)            += vf_scale_vaapi.o
 -OBJS-$(CONFIG_SELECT_FILTER)                 += vf_select.o
 +OBJS-$(CONFIG_SCALE_VAAPI_FILTER)            += vf_scale_vaapi.o scale.o
 +OBJS-$(CONFIG_SCALE2REF_FILTER)              += vf_scale.o scale.o
 +OBJS-$(CONFIG_SELECT_FILTER)                 += f_select.o
 +OBJS-$(CONFIG_SELECTIVECOLOR_FILTER)         += vf_selectivecolor.o
 +OBJS-$(CONFIG_SENDCMD_FILTER)                += f_sendcmd.o
 +OBJS-$(CONFIG_SEPARATEFIELDS_FILTER)         += vf_separatefields.o
  OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
 +OBJS-$(CONFIG_SETFIELD_FILTER)               += vf_setfield.o
  OBJS-$(CONFIG_SETPTS_FILTER)                 += setpts.o
  OBJS-$(CONFIG_SETSAR_FILTER)                 += vf_aspect.o
  OBJS-$(CONFIG_SETTB_FILTER)                  += settb.o
@@@ -193,82 -80,25 +193,83 @@@ static void register_all(void
      REGISTER_FILTER(FORMAT,         format,         vf);
      REGISTER_FILTER(FPS,            fps,            vf);
      REGISTER_FILTER(FRAMEPACK,      framepack,      vf);
 +    REGISTER_FILTER(FRAMERATE,      framerate,      vf);
 +    REGISTER_FILTER(FRAMESTEP,      framestep,      vf);
      REGISTER_FILTER(FREI0R,         frei0r,         vf);
 +    REGISTER_FILTER(FSPP,           fspp,           vf);
 +    REGISTER_FILTER(GBLUR,          gblur,          vf);
 +    REGISTER_FILTER(GEQ,            geq,            vf);
      REGISTER_FILTER(GRADFUN,        gradfun,        vf);
 +    REGISTER_FILTER(HALDCLUT,       haldclut,       vf);
      REGISTER_FILTER(HFLIP,          hflip,          vf);
 +    REGISTER_FILTER(HISTEQ,         histeq,         vf);
 +    REGISTER_FILTER(HISTOGRAM,      histogram,      vf);
      REGISTER_FILTER(HQDN3D,         hqdn3d,         vf);
 +    REGISTER_FILTER(HQX,            hqx,            vf);
 +    REGISTER_FILTER(HSTACK,         hstack,         vf);
 +    REGISTER_FILTER(HUE,            hue,            vf);
      REGISTER_FILTER(HWDOWNLOAD,     hwdownload,     vf);
+     REGISTER_FILTER(HWMAP,          hwmap,          vf);
      REGISTER_FILTER(HWUPLOAD,       hwupload,       vf);
      REGISTER_FILTER(HWUPLOAD_CUDA,  hwupload_cuda,  vf);
 +    REGISTER_FILTER(HYSTERESIS,     hysteresis,     vf);
 +    REGISTER_FILTER(IDET,           idet,           vf);
 +    REGISTER_FILTER(IL,             il,             vf);
 +    REGISTER_FILTER(INFLATE,        inflate,        vf);
      REGISTER_FILTER(INTERLACE,      interlace,      vf);
 +    REGISTER_FILTER(INTERLEAVE,     interleave,     vf);
 +    REGISTER_FILTER(KERNDEINT,      kerndeint,      vf);
 +    REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf);
 +    REGISTER_FILTER(LOOP,           loop,           vf);
      REGISTER_FILTER(LUT,            lut,            vf);
 +    REGISTER_FILTER(LUT2,           lut2,           vf);
 +    REGISTER_FILTER(LUT3D,          lut3d,          vf);
      REGISTER_FILTER(LUTRGB,         lutrgb,         vf);
      REGISTER_FILTER(LUTYUV,         lutyuv,         vf);
 +    REGISTER_FILTER(MASKEDCLAMP,    maskedclamp,    vf);
 +    REGISTER_FILTER(MASKEDMERGE,    maskedmerge,    vf);
 +    REGISTER_FILTER(MCDEINT,        mcdeint,        vf);
 +    REGISTER_FILTER(MERGEPLANES,    mergeplanes,    vf);
 +    REGISTER_FILTER(MESTIMATE,      mestimate,      vf);
 +    REGISTER_FILTER(METADATA,       metadata,       vf);
 +    REGISTER_FILTER(MIDEQUALIZER,   midequalizer,   vf);
 +    REGISTER_FILTER(MINTERPOLATE,   minterpolate,   vf);
 +    REGISTER_FILTER(MPDECIMATE,     mpdecimate,     vf);
      REGISTER_FILTER(NEGATE,         negate,         vf);
 +    REGISTER_FILTER(NLMEANS,        nlmeans,        vf);
 +    REGISTER_FILTER(NNEDI,          nnedi,          vf);
      REGISTER_FILTER(NOFORMAT,       noformat,       vf);
 +    REGISTER_FILTER(NOISE,          noise,          vf);
      REGISTER_FILTER(NULL,           null,           vf);
 +    REGISTER_FILTER(OCR,            ocr,            vf);
      REGISTER_FILTER(OCV,            ocv,            vf);
      REGISTER_FILTER(OVERLAY,        overlay,        vf);
 +    REGISTER_FILTER(OWDENOISE,      owdenoise,      vf);
      REGISTER_FILTER(PAD,            pad,            vf);
 +    REGISTER_FILTER(PALETTEGEN,     palettegen,     vf);
 +    REGISTER_FILTER(PALETTEUSE,     paletteuse,     vf);
 +    REGISTER_FILTER(PERMS,          perms,          vf);
 +    REGISTER_FILTER(PERSPECTIVE,    perspective,    vf);
 +    REGISTER_FILTER(PHASE,          phase,          vf);
      REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
 +    REGISTER_FILTER(PP,             pp,             vf);
 +    REGISTER_FILTER(PP7,            pp7,            vf);
 +    REGISTER_FILTER(PREMULTIPLY,    premultiply,    vf);
 +    REGISTER_FILTER(PREWITT,        prewitt,        vf);
 +    REGISTER_FILTER(PSNR,           psnr,           vf);
 +    REGISTER_FILTER(PULLUP,         pullup,         vf);
 +    REGISTER_FILTER(QP,             qp,             vf);
 +    REGISTER_FILTER(RANDOM,         random,         vf);
 +    REGISTER_FILTER(READEIA608,     readeia608,     vf);
 +    REGISTER_FILTER(READVITC,       readvitc,       vf);
 +    REGISTER_FILTER(REALTIME,       realtime,       vf);
 +    REGISTER_FILTER(REMAP,          remap,          vf);
 +    REGISTER_FILTER(REMOVEGRAIN,    removegrain,    vf);
 +    REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
 +    REGISTER_FILTER(REPEATFIELDS,   repeatfields,   vf);
 +    REGISTER_FILTER(REVERSE,        reverse,        vf);
 +    REGISTER_FILTER(ROTATE,         rotate,         vf);
 +    REGISTER_FILTER(SAB,            sab,            vf);
      REGISTER_FILTER(SCALE,          scale,          vf);
      REGISTER_FILTER(SCALE_NPP,      scale_npp,      vf);
      REGISTER_FILTER(SCALE_QSV,      scale_qsv,      vf);
@@@ -29,9 -29,9 +29,9 @@@
  
  #include "libavutil/version.h"
  
 -#define LIBAVFILTER_VERSION_MAJOR  6
 -#define LIBAVFILTER_VERSION_MINOR  8
 -#define LIBAVFILTER_VERSION_MICRO  0
 +#define LIBAVFILTER_VERSION_MAJOR   6
- #define LIBAVFILTER_VERSION_MINOR  78
- #define LIBAVFILTER_VERSION_MICRO 101
++#define LIBAVFILTER_VERSION_MINOR  79
++#define LIBAVFILTER_VERSION_MICRO 100
  
  #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
                                                 LIBAVFILTER_VERSION_MINOR, \
index 0000000,11595bd..654477c
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,335 +1,334 @@@
 - * This file is part of Libav.
+ /*
 - * Libav is free software; you can redistribute it and/or
++ * This file is part of FFmpeg.
+  *
 - * Libav is distributed in the hope that it will be useful,
++ * 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.
+  *
 - * License along with Libav; if not, write to the Free Software
++ * 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
 -    ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
 -                   &avctx->inputs[0]->out_formats);
 -    ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
 -                   &avctx->outputs[0]->in_formats);
++ * License along with FFmpeg; 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/log.h"
+ #include "libavutil/opt.h"
+ #include "libavutil/pixdesc.h"
+ #include "avfilter.h"
+ #include "formats.h"
+ #include "internal.h"
+ #include "video.h"
+ typedef struct HWMapContext {
+     const AVClass *class;
+     AVBufferRef   *hwdevice_ref;
+     AVBufferRef   *hwframes_ref;
+     int            mode;
+     int            map_backwards;
+ } HWMapContext;
+ static int hwmap_query_formats(AVFilterContext *avctx)
+ {
 -#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
++    int ret;
++
++    if ((ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
++                              &avctx->inputs[0]->out_formats)) < 0 ||
++        (ret = ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_VIDEO),
++                              &avctx->outputs[0]->in_formats)) < 0)
++        return ret;
++
+     return 0;
+ }
+ static int hwmap_config_output(AVFilterLink *outlink)
+ {
+     AVFilterContext *avctx = outlink->src;
+     HWMapContext      *ctx = avctx->priv;
+     AVFilterLink   *inlink = avctx->inputs[0];
+     AVHWFramesContext *hwfc;
+     const AVPixFmtDescriptor *desc;
+     int err;
+     av_log(avctx, AV_LOG_DEBUG, "Configure hwmap %s -> %s.\n",
+            av_get_pix_fmt_name(inlink->format),
+            av_get_pix_fmt_name(outlink->format));
+     if (inlink->hw_frames_ctx) {
+         hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
+         desc = av_pix_fmt_desc_get(outlink->format);
+         if (!desc)
+             return AVERROR(EINVAL);
+         if (inlink->format == hwfc->format &&
+             (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
+             // Map between two hardware formats (including the case of
+             // undoing an existing mapping).
+             ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
+             if (!ctx->hwdevice_ref) {
+                 err = AVERROR(ENOMEM);
+                 goto fail;
+             }
+             err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref,
+                                                 outlink->format,
+                                                 ctx->hwdevice_ref,
+                                                 inlink->hw_frames_ctx, 0);
+             if (err < 0)
+                 goto fail;
+         } else if ((outlink->format == hwfc->format &&
+                     inlink->format  == hwfc->sw_format) ||
+                    inlink->format == hwfc->format) {
+             // Map from a hardware format to a software format, or
+             // undo an existing such mapping.
+             ctx->hwdevice_ref = NULL;
+             ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
+             if (!ctx->hwframes_ref) {
+                 err = AVERROR(ENOMEM);
+                 goto fail;
+             }
+         } else {
+             // Non-matching formats - not supported.
+             av_log(avctx, AV_LOG_ERROR, "Unsupported formats for "
+                    "hwmap: from %s (%s) to %s.\n",
+                    av_get_pix_fmt_name(inlink->format),
+                    av_get_pix_fmt_name(hwfc->format),
+                    av_get_pix_fmt_name(outlink->format));
+             err = AVERROR(EINVAL);
+             goto fail;
+         }
+     } else if (avctx->hw_device_ctx) {
+         // Map from a software format to a hardware format.  This
+         // creates a new hwframe context like hwupload, but then
+         // returns frames mapped from that to the previous link in
+         // order to fill them without an additional copy.
+         ctx->map_backwards = 1;
+         ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
+         if (!ctx->hwdevice_ref) {
+             err = AVERROR(ENOMEM);
+             goto fail;
+         }
+         ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
+         if (!ctx->hwframes_ref) {
+             err = AVERROR(ENOMEM);
+             goto fail;
+         }
+         hwfc = (AVHWFramesContext*)ctx->hwframes_ref->data;
+         hwfc->format    = outlink->format;
+         hwfc->sw_format = inlink->format;
+         hwfc->width     = inlink->w;
+         hwfc->height    = inlink->h;
+         err = av_hwframe_ctx_init(ctx->hwframes_ref);
+         if (err < 0) {
+             av_log(avctx, AV_LOG_ERROR, "Failed to create frame "
+                    "context for backward mapping: %d.\n", err);
+             goto fail;
+         }
+     } else {
+         av_log(avctx, AV_LOG_ERROR, "Mapping requires a hardware "
+                "context (a device, or frames on input).\n");
+         return AVERROR(EINVAL);
+     }
+     outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
+     if (!outlink->hw_frames_ctx) {
+         err = AVERROR(ENOMEM);
+         goto fail;
+     }
+     outlink->w = inlink->w;
+     outlink->h = inlink->h;
+     return 0;
+ fail:
+     av_buffer_unref(&ctx->hwframes_ref);
+     av_buffer_unref(&ctx->hwdevice_ref);
+     return err;
+ }
+ static AVFrame *hwmap_get_buffer(AVFilterLink *inlink, int w, int h)
+ {
+     AVFilterContext *avctx = inlink->dst;
+     AVFilterLink  *outlink = avctx->outputs[0];
+     HWMapContext      *ctx = avctx->priv;
+     if (ctx->map_backwards) {
+         AVFrame *src, *dst;
+         int err;
+         src = ff_get_video_buffer(outlink, w, h);
+         if (!src) {
+             av_log(avctx, AV_LOG_ERROR, "Failed to allocate source "
+                    "frame for software mapping.\n");
+             return NULL;
+         }
+         dst = av_frame_alloc();
+         if (!dst) {
+             av_frame_free(&src);
+             return NULL;
+         }
+         err = av_hwframe_map(dst, src, ctx->mode);
+         if (err) {
+             av_log(avctx, AV_LOG_ERROR, "Failed to map frame to "
+                    "software: %d.\n", err);
+             av_frame_free(&src);
+             av_frame_free(&dst);
+             return NULL;
+         }
+         av_frame_free(&src);
+         return dst;
+     } else {
+         return ff_default_get_video_buffer(inlink, w, h);
+     }
+ }
+ static int hwmap_filter_frame(AVFilterLink *link, AVFrame *input)
+ {
+     AVFilterContext *avctx = link->dst;
+     AVFilterLink  *outlink = avctx->outputs[0];
+     HWMapContext      *ctx = avctx->priv;
+     AVFrame *map = NULL;
+     int err;
+     av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
+            av_get_pix_fmt_name(input->format),
+            input->width, input->height, input->pts);
+     map = av_frame_alloc();
+     if (!map) {
+         err = AVERROR(ENOMEM);
+         goto fail;
+     }
+     map->format = outlink->format;
+     map->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
+     if (!map->hw_frames_ctx) {
+         err = AVERROR(ENOMEM);
+         goto fail;
+     }
+     if (ctx->map_backwards && !input->hw_frames_ctx) {
+         // If we mapped backwards from hardware to software, we need
+         // to attach the hardware frame context to the input frame to
+         // make the mapping visible to av_hwframe_map().
+         input->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
+         if (!input->hw_frames_ctx) {
+             err = AVERROR(ENOMEM);
+             goto fail;
+         }
+     }
+     err = av_hwframe_map(map, input, ctx->mode);
+     if (err < 0) {
+         av_log(avctx, AV_LOG_ERROR, "Failed to map frame: %d.\n", err);
+         goto fail;
+     }
+     err = av_frame_copy_props(map, input);
+     if (err < 0)
+         goto fail;
+     av_frame_free(&input);
+     av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
+            av_get_pix_fmt_name(map->format),
+            map->width, map->height, map->pts);
+     return ff_filter_frame(outlink, map);
+ fail:
+     av_frame_free(&input);
+     av_frame_free(&map);
+     return err;
+ }
+ static av_cold void hwmap_uninit(AVFilterContext *avctx)
+ {
+     HWMapContext *ctx = avctx->priv;
+     av_buffer_unref(&ctx->hwframes_ref);
+     av_buffer_unref(&ctx->hwdevice_ref);
+ }
+ #define OFFSET(x) offsetof(HWMapContext, x)
 -    { NULL },
++#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
+ static const AVOption hwmap_options[] = {
+     { "mode", "Frame mapping mode",
+       OFFSET(mode), AV_OPT_TYPE_FLAGS,
+       { .i64 = AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE },
+       0, INT_MAX, FLAGS, "mode" },
+     { "read", "Mapping should be readable",
+       0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_READ },
+       INT_MIN, INT_MAX, FLAGS, "mode" },
+     { "write", "Mapping should be writeable",
+       0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_WRITE },
+       INT_MIN, INT_MAX, FLAGS, "mode" },
+     { "overwrite", "Mapping will always overwrite the entire frame",
+       0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_OVERWRITE },
+       INT_MIN, INT_MAX, FLAGS, "mode" },
+     { "direct", "Mapping should not involve any copying",
+       0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT },
+       INT_MIN, INT_MAX, FLAGS, "mode" },
 -static const AVClass hwmap_class = {
 -    .class_name = "hwmap",
 -    .item_name  = av_default_item_name,
 -    .option     = hwmap_options,
 -    .version    = LIBAVUTIL_VERSION_INT,
 -};
++    { NULL }
+ };
 -        .get_video_buffer = &hwmap_get_buffer,
 -        .filter_frame     = &hwmap_filter_frame,
++AVFILTER_DEFINE_CLASS(hwmap);
+ static const AVFilterPad hwmap_inputs[] = {
+     {
+         .name             = "default",
+         .type             = AVMEDIA_TYPE_VIDEO,
 -        .config_props = &hwmap_config_output,
++        .get_video_buffer = hwmap_get_buffer,
++        .filter_frame     = hwmap_filter_frame,
+     },
+     { NULL }
+ };
+ static const AVFilterPad hwmap_outputs[] = {
+     {
+         .name         = "default",
+         .type         = AVMEDIA_TYPE_VIDEO,
 -    .uninit         = &hwmap_uninit,
++        .config_props = hwmap_config_output,
+     },
+     { NULL }
+ };
+ AVFilter ff_vf_hwmap = {
+     .name           = "hwmap",
+     .description    = NULL_IF_CONFIG_SMALL("Map hardware frames"),
 -    .query_formats  = &hwmap_query_formats,
++    .uninit         = hwmap_uninit,
+     .priv_size      = sizeof(HWMapContext),
+     .priv_class     = &hwmap_class,
++    .query_formats  = hwmap_query_formats,
+     .inputs         = hwmap_inputs,
+     .outputs        = hwmap_outputs,
+     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
+ };