]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_lut2.c
lavfi: regroup formats lists in a single structure.
[ffmpeg] / libavfilter / vf_lut2.c
1 /*
2  * Copyright (c) 2016 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/attributes.h"
22 #include "libavutil/common.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
26 #include "avfilter.h"
27 #include "drawutils.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31 #include "framesync.h"
32
33 static const char *const var_names[] = {
34     "w",        ///< width of the input video
35     "h",        ///< height of the input video
36     "x",        ///< input value for the pixel from input #1
37     "y",        ///< input value for the pixel from input #2
38     "bdx",      ///< input #1 video bitdepth
39     "bdy",      ///< input #2 video bitdepth
40     NULL
41 };
42
43 enum var_name {
44     VAR_W,
45     VAR_H,
46     VAR_X,
47     VAR_Y,
48     VAR_BITDEPTHX,
49     VAR_BITDEPTHY,
50     VAR_VARS_NB
51 };
52
53 typedef struct LUT2Context {
54     const AVClass *class;
55     FFFrameSync fs;
56
57     int odepth;
58     char   *comp_expr_str[4];
59
60     AVExpr *comp_expr[4];
61     double var_values[VAR_VARS_NB];
62     uint16_t *lut[4];  ///< lookup table for each component
63     int width[4], height[4];
64     int widthx[4], heightx[4];
65     int widthy[4], heighty[4];
66     int nb_planesx;
67     int nb_planesy;
68     int nb_planes;
69     int depth, depthx, depthy;
70     int tlut2;
71     AVFrame *prev_frame;        /* only used with tlut2 */
72
73     int (*lut2)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
74 } LUT2Context;
75
76 typedef struct ThreadData {
77     AVFrame *out, *srcx, *srcy;
78 } ThreadData;
79
80 #define OFFSET(x) offsetof(LUT2Context, x)
81 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
82
83 static const AVOption options[] = {
84     { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
85     { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
86     { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
87     { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
88     { "d",  "set output depth",            OFFSET(odepth),            AV_OPT_TYPE_INT,    { .i64 =  0  }, 0, 16, .flags = FLAGS },
89     { NULL }
90 };
91
92 static av_cold void uninit(AVFilterContext *ctx)
93 {
94     LUT2Context *s = ctx->priv;
95     int i;
96
97     ff_framesync_uninit(&s->fs);
98     av_frame_free(&s->prev_frame);
99
100     for (i = 0; i < 4; i++) {
101         av_expr_free(s->comp_expr[i]);
102         s->comp_expr[i] = NULL;
103         av_freep(&s->comp_expr_str[i]);
104         av_freep(&s->lut[i]);
105     }
106 }
107
108 #define BIT8_FMTS \
109     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, \
110     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, \
111     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, \
112     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, \
113     AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, \
114     AV_PIX_FMT_GRAY8, AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
115
116 #define BIT9_FMTS \
117     AV_PIX_FMT_GBRP9, AV_PIX_FMT_GRAY9, \
118     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
119     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
120
121 #define BIT10_FMTS \
122     AV_PIX_FMT_GRAY10, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, \
123     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
124     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
125
126 #define BIT12_FMTS \
127     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, \
128     AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12, \
129     AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP12,
130
131 #define BIT14_FMTS \
132     AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
133     AV_PIX_FMT_GRAY14, AV_PIX_FMT_GBRP14,
134
135 #define BIT16_FMTS \
136     AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
137     AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, \
138     AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
139
140 static int query_formats(AVFilterContext *ctx)
141 {
142     LUT2Context *s = ctx->priv;
143     static const enum AVPixelFormat all_pix_fmts[] = {
144         BIT8_FMTS
145         BIT9_FMTS
146         BIT10_FMTS
147         BIT12_FMTS
148         AV_PIX_FMT_NONE
149     };
150     static const enum AVPixelFormat bit8_pix_fmts[] = {
151         BIT8_FMTS
152         AV_PIX_FMT_NONE
153     };
154     static const enum AVPixelFormat bit9_pix_fmts[] = {
155         BIT9_FMTS
156         AV_PIX_FMT_NONE
157     };
158     static const enum AVPixelFormat bit10_pix_fmts[] = {
159         BIT10_FMTS
160         AV_PIX_FMT_NONE
161     };
162     static const enum AVPixelFormat bit12_pix_fmts[] = {
163         BIT12_FMTS
164         AV_PIX_FMT_NONE
165     };
166     static const enum AVPixelFormat bit14_pix_fmts[] = {
167         BIT14_FMTS
168         AV_PIX_FMT_NONE
169     };
170     static const enum AVPixelFormat bit16_pix_fmts[] = {
171         BIT16_FMTS
172         AV_PIX_FMT_NONE
173     };
174     const enum AVPixelFormat *pix_fmts;
175     int ret;
176
177     if (s->tlut2 || !s->odepth)
178         return ff_set_common_formats(ctx, ff_make_format_list(all_pix_fmts));
179
180     ret = ff_formats_ref(ff_make_format_list(all_pix_fmts), &ctx->inputs[0]->outcfg.formats);
181     if (ret < 0)
182         return ret;
183
184     switch (s->odepth) {
185     case 8:  pix_fmts = bit8_pix_fmts;  break;
186     case 9:  pix_fmts = bit9_pix_fmts;  break;
187     case 10: pix_fmts = bit10_pix_fmts; break;
188     case 12: pix_fmts = bit12_pix_fmts; break;
189     case 14: pix_fmts = bit14_pix_fmts; break;
190     case 16: pix_fmts = bit16_pix_fmts; break;
191     default: av_log(ctx, AV_LOG_ERROR, "Unsupported output bit depth %d.\n", s->odepth);
192              return AVERROR(EINVAL);
193     }
194
195     return ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->outputs[0]->incfg.formats);
196 }
197
198 static int config_inputx(AVFilterLink *inlink)
199 {
200     AVFilterContext *ctx = inlink->dst;
201     LUT2Context *s = ctx->priv;
202     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
203     int hsub = desc->log2_chroma_w;
204     int vsub = desc->log2_chroma_h;
205
206     s->nb_planesx = av_pix_fmt_count_planes(inlink->format);
207     s->heightx[1] = s->heightx[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
208     s->heightx[0] = s->heightx[3] = inlink->h;
209     s->widthx[1]  = s->widthx[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
210     s->widthx[0]  = s->widthx[3]  = inlink->w;
211
212     s->var_values[VAR_W] = inlink->w;
213     s->var_values[VAR_H] = inlink->h;
214     s->depthx = desc->comp[0].depth;
215     s->var_values[VAR_BITDEPTHX] = s->depthx;
216
217     if (s->tlut2) {
218         s->depthy = desc->comp[0].depth;
219         s->var_values[VAR_BITDEPTHY] = s->depthy;
220     }
221
222     return 0;
223 }
224
225 static int config_inputy(AVFilterLink *inlink)
226 {
227     AVFilterContext *ctx = inlink->dst;
228     LUT2Context *s = ctx->priv;
229     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
230     int hsub = desc->log2_chroma_w;
231     int vsub = desc->log2_chroma_h;
232
233     s->nb_planesy = av_pix_fmt_count_planes(inlink->format);
234     s->depthy = desc->comp[0].depth;
235     s->var_values[VAR_BITDEPTHY] = s->depthy;
236     s->heighty[1] = s->heighty[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
237     s->heighty[0] = s->heighty[3] = inlink->h;
238     s->widthy[1]  = s->widthy[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
239     s->widthy[0]  = s->widthy[3]  = inlink->w;
240
241     return 0;
242 }
243
244 #define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv)  \
245 static int lut2_##zname##_##xname##_##yname(AVFilterContext *ctx,                \
246                                              void *arg,                          \
247                                              int jobnr, int nb_jobs)             \
248 {                                                                                \
249     LUT2Context *s = ctx->priv;                                                  \
250     ThreadData *td = arg;                                                        \
251     AVFrame *out = td->out;                                                      \
252     AVFrame *srcx = td->srcx;                                                    \
253     AVFrame *srcy = td->srcy;                                                    \
254     const int odepth = s->odepth;                                                \
255     int p, y, x;                                                                 \
256                                                                                  \
257     for (p = 0; p < s->nb_planes; p++) {                                         \
258         const int slice_start = (s->heightx[p] * jobnr) / nb_jobs;               \
259         const int slice_end = (s->heightx[p] * (jobnr+1)) / nb_jobs;             \
260         const uint16_t *lut = s->lut[p];                                         \
261         const xtype *srcxx;                                                      \
262         const ytype *srcyy;                                                      \
263         ztype *dst;                                                              \
264                                                                                  \
265         dst   = (ztype *)(out->data[p] + slice_start * out->linesize[p]);        \
266         srcxx = (const xtype *)(srcx->data[p] + slice_start * srcx->linesize[p]);\
267         srcyy = (const ytype *)(srcy->data[p] + slice_start * srcy->linesize[p]);\
268                                                                                  \
269         for (y = slice_start; y < slice_end; y++) {                              \
270             for (x = 0; x < s->widthx[p]; x++) {                                 \
271                 dst[x] = av_clip_uintp2_c(lut[(srcyy[x] << s->depthx) | srcxx[x]], odepth); \
272             }                                                                    \
273                                                                                  \
274             dst   += out->linesize[p] / zdiv;                                    \
275             srcxx += srcx->linesize[p] / xdiv;                                   \
276             srcyy += srcy->linesize[p] / ydiv;                                   \
277         }                                                                        \
278     }                                                                            \
279     return 0;                                                                    \
280 }
281
282 DEFINE_LUT2(8,   8,  8,  uint8_t,  uint8_t,  uint8_t, 1, 1, 1)
283 DEFINE_LUT2(8,   8, 16,  uint8_t,  uint8_t, uint16_t, 1, 1, 2)
284 DEFINE_LUT2(8,  16,  8,  uint8_t, uint16_t,  uint8_t, 1, 2, 1)
285 DEFINE_LUT2(8,  16, 16,  uint8_t, uint16_t, uint16_t, 1, 2, 2)
286 DEFINE_LUT2(16,  8,  8, uint16_t,  uint8_t,  uint8_t, 2, 1, 1)
287 DEFINE_LUT2(16,  8, 16, uint16_t,  uint8_t, uint16_t, 2, 1, 2)
288 DEFINE_LUT2(16, 16,  8, uint16_t, uint16_t,  uint8_t, 2, 2, 1)
289 DEFINE_LUT2(16, 16, 16, uint16_t, uint16_t, uint16_t, 2, 2, 2)
290
291 static int process_frame(FFFrameSync *fs)
292 {
293     AVFilterContext *ctx = fs->parent;
294     LUT2Context *s = fs->opaque;
295     AVFilterLink *outlink = ctx->outputs[0];
296     AVFrame *out, *srcx = NULL, *srcy = NULL;
297     int ret;
298
299     if ((ret = ff_framesync_get_frame(&s->fs, 0, &srcx, 0)) < 0 ||
300         (ret = ff_framesync_get_frame(&s->fs, 1, &srcy, 0)) < 0)
301         return ret;
302
303     if (ctx->is_disabled || !srcy) {
304         out = av_frame_clone(srcx);
305         if (!out)
306             return AVERROR(ENOMEM);
307     } else {
308         ThreadData td;
309
310         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
311         if (!out)
312             return AVERROR(ENOMEM);
313         av_frame_copy_props(out, srcx);
314
315         td.out  = out;
316         td.srcx = srcx;
317         td.srcy = srcy;
318         ctx->internal->execute(ctx, s->lut2, &td, NULL, FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
319     }
320
321     out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
322
323     return ff_filter_frame(outlink, out);
324 }
325
326 static int config_output(AVFilterLink *outlink)
327 {
328     AVFilterContext *ctx = outlink->src;
329     LUT2Context *s = ctx->priv;
330     int p, ret;
331
332     s->depth = s->depthx + s->depthy;
333     s->nb_planes = s->nb_planesx;
334
335     s->lut2 = s->depth > 16 ? lut2_16_16_16 : lut2_8_8_8;
336     if (s->odepth) {
337         if (s->depthx == 8 && s->depthy == 8 && s->odepth > 8)
338             s->lut2 = lut2_16_8_8;
339         if (s->depthx > 8 && s->depthy == 8 && s->odepth > 8)
340             s->lut2 = lut2_16_16_8;
341         if (s->depthx == 8 && s->depthy > 8 && s->odepth > 8)
342             s->lut2 = lut2_16_8_16;
343         if (s->depthx == 8 && s->depthy == 8 && s->odepth == 8)
344             s->lut2 = lut2_8_8_8;
345         if (s->depthx > 8 && s->depthy == 8 && s->odepth == 8)
346             s->lut2 = lut2_8_16_8;
347         if (s->depthx == 8 && s->depthy > 8 && s->odepth == 8)
348             s->lut2 = lut2_8_8_16;
349         if (s->depthx > 8 && s->depthy > 8 && s->odepth == 8)
350             s->lut2 = lut2_8_16_16;
351     } else {
352         s->odepth = s->depthx;
353     }
354
355     for (p = 0; p < s->nb_planes; p++) {
356         s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
357         if (!s->lut[p])
358             return AVERROR(ENOMEM);
359     }
360
361     for (p = 0; p < s->nb_planes; p++) {
362         double res;
363         int x, y;
364
365         /* create the parsed expression */
366         av_expr_free(s->comp_expr[p]);
367         s->comp_expr[p] = NULL;
368         ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
369                             var_names, NULL, NULL, NULL, NULL, 0, ctx);
370         if (ret < 0) {
371             av_log(ctx, AV_LOG_ERROR,
372                    "Error when parsing the expression '%s' for the component %d.\n",
373                    s->comp_expr_str[p], p);
374             return AVERROR(EINVAL);
375         }
376
377         /* compute the lut */
378         for (y = 0; y < (1 << s->depthy); y++) {
379             s->var_values[VAR_Y] = y;
380             for (x = 0; x < (1 << s->depthx); x++) {
381                 s->var_values[VAR_X] = x;
382                 res = av_expr_eval(s->comp_expr[p], s->var_values, s);
383                 if (isnan(res)) {
384                     av_log(ctx, AV_LOG_ERROR,
385                            "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
386                            s->comp_expr_str[p], x, y, p);
387                     return AVERROR(EINVAL);
388                 }
389
390                 s->lut[p][(y << s->depthx) + x] = res;
391             }
392         }
393     }
394
395     return 0;
396 }
397
398 static int lut2_config_output(AVFilterLink *outlink)
399 {
400     AVFilterContext *ctx = outlink->src;
401     LUT2Context *s = ctx->priv;
402     AVFilterLink *srcx = ctx->inputs[0];
403     AVFilterLink *srcy = ctx->inputs[1];
404     FFFrameSyncIn *in;
405     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
406     int hsub = desc->log2_chroma_w;
407     int vsub = desc->log2_chroma_h;
408     int ret;
409
410     outlink->w = srcx->w;
411     outlink->h = srcx->h;
412     outlink->time_base = srcx->time_base;
413     outlink->sample_aspect_ratio = srcx->sample_aspect_ratio;
414     outlink->frame_rate = srcx->frame_rate;
415
416     s->nb_planes = av_pix_fmt_count_planes(outlink->format);
417     s->height[1] = s->height[2] = AV_CEIL_RSHIFT(outlink->h, vsub);
418     s->height[0] = s->height[3] = outlink->h;
419     s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(outlink->w, hsub);
420     s->width[0]  = s->width[3]  = outlink->w;
421
422     if (!s->odepth && srcx->format != srcy->format) {
423         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
424         return AVERROR(EINVAL);
425     }
426
427     if (srcx->w != srcy->w || srcx->h != srcy->h) {
428         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
429                "(size %dx%d) do not match the corresponding "
430                "second input link %s parameters (size %dx%d)\n",
431                ctx->input_pads[0].name, srcx->w, srcx->h,
432                ctx->input_pads[1].name,
433                srcy->w, srcy->h);
434         return AVERROR(EINVAL);
435     }
436
437     if (s->nb_planesx != s->nb_planesy) {
438         av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
439                "(%d) do not match the corresponding "
440                "second input link %s number of planes (%d)\n",
441                ctx->input_pads[0].name, s->nb_planesx,
442                ctx->input_pads[1].name, s->nb_planesy);
443         return AVERROR(EINVAL);
444     }
445
446     if (s->nb_planesx != s->nb_planes) {
447         av_log(ctx, AV_LOG_ERROR, "First input link %s number of planes "
448                "(%d) do not match the corresponding "
449                "output link %s number of planes (%d)\n",
450                ctx->input_pads[0].name, s->nb_planesx,
451                ctx->output_pads[0].name, s->nb_planes);
452         return AVERROR(EINVAL);
453     }
454
455     if (s->widthx[1] != s->widthy[1] || s->heightx[1] != s->heighty[1]) {
456         av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
457                "(size %dx%d) do not match the corresponding "
458                "second input link %s 2nd plane (size %dx%d)\n",
459                ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
460                ctx->input_pads[1].name,
461                s->widthy[1], s->heighty[1]);
462         return AVERROR(EINVAL);
463     }
464
465     if (s->widthx[2] != s->widthy[2] || s->heightx[2] != s->heighty[2]) {
466         av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
467                "(size %dx%d) do not match the corresponding "
468                "second input link %s 3rd plane (size %dx%d)\n",
469                ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
470                ctx->input_pads[1].name,
471                s->widthy[2], s->heighty[2]);
472         return AVERROR(EINVAL);
473     }
474
475     if (s->widthx[1] != s->width[1] || s->heightx[1] != s->height[1]) {
476         av_log(ctx, AV_LOG_ERROR, "First input link %s 2nd plane "
477                "(size %dx%d) do not match the corresponding "
478                "output link %s 2nd plane (size %dx%d)\n",
479                ctx->input_pads[0].name, s->widthx[1], s->heightx[1],
480                ctx->output_pads[0].name, s->width[1], s->height[1]);
481         return AVERROR(EINVAL);
482     }
483
484     if (s->widthx[2] != s->width[2] || s->heightx[2] != s->height[2]) {
485         av_log(ctx, AV_LOG_ERROR, "First input link %s 3rd plane "
486                "(size %dx%d) do not match the corresponding "
487                "output link %s 3rd plane (size %dx%d)\n",
488                ctx->input_pads[0].name, s->widthx[2], s->heightx[2],
489                ctx->output_pads[0].name, s->width[2], s->height[2]);
490         return AVERROR(EINVAL);
491     }
492
493     if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
494         return ret;
495
496     in = s->fs.in;
497     in[0].time_base = srcx->time_base;
498     in[1].time_base = srcy->time_base;
499     in[0].sync   = 2;
500     in[0].before = EXT_STOP;
501     in[0].after  = EXT_INFINITY;
502     in[1].sync   = 1;
503     in[1].before = EXT_STOP;
504     in[1].after  = EXT_INFINITY;
505     s->fs.opaque   = s;
506     s->fs.on_event = process_frame;
507
508     if ((ret = config_output(outlink)) < 0)
509         return ret;
510
511     ret = ff_framesync_configure(&s->fs);
512     outlink->time_base = s->fs.time_base;
513
514     return ret;
515 }
516
517 static int activate(AVFilterContext *ctx)
518 {
519     LUT2Context *s = ctx->priv;
520     return ff_framesync_activate(&s->fs);
521 }
522
523 static const AVFilterPad inputs[] = {
524     {
525         .name         = "srcx",
526         .type         = AVMEDIA_TYPE_VIDEO,
527         .config_props = config_inputx,
528     },
529     {
530         .name         = "srcy",
531         .type         = AVMEDIA_TYPE_VIDEO,
532         .config_props = config_inputy,
533     },
534     { NULL }
535 };
536
537 static const AVFilterPad outputs[] = {
538     {
539         .name          = "default",
540         .type          = AVMEDIA_TYPE_VIDEO,
541         .config_props  = lut2_config_output,
542     },
543     { NULL }
544 };
545
546 #define lut2_options options
547
548 FRAMESYNC_DEFINE_CLASS(lut2, LUT2Context, fs);
549
550 AVFilter ff_vf_lut2 = {
551     .name          = "lut2",
552     .description   = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two video inputs."),
553     .preinit       = lut2_framesync_preinit,
554     .priv_size     = sizeof(LUT2Context),
555     .priv_class    = &lut2_class,
556     .uninit        = uninit,
557     .query_formats = query_formats,
558     .activate      = activate,
559     .inputs        = inputs,
560     .outputs       = outputs,
561     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
562                      AVFILTER_FLAG_SLICE_THREADS,
563 };
564
565 #if CONFIG_TLUT2_FILTER
566
567 static av_cold int init(AVFilterContext *ctx)
568 {
569     LUT2Context *s = ctx->priv;
570
571     s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
572
573     return 0;
574 }
575
576 static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
577 {
578     AVFilterContext *ctx = inlink->dst;
579     LUT2Context *s = ctx->priv;
580     AVFilterLink *outlink = ctx->outputs[0];
581
582     if (s->prev_frame) {
583         AVFrame *out;
584
585         if (ctx->is_disabled) {
586             out = av_frame_clone(frame);
587         } else {
588             ThreadData td;
589
590             out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
591             if (!out) {
592                 av_frame_free(&s->prev_frame);
593                 s->prev_frame = frame;
594                 return AVERROR(ENOMEM);
595             }
596
597             av_frame_copy_props(out, frame);
598
599             td.out  = out;
600             td.srcx = frame;
601             td.srcy = s->prev_frame;
602             ctx->internal->execute(ctx, s->lut2, &td, NULL, FFMIN(s->heightx[1], ff_filter_get_nb_threads(ctx)));
603         }
604         av_frame_free(&s->prev_frame);
605         s->prev_frame = frame;
606         return ff_filter_frame(outlink, out);
607     }
608     s->prev_frame = frame;
609     return 0;
610 }
611
612 static const AVOption tlut2_options[] = {
613     { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
614     { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
615     { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
616     { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
617     { NULL }
618 };
619
620 AVFILTER_DEFINE_CLASS(tlut2);
621
622 static const AVFilterPad tlut2_inputs[] = {
623     {
624         .name          = "default",
625         .type          = AVMEDIA_TYPE_VIDEO,
626         .filter_frame  = tlut2_filter_frame,
627         .config_props  = config_inputx,
628     },
629     { NULL }
630 };
631
632 static const AVFilterPad tlut2_outputs[] = {
633     {
634         .name          = "default",
635         .type          = AVMEDIA_TYPE_VIDEO,
636         .config_props  = config_output,
637     },
638     { NULL }
639 };
640
641 AVFilter ff_vf_tlut2 = {
642     .name          = "tlut2",
643     .description   = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two successive frames."),
644     .priv_size     = sizeof(LUT2Context),
645     .priv_class    = &tlut2_class,
646     .query_formats = query_formats,
647     .init          = init,
648     .uninit        = uninit,
649     .inputs        = tlut2_inputs,
650     .outputs       = tlut2_outputs,
651     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
652                      AVFILTER_FLAG_SLICE_THREADS,
653 };
654
655 #endif