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