Skip to content

Effort free video editing!

Shift an overlay by a fraction of a pixel a row at a time

WyattBluec2e4998dc9b9
diff --git a/src/render/video.nim b/src/render/video.nim
index 8a74a6a7..5d69f417 100644
--- a/src/render/video.nim
+++ b/src/render/video.nim
@@ -2199,23 +2199,58 @@ proc makeNewVideoFrames*(output: var OutputContainer, timeline: ptr v3,
dst.pts = src.pts
dst.time_base = src.time_base
if av_frame_get_buffer(dst, 32) < 0: error "subpixelShift: bad buffer"
+ # A column's left neighbor and weights are the same on every row. The
+ # weights are kept once a byte, not once a pixel, so the loop over a row's
+ # bytes below reads everything at a stride of one and can be vectorized.
+ if w == 0 or h == 0: return dst
+ var x0s = newSeq[int](w)
+ var wxSeq = newSeq[float32](w * 4)
+ var omwSeq = newSeq[float32](w * 4)
+ let wxs = cast[ptr UncheckedArray[float32]](addr wxSeq[0])
+ let omws = cast[ptr UncheckedArray[float32]](addr omwSeq[0])
+ for x in 0 ..< w:
+ let sxf = x.float32 - dx
+ let x0 = floor(sxf).int
+ let wx = sxf - x0.float32
+ x0s[x] = x0
+ for c in 0 ..< 4:
+ wxs[x * 4 + c] = wx
+ omws[x * 4 + c] = 1 - wx
+ # Columns xa ..< xb have both neighbors in the frame, the same `k`
+ # columns away. That is all but an edge column, short of a rounding quirk.
+ var xa = 0
+ while xa < w and (x0s[xa] < 0 or x0s[xa] + 1 >= w): inc xa
+ let k = (if xa < w: x0s[xa] - xa else: 0)
+ var xb = xa
+ while xb < w and x0s[xb] - xb == k and x0s[xb] + 1 < w: inc xb
for y in 0 ..< h:
let dRow = dst.planeRow(0, y)
let syf = y.float32 - dy
let y0 = floor(syf).int
let wy = syf - y0.float32
- for x in 0 ..< w:
- let sxf = x.float32 - dx
- let x0 = floor(sxf).int
- let wx = sxf - x0.float32
- for c in 0 ..< 4: # bilinear blend of the 4 neighbours, per RGBA channel
- template px(xx, yy: int): float32 =
- if xx < 0 or xx >= w or yy < 0 or yy >= h: 0.0'f32
- else:
- src.planeRow(0, yy)[xx * 4 + c].float32
- let t = px(x0, y0) * (1 - wx) + px(x0 + 1, y0) * wx
- let b = px(x0, y0 + 1) * (1 - wx) + px(x0 + 1, y0 + 1) * wx
- dRow[x * 4 + c] = uint8(clamp(t * (1 - wy) + b * wy + 0.5'f32, 0, 255))
+ # The two source rows this one blends, nil where they're off the frame.
+ let top = (if y0 >= 0 and y0 < h: src.planeRow(0, y0) else: nil)
+ let bot = (if y0 + 1 >= 0 and y0 + 1 < h: src.planeRow(0, y0 + 1) else: nil)
+ template checked(x: int) =
+ for c in 0 ..< 4:
+ template px(row: ptr UncheckedArray[uint8], xx: int): float32 =
+ if row == nil or xx < 0 or xx >= w: 0.0'f32
+ else: row[xx * 4 + c].float32
+ let i = x * 4 + c
+ let t = px(top, x0s[x]) * omws[i] + px(top, x0s[x] + 1) * wxs[i]
+ let b = px(bot, x0s[x]) * omws[i] + px(bot, x0s[x] + 1) * wxs[i]
+ dRow[i] = uint8(clamp(t * (1 - wy) + b * wy + 0.5'f32, 0, 255))
+ if top == nil or bot == nil:
+ for x in 0 ..< w: checked(x)
+ else:
+ for x in 0 ..< xa: checked(x)
+ let left = cast[ptr UncheckedArray[uint8]](addr top[k * 4])
+ let leftBelow = cast[ptr UncheckedArray[uint8]](addr bot[k * 4])
+ for i in xa * 4 ..< xb * 4:
+ let t = left[i].float32 * omws[i] + left[i + 4].float32 * wxs[i]
+ let b = leftBelow[i].float32 * omws[i] + leftBelow[i + 4].float32 * wxs[i]
+ dRow[i] = uint8(clamp(t * (1 - wy) + b * wy + 0.5'f32, 0, 255))
+ for x in xb ..< w: checked(x)
return dst
proc overlayFrame(base, top: ptr AVFrame; x, y: float32;