FFmpeg
vf_sharpen_npp.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  * NPP sharpen video filter
22  */
23 
24 #include <nppi.h>
25 #include <nppi_filtering_functions.h>
26 
27 #include "filters.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/cuda_check.h"
30 #include "libavutil/hwcontext.h"
32 #include "libavutil/opt.h"
33 
34 
35 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, device_hwctx->internal->cuda_dl, x)
36 
37 static const enum AVPixelFormat supported_formats[] = {
40 };
41 
42 typedef struct NPPSharpenContext {
43  const AVClass* class;
44 
48 
49  NppiBorderType border_type;
51 
53 {
54  NPPSharpenContext* s = ctx->priv;
55 
56  av_log(ctx, AV_LOG_WARNING, "The libnpp based filters are deprecated.\n");
57 
58  s->own_frame = av_frame_alloc();
59  if (!s->own_frame)
60  goto fail;
61 
62  s->tmp_frame = av_frame_alloc();
63  if (!s->tmp_frame)
64  goto fail;
65 
66  return 0;
67 
68 fail:
69  av_frame_free(&s->own_frame);
70  av_frame_free(&s->tmp_frame);
71  return AVERROR(ENOMEM);
72 }
73 
75 {
76  FilterLink *inl = ff_filter_link(ctx->inputs[0]);
77  FilterLink *outl = ff_filter_link(ctx->outputs[0]);
78  NPPSharpenContext* s = ctx->priv;
79  AVHWFramesContext *out_ctx, *in_ctx;
80  int i, ret, supported_format = 0;
81 
82  if (!inl->hw_frames_ctx) {
83  av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
84  goto fail;
85  }
86 
87  in_ctx = (AVHWFramesContext*)inl->hw_frames_ctx->data;
88 
89  s->frames_ctx = av_hwframe_ctx_alloc(in_ctx->device_ref);
90  if (!s->frames_ctx)
91  goto fail;
92 
93  out_ctx = (AVHWFramesContext*)s->frames_ctx->data;
94  out_ctx->format = AV_PIX_FMT_CUDA;
95  out_ctx->sw_format = in_ctx->sw_format;
96  out_ctx->width = FFALIGN(width, 32);
97  out_ctx->height = FFALIGN(height, 32);
98 
99  for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
100  if (in_ctx->sw_format == supported_formats[i]) {
101  supported_format = 1;
102  break;
103  }
104  }
105 
106  if (!supported_format) {
107  av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
108  goto fail;
109  }
110 
111  ret = av_hwframe_ctx_init(s->frames_ctx);
112  if (ret < 0)
113  goto fail;
114 
115  ret = av_hwframe_get_buffer(s->frames_ctx, s->own_frame, 0);
116  if (ret < 0)
117  goto fail;
118 
119  outl->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
120  if (!outl->hw_frames_ctx)
121  goto fail;
122 
123  return 0;
124 
125 fail:
126  av_buffer_unref(&s->frames_ctx);
127  return AVERROR(ENOMEM);
128 }
129 
131 {
132  NPPSharpenContext* s = ctx->priv;
133 
134  av_buffer_unref(&s->frames_ctx);
135  av_frame_free(&s->own_frame);
136  av_frame_free(&s->tmp_frame);
137 }
138 
140 {
141  AVFilterLink* inlink = outlink->src->inputs[0];
142 
143  outlink->w = inlink->w;
144  outlink->h = inlink->h;
145 
146  if (inlink->sample_aspect_ratio.num)
147  outlink->sample_aspect_ratio = av_mul_q(
148  (AVRational){outlink->h * inlink->w, outlink->w * inlink->h},
149  inlink->sample_aspect_ratio);
150  else
151  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
152 
153  nppsharpen_config(outlink->src, inlink->w, inlink->h);
154 
155  return 0;
156 }
157 
159 {
160  FilterLink *inl = ff_filter_link(ctx->inputs[0]);
162  NPPSharpenContext* s = ctx->priv;
163 
165 
166  for (int i = 0; i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
167  int ow = AV_CEIL_RSHIFT(in->width, (i == 1 || i == 2) ? desc->log2_chroma_w : 0);
168  int oh = AV_CEIL_RSHIFT(in->height, (i == 1 || i == 2) ? desc->log2_chroma_h : 0);
169 
170  NppStatus err = nppiFilterSharpenBorder_8u_C1R(
171  in->data[i], in->linesize[i], (NppiSize){ow, oh}, (NppiPoint){0, 0},
172  out->data[i], out->linesize[i], (NppiSize){ow, oh}, s->border_type);
173  if (err != NPP_SUCCESS) {
174  av_log(ctx, AV_LOG_ERROR, "NPP sharpen error: %d\n", err);
175  return AVERROR_EXTERNAL;
176  }
177  }
178 
179  return 0;
180 }
181 
183 {
184  AVFilterContext* ctx = link->dst;
185  NPPSharpenContext* s = ctx->priv;
186  AVFilterLink* outlink = ctx->outputs[0];
187  FilterLink *outl = ff_filter_link(outlink);
188  AVHWFramesContext* frames_ctx =
190  AVCUDADeviceContext* device_hwctx = frames_ctx->device_ctx->hwctx;
191 
192  AVFrame* out = NULL;
193  CUcontext dummy;
194  int ret = 0;
195 
196  out = av_frame_alloc();
197  if (!out) {
198  ret = AVERROR(ENOMEM);
199  goto fail;
200  }
201 
202  ret = CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(
203  device_hwctx->cuda_ctx));
204  if (ret < 0)
205  goto fail;
206 
207  ret = nppsharpen_sharpen(ctx, s->own_frame, in);
208  if (ret < 0)
209  goto pop_ctx;
210 
211  ret = av_hwframe_get_buffer(s->own_frame->hw_frames_ctx, s->tmp_frame, 0);
212  if (ret < 0)
213  goto pop_ctx;
214 
215  av_frame_move_ref(out, s->own_frame);
216  av_frame_move_ref(s->own_frame, s->tmp_frame);
217 
218  ret = av_frame_copy_props(out, in);
219  if (ret < 0)
220  goto pop_ctx;
221 
222  av_frame_free(&in);
223 
224 pop_ctx:
225  CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy));
226  if (!ret)
227  return ff_filter_frame(outlink, out);
228 fail:
229  av_frame_free(&in);
230  av_frame_free(&out);
231  return ret;
232 }
233 
234 #define OFFSET(x) offsetof(NPPSharpenContext, x)
235 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
236 static const AVOption options[] = {
237  { "border_type", "Type of operation to be performed on image border", OFFSET(border_type), AV_OPT_TYPE_INT, { .i64 = NPP_BORDER_REPLICATE }, NPP_BORDER_REPLICATE, NPP_BORDER_REPLICATE, FLAGS, .unit = "border_type" },
238  { "replicate", "replicate pixels", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_BORDER_REPLICATE }, 0, 0, FLAGS, .unit = "border_type" },
239  {NULL},
240 };
241 
242 static const AVClass nppsharpen_class = {
243  .class_name = "nppsharpen",
244  .item_name = av_default_item_name,
245  .option = options,
246  .version = LIBAVUTIL_VERSION_INT,
247 };
248 
249 static const AVFilterPad nppsharpen_inputs[] = {{
250  .name = "default",
251  .type = AVMEDIA_TYPE_VIDEO,
252  .filter_frame = nppsharpen_filter_frame,
253 }};
254 
255 static const AVFilterPad nppsharpen_outputs[] = {{
256  .name = "default",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .config_props = nppsharpen_config_props,
259 }};
260 
262  .p.name = "sharpen_npp",
263  .p.description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video "
264  "sharpening filter."),
265  .p.priv_class = &nppsharpen_class,
266 
267  .init = nppsharpen_init,
268  .uninit = nppsharpen_uninit,
269 
270  .priv_size = sizeof(NPPSharpenContext),
271 
275 
276  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
277 };
AVHWDeviceContext::hwctx
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:88
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:260
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
hwcontext_cuda_internal.h
out
FILE * out
Definition: movenc.c:55
options
static const AVOption options[]
Definition: vf_sharpen_npp.c:236
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1067
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3447
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:200
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
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:337
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:263
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
pixdesc.h
AVFrame::width
int width
Definition: frame.h:499
av_hwframe_ctx_alloc
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:263
AVOption
AVOption.
Definition: opt.h:429
FLAGS
#define FLAGS
Definition: vf_sharpen_npp.c:235
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:220
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:220
NPPSharpenContext::border_type
NppiBorderType border_type
Definition: vf_sharpen_npp.c:49
nppsharpen_inputs
static const AVFilterPad nppsharpen_inputs[]
Definition: vf_sharpen_npp.c:249
nppsharpen_uninit
static void nppsharpen_uninit(AVFilterContext *ctx)
Definition: vf_sharpen_npp.c:130
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:448
NPPSharpenContext::own_frame
AVFrame * own_frame
Definition: vf_sharpen_npp.c:46
OFFSET
#define OFFSET(x)
Definition: vf_sharpen_npp.c:234
fail
#define fail()
Definition: checkasm.h:206
nppsharpen_filter_frame
static int nppsharpen_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_sharpen_npp.c:182
dummy
int dummy
Definition: motion.c:66
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:39
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
NPPSharpenContext::tmp_frame
AVFrame * tmp_frame
Definition: vf_sharpen_npp.c:47
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
nppsharpen_init
static int nppsharpen_init(AVFilterContext *ctx)
Definition: vf_sharpen_npp.c:52
AVHWFramesContext::height
int height
Definition: hwcontext.h:220
FFFilter
Definition: filters.h:266
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
ff_vf_sharpen_npp
const FFFilter ff_vf_sharpen_npp
Definition: vf_sharpen_npp.c:261
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:264
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
CHECK_CU
#define CHECK_CU(x)
Definition: vf_sharpen_npp.c:35
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:213
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:599
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVHWFramesContext::device_ref
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:129
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:282
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:241
options
Definition: swscale.c:43
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:198
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:207
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
height
#define height
Definition: dsp.h:89
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
supported_formats
static enum AVPixelFormat supported_formats[]
Definition: vf_sharpen_npp.c:37
NPPSharpenContext
Definition: vf_sharpen_npp.c:42
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
nppsharpen_config
static int nppsharpen_config(AVFilterContext *ctx, int width, int height)
Definition: vf_sharpen_npp.c:74
nppsharpen_config_props
static int nppsharpen_config_props(AVFilterLink *outlink)
Definition: vf_sharpen_npp.c:139
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:523
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:45
nppsharpen_outputs
static const AVFilterPad nppsharpen_outputs[]
Definition: vf_sharpen_npp.c:255
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:118
AVCUDADeviceContext
This struct is allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_cuda.h:42
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:81
AVHWFramesContext::device_ctx
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:137
cuda_check.h
AVFrame::height
int height
Definition: frame.h:499
nppsharpen_class
static const AVClass nppsharpen_class
Definition: vf_sharpen_npp.c:242
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
nppsharpen_sharpen
static int nppsharpen_sharpen(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
Definition: vf_sharpen_npp.c:158
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilterContext
An instance of a filter.
Definition: avfilter.h:274
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
FFFilter::p
AVFilter p
The public AVFilter.
Definition: filters.h:270
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
hwcontext.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:472
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
supported_format
#define supported_format(d)
width
#define width
Definition: dsp.h:89
av_hwframe_get_buffer
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:506
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:253
NPPSharpenContext::frames_ctx
AVBufferRef * frames_ctx
Definition: vf_sharpen_npp.c:45