FFmpeg
Loading...
Searching...
No Matches
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/pixdesc.h"
27#include "libavutil/hwcontext.h"
30
31#include "AMF/components/VQEnhancer.h"
32#include "vf_amf_common.h"
33
34#include "avfilter.h"
35#include "avfilter_internal.h"
36#include "formats.h"
37#include "video.h"
38
39#if CONFIG_D3D11VA
40#include <d3d11.h>
41#endif
42
43#if CONFIG_D3D12VA
44#include <d3d12.h>
45#endif
46
53
54static int amf_vqe_init(AVFilterContext *avctx) {
56
57 ctx->common.format = AV_PIX_FMT_NONE;
58 ctx->common.color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
59 ctx->common.shader_input = 1;
60
61 return 0;
62}
63
65{
66 static const enum AVPixelFormat input_pix_fmts[] = {
73 };
74
75 return amf_setup_input_output_formats(avctx, input_pix_fmts);
76}
77
79{
80 AVFilterContext *avctx = outlink->src;
81 AMFComponent *amf_filter = NULL;
82 AVFilterLink *inlink = avctx->inputs[0];
83 AMFVQEFilterContext *vqe_ctx = avctx->priv;
84 AMFFilterContext *amf_ctx = &vqe_ctx->common;
85 AVAMFDeviceContext *device_ctx = NULL;
86 enum AMF_MEMORY_TYPE mem_type = AMF_MEMORY_UNKNOWN;
87
88 int err;
89 AMF_RESULT res;
90 enum AVPixelFormat in_format;
91
92 amf_ctx->format = amf_inlink_sw_format(inlink);
93
94 err = amf_init_filter_config(outlink, &in_format);
95 if (err < 0)
96 return err;
97
98 if (in_format != AV_PIX_FMT_NV12 && in_format != AV_PIX_FMT_P010) {
99 av_log(avctx, AV_LOG_ERROR, "The VQ enhancer only accepts nv12 and p010, got %s.\n",
100 av_get_pix_fmt_name(in_format));
101 return AVERROR(EINVAL);
102 }
103
104 device_ctx = amf_ctx->amf_device_ctx;
105
106 res = AMF_IFACE_CALL(device_ctx->factory, CreateComponent, device_ctx->context, AMFVQEnhancer, &amf_ctx->component);
107 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_FILTER_NOT_FOUND, "CreateComponent(%ls) failed with error %d\n", AMFVQEnhancer, res);
108
109 amf_filter = amf_ctx->component;
110
111 mem_type = av_amf_get_memory_type(device_ctx);
112 if (vqe_ctx->engine_type != -1) {
113 AMF_ASSIGN_PROPERTY_INT64(res, amf_filter, AMF_VIDEO_ENHANCER_ENGINE_TYPE, vqe_ctx->engine_type);
114 } else if (mem_type != AMF_MEMORY_UNKNOWN)
115 AMF_ASSIGN_PROPERTY_INT64(res, amf_filter, AMF_VIDEO_ENHANCER_ENGINE_TYPE, mem_type);
116
117 AMF_ASSIGN_PROPERTY_DOUBLE(res, amf_filter, AMF_VE_FCR_ATTENUATION, vqe_ctx->attenuation);
118 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "Failed to set VQ enhancer attenuation: %d\n", res);
119
120 res = AMF_IFACE_CALL(amf_filter, Init, av_av_to_amf_format(in_format), inlink->w, inlink->h);
121 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFVQEnhancer-Init() failed with error %d\n", res);
122
123 return 0;
124}
125
126#define OFFSET(x) offsetof(AMFVQEFilterContext, x)
127#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
128static const AVOption vqe_amf_options[] = {
129 { "engine_type", "Engine type", OFFSET(engine_type), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_MEMORY_DX12, .flags = FLAGS, "engine_type" },
130 { "dx11", "DirectX 11", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_MEMORY_DX11 }, 0, 0, FLAGS, "engine_type" },
131 { "vulkan", "Vulkan", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_MEMORY_VULKAN }, 0, 0, FLAGS, "engine_type" },
132 { "dx12", "DirectX 12", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_MEMORY_DX12 }, 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
140
142 {
143 .name = "default",
144 .type = AVMEDIA_TYPE_VIDEO,
145 }
146};
147
149 {
150 .name = "default",
151 .type = AVMEDIA_TYPE_VIDEO,
152 .config_props = amf_vqe_filter_config_output,
153 }
154};
155
157 .p.name = "vqe_amf",
158 .p.description = NULL_IF_CONFIG_SMALL("AMD AMF VQ Enhancer"),
159 .p.priv_class = &vqe_amf_class,
160 .p.flags = AVFILTER_FLAG_HWDEVICE,
161 .priv_size = sizeof(AMFVQEFilterContext),
168 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
169};
const FFFilter ff_vf_vqe_amf
Definition vf_vqe_amf.c:156
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value,...)
Error handling helper.
Definition amfenc.h:169
static AVFormatContext * ctx
Main libavfilter public API header.
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
#define AVERROR_FILTER_NOT_FOUND
Filter not found.
Definition error.h:60
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
enum AMF_MEMORY_TYPE av_amf_get_memory_type(AVAMFDeviceContext *amf_ctx)
enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt)
#define AMF_IFACE_CALL(this, function,...)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#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
#define FILTER_QUERY_FUNC(func)
Definition filters.h:238
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3380
#define AV_PIX_FMT_P010
Definition pixfmt.h:608
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ 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
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition pixfmt.h:134
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition pixfmt.h:336
@ AV_PIX_FMT_AMF_SURFACE
HW acceleration through AMF.
Definition pixfmt.h:477
static const float attenuation[10]
Definition speexdata.h:768
enum AVPixelFormat format
AVAMFDeviceContext * amf_device_ctx
AMFComponent * component
AMFFilterContext common
Definition vf_vqe_amf.c:48
This struct is allocated as AVHWDeviceContext.hwctx.
AMFContext * context
AMFFactory * factory
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
A filter pad used for either input or output.
Definition filters.h:40
AVOption.
Definition opt.h:428
#define av_log(a,...)
int amf_setup_input_output_formats(AVFilterContext *avctx, const enum AVPixelFormat *input_pix_fmts)
int amf_filter_activate(AVFilterContext *avctx)
int amf_init_filter_config(AVFilterLink *outlink, enum AVPixelFormat *in_format)
enum AVPixelFormat amf_inlink_sw_format(AVFilterLink *inlink)
void amf_filter_uninit(AVFilterContext *avctx)
static const AVFilterPad amf_filter_inputs[]
Definition vf_frc_amf.c:181
static int amf_filter_query_formats(AVFilterContext *avctx)
Definition vf_frc_amf.c:69
static const AVFilterPad amf_filter_outputs[]
Definition vf_frc_amf.c:189
static int amf_vqe_filter_config_output(AVFilterLink *outlink)
Definition vf_vqe_amf.c:78
#define OFFSET(x)
Definition vf_vqe_amf.c:126
static int amf_filter_query_formats(AVFilterContext *avctx)
Definition vf_vqe_amf.c:64
static const AVOption vqe_amf_options[]
Definition vf_vqe_amf.c:128
static int amf_vqe_init(AVFilterContext *avctx)
Definition vf_vqe_amf.c:54