| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2016 Paul B Mahol | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "libavutil/intreadwrite.h" | ||
| 22 | #include "libavutil/mem.h" | ||
| 23 | #include "libavutil/opt.h" | ||
| 24 | #include "libavutil/pixdesc.h" | ||
| 25 | #include "libavutil/xga_font_data.h" | ||
| 26 | #include "avfilter.h" | ||
| 27 | #include "drawutils.h" | ||
| 28 | #include "filters.h" | ||
| 29 | #include "formats.h" | ||
| 30 | #include "video.h" | ||
| 31 | |||
| 32 | typedef struct DatascopeContext { | ||
| 33 | const AVClass *class; | ||
| 34 | int ow, oh; | ||
| 35 | int x, y; | ||
| 36 | int mode; | ||
| 37 | int dformat; | ||
| 38 | int axis; | ||
| 39 | int components; | ||
| 40 | float opacity; | ||
| 41 | |||
| 42 | int nb_planes; | ||
| 43 | int nb_comps; | ||
| 44 | int chars; | ||
| 45 | FFDrawContext draw; | ||
| 46 | FFDrawColor yellow; | ||
| 47 | FFDrawColor white; | ||
| 48 | FFDrawColor black; | ||
| 49 | FFDrawColor gray; | ||
| 50 | |||
| 51 | void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value); | ||
| 52 | void (*reverse_color)(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse); | ||
| 53 | int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); | ||
| 54 | } DatascopeContext; | ||
| 55 | |||
| 56 | #define OFFSET(x) offsetof(DatascopeContext, x) | ||
| 57 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
| 58 | #define FLAGSR AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM | ||
| 59 | |||
| 60 | static const AVOption datascope_options[] = { | ||
| 61 | { "size", "set output size", OFFSET(ow), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS }, | ||
| 62 | { "s", "set output size", OFFSET(ow), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS }, | ||
| 63 | { "x", "set x offset", OFFSET(x), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGSR }, | ||
| 64 | { "y", "set y offset", OFFSET(y), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGSR }, | ||
| 65 | { "mode", "set scope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGSR, .unit = "mode" }, | ||
| 66 | { "mono", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGSR, .unit = "mode" }, | ||
| 67 | { "color", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGSR, .unit = "mode" }, | ||
| 68 | { "color2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGSR, .unit = "mode" }, | ||
| 69 | { "axis", "draw column/row numbers", OFFSET(axis), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGSR }, | ||
| 70 | { "opacity", "set background opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGSR }, | ||
| 71 | { "format", "set display number format", OFFSET(dformat), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGSR, .unit = "format" }, | ||
| 72 | { "hex", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGSR, .unit = "format" }, | ||
| 73 | { "dec", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGSR, .unit = "format" }, | ||
| 74 | { "components", "set components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=15}, 1, 15, FLAGSR }, | ||
| 75 | { NULL } | ||
| 76 | }; | ||
| 77 | |||
| 78 | AVFILTER_DEFINE_CLASS(datascope); | ||
| 79 | |||
| 80 | ✗ | static int query_formats(const AVFilterContext *ctx, | |
| 81 | AVFilterFormatsConfig **cfg_in, | ||
| 82 | AVFilterFormatsConfig **cfg_out) | ||
| 83 | { | ||
| 84 | ✗ | return ff_set_common_formats2(ctx, cfg_in, cfg_out, | |
| 85 | ff_draw_supported_pixel_formats(0)); | ||
| 86 | } | ||
| 87 | |||
| 88 | ✗ | static void draw_text(FFDrawContext *draw, AVFrame *frame, FFDrawColor *color, | |
| 89 | int x0, int y0, const uint8_t *text, int vertical) | ||
| 90 | { | ||
| 91 | ✗ | const uint8_t *cga_font = avpriv_cga_font_get(); | |
| 92 | ✗ | int x = x0; | |
| 93 | |||
| 94 | ✗ | for (; *text; text++) { | |
| 95 | ✗ | if (*text == '\n') { | |
| 96 | ✗ | x = x0; | |
| 97 | ✗ | y0 += 8; | |
| 98 | ✗ | continue; | |
| 99 | } | ||
| 100 | ✗ | ff_blend_mask(draw, color, frame->data, frame->linesize, | |
| 101 | frame->width, frame->height, | ||
| 102 | ✗ | &cga_font[*text * 8], 1, 8, 8, 0, 0, x, y0); | |
| 103 | ✗ | if (vertical) { | |
| 104 | ✗ | x = x0; | |
| 105 | ✗ | y0 += 8; | |
| 106 | } else { | ||
| 107 | ✗ | x += 8; | |
| 108 | } | ||
| 109 | } | ||
| 110 | ✗ | } | |
| 111 | |||
| 112 | ✗ | static void pick_color8(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value) | |
| 113 | { | ||
| 114 | int p, i; | ||
| 115 | |||
| 116 | ✗ | color->rgba[3] = 255; | |
| 117 | ✗ | for (p = 0; p < draw->nb_planes; p++) { | |
| 118 | ✗ | if (draw->nb_planes == 1) { | |
| 119 | ✗ | for (i = 0; i < 4; i++) { | |
| 120 | ✗ | value[i] = in->data[0][y * in->linesize[0] + x * draw->pixelstep[0] + i]; | |
| 121 | ✗ | color->comp[0].u8[i] = value[i]; | |
| 122 | } | ||
| 123 | } else { | ||
| 124 | ✗ | value[p] = in->data[p][(y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p])]; | |
| 125 | ✗ | color->comp[p].u8[0] = value[p]; | |
| 126 | } | ||
| 127 | } | ||
| 128 | ✗ | } | |
| 129 | |||
| 130 | ✗ | static void pick_color16(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value) | |
| 131 | { | ||
| 132 | int p, i; | ||
| 133 | |||
| 134 | ✗ | color->rgba[3] = 255; | |
| 135 | ✗ | for (p = 0; p < draw->nb_planes; p++) { | |
| 136 | ✗ | if (draw->nb_planes == 1) { | |
| 137 | ✗ | for (i = 0; i < 4; i++) { | |
| 138 | ✗ | value[i] = AV_RL16(in->data[0] + y * in->linesize[0] + x * draw->pixelstep[0] + i * 2); | |
| 139 | ✗ | color->comp[0].u16[i] = value[i]; | |
| 140 | } | ||
| 141 | } else { | ||
| 142 | ✗ | value[p] = AV_RL16(in->data[p] + (y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p]) * 2); | |
| 143 | ✗ | color->comp[p].u16[0] = value[p]; | |
| 144 | } | ||
| 145 | } | ||
| 146 | ✗ | } | |
| 147 | |||
| 148 | ✗ | static void reverse_color8(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse) | |
| 149 | { | ||
| 150 | int p; | ||
| 151 | |||
| 152 | ✗ | reverse->rgba[3] = 255; | |
| 153 | ✗ | for (p = 0; p < draw->nb_planes; p++) { | |
| 154 | ✗ | reverse->comp[p].u8[0] = color->comp[p].u8[0] > 127 ? 0 : 255; | |
| 155 | ✗ | reverse->comp[p].u8[1] = color->comp[p].u8[1] > 127 ? 0 : 255; | |
| 156 | ✗ | reverse->comp[p].u8[2] = color->comp[p].u8[2] > 127 ? 0 : 255; | |
| 157 | } | ||
| 158 | ✗ | } | |
| 159 | |||
| 160 | ✗ | static void reverse_color16(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse) | |
| 161 | { | ||
| 162 | int p; | ||
| 163 | |||
| 164 | ✗ | reverse->rgba[3] = 255; | |
| 165 | ✗ | for (p = 0; p < draw->nb_planes; p++) { | |
| 166 | ✗ | const unsigned max = (1 << draw->desc->comp[p].depth) - 1; | |
| 167 | ✗ | const unsigned mid = (max + 1) / 2; | |
| 168 | |||
| 169 | ✗ | reverse->comp[p].u16[0] = color->comp[p].u16[0] > mid ? 0 : max; | |
| 170 | ✗ | reverse->comp[p].u16[1] = color->comp[p].u16[1] > mid ? 0 : max; | |
| 171 | ✗ | reverse->comp[p].u16[2] = color->comp[p].u16[2] > mid ? 0 : max; | |
| 172 | } | ||
| 173 | ✗ | } | |
| 174 | |||
| 175 | typedef struct ThreadData { | ||
| 176 | AVFrame *in, *out; | ||
| 177 | int xoff, yoff, PP; | ||
| 178 | } ThreadData; | ||
| 179 | |||
| 180 | ✗ | static int filter_color2(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 181 | { | ||
| 182 | ✗ | DatascopeContext *s = ctx->priv; | |
| 183 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 184 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 185 | ✗ | ThreadData *td = arg; | |
| 186 | ✗ | AVFrame *in = td->in; | |
| 187 | ✗ | AVFrame *out = td->out; | |
| 188 | ✗ | const int PP = td->PP; | |
| 189 | ✗ | const int xoff = td->xoff; | |
| 190 | ✗ | const int yoff = td->yoff; | |
| 191 | ✗ | const int P = FFMAX(s->nb_planes, s->nb_comps); | |
| 192 | ✗ | const int C = s->chars; | |
| 193 | ✗ | const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2; | |
| 194 | ✗ | const int W = (outlink->w - xoff) / (C * 10); | |
| 195 | ✗ | const int H = (outlink->h - yoff) / (PP * 12); | |
| 196 | ✗ | const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"}; | |
| 197 | ✗ | const int slice_start = (W * jobnr) / nb_jobs; | |
| 198 | ✗ | const int slice_end = (W * (jobnr+1)) / nb_jobs; | |
| 199 | int x, y, p; | ||
| 200 | |||
| 201 | ✗ | for (y = 0; y < H && (y + s->y < inlink->h); y++) { | |
| 202 | ✗ | for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) { | |
| 203 | ✗ | FFDrawColor color = { { 0 } }; | |
| 204 | ✗ | FFDrawColor reverse = { { 0 } }; | |
| 205 | ✗ | int value[4] = { 0 }, pp = 0; | |
| 206 | |||
| 207 | ✗ | s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value); | |
| 208 | ✗ | s->reverse_color(&s->draw, &color, &reverse); | |
| 209 | ✗ | ff_fill_rectangle(&s->draw, &color, out->data, out->linesize, | |
| 210 | ✗ | xoff + x * C * 10, yoff + y * PP * 12, C * 10, PP * 12); | |
| 211 | |||
| 212 | ✗ | for (p = 0; p < P; p++) { | |
| 213 | char text[256]; | ||
| 214 | |||
| 215 | ✗ | if (!(s->components & (1 << p))) | |
| 216 | ✗ | continue; | |
| 217 | ✗ | snprintf(text, sizeof(text), format[D], value[p]); | |
| 218 | ✗ | draw_text(&s->draw, out, &reverse, xoff + x * C * 10 + 2, yoff + y * PP * 12 + pp * 10 + 2, text, 0); | |
| 219 | ✗ | pp++; | |
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | ✗ | return 0; | |
| 225 | } | ||
| 226 | |||
| 227 | ✗ | static int filter_color(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 228 | { | ||
| 229 | ✗ | DatascopeContext *s = ctx->priv; | |
| 230 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 231 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 232 | ✗ | ThreadData *td = arg; | |
| 233 | ✗ | AVFrame *in = td->in; | |
| 234 | ✗ | AVFrame *out = td->out; | |
| 235 | ✗ | const int PP = td->PP; | |
| 236 | ✗ | const int xoff = td->xoff; | |
| 237 | ✗ | const int yoff = td->yoff; | |
| 238 | ✗ | const int P = FFMAX(s->nb_planes, s->nb_comps); | |
| 239 | ✗ | const int C = s->chars; | |
| 240 | ✗ | const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2; | |
| 241 | ✗ | const int W = (outlink->w - xoff) / (C * 10); | |
| 242 | ✗ | const int H = (outlink->h - yoff) / (PP * 12); | |
| 243 | ✗ | const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"}; | |
| 244 | ✗ | const int slice_start = (W * jobnr) / nb_jobs; | |
| 245 | ✗ | const int slice_end = (W * (jobnr+1)) / nb_jobs; | |
| 246 | int x, y, p; | ||
| 247 | |||
| 248 | ✗ | for (y = 0; y < H && (y + s->y < inlink->h); y++) { | |
| 249 | ✗ | for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) { | |
| 250 | ✗ | FFDrawColor color = { { 0 } }; | |
| 251 | ✗ | int value[4] = { 0 }, pp = 0; | |
| 252 | |||
| 253 | ✗ | s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value); | |
| 254 | |||
| 255 | ✗ | for (p = 0; p < P; p++) { | |
| 256 | char text[256]; | ||
| 257 | |||
| 258 | ✗ | if (!(s->components & (1 << p))) | |
| 259 | ✗ | continue; | |
| 260 | ✗ | snprintf(text, sizeof(text), format[D], value[p]); | |
| 261 | ✗ | draw_text(&s->draw, out, &color, xoff + x * C * 10 + 2, yoff + y * PP * 12 + pp * 10 + 2, text, 0); | |
| 262 | ✗ | pp++; | |
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | ✗ | return 0; | |
| 268 | } | ||
| 269 | |||
| 270 | ✗ | static int filter_mono(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 271 | { | ||
| 272 | ✗ | DatascopeContext *s = ctx->priv; | |
| 273 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 274 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 275 | ✗ | ThreadData *td = arg; | |
| 276 | ✗ | AVFrame *in = td->in; | |
| 277 | ✗ | AVFrame *out = td->out; | |
| 278 | ✗ | const int PP = td->PP; | |
| 279 | ✗ | const int xoff = td->xoff; | |
| 280 | ✗ | const int yoff = td->yoff; | |
| 281 | ✗ | const int P = FFMAX(s->nb_planes, s->nb_comps); | |
| 282 | ✗ | const int C = s->chars; | |
| 283 | ✗ | const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2; | |
| 284 | ✗ | const int W = (outlink->w - xoff) / (C * 10); | |
| 285 | ✗ | const int H = (outlink->h - yoff) / (PP * 12); | |
| 286 | ✗ | const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"}; | |
| 287 | ✗ | const int slice_start = (W * jobnr) / nb_jobs; | |
| 288 | ✗ | const int slice_end = (W * (jobnr+1)) / nb_jobs; | |
| 289 | int x, y, p; | ||
| 290 | |||
| 291 | ✗ | for (y = 0; y < H && (y + s->y < inlink->h); y++) { | |
| 292 | ✗ | for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) { | |
| 293 | ✗ | FFDrawColor color = { { 0 } }; | |
| 294 | ✗ | int value[4] = { 0 }, pp = 0; | |
| 295 | |||
| 296 | ✗ | s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value); | |
| 297 | ✗ | for (p = 0; p < P; p++) { | |
| 298 | char text[256]; | ||
| 299 | |||
| 300 | ✗ | if (!(s->components & (1 << p))) | |
| 301 | ✗ | continue; | |
| 302 | ✗ | snprintf(text, sizeof(text), format[D], value[p]); | |
| 303 | ✗ | draw_text(&s->draw, out, &s->white, xoff + x * C * 10 + 2, yoff + y * PP * 12 + pp * 10 + 2, text, 0); | |
| 304 | ✗ | pp++; | |
| 305 | } | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | ✗ | return 0; | |
| 310 | } | ||
| 311 | |||
| 312 | ✗ | static int filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 313 | { | ||
| 314 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 315 | ✗ | DatascopeContext *s = ctx->priv; | |
| 316 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 317 | ✗ | const int P = FFMAX(s->nb_planes, s->nb_comps); | |
| 318 | ✗ | ThreadData td = { 0 }; | |
| 319 | ✗ | int ymaxlen = 0; | |
| 320 | ✗ | int xmaxlen = 0; | |
| 321 | ✗ | int PP = 0; | |
| 322 | AVFrame *out; | ||
| 323 | |||
| 324 | ✗ | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 325 | ✗ | if (!out) { | |
| 326 | ✗ | av_frame_free(&in); | |
| 327 | ✗ | return AVERROR(ENOMEM); | |
| 328 | } | ||
| 329 | ✗ | av_frame_copy_props(out, in); | |
| 330 | |||
| 331 | ✗ | ff_fill_rectangle(&s->draw, &s->black, out->data, out->linesize, | |
| 332 | 0, 0, outlink->w, outlink->h); | ||
| 333 | |||
| 334 | ✗ | for (int p = 0; p < P; p++) { | |
| 335 | ✗ | if (s->components & (1 << p)) | |
| 336 | ✗ | PP++; | |
| 337 | } | ||
| 338 | ✗ | PP = FFMAX(PP, 1); | |
| 339 | |||
| 340 | ✗ | if (s->axis) { | |
| 341 | ✗ | const int C = s->chars; | |
| 342 | ✗ | int Y = outlink->h / (PP * 12); | |
| 343 | ✗ | int X = outlink->w / (C * 10); | |
| 344 | ✗ | char text[256] = { 0 }; | |
| 345 | int x, y; | ||
| 346 | |||
| 347 | ✗ | snprintf(text, sizeof(text), "%d", s->y + Y); | |
| 348 | ✗ | ymaxlen = strlen(text); | |
| 349 | ✗ | ymaxlen *= 10; | |
| 350 | ✗ | snprintf(text, sizeof(text), "%d", s->x + X); | |
| 351 | ✗ | xmaxlen = strlen(text); | |
| 352 | ✗ | xmaxlen *= 10; | |
| 353 | |||
| 354 | ✗ | Y = (outlink->h - xmaxlen) / (PP * 12); | |
| 355 | ✗ | X = (outlink->w - ymaxlen) / (C * 10); | |
| 356 | |||
| 357 | ✗ | for (y = 0; y < Y; y++) { | |
| 358 | ✗ | snprintf(text, sizeof(text), "%d", s->y + y); | |
| 359 | |||
| 360 | ✗ | ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize, | |
| 361 | ✗ | 0, xmaxlen + y * PP * 12 + (PP + 1) * PP - 2, ymaxlen, 10); | |
| 362 | |||
| 363 | ✗ | draw_text(&s->draw, out, &s->yellow, 2, xmaxlen + y * PP * 12 + (PP + 1) * PP, text, 0); | |
| 364 | } | ||
| 365 | |||
| 366 | ✗ | for (x = 0; x < X; x++) { | |
| 367 | ✗ | snprintf(text, sizeof(text), "%d", s->x + x); | |
| 368 | |||
| 369 | ✗ | ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize, | |
| 370 | ✗ | ymaxlen + x * C * 10 + 2 * C - 2, 0, 10, xmaxlen); | |
| 371 | |||
| 372 | ✗ | draw_text(&s->draw, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1); | |
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | ✗ | td.in = in; td.out = out, td.yoff = xmaxlen, td.xoff = ymaxlen, td.PP = PP; | |
| 377 | ✗ | ff_filter_execute(ctx, s->filter, &td, NULL, | |
| 378 | ✗ | FFMIN(ff_filter_get_nb_threads(ctx), FFMAX(outlink->w / 20, 1))); | |
| 379 | |||
| 380 | ✗ | av_frame_free(&in); | |
| 381 | ✗ | return ff_filter_frame(outlink, out); | |
| 382 | } | ||
| 383 | |||
| 384 | ✗ | static int config_input(AVFilterLink *inlink) | |
| 385 | { | ||
| 386 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 387 | ✗ | DatascopeContext *s = ctx->priv; | |
| 388 | |||
| 389 | ✗ | uint8_t alpha = s->opacity * 255; | |
| 390 | int ret; | ||
| 391 | |||
| 392 | ✗ | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
| 393 | ✗ | ret = ff_draw_init_from_link(&s->draw, inlink, 0); | |
| 394 | ✗ | if (ret < 0) { | |
| 395 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n"); | |
| 396 | ✗ | return ret; | |
| 397 | } | ||
| 398 | ✗ | ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} ); | |
| 399 | ✗ | ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, alpha} ); | |
| 400 | ✗ | ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} ); | |
| 401 | ✗ | ff_draw_color(&s->draw, &s->gray, (uint8_t[]){ 77, 77, 77, 255} ); | |
| 402 | ✗ | s->chars = (s->draw.desc->comp[0].depth + 7) / 8 * 2 + s->dformat; | |
| 403 | ✗ | s->nb_comps = s->draw.desc->nb_components; | |
| 404 | |||
| 405 | ✗ | switch (s->mode) { | |
| 406 | ✗ | case 0: s->filter = filter_mono; break; | |
| 407 | ✗ | case 1: s->filter = filter_color; break; | |
| 408 | ✗ | case 2: s->filter = filter_color2; break; | |
| 409 | } | ||
| 410 | |||
| 411 | ✗ | if (s->draw.desc->comp[0].depth <= 8) { | |
| 412 | ✗ | s->pick_color = pick_color8; | |
| 413 | ✗ | s->reverse_color = reverse_color8; | |
| 414 | } else { | ||
| 415 | ✗ | s->pick_color = pick_color16; | |
| 416 | ✗ | s->reverse_color = reverse_color16; | |
| 417 | } | ||
| 418 | |||
| 419 | ✗ | return 0; | |
| 420 | } | ||
| 421 | |||
| 422 | ✗ | static int config_output(AVFilterLink *outlink) | |
| 423 | { | ||
| 424 | ✗ | DatascopeContext *s = outlink->src->priv; | |
| 425 | |||
| 426 | ✗ | outlink->h = s->oh; | |
| 427 | ✗ | outlink->w = s->ow; | |
| 428 | ✗ | outlink->sample_aspect_ratio = (AVRational){1,1}; | |
| 429 | |||
| 430 | ✗ | return 0; | |
| 431 | } | ||
| 432 | |||
| 433 | ✗ | static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, | |
| 434 | char *res, int res_len, int flags) | ||
| 435 | { | ||
| 436 | int ret; | ||
| 437 | |||
| 438 | ✗ | ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); | |
| 439 | ✗ | if (ret < 0) | |
| 440 | ✗ | return ret; | |
| 441 | |||
| 442 | ✗ | return config_input(ctx->inputs[0]); | |
| 443 | } | ||
| 444 | |||
| 445 | static const AVFilterPad inputs[] = { | ||
| 446 | { | ||
| 447 | .name = "default", | ||
| 448 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 449 | .filter_frame = filter_frame, | ||
| 450 | .config_props = config_input, | ||
| 451 | }, | ||
| 452 | }; | ||
| 453 | |||
| 454 | static const AVFilterPad outputs[] = { | ||
| 455 | { | ||
| 456 | .name = "default", | ||
| 457 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 458 | .config_props = config_output, | ||
| 459 | }, | ||
| 460 | }; | ||
| 461 | |||
| 462 | const FFFilter ff_vf_datascope = { | ||
| 463 | .p.name = "datascope", | ||
| 464 | .p.description = NULL_IF_CONFIG_SMALL("Video data analysis."), | ||
| 465 | .p.priv_class = &datascope_class, | ||
| 466 | .p.flags = AVFILTER_FLAG_SLICE_THREADS, | ||
| 467 | .priv_size = sizeof(DatascopeContext), | ||
| 468 | FILTER_INPUTS(inputs), | ||
| 469 | FILTER_OUTPUTS(outputs), | ||
| 470 | FILTER_QUERY_FUNC2(query_formats), | ||
| 471 | .process_command = process_command, | ||
| 472 | }; | ||
| 473 | |||
| 474 | typedef struct PixscopeContext { | ||
| 475 | const AVClass *class; | ||
| 476 | |||
| 477 | float xpos, ypos; | ||
| 478 | float wx, wy; | ||
| 479 | int w, h; | ||
| 480 | float o; | ||
| 481 | |||
| 482 | int x, y; | ||
| 483 | int ww, wh; | ||
| 484 | |||
| 485 | int nb_planes; | ||
| 486 | int nb_comps; | ||
| 487 | int is_rgb; | ||
| 488 | uint8_t rgba_map[4]; | ||
| 489 | FFDrawContext draw; | ||
| 490 | FFDrawColor dark; | ||
| 491 | FFDrawColor black; | ||
| 492 | FFDrawColor white; | ||
| 493 | FFDrawColor green; | ||
| 494 | FFDrawColor blue; | ||
| 495 | FFDrawColor red; | ||
| 496 | FFDrawColor *colors[4]; | ||
| 497 | |||
| 498 | uint16_t values[4][80][80]; | ||
| 499 | |||
| 500 | void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value); | ||
| 501 | } PixscopeContext; | ||
| 502 | |||
| 503 | #define POFFSET(x) offsetof(PixscopeContext, x) | ||
| 504 | |||
| 505 | static const AVOption pixscope_options[] = { | ||
| 506 | { "x", "set scope x offset", POFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR }, | ||
| 507 | { "y", "set scope y offset", POFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR }, | ||
| 508 | { "w", "set scope width", POFFSET(w), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGSR }, | ||
| 509 | { "h", "set scope height", POFFSET(h), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGSR }, | ||
| 510 | { "o", "set window opacity", POFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR }, | ||
| 511 | { "wx", "set window x offset", POFFSET(wx), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGSR }, | ||
| 512 | { "wy", "set window y offset", POFFSET(wy), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGSR }, | ||
| 513 | { NULL } | ||
| 514 | }; | ||
| 515 | |||
| 516 | AVFILTER_DEFINE_CLASS(pixscope); | ||
| 517 | |||
| 518 | ✗ | static int pixscope_config_input(AVFilterLink *inlink) | |
| 519 | { | ||
| 520 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 521 | ✗ | PixscopeContext *s = ctx->priv; | |
| 522 | int ret; | ||
| 523 | |||
| 524 | ✗ | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
| 525 | ✗ | ret = ff_draw_init(&s->draw, inlink->format, 0); | |
| 526 | ✗ | if (ret < 0) { | |
| 527 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n"); | |
| 528 | ✗ | return ret; | |
| 529 | } | ||
| 530 | ✗ | ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} ); | |
| 531 | ✗ | ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} ); | |
| 532 | ✗ | ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} ); | |
| 533 | ✗ | ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} ); | |
| 534 | ✗ | ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} ); | |
| 535 | ✗ | ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} ); | |
| 536 | ✗ | s->nb_comps = s->draw.desc->nb_components; | |
| 537 | ✗ | s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB; | |
| 538 | |||
| 539 | ✗ | if (s->is_rgb) { | |
| 540 | ✗ | s->colors[0] = &s->red; | |
| 541 | ✗ | s->colors[1] = &s->green; | |
| 542 | ✗ | s->colors[2] = &s->blue; | |
| 543 | ✗ | s->colors[3] = &s->white; | |
| 544 | ✗ | ff_fill_rgba_map(s->rgba_map, inlink->format); | |
| 545 | } else { | ||
| 546 | ✗ | s->colors[0] = &s->white; | |
| 547 | ✗ | s->colors[1] = &s->blue; | |
| 548 | ✗ | s->colors[2] = &s->red; | |
| 549 | ✗ | s->colors[3] = &s->white; | |
| 550 | ✗ | s->rgba_map[0] = 0; | |
| 551 | ✗ | s->rgba_map[1] = 1; | |
| 552 | ✗ | s->rgba_map[2] = 2; | |
| 553 | ✗ | s->rgba_map[3] = 3; | |
| 554 | } | ||
| 555 | |||
| 556 | ✗ | if (s->draw.desc->comp[0].depth <= 8) { | |
| 557 | ✗ | s->pick_color = pick_color8; | |
| 558 | } else { | ||
| 559 | ✗ | s->pick_color = pick_color16; | |
| 560 | } | ||
| 561 | |||
| 562 | ✗ | if (inlink->w < 640 || inlink->h < 480) { | |
| 563 | ✗ | av_log(inlink->dst, AV_LOG_ERROR, "min supported resolution is 640x480\n"); | |
| 564 | ✗ | return AVERROR(EINVAL); | |
| 565 | } | ||
| 566 | |||
| 567 | ✗ | s->ww = 300; | |
| 568 | ✗ | s->wh = 300 * 1.6; | |
| 569 | ✗ | s->x = s->xpos * (inlink->w - 1); | |
| 570 | ✗ | s->y = s->ypos * (inlink->h - 1); | |
| 571 | ✗ | if (s->x + s->w >= inlink->w || s->y + s->h >= inlink->h) { | |
| 572 | ✗ | av_log(inlink->dst, AV_LOG_WARNING, "scope position is out of range, clipping\n"); | |
| 573 | ✗ | s->x = FFMIN(s->x, inlink->w - s->w); | |
| 574 | ✗ | s->y = FFMIN(s->y, inlink->h - s->h); | |
| 575 | } | ||
| 576 | |||
| 577 | ✗ | return 0; | |
| 578 | } | ||
| 579 | |||
| 580 | #define SQR(x) ((x)*(x)) | ||
| 581 | |||
| 582 | ✗ | static int pixscope_filter_frame(AVFilterLink *inlink, AVFrame *in) | |
| 583 | { | ||
| 584 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 585 | ✗ | PixscopeContext *s = ctx->priv; | |
| 586 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 587 | ✗ | AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height); | |
| 588 | ✗ | int max[4] = { 0 }, min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX }; | |
| 589 | ✗ | float average[4] = { 0 }; | |
| 590 | ✗ | double std[4] = { 0 }, rms[4] = { 0 }; | |
| 591 | ✗ | const char rgba[4] = { 'R', 'G', 'B', 'A' }; | |
| 592 | ✗ | const char yuva[4] = { 'Y', 'U', 'V', 'A' }; | |
| 593 | int x, y, X, Y, i, w, h; | ||
| 594 | char text[128]; | ||
| 595 | |||
| 596 | ✗ | if (!out) { | |
| 597 | ✗ | av_frame_free(&in); | |
| 598 | ✗ | return AVERROR(ENOMEM); | |
| 599 | } | ||
| 600 | ✗ | av_frame_copy_props(out, in); | |
| 601 | ✗ | av_frame_copy(out, in); | |
| 602 | |||
| 603 | ✗ | w = s->ww / s->w; | |
| 604 | ✗ | h = s->ww / s->h; | |
| 605 | |||
| 606 | ✗ | if (s->wx >= 0) { | |
| 607 | ✗ | X = (in->width - s->ww) * s->wx; | |
| 608 | } else { | ||
| 609 | ✗ | X = (in->width - s->ww) * -s->wx; | |
| 610 | } | ||
| 611 | ✗ | if (s->wy >= 0) { | |
| 612 | ✗ | Y = (in->height - s->wh) * s->wy; | |
| 613 | } else { | ||
| 614 | ✗ | Y = (in->height - s->wh) * -s->wy; | |
| 615 | } | ||
| 616 | |||
| 617 | ✗ | if (s->wx < 0) { | |
| 618 | ✗ | if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) && | |
| 619 | ✗ | s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) { | |
| 620 | ✗ | X = (in->width - s->ww) * (1 + s->wx); | |
| 621 | } | ||
| 622 | } | ||
| 623 | |||
| 624 | ✗ | if (s->wy < 0) { | |
| 625 | ✗ | if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) && | |
| 626 | ✗ | s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) { | |
| 627 | ✗ | Y = (in->height - s->wh) * (1 + s->wy); | |
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | ✗ | ff_blend_rectangle(&s->draw, &s->dark, out->data, out->linesize, | |
| 632 | out->width, out->height, | ||
| 633 | X, | ||
| 634 | Y, | ||
| 635 | s->ww, | ||
| 636 | s->wh); | ||
| 637 | |||
| 638 | ✗ | for (y = 0; y < s->h; y++) { | |
| 639 | ✗ | for (x = 0; x < s->w; x++) { | |
| 640 | ✗ | FFDrawColor color = { { 0 } }; | |
| 641 | ✗ | int value[4] = { 0 }; | |
| 642 | |||
| 643 | ✗ | s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value); | |
| 644 | ✗ | ff_fill_rectangle(&s->draw, &color, out->data, out->linesize, | |
| 645 | ✗ | x * w + (s->ww - 4 - (s->w * w)) / 2 + X, y * h + 2 + Y, w, h); | |
| 646 | ✗ | for (i = 0; i < 4; i++) { | |
| 647 | ✗ | s->values[i][x][y] = value[i]; | |
| 648 | ✗ | rms[i] += (double)value[i] * (double)value[i]; | |
| 649 | ✗ | average[i] += value[i]; | |
| 650 | ✗ | min[i] = FFMIN(min[i], value[i]); | |
| 651 | ✗ | max[i] = FFMAX(max[i], value[i]); | |
| 652 | } | ||
| 653 | } | ||
| 654 | } | ||
| 655 | |||
| 656 | ✗ | ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize, | |
| 657 | out->width, out->height, | ||
| 658 | ✗ | s->x - 2, s->y - 2, s->w + 4, 1); | |
| 659 | |||
| 660 | ✗ | ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize, | |
| 661 | out->width, out->height, | ||
| 662 | ✗ | s->x - 1, s->y - 1, s->w + 2, 1); | |
| 663 | |||
| 664 | ✗ | ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize, | |
| 665 | out->width, out->height, | ||
| 666 | ✗ | s->x - 1, s->y - 1, 1, s->h + 2); | |
| 667 | |||
| 668 | ✗ | ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize, | |
| 669 | out->width, out->height, | ||
| 670 | ✗ | s->x - 2, s->y - 2, 1, s->h + 4); | |
| 671 | |||
| 672 | ✗ | ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize, | |
| 673 | out->width, out->height, | ||
| 674 | ✗ | s->x - 1, s->y + 1 + s->h, s->w + 3, 1); | |
| 675 | |||
| 676 | ✗ | ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize, | |
| 677 | out->width, out->height, | ||
| 678 | ✗ | s->x - 2, s->y + 2 + s->h, s->w + 4, 1); | |
| 679 | |||
| 680 | ✗ | ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize, | |
| 681 | out->width, out->height, | ||
| 682 | ✗ | s->x + 1 + s->w, s->y - 1, 1, s->h + 2); | |
| 683 | |||
| 684 | ✗ | ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize, | |
| 685 | out->width, out->height, | ||
| 686 | ✗ | s->x + 2 + s->w, s->y - 2, 1, s->h + 5); | |
| 687 | |||
| 688 | ✗ | for (i = 0; i < 4; i++) { | |
| 689 | ✗ | rms[i] /= s->w * s->h; | |
| 690 | ✗ | rms[i] = sqrt(rms[i]); | |
| 691 | ✗ | average[i] /= s->w * s->h; | |
| 692 | } | ||
| 693 | |||
| 694 | ✗ | for (y = 0; y < s->h; y++) { | |
| 695 | ✗ | for (x = 0; x < s->w; x++) { | |
| 696 | ✗ | for (i = 0; i < 4; i++) | |
| 697 | ✗ | std[i] += SQR(s->values[i][x][y] - average[i]); | |
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 | ✗ | for (i = 0; i < 4; i++) { | |
| 702 | ✗ | std[i] /= s->w * s->h; | |
| 703 | ✗ | std[i] = sqrt(std[i]); | |
| 704 | } | ||
| 705 | |||
| 706 | ✗ | snprintf(text, sizeof(text), "CH AVG MIN MAX RMS\n"); | |
| 707 | ✗ | draw_text(&s->draw, out, &s->white, X + 28, Y + s->ww + 5, text, 0); | |
| 708 | ✗ | for (i = 0; i < s->nb_comps; i++) { | |
| 709 | ✗ | int c = s->rgba_map[i]; | |
| 710 | |||
| 711 | ✗ | snprintf(text, sizeof(text), "%c %07.1f %05d %05d %07.1f\n", s->is_rgb ? rgba[i] : yuva[i], average[c], min[c], max[c], rms[c]); | |
| 712 | ✗ | draw_text(&s->draw, out, s->colors[i], X + 28, Y + s->ww + 15 * (i + 1), text, 0); | |
| 713 | } | ||
| 714 | ✗ | snprintf(text, sizeof(text), "CH STD\n"); | |
| 715 | ✗ | draw_text(&s->draw, out, &s->white, X + 28, Y + s->ww + 15 * (0 + 5), text, 0); | |
| 716 | ✗ | for (i = 0; i < s->nb_comps; i++) { | |
| 717 | ✗ | int c = s->rgba_map[i]; | |
| 718 | |||
| 719 | ✗ | snprintf(text, sizeof(text), "%c %07.2f\n", s->is_rgb ? rgba[i] : yuva[i], std[c]); | |
| 720 | ✗ | draw_text(&s->draw, out, s->colors[i], X + 28, Y + s->ww + 15 * (i + 6), text, 0); | |
| 721 | } | ||
| 722 | |||
| 723 | ✗ | av_frame_free(&in); | |
| 724 | ✗ | return ff_filter_frame(outlink, out); | |
| 725 | } | ||
| 726 | |||
| 727 | ✗ | static int pixscope_process_command(AVFilterContext *ctx, const char *cmd, const char *args, | |
| 728 | char *res, int res_len, int flags) | ||
| 729 | { | ||
| 730 | int ret; | ||
| 731 | |||
| 732 | ✗ | ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); | |
| 733 | ✗ | if (ret < 0) | |
| 734 | ✗ | return ret; | |
| 735 | |||
| 736 | ✗ | return pixscope_config_input(ctx->inputs[0]); | |
| 737 | } | ||
| 738 | |||
| 739 | static const AVFilterPad pixscope_inputs[] = { | ||
| 740 | { | ||
| 741 | .name = "default", | ||
| 742 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 743 | .filter_frame = pixscope_filter_frame, | ||
| 744 | .config_props = pixscope_config_input, | ||
| 745 | }, | ||
| 746 | }; | ||
| 747 | |||
| 748 | const FFFilter ff_vf_pixscope = { | ||
| 749 | .p.name = "pixscope", | ||
| 750 | .p.description = NULL_IF_CONFIG_SMALL("Pixel data analysis."), | ||
| 751 | .p.priv_class = &pixscope_class, | ||
| 752 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
| 753 | .priv_size = sizeof(PixscopeContext), | ||
| 754 | FILTER_INPUTS(pixscope_inputs), | ||
| 755 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 756 | FILTER_QUERY_FUNC2(query_formats), | ||
| 757 | .process_command = pixscope_process_command, | ||
| 758 | }; | ||
| 759 | |||
| 760 | typedef struct PixelValues { | ||
| 761 | uint16_t p[4]; | ||
| 762 | } PixelValues; | ||
| 763 | |||
| 764 | typedef struct OscilloscopeContext { | ||
| 765 | const AVClass *class; | ||
| 766 | |||
| 767 | float xpos, ypos; | ||
| 768 | float tx, ty; | ||
| 769 | float size; | ||
| 770 | float tilt; | ||
| 771 | float theight, twidth; | ||
| 772 | float o; | ||
| 773 | int components; | ||
| 774 | int grid; | ||
| 775 | int statistics; | ||
| 776 | int scope; | ||
| 777 | |||
| 778 | int x1, y1, x2, y2; | ||
| 779 | int ox, oy; | ||
| 780 | int height, width; | ||
| 781 | |||
| 782 | int max; | ||
| 783 | int nb_planes; | ||
| 784 | int nb_comps; | ||
| 785 | int is_rgb; | ||
| 786 | uint8_t rgba_map[4]; | ||
| 787 | FFDrawContext draw; | ||
| 788 | FFDrawColor dark; | ||
| 789 | FFDrawColor black; | ||
| 790 | FFDrawColor white; | ||
| 791 | FFDrawColor green; | ||
| 792 | FFDrawColor blue; | ||
| 793 | FFDrawColor red; | ||
| 794 | FFDrawColor cyan; | ||
| 795 | FFDrawColor magenta; | ||
| 796 | FFDrawColor gray; | ||
| 797 | FFDrawColor *colors[4]; | ||
| 798 | |||
| 799 | int nb_values; | ||
| 800 | PixelValues *values; | ||
| 801 | |||
| 802 | void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value); | ||
| 803 | void (*draw_trace)(struct OscilloscopeContext *s, AVFrame *frame); | ||
| 804 | } OscilloscopeContext; | ||
| 805 | |||
| 806 | #define OOFFSET(x) offsetof(OscilloscopeContext, x) | ||
| 807 | |||
| 808 | static const AVOption oscilloscope_options[] = { | ||
| 809 | { "x", "set scope x position", OOFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR }, | ||
| 810 | { "y", "set scope y position", OOFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR }, | ||
| 811 | { "s", "set scope size", OOFFSET(size), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGSR }, | ||
| 812 | { "t", "set scope tilt", OOFFSET(tilt), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR }, | ||
| 813 | { "o", "set trace opacity", OOFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGSR }, | ||
| 814 | { "tx", "set trace x position", OOFFSET(tx), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGSR }, | ||
| 815 | { "ty", "set trace y position", OOFFSET(ty), AV_OPT_TYPE_FLOAT, {.dbl=0.9}, 0, 1, FLAGSR }, | ||
| 816 | { "tw", "set trace width", OOFFSET(twidth), AV_OPT_TYPE_FLOAT, {.dbl=0.8},.1, 1, FLAGSR }, | ||
| 817 | { "th", "set trace height", OOFFSET(theight), AV_OPT_TYPE_FLOAT, {.dbl=0.3},.1, 1, FLAGSR }, | ||
| 818 | { "c", "set components to trace", OOFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGSR }, | ||
| 819 | { "g", "draw trace grid", OOFFSET(grid), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGSR }, | ||
| 820 | { "st", "draw statistics", OOFFSET(statistics), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGSR }, | ||
| 821 | { "sc", "draw scope", OOFFSET(scope), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGSR }, | ||
| 822 | { NULL } | ||
| 823 | }; | ||
| 824 | |||
| 825 | AVFILTER_DEFINE_CLASS(oscilloscope); | ||
| 826 | |||
| 827 | ✗ | static void oscilloscope_uninit(AVFilterContext *ctx) | |
| 828 | { | ||
| 829 | ✗ | OscilloscopeContext *s = ctx->priv; | |
| 830 | |||
| 831 | ✗ | av_freep(&s->values); | |
| 832 | ✗ | } | |
| 833 | |||
| 834 | ✗ | static void draw_line(FFDrawContext *draw, int x0, int y0, int x1, int y1, | |
| 835 | AVFrame *out, FFDrawColor *color) | ||
| 836 | { | ||
| 837 | ✗ | int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1; | |
| 838 | ✗ | int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1; | |
| 839 | ✗ | int err = (dx > dy ? dx : -dy) / 2, e2; | |
| 840 | int p, i; | ||
| 841 | |||
| 842 | for (;;) { | ||
| 843 | ✗ | if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) { | |
| 844 | ✗ | for (p = 0; p < draw->nb_planes; p++) { | |
| 845 | ✗ | if (draw->desc->comp[p].depth == 8) { | |
| 846 | ✗ | if (draw->nb_planes == 1) { | |
| 847 | ✗ | for (i = 0; i < draw->desc->nb_components; i++) { | |
| 848 | ✗ | out->data[0][y0 * out->linesize[0] + x0 * draw->pixelstep[0] + i] = color->comp[0].u8[i]; | |
| 849 | } | ||
| 850 | } else { | ||
| 851 | ✗ | out->data[p][out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p])] = color->comp[p].u8[0]; | |
| 852 | } | ||
| 853 | } else { | ||
| 854 | ✗ | if (draw->nb_planes == 1) { | |
| 855 | ✗ | for (i = 0; i < draw->desc->nb_components; i++) { | |
| 856 | ✗ | AV_WN16(out->data[0] + y0 * out->linesize[0] + (x0 * draw->pixelstep[0] + i), color->comp[0].u16[i]); | |
| 857 | } | ||
| 858 | } else { | ||
| 859 | ✗ | AV_WN16(out->data[p] + out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p]) * 2, color->comp[p].u16[0]); | |
| 860 | } | ||
| 861 | } | ||
| 862 | } | ||
| 863 | } | ||
| 864 | |||
| 865 | ✗ | if (x0 == x1 && y0 == y1) | |
| 866 | ✗ | break; | |
| 867 | |||
| 868 | ✗ | e2 = err; | |
| 869 | |||
| 870 | ✗ | if (e2 >-dx) { | |
| 871 | ✗ | err -= dy; | |
| 872 | ✗ | x0 += sx; | |
| 873 | } | ||
| 874 | |||
| 875 | ✗ | if (e2 < dy) { | |
| 876 | ✗ | err += dx; | |
| 877 | ✗ | y0 += sy; | |
| 878 | } | ||
| 879 | } | ||
| 880 | ✗ | } | |
| 881 | |||
| 882 | ✗ | static void draw_trace8(OscilloscopeContext *s, AVFrame *frame) | |
| 883 | { | ||
| 884 | int i, c; | ||
| 885 | |||
| 886 | ✗ | for (i = 1; i < s->nb_values; i++) { | |
| 887 | ✗ | for (c = 0; c < s->nb_comps; c++) { | |
| 888 | ✗ | if ((1 << c) & s->components) { | |
| 889 | ✗ | int x = i * s->width / s->nb_values; | |
| 890 | ✗ | int px = (i - 1) * s->width / s->nb_values; | |
| 891 | ✗ | int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / 256; | |
| 892 | ✗ | int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / 256; | |
| 893 | |||
| 894 | ✗ | draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]); | |
| 895 | } | ||
| 896 | } | ||
| 897 | } | ||
| 898 | ✗ | } | |
| 899 | |||
| 900 | |||
| 901 | ✗ | static void draw_trace16(OscilloscopeContext *s, AVFrame *frame) | |
| 902 | { | ||
| 903 | int i, c; | ||
| 904 | |||
| 905 | ✗ | for (i = 1; i < s->nb_values; i++) { | |
| 906 | ✗ | for (c = 0; c < s->nb_comps; c++) { | |
| 907 | ✗ | if ((1 << c) & s->components) { | |
| 908 | ✗ | int x = i * s->width / s->nb_values; | |
| 909 | ✗ | int px = (i - 1) * s->width / s->nb_values; | |
| 910 | ✗ | int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / s->max; | |
| 911 | ✗ | int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / s->max; | |
| 912 | |||
| 913 | ✗ | draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]); | |
| 914 | } | ||
| 915 | } | ||
| 916 | } | ||
| 917 | ✗ | } | |
| 918 | |||
| 919 | ✗ | static void update_oscilloscope(AVFilterContext *ctx) | |
| 920 | { | ||
| 921 | ✗ | OscilloscopeContext *s = ctx->priv; | |
| 922 | ✗ | AVFilterLink *inlink = ctx->inputs[0]; | |
| 923 | int cx, cy, size; | ||
| 924 | double tilt; | ||
| 925 | |||
| 926 | ✗ | ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} ); | |
| 927 | ✗ | s->height = s->theight * inlink->h; | |
| 928 | ✗ | s->width = s->twidth * inlink->w; | |
| 929 | ✗ | size = hypot(inlink->w, inlink->h); | |
| 930 | ✗ | size *= s->size; | |
| 931 | ✗ | tilt = (s->tilt - 0.5) * M_PI; | |
| 932 | ✗ | cx = s->xpos * (inlink->w - 1); | |
| 933 | ✗ | cy = s->ypos * (inlink->h - 1); | |
| 934 | ✗ | s->x1 = cx - size / 2.0 * cos(tilt); | |
| 935 | ✗ | s->x2 = cx + size / 2.0 * cos(tilt); | |
| 936 | ✗ | s->y1 = cy - size / 2.0 * sin(tilt); | |
| 937 | ✗ | s->y2 = cy + size / 2.0 * sin(tilt); | |
| 938 | ✗ | s->ox = (inlink->w - s->width) * s->tx; | |
| 939 | ✗ | s->oy = (inlink->h - s->height) * s->ty; | |
| 940 | ✗ | } | |
| 941 | |||
| 942 | ✗ | static int oscilloscope_config_input(AVFilterLink *inlink) | |
| 943 | { | ||
| 944 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 945 | ✗ | OscilloscopeContext *s = ctx->priv; | |
| 946 | int size; | ||
| 947 | int ret; | ||
| 948 | |||
| 949 | ✗ | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
| 950 | ✗ | ret = ff_draw_init(&s->draw, inlink->format, 0); | |
| 951 | ✗ | if (ret < 0) { | |
| 952 | ✗ | av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n"); | |
| 953 | ✗ | return ret; | |
| 954 | } | ||
| 955 | ✗ | ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} ); | |
| 956 | ✗ | ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} ); | |
| 957 | ✗ | ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} ); | |
| 958 | ✗ | ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} ); | |
| 959 | ✗ | ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} ); | |
| 960 | ✗ | ff_draw_color(&s->draw, &s->cyan, (uint8_t[]){ 0, 255, 255, 255} ); | |
| 961 | ✗ | ff_draw_color(&s->draw, &s->magenta, (uint8_t[]){ 255, 0, 255, 255} ); | |
| 962 | ✗ | ff_draw_color(&s->draw, &s->gray, (uint8_t[]){ 128, 128, 128, 255} ); | |
| 963 | ✗ | s->nb_comps = s->draw.desc->nb_components; | |
| 964 | ✗ | s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB; | |
| 965 | |||
| 966 | ✗ | if (s->is_rgb) { | |
| 967 | ✗ | s->colors[0] = &s->red; | |
| 968 | ✗ | s->colors[1] = &s->green; | |
| 969 | ✗ | s->colors[2] = &s->blue; | |
| 970 | ✗ | s->colors[3] = &s->white; | |
| 971 | ✗ | ff_fill_rgba_map(s->rgba_map, inlink->format); | |
| 972 | } else { | ||
| 973 | ✗ | s->colors[0] = &s->white; | |
| 974 | ✗ | s->colors[1] = &s->cyan; | |
| 975 | ✗ | s->colors[2] = &s->magenta; | |
| 976 | ✗ | s->colors[3] = &s->white; | |
| 977 | ✗ | s->rgba_map[0] = 0; | |
| 978 | ✗ | s->rgba_map[1] = 1; | |
| 979 | ✗ | s->rgba_map[2] = 2; | |
| 980 | ✗ | s->rgba_map[3] = 3; | |
| 981 | } | ||
| 982 | |||
| 983 | ✗ | if (s->draw.desc->comp[0].depth <= 8) { | |
| 984 | ✗ | s->pick_color = pick_color8; | |
| 985 | ✗ | s->draw_trace = draw_trace8; | |
| 986 | } else { | ||
| 987 | ✗ | s->pick_color = pick_color16; | |
| 988 | ✗ | s->draw_trace = draw_trace16; | |
| 989 | } | ||
| 990 | |||
| 991 | ✗ | s->max = (1 << s->draw.desc->comp[0].depth); | |
| 992 | ✗ | size = hypot(inlink->w, inlink->h); | |
| 993 | |||
| 994 | ✗ | s->values = av_calloc(size, sizeof(*s->values)); | |
| 995 | ✗ | if (!s->values) | |
| 996 | ✗ | return AVERROR(ENOMEM); | |
| 997 | |||
| 998 | ✗ | update_oscilloscope(inlink->dst); | |
| 999 | |||
| 1000 | ✗ | return 0; | |
| 1001 | } | ||
| 1002 | |||
| 1003 | ✗ | static void draw_scope(OscilloscopeContext *s, int x0, int y0, int x1, int y1, | |
| 1004 | AVFrame *out, PixelValues *p, int state) | ||
| 1005 | { | ||
| 1006 | ✗ | int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1; | |
| 1007 | ✗ | int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1; | |
| 1008 | ✗ | int err = (dx > dy ? dx : -dy) / 2, e2; | |
| 1009 | |||
| 1010 | for (;;) { | ||
| 1011 | ✗ | if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) { | |
| 1012 | ✗ | FFDrawColor color = { { 0 } }; | |
| 1013 | ✗ | int value[4] = { 0 }; | |
| 1014 | |||
| 1015 | ✗ | s->pick_color(&s->draw, &color, out, x0, y0, value); | |
| 1016 | ✗ | s->values[s->nb_values].p[0] = value[0]; | |
| 1017 | ✗ | s->values[s->nb_values].p[1] = value[1]; | |
| 1018 | ✗ | s->values[s->nb_values].p[2] = value[2]; | |
| 1019 | ✗ | s->values[s->nb_values].p[3] = value[3]; | |
| 1020 | ✗ | s->nb_values++; | |
| 1021 | |||
| 1022 | ✗ | if (s->scope) { | |
| 1023 | ✗ | if (s->draw.desc->comp[0].depth == 8) { | |
| 1024 | ✗ | if (s->draw.nb_planes == 1) { | |
| 1025 | int i; | ||
| 1026 | |||
| 1027 | ✗ | for (i = 0; i < s->nb_comps; i++) | |
| 1028 | ✗ | out->data[0][out->linesize[0] * y0 + x0 * s->draw.pixelstep[0] + i] = 255 * ((s->nb_values + state) & 1); | |
| 1029 | } else { | ||
| 1030 | ✗ | out->data[0][out->linesize[0] * y0 + x0] = 255 * ((s->nb_values + state) & 1); | |
| 1031 | } | ||
| 1032 | } else { | ||
| 1033 | ✗ | if (s->draw.nb_planes == 1) { | |
| 1034 | int i; | ||
| 1035 | |||
| 1036 | ✗ | for (i = 0; i < s->nb_comps; i++) | |
| 1037 | ✗ | AV_WN16(out->data[0] + out->linesize[0] * y0 + x0 * s->draw.pixelstep[0] + i, (s->max - 1) * ((s->nb_values + state) & 1)); | |
| 1038 | } else { | ||
| 1039 | ✗ | AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0, (s->max - 1) * ((s->nb_values + state) & 1)); | |
| 1040 | } | ||
| 1041 | } | ||
| 1042 | } | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | ✗ | if (x0 == x1 && y0 == y1) | |
| 1046 | ✗ | break; | |
| 1047 | |||
| 1048 | ✗ | e2 = err; | |
| 1049 | |||
| 1050 | ✗ | if (e2 >-dx) { | |
| 1051 | ✗ | err -= dy; | |
| 1052 | ✗ | x0 += sx; | |
| 1053 | } | ||
| 1054 | |||
| 1055 | ✗ | if (e2 < dy) { | |
| 1056 | ✗ | err += dx; | |
| 1057 | ✗ | y0 += sy; | |
| 1058 | } | ||
| 1059 | } | ||
| 1060 | ✗ | } | |
| 1061 | |||
| 1062 | ✗ | static int oscilloscope_filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
| 1063 | { | ||
| 1064 | ✗ | FilterLink *inl = ff_filter_link(inlink); | |
| 1065 | ✗ | AVFilterContext *ctx = inlink->dst; | |
| 1066 | ✗ | OscilloscopeContext *s = ctx->priv; | |
| 1067 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 1068 | ✗ | float average[4] = { 0 }; | |
| 1069 | ✗ | int max[4] = { 0 }; | |
| 1070 | ✗ | int min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX }; | |
| 1071 | int i, c; | ||
| 1072 | |||
| 1073 | ✗ | s->nb_values = 0; | |
| 1074 | ✗ | draw_scope(s, s->x1, s->y1, s->x2, s->y2, frame, s->values, inl->frame_count_in & 1); | |
| 1075 | ✗ | ff_blend_rectangle(&s->draw, &s->dark, frame->data, frame->linesize, | |
| 1076 | frame->width, frame->height, | ||
| 1077 | ✗ | s->ox, s->oy, s->width, s->height + 20 * s->statistics); | |
| 1078 | |||
| 1079 | ✗ | if (s->grid && outlink->h >= 10) { | |
| 1080 | ✗ | ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize, | |
| 1081 | ✗ | s->ox, s->oy, s->width - 1, 1); | |
| 1082 | |||
| 1083 | ✗ | for (i = 1; i < 5; i++) { | |
| 1084 | ✗ | ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize, | |
| 1085 | ✗ | s->ox, s->oy + i * (s->height - 1) / 4, s->width, 1); | |
| 1086 | } | ||
| 1087 | |||
| 1088 | ✗ | for (i = 0; i < 10; i++) { | |
| 1089 | ✗ | ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize, | |
| 1090 | ✗ | s->ox + i * (s->width - 1) / 10, s->oy, 1, s->height); | |
| 1091 | } | ||
| 1092 | |||
| 1093 | ✗ | ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize, | |
| 1094 | ✗ | s->ox + s->width - 1, s->oy, 1, s->height); | |
| 1095 | } | ||
| 1096 | |||
| 1097 | ✗ | s->draw_trace(s, frame); | |
| 1098 | |||
| 1099 | ✗ | for (i = 0; i < s->nb_values; i++) { | |
| 1100 | ✗ | for (c = 0; c < s->nb_comps; c++) { | |
| 1101 | ✗ | if ((1 << c) & s->components) { | |
| 1102 | ✗ | max[c] = FFMAX(max[c], s->values[i].p[s->rgba_map[c]]); | |
| 1103 | ✗ | min[c] = FFMIN(min[c], s->values[i].p[s->rgba_map[c]]); | |
| 1104 | ✗ | average[c] += s->values[i].p[s->rgba_map[c]]; | |
| 1105 | } | ||
| 1106 | } | ||
| 1107 | } | ||
| 1108 | ✗ | for (c = 0; c < s->nb_comps; c++) { | |
| 1109 | ✗ | average[c] /= s->nb_values; | |
| 1110 | } | ||
| 1111 | |||
| 1112 | ✗ | if (s->statistics && s->height > 10 && s->width > 280 * av_popcount(s->components)) { | |
| 1113 | ✗ | for (c = 0, i = 0; c < s->nb_comps; c++) { | |
| 1114 | ✗ | if ((1 << c) & s->components) { | |
| 1115 | ✗ | const char rgba[4] = { 'R', 'G', 'B', 'A' }; | |
| 1116 | ✗ | const char yuva[4] = { 'Y', 'U', 'V', 'A' }; | |
| 1117 | char text[128]; | ||
| 1118 | |||
| 1119 | ✗ | snprintf(text, sizeof(text), "%c avg:%.1f min:%d max:%d\n", s->is_rgb ? rgba[c] : yuva[c], average[c], min[c], max[c]); | |
| 1120 | ✗ | draw_text(&s->draw, frame, &s->white, s->ox + 2 + 280 * i++, s->oy + s->height + 4, text, 0); | |
| 1121 | } | ||
| 1122 | } | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | ✗ | return ff_filter_frame(outlink, frame); | |
| 1126 | } | ||
| 1127 | |||
| 1128 | ✗ | static int oscilloscope_process_command(AVFilterContext *ctx, const char *cmd, const char *args, | |
| 1129 | char *res, int res_len, int flags) | ||
| 1130 | { | ||
| 1131 | int ret; | ||
| 1132 | |||
| 1133 | ✗ | ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); | |
| 1134 | ✗ | if (ret < 0) | |
| 1135 | ✗ | return ret; | |
| 1136 | |||
| 1137 | ✗ | update_oscilloscope(ctx); | |
| 1138 | |||
| 1139 | ✗ | return 0; | |
| 1140 | } | ||
| 1141 | |||
| 1142 | static const AVFilterPad oscilloscope_inputs[] = { | ||
| 1143 | { | ||
| 1144 | .name = "default", | ||
| 1145 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 1146 | .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, | ||
| 1147 | .filter_frame = oscilloscope_filter_frame, | ||
| 1148 | .config_props = oscilloscope_config_input, | ||
| 1149 | }, | ||
| 1150 | }; | ||
| 1151 | |||
| 1152 | const FFFilter ff_vf_oscilloscope = { | ||
| 1153 | .p.name = "oscilloscope", | ||
| 1154 | .p.description = NULL_IF_CONFIG_SMALL("2D Video Oscilloscope."), | ||
| 1155 | .p.priv_class = &oscilloscope_class, | ||
| 1156 | .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
| 1157 | .priv_size = sizeof(OscilloscopeContext), | ||
| 1158 | .uninit = oscilloscope_uninit, | ||
| 1159 | FILTER_INPUTS(oscilloscope_inputs), | ||
| 1160 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 1161 | FILTER_QUERY_FUNC2(query_formats), | ||
| 1162 | .process_command = oscilloscope_process_command, | ||
| 1163 | }; | ||
| 1164 |