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
23 #include "libavutil/common.h"
24 #include "libavutil/hwcontext.h"
25 #include "libavutil/hwcontext_cuda_internal.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
35 static const enum AVPixelFormat supported_formats[] = {
47 NPP_TRANSPOSE_CCLOCK_FLIP = 0,
48 NPP_TRANSPOSE_CLOCK = 1,
49 NPP_TRANSPOSE_CCLOCK = 2,
50 NPP_TRANSPOSE_CLOCK_FLIP = 3
54 NPP_TRANSPOSE_PT_TYPE_NONE = 0,
55 NPP_TRANSPOSE_PT_TYPE_LANDSCAPE,
56 NPP_TRANSPOSE_PT_TYPE_PORTRAIT
59 typedef struct NPPTransposeStageContext {
61 enum AVPixelFormat in_fmt;
62 enum AVPixelFormat out_fmt;
66 } planes_in[3], planes_out[3];
67 AVBufferRef *frames_ctx;
69 } NPPTransposeStageContext;
71 typedef struct NPPTransposeContext {
73 NPPTransposeStageContext stages[STAGE_NB];
76 int passthrough; ///< PassthroughType, landscape passthrough mode enabled
77 int dir; ///< TransposeDir
78 } NPPTransposeContext;
80 static int npptranspose_init(AVFilterContext *ctx)
82 NPPTransposeContext *s = ctx->priv;
85 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
86 s->stages[i].frame = av_frame_alloc();
87 if (!s->stages[i].frame)
88 return AVERROR(ENOMEM);
91 s->tmp_frame = av_frame_alloc();
93 return AVERROR(ENOMEM);
98 static void npptranspose_uninit(AVFilterContext *ctx)
100 NPPTransposeContext *s = ctx->priv;
103 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
104 av_frame_free(&s->stages[i].frame);
105 av_buffer_unref(&s->stages[i].frames_ctx);
108 av_frame_free(&s->tmp_frame);
111 static int npptranspose_query_formats(AVFilterContext *ctx)
113 static const enum AVPixelFormat pixel_formats[] = {
114 AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
117 AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
118 return ff_set_common_formats(ctx, pix_fmts);
121 static int init_stage(NPPTransposeStageContext *stage, AVBufferRef *device_ctx)
123 AVBufferRef *out_ref = NULL;
124 AVHWFramesContext *out_ctx;
125 int in_sw, in_sh, out_sw, out_sh;
128 av_pix_fmt_get_chroma_sub_sample(stage->in_fmt, &in_sw, &in_sh);
129 av_pix_fmt_get_chroma_sub_sample(stage->out_fmt, &out_sw, &out_sh);
131 if (!stage->planes_out[0].width) {
132 stage->planes_out[0].width = stage->planes_in[0].width;
133 stage->planes_out[0].height = stage->planes_in[0].height;
136 for (i = 1; i < FF_ARRAY_ELEMS(stage->planes_in); i++) {
137 stage->planes_in[i].width = stage->planes_in[0].width >> in_sw;
138 stage->planes_in[i].height = stage->planes_in[0].height >> in_sh;
139 stage->planes_out[i].width = stage->planes_out[0].width >> out_sw;
140 stage->planes_out[i].height = stage->planes_out[0].height >> out_sh;
143 out_ref = av_hwframe_ctx_alloc(device_ctx);
145 return AVERROR(ENOMEM);
146 out_ctx = (AVHWFramesContext*)out_ref->data;
148 out_ctx->format = AV_PIX_FMT_CUDA;
149 out_ctx->sw_format = stage->out_fmt;
150 out_ctx->width = FFALIGN(stage->planes_out[0].width, 32);
151 out_ctx->height = FFALIGN(stage->planes_out[0].height, 32);
153 ret = av_hwframe_ctx_init(out_ref);
157 av_frame_unref(stage->frame);
158 ret = av_hwframe_get_buffer(out_ref, stage->frame, 0);
162 stage->frame->width = stage->planes_out[0].width;
163 stage->frame->height = stage->planes_out[0].height;
164 av_buffer_unref(&stage->frames_ctx);
165 stage->frames_ctx = out_ref;
170 av_buffer_unref(&out_ref);
174 static int format_is_supported(enum AVPixelFormat fmt)
178 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
179 if (supported_formats[i] == fmt)
185 static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
186 int out_width, int out_height)
188 NPPTransposeContext *s = ctx->priv;
189 AVHWFramesContext *in_frames_ctx;
190 enum AVPixelFormat format;
191 int i, ret, last_stage = -1;
192 int rot_width = out_width, rot_height = out_height;
194 /* check that we have a hw context */
195 if (!ctx->inputs[0]->hw_frames_ctx) {
196 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
197 return AVERROR(EINVAL);
200 in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
201 format = in_frames_ctx->sw_format;
203 if (!format_is_supported(format)) {
204 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
205 av_get_pix_fmt_name(format));
206 return AVERROR(ENOSYS);
209 if (s->dir != NPP_TRANSPOSE_CCLOCK_FLIP) {
210 s->stages[STAGE_ROTATE].stage_needed = 1;
213 if (s->dir == NPP_TRANSPOSE_CCLOCK_FLIP || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) {
214 s->stages[STAGE_TRANSPOSE].stage_needed = 1;
216 /* Rotating by 180° in case of clock_flip, or not at all for cclock_flip, so width/height unchanged by rotation */
217 rot_width = in_width;
218 rot_height = in_height;
221 s->stages[STAGE_ROTATE].in_fmt = format;
222 s->stages[STAGE_ROTATE].out_fmt = format;
223 s->stages[STAGE_ROTATE].planes_in[0].width = in_width;
224 s->stages[STAGE_ROTATE].planes_in[0].height = in_height;
225 s->stages[STAGE_ROTATE].planes_out[0].width = rot_width;
226 s->stages[STAGE_ROTATE].planes_out[0].height = rot_height;
227 s->stages[STAGE_TRANSPOSE].in_fmt = format;
228 s->stages[STAGE_TRANSPOSE].out_fmt = format;
229 s->stages[STAGE_TRANSPOSE].planes_in[0].width = rot_width;
230 s->stages[STAGE_TRANSPOSE].planes_in[0].height = rot_height;
231 s->stages[STAGE_TRANSPOSE].planes_out[0].width = out_width;
232 s->stages[STAGE_TRANSPOSE].planes_out[0].height = out_height;
234 /* init the hardware contexts */
235 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
236 if (!s->stages[i].stage_needed)
238 ret = init_stage(&s->stages[i], in_frames_ctx->device_ref);
244 if (last_stage >= 0) {
245 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
247 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
251 if (!ctx->outputs[0]->hw_frames_ctx)
252 return AVERROR(ENOMEM);
257 static int npptranspose_config_props(AVFilterLink *outlink)
259 AVFilterContext *ctx = outlink->src;
260 AVFilterLink *inlink = ctx->inputs[0];
261 NPPTransposeContext *s = ctx->priv;
264 if ((inlink->w >= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_LANDSCAPE) ||
265 (inlink->w <= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_PORTRAIT))
267 if (inlink->hw_frames_ctx) {
268 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
269 if (!outlink->hw_frames_ctx)
270 return AVERROR(ENOMEM);
273 av_log(ctx, AV_LOG_VERBOSE,
274 "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
275 inlink->w, inlink->h, inlink->w, inlink->h);
278 s->passthrough = NPP_TRANSPOSE_PT_TYPE_NONE;
281 outlink->w = inlink->h;
282 outlink->h = inlink->w;
283 outlink->sample_aspect_ratio = (AVRational){inlink->sample_aspect_ratio.den, inlink->sample_aspect_ratio.num};
285 ret = init_processing_chain(ctx, inlink->w, inlink->h, outlink->w, outlink->h);
289 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -transpose-> w:%d h:%d\n",
290 inlink->w, inlink->h, outlink->w, outlink->h);
295 static int npptranspose_rotate(AVFilterContext *ctx, NPPTransposeStageContext *stage,
296 AVFrame *out, AVFrame *in)
298 NPPTransposeContext *s = ctx->priv;
302 for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
303 int iw = stage->planes_in[i].width;
304 int ih = stage->planes_in[i].height;
305 int ow = stage->planes_out[i].width;
306 int oh = stage->planes_out[i].height;
308 // nppRotate uses 0,0 as the rotation point
309 // need to shift the image accordingly after rotation
310 // need to substract 1 to get the correct coordinates
311 double angle = s->dir == NPP_TRANSPOSE_CLOCK ? -90.0 : s->dir == NPP_TRANSPOSE_CCLOCK ? 90.0 : 180.0;
312 int shiftw = (s->dir == NPP_TRANSPOSE_CLOCK || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? ow - 1 : 0;
313 int shifth = (s->dir == NPP_TRANSPOSE_CCLOCK || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? oh - 1 : 0;
315 err = nppiRotate_8u_C1R(in->data[i], (NppiSize){ iw, ih },
316 in->linesize[i], (NppiRect){ 0, 0, iw, ih },
317 out->data[i], out->linesize[i],
318 (NppiRect){ 0, 0, ow, oh },
319 angle, shiftw, shifth, NPPI_INTER_NN);
320 if (err != NPP_SUCCESS) {
321 av_log(ctx, AV_LOG_ERROR, "NPP rotate error: %d\n", err);
322 return AVERROR_UNKNOWN;
329 static int npptranspose_transpose(AVFilterContext *ctx, NPPTransposeStageContext *stage,
330 AVFrame *out, AVFrame *in)
335 for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
336 int iw = stage->planes_in[i].width;
337 int ih = stage->planes_in[i].height;
339 err = nppiTranspose_8u_C1R(in->data[i], in->linesize[i],
340 out->data[i], out->linesize[i],
341 (NppiSize){ iw, ih });
342 if (err != NPP_SUCCESS) {
343 av_log(ctx, AV_LOG_ERROR, "NPP transpose error: %d\n", err);
344 return AVERROR_UNKNOWN;
351 static int (*const npptranspose_process[])(AVFilterContext *ctx, NPPTransposeStageContext *stage,
352 AVFrame *out, AVFrame *in) = {
353 [STAGE_ROTATE] = npptranspose_rotate,
354 [STAGE_TRANSPOSE] = npptranspose_transpose
357 static int npptranspose_filter(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
359 NPPTransposeContext *s = ctx->priv;
361 int i, ret, last_stage = -1;
363 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
364 if (!s->stages[i].stage_needed)
367 ret = npptranspose_process[i](ctx, &s->stages[i], s->stages[i].frame, src);
371 src = s->stages[i].frame;
378 ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
382 av_frame_move_ref(out, src);
383 av_frame_move_ref(src, s->tmp_frame);
385 ret = av_frame_copy_props(out, in);
392 static int npptranspose_filter_frame(AVFilterLink *link, AVFrame *in)
394 AVFilterContext *ctx = link->dst;
395 NPPTransposeContext *s = ctx->priv;
396 AVFilterLink *outlink = ctx->outputs[0];
397 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
398 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
405 return ff_filter_frame(outlink, in);
407 out = av_frame_alloc();
409 ret = AVERROR(ENOMEM);
413 err = device_hwctx->internal->cuda_dl->cuCtxPushCurrent(device_hwctx->cuda_ctx);
414 if (err != CUDA_SUCCESS) {
415 ret = AVERROR_UNKNOWN;
419 ret = npptranspose_filter(ctx, out, in);
421 device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy);
427 return ff_filter_frame(outlink, out);
435 #define OFFSET(x) offsetof(NPPTransposeContext, x)
436 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
438 static const AVOption options[] = {
439 { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = NPP_TRANSPOSE_CCLOCK_FLIP }, 0, 3, FLAGS, "dir" },
440 { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CCLOCK_FLIP }, 0, 0, FLAGS, "dir" },
441 { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CLOCK }, 0, 0, FLAGS, "dir" },
442 { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CCLOCK }, 0, 0, FLAGS, "dir" },
443 { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CLOCK_FLIP }, 0, 0, FLAGS, "dir" },
444 { "passthrough", "do not apply transposition if the input matches the specified geometry", OFFSET(passthrough), AV_OPT_TYPE_INT, { .i64 = NPP_TRANSPOSE_PT_TYPE_NONE }, 0, 2, FLAGS, "passthrough" },
445 { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_NONE }, 0, 0, FLAGS, "passthrough" },
446 { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_LANDSCAPE }, 0, 0, FLAGS, "passthrough" },
447 { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_PORTRAIT }, 0, 0, FLAGS, "passthrough" },
451 static const AVClass npptranspose_class = {
452 .class_name = "npptranspose",
453 .item_name = av_default_item_name,
455 .version = LIBAVUTIL_VERSION_INT,
458 static const AVFilterPad npptranspose_inputs[] = {
461 .type = AVMEDIA_TYPE_VIDEO,
462 .filter_frame = npptranspose_filter_frame,
467 static const AVFilterPad npptranspose_outputs[] = {
470 .type = AVMEDIA_TYPE_VIDEO,
471 .config_props = npptranspose_config_props,
476 AVFilter ff_vf_transpose_npp = {
477 .name = "transpose_npp",
478 .description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video transpose"),
479 .init = npptranspose_init,
480 .uninit = npptranspose_uninit,
481 .query_formats = npptranspose_query_formats,
482 .priv_size = sizeof(NPPTransposeContext),
483 .priv_class = &npptranspose_class,
484 .inputs = npptranspose_inputs,
485 .outputs = npptranspose_outputs,
486 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,