From patchwork Tue May 19 02:00:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Reid X-Patchwork-Id: 19766 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id D701A44B180 for ; Tue, 19 May 2020 05:00:38 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B02B5687F7E; Tue, 19 May 2020 05:00:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6D5A968AAFD for ; Tue, 19 May 2020 05:00:31 +0300 (EEST) Received: by mail-pj1-f42.google.com with SMTP id n15so667427pjt.4 for ; Mon, 18 May 2020 19:00:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZB6eu2KLbAr5IA0Zlm5TCOjrWK+Fzif3q74y5JWrVjM=; b=cheiPUR3M1Y5on2Vawf3CQVGOlKGBn9bYcZgaI9eSpeRvctFI4oRniaIOaY+q5sLlK QbwQDzKUw+C0iaDatyF0Ro+K+ITFXBDyUTeWkSLsmis25rU99DPGGs+lGttbwJsHqxKj 0v6UZ+vtpJTk+pYTAOJYLtRdJ/5dsVQ+PFgP0wANRuWU9t3N4gTfQ1BUFBUr1lS3fOvY YlrFGWxZ3S/0/qd3pFymBQiITN5486rZzz+cD4NbJIPeIGfwXFc2ENZhnCC5VGiPkLJF nxqqHhw2H90uLVykFZH3+gjBtpSPtvhOHmWoT3ffAEGpkmEitNoX5/YLjxlmdY4LMbyy RKrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ZB6eu2KLbAr5IA0Zlm5TCOjrWK+Fzif3q74y5JWrVjM=; b=TMn56AVdB5ARilO6h7uELX7Gojtnq5spCEMQrD+4qqDA2jtki9IrCX+7K676LyjBmU fl92fQ4oU62KTgxa44jsbR/zslf+qCfc4DWIGZBqB3xpvFIAi+X82BoQrUK6hZJf+3cK eLYfxgBzjXTq4justjs5jHKxhlnkoZYntq39kUpdM70XJlN/WDt9ATQQmzVql9FQ+Yvj WANW0mpI0xVqB8pgFKbInGY1u6opLqvJU87eLflR7bStUWHiyJrA+1NeeyYOCP8MV0+5 lB97aYUDxEZvYLkSWYbW8oJMlWa36bTryrw5BtHvj7TjwjFOsFZPdl9x5v4qOtxD/8Wo d0pA== X-Gm-Message-State: AOAM530J+WQPpXJ4/k1JvE+S1Q8WnjrvHUSrVst0wMOz+tRQDBtXMkGj 6QsdRD/tp3qkx5n3nXmyUoGY0M2S X-Google-Smtp-Source: ABdhPJw6eq7wjFTQEgi7RkibFM7dFoqOYI0yOJsaL+ebhQgRgDEFYFhaZmcrgFudDhpI7j9z9a0U8g== X-Received: by 2002:a17:90b:1104:: with SMTP id gi4mr2222685pjb.115.1589853629196; Mon, 18 May 2020 19:00:29 -0700 (PDT) Received: from marks-macbook.hitronhub.home (S01069050ca607903.vc.shawcable.net. [174.7.236.190]) by smtp.gmail.com with ESMTPSA id w23sm643399pjy.35.2020.05.18.19.00.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 May 2020 19:00:28 -0700 (PDT) From: mindmark@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 18 May 2020 19:00:25 -0700 Message-Id: <20200519020026.24245-1-mindmark@gmail.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avfilter/vf_lut3d: initial float pixel format support X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Mark Reid Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Mark Reid --- libavfilter/vf_lut3d.c | 207 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 201 insertions(+), 6 deletions(-) diff --git a/libavfilter/vf_lut3d.c b/libavfilter/vf_lut3d.c index fda85b16b4..482e2394a7 100644 --- a/libavfilter/vf_lut3d.c +++ b/libavfilter/vf_lut3d.c @@ -24,9 +24,12 @@ * 3D Lookup table filter */ +#include "float.h" + #include "libavutil/opt.h" #include "libavutil/file.h" #include "libavutil/intreadwrite.h" +#include "libavutil/intfloat.h" #include "libavutil/avassert.h" #include "libavutil/pixdesc.h" #include "libavutil/avstring.h" @@ -73,6 +76,7 @@ typedef struct LUT3DContext { int clut_step; int clut_bits; int clut_planar; + int clut_float; int clut_width; FFFrameSync fs; #endif @@ -91,6 +95,30 @@ typedef struct ThreadData { { "tetrahedral", "interpolate values using a tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=INTERPOLATE_TETRAHEDRAL}, INT_MIN, INT_MAX, FLAGS, "interp_mode" }, \ { NULL } +#define EXPONENT_MASK 0x7F800000 +#define MANTISSA_MASK 0x007FFFFF +#define SIGN_MASK 0x7FFFFFFF + +static inline float sanitizef(float f) +{ + union av_intfloat32 t; + t.f = f; + + if ((t.i & EXPONENT_MASK) == EXPONENT_MASK) { + if ((t.i & MANTISSA_MASK) != 0) { + // NAN + return 0.0f; + } else if (t.i & SIGN_MASK) { + // -INF + return FLT_MIN; + } else { + // +INF + return FLT_MAX; + } + } + return f; +} + static inline float lerpf(float v0, float v1, float f) { return v0 + (v1 - v0) * f; @@ -285,6 +313,66 @@ DEFINE_INTERP_FUNC_PLANAR(nearest, 16, 16) DEFINE_INTERP_FUNC_PLANAR(trilinear, 16, 16) DEFINE_INTERP_FUNC_PLANAR(tetrahedral, 16, 16) +#define DEFINE_INTERP_FUNC_PLANAR_FLOAT(name, depth) \ +static int interp_##name##_pf##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ +{ \ + int x, y; \ + const LUT3DContext *lut3d = ctx->priv; \ + const ThreadData *td = arg; \ + const AVFrame *in = td->in; \ + const AVFrame *out = td->out; \ + const int direct = out == in; \ + const int slice_start = (in->height * jobnr ) / nb_jobs; \ + const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \ + uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \ + uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \ + uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \ + uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \ + const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \ + const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \ + const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \ + const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \ + const float lutsize = lut3d->lutsize - 1; \ + const float scale_r = lut3d->scale.r * lutsize; \ + const float scale_g = lut3d->scale.g * lutsize; \ + const float scale_b = lut3d->scale.b * lutsize; \ + \ + for (y = slice_start; y < slice_end; y++) { \ + float *dstg = (float *)grow; \ + float *dstb = (float *)brow; \ + float *dstr = (float *)rrow; \ + float *dsta = (float *)arow; \ + const float *srcg = (const float *)srcgrow; \ + const float *srcb = (const float *)srcbrow; \ + const float *srcr = (const float *)srcrrow; \ + const float *srca = (const float *)srcarow; \ + for (x = 0; x < in->width; x++) { \ + const struct rgbvec scaled_rgb = {av_clipf(sanitizef(srcr[x]) * scale_r, 0, lutsize), \ + av_clipf(sanitizef(srcg[x]) * scale_g, 0, lutsize), \ + av_clipf(sanitizef(srcb[x]) * scale_b, 0, lutsize)}; \ + struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \ + dstr[x] = vec.r; \ + dstg[x] = vec.g; \ + dstb[x] = vec.b; \ + if (!direct && in->linesize[3]) \ + dsta[x] = srca[x]; \ + } \ + grow += out->linesize[0]; \ + brow += out->linesize[1]; \ + rrow += out->linesize[2]; \ + arow += out->linesize[3]; \ + srcgrow += in->linesize[0]; \ + srcbrow += in->linesize[1]; \ + srcrrow += in->linesize[2]; \ + srcarow += in->linesize[3]; \ + } \ + return 0; \ +} + +DEFINE_INTERP_FUNC_PLANAR_FLOAT(nearest, 32) +DEFINE_INTERP_FUNC_PLANAR_FLOAT(trilinear, 32) +DEFINE_INTERP_FUNC_PLANAR_FLOAT(tetrahedral, 32) + #define DEFINE_INTERP_FUNC(name, nbits) \ static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ { \ @@ -700,7 +788,8 @@ static int query_formats(AVFilterContext *ctx) AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRP14, - AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, + AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, + AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32, AV_PIX_FMT_NONE }; AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); @@ -711,18 +800,19 @@ static int query_formats(AVFilterContext *ctx) static int config_input(AVFilterLink *inlink) { - int depth, is16bit, planar; + int depth, is16bit, isfloat, planar; LUT3DContext *lut3d = inlink->dst->priv; const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); depth = desc->comp[0].depth; is16bit = desc->comp[0].depth > 8; planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR; + isfloat = desc->flags & AV_PIX_FMT_FLAG_FLOAT; ff_fill_rgba_map(lut3d->rgba_map, inlink->format); lut3d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit); #define SET_FUNC(name) do { \ - if (planar) { \ + if (planar && !isfloat) { \ switch (depth) { \ case 8: lut3d->interp = interp_8_##name##_p8; break; \ case 9: lut3d->interp = interp_16_##name##_p9; break; \ @@ -731,6 +821,7 @@ static int config_input(AVFilterLink *inlink) case 14: lut3d->interp = interp_16_##name##_p14; break; \ case 16: lut3d->interp = interp_16_##name##_p16; break; \ } \ + } else if (isfloat) { lut3d->interp = interp_##name##_pf32; \ } else if (is16bit) { lut3d->interp = interp_16_##name; \ } else { lut3d->interp = interp_8_##name; } \ } while (0) @@ -970,6 +1061,39 @@ static void update_clut_planar(LUT3DContext *lut3d, const AVFrame *frame) } } +static void update_clut_float(LUT3DContext *lut3d, const AVFrame *frame) +{ + const uint8_t *datag = frame->data[0]; + const uint8_t *datab = frame->data[1]; + const uint8_t *datar = frame->data[2]; + const int glinesize = frame->linesize[0]; + const int blinesize = frame->linesize[1]; + const int rlinesize = frame->linesize[2]; + const int w = lut3d->clut_width; + const int level = lut3d->lutsize; + const int level2 = lut3d->lutsize2; + + int i, j, k, x = 0, y = 0; + + for (k = 0; k < level; k++) { + for (j = 0; j < level; j++) { + for (i = 0; i < level; i++) { + const float *gsrc = (const float *)(datag + y*glinesize); + const float *bsrc = (const float *)(datab + y*blinesize); + const float *rsrc = (const float *)(datar + y*rlinesize); + struct rgbvec *vec = &lut3d->lut[i * level2 + j * level + k]; + vec->r = rsrc[x]; + vec->g = gsrc[x]; + vec->b = bsrc[x]; + if (++x == w) { + x = 0; + y++; + } + } + } + } +} + static int config_output(AVFilterLink *outlink) { AVFilterContext *ctx = outlink->src; @@ -1004,6 +1128,7 @@ static int config_clut(AVFilterLink *inlink) lut3d->clut_bits = desc->comp[0].depth; lut3d->clut_planar = av_pix_fmt_count_planes(inlink->format) > 1; + lut3d->clut_float = desc->flags & AV_PIX_FMT_FLAG_FLOAT; lut3d->clut_step = av_get_padded_bits_per_pixel(desc) >> 3; ff_fill_rgba_map(lut3d->clut_rgba_map, inlink->format); @@ -1049,7 +1174,9 @@ static int update_apply_clut(FFFrameSync *fs) return ret; if (!second) return ff_filter_frame(ctx->outputs[0], master); - if (lut3d->clut_planar) + if (lut3d->clut_float) + update_clut_float(ctx->priv, second); + else if (lut3d->clut_planar) update_clut_planar(ctx->priv, second); else update_clut_packed(ctx->priv, second); @@ -1477,6 +1604,72 @@ DEFINE_INTERP_FUNC_PLANAR_1D(cosine, 16, 16) DEFINE_INTERP_FUNC_PLANAR_1D(cubic, 16, 16) DEFINE_INTERP_FUNC_PLANAR_1D(spline, 16, 16) +#define DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(name, depth) \ +static int interp_1d_##name##_pf##depth(AVFilterContext *ctx, \ + void *arg, int jobnr, \ + int nb_jobs) \ +{ \ + int x, y; \ + const LUT1DContext *lut1d = ctx->priv; \ + const ThreadData *td = arg; \ + const AVFrame *in = td->in; \ + const AVFrame *out = td->out; \ + const int direct = out == in; \ + const int slice_start = (in->height * jobnr ) / nb_jobs; \ + const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \ + uint8_t *grow = out->data[0] + slice_start * out->linesize[0]; \ + uint8_t *brow = out->data[1] + slice_start * out->linesize[1]; \ + uint8_t *rrow = out->data[2] + slice_start * out->linesize[2]; \ + uint8_t *arow = out->data[3] + slice_start * out->linesize[3]; \ + const uint8_t *srcgrow = in->data[0] + slice_start * in->linesize[0]; \ + const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \ + const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \ + const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \ + const float lutsize = lut1d->lutsize - 1; \ + const float scale_r = lut1d->scale.r * lutsize; \ + const float scale_g = lut1d->scale.g * lutsize; \ + const float scale_b = lut1d->scale.b * lutsize; \ + \ + for (y = slice_start; y < slice_end; y++) { \ + float *dstg = (float *)grow; \ + float *dstb = (float *)brow; \ + float *dstr = (float *)rrow; \ + float *dsta = (float *)arow; \ + const float *srcg = (const float *)srcgrow; \ + const float *srcb = (const float *)srcbrow; \ + const float *srcr = (const float *)srcrrow; \ + const float *srca = (const float *)srcarow; \ + for (x = 0; x < in->width; x++) { \ + float r = av_clipf(sanitizef(srcr[x]) * scale_r, 0.0f, lutsize); \ + float g = av_clipf(sanitizef(srcg[x]) * scale_g, 0.0f, lutsize); \ + float b = av_clipf(sanitizef(srcb[x]) * scale_b, 0.0f, lutsize); \ + r = interp_1d_##name(lut1d, 0, r); \ + g = interp_1d_##name(lut1d, 1, g); \ + b = interp_1d_##name(lut1d, 2, b); \ + dstr[x] = r; \ + dstg[x] = g; \ + dstb[x] = b; \ + if (!direct && in->linesize[3]) \ + dsta[x] = srca[x]; \ + } \ + grow += out->linesize[0]; \ + brow += out->linesize[1]; \ + rrow += out->linesize[2]; \ + arow += out->linesize[3]; \ + srcgrow += in->linesize[0]; \ + srcbrow += in->linesize[1]; \ + srcrrow += in->linesize[2]; \ + srcarow += in->linesize[3]; \ + } \ + return 0; \ +} + +DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(nearest, 32) +DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(linear, 32) +DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(cosine, 32) +DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(cubic, 32) +DEFINE_INTERP_FUNC_PLANAR_1D_FLOAT(spline, 32) + #define DEFINE_INTERP_FUNC_1D(name, nbits) \ static int interp_1d_##nbits##_##name(AVFilterContext *ctx, void *arg, \ int jobnr, int nb_jobs) \ @@ -1537,18 +1730,19 @@ DEFINE_INTERP_FUNC_1D(spline, 16) static int config_input_1d(AVFilterLink *inlink) { - int depth, is16bit, planar; + int depth, is16bit, isfloat, planar; LUT1DContext *lut1d = inlink->dst->priv; const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); depth = desc->comp[0].depth; is16bit = desc->comp[0].depth > 8; planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR; + isfloat = desc->flags & AV_PIX_FMT_FLAG_FLOAT; ff_fill_rgba_map(lut1d->rgba_map, inlink->format); lut1d->step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit); #define SET_FUNC_1D(name) do { \ - if (planar) { \ + if (planar && !isfloat) { \ switch (depth) { \ case 8: lut1d->interp = interp_1d_8_##name##_p8; break; \ case 9: lut1d->interp = interp_1d_16_##name##_p9; break; \ @@ -1557,6 +1751,7 @@ static int config_input_1d(AVFilterLink *inlink) case 14: lut1d->interp = interp_1d_16_##name##_p14; break; \ case 16: lut1d->interp = interp_1d_16_##name##_p16; break; \ } \ + } else if (isfloat) { lut1d->interp = interp_1d_##name##_pf32; \ } else if (is16bit) { lut1d->interp = interp_1d_16_##name; \ } else { lut1d->interp = interp_1d_8_##name; } \ } while (0) From patchwork Tue May 19 02:00:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Reid X-Patchwork-Id: 19767 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id AEA9C44B180 for ; Tue, 19 May 2020 05:00:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 934296808B0; Tue, 19 May 2020 05:00:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f65.google.com (mail-pj1-f65.google.com [209.85.216.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AB91768AAFD for ; Tue, 19 May 2020 05:00:32 +0300 (EEST) Received: by mail-pj1-f65.google.com with SMTP id q9so672323pjm.2 for ; Mon, 18 May 2020 19:00:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oEQgjCHOQ8CPUh3iIZXi8vROrg0YcNfA2ukQW16tiUk=; b=Xnln1KW/CGstRG91H1JJwOkpGdfKoGqDb5Bu3srRk0t+5pJG0zkNVk/H+UD4WRdRtj pAR+BiBJXtRrqJ4MM2RNY99KpQzTKUvUWyo+T6+jqFXKWY3E4N9NhRpFZ40onMi7r3Z4 6Rmi5cdJkNoE3qmIBHsloAw0X72mk7B7LHqZmcSGytz7DVWo62InLnUo3XGx/9Zfq22K u7SQanhdJQ0O68Q1GdQvxRholOnrN1gizuZl6P0n1BjKIZmw6t2WAceOmWPUPls7Z9U0 rMUTzog0OQcC8GLCd5jWZtW6EWRI8IZwqpT47+hVmwinFT3kUpBmCXQ1RgxBpRebEk4X NeKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oEQgjCHOQ8CPUh3iIZXi8vROrg0YcNfA2ukQW16tiUk=; b=jJcOFUlhlKhJP/7z15+TUZ2QvP5RETNNcMMu7sp2+7u2YL8jaJboHIXJZ4wMaWqrZY y7xENlb/D+X8eIacII0Jbbh7S7ijPY8z3GH+ctNwgK8soOnQ33PGJeVzr5agpya0zvlG 9TYhPy4NGoGANhd0vopYtbfHDBKnkKkz9MrmsEudGllFe+m570uNH5nWpsz4/yBBp1k4 Lfmd8fEICR2Ct9oE6/d8BztRVHghRouiaBCu3V8L1iP7K02AdeBrLes+2lMM3DiiTxId aCOISqsG+KLcuDgP4+UbOoZ2bju8lesUtm210hzdmjQpCY0FmxeL+MXqpES7siDP/aYP fTvA== X-Gm-Message-State: AOAM533GpOaeGNcxHqvEUe96BsCuzCpjU99W+rGpYioXZDGPZVFwBOJ1 gSacog7s0iIxwuy82ut81l2axb8X X-Google-Smtp-Source: ABdhPJxFLEXfqtgdF7pmkd/w9OEhyNc8At/RxhL1NzSxQO0JOCMqZcQsC++eQakeOLsMF2Uw7/Tkcg== X-Received: by 2002:a17:902:7407:: with SMTP id g7mr18544369pll.159.1589853630425; Mon, 18 May 2020 19:00:30 -0700 (PDT) Received: from marks-macbook.hitronhub.home (S01069050ca607903.vc.shawcable.net. [174.7.236.190]) by smtp.gmail.com with ESMTPSA id w23sm643399pjy.35.2020.05.18.19.00.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 May 2020 19:00:29 -0700 (PDT) From: mindmark@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Mon, 18 May 2020 19:00:26 -0700 Message-Id: <20200519020026.24245-2-mindmark@gmail.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200519020026.24245-1-mindmark@gmail.com> References: <20200519020026.24245-1-mindmark@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] avfilter/vf_lut3d: prelut support for 3d cinespace luts X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Mark Reid Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Mark Reid --- libavfilter/vf_lut3d.c | 367 +++++++++++++++++++++++++++++++++++------ 1 file changed, 312 insertions(+), 55 deletions(-) diff --git a/libavfilter/vf_lut3d.c b/libavfilter/vf_lut3d.c index 482e2394a7..4067c4a60a 100644 --- a/libavfilter/vf_lut3d.c +++ b/libavfilter/vf_lut3d.c @@ -59,6 +59,15 @@ struct rgbvec { /* 3D LUT don't often go up to level 32, but it is common to have a Hald CLUT * of 512x512 (64x64x64) */ #define MAX_LEVEL 256 +#define PRELUT_SIZE 65536 + +typedef struct Lut3DPreLut { + int size; + float min[3]; + float max[3]; + float scale[3]; + float* lut[3]; +} Lut3DPreLut; typedef struct LUT3DContext { const AVClass *class; @@ -71,6 +80,7 @@ typedef struct LUT3DContext { struct rgbvec *lut; int lutsize; int lutsize2; + Lut3DPreLut prelut; #if CONFIG_HALDCLUT_FILTER uint8_t clut_rgba_map[4]; int clut_step; @@ -234,6 +244,31 @@ static inline struct rgbvec interp_tetrahedral(const LUT3DContext *lut3d, return c; } +static inline float apply_prelut_channel(const LUT3DContext *lut3d, float x, int c) +{ + + x = (x - lut3d->prelut.min[c]) * lut3d->prelut.scale[c]; + x = av_clipf(x, 0.0f, lut3d->prelut.size-1); + + const float a = lut3d->prelut.lut[c][PREV(x)]; + const float b = lut3d->prelut.lut[c][(FFMIN((int)(x) + 1, lut3d->prelut.size - 1))]; + const float mix = x - (float)PREV(x); + + return lerpf(a, b, mix); +} + +static inline struct rgbvec apply_prelut(const LUT3DContext *lut3d, const struct rgbvec *s) +{ + if (lut3d->prelut.size <= 0) + return *s; + + struct rgbvec c; + c.r = apply_prelut_channel(lut3d, s->r, 0); + c.g = apply_prelut_channel(lut3d, s->g, 1); + c.b = apply_prelut_channel(lut3d, s->b, 2); + return c; +} + #define DEFINE_INTERP_FUNC_PLANAR(name, nbits, depth) \ static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ { \ @@ -253,9 +288,11 @@ static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, i const uint8_t *srcbrow = in->data[1] + slice_start * in->linesize[1]; \ const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \ const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \ - const float scale_r = (lut3d->scale.r / ((1<lutsize - 1); \ - const float scale_g = (lut3d->scale.g / ((1<lutsize - 1); \ - const float scale_b = (lut3d->scale.b / ((1<lutsize - 1); \ + const float lut_max = lut3d->lutsize - 1; \ + const float scale_f = 1.0f / ((1<scale.r * lut_max; \ + const float scale_g = lut3d->scale.g * lut_max; \ + const float scale_b = lut3d->scale.b * lut_max; \ \ for (y = slice_start; y < slice_end; y++) { \ uint##nbits##_t *dstg = (uint##nbits##_t *)grow; \ @@ -267,9 +304,13 @@ static int interp_##nbits##_##name##_p##depth(AVFilterContext *ctx, void *arg, i const uint##nbits##_t *srcr = (const uint##nbits##_t *)srcrrow; \ const uint##nbits##_t *srca = (const uint##nbits##_t *)srcarow; \ for (x = 0; x < in->width; x++) { \ - const struct rgbvec scaled_rgb = {srcr[x] * scale_r, \ - srcg[x] * scale_g, \ - srcb[x] * scale_b}; \ + const struct rgbvec rgb = {srcr[x] * scale_f, \ + srcg[x] * scale_f, \ + srcb[x] * scale_f}; \ + const struct rgbvec prelut_rgb = apply_prelut(lut3d, &rgb); \ + const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \ + av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \ + av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \ struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \ dstr[x] = av_clip_uintp2(vec.r * (float)((1<data[1] + slice_start * in->linesize[1]; \ const uint8_t *srcrrow = in->data[2] + slice_start * in->linesize[2]; \ const uint8_t *srcarow = in->data[3] + slice_start * in->linesize[3]; \ - const float lutsize = lut3d->lutsize - 1; \ - const float scale_r = lut3d->scale.r * lutsize; \ - const float scale_g = lut3d->scale.g * lutsize; \ - const float scale_b = lut3d->scale.b * lutsize; \ + const float lut_max = lut3d->lutsize - 1; \ + const float scale_r = lut3d->scale.r * lut_max; \ + const float scale_g = lut3d->scale.g * lut_max; \ + const float scale_b = lut3d->scale.b * lut_max; \ \ for (y = slice_start; y < slice_end; y++) { \ float *dstg = (float *)grow; \ @@ -347,9 +388,13 @@ static int interp_##name##_pf##depth(AVFilterContext *ctx, void *arg, int jobnr, const float *srcr = (const float *)srcrrow; \ const float *srca = (const float *)srcarow; \ for (x = 0; x < in->width; x++) { \ - const struct rgbvec scaled_rgb = {av_clipf(sanitizef(srcr[x]) * scale_r, 0, lutsize), \ - av_clipf(sanitizef(srcg[x]) * scale_g, 0, lutsize), \ - av_clipf(sanitizef(srcb[x]) * scale_b, 0, lutsize)}; \ + const struct rgbvec rgb = {sanitizef(srcr[x]), \ + sanitizef(srcg[x]), \ + sanitizef(srcb[x])}; \ + const struct rgbvec prelut_rgb = apply_prelut(lut3d, &rgb); \ + const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \ + av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \ + av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \ struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \ dstr[x] = vec.r; \ dstg[x] = vec.g; \ @@ -391,17 +436,23 @@ static int interp_##nbits##_##name(AVFilterContext *ctx, void *arg, int jobnr, i const int slice_end = (in->height * (jobnr+1)) / nb_jobs; \ uint8_t *dstrow = out->data[0] + slice_start * out->linesize[0]; \ const uint8_t *srcrow = in ->data[0] + slice_start * in ->linesize[0]; \ - const float scale_r = (lut3d->scale.r / ((1<lutsize - 1); \ - const float scale_g = (lut3d->scale.g / ((1<lutsize - 1); \ - const float scale_b = (lut3d->scale.b / ((1<lutsize - 1); \ + const float lut_max = lut3d->lutsize - 1; \ + const float scale_f = 1.0f / ((1<scale.r * lut_max; \ + const float scale_g = lut3d->scale.g * lut_max; \ + const float scale_b = lut3d->scale.b * lut_max; \ \ for (y = slice_start; y < slice_end; y++) { \ uint##nbits##_t *dst = (uint##nbits##_t *)dstrow; \ const uint##nbits##_t *src = (const uint##nbits##_t *)srcrow; \ for (x = 0; x < in->width * step; x += step) { \ - const struct rgbvec scaled_rgb = {src[x + r] * scale_r, \ - src[x + g] * scale_g, \ - src[x + b] * scale_b}; \ + const struct rgbvec rgb = {src[x + r] * scale_f, \ + src[x + g] * scale_f, \ + src[x + b] * scale_f}; \ + const struct rgbvec prelut_rgb = apply_prelut(lut3d, &rgb); \ + const struct rgbvec scaled_rgb = {av_clipf(prelut_rgb.r * scale_r, 0, lut_max), \ + av_clipf(prelut_rgb.g * scale_g, 0, lut_max), \ + av_clipf(prelut_rgb.b * scale_b, 0, lut_max)}; \ struct rgbvec vec = interp_##name(lut3d, &scaled_rgb); \ dst[x + r] = av_clip_uint##nbits(vec.r * (float)((1< 0; max--) { + if ((c = fgetc(f)) == EOF) + break; + + if (av_isspace(c)) + break; + + *p++ = c; + } + + *p = 0; + if (p == dst) + return NULL; + return p; +} + #define NEXT_LINE(loop_cond) do { \ if (!fgets(line, sizeof(line), f)) { \ av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \ @@ -439,10 +524,18 @@ static int skip_line(const char *p) } \ } while (loop_cond) -static int allocate_3dlut(AVFilterContext *ctx, int lutsize) +#define NEXT_LINE_OR_GOTO(loop_cond, label) do { \ + if (!fgets(line, sizeof(line), f)) { \ + av_log(ctx, AV_LOG_ERROR, "Unexpected EOF\n"); \ + ret = AVERROR_INVALIDDATA; \ + goto label; \ + } \ +} while (loop_cond) + +static int allocate_3dlut(AVFilterContext *ctx, int lutsize, int prelut) { LUT3DContext *lut3d = ctx->priv; - + int i; if (lutsize < 2 || lutsize > MAX_LEVEL) { av_log(ctx, AV_LOG_ERROR, "Too large or invalid 3D LUT size\n"); return AVERROR(EINVAL); @@ -452,6 +545,21 @@ static int allocate_3dlut(AVFilterContext *ctx, int lutsize) lut3d->lut = av_malloc_array(lutsize * lutsize * lutsize, sizeof(*lut3d->lut)); if (!lut3d->lut) return AVERROR(ENOMEM); + + if (prelut) { + lut3d->prelut.size = PRELUT_SIZE; + for (i = 0; i < 3; i++) { + av_freep(&lut3d->prelut.lut[i]); + lut3d->prelut.lut[i] = av_malloc_array(PRELUT_SIZE, sizeof(*lut3d->prelut.lut[0])); + if (!lut3d->prelut.lut[i]) + return AVERROR(ENOMEM); + } + } else { + lut3d->prelut.size = 0; + for (i = 0; i < 3; i++) { + av_freep(&lut3d->prelut.lut[i]); + } + } lut3d->lutsize = lutsize; lut3d->lutsize2 = lutsize * lutsize; return 0; @@ -475,7 +583,7 @@ static int parse_dat(AVFilterContext *ctx, FILE *f) NEXT_LINE(skip_line(line)); } - ret = allocate_3dlut(ctx, size); + ret = allocate_3dlut(ctx, size, 0); if (ret < 0) return ret; @@ -507,7 +615,7 @@ static int parse_cube(AVFilterContext *ctx, FILE *f) const int size = strtol(line + 12, NULL, 0); const int size2 = size * size; - ret = allocate_3dlut(ctx, size); + ret = allocate_3dlut(ctx, size, 0); if (ret < 0) return ret; @@ -562,7 +670,7 @@ static int parse_3dl(AVFilterContext *ctx, FILE *f) lut3d->lutsize = size; - ret = allocate_3dlut(ctx, size); + ret = allocate_3dlut(ctx, size, 0); if (ret < 0) return ret; @@ -631,7 +739,7 @@ static int parse_m3d(AVFilterContext *ctx, FILE *f) lut3d->lutsize = size; size2 = size * size; - ret = allocate_3dlut(ctx, size); + ret = allocate_3dlut(ctx, size, 0); if (ret < 0) return ret; @@ -655,6 +763,44 @@ static int parse_m3d(AVFilterContext *ctx, FILE *f) return 0; } +static int nearest_sample_index(float *data, float x, int low, int hi) +{ + int mid; + if (x < data[low]) + return low; + + if (x > data[hi]) + return hi; + + for (;;) { + av_assert0(x >= data[low]); + av_assert0(x <= data[hi]); + av_assert0((hi-low) > 0); + + if(hi - low == 1) + return low; + + mid = (low + hi) / 2; + + if(x < data[mid]) + hi = mid; + else + low = mid; + } + + return 0; +} + +#define NEXT_FLOAT_OR_GOTO(value, label) \ + if (!fget_next_word(line, sizeof(line) ,f)) { \ + ret = AVERROR_INVALIDDATA; \ + goto label; \ + } \ + if (av_sscanf(line, "%f", &value) != 1) { \ + ret = AVERROR_INVALIDDATA; \ + goto label; \ + } + static int parse_cinespace(AVFilterContext *ctx, FILE *f) { LUT3DContext *lut3d = ctx->priv; @@ -663,22 +809,30 @@ static int parse_cinespace(AVFilterContext *ctx, FILE *f) float in_max[3] = {1.0, 1.0, 1.0}; float out_min[3] = {0.0, 0.0, 0.0}; float out_max[3] = {1.0, 1.0, 1.0}; - int ret, inside_metadata = 0, size, size2; + int inside_metadata = 0, size, size2; + int prelut = 0; + int ret = 0; - NEXT_LINE(skip_line(line)); + int prelut_sizes[3] = {0, 0, 0}; + float *in_prelut[3] = {NULL, NULL, NULL}; + float *out_prelut[3] = {NULL, NULL, NULL}; + + NEXT_LINE_OR_GOTO(skip_line(line), end); if (strncmp(line, "CSPLUTV100", 10)) { av_log(ctx, AV_LOG_ERROR, "Not cineSpace LUT format\n"); - return AVERROR(EINVAL); + ret = AVERROR(EINVAL); + goto end; } - NEXT_LINE(skip_line(line)); + NEXT_LINE_OR_GOTO(skip_line(line), end); if (strncmp(line, "3D", 2)) { av_log(ctx, AV_LOG_ERROR, "Not 3D LUT format\n"); - return AVERROR(EINVAL); + ret = AVERROR(EINVAL); + goto end; } while (1) { - NEXT_LINE(skip_line(line)); + NEXT_LINE_OR_GOTO(skip_line(line), end); if (!strncmp(line, "BEGIN METADATA", 14)) { inside_metadata = 1; @@ -694,31 +848,94 @@ static int parse_cinespace(AVFilterContext *ctx, FILE *f) for (int i = 0; i < 3; i++) { int npoints = strtol(line, NULL, 0); - if (npoints != 2) { + if (npoints > 2) { + float v,last; + + if (npoints > PRELUT_SIZE) { + av_log(ctx, AV_LOG_ERROR, "Prelut size too large.\n"); + ret = AVERROR_INVALIDDATA; + goto end; + } + + if (in_prelut[i] || out_prelut[i]) { + av_log(ctx, AV_LOG_ERROR, "Invalid file has multiple preluts.\n"); + ret = AVERROR_INVALIDDATA; + goto end; + } + + in_prelut[i] = (float*)av_malloc(npoints * sizeof(float)); + out_prelut[i] = (float*)av_malloc(npoints * sizeof(float)); + if (!in_prelut[i] || !out_prelut[i]) { + ret = AVERROR(ENOMEM); + goto end; + } + + prelut_sizes[i] = npoints; + in_min[i] = FLT_MAX; + in_max[i] = FLT_MIN; + out_min[i] = FLT_MAX; + out_max[i] = FLT_MIN; + + last = FLT_MIN; + + for (int j = 0; j < npoints; j++) { + NEXT_FLOAT_OR_GOTO(v, end) + in_min[i] = FFMIN(in_min[i], v); + in_max[i] = FFMAX(in_max[i], v); + in_prelut[i][j] = v; + if (v < last) { + av_log(ctx, AV_LOG_ERROR, "Invalid file, non increasing prelut.\n"); + ret = AVERROR(ENOMEM); + goto end; + } + last = v; + } + + for (int j = 0; j < npoints; j++) { + NEXT_FLOAT_OR_GOTO(v, end) + out_min[i] = FFMIN(out_min[i], v); + out_max[i] = FFMAX(out_max[i], v); + out_prelut[i][j] = v; + } + + } else if (npoints == 2) { + NEXT_LINE_OR_GOTO(skip_line(line), end); + if (av_sscanf(line, "%f %f", &in_min[i], &in_max[i]) != 2) { + ret = AVERROR_INVALIDDATA; + goto end; + } + NEXT_LINE_OR_GOTO(skip_line(line), end); + if (av_sscanf(line, "%f %f", &out_min[i], &out_max[i]) != 2) { + ret = AVERROR_INVALIDDATA; + goto end; + } + + } else { av_log(ctx, AV_LOG_ERROR, "Unsupported number of pre-lut points.\n"); - return AVERROR_PATCHWELCOME; + ret = AVERROR_PATCHWELCOME; + goto end; } - NEXT_LINE(skip_line(line)); - if (av_sscanf(line, "%f %f", &in_min[i], &in_max[i]) != 2) - return AVERROR_INVALIDDATA; - NEXT_LINE(skip_line(line)); - if (av_sscanf(line, "%f %f", &out_min[i], &out_max[i]) != 2) - return AVERROR_INVALIDDATA; - NEXT_LINE(skip_line(line)); + NEXT_LINE_OR_GOTO(skip_line(line), end); } - if (av_sscanf(line, "%d %d %d", &size_r, &size_g, &size_b) != 3) - return AVERROR(EINVAL); + if (av_sscanf(line, "%d %d %d", &size_r, &size_g, &size_b) != 3) { + ret = AVERROR(EINVAL); + goto end; + } if (size_r != size_g || size_r != size_b) { av_log(ctx, AV_LOG_ERROR, "Unsupported size combination: %dx%dx%d.\n", size_r, size_g, size_b); - return AVERROR_PATCHWELCOME; + ret = AVERROR_PATCHWELCOME; + goto end; } size = size_r; size2 = size * size; - ret = allocate_3dlut(ctx, size); + if(prelut_sizes[0] && prelut_sizes[1] && prelut_sizes[2]) + prelut = 1; + + ret = allocate_3dlut(ctx, size, prelut); if (ret < 0) return ret; @@ -726,10 +943,13 @@ static int parse_cinespace(AVFilterContext *ctx, FILE *f) for (int j = 0; j < size; j++) { for (int i = 0; i < size; i++) { struct rgbvec *vec = &lut3d->lut[i * size2 + j * size + k]; - if (k != 0 || j != 0 || i != 0) - NEXT_LINE(skip_line(line)); - if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3) - return AVERROR_INVALIDDATA; + + NEXT_LINE_OR_GOTO(skip_line(line), end); + if (av_sscanf(line, "%f %f %f", &vec->r, &vec->g, &vec->b) != 3) { + ret = AVERROR_INVALIDDATA; + goto end; + } + vec->r *= out_max[0] - out_min[0]; vec->g *= out_max[1] - out_min[1]; vec->b *= out_max[2] - out_min[2]; @@ -741,11 +961,44 @@ static int parse_cinespace(AVFilterContext *ctx, FILE *f) } } - lut3d->scale.r = av_clipf(1. / (in_max[0] - in_min[0]), 0.f, 1.f); - lut3d->scale.g = av_clipf(1. / (in_max[1] - in_min[1]), 0.f, 1.f); - lut3d->scale.b = av_clipf(1. / (in_max[2] - in_min[2]), 0.f, 1.f); + if (prelut) { + for (int c = 0; c < 3; c++) { - return 0; + lut3d->prelut.min[c] = in_min[c]; + lut3d->prelut.max[c] = in_max[c]; + lut3d->prelut.scale[c] = (1.0f / (float)(in_max[c] - in_min[c])) * (lut3d->prelut.size - 1); + + for (int i = 0; i < lut3d->prelut.size; ++i) { + float mix = (float) i / (float)(lut3d->prelut.size - 1); + float x = lerpf(in_min[c], in_max[c], mix); + + int idx = nearest_sample_index(in_prelut[c], x, 0, prelut_sizes[c]-1); + av_assert0(idx + 1 < prelut_sizes[c]); + + float a = out_prelut[c][idx + 0]; + float b = out_prelut[c][idx + 1]; + mix = x - in_prelut[c][idx]; + + float outval = lerpf(a, b, mix); + lut3d->prelut.lut[c][i] = sanitizef(outval); + } + } + lut3d->scale.r = 1.00f; + lut3d->scale.g = 1.00f; + lut3d->scale.b = 1.00f; + + } else { + lut3d->scale.r = av_clipf(1. / (in_max[0] - in_min[0]), 0.f, 1.f); + lut3d->scale.g = av_clipf(1. / (in_max[1] - in_min[1]), 0.f, 1.f); + lut3d->scale.b = av_clipf(1. / (in_max[2] - in_min[2]), 0.f, 1.f); + } + +end: + for (int c = 0; c < 3; c++) { + av_freep(&in_prelut[c]); + av_freep(&out_prelut[c]); + } + return ret; } static int set_identity_matrix(AVFilterContext *ctx, int size) @@ -755,7 +1008,7 @@ static int set_identity_matrix(AVFilterContext *ctx, int size) const int size2 = size * size; const float c = 1. / (size - 1); - ret = allocate_3dlut(ctx, size); + ret = allocate_3dlut(ctx, size, 0); if (ret < 0) return ret; @@ -939,8 +1192,12 @@ end: static av_cold void lut3d_uninit(AVFilterContext *ctx) { LUT3DContext *lut3d = ctx->priv; - + int i; av_freep(&lut3d->lut); + + for(i = 0; i < 3; i++) { + av_freep(&lut3d->prelut.lut[i]); + } } static const AVFilterPad lut3d_inputs[] = { @@ -1158,7 +1415,7 @@ static int config_clut(AVFilterLink *inlink) return AVERROR(EINVAL); } - return allocate_3dlut(ctx, level); + return allocate_3dlut(ctx, level, 0); } static int update_apply_clut(FFFrameSync *fs)