FFmpeg
Loading...
Searching...
No Matches
vf_bwdif_cuda.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019 Philip Langdale <philipl@overt.org>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/avassert.h"
22#include "libavutil/hwcontext.h"
25
26#include "filters.h"
27#include "yadif.h"
28
29#include "cuda/load_helper.h"
30
31extern const unsigned char ff_vf_bwdif_cuda_ptx_data[];
32extern const unsigned int ff_vf_bwdif_cuda_ptx_len;
33
48
49#define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
50#define ALIGN_UP(a, b) (((a) + (b) - 1) & ~((b) - 1))
51#define BLOCKX 32
52#define BLOCKY 16
53
54#define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
55
56static CUresult call_kernel(AVFilterContext *ctx, CUfunction func,
57 CUdeviceptr prev, CUdeviceptr cur, CUdeviceptr next,
58 CUarray_format format, int channels,
59 int src_width, // Width is pixels per channel
60 int src_height, // Height is pixels per channel
61 int src_pitch, // Pitch is bytes
62 CUdeviceptr dst,
63 int dst_width, // Width is pixels per channel
64 int dst_height, // Height is pixels per channel
65 int dst_pitch, // Pitch is pixels per channel
66 int parity, int tff, int clip_max)
67{
68 DeintCUDAContext *s = ctx->priv;
69 YADIFContext *y = &s->yadif;
70 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
71 CUtexObject tex_prev = 0, tex_cur = 0, tex_next = 0;
72 int is_field_end = y->current_field == YADIF_FIELD_END;
73 int ret;
74
75 void *args[] = { &dst, &tex_prev, &tex_cur, &tex_next,
76 &dst_width, &dst_height, &dst_pitch,
77 &src_width, &src_height, &parity, &tff,
78 &is_field_end, &clip_max };
79
80 CUDA_TEXTURE_DESC tex_desc = {
81 .filterMode = CU_TR_FILTER_MODE_POINT,
82 .flags = CU_TRSF_READ_AS_INTEGER,
83 };
84
85 CUDA_RESOURCE_DESC res_desc = {
86 .resType = CU_RESOURCE_TYPE_PITCH2D,
87 .res.pitch2D.format = format,
88 .res.pitch2D.numChannels = channels,
89 .res.pitch2D.width = src_width,
90 .res.pitch2D.height = src_height,
91 .res.pitch2D.pitchInBytes = src_pitch,
92 };
93
94 res_desc.res.pitch2D.devPtr = (CUdeviceptr)prev;
95 ret = CHECK_CU(cu->cuTexObjectCreate(&tex_prev, &res_desc, &tex_desc, NULL));
96 if (ret < 0)
97 goto exit;
98
99 res_desc.res.pitch2D.devPtr = (CUdeviceptr)cur;
100 ret = CHECK_CU(cu->cuTexObjectCreate(&tex_cur, &res_desc, &tex_desc, NULL));
101 if (ret < 0)
102 goto exit;
103
104 res_desc.res.pitch2D.devPtr = (CUdeviceptr)next;
105 ret = CHECK_CU(cu->cuTexObjectCreate(&tex_next, &res_desc, &tex_desc, NULL));
106 if (ret < 0)
107 goto exit;
108
109 ret = CHECK_CU(cu->cuLaunchKernel(func,
110 DIV_UP(dst_width, BLOCKX), DIV_UP(dst_height, BLOCKY), 1,
111 BLOCKX, BLOCKY, 1,
112 0, s->hwctx->stream, args, NULL));
113
114exit:
115 if (tex_prev)
116 CHECK_CU(cu->cuTexObjectDestroy(tex_prev));
117 if (tex_cur)
118 CHECK_CU(cu->cuTexObjectDestroy(tex_cur));
119 if (tex_next)
120 CHECK_CU(cu->cuTexObjectDestroy(tex_next));
121
122 return ret;
123}
124
126 int parity, int tff)
127{
128 DeintCUDAContext *s = ctx->priv;
129 YADIFContext *y = &s->yadif;
130 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
131 CUcontext dummy;
132 int i, ret;
133
134 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
135 if (ret < 0)
136 return;
137
138 for (i = 0; i < y->csp->nb_components; i++) {
139 CUfunction func;
140 CUarray_format format;
141 int pixel_size, channels, clip_max;
142 const AVComponentDescriptor *comp = &y->csp->comp[i];
143
144 if (comp->plane < i) {
145 // We process planes as a whole, so don't reprocess
146 // them for additional components
147 continue;
148 }
149
150 pixel_size = (comp->depth + comp->shift) / 8;
151 channels = comp->step / pixel_size;
152 if (pixel_size > 2 || channels > 2) {
153 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n", y->csp->name);
154 goto exit;
155 }
156 switch (pixel_size) {
157 case 1:
158 func = channels == 1 ? s->cu_func_uchar : s->cu_func_uchar2;
159 format = CU_AD_FORMAT_UNSIGNED_INT8;
160 break;
161 case 2:
162 func = channels == 1 ? s->cu_func_ushort : s->cu_func_ushort2;
163 format = CU_AD_FORMAT_UNSIGNED_INT16;
164 break;
165 default:
166 av_log(ctx, AV_LOG_ERROR, "Unsupported pixel format: %s\n", y->csp->name);
167 goto exit;
168 }
169
170 clip_max = (1 << (comp->depth + comp->shift)) - 1;
171
173 "Deinterlacing plane %d: pixel_size: %d channels: %d\n",
174 comp->plane, pixel_size, channels);
176 (CUdeviceptr)y->prev->data[i],
177 (CUdeviceptr)y->cur->data[i],
178 (CUdeviceptr)y->next->data[i],
180 AV_CEIL_RSHIFT(y->cur->width, i ? y->csp->log2_chroma_w : 0),
181 AV_CEIL_RSHIFT(y->cur->height, i ? y->csp->log2_chroma_h : 0),
182 y->cur->linesize[i],
183 (CUdeviceptr)dst->data[i],
184 AV_CEIL_RSHIFT(dst->width, i ? y->csp->log2_chroma_w : 0),
185 AV_CEIL_RSHIFT(dst->height, i ? y->csp->log2_chroma_h : 0),
186 dst->linesize[i] / comp->step,
187 parity, tff, clip_max);
188 }
189
190 if (y->current_field == YADIF_FIELD_END) {
192 }
193
194exit:
195 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
196 return;
197}
198
200{
201 CUcontext dummy;
202 DeintCUDAContext *s = ctx->priv;
203
204 if (s->hwctx && s->cu_module) {
205 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
206 CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
207 CHECK_CU(cu->cuModuleUnload(s->cu_module));
208 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
209 }
210
212
213 av_buffer_unref(&s->device_ref);
214 s->hwctx = NULL;
215 av_buffer_unref(&s->input_frames_ref);
216 s->input_frames = NULL;
217}
218
219static int config_input(AVFilterLink *inlink)
220{
221 FilterLink *l = ff_filter_link(inlink);
222 AVFilterContext *ctx = inlink->dst;
223 DeintCUDAContext *s = ctx->priv;
224
225 if (!l->hw_frames_ctx) {
226 av_log(ctx, AV_LOG_ERROR, "A hardware frames reference is "
227 "required to associate the processing device.\n");
228 return AVERROR(EINVAL);
229 }
230
231 s->input_frames_ref = av_buffer_ref(l->hw_frames_ctx);
232 if (!s->input_frames_ref) {
233 av_log(ctx, AV_LOG_ERROR, "A input frames reference create "
234 "failed.\n");
235 return AVERROR(ENOMEM);
236 }
237 s->input_frames = (AVHWFramesContext*)s->input_frames_ref->data;
238
239 return 0;
240}
241
243{
244 FilterLink *l = ff_filter_link(link);
245 AVHWFramesContext *output_frames;
246 AVFilterContext *ctx = link->src;
247 DeintCUDAContext *s = ctx->priv;
248 YADIFContext *y = &s->yadif;
249 CudaFunctions *cu;
250 int ret = 0;
251 CUcontext dummy;
252
253 av_assert0(s->input_frames);
254 s->device_ref = av_buffer_ref(s->input_frames->device_ref);
255 if (!s->device_ref) {
256 av_log(ctx, AV_LOG_ERROR, "A device reference create "
257 "failed.\n");
258 return AVERROR(ENOMEM);
259 }
260 s->hwctx = ((AVHWDeviceContext*)s->device_ref->data)->hwctx;
261 cu = s->hwctx->internal->cuda_dl;
262
263 l->hw_frames_ctx = av_hwframe_ctx_alloc(s->device_ref);
264 if (!l->hw_frames_ctx) {
265 av_log(ctx, AV_LOG_ERROR, "Failed to create HW frame context "
266 "for output.\n");
267 ret = AVERROR(ENOMEM);
268 goto exit;
269 }
270
271 output_frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
272
273 output_frames->format = AV_PIX_FMT_CUDA;
274 output_frames->sw_format = s->input_frames->sw_format;
275 output_frames->width = ctx->inputs[0]->w;
276 output_frames->height = ctx->inputs[0]->h;
277
278 output_frames->initial_pool_size = 4;
279
280 ret = ff_filter_init_hw_frames(ctx, link, 10);
281 if (ret < 0)
282 goto exit;
283
285 if (ret < 0) {
286 av_log(ctx, AV_LOG_ERROR, "Failed to initialise CUDA frame "
287 "context for output: %d\n", ret);
288 goto exit;
289 }
290
292 if (ret < 0)
293 goto exit;
294
295 y->csp = av_pix_fmt_desc_get(output_frames->sw_format);
296 y->filter = filter;
297
298 if (AV_CEIL_RSHIFT(link->w, y->csp->log2_chroma_w) < 3 || AV_CEIL_RSHIFT(link->h, y->csp->log2_chroma_h) < 3) {
299 av_log(ctx, AV_LOG_ERROR, "Video with planes less than 3 columns or lines is not supported\n");
300 ret = AVERROR(EINVAL);
301 goto exit;
302 }
303
304 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
305 if (ret < 0)
306 goto exit;
307
309 if (ret < 0)
310 goto exit;
311
312 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar, s->cu_module, "bwdif_uchar"));
313 if (ret < 0)
314 goto exit;
315
316 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar2, s->cu_module, "bwdif_uchar2"));
317 if (ret < 0)
318 goto exit;
319
320 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort, s->cu_module, "bwdif_ushort"));
321 if (ret < 0)
322 goto exit;
323
324 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort2, s->cu_module, "bwdif_ushort2"));
325 if (ret < 0)
326 goto exit;
327
328exit:
329 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
330
331 return ret;
332}
333
334static const AVClass bwdif_cuda_class = {
335 .class_name = "bwdif_cuda",
336 .item_name = av_default_item_name,
337 .option = ff_yadif_options,
338 .version = LIBAVUTIL_VERSION_INT,
339 .category = AV_CLASS_CATEGORY_FILTER,
340};
341
343 {
344 .name = "default",
345 .type = AVMEDIA_TYPE_VIDEO,
346 .filter_frame = ff_yadif_filter_frame,
347 .config_props = config_input,
348 },
349};
350
352 {
353 .name = "default",
354 .type = AVMEDIA_TYPE_VIDEO,
355 .request_frame = ff_yadif_request_frame,
356 .config_props = config_output,
357 },
358};
359
361 .p.name = "bwdif_cuda",
362 .p.description = NULL_IF_CONFIG_SMALL("Deinterlace CUDA frames"),
363 .p.priv_class = &bwdif_cuda_class,
365 .priv_size = sizeof(DeintCUDAContext),
370 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
371};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static int config_input(AVFilterLink *inlink)
static const char *const format[]
Definition af_aiir.c:444
const FFFilter ff_vf_bwdif_cuda
static AVFormatContext * ctx
channels
Definition aptx.h:31
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link, int default_pool_size)
Perform any additional setup required for hardware frames.
Definition avfilter.c:1668
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition eamad.c:79
static int dummy
Definition ffplay.c:3754
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
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
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:337
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:263
FFmpeg internal API for CUDA.
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition jacosubdec.c:66
#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
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
int ff_cuda_load_module(void *avctx, AVCUDADeviceContext *hwctx, CUmodule *cu_module, const unsigned char *data, const unsigned int length)
Loads a CUDA module and applies any decompression, if necessary.
Definition load_helper.c:34
@ AV_CLASS_CATEGORY_FILTER
Definition log.h:36
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
This struct is allocated as AVHWDeviceContext.hwctx.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
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:517
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int initial_pool_size
Initial size of the frame pool.
Definition hwcontext.h:190
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
const char * name
Definition pixdesc.h:70
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition pixdesc.h:105
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition pixdesc.h:71
AVBufferRef * device_ref
AVCUDADeviceContext * hwctx
AVHWFramesContext * input_frames
CUfunction cu_func_uchar2
CUfunction cu_func_ushort2
AVBufferRef * input_frames_ref
CUfunction cu_func_ushort
CUfunction cu_func_uchar
YADIFContext yadif
AVFrame * prev
Definition yadif.h:62
AVFrame * cur
Definition yadif.h:60
AVFrame * next
Definition yadif.h:61
void(* filter)(AVFilterContext *ctx, AVFrame *dstpic, int parity, int tff)
Definition yadif.h:65
int current_field
YADIFCurrentField.
Definition yadif.h:88
const AVPixFmtDescriptor * csp
Definition yadif.h:76
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
#define DIV_UP(a, b)
#define BLOCKX
#define BLOCKY
static const AVClass bwdif_cuda_class
static CUresult call_kernel(AVFilterContext *ctx, CUfunction func, CUdeviceptr prev, CUdeviceptr cur, CUdeviceptr next, CUarray_format format, int channels, int src_width, int src_height, int src_pitch, CUdeviceptr dst, int dst_width, int dst_height, int dst_pitch, int parity, int tff, int clip_max)
#define DIV_UP(a, b)
static av_cold void deint_cuda_uninit(AVFilterContext *ctx)
static int config_input(AVFilterLink *inlink)
const unsigned int ff_vf_bwdif_cuda_ptx_len
#define CHECK_CU(x)
static const AVFilterPad deint_cuda_inputs[]
const unsigned char ff_vf_bwdif_cuda_ptx_data[]
static int config_output(AVFilterLink *link)
static const AVFilterPad deint_cuda_outputs[]
mcdeint parity
Definition vf_mcdeint.c:293
@ YADIF_FIELD_NORMAL
A normal field in the middle of a sequence.
Definition yadif.h:48
@ YADIF_FIELD_END
The first or last field in a sequence.
Definition yadif.h:47
int ff_yadif_filter_frame(AVFilterLink *link, AVFrame *frame)
int ff_yadif_request_frame(AVFilterLink *link)
const AVOption ff_yadif_options[]
void ff_yadif_uninit(AVFilterContext *ctx)
int ff_yadif_config_output_common(AVFilterLink *outlink)