/* * vid.stab for the web: johnmcm.com's Video Stabilizer gives it one * greyscale frame at a time (the browser decodes the video and shrinks each * frame), collects the local motions vid.stab finds, and at the end asks it * for the smoothed camera path as one transform per frame. The browser then * draws each full-size frame through its transform; vid.stab never sees the * full picture. * * Only the similarity model is used (shift, rotation, zoom): lens distortion * estimation, wobble correction and the rotational (fov) model are switched * off, because the page applies the transforms with a 2D canvas. * * Written for johnmcm.com (2026); LGPL-2.1-or-later, as vid.stab is. * SPDX-License-Identifier: LGPL-2.1-or-later */ #include #include #include #include #include "libvidstab.h" static VSMotionDetect md; static int detecting = 0; static VSManyLocalMotions motions; static int haveMotions = 0; static VSFrameInfo info; static uint8_t* frame = NULL; static double* result = NULL; static int resultCount = 0; static int tripodMode = 0; static int quiet(int type, const char* tag, const char* format, ...) { (void)type; (void)tag; (void)format; return VS_OK; } static void release(void) { if (detecting) vsMotionDetectionCleanup(&md); detecting = 0; if (haveMotions) { for (int i = 0; i < vs_vector_size(&motions); i++) { LocalMotions* lm = (LocalMotions*)vs_vector_get(&motions, i); if (lm) vs_vector_del(lm); } vs_vector_del(&motions); } haveMotions = 0; free(frame); frame = NULL; free(result); result = NULL; resultCount = 0; } /* * Starts a new video of `width` x `height` greyscale frames. shakiness 1-10 * (how shaky the video is), accuracy 1-15, stepSize in pixels; tripod > 0 * compares every frame with that frame (1 = the first), to hold the camera * still. Returns the buffer to write each frame's pixels into, or 0. */ EMSCRIPTEN_KEEPALIVE uint8_t* stab_start(int width, int height, int shakiness, int accuracy, int stepSize, int tripod) { release(); vs_log = quiet; // vsFrameInfoInit returns 1 on success, not VS_OK. if (!vsFrameInfoInit(&info, width, height, PF_GRAY8)) return 0; VSMotionDetectConfig conf = vsMotionDetectGetDefaultConfig("vidstab"); conf.shakiness = shakiness; conf.accuracy = accuracy; conf.stepSize = stepSize; conf.virtualTripod = tripod; conf.show = 0; conf.numThreads = 1; tripodMode = tripod > 0; if (vsMotionDetectInit(&md, &conf, &info) != VS_OK) return 0; detecting = 1; if (vs_vector_init(&motions, 1024) != VS_OK) return 0; haveMotions = 1; frame = malloc((size_t)width * height); return frame; } /* Finds the motion in the frame now in the buffer. Returns the number of measuring fields used, or -1. */ EMSCRIPTEN_KEEPALIVE int stab_detect(void) { if (!detecting) return -1; VSFrame f; vsFrameNull(&f); f.data[0] = frame; f.linesize[0] = info.width; LocalMotions lm; if (vsMotionDetection(&md, &lm, &f) != VS_OK) return -1; int n = vs_vector_size(&lm); // The vector's struct is copied in; its items now belong to `motions`. if (vs_vector_append_dup(&motions, &lm, sizeof(LocalMotions)) != VS_OK) return -1; return n; } /* * The camera path for every frame detected so far, as x, y, alpha, zoom per * frame (pixels of the detection size, radians, percent): what each frame * must be moved by. smoothing is in frames either side; algo 0 is the L1 * optimal path, 1 Gaussian, 2 averaging; optZoom 0 none, 1 static, 2 * adaptive; zoom is extra percent. Returns the number of frames, or -1. */ EMSCRIPTEN_KEEPALIVE int stab_transforms(int smoothing, int algo, int optZoom, double zoom) { if (!haveMotions) return -1; free(result); result = NULL; resultCount = 0; VSTransformConfig conf = vsTransformGetDefaultConfig("vidstab"); conf.smoothing = smoothing; conf.camPathAlgo = algo == 1 ? VSGaussian : algo == 2 ? VSAvg : VSOptimalL1; conf.optZoom = optZoom; conf.zoom = zoom; conf.crop = VSKeepBorder; conf.estimateLensDistortion = 0; conf.lensCorrection = VSLensCorrectOff; conf.fov = 0.0; if (tripodMode) { // As FFmpeg's vidstabtransform does for tripod: absolute transforms, no smoothing. conf.relative = 0; conf.smoothing = 0; } VSTransformData td; if (vsTransformDataInit(&td, &conf, &info, &info) != VS_OK) return -1; VSTransformations trans; vsTransformationsInit(&trans); int ok = vsLocalmotions2Transforms(&td, &motions, &trans) == VS_OK && vsPreprocessTransforms(&td, &trans) == VS_OK; if (ok) { result = malloc(sizeof(double) * 4 * (trans.len > 0 ? trans.len : 1)); for (int i = 0; i < trans.len; i++) { result[i * 4] = trans.ts[i].x; result[i * 4 + 1] = trans.ts[i].y; result[i * 4 + 2] = trans.ts[i].alpha; result[i * 4 + 3] = trans.ts[i].zoom; } resultCount = trans.len; } vsTransformationsCleanup(&trans); vsTransformDataCleanup(&td); return ok ? resultCount : -1; } EMSCRIPTEN_KEEPALIVE double* stab_result(void) { return result; } EMSCRIPTEN_KEEPALIVE void stab_end(void) { release(); }