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