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