+static void reflect_borders8(FillBordersContext *s, AVFrame *frame)
+{
+ int p, y, x;
+
+ for (p = 0; p < s->nb_planes; p++) {
+ uint8_t *ptr = frame->data[p];
+ int linesize = frame->linesize[p];
+
+ for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
+ for (x = 0; x < s->borders[p].left; x++) {
+ ptr[y * linesize + x] = ptr[y * linesize + s->borders[p].left * 2 - x];
+ }
+
+ for (x = 0; x < s->borders[p].right; x++) {
+ ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
+ ptr[y * linesize + s->planewidth[p] - s->borders[p].right - 2 - x];
+ }
+ }
+
+ for (y = 0; y < s->borders[p].top; y++) {
+ memcpy(ptr + y * linesize,
+ ptr + (s->borders[p].top * 2 - y) * linesize,
+ s->planewidth[p]);
+ }
+
+ for (y = 0; y < s->borders[p].bottom; y++) {
+ memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
+ ptr + (s->planeheight[p] - s->borders[p].bottom - 2 - y) * linesize,
+ s->planewidth[p]);
+ }
+ }
+}
+
+static void reflect_borders16(FillBordersContext *s, AVFrame *frame)
+{
+ int p, y, x;
+
+ for (p = 0; p < s->nb_planes; p++) {
+ uint16_t *ptr = (uint16_t *)frame->data[p];
+ int linesize = frame->linesize[p] / 2;
+
+ for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
+ for (x = 0; x < s->borders[p].left; x++) {
+ ptr[y * linesize + x] = ptr[y * linesize + s->borders[p].left * 2 - x];
+ }
+
+ for (x = 0; x < s->borders[p].right; x++) {
+ ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
+ ptr[y * linesize + s->planewidth[p] - s->borders[p].right - 2 - x];
+ }
+ }
+
+ for (y = 0; y < s->borders[p].top; y++) {
+ memcpy(ptr + y * linesize,
+ ptr + (s->borders[p].top * 2 - y) * linesize,
+ s->planewidth[p] * 2);
+ }
+
+ for (y = 0; y < s->borders[p].bottom; y++) {
+ memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
+ ptr + (s->planeheight[p] - s->borders[p].bottom - 2 - y) * linesize,
+ s->planewidth[p] * 2);
+ }
+ }
+}
+
+static void wrap_borders8(FillBordersContext *s, AVFrame *frame)
+{
+ int p, y, x;
+
+ for (p = 0; p < s->nb_planes; p++) {
+ uint8_t *ptr = frame->data[p];
+ int linesize = frame->linesize[p];
+
+ for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
+ for (x = 0; x < s->borders[p].left; x++) {
+ ptr[y * linesize + x] = ptr[y * linesize + s->planewidth[p] - s->borders[p].right - s->borders[p].left + x];
+ }
+
+ for (x = 0; x < s->borders[p].right; x++) {
+ ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
+ ptr[y * linesize + s->borders[p].left + x];
+ }
+ }
+
+ for (y = 0; y < s->borders[p].top; y++) {
+ memcpy(ptr + y * linesize,
+ ptr + (s->planeheight[p] - s->borders[p].bottom - s->borders[p].top + y) * linesize,
+ s->planewidth[p]);
+ }
+
+ for (y = 0; y < s->borders[p].bottom; y++) {
+ memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
+ ptr + (s->borders[p].top + y) * linesize,
+ s->planewidth[p]);
+ }
+ }
+}
+
+static void wrap_borders16(FillBordersContext *s, AVFrame *frame)
+{
+ int p, y, x;
+
+ for (p = 0; p < s->nb_planes; p++) {
+ uint16_t *ptr = (uint16_t *)frame->data[p];
+ int linesize = frame->linesize[p] / 2;
+
+ for (y = s->borders[p].top; y < s->planeheight[p] - s->borders[p].bottom; y++) {
+ for (x = 0; x < s->borders[p].left; x++) {
+ ptr[y * linesize + x] = ptr[y * linesize + s->planewidth[p] - s->borders[p].right - s->borders[p].left + x];
+ }
+
+ for (x = 0; x < s->borders[p].right; x++) {
+ ptr[y * linesize + s->planewidth[p] - s->borders[p].right + x] =
+ ptr[y * linesize + s->borders[p].left + x];
+ }
+ }
+
+ for (y = 0; y < s->borders[p].top; y++) {
+ memcpy(ptr + y * linesize,
+ ptr + (s->planeheight[p] - s->borders[p].bottom - s->borders[p].top + y) * linesize,
+ s->planewidth[p] * 2);
+ }
+
+ for (y = 0; y < s->borders[p].bottom; y++) {
+ memcpy(ptr + (s->planeheight[p] - s->borders[p].bottom + y) * linesize,
+ ptr + (s->borders[p].top + y) * linesize,
+ s->planewidth[p] * 2);
+ }
+ }
+}
+
+static int lerp8(int fill, int src, int pos, int size)
+{
+ return av_clip_uint8(((fill * 256 * pos / size) + (src * 256 * (size - pos) / size)) >> 8);
+}
+
+static int lerp16(int fill, int src, int pos, int size, int depth)
+{
+ return av_clip_uintp2_c(((fill * (1LL << depth) * pos / size) + (src * (1LL << depth) * (size - pos) / size)) >> depth, depth);
+}
+
+static void fade_borders8(FillBordersContext *s, AVFrame *frame)
+{
+ int p, y, x;
+
+ for (p = 0; p < s->nb_planes; p++) {
+ uint8_t *ptr = frame->data[p];
+ const uint8_t fill = s->fill[p];
+ const int linesize = frame->linesize[p];
+ const int start_left = s->borders[p].left;
+ const int start_right = s->planewidth[p] - s->borders[p].right;
+ const int start_top = s->borders[p].top;
+ const int start_bottom = s->planeheight[p] - s->borders[p].bottom;
+
+ for (y = 0; y < start_top; y++) {
+ for (x = 0; x < s->planewidth[p]; x++) {
+ int src = ptr[y * linesize + x];
+ ptr[y * linesize + x] = lerp8(fill, src, start_top - y, start_top);
+ }
+ }
+
+ for (y = start_bottom; y < s->planeheight[p]; y++) {
+ for (x = 0; x < s->planewidth[p]; x++) {
+ int src = ptr[y * linesize + x];
+ ptr[y * linesize + x] = lerp8(fill, src, y - start_bottom, s->borders[p].bottom);
+ }
+ }
+
+ for (y = 0; y < s->planeheight[p]; y++) {
+ for (x = 0; x < start_left; x++) {
+ int src = ptr[y * linesize + x];
+ ptr[y * linesize + x] = lerp8(fill, src, start_left - x, start_left);
+ }
+
+ for (x = 0; x < s->borders[p].right; x++) {
+ int src = ptr[y * linesize + start_right + x];
+ ptr[y * linesize + start_right + x] = lerp8(fill, src, x, s->borders[p].right);
+ }
+ }
+ }
+}
+
+static void fade_borders16(FillBordersContext *s, AVFrame *frame)
+{
+ const int depth = s->depth;
+ int p, y, x;
+
+ for (p = 0; p < s->nb_planes; p++) {
+ uint16_t *ptr = (uint16_t *)frame->data[p];
+ const uint16_t fill = s->fill[p] << (depth - 8);
+ const int linesize = frame->linesize[p] / 2;
+ const int start_left = s->borders[p].left;
+ const int start_right = s->planewidth[p] - s->borders[p].right;
+ const int start_top = s->borders[p].top;
+ const int start_bottom = s->planeheight[p] - s->borders[p].bottom;
+
+ for (y = 0; y < start_top; y++) {
+ for (x = 0; x < s->planewidth[p]; x++) {
+ int src = ptr[y * linesize + x];
+ ptr[y * linesize + x] = lerp16(fill, src, start_top - y, start_top, depth);
+ }
+ }
+
+ for (y = start_bottom; y < s->planeheight[p]; y++) {
+ for (x = 0; x < s->planewidth[p]; x++) {
+ int src = ptr[y * linesize + x];
+ ptr[y * linesize + x] = lerp16(fill, src, y - start_bottom, s->borders[p].bottom, depth);
+ }
+ }
+
+ for (y = 0; y < s->planeheight[p]; y++) {
+ for (x = 0; x < start_left; x++) {
+ int src = ptr[y * linesize + x];
+ ptr[y * linesize + x] = lerp16(fill, src, start_left - x, start_left, depth);
+ }
+
+ for (x = 0; x < s->borders[p].right; x++) {
+ int src = ptr[y * linesize + start_right + x];
+ ptr[y * linesize + start_right + x] = lerp16(fill, src, x, s->borders[p].right, depth);
+ }
+ }
+ }
+}
+