]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_vpp_qsv.c
lavu: Add OpenCL hardware pixfmt
[ffmpeg] / libavfilter / vf_vpp_qsv.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 /**
20  ** @file
21  ** Hardware accelerated common filters based on Intel Quick Sync Video VPP
22  **/
23
24 #include <float.h>
25
26 #include "libavutil/opt.h"
27 #include "libavutil/eval.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixdesc.h"
30
31 #include "formats.h"
32 #include "internal.h"
33 #include "avfilter.h"
34 #include "libavcodec/avcodec.h"
35 #include "libavformat/avformat.h"
36
37 #include "qsvvpp.h"
38
39 #define OFFSET(x) offsetof(VPPContext, x)
40 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
41
42 /* number of video enhancement filters */
43 #define ENH_FILTERS_COUNT (5)
44
45 typedef struct VPPContext{
46     const AVClass *class;
47
48     QSVVPPContext *qsv;
49
50     /* Video Enhancement Algorithms */
51     mfxExtVPPDeinterlacing  deinterlace_conf;
52     mfxExtVPPFrameRateConversion frc_conf;
53     mfxExtVPPDenoise denoise_conf;
54     mfxExtVPPDetail detail_conf;
55     mfxExtVPPProcAmp procamp_conf;
56
57     int out_width;
58     int out_height;
59
60     AVRational framerate;       /* target framerate */
61     int use_frc;                /* use framerate conversion */
62     int deinterlace;            /* deinterlace mode : 0=off, 1=bob, 2=advanced */
63     int denoise;                /* Enable Denoise algorithm. Value [0, 100] */
64     int detail;                 /* Enable Detail Enhancement algorithm. */
65                                 /* Level is the optional, value [0, 100] */
66     int use_crop;               /* 1 = use crop; 0=none */
67     int crop_w;
68     int crop_h;
69     int crop_x;
70     int crop_y;
71
72     /* param for the procamp */
73     int    procamp;            /* enable procamp */
74     float  hue;
75     float  saturation;
76     float  contrast;
77     float  brightness;
78
79     char *cx, *cy, *cw, *ch;
80     char *ow, *oh;
81 } VPPContext;
82
83 static const AVOption options[] = {
84     { "deinterlace", "deinterlace mode: 0=off, 1=bob, 2=advanced", OFFSET(deinterlace), AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, MFX_DEINTERLACING_ADVANCED, .flags = FLAGS, "deinterlace" },
85     { "bob",         "Bob deinterlace mode.",                      0,                   AV_OPT_TYPE_CONST,    { .i64 = MFX_DEINTERLACING_BOB },            .flags = FLAGS, "deinterlace" },
86     { "advanced",    "Advanced deinterlace mode. ",                0,                   AV_OPT_TYPE_CONST,    { .i64 = MFX_DEINTERLACING_ADVANCED },       .flags = FLAGS, "deinterlace" },
87
88     { "denoise",     "denoise level [0, 100]",       OFFSET(denoise),     AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, 100, .flags = FLAGS },
89     { "detail",      "enhancement level [0, 100]",   OFFSET(detail),      AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, 100, .flags = FLAGS },
90     { "framerate",   "output framerate",             OFFSET(framerate),   AV_OPT_TYPE_RATIONAL, { .dbl = 0.0 },0, DBL_MAX, .flags = FLAGS },
91     { "procamp",     "Enable ProcAmp",               OFFSET(procamp),     AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, 1, .flags = FLAGS},
92     { "hue",         "ProcAmp hue",                  OFFSET(hue),         AV_OPT_TYPE_FLOAT,    { .dbl = 0.0 }, -180.0, 180.0, .flags = FLAGS},
93     { "saturation",  "ProcAmp saturation",           OFFSET(saturation),  AV_OPT_TYPE_FLOAT,    { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
94     { "contrast",    "ProcAmp contrast",             OFFSET(contrast),    AV_OPT_TYPE_FLOAT,    { .dbl = 1.0 }, 0.0, 10.0, .flags = FLAGS},
95     { "brightness",  "ProcAmp brightness",           OFFSET(brightness),  AV_OPT_TYPE_FLOAT,    { .dbl = 0.0 }, -100.0, 100.0, .flags = FLAGS},
96
97     { "cw",   "set the width crop area expression",   OFFSET(cw), AV_OPT_TYPE_STRING, { .str = "iw" }, CHAR_MIN, CHAR_MAX, FLAGS },
98     { "ch",   "set the height crop area expression",  OFFSET(ch), AV_OPT_TYPE_STRING, { .str = "ih" }, CHAR_MIN, CHAR_MAX, FLAGS },
99     { "cx",   "set the x crop area expression",       OFFSET(cx), AV_OPT_TYPE_STRING, { .str = "(in_w-out_w)/2" }, CHAR_MIN, CHAR_MAX, FLAGS },
100     { "cy",   "set the y crop area expression",       OFFSET(cy), AV_OPT_TYPE_STRING, { .str = "(in_h-out_h)/2" }, CHAR_MIN, CHAR_MAX, FLAGS },
101
102     { "w",      "Output video width",  OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
103     { "width",  "Output video width",  OFFSET(ow), AV_OPT_TYPE_STRING, { .str="cw" }, 0, 255, .flags = FLAGS },
104     { "h",      "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
105     { "height", "Output video height", OFFSET(oh), AV_OPT_TYPE_STRING, { .str="w*ch/cw" }, 0, 255, .flags = FLAGS },
106     { NULL }
107 };
108
109 static const char *const var_names[] = {
110     "iw", "in_w",
111     "ih", "in_h",
112     "ow", "out_w", "w",
113     "oh", "out_h", "h",
114     "cw",
115     "ch",
116     "cx",
117     "cy",
118     NULL
119 };
120
121 enum var_name {
122     VAR_iW, VAR_IN_W,
123     VAR_iH, VAR_IN_H,
124     VAR_oW, VAR_OUT_W, VAR_W,
125     VAR_oH, VAR_OUT_H, VAR_H,
126     CW,
127     CH,
128     CX,
129     CY,
130     VAR_VARS_NB
131 };
132
133 static int eval_expr(AVFilterContext *ctx)
134 {
135 #define PASS_EXPR(e, s) {\
136     ret = av_expr_parse(&e, s, var_names, NULL, NULL, NULL, NULL, 0, ctx); \
137     if (ret < 0) {\
138         av_log(ctx, AV_LOG_ERROR, "Error when passing '%s'.\n", s);\
139         goto release;\
140     }\
141 }
142 #define CALC_EXPR(e, v, i) {\
143     i = v = av_expr_eval(e, var_values, NULL); \
144 }
145     VPPContext *vpp = ctx->priv;
146     double  var_values[VAR_VARS_NB] = { NAN };
147     AVExpr *w_expr  = NULL, *h_expr  = NULL;
148     AVExpr *cw_expr = NULL, *ch_expr = NULL;
149     AVExpr *cx_expr = NULL, *cy_expr = NULL;
150     int     ret = 0;
151
152     PASS_EXPR(cw_expr, vpp->cw);
153     PASS_EXPR(ch_expr, vpp->ch);
154
155     PASS_EXPR(w_expr, vpp->ow);
156     PASS_EXPR(h_expr, vpp->oh);
157
158     PASS_EXPR(cx_expr, vpp->cx);
159     PASS_EXPR(cy_expr, vpp->cy);
160
161     var_values[VAR_iW] =
162     var_values[VAR_IN_W] = ctx->inputs[0]->w;
163
164     var_values[VAR_iH] =
165     var_values[VAR_IN_H] = ctx->inputs[0]->h;
166
167     /* crop params */
168     CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
169     CALC_EXPR(ch_expr, var_values[CH], vpp->crop_h);
170
171     /* calc again in case cw is relative to ch */
172     CALC_EXPR(cw_expr, var_values[CW], vpp->crop_w);
173
174     CALC_EXPR(w_expr,
175             var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
176             vpp->out_width);
177     CALC_EXPR(h_expr,
178             var_values[VAR_OUT_H] = var_values[VAR_oH] = var_values[VAR_H],
179             vpp->out_height);
180
181     /* calc again in case ow is relative to oh */
182     CALC_EXPR(w_expr,
183             var_values[VAR_OUT_W] = var_values[VAR_oW] = var_values[VAR_W],
184             vpp->out_width);
185
186
187     CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
188     CALC_EXPR(cy_expr, var_values[CY], vpp->crop_y);
189
190     /* calc again in case cx is relative to cy */
191     CALC_EXPR(cx_expr, var_values[CX], vpp->crop_x);
192
193     if ((vpp->crop_w != var_values[VAR_iW]) || (vpp->crop_h != var_values[VAR_iH]))
194         vpp->use_crop = 1;
195
196 release:
197     av_expr_free(w_expr);
198     av_expr_free(h_expr);
199     av_expr_free(cw_expr);
200     av_expr_free(ch_expr);
201     av_expr_free(cx_expr);
202     av_expr_free(cy_expr);
203 #undef PASS_EXPR
204 #undef CALC_EXPR
205
206     return ret;
207 }
208
209 static int config_input(AVFilterLink *inlink)
210 {
211     AVFilterContext *ctx = inlink->dst;
212     VPPContext      *vpp = ctx->priv;
213     int              ret;
214
215     if (vpp->framerate.den == 0 || vpp->framerate.num == 0)
216         vpp->framerate = inlink->frame_rate;
217
218     if (av_cmp_q(vpp->framerate, inlink->frame_rate))
219         vpp->use_frc = 1;
220
221     ret = eval_expr(ctx);
222     if (ret != 0) {
223         av_log(ctx, AV_LOG_ERROR, "Fail to eval expr.\n");
224         return ret;
225     }
226
227     if (vpp->out_height == 0 || vpp->out_width == 0) {
228         vpp->out_width  = inlink->w;
229         vpp->out_height = inlink->h;
230     }
231
232     if (vpp->use_crop) {
233         vpp->crop_x = FFMAX(vpp->crop_x, 0);
234         vpp->crop_y = FFMAX(vpp->crop_y, 0);
235
236         if(vpp->crop_w + vpp->crop_x > inlink->w)
237            vpp->crop_x = inlink->w - vpp->crop_w;
238         if(vpp->crop_h + vpp->crop_y > inlink->h)
239            vpp->crop_y = inlink->h - vpp->crop_h;
240     }
241
242     return 0;
243 }
244
245 static int config_output(AVFilterLink *outlink)
246 {
247     AVFilterContext *ctx = outlink->src;
248     VPPContext      *vpp = ctx->priv;
249     QSVVPPParam     param = { NULL };
250     QSVVPPCrop      crop  = { 0 };
251     mfxExtBuffer    *ext_buf[ENH_FILTERS_COUNT];
252
253     outlink->w          = vpp->out_width;
254     outlink->h          = vpp->out_height;
255     outlink->frame_rate = vpp->framerate;
256     outlink->time_base  = av_inv_q(vpp->framerate);
257
258     param.filter_frame  = NULL;
259     param.out_sw_format = AV_PIX_FMT_NV12;
260     param.num_ext_buf   = 0;
261     param.ext_buf       = ext_buf;
262
263     if (vpp->use_crop) {
264         crop.in_idx = 0;
265         crop.x = vpp->crop_x;
266         crop.y = vpp->crop_y;
267         crop.w = vpp->crop_w;
268         crop.h = vpp->crop_h;
269
270         param.num_crop = 1;
271         param.crop     = &crop;
272     }
273
274     if (vpp->deinterlace) {
275         memset(&vpp->deinterlace_conf, 0, sizeof(mfxExtVPPDeinterlacing));
276         vpp->deinterlace_conf.Header.BufferId = MFX_EXTBUFF_VPP_DEINTERLACING;
277         vpp->deinterlace_conf.Header.BufferSz = sizeof(mfxExtVPPDeinterlacing);
278         vpp->deinterlace_conf.Mode = vpp->deinterlace == 1 ?
279                                      MFX_DEINTERLACING_BOB : MFX_DEINTERLACING_ADVANCED;
280
281         param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->deinterlace_conf;
282     }
283
284     if (vpp->use_frc) {
285         memset(&vpp->frc_conf, 0, sizeof(mfxExtVPPFrameRateConversion));
286         vpp->frc_conf.Header.BufferId = MFX_EXTBUFF_VPP_FRAME_RATE_CONVERSION;
287         vpp->frc_conf.Header.BufferSz = sizeof(mfxExtVPPFrameRateConversion);
288         vpp->frc_conf.Algorithm = MFX_FRCALGM_DISTRIBUTED_TIMESTAMP;
289
290         param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->frc_conf;
291     }
292
293     if (vpp->denoise) {
294         memset(&vpp->denoise_conf, 0, sizeof(mfxExtVPPDenoise));
295         vpp->denoise_conf.Header.BufferId = MFX_EXTBUFF_VPP_DENOISE;
296         vpp->denoise_conf.Header.BufferSz = sizeof(mfxExtVPPDenoise);
297         vpp->denoise_conf.DenoiseFactor   = vpp->denoise;
298
299         param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->denoise_conf;
300     }
301
302     if (vpp->detail) {
303         memset(&vpp->detail_conf, 0, sizeof(mfxExtVPPDetail));
304         vpp->detail_conf.Header.BufferId  = MFX_EXTBUFF_VPP_DETAIL;
305         vpp->detail_conf.Header.BufferSz  = sizeof(mfxExtVPPDetail);
306         vpp->detail_conf.DetailFactor = vpp->detail;
307
308         param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->detail_conf;
309     }
310
311     if (vpp->procamp) {
312         memset(&vpp->procamp_conf, 0, sizeof(mfxExtVPPProcAmp));
313         vpp->procamp_conf.Header.BufferId  = MFX_EXTBUFF_VPP_PROCAMP;
314         vpp->procamp_conf.Header.BufferSz  = sizeof(mfxExtVPPProcAmp);
315         vpp->procamp_conf.Hue              = vpp->hue;
316         vpp->procamp_conf.Saturation       = vpp->saturation;
317         vpp->procamp_conf.Contrast         = vpp->contrast;
318         vpp->procamp_conf.Brightness       = vpp->brightness;
319
320         param.ext_buf[param.num_ext_buf++] = (mfxExtBuffer*)&vpp->procamp_conf;
321     }
322
323     return ff_qsvvpp_create(ctx, &vpp->qsv, &param);
324 }
325
326 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
327 {
328     VPPContext *vpp = inlink->dst->priv;
329
330     return ff_qsvvpp_filter_frame(vpp->qsv, inlink, picref);
331 }
332
333 static int query_formats(AVFilterContext *ctx)
334 {
335     int ret;
336     AVFilterFormats *in_fmts, *out_fmts;
337     static const enum AVPixelFormat in_pix_fmts[] = {
338         AV_PIX_FMT_YUV420P,
339         AV_PIX_FMT_NV12,
340         AV_PIX_FMT_YUYV422,
341         AV_PIX_FMT_RGB32,
342         AV_PIX_FMT_QSV,
343         AV_PIX_FMT_NONE
344     };
345     static const enum AVPixelFormat out_pix_fmts[] = {
346         AV_PIX_FMT_NV12,
347         AV_PIX_FMT_QSV,
348         AV_PIX_FMT_NONE
349     };
350
351     in_fmts  = ff_make_format_list(in_pix_fmts);
352     out_fmts = ff_make_format_list(out_pix_fmts);
353     ret = ff_formats_ref(in_fmts, &ctx->inputs[0]->out_formats);
354     if (ret < 0)
355         return ret;
356     ret = ff_formats_ref(out_fmts, &ctx->outputs[0]->in_formats);
357     if (ret < 0)
358         return ret;
359
360     return 0;
361 }
362
363 static av_cold void vpp_uninit(AVFilterContext *ctx)
364 {
365     VPPContext *vpp = ctx->priv;
366
367     ff_qsvvpp_free(&vpp->qsv);
368 }
369
370 static const AVClass vpp_class = {
371     .class_name = "vpp_qsv",
372     .item_name  = av_default_item_name,
373     .option     = options,
374     .version    = LIBAVUTIL_VERSION_INT,
375 };
376
377 static const AVFilterPad vpp_inputs[] = {
378     {
379         .name          = "default",
380         .type          = AVMEDIA_TYPE_VIDEO,
381         .config_props  = config_input,
382         .filter_frame  = filter_frame,
383     },
384     { NULL }
385 };
386
387 static const AVFilterPad vpp_outputs[] = {
388     {
389         .name          = "default",
390         .type          = AVMEDIA_TYPE_VIDEO,
391         .config_props  = config_output,
392     },
393     { NULL }
394 };
395
396 AVFilter ff_vf_vpp_qsv = {
397     .name          = "vpp_qsv",
398     .description   = NULL_IF_CONFIG_SMALL("Quick Sync Video VPP."),
399     .priv_size     = sizeof(VPPContext),
400     .query_formats = query_formats,
401     .uninit        = vpp_uninit,
402     .inputs        = vpp_inputs,
403     .outputs       = vpp_outputs,
404     .priv_class    = &vpp_class,
405     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
406 };