Extract common code of filter_frame() and drawgrid_filter_frame() to draw_region().
Unified · Split
| @@ -85,6 +85,61 @@ typedef struct DrawBoxContext { · 84 unmodified lines | |||
| 85 | 85 | ||
| 86 | static const int NUM_EXPR_EVALS = 5; | 86 | static const int NUM_EXPR_EVALS = 5; |
| 87 | 87 | ||
| 88 | typedef int (*PixelBelongsToRegion)(DrawBoxContext *s, int x, int y); | ||
| 89 | |||
| 90 | #define ASSIGN_THREE_CHANNELS \ | ||
| 91 | row[0] = frame->data[0] + y * frame->linesize[0]; \ | ||
| 92 | row[1] = frame->data[1] + (y >> ctx->vsub) * frame->linesize[1]; \ | ||
| 93 | row[2] = frame->data[2] + (y >> ctx->vsub) * frame->linesize[2]; | ||
| 94 | |||
| 95 | #define ASSIGN_FOUR_CHANNELS \ | ||
| 96 | ASSIGN_THREE_CHANNELS \ | ||
| 97 | row[3] = frame->data[3] + y * frame->linesize[3]; | ||
| 98 | |||
| 99 | static void draw_region(AVFrame *frame, DrawBoxContext *ctx, int left, int top, int right, int down, | ||
| 100 | PixelBelongsToRegion pixel_belongs_to_region) | ||
| 101 | { | ||
| 102 | unsigned char *row[4]; | ||
| 103 | int x, y; | ||
| 104 | if (ctx->have_alpha && ctx->replace) { | ||
| 105 | for (y = top; y < down; y++) { | ||
| 106 | ASSIGN_FOUR_CHANNELS | ||
| 107 | if (ctx->invert_color) { | ||
| 108 | for (x = left; x < right; x++) | ||
| 109 | if (pixel_belongs_to_region(ctx, x, y)) | ||
| 110 | row[0][x] = 0xff - row[0][x]; | ||
| 111 | } else { | ||
| 112 | for (x = left; x < right; x++) { | ||
| 113 | if (pixel_belongs_to_region(ctx, x, y)) { | ||
| 114 | row[0][x ] = ctx->yuv_color[Y]; | ||
| 115 | row[1][x >> ctx->hsub] = ctx->yuv_color[U]; | ||
| 116 | row[2][x >> ctx->hsub] = ctx->yuv_color[V]; | ||
| 117 | row[3][x ] = ctx->yuv_color[A]; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } else { | ||
| 123 | for (y = top; y < down; y++) { | ||
| 124 | ASSIGN_THREE_CHANNELS | ||
| 125 | if (ctx->invert_color) { | ||
| 126 | if (pixel_belongs_to_region(ctx, x, y)) | ||
| 127 | row[0][x] = 0xff - row[0][x]; | ||
| 128 | } else { | ||
| 129 | for (x = left; x < right; x++) { | ||
| 130 | double alpha = (double)ctx->yuv_color[A] / 255; | ||
| 131 | |||
| 132 | if (pixel_belongs_to_region(ctx, x, y)) { | ||
| 133 | row[0][x ] = (1 - alpha) * row[0][x ] + alpha * ctx->yuv_color[Y]; | ||
| 134 | row[1][x >> ctx->hsub] = (1 - alpha) * row[1][x >> ctx->hsub] + alpha * ctx->yuv_color[U]; | ||
| 135 | row[2][x >> ctx->hsub] = (1 - alpha) * row[2][x >> ctx->hsub] + alpha * ctx->yuv_color[V]; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 88 | static av_cold int init(AVFilterContext *ctx) | 143 | static av_cold int init(AVFilterContext *ctx) |
| 89 | { | 144 | { |
| 90 | DrawBoxContext *s = ctx->priv; | 145 | DrawBoxContext *s = ctx->priv; |
| @@ -217,58 +272,9 @@ static av_pure av_always_inline int pixel_belongs_to_box(DrawBoxContext *s, int · 126 unmodified lines | |||
| 217 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | 272 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) |
| 218 | { | 273 | { |
| 219 | DrawBoxContext *s = inlink->dst->priv; | 274 | DrawBoxContext *s = inlink->dst->priv; |
| 220 | int plane, x, y, xb = s->x, yb = s->y; | ||
| 221 | unsigned char *row[4]; | ||
| 222 | |||
| 223 | if (s->have_alpha && s->replace) { | ||
| 224 | for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) { | ||
| 225 | row[0] = frame->data[0] + y * frame->linesize[0]; | ||
| 226 | row[3] = frame->data[3] + y * frame->linesize[3]; | ||
| 227 | |||
| 228 | for (plane = 1; plane < 3; plane++) | ||
| 229 | row[plane] = frame->data[plane] + | ||
| 230 | frame->linesize[plane] * (y >> s->vsub); | ||
| 231 | |||
| 232 | if (s->invert_color) { | ||
| 233 | for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) | ||
| 234 | if (pixel_belongs_to_box(s, x, y)) | ||
| 235 | row[0][x] = 0xff - row[0][x]; | ||
| 236 | } else { | ||
| 237 | for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) { | ||
| 238 | if (pixel_belongs_to_box(s, x, y)) { | ||
| 239 | row[0][x ] = s->yuv_color[Y]; | ||
| 240 | row[1][x >> s->hsub] = s->yuv_color[U]; | ||
| 241 | row[2][x >> s->hsub] = s->yuv_color[V]; | ||
| 242 | row[3][x ] = s->yuv_color[A]; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } else { | ||
| 248 | for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) { | ||
| 249 | row[0] = frame->data[0] + y * frame->linesize[0]; | ||
| 250 | 275 | ||
| 251 | for (plane = 1; plane < 3; plane++) | 276 | draw_region(frame, s, FFMAX(s->x, 0), FFMAX(s->y, 0), FFMIN(s->x + s->w, frame->width), |
| 252 | row[plane] = frame->data[plane] + | 277 | FFMIN(s->y + s->h, frame->height), pixel_belongs_to_box); |
| 253 | frame->linesize[plane] * (y >> s->vsub); | ||
| 254 | |||
| 255 | if (s->invert_color) { | ||
| 256 | for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) | ||
| 257 | if (pixel_belongs_to_box(s, x, y)) | ||
| 258 | row[0][x] = 0xff - row[0][x]; | ||
| 259 | } else { | ||
| 260 | for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) { | ||
| 261 | double alpha = (double)s->yuv_color[A] / 255; | ||
| 262 | |||
| 263 | if (pixel_belongs_to_box(s, x, y)) { | ||
| 264 | row[0][x ] = (1 - alpha) * row[0][x ] + alpha * s->yuv_color[Y]; | ||
| 265 | row[1][x >> s->hsub] = (1 - alpha) * row[1][x >> s->hsub] + alpha * s->yuv_color[U]; | ||
| 266 | row[2][x >> s->hsub] = (1 - alpha) * row[2][x >> s->hsub] + alpha * s->yuv_color[V]; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | } | ||
| 272 | 278 | ||
| 273 | return ff_filter_frame(inlink->dst->outputs[0], frame); | 279 | return ff_filter_frame(inlink->dst->outputs[0], frame); |
| 274 | } | 280 | } |
| @@ -389,58 +395,8 @@ static av_pure av_always_inline int pixel_belongs_to_grid(DrawBoxContext *drawgr · 114 unmodified lines | |||
| 389 | static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame) | 395 | static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame) |
| 390 | { | 396 | { |
| 391 | DrawBoxContext *drawgrid = inlink->dst->priv; | 397 | DrawBoxContext *drawgrid = inlink->dst->priv; |
| 392 | int plane, x, y; | ||
| 393 | uint8_t *row[4]; | ||
| 394 | |||
| 395 | if (drawgrid->have_alpha && drawgrid->replace) { | ||
| 396 | for (y = 0; y < frame->height; y++) { | ||
| 397 | row[0] = frame->data[0] + y * frame->linesize[0]; | ||
| 398 | row[3] = frame->data[3] + y * frame->linesize[3]; | ||
| 399 | |||
| 400 | for (plane = 1; plane < 3; plane++) | ||
| 401 | row[plane] = frame->data[plane] + | ||
| 402 | frame->linesize[plane] * (y >> drawgrid->vsub); | ||
| 403 | |||
| 404 | if (drawgrid->invert_color) { | ||
| 405 | for (x = 0; x < frame->width; x++) | ||
| 406 | if (pixel_belongs_to_grid(drawgrid, x, y)) | ||
| 407 | row[0][x] = 0xff - row[0][x]; | ||
| 408 | } else { | ||
| 409 | for (x = 0; x < frame->width; x++) { | ||
| 410 | if (pixel_belongs_to_grid(drawgrid, x, y)) { | ||
| 411 | row[0][x ] = drawgrid->yuv_color[Y]; | ||
| 412 | row[1][x >> drawgrid->hsub] = drawgrid->yuv_color[U]; | ||
| 413 | row[2][x >> drawgrid->hsub] = drawgrid->yuv_color[V]; | ||
| 414 | row[3][x ] = drawgrid->yuv_color[A]; | ||
| 415 | } | ||
| 416 | } | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } else { | ||
| 420 | for (y = 0; y < frame->height; y++) { | ||
| 421 | row[0] = frame->data[0] + y * frame->linesize[0]; | ||
| 422 | 398 | ||
| 423 | for (plane = 1; plane < 3; plane++) | 399 | draw_region(frame, drawgrid, 0, 0, frame->width, frame->height, pixel_belongs_to_grid); |
| 424 | row[plane] = frame->data[plane] + | ||
| 425 | frame->linesize[plane] * (y >> drawgrid->vsub); | ||
| 426 | |||
| 427 | if (drawgrid->invert_color) { | ||
| 428 | for (x = 0; x < frame->width; x++) | ||
| 429 | if (pixel_belongs_to_grid(drawgrid, x, y)) | ||
| 430 | row[0][x] = 0xff - row[0][x]; | ||
| 431 | } else { | ||
| 432 | for (x = 0; x < frame->width; x++) { | ||
| 433 | double alpha = (double)drawgrid->yuv_color[A] / 255; | ||
| 434 | |||
| 435 | if (pixel_belongs_to_grid(drawgrid, x, y)) { | ||
| 436 | row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawgrid->yuv_color[Y]; | ||
| 437 | row[1][x >> drawgrid->hsub] = (1 - alpha) * row[1][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[U]; | ||
| 438 | row[2][x >> drawgrid->hsub] = (1 - alpha) * row[2][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[V]; | ||
| 439 | } | ||
| 440 | } | ||
| 441 | } | ||
| 442 | } | ||
| 443 | } | ||
| 444 | 400 | ||
| 445 | return ff_filter_frame(inlink->dst->outputs[0], frame); | 401 | return ff_filter_frame(inlink->dst->outputs[0], frame); |
| 446 | } | 402 | } |