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