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)
58 typedef struct CUDAScaleContext {
61 AVCUDADeviceContext *hwctx;
63 enum AVPixelFormat in_fmt;
64 enum AVPixelFormat out_fmt;
69 } planes_in[3], planes_out[3];
71 AVBufferRef *frames_ctx;
78 * Output sw format. AV_PIX_FMT_NONE for no conversion.
80 enum AVPixelFormat format;
82 char *w_expr; ///< width expression string
83 char *h_expr; ///< height expression string
85 int force_original_aspect_ratio;
86 int force_divisible_by;
90 CUfunction cu_func_uchar;
91 CUfunction cu_func_uchar2;
92 CUfunction cu_func_uchar4;
93 CUfunction cu_func_ushort;
94 CUfunction cu_func_ushort2;
95 CUfunction cu_func_ushort4;
98 CUdeviceptr srcBuffer;
99 CUdeviceptr dstBuffer;
103 static av_cold int cudascale_init(AVFilterContext *ctx)
105 CUDAScaleContext *s = ctx->priv;
107 s->format = AV_PIX_FMT_NONE;
108 s->frame = av_frame_alloc();
110 return AVERROR(ENOMEM);
112 s->tmp_frame = av_frame_alloc();
114 return AVERROR(ENOMEM);
119 static av_cold void cudascale_uninit(AVFilterContext *ctx)
121 CUDAScaleContext *s = ctx->priv;
123 if (s->hwctx && s->cu_module) {
124 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
127 CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
128 CHECK_CU(cu->cuModuleUnload(s->cu_module));
130 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
133 av_frame_free(&s->frame);
134 av_buffer_unref(&s->frames_ctx);
135 av_frame_free(&s->tmp_frame);
138 static int cudascale_query_formats(AVFilterContext *ctx)
140 static const enum AVPixelFormat pixel_formats[] = {
141 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
143 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
145 return ff_set_common_formats(ctx, pix_fmts);
148 static av_cold int init_stage(CUDAScaleContext *s, AVBufferRef *device_ctx)
150 AVBufferRef *out_ref = NULL;
151 AVHWFramesContext *out_ctx;
152 int in_sw, in_sh, out_sw, out_sh;
155 av_pix_fmt_get_chroma_sub_sample(s->in_fmt, &in_sw, &in_sh);
156 av_pix_fmt_get_chroma_sub_sample(s->out_fmt, &out_sw, &out_sh);
157 if (!s->planes_out[0].width) {
158 s->planes_out[0].width = s->planes_in[0].width;
159 s->planes_out[0].height = s->planes_in[0].height;
162 for (i = 1; i < FF_ARRAY_ELEMS(s->planes_in); i++) {
163 s->planes_in[i].width = s->planes_in[0].width >> in_sw;
164 s->planes_in[i].height = s->planes_in[0].height >> in_sh;
165 s->planes_out[i].width = s->planes_out[0].width >> out_sw;
166 s->planes_out[i].height = s->planes_out[0].height >> out_sh;
169 out_ref = av_hwframe_ctx_alloc(device_ctx);
171 return AVERROR(ENOMEM);
172 out_ctx = (AVHWFramesContext*)out_ref->data;
174 out_ctx->format = AV_PIX_FMT_CUDA;
175 out_ctx->sw_format = s->out_fmt;
176 out_ctx->width = FFALIGN(s->planes_out[0].width, 32);
177 out_ctx->height = FFALIGN(s->planes_out[0].height, 32);
179 ret = av_hwframe_ctx_init(out_ref);
183 av_frame_unref(s->frame);
184 ret = av_hwframe_get_buffer(out_ref, s->frame, 0);
188 s->frame->width = s->planes_out[0].width;
189 s->frame->height = s->planes_out[0].height;
191 av_buffer_unref(&s->frames_ctx);
192 s->frames_ctx = out_ref;
196 av_buffer_unref(&out_ref);
200 static int format_is_supported(enum AVPixelFormat fmt)
204 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
205 if (supported_formats[i] == fmt)
210 static av_cold int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
211 int out_width, int out_height)
213 CUDAScaleContext *s = ctx->priv;
215 AVHWFramesContext *in_frames_ctx;
217 enum AVPixelFormat in_format;
218 enum AVPixelFormat out_format;
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);
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;
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);
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);
241 if (in_width == out_width && in_height == out_height)
244 s->in_fmt = in_format;
245 s->out_fmt = out_format;
247 s->planes_in[0].width = in_width;
248 s->planes_in[0].height = in_height;
249 s->planes_out[0].width = out_width;
250 s->planes_out[0].height = out_height;
252 ret = init_stage(s, in_frames_ctx->device_ref);
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);
263 static av_cold int cudascale_config_props(AVFilterLink *outlink)
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;
275 extern char vf_scale_cuda_ptx[];
277 s->hwctx = device_hwctx;
278 s->cu_stream = s->hwctx->stream;
280 ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
284 ret = CHECK_CU(cu->cuModuleLoadData(&s->cu_module, vf_scale_cuda_ptx));
288 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar, s->cu_module, "Subsample_Bilinear_uchar"));
292 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar2, s->cu_module, "Subsample_Bilinear_uchar2"));
296 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar4, s->cu_module, "Subsample_Bilinear_uchar4"));
300 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort, s->cu_module, "Subsample_Bilinear_ushort"));
304 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort2, s->cu_module, "Subsample_Bilinear_ushort2"));
308 CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort4, s->cu_module, "Subsample_Bilinear_ushort4"));
313 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
315 if ((ret = ff_scale_eval_dimensions(s,
316 s->w_expr, s->h_expr,
321 ff_scale_adjust_dimensions(inlink, &w, &h,
322 s->force_original_aspect_ratio, s->force_divisible_by);
324 if (((int64_t)h * inlink->w) > INT_MAX ||
325 ((int64_t)w * inlink->h) > INT_MAX)
326 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
331 ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
335 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
336 inlink->w, inlink->h, outlink->w, outlink->h);
338 if (inlink->sample_aspect_ratio.num) {
339 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
340 outlink->w*inlink->h},
341 inlink->sample_aspect_ratio);
343 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
352 static int call_resize_kernel(AVFilterContext *ctx, CUfunction func, int channels,
353 uint8_t *src_dptr, int src_width, int src_height, int src_pitch,
354 uint8_t *dst_dptr, int dst_width, int dst_height, int dst_pitch,
357 CUDAScaleContext *s = ctx->priv;
358 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
359 CUdeviceptr dst_devptr = (CUdeviceptr)dst_dptr;
361 void *args_uchar[] = { &tex, &dst_devptr, &dst_width, &dst_height, &dst_pitch, &src_width, &src_height };
364 CUDA_TEXTURE_DESC tex_desc = {
365 .filterMode = CU_TR_FILTER_MODE_LINEAR,
366 .flags = CU_TRSF_READ_AS_INTEGER,
369 CUDA_RESOURCE_DESC res_desc = {
370 .resType = CU_RESOURCE_TYPE_PITCH2D,
371 .res.pitch2D.format = pixel_size == 1 ?
372 CU_AD_FORMAT_UNSIGNED_INT8 :
373 CU_AD_FORMAT_UNSIGNED_INT16,
374 .res.pitch2D.numChannels = channels,
375 .res.pitch2D.width = src_width,
376 .res.pitch2D.height = src_height,
377 .res.pitch2D.pitchInBytes = src_pitch * pixel_size,
378 .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
381 ret = CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc, NULL));
385 ret = CHECK_CU(cu->cuLaunchKernel(func,
386 DIV_UP(dst_width, BLOCKX), DIV_UP(dst_height, BLOCKY), 1,
387 BLOCKX, BLOCKY, 1, 0, s->cu_stream, args_uchar, NULL));
391 CHECK_CU(cu->cuTexObjectDestroy(tex));
396 static int scalecuda_resize(AVFilterContext *ctx,
397 AVFrame *out, AVFrame *in)
399 AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
400 CUDAScaleContext *s = ctx->priv;
402 switch (in_frames_ctx->sw_format) {
403 case AV_PIX_FMT_YUV420P:
404 call_resize_kernel(ctx, s->cu_func_uchar, 1,
405 in->data[0], in->width, in->height, in->linesize[0],
406 out->data[0], out->width, out->height, out->linesize[0],
408 call_resize_kernel(ctx, s->cu_func_uchar, 1,
409 in->data[1], in->width/2, in->height/2, in->linesize[0]/2,
410 out->data[1], out->width/2, out->height/2, out->linesize[0]/2,
412 call_resize_kernel(ctx, s->cu_func_uchar, 1,
413 in->data[2], in->width/2, in->height/2, in->linesize[0]/2,
414 out->data[2], out->width/2, out->height/2, out->linesize[0]/2,
417 case AV_PIX_FMT_YUV444P:
418 call_resize_kernel(ctx, s->cu_func_uchar, 1,
419 in->data[0], in->width, in->height, in->linesize[0],
420 out->data[0], out->width, out->height, out->linesize[0],
422 call_resize_kernel(ctx, s->cu_func_uchar, 1,
423 in->data[1], in->width, in->height, in->linesize[0],
424 out->data[1], out->width, out->height, out->linesize[0],
426 call_resize_kernel(ctx, s->cu_func_uchar, 1,
427 in->data[2], in->width, in->height, in->linesize[0],
428 out->data[2], out->width, out->height, out->linesize[0],
431 case AV_PIX_FMT_YUV444P16:
432 call_resize_kernel(ctx, s->cu_func_ushort, 1,
433 in->data[0], in->width, in->height, in->linesize[0] / 2,
434 out->data[0], out->width, out->height, out->linesize[0] / 2,
436 call_resize_kernel(ctx, s->cu_func_ushort, 1,
437 in->data[1], in->width, in->height, in->linesize[1] / 2,
438 out->data[1], out->width, out->height, out->linesize[1] / 2,
440 call_resize_kernel(ctx, s->cu_func_ushort, 1,
441 in->data[2], in->width, in->height, in->linesize[2] / 2,
442 out->data[2], out->width, out->height, out->linesize[2] / 2,
445 case AV_PIX_FMT_NV12:
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_uchar2, 2,
451 in->data[1], in->width/2, in->height/2, in->linesize[1],
452 out->data[1], out->width/2, out->height/2, out->linesize[1]/2,
455 case AV_PIX_FMT_P010LE:
456 call_resize_kernel(ctx, s->cu_func_ushort, 1,
457 in->data[0], in->width, in->height, in->linesize[0]/2,
458 out->data[0], out->width, out->height, out->linesize[0]/2,
460 call_resize_kernel(ctx, s->cu_func_ushort2, 2,
461 in->data[1], in->width / 2, in->height / 2, in->linesize[1]/2,
462 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
465 case AV_PIX_FMT_P016LE:
466 call_resize_kernel(ctx, s->cu_func_ushort, 1,
467 in->data[0], in->width, in->height, in->linesize[0] / 2,
468 out->data[0], out->width, out->height, out->linesize[0] / 2,
470 call_resize_kernel(ctx, s->cu_func_ushort2, 2,
471 in->data[1], in->width / 2, in->height / 2, in->linesize[1] / 2,
472 out->data[1], out->width / 2, out->height / 2, out->linesize[1] / 4,
482 static int cudascale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
484 CUDAScaleContext *s = ctx->priv;
488 ret = scalecuda_resize(ctx, s->frame, src);
493 ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
497 av_frame_move_ref(out, s->frame);
498 av_frame_move_ref(s->frame, s->tmp_frame);
500 s->frame->width = s->planes_out[0].width;
501 s->frame->height = s->planes_out[0].height;
503 ret = av_frame_copy_props(out, in);
510 static int cudascale_filter_frame(AVFilterLink *link, AVFrame *in)
512 AVFilterContext *ctx = link->dst;
513 CUDAScaleContext *s = ctx->priv;
514 AVFilterLink *outlink = ctx->outputs[0];
515 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
521 out = av_frame_alloc();
523 ret = AVERROR(ENOMEM);
527 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
531 ret = cudascale_scale(ctx, out, in);
533 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
537 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
538 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
539 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
543 return ff_filter_frame(outlink, out);
550 #define OFFSET(x) offsetof(CUDAScaleContext, x)
551 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
552 static const AVOption options[] = {
553 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
554 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
555 { "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" },
556 { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
557 { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
558 { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
559 { "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 },
563 static const AVClass cudascale_class = {
564 .class_name = "cudascale",
565 .item_name = av_default_item_name,
567 .version = LIBAVUTIL_VERSION_INT,
570 static const AVFilterPad cudascale_inputs[] = {
573 .type = AVMEDIA_TYPE_VIDEO,
574 .filter_frame = cudascale_filter_frame,
579 static const AVFilterPad cudascale_outputs[] = {
582 .type = AVMEDIA_TYPE_VIDEO,
583 .config_props = cudascale_config_props,
588 AVFilter ff_vf_scale_cuda = {
589 .name = "scale_cuda",
590 .description = NULL_IF_CONFIG_SMALL("GPU accelerated video resizer"),
592 .init = cudascale_init,
593 .uninit = cudascale_uninit,
594 .query_formats = cudascale_query_formats,
596 .priv_size = sizeof(CUDAScaleContext),
597 .priv_class = &cudascale_class,
599 .inputs = cudascale_inputs,
600 .outputs = cudascale_outputs,
602 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,