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