LCOV - code coverage report
Current view: top level - libavfilter - vf_waveform.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 236 1521 15.5 %
Date: 2017-12-15 18:13:28 Functions: 13 46 28.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012-2016 Paul B Mahol
       3             :  * Copyright (c) 2013 Marton Balint
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/avassert.h"
      23             : #include "libavutil/opt.h"
      24             : #include "libavutil/parseutils.h"
      25             : #include "libavutil/pixdesc.h"
      26             : #include "libavutil/xga_font_data.h"
      27             : #include "avfilter.h"
      28             : #include "formats.h"
      29             : #include "internal.h"
      30             : #include "video.h"
      31             : 
      32             : enum FilterType {
      33             :     LOWPASS,
      34             :     FLAT,
      35             :     AFLAT,
      36             :     CHROMA,
      37             :     COLOR,
      38             :     ACOLOR,
      39             :     NB_FILTERS
      40             : };
      41             : 
      42             : enum DisplayType {
      43             :     OVERLAY,
      44             :     STACK,
      45             :     PARADE,
      46             :     NB_DISPLAYS
      47             : };
      48             : 
      49             : enum ScaleType {
      50             :     DIGITAL,
      51             :     MILLIVOLTS,
      52             :     IRE,
      53             :     NB_SCALES
      54             : };
      55             : 
      56             : typedef struct GraticuleLine {
      57             :     const char *name;
      58             :     uint16_t pos;
      59             : } GraticuleLine;
      60             : 
      61             : typedef struct GraticuleLines {
      62             :     struct GraticuleLine line[4];
      63             : } GraticuleLines;
      64             : 
      65             : typedef struct WaveformContext {
      66             :     const AVClass *class;
      67             :     int            mode;
      68             :     int            acomp;
      69             :     int            dcomp;
      70             :     int            ncomp;
      71             :     int            pcomp;
      72             :     uint8_t        bg_color[4];
      73             :     float          fintensity;
      74             :     int            intensity;
      75             :     int            mirror;
      76             :     int            display;
      77             :     int            envelope;
      78             :     int            graticule;
      79             :     float          opacity;
      80             :     float          bgopacity;
      81             :     int            estart[4];
      82             :     int            eend[4];
      83             :     int            *emax[4][4];
      84             :     int            *emin[4][4];
      85             :     int            *peak;
      86             :     int            filter;
      87             :     int            flags;
      88             :     int            bits;
      89             :     int            max;
      90             :     int            size;
      91             :     int            scale;
      92             :     int            shift_w[4], shift_h[4];
      93             :     GraticuleLines *glines;
      94             :     int            nb_glines;
      95             :     void (*waveform)(struct WaveformContext *s,
      96             :                      AVFrame *in, AVFrame *out,
      97             :                      int component, int intensity,
      98             :                      int offset_y, int offset_x,
      99             :                      int column, int mirror);
     100             :     void (*graticulef)(struct WaveformContext *s, AVFrame *out);
     101             :     const AVPixFmtDescriptor *desc;
     102             :     const AVPixFmtDescriptor *odesc;
     103             : } WaveformContext;
     104             : 
     105             : #define OFFSET(x) offsetof(WaveformContext, x)
     106             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
     107             : 
     108             : static const AVOption waveform_options[] = {
     109             :     { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
     110             :     { "m",    "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
     111             :         { "row",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
     112             :         { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
     113             :     { "intensity", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 1, FLAGS },
     114             :     { "i",         "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 1, FLAGS },
     115             :     { "mirror", "set mirroring", OFFSET(mirror), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
     116             :     { "r",      "set mirroring", OFFSET(mirror), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
     117             :     { "display", "set display mode", OFFSET(display), AV_OPT_TYPE_INT, {.i64=STACK}, 0, NB_DISPLAYS-1, FLAGS, "display" },
     118             :     { "d",       "set display mode", OFFSET(display), AV_OPT_TYPE_INT, {.i64=STACK}, 0, NB_DISPLAYS-1, FLAGS, "display" },
     119             :         { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY}, 0, 0, FLAGS, "display" },
     120             :         { "stack",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=STACK},   0, 0, FLAGS, "display" },
     121             :         { "parade",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=PARADE},  0, 0, FLAGS, "display" },
     122             :     { "components", "set components to display", OFFSET(pcomp), AV_OPT_TYPE_INT, {.i64=1}, 1, 15, FLAGS },
     123             :     { "c",          "set components to display", OFFSET(pcomp), AV_OPT_TYPE_INT, {.i64=1}, 1, 15, FLAGS },
     124             :     { "envelope", "set envelope to display", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope" },
     125             :     { "e",        "set envelope to display", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope" },
     126             :         { "none",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "envelope" },
     127             :         { "instant",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
     128             :         { "peak",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
     129             :         { "peak+instant", NULL, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
     130             :     { "filter", "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS-1, FLAGS, "filter" },
     131             :     { "f",      "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS-1, FLAGS, "filter" },
     132             :         { "lowpass", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LOWPASS}, 0, 0, FLAGS, "filter" },
     133             :         { "flat"   , NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAT},    0, 0, FLAGS, "filter" },
     134             :         { "aflat"  , NULL, 0, AV_OPT_TYPE_CONST, {.i64=AFLAT},   0, 0, FLAGS, "filter" },
     135             :         { "chroma",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=CHROMA},  0, 0, FLAGS, "filter" },
     136             :         { "color",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},   0, 0, FLAGS, "filter" },
     137             :         { "acolor",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=ACOLOR},  0, 0, FLAGS, "filter" },
     138             :     { "graticule", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "graticule" },
     139             :     { "g",         "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "graticule" },
     140             :         { "none",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "graticule" },
     141             :         { "green", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "graticule" },
     142             :     { "opacity", "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
     143             :     { "o",       "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
     144             :     { "flags", "set graticule flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, 3, FLAGS, "flags" },
     145             :     { "fl",    "set graticule flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, 3, FLAGS, "flags" },
     146             :         { "numbers",  "draw numbers", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "flags" },
     147             :         { "dots",     "draw dots instead of lines", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "flags" },
     148             :     { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SCALES-1, FLAGS, "scale" },
     149             :     { "s",     "set scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SCALES-1, FLAGS, "scale" },
     150             :         { "digital",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=DIGITAL},    0, 0, FLAGS, "scale" },
     151             :         { "millivolts", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MILLIVOLTS}, 0, 0, FLAGS, "scale" },
     152             :         { "ire",        NULL, 0, AV_OPT_TYPE_CONST, {.i64=IRE},        0, 0, FLAGS, "scale" },
     153             :     { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
     154             :     { "b",         "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
     155             :     { NULL }
     156             : };
     157             : 
     158             : AVFILTER_DEFINE_CLASS(waveform);
     159             : 
     160             : static const enum AVPixelFormat in_lowpass_pix_fmts[] = {
     161             :     AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
     162             :     AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
     163             :     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
     164             :     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
     165             :     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
     166             :     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
     167             :     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
     168             :     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
     169             :     AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
     170             :     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
     171             :     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
     172             :     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
     173             :     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
     174             :     AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12,
     175             :     AV_PIX_FMT_NONE
     176             : };
     177             : 
     178             : static const enum AVPixelFormat in_color_pix_fmts[] = {
     179             :     AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
     180             :     AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
     181             :     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
     182             :     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
     183             :     AV_PIX_FMT_YUV411P,
     184             :     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
     185             :     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
     186             :     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
     187             :     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
     188             :     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
     189             :     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
     190             :     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
     191             :     AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12,
     192             :     AV_PIX_FMT_NONE
     193             : };
     194             : 
     195             : static const enum AVPixelFormat in_flat_pix_fmts[] = {
     196             :     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
     197             :     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
     198             :     AV_PIX_FMT_YUV411P,
     199             :     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
     200             :     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
     201             :     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
     202             :     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
     203             :     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
     204             :     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
     205             :     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
     206             :     AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12,
     207             :     AV_PIX_FMT_NONE
     208             : };
     209             : 
     210             : static const enum AVPixelFormat out_rgb8_lowpass_pix_fmts[] = {
     211             :     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
     212             :     AV_PIX_FMT_NONE
     213             : };
     214             : 
     215             : static const enum AVPixelFormat out_rgb9_lowpass_pix_fmts[] = {
     216             :     AV_PIX_FMT_GBRP9,
     217             :     AV_PIX_FMT_NONE
     218             : };
     219             : 
     220             : static const enum AVPixelFormat out_rgb10_lowpass_pix_fmts[] = {
     221             :     AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
     222             :     AV_PIX_FMT_NONE
     223             : };
     224             : 
     225             : static const enum AVPixelFormat out_rgb12_lowpass_pix_fmts[] = {
     226             :     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
     227             :     AV_PIX_FMT_NONE
     228             : };
     229             : 
     230             : static const enum AVPixelFormat out_yuv8_lowpass_pix_fmts[] = {
     231             :     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P,
     232             :     AV_PIX_FMT_NONE
     233             : };
     234             : 
     235             : static const enum AVPixelFormat out_yuv9_lowpass_pix_fmts[] = {
     236             :     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUVA444P9,
     237             :     AV_PIX_FMT_NONE
     238             : };
     239             : 
     240             : static const enum AVPixelFormat out_yuv10_lowpass_pix_fmts[] = {
     241             :     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10,
     242             :     AV_PIX_FMT_NONE
     243             : };
     244             : 
     245             : static const enum AVPixelFormat out_yuv12_lowpass_pix_fmts[] = {
     246             :     AV_PIX_FMT_YUV444P12,
     247             :     AV_PIX_FMT_NONE
     248             : };
     249             : 
     250             : static const enum AVPixelFormat out_gray8_lowpass_pix_fmts[] = {
     251             :     AV_PIX_FMT_GRAY8,
     252             :     AV_PIX_FMT_NONE
     253             : };
     254             : 
     255             : static const enum AVPixelFormat out_gray9_lowpass_pix_fmts[] = {
     256             :     AV_PIX_FMT_GRAY9,
     257             :     AV_PIX_FMT_NONE
     258             : };
     259             : 
     260             : static const enum AVPixelFormat out_gray10_lowpass_pix_fmts[] = {
     261             :     AV_PIX_FMT_GRAY10,
     262             :     AV_PIX_FMT_NONE
     263             : };
     264             : 
     265             : static const enum AVPixelFormat out_gray12_lowpass_pix_fmts[] = {
     266             :     AV_PIX_FMT_GRAY12,
     267             :     AV_PIX_FMT_NONE
     268             : };
     269             : 
     270             : static const enum AVPixelFormat flat_pix_fmts[] = {
     271             :     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
     272             :     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
     273             :     AV_PIX_FMT_YUV444P12,
     274             :     AV_PIX_FMT_NONE
     275             : };
     276             : 
     277           8 : static int query_formats(AVFilterContext *ctx)
     278             : {
     279           8 :     WaveformContext *s = ctx->priv;
     280             :     const enum AVPixelFormat *out_pix_fmts;
     281             :     const enum AVPixelFormat *in_pix_fmts;
     282             :     const AVPixFmtDescriptor *desc;
     283             :     AVFilterFormats *avff;
     284             :     int depth, rgb, i, ret, ncomp;
     285             : 
     286          12 :     if (!ctx->inputs[0]->in_formats ||
     287           4 :         !ctx->inputs[0]->in_formats->nb_formats) {
     288           4 :         return AVERROR(EAGAIN);
     289             :     }
     290             : 
     291           4 :     switch (s->filter) {
     292           4 :     case LOWPASS: in_pix_fmts = in_lowpass_pix_fmts; break;
     293           0 :     case CHROMA:
     294             :     case AFLAT:
     295           0 :     case FLAT:    in_pix_fmts = in_flat_pix_fmts;    break;
     296           0 :     case ACOLOR:
     297           0 :     case COLOR:   in_pix_fmts = in_color_pix_fmts;   break;
     298           0 :     default: return AVERROR_BUG;
     299             :     }
     300             : 
     301           4 :     if (!ctx->inputs[0]->out_formats) {
     302           4 :         if ((ret = ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
     303           0 :             return ret;
     304             :     }
     305             : 
     306           4 :     avff = ctx->inputs[0]->in_formats;
     307           4 :     desc = av_pix_fmt_desc_get(avff->formats[0]);
     308           4 :     ncomp = desc->nb_components;
     309           4 :     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
     310           4 :     depth = desc->comp[0].depth;
     311           4 :     for (i = 1; i < avff->nb_formats; i++) {
     312           0 :         desc = av_pix_fmt_desc_get(avff->formats[i]);
     313           0 :         if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
     314           0 :             depth != desc->comp[0].depth)
     315           0 :             return AVERROR(EAGAIN);
     316             :     }
     317             : 
     318           4 :     if (s->filter == LOWPASS && ncomp == 1 && depth == 8)
     319           0 :         out_pix_fmts = out_gray8_lowpass_pix_fmts;
     320           4 :     else if (s->filter == LOWPASS && ncomp == 1 && depth == 9)
     321           0 :         out_pix_fmts = out_gray9_lowpass_pix_fmts;
     322           4 :     else if (s->filter == LOWPASS && ncomp == 1 && depth == 10)
     323           0 :         out_pix_fmts = out_gray10_lowpass_pix_fmts;
     324           4 :     else if (s->filter == LOWPASS && ncomp == 1 && depth == 12)
     325           0 :         out_pix_fmts = out_gray12_lowpass_pix_fmts;
     326           4 :     else if (rgb && depth == 8 && ncomp > 2)
     327           0 :         out_pix_fmts = out_rgb8_lowpass_pix_fmts;
     328           4 :     else if (rgb && depth == 9 && ncomp > 2)
     329           0 :         out_pix_fmts = out_rgb9_lowpass_pix_fmts;
     330           4 :     else if (rgb && depth == 10 && ncomp > 2)
     331           0 :         out_pix_fmts = out_rgb10_lowpass_pix_fmts;
     332           4 :     else if (rgb && depth == 12 && ncomp > 2)
     333           0 :         out_pix_fmts = out_rgb12_lowpass_pix_fmts;
     334           4 :     else if (depth == 8 && ncomp > 2)
     335           4 :         out_pix_fmts = out_yuv8_lowpass_pix_fmts;
     336           0 :     else if (depth == 9 && ncomp > 2)
     337           0 :         out_pix_fmts = out_yuv9_lowpass_pix_fmts;
     338           0 :     else if (depth == 10 && ncomp > 2)
     339           0 :         out_pix_fmts = out_yuv10_lowpass_pix_fmts;
     340           0 :     else if (depth == 12 && ncomp > 2)
     341           0 :         out_pix_fmts = out_yuv12_lowpass_pix_fmts;
     342             :     else
     343           0 :         return AVERROR(EAGAIN);
     344           4 :     if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
     345           0 :         return ret;
     346             : 
     347           4 :     return 0;
     348             : }
     349             : 
     350           0 : static void envelope_instant16(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
     351             : {
     352           0 :     const int dst_linesize = out->linesize[component] / 2;
     353           0 :     const int bg = s->bg_color[component] * (s->max / 256);
     354           0 :     const int limit = s->max - 1;
     355           0 :     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
     356           0 :     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
     357           0 :     const int start = s->estart[plane];
     358           0 :     const int end = s->eend[plane];
     359             :     uint16_t *dst;
     360             :     int x, y;
     361             : 
     362           0 :     if (s->mode) {
     363           0 :         for (x = offset; x < offset + dst_w; x++) {
     364           0 :             for (y = start; y < end; y++) {
     365           0 :                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
     366           0 :                 if (dst[0] != bg) {
     367           0 :                     dst[0] = limit;
     368           0 :                     break;
     369             :                 }
     370             :             }
     371           0 :             for (y = end - 1; y >= start; y--) {
     372           0 :                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
     373           0 :                 if (dst[0] != bg) {
     374           0 :                     dst[0] = limit;
     375           0 :                     break;
     376             :                 }
     377             :             }
     378             :         }
     379             :     } else {
     380           0 :         for (y = offset; y < offset + dst_h; y++) {
     381           0 :             dst = (uint16_t *)out->data[component] + y * dst_linesize;
     382           0 :             for (x = start; x < end; x++) {
     383           0 :                 if (dst[x] != bg) {
     384           0 :                     dst[x] = limit;
     385           0 :                     break;
     386             :                 }
     387             :             }
     388           0 :             for (x = end - 1; x >= start; x--) {
     389           0 :                 if (dst[x] != bg) {
     390           0 :                     dst[x] = limit;
     391           0 :                     break;
     392             :                 }
     393             :             }
     394             :         }
     395             :     }
     396           0 : }
     397             : 
     398          50 : static void envelope_instant(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
     399             : {
     400          50 :     const int dst_linesize = out->linesize[component];
     401          50 :     const uint8_t bg = s->bg_color[component];
     402          50 :     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
     403          50 :     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
     404          50 :     const int start = s->estart[plane];
     405          50 :     const int end = s->eend[plane];
     406             :     uint8_t *dst;
     407             :     int x, y;
     408             : 
     409          50 :     if (s->mode) {
     410       17650 :         for (x = offset; x < offset + dst_w; x++) {
     411      375540 :             for (y = start; y < end; y++) {
     412      375540 :                 dst = out->data[component] + y * dst_linesize + x;
     413      375540 :                 if (dst[0] != bg) {
     414       17600 :                     dst[0] = 255;
     415       17600 :                     break;
     416             :                 }
     417             :             }
     418      385656 :             for (y = end - 1; y >= start; y--) {
     419      385656 :                 dst = out->data[component] + y * dst_linesize + x;
     420      385656 :                 if (dst[0] != bg) {
     421       17600 :                     dst[0] = 255;
     422       17600 :                     break;
     423             :                 }
     424             :             }
     425             :         }
     426             :     } else {
     427           0 :         for (y = offset; y < offset + dst_h; y++) {
     428           0 :             dst = out->data[component] + y * dst_linesize;
     429           0 :             for (x = start; x < end; x++) {
     430           0 :                 if (dst[x] != bg) {
     431           0 :                     dst[x] = 255;
     432           0 :                     break;
     433             :                 }
     434             :             }
     435           0 :             for (x = end - 1; x >= start; x--) {
     436           0 :                 if (dst[x] != bg) {
     437           0 :                     dst[x] = 255;
     438           0 :                     break;
     439             :                 }
     440             :             }
     441             :         }
     442             :     }
     443          50 : }
     444             : 
     445           0 : static void envelope_peak16(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
     446             : {
     447           0 :     const int dst_linesize = out->linesize[component] / 2;
     448           0 :     const int bg = s->bg_color[component] * (s->max / 256);
     449           0 :     const int limit = s->max - 1;
     450           0 :     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
     451           0 :     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
     452           0 :     const int start = s->estart[plane];
     453           0 :     const int end = s->eend[plane];
     454           0 :     int *emax = s->emax[plane][component];
     455           0 :     int *emin = s->emin[plane][component];
     456             :     uint16_t *dst;
     457             :     int x, y;
     458             : 
     459           0 :     if (s->mode) {
     460           0 :         for (x = offset; x < offset + dst_w; x++) {
     461           0 :             for (y = start; y < end && y < emin[x - offset]; y++) {
     462           0 :                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
     463           0 :                 if (dst[0] != bg) {
     464           0 :                     emin[x - offset] = y;
     465           0 :                     break;
     466             :                 }
     467             :             }
     468           0 :             for (y = end - 1; y >= start && y >= emax[x - offset]; y--) {
     469           0 :                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
     470           0 :                 if (dst[0] != bg) {
     471           0 :                     emax[x - offset] = y;
     472           0 :                     break;
     473             :                 }
     474             :             }
     475             :         }
     476             : 
     477           0 :         if (s->envelope == 3)
     478           0 :             envelope_instant16(s, out, plane, component, offset);
     479             : 
     480           0 :         for (x = offset; x < offset + dst_w; x++) {
     481           0 :             dst = (uint16_t *)out->data[component] + emin[x - offset] * dst_linesize + x;
     482           0 :             dst[0] = limit;
     483           0 :             dst = (uint16_t *)out->data[component] + emax[x - offset] * dst_linesize + x;
     484           0 :             dst[0] = limit;
     485             :         }
     486             :     } else {
     487           0 :         for (y = offset; y < offset + dst_h; y++) {
     488           0 :             dst = (uint16_t *)out->data[component] + y * dst_linesize;
     489           0 :             for (x = start; x < end && x < emin[y - offset]; x++) {
     490           0 :                 if (dst[x] != bg) {
     491           0 :                     emin[y - offset] = x;
     492           0 :                     break;
     493             :                 }
     494             :             }
     495           0 :             for (x = end - 1; x >= start && x >= emax[y - offset]; x--) {
     496           0 :                 if (dst[x] != bg) {
     497           0 :                     emax[y - offset] = x;
     498           0 :                     break;
     499             :                 }
     500             :             }
     501             :         }
     502             : 
     503           0 :         if (s->envelope == 3)
     504           0 :             envelope_instant16(s, out, plane, component, offset);
     505             : 
     506           0 :         for (y = offset; y < offset + dst_h; y++) {
     507           0 :             dst = (uint16_t *)out->data[component] + y * dst_linesize + emin[y - offset];
     508           0 :             dst[0] = limit;
     509           0 :             dst = (uint16_t *)out->data[component] + y * dst_linesize + emax[y - offset];
     510           0 :             dst[0] = limit;
     511             :         }
     512             :     }
     513           0 : }
     514             : 
     515          50 : static void envelope_peak(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
     516             : {
     517          50 :     const int dst_linesize = out->linesize[component];
     518          50 :     const int bg = s->bg_color[component];
     519          50 :     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
     520          50 :     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
     521          50 :     const int start = s->estart[plane];
     522          50 :     const int end = s->eend[plane];
     523          50 :     int *emax = s->emax[plane][component];
     524          50 :     int *emin = s->emin[plane][component];
     525             :     uint8_t *dst;
     526             :     int x, y;
     527             : 
     528          50 :     if (s->mode) {
     529       17650 :         for (x = offset; x < offset + dst_w; x++) {
     530      152559 :             for (y = start; y < end && y < emin[x - offset]; y++) {
     531      136288 :                 dst = out->data[component] + y * dst_linesize + x;
     532      136288 :                 if (dst[0] != bg) {
     533        1329 :                     emin[x - offset] = y;
     534        1329 :                     break;
     535             :                 }
     536             :             }
     537      159188 :             for (y = end - 1; y >= start && y >= emax[x - offset]; y--) {
     538      144269 :                 dst = out->data[component] + y * dst_linesize + x;
     539      144269 :                 if (dst[0] != bg) {
     540        2681 :                     emax[x - offset] = y;
     541        2681 :                     break;
     542             :                 }
     543             :             }
     544             :         }
     545             : 
     546          50 :         if (s->envelope == 3)
     547          50 :             envelope_instant(s, out, plane, component, offset);
     548             : 
     549       17650 :         for (x = offset; x < offset + dst_w; x++) {
     550       17600 :             dst = out->data[component] + emin[x - offset] * dst_linesize + x;
     551       17600 :             dst[0] = 255;
     552       17600 :             dst = out->data[component] + emax[x - offset] * dst_linesize + x;
     553       17600 :             dst[0] = 255;
     554             :         }
     555             :     } else {
     556           0 :         for (y = offset; y < offset + dst_h; y++) {
     557           0 :             dst = out->data[component] + y * dst_linesize;
     558           0 :             for (x = start; x < end && x < emin[y - offset]; x++) {
     559           0 :                 if (dst[x] != bg) {
     560           0 :                     emin[y - offset] = x;
     561           0 :                     break;
     562             :                 }
     563             :             }
     564           0 :             for (x = end - 1; x >= start && x >= emax[y - offset]; x--) {
     565           0 :                 if (dst[x] != bg) {
     566           0 :                     emax[y - offset] = x;
     567           0 :                     break;
     568             :                 }
     569             :             }
     570             :         }
     571             : 
     572           0 :         if (s->envelope == 3)
     573           0 :             envelope_instant(s, out, plane, component, offset);
     574             : 
     575           0 :         for (y = offset; y < offset + dst_h; y++) {
     576           0 :             dst = out->data[component] + y * dst_linesize + emin[y - offset];
     577           0 :             dst[0] = 255;
     578           0 :             dst = out->data[component] + y * dst_linesize + emax[y - offset];
     579           0 :             dst[0] = 255;
     580             :         }
     581             :     }
     582          50 : }
     583             : 
     584           0 : static void envelope16(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
     585             : {
     586           0 :     if (s->envelope == 0) {
     587           0 :         return;
     588           0 :     } else if (s->envelope == 1) {
     589           0 :         envelope_instant16(s, out, plane, component, offset);
     590             :     } else {
     591           0 :         envelope_peak16(s, out, plane, component, offset);
     592             :     }
     593             : }
     594             : 
     595         250 : static void envelope(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
     596             : {
     597         250 :     if (s->envelope == 0) {
     598         200 :         return;
     599          50 :     } else if (s->envelope == 1) {
     600           0 :         envelope_instant(s, out, plane, component, offset);
     601             :     } else {
     602          50 :         envelope_peak(s, out, plane, component, offset);
     603             :     }
     604             : }
     605             : 
     606           0 : static void update16(uint16_t *target, int max, int intensity, int limit)
     607             : {
     608           0 :     if (*target <= max)
     609           0 :         *target += intensity;
     610             :     else
     611           0 :         *target = limit;
     612           0 : }
     613             : 
     614    17740800 : static void update(uint8_t *target, int max, int intensity)
     615             : {
     616    17740800 :     if (*target <= max)
     617    17740797 :         *target += intensity;
     618             :     else
     619           3 :         *target = 255;
     620    17740800 : }
     621             : 
     622           0 : static av_always_inline void lowpass16(WaveformContext *s,
     623             :                                        AVFrame *in, AVFrame *out,
     624             :                                        int component, int intensity,
     625             :                                        int offset_y, int offset_x,
     626             :                                        int column, int mirror)
     627             : {
     628           0 :     const int plane = s->desc->comp[component].plane;
     629           0 :     const int shift_w = s->shift_w[component];
     630           0 :     const int shift_h = s->shift_h[component];
     631           0 :     const int src_linesize = in->linesize[plane] / 2;
     632           0 :     const int dst_linesize = out->linesize[plane] / 2;
     633           0 :     const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
     634           0 :     const int limit = s->max - 1;
     635           0 :     const int max = limit - intensity;
     636           0 :     const int src_h = AV_CEIL_RSHIFT(in->height, shift_h);
     637           0 :     const int src_w = AV_CEIL_RSHIFT(in->width, shift_w);
     638           0 :     const uint16_t *src_data = (const uint16_t *)in->data[plane];
     639           0 :     uint16_t *dst_data = (uint16_t *)out->data[plane] + offset_y * dst_linesize + offset_x;
     640           0 :     uint16_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
     641           0 :     uint16_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
     642           0 :     const int step = column ? 1 << shift_w : 1 << shift_h;
     643             :     const uint16_t *p;
     644             :     int y;
     645             : 
     646           0 :     if (!column && mirror)
     647           0 :         dst_data += s->size;
     648             : 
     649           0 :     for (y = 0; y < src_h; y++) {
     650           0 :         const uint16_t *src_data_end = src_data + src_w;
     651           0 :         uint16_t *dst = dst_line;
     652             : 
     653           0 :         for (p = src_data; p < src_data_end; p++) {
     654             :             uint16_t *target;
     655           0 :             int i = 0, v = FFMIN(*p, limit);
     656             : 
     657           0 :             if (column) {
     658             :                 do {
     659           0 :                     target = dst++ + dst_signed_linesize * v;
     660           0 :                     update16(target, max, intensity, limit);
     661           0 :                 } while (++i < step);
     662             :             } else {
     663           0 :                 uint16_t *row = dst_data;
     664             :                 do {
     665           0 :                     if (mirror)
     666           0 :                         target = row - v - 1;
     667             :                     else
     668           0 :                         target = row + v;
     669           0 :                     update16(target, max, intensity, limit);
     670           0 :                     row += dst_linesize;
     671           0 :                 } while (++i < step);
     672             :             }
     673             :         }
     674           0 :         src_data += src_linesize;
     675           0 :         dst_data += dst_linesize * step;
     676             :     }
     677             : 
     678           0 :     envelope16(s, out, plane, plane, column ? offset_x : offset_y);
     679           0 : }
     680             : 
     681             : #define LOWPASS16_FUNC(name, column, mirror)               \
     682             : static void lowpass16_##name(WaveformContext *s,           \
     683             :                              AVFrame *in, AVFrame *out,    \
     684             :                              int component, int intensity, \
     685             :                              int offset_y, int offset_x,   \
     686             :                              int unused1, int unused2)     \
     687             : {                                                          \
     688             :     lowpass16(s, in, out, component, intensity,            \
     689             :               offset_y, offset_x, column, mirror);         \
     690             : }
     691             : 
     692           0 : LOWPASS16_FUNC(column_mirror, 1, 1)
     693           0 : LOWPASS16_FUNC(column,        1, 0)
     694           0 : LOWPASS16_FUNC(row_mirror,    0, 1)
     695           0 : LOWPASS16_FUNC(row,           0, 0)
     696             : 
     697         250 : static av_always_inline void lowpass(WaveformContext *s,
     698             :                                      AVFrame *in, AVFrame *out,
     699             :                                      int component, int intensity,
     700             :                                      int offset_y, int offset_x,
     701             :                                      int column, int mirror)
     702             : {
     703         250 :     const int plane = s->desc->comp[component].plane;
     704         250 :     const int shift_w = s->shift_w[component];
     705         250 :     const int shift_h = s->shift_h[component];
     706         250 :     const int src_linesize = in->linesize[plane];
     707         250 :     const int dst_linesize = out->linesize[plane];
     708         250 :     const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
     709         250 :     const int max = 255 - intensity;
     710         250 :     const int src_h = AV_CEIL_RSHIFT(in->height, shift_h);
     711         250 :     const int src_w = AV_CEIL_RSHIFT(in->width, shift_w);
     712         250 :     const uint8_t *src_data = in->data[plane];
     713         250 :     uint8_t *dst_data = out->data[plane] + offset_y * dst_linesize + offset_x;
     714         250 :     uint8_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
     715         250 :     uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
     716         250 :     const int step = column ? 1 << shift_w : 1 << shift_h;
     717             :     const uint8_t *p;
     718             :     int y;
     719             : 
     720         250 :     if (!column && mirror)
     721          50 :         dst_data += s->size;
     722             : 
     723       57850 :     for (y = 0; y < src_h; y++) {
     724       57600 :         const uint8_t *src_data_end = src_data + src_w;
     725       57600 :         uint8_t *dst = dst_line;
     726             : 
     727    17798400 :         for (p = src_data; p < src_data_end; p++) {
     728             :             uint8_t *target;
     729    17740800 :             if (column) {
     730    12672000 :                 target = dst + dst_signed_linesize * *p;
     731    12672000 :                 dst += step;
     732    12672000 :                 update(target, max, intensity);
     733             :             } else {
     734     5068800 :                 uint8_t *row = dst_data;
     735     5068800 :                 if (mirror)
     736     5068800 :                     target = row - *p - 1;
     737             :                 else
     738           0 :                     target = row + *p;
     739     5068800 :                 update(target, max, intensity);
     740     5068800 :                 row += dst_linesize;
     741             :             }
     742             :         }
     743       57600 :         src_data += src_linesize;
     744       57600 :         dst_data += dst_linesize * step;
     745             :     }
     746             : 
     747         350 :     if (column && step > 1) {
     748         100 :         const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
     749         100 :         const int dst_h = 256;
     750             :         uint8_t *dst;
     751             :         int x, z;
     752             : 
     753         100 :         dst = out->data[plane] + offset_y * dst_linesize + offset_x;
     754       25700 :         for (y = 0; y < dst_h; y++) {
     755     4531200 :             for (x = 0; x < dst_w; x+=step) {
     756     9011200 :                 for (z = 1; z < step; z++) {
     757     4505600 :                     dst[x + z] = dst[x];
     758             :                 }
     759             :             }
     760       25600 :             dst += dst_linesize;
     761             :         }
     762         150 :     } else if (step > 1) {
     763           0 :         const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
     764           0 :         const int dst_w = 256;
     765             :         uint8_t *dst;
     766             :         int z;
     767             : 
     768           0 :         dst = out->data[plane] + offset_y * dst_linesize + offset_x;
     769           0 :         for (y = 0; y < dst_h; y+=step) {
     770           0 :             for (z = 1; z < step; z++)
     771           0 :                 memcpy(dst + dst_linesize * z, dst, dst_w);
     772           0 :             dst += dst_linesize * step;
     773             :         }
     774             :     }
     775             : 
     776         250 :     envelope(s, out, plane, plane, column ? offset_x : offset_y);
     777         250 : }
     778             : 
     779             : #define LOWPASS_FUNC(name, column, mirror)               \
     780             : static void lowpass_##name(WaveformContext *s,           \
     781             :                            AVFrame *in, AVFrame *out,    \
     782             :                            int component, int intensity, \
     783             :                            int offset_y, int offset_x,   \
     784             :                            int unused1, int unused2)     \
     785             : {                                                        \
     786             :     lowpass(s, in, out, component, intensity,            \
     787             :             offset_y, offset_x, column, mirror);         \
     788             : }
     789             : 
     790         200 : LOWPASS_FUNC(column_mirror, 1, 1)
     791           0 : LOWPASS_FUNC(column,        1, 0)
     792          50 : LOWPASS_FUNC(row_mirror,    0, 1)
     793           0 : LOWPASS_FUNC(row,           0, 0)
     794             : 
     795           0 : static av_always_inline void flat16(WaveformContext *s,
     796             :                                     AVFrame *in, AVFrame *out,
     797             :                                     int component, int intensity,
     798             :                                     int offset_y, int offset_x,
     799             :                                     int column, int mirror)
     800             : {
     801           0 :     const int plane = s->desc->comp[component].plane;
     802           0 :     const int c0_linesize = in->linesize[ plane + 0 ] / 2;
     803           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
     804           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
     805           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
     806           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
     807           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
     808           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
     809           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
     810           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
     811           0 :     const int d0_linesize = out->linesize[ plane + 0 ] / 2;
     812           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
     813           0 :     const int limit = s->max - 1;
     814           0 :     const int max = limit - intensity;
     815           0 :     const int mid = s->max / 2;
     816           0 :     const int src_h = in->height;
     817           0 :     const int src_w = in->width;
     818             :     int x, y;
     819             : 
     820           0 :     if (column) {
     821           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
     822           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
     823             : 
     824           0 :         for (x = 0; x < src_w; x++) {
     825           0 :             const uint16_t *c0_data = (uint16_t *)in->data[plane + 0];
     826           0 :             const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
     827           0 :             const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
     828           0 :             uint16_t *d0_data = (uint16_t *)(out->data[plane]) + offset_y * d0_linesize + offset_x;
     829           0 :             uint16_t *d1_data = (uint16_t *)(out->data[(plane + 1) % s->ncomp]) + offset_y * d1_linesize + offset_x;
     830           0 :             uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
     831           0 :             uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
     832           0 :             uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
     833           0 :             uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
     834             : 
     835           0 :             for (y = 0; y < src_h; y++) {
     836           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + s->max;
     837           0 :                 const int c1 = FFMIN(FFABS(c1_data[x >> c1_shift_w] - mid) + FFABS(c2_data[x >> c2_shift_w] - mid), limit);
     838             :                 uint16_t *target;
     839             : 
     840           0 :                 target = d0 + x + d0_signed_linesize * c0;
     841           0 :                 update16(target, max, intensity, limit);
     842           0 :                 target = d1 + x + d1_signed_linesize * (c0 - c1);
     843           0 :                 update16(target, max, intensity, limit);
     844           0 :                 target = d1 + x + d1_signed_linesize * (c0 + c1);
     845           0 :                 update16(target, max, intensity, limit);
     846             : 
     847           0 :                 if (!c0_shift_h || (y & c0_shift_h))
     848           0 :                     c0_data += c0_linesize;
     849           0 :                 if (!c1_shift_h || (y & c1_shift_h))
     850           0 :                     c1_data += c1_linesize;
     851           0 :                 if (!c2_shift_h || (y & c2_shift_h))
     852           0 :                     c2_data += c2_linesize;
     853           0 :                 d0_data += d0_linesize;
     854           0 :                 d1_data += d1_linesize;
     855             :             }
     856             :         }
     857             :     } else {
     858           0 :         const uint16_t *c0_data = (uint16_t *)in->data[plane];
     859           0 :         const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
     860           0 :         const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
     861           0 :         uint16_t *d0_data = (uint16_t *)(out->data[plane]) + offset_y * d0_linesize + offset_x;
     862           0 :         uint16_t *d1_data = (uint16_t *)(out->data[(plane + 1) % s->ncomp]) + offset_y * d1_linesize + offset_x;
     863             : 
     864           0 :         if (mirror) {
     865           0 :             d0_data += s->size - 1;
     866           0 :             d1_data += s->size - 1;
     867             :         }
     868             : 
     869           0 :         for (y = 0; y < src_h; y++) {
     870           0 :             for (x = 0; x < src_w; x++) {
     871           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + s->max;
     872           0 :                 const int c1 = FFMIN(FFABS(c1_data[x >> c1_shift_w] - mid) + FFABS(c2_data[x >> c2_shift_w] - mid), limit);
     873             :                 uint16_t *target;
     874             : 
     875           0 :                 if (mirror) {
     876           0 :                     target = d0_data - c0;
     877           0 :                     update16(target, max, intensity, limit);
     878           0 :                     target = d1_data - (c0 - c1);
     879           0 :                     update16(target, max, intensity, limit);
     880           0 :                     target = d1_data - (c0 + c1);
     881           0 :                     update16(target, max, intensity, limit);
     882             :                 } else {
     883           0 :                     target = d0_data + c0;
     884           0 :                     update16(target, max, intensity, limit);
     885           0 :                     target = d1_data + (c0 - c1);
     886           0 :                     update16(target, max, intensity, limit);
     887           0 :                     target = d1_data + (c0 + c1);
     888           0 :                     update16(target, max, intensity, limit);
     889             :                 }
     890             :             }
     891             : 
     892           0 :             if (!c0_shift_h || (y & c0_shift_h))
     893           0 :                 c0_data += c0_linesize;
     894           0 :             if (!c1_shift_h || (y & c1_shift_h))
     895           0 :                 c1_data += c1_linesize;
     896           0 :             if (!c2_shift_h || (y & c2_shift_h))
     897           0 :                 c2_data += c2_linesize;
     898           0 :             d0_data += d0_linesize;
     899           0 :             d1_data += d1_linesize;
     900             :         }
     901             :     }
     902             : 
     903           0 :     envelope16(s, out, plane, plane, column ? offset_x : offset_y);
     904           0 :     envelope16(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y);
     905           0 : }
     906             : 
     907           0 : static av_always_inline void flat(WaveformContext *s,
     908             :                                   AVFrame *in, AVFrame *out,
     909             :                                   int component, int intensity,
     910             :                                   int offset_y, int offset_x,
     911             :                                   int column, int mirror)
     912             : {
     913           0 :     const int plane = s->desc->comp[component].plane;
     914           0 :     const int c0_linesize = in->linesize[ plane + 0 ];
     915           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
     916           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
     917           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
     918           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
     919           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
     920           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
     921           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
     922           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
     923           0 :     const int d0_linesize = out->linesize[ plane + 0 ];
     924           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
     925           0 :     const int max = 255 - intensity;
     926           0 :     const int src_h = in->height;
     927           0 :     const int src_w = in->width;
     928             :     int x, y;
     929             : 
     930           0 :     if (column) {
     931           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
     932           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
     933             : 
     934           0 :         for (x = 0; x < src_w; x++) {
     935           0 :             const uint8_t *c0_data = in->data[plane + 0];
     936           0 :             const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
     937           0 :             const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
     938           0 :             uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
     939           0 :             uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
     940           0 :             uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
     941           0 :             uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
     942           0 :             uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
     943           0 :             uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
     944             : 
     945           0 :             for (y = 0; y < src_h; y++) {
     946           0 :                 const int c0 = c0_data[x >> c0_shift_w] + 256;
     947           0 :                 const int c1 = FFABS(c1_data[x >> c1_shift_w] - 128) + FFABS(c2_data[x >> c2_shift_w] - 128);
     948             :                 uint8_t *target;
     949             : 
     950           0 :                 target = d0 + x + d0_signed_linesize * c0;
     951           0 :                 update(target, max, intensity);
     952           0 :                 target = d1 + x + d1_signed_linesize * (c0 - c1);
     953           0 :                 update(target, max, intensity);
     954           0 :                 target = d1 + x + d1_signed_linesize * (c0 + c1);
     955           0 :                 update(target, max, intensity);
     956             : 
     957           0 :                 if (!c0_shift_h || (y & c0_shift_h))
     958           0 :                     c0_data += c0_linesize;
     959           0 :                 if (!c1_shift_h || (y & c1_shift_h))
     960           0 :                     c1_data += c1_linesize;
     961           0 :                 if (!c2_shift_h || (y & c2_shift_h))
     962           0 :                     c2_data += c2_linesize;
     963           0 :                 d0_data += d0_linesize;
     964           0 :                 d1_data += d1_linesize;
     965             :             }
     966             :         }
     967             :     } else {
     968           0 :         const uint8_t *c0_data = in->data[plane];
     969           0 :         const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
     970           0 :         const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
     971           0 :         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
     972           0 :         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
     973             : 
     974           0 :         if (mirror) {
     975           0 :             d0_data += s->size - 1;
     976           0 :             d1_data += s->size - 1;
     977             :         }
     978             : 
     979           0 :         for (y = 0; y < src_h; y++) {
     980           0 :             for (x = 0; x < src_w; x++) {
     981           0 :                 int c0 = c0_data[x >> c0_shift_w] + 256;
     982           0 :                 const int c1 = FFABS(c1_data[x >> c1_shift_w] - 128) + FFABS(c2_data[x >> c2_shift_w] - 128);
     983             :                 uint8_t *target;
     984             : 
     985           0 :                 if (mirror) {
     986           0 :                     target = d0_data - c0;
     987           0 :                     update(target, max, intensity);
     988           0 :                     target = d1_data - (c0 - c1);
     989           0 :                     update(target, max, intensity);
     990           0 :                     target = d1_data - (c0 + c1);
     991           0 :                     update(target, max, intensity);
     992             :                 } else {
     993           0 :                     target = d0_data + c0;
     994           0 :                     update(target, max, intensity);
     995           0 :                     target = d1_data + (c0 - c1);
     996           0 :                     update(target, max, intensity);
     997           0 :                     target = d1_data + (c0 + c1);
     998           0 :                     update(target, max, intensity);
     999             :                 }
    1000             :             }
    1001             : 
    1002           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1003           0 :                 c0_data += c0_linesize;
    1004           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1005           0 :                 c1_data += c1_linesize;
    1006           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1007           0 :                 c2_data += c2_linesize;
    1008           0 :             d0_data += d0_linesize;
    1009           0 :             d1_data += d1_linesize;
    1010             :         }
    1011             :     }
    1012             : 
    1013           0 :     envelope(s, out, plane, plane, column ? offset_x : offset_y);
    1014           0 :     envelope(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y);
    1015           0 : }
    1016             : 
    1017           0 : static av_always_inline void aflat16(WaveformContext *s,
    1018             :                                      AVFrame *in, AVFrame *out,
    1019             :                                      int component, int intensity,
    1020             :                                      int offset_y, int offset_x,
    1021             :                                      int column, int mirror)
    1022             : {
    1023           0 :     const int plane = s->desc->comp[component].plane;
    1024           0 :     const int c0_linesize = in->linesize[ plane + 0 ] / 2;
    1025           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
    1026           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
    1027           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
    1028           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1029           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1030           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
    1031           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1032           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1033           0 :     const int d0_linesize = out->linesize[ plane + 0 ] / 2;
    1034           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
    1035           0 :     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2;
    1036           0 :     const int limit = s->max - 1;
    1037           0 :     const int max = limit - intensity;
    1038           0 :     const int mid = s->max / 2;
    1039           0 :     const int src_h = in->height;
    1040           0 :     const int src_w = in->width;
    1041             :     int x, y;
    1042             : 
    1043           0 :     if (column) {
    1044           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
    1045           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
    1046           0 :         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
    1047             : 
    1048           0 :         for (x = 0; x < src_w; x++) {
    1049           0 :             const uint16_t *c0_data = (uint16_t *)in->data[plane + 0];
    1050           0 :             const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
    1051           0 :             const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
    1052           0 :             uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
    1053           0 :             uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1054           0 :             uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1055           0 :             uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
    1056           0 :             uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
    1057           0 :             uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
    1058           0 :             uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
    1059           0 :             uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
    1060           0 :             uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data);
    1061             : 
    1062           0 :             for (y = 0; y < src_h; y++) {
    1063           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid;
    1064           0 :                 const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid;
    1065           0 :                 const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid;
    1066             :                 uint16_t *target;
    1067             : 
    1068           0 :                 target = d0 + x + d0_signed_linesize * c0;
    1069           0 :                 update16(target, max, intensity, limit);
    1070             : 
    1071           0 :                 target = d1 + x + d1_signed_linesize * (c0 + c1);
    1072           0 :                 update16(target, max, intensity, limit);
    1073             : 
    1074           0 :                 target = d2 + x + d2_signed_linesize * (c0 + c2);
    1075           0 :                 update16(target, max, intensity, limit);
    1076             : 
    1077           0 :                 if (!c0_shift_h || (y & c0_shift_h))
    1078           0 :                     c0_data += c0_linesize;
    1079           0 :                 if (!c1_shift_h || (y & c1_shift_h))
    1080           0 :                     c1_data += c1_linesize;
    1081           0 :                 if (!c2_shift_h || (y & c2_shift_h))
    1082           0 :                     c2_data += c2_linesize;
    1083           0 :                 d0_data += d0_linesize;
    1084           0 :                 d1_data += d1_linesize;
    1085           0 :                 d2_data += d2_linesize;
    1086             :             }
    1087             :         }
    1088             :     } else {
    1089           0 :         const uint16_t *c0_data = (uint16_t *)in->data[plane];
    1090           0 :         const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
    1091           0 :         const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
    1092           0 :         uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
    1093           0 :         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1094           0 :         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1095             : 
    1096           0 :         if (mirror) {
    1097           0 :             d0_data += s->size - 1;
    1098           0 :             d1_data += s->size - 1;
    1099           0 :             d2_data += s->size - 1;
    1100             :         }
    1101             : 
    1102           0 :         for (y = 0; y < src_h; y++) {
    1103           0 :             for (x = 0; x < src_w; x++) {
    1104           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid;
    1105           0 :                 const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid;
    1106           0 :                 const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid;
    1107             :                 uint16_t *target;
    1108             : 
    1109           0 :                 if (mirror) {
    1110           0 :                     target = d0_data - c0;
    1111           0 :                     update16(target, max, intensity, limit);
    1112           0 :                     target = d1_data - (c0 + c1);
    1113           0 :                     update16(target, max, intensity, limit);
    1114           0 :                     target = d2_data - (c0 + c2);
    1115           0 :                     update16(target, max, intensity, limit);
    1116             :                 } else {
    1117           0 :                     target = d0_data + c0;
    1118           0 :                     update16(target, max, intensity, limit);
    1119           0 :                     target = d1_data + (c0 + c1);
    1120           0 :                     update16(target, max, intensity, limit);
    1121           0 :                     target = d2_data + (c0 + c2);
    1122           0 :                     update16(target, max, intensity, limit);
    1123             :                 }
    1124             :             }
    1125             : 
    1126           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1127           0 :                 c0_data += c0_linesize;
    1128           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1129           0 :                 c1_data += c1_linesize;
    1130           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1131           0 :                 c2_data += c2_linesize;
    1132           0 :             d0_data += d0_linesize;
    1133           0 :             d1_data += d1_linesize;
    1134           0 :             d2_data += d2_linesize;
    1135             :         }
    1136             :     }
    1137             : 
    1138           0 :     envelope16(s, out, plane, (plane + 0) % s->ncomp, column ? offset_x : offset_y);
    1139           0 :     envelope16(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y);
    1140           0 :     envelope16(s, out, plane, (plane + 2) % s->ncomp, column ? offset_x : offset_y);
    1141           0 : }
    1142             : 
    1143           0 : static av_always_inline void aflat(WaveformContext *s,
    1144             :                                    AVFrame *in, AVFrame *out,
    1145             :                                    int component, int intensity,
    1146             :                                    int offset_y, int offset_x,
    1147             :                                    int column, int mirror)
    1148             : {
    1149           0 :     const int plane = s->desc->comp[component].plane;
    1150           0 :     const int c0_linesize = in->linesize[ plane + 0 ];
    1151           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
    1152           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
    1153           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
    1154           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1155           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1156           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
    1157           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1158           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1159           0 :     const int d0_linesize = out->linesize[ plane + 0 ];
    1160           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
    1161           0 :     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
    1162           0 :     const int max = 255 - intensity;
    1163           0 :     const int src_h = in->height;
    1164           0 :     const int src_w = in->width;
    1165             :     int x, y;
    1166             : 
    1167           0 :     if (column) {
    1168           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
    1169           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
    1170           0 :         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
    1171             : 
    1172           0 :         for (x = 0; x < src_w; x++) {
    1173           0 :             const uint8_t *c0_data = in->data[plane + 0];
    1174           0 :             const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
    1175           0 :             const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
    1176           0 :             uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
    1177           0 :             uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1178           0 :             uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1179           0 :             uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
    1180           0 :             uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
    1181           0 :             uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
    1182           0 :             uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
    1183           0 :             uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
    1184           0 :             uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
    1185             : 
    1186           0 :             for (y = 0; y < src_h; y++) {
    1187           0 :                 const int c0 = c0_data[x >> c0_shift_w] + 128;
    1188           0 :                 const int c1 = c1_data[x >> c1_shift_w] - 128;
    1189           0 :                 const int c2 = c2_data[x >> c2_shift_w] - 128;
    1190             :                 uint8_t *target;
    1191             : 
    1192           0 :                 target = d0 + x + d0_signed_linesize * c0;
    1193           0 :                 update(target, max, intensity);
    1194             : 
    1195           0 :                 target = d1 + x + d1_signed_linesize * (c0 + c1);
    1196           0 :                 update(target, max, intensity);
    1197             : 
    1198           0 :                 target = d2 + x + d2_signed_linesize * (c0 + c2);
    1199           0 :                 update(target, max, intensity);
    1200             : 
    1201           0 :                 if (!c0_shift_h || (y & c0_shift_h))
    1202           0 :                     c0_data += c0_linesize;
    1203           0 :                 if (!c1_shift_h || (y & c1_shift_h))
    1204           0 :                     c1_data += c1_linesize;
    1205           0 :                 if (!c1_shift_h || (y & c1_shift_h))
    1206           0 :                     c2_data += c1_linesize;
    1207           0 :                 d0_data += d0_linesize;
    1208           0 :                 d1_data += d1_linesize;
    1209           0 :                 d2_data += d2_linesize;
    1210             :             }
    1211             :         }
    1212             :     } else {
    1213           0 :         const uint8_t *c0_data = in->data[plane];
    1214           0 :         const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
    1215           0 :         const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
    1216           0 :         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
    1217           0 :         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1218           0 :         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1219             : 
    1220           0 :         if (mirror) {
    1221           0 :             d0_data += s->size - 1;
    1222           0 :             d1_data += s->size - 1;
    1223           0 :             d2_data += s->size - 1;
    1224             :         }
    1225             : 
    1226           0 :         for (y = 0; y < src_h; y++) {
    1227           0 :             for (x = 0; x < src_w; x++) {
    1228           0 :                 const int c0 = c0_data[x >> c0_shift_w] + 128;
    1229           0 :                 const int c1 = c1_data[x >> c1_shift_w] - 128;
    1230           0 :                 const int c2 = c2_data[x >> c2_shift_w] - 128;
    1231             :                 uint8_t *target;
    1232             : 
    1233           0 :                 if (mirror) {
    1234           0 :                     target = d0_data - c0;
    1235           0 :                     update(target, max, intensity);
    1236           0 :                     target = d1_data - (c0 + c1);
    1237           0 :                     update(target, max, intensity);
    1238           0 :                     target = d2_data - (c0 + c2);
    1239           0 :                     update(target, max, intensity);
    1240             :                 } else {
    1241           0 :                     target = d0_data + c0;
    1242           0 :                     update(target, max, intensity);
    1243           0 :                     target = d1_data + (c0 + c1);
    1244           0 :                     update(target, max, intensity);
    1245           0 :                     target = d2_data + (c0 + c2);
    1246           0 :                     update(target, max, intensity);
    1247             :                 }
    1248             :             }
    1249             : 
    1250           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1251           0 :                 c0_data += c0_linesize;
    1252           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1253           0 :                 c1_data += c1_linesize;
    1254           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1255           0 :                 c2_data += c2_linesize;
    1256           0 :             d0_data += d0_linesize;
    1257           0 :             d1_data += d1_linesize;
    1258           0 :             d2_data += d2_linesize;
    1259             :         }
    1260             :     }
    1261             : 
    1262           0 :     envelope(s, out, plane, (plane + 0) % s->ncomp, column ? offset_x : offset_y);
    1263           0 :     envelope(s, out, plane, (plane + 1) % s->ncomp, column ? offset_x : offset_y);
    1264           0 :     envelope(s, out, plane, (plane + 2) % s->ncomp, column ? offset_x : offset_y);
    1265           0 : }
    1266             : 
    1267           0 : static av_always_inline void chroma16(WaveformContext *s,
    1268             :                                       AVFrame *in, AVFrame *out,
    1269             :                                       int component, int intensity,
    1270             :                                       int offset_y, int offset_x,
    1271             :                                       int column, int mirror)
    1272             : {
    1273           0 :     const int plane = s->desc->comp[component].plane;
    1274           0 :     const int c0_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
    1275           0 :     const int c1_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
    1276           0 :     const int dst_linesize = out->linesize[plane] / 2;
    1277           0 :     const int limit = s->max - 1;
    1278           0 :     const int max = limit - intensity;
    1279           0 :     const int mid = s->max / 2;
    1280           0 :     const int c0_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1281           0 :     const int c1_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1282           0 :     const int c0_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1283           0 :     const int c1_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1284           0 :     const int src_h = in->height;
    1285           0 :     const int src_w = in->width;
    1286             :     int x, y;
    1287             : 
    1288           0 :     if (column) {
    1289           0 :         const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
    1290             : 
    1291           0 :         for (x = 0; x < src_w; x++) {
    1292           0 :             const uint16_t *c0_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
    1293           0 :             const uint16_t *c1_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
    1294           0 :             uint16_t *dst_data = (uint16_t *)out->data[plane] + offset_y * dst_linesize + offset_x;
    1295           0 :             uint16_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
    1296           0 :             uint16_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
    1297           0 :             uint16_t *dst = dst_line;
    1298             : 
    1299           0 :             for (y = 0; y < src_h; y++) {
    1300           0 :                 const int sum = FFMIN(FFABS(c0_data[x >> c0_shift_w] - mid) + FFABS(c1_data[x >> c1_shift_w] - mid - 1), limit);
    1301             :                 uint16_t *target;
    1302             : 
    1303           0 :                 target = dst + x + dst_signed_linesize * sum;
    1304           0 :                 update16(target, max, intensity, limit);
    1305             : 
    1306           0 :                 if (!c0_shift_h || (y & c0_shift_h))
    1307           0 :                     c0_data += c0_linesize;
    1308           0 :                 if (!c1_shift_h || (y & c1_shift_h))
    1309           0 :                     c1_data += c1_linesize;
    1310           0 :                 dst_data += dst_linesize;
    1311             :             }
    1312             :         }
    1313             :     } else {
    1314           0 :         const uint16_t *c0_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
    1315           0 :         const uint16_t *c1_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
    1316           0 :         uint16_t *dst_data = (uint16_t *)out->data[plane] + offset_y * dst_linesize + offset_x;
    1317             : 
    1318           0 :         if (mirror)
    1319           0 :             dst_data += s->size - 1;
    1320           0 :         for (y = 0; y < src_h; y++) {
    1321           0 :             for (x = 0; x < src_w; x++) {
    1322           0 :                 const int sum = FFMIN(FFABS(c0_data[x >> c0_shift_w] - mid) + FFABS(c1_data[x >> c1_shift_w] - mid - 1), limit);
    1323             :                 uint16_t *target;
    1324             : 
    1325           0 :                 if (mirror) {
    1326           0 :                     target = dst_data - sum;
    1327           0 :                     update16(target, max, intensity, limit);
    1328             :                 } else {
    1329           0 :                     target = dst_data + sum;
    1330           0 :                     update16(target, max, intensity, limit);
    1331             :                 }
    1332             :             }
    1333             : 
    1334           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1335           0 :                 c0_data += c0_linesize;
    1336           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1337           0 :                 c1_data += c1_linesize;
    1338           0 :             dst_data += dst_linesize;
    1339             :         }
    1340             :     }
    1341             : 
    1342           0 :     envelope16(s, out, plane, plane, column ? offset_x : offset_y);
    1343           0 : }
    1344             : 
    1345           0 : static av_always_inline void chroma(WaveformContext *s,
    1346             :                                     AVFrame *in, AVFrame *out,
    1347             :                                     int component, int intensity,
    1348             :                                     int offset_y, int offset_x,
    1349             :                                     int column, int mirror)
    1350             : {
    1351           0 :     const int plane = s->desc->comp[component].plane;
    1352           0 :     const int c0_linesize = in->linesize[(plane + 1) % s->ncomp];
    1353           0 :     const int c1_linesize = in->linesize[(plane + 2) % s->ncomp];
    1354           0 :     const int dst_linesize = out->linesize[plane];
    1355           0 :     const int max = 255 - intensity;
    1356           0 :     const int c0_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1357           0 :     const int c1_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1358           0 :     const int c0_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1359           0 :     const int c1_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1360           0 :     const int src_h = in->height;
    1361           0 :     const int src_w = in->width;
    1362             :     int x, y;
    1363             : 
    1364           0 :     if (column) {
    1365           0 :         const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
    1366             : 
    1367           0 :         for (x = 0; x < src_w; x++) {
    1368           0 :             const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp];
    1369           0 :             const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp];
    1370           0 :             uint8_t *dst_data = out->data[plane] + offset_y * dst_linesize + offset_x;
    1371           0 :             uint8_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
    1372           0 :             uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
    1373           0 :             uint8_t *dst = dst_line;
    1374             : 
    1375           0 :             for (y = 0; y < src_h; y++) {
    1376           0 :                 const int sum = FFABS(c0_data[x >> c0_shift_w] - 128) + FFABS(c1_data[x >> c1_shift_w] - 127);
    1377             :                 uint8_t *target;
    1378             : 
    1379           0 :                 target = dst + x + dst_signed_linesize * sum;
    1380           0 :                 update(target, max, intensity);
    1381             : 
    1382           0 :                 if (!c0_shift_h || (y & c0_shift_h))
    1383           0 :                     c0_data += c0_linesize;
    1384           0 :                 if (!c1_shift_h || (y & c1_shift_h))
    1385           0 :                     c1_data += c1_linesize;
    1386           0 :                 dst_data += dst_linesize;
    1387             :             }
    1388             :         }
    1389             :     } else {
    1390           0 :         const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp];
    1391           0 :         const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp];
    1392           0 :         uint8_t *dst_data = out->data[plane] + offset_y * dst_linesize + offset_x;
    1393             : 
    1394           0 :         if (mirror)
    1395           0 :             dst_data += s->size - 1;
    1396           0 :         for (y = 0; y < src_h; y++) {
    1397           0 :             for (x = 0; x < src_w; x++) {
    1398           0 :                 const int sum = FFABS(c0_data[x >> c0_shift_w] - 128) + FFABS(c1_data[x >> c1_shift_w] - 127);
    1399             :                 uint8_t *target;
    1400             : 
    1401           0 :                 if (mirror) {
    1402           0 :                     target = dst_data - sum;
    1403           0 :                     update(target, max, intensity);
    1404             :                 } else {
    1405           0 :                     target = dst_data + sum;
    1406           0 :                     update(target, max, intensity);
    1407             :                 }
    1408             :             }
    1409             : 
    1410           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1411           0 :                 c0_data += c0_linesize;
    1412           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1413           0 :                 c1_data += c1_linesize;
    1414           0 :             dst_data += dst_linesize;
    1415             :         }
    1416             :     }
    1417             : 
    1418           0 :     envelope(s, out, plane, plane, column ? offset_x : offset_y);
    1419           0 : }
    1420             : 
    1421           0 : static av_always_inline void color16(WaveformContext *s,
    1422             :                                      AVFrame *in, AVFrame *out,
    1423             :                                      int component, int intensity,
    1424             :                                      int offset_y, int offset_x,
    1425             :                                      int column, int mirror)
    1426             : {
    1427           0 :     const int plane = s->desc->comp[component].plane;
    1428           0 :     const int limit = s->max - 1;
    1429           0 :     const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0];
    1430           0 :     const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp];
    1431           0 :     const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp];
    1432           0 :     const int c0_linesize = in->linesize[ plane + 0 ] / 2;
    1433           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
    1434           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
    1435           0 :     const int d0_linesize = out->linesize[ plane + 0 ] / 2;
    1436           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
    1437           0 :     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2;
    1438           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
    1439           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1440           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1441           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
    1442           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1443           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1444           0 :     const int src_h = in->height;
    1445           0 :     const int src_w = in->width;
    1446             :     int x, y;
    1447             : 
    1448           0 :     if (column) {
    1449           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
    1450           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
    1451           0 :         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
    1452           0 :         uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
    1453           0 :         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1454           0 :         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1455           0 :         uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
    1456           0 :         uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
    1457           0 :         uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
    1458           0 :         uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
    1459           0 :         uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
    1460           0 :         uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data);
    1461             : 
    1462           0 :         for (y = 0; y < src_h; y++) {
    1463           0 :             for (x = 0; x < src_w; x++) {
    1464           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
    1465           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1466           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1467             : 
    1468           0 :                 *(d0 + d0_signed_linesize * c0 + x) = c0;
    1469           0 :                 *(d1 + d1_signed_linesize * c0 + x) = c1;
    1470           0 :                 *(d2 + d2_signed_linesize * c0 + x) = c2;
    1471             :             }
    1472             : 
    1473           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1474           0 :                 c0_data += c0_linesize;
    1475           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1476           0 :                 c1_data += c1_linesize;
    1477           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1478           0 :                 c2_data += c2_linesize;
    1479           0 :             d0_data += d0_linesize;
    1480           0 :             d1_data += d1_linesize;
    1481           0 :             d2_data += d2_linesize;
    1482             :         }
    1483             :     } else {
    1484           0 :         uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
    1485           0 :         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1486           0 :         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1487             : 
    1488           0 :         if (mirror) {
    1489           0 :             d0_data += s->size - 1;
    1490           0 :             d1_data += s->size - 1;
    1491           0 :             d2_data += s->size - 1;
    1492             :         }
    1493             : 
    1494           0 :         for (y = 0; y < src_h; y++) {
    1495           0 :             for (x = 0; x < src_w; x++) {
    1496           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
    1497           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1498           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1499             : 
    1500           0 :                 if (mirror) {
    1501           0 :                     *(d0_data - c0) = c0;
    1502           0 :                     *(d1_data - c0) = c1;
    1503           0 :                     *(d2_data - c0) = c2;
    1504             :                 } else {
    1505           0 :                     *(d0_data + c0) = c0;
    1506           0 :                     *(d1_data + c0) = c1;
    1507           0 :                     *(d2_data + c0) = c2;
    1508             :                 }
    1509             :             }
    1510             : 
    1511           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1512           0 :                 c0_data += c0_linesize;
    1513           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1514           0 :                 c1_data += c1_linesize;
    1515           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1516           0 :                 c2_data += c2_linesize;
    1517           0 :             d0_data += d0_linesize;
    1518           0 :             d1_data += d1_linesize;
    1519           0 :             d2_data += d2_linesize;
    1520             :         }
    1521             :     }
    1522             : 
    1523           0 :     envelope16(s, out, plane, plane, column ? offset_x : offset_y);
    1524           0 : }
    1525             : 
    1526           0 : static av_always_inline void color(WaveformContext *s,
    1527             :                                    AVFrame *in, AVFrame *out,
    1528             :                                    int component, int intensity,
    1529             :                                    int offset_y, int offset_x,
    1530             :                                    int column, int mirror)
    1531             : {
    1532           0 :     const int plane = s->desc->comp[component].plane;
    1533           0 :     const uint8_t *c0_data = in->data[plane + 0];
    1534           0 :     const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
    1535           0 :     const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
    1536           0 :     const int c0_linesize = in->linesize[ plane + 0 ];
    1537           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
    1538           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
    1539           0 :     const int d0_linesize = out->linesize[ plane + 0 ];
    1540           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
    1541           0 :     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
    1542           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
    1543           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1544           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1545           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
    1546           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1547           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1548           0 :     const int src_h = in->height;
    1549           0 :     const int src_w = in->width;
    1550             :     int x, y;
    1551             : 
    1552           0 :     if (s->mode) {
    1553           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
    1554           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
    1555           0 :         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
    1556           0 :         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
    1557           0 :         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1558           0 :         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1559           0 :         uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
    1560           0 :         uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
    1561           0 :         uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
    1562           0 :         uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
    1563           0 :         uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
    1564           0 :         uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
    1565             : 
    1566           0 :         for (y = 0; y < src_h; y++) {
    1567           0 :             for (x = 0; x < src_w; x++) {
    1568           0 :                 const int c0 = c0_data[x >> c0_shift_w];
    1569           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1570           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1571             : 
    1572           0 :                 *(d0 + d0_signed_linesize * c0 + x) = c0;
    1573           0 :                 *(d1 + d1_signed_linesize * c0 + x) = c1;
    1574           0 :                 *(d2 + d2_signed_linesize * c0 + x) = c2;
    1575             :             }
    1576             : 
    1577           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1578           0 :                 c0_data += c0_linesize;
    1579           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1580           0 :                 c1_data += c1_linesize;
    1581           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1582           0 :                 c2_data += c2_linesize;
    1583           0 :             d0_data += d0_linesize;
    1584           0 :             d1_data += d1_linesize;
    1585           0 :             d2_data += d2_linesize;
    1586             :         }
    1587             :     } else {
    1588           0 :         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
    1589           0 :         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1590           0 :         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1591             : 
    1592           0 :         if (mirror) {
    1593           0 :             d0_data += s->size - 1;
    1594           0 :             d1_data += s->size - 1;
    1595           0 :             d2_data += s->size - 1;
    1596             :         }
    1597             : 
    1598           0 :         for (y = 0; y < src_h; y++) {
    1599           0 :             for (x = 0; x < src_w; x++) {
    1600           0 :                 const int c0 = c0_data[x >> c0_shift_w];
    1601           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1602           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1603             : 
    1604           0 :                 if (mirror) {
    1605           0 :                     *(d0_data - c0) = c0;
    1606           0 :                     *(d1_data - c0) = c1;
    1607           0 :                     *(d2_data - c0) = c2;
    1608             :                 } else {
    1609           0 :                     *(d0_data + c0) = c0;
    1610           0 :                     *(d1_data + c0) = c1;
    1611           0 :                     *(d2_data + c0) = c2;
    1612             :                 }
    1613             :             }
    1614             : 
    1615           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1616           0 :                 c0_data += c0_linesize;
    1617           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1618           0 :                 c1_data += c1_linesize;
    1619           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1620           0 :                 c2_data += c2_linesize;
    1621           0 :             d0_data += d0_linesize;
    1622           0 :             d1_data += d1_linesize;
    1623           0 :             d2_data += d2_linesize;
    1624             :         }
    1625             :     }
    1626             : 
    1627           0 :     envelope(s, out, plane, plane, column ? offset_x : offset_y);
    1628           0 : }
    1629             : 
    1630           0 : static av_always_inline void acolor16(WaveformContext *s,
    1631             :                                       AVFrame *in, AVFrame *out,
    1632             :                                       int component, int intensity,
    1633             :                                       int offset_y, int offset_x,
    1634             :                                       int column, int mirror)
    1635             : {
    1636           0 :     const int plane = s->desc->comp[component].plane;
    1637           0 :     const int limit = s->max - 1;
    1638           0 :     const int max = limit - intensity;
    1639           0 :     const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0];
    1640           0 :     const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp];
    1641           0 :     const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp];
    1642           0 :     const int c0_linesize = in->linesize[ plane + 0 ] / 2;
    1643           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
    1644           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
    1645           0 :     const int d0_linesize = out->linesize[ plane + 0 ] / 2;
    1646           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
    1647           0 :     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2;
    1648           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
    1649           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1650           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1651           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
    1652           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1653           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1654           0 :     const int src_h = in->height;
    1655           0 :     const int src_w = in->width;
    1656             :     int x, y;
    1657             : 
    1658           0 :     if (s->mode) {
    1659           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
    1660           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
    1661           0 :         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
    1662           0 :         uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
    1663           0 :         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1664           0 :         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1665           0 :         uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
    1666           0 :         uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
    1667           0 :         uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
    1668           0 :         uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
    1669           0 :         uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
    1670           0 :         uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data);
    1671             : 
    1672           0 :         for (y = 0; y < src_h; y++) {
    1673           0 :             for (x = 0; x < src_w; x++) {
    1674           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
    1675           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1676           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1677             : 
    1678           0 :                 update16(d0 + d0_signed_linesize * c0 + x, max, intensity, limit);
    1679           0 :                 *(d1 + d1_signed_linesize * c0 + x) = c1;
    1680           0 :                 *(d2 + d2_signed_linesize * c0 + x) = c2;
    1681             :             }
    1682             : 
    1683           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1684           0 :                 c0_data += c0_linesize;
    1685           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1686           0 :                 c1_data += c1_linesize;
    1687           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1688           0 :                 c2_data += c2_linesize;
    1689           0 :             d0_data += d0_linesize;
    1690           0 :             d1_data += d1_linesize;
    1691           0 :             d2_data += d2_linesize;
    1692             :         }
    1693             :     } else {
    1694           0 :         uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
    1695           0 :         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1696           0 :         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1697             : 
    1698           0 :         if (mirror) {
    1699           0 :             d0_data += s->size - 1;
    1700           0 :             d1_data += s->size - 1;
    1701           0 :             d2_data += s->size - 1;
    1702             :         }
    1703             : 
    1704           0 :         for (y = 0; y < src_h; y++) {
    1705           0 :             for (x = 0; x < src_w; x++) {
    1706           0 :                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
    1707           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1708           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1709             : 
    1710           0 :                 if (mirror) {
    1711           0 :                     update16(d0_data - c0, max, intensity, limit);
    1712           0 :                     *(d1_data - c0) = c1;
    1713           0 :                     *(d2_data - c0) = c2;
    1714             :                 } else {
    1715           0 :                     update16(d0_data + c0, max, intensity, limit);
    1716           0 :                     *(d1_data + c0) = c1;
    1717           0 :                     *(d2_data + c0) = c2;
    1718             :                 }
    1719             :             }
    1720             : 
    1721           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1722           0 :                 c0_data += c0_linesize;
    1723           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1724           0 :                 c1_data += c1_linesize;
    1725           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1726           0 :                 c2_data += c2_linesize;
    1727           0 :             d0_data += d0_linesize;
    1728           0 :             d1_data += d1_linesize;
    1729           0 :             d2_data += d2_linesize;
    1730             :         }
    1731             :     }
    1732             : 
    1733           0 :     envelope16(s, out, plane, plane, column ? offset_x : offset_y);
    1734           0 : }
    1735             : 
    1736           0 : static av_always_inline void acolor(WaveformContext *s,
    1737             :                                     AVFrame *in, AVFrame *out,
    1738             :                                     int component, int intensity,
    1739             :                                     int offset_y, int offset_x,
    1740             :                                     int column, int mirror)
    1741             : {
    1742           0 :     const int plane = s->desc->comp[component].plane;
    1743           0 :     const uint8_t *c0_data = in->data[plane + 0];
    1744           0 :     const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
    1745           0 :     const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
    1746           0 :     const int c0_linesize = in->linesize[ plane + 0 ];
    1747           0 :     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
    1748           0 :     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
    1749           0 :     const int d0_linesize = out->linesize[ plane + 0 ];
    1750           0 :     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
    1751           0 :     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
    1752           0 :     const int c0_shift_w = s->shift_w[ component + 0 ];
    1753           0 :     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
    1754           0 :     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
    1755           0 :     const int c0_shift_h = s->shift_h[ component + 0 ];
    1756           0 :     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
    1757           0 :     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
    1758           0 :     const int max = 255 - intensity;
    1759           0 :     const int src_h = in->height;
    1760           0 :     const int src_w = in->width;
    1761             :     int x, y;
    1762             : 
    1763           0 :     if (s->mode) {
    1764           0 :         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
    1765           0 :         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
    1766           0 :         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
    1767           0 :         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
    1768           0 :         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1769           0 :         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1770           0 :         uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
    1771           0 :         uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
    1772           0 :         uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
    1773           0 :         uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
    1774           0 :         uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
    1775           0 :         uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
    1776             : 
    1777           0 :         for (y = 0; y < src_h; y++) {
    1778           0 :             for (x = 0; x < src_w; x++) {
    1779           0 :                 const int c0 = c0_data[x >> c0_shift_w];
    1780           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1781           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1782             : 
    1783           0 :                 update(d0 + d0_signed_linesize * c0 + x, max, intensity);
    1784           0 :                 *(d1 + d1_signed_linesize * c0 + x) = c1;
    1785           0 :                 *(d2 + d2_signed_linesize * c0 + x) = c2;
    1786             :             }
    1787             : 
    1788           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1789           0 :                 c0_data += c0_linesize;
    1790           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1791           0 :                 c1_data += c1_linesize;
    1792           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1793           0 :                 c2_data += c2_linesize;
    1794           0 :             d0_data += d0_linesize;
    1795           0 :             d1_data += d1_linesize;
    1796           0 :             d2_data += d2_linesize;
    1797             :         }
    1798             :     } else {
    1799           0 :         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
    1800           0 :         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
    1801           0 :         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
    1802             : 
    1803           0 :         if (mirror) {
    1804           0 :             d0_data += s->size - 1;
    1805           0 :             d1_data += s->size - 1;
    1806           0 :             d2_data += s->size - 1;
    1807             :         }
    1808             : 
    1809           0 :         for (y = 0; y < src_h; y++) {
    1810           0 :             for (x = 0; x < src_w; x++) {
    1811           0 :                 const int c0 = c0_data[x >> c0_shift_w];
    1812           0 :                 const int c1 = c1_data[x >> c1_shift_w];
    1813           0 :                 const int c2 = c2_data[x >> c2_shift_w];
    1814             : 
    1815           0 :                 if (mirror) {
    1816           0 :                     update(d0_data - c0, max, intensity);
    1817           0 :                     *(d1_data - c0) = c1;
    1818           0 :                     *(d2_data - c0) = c2;
    1819             :                 } else {
    1820           0 :                     update(d0_data + c0, max, intensity);
    1821           0 :                     *(d1_data + c0) = c1;
    1822           0 :                     *(d2_data + c0) = c2;
    1823             :                 }
    1824             :             }
    1825             : 
    1826           0 :             if (!c0_shift_h || (y & c0_shift_h))
    1827           0 :                 c0_data += c0_linesize;
    1828           0 :             if (!c1_shift_h || (y & c1_shift_h))
    1829           0 :                 c1_data += c1_linesize;
    1830           0 :             if (!c2_shift_h || (y & c2_shift_h))
    1831           0 :                 c2_data += c2_linesize;
    1832           0 :             d0_data += d0_linesize;
    1833           0 :             d1_data += d1_linesize;
    1834           0 :             d2_data += d2_linesize;
    1835             :         }
    1836             :     }
    1837             : 
    1838           0 :     envelope(s, out, plane, plane, column ? offset_x : offset_y);
    1839           0 : }
    1840             : 
    1841             : static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
    1842             : static const uint8_t green_yuva_color[4] = { 255, 0, 0, 255 };
    1843             : static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
    1844             : 
    1845             : static const GraticuleLines aflat_digital8[] = {
    1846             :     { { {  "16",  16+128 }, {  "16",  16+128 }, {  "16",  16+128 }, {   "0",   0+128 } } },
    1847             :     { { { "128", 128+128 }, { "128", 128+128 }, { "128", 128+128 }, { "128", 128+128 } } },
    1848             :     { { { "235", 235+128 }, { "240", 240+128 }, { "240", 240+128 }, { "255", 255+128 } } },
    1849             : };
    1850             : 
    1851             : static const GraticuleLines aflat_digital9[] = {
    1852             :     { { {  "32",  32+256 }, {  "32",  32+256 }, {  "32",  32+256 }, {   "0",   0+256 } } },
    1853             :     { { { "256", 256+256 }, { "256", 256+256 }, { "256", 256+256 }, { "256", 256+256 } } },
    1854             :     { { { "470", 470+256 }, { "480", 480+256 }, { "480", 480+256 }, { "511", 511+256 } } },
    1855             : };
    1856             : 
    1857             : static const GraticuleLines aflat_digital10[] = {
    1858             :     { { {  "64",  64+512 }, {  "64",  64+512 }, {  "64",  64+512 }, {    "0",    0+512 } } },
    1859             :     { { { "512", 512+512 }, { "512", 512+512 }, { "512", 512+512 }, {  "512",  512+512 } } },
    1860             :     { { { "940", 940+512 }, { "960", 960+512 }, { "960", 960+512 }, { "1023", 1023+512 } } },
    1861             : };
    1862             : 
    1863             : static const GraticuleLines aflat_digital12[] = {
    1864             :     { { {  "256",  256+2048 }, {  "256",  256+2048 }, {  "256",  256+2048 }, {    "0",    0+2048 } } },
    1865             :     { { { "2048", 2048+2048 }, { "2048", 2048+2048 }, { "2048", 2048+2048 }, { "2048", 2048+2048 } } },
    1866             :     { { { "3760", 3760+2048 }, { "3840", 3840+2048 }, { "3840", 3840+2048 }, { "4095", 4095+2048 } } },
    1867             : };
    1868             : 
    1869             : static const GraticuleLines aflat_millivolts8[] = {
    1870             :     { { {   "0",  16+128 }, {   "0",  16+128 }, {   "0",  16+128 }, {   "0",   0+128 } } },
    1871             :     { { { "175",  71+128 }, { "175",  72+128 }, { "175",  72+128 }, { "175",  64+128 } } },
    1872             :     { { { "350", 126+128 }, { "350", 128+128 }, { "350", 128+128 }, { "350", 128+128 } } },
    1873             :     { { { "525", 180+128 }, { "525", 184+128 }, { "525", 184+128 }, { "525", 192+128 } } },
    1874             :     { { { "700", 235+128 }, { "700", 240+128 }, { "700", 240+128 }, { "700", 255+128 } } },
    1875             : };
    1876             : 
    1877             : static const GraticuleLines aflat_millivolts9[] = {
    1878             :     { { {   "0",  32+256 }, {   "0",  32+256 }, {   "0",  32+256 }, {   "0",   0+256 } } },
    1879             :     { { { "175", 142+256 }, { "175", 144+256 }, { "175", 144+256 }, { "175", 128+256 } } },
    1880             :     { { { "350", 251+256 }, { "350", 256+256 }, { "350", 256+256 }, { "350", 256+256 } } },
    1881             :     { { { "525", 361+256 }, { "525", 368+256 }, { "525", 368+256 }, { "525", 384+256 } } },
    1882             :     { { { "700", 470+256 }, { "700", 480+256 }, { "700", 480+256 }, { "700", 511+256 } } },
    1883             : };
    1884             : 
    1885             : static const GraticuleLines aflat_millivolts10[] = {
    1886             :     { { {   "0",  64+512 }, {   "0",  64+512 }, {   "0",  64+512 }, {   "0",    0+512 } } },
    1887             :     { { { "175", 283+512 }, { "175", 288+512 }, { "175", 288+512 }, { "175",  256+512 } } },
    1888             :     { { { "350", 502+512 }, { "350", 512+512 }, { "350", 512+512 }, { "350",  512+512 } } },
    1889             :     { { { "525", 721+512 }, { "525", 736+512 }, { "525", 736+512 }, { "525",  768+512 } } },
    1890             :     { { { "700", 940+512 }, { "700", 960+512 }, { "700", 960+512 }, { "700", 1023+512 } } },
    1891             : };
    1892             : 
    1893             : static const GraticuleLines aflat_millivolts12[] = {
    1894             :     { { {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",    0+2048 } } },
    1895             :     { { { "175", 1132+2048 }, { "175", 1152+2048 }, { "175", 1152+2048 }, { "175", 1024+2048 } } },
    1896             :     { { { "350", 2008+2048 }, { "350", 2048+2048 }, { "350", 2048+2048 }, { "350", 2048+2048 } } },
    1897             :     { { { "525", 2884+2048 }, { "525", 2944+2048 }, { "525", 2944+2048 }, { "525", 3072+2048 } } },
    1898             :     { { { "700", 3760+2048 }, { "700", 3840+2048 }, { "700", 3840+2048 }, { "700", 4095+2048 } } },
    1899             : };
    1900             : 
    1901             : static const GraticuleLines aflat_ire8[] = {
    1902             :     { { { "-25", -39+128 }, { "-25", -40+128 }, { "-25", -40+128 }, { "-25", -64+128 } } },
    1903             :     { { {   "0",  16+128 }, {   "0",  16+128 }, {   "0",  16+128 }, {   "0",   0+128 } } },
    1904             :     { { {  "25",  71+128 }, {  "25",  72+128 }, {  "25",  72+128 }, {  "25",  64+128 } } },
    1905             :     { { {  "50", 126+128 }, {  "50", 128+128 }, {  "50", 128+128 }, {  "50", 128+128 } } },
    1906             :     { { {  "75", 180+128 }, {  "75", 184+128 }, {  "75", 184+128 }, {  "75", 192+128 } } },
    1907             :     { { { "100", 235+128 }, { "100", 240+128 }, { "100", 240+128 }, { "100", 256+128 } } },
    1908             :     { { { "125", 290+128 }, { "125", 296+128 }, { "125", 296+128 }, { "125", 320+128 } } },
    1909             : };
    1910             : 
    1911             : static const GraticuleLines aflat_ire9[] = {
    1912             :     { { { "-25", -78+256 }, { "-25", -80+256 }, { "-25", -80+256 }, { "-25",-128+256 } } },
    1913             :     { { {   "0",  32+256 }, {   "0",  32+256 }, {   "0",  32+256 }, {   "0",   0+256 } } },
    1914             :     { { {  "25", 142+256 }, {  "25", 144+256 }, {  "25", 144+256 }, {  "25", 128+256 } } },
    1915             :     { { {  "50", 251+256 }, {  "50", 256+256 }, {  "50", 256+256 }, {  "50", 256+256 } } },
    1916             :     { { {  "75", 361+256 }, {  "75", 368+256 }, {  "75", 368+256 }, {  "75", 384+256 } } },
    1917             :     { { { "100", 470+256 }, { "100", 480+256 }, { "100", 480+256 }, { "100", 512+256 } } },
    1918             :     { { { "125", 580+256 }, { "125", 592+256 }, { "125", 592+256 }, { "125", 640+256 } } },
    1919             : };
    1920             : 
    1921             : static const GraticuleLines aflat_ire10[] = {
    1922             :     { { { "-25",-156+512 }, { "-25",-160+512 }, { "-25",-160+512 }, { "-25", -256+512 } } },
    1923             :     { { {   "0",  64+512 }, {   "0",  64+512 }, {  "0",   64+512 }, {   "0",    0+512 } } },
    1924             :     { { {  "25", 283+512 }, {  "25", 288+512 }, {  "25", 288+512 }, {  "25",  256+512 } } },
    1925             :     { { {  "50", 502+512 }, {  "50", 512+512 }, {  "50", 512+512 }, {  "50",  512+512 } } },
    1926             :     { { {  "75", 721+512 }, {  "75", 736+512 }, {  "75", 736+512 }, {  "75",  768+512 } } },
    1927             :     { { { "100", 940+512 }, { "100", 960+512 }, { "100", 960+512 }, { "100", 1024+512 } } },
    1928             :     { { { "125",1160+512 }, { "125",1184+512 }, { "125",1184+512 }, { "125", 1280+512 } } },
    1929             : };
    1930             : 
    1931             : static const GraticuleLines aflat_ire12[] = {
    1932             :     { { { "-25", -624+2048 }, { "-25", -640+2048 }, { "-25", -640+2048 }, { "-25",-1024+2048 } } },
    1933             :     { { {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",    0+2048 } } },
    1934             :     { { {  "25", 1132+2048 }, {  "25", 1152+2048 }, {  "25", 1152+2048 }, {  "25", 1024+2048 } } },
    1935             :     { { {  "50", 2008+2048 }, {  "50", 2048+2048 }, {  "50", 2048+2048 }, {  "50", 2048+2048 } } },
    1936             :     { { {  "75", 2884+2048 }, {  "75", 2944+2048 }, {  "75", 2944+2048 }, {  "75", 3072+2048 } } },
    1937             :     { { { "100", 3760+2048 }, { "100", 3840+2048 }, { "100", 3840+2048 }, { "100", 4096+2048 } } },
    1938             :     { { { "125", 4640+2048 }, { "125", 4736+2048 }, { "125", 4736+2048 }, { "125", 5120+2048 } } },
    1939             : };
    1940             : 
    1941             : static const GraticuleLines flat_digital8[] = {
    1942             :     { { {  "16",  16+256 }, {  "16",  16+256 }, {  "16",  16+256 }, {   "0",   0+256 } } },
    1943             :     { { { "128", 128+256 }, { "128", 128+256 }, { "128", 128+256 }, { "128", 128+256 } } },
    1944             :     { { { "235", 235+256 }, { "240", 240+256 }, { "240", 240+256 }, { "255", 255+256 } } },
    1945             : };
    1946             : 
    1947             : static const GraticuleLines flat_digital9[] = {
    1948             :     { { {  "32",  32+512 }, {  "32",  32+512 }, {  "32",  32+512 }, {   "0",   0+512 } } },
    1949             :     { { { "256", 256+512 }, { "256", 256+512 }, { "256", 256+512 }, { "256", 256+512 } } },
    1950             :     { { { "470", 470+512 }, { "480", 480+512 }, { "480", 480+512 }, { "511", 511+512 } } },
    1951             : };
    1952             : 
    1953             : static const GraticuleLines flat_digital10[] = {
    1954             :     { { {  "64",  64+1024 }, {  "64",  64+1024 }, {  "64",  64+1024 }, {    "0",    0+1024 } } },
    1955             :     { { { "512", 512+1024 }, { "512", 512+1024 }, { "512", 512+1024 }, {  "512",  512+1024 } } },
    1956             :     { { { "940", 940+1024 }, { "960", 960+1024 }, { "960", 960+1024 }, { "1023", 1023+1024 } } },
    1957             : };
    1958             : 
    1959             : static const GraticuleLines flat_digital12[] = {
    1960             :     { { {  "256",  256+4096 }, {  "256",  256+4096 }, {  "256",  256+4096 }, {    "0",    0+4096 } } },
    1961             :     { { { "2048", 2048+4096 }, { "2048", 2048+4096 }, { "2048", 2048+4096 }, { "2048", 2048+4096 } } },
    1962             :     { { { "3760", 3760+4096 }, { "3840", 3840+4096 }, { "3840", 3840+4096 }, { "4095", 4095+4096 } } },
    1963             : };
    1964             : 
    1965             : static const GraticuleLines flat_millivolts8[] = {
    1966             :     { { {   "0",  16+256 }, {   "0",  16+256 }, {   "0",  16+256 }, {   "0",   0+256 } } },
    1967             :     { { { "175",  71+256 }, { "175",  72+256 }, { "175",  72+256 }, { "175",  64+256 } } },
    1968             :     { { { "350", 126+256 }, { "350", 128+256 }, { "350", 128+256 }, { "350", 128+256 } } },
    1969             :     { { { "525", 180+256 }, { "525", 184+256 }, { "525", 184+256 }, { "525", 192+256 } } },
    1970             :     { { { "700", 235+256 }, { "700", 240+256 }, { "700", 240+256 }, { "700", 255+256 } } },
    1971             : };
    1972             : 
    1973             : static const GraticuleLines flat_millivolts9[] = {
    1974             :     { { {   "0",  32+512 }, {   "0",  32+512 }, {   "0",  32+512 }, {   "0",   0+512 } } },
    1975             :     { { { "175", 142+512 }, { "175", 144+512 }, { "175", 144+512 }, { "175", 128+512 } } },
    1976             :     { { { "350", 251+512 }, { "350", 256+512 }, { "350", 256+512 }, { "350", 256+512 } } },
    1977             :     { { { "525", 361+512 }, { "525", 368+512 }, { "525", 368+512 }, { "525", 384+512 } } },
    1978             :     { { { "700", 470+512 }, { "700", 480+512 }, { "700", 480+512 }, { "700", 511+512 } } },
    1979             : };
    1980             : 
    1981             : static const GraticuleLines flat_millivolts10[] = {
    1982             :     { { {   "0",  64+1024 }, {   "0",  64+1024 }, {   "0",  64+1024 }, {   "0",    0+1024 } } },
    1983             :     { { { "175", 283+1024 }, { "175", 288+1024 }, { "175", 288+1024 }, { "175",  256+1024 } } },
    1984             :     { { { "350", 502+1024 }, { "350", 512+1024 }, { "350", 512+1024 }, { "350",  512+1024 } } },
    1985             :     { { { "525", 721+1024 }, { "525", 736+1024 }, { "525", 736+1024 }, { "525",  768+1024 } } },
    1986             :     { { { "700", 940+1024 }, { "700", 960+1024 }, { "700", 960+1024 }, { "700", 1023+1024 } } },
    1987             : };
    1988             : 
    1989             : static const GraticuleLines flat_millivolts12[] = {
    1990             :     { { {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",    0+4096 } } },
    1991             :     { { { "175", 1132+4096 }, { "175", 1152+4096 }, { "175", 1152+4096 }, { "175", 1024+4096 } } },
    1992             :     { { { "350", 2008+4096 }, { "350", 2048+4096 }, { "350", 2048+4096 }, { "350", 2048+4096 } } },
    1993             :     { { { "525", 2884+4096 }, { "525", 2944+4096 }, { "525", 2944+4096 }, { "525", 3072+4096 } } },
    1994             :     { { { "700", 3760+4096 }, { "700", 3840+4096 }, { "700", 3840+4096 }, { "700", 4095+4096 } } },
    1995             : };
    1996             : 
    1997             : static const GraticuleLines flat_ire8[] = {
    1998             :     { { { "-25", -39+256 }, { "-25", -40+256 }, { "-25", -40+256 }, { "-25", -64+256 } } },
    1999             :     { { {   "0",  16+256 }, {   "0",  16+256 }, {   "0",  16+256 }, {   "0",   0+256 } } },
    2000             :     { { {  "25",  71+256 }, {  "25",  72+256 }, {  "25",  72+256 }, {  "25",  64+256 } } },
    2001             :     { { {  "50", 126+256 }, {  "50", 128+256 }, {  "50", 128+256 }, {  "50", 128+256 } } },
    2002             :     { { {  "75", 180+256 }, {  "75", 184+256 }, {  "75", 184+256 }, {  "75", 192+256 } } },
    2003             :     { { { "100", 235+256 }, { "100", 240+256 }, { "100", 240+256 }, { "100", 256+256 } } },
    2004             :     { { { "125", 290+256 }, { "125", 296+256 }, { "125", 296+256 }, { "125", 320+256 } } },
    2005             : };
    2006             : 
    2007             : static const GraticuleLines flat_ire9[] = {
    2008             :     { { { "-25", -78+512 }, { "-25", -80+512 }, { "-25", -80+512 }, { "-25",-128+512 } } },
    2009             :     { { {   "0",  32+512 }, {   "0",  32+512 }, {   "0",  32+512 }, {   "0",   0+512 } } },
    2010             :     { { {  "25", 142+512 }, {  "25", 144+512 }, {  "25", 144+512 }, {  "25", 128+512 } } },
    2011             :     { { {  "50", 251+512 }, {  "50", 256+512 }, {  "50", 256+512 }, {  "50", 256+512 } } },
    2012             :     { { {  "75", 361+512 }, {  "75", 368+512 }, {  "75", 368+512 }, {  "75", 384+512 } } },
    2013             :     { { { "100", 470+512 }, { "100", 480+512 }, { "100", 480+512 }, { "100", 512+512 } } },
    2014             :     { { { "125", 580+512 }, { "125", 592+512 }, { "125", 592+512 }, { "125", 640+512 } } },
    2015             : };
    2016             : 
    2017             : static const GraticuleLines flat_ire10[] = {
    2018             :     { { { "-25",-156+1024 }, { "-25",-160+1024 }, { "-25",-160+1024 }, { "-25", -256+1024 } } },
    2019             :     { { {   "0",  64+1024 }, {   "0",  64+1024 }, {  "0",   64+1024 }, {   "0",    0+1024 } } },
    2020             :     { { {  "25", 283+1024 }, {  "25", 288+1024 }, {  "25", 288+1024 }, {  "25",  256+1024 } } },
    2021             :     { { {  "50", 502+1024 }, {  "50", 512+1024 }, {  "50", 512+1024 }, {  "50",  512+1024 } } },
    2022             :     { { {  "75", 721+1024 }, {  "75", 736+1024 }, {  "75", 736+1024 }, {  "75",  768+1024 } } },
    2023             :     { { { "100", 940+1024 }, { "100", 960+1024 }, { "100", 960+1024 }, { "100", 1024+1024 } } },
    2024             :     { { { "125",1160+1024 }, { "125",1184+1024 }, { "125",1184+1024 }, { "125", 1280+1024 } } },
    2025             : };
    2026             : 
    2027             : static const GraticuleLines flat_ire12[] = {
    2028             :     { { { "-25", -624+4096 }, { "-25", -640+4096 }, { "-25", -640+4096 }, { "-25",-1024+4096 } } },
    2029             :     { { {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",    0+4096 } } },
    2030             :     { { {  "25", 1132+4096 }, {  "25", 1152+4096 }, {  "25", 1152+4096 }, {  "25", 1024+4096 } } },
    2031             :     { { {  "50", 2008+4096 }, {  "50", 2048+4096 }, {  "50", 2048+4096 }, {  "50", 2048+4096 } } },
    2032             :     { { {  "75", 2884+4096 }, {  "75", 2944+4096 }, {  "75", 2944+4096 }, {  "75", 3072+4096 } } },
    2033             :     { { { "100", 3760+4096 }, { "100", 3840+4096 }, { "100", 3840+4096 }, { "100", 4096+4096 } } },
    2034             :     { { { "125", 4640+4096 }, { "125", 4736+4096 }, { "125", 4736+4096 }, { "125", 5120+4096 } } },
    2035             : };
    2036             : 
    2037             : static const GraticuleLines digital8[] = {
    2038             :     { { {  "16",  16 }, {  "16",  16 }, {  "16",  16 }, {   "0",   0 } } },
    2039             :     { { { "128", 128 }, { "128", 128 }, { "128", 128 }, { "128", 128 } } },
    2040             :     { { { "235", 235 }, { "240", 240 }, { "240", 240 }, { "255", 255 } } },
    2041             : };
    2042             : 
    2043             : static const GraticuleLines digital9[] = {
    2044             :     { { {  "32",  32 }, {  "32",  32 }, {  "32",  32 }, {   "0",   0 } } },
    2045             :     { { { "256", 256 }, { "256", 256 }, { "256", 256 }, { "256", 256 } } },
    2046             :     { { { "470", 470 }, { "480", 480 }, { "480", 480 }, { "511", 511 } } },
    2047             : };
    2048             : 
    2049             : static const GraticuleLines digital10[] = {
    2050             :     { { {  "64",  64 }, {  "64",  64 }, {  "64",  64 }, {    "0",    0 } } },
    2051             :     { { { "512", 512 }, { "512", 512 }, { "512", 512 }, {  "512",  512 } } },
    2052             :     { { { "940", 940 }, { "960", 960 }, { "960", 960 }, { "1023", 1023 } } },
    2053             : };
    2054             : 
    2055             : static const GraticuleLines digital12[] = {
    2056             :     { { {  "256",  256 }, {  "256",  256 }, {  "256",  256 }, {    "0",    0 } } },
    2057             :     { { { "2048", 2048 }, { "2048", 2048 }, { "2048", 2048 }, { "2048", 2048 } } },
    2058             :     { { { "3760", 3760 }, { "3840", 3840 }, { "3840", 3840 }, { "4095", 4095 } } },
    2059             : };
    2060             : 
    2061             : static const GraticuleLines millivolts8[] = {
    2062             :     { { {   "0",  16 }, {   "0",  16 }, {   "0",  16 }, {   "0",   0 } } },
    2063             :     { { { "175",  71 }, { "175",  72 }, { "175",  72 }, { "175",  64 } } },
    2064             :     { { { "350", 126 }, { "350", 128 }, { "350", 128 }, { "350", 128 } } },
    2065             :     { { { "525", 180 }, { "525", 184 }, { "525", 184 }, { "525", 192 } } },
    2066             :     { { { "700", 235 }, { "700", 240 }, { "700", 240 }, { "700", 255 } } },
    2067             : };
    2068             : 
    2069             : static const GraticuleLines millivolts9[] = {
    2070             :     { { {   "0",  32 }, {   "0",  32 }, {   "0",  32 }, {   "0",   0 } } },
    2071             :     { { { "175", 142 }, { "175", 144 }, { "175", 144 }, { "175", 128 } } },
    2072             :     { { { "350", 251 }, { "350", 256 }, { "350", 256 }, { "350", 256 } } },
    2073             :     { { { "525", 361 }, { "525", 368 }, { "525", 368 }, { "525", 384 } } },
    2074             :     { { { "700", 470 }, { "700", 480 }, { "700", 480 }, { "700", 511 } } },
    2075             : };
    2076             : 
    2077             : static const GraticuleLines millivolts10[] = {
    2078             :     { { {   "0",  64 }, {   "0",  64 }, {   "0",  64 }, {   "0",    0 } } },
    2079             :     { { { "175", 283 }, { "175", 288 }, { "175", 288 }, { "175",  256 } } },
    2080             :     { { { "350", 502 }, { "350", 512 }, { "350", 512 }, { "350",  512 } } },
    2081             :     { { { "525", 721 }, { "525", 736 }, { "525", 736 }, { "525",  768 } } },
    2082             :     { { { "700", 940 }, { "700", 960 }, { "700", 960 }, { "700", 1023 } } },
    2083             : };
    2084             : 
    2085             : static const GraticuleLines millivolts12[] = {
    2086             :     { { {   "0",  256 }, {   "0",  256 }, {   "0",  256 }, {   "0",    0 } } },
    2087             :     { { { "175", 1132 }, { "175", 1152 }, { "175", 1152 }, { "175", 1024 } } },
    2088             :     { { { "350", 2008 }, { "350", 2048 }, { "350", 2048 }, { "350", 2048 } } },
    2089             :     { { { "525", 2884 }, { "525", 2944 }, { "525", 2944 }, { "525", 3072 } } },
    2090             :     { { { "700", 3760 }, { "700", 3840 }, { "700", 3840 }, { "700", 4095 } } },
    2091             : };
    2092             : 
    2093             : static const GraticuleLines ire8[] = {
    2094             :     { { {   "0",  16 }, {   "0",  16 }, {   "0",  16 }, {   "0",   0 } } },
    2095             :     { { {  "25",  71 }, {  "25",  72 }, {  "25",  72 }, {  "25",  64 } } },
    2096             :     { { {  "50", 126 }, {  "50", 128 }, {  "50", 128 }, {  "50", 128 } } },
    2097             :     { { {  "75", 180 }, {  "75", 184 }, {  "75", 184 }, {  "75", 192 } } },
    2098             :     { { { "100", 235 }, { "100", 240 }, { "100", 240 }, { "100", 255 } } },
    2099             : };
    2100             : 
    2101             : static const GraticuleLines ire9[] = {
    2102             :     { { {   "0",  32 }, {   "0",  32 }, {   "0",  32 }, {   "0",   0 } } },
    2103             :     { { {  "25", 142 }, {  "25", 144 }, {  "25", 144 }, {  "25", 128 } } },
    2104             :     { { {  "50", 251 }, {  "50", 256 }, {  "50", 256 }, {  "50", 256 } } },
    2105             :     { { {  "75", 361 }, {  "75", 368 }, {  "75", 368 }, {  "75", 384 } } },
    2106             :     { { { "100", 470 }, { "100", 480 }, { "100", 480 }, { "100", 511 } } },
    2107             : };
    2108             : 
    2109             : static const GraticuleLines ire10[] = {
    2110             :     { { {   "0",  64 }, {   "0",  64 }, {  "0",   64 }, {   "0",    0 } } },
    2111             :     { { {  "25", 283 }, {  "25", 288 }, {  "25", 288 }, {  "25",  256 } } },
    2112             :     { { {  "50", 502 }, {  "50", 512 }, {  "50", 512 }, {  "50",  512 } } },
    2113             :     { { {  "75", 721 }, {  "75", 736 }, {  "75", 736 }, {  "75",  768 } } },
    2114             :     { { { "100", 940 }, { "100", 960 }, { "100", 960 }, { "100", 1023 } } },
    2115             : };
    2116             : 
    2117             : static const GraticuleLines ire12[] = {
    2118             :     { { {   "0",  256 }, {   "0",  256 }, {   "0",  256 }, {   "0",    0 } } },
    2119             :     { { {  "25", 1132 }, {  "25", 1152 }, {  "25", 1152 }, {  "25", 1024 } } },
    2120             :     { { {  "50", 2008 }, {  "50", 2048 }, {  "50", 2048 }, {  "50", 2048 } } },
    2121             :     { { {  "75", 2884 }, {  "75", 2944 }, {  "75", 2944 }, {  "75", 3072 } } },
    2122             :     { { { "100", 3760 }, { "100", 3840 }, { "100", 3840 }, { "100", 4095 } } },
    2123             : };
    2124             : 
    2125             : static const GraticuleLines chroma_digital8[] = {
    2126             :     { { {  "50",  50 }, {  "50",  50 }, {  "50",  50 }, {  "50",  50 } } },
    2127             :     { { { "100", 100 }, { "100", 100 }, { "100", 100 }, { "100", 100 } } },
    2128             :     { { { "150", 150 }, { "150", 150 }, { "150", 150 }, { "150", 150 } } },
    2129             :     { { { "200", 200 }, { "200", 200 }, { "200", 200 }, { "200", 200 } } },
    2130             :     { { { "255", 255 }, { "255", 255 }, { "255", 255 }, { "255", 255 } } },
    2131             : };
    2132             : 
    2133             : static const GraticuleLines chroma_digital9[] = {
    2134             :     { { { "100", 100 }, { "100", 100 }, { "100", 100 }, { "100", 100 } } },
    2135             :     { { { "200", 200 }, { "200", 200 }, { "200", 200 }, { "200", 200 } } },
    2136             :     { { { "300", 300 }, { "300", 300 }, { "300", 300 }, { "300", 300 } } },
    2137             :     { { { "400", 400 }, { "400", 400 }, { "400", 400 }, { "400", 400 } } },
    2138             :     { { { "500", 500 }, { "500", 500 }, { "500", 500 }, { "500", 500 } } },
    2139             : };
    2140             : 
    2141             : static const GraticuleLines chroma_digital10[] = {
    2142             :     { { { "200", 200 }, { "200", 200 }, { "200", 200 }, { "200", 200 } } },
    2143             :     { { { "400", 400 }, { "400", 400 }, { "400", 400 }, { "400", 400 } } },
    2144             :     { { { "600", 600 }, { "600", 600 }, { "600", 600 }, { "600", 600 } } },
    2145             :     { { { "800", 800 }, { "800", 800 }, { "800", 800 }, { "800", 800 } } },
    2146             :     { { {"1000",1000 }, {"1000",1000 }, {"1000",1000 }, {"1000",1000 } } },
    2147             : };
    2148             : 
    2149             : static const GraticuleLines chroma_digital12[] = {
    2150             :     { { {  "800",  800 }, {  "800",  800 }, {  "800",  800 }, {  "800",  800 } } },
    2151             :     { { { "1600", 1600 }, { "1600", 1600 }, { "1600", 1600 }, { "1600", 1600 } } },
    2152             :     { { { "2400", 2400 }, { "2400", 2400 }, { "2400", 2400 }, { "2400", 2400 } } },
    2153             :     { { { "3200", 3200 }, { "3200", 3200 }, { "3200", 3200 }, { "3200", 3200 } } },
    2154             :     { { { "4000", 4000 }, { "4000", 4000 }, { "4000", 4000 }, { "4000", 4000 } } },
    2155             : };
    2156             : 
    2157           0 : static void blend_vline(uint8_t *dst, int height, int linesize, float o1, float o2, int v, int step)
    2158             : {
    2159             :     int y;
    2160             : 
    2161           0 :     for (y = 0; y < height; y += step) {
    2162           0 :         dst[0] = v * o1 + dst[0] * o2;
    2163             : 
    2164           0 :         dst += linesize * step;
    2165             :     }
    2166           0 : }
    2167             : 
    2168           0 : static void blend_vline16(uint16_t *dst, int height, int linesize, float o1, float o2, int v, int step)
    2169             : {
    2170             :     int y;
    2171             : 
    2172           0 :     for (y = 0; y < height; y += step) {
    2173           0 :         dst[0] = v * o1 + dst[0] * o2;
    2174             : 
    2175           0 :         dst += (linesize / 2) * step;
    2176             :     }
    2177           0 : }
    2178             : 
    2179           0 : static void blend_hline(uint8_t *dst, int width, float o1, float o2, int v, int step)
    2180             : {
    2181             :     int x;
    2182             : 
    2183           0 :     for (x = 0; x < width; x += step) {
    2184           0 :         dst[x] = v * o1 + dst[x] * o2;
    2185             :     }
    2186           0 : }
    2187             : 
    2188           0 : static void blend_hline16(uint16_t *dst, int width, float o1, float o2, int v, int step)
    2189             : {
    2190             :     int x;
    2191             : 
    2192           0 :     for (x = 0; x < width; x += step) {
    2193           0 :         dst[x] = v * o1 + dst[x] * o2;
    2194             :     }
    2195           0 : }
    2196             : 
    2197           0 : static void draw_htext(AVFrame *out, int x, int y, float o1, float o2, const char *txt, const uint8_t color[4])
    2198             : {
    2199             :     const uint8_t *font;
    2200             :     int font_height;
    2201             :     int i, plane;
    2202             : 
    2203           0 :     font = avpriv_cga_font,   font_height =  8;
    2204             : 
    2205           0 :     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
    2206           0 :         for (i = 0; txt[i]; i++) {
    2207             :             int char_y, mask;
    2208           0 :             int v = color[plane];
    2209             : 
    2210           0 :             uint8_t *p = out->data[plane] + y * out->linesize[plane] + (x + i * 8);
    2211           0 :             for (char_y = 0; char_y < font_height; char_y++) {
    2212           0 :                 for (mask = 0x80; mask; mask >>= 1) {
    2213           0 :                     if (font[txt[i] * font_height + char_y] & mask)
    2214           0 :                         p[0] = p[0] * o2 + v * o1;
    2215           0 :                     p++;
    2216             :                 }
    2217           0 :                 p += out->linesize[plane] - 8;
    2218             :             }
    2219             :         }
    2220             :     }
    2221           0 : }
    2222             : 
    2223           0 : static void draw_htext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
    2224             : {
    2225             :     const uint8_t *font;
    2226             :     int font_height;
    2227             :     int i, plane;
    2228             : 
    2229           0 :     font = avpriv_cga_font,   font_height =  8;
    2230             : 
    2231           0 :     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
    2232           0 :         for (i = 0; txt[i]; i++) {
    2233             :             int char_y, mask;
    2234           0 :             int v = color[plane] * mult;
    2235             : 
    2236           0 :             uint16_t *p = (uint16_t *)(out->data[plane] + y * out->linesize[plane]) + (x + i * 8);
    2237           0 :             for (char_y = 0; char_y < font_height; char_y++) {
    2238           0 :                 for (mask = 0x80; mask; mask >>= 1) {
    2239           0 :                     if (font[txt[i] * font_height + char_y] & mask)
    2240           0 :                         p[0] = p[0] * o2 + v * o1;
    2241           0 :                     p++;
    2242             :                 }
    2243           0 :                 p += out->linesize[plane] / 2 - 8;
    2244             :             }
    2245             :         }
    2246             :     }
    2247           0 : }
    2248             : 
    2249           0 : static void draw_vtext(AVFrame *out, int x, int y, float o1, float o2, const char *txt, const uint8_t color[4])
    2250             : {
    2251             :     const uint8_t *font;
    2252             :     int font_height;
    2253             :     int i, plane;
    2254             : 
    2255           0 :     font = avpriv_cga_font,   font_height =  8;
    2256             : 
    2257           0 :     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
    2258           0 :         for (i = 0; txt[i]; i++) {
    2259             :             int char_y, mask;
    2260           0 :             int v = color[plane];
    2261             : 
    2262           0 :             for (char_y = font_height - 1; char_y >= 0; char_y--) {
    2263           0 :                 uint8_t *p = out->data[plane] + (y + i * 10) * out->linesize[plane] + x;
    2264           0 :                 for (mask = 0x80; mask; mask >>= 1) {
    2265           0 :                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
    2266           0 :                         p[char_y] = p[char_y] * o2 + v * o1;
    2267           0 :                     p += out->linesize[plane];
    2268             :                 }
    2269             :             }
    2270             :         }
    2271             :     }
    2272           0 : }
    2273             : 
    2274           0 : static void draw_vtext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
    2275             : {
    2276             :     const uint8_t *font;
    2277             :     int font_height;
    2278             :     int i, plane;
    2279             : 
    2280           0 :     font = avpriv_cga_font,   font_height =  8;
    2281             : 
    2282           0 :     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
    2283           0 :         for (i = 0; txt[i]; i++) {
    2284             :             int char_y, mask;
    2285           0 :             int v = color[plane] * mult;
    2286             : 
    2287           0 :             for (char_y = 0; char_y < font_height; char_y++) {
    2288           0 :                 uint16_t *p = (uint16_t *)(out->data[plane] + (y + i * 10) * out->linesize[plane]) + x;
    2289           0 :                 for (mask = 0x80; mask; mask >>= 1) {
    2290           0 :                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
    2291           0 :                         p[char_y] = p[char_y] * o2 + v * o1;
    2292           0 :                     p += out->linesize[plane] / 2;
    2293             :                 }
    2294             :             }
    2295             :         }
    2296             :     }
    2297           0 : }
    2298             : 
    2299         200 : static void graticule_none(WaveformContext *s, AVFrame *out)
    2300             : {
    2301         200 : }
    2302             : 
    2303           0 : static void graticule_green_row(WaveformContext *s, AVFrame *out)
    2304             : {
    2305           0 :     const int step = (s->flags & 2) + 1;
    2306           0 :     const float o1 = s->opacity;
    2307           0 :     const float o2 = 1. - o1;
    2308           0 :     const int height = s->display == PARADE ? out->height / s->acomp : out->height;
    2309           0 :     int k = 0, c, p, l, offset_x = 0, offset_y = 0;
    2310             : 
    2311           0 :     for (c = 0; c < s->ncomp; c++) {
    2312           0 :         if (!((1 << c) & s->pcomp) || (!s->display && k > 0))
    2313           0 :             continue;
    2314             : 
    2315           0 :         k++;
    2316           0 :         for (p = 0; p < s->ncomp; p++) {
    2317           0 :             const int v = green_yuva_color[p];
    2318           0 :             for (l = 0; l < s->nb_glines; l++) {
    2319           0 :                 const uint16_t pos = s->glines[l].line[c].pos;
    2320           0 :                 int x = offset_x + (s->mirror ? s->size - 1 - pos : pos);
    2321           0 :                 uint8_t *dst = out->data[p] + offset_y * out->linesize[p] + x;
    2322             : 
    2323           0 :                 blend_vline(dst, height, out->linesize[p], o1, o2, v, step);
    2324             :             }
    2325             :         }
    2326             : 
    2327           0 :         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
    2328           0 :             const char *name = s->glines[l].line[c].name;
    2329           0 :             const uint16_t pos = s->glines[l].line[c].pos;
    2330           0 :             int x = offset_x + (s->mirror ? s->size - 1 - pos : pos) - 10;
    2331             : 
    2332           0 :             if (x < 0)
    2333           0 :                 x = 4;
    2334             : 
    2335           0 :             draw_vtext(out, x, offset_y + 2, o1, o2, name, green_yuva_color);
    2336             :         }
    2337             : 
    2338           0 :         offset_x += s->size * (s->display == STACK);
    2339           0 :         offset_y += height * (s->display == PARADE);
    2340             :     }
    2341           0 : }
    2342             : 
    2343           0 : static void graticule16_green_row(WaveformContext *s, AVFrame *out)
    2344             : {
    2345           0 :     const int step = (s->flags & 2) + 1;
    2346           0 :     const float o1 = s->opacity;
    2347           0 :     const float o2 = 1. - o1;
    2348           0 :     const int mult = s->size / 256;
    2349           0 :     const int height = s->display == PARADE ? out->height / s->acomp : out->height;
    2350           0 :     int k = 0, c, p, l, offset_x = 0, offset_y = 0;
    2351             : 
    2352           0 :     for (c = 0; c < s->ncomp; c++) {
    2353           0 :         if (!((1 << c) & s->pcomp) || (!s->display && k > 0))
    2354           0 :             continue;
    2355             : 
    2356           0 :         k++;
    2357           0 :         for (p = 0; p < s->ncomp; p++) {
    2358           0 :             const int v = green_yuva_color[p] * mult;
    2359           0 :             for (l = 0; l < s->nb_glines ; l++) {
    2360           0 :                 const uint16_t pos = s->glines[l].line[c].pos;
    2361           0 :                 int x = offset_x + (s->mirror ? s->size - 1 - pos : pos);
    2362           0 :                 uint16_t *dst = (uint16_t *)(out->data[p] + offset_y * out->linesize[p]) + x;
    2363             : 
    2364           0 :                 blend_vline16(dst, height, out->linesize[p], o1, o2, v, step);
    2365             :             }
    2366             :         }
    2367             : 
    2368           0 :         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
    2369           0 :             const char *name = s->glines[l].line[c].name;
    2370           0 :             const uint16_t pos = s->glines[l].line[c].pos;
    2371           0 :             int x = offset_x + (s->mirror ? s->size - 1 - pos : pos) - 10;
    2372             : 
    2373           0 :             if (x < 0)
    2374           0 :                 x = 4;
    2375             : 
    2376           0 :             draw_vtext16(out, x, offset_y + 2, mult, o1, o2, name, green_yuva_color);
    2377             :         }
    2378             : 
    2379           0 :         offset_x += s->size * (s->display == STACK);
    2380           0 :         offset_y += height * (s->display == PARADE);
    2381             :     }
    2382           0 : }
    2383             : 
    2384           0 : static void graticule_green_column(WaveformContext *s, AVFrame *out)
    2385             : {
    2386           0 :     const int step = (s->flags & 2) + 1;
    2387           0 :     const float o1 = s->opacity;
    2388           0 :     const float o2 = 1. - o1;
    2389           0 :     const int width = s->display == PARADE ? out->width / s->acomp : out->width;
    2390           0 :     int k = 0, c, p, l, offset_y = 0, offset_x = 0;
    2391             : 
    2392           0 :     for (c = 0; c < s->ncomp; c++) {
    2393           0 :         if ((!((1 << c) & s->pcomp) || (!s->display && k > 0)))
    2394           0 :             continue;
    2395             : 
    2396           0 :         k++;
    2397           0 :         for (p = 0; p < s->ncomp; p++) {
    2398           0 :             const int v = green_yuva_color[p];
    2399           0 :             for (l = 0; l < s->nb_glines ; l++) {
    2400           0 :                 const uint16_t pos = s->glines[l].line[c].pos;
    2401           0 :                 int y = offset_y + (s->mirror ? s->size - 1 - pos : pos);
    2402           0 :                 uint8_t *dst = out->data[p] + y * out->linesize[p] + offset_x;
    2403             : 
    2404           0 :                 blend_hline(dst, width, o1, o2, v, step);
    2405             :             }
    2406             :         }
    2407             : 
    2408           0 :         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
    2409           0 :             const char *name = s->glines[l].line[c].name;
    2410           0 :             const uint16_t pos = s->glines[l].line[c].pos;
    2411           0 :             int y = offset_y + (s->mirror ? s->size - 1 - pos : pos) - 10;
    2412             : 
    2413           0 :             if (y < 0)
    2414           0 :                 y = 4;
    2415             : 
    2416           0 :             draw_htext(out, 2 + offset_x, y, o1, o2, name, green_yuva_color);
    2417             :         }
    2418             : 
    2419           0 :         offset_y += s->size * (s->display == STACK);
    2420           0 :         offset_x += width * (s->display == PARADE);
    2421             :     }
    2422           0 : }
    2423             : 
    2424           0 : static void graticule16_green_column(WaveformContext *s, AVFrame *out)
    2425             : {
    2426           0 :     const int step = (s->flags & 2) + 1;
    2427           0 :     const float o1 = s->opacity;
    2428           0 :     const float o2 = 1. - o1;
    2429           0 :     const int mult = s->size / 256;
    2430           0 :     const int width = s->display == PARADE ? out->width / s->acomp : out->width;
    2431           0 :     int k = 0, c, p, l, offset_x = 0, offset_y = 0;
    2432             : 
    2433           0 :     for (c = 0; c < s->ncomp; c++) {
    2434           0 :         if ((!((1 << c) & s->pcomp) || (!s->display && k > 0)))
    2435           0 :             continue;
    2436             : 
    2437           0 :         k++;
    2438           0 :         for (p = 0; p < s->ncomp; p++) {
    2439           0 :             const int v = green_yuva_color[p] * mult;
    2440           0 :             for (l = 0; l < s->nb_glines ; l++) {
    2441           0 :                 const uint16_t pos = s->glines[l].line[c].pos;
    2442           0 :                 int y = offset_y + (s->mirror ? s->size - 1 - pos : pos);
    2443           0 :                 uint16_t *dst = (uint16_t *)(out->data[p] + y * out->linesize[p]) + offset_x;
    2444             : 
    2445           0 :                 blend_hline16(dst, width, o1, o2, v, step);
    2446             :             }
    2447             :         }
    2448             : 
    2449           0 :         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
    2450           0 :             const char *name = s->glines[l].line[c].name;
    2451           0 :             const uint16_t pos = s->glines[l].line[c].pos;
    2452           0 :             int y = offset_y + (s->mirror ? s->size - 1 - pos: pos) - 10;
    2453             : 
    2454           0 :             if (y < 0)
    2455           0 :                 y = 4;
    2456             : 
    2457           0 :             draw_htext16(out, 2 + offset_x, y, mult, o1, o2, name, green_yuva_color);
    2458             :         }
    2459             : 
    2460           0 :         offset_y += s->size * (s->display == STACK);
    2461           0 :         offset_x += width * (s->display == PARADE);
    2462             :     }
    2463           0 : }
    2464             : 
    2465           4 : static int config_input(AVFilterLink *inlink)
    2466             : {
    2467           4 :     AVFilterContext *ctx = inlink->dst;
    2468           4 :     WaveformContext *s = ctx->priv;
    2469             : 
    2470           4 :     s->desc  = av_pix_fmt_desc_get(inlink->format);
    2471           4 :     s->ncomp = s->desc->nb_components;
    2472           4 :     s->bits = s->desc->comp[0].depth;
    2473           4 :     s->max = 1 << s->bits;
    2474           4 :     s->intensity = s->fintensity * (s->max - 1);
    2475             : 
    2476           4 :     s->shift_w[0] = s->shift_w[3] = 0;
    2477           4 :     s->shift_h[0] = s->shift_h[3] = 0;
    2478           4 :     s->shift_w[1] = s->shift_w[2] = s->desc->log2_chroma_w;
    2479           4 :     s->shift_h[1] = s->shift_h[2] = s->desc->log2_chroma_h;
    2480             : 
    2481           4 :     s->graticulef = graticule_none;
    2482             : 
    2483           4 :     switch (s->filter) {
    2484           0 :     case AFLAT: s->size = 256 * 2; break;
    2485           0 :     case FLAT:  s->size = 256 * 3; break;
    2486           4 :     default:    s->size = 256;     break;
    2487             :     }
    2488             : 
    2489          12 :     switch (s->filter | ((s->bits > 8) << 4) |
    2490           8 :             (s->mode << 8) | (s->mirror << 12)) {
    2491           3 :     case 0x1100: s->waveform = lowpass_column_mirror; break;
    2492           1 :     case 0x1000: s->waveform = lowpass_row_mirror;    break;
    2493           0 :     case 0x0100: s->waveform = lowpass_column;        break;
    2494           0 :     case 0x0000: s->waveform = lowpass_row;           break;
    2495           0 :     case 0x1110: s->waveform = lowpass16_column_mirror; break;
    2496           0 :     case 0x1010: s->waveform = lowpass16_row_mirror;    break;
    2497           0 :     case 0x0110: s->waveform = lowpass16_column;        break;
    2498           0 :     case 0x0010: s->waveform = lowpass16_row;           break;
    2499           0 :     case 0x1101:
    2500             :     case 0x1001:
    2501             :     case 0x0101:
    2502           0 :     case 0x0001: s->waveform = flat;      break;
    2503           0 :     case 0x1111:
    2504             :     case 0x1011:
    2505             :     case 0x0111:
    2506           0 :     case 0x0011: s->waveform = flat16;    break;
    2507           0 :     case 0x1102:
    2508             :     case 0x1002:
    2509             :     case 0x0102:
    2510           0 :     case 0x0002: s->waveform = aflat;     break;
    2511           0 :     case 0x1112:
    2512             :     case 0x1012:
    2513             :     case 0x0112:
    2514           0 :     case 0x0012: s->waveform = aflat16;   break;
    2515           0 :     case 0x1103:
    2516             :     case 0x1003:
    2517             :     case 0x0103:
    2518           0 :     case 0x0003: s->waveform = chroma;    break;
    2519           0 :     case 0x1113:
    2520             :     case 0x1013:
    2521             :     case 0x0113:
    2522           0 :     case 0x0013: s->waveform = chroma16;  break;
    2523           0 :     case 0x1104:
    2524             :     case 0x1004:
    2525             :     case 0x0104:
    2526           0 :     case 0x0004: s->waveform = color;     break;
    2527           0 :     case 0x1114:
    2528             :     case 0x1014:
    2529             :     case 0x0114:
    2530           0 :     case 0x0014: s->waveform = color16;   break;
    2531           0 :     case 0x1105:
    2532             :     case 0x1005:
    2533             :     case 0x0105:
    2534           0 :     case 0x0005: s->waveform = acolor;    break;
    2535           0 :     case 0x1115:
    2536             :     case 0x1015:
    2537             :     case 0x0115:
    2538           0 :     case 0x0015: s->waveform = acolor16;  break;
    2539             :     }
    2540             : 
    2541           4 :     switch (s->filter) {
    2542           4 :     case LOWPASS:
    2543             :     case COLOR:
    2544             :     case ACOLOR:
    2545             :     case CHROMA:
    2546             :     case AFLAT:
    2547             :     case FLAT:
    2548           4 :         if (s->graticule && s->mode == 1)
    2549           0 :             s->graticulef = s->bits > 8 ? graticule16_green_column : graticule_green_column;
    2550           4 :         else if (s->graticule && s->mode == 0)
    2551           0 :             s->graticulef = s->bits > 8 ? graticule16_green_row : graticule_green_row;
    2552           4 :         break;
    2553             :     }
    2554             : 
    2555           4 :     switch (s->filter) {
    2556           4 :     case COLOR:
    2557             :     case ACOLOR:
    2558             :     case LOWPASS:
    2559           4 :         switch (s->scale) {
    2560           4 :         case DIGITAL:
    2561           4 :             switch (s->bits) {
    2562           4 :             case  8: s->glines = (GraticuleLines *)digital8;  s->nb_glines = FF_ARRAY_ELEMS(digital8);  break;
    2563           0 :             case  9: s->glines = (GraticuleLines *)digital9;  s->nb_glines = FF_ARRAY_ELEMS(digital9);  break;
    2564           0 :             case 10: s->glines = (GraticuleLines *)digital10; s->nb_glines = FF_ARRAY_ELEMS(digital10); break;
    2565           0 :             case 12: s->glines = (GraticuleLines *)digital12; s->nb_glines = FF_ARRAY_ELEMS(digital12); break;
    2566             :             }
    2567           4 :             break;
    2568           0 :         case MILLIVOLTS:
    2569           0 :             switch (s->bits) {
    2570           0 :             case  8: s->glines = (GraticuleLines *)millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(millivolts8);  break;
    2571           0 :             case  9: s->glines = (GraticuleLines *)millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(millivolts9);  break;
    2572           0 :             case 10: s->glines = (GraticuleLines *)millivolts10; s->nb_glines = FF_ARRAY_ELEMS(millivolts10); break;
    2573           0 :             case 12: s->glines = (GraticuleLines *)millivolts12; s->nb_glines = FF_ARRAY_ELEMS(millivolts12); break;
    2574             :             }
    2575           0 :             break;
    2576           0 :         case IRE:
    2577           0 :             switch (s->bits) {
    2578           0 :             case  8: s->glines = (GraticuleLines *)ire8;  s->nb_glines = FF_ARRAY_ELEMS(ire8);  break;
    2579           0 :             case  9: s->glines = (GraticuleLines *)ire9;  s->nb_glines = FF_ARRAY_ELEMS(ire9);  break;
    2580           0 :             case 10: s->glines = (GraticuleLines *)ire10; s->nb_glines = FF_ARRAY_ELEMS(ire10); break;
    2581           0 :             case 12: s->glines = (GraticuleLines *)ire12; s->nb_glines = FF_ARRAY_ELEMS(ire12); break;
    2582             :             }
    2583           0 :             break;
    2584             :         }
    2585           4 :         break;
    2586           0 :     case CHROMA:
    2587           0 :         switch (s->scale) {
    2588           0 :         case DIGITAL:
    2589           0 :             switch (s->bits) {
    2590           0 :             case  8: s->glines = (GraticuleLines *)chroma_digital8;  s->nb_glines = FF_ARRAY_ELEMS(chroma_digital8);  break;
    2591           0 :             case  9: s->glines = (GraticuleLines *)chroma_digital9;  s->nb_glines = FF_ARRAY_ELEMS(chroma_digital9);  break;
    2592           0 :             case 10: s->glines = (GraticuleLines *)chroma_digital10; s->nb_glines = FF_ARRAY_ELEMS(chroma_digital10); break;
    2593           0 :             case 12: s->glines = (GraticuleLines *)chroma_digital12; s->nb_glines = FF_ARRAY_ELEMS(chroma_digital12); break;
    2594             :             }
    2595           0 :             break;
    2596           0 :         case MILLIVOLTS:
    2597           0 :             switch (s->bits) {
    2598           0 :             case  8: s->glines = (GraticuleLines *)millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(millivolts8);  break;
    2599           0 :             case  9: s->glines = (GraticuleLines *)millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(millivolts9);  break;
    2600           0 :             case 10: s->glines = (GraticuleLines *)millivolts10; s->nb_glines = FF_ARRAY_ELEMS(millivolts10); break;
    2601           0 :             case 12: s->glines = (GraticuleLines *)millivolts12; s->nb_glines = FF_ARRAY_ELEMS(millivolts12); break;
    2602             :             }
    2603           0 :             break;
    2604           0 :         case IRE:
    2605           0 :             switch (s->bits) {
    2606           0 :             case  8: s->glines = (GraticuleLines *)ire8;  s->nb_glines = FF_ARRAY_ELEMS(ire8);  break;
    2607           0 :             case  9: s->glines = (GraticuleLines *)ire9;  s->nb_glines = FF_ARRAY_ELEMS(ire9);  break;
    2608           0 :             case 10: s->glines = (GraticuleLines *)ire10; s->nb_glines = FF_ARRAY_ELEMS(ire10); break;
    2609           0 :             case 12: s->glines = (GraticuleLines *)ire12; s->nb_glines = FF_ARRAY_ELEMS(ire12); break;
    2610             :             }
    2611           0 :             break;
    2612             :         }
    2613           0 :         break;
    2614           0 :     case AFLAT:
    2615           0 :         switch (s->scale) {
    2616           0 :         case DIGITAL:
    2617           0 :             switch (s->bits) {
    2618           0 :             case  8: s->glines = (GraticuleLines *)aflat_digital8;  s->nb_glines = FF_ARRAY_ELEMS(aflat_digital8);  break;
    2619           0 :             case  9: s->glines = (GraticuleLines *)aflat_digital9;  s->nb_glines = FF_ARRAY_ELEMS(aflat_digital9);  break;
    2620           0 :             case 10: s->glines = (GraticuleLines *)aflat_digital10; s->nb_glines = FF_ARRAY_ELEMS(aflat_digital10); break;
    2621           0 :             case 12: s->glines = (GraticuleLines *)aflat_digital12; s->nb_glines = FF_ARRAY_ELEMS(aflat_digital12); break;
    2622             :             }
    2623           0 :             break;
    2624           0 :         case MILLIVOLTS:
    2625           0 :             switch (s->bits) {
    2626           0 :             case  8: s->glines = (GraticuleLines *)aflat_millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts8);  break;
    2627           0 :             case  9: s->glines = (GraticuleLines *)aflat_millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts9);  break;
    2628           0 :             case 10: s->glines = (GraticuleLines *)aflat_millivolts10; s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts10); break;
    2629           0 :             case 12: s->glines = (GraticuleLines *)aflat_millivolts12; s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts12); break;
    2630             :             }
    2631           0 :             break;
    2632           0 :         case IRE:
    2633           0 :             switch (s->bits) {
    2634           0 :             case  8: s->glines = (GraticuleLines *)aflat_ire8;  s->nb_glines = FF_ARRAY_ELEMS(aflat_ire8);  break;
    2635           0 :             case  9: s->glines = (GraticuleLines *)aflat_ire9;  s->nb_glines = FF_ARRAY_ELEMS(aflat_ire9);  break;
    2636           0 :             case 10: s->glines = (GraticuleLines *)aflat_ire10; s->nb_glines = FF_ARRAY_ELEMS(aflat_ire10); break;
    2637           0 :             case 12: s->glines = (GraticuleLines *)aflat_ire12; s->nb_glines = FF_ARRAY_ELEMS(aflat_ire12); break;
    2638             :             }
    2639           0 :             break;
    2640             :         }
    2641           0 :         break;
    2642           0 :     case FLAT:
    2643           0 :         switch (s->scale) {
    2644           0 :         case DIGITAL:
    2645           0 :             switch (s->bits) {
    2646           0 :             case  8: s->glines = (GraticuleLines *)flat_digital8;  s->nb_glines = FF_ARRAY_ELEMS(flat_digital8);  break;
    2647           0 :             case  9: s->glines = (GraticuleLines *)flat_digital9;  s->nb_glines = FF_ARRAY_ELEMS(flat_digital9);  break;
    2648           0 :             case 10: s->glines = (GraticuleLines *)flat_digital10; s->nb_glines = FF_ARRAY_ELEMS(flat_digital10); break;
    2649           0 :             case 12: s->glines = (GraticuleLines *)flat_digital12; s->nb_glines = FF_ARRAY_ELEMS(flat_digital12); break;
    2650             :             }
    2651           0 :             break;
    2652           0 :         case MILLIVOLTS:
    2653           0 :             switch (s->bits) {
    2654           0 :             case  8: s->glines = (GraticuleLines *)flat_millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts8);  break;
    2655           0 :             case  9: s->glines = (GraticuleLines *)flat_millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts9);  break;
    2656           0 :             case 10: s->glines = (GraticuleLines *)flat_millivolts10; s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts10); break;
    2657           0 :             case 12: s->glines = (GraticuleLines *)flat_millivolts12; s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts12); break;
    2658             :             }
    2659           0 :             break;
    2660           0 :         case IRE:
    2661           0 :             switch (s->bits) {
    2662           0 :             case  8: s->glines = (GraticuleLines *)flat_ire8;  s->nb_glines = FF_ARRAY_ELEMS(flat_ire8);  break;
    2663           0 :             case  9: s->glines = (GraticuleLines *)flat_ire9;  s->nb_glines = FF_ARRAY_ELEMS(flat_ire9);  break;
    2664           0 :             case 10: s->glines = (GraticuleLines *)flat_ire10; s->nb_glines = FF_ARRAY_ELEMS(flat_ire10); break;
    2665           0 :             case 12: s->glines = (GraticuleLines *)flat_ire12; s->nb_glines = FF_ARRAY_ELEMS(flat_ire12); break;
    2666             :             }
    2667           0 :             break;
    2668             :         }
    2669           0 :         break;
    2670             :     }
    2671             : 
    2672           4 :     s->size = s->size << (s->bits - 8);
    2673             : 
    2674           4 :     switch (inlink->format) {
    2675           0 :     case AV_PIX_FMT_GBRAP:
    2676             :     case AV_PIX_FMT_GBRP:
    2677             :     case AV_PIX_FMT_GBRP9:
    2678             :     case AV_PIX_FMT_GBRP10:
    2679             :     case AV_PIX_FMT_GBRP12:
    2680           0 :         memcpy(s->bg_color, black_gbrp_color, sizeof(s->bg_color));
    2681           0 :         s->graticulef = graticule_none;
    2682           0 :         break;
    2683           4 :     default:
    2684           4 :         memcpy(s->bg_color, black_yuva_color, sizeof(s->bg_color));
    2685             :     }
    2686             : 
    2687           4 :     s->bg_color[3] *= s->bgopacity;
    2688             : 
    2689           4 :     return 0;
    2690             : }
    2691             : 
    2692           4 : static int config_output(AVFilterLink *outlink)
    2693             : {
    2694           4 :     AVFilterContext *ctx = outlink->src;
    2695           4 :     AVFilterLink *inlink = ctx->inputs[0];
    2696           4 :     WaveformContext *s = ctx->priv;
    2697           4 :     int comp = 0, i, j = 0, k, p, size;
    2698             : 
    2699          16 :     for (i = 0; i < s->ncomp; i++) {
    2700          12 :         if ((1 << i) & s->pcomp)
    2701           5 :             comp++;
    2702             :     }
    2703           4 :     s->acomp = comp;
    2704           4 :     s->odesc = av_pix_fmt_desc_get(outlink->format);
    2705           4 :     s->dcomp = s->odesc->nb_components;
    2706             : 
    2707           4 :     av_freep(&s->peak);
    2708             : 
    2709           4 :     if (s->mode) {
    2710           3 :         outlink->h = s->size * FFMAX(comp * (s->display == STACK), 1);
    2711           3 :         outlink->w = inlink->w * FFMAX(comp * (s->display == PARADE), 1);
    2712           3 :         size = inlink->w;
    2713             :     } else {
    2714           1 :         outlink->w = s->size * FFMAX(comp * (s->display == STACK), 1);
    2715           1 :         outlink->h = inlink->h * FFMAX(comp * (s->display == PARADE), 1);
    2716           1 :         size = inlink->h;
    2717             :     }
    2718             : 
    2719           4 :     s->peak = av_malloc_array(size, 32 * sizeof(*s->peak));
    2720           4 :     if (!s->peak)
    2721           0 :         return AVERROR(ENOMEM);
    2722             : 
    2723          16 :     for (p = 0; p < s->ncomp; p++) {
    2724          12 :         const int plane = s->desc->comp[p].plane;
    2725             :         int offset;
    2726             : 
    2727          12 :         if (!((1 << p) & s->pcomp))
    2728           7 :             continue;
    2729             : 
    2730          25 :         for (k = 0; k < 4; k++) {
    2731          20 :             s->emax[plane][k] = s->peak + size * (plane * 4 + k + 0);
    2732          20 :             s->emin[plane][k] = s->peak + size * (plane * 4 + k + 16);
    2733             :         }
    2734             : 
    2735           5 :         offset = j++ * s->size * (s->display == STACK);
    2736           5 :         s->estart[plane] = offset;
    2737           5 :         s->eend[plane]   = (offset + s->size - 1);
    2738        1701 :         for (i = 0; i < size; i++) {
    2739        8480 :             for (k = 0; k < 4; k++) {
    2740        6784 :                 s->emax[plane][k][i] = s->estart[plane];
    2741        6784 :                 s->emin[plane][k][i] = s->eend[plane];
    2742             :             }
    2743             :         }
    2744             :     }
    2745             : 
    2746           4 :     outlink->sample_aspect_ratio = (AVRational){1,1};
    2747             : 
    2748           4 :     return 0;
    2749             : }
    2750             : 
    2751         200 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
    2752             : {
    2753         200 :     AVFilterContext *ctx  = inlink->dst;
    2754         200 :     WaveformContext *s    = ctx->priv;
    2755         200 :     AVFilterLink *outlink = ctx->outputs[0];
    2756             :     AVFrame *out;
    2757             :     int i, j, k;
    2758             : 
    2759         200 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
    2760         200 :     if (!out) {
    2761           0 :         av_frame_free(&in);
    2762           0 :         return AVERROR(ENOMEM);
    2763             :     }
    2764         200 :     out->pts = in->pts;
    2765         200 :     out->color_range = AVCOL_RANGE_JPEG;
    2766             : 
    2767         800 :     for (k = 0; k < s->dcomp; k++) {
    2768         600 :         if (s->bits <= 8) {
    2769      197400 :             for (i = 0; i < outlink->h ; i++)
    2770      590400 :                 memset(out->data[s->odesc->comp[k].plane] +
    2771      196800 :                        i * out->linesize[s->odesc->comp[k].plane],
    2772      393600 :                        s->bg_color[k], outlink->w);
    2773             :         } else {
    2774           0 :             const int mult = s->max / 256;
    2775           0 :             uint16_t *dst = (uint16_t *)out->data[s->odesc->comp[k].plane];
    2776             : 
    2777           0 :             for (i = 0; i < outlink->h ; i++) {
    2778           0 :                 for (j = 0; j < outlink->w; j++)
    2779           0 :                     dst[j] = s->bg_color[k] * mult;
    2780           0 :                 dst += out->linesize[s->odesc->comp[k].plane] / 2;
    2781             :             }
    2782             :         }
    2783             :     }
    2784             : 
    2785         800 :     for (k = 0, i = 0; k < s->ncomp; k++) {
    2786         600 :         if ((1 << k) & s->pcomp) {
    2787             :             int offset_y;
    2788             :             int offset_x;
    2789             : 
    2790         250 :             if (s->display == PARADE) {
    2791           0 :                 offset_x = s->mode ? i++ * inlink->w : 0;
    2792           0 :                 offset_y = s->mode ? 0 : i++ * inlink->h;
    2793             :             } else {
    2794         250 :                 offset_y = s->mode ? i++ * s->size * !!s->display : 0;
    2795         250 :                 offset_x = s->mode ? 0 : i++ * s->size * !!s->display;
    2796             :             }
    2797         250 :             s->waveform(s, in, out, k, s->intensity, offset_y, offset_x, s->mode, s->mirror);
    2798             :         }
    2799             :     }
    2800         200 :     s->graticulef(s, out);
    2801             : 
    2802         200 :     av_frame_free(&in);
    2803         200 :     return ff_filter_frame(outlink, out);
    2804             : }
    2805             : 
    2806           4 : static av_cold void uninit(AVFilterContext *ctx)
    2807             : {
    2808           4 :     WaveformContext *s = ctx->priv;
    2809             : 
    2810           4 :     av_freep(&s->peak);
    2811           4 : }
    2812             : 
    2813             : static const AVFilterPad inputs[] = {
    2814             :     {
    2815             :         .name         = "default",
    2816             :         .type         = AVMEDIA_TYPE_VIDEO,
    2817             :         .filter_frame = filter_frame,
    2818             :         .config_props = config_input,
    2819             :     },
    2820             :     { NULL }
    2821             : };
    2822             : 
    2823             : static const AVFilterPad outputs[] = {
    2824             :     {
    2825             :         .name         = "default",
    2826             :         .type         = AVMEDIA_TYPE_VIDEO,
    2827             :         .config_props = config_output,
    2828             :     },
    2829             :     { NULL }
    2830             : };
    2831             : 
    2832             : AVFilter ff_vf_waveform = {
    2833             :     .name          = "waveform",
    2834             :     .description   = NULL_IF_CONFIG_SMALL("Video waveform monitor."),
    2835             :     .priv_size     = sizeof(WaveformContext),
    2836             :     .priv_class    = &waveform_class,
    2837             :     .query_formats = query_formats,
    2838             :     .uninit        = uninit,
    2839             :     .inputs        = inputs,
    2840             :     .outputs       = outputs,
    2841             : };

Generated by: LCOV version 1.13