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