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) )
51 #define ALIGN_UP(a, b) (((a) + (b) - 1) & ~((b) - 1))
56 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
67 typedef struct CUDAScaleContext {
70 AVCUDADeviceContext *hwctx;
72 enum AVPixelFormat in_fmt;
73 enum AVPixelFormat out_fmt;
78 } planes_in[3], planes_out[3];
80 AVBufferRef *frames_ctx;
87 * Output sw format. AV_PIX_FMT_NONE for no conversion.
89 enum AVPixelFormat format;
91 char *w_expr; ///< width expression string
92 char *h_expr; ///< height expression string
94 int force_original_aspect_ratio;
95 int force_divisible_by;
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;
107 CUdeviceptr srcBuffer;
108 CUdeviceptr dstBuffer;
112 int interp_use_linear;
115 static av_cold int cudascale_init(AVFilterContext *ctx)
117 CUDAScaleContext *s = ctx->priv;
119 s->format = AV_PIX_FMT_NONE;
120 s->frame = av_frame_alloc();
122 return AVERROR(ENOMEM);
124 s->tmp_frame = av_frame_alloc();
126 return AVERROR(ENOMEM);
131 static av_cold void cudascale_uninit(AVFilterContext *ctx)
133 CUDAScaleContext *s = ctx->priv;
135 if (s->hwctx && s->cu_module) {
136 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
139 CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
140 CHECK_CU(cu->cuModuleUnload(s->cu_module));
142 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
145 av_frame_free(&s->frame);
146 av_buffer_unref(&s->frames_ctx);
147 av_frame_free(&s->tmp_frame);
150 static int cudascale_query_formats(AVFilterContext *ctx)
152 static const enum AVPixelFormat pixel_formats[] = {
153 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
155 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
157 return ff_set_common_formats(ctx, pix_fmts);
160 static av_cold int init_stage(CUDAScaleContext *s, AVBufferRef *device_ctx)
162 AVBufferRef *out_ref = NULL;
163 AVHWFramesContext *out_ctx;
164 int in_sw, in_sh, out_sw, out_sh;
167 av_pix_fmt_get_chroma_sub_sample(s->in_fmt, &in_sw, &in_sh);
168 av_pix_fmt_get_chroma_sub_sample(s->out_fmt, &out_sw, &out_sh);
169 if (!s->planes_out[0].width) {
170 s->planes_out[0].width = s->planes_in[0].width;
171 s->planes_out[0].height = s->planes_in[0].height;
174 for (i = 1; i < FF_ARRAY_ELEMS(s->planes_in); i++) {
175 s->planes_in[i].width = s->planes_in[0].width >> in_sw;
176 s->planes_in[i].height = s->planes_in[0].height >> in_sh;
177 s->planes_out[i].width = s->planes_out[0].width >> out_sw;
178 s->planes_out[i].height = s->planes_out[0].height >> out_sh;
181 out_ref = av_hwframe_ctx_alloc(device_ctx);
183 return AVERROR(ENOMEM);
184 out_ctx = (AVHWFramesContext*)out_ref->data;
186 out_ctx->format = AV_PIX_FMT_CUDA;
187 out_ctx->sw_format = s->out_fmt;
188 out_ctx->width = FFALIGN(s->planes_out[0].width, 32);
189 out_ctx->height = FFALIGN(s->planes_out[0].height, 32);
191 ret = av_hwframe_ctx_init(out_ref);
195 av_frame_unref(s->frame);
196 ret = av_hwframe_get_buffer(out_ref, s->frame, 0);
200 s->frame->width = s->planes_out[0].width;
201 s->frame->height = s->planes_out[0].height;
203 av_buffer_unref(&s->frames_ctx);
204 s->frames_ctx = out_ref;
208 av_buffer_unref(&out_ref);
212 static int format_is_supported(enum AVPixelFormat fmt)
216 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
217 if (supported_formats[i] == fmt)
222 static av_cold int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
223 int out_width, int out_height)
225 CUDAScaleContext *s = ctx->priv;
227 AVHWFramesContext *in_frames_ctx;
229 enum AVPixelFormat in_format;
230 enum AVPixelFormat out_format;
233 /* check that we have a hw context */
234 if (!ctx->inputs[0]->hw_frames_ctx) {
235 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
236 return AVERROR(EINVAL);
238 in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
239 in_format = in_frames_ctx->sw_format;
240 out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
242 if (!format_is_supported(in_format)) {
243 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
244 av_get_pix_fmt_name(in_format));
245 return AVERROR(ENOSYS);
247 if (!format_is_supported(out_format)) {
248 av_log(ctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
249 av_get_pix_fmt_name(out_format));
250 return AVERROR(ENOSYS);
253 if (in_width == out_width && in_height == out_height)
256 s->in_fmt = in_format;
257 s->out_fmt = out_format;
259 s->planes_in[0].width = in_width;
260 s->planes_in[0].height = in_height;
261 s->planes_out[0].width = out_width;
262 s->planes_out[0].height = out_height;
264 ret = init_stage(s, in_frames_ctx->device_ref);
268 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->frames_ctx);
269 if (!ctx->outputs[0]->hw_frames_ctx)
270 return AVERROR(ENOMEM);
275 static av_cold int cudascale_config_props(AVFilterLink *outlink)
277 AVFilterContext *ctx = outlink->src;
278 AVFilterLink *inlink = outlink->src->inputs[0];
279 CUDAScaleContext *s = ctx->priv;
280 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
281 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
282 CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
283 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
289 const char *function_infix = "";
291 extern char vf_scale_cuda_ptx[];
292 extern char vf_scale_cuda_bicubic_ptx[];
294 switch(s->interp_algo) {
295 case INTERP_ALGO_BILINEAR:
296 scaler_ptx = vf_scale_cuda_ptx;
297 function_infix = "_Bilinear";
298 s->interp_use_linear = 1;
300 case INTERP_ALGO_DEFAULT:
301 case INTERP_ALGO_BICUBIC:
302 scaler_ptx = vf_scale_cuda_bicubic_ptx;
303 function_infix = "_Bicubic";
304 s->interp_use_linear = 0;
307 av_log(ctx, AV_LOG_ERROR, "Unknown interpolation algorithm\n");
311 s->hwctx = device_hwctx;
312 s->cu_stream = s->hwctx->stream;
314 ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
318 ret = CHECK_CU(cu->cuModuleLoadData(&s->cu_module, scaler_ptx));
322 snprintf(buf, sizeof(buf), "Subsample%s_uchar", function_infix);
323 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar, s->cu_module, buf));
327 snprintf(buf, sizeof(buf), "Subsample%s_uchar2", function_infix);
328 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar2, s->cu_module, buf));
332 snprintf(buf, sizeof(buf), "Subsample%s_uchar4", function_infix);
333 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar4, s->cu_module, buf));
337 snprintf(buf, sizeof(buf), "Subsample%s_ushort", function_infix);
338 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort, s->cu_module, buf));
342 snprintf(buf, sizeof(buf), "Subsample%s_ushort2", function_infix);
343 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort2, s->cu_module, buf));
347 snprintf(buf, sizeof(buf), "Subsample%s_ushort4", function_infix);
348 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort4, s->cu_module, buf));
353 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
355 if ((ret = ff_scale_eval_dimensions(s,
356 s->w_expr, s->h_expr,
361 ff_scale_adjust_dimensions(inlink, &w, &h,
362 s->force_original_aspect_ratio, s->force_divisible_by);
364 if (((int64_t)h * inlink->w) > INT_MAX ||
365 ((int64_t)w * inlink->h) > INT_MAX)
366 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
371 ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
375 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
376 inlink->w, inlink->h, outlink->w, outlink->h);
378 if (inlink->sample_aspect_ratio.num) {
379 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
380 outlink->w*inlink->h},
381 inlink->sample_aspect_ratio);
383 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
392 static int call_resize_kernel(AVFilterContext *ctx, CUfunction func, int channels,
393 uint8_t *src_dptr, int src_width, int src_height, int src_pitch,
394 uint8_t *dst_dptr, int dst_width, int dst_height, int dst_pitch,
395 int pixel_size, int bit_depth)
397 CUDAScaleContext *s = ctx->priv;
398 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
399 CUdeviceptr dst_devptr = (CUdeviceptr)dst_dptr;
401 void *args_uchar[] = { &tex, &dst_devptr, &dst_width, &dst_height, &dst_pitch, &src_width, &src_height, &src_pitch, &bit_depth };
404 CUDA_TEXTURE_DESC tex_desc = {
405 .filterMode = s->interp_use_linear ?
406 CU_TR_FILTER_MODE_LINEAR :
407 CU_TR_FILTER_MODE_POINT,
408 .flags = CU_TRSF_READ_AS_INTEGER,
411 CUDA_RESOURCE_DESC res_desc = {
412 .resType = CU_RESOURCE_TYPE_PITCH2D,
413 .res.pitch2D.format = pixel_size == 1 ?
414 CU_AD_FORMAT_UNSIGNED_INT8 :
415 CU_AD_FORMAT_UNSIGNED_INT16,
416 .res.pitch2D.numChannels = channels,
417 .res.pitch2D.width = src_width,
418 .res.pitch2D.height = src_height,
419 .res.pitch2D.pitchInBytes = src_pitch * pixel_size,
420 .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
423 ret = CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc, NULL));
427 ret = CHECK_CU(cu->cuLaunchKernel(func,
428 DIV_UP(dst_width, BLOCKX), DIV_UP(dst_height, BLOCKY), 1,
429 BLOCKX, BLOCKY, 1, 0, s->cu_stream, args_uchar, NULL));
433 CHECK_CU(cu->cuTexObjectDestroy(tex));
438 static int scalecuda_resize(AVFilterContext *ctx,
439 AVFrame *out, AVFrame *in)
441 AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
442 CUDAScaleContext *s = ctx->priv;
444 switch (in_frames_ctx->sw_format) {
445 case AV_PIX_FMT_YUV420P:
446 call_resize_kernel(ctx, s->cu_func_uchar, 1,
447 in->data[0], in->width, in->height, in->linesize[0],
448 out->data[0], out->width, out->height, out->linesize[0],
450 call_resize_kernel(ctx, s->cu_func_uchar, 1,
451 in->data[1], in->width / 2, in->height / 2, in->linesize[0] / 2,
452 out->data[1], out->width / 2, out->height / 2, out->linesize[0] / 2,
454 call_resize_kernel(ctx, s->cu_func_uchar, 1,
455 in->data[2], in->width / 2, in->height / 2, in->linesize[0] / 2,
456 out->data[2], out->width / 2, out->height / 2, out->linesize[0] / 2,
459 case AV_PIX_FMT_YUV444P:
460 call_resize_kernel(ctx, s->cu_func_uchar, 1,
461 in->data[0], in->width, in->height, in->linesize[0],
462 out->data[0], out->width, out->height, out->linesize[0],
464 call_resize_kernel(ctx, s->cu_func_uchar, 1,
465 in->data[1], in->width, in->height, in->linesize[0],
466 out->data[1], out->width, out->height, out->linesize[0],
468 call_resize_kernel(ctx, s->cu_func_uchar, 1,
469 in->data[2], in->width, in->height, in->linesize[0],
470 out->data[2], out->width, out->height, out->linesize[0],
473 case AV_PIX_FMT_YUV444P16:
474 call_resize_kernel(ctx, s->cu_func_ushort, 1,
475 in->data[0], in->width, in->height, in->linesize[0] / 2,
476 out->data[0], out->width, out->height, out->linesize[0] / 2,
478 call_resize_kernel(ctx, s->cu_func_ushort, 1,
479 in->data[1], in->width, in->height, in->linesize[1] / 2,
480 out->data[1], out->width, out->height, out->linesize[1] / 2,
482 call_resize_kernel(ctx, s->cu_func_ushort, 1,
483 in->data[2], in->width, in->height, in->linesize[2] / 2,
484 out->data[2], out->width, out->height, out->linesize[2] / 2,
487 case AV_PIX_FMT_NV12:
488 call_resize_kernel(ctx, s->cu_func_uchar, 1,
489 in->data[0], in->width, in->height, in->linesize[0],
490 out->data[0], out->width, out->height, out->linesize[0],
492 call_resize_kernel(ctx, s->cu_func_uchar2, 2,
493 in->data[1], in->width / 2, in->height / 2, in->linesize[1],
494 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 2,
497 case AV_PIX_FMT_P010LE:
498 call_resize_kernel(ctx, s->cu_func_ushort, 1,
499 in->data[0], in->width, in->height, in->linesize[0] / 2,
500 out->data[0], out->width, out->height, out->linesize[0] / 2,
502 call_resize_kernel(ctx, s->cu_func_ushort2, 2,
503 in->data[1], in->width / 2, in->height / 2, in->linesize[1] / 2,
504 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
507 case AV_PIX_FMT_P016LE:
508 call_resize_kernel(ctx, s->cu_func_ushort, 1,
509 in->data[0], in->width, in->height, in->linesize[0] / 2,
510 out->data[0], out->width, out->height, out->linesize[0] / 2,
512 call_resize_kernel(ctx, s->cu_func_ushort2, 2,
513 in->data[1], in->width / 2, in->height / 2, in->linesize[1] / 2,
514 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
524 static int cudascale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
526 CUDAScaleContext *s = ctx->priv;
530 ret = scalecuda_resize(ctx, s->frame, src);
535 ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
539 av_frame_move_ref(out, s->frame);
540 av_frame_move_ref(s->frame, s->tmp_frame);
542 s->frame->width = s->planes_out[0].width;
543 s->frame->height = s->planes_out[0].height;
545 ret = av_frame_copy_props(out, in);
552 static int cudascale_filter_frame(AVFilterLink *link, AVFrame *in)
554 AVFilterContext *ctx = link->dst;
555 CUDAScaleContext *s = ctx->priv;
556 AVFilterLink *outlink = ctx->outputs[0];
557 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
563 out = av_frame_alloc();
565 ret = AVERROR(ENOMEM);
569 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
573 ret = cudascale_scale(ctx, out, in);
575 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
579 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
580 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
581 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
585 return ff_filter_frame(outlink, out);
592 #define OFFSET(x) offsetof(CUDAScaleContext, x)
593 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
594 static const AVOption options[] = {
595 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
596 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
597 { "interp_algo", "Interpolation algorithm used for resizing", OFFSET(interp_algo), AV_OPT_TYPE_INT, { .i64 = INTERP_ALGO_DEFAULT }, 0, INTERP_ALGO_COUNT - 1, FLAGS, "interp_algo" },
598 { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BILINEAR }, 0, 0, FLAGS, "interp_algo" },
599 { "bicubic", "bicubic", 0, AV_OPT_TYPE_CONST, { .i64 = INTERP_ALGO_BICUBIC }, 0, 0, FLAGS, "interp_algo" },
600 { "force_original_aspect_ratio", "decrease or increase w/h if necessary to keep the original AR", OFFSET(force_original_aspect_ratio), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 2, FLAGS, "force_oar" },
601 { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
602 { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
603 { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
604 { "force_divisible_by", "enforce that the output resolution is divisible by a defined integer when force_original_aspect_ratio is used", OFFSET(force_divisible_by), AV_OPT_TYPE_INT, { .i64 = 1}, 1, 256, FLAGS },
608 static const AVClass cudascale_class = {
609 .class_name = "cudascale",
610 .item_name = av_default_item_name,
612 .version = LIBAVUTIL_VERSION_INT,
615 static const AVFilterPad cudascale_inputs[] = {
618 .type = AVMEDIA_TYPE_VIDEO,
619 .filter_frame = cudascale_filter_frame,
624 static const AVFilterPad cudascale_outputs[] = {
627 .type = AVMEDIA_TYPE_VIDEO,
628 .config_props = cudascale_config_props,
633 AVFilter ff_vf_scale_cuda = {
634 .name = "scale_cuda",
635 .description = NULL_IF_CONFIG_SMALL("GPU accelerated video resizer"),
637 .init = cudascale_init,
638 .uninit = cudascale_uninit,
639 .query_formats = cudascale_query_formats,
641 .priv_size = sizeof(CUDAScaleContext),
642 .priv_class = &cudascale_class,
644 .inputs = cudascale_inputs,
645 .outputs = cudascale_outputs,
647 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,