FFmpeg
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->color_profile = AMF_VIDEO_CONVERTER_COLOR_PROFILE_UNKNOWN;
121  ctx->color_range = AMF_COLOR_RANGE_UNDEFINED;
122  ctx->primaries = AMF_COLOR_PRIMARIES_UNDEFINED;
123  ctx->trc = AMF_COLOR_TRANSFER_CHARACTERISTIC_UNDEFINED;
124 
125  res = ctx->component->pVtbl->Init(ctx->component, av_av_to_amf_format(in_format), inlink->w, inlink->h);
126  AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFHQScaler-Init() failed with error %d\n", res);
127 
128  return 0;
129 }
130 
131 #define OFFSET(x) offsetof(AMFFilterContext, x)
132 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
133 static const AVOption sr_amf_options[] = {
134  { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
135  { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
136 
137  { "format", "Output pixel format", OFFSET(format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
138  { "sharpness", "Sharpness", OFFSET(sharpness), AV_OPT_TYPE_FLOAT, { .dbl = -1 }, -1, 2., FLAGS, "sharpness" },
139  { "keep-ratio", "Keep aspect ratio", OFFSET(keep_ratio), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS, "keep_ration" },
140  { "fill", "Fill", OFFSET(fill), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS, "fill" },
141 
142  { "algorithm", "Scaling algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_1, FLAGS, "algorithm" },
143  { "bilinear", "Bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_BILINEAR }, 0, 0, FLAGS, "algorithm" },
144  { "bicubic", "Bicubic", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_BICUBIC }, 0, 0, FLAGS, "algorithm" },
145  { "sr1-0", "Video SR1.0", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_0 }, 0, 0, FLAGS, "algorithm" },
146  { "point", "Point", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_POINT }, 0, 0, FLAGS, "algorithm" },
147  { "sr1-1", "Video SR1.1", 0, AV_OPT_TYPE_CONST, { .i64 = AMF_HQ_SCALER_ALGORITHM_VIDEOSR1_1 }, 0, 0, FLAGS, "algorithm" },
148 
149  { NULL },
150 };
151 
152 
153 AVFILTER_DEFINE_CLASS(sr_amf);
154 
155 static const AVFilterPad amf_filter_inputs[] = {
156  {
157  .name = "default",
158  .type = AVMEDIA_TYPE_VIDEO,
159  .filter_frame = amf_filter_filter_frame,
160  }
161 };
162 
163 static const AVFilterPad amf_filter_outputs[] = {
164  {
165  .name = "default",
166  .type = AVMEDIA_TYPE_VIDEO,
167  .config_props = amf_filter_config_output,
168  }
169 };
170 
172  .p.name = "sr_amf",
173  .p.description = NULL_IF_CONFIG_SMALL("AMF HQ video upscaling"),
174  .p.priv_class = &sr_amf_class,
175  .p.flags = AVFILTER_FLAG_HWDEVICE,
176  .priv_size = sizeof(AMFFilterContext),
177 
184  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
185 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
opt.h
amf_filter_config_output
static int amf_filter_config_output(AVFilterLink *outlink)
Definition: vf_sr_amf.c:83
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
FLAGS
#define FLAGS
Definition: vf_sr_amf.c:132
out_size
int out_size
Definition: movenc.c:56
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
pixdesc.h
OFFSET
#define OFFSET(x)
Definition: vf_sr_amf.c:131
AVOption
AVOption.
Definition: opt.h:429
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:171
AMF_RETURN_IF_FALSE
#define AMF_RETURN_IF_FALSE(avctx, exp, ret_value,...)
Error handling helper.
Definition: amfenc.h:162
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:203
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
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:272
amf_filter_inputs
static const AVFilterPad amf_filter_inputs[]
Definition: vf_sr_amf.c:155
amf_filter_query_formats
static int amf_filter_query_formats(AVFilterContext *avctx)
Definition: vf_sr_amf.c:55
av_av_to_amf_format
enum AMF_SURFACE_FORMAT av_av_to_amf_format(enum AVPixelFormat fmt)
Definition: hwcontext_amf.c:116
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FFFilter
Definition: filters.h:265
AV_PIX_FMT_DXVA2_VLD
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition: pixfmt.h:134
vf_amf_common.h
AMFFilterContext
Definition: vf_amf_common.h:28
ctx
AVFormatContext * ctx
Definition: movenc.c:49
hwcontext_amf.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
NULL
#define NULL
Definition: coverity.c:32
sr_amf_options
static const AVOption sr_amf_options[]
Definition: vf_sr_amf.c:133
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:265
time.h
avfilter_internal.h
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:206
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:94
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:171
AV_PIX_FMT_D3D11
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:336
amf_filter_filter_frame
int amf_filter_filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_amf_common.c:74
amf_filter_uninit
void amf_filter_uninit(AVFilterContext *avctx)
Definition: vf_amf_common.c:58
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition: opt.h:271
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
amf_filter_init
int amf_filter_init(AVFilterContext *avctx)
Definition: vf_amf_common.c:41
amf_filter_outputs
static const AVFilterPad amf_filter_outputs[]
Definition: vf_sr_amf.c:163
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
hwcontext_amf_internal.h
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:259
avfilter.h
amf_init_filter_config
int amf_init_filter_config(AVFilterLink *outlink, enum AVPixelFormat *in_format)
Definition: vf_amf_common.c:259
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:257
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:568
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:269
ff_vf_sr_amf
FFFilter ff_vf_sr_amf
Definition: vf_sr_amf.c:171
AV_PIX_FMT_RGBAF16
#define AV_PIX_FMT_RGBAF16
Definition: pixfmt.h:590
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
imgutils.h
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_STRING
@ 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:276
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(sr_amf)
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:252
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:236