FFmpeg
Loading...
Searching...
No Matches
vf_frc_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 * Video Frame Rate Converter filter with AMF hardware acceleration
22 */
23
24#include "libavutil/internal.h"
25#include "libavutil/opt.h"
26
27#include "avfilter_internal.h"
28
29#include "vf_amf_common.h"
30#include "AMF/components/FRC.h"
32
33#if CONFIG_D3D11VA
34#include <d3d11.h>
35#endif
36
37#if CONFIG_D3D12VA
38#include <d3d12.h>
39#endif
40
41// TODO: move this elsewhere.
42#define AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, this, name, val) {\
43 AMF_ASSIGN_PROPERTY_INT64(res, this, name, val);\
44 if (res != AMF_OK)\
45 av_log(avctx, AV_LOG_WARNING, "AMFFRC->SetProperty(%ls, %d) failed with error %d\n", name, val, res);\
46}
47
59
60static int amf_frc_init(AVFilterContext *avctx) {
62
63 ctx->common.format = AV_PIX_FMT_NONE;
64 ctx->common.color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
65
66 return 0;
67}
68
86
88{
89 AVFilterContext *avctx = outlink->src;
90 AMFComponent *amf_filter = NULL;
91 AVFilterLink *inlink = avctx->inputs[0];
92 FilterLink *il = ff_filter_link(inlink);
93 FilterLink *ol = ff_filter_link(outlink);
94 AMFFRCFilterContext *frc_ctx = avctx->priv;
95 AMFFilterContext *amf_ctx = &frc_ctx->common;
96 AVAMFDeviceContext *device_ctx = NULL;
97
98 int err;
99 AMF_RESULT res;
100 enum AVPixelFormat in_format;
101
102 amf_ctx->format = amf_inlink_sw_format(inlink);
103
104 err = amf_init_filter_config(outlink, &in_format);
105 if (err < 0)
106 return err;
107
108 device_ctx = amf_ctx->amf_device_ctx;
109
110 res = AMF_IFACE_CALL(device_ctx->factory, CreateComponent, device_ctx->context, AMFFRC, &amf_ctx->component);
111 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_FILTER_NOT_FOUND, "CreateComponent(%ls) failed with error %d\n", AMFFRC, res);
112
113 amf_filter = amf_ctx->component;
114
115 outlink->time_base = inlink->time_base;
116 ol->frame_rate = il->frame_rate;
117 if (frc_ctx->enable) {
118 ol->frame_rate.num *= 2;
119 amf_ctx->outputs_per_input = 2;
120 }
121
122 // Possible bug: FRC must be initialized enabled to be toggleable on the fly after init.
123 AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, AMF_FRC_MODE, FRC_x2_PRESENT);
124
125 if (frc_ctx->engine_type != -1)
126 AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, AMF_FRC_ENGINE_TYPE, frc_ctx->engine_type);
127
128 AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, AMF_FRC_ENABLE_FALLBACK, frc_ctx->fallback);
129
130 AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, AMF_FRC_INDICATOR, frc_ctx->indicator);
131
132 AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, AMF_FRC_PROFILE, frc_ctx->profile);
133
134 AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, AMF_FRC_MV_SEARCH_MODE, frc_ctx->mv_search_mode);
135
136 AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, AMF_FRC_USE_FUTURE_FRAME, frc_ctx->use_future_frame);
137
138 res = AMF_IFACE_CALL(amf_filter, Init, av_av_to_amf_format(in_format), inlink->w, inlink->h);
139 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFFRC->Init() failed with error %d\n", res);
140
141 AMF_ASSIGN_PROPERTY_INT64(res, amf_filter, AMF_FRC_MODE, frc_ctx->enable ? FRC_x2_PRESENT : FRC_OFF);
142 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "Failed to %s FRC, error:%d\n", frc_ctx->enable ? "enable" : "disable", res);
143
144 return 0;
145}
146
147#define OFFSET(x) offsetof(AMFFRCFilterContext, x)
148#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
149static const AVOption frc_amf_options[] = {
150 { "engine_type", "Engine type", OFFSET(engine_type), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, FRC_ENGINE_DX11, .flags = FLAGS, "engine_type" },
151 { "dx11", "DirectX 11", 0, AV_OPT_TYPE_CONST, { .i64 = FRC_ENGINE_DX11 }, 0, 0, FLAGS, "engine_type" },
152 { "dx12", "DirectX 12", 0, AV_OPT_TYPE_CONST, { .i64 = FRC_ENGINE_DX12 }, 0, 0, FLAGS, "engine_type" },
153
154 { "enable", "Enable FRC", OFFSET(enable), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, .flags = FLAGS },
155
156 { "fallback_mode", "Fallback behavior in case of low interpolation confidence", OFFSET(fallback), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, .flags = FLAGS, "fallback_mode" },
157 { "duplicate", "Duplicate frame", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "fallback_mode" },
158 { "blend", "Blend two frames together", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "fallback_mode" },
159
160 { "indicator", "Show FRC indicator square in the top left corner of the video.", OFFSET(indicator), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = FLAGS },
161
162 { "profile", "Level of hierarchical motion search", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = FRC_PROFILE_HIGH }, FRC_PROFILE_LOW, FRC_PROFILE_SUPER, FLAGS, "profile" },
163 { "low", "Less levels of hierarchical motion search. "
164 "Only recommended for extremely low resolutions.", 0, AV_OPT_TYPE_CONST, { .i64 = FRC_PROFILE_LOW }, 0, 0, FLAGS, "profile" },
165 { "high", "Recommended for any resolution up to 1440p.", 0, AV_OPT_TYPE_CONST, { .i64 = FRC_PROFILE_HIGH }, 0, 0, FLAGS, "profile" },
166 { "super", "More levels of hierarchical motion search. "
167 "Recommended for resolutions 1440p or higher.", 0, AV_OPT_TYPE_CONST, { .i64 = FRC_PROFILE_SUPER }, 0, 0, FLAGS, "profile" },
168
169 { "mv_search_mode", "Performance mode of the motion search", OFFSET(mv_search_mode), AV_OPT_TYPE_INT, { .i64 = FRC_MV_SEARCH_NATIVE }, FRC_MV_SEARCH_NATIVE, FRC_MV_SEARCH_PERFORMANCE, FLAGS, "mv_search_mode" },
170 { "native", "Conduct motion search on the full resolution of source images.", 0, AV_OPT_TYPE_CONST, { .i64 = FRC_MV_SEARCH_NATIVE }, 0, 0, FLAGS, "mv_search_mode" },
171 { "performance", "Conduct motion search on the down scaled source images. "
172 "Recommended for APU or low end GPU for better performance.", 0, AV_OPT_TYPE_CONST, { .i64 = FRC_MV_SEARCH_PERFORMANCE }, 0, 0, FLAGS, "mv_search_mode" },
173
174 { "use_future_frame", "Enable dependency on future frame, improves quality for the cost of latency", OFFSET(use_future_frame), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, .flags = FLAGS },
175
176 { NULL },
177};
178
180
182 {
183 .name = "default",
184 .type = AVMEDIA_TYPE_VIDEO,
185 }
186};
187
188
190 {
191 .name = "default",
192 .type = AVMEDIA_TYPE_VIDEO,
193 .config_props = amf_frc_filter_config_output,
194 }
195};
196
198 .p.name = "frc_amf",
199 .p.description = NULL_IF_CONFIG_SMALL("AMF video Frame Rate Converter"),
200 .p.priv_class = &frc_amf_class,
201 .p.flags = AVFILTER_FLAG_HWDEVICE,
202 .priv_size = sizeof(AMFFRCFilterContext),
209 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
210};
const FFFilter ff_vf_frc_amf
Definition vf_frc_amf.c:197
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value,...)
Error handling helper.
Definition amfenc.h:169
static AVFormatContext * ctx
#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_BOOL
Underlying C type is int.
Definition opt.h:326
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:193
#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
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
int profile
Definition mxfenc.c:2299
AVOptions.
#define AV_PIX_FMT_RGBAF16
Definition pixfmt.h:630
#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_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ 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
#define AV_PIX_FMT_X2BGR10
Definition pixfmt.h:620
AMFFilterContext common
Definition vf_frc_amf.c:49
enum AVPixelFormat format
AVAMFDeviceContext * amf_device_ctx
AMFComponent * component
This struct is allocated as AVHWDeviceContext.hwctx.
AMFContext * context
AMFFactory * factory
An instance of a filter.
Definition avfilter.h:279
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:287
void * priv
private data for use by the filter
Definition avfilter.h:294
A filter pad used for either input or output.
Definition filters.h:40
AVOption.
Definition opt.h:428
int num
Numerator.
Definition rational.h:59
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 AVOption frc_amf_options[]
Definition vf_frc_amf.c:149
static const AVFilterPad amf_filter_inputs[]
Definition vf_frc_amf.c:181
static int amf_frc_filter_config_output(AVFilterLink *outlink)
Definition vf_frc_amf.c:87
static int amf_frc_init(AVFilterContext *avctx)
Definition vf_frc_amf.c:60
#define OFFSET(x)
Definition vf_frc_amf.c:147
static int amf_filter_query_formats(AVFilterContext *avctx)
Definition vf_frc_amf.c:69
#define AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, this, name, val)
Definition vf_frc_amf.c:42
static const AVFilterPad amf_filter_outputs[]
Definition vf_frc_amf.c:189