2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
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"
27 #include "drawutils.h"
31 #include "framesync.h"
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
53 typedef struct LUT2Context {
58 char *comp_expr_str[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];
69 int depth, depthx, depthy;
71 AVFrame *prev_frame; /* only used with tlut2 */
73 void (*lut2)(struct LUT2Context *s, AVFrame *dst, AVFrame *srcx, AVFrame *srcy);
77 #define OFFSET(x) offsetof(LUT2Context, x)
78 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
89 static av_cold void uninit(AVFilterContext *ctx)
91 LUT2Context *s = ctx->priv;
94 ff_framesync_uninit(&s->fs);
95 av_frame_free(&s->prev_frame);
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]);
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,
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,
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,
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,
128 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
129 AV_PIX_FMT_GRAY12, AV_PIX_FMT_GBRP14,
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,
136 static int query_formats(AVFilterContext *ctx)
138 LUT2Context *s = ctx->priv;
139 static const enum AVPixelFormat all_pix_fmts[] = {
146 static const enum AVPixelFormat bit8_pix_fmts[] = {
150 static const enum AVPixelFormat bit9_pix_fmts[] = {
154 static const enum AVPixelFormat bit10_pix_fmts[] = {
158 static const enum AVPixelFormat bit12_pix_fmts[] = {
162 static const enum AVPixelFormat bit14_pix_fmts[] = {
166 static const enum AVPixelFormat bit16_pix_fmts[] = {
170 const enum AVPixelFormat *pix_fmts;
173 if (s->tlut2 || !s->odepth)
174 return ff_set_common_formats(ctx, ff_make_format_list(all_pix_fmts));
176 ret = ff_formats_ref(ff_make_format_list(all_pix_fmts), &ctx->inputs[0]->out_formats);
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);
191 return ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->outputs[0]->in_formats);
194 static int config_inputx(AVFilterLink *inlink)
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;
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;
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;
214 s->depthy = desc->comp[0].depth;
215 s->var_values[VAR_BITDEPTHY] = s->depthy;
221 static int config_inputy(AVFilterLink *inlink)
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;
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;
240 #define DEFINE_LUT2(zname, xname, yname, ztype, xtype, ytype, zdiv, xdiv, ydiv) \
241 static void lut2_##zname##_##xname##_##yname(struct LUT2Context *s, \
243 AVFrame *srcx, AVFrame *srcy) \
245 const int odepth = s->odepth; \
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; \
254 dst = (ztype *)out->data[p]; \
255 srcxx = (const xtype *)srcx->data[p]; \
256 srcyy = (const ytype *)srcy->data[p]; \
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); \
263 dst += out->linesize[p] / zdiv; \
264 srcxx += srcx->linesize[p] / xdiv; \
265 srcyy += srcy->linesize[p] / ydiv; \
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)
279 static int process_frame(FFFrameSync *fs)
281 AVFilterContext *ctx = fs->parent;
282 LUT2Context *s = fs->opaque;
283 AVFilterLink *outlink = ctx->outputs[0];
284 AVFrame *out, *srcx = NULL, *srcy = NULL;
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)
291 if (ctx->is_disabled || !srcy) {
292 out = av_frame_clone(srcx);
294 return AVERROR(ENOMEM);
296 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
298 return AVERROR(ENOMEM);
299 av_frame_copy_props(out, srcx);
301 s->lut2(s, out, srcx, srcy);
304 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
306 return ff_filter_frame(outlink, out);
309 static int config_output(AVFilterLink *outlink)
311 AVFilterContext *ctx = outlink->src;
312 LUT2Context *s = ctx->priv;
315 s->depth = s->depthx + s->depthy;
316 s->nb_planes = s->nb_planesx;
318 s->lut2 = s->depth > 16 ? lut2_16_16_16 : lut2_8_8_8;
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;
335 s->odepth = s->depthx;
338 for (p = 0; p < s->nb_planes; p++) {
339 s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
341 return AVERROR(ENOMEM);
344 for (p = 0; p < s->nb_planes; p++) {
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);
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);
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);
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);
373 s->lut[p][(y << s->depthx) + x] = res;
381 static int lut2_config_output(AVFilterLink *outlink)
383 AVFilterContext *ctx = outlink->src;
384 LUT2Context *s = ctx->priv;
385 AVFilterLink *srcx = ctx->inputs[0];
386 AVFilterLink *srcy = ctx->inputs[1];
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;
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;
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;
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);
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,
417 return AVERROR(EINVAL);
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);
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);
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);
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);
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);
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);
476 if ((ret = ff_framesync_init(&s->fs, ctx, 2)) < 0)
480 in[0].time_base = srcx->time_base;
481 in[1].time_base = srcy->time_base;
483 in[0].before = EXT_STOP;
484 in[0].after = EXT_INFINITY;
486 in[1].before = EXT_STOP;
487 in[1].after = EXT_INFINITY;
489 s->fs.on_event = process_frame;
491 if ((ret = config_output(outlink)) < 0)
494 return ff_framesync_configure(&s->fs);
497 static int activate(AVFilterContext *ctx)
499 LUT2Context *s = ctx->priv;
500 return ff_framesync_activate(&s->fs);
503 static const AVFilterPad inputs[] = {
506 .type = AVMEDIA_TYPE_VIDEO,
507 .config_props = config_inputx,
511 .type = AVMEDIA_TYPE_VIDEO,
512 .config_props = config_inputy,
517 static const AVFilterPad outputs[] = {
520 .type = AVMEDIA_TYPE_VIDEO,
521 .config_props = lut2_config_output,
526 #define lut2_options options
528 FRAMESYNC_DEFINE_CLASS(lut2, LUT2Context, fs);
530 AVFilter ff_vf_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,
537 .query_formats = query_formats,
538 .activate = activate,
541 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
544 #if CONFIG_TLUT2_FILTER
546 static av_cold int init(AVFilterContext *ctx)
548 LUT2Context *s = ctx->priv;
550 s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
555 static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
557 AVFilterContext *ctx = inlink->dst;
558 LUT2Context *s = ctx->priv;
559 AVFilterLink *outlink = ctx->outputs[0];
564 if (ctx->is_disabled) {
565 out = av_frame_clone(frame);
567 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
569 av_frame_free(&s->prev_frame);
570 s->prev_frame = frame;
571 return AVERROR(ENOMEM);
574 av_frame_copy_props(out, frame);
575 s->lut2(s, out, frame, s->prev_frame);
577 av_frame_free(&s->prev_frame);
578 s->prev_frame = frame;
579 return ff_filter_frame(outlink, out);
581 s->prev_frame = frame;
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 },
593 AVFILTER_DEFINE_CLASS(tlut2);
595 static const AVFilterPad tlut2_inputs[] = {
598 .type = AVMEDIA_TYPE_VIDEO,
599 .filter_frame = tlut2_filter_frame,
600 .config_props = config_inputx,
605 static const AVFilterPad tlut2_outputs[] = {
608 .type = AVMEDIA_TYPE_VIDEO,
609 .config_props = config_output,
614 AVFilter ff_vf_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,
622 .inputs = tlut2_inputs,
623 .outputs = tlut2_outputs,
624 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,