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