FFmpeg
vf_vqe_amf.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * Quality Enhancer video filter with AMF hardware acceleration
22  */
23 
24 #include "libavutil/opt.h"
25 
26 #include "libavutil/hwcontext.h"
29 
30 #include "AMF/components/VQEnhancer.h"
31 #include "vf_amf_common.h"
32 
33 #include "avfilter.h"
34 #include "avfilter_internal.h"
35 #include "formats.h"
36 #include "video.h"
37 
38 #if CONFIG_D3D11VA
39 #include <d3d11.h>
40 #endif
41 
42 #if CONFIG_D3D12VA
43 #include <d3d12.h>
44 #endif
45 
46 typedef struct AMFVQEFilterContext {
48 
50  double attenuation;
52 
53 static int amf_vqe_init(AVFilterContext *avctx) {
54  AMFVQEFilterContext *ctx = avctx->priv;
55 
56  ctx->common.format = AV_PIX_FMT_NONE;
57 
58  return 0;
59 }
60 
62 {
63  const enum AVPixelFormat *output_pix_fmts;
64  static const enum AVPixelFormat input_pix_fmts[] = {
73  };
74  static const enum AVPixelFormat output_pix_fmts_default[] = {
83  };
84  output_pix_fmts = output_pix_fmts_default;
85 
86  return amf_setup_input_output_formats(avctx, input_pix_fmts, output_pix_fmts);
87 }
88 
90 {
91  AVFilterContext *avctx = outlink->src;
92  AMFComponent *amf_filter = NULL;
93  AVFilterLink *inlink = avctx->inputs[0];
94  AMFVQEFilterContext *vqe_ctx = avctx->priv;
95  AMFFilterContext *amf_ctx = &vqe_ctx->common;
96  AVAMFDeviceContext *device_ctx = NULL;
97 
98  int err;
99  AMF_RESULT res;
100  enum AVPixelFormat in_format;
101 
102  err = amf_init_filter_config(outlink, &in_format);
103  if (err < 0)
104  return err;
105 
106  device_ctx = amf_ctx->amf_device_ctx;
107 
108  res = AMF_IFACE_CALL(device_ctx->factory, CreateComponent, device_ctx->context, AMFVQEnhancer, &amf_ctx->component);
109  AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_FILTER_NOT_FOUND, "CreateComponent(%ls) failed with error %d\n", AMFVQEnhancer, res);
110 
111  amf_filter = amf_ctx->component;
112 
113  if (vqe_ctx->engine_type != -1)
114  AMF_ASSIGN_PROPERTY_INT64(res, amf_filter, AMF_VIDEO_ENHANCER_ENGINE_TYPE, vqe_ctx->engine_type);
115 
116  AMF_ASSIGN_PROPERTY_DOUBLE(res, amf_filter, AMF_VE_FCR_ATTENUATION, vqe_ctx->attenuation);
117  AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "Failed to set VQ enhancer attenuation: %d\n", res);
118 
119  res = AMF_IFACE_CALL(amf_filter, Init, av_av_to_amf_format(in_format), inlink->w, inlink->h);
120  AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFVQEnhancer-Init() failed with error %d\n", res);
121 
122  return 0;
123 }
124 
125 #define OFFSET(x) offsetof(AMFVQEFilterContext, x)
126 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
127 static const AVOption vqe_amf_options[] = {
128  { "engine_type", "Engine type", OFFSET(engine_type), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_MEMORY_OPENCL, .flags = FLAGS, "engine_type" },
129  { "dx11", "DirectX 11", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_MEMORY_DX11 }, 0, 0, FLAGS, "engine_type" },
130  { "dx12", "DirectX 12", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_MEMORY_DX12 }, 0, 0, FLAGS, "engine_type" },
131  { "vulkan", "Vulkan", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_MEMORY_VULKAN }, 0, 0, FLAGS, "engine_type" },
132  { "opencl", "OpenCL", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_MEMORY_OPENCL }, 0, 0, FLAGS, "engine_type" },
133 
134  { "attenuation", "Control VQEnhancer strength", OFFSET(attenuation), AV_OPT_TYPE_DOUBLE, { .dbl = 0.1 }, 0.02, 0.4, FLAGS, "attenuation" },
135 
136  { NULL },
137 };
138 
139 AVFILTER_DEFINE_CLASS(vqe_amf);
140 
141 static const AVFilterPad amf_filter_inputs[] = {
142  {
143  .name = "default",
144  .type = AVMEDIA_TYPE_VIDEO,
145  .filter_frame = amf_filter_filter_frame,
146  }
147 };
148 
149 static const AVFilterPad amf_filter_outputs[] = {
150  {
151  .name = "default",
152  .type = AVMEDIA_TYPE_VIDEO,
153  .config_props = amf_vqe_filter_config_output,
154  }
155 };
156 
158  .p.name = "vqe_amf",
159  .p.description = NULL_IF_CONFIG_SMALL("AMD AMF VQ Enhancer"),
160  .p.priv_class = &vqe_amf_class,
161  .p.flags = AVFILTER_FLAG_HWDEVICE,
162  .priv_size = sizeof(AMFVQEFilterContext),
163  .init = amf_vqe_init,
168  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
169 };
amf_filter_outputs
static const AVFilterPad amf_filter_outputs[]
Definition: vf_vqe_amf.c:149
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
opt.h
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
FF_FILTER_FLAG_HWFRAME_AWARE
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: filters.h:208
FLAGS
#define FLAGS
Definition: vf_vqe_amf.c:126
AVOption
AVOption.
Definition: opt.h:428
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
amf_setup_input_output_formats
int amf_setup_input_output_formats(AVFilterContext *avctx, const enum AVPixelFormat *input_pix_fmts, const enum AVPixelFormat *output_pix_fmts)
Definition: vf_amf_common.c:180
OFFSET
#define OFFSET(x)
Definition: vf_vqe_amf.c:125
AMF_RETURN_IF_FALSE
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value,...)
Error handling helper.
Definition: amfenc.h:169
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:219
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
video.h
AV_PIX_FMT_AMF_SURFACE
@ AV_PIX_FMT_AMF_SURFACE
HW acceleration through AMF.
Definition: pixfmt.h:477
formats.h
attenuation
static const float attenuation[10]
Definition: speexdata.h:768
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:288
amf_vqe_init
static int amf_vqe_init(AVFilterContext *avctx)
Definition: vf_vqe_amf.c:53
AMFVQEFilterContext::attenuation
double attenuation
Definition: vf_vqe_amf.c:50
av_av_to_amf_format
enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt)
Definition: hwcontext_amf.c:133
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:40
AVAMFDeviceContext::context
AMFContext * context
Definition: hwcontext_amf.h:41
AMFVQEFilterContext::engine_type
int engine_type
Definition: vf_vqe_amf.c:49
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(vqe_amf)
FFFilter
Definition: filters.h:267
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:265
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition: opt.h:266
vf_amf_common.h
AMFFilterContext
Definition: vf_amf_common.h:29
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
hwcontext_amf.h
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:238
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
amf_filter_inputs
static const AVFilterPad amf_filter_inputs[]
Definition: vf_vqe_amf.c:141
AMFVQEFilterContext
Definition: vf_vqe_amf.c:46
NULL
#define NULL
Definition: coverity.c:32
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:281
avfilter_internal.h
AMF_IFACE_CALL
#define AMF_IFACE_CALL(this, function,...)
Definition: hwcontext_amf_internal.h:43
AV_PIX_FMT_X2BGR10
#define AV_PIX_FMT_X2BGR10
Definition: pixfmt.h:620
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:608
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:88
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:187
AVAMFDeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_amf.h:35
amf_filter_filter_frame
int amf_filter_filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_amf_common.c:80
amf_filter_uninit
void amf_filter_uninit(AVFilterContext *avctx)
Definition: vf_amf_common.c:58
AVAMFDeviceContext::factory
AMFFactory * factory
Definition: hwcontext_amf.h:37
uninit
static av_cold void uninit(AVBitStreamFilterContext *ctx)
Definition: lcevc_merge.c:135
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:46
vqe_amf_options
static const AVOption vqe_amf_options[]
Definition: vf_vqe_amf.c:127
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
AMFFilterContext::component
AMFComponent * component
Definition: vf_amf_common.h:60
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:264
hwcontext_amf_internal.h
amf_filter_query_formats
static int amf_filter_query_formats(AVFilterContext *avctx)
Definition: vf_vqe_amf.c:61
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:258
avfilter.h
amf_init_filter_config
int amf_init_filter_config(AVFilterLink *outlink, enum AVPixelFormat *in_format)
Definition: vf_amf_common.c:268
AVERROR_FILTER_NOT_FOUND
#define AVERROR_FILTER_NOT_FOUND
Filter not found.
Definition: error.h:60
AVFilterContext
An instance of a filter.
Definition: avfilter.h:273
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:608
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:271
AV_PIX_FMT_RGBAF16
#define AV_PIX_FMT_RGBAF16
Definition: pixfmt.h:630
AMFFilterContext::amf_device_ctx
AVAMFDeviceContext * amf_device_ctx
Definition: vf_amf_common.h:67
AMFVQEFilterContext::common
AMFFilterContext common
Definition: vf_vqe_amf.c:47
hwcontext.h
amf_vqe_filter_config_output
static int amf_vqe_filter_config_output(AVFilterLink *outlink)
Definition: vf_vqe_amf.c:89
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:298
ff_vf_vqe_amf
FFFilter ff_vf_vqe_amf
Definition: vf_vqe_amf.c:157