]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_transpose_npp.c
3ea031667caf4fddaca21b22777f01859d19a9a4
[ffmpeg] / libavfilter / vf_transpose_npp.c
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
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
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
114 static int npptranspose_query_formats(AVFilterContext *ctx)
115 {
116     static const enum AVPixelFormat pixel_formats[] = {
117         AV_PIX_FMT_CUDA, AV_PIX_FMT_NONE,
118     };
119
120     AVFilterFormats *pix_fmts = ff_make_format_list(pixel_formats);
121     return ff_set_common_formats(ctx, pix_fmts);
122 }
123
124 static int init_stage(NPPTransposeStageContext *stage, AVBufferRef *device_ctx)
125 {
126     AVBufferRef *out_ref = NULL;
127     AVHWFramesContext *out_ctx;
128     int in_sw, in_sh, out_sw, out_sh;
129     int ret, i;
130
131     av_pix_fmt_get_chroma_sub_sample(stage->in_fmt,  &in_sw,  &in_sh);
132     av_pix_fmt_get_chroma_sub_sample(stage->out_fmt, &out_sw, &out_sh);
133
134     if (!stage->planes_out[0].width) {
135         stage->planes_out[0].width  = stage->planes_in[0].width;
136         stage->planes_out[0].height = stage->planes_in[0].height;
137     }
138
139     for (i = 1; i < FF_ARRAY_ELEMS(stage->planes_in); i++) {
140         stage->planes_in[i].width   = stage->planes_in[0].width   >> in_sw;
141         stage->planes_in[i].height  = stage->planes_in[0].height  >> in_sh;
142         stage->planes_out[i].width  = stage->planes_out[0].width  >> out_sw;
143         stage->planes_out[i].height = stage->planes_out[0].height >> out_sh;
144     }
145
146     out_ref = av_hwframe_ctx_alloc(device_ctx);
147     if (!out_ref)
148         return AVERROR(ENOMEM);
149     out_ctx = (AVHWFramesContext*)out_ref->data;
150
151     out_ctx->format    = AV_PIX_FMT_CUDA;
152     out_ctx->sw_format = stage->out_fmt;
153     out_ctx->width     = FFALIGN(stage->planes_out[0].width,  32);
154     out_ctx->height    = FFALIGN(stage->planes_out[0].height, 32);
155
156     ret = av_hwframe_ctx_init(out_ref);
157     if (ret < 0)
158         goto fail;
159
160     av_frame_unref(stage->frame);
161     ret = av_hwframe_get_buffer(out_ref, stage->frame, 0);
162     if (ret < 0)
163         goto fail;
164
165     stage->frame->width  = stage->planes_out[0].width;
166     stage->frame->height = stage->planes_out[0].height;
167     av_buffer_unref(&stage->frames_ctx);
168     stage->frames_ctx = out_ref;
169
170     return 0;
171
172 fail:
173     av_buffer_unref(&out_ref);
174     return ret;
175 }
176
177 static int format_is_supported(enum AVPixelFormat fmt)
178 {
179     int i;
180
181     for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
182         if (supported_formats[i] == fmt)
183             return 1;
184
185     return 0;
186 }
187
188 static int init_processing_chain(AVFilterContext *ctx, int in_width, int in_height,
189                                  int out_width, int out_height)
190 {
191     NPPTransposeContext *s = ctx->priv;
192     AVHWFramesContext *in_frames_ctx;
193     enum AVPixelFormat format;
194     int i, ret, last_stage = -1;
195     int rot_width = out_width, rot_height = out_height;
196
197     /* check that we have a hw context */
198     if (!ctx->inputs[0]->hw_frames_ctx) {
199         av_log(ctx, AV_LOG_ERROR, "No hw context provided on input\n");
200         return AVERROR(EINVAL);
201     }
202
203     in_frames_ctx = (AVHWFramesContext*)ctx->inputs[0]->hw_frames_ctx->data;
204     format        = in_frames_ctx->sw_format;
205
206     if (!format_is_supported(format)) {
207         av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n",
208                av_get_pix_fmt_name(format));
209         return AVERROR(ENOSYS);
210     }
211
212     if (s->dir != NPP_TRANSPOSE_CCLOCK_FLIP) {
213         s->stages[STAGE_ROTATE].stage_needed = 1;
214     }
215
216     if (s->dir == NPP_TRANSPOSE_CCLOCK_FLIP || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) {
217         s->stages[STAGE_TRANSPOSE].stage_needed = 1;
218
219         /* Rotating by 180° in case of clock_flip, or not at all for cclock_flip, so width/height unchanged by rotation */
220         rot_width = in_width;
221         rot_height = in_height;
222     }
223
224     s->stages[STAGE_ROTATE].in_fmt               = format;
225     s->stages[STAGE_ROTATE].out_fmt              = format;
226     s->stages[STAGE_ROTATE].planes_in[0].width   = in_width;
227     s->stages[STAGE_ROTATE].planes_in[0].height  = in_height;
228     s->stages[STAGE_ROTATE].planes_out[0].width  = rot_width;
229     s->stages[STAGE_ROTATE].planes_out[0].height = rot_height;
230     s->stages[STAGE_TRANSPOSE].in_fmt               = format;
231     s->stages[STAGE_TRANSPOSE].out_fmt              = format;
232     s->stages[STAGE_TRANSPOSE].planes_in[0].width   = rot_width;
233     s->stages[STAGE_TRANSPOSE].planes_in[0].height  = rot_height;
234     s->stages[STAGE_TRANSPOSE].planes_out[0].width  = out_width;
235     s->stages[STAGE_TRANSPOSE].planes_out[0].height = out_height;
236
237     /* init the hardware contexts */
238     for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
239         if (!s->stages[i].stage_needed)
240             continue;
241         ret = init_stage(&s->stages[i], in_frames_ctx->device_ref);
242         if (ret < 0)
243             return ret;
244         last_stage = i;
245     }
246
247     if (last_stage >= 0) {
248         ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->stages[last_stage].frames_ctx);
249     } else {
250         ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(ctx->inputs[0]->hw_frames_ctx);
251         s->passthrough = 1;
252     }
253
254     if (!ctx->outputs[0]->hw_frames_ctx)
255         return AVERROR(ENOMEM);
256
257     return 0;
258 }
259
260 static int npptranspose_config_props(AVFilterLink *outlink)
261 {
262     AVFilterContext *ctx   = outlink->src;
263     AVFilterLink *inlink   = ctx->inputs[0];
264     NPPTransposeContext *s = ctx->priv;
265     int ret;
266
267     if ((inlink->w >= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_LANDSCAPE) ||
268         (inlink->w <= inlink->h && s->passthrough == NPP_TRANSPOSE_PT_TYPE_PORTRAIT))
269     {
270         if (inlink->hw_frames_ctx) {
271             outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
272             if (!outlink->hw_frames_ctx)
273                 return AVERROR(ENOMEM);
274         }
275
276         av_log(ctx, AV_LOG_VERBOSE,
277                "w:%d h:%d -> w:%d h:%d (passthrough mode)\n",
278                inlink->w, inlink->h, inlink->w, inlink->h);
279         return 0;
280     } else {
281         s->passthrough = NPP_TRANSPOSE_PT_TYPE_NONE;
282     }
283
284     outlink->w = inlink->h;
285     outlink->h = inlink->w;
286     outlink->sample_aspect_ratio = (AVRational){inlink->sample_aspect_ratio.den, inlink->sample_aspect_ratio.num};
287
288     ret = init_processing_chain(ctx, inlink->w, inlink->h, outlink->w, outlink->h);
289     if (ret < 0)
290         return ret;
291
292     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d -transpose-> w:%d h:%d\n",
293            inlink->w, inlink->h, outlink->w, outlink->h);
294
295     return 0;
296 }
297
298 static int npptranspose_rotate(AVFilterContext *ctx, NPPTransposeStageContext *stage,
299                                AVFrame *out, AVFrame *in)
300 {
301     NPPTransposeContext *s = ctx->priv;
302     NppStatus err;
303     int i;
304
305     for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
306         int iw = stage->planes_in[i].width;
307         int ih = stage->planes_in[i].height;
308         int ow = stage->planes_out[i].width;
309         int oh = stage->planes_out[i].height;
310
311         // nppRotate uses 0,0 as the rotation point
312         // need to shift the image accordingly after rotation
313         // need to substract 1 to get the correct coordinates
314         double angle = s->dir == NPP_TRANSPOSE_CLOCK ? -90.0 : s->dir == NPP_TRANSPOSE_CCLOCK ? 90.0 : 180.0;
315         int shiftw = (s->dir == NPP_TRANSPOSE_CLOCK  || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? ow - 1 : 0;
316         int shifth = (s->dir == NPP_TRANSPOSE_CCLOCK || s->dir == NPP_TRANSPOSE_CLOCK_FLIP) ? oh - 1 : 0;
317
318         err = nppiRotate_8u_C1R(in->data[i], (NppiSize){ iw, ih },
319                                 in->linesize[i], (NppiRect){ 0, 0, iw, ih },
320                                 out->data[i], out->linesize[i],
321                                 (NppiRect){ 0, 0, ow, oh },
322                                 angle, shiftw, shifth, NPPI_INTER_NN);
323         if (err != NPP_SUCCESS) {
324             av_log(ctx, AV_LOG_ERROR, "NPP rotate error: %d\n", err);
325             return AVERROR_UNKNOWN;
326         }
327     }
328
329     return 0;
330 }
331
332 static int npptranspose_transpose(AVFilterContext *ctx, NPPTransposeStageContext *stage,
333                                   AVFrame *out, AVFrame *in)
334 {
335     NppStatus err;
336     int i;
337
338     for (i = 0; i < FF_ARRAY_ELEMS(stage->planes_in) && i < FF_ARRAY_ELEMS(in->data) && in->data[i]; i++) {
339         int iw = stage->planes_in[i].width;
340         int ih = stage->planes_in[i].height;
341
342         err = nppiTranspose_8u_C1R(in->data[i], in->linesize[i],
343                                    out->data[i], out->linesize[i],
344                                    (NppiSize){ iw, ih });
345         if (err != NPP_SUCCESS) {
346             av_log(ctx, AV_LOG_ERROR, "NPP transpose error: %d\n", err);
347             return AVERROR_UNKNOWN;
348         }
349     }
350
351     return 0;
352 }
353
354 static int (*const npptranspose_process[])(AVFilterContext *ctx, NPPTransposeStageContext *stage,
355                                            AVFrame *out, AVFrame *in) = {
356     [STAGE_ROTATE]       = npptranspose_rotate,
357     [STAGE_TRANSPOSE]    = npptranspose_transpose
358 };
359
360 static int npptranspose_filter(AVFilterContext *ctx, AVFrame *out, AVFrame *in)
361 {
362     NPPTransposeContext *s = ctx->priv;
363     AVFrame *src = in;
364     int i, ret, last_stage = -1;
365
366     for (i = 0; i < FF_ARRAY_ELEMS(s->stages); i++) {
367         if (!s->stages[i].stage_needed)
368             continue;
369
370         ret = npptranspose_process[i](ctx, &s->stages[i], s->stages[i].frame, src);
371         if (ret < 0)
372             return ret;
373
374         src        = s->stages[i].frame;
375         last_stage = i;
376     }
377
378     if (last_stage < 0)
379         return AVERROR_BUG;
380
381     ret = av_hwframe_get_buffer(src->hw_frames_ctx, s->tmp_frame, 0);
382     if (ret < 0)
383         return ret;
384
385     av_frame_move_ref(out, src);
386     av_frame_move_ref(src, s->tmp_frame);
387
388     ret = av_frame_copy_props(out, in);
389     if (ret < 0)
390         return ret;
391
392     return 0;
393 }
394
395 static int npptranspose_filter_frame(AVFilterLink *link, AVFrame *in)
396 {
397     AVFilterContext              *ctx = link->dst;
398     NPPTransposeContext            *s = ctx->priv;
399     AVFilterLink             *outlink = ctx->outputs[0];
400     AVHWFramesContext     *frames_ctx = (AVHWFramesContext*)outlink->hw_frames_ctx->data;
401     AVCUDADeviceContext *device_hwctx = frames_ctx->device_ctx->hwctx;
402     AVFrame *out = NULL;
403     CUcontext dummy;
404     int ret = 0;
405
406     if (s->passthrough)
407         return ff_filter_frame(outlink, in);
408
409     out = av_frame_alloc();
410     if (!out) {
411         ret = AVERROR(ENOMEM);
412         goto fail;
413     }
414
415     ret = CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPushCurrent(device_hwctx->cuda_ctx));
416     if (ret < 0)
417         goto fail;
418
419     ret = npptranspose_filter(ctx, out, in);
420
421     CHECK_CU(device_hwctx->internal->cuda_dl->cuCtxPopCurrent(&dummy));
422     if (ret < 0)
423         goto fail;
424
425     av_frame_free(&in);
426
427     return ff_filter_frame(outlink, out);
428
429 fail:
430     av_frame_free(&in);
431     av_frame_free(&out);
432     return ret;
433 }
434
435 #define OFFSET(x) offsetof(NPPTransposeContext, x)
436 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
437
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" },
448     { NULL },
449 };
450
451 static const AVClass npptranspose_class = {
452     .class_name = "npptranspose",
453     .item_name  = av_default_item_name,
454     .option     = options,
455     .version    = LIBAVUTIL_VERSION_INT,
456 };
457
458 static const AVFilterPad npptranspose_inputs[] = {
459     {
460         .name         = "default",
461         .type         = AVMEDIA_TYPE_VIDEO,
462         .filter_frame = npptranspose_filter_frame,
463     },
464     { NULL }
465 };
466
467 static const AVFilterPad npptranspose_outputs[] = {
468     {
469         .name         = "default",
470         .type         = AVMEDIA_TYPE_VIDEO,
471         .config_props = npptranspose_config_props,
472     },
473     { NULL }
474 };
475
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,
487 };