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