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