1 /*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <nppi.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "libavutil/common.h"
24 #include "libavutil/hwcontext.h"
25 #include "libavutil/hwcontext_cuda_internal.h"
26 #include "libavutil/cuda_check.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, device_hwctx->internal->cuda_dl, x)
37
38 static const enum AVPixelFormat supported_formats[] = {
39 AV_PIX_FMT_YUV420P,
40 AV_PIX_FMT_YUV444P
41 };
42
43 enum TransposeStage {
44 STAGE_ROTATE,
45 STAGE_TRANSPOSE,
46 STAGE_NB
47 };
48
49 enum Transpose {
50 NPP_TRANSPOSE_CCLOCK_FLIP = 0,
51 NPP_TRANSPOSE_CLOCK = 1,
52 NPP_TRANSPOSE_CCLOCK = 2,
53 NPP_TRANSPOSE_CLOCK_FLIP = 3
54 };
55
56 enum Passthrough {
57 NPP_TRANSPOSE_PT_TYPE_NONE = 0,
58 NPP_TRANSPOSE_PT_TYPE_LANDSCAPE,
59 NPP_TRANSPOSE_PT_TYPE_PORTRAIT
60 };
61
62 typedef struct NPPTransposeStageContext {
63 int stage_needed;
64 enum AVPixelFormat in_fmt;
65 enum AVPixelFormat out_fmt;
66 struct {
67 int width;
68 int height;
69 } planes_in[3], planes_out[3];
70 AVBufferRef *frames_ctx;
71 AVFrame *frame;
72 } NPPTransposeStageContext;
73
74 typedef struct NPPTransposeContext {
75 const AVClass *class;
76 NPPTransposeStageContext stages[STAGE_NB];
77 AVFrame *tmp_frame;
78
79 int passthrough; ///< PassthroughType, landscape passthrough mode enabled
80 int dir; ///< TransposeDir
81 } NPPTransposeContext;
82
npptranspose_init(AVFilterContext * ctx)83 static int npptranspose_init(AVFilterContext *ctx)
84 {
85 NPPTransposeContext *s = ctx->priv;
86 int i;
87
88 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
89 s->stages[i].frame = av_frame_alloc();
90 if (!s->stages[i].frame)
91 return AVERROR(ENOMEM);
92 }
93
94 s->tmp_frame = av_frame_alloc();
95 if (!s->tmp_frame)
96 return AVERROR(ENOMEM);
97
98 return 0;
99 }
100
npptranspose_uninit(AVFilterContext * ctx)101 static void npptranspose_uninit(AVFilterContext *ctx)
102 {
103 NPPTransposeContext *s = ctx->priv;
104 int i;
105
106 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
107 av_frame_free(&s->stages[i].frame);
108 av_buffer_unref(&s->stages[i].frames_ctx);
109 }
110
111 av_frame_free(&s->tmp_frame);
112 }
113
init_stage(NPPTransposeStageContext * stage,AVBufferRef * device_ctx)114 static int init_stage(NPPTransposeStageContext *stage, AVBufferRef *device_ctx)
115 {
116 AVBufferRef *out_ref = NULL;
117 AVHWFramesContext *out_ctx;
118 int in_sw, in_sh, out_sw, out_sh;
119 int ret, i;
120
121 av_pix_fmt_get_chroma_sub_sample(stage->in_fmt, &in_sw, &in_sh);
122 av_pix_fmt_get_chroma_sub_sample(stage->out_fmt, &out_sw, &out_sh);
123
124 if (!stage->planes_out[0].width) {
125 stage->planes_out[0].width = stage->planes_in[0].width;
126 stage->planes_out[0].height = stage->planes_in[0].height;
127 }
128
129 for (i = 1; i < FF_ARRAY_ELEMS(stage->planes_in); i++) {
130 stage->planes_in[i].width = stage->planes_in[0].width >> in_sw;
131 stage->planes_in[i].height = stage->planes_in[0].height >> in_sh;
132 stage->planes_out[i].width = stage->planes_out[0].width >> out_sw;
133 stage->planes_out[i].height = stage->planes_out[0].height >> out_sh;
134 }
135
136 out_ref = av_hwframe_ctx_alloc(device_ctx);
137 if (!out_ref)
138 return AVERROR(ENOMEM);
139 out_ctx = (AVHWFramesContext*)out_ref->data;
140
141 out_ctx->format = AV_PIX_FMT_CUDA;
142 out_ctx->sw_format = stage->out_fmt;
143 out_ctx->width = FFALIGN(stage->planes_out[0].width, 32);
144 out_ctx->height = FFALIGN(stage->planes_out[0].height, 32);
145
146 ret = av_hwframe_ctx_init(out_ref);
147 if (ret < 0)
148 goto fail;
149
150 av_frame_unref(stage->frame);
151 ret = av_hwframe_get_buffer(out_ref, stage->frame, 0);
152 if (ret < 0)
153 goto fail;
154
155 stage->frame->width = stage->planes_out[0].width;
156 stage->frame->height = stage->planes_out[0].height;
157 av_buffer_unref(&stage->frames_ctx);
158 stage->frames_ctx = out_ref;
159
160 return 0;
161
162 fail:
163 av_buffer_unref(&out_ref);
164 return ret;
165 }
166
format_is_supported(enum AVPixelFormat fmt)167 static int format_is_supported(enum AVPixelFormat fmt)
168 {
169 int i;
170
171 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
172 if (supported_formats[i] == fmt)
173 return 1;
174
175 return 0;
176 }
177
init_processing_chain(AVFilterContext * ctx,int in_width,int in_height,int out_width,int out_height)178 static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
179 int out_width, int out_height)
180 {
181 NPPTransposeContext *s = ctx->priv;
182 AVHWFramesContext *in_frames_ctx;
183 enum AVPixelFormat format;
184 int i, ret, last_stage = -1;
185 int rot_width = out_width, rot_height = out_height;
186
187 /* check that we have a hw context */
188 if (!ctx->inputs[0]->hw_frames_ctx) {
189 av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
190 return AVERROR(EINVAL);
191 }
192
193 in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
194 format = in_frames_ctx->sw_format;
195
196 if (!format_is_supported(format)) {
197 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
198 av_get_pix_fmt_name(format));
199 return AVERROR(ENOSYS);
200 }
201
202 if (s->dir != NPP_TRANSPOSE_CCLOCK_FLIP) {
203 s->stages[STAGE_ROTATE].stage_needed = 1;
204 }
205
206 if (s->dir == NPP_TRANSPOSE_CCLOCK_FLIP || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) {
207 s->stages[STAGE_TRANSPOSE].stage_needed = 1;
208
209 /* Rotating by 180° in case of clock_flip, or not at all for cclock_flip, so width/height unchanged by rotation */
210 rot_width = in_width;
211 rot_height = in_height;
212 }
213
214 s->stages[STAGE_ROTATE].in_fmt = format;
215 s->stages[STAGE_ROTATE].out_fmt = format;
216 s->stages[STAGE_ROTATE].planes_in[0].width = in_width;
217 s->stages[STAGE_ROTATE].planes_in[0].height = in_height;
218 s->stages[STAGE_ROTATE].planes_out[0].width = rot_width;
219 s->stages[STAGE_ROTATE].planes_out[0].height = rot_height;
220 s->stages[STAGE_TRANSPOSE].in_fmt = format;
221 s->stages[STAGE_TRANSPOSE].out_fmt = format;
222 s->stages[STAGE_TRANSPOSE].planes_in[0].width = rot_width;
223 s->stages[STAGE_TRANSPOSE].planes_in[0].height = rot_height;
224 s->stages[STAGE_TRANSPOSE].planes_out[0].width = out_width;
225 s->stages[STAGE_TRANSPOSE].planes_out[0].height = out_height;
226
227 /* init the hardware contexts */
228 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
229 if (!s->stages[i].stage_needed)
230 continue;
231 ret = init_stage(&s->stages[i], in_frames_ctx->device_ref);
232 if (ret < 0)
233 return ret;
234 last_stage = i;
235 }
236
237 if (last_stage >= 0) {
238 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
239 } else {
240 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
241 s->passthrough = 1;
242 }
243
244 if (!ctx->outputs[0]->hw_frames_ctx)
245 return AVERROR(ENOMEM);
246
247 return 0;
248 }
249
npptranspose_config_props(AVFilterLink * outlink)250 static int npptranspose_config_props(AVFilterLink *outlink)
251 {
252 AVFilterContext *ctx = outlink->src;
253 AVFilterLink *inlink = ctx->inputs[0];
254 NPPTransposeContext *s = ctx->priv;
255 int ret;
256
257 if ((inlink->w >= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_LANDSCAPE) ||
258 (inlink->w <= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_PORTRAIT))
259 {
260 if (inlink->hw_frames_ctx) {
261 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
262 if (!outlink->hw_frames_ctx)
263 return AVERROR(ENOMEM);
264 }
265
266 av_log(ctx, AV_LOG_VERBOSE,
267 "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
268 inlink->w, inlink->h, inlink->w, inlink->h);
269 return 0;
270 } else {
271 s->passthrough = NPP_TRANSPOSE_PT_TYPE_NONE;
272 }
273
274 outlink->w = inlink->h;
275 outlink->h = inlink->w;
276 outlink->sample_aspect_ratio = (AVRational){inlink->sample_aspect_ratio.den, inlink->sample_aspect_ratio.num};
277
278 ret = init_processing_chain(ctx, inlink->w, inlink->h, outlink->w, outlink->h);
279 if (ret < 0)
280 return ret;
281
282 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -transpose-> w:%d h:%d\n",
283 inlink->w, inlink->h, outlink->w, outlink->h);
284
285 return 0;
286 }
287
npptranspose_rotate(AVFilterContext * ctx,NPPTransposeStageContext * stage,AVFrame * out,AVFrame * in)288 static int npptranspose_rotate(AVFilterContext *ctx, NPPTransposeStageContext *stage,
289 AVFrame *out, AVFrame *in)
290 {
291 NPPTransposeContext *s = ctx->priv;
292 NppStatus err;
293 int i;
294
295 for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
296 int iw = stage->planes_in[i].width;
297 int ih = stage->planes_in[i].height;
298 int ow = stage->planes_out[i].width;
299 int oh = stage->planes_out[i].height;
300
301 // nppRotate uses 0,0 as the rotation point
302 // need to shift the image accordingly after rotation
303 // need to substract 1 to get the correct coordinates
304 double angle = s->dir == NPP_TRANSPOSE_CLOCK ? -90.0 : s->dir == NPP_TRANSPOSE_CCLOCK ? 90.0 : 180.0;
305 int shiftw = (s->dir == NPP_TRANSPOSE_CLOCK || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? ow - 1 : 0;
306 int shifth = (s->dir == NPP_TRANSPOSE_CCLOCK || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? oh - 1 : 0;
307
308 err = nppiRotate_8u_C1R(in->data[i], (NppiSize){ iw, ih },
309 in->linesize[i], (NppiRect){ 0, 0, iw, ih },
310 out->data[i], out->linesize[i],
311 (NppiRect){ 0, 0, ow, oh },
312 angle, shiftw, shifth, NPPI_INTER_NN);
313 if (err != NPP_SUCCESS) {
314 av_log(ctx, AV_LOG_ERROR, "NPP rotate error: %d\n", err);
315 return AVERROR_UNKNOWN;
316 }
317 }
318
319 return 0;
320 }
321
npptranspose_transpose(AVFilterContext * ctx,NPPTransposeStageContext * stage,AVFrame * out,AVFrame * in)322 static int npptranspose_transpose(AVFilterContext *ctx, NPPTransposeStageContext *stage,
323 AVFrame *out, AVFrame *in)
324 {
325 NppStatus err;
326 int i;
327
328 for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
329 int iw = stage->planes_in[i].width;
330 int ih = stage->planes_in[i].height;
331
332 err = nppiTranspose_8u_C1R(in->data[i], in->linesize[i],
333 out->data[i], out->linesize[i],
334 (NppiSize){ iw, ih });
335 if (err != NPP_SUCCESS) {
336 av_log(ctx, AV_LOG_ERROR, "NPP transpose error: %d\n", err);
337 return AVERROR_UNKNOWN;
338 }
339 }
340
341 return 0;
342 }
343
344 static int (*const npptranspose_process[])(AVFilterContext *ctx, NPPTransposeStageContext *stage,
345 AVFrame *out, AVFrame *in) = {
346 [STAGE_ROTATE] = npptranspose_rotate,
347 [STAGE_TRANSPOSE] = npptranspose_transpose
348 };
349
npptranspose_filter(AVFilterContext * ctx,AVFrame * out,AVFrame * in)350 static int npptranspose_filter(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
351 {
352 NPPTransposeContext *s = ctx->priv;
353 AVFrame *src = in;
354 int i, ret, last_stage = -1;
355
356 for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
357 if (!s->stages[i].stage_needed)
358 continue;
359
360 ret = npptranspose_process[i](ctx, &s->stages[i], s->stages[i].frame, src);
361 if (ret < 0)
362 return ret;
363
364 src = s->stages[i].frame;
365 last_stage = i;
366 }
367
368 if (last_stage < 0)
369 return AVERROR_BUG;
370
371 ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
372 if (ret < 0)
373 return ret;
374
375 av_frame_move_ref(out, src);
376 av_frame_move_ref(src, s->tmp_frame);
377
378 ret = av_frame_copy_props(out, in);
379 if (ret < 0)
380 return ret;
381
382 return 0;
383 }
384
npptranspose_filter_frame(AVFilterLink * link,AVFrame * in)385 static int npptranspose_filter_frame(AVFilterLink *link, AVFrame *in)
386 {
387 AVFilterContext *ctx = link->dst;
388 NPPTransposeContext *s = ctx->priv;
389 AVFilterLink *outlink = ctx->outputs[0];
390 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
391 AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
392 AVFrame *out = NULL;
393 CUcontext dummy;
394 int ret = 0;
395
396 if (s->passthrough)
397 return ff_filter_frame(outlink, in);
398
399 out = av_frame_alloc();
400 if (!out) {
401 ret = AVERROR(ENOMEM);
402 goto fail;
403 }
404
405 ret = CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(device_hwctx->cuda_ctx));
406 if (ret < 0)
407 goto fail;
408
409 ret = npptranspose_filter(ctx, out, in);
410
411 CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy));
412 if (ret < 0)
413 goto fail;
414
415 av_frame_free(&in);
416
417 return ff_filter_frame(outlink, out);
418
419 fail:
420 av_frame_free(&in);
421 av_frame_free(&out);
422 return ret;
423 }
424
425 #define OFFSET(x) offsetof(NPPTransposeContext, x)
426 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
427
428 static const AVOption options[] = {
429 { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = NPP_TRANSPOSE_CCLOCK_FLIP }, 0, 3, FLAGS, "dir" },
430 { "cclock_flip", "rotate counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CCLOCK_FLIP }, 0, 0, FLAGS, "dir" },
431 { "clock", "rotate clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CLOCK }, 0, 0, FLAGS, "dir" },
432 { "cclock", "rotate counter-clockwise", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CCLOCK }, 0, 0, FLAGS, "dir" },
433 { "clock_flip", "rotate clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_CLOCK_FLIP }, 0, 0, FLAGS, "dir" },
434 { "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" },
435 { "none", "always apply transposition", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_NONE }, 0, 0, FLAGS, "passthrough" },
436 { "landscape", "preserve landscape geometry", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_LANDSCAPE }, 0, 0, FLAGS, "passthrough" },
437 { "portrait", "preserve portrait geometry", 0, AV_OPT_TYPE_CONST, { .i64 = NPP_TRANSPOSE_PT_TYPE_PORTRAIT }, 0, 0, FLAGS, "passthrough" },
438 { NULL },
439 };
440
441 static const AVClass npptranspose_class = {
442 .class_name = "npptranspose",
443 .item_name = av_default_item_name,
444 .option = options,
445 .version = LIBAVUTIL_VERSION_INT,
446 };
447
448 static const AVFilterPad npptranspose_inputs[] = {
449 {
450 .name = "default",
451 .type = AVMEDIA_TYPE_VIDEO,
452 .filter_frame = npptranspose_filter_frame,
453 },
454 };
455
456 static const AVFilterPad npptranspose_outputs[] = {
457 {
458 .name = "default",
459 .type = AVMEDIA_TYPE_VIDEO,
460 .config_props = npptranspose_config_props,
461 },
462 };
463
464 const AVFilter ff_vf_transpose_npp = {
465 .name = "transpose_npp",
466 .description = NULL_IF_CONFIG_SMALL("NVIDIA Performance Primitives video transpose"),
467 .init = npptranspose_init,
468 .uninit = npptranspose_uninit,
469 .priv_size = sizeof(NPPTransposeContext),
470 .priv_class = &npptranspose_class,
471 FILTER_INPUTS(npptranspose_inputs),
472 FILTER_OUTPUTS(npptranspose_outputs),
473 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_CUDA),
474 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
475 };
476