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