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/eval.h"
31 #include "libavutil/hwcontext.h"
32 #include "libavutil/hwcontext_cuda.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
43 static const enum AVPixelFormat supported_formats[] = {
49 static const enum AVPixelFormat deinterleaved_formats[][2] = {
50 { AV_PIX_FMT_NV12, AV_PIX_FMT_YUV420P },
53 static const char *const var_names[] = {
86 typedef struct NPPScaleStageContext {
88 enum AVPixelFormat in_fmt;
89 enum AVPixelFormat out_fmt;
94 } planes_in[3], planes_out[3];
96 AVBufferRef *frames_ctx;
98 } NPPScaleStageContext;
100 typedef struct NPPScaleContext {
101 const AVClass *class;
103 NPPScaleStageContext stages[STAGE_NB];
107 int shift_width, shift_height;
110 * New dimensions. Special values are:
111 * 0 = original width/height
112 * -1 = keep original aspect
117 * Output sw format. AV_PIX_FMT_NONE for no conversion.
119 enum AVPixelFormat format;
121 char *w_expr; ///< width expression string
122 char *h_expr; ///< height expression string
128 static int nppscale_init(AVFilterContext *ctx)
130 NPPScaleContext *s = ctx->priv;
133 if (!strcmp(s->format_str, "same")) {
134 s->format = AV_PIX_FMT_NONE;
136 s->format = av_get_pix_fmt(s->format_str);
137 if (s->format == AV_PIX_FMT_NONE) {
138 av_log(ctx, AV_LOG_ERROR, "Unrecognized pixel format: %s\n", s->format_str);
139 return AVERROR(EINVAL);
143 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
144 s->stages[i].frame = av_frame_alloc();
145 if (!s->stages[i].frame)
146 return AVERROR(ENOMEM);
148 s->tmp_frame = av_frame_alloc();
150 return AVERROR(ENOMEM);
155 static void nppscale_uninit(AVFilterContext *ctx)
157 NPPScaleContext *s = ctx->priv;
160 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
161 av_frame_free(&s->stages[i].frame);
162 av_buffer_unref(&s->stages[i].frames_ctx);
164 av_frame_free(&s->tmp_frame);
167 static int nppscale_query_formats(AVFilterContext *ctx)
169 static const enum AVPixelFormat pixel_formats[] = {
170 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
172 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
174 ff_set_common_formats(ctx, pix_fmts);
179 static int init_stage(NPPScaleStageContext *stage, AVBufferRef *device_ctx)
181 AVBufferRef *out_ref = NULL;
182 AVHWFramesContext *out_ctx;
183 int in_sw, in_sh, out_sw, out_sh;
186 av_pix_fmt_get_chroma_sub_sample(stage->in_fmt, &in_sw, &in_sh);
187 av_pix_fmt_get_chroma_sub_sample(stage->out_fmt, &out_sw, &out_sh);
188 if (!stage->planes_out[0].width) {
189 stage->planes_out[0].width = stage->planes_in[0].width;
190 stage->planes_out[0].height = stage->planes_in[0].height;
193 for (i = 1; i < FF_ARRAY_ELEMS(stage->planes_in); i++) {
194 stage->planes_in[i].width = stage->planes_in[0].width >> in_sw;
195 stage->planes_in[i].height = stage->planes_in[0].height >> in_sh;
196 stage->planes_out[i].width = stage->planes_out[0].width >> out_sw;
197 stage->planes_out[i].height = stage->planes_out[0].height >> out_sh;
200 out_ref = av_hwframe_ctx_alloc(device_ctx);
202 return AVERROR(ENOMEM);
203 out_ctx = (AVHWFramesContext*)out_ref->data;
205 out_ctx->format = AV_PIX_FMT_CUDA;
206 out_ctx->sw_format = stage->out_fmt;
207 out_ctx->width = FFALIGN(stage->planes_out[0].width, 32);
208 out_ctx->height = FFALIGN(stage->planes_out[0].height, 32);
210 ret = av_hwframe_ctx_init(out_ref);
214 av_frame_unref(stage->frame);
215 ret = av_hwframe_get_buffer(out_ref, stage->frame, 0);
219 stage->frame->width = stage->planes_out[0].width;
220 stage->frame->height = stage->planes_out[0].height;
222 av_buffer_unref(&stage->frames_ctx);
223 stage->frames_ctx = out_ref;
227 av_buffer_unref(&out_ref);
231 static int format_is_supported(enum AVPixelFormat fmt)
235 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
236 if (supported_formats[i] == fmt)
241 static enum AVPixelFormat get_deinterleaved_format(enum AVPixelFormat fmt)
243 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
246 planes = av_pix_fmt_count_planes(fmt);
247 if (planes == desc->nb_components)
249 for (i = 0; i < FF_ARRAY_ELEMS(deinterleaved_formats); i++)
250 if (deinterleaved_formats[i][0] == fmt)
251 return deinterleaved_formats[i][1];
252 return AV_PIX_FMT_NONE;
255 static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
256 int out_width, int out_height)
258 NPPScaleContext *s = ctx->priv;
260 AVHWFramesContext *in_frames_ctx;
262 enum AVPixelFormat in_format;
263 enum AVPixelFormat out_format;
264 enum AVPixelFormat in_deinterleaved_format;
265 enum AVPixelFormat out_deinterleaved_format;
267 int i, ret, last_stage = -1;
269 /* check that we have a hw context */
270 if (!ctx->inputs[0]->hw_frames_ctx) {
271 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
272 return AVERROR(EINVAL);
274 in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
275 in_format = in_frames_ctx->sw_format;
276 out_format = (s->format == AV_PIX_FMT_NONE) ? in_format : s->format;
278 if (!format_is_supported(in_format)) {
279 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
280 av_get_pix_fmt_name(in_format));
281 return AVERROR(ENOSYS);
283 if (!format_is_supported(out_format)) {
284 av_log(ctx, AV_LOG_ERROR, "Unsupported output format: %s\n",
285 av_get_pix_fmt_name(out_format));
286 return AVERROR(ENOSYS);
289 in_deinterleaved_format = get_deinterleaved_format(in_format);
290 out_deinterleaved_format = get_deinterleaved_format(out_format);
291 if (in_deinterleaved_format == AV_PIX_FMT_NONE ||
292 out_deinterleaved_format == AV_PIX_FMT_NONE)
295 /* figure out which stages need to be done */
296 if (in_width != out_width || in_height != out_height ||
297 in_deinterleaved_format != out_deinterleaved_format) {
298 s->stages[STAGE_RESIZE].stage_needed = 1;
300 if (s->interp_algo == NPPI_INTER_SUPER &&
301 (out_width > in_width && out_height > in_height)) {
302 s->interp_algo = NPPI_INTER_LANCZOS;
303 av_log(ctx, AV_LOG_WARNING, "super-sampling not supported for output dimensions, using lanczos instead.\n");
305 if (s->interp_algo == NPPI_INTER_SUPER &&
306 !(out_width < in_width && out_height < in_height)) {
307 s->interp_algo = NPPI_INTER_CUBIC;
308 av_log(ctx, AV_LOG_WARNING, "super-sampling not supported for output dimensions, using cubic instead.\n");
312 if (!s->stages[STAGE_RESIZE].stage_needed && in_format == out_format)
315 if (!s->passthrough) {
316 if (in_format != in_deinterleaved_format)
317 s->stages[STAGE_DEINTERLEAVE].stage_needed = 1;
318 if (out_format != out_deinterleaved_format)
319 s->stages[STAGE_INTERLEAVE].stage_needed = 1;
322 s->stages[STAGE_DEINTERLEAVE].in_fmt = in_format;
323 s->stages[STAGE_DEINTERLEAVE].out_fmt = in_deinterleaved_format;
324 s->stages[STAGE_DEINTERLEAVE].planes_in[0].width = in_width;
325 s->stages[STAGE_DEINTERLEAVE].planes_in[0].height = in_height;
327 s->stages[STAGE_RESIZE].in_fmt = in_deinterleaved_format;
328 s->stages[STAGE_RESIZE].out_fmt = out_deinterleaved_format;
329 s->stages[STAGE_RESIZE].planes_in[0].width = in_width;
330 s->stages[STAGE_RESIZE].planes_in[0].height = in_height;
331 s->stages[STAGE_RESIZE].planes_out[0].width = out_width;
332 s->stages[STAGE_RESIZE].planes_out[0].height = out_height;
334 s->stages[STAGE_INTERLEAVE].in_fmt = out_deinterleaved_format;
335 s->stages[STAGE_INTERLEAVE].out_fmt = out_format;
336 s->stages[STAGE_INTERLEAVE].planes_in[0].width = out_width;
337 s->stages[STAGE_INTERLEAVE].planes_in[0].height = out_height;
339 /* init the hardware contexts */
340 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
341 if (!s->stages[i].stage_needed)
344 ret = init_stage(&s->stages[i], in_frames_ctx->device_ref);
353 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
354 if (!ctx->outputs[0]->hw_frames_ctx)
355 return AVERROR(ENOMEM);
360 static int nppscale_config_props(AVFilterLink *outlink)
362 AVFilterContext *ctx = outlink->src;
363 AVFilterLink *inlink = outlink->src->inputs[0];
364 NPPScaleContext *s = ctx->priv;
366 double var_values[VARS_NB], res;
370 var_values[VAR_PI] = M_PI;
371 var_values[VAR_PHI] = M_PHI;
372 var_values[VAR_E] = M_E;
373 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
374 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
375 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
376 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
377 var_values[VAR_A] = (double) inlink->w / inlink->h;
378 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
379 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
380 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
382 /* evaluate width and height */
383 av_expr_parse_and_eval(&res, (expr = s->w_expr),
384 var_names, var_values,
385 NULL, NULL, NULL, NULL, NULL, 0, ctx);
386 s->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
387 if ((ret = av_expr_parse_and_eval(&res, (expr = s->h_expr),
388 var_names, var_values,
389 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
391 s->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
392 /* evaluate again the width, as it may depend on the output height */
393 if ((ret = av_expr_parse_and_eval(&res, (expr = s->w_expr),
394 var_names, var_values,
395 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
402 /* sanity check params */
403 if (w < -1 || h < -1) {
404 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
405 return AVERROR(EINVAL);
407 if (w == -1 && h == -1)
415 w = av_rescale(h, inlink->w, inlink->h);
417 h = av_rescale(w, inlink->h, inlink->w);
419 if (w > INT_MAX || h > INT_MAX ||
420 (h * inlink->w) > INT_MAX ||
421 (w * inlink->h) > INT_MAX)
422 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
427 ret = init_processing_chain(ctx, inlink->w, inlink->h, w, h);
431 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -> w:%d h:%d\n",
432 inlink->w, inlink->h, outlink->w, outlink->h);
434 if (inlink->sample_aspect_ratio.num)
435 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
436 outlink->w*inlink->h},
437 inlink->sample_aspect_ratio);
439 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
444 av_log(NULL, AV_LOG_ERROR,
445 "Error when evaluating the expression '%s'\n", expr);
449 static int nppscale_deinterleave(AVFilterContext *ctx, NPPScaleStageContext *stage,
450 AVFrame *out, AVFrame *in)
452 AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
455 switch (in_frames_ctx->sw_format) {
456 case AV_PIX_FMT_NV12:
457 err = nppiYCbCr420_8u_P2P3R(in->data[0], in->linesize[0],
458 in->data[1], in->linesize[1],
459 out->data, out->linesize,
460 (NppiSize){ in->width, in->height });
465 if (err != NPP_SUCCESS) {
466 av_log(ctx, AV_LOG_ERROR, "NPP deinterleave error: %d\n", err);
467 return AVERROR_UNKNOWN;
473 static int nppscale_resize(AVFilterContext *ctx, NPPScaleStageContext *stage,
474 AVFrame *out, AVFrame *in)
476 NPPScaleContext *s = ctx->priv;
480 for (i = 0; i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
481 int iw = stage->planes_in[i].width;
482 int ih = stage->planes_in[i].height;
483 int ow = stage->planes_out[i].width;
484 int oh = stage->planes_out[i].height;
486 err = nppiResizeSqrPixel_8u_C1R(in->data[i], (NppiSize){ iw, ih },
487 in->linesize[i], (NppiRect){ 0, 0, iw, ih },
488 out->data[i], out->linesize[i],
489 (NppiRect){ 0, 0, ow, oh },
490 (double)ow / iw, (double)oh / ih,
491 0.0, 0.0, s->interp_algo);
492 if (err != NPP_SUCCESS) {
493 av_log(ctx, AV_LOG_ERROR, "NPP resize error: %d\n", err);
494 return AVERROR_UNKNOWN;
501 static int nppscale_interleave(AVFilterContext *ctx, NPPScaleStageContext *stage,
502 AVFrame *out, AVFrame *in)
504 AVHWFramesContext *out_frames_ctx = (AVHWFramesContext*)out->hw_frames_ctx->data;
507 switch (out_frames_ctx->sw_format) {
508 case AV_PIX_FMT_NV12:
509 err = nppiYCbCr420_8u_P3P2R((const uint8_t**)in->data,
511 out->data[0], out->linesize[0],
512 out->data[1], out->linesize[1],
513 (NppiSize){ in->width, in->height });
518 if (err != NPP_SUCCESS) {
519 av_log(ctx, AV_LOG_ERROR, "NPP deinterleave error: %d\n", err);
520 return AVERROR_UNKNOWN;
526 static int (*const nppscale_process[])(AVFilterContext *ctx, NPPScaleStageContext *stage,
527 AVFrame *out, AVFrame *in) = {
528 [STAGE_DEINTERLEAVE] = nppscale_deinterleave,
529 [STAGE_RESIZE] = nppscale_resize,
530 [STAGE_INTERLEAVE] = nppscale_interleave,
533 static int nppscale_scale(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
535 NPPScaleContext *s = ctx->priv;
537 int i, ret, last_stage = -1;
539 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
540 if (!s->stages[i].stage_needed)
543 ret = nppscale_process[i](ctx, &s->stages[i], s->stages[i].frame, src);
547 src = s->stages[i].frame;
553 ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
557 av_frame_move_ref(out, src);
558 av_frame_move_ref(src, s->tmp_frame);
560 ret = av_frame_copy_props(out, in);
567 static int nppscale_filter_frame(AVFilterLink *link, AVFrame *in)
569 AVFilterContext *ctx = link->dst;
570 NPPScaleContext *s = ctx->priv;
571 AVFilterLink *outlink = ctx->outputs[0];
572 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
573 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
581 return ff_filter_frame(outlink, in);
583 out = av_frame_alloc();
585 ret = AVERROR(ENOMEM);
589 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
590 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
591 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
594 err = cuCtxPushCurrent(device_hwctx->cuda_ctx);
595 if (err != CUDA_SUCCESS) {
596 ret = AVERROR_UNKNOWN;
600 ret = nppscale_scale(ctx, out, in);
602 cuCtxPopCurrent(&dummy);
607 return ff_filter_frame(outlink, out);
614 #define OFFSET(x) offsetof(NPPScaleContext, x)
615 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
616 static const AVOption options[] = {
617 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
618 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
619 { "format", "Output pixel format", OFFSET(format_str), AV_OPT_TYPE_STRING, { .str = "same" }, .flags = FLAGS },
621 { "interp_algo", "Interpolation algorithm used for resizing", OFFSET(interp_algo), AV_OPT_TYPE_INT, { .i64 = NPPI_INTER_CUBIC }, 0, INT_MAX, FLAGS, "interp_algo" },
622 { "nn", "nearest neighbour", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_NN }, 0, 0, FLAGS, "interp_algo" },
623 { "linear", "linear", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_LINEAR }, 0, 0, FLAGS, "interp_algo" },
624 { "cubic", "cubic", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC }, 0, 0, FLAGS, "interp_algo" },
625 { "cubic2p_bspline", "2-parameter cubic (B=1, C=0)", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_CUBIC2P_BSPLINE }, 0, 0, FLAGS, "interp_algo" },
626 { "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" },
627 { "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" },
628 { "super", "supersampling", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_SUPER }, 0, 0, FLAGS, "interp_algo" },
629 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, { .i64 = NPPI_INTER_LANCZOS }, 0, 0, FLAGS, "interp_algo" },
633 static const AVClass nppscale_class = {
634 .class_name = "nppscale",
635 .item_name = av_default_item_name,
637 .version = LIBAVUTIL_VERSION_INT,
640 static const AVFilterPad nppscale_inputs[] = {
643 .type = AVMEDIA_TYPE_VIDEO,
644 .filter_frame = nppscale_filter_frame,
649 static const AVFilterPad nppscale_outputs[] = {
652 .type = AVMEDIA_TYPE_VIDEO,
653 .config_props = nppscale_config_props,
658 AVFilter ff_vf_scale_npp = {
660 .description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video "
661 "scaling and format conversion"),
663 .init = nppscale_init,
664 .uninit = nppscale_uninit,
665 .query_formats = nppscale_query_formats,
667 .priv_size = sizeof(NPPScaleContext),
668 .priv_class = &nppscale_class,
670 .inputs = nppscale_inputs,
671 .outputs = nppscale_outputs,