FFmpeg
Loading...
Searching...
No Matches
vf_sr_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 * Super resolution video filter with AMF hardware acceleration
22 */
23
24#include <stdio.h>
25#include <string.h>
26
27#include "libavutil/avassert.h"
28#include "libavutil/imgutils.h"
29#include "libavutil/opt.h"
30#include "libavutil/pixdesc.h"
31#include "libavutil/time.h"
32
33#include "libavutil/hwcontext.h"
36
37#include "AMF/components/HQScaler.h"
38#include "AMF/components/ColorSpace.h"
39#include "vf_amf_common.h"
40
41#include "avfilter.h"
42#include "avfilter_internal.h"
43#include "formats.h"
44#include "video.h"
45
46#if CONFIG_DXVA2
47#include <d3d9.h>
48#endif
49
50#if CONFIG_D3D11VA
51#include <d3d11.h>
52#endif
53
54
56{
57 const enum AVPixelFormat *output_pix_fmts;
58 static const enum AVPixelFormat input_pix_fmts[] = {
66 };
67 static const enum AVPixelFormat output_pix_fmts_default[] = {
77 };
78 output_pix_fmts = output_pix_fmts_default;
79
80 return amf_setup_input_output_formats(avctx, input_pix_fmts, output_pix_fmts);
81}
82
84{
85 AVFilterContext *avctx = outlink->src;
86 AVFilterLink *inlink = avctx->inputs[0];
87 AMFFilterContext *ctx = avctx->priv;
88 AMFSize out_size;
89 int err;
90 AMF_RESULT res;
91 enum AVPixelFormat in_format;
92
93 err = amf_init_filter_config(outlink, &in_format);
94 if (err < 0)
95 return err;
96
97 // HQ scaler should be used for upscaling only
98 if (inlink->w > outlink->w || inlink->h > outlink->h) {
99 av_log(avctx, AV_LOG_ERROR, "AMF HQ scaler should be used for upscaling only.\n");
100 return AVERROR_UNKNOWN;
101 }
102 // FIXME: add checks whether we have HW context
103 res = ctx->amf_device_ctx->factory->pVtbl->CreateComponent(ctx->amf_device_ctx->factory, ctx->amf_device_ctx->context, AMFHQScaler, &ctx->component);
104 AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_FILTER_NOT_FOUND, "CreateComponent(%ls) failed with error %d\n", AMFHQScaler, res);
105
106 out_size.width = outlink->w;
107 out_size.height = outlink->h;
108 AMF_ASSIGN_PROPERTY_SIZE(res, ctx->component, AMF_HQ_SCALER_OUTPUT_SIZE, out_size);
109 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFHQScaler-SetProperty() failed with error %d\n", res);
110
111 if (ctx->algorithm != -1) {
112 AMF_ASSIGN_PROPERTY_INT64(res, ctx->component, AMF_HQ_SCALER_ALGORITHM, ctx->algorithm);
113 }
114 if (ctx->sharpness != -1) {
115 AMF_ASSIGN_PROPERTY_DOUBLE(res, ctx->component, AMF_HQ_SCALER_SHARPNESS, ctx->sharpness);
116 }
117 AMF_ASSIGN_PROPERTY_BOOL(res, ctx->component, AMF_HQ_SCALER_FILL, ctx->fill);
118 AMF_ASSIGN_PROPERTY_BOOL(res, ctx->component, AMF_HQ_SCALER_KEEP_ASPECT_RATIO, ctx->keep_ratio);
119 // Setup default options to skip color conversion
120 ctx->in_color_range = AMF_COLOR_RANGE_UNDEFINED;
121 ctx->in_primaries = AMF_COLOR_PRIMARIES_UNDEFINED;
122 ctx->in_trc = AMF_COLOR_TRANSFER_CHARACTERISTIC_UNDEFINED;
123 ctx->color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
124 ctx->out_color_range = AMF_COLOR_RANGE_UNDEFINED;
125 ctx->out_primaries = AMF_COLOR_PRIMARIES_UNDEFINED;
126 ctx->out_trc = AMF_COLOR_TRANSFER_CHARACTERISTIC_UNDEFINED;
127
128 res = ctx->component->pVtbl->Init(ctx->component, av_av_to_amf_format(in_format), inlink->w, inlink->h);
129 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFHQScaler-Init() failed with error %d\n", res);
130
131 return 0;
132}
133
134#define OFFSET(x) offsetof(AMFFilterContext, x)
135#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
136static const AVOption sr_amf_options[] = {
137 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
138 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
139
140 { "format", "Output pixel format", OFFSET(format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
141 { "sharpness", "Sharpness", OFFSET(sharpness), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, 2., FLAGS, "sharpness" },
142 { "keep-ratio", "Keep aspect ratio", OFFSET(keep_ratio), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS, "keep_ration" },
143 { "fill", "Fill", OFFSET(fill), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS, "fill" },
144
145 { "algorithm", "Scaling algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_1, FLAGS, "algorithm" },
146 { "bilinear", "Bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_BILINEAR }, 0, 0, FLAGS, "algorithm" },
147 { "bicubic", "Bicubic", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_BICUBIC }, 0, 0, FLAGS, "algorithm" },
148 { "sr1-0", "Video SR1.0", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_0 }, 0, 0, FLAGS, "algorithm" },
149 { "point", "Point", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_POINT }, 0, 0, FLAGS, "algorithm" },
150 { "sr1-1", "Video SR1.1", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_1 }, 0, 0, FLAGS, "algorithm" },
151
152 { NULL },
153};
154
155
157
159 {
160 .name = "default",
161 .type = AVMEDIA_TYPE_VIDEO,
162 .filter_frame = amf_filter_filter_frame,
163 }
164};
165
167 {
168 .name = "default",
169 .type = AVMEDIA_TYPE_VIDEO,
170 .config_props = amf_filter_config_output,
171 }
172};
173
175 .p.name = "sr_amf",
176 .p.description = NULL_IF_CONFIG_SMALL("AMF HQ video upscaling"),
177 .p.priv_class = &sr_amf_class,
178 .p.flags = AVFILTER_FLAG_HWDEVICE,
179 .priv_size = sizeof(AMFFilterContext),
180
186 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
187};
const FFFilter ff_vf_sr_amf
Definition vf_sr_amf.c:174
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value,...)
Error handling helper.
Definition amfenc.h:169
simple assert() macros that are a bit more flexible than ISO C assert().
Main libavfilter public API header.
#define FLAGS
Definition cmdutils.c:598
#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_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#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 AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt)
misc image utilities
static av_cold void uninit(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:88
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
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,...)
static int out_size
Definition movenc.c:56
static AVFormatContext * ctx
Definition movenc.c:49
int amf_filter_init(AVFilterContext *avctx)
int amf_init_filter_config(AVFilterLink *outlink, enum AVPixelFormat *in_format)
int amf_filter_filter_frame(AVFilterLink *inlink, AVFrame *in)
int amf_setup_input_output_formats(AVFilterContext *avctx, const enum AVPixelFormat *input_pix_fmts, const enum AVPixelFormat *output_pix_fmts)
void amf_filter_uninit(AVFilterContext *avctx)
static const AVFilterPad amf_filter_inputs[]
Definition vf_frc_amf.c:264
static int amf_filter_query_formats(AVFilterContext *avctx)
Definition vf_frc_amf.c:68
static const AVFilterPad amf_filter_outputs[]
Definition vf_frc_amf.c:273
static int amf_filter_config_output(AVFilterLink *outlink)
Definition vf_sr_amf.c:83
#define OFFSET(x)
Definition vf_sr_amf.c:134
static int amf_filter_query_formats(AVFilterContext *avctx)
Definition vf_sr_amf.c:55
static const AVOption sr_amf_options[]
Definition vf_sr_amf.c:136