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  s->own_frame = av_frame_alloc();
57  if (!s->own_frame)
58  goto fail;
59 
60  s->tmp_frame = av_frame_alloc();
61  if (!s->tmp_frame)
62  goto fail;
63 
64  return 0;
65 
66 fail:
67  av_frame_free(&s->own_frame);
68  av_frame_free(&s->tmp_frame);
69  return AVERROR(ENOMEM);
70 }
71 
73 {
74  FilterLink *inl = ff_filter_link(ctx->inputs[0]);
75  FilterLink *outl = ff_filter_link(ctx->outputs[0]);
76  NPPSharpenContext* s = ctx->priv;
77  AVHWFramesContext *out_ctx, *in_ctx;
78  int i, ret, supported_format = 0;
79 
80  if (!inl->hw_frames_ctx) {
81  av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
82  goto fail;
83  }
84 
85  in_ctx = (AVHWFramesContext*)inl->hw_frames_ctx->data;
86 
87  s->frames_ctx = av_hwframe_ctx_alloc(in_ctx->device_ref);
88  if (!s->frames_ctx)
89  goto fail;
90 
91  out_ctx = (AVHWFramesContext*)s->frames_ctx->data;
92  out_ctx->format = AV_PIX_FMT_CUDA;
93  out_ctx->sw_format = in_ctx->sw_format;
94  out_ctx->width = FFALIGN(width, 32);
95  out_ctx->height = FFALIGN(height, 32);
96 
97  for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
98  if (in_ctx->sw_format == supported_formats[i]) {
99  supported_format = 1;
100  break;
101  }
102  }
103 
104  if (!supported_format) {
105  av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format\n");
106  goto fail;
107  }
108 
109  ret = av_hwframe_ctx_init(s->frames_ctx);
110  if (ret < 0)
111  goto fail;
112 
113  ret = av_hwframe_get_buffer(s->frames_ctx, s->own_frame, 0);
114  if (ret < 0)
115  goto fail;
116 
117  outl->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
118  if (!outl->hw_frames_ctx)
119  goto fail;
120 
121  return 0;
122 
123 fail:
124  av_buffer_unref(&s->frames_ctx);
125  return AVERROR(ENOMEM);
126 }
127 
129 {
130  NPPSharpenContext* s = ctx->priv;
131 
132  av_buffer_unref(&s->frames_ctx);
133  av_frame_free(&s->own_frame);
134  av_frame_free(&s->tmp_frame);
135 }
136 
138 {
139  AVFilterLink* inlink = outlink->src->inputs[0];
140 
141  outlink->w = inlink->w;
142  outlink->h = inlink->h;
143 
144  if (inlink->sample_aspect_ratio.num)
145  outlink->sample_aspect_ratio = av_mul_q(
146  (AVRational){outlink->h * inlink->w, outlink->w * inlink->h},
147  inlink->sample_aspect_ratio);
148  else
149  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
150 
151  nppsharpen_config(outlink->src, inlink->w, inlink->h);
152 
153  return 0;
154 }
155 
157 {
158  FilterLink *inl = ff_filter_link(ctx->inputs[0]);
160  NPPSharpenContext* s = ctx->priv;
161 
163 
164  for (int i = 0; i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
165  int ow = AV_CEIL_RSHIFT(in->width, (i == 1 || i == 2) ? desc->log2_chroma_w : 0);
166  int oh = AV_CEIL_RSHIFT(in->height, (i == 1 || i == 2) ? desc->log2_chroma_h : 0);
167 
168  NppStatus err = nppiFilterSharpenBorder_8u_C1R(
169  in->data[i], in->linesize[i], (NppiSize){ow, oh}, (NppiPoint){0, 0},
170  out->data[i], out->linesize[i], (NppiSize){ow, oh}, s->border_type);
171  if (err != NPP_SUCCESS) {
172  av_log(ctx, AV_LOG_ERROR, "NPP sharpen error: %d\n", err);
173  return AVERROR_EXTERNAL;
174  }
175  }
176 
177  return 0;
178 }
179 
181 {
182  AVFilterContext* ctx = link->dst;
183  NPPSharpenContext* s = ctx->priv;
184  AVFilterLink* outlink = ctx->outputs[0];
185  FilterLink *outl = ff_filter_link(outlink);
186  AVHWFramesContext* frames_ctx =
188  AVCUDADeviceContext* device_hwctx = frames_ctx->device_ctx->hwctx;
189 
190  AVFrame* out = NULL;
191  CUcontext dummy;
192  int ret = 0;
193 
194  out = av_frame_alloc();
195  if (!out) {
196  ret = AVERROR(ENOMEM);
197  goto fail;
198  }
199 
200  ret = CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(
201  device_hwctx->cuda_ctx));
202  if (ret < 0)
203  goto fail;
204 
205  ret = nppsharpen_sharpen(ctx, s->own_frame, in);
206  if (ret < 0)
207  goto pop_ctx;
208 
209  ret = av_hwframe_get_buffer(s->own_frame->hw_frames_ctx, s->tmp_frame, 0);
210  if (ret < 0)
211  goto pop_ctx;
212 
213  av_frame_move_ref(out, s->own_frame);
214  av_frame_move_ref(s->own_frame, s->tmp_frame);
215 
216  ret = av_frame_copy_props(out, in);
217  if (ret < 0)
218  goto pop_ctx;
219 
220  av_frame_free(&in);
221 
222 pop_ctx:
223  CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy));
224  if (!ret)
225  return ff_filter_frame(outlink, out);
226 fail:
227  av_frame_free(&in);
228  av_frame_free(&out);
229  return ret;
230 }
231 
232 #define OFFSET(x) offsetof(NPPSharpenContext, x)
233 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
234 static const AVOption options[] = {
235  { "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" },
236  { "replicate", "replicate pixels", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_BORDER_REPLICATE }, 0, 0, FLAGS, .unit = "border_type" },
237  {NULL},
238 };
239 
240 static const AVClass nppsharpen_class = {
241  .class_name = "nppsharpen",
242  .item_name = av_default_item_name,
243  .option = options,
244  .version = LIBAVUTIL_VERSION_INT,
245 };
246 
247 static const AVFilterPad nppsharpen_inputs[] = {{
248  .name = "default",
249  .type = AVMEDIA_TYPE_VIDEO,
250  .filter_frame = nppsharpen_filter_frame,
251 }};
252 
253 static const AVFilterPad nppsharpen_outputs[] = {{
254  .name = "default",
255  .type = AVMEDIA_TYPE_VIDEO,
256  .config_props = nppsharpen_config_props,
257 }};
258 
260  .name = "sharpen_npp",
261  .description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video "
262  "sharpening filter."),
263 
264  .init = nppsharpen_init,
265  .uninit = nppsharpen_uninit,
266 
267  .priv_size = sizeof(NPPSharpenContext),
268  .priv_class = &nppsharpen_class,
269 
273 
274  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
275 };
AVHWDeviceContext::hwctx
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
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:234
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1023
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
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:197
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:162
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:322
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
AVFrame::width
int width
Definition: frame.h:461
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:248
AVOption
AVOption.
Definition: opt.h:429
FLAGS
#define FLAGS
Definition: vf_sharpen_npp.c:233
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:205
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:217
NPPSharpenContext::border_type
NppiBorderType border_type
Definition: vf_sharpen_npp.c:49
nppsharpen_inputs
static const AVFilterPad nppsharpen_inputs[]
Definition: vf_sharpen_npp.c:247
nppsharpen_uninit
static void nppsharpen_uninit(AVFilterContext *ctx)
Definition: vf_sharpen_npp.c:128
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
NPPSharpenContext::own_frame
AVFrame * own_frame
Definition: vf_sharpen_npp.c:46
OFFSET
#define OFFSET(x)
Definition: vf_sharpen_npp.c:232
fail
#define fail()
Definition: checkasm.h:188
nppsharpen_filter_frame
static int nppsharpen_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_sharpen_npp.c:180
dummy
int dummy
Definition: motion.c:66
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
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:180
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:217
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
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:263
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:66
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:210
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:713
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:126
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:465
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
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
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:85
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:72
nppsharpen_config_props
static int nppsharpen_config_props(AVFilterLink *outlink)
Definition: vf_sharpen_npp.c:137
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:637
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
nppsharpen_outputs
static const AVFilterPad nppsharpen_outputs[]
Definition: vf_sharpen_npp.c:253
AVFilter
Filter definition.
Definition: avfilter.h:201
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
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:71
AVHWFramesContext::device_ctx
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:134
cuda_check.h
AVFrame::height
int height
Definition: frame.h:461
nppsharpen_class
static const AVClass nppsharpen_class
Definition: vf_sharpen_npp.c:240
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:156
ff_vf_sharpen_npp
const AVFilter ff_vf_sharpen_npp
Definition: vf_sharpen_npp.c:259
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:457
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
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:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
supported_format
#define supported_format(d)
width
#define width
Definition: dsp.h:85
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:491
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
NPPSharpenContext::frames_ctx
AVBufferRef * frames_ctx
Definition: vf_sharpen_npp.c:45