]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_waveform.c
avfilter/vf_waveform: implement tint options
[ffmpeg] / libavfilter / vf_waveform.c
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 typedef struct ThreadData {
33     AVFrame *in;
34     AVFrame *out;
35     int component;
36     int offset_y;
37     int offset_x;
38 } ThreadData;
39
40 enum FilterType {
41     LOWPASS,
42     FLAT,
43     AFLAT,
44     CHROMA,
45     COLOR,
46     ACOLOR,
47     XFLAT,
48     YFLAT,
49     NB_FILTERS
50 };
51
52 enum DisplayType {
53     OVERLAY,
54     STACK,
55     PARADE,
56     NB_DISPLAYS
57 };
58
59 enum ScaleType {
60     DIGITAL,
61     MILLIVOLTS,
62     IRE,
63     NB_SCALES
64 };
65
66 enum GraticuleType {
67     GRAT_NONE,
68     GRAT_GREEN,
69     GRAT_ORANGE,
70     GRAT_INVERT,
71     NB_GRATICULES
72 };
73
74 typedef struct GraticuleLine {
75     const char *name;
76     uint16_t pos;
77 } GraticuleLine;
78
79 typedef struct GraticuleLines {
80     struct GraticuleLine line[4];
81 } GraticuleLines;
82
83 typedef struct WaveformContext {
84     const AVClass *class;
85     int            mode;
86     int            acomp;
87     int            dcomp;
88     int            ncomp;
89     int            pcomp;
90     uint8_t        bg_color[4];
91     float          fintensity;
92     int            intensity;
93     int            mirror;
94     int            display;
95     int            envelope;
96     int            graticule;
97     float          opacity;
98     float          bgopacity;
99     int            estart[4];
100     int            eend[4];
101     int            *emax[4][4];
102     int            *emin[4][4];
103     int            *peak;
104     int            filter;
105     int            flags;
106     int            bits;
107     int            max;
108     int            size;
109     int            scale;
110     uint8_t        grat_yuva_color[4];
111     int            shift_w[4], shift_h[4];
112     GraticuleLines *glines;
113     int            nb_glines;
114     int            rgb;
115     float          ftint[2];
116     int            tint[2];
117
118     int (*waveform_slice)(AVFilterContext *ctx, void *arg,
119                           int jobnr, int nb_jobs);
120     void (*graticulef)(struct WaveformContext *s, AVFrame *out);
121     void (*blend_line)(uint8_t *dst, int size, int linesize, float o1, float o2,
122                        int v, int step);
123     void (*draw_text)(AVFrame *out, int x, int y, int mult,
124                       float o1, float o2, const char *txt,
125                       const uint8_t color[4]);
126     const AVPixFmtDescriptor *desc;
127     const AVPixFmtDescriptor *odesc;
128 } WaveformContext;
129
130 #define OFFSET(x) offsetof(WaveformContext, x)
131 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
132
133 static const AVOption waveform_options[] = {
134     { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
135     { "m",    "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
136         { "row",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
137         { "column", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
138     { "intensity", "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 1, FLAGS },
139     { "i",         "set intensity", OFFSET(fintensity), AV_OPT_TYPE_FLOAT, {.dbl=0.04}, 0, 1, FLAGS },
140     { "mirror", "set mirroring", OFFSET(mirror), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
141     { "r",      "set mirroring", OFFSET(mirror), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
142     { "display", "set display mode", OFFSET(display), AV_OPT_TYPE_INT, {.i64=STACK}, 0, NB_DISPLAYS-1, FLAGS, "display" },
143     { "d",       "set display mode", OFFSET(display), AV_OPT_TYPE_INT, {.i64=STACK}, 0, NB_DISPLAYS-1, FLAGS, "display" },
144         { "overlay", NULL, 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY}, 0, 0, FLAGS, "display" },
145         { "stack",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=STACK},   0, 0, FLAGS, "display" },
146         { "parade",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=PARADE},  0, 0, FLAGS, "display" },
147     { "components", "set components to display", OFFSET(pcomp), AV_OPT_TYPE_INT, {.i64=1}, 1, 15, FLAGS },
148     { "c",          "set components to display", OFFSET(pcomp), AV_OPT_TYPE_INT, {.i64=1}, 1, 15, FLAGS },
149     { "envelope", "set envelope to display", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope" },
150     { "e",        "set envelope to display", OFFSET(envelope), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "envelope" },
151         { "none",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "envelope" },
152         { "instant",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "envelope" },
153         { "peak",         NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "envelope" },
154         { "peak+instant", NULL, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "envelope" },
155     { "filter", "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS-1, FLAGS, "filter" },
156     { "f",      "set filter", OFFSET(filter), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FILTERS-1, FLAGS, "filter" },
157         { "lowpass", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LOWPASS}, 0, 0, FLAGS, "filter" },
158         { "flat"   , NULL, 0, AV_OPT_TYPE_CONST, {.i64=FLAT},    0, 0, FLAGS, "filter" },
159         { "aflat"  , NULL, 0, AV_OPT_TYPE_CONST, {.i64=AFLAT},   0, 0, FLAGS, "filter" },
160         { "chroma",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=CHROMA},  0, 0, FLAGS, "filter" },
161         { "color",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},   0, 0, FLAGS, "filter" },
162         { "acolor",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=ACOLOR},  0, 0, FLAGS, "filter" },
163         { "xflat",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=XFLAT},   0, 0, FLAGS, "filter" },
164         { "yflat",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=YFLAT},   0, 0, FLAGS, "filter" },
165     { "graticule", "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_GRATICULES-1, FLAGS, "graticule" },
166     { "g",         "set graticule", OFFSET(graticule), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_GRATICULES-1, FLAGS, "graticule" },
167         { "none",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_NONE},   0, 0, FLAGS, "graticule" },
168         { "green",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_GREEN},  0, 0, FLAGS, "graticule" },
169         { "orange", NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_ORANGE}, 0, 0, FLAGS, "graticule" },
170         { "invert", NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRAT_INVERT}, 0, 0, FLAGS, "graticule" },
171     { "opacity", "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
172     { "o",       "set graticule opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
173     { "flags", "set graticule flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, 3, FLAGS, "flags" },
174     { "fl",    "set graticule flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64=1}, 0, 3, FLAGS, "flags" },
175         { "numbers",  "draw numbers", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "flags" },
176         { "dots",     "draw dots instead of lines", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "flags" },
177     { "scale", "set scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SCALES-1, FLAGS, "scale" },
178     { "s",     "set scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SCALES-1, FLAGS, "scale" },
179         { "digital",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=DIGITAL},    0, 0, FLAGS, "scale" },
180         { "millivolts", NULL, 0, AV_OPT_TYPE_CONST, {.i64=MILLIVOLTS}, 0, 0, FLAGS, "scale" },
181         { "ire",        NULL, 0, AV_OPT_TYPE_CONST, {.i64=IRE},        0, 0, FLAGS, "scale" },
182     { "bgopacity", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
183     { "b",         "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
184     { "tint0", "set 1st tint", OFFSET(ftint[0]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS},
185     { "t0",    "set 1st tint", OFFSET(ftint[0]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS},
186     { "tint1", "set 2nd tint", OFFSET(ftint[1]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS},
187     { "t1",    "set 2nd tint", OFFSET(ftint[1]), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS},
188     { NULL }
189 };
190
191 AVFILTER_DEFINE_CLASS(waveform);
192
193 static const enum AVPixelFormat in_lowpass_pix_fmts[] = {
194     AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
195     AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
196     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
197     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
198     AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
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_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
203     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
204     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
205     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
206     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
207     AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12,
208     AV_PIX_FMT_NONE
209 };
210
211 static const enum AVPixelFormat in_color_pix_fmts[] = {
212     AV_PIX_FMT_GBRP,     AV_PIX_FMT_GBRAP,
213     AV_PIX_FMT_GBRP9,    AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
214     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
215     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
216     AV_PIX_FMT_YUV411P,
217     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
218     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
219     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
220     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
221     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
222     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
223     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
224     AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12,
225     AV_PIX_FMT_NONE
226 };
227
228 static const enum AVPixelFormat in_flat_pix_fmts[] = {
229     AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
230     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV440P,
231     AV_PIX_FMT_YUV411P,
232     AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P,
233     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
234     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
235     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
236     AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
237     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
238     AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
239     AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV440P12,
240     AV_PIX_FMT_NONE
241 };
242
243 static const enum AVPixelFormat out_rgb8_lowpass_pix_fmts[] = {
244     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
245     AV_PIX_FMT_NONE
246 };
247
248 static const enum AVPixelFormat out_rgb9_lowpass_pix_fmts[] = {
249     AV_PIX_FMT_GBRP9,
250     AV_PIX_FMT_NONE
251 };
252
253 static const enum AVPixelFormat out_rgb10_lowpass_pix_fmts[] = {
254     AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
255     AV_PIX_FMT_NONE
256 };
257
258 static const enum AVPixelFormat out_rgb12_lowpass_pix_fmts[] = {
259     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
260     AV_PIX_FMT_NONE
261 };
262
263 static const enum AVPixelFormat out_yuv8_lowpass_pix_fmts[] = {
264     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVA444P,
265     AV_PIX_FMT_NONE
266 };
267
268 static const enum AVPixelFormat out_yuv9_lowpass_pix_fmts[] = {
269     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUVA444P9,
270     AV_PIX_FMT_NONE
271 };
272
273 static const enum AVPixelFormat out_yuv10_lowpass_pix_fmts[] = {
274     AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUVA444P10,
275     AV_PIX_FMT_NONE
276 };
277
278 static const enum AVPixelFormat out_yuv12_lowpass_pix_fmts[] = {
279     AV_PIX_FMT_YUV444P12,
280     AV_PIX_FMT_NONE
281 };
282
283 static const enum AVPixelFormat out_gray8_lowpass_pix_fmts[] = {
284     AV_PIX_FMT_GRAY8,
285     AV_PIX_FMT_NONE
286 };
287
288 static const enum AVPixelFormat out_gray9_lowpass_pix_fmts[] = {
289     AV_PIX_FMT_GRAY9,
290     AV_PIX_FMT_NONE
291 };
292
293 static const enum AVPixelFormat out_gray10_lowpass_pix_fmts[] = {
294     AV_PIX_FMT_GRAY10,
295     AV_PIX_FMT_NONE
296 };
297
298 static const enum AVPixelFormat out_gray12_lowpass_pix_fmts[] = {
299     AV_PIX_FMT_GRAY12,
300     AV_PIX_FMT_NONE
301 };
302
303 static const enum AVPixelFormat flat_pix_fmts[] = {
304     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
305     AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
306     AV_PIX_FMT_YUV444P12,
307     AV_PIX_FMT_NONE
308 };
309
310 static int query_formats(AVFilterContext *ctx)
311 {
312     WaveformContext *s = ctx->priv;
313     const enum AVPixelFormat *out_pix_fmts;
314     const enum AVPixelFormat *in_pix_fmts;
315     const AVPixFmtDescriptor *desc, *desc2;
316     AVFilterFormats *avff, *avff2;
317     int depth, depth2, rgb, i, ret, ncomp, ncomp2;
318
319     if (!ctx->inputs[0]->in_formats ||
320         !ctx->inputs[0]->in_formats->nb_formats) {
321         return AVERROR(EAGAIN);
322     }
323
324     switch (s->filter) {
325     case LOWPASS: in_pix_fmts = in_lowpass_pix_fmts; break;
326     case CHROMA:
327     case XFLAT:
328     case YFLAT:
329     case AFLAT:
330     case FLAT:    in_pix_fmts = in_flat_pix_fmts;    break;
331     case ACOLOR:
332     case COLOR:   in_pix_fmts = in_color_pix_fmts;   break;
333     default: return AVERROR_BUG;
334     }
335
336     if (!ctx->inputs[0]->out_formats) {
337         if ((ret = ff_formats_ref(ff_make_format_list(in_pix_fmts), &ctx->inputs[0]->out_formats)) < 0)
338             return ret;
339     }
340
341     avff = ctx->inputs[0]->in_formats;
342     avff2 = ctx->inputs[0]->out_formats;
343     desc = av_pix_fmt_desc_get(avff->formats[0]);
344     desc2 = av_pix_fmt_desc_get(avff2->formats[0]);
345     ncomp = desc->nb_components;
346     ncomp2 = desc2->nb_components;
347     rgb = desc->flags & AV_PIX_FMT_FLAG_RGB;
348     depth = desc->comp[0].depth;
349     depth2 = desc2->comp[0].depth;
350     if (ncomp != ncomp2 || depth != depth2)
351         return AVERROR(EAGAIN);
352     for (i = 1; i < avff->nb_formats; i++) {
353         desc = av_pix_fmt_desc_get(avff->formats[i]);
354         if (rgb != (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
355             depth != desc->comp[0].depth)
356             return AVERROR(EAGAIN);
357     }
358
359     if (s->filter == LOWPASS && ncomp == 1 && depth == 8)
360         out_pix_fmts = out_gray8_lowpass_pix_fmts;
361     else if (s->filter == LOWPASS && ncomp == 1 && depth == 9)
362         out_pix_fmts = out_gray9_lowpass_pix_fmts;
363     else if (s->filter == LOWPASS && ncomp == 1 && depth == 10)
364         out_pix_fmts = out_gray10_lowpass_pix_fmts;
365     else if (s->filter == LOWPASS && ncomp == 1 && depth == 12)
366         out_pix_fmts = out_gray12_lowpass_pix_fmts;
367     else if (rgb && depth == 8 && ncomp > 2)
368         out_pix_fmts = out_rgb8_lowpass_pix_fmts;
369     else if (rgb && depth == 9 && ncomp > 2)
370         out_pix_fmts = out_rgb9_lowpass_pix_fmts;
371     else if (rgb && depth == 10 && ncomp > 2)
372         out_pix_fmts = out_rgb10_lowpass_pix_fmts;
373     else if (rgb && depth == 12 && ncomp > 2)
374         out_pix_fmts = out_rgb12_lowpass_pix_fmts;
375     else if (depth == 8 && ncomp > 2)
376         out_pix_fmts = out_yuv8_lowpass_pix_fmts;
377     else if (depth == 9 && ncomp > 2)
378         out_pix_fmts = out_yuv9_lowpass_pix_fmts;
379     else if (depth == 10 && ncomp > 2)
380         out_pix_fmts = out_yuv10_lowpass_pix_fmts;
381     else if (depth == 12 && ncomp > 2)
382         out_pix_fmts = out_yuv12_lowpass_pix_fmts;
383     else
384         return AVERROR(EAGAIN);
385     if ((ret = ff_formats_ref(ff_make_format_list(out_pix_fmts), &ctx->outputs[0]->in_formats)) < 0)
386         return ret;
387
388     return 0;
389 }
390
391 static void envelope_instant16(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
392 {
393     const int dst_linesize = out->linesize[component] / 2;
394     const int bg = s->bg_color[component] * (s->max / 256);
395     const int limit = s->max - 1;
396     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
397     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
398     const int start = s->estart[plane];
399     const int end = s->eend[plane];
400     uint16_t *dst;
401     int x, y;
402
403     if (s->mode) {
404         for (x = offset; x < offset + dst_w; x++) {
405             for (y = start; y < end; y++) {
406                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
407                 if (dst[0] != bg) {
408                     dst[0] = limit;
409                     break;
410                 }
411             }
412             for (y = end - 1; y >= start; y--) {
413                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
414                 if (dst[0] != bg) {
415                     dst[0] = limit;
416                     break;
417                 }
418             }
419         }
420     } else {
421         for (y = offset; y < offset + dst_h; y++) {
422             dst = (uint16_t *)out->data[component] + y * dst_linesize;
423             for (x = start; x < end; x++) {
424                 if (dst[x] != bg) {
425                     dst[x] = limit;
426                     break;
427                 }
428             }
429             for (x = end - 1; x >= start; x--) {
430                 if (dst[x] != bg) {
431                     dst[x] = limit;
432                     break;
433                 }
434             }
435         }
436     }
437 }
438
439 static void envelope_instant(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
440 {
441     const int dst_linesize = out->linesize[component];
442     const uint8_t bg = s->bg_color[component];
443     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
444     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
445     const int start = s->estart[plane];
446     const int end = s->eend[plane];
447     uint8_t *dst;
448     int x, y;
449
450     if (s->mode) {
451         for (x = offset; x < offset + dst_w; x++) {
452             for (y = start; y < end; y++) {
453                 dst = out->data[component] + y * dst_linesize + x;
454                 if (dst[0] != bg) {
455                     dst[0] = 255;
456                     break;
457                 }
458             }
459             for (y = end - 1; y >= start; y--) {
460                 dst = out->data[component] + y * dst_linesize + x;
461                 if (dst[0] != bg) {
462                     dst[0] = 255;
463                     break;
464                 }
465             }
466         }
467     } else {
468         for (y = offset; y < offset + dst_h; y++) {
469             dst = out->data[component] + y * dst_linesize;
470             for (x = start; x < end; x++) {
471                 if (dst[x] != bg) {
472                     dst[x] = 255;
473                     break;
474                 }
475             }
476             for (x = end - 1; x >= start; x--) {
477                 if (dst[x] != bg) {
478                     dst[x] = 255;
479                     break;
480                 }
481             }
482         }
483     }
484 }
485
486 static void envelope_peak16(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
487 {
488     const int dst_linesize = out->linesize[component] / 2;
489     const int bg = s->bg_color[component] * (s->max / 256);
490     const int limit = s->max - 1;
491     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
492     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
493     const int start = s->estart[plane];
494     const int end = s->eend[plane];
495     int *emax = s->emax[plane][component];
496     int *emin = s->emin[plane][component];
497     uint16_t *dst;
498     int x, y;
499
500     if (s->mode) {
501         for (x = offset; x < offset + dst_w; x++) {
502             for (y = start; y < end && y < emin[x - offset]; y++) {
503                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
504                 if (dst[0] != bg) {
505                     emin[x - offset] = y;
506                     break;
507                 }
508             }
509             for (y = end - 1; y >= start && y >= emax[x - offset]; y--) {
510                 dst = (uint16_t *)out->data[component] + y * dst_linesize + x;
511                 if (dst[0] != bg) {
512                     emax[x - offset] = y;
513                     break;
514                 }
515             }
516         }
517
518         if (s->envelope == 3)
519             envelope_instant16(s, out, plane, component, offset);
520
521         for (x = offset; x < offset + dst_w; x++) {
522             dst = (uint16_t *)out->data[component] + emin[x - offset] * dst_linesize + x;
523             dst[0] = limit;
524             dst = (uint16_t *)out->data[component] + emax[x - offset] * dst_linesize + x;
525             dst[0] = limit;
526         }
527     } else {
528         for (y = offset; y < offset + dst_h; y++) {
529             dst = (uint16_t *)out->data[component] + y * dst_linesize;
530             for (x = start; x < end && x < emin[y - offset]; x++) {
531                 if (dst[x] != bg) {
532                     emin[y - offset] = x;
533                     break;
534                 }
535             }
536             for (x = end - 1; x >= start && x >= emax[y - offset]; x--) {
537                 if (dst[x] != bg) {
538                     emax[y - offset] = x;
539                     break;
540                 }
541             }
542         }
543
544         if (s->envelope == 3)
545             envelope_instant16(s, out, plane, component, offset);
546
547         for (y = offset; y < offset + dst_h; y++) {
548             dst = (uint16_t *)out->data[component] + y * dst_linesize + emin[y - offset];
549             dst[0] = limit;
550             dst = (uint16_t *)out->data[component] + y * dst_linesize + emax[y - offset];
551             dst[0] = limit;
552         }
553     }
554 }
555
556 static void envelope_peak(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
557 {
558     const int dst_linesize = out->linesize[component];
559     const int bg = s->bg_color[component];
560     const int dst_h = s->display == PARADE ? out->height / s->acomp : out->height;
561     const int dst_w = s->display == PARADE ? out->width / s->acomp : out->width;
562     const int start = s->estart[plane];
563     const int end = s->eend[plane];
564     int *emax = s->emax[plane][component];
565     int *emin = s->emin[plane][component];
566     uint8_t *dst;
567     int x, y;
568
569     if (s->mode) {
570         for (x = offset; x < offset + dst_w; x++) {
571             for (y = start; y < end && y < emin[x - offset]; y++) {
572                 dst = out->data[component] + y * dst_linesize + x;
573                 if (dst[0] != bg) {
574                     emin[x - offset] = y;
575                     break;
576                 }
577             }
578             for (y = end - 1; y >= start && y >= emax[x - offset]; y--) {
579                 dst = out->data[component] + y * dst_linesize + x;
580                 if (dst[0] != bg) {
581                     emax[x - offset] = y;
582                     break;
583                 }
584             }
585         }
586
587         if (s->envelope == 3)
588             envelope_instant(s, out, plane, component, offset);
589
590         for (x = offset; x < offset + dst_w; x++) {
591             dst = out->data[component] + emin[x - offset] * dst_linesize + x;
592             dst[0] = 255;
593             dst = out->data[component] + emax[x - offset] * dst_linesize + x;
594             dst[0] = 255;
595         }
596     } else {
597         for (y = offset; y < offset + dst_h; y++) {
598             dst = out->data[component] + y * dst_linesize;
599             for (x = start; x < end && x < emin[y - offset]; x++) {
600                 if (dst[x] != bg) {
601                     emin[y - offset] = x;
602                     break;
603                 }
604             }
605             for (x = end - 1; x >= start && x >= emax[y - offset]; x--) {
606                 if (dst[x] != bg) {
607                     emax[y - offset] = x;
608                     break;
609                 }
610             }
611         }
612
613         if (s->envelope == 3)
614             envelope_instant(s, out, plane, component, offset);
615
616         for (y = offset; y < offset + dst_h; y++) {
617             dst = out->data[component] + y * dst_linesize + emin[y - offset];
618             dst[0] = 255;
619             dst = out->data[component] + y * dst_linesize + emax[y - offset];
620             dst[0] = 255;
621         }
622     }
623 }
624
625 static void envelope16(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
626 {
627     if (s->envelope == 0) {
628         return;
629     } else if (s->envelope == 1) {
630         envelope_instant16(s, out, plane, component, offset);
631     } else {
632         envelope_peak16(s, out, plane, component, offset);
633     }
634 }
635
636 static void envelope(WaveformContext *s, AVFrame *out, int plane, int component, int offset)
637 {
638     if (s->envelope == 0) {
639         return;
640     } else if (s->envelope == 1) {
641         envelope_instant(s, out, plane, component, offset);
642     } else {
643         envelope_peak(s, out, plane, component, offset);
644     }
645 }
646
647 static void update16(uint16_t *target, int max, int intensity, int limit)
648 {
649     if (*target <= max)
650         *target += intensity;
651     else
652         *target = limit;
653 }
654
655 static void update(uint8_t *target, int max, int intensity)
656 {
657     if (*target <= max)
658         *target += intensity;
659     else
660         *target = 255;
661 }
662
663 static void update_cr(uint8_t *target, int unused, int intensity)
664 {
665     if (*target - intensity > 0)
666         *target -= intensity;
667     else
668         *target = 0;
669 }
670
671 static void update16_cr(uint16_t *target, int unused, int intensity, int limit)
672 {
673     if (*target - intensity > 0)
674         *target -= intensity;
675     else
676         *target = 0;
677 }
678
679 static av_always_inline void lowpass16(WaveformContext *s,
680                                        AVFrame *in, AVFrame *out,
681                                        int component, int intensity,
682                                        int offset_y, int offset_x,
683                                        int column, int mirror,
684                                        int jobnr, int nb_jobs)
685 {
686     const int plane = s->desc->comp[component].plane;
687     const int dplane = (s->rgb || s->display == OVERLAY) ? plane : 0;
688     const int shift_w = s->shift_w[component];
689     const int shift_h = s->shift_h[component];
690     const int src_linesize = in->linesize[plane] / 2;
691     const int dst_linesize = out->linesize[dplane] / 2;
692     const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
693     const int limit = s->max - 1;
694     const int max = limit - intensity;
695     const int src_h = AV_CEIL_RSHIFT(in->height, shift_h);
696     const int src_w = AV_CEIL_RSHIFT(in->width, shift_w);
697     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
698     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
699     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
700     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
701     const int step = column ? 1 << shift_w : 1 << shift_h;
702     const uint16_t *src_data = (const uint16_t *)in->data[plane] + sliceh_start * src_linesize;
703     uint16_t *dst_data = (uint16_t *)out->data[dplane] + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
704     uint16_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
705     uint16_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
706     const uint16_t *p;
707     int y;
708
709     if (!column && mirror)
710         dst_data += s->size;
711
712     for (y = sliceh_start; y < sliceh_end; y++) {
713         const uint16_t *src_data_end = src_data + slicew_end;
714         uint16_t *dst = dst_line + slicew_start * step;
715
716         for (p = src_data + slicew_start; p < src_data_end; p++) {
717             uint16_t *target;
718             int i = 0, v = FFMIN(*p, limit);
719
720             if (column) {
721                 do {
722                     target = dst++ + dst_signed_linesize * v;
723                     update16(target, max, intensity, limit);
724                 } while (++i < step);
725             } else {
726                 uint16_t *row = dst_data;
727                 do {
728                     if (mirror)
729                         target = row - v - 1;
730                     else
731                         target = row + v;
732                     update16(target, max, intensity, limit);
733                     row += dst_linesize;
734                 } while (++i < step);
735             }
736         }
737         src_data += src_linesize;
738         dst_data += dst_linesize * step;
739     }
740
741     if (s->display != OVERLAY && column && !s->rgb) {
742         const int mult = s->max / 256;
743         const int bg = s->bg_color[0] * mult;
744         const int t0 = s->tint[0];
745         const int t1 = s->tint[1];
746         uint16_t *dst0, *dst1;
747         const uint16_t *src;
748         int x;
749
750         src  = (const uint16_t *)(out->data[0]) + offset_y * dst_linesize + offset_x;
751         dst0 = (uint16_t *)(out->data[1]) + offset_y * dst_linesize + offset_x;
752         dst1 = (uint16_t *)(out->data[2]) + offset_y * dst_linesize + offset_x;
753         for (y = 0; y < s->max; y++) {
754             for (x = slicew_start * step; x < slicew_end * step; x++) {
755                 if (src[x] != bg) {
756                     dst0[x] = t0;
757                     dst1[x] = t1;
758                 }
759             }
760
761             src  += dst_linesize;
762             dst0 += dst_linesize;
763             dst1 += dst_linesize;
764         }
765     } else if (s->display != OVERLAY && !s->rgb) {
766         const int mult = s->max / 256;
767         const int bg = s->bg_color[0] * mult;
768         const int t0 = s->tint[0];
769         const int t1 = s->tint[1];
770         uint16_t *dst0, *dst1;
771         const uint16_t *src;
772         int x;
773
774         src  = (const uint16_t *)out->data[0] + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
775         dst0 = (uint16_t *)(out->data[1]) + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
776         dst1 = (uint16_t *)(out->data[2]) + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
777         for (y = sliceh_start * step; y < sliceh_end * step; y++) {
778             for (x = 0; x < s->max; x++) {
779                 if (src[x] != bg) {
780                     dst0[x] = t0;
781                     dst1[x] = t1;
782                 }
783             }
784
785             src  += dst_linesize;
786             dst0 += dst_linesize;
787             dst1 += dst_linesize;
788         }
789     }
790 }
791
792 #define LOWPASS16_FUNC(name, column, mirror)        \
793 static int lowpass16_##name(AVFilterContext *ctx,   \
794                              void *arg, int jobnr,  \
795                              int nb_jobs)           \
796 {                                                   \
797     WaveformContext *s = ctx->priv;                 \
798     ThreadData *td = arg;                           \
799     AVFrame *in = td->in;                           \
800     AVFrame *out = td->out;                         \
801     int component = td->component;                  \
802     int offset_y = td->offset_y;                    \
803     int offset_x = td->offset_x;                    \
804                                                     \
805     lowpass16(s, in, out, component, s->intensity,  \
806               offset_y, offset_x, column, mirror,   \
807               jobnr, nb_jobs);                      \
808                                                     \
809     return 0;                                       \
810 }
811
812 LOWPASS16_FUNC(column_mirror, 1, 1)
813 LOWPASS16_FUNC(column,        1, 0)
814 LOWPASS16_FUNC(row_mirror,    0, 1)
815 LOWPASS16_FUNC(row,           0, 0)
816
817 static av_always_inline void lowpass(WaveformContext *s,
818                                      AVFrame *in, AVFrame *out,
819                                      int component, int intensity,
820                                      int offset_y, int offset_x,
821                                      int column, int mirror,
822                                      int jobnr, int nb_jobs)
823 {
824     const int plane = s->desc->comp[component].plane;
825     const int dplane = (s->rgb || s->display == OVERLAY) ? plane : 0;
826     const int shift_w = s->shift_w[component];
827     const int shift_h = s->shift_h[component];
828     const int src_linesize = in->linesize[plane];
829     const int dst_linesize = out->linesize[dplane];
830     const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
831     const int max = 255 - intensity;
832     const int src_h = AV_CEIL_RSHIFT(in->height, shift_h);
833     const int src_w = AV_CEIL_RSHIFT(in->width, shift_w);
834     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
835     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
836     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
837     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
838     const int step = column ? 1 << shift_w : 1 << shift_h;
839     const uint8_t *src_data = in->data[plane] + sliceh_start * src_linesize;
840     uint8_t *dst_data = out->data[dplane] + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
841     uint8_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
842     uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
843     const uint8_t *p;
844     int y;
845
846     if (!column && mirror)
847         dst_data += s->size;
848
849     for (y = sliceh_start; y < sliceh_end; y++) {
850         const uint8_t *src_data_end = src_data + slicew_end;
851         uint8_t *dst = dst_line + slicew_start * step;
852
853         for (p = src_data + slicew_start; p < src_data_end; p++) {
854             uint8_t *target;
855             int i = 0;
856
857             if (column) {
858                 do {
859                     target = dst++ + dst_signed_linesize * *p;
860                     update(target, max, intensity);
861                 } while (++i < step);
862             } else {
863                 uint8_t *row = dst_data;
864                 do {
865                     if (mirror)
866                         target = row - *p - 1;
867                     else
868                         target = row + *p;
869                     update(target, max, intensity);
870                     row += dst_linesize;
871                 } while (++i < step);
872             }
873         }
874         src_data += src_linesize;
875         dst_data += dst_linesize * step;
876     }
877
878     if (s->display != OVERLAY && column && !s->rgb) {
879         const int bg = s->bg_color[0];
880         const int dst_h = 256;
881         const int t0 = s->tint[0];
882         const int t1 = s->tint[1];
883         uint8_t *dst0, *dst1;
884         const uint8_t *src;
885         int x;
886
887         src  = out->data[0] + offset_y * dst_linesize + offset_x;
888         dst0 = out->data[1] + offset_y * dst_linesize + offset_x;
889         dst1 = out->data[2] + offset_y * dst_linesize + offset_x;
890         for (y = 0; y < dst_h; y++) {
891             for (x = slicew_start * step; x < slicew_end * step; x++) {
892                 if (src[x] != bg) {
893                     dst0[x] = t0;
894                     dst1[x] = t1;
895                 }
896             }
897
898             src  += dst_linesize;
899             dst0 += dst_linesize;
900             dst1 += dst_linesize;
901         }
902     } else if (s->display != OVERLAY && !s->rgb) {
903         const int bg = s->bg_color[0];
904         const int dst_w = 256;
905         const int t0 = s->tint[0];
906         const int t1 = s->tint[1];
907         uint8_t *dst0, *dst1;
908         const uint8_t *src;
909         int x;
910
911         src  = out->data[0] + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
912         dst0 = out->data[1] + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
913         dst1 = out->data[2] + (offset_y + sliceh_start * step) * dst_linesize + offset_x;
914         for (y = sliceh_start * step; y < sliceh_end * step; y++) {
915             for (x = 0; x < dst_w; x++) {
916                 if (src[x] != bg) {
917                     dst0[x] = t0;
918                     dst1[x] = t1;
919                 }
920             }
921
922             src  += dst_linesize;
923             dst0 += dst_linesize;
924             dst1 += dst_linesize;
925         }
926     }
927 }
928
929 #define LOWPASS_FUNC(name, column, mirror)        \
930 static int lowpass_##name(AVFilterContext *ctx,   \
931                           void *arg, int jobnr,   \
932                           int nb_jobs)            \
933 {                                                 \
934     WaveformContext *s = ctx->priv;               \
935     ThreadData *td = arg;                         \
936     AVFrame *in = td->in;                         \
937     AVFrame *out = td->out;                       \
938     int component = td->component;                \
939     int offset_y = td->offset_y;                  \
940     int offset_x = td->offset_x;                  \
941                                                   \
942     lowpass(s, in, out, component, s->intensity,  \
943             offset_y, offset_x, column, mirror,   \
944             jobnr, nb_jobs);                      \
945                                                   \
946     return 0;                                     \
947 }
948
949 LOWPASS_FUNC(column_mirror, 1, 1)
950 LOWPASS_FUNC(column,        1, 0)
951 LOWPASS_FUNC(row_mirror,    0, 1)
952 LOWPASS_FUNC(row,           0, 0)
953
954 static av_always_inline void flat16(WaveformContext *s,
955                                     AVFrame *in, AVFrame *out,
956                                     int component, int intensity,
957                                     int offset_y, int offset_x,
958                                     int column, int mirror,
959                                     int jobnr, int nb_jobs)
960 {
961     const int plane = s->desc->comp[component].plane;
962     const int c0_linesize = in->linesize[ plane + 0 ] / 2;
963     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
964     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
965     const int c0_shift_w = s->shift_w[ component + 0 ];
966     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
967     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
968     const int c0_shift_h = s->shift_h[ component + 0 ];
969     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
970     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
971     const int d0_linesize = out->linesize[ plane + 0 ] / 2;
972     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
973     const int limit = s->max - 1;
974     const int max = limit - intensity;
975     const int mid = s->max / 2;
976     const int src_h = in->height;
977     const int src_w = in->width;
978     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
979     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
980     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
981     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
982     int x, y;
983
984     if (column) {
985         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
986         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
987
988         for (x = slicew_start; x < slicew_end; x++) {
989             const uint16_t *c0_data = (uint16_t *)in->data[plane + 0];
990             const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
991             const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
992             uint16_t *d0_data = (uint16_t *)(out->data[plane]) + offset_y * d0_linesize + offset_x;
993             uint16_t *d1_data = (uint16_t *)(out->data[(plane + 1) % s->ncomp]) + offset_y * d1_linesize + offset_x;
994             uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
995             uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
996             uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
997             uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
998
999             for (y = 0; y < src_h; y++) {
1000                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + s->max;
1001                 const int c1 = FFMIN(FFABS(c1_data[x >> c1_shift_w] - mid) + FFABS(c2_data[x >> c2_shift_w] - mid), limit);
1002                 uint16_t *target;
1003
1004                 target = d0 + x + d0_signed_linesize * c0;
1005                 update16(target, max, intensity, limit);
1006                 target = d1 + x + d1_signed_linesize * (c0 - c1);
1007                 update16(target, max, intensity, limit);
1008                 target = d1 + x + d1_signed_linesize * (c0 + c1);
1009                 update16(target, max, intensity, limit);
1010
1011                 if (!c0_shift_h || (y & c0_shift_h))
1012                     c0_data += c0_linesize;
1013                 if (!c1_shift_h || (y & c1_shift_h))
1014                     c1_data += c1_linesize;
1015                 if (!c2_shift_h || (y & c2_shift_h))
1016                     c2_data += c2_linesize;
1017                 d0_data += d0_linesize;
1018                 d1_data += d1_linesize;
1019             }
1020         }
1021     } else {
1022         const uint16_t *c0_data = (uint16_t *)(in->data[plane]) +                  (sliceh_start >> c0_shift_h) * c0_linesize;
1023         const uint16_t *c1_data = (uint16_t *)(in->data[(plane + 1) % s->ncomp]) + (sliceh_start >> c1_shift_h) * c1_linesize;
1024         const uint16_t *c2_data = (uint16_t *)(in->data[(plane + 2) % s->ncomp]) + (sliceh_start >> c2_shift_h) * c2_linesize;
1025         uint16_t *d0_data = (uint16_t *)(out->data[plane]) + (offset_y + sliceh_start) * d0_linesize + offset_x;
1026         uint16_t *d1_data = (uint16_t *)(out->data[(plane + 1) % s->ncomp]) + (offset_y + sliceh_start) * d1_linesize + offset_x;
1027
1028         if (mirror) {
1029             d0_data += s->size - 1;
1030             d1_data += s->size - 1;
1031         }
1032
1033         for (y = sliceh_start; y < sliceh_end; y++) {
1034             for (x = 0; x < src_w; x++) {
1035                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + s->max;
1036                 const int c1 = FFMIN(FFABS(c1_data[x >> c1_shift_w] - mid) + FFABS(c2_data[x >> c2_shift_w] - mid), limit);
1037                 uint16_t *target;
1038
1039                 if (mirror) {
1040                     target = d0_data - c0;
1041                     update16(target, max, intensity, limit);
1042                     target = d1_data - (c0 - c1);
1043                     update16(target, max, intensity, limit);
1044                     target = d1_data - (c0 + c1);
1045                     update16(target, max, intensity, limit);
1046                 } else {
1047                     target = d0_data + c0;
1048                     update16(target, max, intensity, limit);
1049                     target = d1_data + (c0 - c1);
1050                     update16(target, max, intensity, limit);
1051                     target = d1_data + (c0 + c1);
1052                     update16(target, max, intensity, limit);
1053                 }
1054             }
1055
1056             if (!c0_shift_h || (y & c0_shift_h))
1057                 c0_data += c0_linesize;
1058             if (!c1_shift_h || (y & c1_shift_h))
1059                 c1_data += c1_linesize;
1060             if (!c2_shift_h || (y & c2_shift_h))
1061                 c2_data += c2_linesize;
1062             d0_data += d0_linesize;
1063             d1_data += d1_linesize;
1064         }
1065     }
1066 }
1067
1068 #define FLAT16_FUNC(name, column, mirror)        \
1069 static int flat16_##name(AVFilterContext *ctx,   \
1070                          void *arg, int jobnr,   \
1071                          int nb_jobs)            \
1072 {                                                \
1073     WaveformContext *s = ctx->priv;              \
1074     ThreadData *td = arg;                        \
1075     AVFrame *in = td->in;                        \
1076     AVFrame *out = td->out;                      \
1077     int component = td->component;               \
1078     int offset_y = td->offset_y;                 \
1079     int offset_x = td->offset_x;                 \
1080                                                  \
1081     flat16(s, in, out, component, s->intensity,  \
1082            offset_y, offset_x, column, mirror,   \
1083            jobnr, nb_jobs);                      \
1084                                                  \
1085     return 0;                                    \
1086 }
1087
1088 FLAT16_FUNC(column_mirror, 1, 1)
1089 FLAT16_FUNC(column,        1, 0)
1090 FLAT16_FUNC(row_mirror,    0, 1)
1091 FLAT16_FUNC(row,           0, 0)
1092
1093 static av_always_inline void flat(WaveformContext *s,
1094                                   AVFrame *in, AVFrame *out,
1095                                   int component, int intensity,
1096                                   int offset_y, int offset_x,
1097                                   int column, int mirror,
1098                                   int jobnr, int nb_jobs)
1099 {
1100     const int plane = s->desc->comp[component].plane;
1101     const int c0_linesize = in->linesize[ plane + 0 ];
1102     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
1103     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
1104     const int c0_shift_w = s->shift_w[ component + 0 ];
1105     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
1106     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
1107     const int c0_shift_h = s->shift_h[ component + 0 ];
1108     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
1109     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
1110     const int d0_linesize = out->linesize[ plane + 0 ];
1111     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
1112     const int max = 255 - intensity;
1113     const int src_h = in->height;
1114     const int src_w = in->width;
1115     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
1116     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
1117     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
1118     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
1119     int x, y;
1120
1121     if (column) {
1122         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
1123         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
1124
1125         for (x = slicew_start; x < slicew_end; x++) {
1126             const uint8_t *c0_data = in->data[plane + 0];
1127             const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];
1128             const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];
1129             uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
1130             uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
1131             uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
1132             uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
1133             uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
1134             uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
1135
1136             for (y = 0; y < src_h; y++) {
1137                 const int c0 = c0_data[x >> c0_shift_w] + 256;
1138                 const int c1 = FFABS(c1_data[x >> c1_shift_w] - 128) + FFABS(c2_data[x >> c2_shift_w] - 128);
1139                 uint8_t *target;
1140
1141                 target = d0 + x + d0_signed_linesize * c0;
1142                 update(target, max, intensity);
1143                 target = d1 + x + d1_signed_linesize * (c0 - c1);
1144                 update(target, max, intensity);
1145                 target = d1 + x + d1_signed_linesize * (c0 + c1);
1146                 update(target, max, intensity);
1147
1148                 if (!c0_shift_h || (y & c0_shift_h))
1149                     c0_data += c0_linesize;
1150                 if (!c1_shift_h || (y & c1_shift_h))
1151                     c1_data += c1_linesize;
1152                 if (!c2_shift_h || (y & c2_shift_h))
1153                     c2_data += c2_linesize;
1154                 d0_data += d0_linesize;
1155                 d1_data += d1_linesize;
1156             }
1157         }
1158     } else {
1159         const uint8_t *c0_data = in->data[plane] +                  (sliceh_start >> c0_shift_h) * c0_linesize;
1160         const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize;
1161         const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize;
1162         uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x;
1163         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x;
1164
1165         if (mirror) {
1166             d0_data += s->size - 1;
1167             d1_data += s->size - 1;
1168         }
1169
1170         for (y = sliceh_start; y < sliceh_end; y++) {
1171             for (x = 0; x < src_w; x++) {
1172                 const int c0 = c0_data[x >> c0_shift_w] + 256;
1173                 const int c1 = FFABS(c1_data[x >> c1_shift_w] - 128) + FFABS(c2_data[x >> c2_shift_w] - 128);
1174                 uint8_t *target;
1175
1176                 if (mirror) {
1177                     target = d0_data - c0;
1178                     update(target, max, intensity);
1179                     target = d1_data - (c0 - c1);
1180                     update(target, max, intensity);
1181                     target = d1_data - (c0 + c1);
1182                     update(target, max, intensity);
1183                 } else {
1184                     target = d0_data + c0;
1185                     update(target, max, intensity);
1186                     target = d1_data + (c0 - c1);
1187                     update(target, max, intensity);
1188                     target = d1_data + (c0 + c1);
1189                     update(target, max, intensity);
1190                 }
1191             }
1192
1193             if (!c0_shift_h || (y & c0_shift_h))
1194                 c0_data += c0_linesize;
1195             if (!c1_shift_h || (y & c1_shift_h))
1196                 c1_data += c1_linesize;
1197             if (!c2_shift_h || (y & c2_shift_h))
1198                 c2_data += c2_linesize;
1199             d0_data += d0_linesize;
1200             d1_data += d1_linesize;
1201         }
1202     }
1203 }
1204
1205 #define FLAT_FUNC(name, column, mirror)        \
1206 static int flat_##name(AVFilterContext *ctx,   \
1207                        void *arg, int jobnr,   \
1208                        int nb_jobs)            \
1209 {                                              \
1210     WaveformContext *s = ctx->priv;            \
1211     ThreadData *td = arg;                      \
1212     AVFrame *in = td->in;                      \
1213     AVFrame *out = td->out;                    \
1214     int component = td->component;             \
1215     int offset_y = td->offset_y;               \
1216     int offset_x = td->offset_x;               \
1217                                                \
1218     flat(s, in, out, component, s->intensity,  \
1219          offset_y, offset_x, column, mirror,   \
1220          jobnr, nb_jobs);                      \
1221                                                \
1222     return 0;                                  \
1223 }
1224
1225 FLAT_FUNC(column_mirror, 1, 1)
1226 FLAT_FUNC(column,        1, 0)
1227 FLAT_FUNC(row_mirror,    0, 1)
1228 FLAT_FUNC(row,           0, 0)
1229
1230 #define AFLAT16(name, update_cb, update_cr, column, mirror)                                                        \
1231 static int name(AVFilterContext *ctx,                                                                              \
1232                 void *arg, int jobnr,                                                                              \
1233                 int nb_jobs)                                                                                       \
1234 {                                                                                                                  \
1235     WaveformContext *s = ctx->priv;                                                                                \
1236     ThreadData *td = arg;                                                                                          \
1237     AVFrame *in = td->in;                                                                                          \
1238     AVFrame *out = td->out;                                                                                        \
1239     int component = td->component;                                                                                 \
1240     int offset_y = td->offset_y;                                                                                   \
1241     int offset_x = td->offset_x;                                                                                   \
1242     const int intensity = s->intensity;                                                                            \
1243     const int plane = s->desc->comp[component].plane;                                                              \
1244     const int c0_linesize = in->linesize[ plane + 0 ] / 2;                                                         \
1245     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;                                              \
1246     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;                                              \
1247     const int c0_shift_w = s->shift_w[ component + 0 ];                                                            \
1248     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];                                                 \
1249     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];                                                 \
1250     const int c0_shift_h = s->shift_h[ component + 0 ];                                                            \
1251     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];                                                 \
1252     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];                                                 \
1253     const int d0_linesize = out->linesize[ plane + 0 ] / 2;                                                        \
1254     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;                                             \
1255     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2;                                             \
1256     const int limit = s->max - 1;                                                                                  \
1257     const int max = limit - intensity;                                                                             \
1258     const int mid = s->max / 2;                                                                                    \
1259     const int src_h = in->height;                                                                                  \
1260     const int src_w = in->width;                                                                                   \
1261     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;                                              \
1262     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;                                        \
1263     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;                                               \
1264     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;                                         \
1265     int x, y;                                                                                                      \
1266                                                                                                                    \
1267     if (column) {                                                                                                  \
1268         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);                                       \
1269         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);                                       \
1270         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);                                       \
1271                                                                                                                    \
1272         for (x = slicew_start; x < slicew_end; x++) {                                                              \
1273             const uint16_t *c0_data = (uint16_t *)in->data[plane + 0];                                             \
1274             const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];                                \
1275             const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];                                \
1276             uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;                  \
1277             uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; \
1278             uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; \
1279             uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);                               \
1280             uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);                                             \
1281             uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);                               \
1282             uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);                                             \
1283             uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);                               \
1284             uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data);                                             \
1285                                                                                                                    \
1286             for (y = 0; y < src_h; y++) {                                                                          \
1287                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid;                                       \
1288                 const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid;                                       \
1289                 const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid;                                       \
1290                 uint16_t *target;                                                                                  \
1291                                                                                                                    \
1292                 target = d0 + x + d0_signed_linesize * c0;                                                         \
1293                 update16(target, max, intensity, limit);                                                           \
1294                                                                                                                    \
1295                 target = d1 + x + d1_signed_linesize * (c0 + c1);                                                  \
1296                 update_cb(target, max, intensity, limit);                                                          \
1297                                                                                                                    \
1298                 target = d2 + x + d2_signed_linesize * (c0 + c2);                                                  \
1299                 update_cr(target, max, intensity, limit);                                                          \
1300                                                                                                                    \
1301                 if (!c0_shift_h || (y & c0_shift_h))                                                               \
1302                     c0_data += c0_linesize;                                                                        \
1303                 if (!c1_shift_h || (y & c1_shift_h))                                                               \
1304                     c1_data += c1_linesize;                                                                        \
1305                 if (!c2_shift_h || (y & c2_shift_h))                                                               \
1306                     c2_data += c2_linesize;                                                                        \
1307                 d0_data += d0_linesize;                                                                            \
1308                 d1_data += d1_linesize;                                                                            \
1309                 d2_data += d2_linesize;                                                                            \
1310             }                                                                                                      \
1311         }                                                                                                          \
1312     } else {                                                                                                       \
1313         const uint16_t *c0_data = (uint16_t *)in->data[plane] + (sliceh_start >> c0_shift_h) * c0_linesize;        \
1314         const uint16_t *c1_data = (uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; \
1315         const uint16_t *c2_data = (uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; \
1316         uint16_t *d0_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x;                      \
1317         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x;     \
1318         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x;     \
1319                                                                                                                    \
1320         if (mirror) {                                                                                              \
1321             d0_data += s->size - 1;                                                                                \
1322             d1_data += s->size - 1;                                                                                \
1323             d2_data += s->size - 1;                                                                                \
1324         }                                                                                                          \
1325                                                                                                                    \
1326         for (y = sliceh_start; y < sliceh_end; y++) {                                                              \
1327             for (x = 0; x < src_w; x++) {                                                                          \
1328                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit) + mid;                                       \
1329                 const int c1 = FFMIN(c1_data[x >> c1_shift_w], limit) - mid;                                       \
1330                 const int c2 = FFMIN(c2_data[x >> c2_shift_w], limit) - mid;                                       \
1331                 uint16_t *target;                                                                                  \
1332                                                                                                                    \
1333                 if (mirror) {                                                                                      \
1334                     target = d0_data - c0;                                                                         \
1335                     update16(target, max, intensity, limit);                                                       \
1336                     target = d1_data - (c0 + c1);                                                                  \
1337                     update_cb(target, max, intensity, limit);                                                      \
1338                     target = d2_data - (c0 + c2);                                                                  \
1339                     update_cr(target, max, intensity, limit);                                                      \
1340                 } else {                                                                                           \
1341                     target = d0_data + c0;                                                                         \
1342                     update16(target, max, intensity, limit);                                                       \
1343                     target = d1_data + (c0 + c1);                                                                  \
1344                     update_cb(target, max, intensity, limit);                                                      \
1345                     target = d2_data + (c0 + c2);                                                                  \
1346                     update_cr(target, max, intensity, limit);                                                      \
1347                 }                                                                                                  \
1348             }                                                                                                      \
1349                                                                                                                    \
1350             if (!c0_shift_h || (y & c0_shift_h))                                                                   \
1351                 c0_data += c0_linesize;                                                                            \
1352             if (!c1_shift_h || (y & c1_shift_h))                                                                   \
1353                 c1_data += c1_linesize;                                                                            \
1354             if (!c2_shift_h || (y & c2_shift_h))                                                                   \
1355                 c2_data += c2_linesize;                                                                            \
1356             d0_data += d0_linesize;                                                                                \
1357             d1_data += d1_linesize;                                                                                \
1358             d2_data += d2_linesize;                                                                                \
1359         }                                                                                                          \
1360     }                                                                                                              \
1361     return 0;                                                                                                      \
1362 }
1363
1364 #define AFLAT(name, update_cb, update_cr, column, mirror)                                             \
1365 static int name(AVFilterContext *ctx,                                                                 \
1366                 void *arg, int jobnr,                                                                 \
1367                 int nb_jobs)                                                                          \
1368 {                                                                                                     \
1369     WaveformContext *s = ctx->priv;                                                                   \
1370     ThreadData *td = arg;                                                                             \
1371     AVFrame *in = td->in;                                                                             \
1372     AVFrame *out = td->out;                                                                           \
1373     int component = td->component;                                                                    \
1374     int offset_y = td->offset_y;                                                                      \
1375     int offset_x = td->offset_x;                                                                      \
1376     const int src_h = in->height;                                                                     \
1377     const int src_w = in->width;                                                                      \
1378     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;                                 \
1379     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;                           \
1380     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;                                  \
1381     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;                            \
1382     const int intensity = s->intensity;                                                               \
1383     const int plane = s->desc->comp[component].plane;                                                 \
1384     const int c0_linesize = in->linesize[ plane + 0 ];                                                \
1385     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];                                     \
1386     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];                                     \
1387     const int c0_shift_w = s->shift_w[ component + 0 ];                                               \
1388     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];                                    \
1389     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];                                    \
1390     const int c0_shift_h = s->shift_h[ component + 0 ];                                               \
1391     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];                                    \
1392     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];                                    \
1393     const int d0_linesize = out->linesize[ plane + 0 ];                                               \
1394     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];                                    \
1395     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];                                    \
1396     const int max = 255 - intensity;                                                                  \
1397     int x, y;                                                                                         \
1398                                                                                                       \
1399     if (column) {                                                                                     \
1400         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);                          \
1401         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);                          \
1402         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);                          \
1403                                                                                                       \
1404         for (x = slicew_start; x < slicew_end; x++) {                                                 \
1405             const uint8_t *c0_data = in->data[plane + 0];                                             \
1406             const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp];                                \
1407             const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp];                                \
1408             uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;                  \
1409             uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x; \
1410             uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x; \
1411             uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);                   \
1412             uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);                                 \
1413             uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);                   \
1414             uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);                                 \
1415             uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);                   \
1416             uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);                                 \
1417                                                                                                       \
1418             for (y = 0; y < src_h; y++) {                                                             \
1419                 const int c0 = c0_data[x >> c0_shift_w] + 128;                                        \
1420                 const int c1 = c1_data[x >> c1_shift_w] - 128;                                        \
1421                 const int c2 = c2_data[x >> c2_shift_w] - 128;                                        \
1422                 uint8_t *target;                                                                      \
1423                                                                                                       \
1424                 target = d0 + x + d0_signed_linesize * c0;                                            \
1425                 update(target, max, intensity);                                                       \
1426                                                                                                       \
1427                 target = d1 + x + d1_signed_linesize * (c0 + c1);                                     \
1428                 update_cb(target, max, intensity);                                                    \
1429                                                                                                       \
1430                 target = d2 + x + d2_signed_linesize * (c0 + c2);                                     \
1431                 update_cr(target, max, intensity);                                                    \
1432                                                                                                       \
1433                 if (!c0_shift_h || (y & c0_shift_h))                                                  \
1434                     c0_data += c0_linesize;                                                           \
1435                 if (!c1_shift_h || (y & c1_shift_h))                                                  \
1436                     c1_data += c1_linesize;                                                           \
1437                 if (!c2_shift_h || (y & c2_shift_h))                                                  \
1438                     c2_data += c2_linesize;                                                           \
1439                 d0_data += d0_linesize;                                                               \
1440                 d1_data += d1_linesize;                                                               \
1441                 d2_data += d2_linesize;                                                               \
1442             }                                                                                         \
1443         }                                                                                             \
1444     } else {                                                                                          \
1445         const uint8_t *c0_data = in->data[plane] + (sliceh_start >> c0_shift_h) * c0_linesize;        \
1446         const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize; \
1447         const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize; \
1448         uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x;     \
1449         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x; \
1450         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x; \
1451                                                                                                       \
1452         if (mirror) {                                                                                 \
1453             d0_data += s->size - 1;                                                                   \
1454             d1_data += s->size - 1;                                                                   \
1455             d2_data += s->size - 1;                                                                   \
1456         }                                                                                             \
1457                                                                                                       \
1458         for (y = sliceh_start; y < sliceh_end; y++) {                                                 \
1459             for (x = 0; x < src_w; x++) {                                                             \
1460                 const int c0 = c0_data[x >> c0_shift_w] + 128;                                        \
1461                 const int c1 = c1_data[x >> c1_shift_w] - 128;                                        \
1462                 const int c2 = c2_data[x >> c2_shift_w] - 128;                                        \
1463                 uint8_t *target;                                                                      \
1464                                                                                                       \
1465                 if (mirror) {                                                                         \
1466                     target = d0_data - c0;                                                            \
1467                     update(target, max, intensity);                                                   \
1468                     target = d1_data - (c0 + c1);                                                     \
1469                     update_cb(target, max, intensity);                                                \
1470                     target = d2_data - (c0 + c2);                                                     \
1471                     update_cr(target, max, intensity);                                                \
1472                 } else {                                                                              \
1473                     target = d0_data + c0;                                                            \
1474                     update(target, max, intensity);                                                   \
1475                     target = d1_data + (c0 + c1);                                                     \
1476                     update_cb(target, max, intensity);                                                \
1477                     target = d2_data + (c0 + c2);                                                     \
1478                     update_cr(target, max, intensity);                                                \
1479                 }                                                                                     \
1480             }                                                                                         \
1481                                                                                                       \
1482             if (!c0_shift_h || (y & c0_shift_h))                                                      \
1483                 c0_data += c0_linesize;                                                               \
1484             if (!c1_shift_h || (y & c1_shift_h))                                                      \
1485                 c1_data += c1_linesize;                                                               \
1486             if (!c2_shift_h || (y & c2_shift_h))                                                      \
1487                 c2_data += c2_linesize;                                                               \
1488             d0_data += d0_linesize;                                                                   \
1489             d1_data += d1_linesize;                                                                   \
1490             d2_data += d2_linesize;                                                                   \
1491         }                                                                                             \
1492     }                                                                                                 \
1493     return 0;                                                                                         \
1494 }
1495
1496 AFLAT16(aflat16_row,           update16, update16,    0, 0)
1497 AFLAT16(aflat16_row_mirror,    update16, update16,    0, 1)
1498 AFLAT16(aflat16_column,        update16, update16,    1, 0)
1499 AFLAT16(aflat16_column_mirror, update16, update16,    1, 1)
1500 AFLAT16(xflat16_row,           update16, update16_cr, 0, 0)
1501 AFLAT16(xflat16_row_mirror,    update16, update16_cr, 0, 1)
1502 AFLAT16(xflat16_column,        update16, update16_cr, 1, 0)
1503 AFLAT16(xflat16_column_mirror, update16, update16_cr, 1, 1)
1504 AFLAT16(yflat16_row,           update16_cr, update16_cr, 0, 0)
1505 AFLAT16(yflat16_row_mirror,    update16_cr, update16_cr, 0, 1)
1506 AFLAT16(yflat16_column,        update16_cr, update16_cr, 1, 0)
1507 AFLAT16(yflat16_column_mirror, update16_cr, update16_cr, 1, 1)
1508
1509 AFLAT(aflat_row,           update, update,    0, 0)
1510 AFLAT(aflat_row_mirror,    update, update,    0, 1)
1511 AFLAT(aflat_column,        update, update,    1, 0)
1512 AFLAT(aflat_column_mirror, update, update,    1, 1)
1513 AFLAT(xflat_row,           update, update_cr, 0, 0)
1514 AFLAT(xflat_row_mirror,    update, update_cr, 0, 1)
1515 AFLAT(xflat_column,        update, update_cr, 1, 0)
1516 AFLAT(xflat_column_mirror, update, update_cr, 1, 1)
1517 AFLAT(yflat_row,           update_cr, update_cr, 0, 0)
1518 AFLAT(yflat_row_mirror,    update_cr, update_cr, 0, 1)
1519 AFLAT(yflat_column,        update_cr, update_cr, 1, 0)
1520 AFLAT(yflat_column_mirror, update_cr, update_cr, 1, 1)
1521
1522 static av_always_inline void chroma16(WaveformContext *s,
1523                                       AVFrame *in, AVFrame *out,
1524                                       int component, int intensity,
1525                                       int offset_y, int offset_x,
1526                                       int column, int mirror,
1527                                       int jobnr, int nb_jobs)
1528 {
1529     const int plane = s->desc->comp[component].plane;
1530     const int c0_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
1531     const int c1_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
1532     const int dst_linesize = out->linesize[plane] / 2;
1533     const int limit = s->max - 1;
1534     const int max = limit - intensity;
1535     const int mid = s->max / 2;
1536     const int c0_shift_w = s->shift_w[(component + 1) % s->ncomp];
1537     const int c1_shift_w = s->shift_w[(component + 2) % s->ncomp];
1538     const int c0_shift_h = s->shift_h[(component + 1) % s->ncomp];
1539     const int c1_shift_h = s->shift_h[(component + 2) % s->ncomp];
1540     const int src_h = in->height;
1541     const int src_w = in->width;
1542     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
1543     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
1544     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
1545     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
1546     int x, y;
1547
1548     if (column) {
1549         const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
1550
1551         for (x = slicew_start; x < slicew_end; x++) {
1552             const uint16_t *c0_data = (uint16_t *)in->data[(plane + 1) % s->ncomp];
1553             const uint16_t *c1_data = (uint16_t *)in->data[(plane + 2) % s->ncomp];
1554             uint16_t *dst_data = (uint16_t *)out->data[plane] + offset_y * dst_linesize + offset_x;
1555             uint16_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
1556             uint16_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
1557             uint16_t *dst = dst_line;
1558
1559             for (y = 0; y < src_h; y++) {
1560                 const int sum = FFMIN(FFABS(c0_data[x >> c0_shift_w] - mid) + FFABS(c1_data[x >> c1_shift_w] - mid - 1), limit);
1561                 uint16_t *target;
1562
1563                 target = dst + x + dst_signed_linesize * sum;
1564                 update16(target, max, intensity, limit);
1565
1566                 if (!c0_shift_h || (y & c0_shift_h))
1567                     c0_data += c0_linesize;
1568                 if (!c1_shift_h || (y & c1_shift_h))
1569                     c1_data += c1_linesize;
1570                 dst_data += dst_linesize;
1571             }
1572         }
1573     } else {
1574         const uint16_t *c0_data = (uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c0_shift_h) * c0_linesize;
1575         const uint16_t *c1_data = (uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize;
1576         uint16_t *dst_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * dst_linesize + offset_x;
1577
1578         if (mirror)
1579             dst_data += s->size - 1;
1580         for (y = sliceh_start; y < sliceh_end; y++) {
1581             for (x = 0; x < src_w; x++) {
1582                 const int sum = FFMIN(FFABS(c0_data[x >> c0_shift_w] - mid) + FFABS(c1_data[x >> c1_shift_w] - mid - 1), limit);
1583                 uint16_t *target;
1584
1585                 if (mirror) {
1586                     target = dst_data - sum;
1587                     update16(target, max, intensity, limit);
1588                 } else {
1589                     target = dst_data + sum;
1590                     update16(target, max, intensity, limit);
1591                 }
1592             }
1593
1594             if (!c0_shift_h || (y & c0_shift_h))
1595                 c0_data += c0_linesize;
1596             if (!c1_shift_h || (y & c1_shift_h))
1597                 c1_data += c1_linesize;
1598             dst_data += dst_linesize;
1599         }
1600     }
1601 }
1602
1603 #define CHROMA16_FUNC(name, column, mirror)      \
1604 static int chroma16_##name(AVFilterContext *ctx, \
1605                            void *arg, int jobnr, \
1606                            int nb_jobs)          \
1607 {                                                \
1608     WaveformContext *s = ctx->priv;              \
1609     ThreadData *td = arg;                        \
1610     AVFrame *in = td->in;                        \
1611     AVFrame *out = td->out;                      \
1612     int component = td->component;               \
1613     int offset_y = td->offset_y;                 \
1614     int offset_x = td->offset_x;                 \
1615                                                  \
1616     chroma16(s, in, out, component, s->intensity,\
1617            offset_y, offset_x, column, mirror,   \
1618            jobnr, nb_jobs);                      \
1619                                                  \
1620     return 0;                                    \
1621 }
1622
1623 CHROMA16_FUNC(column_mirror, 1, 1)
1624 CHROMA16_FUNC(column,        1, 0)
1625 CHROMA16_FUNC(row_mirror,    0, 1)
1626 CHROMA16_FUNC(row,           0, 0)
1627
1628 static av_always_inline void chroma(WaveformContext *s,
1629                                     AVFrame *in, AVFrame *out,
1630                                     int component, int intensity,
1631                                     int offset_y, int offset_x,
1632                                     int column, int mirror,
1633                                     int jobnr, int nb_jobs)
1634 {
1635     const int plane = s->desc->comp[component].plane;
1636     const int src_h = in->height;
1637     const int src_w = in->width;
1638     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
1639     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
1640     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
1641     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
1642     const int c0_linesize = in->linesize[(plane + 1) % s->ncomp];
1643     const int c1_linesize = in->linesize[(plane + 2) % s->ncomp];
1644     const int dst_linesize = out->linesize[plane];
1645     const int max = 255 - intensity;
1646     const int c0_shift_w = s->shift_w[(component + 1) % s->ncomp];
1647     const int c1_shift_w = s->shift_w[(component + 2) % s->ncomp];
1648     const int c0_shift_h = s->shift_h[(component + 1) % s->ncomp];
1649     const int c1_shift_h = s->shift_h[(component + 2) % s->ncomp];
1650     int x, y;
1651
1652     if (column) {
1653         const int dst_signed_linesize = dst_linesize * (mirror == 1 ? -1 : 1);
1654
1655         for (x = slicew_start; x < slicew_end; x++) {
1656             const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp];
1657             const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp];
1658             uint8_t *dst_data = out->data[plane] + offset_y * dst_linesize + offset_x;
1659             uint8_t * const dst_bottom_line = dst_data + dst_linesize * (s->size - 1);
1660             uint8_t * const dst_line = (mirror ? dst_bottom_line : dst_data);
1661             uint8_t *dst = dst_line;
1662
1663             for (y = 0; y < src_h; y++) {
1664                 const int sum = FFABS(c0_data[x >> c0_shift_w] - 128) + FFABS(c1_data[x >> c1_shift_w] - 127);
1665                 uint8_t *target;
1666
1667                 target = dst + x + dst_signed_linesize * sum;
1668                 update(target, max, intensity);
1669
1670                 if (!c0_shift_h || (y & c0_shift_h))
1671                     c0_data += c0_linesize;
1672                 if (!c1_shift_h || (y & c1_shift_h))
1673                     c1_data += c1_linesize;
1674                 dst_data += dst_linesize;
1675             }
1676         }
1677     } else {
1678         const uint8_t *c0_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c0_shift_h) * c0_linesize;
1679         const uint8_t *c1_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize;
1680         uint8_t *dst_data = out->data[plane] + (offset_y + sliceh_start) * dst_linesize + offset_x;
1681
1682         if (mirror)
1683             dst_data += s->size - 1;
1684         for (y = sliceh_start; y < sliceh_end; y++) {
1685             for (x = 0; x < src_w; x++) {
1686                 const int sum = FFABS(c0_data[x >> c0_shift_w] - 128) + FFABS(c1_data[x >> c1_shift_w] - 127);
1687                 uint8_t *target;
1688
1689                 if (mirror) {
1690                     target = dst_data - sum;
1691                     update(target, max, intensity);
1692                 } else {
1693                     target = dst_data + sum;
1694                     update(target, max, intensity);
1695                 }
1696             }
1697
1698             if (!c0_shift_h || (y & c0_shift_h))
1699                 c0_data += c0_linesize;
1700             if (!c1_shift_h || (y & c1_shift_h))
1701                 c1_data += c1_linesize;
1702             dst_data += dst_linesize;
1703         }
1704     }
1705 }
1706
1707 #define CHROMA_FUNC(name, column, mirror)        \
1708 static int chroma_##name(AVFilterContext *ctx,   \
1709                          void *arg, int jobnr,   \
1710                          int nb_jobs)            \
1711 {                                                \
1712     WaveformContext *s = ctx->priv;              \
1713     ThreadData *td = arg;                        \
1714     AVFrame *in = td->in;                        \
1715     AVFrame *out = td->out;                      \
1716     int component = td->component;               \
1717     int offset_y = td->offset_y;                 \
1718     int offset_x = td->offset_x;                 \
1719                                                  \
1720     chroma(s, in, out, component, s->intensity,  \
1721            offset_y, offset_x, column, mirror,   \
1722            jobnr, nb_jobs);                      \
1723                                                  \
1724     return 0;                                    \
1725 }
1726
1727 CHROMA_FUNC(column_mirror, 1, 1)
1728 CHROMA_FUNC(column,        1, 0)
1729 CHROMA_FUNC(row_mirror,    0, 1)
1730 CHROMA_FUNC(row,           0, 0)
1731
1732 static av_always_inline void color16(WaveformContext *s,
1733                                      AVFrame *in, AVFrame *out,
1734                                      int component, int intensity,
1735                                      int offset_y, int offset_x,
1736                                      int column, int mirror,
1737                                      int jobnr, int nb_jobs)
1738 {
1739     const int plane = s->desc->comp[component].plane;
1740     const int limit = s->max - 1;
1741     const int src_h = in->height;
1742     const int src_w = in->width;
1743     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
1744     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
1745     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
1746     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
1747     const int c0_linesize = in->linesize[ plane + 0 ] / 2;
1748     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
1749     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
1750     const int c0_shift_h = s->shift_h[ component + 0 ];
1751     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
1752     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
1753     const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0] + (sliceh_start >> c0_shift_h) * c0_linesize;
1754     const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize;
1755     const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize;
1756     const int d0_linesize = out->linesize[ plane + 0 ] / 2;
1757     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
1758     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2;
1759     const int c0_shift_w = s->shift_w[ component + 0 ];
1760     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
1761     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
1762     int x, y;
1763
1764     if (column) {
1765         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
1766         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
1767         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
1768         uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
1769         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
1770         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
1771         uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
1772         uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
1773         uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
1774         uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
1775         uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
1776         uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data);
1777
1778         for (y = 0; y < src_h; y++) {
1779             for (x = slicew_start; x < slicew_end; x++) {
1780                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
1781                 const int c1 = c1_data[x >> c1_shift_w];
1782                 const int c2 = c2_data[x >> c2_shift_w];
1783
1784                 *(d0 + d0_signed_linesize * c0 + x) = c0;
1785                 *(d1 + d1_signed_linesize * c0 + x) = c1;
1786                 *(d2 + d2_signed_linesize * c0 + x) = c2;
1787             }
1788
1789             if (!c0_shift_h || (y & c0_shift_h))
1790                 c0_data += c0_linesize;
1791             if (!c1_shift_h || (y & c1_shift_h))
1792                 c1_data += c1_linesize;
1793             if (!c2_shift_h || (y & c2_shift_h))
1794                 c2_data += c2_linesize;
1795             d0_data += d0_linesize;
1796             d1_data += d1_linesize;
1797             d2_data += d2_linesize;
1798         }
1799     } else {
1800         uint16_t *d0_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x;
1801         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x;
1802         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x;
1803
1804         if (mirror) {
1805             d0_data += s->size - 1;
1806             d1_data += s->size - 1;
1807             d2_data += s->size - 1;
1808         }
1809
1810         for (y = sliceh_start; y < sliceh_end; y++) {
1811             for (x = 0; x < src_w; x++) {
1812                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
1813                 const int c1 = c1_data[x >> c1_shift_w];
1814                 const int c2 = c2_data[x >> c2_shift_w];
1815
1816                 if (mirror) {
1817                     *(d0_data - c0) = c0;
1818                     *(d1_data - c0) = c1;
1819                     *(d2_data - c0) = c2;
1820                 } else {
1821                     *(d0_data + c0) = c0;
1822                     *(d1_data + c0) = c1;
1823                     *(d2_data + c0) = c2;
1824                 }
1825             }
1826
1827             if (!c0_shift_h || (y & c0_shift_h))
1828                 c0_data += c0_linesize;
1829             if (!c1_shift_h || (y & c1_shift_h))
1830                 c1_data += c1_linesize;
1831             if (!c2_shift_h || (y & c2_shift_h))
1832                 c2_data += c2_linesize;
1833             d0_data += d0_linesize;
1834             d1_data += d1_linesize;
1835             d2_data += d2_linesize;
1836         }
1837     }
1838 }
1839
1840 #define COLOR16_FUNC(name, column, mirror)       \
1841 static int color16_##name(AVFilterContext *ctx,  \
1842                           void *arg, int jobnr,  \
1843                           int nb_jobs)           \
1844 {                                                \
1845     WaveformContext *s = ctx->priv;              \
1846     ThreadData *td = arg;                        \
1847     AVFrame *in = td->in;                        \
1848     AVFrame *out = td->out;                      \
1849     int component = td->component;               \
1850     int offset_y = td->offset_y;                 \
1851     int offset_x = td->offset_x;                 \
1852                                                  \
1853     color16(s, in, out, component, s->intensity, \
1854             offset_y, offset_x, column, mirror,  \
1855             jobnr, nb_jobs);                     \
1856                                                  \
1857     return 0;                                    \
1858 }
1859
1860 COLOR16_FUNC(column_mirror, 1, 1)
1861 COLOR16_FUNC(column,        1, 0)
1862 COLOR16_FUNC(row_mirror,    0, 1)
1863 COLOR16_FUNC(row,           0, 0)
1864
1865 static av_always_inline void color(WaveformContext *s,
1866                                    AVFrame *in, AVFrame *out,
1867                                    int component, int intensity,
1868                                    int offset_y, int offset_x,
1869                                    int column, int mirror,
1870                                    int jobnr, int nb_jobs)
1871 {
1872     const int plane = s->desc->comp[component].plane;
1873     const int src_h = in->height;
1874     const int src_w = in->width;
1875     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
1876     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
1877     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
1878     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
1879     const int c0_linesize = in->linesize[ plane + 0 ];
1880     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
1881     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
1882     const int c0_shift_h = s->shift_h[ component + 0 ];
1883     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
1884     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
1885     const uint8_t *c0_data = in->data[plane] +                  (sliceh_start >> c0_shift_h) * c0_linesize;
1886     const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize;
1887     const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize;
1888     const int d0_linesize = out->linesize[ plane + 0 ];
1889     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
1890     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
1891     const int c0_shift_w = s->shift_w[ component + 0 ];
1892     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
1893     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
1894     int x, y;
1895
1896     if (column) {
1897         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
1898         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
1899         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
1900         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
1901         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
1902         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
1903         uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
1904         uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
1905         uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
1906         uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
1907         uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
1908         uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
1909
1910         for (y = 0; y < src_h; y++) {
1911             for (x = slicew_start; x < slicew_end; x++) {
1912                 const int c0 = c0_data[x >> c0_shift_w];
1913                 const int c1 = c1_data[x >> c1_shift_w];
1914                 const int c2 = c2_data[x >> c2_shift_w];
1915
1916                 *(d0 + d0_signed_linesize * c0 + x) = c0;
1917                 *(d1 + d1_signed_linesize * c0 + x) = c1;
1918                 *(d2 + d2_signed_linesize * c0 + x) = c2;
1919             }
1920
1921             if (!c0_shift_h || (y & c0_shift_h))
1922                 c0_data += c0_linesize;
1923             if (!c1_shift_h || (y & c1_shift_h))
1924                 c1_data += c1_linesize;
1925             if (!c2_shift_h || (y & c2_shift_h))
1926                 c2_data += c2_linesize;
1927             d0_data += d0_linesize;
1928             d1_data += d1_linesize;
1929             d2_data += d2_linesize;
1930         }
1931     } else {
1932         uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x;
1933         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x;
1934         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x;
1935
1936         if (mirror) {
1937             d0_data += s->size - 1;
1938             d1_data += s->size - 1;
1939             d2_data += s->size - 1;
1940         }
1941
1942         for (y = sliceh_start; y < sliceh_end; y++) {
1943             for (x = 0; x < src_w; x++) {
1944                 const int c0 = c0_data[x >> c0_shift_w];
1945                 const int c1 = c1_data[x >> c1_shift_w];
1946                 const int c2 = c2_data[x >> c2_shift_w];
1947
1948                 if (mirror) {
1949                     *(d0_data - c0) = c0;
1950                     *(d1_data - c0) = c1;
1951                     *(d2_data - c0) = c2;
1952                 } else {
1953                     *(d0_data + c0) = c0;
1954                     *(d1_data + c0) = c1;
1955                     *(d2_data + c0) = c2;
1956                 }
1957             }
1958
1959             if (!c0_shift_h || (y & c0_shift_h))
1960                 c0_data += c0_linesize;
1961             if (!c1_shift_h || (y & c1_shift_h))
1962                 c1_data += c1_linesize;
1963             if (!c2_shift_h || (y & c2_shift_h))
1964                 c2_data += c2_linesize;
1965             d0_data += d0_linesize;
1966             d1_data += d1_linesize;
1967             d2_data += d2_linesize;
1968         }
1969     }
1970 }
1971
1972 #define COLOR_FUNC(name, column, mirror)       \
1973 static int color_##name(AVFilterContext *ctx,  \
1974                         void *arg, int jobnr,  \
1975                         int nb_jobs)           \
1976 {                                              \
1977     WaveformContext *s = ctx->priv;            \
1978     ThreadData *td = arg;                      \
1979     AVFrame *in = td->in;                      \
1980     AVFrame *out = td->out;                    \
1981     int component = td->component;             \
1982     int offset_y = td->offset_y;               \
1983     int offset_x = td->offset_x;               \
1984                                                \
1985     color(s, in, out, component, s->intensity, \
1986           offset_y, offset_x, column, mirror,  \
1987           jobnr, nb_jobs);                     \
1988                                                \
1989     return 0;                                  \
1990 }
1991
1992 COLOR_FUNC(column_mirror, 1, 1)
1993 COLOR_FUNC(column,        1, 0)
1994 COLOR_FUNC(row_mirror,    0, 1)
1995 COLOR_FUNC(row,           0, 0)
1996
1997 static av_always_inline void acolor16(WaveformContext *s,
1998                                       AVFrame *in, AVFrame *out,
1999                                       int component, int intensity,
2000                                       int offset_y, int offset_x,
2001                                       int column, int mirror,
2002                                       int jobnr, int nb_jobs)
2003 {
2004     const int plane = s->desc->comp[component].plane;
2005     const int limit = s->max - 1;
2006     const int max = limit - intensity;
2007     const int src_h = in->height;
2008     const int src_w = in->width;
2009     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
2010     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
2011     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
2012     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
2013     const int c0_shift_h = s->shift_h[ component + 0 ];
2014     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
2015     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
2016     const int c0_linesize = in->linesize[ plane + 0 ] / 2;
2017     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp] / 2;
2018     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp] / 2;
2019     const uint16_t *c0_data = (const uint16_t *)in->data[plane + 0] + (sliceh_start >> c0_shift_h) * c0_linesize;
2020     const uint16_t *c1_data = (const uint16_t *)in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize;
2021     const uint16_t *c2_data = (const uint16_t *)in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize;
2022     const int d0_linesize = out->linesize[ plane + 0 ] / 2;
2023     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp] / 2;
2024     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp] / 2;
2025     const int c0_shift_w = s->shift_w[ component + 0 ];
2026     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
2027     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
2028     int x, y;
2029
2030     if (column) {
2031         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
2032         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
2033         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
2034         uint16_t *d0_data = (uint16_t *)out->data[plane] + offset_y * d0_linesize + offset_x;
2035         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
2036         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
2037         uint16_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
2038         uint16_t * const d0 = (mirror ? d0_bottom_line : d0_data);
2039         uint16_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
2040         uint16_t * const d1 = (mirror ? d1_bottom_line : d1_data);
2041         uint16_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
2042         uint16_t * const d2 = (mirror ? d2_bottom_line : d2_data);
2043
2044         for (y = 0; y < src_h; y++) {
2045             for (x = slicew_start; x < slicew_end; x++) {
2046                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
2047                 const int c1 = c1_data[x >> c1_shift_w];
2048                 const int c2 = c2_data[x >> c2_shift_w];
2049
2050                 update16(d0 + d0_signed_linesize * c0 + x, max, intensity, limit);
2051                 *(d1 + d1_signed_linesize * c0 + x) = c1;
2052                 *(d2 + d2_signed_linesize * c0 + x) = c2;
2053             }
2054
2055             if (!c0_shift_h || (y & c0_shift_h))
2056                 c0_data += c0_linesize;
2057             if (!c1_shift_h || (y & c1_shift_h))
2058                 c1_data += c1_linesize;
2059             if (!c2_shift_h || (y & c2_shift_h))
2060                 c2_data += c2_linesize;
2061             d0_data += d0_linesize;
2062             d1_data += d1_linesize;
2063             d2_data += d2_linesize;
2064         }
2065     } else {
2066         uint16_t *d0_data = (uint16_t *)out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x;
2067         uint16_t *d1_data = (uint16_t *)out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x;
2068         uint16_t *d2_data = (uint16_t *)out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x;
2069
2070         if (mirror) {
2071             d0_data += s->size - 1;
2072             d1_data += s->size - 1;
2073             d2_data += s->size - 1;
2074         }
2075
2076         for (y = sliceh_start; y < sliceh_end; y++) {
2077             for (x = 0; x < src_w; x++) {
2078                 const int c0 = FFMIN(c0_data[x >> c0_shift_w], limit);
2079                 const int c1 = c1_data[x >> c1_shift_w];
2080                 const int c2 = c2_data[x >> c2_shift_w];
2081
2082                 if (mirror) {
2083                     update16(d0_data - c0, max, intensity, limit);
2084                     *(d1_data - c0) = c1;
2085                     *(d2_data - c0) = c2;
2086                 } else {
2087                     update16(d0_data + c0, max, intensity, limit);
2088                     *(d1_data + c0) = c1;
2089                     *(d2_data + c0) = c2;
2090                 }
2091             }
2092
2093             if (!c0_shift_h || (y & c0_shift_h))
2094                 c0_data += c0_linesize;
2095             if (!c1_shift_h || (y & c1_shift_h))
2096                 c1_data += c1_linesize;
2097             if (!c2_shift_h || (y & c2_shift_h))
2098                 c2_data += c2_linesize;
2099             d0_data += d0_linesize;
2100             d1_data += d1_linesize;
2101             d2_data += d2_linesize;
2102         }
2103     }
2104 }
2105
2106 #define ACOLOR16_FUNC(name, column, mirror)      \
2107 static int acolor16_##name(AVFilterContext *ctx, \
2108                            void *arg, int jobnr, \
2109                            int nb_jobs)          \
2110 {                                                \
2111     WaveformContext *s = ctx->priv;              \
2112     ThreadData *td = arg;                        \
2113     AVFrame *in = td->in;                        \
2114     AVFrame *out = td->out;                      \
2115     int component = td->component;               \
2116     int offset_y = td->offset_y;                 \
2117     int offset_x = td->offset_x;                 \
2118                                                  \
2119     acolor16(s, in, out, component, s->intensity,\
2120              offset_y, offset_x, column, mirror, \
2121              jobnr, nb_jobs);                    \
2122                                                  \
2123     return 0;                                    \
2124 }
2125
2126 ACOLOR16_FUNC(column_mirror, 1, 1)
2127 ACOLOR16_FUNC(column,        1, 0)
2128 ACOLOR16_FUNC(row_mirror,    0, 1)
2129 ACOLOR16_FUNC(row,           0, 0)
2130
2131 static av_always_inline void acolor(WaveformContext *s,
2132                                     AVFrame *in, AVFrame *out,
2133                                     int component, int intensity,
2134                                     int offset_y, int offset_x,
2135                                     int column, int mirror,
2136                                     int jobnr, int nb_jobs)
2137 {
2138     const int plane = s->desc->comp[component].plane;
2139     const int src_h = in->height;
2140     const int src_w = in->width;
2141     const int sliceh_start = !column ? (src_h * jobnr) / nb_jobs : 0;
2142     const int sliceh_end = !column ? (src_h * (jobnr+1)) / nb_jobs : src_h;
2143     const int slicew_start = column ? (src_w * jobnr) / nb_jobs : 0;
2144     const int slicew_end = column ? (src_w * (jobnr+1)) / nb_jobs : src_w;
2145     const int c0_shift_w = s->shift_w[ component + 0 ];
2146     const int c1_shift_w = s->shift_w[(component + 1) % s->ncomp];
2147     const int c2_shift_w = s->shift_w[(component + 2) % s->ncomp];
2148     const int c0_shift_h = s->shift_h[ component + 0 ];
2149     const int c1_shift_h = s->shift_h[(component + 1) % s->ncomp];
2150     const int c2_shift_h = s->shift_h[(component + 2) % s->ncomp];
2151     const int c0_linesize = in->linesize[ plane + 0 ];
2152     const int c1_linesize = in->linesize[(plane + 1) % s->ncomp];
2153     const int c2_linesize = in->linesize[(plane + 2) % s->ncomp];
2154     const uint8_t *c0_data = in->data[plane + 0] + (sliceh_start >> c0_shift_h) * c0_linesize;
2155     const uint8_t *c1_data = in->data[(plane + 1) % s->ncomp] + (sliceh_start >> c1_shift_h) * c1_linesize;
2156     const uint8_t *c2_data = in->data[(plane + 2) % s->ncomp] + (sliceh_start >> c2_shift_h) * c2_linesize;
2157     const int d0_linesize = out->linesize[ plane + 0 ];
2158     const int d1_linesize = out->linesize[(plane + 1) % s->ncomp];
2159     const int d2_linesize = out->linesize[(plane + 2) % s->ncomp];
2160     const int max = 255 - intensity;
2161     int x, y;
2162
2163     if (column) {
2164         const int d0_signed_linesize = d0_linesize * (mirror == 1 ? -1 : 1);
2165         const int d1_signed_linesize = d1_linesize * (mirror == 1 ? -1 : 1);
2166         const int d2_signed_linesize = d2_linesize * (mirror == 1 ? -1 : 1);
2167         uint8_t *d0_data = out->data[plane] + offset_y * d0_linesize + offset_x;
2168         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + offset_y * d1_linesize + offset_x;
2169         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + offset_y * d2_linesize + offset_x;
2170         uint8_t * const d0_bottom_line = d0_data + d0_linesize * (s->size - 1);
2171         uint8_t * const d0 = (mirror ? d0_bottom_line : d0_data);
2172         uint8_t * const d1_bottom_line = d1_data + d1_linesize * (s->size - 1);
2173         uint8_t * const d1 = (mirror ? d1_bottom_line : d1_data);
2174         uint8_t * const d2_bottom_line = d2_data + d2_linesize * (s->size - 1);
2175         uint8_t * const d2 = (mirror ? d2_bottom_line : d2_data);
2176
2177         for (y = 0; y < src_h; y++) {
2178             for (x = slicew_start; x < slicew_end; x++) {
2179                 const int c0 = c0_data[x >> c0_shift_w];
2180                 const int c1 = c1_data[x >> c1_shift_w];
2181                 const int c2 = c2_data[x >> c2_shift_w];
2182
2183                 update(d0 + d0_signed_linesize * c0 + x, max, intensity);
2184                 *(d1 + d1_signed_linesize * c0 + x) = c1;
2185                 *(d2 + d2_signed_linesize * c0 + x) = c2;
2186             }
2187
2188             if (!c0_shift_h || (y & c0_shift_h))
2189                 c0_data += c0_linesize;
2190             if (!c1_shift_h || (y & c1_shift_h))
2191                 c1_data += c1_linesize;
2192             if (!c2_shift_h || (y & c2_shift_h))
2193                 c2_data += c2_linesize;
2194             d0_data += d0_linesize;
2195             d1_data += d1_linesize;
2196             d2_data += d2_linesize;
2197         }
2198     } else {
2199         uint8_t *d0_data = out->data[plane] + (offset_y + sliceh_start) * d0_linesize + offset_x;
2200         uint8_t *d1_data = out->data[(plane + 1) % s->ncomp] + (offset_y + sliceh_start) * d1_linesize + offset_x;
2201         uint8_t *d2_data = out->data[(plane + 2) % s->ncomp] + (offset_y + sliceh_start) * d2_linesize + offset_x;
2202
2203         if (mirror) {
2204             d0_data += s->size - 1;
2205             d1_data += s->size - 1;
2206             d2_data += s->size - 1;
2207         }
2208
2209         for (y = sliceh_start; y < sliceh_end; y++) {
2210             for (x = 0; x < src_w; x++) {
2211                 const int c0 = c0_data[x >> c0_shift_w];
2212                 const int c1 = c1_data[x >> c1_shift_w];
2213                 const int c2 = c2_data[x >> c2_shift_w];
2214
2215                 if (mirror) {
2216                     update(d0_data - c0, max, intensity);
2217                     *(d1_data - c0) = c1;
2218                     *(d2_data - c0) = c2;
2219                 } else {
2220                     update(d0_data + c0, max, intensity);
2221                     *(d1_data + c0) = c1;
2222                     *(d2_data + c0) = c2;
2223                 }
2224             }
2225
2226             if (!c0_shift_h || (y & c0_shift_h))
2227                 c0_data += c0_linesize;
2228             if (!c1_shift_h || (y & c1_shift_h))
2229                 c1_data += c1_linesize;
2230             if (!c2_shift_h || (y & c2_shift_h))
2231                 c2_data += c2_linesize;
2232             d0_data += d0_linesize;
2233             d1_data += d1_linesize;
2234             d2_data += d2_linesize;
2235         }
2236     }
2237 }
2238
2239 #define ACOLOR_FUNC(name, column, mirror)        \
2240 static int acolor_##name(AVFilterContext *ctx,   \
2241                          void *arg, int jobnr,   \
2242                          int nb_jobs)            \
2243 {                                                \
2244     WaveformContext *s = ctx->priv;              \
2245     ThreadData *td = arg;                        \
2246     AVFrame *in = td->in;                        \
2247     AVFrame *out = td->out;                      \
2248     int component = td->component;               \
2249     int offset_y = td->offset_y;                 \
2250     int offset_x = td->offset_x;                 \
2251                                                  \
2252     acolor(s, in, out, component, s->intensity,  \
2253            offset_y, offset_x, column, mirror,   \
2254            jobnr, nb_jobs);                      \
2255                                                  \
2256     return 0;                                    \
2257 }
2258
2259 ACOLOR_FUNC(column_mirror, 1, 1)
2260 ACOLOR_FUNC(column,        1, 0)
2261 ACOLOR_FUNC(row_mirror,    0, 1)
2262 ACOLOR_FUNC(row,           0, 0)
2263
2264 static const uint8_t black_yuva_color[4] = { 0, 127, 127, 255 };
2265 static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 255 };
2266
2267 static const GraticuleLines aflat_digital8[] = {
2268     { { {  "16",  16+128 }, {  "16",  16+128 }, {  "16",  16+128 }, {   "0",   0+128 } } },
2269     { { { "128", 128+128 }, { "128", 128+128 }, { "128", 128+128 }, { "128", 128+128 } } },
2270     { { { "235", 235+128 }, { "240", 240+128 }, { "240", 240+128 }, { "255", 255+128 } } },
2271 };
2272
2273 static const GraticuleLines aflat_digital9[] = {
2274     { { {  "32",  32+256 }, {  "32",  32+256 }, {  "32",  32+256 }, {   "0",   0+256 } } },
2275     { { { "256", 256+256 }, { "256", 256+256 }, { "256", 256+256 }, { "256", 256+256 } } },
2276     { { { "470", 470+256 }, { "480", 480+256 }, { "480", 480+256 }, { "511", 511+256 } } },
2277 };
2278
2279 static const GraticuleLines aflat_digital10[] = {
2280     { { {  "64",  64+512 }, {  "64",  64+512 }, {  "64",  64+512 }, {    "0",    0+512 } } },
2281     { { { "512", 512+512 }, { "512", 512+512 }, { "512", 512+512 }, {  "512",  512+512 } } },
2282     { { { "940", 940+512 }, { "960", 960+512 }, { "960", 960+512 }, { "1023", 1023+512 } } },
2283 };
2284
2285 static const GraticuleLines aflat_digital12[] = {
2286     { { {  "256",  256+2048 }, {  "256",  256+2048 }, {  "256",  256+2048 }, {    "0",    0+2048 } } },
2287     { { { "2048", 2048+2048 }, { "2048", 2048+2048 }, { "2048", 2048+2048 }, { "2048", 2048+2048 } } },
2288     { { { "3760", 3760+2048 }, { "3840", 3840+2048 }, { "3840", 3840+2048 }, { "4095", 4095+2048 } } },
2289 };
2290
2291 static const GraticuleLines aflat_millivolts8[] = {
2292     { { {   "0",  16+128 }, {   "0",  16+128 }, {   "0",  16+128 }, {   "0",   0+128 } } },
2293     { { { "175",  71+128 }, { "175",  72+128 }, { "175",  72+128 }, { "175",  64+128 } } },
2294     { { { "350", 126+128 }, { "350", 128+128 }, { "350", 128+128 }, { "350", 128+128 } } },
2295     { { { "525", 180+128 }, { "525", 184+128 }, { "525", 184+128 }, { "525", 192+128 } } },
2296     { { { "700", 235+128 }, { "700", 240+128 }, { "700", 240+128 }, { "700", 255+128 } } },
2297 };
2298
2299 static const GraticuleLines aflat_millivolts9[] = {
2300     { { {   "0",  32+256 }, {   "0",  32+256 }, {   "0",  32+256 }, {   "0",   0+256 } } },
2301     { { { "175", 142+256 }, { "175", 144+256 }, { "175", 144+256 }, { "175", 128+256 } } },
2302     { { { "350", 251+256 }, { "350", 256+256 }, { "350", 256+256 }, { "350", 256+256 } } },
2303     { { { "525", 361+256 }, { "525", 368+256 }, { "525", 368+256 }, { "525", 384+256 } } },
2304     { { { "700", 470+256 }, { "700", 480+256 }, { "700", 480+256 }, { "700", 511+256 } } },
2305 };
2306
2307 static const GraticuleLines aflat_millivolts10[] = {
2308     { { {   "0",  64+512 }, {   "0",  64+512 }, {   "0",  64+512 }, {   "0",    0+512 } } },
2309     { { { "175", 283+512 }, { "175", 288+512 }, { "175", 288+512 }, { "175",  256+512 } } },
2310     { { { "350", 502+512 }, { "350", 512+512 }, { "350", 512+512 }, { "350",  512+512 } } },
2311     { { { "525", 721+512 }, { "525", 736+512 }, { "525", 736+512 }, { "525",  768+512 } } },
2312     { { { "700", 940+512 }, { "700", 960+512 }, { "700", 960+512 }, { "700", 1023+512 } } },
2313 };
2314
2315 static const GraticuleLines aflat_millivolts12[] = {
2316     { { {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",    0+2048 } } },
2317     { { { "175", 1132+2048 }, { "175", 1152+2048 }, { "175", 1152+2048 }, { "175", 1024+2048 } } },
2318     { { { "350", 2008+2048 }, { "350", 2048+2048 }, { "350", 2048+2048 }, { "350", 2048+2048 } } },
2319     { { { "525", 2884+2048 }, { "525", 2944+2048 }, { "525", 2944+2048 }, { "525", 3072+2048 } } },
2320     { { { "700", 3760+2048 }, { "700", 3840+2048 }, { "700", 3840+2048 }, { "700", 4095+2048 } } },
2321 };
2322
2323 static const GraticuleLines aflat_ire8[] = {
2324     { { { "-25", -39+128 }, { "-25", -40+128 }, { "-25", -40+128 }, { "-25", -64+128 } } },
2325     { { {   "0",  16+128 }, {   "0",  16+128 }, {   "0",  16+128 }, {   "0",   0+128 } } },
2326     { { {  "25",  71+128 }, {  "25",  72+128 }, {  "25",  72+128 }, {  "25",  64+128 } } },
2327     { { {  "50", 126+128 }, {  "50", 128+128 }, {  "50", 128+128 }, {  "50", 128+128 } } },
2328     { { {  "75", 180+128 }, {  "75", 184+128 }, {  "75", 184+128 }, {  "75", 192+128 } } },
2329     { { { "100", 235+128 }, { "100", 240+128 }, { "100", 240+128 }, { "100", 256+128 } } },
2330     { { { "125", 290+128 }, { "125", 296+128 }, { "125", 296+128 }, { "125", 320+128 } } },
2331 };
2332
2333 static const GraticuleLines aflat_ire9[] = {
2334     { { { "-25", -78+256 }, { "-25", -80+256 }, { "-25", -80+256 }, { "-25",-128+256 } } },
2335     { { {   "0",  32+256 }, {   "0",  32+256 }, {   "0",  32+256 }, {   "0",   0+256 } } },
2336     { { {  "25", 142+256 }, {  "25", 144+256 }, {  "25", 144+256 }, {  "25", 128+256 } } },
2337     { { {  "50", 251+256 }, {  "50", 256+256 }, {  "50", 256+256 }, {  "50", 256+256 } } },
2338     { { {  "75", 361+256 }, {  "75", 368+256 }, {  "75", 368+256 }, {  "75", 384+256 } } },
2339     { { { "100", 470+256 }, { "100", 480+256 }, { "100", 480+256 }, { "100", 512+256 } } },
2340     { { { "125", 580+256 }, { "125", 592+256 }, { "125", 592+256 }, { "125", 640+256 } } },
2341 };
2342
2343 static const GraticuleLines aflat_ire10[] = {
2344     { { { "-25",-156+512 }, { "-25",-160+512 }, { "-25",-160+512 }, { "-25", -256+512 } } },
2345     { { {   "0",  64+512 }, {   "0",  64+512 }, {  "0",   64+512 }, {   "0",    0+512 } } },
2346     { { {  "25", 283+512 }, {  "25", 288+512 }, {  "25", 288+512 }, {  "25",  256+512 } } },
2347     { { {  "50", 502+512 }, {  "50", 512+512 }, {  "50", 512+512 }, {  "50",  512+512 } } },
2348     { { {  "75", 721+512 }, {  "75", 736+512 }, {  "75", 736+512 }, {  "75",  768+512 } } },
2349     { { { "100", 940+512 }, { "100", 960+512 }, { "100", 960+512 }, { "100", 1024+512 } } },
2350     { { { "125",1160+512 }, { "125",1184+512 }, { "125",1184+512 }, { "125", 1280+512 } } },
2351 };
2352
2353 static const GraticuleLines aflat_ire12[] = {
2354     { { { "-25", -624+2048 }, { "-25", -640+2048 }, { "-25", -640+2048 }, { "-25",-1024+2048 } } },
2355     { { {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",  256+2048 }, {   "0",    0+2048 } } },
2356     { { {  "25", 1132+2048 }, {  "25", 1152+2048 }, {  "25", 1152+2048 }, {  "25", 1024+2048 } } },
2357     { { {  "50", 2008+2048 }, {  "50", 2048+2048 }, {  "50", 2048+2048 }, {  "50", 2048+2048 } } },
2358     { { {  "75", 2884+2048 }, {  "75", 2944+2048 }, {  "75", 2944+2048 }, {  "75", 3072+2048 } } },
2359     { { { "100", 3760+2048 }, { "100", 3840+2048 }, { "100", 3840+2048 }, { "100", 4096+2048 } } },
2360     { { { "125", 4640+2048 }, { "125", 4736+2048 }, { "125", 4736+2048 }, { "125", 5120+2048 } } },
2361 };
2362
2363 static const GraticuleLines flat_digital8[] = {
2364     { { {  "16",  16+256 }, {  "16",  16+256 }, {  "16",  16+256 }, {   "0",   0+256 } } },
2365     { { { "128", 128+256 }, { "128", 128+256 }, { "128", 128+256 }, { "128", 128+256 } } },
2366     { { { "235", 235+256 }, { "240", 240+256 }, { "240", 240+256 }, { "255", 255+256 } } },
2367 };
2368
2369 static const GraticuleLines flat_digital9[] = {
2370     { { {  "32",  32+512 }, {  "32",  32+512 }, {  "32",  32+512 }, {   "0",   0+512 } } },
2371     { { { "256", 256+512 }, { "256", 256+512 }, { "256", 256+512 }, { "256", 256+512 } } },
2372     { { { "470", 470+512 }, { "480", 480+512 }, { "480", 480+512 }, { "511", 511+512 } } },
2373 };
2374
2375 static const GraticuleLines flat_digital10[] = {
2376     { { {  "64",  64+1024 }, {  "64",  64+1024 }, {  "64",  64+1024 }, {    "0",    0+1024 } } },
2377     { { { "512", 512+1024 }, { "512", 512+1024 }, { "512", 512+1024 }, {  "512",  512+1024 } } },
2378     { { { "940", 940+1024 }, { "960", 960+1024 }, { "960", 960+1024 }, { "1023", 1023+1024 } } },
2379 };
2380
2381 static const GraticuleLines flat_digital12[] = {
2382     { { {  "256",  256+4096 }, {  "256",  256+4096 }, {  "256",  256+4096 }, {    "0",    0+4096 } } },
2383     { { { "2048", 2048+4096 }, { "2048", 2048+4096 }, { "2048", 2048+4096 }, { "2048", 2048+4096 } } },
2384     { { { "3760", 3760+4096 }, { "3840", 3840+4096 }, { "3840", 3840+4096 }, { "4095", 4095+4096 } } },
2385 };
2386
2387 static const GraticuleLines flat_millivolts8[] = {
2388     { { {   "0",  16+256 }, {   "0",  16+256 }, {   "0",  16+256 }, {   "0",   0+256 } } },
2389     { { { "175",  71+256 }, { "175",  72+256 }, { "175",  72+256 }, { "175",  64+256 } } },
2390     { { { "350", 126+256 }, { "350", 128+256 }, { "350", 128+256 }, { "350", 128+256 } } },
2391     { { { "525", 180+256 }, { "525", 184+256 }, { "525", 184+256 }, { "525", 192+256 } } },
2392     { { { "700", 235+256 }, { "700", 240+256 }, { "700", 240+256 }, { "700", 255+256 } } },
2393 };
2394
2395 static const GraticuleLines flat_millivolts9[] = {
2396     { { {   "0",  32+512 }, {   "0",  32+512 }, {   "0",  32+512 }, {   "0",   0+512 } } },
2397     { { { "175", 142+512 }, { "175", 144+512 }, { "175", 144+512 }, { "175", 128+512 } } },
2398     { { { "350", 251+512 }, { "350", 256+512 }, { "350", 256+512 }, { "350", 256+512 } } },
2399     { { { "525", 361+512 }, { "525", 368+512 }, { "525", 368+512 }, { "525", 384+512 } } },
2400     { { { "700", 470+512 }, { "700", 480+512 }, { "700", 480+512 }, { "700", 511+512 } } },
2401 };
2402
2403 static const GraticuleLines flat_millivolts10[] = {
2404     { { {   "0",  64+1024 }, {   "0",  64+1024 }, {   "0",  64+1024 }, {   "0",    0+1024 } } },
2405     { { { "175", 283+1024 }, { "175", 288+1024 }, { "175", 288+1024 }, { "175",  256+1024 } } },
2406     { { { "350", 502+1024 }, { "350", 512+1024 }, { "350", 512+1024 }, { "350",  512+1024 } } },
2407     { { { "525", 721+1024 }, { "525", 736+1024 }, { "525", 736+1024 }, { "525",  768+1024 } } },
2408     { { { "700", 940+1024 }, { "700", 960+1024 }, { "700", 960+1024 }, { "700", 1023+1024 } } },
2409 };
2410
2411 static const GraticuleLines flat_millivolts12[] = {
2412     { { {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",    0+4096 } } },
2413     { { { "175", 1132+4096 }, { "175", 1152+4096 }, { "175", 1152+4096 }, { "175", 1024+4096 } } },
2414     { { { "350", 2008+4096 }, { "350", 2048+4096 }, { "350", 2048+4096 }, { "350", 2048+4096 } } },
2415     { { { "525", 2884+4096 }, { "525", 2944+4096 }, { "525", 2944+4096 }, { "525", 3072+4096 } } },
2416     { { { "700", 3760+4096 }, { "700", 3840+4096 }, { "700", 3840+4096 }, { "700", 4095+4096 } } },
2417 };
2418
2419 static const GraticuleLines flat_ire8[] = {
2420     { { { "-25", -39+256 }, { "-25", -40+256 }, { "-25", -40+256 }, { "-25", -64+256 } } },
2421     { { {   "0",  16+256 }, {   "0",  16+256 }, {   "0",  16+256 }, {   "0",   0+256 } } },
2422     { { {  "25",  71+256 }, {  "25",  72+256 }, {  "25",  72+256 }, {  "25",  64+256 } } },
2423     { { {  "50", 126+256 }, {  "50", 128+256 }, {  "50", 128+256 }, {  "50", 128+256 } } },
2424     { { {  "75", 180+256 }, {  "75", 184+256 }, {  "75", 184+256 }, {  "75", 192+256 } } },
2425     { { { "100", 235+256 }, { "100", 240+256 }, { "100", 240+256 }, { "100", 256+256 } } },
2426     { { { "125", 290+256 }, { "125", 296+256 }, { "125", 296+256 }, { "125", 320+256 } } },
2427 };
2428
2429 static const GraticuleLines flat_ire9[] = {
2430     { { { "-25", -78+512 }, { "-25", -80+512 }, { "-25", -80+512 }, { "-25",-128+512 } } },
2431     { { {   "0",  32+512 }, {   "0",  32+512 }, {   "0",  32+512 }, {   "0",   0+512 } } },
2432     { { {  "25", 142+512 }, {  "25", 144+512 }, {  "25", 144+512 }, {  "25", 128+512 } } },
2433     { { {  "50", 251+512 }, {  "50", 256+512 }, {  "50", 256+512 }, {  "50", 256+512 } } },
2434     { { {  "75", 361+512 }, {  "75", 368+512 }, {  "75", 368+512 }, {  "75", 384+512 } } },
2435     { { { "100", 470+512 }, { "100", 480+512 }, { "100", 480+512 }, { "100", 512+512 } } },
2436     { { { "125", 580+512 }, { "125", 592+512 }, { "125", 592+512 }, { "125", 640+512 } } },
2437 };
2438
2439 static const GraticuleLines flat_ire10[] = {
2440     { { { "-25",-156+1024 }, { "-25",-160+1024 }, { "-25",-160+1024 }, { "-25", -256+1024 } } },
2441     { { {   "0",  64+1024 }, {   "0",  64+1024 }, {  "0",   64+1024 }, {   "0",    0+1024 } } },
2442     { { {  "25", 283+1024 }, {  "25", 288+1024 }, {  "25", 288+1024 }, {  "25",  256+1024 } } },
2443     { { {  "50", 502+1024 }, {  "50", 512+1024 }, {  "50", 512+1024 }, {  "50",  512+1024 } } },
2444     { { {  "75", 721+1024 }, {  "75", 736+1024 }, {  "75", 736+1024 }, {  "75",  768+1024 } } },
2445     { { { "100", 940+1024 }, { "100", 960+1024 }, { "100", 960+1024 }, { "100", 1024+1024 } } },
2446     { { { "125",1160+1024 }, { "125",1184+1024 }, { "125",1184+1024 }, { "125", 1280+1024 } } },
2447 };
2448
2449 static const GraticuleLines flat_ire12[] = {
2450     { { { "-25", -624+4096 }, { "-25", -640+4096 }, { "-25", -640+4096 }, { "-25",-1024+4096 } } },
2451     { { {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",  256+4096 }, {   "0",    0+4096 } } },
2452     { { {  "25", 1132+4096 }, {  "25", 1152+4096 }, {  "25", 1152+4096 }, {  "25", 1024+4096 } } },
2453     { { {  "50", 2008+4096 }, {  "50", 2048+4096 }, {  "50", 2048+4096 }, {  "50", 2048+4096 } } },
2454     { { {  "75", 2884+4096 }, {  "75", 2944+4096 }, {  "75", 2944+4096 }, {  "75", 3072+4096 } } },
2455     { { { "100", 3760+4096 }, { "100", 3840+4096 }, { "100", 3840+4096 }, { "100", 4096+4096 } } },
2456     { { { "125", 4640+4096 }, { "125", 4736+4096 }, { "125", 4736+4096 }, { "125", 5120+4096 } } },
2457 };
2458
2459 static const GraticuleLines digital8[] = {
2460     { { {  "16",  16 }, {  "16",  16 }, {  "16",  16 }, {   "0",   0 } } },
2461     { { { "128", 128 }, { "128", 128 }, { "128", 128 }, { "128", 128 } } },
2462     { { { "235", 235 }, { "240", 240 }, { "240", 240 }, { "255", 255 } } },
2463 };
2464
2465 static const GraticuleLines digital9[] = {
2466     { { {  "32",  32 }, {  "32",  32 }, {  "32",  32 }, {   "0",   0 } } },
2467     { { { "256", 256 }, { "256", 256 }, { "256", 256 }, { "256", 256 } } },
2468     { { { "470", 470 }, { "480", 480 }, { "480", 480 }, { "511", 511 } } },
2469 };
2470
2471 static const GraticuleLines digital10[] = {
2472     { { {  "64",  64 }, {  "64",  64 }, {  "64",  64 }, {    "0",    0 } } },
2473     { { { "512", 512 }, { "512", 512 }, { "512", 512 }, {  "512",  512 } } },
2474     { { { "940", 940 }, { "960", 960 }, { "960", 960 }, { "1023", 1023 } } },
2475 };
2476
2477 static const GraticuleLines digital12[] = {
2478     { { {  "256",  256 }, {  "256",  256 }, {  "256",  256 }, {    "0",    0 } } },
2479     { { { "2048", 2048 }, { "2048", 2048 }, { "2048", 2048 }, { "2048", 2048 } } },
2480     { { { "3760", 3760 }, { "3840", 3840 }, { "3840", 3840 }, { "4095", 4095 } } },
2481 };
2482
2483 static const GraticuleLines millivolts8[] = {
2484     { { {   "0",  16 }, {   "0",  16 }, {   "0",  16 }, {   "0",   0 } } },
2485     { { { "175",  71 }, { "175",  72 }, { "175",  72 }, { "175",  64 } } },
2486     { { { "350", 126 }, { "350", 128 }, { "350", 128 }, { "350", 128 } } },
2487     { { { "525", 180 }, { "525", 184 }, { "525", 184 }, { "525", 192 } } },
2488     { { { "700", 235 }, { "700", 240 }, { "700", 240 }, { "700", 255 } } },
2489 };
2490
2491 static const GraticuleLines millivolts9[] = {
2492     { { {   "0",  32 }, {   "0",  32 }, {   "0",  32 }, {   "0",   0 } } },
2493     { { { "175", 142 }, { "175", 144 }, { "175", 144 }, { "175", 128 } } },
2494     { { { "350", 251 }, { "350", 256 }, { "350", 256 }, { "350", 256 } } },
2495     { { { "525", 361 }, { "525", 368 }, { "525", 368 }, { "525", 384 } } },
2496     { { { "700", 470 }, { "700", 480 }, { "700", 480 }, { "700", 511 } } },
2497 };
2498
2499 static const GraticuleLines millivolts10[] = {
2500     { { {   "0",  64 }, {   "0",  64 }, {   "0",  64 }, {   "0",    0 } } },
2501     { { { "175", 283 }, { "175", 288 }, { "175", 288 }, { "175",  256 } } },
2502     { { { "350", 502 }, { "350", 512 }, { "350", 512 }, { "350",  512 } } },
2503     { { { "525", 721 }, { "525", 736 }, { "525", 736 }, { "525",  768 } } },
2504     { { { "700", 940 }, { "700", 960 }, { "700", 960 }, { "700", 1023 } } },
2505 };
2506
2507 static const GraticuleLines millivolts12[] = {
2508     { { {   "0",  256 }, {   "0",  256 }, {   "0",  256 }, {   "0",    0 } } },
2509     { { { "175", 1132 }, { "175", 1152 }, { "175", 1152 }, { "175", 1024 } } },
2510     { { { "350", 2008 }, { "350", 2048 }, { "350", 2048 }, { "350", 2048 } } },
2511     { { { "525", 2884 }, { "525", 2944 }, { "525", 2944 }, { "525", 3072 } } },
2512     { { { "700", 3760 }, { "700", 3840 }, { "700", 3840 }, { "700", 4095 } } },
2513 };
2514
2515 static const GraticuleLines ire8[] = {
2516     { { {   "0",  16 }, {   "0",  16 }, {   "0",  16 }, {   "0",   0 } } },
2517     { { {  "25",  71 }, {  "25",  72 }, {  "25",  72 }, {  "25",  64 } } },
2518     { { {  "50", 126 }, {  "50", 128 }, {  "50", 128 }, {  "50", 128 } } },
2519     { { {  "75", 180 }, {  "75", 184 }, {  "75", 184 }, {  "75", 192 } } },
2520     { { { "100", 235 }, { "100", 240 }, { "100", 240 }, { "100", 255 } } },
2521 };
2522
2523 static const GraticuleLines ire9[] = {
2524     { { {   "0",  32 }, {   "0",  32 }, {   "0",  32 }, {   "0",   0 } } },
2525     { { {  "25", 142 }, {  "25", 144 }, {  "25", 144 }, {  "25", 128 } } },
2526     { { {  "50", 251 }, {  "50", 256 }, {  "50", 256 }, {  "50", 256 } } },
2527     { { {  "75", 361 }, {  "75", 368 }, {  "75", 368 }, {  "75", 384 } } },
2528     { { { "100", 470 }, { "100", 480 }, { "100", 480 }, { "100", 511 } } },
2529 };
2530
2531 static const GraticuleLines ire10[] = {
2532     { { {   "0",  64 }, {   "0",  64 }, {  "0",   64 }, {   "0",    0 } } },
2533     { { {  "25", 283 }, {  "25", 288 }, {  "25", 288 }, {  "25",  256 } } },
2534     { { {  "50", 502 }, {  "50", 512 }, {  "50", 512 }, {  "50",  512 } } },
2535     { { {  "75", 721 }, {  "75", 736 }, {  "75", 736 }, {  "75",  768 } } },
2536     { { { "100", 940 }, { "100", 960 }, { "100", 960 }, { "100", 1023 } } },
2537 };
2538
2539 static const GraticuleLines ire12[] = {
2540     { { {   "0",  256 }, {   "0",  256 }, {   "0",  256 }, {   "0",    0 } } },
2541     { { {  "25", 1132 }, {  "25", 1152 }, {  "25", 1152 }, {  "25", 1024 } } },
2542     { { {  "50", 2008 }, {  "50", 2048 }, {  "50", 2048 }, {  "50", 2048 } } },
2543     { { {  "75", 2884 }, {  "75", 2944 }, {  "75", 2944 }, {  "75", 3072 } } },
2544     { { { "100", 3760 }, { "100", 3840 }, { "100", 3840 }, { "100", 4095 } } },
2545 };
2546
2547 static const GraticuleLines chroma_digital8[] = {
2548     { { {  "50",  50 }, {  "50",  50 }, {  "50",  50 }, {  "50",  50 } } },
2549     { { { "100", 100 }, { "100", 100 }, { "100", 100 }, { "100", 100 } } },
2550     { { { "150", 150 }, { "150", 150 }, { "150", 150 }, { "150", 150 } } },
2551     { { { "200", 200 }, { "200", 200 }, { "200", 200 }, { "200", 200 } } },
2552     { { { "255", 255 }, { "255", 255 }, { "255", 255 }, { "255", 255 } } },
2553 };
2554
2555 static const GraticuleLines chroma_digital9[] = {
2556     { { { "100", 100 }, { "100", 100 }, { "100", 100 }, { "100", 100 } } },
2557     { { { "200", 200 }, { "200", 200 }, { "200", 200 }, { "200", 200 } } },
2558     { { { "300", 300 }, { "300", 300 }, { "300", 300 }, { "300", 300 } } },
2559     { { { "400", 400 }, { "400", 400 }, { "400", 400 }, { "400", 400 } } },
2560     { { { "500", 500 }, { "500", 500 }, { "500", 500 }, { "500", 500 } } },
2561 };
2562
2563 static const GraticuleLines chroma_digital10[] = {
2564     { { { "200", 200 }, { "200", 200 }, { "200", 200 }, { "200", 200 } } },
2565     { { { "400", 400 }, { "400", 400 }, { "400", 400 }, { "400", 400 } } },
2566     { { { "600", 600 }, { "600", 600 }, { "600", 600 }, { "600", 600 } } },
2567     { { { "800", 800 }, { "800", 800 }, { "800", 800 }, { "800", 800 } } },
2568     { { {"1000",1000 }, {"1000",1000 }, {"1000",1000 }, {"1000",1000 } } },
2569 };
2570
2571 static const GraticuleLines chroma_digital12[] = {
2572     { { {  "800",  800 }, {  "800",  800 }, {  "800",  800 }, {  "800",  800 } } },
2573     { { { "1600", 1600 }, { "1600", 1600 }, { "1600", 1600 }, { "1600", 1600 } } },
2574     { { { "2400", 2400 }, { "2400", 2400 }, { "2400", 2400 }, { "2400", 2400 } } },
2575     { { { "3200", 3200 }, { "3200", 3200 }, { "3200", 3200 }, { "3200", 3200 } } },
2576     { { { "4000", 4000 }, { "4000", 4000 }, { "4000", 4000 }, { "4000", 4000 } } },
2577 };
2578
2579 static void blend_vline(uint8_t *dst, int height, int linesize, float o1, float o2, int v, int step)
2580 {
2581     int y;
2582
2583     for (y = 0; y < height; y += step) {
2584         dst[0] = v * o1 + dst[0] * o2;
2585
2586         dst += linesize * step;
2587     }
2588 }
2589
2590 static void blend_vline16(uint8_t *ddst, int height, int linesize, float o1, float o2, int v, int step)
2591 {
2592     uint16_t *dst = (uint16_t *)ddst;
2593     int y;
2594
2595     for (y = 0; y < height; y += step) {
2596         dst[0] = v * o1 + dst[0] * o2;
2597
2598         dst += (linesize / 2) * step;
2599     }
2600 }
2601
2602 static void blend_hline(uint8_t *dst, int width, int unused, float o1, float o2, int v, int step)
2603 {
2604     int x;
2605
2606     for (x = 0; x < width; x += step) {
2607         dst[x] = v * o1 + dst[x] * o2;
2608     }
2609 }
2610
2611 static void blend_hline16(uint8_t *ddst, int width, int unused, float o1, float o2, int v, int step)
2612 {
2613     uint16_t *dst = (uint16_t *)ddst;
2614     int x;
2615
2616     for (x = 0; x < width; x += step) {
2617         dst[x] = v * o1 + dst[x] * o2;
2618     }
2619 }
2620
2621 static void draw_htext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2622 {
2623     const uint8_t *font;
2624     int font_height;
2625     int i, plane;
2626
2627     font = avpriv_cga_font,   font_height =  8;
2628
2629     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2630         for (i = 0; txt[i]; i++) {
2631             int char_y, mask;
2632             int v = color[plane];
2633
2634             uint8_t *p = out->data[plane] + y * out->linesize[plane] + (x + i * 8);
2635             for (char_y = 0; char_y < font_height; char_y++) {
2636                 for (mask = 0x80; mask; mask >>= 1) {
2637                     if (font[txt[i] * font_height + char_y] & mask)
2638                         p[0] = p[0] * o2 + v * o1;
2639                     p++;
2640                 }
2641                 p += out->linesize[plane] - 8;
2642             }
2643         }
2644     }
2645 }
2646
2647 static void draw_htext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2648 {
2649     const uint8_t *font;
2650     int font_height;
2651     int i, plane;
2652
2653     font = avpriv_cga_font,   font_height =  8;
2654
2655     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2656         for (i = 0; txt[i]; i++) {
2657             int char_y, mask;
2658             int v = color[plane] * mult;
2659
2660             uint16_t *p = (uint16_t *)(out->data[plane] + y * out->linesize[plane]) + (x + i * 8);
2661             for (char_y = 0; char_y < font_height; char_y++) {
2662                 for (mask = 0x80; mask; mask >>= 1) {
2663                     if (font[txt[i] * font_height + char_y] & mask)
2664                         p[0] = p[0] * o2 + v * o1;
2665                     p++;
2666                 }
2667                 p += out->linesize[plane] / 2 - 8;
2668             }
2669         }
2670     }
2671 }
2672
2673 static void draw_vtext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2674 {
2675     const uint8_t *font;
2676     int font_height;
2677     int i, plane;
2678
2679     font = avpriv_cga_font,   font_height =  8;
2680
2681     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2682         for (i = 0; txt[i]; i++) {
2683             int char_y, mask;
2684             int v = color[plane];
2685
2686             for (char_y = font_height - 1; char_y >= 0; char_y--) {
2687                 uint8_t *p = out->data[plane] + (y + i * 10) * out->linesize[plane] + x;
2688                 for (mask = 0x80; mask; mask >>= 1) {
2689                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
2690                         p[char_y] = p[char_y] * o2 + v * o1;
2691                     p += out->linesize[plane];
2692                 }
2693             }
2694         }
2695     }
2696 }
2697
2698 static void draw_vtext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2699 {
2700     const uint8_t *font;
2701     int font_height;
2702     int i, plane;
2703
2704     font = avpriv_cga_font,   font_height =  8;
2705
2706     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2707         for (i = 0; txt[i]; i++) {
2708             int char_y, mask;
2709             int v = color[plane] * mult;
2710
2711             for (char_y = 0; char_y < font_height; char_y++) {
2712                 uint16_t *p = (uint16_t *)(out->data[plane] + (y + i * 10) * out->linesize[plane]) + x;
2713                 for (mask = 0x80; mask; mask >>= 1) {
2714                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
2715                         p[char_y] = p[char_y] * o2 + v * o1;
2716                     p += out->linesize[plane] / 2;
2717                 }
2718             }
2719         }
2720     }
2721 }
2722
2723 static void iblend_vline(uint8_t *dst, int height, int linesize, float o1, float o2, int v, int step)
2724 {
2725     int y;
2726
2727     for (y = 0; y < height; y += step) {
2728         dst[0] = (v - dst[0]) * o1 + dst[0] * o2;
2729
2730         dst += linesize * step;
2731     }
2732 }
2733
2734 static void iblend_vline16(uint8_t *ddst, int height, int linesize, float o1, float o2, int v, int step)
2735 {
2736     uint16_t *dst = (uint16_t *)ddst;
2737     int y;
2738
2739     for (y = 0; y < height; y += step) {
2740         dst[0] = (v - dst[0]) * o1 + dst[0] * o2;
2741
2742         dst += (linesize / 2) * step;
2743     }
2744 }
2745
2746 static void iblend_hline(uint8_t *dst, int width, int unused, float o1, float o2, int v, int step)
2747 {
2748     int x;
2749
2750     for (x = 0; x < width; x += step) {
2751         dst[x] = (v - dst[x]) * o1 + dst[x] * o2;
2752     }
2753 }
2754
2755 static void iblend_hline16(uint8_t *ddst, int width, int unused, float o1, float o2, int v, int step)
2756 {
2757     uint16_t *dst = (uint16_t *)ddst;
2758     int x;
2759
2760     for (x = 0; x < width; x += step) {
2761         dst[x] = (v - dst[x]) * o1 + dst[x] * o2;
2762     }
2763 }
2764
2765 static void idraw_htext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2766 {
2767     const uint8_t *font;
2768     int font_height;
2769     int i, plane;
2770
2771     font = avpriv_cga_font,   font_height =  8;
2772
2773     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2774         for (i = 0; txt[i]; i++) {
2775             int char_y, mask;
2776             int v = color[plane];
2777
2778             uint8_t *p = out->data[plane] + y * out->linesize[plane] + (x + i * 8);
2779             for (char_y = 0; char_y < font_height; char_y++) {
2780                 for (mask = 0x80; mask; mask >>= 1) {
2781                     if (font[txt[i] * font_height + char_y] & mask)
2782                         p[0] = p[0] * o2 + (v - p[0]) * o1;
2783                     p++;
2784                 }
2785                 p += out->linesize[plane] - 8;
2786             }
2787         }
2788     }
2789 }
2790
2791 static void idraw_htext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2792 {
2793     const uint8_t *font;
2794     int font_height;
2795     int i, plane;
2796
2797     font = avpriv_cga_font,   font_height =  8;
2798
2799     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2800         for (i = 0; txt[i]; i++) {
2801             int char_y, mask;
2802             int v = color[plane] * mult;
2803
2804             uint16_t *p = (uint16_t *)(out->data[plane] + y * out->linesize[plane]) + (x + i * 8);
2805             for (char_y = 0; char_y < font_height; char_y++) {
2806                 for (mask = 0x80; mask; mask >>= 1) {
2807                     if (font[txt[i] * font_height + char_y] & mask)
2808                         p[0] = p[0] * o2 + (v - p[0]) * o1;
2809                     p++;
2810                 }
2811                 p += out->linesize[plane] / 2 - 8;
2812             }
2813         }
2814     }
2815 }
2816
2817 static void idraw_vtext(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2818 {
2819     const uint8_t *font;
2820     int font_height;
2821     int i, plane;
2822
2823     font = avpriv_cga_font,   font_height =  8;
2824
2825     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2826         for (i = 0; txt[i]; i++) {
2827             int char_y, mask;
2828             int v = color[plane];
2829
2830             for (char_y = font_height - 1; char_y >= 0; char_y--) {
2831                 uint8_t *p = out->data[plane] + (y + i * 10) * out->linesize[plane] + x;
2832                 for (mask = 0x80; mask; mask >>= 1) {
2833                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
2834                         p[char_y] = p[char_y] * o2 + (v - p[char_y]) * o1;
2835                     p += out->linesize[plane];
2836                 }
2837             }
2838         }
2839     }
2840 }
2841
2842 static void idraw_vtext16(AVFrame *out, int x, int y, int mult, float o1, float o2, const char *txt, const uint8_t color[4])
2843 {
2844     const uint8_t *font;
2845     int font_height;
2846     int i, plane;
2847
2848     font = avpriv_cga_font,   font_height =  8;
2849
2850     for (plane = 0; plane < 4 && out->data[plane]; plane++) {
2851         for (i = 0; txt[i]; i++) {
2852             int char_y, mask;
2853             int v = color[plane] * mult;
2854
2855             for (char_y = 0; char_y < font_height; char_y++) {
2856                 uint16_t *p = (uint16_t *)(out->data[plane] + (y + i * 10) * out->linesize[plane]) + x;
2857                 for (mask = 0x80; mask; mask >>= 1) {
2858                     if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
2859                         p[char_y] = p[char_y] * o2 + (v - p[char_y]) * o1;
2860                     p += out->linesize[plane] / 2;
2861                 }
2862             }
2863         }
2864     }
2865 }
2866
2867 static void graticule_none(WaveformContext *s, AVFrame *out)
2868 {
2869 }
2870
2871 static void graticule_row(WaveformContext *s, AVFrame *out)
2872 {
2873     const int step = (s->flags & 2) + 1;
2874     const float o1 = s->opacity;
2875     const float o2 = 1. - o1;
2876     const int height = s->display == PARADE ? out->height / s->acomp : out->height;
2877     int C, k = 0, c, p, l, offset_x = 0, offset_y = 0;
2878
2879     for (c = 0; c < s->ncomp; c++) {
2880         if (!((1 << c) & s->pcomp) || (!s->display && k > 0))
2881             continue;
2882
2883         k++;
2884         C = s->rgb ? 0 : c;
2885         for (p = 0; p < s->ncomp; p++) {
2886             const int v = s->grat_yuva_color[p];
2887             for (l = 0; l < s->nb_glines; l++) {
2888                 const uint16_t pos = s->glines[l].line[C].pos;
2889                 int x = offset_x + (s->mirror ? s->size - 1 - pos : pos);
2890                 uint8_t *dst = out->data[p] + offset_y * out->linesize[p] + x;
2891
2892                 s->blend_line(dst, height, out->linesize[p], o1, o2, v, step);
2893             }
2894         }
2895
2896         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
2897             const char *name = s->glines[l].line[C].name;
2898             const uint16_t pos = s->glines[l].line[C].pos;
2899             int x = offset_x + (s->mirror ? s->size - 1 - pos : pos) - 10;
2900
2901             if (x < 0)
2902                 x = 4;
2903
2904             s->draw_text(out, x, offset_y + 2, 1, o1, o2, name, s->grat_yuva_color);
2905         }
2906
2907         offset_x += s->size * (s->display == STACK);
2908         offset_y += height * (s->display == PARADE);
2909     }
2910 }
2911
2912 static void graticule16_row(WaveformContext *s, AVFrame *out)
2913 {
2914     const int step = (s->flags & 2) + 1;
2915     const float o1 = s->opacity;
2916     const float o2 = 1. - o1;
2917     const int mult = s->max / 256;
2918     const int height = s->display == PARADE ? out->height / s->acomp : out->height;
2919     int C, k = 0, c, p, l, offset_x = 0, offset_y = 0;
2920
2921     for (c = 0; c < s->ncomp; c++) {
2922         if (!((1 << c) & s->pcomp) || (!s->display && k > 0))
2923             continue;
2924
2925         k++;
2926         C = s->rgb ? 0 : c;
2927         for (p = 0; p < s->ncomp; p++) {
2928             const int v = s->grat_yuva_color[p] * mult;
2929             for (l = 0; l < s->nb_glines ; l++) {
2930                 const uint16_t pos = s->glines[l].line[C].pos;
2931                 int x = offset_x + (s->mirror ? s->size - 1 - pos : pos);
2932                 uint8_t *dst = (uint8_t *)(out->data[p] + offset_y * out->linesize[p]) + x * 2;
2933
2934                 s->blend_line(dst, height, out->linesize[p], o1, o2, v, step);
2935             }
2936         }
2937
2938         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
2939             const char *name = s->glines[l].line[C].name;
2940             const uint16_t pos = s->glines[l].line[C].pos;
2941             int x = offset_x + (s->mirror ? s->size - 1 - pos : pos) - 10;
2942
2943             if (x < 0)
2944                 x = 4;
2945
2946             s->draw_text(out, x, offset_y + 2, mult, o1, o2, name, s->grat_yuva_color);
2947         }
2948
2949         offset_x += s->size * (s->display == STACK);
2950         offset_y += height * (s->display == PARADE);
2951     }
2952 }
2953
2954 static void graticule_column(WaveformContext *s, AVFrame *out)
2955 {
2956     const int step = (s->flags & 2) + 1;
2957     const float o1 = s->opacity;
2958     const float o2 = 1. - o1;
2959     const int width = s->display == PARADE ? out->width / s->acomp : out->width;
2960     int C, k = 0, c, p, l, offset_y = 0, offset_x = 0;
2961
2962     for (c = 0; c < s->ncomp; c++) {
2963         if ((!((1 << c) & s->pcomp) || (!s->display && k > 0)))
2964             continue;
2965
2966         k++;
2967         C = s->rgb ? 0 : c;
2968         for (p = 0; p < s->ncomp; p++) {
2969             const int v = s->grat_yuva_color[p];
2970             for (l = 0; l < s->nb_glines ; l++) {
2971                 const uint16_t pos = s->glines[l].line[C].pos;
2972                 int y = offset_y + (s->mirror ? s->size - 1 - pos : pos);
2973                 uint8_t *dst = out->data[p] + y * out->linesize[p] + offset_x;
2974
2975                 s->blend_line(dst, width, 1, o1, o2, v, step);
2976             }
2977         }
2978
2979         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
2980             const char *name = s->glines[l].line[C].name;
2981             const uint16_t pos = s->glines[l].line[C].pos;
2982             int y = offset_y + (s->mirror ? s->size - 1 - pos : pos) - 10;
2983
2984             if (y < 0)
2985                 y = 4;
2986
2987             s->draw_text(out, 2 + offset_x, y, 1, o1, o2, name, s->grat_yuva_color);
2988         }
2989
2990         offset_y += s->size * (s->display == STACK);
2991         offset_x += width * (s->display == PARADE);
2992     }
2993 }
2994
2995 static void graticule16_column(WaveformContext *s, AVFrame *out)
2996 {
2997     const int step = (s->flags & 2) + 1;
2998     const float o1 = s->opacity;
2999     const float o2 = 1. - o1;
3000     const int mult = s->max / 256;
3001     const int width = s->display == PARADE ? out->width / s->acomp : out->width;
3002     int C, k = 0, c, p, l, offset_x = 0, offset_y = 0;
3003
3004     for (c = 0; c < s->ncomp; c++) {
3005         if ((!((1 << c) & s->pcomp) || (!s->display && k > 0)))
3006             continue;
3007
3008         k++;
3009         C = s->rgb ? 0 : c;
3010         for (p = 0; p < s->ncomp; p++) {
3011             const int v = s->grat_yuva_color[p] * mult;
3012             for (l = 0; l < s->nb_glines ; l++) {
3013                 const uint16_t pos = s->glines[l].line[C].pos;
3014                 int y = offset_y + (s->mirror ? s->size - 1 - pos : pos);
3015                 uint8_t *dst = (uint8_t *)(out->data[p] + y * out->linesize[p]) + offset_x * 2;
3016
3017                 s->blend_line(dst, width, 1, o1, o2, v, step);
3018             }
3019         }
3020
3021         for (l = 0; l < s->nb_glines && (s->flags & 1); l++) {
3022             const char *name = s->glines[l].line[C].name;
3023             const uint16_t pos = s->glines[l].line[C].pos;
3024             int y = offset_y + (s->mirror ? s->size - 1 - pos: pos) - 10;
3025
3026             if (y < 0)
3027                 y = 4;
3028
3029             s->draw_text(out, 2 + offset_x, y, mult, o1, o2, name, s->grat_yuva_color);
3030         }
3031
3032         offset_y += s->size * (s->display == STACK);
3033         offset_x += width * (s->display == PARADE);
3034     }
3035 }
3036
3037 static int config_input(AVFilterLink *inlink)
3038 {
3039     AVFilterContext *ctx = inlink->dst;
3040     WaveformContext *s = ctx->priv;
3041
3042     s->desc  = av_pix_fmt_desc_get(inlink->format);
3043     s->ncomp = s->desc->nb_components;
3044     s->bits = s->desc->comp[0].depth;
3045     s->max = 1 << s->bits;
3046     s->intensity = s->fintensity * (s->max - 1);
3047
3048     s->shift_w[0] = s->shift_w[3] = 0;
3049     s->shift_h[0] = s->shift_h[3] = 0;
3050     s->shift_w[1] = s->shift_w[2] = s->desc->log2_chroma_w;
3051     s->shift_h[1] = s->shift_h[2] = s->desc->log2_chroma_h;
3052
3053     s->graticulef = graticule_none;
3054
3055     switch (s->filter) {
3056     case XFLAT:
3057     case YFLAT:
3058     case AFLAT: s->size = 256 * 2; break;
3059     case FLAT:  s->size = 256 * 3; break;
3060     default:    s->size = 256;     break;
3061     }
3062
3063     switch (s->filter | ((s->bits > 8) << 4) |
3064             (s->mode << 8) | (s->mirror << 12)) {
3065     case 0x1100: s->waveform_slice = lowpass_column_mirror; break;
3066     case 0x1000: s->waveform_slice = lowpass_row_mirror;    break;
3067     case 0x0100: s->waveform_slice = lowpass_column;        break;
3068     case 0x0000: s->waveform_slice = lowpass_row;           break;
3069     case 0x1110: s->waveform_slice = lowpass16_column_mirror; break;
3070     case 0x1010: s->waveform_slice = lowpass16_row_mirror;    break;
3071     case 0x0110: s->waveform_slice = lowpass16_column;        break;
3072     case 0x0010: s->waveform_slice = lowpass16_row;           break;
3073     case 0x1101: s->waveform_slice = flat_column_mirror; break;
3074     case 0x1001: s->waveform_slice = flat_row_mirror;    break;
3075     case 0x0101: s->waveform_slice = flat_column;        break;
3076     case 0x0001: s->waveform_slice = flat_row;           break;
3077     case 0x1111: s->waveform_slice = flat16_column_mirror; break;
3078     case 0x1011: s->waveform_slice = flat16_row_mirror;    break;
3079     case 0x0111: s->waveform_slice = flat16_column;        break;
3080     case 0x0011: s->waveform_slice = flat16_row;           break;
3081     case 0x1102: s->waveform_slice = aflat_column_mirror; break;
3082     case 0x1002: s->waveform_slice = aflat_row_mirror;    break;
3083     case 0x0102: s->waveform_slice = aflat_column;        break;
3084     case 0x0002: s->waveform_slice = aflat_row;           break;
3085     case 0x1112: s->waveform_slice = aflat16_column_mirror; break;
3086     case 0x1012: s->waveform_slice = aflat16_row_mirror;    break;
3087     case 0x0112: s->waveform_slice = aflat16_column;        break;
3088     case 0x0012: s->waveform_slice = aflat16_row;           break;
3089     case 0x1103: s->waveform_slice = chroma_column_mirror; break;
3090     case 0x1003: s->waveform_slice = chroma_row_mirror;    break;
3091     case 0x0103: s->waveform_slice = chroma_column;        break;
3092     case 0x0003: s->waveform_slice = chroma_row;           break;
3093     case 0x1113: s->waveform_slice = chroma16_column_mirror; break;
3094     case 0x1013: s->waveform_slice = chroma16_row_mirror;    break;
3095     case 0x0113: s->waveform_slice = chroma16_column;        break;
3096     case 0x0013: s->waveform_slice = chroma16_row;           break;
3097     case 0x1104: s->waveform_slice = color_column_mirror; break;
3098     case 0x1004: s->waveform_slice = color_row_mirror;    break;
3099     case 0x0104: s->waveform_slice = color_column;        break;
3100     case 0x0004: s->waveform_slice = color_row;           break;
3101     case 0x1114: s->waveform_slice = color16_column_mirror; break;
3102     case 0x1014: s->waveform_slice = color16_row_mirror;    break;
3103     case 0x0114: s->waveform_slice = color16_column;        break;
3104     case 0x0014: s->waveform_slice = color16_row;           break;
3105     case 0x1105: s->waveform_slice = acolor_column_mirror; break;
3106     case 0x1005: s->waveform_slice = acolor_row_mirror;    break;
3107     case 0x0105: s->waveform_slice = acolor_column;        break;
3108     case 0x0005: s->waveform_slice = acolor_row;           break;
3109     case 0x1115: s->waveform_slice = acolor16_column_mirror; break;
3110     case 0x1015: s->waveform_slice = acolor16_row_mirror;    break;
3111     case 0x0115: s->waveform_slice = acolor16_column;        break;
3112     case 0x0015: s->waveform_slice = acolor16_row;           break;
3113     case 0x1106: s->waveform_slice = xflat_column_mirror; break;
3114     case 0x1006: s->waveform_slice = xflat_row_mirror;    break;
3115     case 0x0106: s->waveform_slice = xflat_column;        break;
3116     case 0x0006: s->waveform_slice = xflat_row;           break;
3117     case 0x1116: s->waveform_slice = xflat16_column_mirror; break;
3118     case 0x1016: s->waveform_slice = xflat16_row_mirror;    break;
3119     case 0x0116: s->waveform_slice = xflat16_column;        break;
3120     case 0x0016: s->waveform_slice = xflat16_row;           break;
3121     case 0x1107: s->waveform_slice = yflat_column_mirror; break;
3122     case 0x1007: s->waveform_slice = yflat_row_mirror;    break;
3123     case 0x0107: s->waveform_slice = yflat_column;        break;
3124     case 0x0007: s->waveform_slice = yflat_row;           break;
3125     case 0x1117: s->waveform_slice = yflat16_column_mirror; break;
3126     case 0x1017: s->waveform_slice = yflat16_row_mirror;    break;
3127     case 0x0117: s->waveform_slice = yflat16_column;        break;
3128     case 0x0017: s->waveform_slice = yflat16_row;           break;
3129     }
3130
3131     s->grat_yuva_color[0] = 255;
3132     s->grat_yuva_color[1] = s->graticule == GRAT_INVERT ? 255 : 0;
3133     s->grat_yuva_color[2] = s->graticule == GRAT_ORANGE || s->graticule == GRAT_INVERT ? 255 : 0;
3134     s->grat_yuva_color[3] = 255;
3135
3136     if (s->mode == 0 && s->graticule != GRAT_INVERT) {
3137         s->blend_line = s->bits <= 8 ? blend_vline : blend_vline16;
3138         s->draw_text  = s->bits <= 8 ? draw_vtext  : draw_vtext16;
3139     } else if (s->graticule != GRAT_INVERT) {
3140         s->blend_line = s->bits <= 8 ? blend_hline : blend_hline16;
3141         s->draw_text  = s->bits <= 8 ? draw_htext  : draw_htext16;
3142     } else if (s->mode == 0 && s->graticule == GRAT_INVERT) {
3143         s->blend_line = s->bits <= 8 ? iblend_vline : iblend_vline16;
3144         s->draw_text  = s->bits <= 8 ? idraw_vtext  : idraw_vtext16;
3145     } else if (s->graticule == GRAT_INVERT) {
3146         s->blend_line = s->bits <= 8 ? iblend_hline : iblend_hline16;
3147         s->draw_text  = s->bits <= 8 ? idraw_htext  : idraw_htext16;
3148     }
3149
3150     switch (s->filter) {
3151     case LOWPASS:
3152     case COLOR:
3153     case ACOLOR:
3154     case CHROMA:
3155     case AFLAT:
3156     case XFLAT:
3157     case YFLAT:
3158     case FLAT:
3159         if (s->graticule > GRAT_NONE && s->mode == 1)
3160             s->graticulef = s->bits > 8 ? graticule16_column : graticule_column;
3161         else if (s->graticule > GRAT_NONE && s->mode == 0)
3162             s->graticulef = s->bits > 8 ? graticule16_row : graticule_row;
3163         break;
3164     }
3165
3166     switch (s->filter) {
3167     case COLOR:
3168     case ACOLOR:
3169     case LOWPASS:
3170         switch (s->scale) {
3171         case DIGITAL:
3172             switch (s->bits) {
3173             case  8: s->glines = (GraticuleLines *)digital8;  s->nb_glines = FF_ARRAY_ELEMS(digital8);  break;
3174             case  9: s->glines = (GraticuleLines *)digital9;  s->nb_glines = FF_ARRAY_ELEMS(digital9);  break;
3175             case 10: s->glines = (GraticuleLines *)digital10; s->nb_glines = FF_ARRAY_ELEMS(digital10); break;
3176             case 12: s->glines = (GraticuleLines *)digital12; s->nb_glines = FF_ARRAY_ELEMS(digital12); break;
3177             }
3178             break;
3179         case MILLIVOLTS:
3180             switch (s->bits) {
3181             case  8: s->glines = (GraticuleLines *)millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(millivolts8);  break;
3182             case  9: s->glines = (GraticuleLines *)millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(millivolts9);  break;
3183             case 10: s->glines = (GraticuleLines *)millivolts10; s->nb_glines = FF_ARRAY_ELEMS(millivolts10); break;
3184             case 12: s->glines = (GraticuleLines *)millivolts12; s->nb_glines = FF_ARRAY_ELEMS(millivolts12); break;
3185             }
3186             break;
3187         case IRE:
3188             switch (s->bits) {
3189             case  8: s->glines = (GraticuleLines *)ire8;  s->nb_glines = FF_ARRAY_ELEMS(ire8);  break;
3190             case  9: s->glines = (GraticuleLines *)ire9;  s->nb_glines = FF_ARRAY_ELEMS(ire9);  break;
3191             case 10: s->glines = (GraticuleLines *)ire10; s->nb_glines = FF_ARRAY_ELEMS(ire10); break;
3192             case 12: s->glines = (GraticuleLines *)ire12; s->nb_glines = FF_ARRAY_ELEMS(ire12); break;
3193             }
3194             break;
3195         }
3196         break;
3197     case CHROMA:
3198         switch (s->scale) {
3199         case DIGITAL:
3200             switch (s->bits) {
3201             case  8: s->glines = (GraticuleLines *)chroma_digital8;  s->nb_glines = FF_ARRAY_ELEMS(chroma_digital8);  break;
3202             case  9: s->glines = (GraticuleLines *)chroma_digital9;  s->nb_glines = FF_ARRAY_ELEMS(chroma_digital9);  break;
3203             case 10: s->glines = (GraticuleLines *)chroma_digital10; s->nb_glines = FF_ARRAY_ELEMS(chroma_digital10); break;
3204             case 12: s->glines = (GraticuleLines *)chroma_digital12; s->nb_glines = FF_ARRAY_ELEMS(chroma_digital12); break;
3205             }
3206             break;
3207         case MILLIVOLTS:
3208             switch (s->bits) {
3209             case  8: s->glines = (GraticuleLines *)millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(millivolts8);  break;
3210             case  9: s->glines = (GraticuleLines *)millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(millivolts9);  break;
3211             case 10: s->glines = (GraticuleLines *)millivolts10; s->nb_glines = FF_ARRAY_ELEMS(millivolts10); break;
3212             case 12: s->glines = (GraticuleLines *)millivolts12; s->nb_glines = FF_ARRAY_ELEMS(millivolts12); break;
3213             }
3214             break;
3215         case IRE:
3216             switch (s->bits) {
3217             case  8: s->glines = (GraticuleLines *)ire8;  s->nb_glines = FF_ARRAY_ELEMS(ire8);  break;
3218             case  9: s->glines = (GraticuleLines *)ire9;  s->nb_glines = FF_ARRAY_ELEMS(ire9);  break;
3219             case 10: s->glines = (GraticuleLines *)ire10; s->nb_glines = FF_ARRAY_ELEMS(ire10); break;
3220             case 12: s->glines = (GraticuleLines *)ire12; s->nb_glines = FF_ARRAY_ELEMS(ire12); break;
3221             }
3222             break;
3223         }
3224         break;
3225     case XFLAT:
3226     case YFLAT:
3227     case AFLAT:
3228         switch (s->scale) {
3229         case DIGITAL:
3230             switch (s->bits) {
3231             case  8: s->glines = (GraticuleLines *)aflat_digital8;  s->nb_glines = FF_ARRAY_ELEMS(aflat_digital8);  break;
3232             case  9: s->glines = (GraticuleLines *)aflat_digital9;  s->nb_glines = FF_ARRAY_ELEMS(aflat_digital9);  break;
3233             case 10: s->glines = (GraticuleLines *)aflat_digital10; s->nb_glines = FF_ARRAY_ELEMS(aflat_digital10); break;
3234             case 12: s->glines = (GraticuleLines *)aflat_digital12; s->nb_glines = FF_ARRAY_ELEMS(aflat_digital12); break;
3235             }
3236             break;
3237         case MILLIVOLTS:
3238             switch (s->bits) {
3239             case  8: s->glines = (GraticuleLines *)aflat_millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts8);  break;
3240             case  9: s->glines = (GraticuleLines *)aflat_millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts9);  break;
3241             case 10: s->glines = (GraticuleLines *)aflat_millivolts10; s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts10); break;
3242             case 12: s->glines = (GraticuleLines *)aflat_millivolts12; s->nb_glines = FF_ARRAY_ELEMS(aflat_millivolts12); break;
3243             }
3244             break;
3245         case IRE:
3246             switch (s->bits) {
3247             case  8: s->glines = (GraticuleLines *)aflat_ire8;  s->nb_glines = FF_ARRAY_ELEMS(aflat_ire8);  break;
3248             case  9: s->glines = (GraticuleLines *)aflat_ire9;  s->nb_glines = FF_ARRAY_ELEMS(aflat_ire9);  break;
3249             case 10: s->glines = (GraticuleLines *)aflat_ire10; s->nb_glines = FF_ARRAY_ELEMS(aflat_ire10); break;
3250             case 12: s->glines = (GraticuleLines *)aflat_ire12; s->nb_glines = FF_ARRAY_ELEMS(aflat_ire12); break;
3251             }
3252             break;
3253         }
3254         break;
3255     case FLAT:
3256         switch (s->scale) {
3257         case DIGITAL:
3258             switch (s->bits) {
3259             case  8: s->glines = (GraticuleLines *)flat_digital8;  s->nb_glines = FF_ARRAY_ELEMS(flat_digital8);  break;
3260             case  9: s->glines = (GraticuleLines *)flat_digital9;  s->nb_glines = FF_ARRAY_ELEMS(flat_digital9);  break;
3261             case 10: s->glines = (GraticuleLines *)flat_digital10; s->nb_glines = FF_ARRAY_ELEMS(flat_digital10); break;
3262             case 12: s->glines = (GraticuleLines *)flat_digital12; s->nb_glines = FF_ARRAY_ELEMS(flat_digital12); break;
3263             }
3264             break;
3265         case MILLIVOLTS:
3266             switch (s->bits) {
3267             case  8: s->glines = (GraticuleLines *)flat_millivolts8;  s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts8);  break;
3268             case  9: s->glines = (GraticuleLines *)flat_millivolts9;  s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts9);  break;
3269             case 10: s->glines = (GraticuleLines *)flat_millivolts10; s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts10); break;
3270             case 12: s->glines = (GraticuleLines *)flat_millivolts12; s->nb_glines = FF_ARRAY_ELEMS(flat_millivolts12); break;
3271             }
3272             break;
3273         case IRE:
3274             switch (s->bits) {
3275             case  8: s->glines = (GraticuleLines *)flat_ire8;  s->nb_glines = FF_ARRAY_ELEMS(flat_ire8);  break;
3276             case  9: s->glines = (GraticuleLines *)flat_ire9;  s->nb_glines = FF_ARRAY_ELEMS(flat_ire9);  break;
3277             case 10: s->glines = (GraticuleLines *)flat_ire10; s->nb_glines = FF_ARRAY_ELEMS(flat_ire10); break;
3278             case 12: s->glines = (GraticuleLines *)flat_ire12; s->nb_glines = FF_ARRAY_ELEMS(flat_ire12); break;
3279             }
3280             break;
3281         }
3282         break;
3283     }
3284
3285     s->size = s->size << (s->bits - 8);
3286
3287     s->tint[0] = .5f * (s->ftint[0] + 1.f) * (s->size - 1);
3288     s->tint[1] = .5f * (s->ftint[1] + 1.f) * (s->size - 1);
3289
3290     switch (inlink->format) {
3291     case AV_PIX_FMT_GBRAP:
3292     case AV_PIX_FMT_GBRP:
3293     case AV_PIX_FMT_GBRP9:
3294     case AV_PIX_FMT_GBRP10:
3295     case AV_PIX_FMT_GBRP12:
3296         s->rgb = 1;
3297         memcpy(s->bg_color, black_gbrp_color, sizeof(s->bg_color));
3298         break;
3299     default:
3300         memcpy(s->bg_color, black_yuva_color, sizeof(s->bg_color));
3301     }
3302
3303     s->bg_color[3] *= s->bgopacity;
3304
3305     return 0;
3306 }
3307
3308 static int config_output(AVFilterLink *outlink)
3309 {
3310     AVFilterContext *ctx = outlink->src;
3311     AVFilterLink *inlink = ctx->inputs[0];
3312     WaveformContext *s = ctx->priv;
3313     int comp = 0, i, j = 0, k, p, size;
3314
3315     for (i = 0; i < s->ncomp; i++) {
3316         if ((1 << i) & s->pcomp)
3317             comp++;
3318     }
3319     s->acomp = comp;
3320     if (s->acomp == 0)
3321         return AVERROR(EINVAL);
3322
3323     s->odesc = av_pix_fmt_desc_get(outlink->format);
3324     s->dcomp = s->odesc->nb_components;
3325
3326     av_freep(&s->peak);
3327
3328     if (s->mode) {
3329         outlink->h = s->size * FFMAX(comp * (s->display == STACK), 1);
3330         outlink->w = inlink->w * FFMAX(comp * (s->display == PARADE), 1);
3331         size = inlink->w;
3332     } else {
3333         outlink->w = s->size * FFMAX(comp * (s->display == STACK), 1);
3334         outlink->h = inlink->h * FFMAX(comp * (s->display == PARADE), 1);
3335         size = inlink->h;
3336     }
3337
3338     s->peak = av_malloc_array(size, 32 * sizeof(*s->peak));
3339     if (!s->peak)
3340         return AVERROR(ENOMEM);
3341
3342     for (p = 0; p < s->ncomp; p++) {
3343         const int plane = s->desc->comp[p].plane;
3344         int offset;
3345
3346         if (!((1 << p) & s->pcomp))
3347             continue;
3348
3349         for (k = 0; k < 4; k++) {
3350             s->emax[plane][k] = s->peak + size * (plane * 4 + k + 0);
3351             s->emin[plane][k] = s->peak + size * (plane * 4 + k + 16);
3352         }
3353
3354         offset = j++ * s->size * (s->display == STACK);
3355         s->estart[plane] = offset;
3356         s->eend[plane]   = (offset + s->size - 1);
3357         for (i = 0; i < size; i++) {
3358             for (k = 0; k < 4; k++) {
3359                 s->emax[plane][k][i] = s->estart[plane];
3360                 s->emin[plane][k][i] = s->eend[plane];
3361             }
3362         }
3363     }
3364
3365     outlink->sample_aspect_ratio = (AVRational){1,1};
3366
3367     return 0;
3368 }
3369
3370 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
3371 {
3372     AVFilterContext *ctx  = inlink->dst;
3373     WaveformContext *s    = ctx->priv;
3374     AVFilterLink *outlink = ctx->outputs[0];
3375     AVFrame *out;
3376     int i, j, k;
3377
3378     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
3379     if (!out) {
3380         av_frame_free(&in);
3381         return AVERROR(ENOMEM);
3382     }
3383     out->pts = in->pts;
3384     out->color_range = AVCOL_RANGE_JPEG;
3385
3386     for (k = 0; k < s->dcomp; k++) {
3387         if (s->bits <= 8) {
3388             for (i = 0; i < outlink->h ; i++)
3389                 memset(out->data[s->odesc->comp[k].plane] +
3390                        i * out->linesize[s->odesc->comp[k].plane],
3391                        s->bg_color[k], outlink->w);
3392         } else {
3393             const int mult = s->max / 256;
3394             uint16_t *dst = (uint16_t *)out->data[s->odesc->comp[k].plane];
3395
3396             for (i = 0; i < outlink->h ; i++) {
3397                 for (j = 0; j < outlink->w; j++)
3398                     dst[j] = s->bg_color[k] * mult;
3399                 dst += out->linesize[s->odesc->comp[k].plane] / 2;
3400             }
3401         }
3402     }
3403
3404     for (k = 0, i = 0; k < s->ncomp; k++) {
3405         if ((1 << k) & s->pcomp) {
3406             const int plane = s->desc->comp[k].plane;
3407             ThreadData td;
3408             int offset_y;
3409             int offset_x;
3410
3411             if (s->display == PARADE) {
3412                 offset_x = s->mode ? i++ * inlink->w : 0;
3413                 offset_y = s->mode ? 0 : i++ * inlink->h;
3414             } else {
3415                 offset_y = s->mode ? i++ * s->size * !!s->display : 0;
3416                 offset_x = s->mode ? 0 : i++ * s->size * !!s->display;
3417             }
3418
3419             td.in = in;
3420             td.out = out;
3421             td.component = k;
3422             td.offset_y = offset_y;
3423             td.offset_x = offset_x;
3424             ctx->internal->execute(ctx, s->waveform_slice, &td, NULL, ff_filter_get_nb_threads(ctx));
3425             switch (s->filter) {
3426             case LOWPASS:
3427                 if (s->bits <= 8)
3428                     envelope(s, out, plane, s->rgb || s->display == OVERLAY ? plane : 0, s->mode ? offset_x : offset_y);
3429                 else
3430                     envelope16(s, out, plane, s->rgb || s->display == OVERLAY ? plane : 0, s->mode ? offset_x : offset_y);
3431                 break;
3432             case ACOLOR:
3433             case CHROMA:
3434             case COLOR:
3435                 if (s->bits <= 8)
3436                     envelope(s, out, plane, plane, s->mode ? offset_x : offset_y);
3437                 else
3438                     envelope16(s, out, plane, plane, s->mode ? offset_x : offset_y);
3439                 break;
3440             case FLAT:
3441                 if (s->bits <= 8) {
3442                     envelope(s, out, plane, plane, s->mode ? offset_x : offset_y);
3443                     envelope(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y);
3444                 } else {
3445                     envelope16(s, out, plane, plane, s->mode ? offset_x : offset_y);
3446                     envelope16(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y);
3447                 }
3448                 break;
3449             case AFLAT:
3450             case XFLAT:
3451             case YFLAT:
3452                 if (s->bits <= 8) {
3453                     envelope(s, out, plane, (plane + 0) % s->ncomp, s->mode ? offset_x : offset_y);
3454                     envelope(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y);
3455                     envelope(s, out, plane, (plane + 2) % s->ncomp, s->mode ? offset_x : offset_y);
3456                 } else {
3457                     envelope16(s, out, plane, (plane + 0) % s->ncomp, s->mode ? offset_x : offset_y);
3458                     envelope16(s, out, plane, (plane + 1) % s->ncomp, s->mode ? offset_x : offset_y);
3459                     envelope16(s, out, plane, (plane + 2) % s->ncomp, s->mode ? offset_x : offset_y);
3460                 }
3461                 break;
3462             }
3463         }
3464     }
3465     s->graticulef(s, out);
3466
3467     av_frame_free(&in);
3468     return ff_filter_frame(outlink, out);
3469 }
3470
3471 static av_cold void uninit(AVFilterContext *ctx)
3472 {
3473     WaveformContext *s = ctx->priv;
3474
3475     av_freep(&s->peak);
3476 }
3477
3478 static const AVFilterPad inputs[] = {
3479     {
3480         .name         = "default",
3481         .type         = AVMEDIA_TYPE_VIDEO,
3482         .filter_frame = filter_frame,
3483         .config_props = config_input,
3484     },
3485     { NULL }
3486 };
3487
3488 static const AVFilterPad outputs[] = {
3489     {
3490         .name         = "default",
3491         .type         = AVMEDIA_TYPE_VIDEO,
3492         .config_props = config_output,
3493     },
3494     { NULL }
3495 };
3496
3497 AVFilter ff_vf_waveform = {
3498     .name          = "waveform",
3499     .description   = NULL_IF_CONFIG_SMALL("Video waveform monitor."),
3500     .priv_size     = sizeof(WaveformContext),
3501     .priv_class    = &waveform_class,
3502     .query_formats = query_formats,
3503     .uninit        = uninit,
3504     .inputs        = inputs,
3505     .outputs       = outputs,
3506     .flags         = AVFILTER_FLAG_SLICE_THREADS,
3507 };