LCOV - code coverage report
Current view: top level - libavfilter - vf_deshake.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 224 0.0 %
Date: 2017-12-15 11:05:35 Functions: 0 12 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2010 Georg Martius <georg.martius@web.de>
       3             :  * Copyright (C) 2010 Daniel G. Taylor <dan@programmer-art.org>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * fast deshake / depan video filter
      25             :  *
      26             :  * SAD block-matching motion compensation to fix small changes in
      27             :  * horizontal and/or vertical shift. This filter helps remove camera shake
      28             :  * from hand-holding a camera, bumping a tripod, moving on a vehicle, etc.
      29             :  *
      30             :  * Algorithm:
      31             :  *   - For each frame with one previous reference frame
      32             :  *       - For each block in the frame
      33             :  *           - If contrast > threshold then find likely motion vector
      34             :  *       - For all found motion vectors
      35             :  *           - Find most common, store as global motion vector
      36             :  *       - Find most likely rotation angle
      37             :  *       - Transform image along global motion
      38             :  *
      39             :  * TODO:
      40             :  *   - Fill frame edges based on previous/next reference frames
      41             :  *   - Fill frame edges by stretching image near the edges?
      42             :  *       - Can this be done quickly and look decent?
      43             :  *
      44             :  * Dark Shikari links to http://wiki.videolan.org/SoC_x264_2010#GPU_Motion_Estimation_2
      45             :  * for an algorithm similar to what could be used here to get the gmv
      46             :  * It requires only a couple diamond searches + fast downscaling
      47             :  *
      48             :  * Special thanks to Jason Kotenko for his help with the algorithm and my
      49             :  * inability to see simple errors in C code.
      50             :  */
      51             : 
      52             : #include "avfilter.h"
      53             : #include "formats.h"
      54             : #include "internal.h"
      55             : #include "video.h"
      56             : #include "libavutil/common.h"
      57             : #include "libavutil/mem.h"
      58             : #include "libavutil/opt.h"
      59             : #include "libavutil/pixdesc.h"
      60             : #include "libavutil/qsort.h"
      61             : 
      62             : #include "deshake.h"
      63             : 
      64             : #define OFFSET(x) offsetof(DeshakeContext, x)
      65             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      66             : 
      67             : static const AVOption deshake_options[] = {
      68             :     { "x", "set x for the rectangular search area",      OFFSET(cx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
      69             :     { "y", "set y for the rectangular search area",      OFFSET(cy), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
      70             :     { "w", "set width for the rectangular search area",  OFFSET(cw), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
      71             :     { "h", "set height for the rectangular search area", OFFSET(ch), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
      72             :     { "rx", "set x for the rectangular search area",     OFFSET(rx), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
      73             :     { "ry", "set y for the rectangular search area",     OFFSET(ry), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
      74             :     { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=FILL_MIRROR}, FILL_BLANK, FILL_COUNT-1, FLAGS, "edge"},
      75             :         { "blank",    "fill zeroes at blank locations",         0, AV_OPT_TYPE_CONST, {.i64=FILL_BLANK},    INT_MIN, INT_MAX, FLAGS, "edge" },
      76             :         { "original", "original image at blank locations",      0, AV_OPT_TYPE_CONST, {.i64=FILL_ORIGINAL}, INT_MIN, INT_MAX, FLAGS, "edge" },
      77             :         { "clamp",    "extruded edge value at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_CLAMP},    INT_MIN, INT_MAX, FLAGS, "edge" },
      78             :         { "mirror",   "mirrored edge at blank locations",       0, AV_OPT_TYPE_CONST, {.i64=FILL_MIRROR},   INT_MIN, INT_MAX, FLAGS, "edge" },
      79             :     { "blocksize", "set motion search blocksize",       OFFSET(blocksize), AV_OPT_TYPE_INT, {.i64=8},   4, 128, .flags = FLAGS },
      80             :     { "contrast",  "set contrast threshold for blocks", OFFSET(contrast),  AV_OPT_TYPE_INT, {.i64=125}, 1, 255, .flags = FLAGS },
      81             :     { "search",  "set search strategy", OFFSET(search), AV_OPT_TYPE_INT, {.i64=EXHAUSTIVE}, EXHAUSTIVE, SEARCH_COUNT-1, FLAGS, "smode" },
      82             :         { "exhaustive", "exhaustive search",      0, AV_OPT_TYPE_CONST, {.i64=EXHAUSTIVE},       INT_MIN, INT_MAX, FLAGS, "smode" },
      83             :         { "less",       "less exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=SMART_EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
      84             :     { "filename", "set motion search detailed log file name", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
      85             :     { "opencl", "ignored",                              OFFSET(opencl), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, .flags = FLAGS },
      86             :     { NULL }
      87             : };
      88             : 
      89             : AVFILTER_DEFINE_CLASS(deshake);
      90             : 
      91           0 : static int cmp(const void *a, const void *b)
      92             : {
      93           0 :     return FFDIFFSIGN(*(const double *)a, *(const double *)b);
      94             : }
      95             : 
      96             : /**
      97             :  * Cleaned mean (cuts off 20% of values to remove outliers and then averages)
      98             :  */
      99           0 : static double clean_mean(double *values, int count)
     100             : {
     101           0 :     double mean = 0;
     102           0 :     int cut = count / 5;
     103             :     int x;
     104             : 
     105           0 :     AV_QSORT(values, count, double, cmp);
     106             : 
     107           0 :     for (x = cut; x < count - cut; x++) {
     108           0 :         mean += values[x];
     109             :     }
     110             : 
     111           0 :     return mean / (count - cut * 2);
     112             : }
     113             : 
     114             : /**
     115             :  * Find the most likely shift in motion between two frames for a given
     116             :  * macroblock. Test each block against several shifts given by the rx
     117             :  * and ry attributes. Searches using a simple matrix of those shifts and
     118             :  * chooses the most likely shift by the smallest difference in blocks.
     119             :  */
     120           0 : static void find_block_motion(DeshakeContext *deshake, uint8_t *src1,
     121             :                               uint8_t *src2, int cx, int cy, int stride,
     122             :                               IntMotionVector *mv)
     123             : {
     124             :     int x, y;
     125             :     int diff;
     126           0 :     int smallest = INT_MAX;
     127             :     int tmp, tmp2;
     128             : 
     129             :     #define CMP(i, j) deshake->sad(src1 + cy  * stride + cx,  stride,\
     130             :                                    src2 + (j) * stride + (i), stride)
     131             : 
     132           0 :     if (deshake->search == EXHAUSTIVE) {
     133             :         // Compare every possible position - this is sloooow!
     134           0 :         for (y = -deshake->ry; y <= deshake->ry; y++) {
     135           0 :             for (x = -deshake->rx; x <= deshake->rx; x++) {
     136           0 :                 diff = CMP(cx - x, cy - y);
     137           0 :                 if (diff < smallest) {
     138           0 :                     smallest = diff;
     139           0 :                     mv->x = x;
     140           0 :                     mv->y = y;
     141             :                 }
     142             :             }
     143             :         }
     144           0 :     } else if (deshake->search == SMART_EXHAUSTIVE) {
     145             :         // Compare every other possible position and find the best match
     146           0 :         for (y = -deshake->ry + 1; y < deshake->ry; y += 2) {
     147           0 :             for (x = -deshake->rx + 1; x < deshake->rx; x += 2) {
     148           0 :                 diff = CMP(cx - x, cy - y);
     149           0 :                 if (diff < smallest) {
     150           0 :                     smallest = diff;
     151           0 :                     mv->x = x;
     152           0 :                     mv->y = y;
     153             :                 }
     154             :             }
     155             :         }
     156             : 
     157             :         // Hone in on the specific best match around the match we found above
     158           0 :         tmp = mv->x;
     159           0 :         tmp2 = mv->y;
     160             : 
     161           0 :         for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
     162           0 :             for (x = tmp - 1; x <= tmp + 1; x++) {
     163           0 :                 if (x == tmp && y == tmp2)
     164           0 :                     continue;
     165             : 
     166           0 :                 diff = CMP(cx - x, cy - y);
     167           0 :                 if (diff < smallest) {
     168           0 :                     smallest = diff;
     169           0 :                     mv->x = x;
     170           0 :                     mv->y = y;
     171             :                 }
     172             :             }
     173             :         }
     174             :     }
     175             : 
     176           0 :     if (smallest > 512) {
     177           0 :         mv->x = -1;
     178           0 :         mv->y = -1;
     179             :     }
     180           0 :     emms_c();
     181             :     //av_log(NULL, AV_LOG_ERROR, "%d\n", smallest);
     182             :     //av_log(NULL, AV_LOG_ERROR, "Final: (%d, %d) = %d x %d\n", cx, cy, mv->x, mv->y);
     183           0 : }
     184             : 
     185             : /**
     186             :  * Find the contrast of a given block. When searching for global motion we
     187             :  * really only care about the high contrast blocks, so using this method we
     188             :  * can actually skip blocks we don't care much about.
     189             :  */
     190           0 : static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
     191             : {
     192           0 :     int highest = 0;
     193           0 :     int lowest = 255;
     194             :     int i, j, pos;
     195             : 
     196           0 :     for (i = 0; i <= blocksize * 2; i++) {
     197             :         // We use a width of 16 here to match the sad function
     198           0 :         for (j = 0; j <= 15; j++) {
     199           0 :             pos = (y - i) * stride + (x - j);
     200           0 :             if (src[pos] < lowest)
     201           0 :                 lowest = src[pos];
     202           0 :             else if (src[pos] > highest) {
     203           0 :                 highest = src[pos];
     204             :             }
     205             :         }
     206             :     }
     207             : 
     208           0 :     return highest - lowest;
     209             : }
     210             : 
     211             : /**
     212             :  * Find the rotation for a given block.
     213             :  */
     214           0 : static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
     215             : {
     216             :     double a1, a2, diff;
     217             : 
     218           0 :     a1 = atan2(y - cy, x - cx);
     219           0 :     a2 = atan2(y - cy + shift->y, x - cx + shift->x);
     220             : 
     221           0 :     diff = a2 - a1;
     222             : 
     223           0 :     return (diff > M_PI)  ? diff - 2 * M_PI :
     224           0 :            (diff < -M_PI) ? diff + 2 * M_PI :
     225             :            diff;
     226             : }
     227             : 
     228             : /**
     229             :  * Find the estimated global motion for a scene given the most likely shift
     230             :  * for each block in the frame. The global motion is estimated to be the
     231             :  * same as the motion from most blocks in the frame, so if most blocks
     232             :  * move one pixel to the right and two pixels down, this would yield a
     233             :  * motion vector (1, -2).
     234             :  */
     235           0 : static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
     236             :                         int width, int height, int stride, Transform *t)
     237             : {
     238             :     int x, y;
     239           0 :     IntMotionVector mv = {0, 0};
     240           0 :     int count_max_value = 0;
     241             :     int contrast;
     242             : 
     243             :     int pos;
     244           0 :     int center_x = 0, center_y = 0;
     245             :     double p_x, p_y;
     246             : 
     247           0 :     av_fast_malloc(&deshake->angles, &deshake->angles_size, width * height / (16 * deshake->blocksize) * sizeof(*deshake->angles));
     248             : 
     249             :     // Reset counts to zero
     250           0 :     for (x = 0; x < deshake->rx * 2 + 1; x++) {
     251           0 :         for (y = 0; y < deshake->ry * 2 + 1; y++) {
     252           0 :             deshake->counts[x][y] = 0;
     253             :         }
     254             :     }
     255             : 
     256           0 :     pos = 0;
     257             :     // Find motion for every block and store the motion vector in the counts
     258           0 :     for (y = deshake->ry; y < height - deshake->ry - (deshake->blocksize * 2); y += deshake->blocksize * 2) {
     259             :         // We use a width of 16 here to match the sad function
     260           0 :         for (x = deshake->rx; x < width - deshake->rx - 16; x += 16) {
     261             :             // If the contrast is too low, just skip this block as it probably
     262             :             // won't be very useful to us.
     263           0 :             contrast = block_contrast(src2, x, y, stride, deshake->blocksize);
     264           0 :             if (contrast > deshake->contrast) {
     265             :                 //av_log(NULL, AV_LOG_ERROR, "%d\n", contrast);
     266           0 :                 find_block_motion(deshake, src1, src2, x, y, stride, &mv);
     267           0 :                 if (mv.x != -1 && mv.y != -1) {
     268           0 :                     deshake->counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
     269           0 :                     if (x > deshake->rx && y > deshake->ry)
     270           0 :                         deshake->angles[pos++] = block_angle(x, y, 0, 0, &mv);
     271             : 
     272           0 :                     center_x += mv.x;
     273           0 :                     center_y += mv.y;
     274             :                 }
     275             :             }
     276             :         }
     277             :     }
     278             : 
     279           0 :     if (pos) {
     280           0 :          center_x /= pos;
     281           0 :          center_y /= pos;
     282           0 :          t->angle = clean_mean(deshake->angles, pos);
     283           0 :          if (t->angle < 0.001)
     284           0 :               t->angle = 0;
     285             :     } else {
     286           0 :          t->angle = 0;
     287             :     }
     288             : 
     289             :     // Find the most common motion vector in the frame and use it as the gmv
     290           0 :     for (y = deshake->ry * 2; y >= 0; y--) {
     291           0 :         for (x = 0; x < deshake->rx * 2 + 1; x++) {
     292             :             //av_log(NULL, AV_LOG_ERROR, "%5d ", deshake->counts[x][y]);
     293           0 :             if (deshake->counts[x][y] > count_max_value) {
     294           0 :                 t->vec.x = x - deshake->rx;
     295           0 :                 t->vec.y = y - deshake->ry;
     296           0 :                 count_max_value = deshake->counts[x][y];
     297             :             }
     298             :         }
     299             :         //av_log(NULL, AV_LOG_ERROR, "\n");
     300             :     }
     301             : 
     302           0 :     p_x = (center_x - width / 2.0);
     303           0 :     p_y = (center_y - height / 2.0);
     304           0 :     t->vec.x += (cos(t->angle)-1)*p_x  - sin(t->angle)*p_y;
     305           0 :     t->vec.y += sin(t->angle)*p_x  + (cos(t->angle)-1)*p_y;
     306             : 
     307             :     // Clamp max shift & rotation?
     308           0 :     t->vec.x = av_clipf(t->vec.x, -deshake->rx * 2, deshake->rx * 2);
     309           0 :     t->vec.y = av_clipf(t->vec.y, -deshake->ry * 2, deshake->ry * 2);
     310           0 :     t->angle = av_clipf(t->angle, -0.1, 0.1);
     311             : 
     312             :     //av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
     313           0 : }
     314             : 
     315           0 : static int deshake_transform_c(AVFilterContext *ctx,
     316             :                                     int width, int height, int cw, int ch,
     317             :                                     const float *matrix_y, const float *matrix_uv,
     318             :                                     enum InterpolateMethod interpolate,
     319             :                                     enum FillMethod fill, AVFrame *in, AVFrame *out)
     320             : {
     321           0 :     int i = 0, ret = 0;
     322             :     const float *matrixs[3];
     323             :     int plane_w[3], plane_h[3];
     324           0 :     matrixs[0] = matrix_y;
     325           0 :     matrixs[1] =  matrixs[2] = matrix_uv;
     326           0 :     plane_w[0] = width;
     327           0 :     plane_w[1] = plane_w[2] = cw;
     328           0 :     plane_h[0] = height;
     329           0 :     plane_h[1] = plane_h[2] = ch;
     330             : 
     331           0 :     for (i = 0; i < 3; i++) {
     332             :         // Transform the luma and chroma planes
     333           0 :         ret = avfilter_transform(in->data[i], out->data[i], in->linesize[i], out->linesize[i],
     334             :                                  plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
     335           0 :         if (ret < 0)
     336           0 :             return ret;
     337             :     }
     338           0 :     return ret;
     339             : }
     340             : 
     341           0 : static av_cold int init(AVFilterContext *ctx)
     342             : {
     343           0 :     DeshakeContext *deshake = ctx->priv;
     344             : 
     345           0 :     deshake->sad = av_pixelutils_get_sad_fn(4, 4, 1, deshake); // 16x16, 2nd source unaligned
     346           0 :     if (!deshake->sad)
     347           0 :         return AVERROR(EINVAL);
     348             : 
     349           0 :     deshake->refcount = 20; // XXX: add to options?
     350           0 :     deshake->blocksize /= 2;
     351           0 :     deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
     352             : 
     353           0 :     if (deshake->rx % 16) {
     354           0 :         av_log(ctx, AV_LOG_ERROR, "rx must be a multiple of 16\n");
     355           0 :         return AVERROR_PATCHWELCOME;
     356             :     }
     357             : 
     358           0 :     if (deshake->filename)
     359           0 :         deshake->fp = fopen(deshake->filename, "w");
     360           0 :     if (deshake->fp)
     361           0 :         fwrite("Ori x, Avg x, Fin x, Ori y, Avg y, Fin y, Ori angle, Avg angle, Fin angle, Ori zoom, Avg zoom, Fin zoom\n", sizeof(char), 104, deshake->fp);
     362             : 
     363             :     // Quadword align left edge of box for MMX code, adjust width if necessary
     364             :     // to keep right margin
     365           0 :     if (deshake->cx > 0) {
     366           0 :         deshake->cw += deshake->cx - (deshake->cx & ~15);
     367           0 :         deshake->cx &= ~15;
     368             :     }
     369           0 :     deshake->transform = deshake_transform_c;
     370             : 
     371           0 :     av_log(ctx, AV_LOG_VERBOSE, "cx: %d, cy: %d, cw: %d, ch: %d, rx: %d, ry: %d, edge: %d blocksize: %d contrast: %d search: %d\n",
     372             :            deshake->cx, deshake->cy, deshake->cw, deshake->ch,
     373           0 :            deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
     374             : 
     375           0 :     return 0;
     376             : }
     377             : 
     378           0 : static int query_formats(AVFilterContext *ctx)
     379             : {
     380             :     static const enum AVPixelFormat pix_fmts[] = {
     381             :         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,
     382             :         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
     383             :         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
     384             :     };
     385           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     386           0 :     if (!fmts_list)
     387           0 :         return AVERROR(ENOMEM);
     388           0 :     return ff_set_common_formats(ctx, fmts_list);
     389             : }
     390             : 
     391           0 : static int config_props(AVFilterLink *link)
     392             : {
     393           0 :     DeshakeContext *deshake = link->dst->priv;
     394             : 
     395           0 :     deshake->ref = NULL;
     396           0 :     deshake->last.vec.x = 0;
     397           0 :     deshake->last.vec.y = 0;
     398           0 :     deshake->last.angle = 0;
     399           0 :     deshake->last.zoom = 0;
     400             : 
     401           0 :     return 0;
     402             : }
     403             : 
     404           0 : static av_cold void uninit(AVFilterContext *ctx)
     405             : {
     406           0 :     DeshakeContext *deshake = ctx->priv;
     407           0 :     av_frame_free(&deshake->ref);
     408           0 :     av_freep(&deshake->angles);
     409           0 :     deshake->angles_size = 0;
     410           0 :     if (deshake->fp)
     411           0 :         fclose(deshake->fp);
     412           0 : }
     413             : 
     414           0 : static int filter_frame(AVFilterLink *link, AVFrame *in)
     415             : {
     416           0 :     DeshakeContext *deshake = link->dst->priv;
     417           0 :     AVFilterLink *outlink = link->dst->outputs[0];
     418             :     AVFrame *out;
     419           0 :     Transform t = {{0},0}, orig = {{0},0};
     420             :     float matrix_y[9], matrix_uv[9];
     421           0 :     float alpha = 2.0 / deshake->refcount;
     422             :     char tmp[256];
     423           0 :     int ret = 0;
     424           0 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
     425           0 :     const int chroma_width  = AV_CEIL_RSHIFT(link->w, desc->log2_chroma_w);
     426           0 :     const int chroma_height = AV_CEIL_RSHIFT(link->h, desc->log2_chroma_h);
     427             : 
     428           0 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     429           0 :     if (!out) {
     430           0 :         av_frame_free(&in);
     431           0 :         return AVERROR(ENOMEM);
     432             :     }
     433           0 :     av_frame_copy_props(out, in);
     434             : 
     435           0 :     if (deshake->cx < 0 || deshake->cy < 0 || deshake->cw < 0 || deshake->ch < 0) {
     436             :         // Find the most likely global motion for the current frame
     437           0 :         find_motion(deshake, (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0], in->data[0], link->w, link->h, in->linesize[0], &t);
     438             :     } else {
     439           0 :         uint8_t *src1 = (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0];
     440           0 :         uint8_t *src2 = in->data[0];
     441             : 
     442           0 :         deshake->cx = FFMIN(deshake->cx, link->w);
     443           0 :         deshake->cy = FFMIN(deshake->cy, link->h);
     444             : 
     445           0 :         if ((unsigned)deshake->cx + (unsigned)deshake->cw > link->w) deshake->cw = link->w - deshake->cx;
     446           0 :         if ((unsigned)deshake->cy + (unsigned)deshake->ch > link->h) deshake->ch = link->h - deshake->cy;
     447             : 
     448             :         // Quadword align right margin
     449           0 :         deshake->cw &= ~15;
     450             : 
     451           0 :         src1 += deshake->cy * in->linesize[0] + deshake->cx;
     452           0 :         src2 += deshake->cy * in->linesize[0] + deshake->cx;
     453             : 
     454           0 :         find_motion(deshake, src1, src2, deshake->cw, deshake->ch, in->linesize[0], &t);
     455             :     }
     456             : 
     457             : 
     458             :     // Copy transform so we can output it later to compare to the smoothed value
     459           0 :     orig.vec.x = t.vec.x;
     460           0 :     orig.vec.y = t.vec.y;
     461           0 :     orig.angle = t.angle;
     462           0 :     orig.zoom = t.zoom;
     463             : 
     464             :     // Generate a one-sided moving exponential average
     465           0 :     deshake->avg.vec.x = alpha * t.vec.x + (1.0 - alpha) * deshake->avg.vec.x;
     466           0 :     deshake->avg.vec.y = alpha * t.vec.y + (1.0 - alpha) * deshake->avg.vec.y;
     467           0 :     deshake->avg.angle = alpha * t.angle + (1.0 - alpha) * deshake->avg.angle;
     468           0 :     deshake->avg.zoom = alpha * t.zoom + (1.0 - alpha) * deshake->avg.zoom;
     469             : 
     470             :     // Remove the average from the current motion to detect the motion that
     471             :     // is not on purpose, just as jitter from bumping the camera
     472           0 :     t.vec.x -= deshake->avg.vec.x;
     473           0 :     t.vec.y -= deshake->avg.vec.y;
     474           0 :     t.angle -= deshake->avg.angle;
     475           0 :     t.zoom -= deshake->avg.zoom;
     476             : 
     477             :     // Invert the motion to undo it
     478           0 :     t.vec.x *= -1;
     479           0 :     t.vec.y *= -1;
     480           0 :     t.angle *= -1;
     481             : 
     482             :     // Write statistics to file
     483           0 :     if (deshake->fp) {
     484           0 :         snprintf(tmp, 256, "%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n", orig.vec.x, deshake->avg.vec.x, t.vec.x, orig.vec.y, deshake->avg.vec.y, t.vec.y, orig.angle, deshake->avg.angle, t.angle, orig.zoom, deshake->avg.zoom, t.zoom);
     485           0 :         fwrite(tmp, sizeof(char), strlen(tmp), deshake->fp);
     486             :     }
     487             : 
     488             :     // Turn relative current frame motion into absolute by adding it to the
     489             :     // last absolute motion
     490           0 :     t.vec.x += deshake->last.vec.x;
     491           0 :     t.vec.y += deshake->last.vec.y;
     492           0 :     t.angle += deshake->last.angle;
     493           0 :     t.zoom += deshake->last.zoom;
     494             : 
     495             :     // Shrink motion by 10% to keep things centered in the camera frame
     496           0 :     t.vec.x *= 0.9;
     497           0 :     t.vec.y *= 0.9;
     498           0 :     t.angle *= 0.9;
     499             : 
     500             :     // Store the last absolute motion information
     501           0 :     deshake->last.vec.x = t.vec.x;
     502           0 :     deshake->last.vec.y = t.vec.y;
     503           0 :     deshake->last.angle = t.angle;
     504           0 :     deshake->last.zoom = t.zoom;
     505             : 
     506             :     // Generate a luma transformation matrix
     507           0 :     avfilter_get_matrix(t.vec.x, t.vec.y, t.angle, 1.0 + t.zoom / 100.0, matrix_y);
     508             :     // Generate a chroma transformation matrix
     509           0 :     avfilter_get_matrix(t.vec.x / (link->w / chroma_width), t.vec.y / (link->h / chroma_height), t.angle, 1.0 + t.zoom / 100.0, matrix_uv);
     510             :     // Transform the luma and chroma planes
     511           0 :     ret = deshake->transform(link->dst, link->w, link->h, chroma_width, chroma_height,
     512           0 :                              matrix_y, matrix_uv, INTERPOLATE_BILINEAR, deshake->edge, in, out);
     513             : 
     514             :     // Cleanup the old reference frame
     515           0 :     av_frame_free(&deshake->ref);
     516             : 
     517           0 :     if (ret < 0)
     518           0 :         goto fail;
     519             : 
     520             :     // Store the current frame as the reference frame for calculating the
     521             :     // motion of the next frame
     522           0 :     deshake->ref = in;
     523             : 
     524           0 :     return ff_filter_frame(outlink, out);
     525           0 : fail:
     526           0 :     av_frame_free(&out);
     527           0 :     return ret;
     528             : }
     529             : 
     530             : static const AVFilterPad deshake_inputs[] = {
     531             :     {
     532             :         .name         = "default",
     533             :         .type         = AVMEDIA_TYPE_VIDEO,
     534             :         .filter_frame = filter_frame,
     535             :         .config_props = config_props,
     536             :     },
     537             :     { NULL }
     538             : };
     539             : 
     540             : static const AVFilterPad deshake_outputs[] = {
     541             :     {
     542             :         .name = "default",
     543             :         .type = AVMEDIA_TYPE_VIDEO,
     544             :     },
     545             :     { NULL }
     546             : };
     547             : 
     548             : AVFilter ff_vf_deshake = {
     549             :     .name          = "deshake",
     550             :     .description   = NULL_IF_CONFIG_SMALL("Stabilize shaky video."),
     551             :     .priv_size     = sizeof(DeshakeContext),
     552             :     .init          = init,
     553             :     .uninit        = uninit,
     554             :     .query_formats = query_formats,
     555             :     .inputs        = deshake_inputs,
     556             :     .outputs       = deshake_outputs,
     557             :     .priv_class    = &deshake_class,
     558             : };

Generated by: LCOV version 1.13