2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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"
38 #include "scale_eval.h"
41 static const enum AVPixelFormat supported_formats[] = {
50 #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
54 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
65 typedef struct CUDAScaleContext {
68 AVCUDADeviceContext *hwctx;
70 enum AVPixelFormat in_fmt;
71 enum AVPixelFormat out_fmt;
73 AVBufferRef *frames_ctx;
80 * Output sw format. AV_PIX_FMT_NONE for no conversion.
82 enum AVPixelFormat format;
84 char *w_expr; ///< width expression string
85 char *h_expr; ///< height expression string
87 int force_original_aspect_ratio;
88 int force_divisible_by;
92 CUfunction cu_func_uchar;
93 CUfunction cu_func_uchar2;
94 CUfunction cu_func_uchar4;
95 CUfunction cu_func_ushort;
96 CUfunction cu_func_ushort2;
97 CUfunction cu_func_ushort4;
100 CUdeviceptr srcBuffer;
101 CUdeviceptr dstBuffer;
105 int interp_use_linear;
106 int interp_as_integer;
109 static av_cold int cudascale_init(AVFilterContext *ctx)
111 CUDAScaleContext *s = ctx->priv;
113 s->format = AV_PIX_FMT_NONE;
114 s->frame = av_frame_alloc();
116 return AVERROR(ENOMEM);
118 s->tmp_frame = av_frame_alloc();
120 return AVERROR(ENOMEM);
125 static av_cold void cudascale_uninit(AVFilterContext *ctx)
127 CUDAScaleContext *s = ctx->priv;
129 if (s->hwctx && s->cu_module) {
130 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
133 CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
134 CHECK_CU(cu->cuModuleUnload(s->cu_module));
136 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
139 av_frame_free(&s->frame);
140 av_buffer_unref(&s->frames_ctx);
141 av_frame_free(&s->tmp_frame);
144 static int cudascale_query_formats(AVFilterContext *ctx)
146 static const enum AVPixelFormat pixel_formats[] = {
147 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
149 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
151 return AVERROR(ENOMEM);
153 return ff_set_common_formats(ctx, pix_fmts);
156 static av_cold int init_hwframe_ctx(CUDAScaleContext *s, AVBufferRef *device_ctx, int width, int height)
158 AVBufferRef *out_ref = NULL;
159 AVHWFramesContext *out_ctx;
162 out_ref = av_hwframe_ctx_alloc(device_ctx);
164 return AVERROR(ENOMEM);
165 out_ctx = (AVHWFramesContext*)out_ref->data;
167 out_ctx->format = AV_PIX_FMT_CUDA;
168 out_ctx->sw_format = s->out_fmt;
169 out_ctx->width = FFALIGN(width, 32);
170 out_ctx->height = FFALIGN(height, 32);
172 ret = av_hwframe_ctx_init(out_ref);
176 av_frame_unref(s->frame);
177 ret = av_hwframe_get_buffer(out_ref, s->frame, 0);
181 s->frame->width = width;
182 s->frame->height = height;
184 av_buffer_unref(&s->frames_ctx);
185 s->frames_ctx = out_ref;
189 av_buffer_unref(&out_ref);
193 static int format_is_supported(enum AVPixelFormat fmt)
197 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
198 if (supported_formats[i] == fmt)
203 static av_cold int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
204 int out_width, int out_height)
206 CUDAScaleContext *s = ctx->priv;
208 AVHWFramesContext *in_frames_ctx;
210 enum AVPixelFormat in_format;
211 enum AVPixelFormat out_format;
214 /* check that we have a hw context */
215 if (!ctx->inputs[0]->hw_frames_ctx) {
216 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
217 return AVERROR(EINVAL);
219 in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
220 in_format = in_frames_ctx->sw_format;
221 out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
223 if (!format_is_supported(in_format)) {
224 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
225 av_get_pix_fmt_name(in_format));
226 return AVERROR(ENOSYS);
228 if (!format_is_supported(out_format)) {
229 av_log(ctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
230 av_get_pix_fmt_name(out_format));
231 return AVERROR(ENOSYS);
234 s->in_fmt = in_format;
235 s->out_fmt = out_format;
237 if (s->passthrough && in_width == out_width && in_height == out_height && in_format == out_format) {
238 s->frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
240 return AVERROR(ENOMEM);
244 ret = init_hwframe_ctx(s, in_frames_ctx->device_ref, out_width, out_height);
249 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
250 if (!ctx->outputs[0]->hw_frames_ctx)
251 return AVERROR(ENOMEM);
256 static av_cold int cudascale_config_props(AVFilterLink *outlink)
258 AVFilterContext *ctx = outlink->src;
259 AVFilterLink *inlink = outlink->src->inputs[0];
260 CUDAScaleContext *s = ctx->priv;
261 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
262 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
263 CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
264 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
270 const char *function_infix = "";
272 extern char vf_scale_cuda_ptx[];
273 extern char vf_scale_cuda_bicubic_ptx[];
275 switch(s->interp_algo) {
276 case INTERP_ALGO_BILINEAR:
277 scaler_ptx = vf_scale_cuda_ptx;
278 function_infix = "_Bilinear";
279 s->interp_use_linear = 1;
280 s->interp_as_integer = 1;
282 case INTERP_ALGO_DEFAULT:
283 case INTERP_ALGO_BICUBIC:
284 scaler_ptx = vf_scale_cuda_bicubic_ptx;
285 function_infix = "_Bicubic";
286 s->interp_use_linear = 0;
287 s->interp_as_integer = 0;
290 av_log(ctx, AV_LOG_ERROR, "Unknown interpolation algorithm\n");
294 s->hwctx = device_hwctx;
295 s->cu_stream = s->hwctx->stream;
297 ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
301 ret = CHECK_CU(cu->cuModuleLoadData(&s->cu_module, scaler_ptx));
305 snprintf(buf, sizeof(buf), "Subsample%s_uchar", function_infix);
306 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar, s->cu_module, buf));
310 snprintf(buf, sizeof(buf), "Subsample%s_uchar2", function_infix);
311 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar2, s->cu_module, buf));
315 snprintf(buf, sizeof(buf), "Subsample%s_uchar4", function_infix);
316 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar4, s->cu_module, buf));
320 snprintf(buf, sizeof(buf), "Subsample%s_ushort", function_infix);
321 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort, s->cu_module, buf));
325 snprintf(buf, sizeof(buf), "Subsample%s_ushort2", function_infix);
326 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort2, s->cu_module, buf));
330 snprintf(buf, sizeof(buf), "Subsample%s_ushort4", function_infix);
331 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort4, s->cu_module, buf));
336 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
338 if ((ret = ff_scale_eval_dimensions(s,
339 s->w_expr, s->h_expr,
344 ff_scale_adjust_dimensions(inlink, &w, &h,
345 s->force_original_aspect_ratio, s->force_divisible_by);
347 if (((int64_t)h * inlink->w) > INT_MAX ||
348 ((int64_t)w * inlink->h) > INT_MAX)
349 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
354 ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
358 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d%s\n",
359 inlink->w, inlink->h, outlink->w, outlink->h, s->passthrough ? " (passthrough)" : "");
361 if (inlink->sample_aspect_ratio.num) {
362 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
363 outlink->w*inlink->h},
364 inlink->sample_aspect_ratio);
366 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
375 static int call_resize_kernel(AVFilterContext *ctx, CUfunction func, int channels,
376 uint8_t *src_dptr, int src_width, int src_height, int src_pitch,
377 uint8_t *dst_dptr, int dst_width, int dst_height, int dst_pitch,
378 int pixel_size, int bit_depth)
380 CUDAScaleContext *s = ctx->priv;
381 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
382 CUdeviceptr dst_devptr = (CUdeviceptr)dst_dptr;
384 void *args_uchar[] = { &tex, &dst_devptr, &dst_width, &dst_height, &dst_pitch, &src_width, &src_height, &bit_depth };
387 CUDA_TEXTURE_DESC tex_desc = {
388 .filterMode = s->interp_use_linear ?
389 CU_TR_FILTER_MODE_LINEAR :
390 CU_TR_FILTER_MODE_POINT,
391 .flags = s->interp_as_integer ? CU_TRSF_READ_AS_INTEGER : 0,
394 CUDA_RESOURCE_DESC res_desc = {
395 .resType = CU_RESOURCE_TYPE_PITCH2D,
396 .res.pitch2D.format = pixel_size == 1 ?
397 CU_AD_FORMAT_UNSIGNED_INT8 :
398 CU_AD_FORMAT_UNSIGNED_INT16,
399 .res.pitch2D.numChannels = channels,
400 .res.pitch2D.width = src_width,
401 .res.pitch2D.height = src_height,
402 .res.pitch2D.pitchInBytes = src_pitch * pixel_size,
403 .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
406 ret = CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc, NULL));
410 ret = CHECK_CU(cu->cuLaunchKernel(func,
411 DIV_UP(dst_width, BLOCKX), DIV_UP(dst_height, BLOCKY), 1,
412 BLOCKX, BLOCKY, 1, 0, s->cu_stream, args_uchar, NULL));
416 CHECK_CU(cu->cuTexObjectDestroy(tex));
421 static int scalecuda_resize(AVFilterContext *ctx,
422 AVFrame *out, AVFrame *in)
424 AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
425 CUDAScaleContext *s = ctx->priv;
427 switch (in_frames_ctx->sw_format) {
428 case AV_PIX_FMT_YUV420P:
429 call_resize_kernel(ctx, s->cu_func_uchar, 1,
430 in->data[0], in->width, in->height, in->linesize[0],
431 out->data[0], out->width, out->height, out->linesize[0],
433 call_resize_kernel(ctx, s->cu_func_uchar, 1,
434 in->data[1], in->width / 2, in->height / 2, in->linesize[1],
435 out->data[1], out->width / 2, out->height / 2, out->linesize[1],
437 call_resize_kernel(ctx, s->cu_func_uchar, 1,
438 in->data[2], in->width / 2, in->height / 2, in->linesize[2],
439 out->data[2], out->width / 2, out->height / 2, out->linesize[2],
442 case AV_PIX_FMT_YUV444P:
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],
447 call_resize_kernel(ctx, s->cu_func_uchar, 1,
448 in->data[1], in->width, in->height, in->linesize[1],
449 out->data[1], out->width, out->height, out->linesize[1],
451 call_resize_kernel(ctx, s->cu_func_uchar, 1,
452 in->data[2], in->width, in->height, in->linesize[2],
453 out->data[2], out->width, out->height, out->linesize[2],
456 case AV_PIX_FMT_YUV444P16:
457 call_resize_kernel(ctx, s->cu_func_ushort, 1,
458 in->data[0], in->width, in->height, in->linesize[0] / 2,
459 out->data[0], out->width, out->height, out->linesize[0] / 2,
461 call_resize_kernel(ctx, s->cu_func_ushort, 1,
462 in->data[1], in->width, in->height, in->linesize[1] / 2,
463 out->data[1], out->width, out->height, out->linesize[1] / 2,
465 call_resize_kernel(ctx, s->cu_func_ushort, 1,
466 in->data[2], in->width, in->height, in->linesize[2] / 2,
467 out->data[2], out->width, out->height, out->linesize[2] / 2,
470 case AV_PIX_FMT_NV12:
471 call_resize_kernel(ctx, s->cu_func_uchar, 1,
472 in->data[0], in->width, in->height, in->linesize[0],
473 out->data[0], out->width, out->height, out->linesize[0],
475 call_resize_kernel(ctx, s->cu_func_uchar2, 2,
476 in->data[1], in->width / 2, in->height / 2, in->linesize[1],
477 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 2,
480 case AV_PIX_FMT_P010LE:
481 call_resize_kernel(ctx, s->cu_func_ushort, 1,
482 in->data[0], in->width, in->height, in->linesize[0] / 2,
483 out->data[0], out->width, out->height, out->linesize[0] / 2,
485 call_resize_kernel(ctx, s->cu_func_ushort2, 2,
486 in->data[1], in->width / 2, in->height / 2, in->linesize[1] / 2,
487 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
490 case AV_PIX_FMT_P016LE:
491 call_resize_kernel(ctx, s->cu_func_ushort, 1,
492 in->data[0], in->width, in->height, in->linesize[0] / 2,
493 out->data[0], out->width, out->height, out->linesize[0] / 2,
495 call_resize_kernel(ctx, s->cu_func_ushort2, 2,
496 in->data[1], in->width / 2, in->height / 2, in->linesize[1] / 2,
497 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
507 static int cudascale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
509 CUDAScaleContext *s = ctx->priv;
510 AVFilterLink *outlink = ctx->outputs[0];
514 ret = scalecuda_resize(ctx, s->frame, src);
519 ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
523 av_frame_move_ref(out, s->frame);
524 av_frame_move_ref(s->frame, s->tmp_frame);
526 s->frame->width = outlink->w;
527 s->frame->height = outlink->h;
529 ret = av_frame_copy_props(out, in);
536 static int cudascale_filter_frame(AVFilterLink *link, AVFrame *in)
538 AVFilterContext *ctx = link->dst;
539 CUDAScaleContext *s = ctx->priv;
540 AVFilterLink *outlink = ctx->outputs[0];
541 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
548 return ff_filter_frame(outlink, in);
550 out = av_frame_alloc();
552 ret = AVERROR(ENOMEM);
556 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
560 ret = cudascale_scale(ctx, out, in);
562 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
566 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
567 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
568 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
572 return ff_filter_frame(outlink, out);
579 static AVFrame *cudascale_get_video_buffer(AVFilterLink *inlink, int w, int h)
581 CUDAScaleContext *s = inlink->dst->priv;
583 return s->passthrough ?
584 ff_null_get_video_buffer (inlink, w, h) :
585 ff_default_get_video_buffer(inlink, w, h);
588 #define OFFSET(x) offsetof(CUDAScaleContext, x)
589 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
590 static const AVOption options[] = {
591 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
592 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
593 { "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" },
594 { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BILINEAR }, 0, 0, FLAGS, "interp_algo" },
595 { "bicubic", "bicubic", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BICUBIC }, 0, 0, FLAGS, "interp_algo" },
596 { "passthrough", "Do not process frames at all if parameters match", OFFSET(passthrough), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
597 { "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" },
598 { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
599 { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
600 { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
601 { "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 static const AVClass cudascale_class = {
606 .class_name = "cudascale",
607 .item_name = av_default_item_name,
609 .version = LIBAVUTIL_VERSION_INT,
612 static const AVFilterPad cudascale_inputs[] = {
615 .type = AVMEDIA_TYPE_VIDEO,
616 .filter_frame = cudascale_filter_frame,
617 .get_video_buffer = cudascale_get_video_buffer,
622 static const AVFilterPad cudascale_outputs[] = {
625 .type = AVMEDIA_TYPE_VIDEO,
626 .config_props = cudascale_config_props,
631 AVFilter ff_vf_scale_cuda = {
632 .name = "scale_cuda",
633 .description = NULL_IF_CONFIG_SMALL("GPU accelerated video resizer"),
635 .init = cudascale_init,
636 .uninit = cudascale_uninit,
637 .query_formats = cudascale_query_formats,
639 .priv_size = sizeof(CUDAScaleContext),
640 .priv_class = &cudascale_class,
642 .inputs = cudascale_inputs,
643 .outputs = cudascale_outputs,
645 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,