diff options
-rw-r--r-- | doc/filters.texi | 3 | ||||
-rw-r--r-- | libavfilter/vf_fillborders.c | 97 |
2 files changed, 99 insertions, 1 deletions
diff --git a/doc/filters.texi b/doc/filters.texi index 7c1d3e49ae..d9e5b79695 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -12280,6 +12280,9 @@ fill pixels using wrapping @item fade fade pixels to constant value + +@item margins +fill pixels at top and bottom with weighted averages pixels near borders @end table Default is @var{smear}. diff --git a/libavfilter/vf_fillborders.c b/libavfilter/vf_fillborders.c index 36b96f9e51..2515bc9de9 100644 --- a/libavfilter/vf_fillborders.c +++ b/libavfilter/vf_fillborders.c @@ -32,7 +32,7 @@ enum { Y, U, V, A }; enum { R, G, B }; -enum FillMode { FM_SMEAR, FM_MIRROR, FM_FIXED, FM_REFLECT, FM_WRAP, FM_FADE, FM_NB_MODES }; +enum FillMode { FM_SMEAR, FM_MIRROR, FM_FIXED, FM_REFLECT, FM_WRAP, FM_FADE, FM_MARGINS, FM_NB_MODES }; typedef struct Borders { int left, right, top, bottom; @@ -495,6 +495,99 @@ static void fade_borders16(FillBordersContext *s, AVFrame *frame) } } +static void margins_borders8(FillBordersContext *s, AVFrame *frame) +{ + for (int p = 0; p < s->nb_planes; p++) { + uint8_t *ptr = (uint8_t *)frame->data[p]; + const int linesize = frame->linesize[p]; + const int left = s->borders[p].left; + const int right = s->borders[p].right; + const int top = s->borders[p].top; + const int bottom = s->borders[p].bottom; + const int width = s->planewidth[p]; + const int height = s->planeheight[p]; + + for (int y = top; y < height - bottom; y++) { + memset(ptr + linesize * y, ptr[linesize * y + left], left); + memset(ptr + linesize * y + width - right, (ptr + linesize * y + width - right)[-1], right); + } + + for (int y = top - 1; y >= 0; y--) { + ptr[linesize * y] = ptr[linesize * (y + 1)]; + memcpy(ptr + linesize * y + width - 8, ptr + linesize * (y + 1) + width - 8, 8); + + for (int x = 1; x < width - 8; x++) { + int prev = ptr[linesize * (y + 1) + x - 1]; + int cur = ptr[linesize * (y + 1) + x]; + int next = ptr[linesize * (y + 1) + x + 1]; + + ptr[linesize * y + x] = (3 * prev + 2 * cur + 3 * next + 4) >> 3; + } + } + + for (int y = height - bottom; y < height; y++) { + ptr[linesize * y] = ptr[linesize * (y - 1)]; + memcpy(ptr + linesize * y + width - 8, ptr + linesize * (y - 1) + width - 8, 8); + + for (int x = 1; x < width - 8; x++) { + int prev = ptr[linesize * (y - 1) + x - 1]; + int cur = ptr[linesize * (y - 1) + x]; + int next = ptr[linesize * (y - 1) + x + 1]; + + ptr[linesize * y + x] = (3 * prev + 2 * cur + 3 * next + 4) >> 3; + } + } + } +} + +static void margins_borders16(FillBordersContext *s, AVFrame *frame) +{ + for (int p = 0; p < s->nb_planes; p++) { + uint16_t *ptr = (uint16_t *)frame->data[p]; + const int linesize = frame->linesize[p] / 2; + const int left = s->borders[p].left; + const int right = s->borders[p].right; + const int top = s->borders[p].top; + const int bottom = s->borders[p].bottom; + const int width = s->planewidth[p]; + const int height = s->planeheight[p]; + + for (int y = top; y < height - bottom; y++) { + for (int x = 0; x < left; x++) + ptr[linesize * y + x] = ptr[linesize * y + left]; + + for (int x = 0; x < right; x++) + ptr[linesize * y + width - right + x] = ptr[linesize * y + width - right - 1]; + } + + for (int y = top - 1; y >= 0; y--) { + ptr[linesize * y] = ptr[linesize * (y + 1)]; + memcpy(ptr + linesize * y + width - 8, ptr + linesize * (y + 1) + width - 8, 16); + + for (int x = 1; x < width - 8; x++) { + int prev = ptr[linesize * (y + 1) + x - 1]; + int cur = ptr[linesize * (y + 1) + x]; + int next = ptr[linesize * (y + 1) + x + 1]; + + ptr[linesize * y + x] = (3 * prev + 2 * cur + 3 * next + 4) >> 3; + } + } + + for (int y = height - bottom; y < height; y++) { + ptr[linesize * y] = ptr[linesize * (y - 1)]; + memcpy(ptr + linesize * y + width - 8, ptr + linesize * (y - 1) + width - 8, 16); + + for (int x = 1; x < width - 8; x++) { + int prev = ptr[linesize * (y - 1) + x - 1]; + int cur = ptr[linesize * (y - 1) + x]; + int next = ptr[linesize * (y - 1) + x + 1]; + + ptr[linesize * y + x] = (3 * prev + 2 * cur + 3 * next + 4) >> 3; + } + } + } +} + static int filter_frame(AVFilterLink *inlink, AVFrame *frame) { FillBordersContext *s = inlink->dst->priv; @@ -554,6 +647,7 @@ static int config_input(AVFilterLink *inlink) case FM_REFLECT:s->fillborders = s->depth <= 8 ? reflect_borders8: reflect_borders16;break; case FM_WRAP: s->fillborders = s->depth <= 8 ? wrap_borders8 : wrap_borders16; break; case FM_FADE: s->fillborders = s->depth <= 8 ? fade_borders8 : fade_borders16; break; + case FM_MARGINS:s->fillborders = s->depth <= 8 ? margins_borders8: margins_borders16;break; default: av_assert0(0); } @@ -603,6 +697,7 @@ static const AVOption fillborders_options[] = { { "reflect",NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_REFLECT},0, 0, FLAGS, "mode" }, { "wrap", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_WRAP}, 0, 0, FLAGS, "mode" }, { "fade", NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_FADE}, 0, 0, FLAGS, "mode" }, + { "margins",NULL, 0, AV_OPT_TYPE_CONST, {.i64=FM_MARGINS},0, 0, FLAGS, "mode" }, { "color", "set the color for the fixed/fade mode", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str = "black"}, .flags = FLAGS }, { NULL } }; |