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