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