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