2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "libavutil/avstring.h"
29 #include "libavutil/common.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/hwcontext_cuda_internal.h"
32 #include "libavutil/cuda_check.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
40 #include "scale_eval.h"
43 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, device_hwctx->internal->cuda_dl, x)
45 static const enum AVPixelFormat supported_formats[] = {
51 static const enum AVPixelFormat deinterleaved_formats[][2] = {
52 { AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P },
62 typedef struct NPPScaleStageContext {
64 enum AVPixelFormat in_fmt;
65 enum AVPixelFormat out_fmt;
70 } planes_in[3], planes_out[3];
72 AVBufferRef *frames_ctx;
74 } NPPScaleStageContext;
76 typedef struct NPPScaleContext {
79 NPPScaleStageContext stages[STAGE_NB];
83 int shift_width, shift_height;
86 * New dimensions. Special values are:
87 * 0 = original width/height
88 * -1 = keep original aspect
93 * Output sw format. AV_PIX_FMT_NONE for no conversion.
95 enum AVPixelFormat format;
97 char *w_expr; ///< width expression string
98 char *h_expr; ///< height expression string
101 int force_original_aspect_ratio;
102 int force_divisible_by;
107 static int nppscale_init(AVFilterContext *ctx)
109 NPPScaleContext *s = ctx->priv;
112 if (!strcmp(s->format_str, "same")) {
113 s->format = AV_PIX_FMT_NONE;
115 s->format = av_get_pix_fmt(s->format_str);
116 if (s->format == AV_PIX_FMT_NONE) {
117 av_log(ctx, AV_LOG_ERROR, "Unrecognized pixel format: %s\n", s->format_str);
118 return AVERROR(EINVAL);
122 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
123 s->stages[i].frame = av_frame_alloc();
124 if (!s->stages[i].frame)
125 return AVERROR(ENOMEM);
127 s->tmp_frame = av_frame_alloc();
129 return AVERROR(ENOMEM);
134 static void nppscale_uninit(AVFilterContext *ctx)
136 NPPScaleContext *s = ctx->priv;
139 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
140 av_frame_free(&s->stages[i].frame);
141 av_buffer_unref(&s->stages[i].frames_ctx);
143 av_frame_free(&s->tmp_frame);
146 static int nppscale_query_formats(AVFilterContext *ctx)
148 static const enum AVPixelFormat pixel_formats[] = {
149 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
151 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
153 return ff_set_common_formats(ctx, pix_fmts);
156 static int init_stage(NPPScaleStageContext *stage, AVBufferRef *device_ctx)
158 AVBufferRef *out_ref = NULL;
159 AVHWFramesContext *out_ctx;
160 int in_sw, in_sh, out_sw, out_sh;
163 av_pix_fmt_get_chroma_sub_sample(stage->in_fmt, &in_sw, &in_sh);
164 av_pix_fmt_get_chroma_sub_sample(stage->out_fmt, &out_sw, &out_sh);
165 if (!stage->planes_out[0].width) {
166 stage->planes_out[0].width = stage->planes_in[0].width;
167 stage->planes_out[0].height = stage->planes_in[0].height;
170 for (i = 1; i < FF_ARRAY_ELEMS(stage->planes_in); i++) {
171 stage->planes_in[i].width = stage->planes_in[0].width >> in_sw;
172 stage->planes_in[i].height = stage->planes_in[0].height >> in_sh;
173 stage->planes_out[i].width = stage->planes_out[0].width >> out_sw;
174 stage->planes_out[i].height = stage->planes_out[0].height >> out_sh;
177 out_ref = av_hwframe_ctx_alloc(device_ctx);
179 return AVERROR(ENOMEM);
180 out_ctx = (AVHWFramesContext*)out_ref->data;
182 out_ctx->format = AV_PIX_FMT_CUDA;
183 out_ctx->sw_format = stage->out_fmt;
184 out_ctx->width = FFALIGN(stage->planes_out[0].width, 32);
185 out_ctx->height = FFALIGN(stage->planes_out[0].height, 32);
187 ret = av_hwframe_ctx_init(out_ref);
191 av_frame_unref(stage->frame);
192 ret = av_hwframe_get_buffer(out_ref, stage->frame, 0);
196 stage->frame->width = stage->planes_out[0].width;
197 stage->frame->height = stage->planes_out[0].height;
199 av_buffer_unref(&stage->frames_ctx);
200 stage->frames_ctx = out_ref;
204 av_buffer_unref(&out_ref);
208 static int format_is_supported(enum AVPixelFormat fmt)
212 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
213 if (supported_formats[i] == fmt)
218 static enum AVPixelFormat get_deinterleaved_format(enum AVPixelFormat fmt)
220 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
223 planes = av_pix_fmt_count_planes(fmt);
224 if (planes == desc->nb_components)
226 for (i = 0; i < FF_ARRAY_ELEMS(deinterleaved_formats); i++)
227 if (deinterleaved_formats[i][0] == fmt)
228 return deinterleaved_formats[i][1];
229 return AV_PIX_FMT_NONE;
232 static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
233 int out_width, int out_height)
235 NPPScaleContext *s = ctx->priv;
237 AVHWFramesContext *in_frames_ctx;
239 enum AVPixelFormat in_format;
240 enum AVPixelFormat out_format;
241 enum AVPixelFormat in_deinterleaved_format;
242 enum AVPixelFormat out_deinterleaved_format;
244 int i, ret, last_stage = -1;
246 /* check that we have a hw context */
247 if (!ctx->inputs[0]->hw_frames_ctx) {
248 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
249 return AVERROR(EINVAL);
251 in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
252 in_format = in_frames_ctx->sw_format;
253 out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
255 if (!format_is_supported(in_format)) {
256 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
257 av_get_pix_fmt_name(in_format));
258 return AVERROR(ENOSYS);
260 if (!format_is_supported(out_format)) {
261 av_log(ctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
262 av_get_pix_fmt_name(out_format));
263 return AVERROR(ENOSYS);
266 in_deinterleaved_format = get_deinterleaved_format(in_format);
267 out_deinterleaved_format = get_deinterleaved_format(out_format);
268 if (in_deinterleaved_format == AV_PIX_FMT_NONE ||
269 out_deinterleaved_format == AV_PIX_FMT_NONE)
272 /* figure out which stages need to be done */
273 if (in_width != out_width || in_height != out_height ||
274 in_deinterleaved_format != out_deinterleaved_format) {
275 s->stages[STAGE_RESIZE].stage_needed = 1;
277 if (s->interp_algo == NPPI_INTER_SUPER &&
278 (out_width > in_width && out_height > in_height)) {
279 s->interp_algo = NPPI_INTER_LANCZOS;
280 av_log(ctx, AV_LOG_WARNING, "super-sampling not supported for output dimensions, using lanczos instead.\n");
282 if (s->interp_algo == NPPI_INTER_SUPER &&
283 !(out_width < in_width && out_height < in_height)) {
284 s->interp_algo = NPPI_INTER_CUBIC;
285 av_log(ctx, AV_LOG_WARNING, "super-sampling not supported for output dimensions, using cubic instead.\n");
289 if (!s->stages[STAGE_RESIZE].stage_needed && in_format == out_format)
292 if (!s->passthrough) {
293 if (in_format != in_deinterleaved_format)
294 s->stages[STAGE_DEINTERLEAVE].stage_needed = 1;
295 if (out_format != out_deinterleaved_format)
296 s->stages[STAGE_INTERLEAVE].stage_needed = 1;
299 s->stages[STAGE_DEINTERLEAVE].in_fmt = in_format;
300 s->stages[STAGE_DEINTERLEAVE].out_fmt = in_deinterleaved_format;
301 s->stages[STAGE_DEINTERLEAVE].planes_in[0].width = in_width;
302 s->stages[STAGE_DEINTERLEAVE].planes_in[0].height = in_height;
304 s->stages[STAGE_RESIZE].in_fmt = in_deinterleaved_format;
305 s->stages[STAGE_RESIZE].out_fmt = out_deinterleaved_format;
306 s->stages[STAGE_RESIZE].planes_in[0].width = in_width;
307 s->stages[STAGE_RESIZE].planes_in[0].height = in_height;
308 s->stages[STAGE_RESIZE].planes_out[0].width = out_width;
309 s->stages[STAGE_RESIZE].planes_out[0].height = out_height;
311 s->stages[STAGE_INTERLEAVE].in_fmt = out_deinterleaved_format;
312 s->stages[STAGE_INTERLEAVE].out_fmt = out_format;
313 s->stages[STAGE_INTERLEAVE].planes_in[0].width = out_width;
314 s->stages[STAGE_INTERLEAVE].planes_in[0].height = out_height;
316 /* init the hardware contexts */
317 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
318 if (!s->stages[i].stage_needed)
321 ret = init_stage(&s->stages[i], in_frames_ctx->device_ref);
329 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
331 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
333 if (!ctx->outputs[0]->hw_frames_ctx)
334 return AVERROR(ENOMEM);
339 static int nppscale_config_props(AVFilterLink *outlink)
341 AVFilterContext *ctx = outlink->src;
342 AVFilterLink *inlink = outlink->src->inputs[0];
343 NPPScaleContext *s = ctx->priv;
347 if ((ret = ff_scale_eval_dimensions(s,
348 s->w_expr, s->h_expr,
353 ff_scale_adjust_dimensions(inlink, &w, &h,
354 s->force_original_aspect_ratio, s->force_divisible_by);
356 if (((int64_t)h * inlink->w) > INT_MAX ||
357 ((int64_t)w * inlink->h) > INT_MAX)
358 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
363 ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
367 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
368 inlink->w, inlink->h, outlink->w, outlink->h);
370 if (inlink->sample_aspect_ratio.num)
371 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
372 outlink->w*inlink->h},
373 inlink->sample_aspect_ratio);
375 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
383 static int nppscale_deinterleave(AVFilterContext *ctx, NPPScaleStageContext *stage,
384 AVFrame *out, AVFrame *in)
386 AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
389 switch (in_frames_ctx->sw_format) {
390 case AV_PIX_FMT_NV12:
391 err = nppiYCbCr420_8u_P2P3R(in->data[0], in->linesize[0],
392 in->data[1], in->linesize[1],
393 out->data, out->linesize,
394 (NppiSize){ in->width, in->height });
399 if (err != NPP_SUCCESS) {
400 av_log(ctx, AV_LOG_ERROR, "NPP deinterleave error: %d\n", err);
401 return AVERROR_UNKNOWN;
407 static int nppscale_resize(AVFilterContext *ctx, NPPScaleStageContext *stage,
408 AVFrame *out, AVFrame *in)
410 NPPScaleContext *s = ctx->priv;
414 for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
415 int iw = stage->planes_in[i].width;
416 int ih = stage->planes_in[i].height;
417 int ow = stage->planes_out[i].width;
418 int oh = stage->planes_out[i].height;
420 err = nppiResizeSqrPixel_8u_C1R(in->data[i], (NppiSize){ iw, ih },
421 in->linesize[i], (NppiRect){ 0, 0, iw, ih },
422 out->data[i], out->linesize[i],
423 (NppiRect){ 0, 0, ow, oh },
424 (double)ow / iw, (double)oh / ih,
425 0.0, 0.0, s->interp_algo);
426 if (err != NPP_SUCCESS) {
427 av_log(ctx, AV_LOG_ERROR, "NPP resize error: %d\n", err);
428 return AVERROR_UNKNOWN;
435 static int nppscale_interleave(AVFilterContext *ctx, NPPScaleStageContext *stage,
436 AVFrame *out, AVFrame *in)
438 AVHWFramesContext *out_frames_ctx = (AVHWFramesContext*)out->hw_frames_ctx->data;
441 switch (out_frames_ctx->sw_format) {
442 case AV_PIX_FMT_NV12:
443 err = nppiYCbCr420_8u_P3P2R((const uint8_t**)in->data,
445 out->data[0], out->linesize[0],
446 out->data[1], out->linesize[1],
447 (NppiSize){ in->width, in->height });
452 if (err != NPP_SUCCESS) {
453 av_log(ctx, AV_LOG_ERROR, "NPP deinterleave error: %d\n", err);
454 return AVERROR_UNKNOWN;
460 static int (*const nppscale_process[])(AVFilterContext *ctx, NPPScaleStageContext *stage,
461 AVFrame *out, AVFrame *in) = {
462 [STAGE_DEINTERLEAVE] = nppscale_deinterleave,
463 [STAGE_RESIZE] = nppscale_resize,
464 [STAGE_INTERLEAVE] = nppscale_interleave,
467 static int nppscale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
469 NPPScaleContext *s = ctx->priv;
471 int i, ret, last_stage = -1;
473 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
474 if (!s->stages[i].stage_needed)
477 ret = nppscale_process[i](ctx, &s->stages[i], s->stages[i].frame, src);
481 src = s->stages[i].frame;
487 ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
491 av_frame_move_ref(out, src);
492 av_frame_move_ref(src, s->tmp_frame);
494 ret = av_frame_copy_props(out, in);
501 static int nppscale_filter_frame(AVFilterLink *link, AVFrame *in)
503 AVFilterContext *ctx = link->dst;
504 NPPScaleContext *s = ctx->priv;
505 AVFilterLink *outlink = ctx->outputs[0];
506 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
507 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
514 return ff_filter_frame(outlink, in);
516 out = av_frame_alloc();
518 ret = AVERROR(ENOMEM);
522 ret = CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(device_hwctx->cuda_ctx));
526 ret = nppscale_scale(ctx, out, in);
528 CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy));
532 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
533 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
534 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
538 return ff_filter_frame(outlink, out);
545 #define OFFSET(x) offsetof(NPPScaleContext, x)
546 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
547 static const AVOption options[] = {
548 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
549 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
550 { "format", "Output pixel format", OFFSET(format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
552 { "interp_algo", "Interpolation algorithm used for resizing", OFFSET(interp_algo), AV_OPT_TYPE_INT, { .i64 = NPPI_INTER_CUBIC }, 0, INT_MAX, FLAGS, "interp_algo" },
553 { "nn", "nearest neighbour", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_NN }, 0, 0, FLAGS, "interp_algo" },
554 { "linear", "linear", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_LINEAR }, 0, 0, FLAGS, "interp_algo" },
555 { "cubic", "cubic", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC }, 0, 0, FLAGS, "interp_algo" },
556 { "cubic2p_bspline", "2-parameter cubic (B=1, C=0)", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC2P_BSPLINE }, 0, 0, FLAGS, "interp_algo" },
557 { "cubic2p_catmullrom", "2-parameter cubic (B=0, C=1/2)", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC2P_CATMULLROM }, 0, 0, FLAGS, "interp_algo" },
558 { "cubic2p_b05c03", "2-parameter cubic (B=1/2, C=3/10)", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC2P_B05C03 }, 0, 0, FLAGS, "interp_algo" },
559 { "super", "supersampling", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_SUPER }, 0, 0, FLAGS, "interp_algo" },
560 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_LANCZOS }, 0, 0, FLAGS, "interp_algo" },
561 { "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" },
562 { "disable", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, FLAGS, "force_oar" },
563 { "decrease", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, FLAGS, "force_oar" },
564 { "increase", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 0, FLAGS, "force_oar" },
565 { "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 },
569 static const AVClass nppscale_class = {
570 .class_name = "nppscale",
571 .item_name = av_default_item_name,
573 .version = LIBAVUTIL_VERSION_INT,
576 static const AVFilterPad nppscale_inputs[] = {
579 .type = AVMEDIA_TYPE_VIDEO,
580 .filter_frame = nppscale_filter_frame,
585 static const AVFilterPad nppscale_outputs[] = {
588 .type = AVMEDIA_TYPE_VIDEO,
589 .config_props = nppscale_config_props,
594 AVFilter ff_vf_scale_npp = {
596 .description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video "
597 "scaling and format conversion"),
599 .init = nppscale_init,
600 .uninit = nppscale_uninit,
601 .query_formats = nppscale_query_formats,
603 .priv_size = sizeof(NPPScaleContext),
604 .priv_class = &nppscale_class,
606 .inputs = nppscale_inputs,
607 .outputs = nppscale_outputs,
609 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,