]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale_cuda.c
avfilter/scale_cuda: code cleanup
[ffmpeg] / libavfilter / vf_scale_cuda.c
1 /*
2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "libavutil/avstring.h"
27 #include "libavutil/common.h"
28 #include "libavutil/hwcontext.h"
29 #include "libavutil/hwcontext_cuda_internal.h"
30 #include "libavutil/cuda_check.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34
35 #include "avfilter.h"
36 #include "formats.h"
37 #include "internal.h"
38 #include "scale_eval.h"
39 #include "video.h"
40
41 static const enum AVPixelFormat supported_formats[] = {
42     AV_PIX_FMT_YUV420P,
43     AV_PIX_FMT_NV12,
44     AV_PIX_FMT_YUV444P,
45     AV_PIX_FMT_P010,
46     AV_PIX_FMT_P016,
47     AV_PIX_FMT_YUV444P16,
48 };
49
50 #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
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
56 enum {
57     INTERP_ALGO_DEFAULT,
58
59     INTERP_ALGO_BILINEAR,
60     INTERP_ALGO_BICUBIC,
61
62     INTERP_ALGO_COUNT
63 };
64
65 typedef struct CUDAScaleContext {
66     const AVClass *class;
67
68     AVCUDADeviceContext *hwctx;
69
70     enum AVPixelFormat in_fmt;
71     enum AVPixelFormat out_fmt;
72
73     AVBufferRef *frames_ctx;
74     AVFrame     *frame;
75
76     AVFrame *tmp_frame;
77     int passthrough;
78
79     /**
80      * Output sw format. AV_PIX_FMT_NONE for no conversion.
81      */
82     enum AVPixelFormat format;
83
84     char *w_expr;               ///< width  expression string
85     char *h_expr;               ///< height expression string
86
87     int force_original_aspect_ratio;
88     int force_divisible_by;
89
90     CUcontext   cu_ctx;
91     CUmodule    cu_module;
92     CUfunction  cu_func_uchar;
93     CUfunction  cu_func_uchar2;
94     CUfunction  cu_func_uchar4;
95     CUfunction  cu_func_ushort;
96     CUfunction  cu_func_ushort2;
97     CUfunction  cu_func_ushort4;
98     CUstream    cu_stream;
99
100     CUdeviceptr srcBuffer;
101     CUdeviceptr dstBuffer;
102     int         tex_alignment;
103
104     int interp_algo;
105     int interp_use_linear;
106     int interp_as_integer;
107 } CUDAScaleContext;
108
109 static av_cold int cudascale_init(AVFilterContext *ctx)
110 {
111     CUDAScaleContext *s = ctx->priv;
112
113     s->format = AV_PIX_FMT_NONE;
114     s->frame = av_frame_alloc();
115     if (!s->frame)
116         return AVERROR(ENOMEM);
117
118     s->tmp_frame = av_frame_alloc();
119     if (!s->tmp_frame)
120         return AVERROR(ENOMEM);
121
122     return 0;
123 }
124
125 static av_cold void cudascale_uninit(AVFilterContext *ctx)
126 {
127     CUDAScaleContext *s = ctx->priv;
128
129     if (s->hwctx && s->cu_module) {
130         CudaFunctions *cu = s->hwctx->internal->cuda_dl;
131         CUcontext dummy;
132
133         CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
134         CHECK_CU(cu->cuModuleUnload(s->cu_module));
135         s->cu_module = NULL;
136         CHECK_CU(cu->cuCtxPopCurrent(&dummy));
137     }
138
139     av_frame_free(&s->frame);
140     av_buffer_unref(&s->frames_ctx);
141     av_frame_free(&s->tmp_frame);
142 }
143
144 static int cudascale_query_formats(AVFilterContext *ctx)
145 {
146     static const enum AVPixelFormat pixel_formats[] = {
147         AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
148     };
149     AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
150     if (!pix_fmts)
151         return AVERROR(ENOMEM);
152
153     return ff_set_common_formats(ctx, pix_fmts);
154 }
155
156 static av_cold int init_hwframe_ctx(CUDAScaleContext *s, AVBufferRef *device_ctx, int width, int height)
157 {
158     AVBufferRef *out_ref = NULL;
159     AVHWFramesContext *out_ctx;
160     int ret;
161
162     out_ref = av_hwframe_ctx_alloc(device_ctx);
163     if (!out_ref)
164         return AVERROR(ENOMEM);
165     out_ctx = (AVHWFramesContext*)out_ref->data;
166
167     out_ctx->format    = AV_PIX_FMT_CUDA;
168     out_ctx->sw_format = s->out_fmt;
169     out_ctx->width     = FFALIGN(width,  32);
170     out_ctx->height    = FFALIGN(height, 32);
171
172     ret = av_hwframe_ctx_init(out_ref);
173     if (ret < 0)
174         goto fail;
175
176     av_frame_unref(s->frame);
177     ret = av_hwframe_get_buffer(out_ref, s->frame, 0);
178     if (ret < 0)
179         goto fail;
180
181     s->frame->width  = width;
182     s->frame->height = height;
183
184     av_buffer_unref(&s->frames_ctx);
185     s->frames_ctx = out_ref;
186
187     return 0;
188 fail:
189     av_buffer_unref(&out_ref);
190     return ret;
191 }
192
193 static int format_is_supported(enum AVPixelFormat fmt)
194 {
195     int i;
196
197     for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
198         if (supported_formats[i] == fmt)
199             return 1;
200     return 0;
201 }
202
203 static av_cold int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
204                                          int out_width, int out_height)
205 {
206     CUDAScaleContext *s = ctx->priv;
207
208     AVHWFramesContext *in_frames_ctx;
209
210     enum AVPixelFormat in_format;
211     enum AVPixelFormat out_format;
212     int ret;
213
214     /* check that we have a hw context */
215     if (!ctx->inputs[0]->hw_frames_ctx) {
216         av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
217         return AVERROR(EINVAL);
218     }
219     in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
220     in_format     = in_frames_ctx->sw_format;
221     out_format    = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
222
223     if (!format_is_supported(in_format)) {
224         av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
225                av_get_pix_fmt_name(in_format));
226         return AVERROR(ENOSYS);
227     }
228     if (!format_is_supported(out_format)) {
229         av_log(ctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
230                av_get_pix_fmt_name(out_format));
231         return AVERROR(ENOSYS);
232     }
233
234     s->in_fmt = in_format;
235     s->out_fmt = out_format;
236
237     if (s->passthrough && in_width == out_width && in_height == out_height && in_format == out_format) {
238         s->frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
239         if (!s->frames_ctx)
240             return AVERROR(ENOMEM);
241     } else {
242         s->passthrough = 0;
243
244         ret = init_hwframe_ctx(s, in_frames_ctx->device_ref, out_width, out_height);
245         if (ret < 0)
246             return ret;
247     }
248
249     ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
250     if (!ctx->outputs[0]->hw_frames_ctx)
251         return AVERROR(ENOMEM);
252
253     return 0;
254 }
255
256 static av_cold int cudascale_config_props(AVFilterLink *outlink)
257 {
258     AVFilterContext *ctx = outlink->src;
259     AVFilterLink *inlink = outlink->src->inputs[0];
260     CUDAScaleContext *s  = ctx->priv;
261     AVHWFramesContext     *frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
262     AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
263     CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
264     CudaFunctions *cu = device_hwctx->internal->cuda_dl;
265     char buf[64];
266     int w, h;
267     int ret;
268
269     char *scaler_ptx;
270     const char *function_infix = "";
271
272     extern char vf_scale_cuda_ptx[];
273     extern char vf_scale_cuda_bicubic_ptx[];
274
275     switch(s->interp_algo) {
276     case INTERP_ALGO_BILINEAR:
277         scaler_ptx = vf_scale_cuda_ptx;
278         function_infix = "_Bilinear";
279         s->interp_use_linear = 1;
280         s->interp_as_integer = 1;
281         break;
282     case INTERP_ALGO_DEFAULT:
283     case INTERP_ALGO_BICUBIC:
284         scaler_ptx = vf_scale_cuda_bicubic_ptx;
285         function_infix = "_Bicubic";
286         s->interp_use_linear = 0;
287         s->interp_as_integer = 0;
288         break;
289     default:
290         av_log(ctx, AV_LOG_ERROR, "Unknown interpolation algorithm\n");
291         return AVERROR_BUG;
292     }
293
294     s->hwctx = device_hwctx;
295     s->cu_stream = s->hwctx->stream;
296
297     ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
298     if (ret < 0)
299         goto fail;
300
301     ret = CHECK_CU(cu->cuModuleLoadData(&s->cu_module, scaler_ptx));
302     if (ret < 0)
303         goto fail;
304
305     snprintf(buf, sizeof(buf), "Subsample%s_uchar", function_infix);
306     CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar, s->cu_module, buf));
307     if (ret < 0)
308         goto fail;
309
310     snprintf(buf, sizeof(buf), "Subsample%s_uchar2", function_infix);
311     CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar2, s->cu_module, buf));
312     if (ret < 0)
313         goto fail;
314
315     snprintf(buf, sizeof(buf), "Subsample%s_uchar4", function_infix);
316     CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar4, s->cu_module, buf));
317     if (ret < 0)
318         goto fail;
319
320     snprintf(buf, sizeof(buf), "Subsample%s_ushort", function_infix);
321     CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort, s->cu_module, buf));
322     if (ret < 0)
323         goto fail;
324
325     snprintf(buf, sizeof(buf), "Subsample%s_ushort2", function_infix);
326     CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort2, s->cu_module, buf));
327     if (ret < 0)
328         goto fail;
329
330     snprintf(buf, sizeof(buf), "Subsample%s_ushort4", function_infix);
331     CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort4, s->cu_module, buf));
332     if (ret < 0)
333         goto fail;
334
335
336     CHECK_CU(cu->cuCtxPopCurrent(&dummy));
337
338     if ((ret = ff_scale_eval_dimensions(s,
339                                         s->w_expr, s->h_expr,
340                                         inlink, outlink,
341                                         &w, &h)) < 0)
342         goto fail;
343
344     ff_scale_adjust_dimensions(inlink, &w, &h,
345                                s->force_original_aspect_ratio, s->force_divisible_by);
346
347     if (((int64_t)h * inlink->w) > INT_MAX  ||
348         ((int64_t)w * inlink->h) > INT_MAX)
349         av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
350
351     outlink->w = w;
352     outlink->h = h;
353
354     ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
355     if (ret < 0)
356         return ret;
357
358     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d%s\n",
359            inlink->w, inlink->h, outlink->w, outlink->h, s->passthrough ? " (passthrough)" : "");
360
361     if (inlink->sample_aspect_ratio.num) {
362         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
363                                                              outlink->w*inlink->h},
364                                                 inlink->sample_aspect_ratio);
365     } else {
366         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
367     }
368
369     return 0;
370
371 fail:
372     return ret;
373 }
374
375 static int call_resize_kernel(AVFilterContext *ctx, CUfunction func, int channels,
376                               uint8_t *src_dptr, int src_width, int src_height, int src_pitch,
377                               uint8_t *dst_dptr, int dst_width, int dst_height, int dst_pitch,
378                               int pixel_size, int bit_depth)
379 {
380     CUDAScaleContext *s = ctx->priv;
381     CudaFunctions *cu = s->hwctx->internal->cuda_dl;
382     CUdeviceptr dst_devptr = (CUdeviceptr)dst_dptr;
383     CUtexObject tex = 0;
384     void *args_uchar[] = { &tex, &dst_devptr, &dst_width, &dst_height, &dst_pitch, &src_width, &src_height, &bit_depth };
385     int ret;
386
387     CUDA_TEXTURE_DESC tex_desc = {
388         .filterMode = s->interp_use_linear ?
389                       CU_TR_FILTER_MODE_LINEAR :
390                       CU_TR_FILTER_MODE_POINT,
391         .flags = s->interp_as_integer ? CU_TRSF_READ_AS_INTEGER : 0,
392     };
393
394     CUDA_RESOURCE_DESC res_desc = {
395         .resType = CU_RESOURCE_TYPE_PITCH2D,
396         .res.pitch2D.format = pixel_size == 1 ?
397                               CU_AD_FORMAT_UNSIGNED_INT8 :
398                               CU_AD_FORMAT_UNSIGNED_INT16,
399         .res.pitch2D.numChannels = channels,
400         .res.pitch2D.width = src_width,
401         .res.pitch2D.height = src_height,
402         .res.pitch2D.pitchInBytes = src_pitch * pixel_size,
403         .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
404     };
405
406     ret = CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc, NULL));
407     if (ret < 0)
408         goto exit;
409
410     ret = CHECK_CU(cu->cuLaunchKernel(func,
411                                       DIV_UP(dst_width, BLOCKX), DIV_UP(dst_height, BLOCKY), 1,
412                                       BLOCKX, BLOCKY, 1, 0, s->cu_stream, args_uchar, NULL));
413
414 exit:
415     if (tex)
416         CHECK_CU(cu->cuTexObjectDestroy(tex));
417
418     return ret;
419 }
420
421 static int scalecuda_resize(AVFilterContext *ctx,
422                             AVFrame *out, AVFrame *in)
423 {
424     AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
425     CUDAScaleContext *s = ctx->priv;
426
427     switch (in_frames_ctx->sw_format) {
428     case AV_PIX_FMT_YUV420P:
429         call_resize_kernel(ctx, s->cu_func_uchar, 1,
430                            in->data[0], in->width, in->height, in->linesize[0],
431                            out->data[0], out->width, out->height, out->linesize[0],
432                            1, 8);
433         call_resize_kernel(ctx, s->cu_func_uchar, 1,
434                            in->data[1], in->width / 2, in->height / 2, in->linesize[1],
435                            out->data[1], out->width / 2, out->height / 2, out->linesize[1],
436                            1, 8);
437         call_resize_kernel(ctx, s->cu_func_uchar, 1,
438                            in->data[2], in->width / 2, in->height / 2, in->linesize[2],
439                            out->data[2], out->width / 2, out->height / 2, out->linesize[2],
440                            1, 8);
441         break;
442     case AV_PIX_FMT_YUV444P:
443         call_resize_kernel(ctx, s->cu_func_uchar, 1,
444                            in->data[0], in->width, in->height, in->linesize[0],
445                            out->data[0], out->width, out->height, out->linesize[0],
446                            1, 8);
447         call_resize_kernel(ctx, s->cu_func_uchar, 1,
448                            in->data[1], in->width, in->height, in->linesize[1],
449                            out->data[1], out->width, out->height, out->linesize[1],
450                            1, 8);
451         call_resize_kernel(ctx, s->cu_func_uchar, 1,
452                            in->data[2], in->width, in->height, in->linesize[2],
453                            out->data[2], out->width, out->height, out->linesize[2],
454                            1, 8);
455         break;
456     case AV_PIX_FMT_YUV444P16:
457         call_resize_kernel(ctx, s->cu_func_ushort, 1,
458                            in->data[0], in->width, in->height, in->linesize[0] / 2,
459                            out->data[0], out->width, out->height, out->linesize[0] / 2,
460                            2, 16);
461         call_resize_kernel(ctx, s->cu_func_ushort, 1,
462                            in->data[1], in->width, in->height, in->linesize[1] / 2,
463                            out->data[1], out->width, out->height, out->linesize[1] / 2,
464                            2, 16);
465         call_resize_kernel(ctx, s->cu_func_ushort, 1,
466                            in->data[2], in->width, in->height, in->linesize[2] / 2,
467                            out->data[2], out->width, out->height, out->linesize[2] / 2,
468                            2, 16);
469         break;
470     case AV_PIX_FMT_NV12:
471         call_resize_kernel(ctx, s->cu_func_uchar, 1,
472                            in->data[0], in->width, in->height, in->linesize[0],
473                            out->data[0], out->width, out->height, out->linesize[0],
474                            1, 8);
475         call_resize_kernel(ctx, s->cu_func_uchar2, 2,
476                            in->data[1], in->width / 2, in->height / 2, in->linesize[1],
477                            out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 2,
478                            1, 8);
479         break;
480     case AV_PIX_FMT_P010LE:
481         call_resize_kernel(ctx, s->cu_func_ushort, 1,
482                            in->data[0], in->width, in->height, in->linesize[0] / 2,
483                            out->data[0], out->width, out->height, out->linesize[0] / 2,
484                            2, 10);
485         call_resize_kernel(ctx, s->cu_func_ushort2, 2,
486                            in->data[1], in->width / 2, in->height / 2, in->linesize[1] / 2,
487                            out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
488                            2, 10);
489         break;
490     case AV_PIX_FMT_P016LE:
491         call_resize_kernel(ctx, s->cu_func_ushort, 1,
492                            in->data[0], in->width, in->height, in->linesize[0] / 2,
493                            out->data[0], out->width, out->height, out->linesize[0] / 2,
494                            2, 16);
495         call_resize_kernel(ctx, s->cu_func_ushort2, 2,
496                            in->data[1], in->width / 2, in->height / 2, in->linesize[1] / 2,
497                            out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
498                            2, 16);
499         break;
500     default:
501         return AVERROR_BUG;
502     }
503
504     return 0;
505 }
506
507 static int cudascale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
508 {
509     CUDAScaleContext *s = ctx->priv;
510     AVFilterLink *outlink = ctx->outputs[0];
511     AVFrame *src = in;
512     int ret;
513
514     ret = scalecuda_resize(ctx, s->frame, src);
515     if (ret < 0)
516         return ret;
517
518     src = s->frame;
519     ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
520     if (ret < 0)
521         return ret;
522
523     av_frame_move_ref(out, s->frame);
524     av_frame_move_ref(s->frame, s->tmp_frame);
525
526     s->frame->width  = outlink->w;
527     s->frame->height = outlink->h;
528
529     ret = av_frame_copy_props(out, in);
530     if (ret < 0)
531         return ret;
532
533     return 0;
534 }
535
536 static int cudascale_filter_frame(AVFilterLink *link, AVFrame *in)
537 {
538     AVFilterContext       *ctx = link->dst;
539     CUDAScaleContext        *s = ctx->priv;
540     AVFilterLink      *outlink = ctx->outputs[0];
541     CudaFunctions          *cu = s->hwctx->internal->cuda_dl;
542
543     AVFrame *out = NULL;
544     CUcontext dummy;
545     int ret = 0;
546
547     if (s->passthrough)
548         return ff_filter_frame(outlink, in);
549
550     out = av_frame_alloc();
551     if (!out) {
552         ret = AVERROR(ENOMEM);
553         goto fail;
554     }
555
556     ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
557     if (ret < 0)
558         goto fail;
559
560     ret = cudascale_scale(ctx, out, in);
561
562     CHECK_CU(cu->cuCtxPopCurrent(&dummy));
563     if (ret < 0)
564         goto fail;
565
566     av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
567               (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
568               (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
569               INT_MAX);
570
571     av_frame_free(&in);
572     return ff_filter_frame(outlink, out);
573 fail:
574     av_frame_free(&in);
575     av_frame_free(&out);
576     return ret;
577 }
578
579 static AVFrame *cudascale_get_video_buffer(AVFilterLink *inlink, int w, int h)
580 {
581     CUDAScaleContext *s = inlink->dst->priv;
582
583     return s->passthrough ?
584         ff_null_get_video_buffer   (inlink, w, h) :
585         ff_default_get_video_buffer(inlink, w, h);
586 }
587
588 #define OFFSET(x) offsetof(CUDAScaleContext, x)
589 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
590 static const AVOption options[] = {
591     { "w",      "Output video width",  OFFSET(w_expr),     AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
592     { "h",      "Output video height", OFFSET(h_expr),     AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
593     { "interp_algo", "Interpolation algorithm used for resizing", OFFSET(interp_algo), AV_OPT_TYPE_INT, { .i64 = INTERP_ALGO_DEFAULT }, 0, INTERP_ALGO_COUNT - 1, FLAGS, "interp_algo" },
594         { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BILINEAR }, 0, 0, FLAGS, "interp_algo" },
595         { "bicubic",  "bicubic",  0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BICUBIC  }, 0, 0, FLAGS, "interp_algo" },
596     { "passthrough", "Do not process frames at all if parameters match", OFFSET(passthrough), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
597     { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, FLAGS, "force_oar" },
598         { "disable",  NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
599         { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
600         { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
601     { "force_divisible_by", "enforce that the output resolution is divisible by a defined integer when force_original_aspect_ratio is used", OFFSET(force_divisible_by), AV_OPT_TYPE_INT, { .i64 = 1}, 1, 256, FLAGS },
602     { NULL },
603 };
604
605 static const AVClass cudascale_class = {
606     .class_name = "cudascale",
607     .item_name  = av_default_item_name,
608     .option     = options,
609     .version    = LIBAVUTIL_VERSION_INT,
610 };
611
612 static const AVFilterPad cudascale_inputs[] = {
613     {
614         .name        = "default",
615         .type        = AVMEDIA_TYPE_VIDEO,
616         .filter_frame = cudascale_filter_frame,
617         .get_video_buffer = cudascale_get_video_buffer,
618     },
619     { NULL }
620 };
621
622 static const AVFilterPad cudascale_outputs[] = {
623     {
624         .name         = "default",
625         .type         = AVMEDIA_TYPE_VIDEO,
626         .config_props = cudascale_config_props,
627     },
628     { NULL }
629 };
630
631 AVFilter ff_vf_scale_cuda = {
632     .name      = "scale_cuda",
633     .description = NULL_IF_CONFIG_SMALL("GPU accelerated video resizer"),
634
635     .init          = cudascale_init,
636     .uninit        = cudascale_uninit,
637     .query_formats = cudascale_query_formats,
638
639     .priv_size = sizeof(CUDAScaleContext),
640     .priv_class = &cudascale_class,
641
642     .inputs    = cudascale_inputs,
643     .outputs   = cudascale_outputs,
644
645     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
646 };