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