]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_deshake.c
deshake opencl based on comments on 20130402 3rd
[ffmpeg] / libavfilter / vf_deshake.c
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 "libavcodec/dsputil.h"
61
62 #include "deshake.h"
63 #include "deshake_opencl.h"
64
65 #define CHROMA_WIDTH(link)  -((-link->w) >> av_pix_fmt_desc_get(link->format)->log2_chroma_w)
66 #define CHROMA_HEIGHT(link) -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h)
67
68 #define OFFSET(x) offsetof(DeshakeContext, x)
69 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
70
71 static const AVOption deshake_options[] = {
72     { "x", "set x for the rectangular search area",      OFFSET(cx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
73     { "y", "set y for the rectangular search area",      OFFSET(cy), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
74     { "w", "set width for the rectangular search area",  OFFSET(cw), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
75     { "h", "set height for the rectangular search area", OFFSET(ch), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
76     { "rx", "set x for the rectangular search area",     OFFSET(rx), AV_OPT_TYPE_INT, {.i64=16}, 0, 64, .flags = FLAGS },
77     { "ry", "set y for the rectangular search area",     OFFSET(ry), AV_OPT_TYPE_INT, {.i64=16}, 0, 64, .flags = FLAGS },
78     { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=FILL_MIRROR}, FILL_BLANK, FILL_COUNT-1, FLAGS, "edge"},
79         { "blank",    "fill zeroes at blank locations",         0, AV_OPT_TYPE_CONST, {.i64=FILL_BLANK},    INT_MIN, INT_MAX, FLAGS, "edge" },
80         { "original", "original image at blank locations",      0, AV_OPT_TYPE_CONST, {.i64=FILL_ORIGINAL}, INT_MIN, INT_MAX, FLAGS, "edge" },
81         { "clamp",    "extruded edge value at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_CLAMP},    INT_MIN, INT_MAX, FLAGS, "edge" },
82         { "mirror",   "mirrored edge at blank locations",       0, AV_OPT_TYPE_CONST, {.i64=FILL_MIRROR},   INT_MIN, INT_MAX, FLAGS, "edge" },
83     { "blocksize", "set motion search blocksize",       OFFSET(blocksize), AV_OPT_TYPE_INT, {.i64=8},   4, 128, .flags = FLAGS },
84     { "contrast",  "set contrast threshold for blocks", OFFSET(contrast),  AV_OPT_TYPE_INT, {.i64=125}, 1, 255, .flags = FLAGS },
85     { "search",  "set search strategy", OFFSET(search), AV_OPT_TYPE_INT, {.i64=EXHAUSTIVE}, EXHAUSTIVE, SEARCH_COUNT-1, FLAGS, "smode" },
86         { "exhaustive", "exhaustive search",      0, AV_OPT_TYPE_CONST, {.i64=EXHAUSTIVE},       INT_MIN, INT_MAX, FLAGS, "smode" },
87         { "less",       "less exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=SMART_EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
88     { "filename", "set motion search detailed log file name", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
89     { "opencl", "use OpenCL filtering capabilities", OFFSET(opencl), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = FLAGS },
90     { NULL }
91 };
92
93 AVFILTER_DEFINE_CLASS(deshake);
94
95 static int cmp(const double *a, const double *b)
96 {
97     return *a < *b ? -1 : ( *a > *b ? 1 : 0 );
98 }
99
100 /**
101  * Cleaned mean (cuts off 20% of values to remove outliers and then averages)
102  */
103 static double clean_mean(double *values, int count)
104 {
105     double mean = 0;
106     int cut = count / 5;
107     int x;
108
109     qsort(values, count, sizeof(double), (void*)cmp);
110
111     for (x = cut; x < count - cut; x++) {
112         mean += values[x];
113     }
114
115     return mean / (count - cut * 2);
116 }
117
118 /**
119  * Find the most likely shift in motion between two frames for a given
120  * macroblock. Test each block against several shifts given by the rx
121  * and ry attributes. Searches using a simple matrix of those shifts and
122  * chooses the most likely shift by the smallest difference in blocks.
123  */
124 static void find_block_motion(DeshakeContext *deshake, uint8_t *src1,
125                               uint8_t *src2, int cx, int cy, int stride,
126                               IntMotionVector *mv)
127 {
128     int x, y;
129     int diff;
130     int smallest = INT_MAX;
131     int tmp, tmp2;
132
133     #define CMP(i, j) deshake->c.sad[0](deshake, src1 + cy * stride + cx, \
134                                         src2 + (j) * stride + (i), stride, \
135                                         deshake->blocksize)
136
137     if (deshake->search == EXHAUSTIVE) {
138         // Compare every possible position - this is sloooow!
139         for (y = -deshake->ry; y <= deshake->ry; y++) {
140             for (x = -deshake->rx; x <= deshake->rx; x++) {
141                 diff = CMP(cx - x, cy - y);
142                 if (diff < smallest) {
143                     smallest = diff;
144                     mv->x = x;
145                     mv->y = y;
146                 }
147             }
148         }
149     } else if (deshake->search == SMART_EXHAUSTIVE) {
150         // Compare every other possible position and find the best match
151         for (y = -deshake->ry + 1; y < deshake->ry - 2; y += 2) {
152             for (x = -deshake->rx + 1; x < deshake->rx - 2; x += 2) {
153                 diff = CMP(cx - x, cy - y);
154                 if (diff < smallest) {
155                     smallest = diff;
156                     mv->x = x;
157                     mv->y = y;
158                 }
159             }
160         }
161
162         // Hone in on the specific best match around the match we found above
163         tmp = mv->x;
164         tmp2 = mv->y;
165
166         for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
167             for (x = tmp - 1; x <= tmp + 1; x++) {
168                 if (x == tmp && y == tmp2)
169                     continue;
170
171                 diff = CMP(cx - x, cy - y);
172                 if (diff < smallest) {
173                     smallest = diff;
174                     mv->x = x;
175                     mv->y = y;
176                 }
177             }
178         }
179     }
180
181     if (smallest > 512) {
182         mv->x = -1;
183         mv->y = -1;
184     }
185     emms_c();
186     //av_log(NULL, AV_LOG_ERROR, "%d\n", smallest);
187     //av_log(NULL, AV_LOG_ERROR, "Final: (%d, %d) = %d x %d\n", cx, cy, mv->x, mv->y);
188 }
189
190 /**
191  * Find the contrast of a given block. When searching for global motion we
192  * really only care about the high contrast blocks, so using this method we
193  * can actually skip blocks we don't care much about.
194  */
195 static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
196 {
197     int highest = 0;
198     int lowest = 0;
199     int i, j, pos;
200
201     for (i = 0; i <= blocksize * 2; i++) {
202         // We use a width of 16 here to match the libavcodec sad functions
203         for (j = 0; i <= 15; i++) {
204             pos = (y - i) * stride + (x - j);
205             if (src[pos] < lowest)
206                 lowest = src[pos];
207             else if (src[pos] > highest) {
208                 highest = src[pos];
209             }
210         }
211     }
212
213     return highest - lowest;
214 }
215
216 /**
217  * Find the rotation for a given block.
218  */
219 static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
220 {
221     double a1, a2, diff;
222
223     a1 = atan2(y - cy, x - cx);
224     a2 = atan2(y - cy + shift->y, x - cx + shift->x);
225
226     diff = a2 - a1;
227
228     return (diff > M_PI)  ? diff - 2 * M_PI :
229            (diff < -M_PI) ? diff + 2 * M_PI :
230            diff;
231 }
232
233 /**
234  * Find the estimated global motion for a scene given the most likely shift
235  * for each block in the frame. The global motion is estimated to be the
236  * same as the motion from most blocks in the frame, so if most blocks
237  * move one pixel to the right and two pixels down, this would yield a
238  * motion vector (1, -2).
239  */
240 static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
241                         int width, int height, int stride, Transform *t)
242 {
243     int x, y;
244     IntMotionVector mv = {0, 0};
245     int counts[128][128];
246     int count_max_value = 0;
247     int contrast;
248
249     int pos;
250     double *angles = av_malloc(sizeof(*angles) * width * height / (16 * deshake->blocksize));
251     int center_x = 0, center_y = 0;
252     double p_x, p_y;
253
254     // Reset counts to zero
255     for (x = 0; x < deshake->rx * 2 + 1; x++) {
256         for (y = 0; y < deshake->ry * 2 + 1; y++) {
257             counts[x][y] = 0;
258         }
259     }
260
261     pos = 0;
262     // Find motion for every block and store the motion vector in the counts
263     for (y = deshake->ry; y < height - deshake->ry - (deshake->blocksize * 2); y += deshake->blocksize * 2) {
264         // We use a width of 16 here to match the libavcodec sad functions
265         for (x = deshake->rx; x < width - deshake->rx - 16; x += 16) {
266             // If the contrast is too low, just skip this block as it probably
267             // won't be very useful to us.
268             contrast = block_contrast(src2, x, y, stride, deshake->blocksize);
269             if (contrast > deshake->contrast) {
270                 //av_log(NULL, AV_LOG_ERROR, "%d\n", contrast);
271                 find_block_motion(deshake, src1, src2, x, y, stride, &mv);
272                 if (mv.x != -1 && mv.y != -1) {
273                     counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
274                     if (x > deshake->rx && y > deshake->ry)
275                         angles[pos++] = block_angle(x, y, 0, 0, &mv);
276
277                     center_x += mv.x;
278                     center_y += mv.y;
279                 }
280             }
281         }
282     }
283
284     if (pos) {
285          center_x /= pos;
286          center_y /= pos;
287          t->angle = clean_mean(angles, pos);
288          if (t->angle < 0.001)
289               t->angle = 0;
290     } else {
291          t->angle = 0;
292     }
293
294     // Find the most common motion vector in the frame and use it as the gmv
295     for (y = deshake->ry * 2; y >= 0; y--) {
296         for (x = 0; x < deshake->rx * 2 + 1; x++) {
297             //av_log(NULL, AV_LOG_ERROR, "%5d ", counts[x][y]);
298             if (counts[x][y] > count_max_value) {
299                 t->vector.x = x - deshake->rx;
300                 t->vector.y = y - deshake->ry;
301                 count_max_value = counts[x][y];
302             }
303         }
304         //av_log(NULL, AV_LOG_ERROR, "\n");
305     }
306
307     p_x = (center_x - width / 2);
308     p_y = (center_y - height / 2);
309     t->vector.x += (cos(t->angle)-1)*p_x  - sin(t->angle)*p_y;
310     t->vector.y += sin(t->angle)*p_x  + (cos(t->angle)-1)*p_y;
311
312     // Clamp max shift & rotation?
313     t->vector.x = av_clipf(t->vector.x, -deshake->rx * 2, deshake->rx * 2);
314     t->vector.y = av_clipf(t->vector.y, -deshake->ry * 2, deshake->ry * 2);
315     t->angle = av_clipf(t->angle, -0.1, 0.1);
316
317     //av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
318     av_free(angles);
319 }
320
321 static int deshake_transform_c(AVFilterContext *ctx,
322                                     int width, int height, int cw, int ch,
323                                     const float *matrix_y, const float *matrix_uv,
324                                     enum InterpolateMethod interpolate,
325                                     enum FillMethod fill, AVFrame *in, AVFrame *out)
326 {
327     int i = 0, ret = 0;
328     const float *matrixs[3];
329     int plane_w[3], plane_h[3];
330     matrixs[0] = matrix_y;
331     matrixs[1] =  matrixs[2] = matrix_uv;
332     plane_w[0] = width;
333     plane_w[1] = plane_w[2] = cw;
334     plane_h[0] = height;
335     plane_h[1] = plane_h[2] = ch;
336
337     for (i = 0; i < 3; i++) {
338         // Transform the luma and chroma planes
339         ret = avfilter_transform(in->data[i], out->data[i], in->linesize[i], out->linesize[i],
340                                  plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
341         if (ret < 0)
342             return ret;
343     }
344     return ret;
345 }
346
347 static av_cold int init(AVFilterContext *ctx, const char *args)
348 {
349     int ret;
350     DeshakeContext *deshake = ctx->priv;
351
352     deshake->refcount = 20; // XXX: add to options?
353     deshake->blocksize /= 2;
354     deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
355
356     if (deshake->filename)
357         deshake->fp = fopen(deshake->filename, "w");
358     if (deshake->fp)
359         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);
360
361     // Quadword align left edge of box for MMX code, adjust width if necessary
362     // to keep right margin
363     if (deshake->cx > 0) {
364         deshake->cw += deshake->cx - (deshake->cx & ~15);
365         deshake->cx &= ~15;
366     }
367     deshake->transform = deshake_transform_c;
368     if (!CONFIG_OPENCL && deshake->opencl) {
369         av_log(ctx, AV_LOG_ERROR, "OpenCL support was not enabled in this build, cannot be selected\n");
370         return AVERROR(EINVAL);
371     }
372
373     if (deshake->opencl && CONFIG_OPENCL) {
374         deshake->transform = ff_opencl_transform;
375         ret = ff_opencl_deshake_init(ctx);
376         if (ret < 0)
377             return ret;
378     }
379     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",
380            deshake->cx, deshake->cy, deshake->cw, deshake->ch,
381            deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
382
383     return 0;
384 }
385
386 static int query_formats(AVFilterContext *ctx)
387 {
388     static const enum AVPixelFormat pix_fmts[] = {
389         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,
390         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV440P,  AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
391         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
392     };
393
394     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
395
396     return 0;
397 }
398
399 static int config_props(AVFilterLink *link)
400 {
401     DeshakeContext *deshake = link->dst->priv;
402
403     deshake->ref = NULL;
404     deshake->last.vector.x = 0;
405     deshake->last.vector.y = 0;
406     deshake->last.angle = 0;
407     deshake->last.zoom = 0;
408
409     deshake->avctx = avcodec_alloc_context3(NULL);
410     dsputil_init(&deshake->c, deshake->avctx);
411
412     return 0;
413 }
414
415 static av_cold void uninit(AVFilterContext *ctx)
416 {
417     DeshakeContext *deshake = ctx->priv;
418     if (deshake->opencl && CONFIG_OPENCL) {
419         ff_opencl_deshake_uninit(ctx);
420     }
421     av_frame_free(&deshake->ref);
422     if (deshake->fp)
423         fclose(deshake->fp);
424     if (deshake->avctx)
425         avcodec_close(deshake->avctx);
426     av_freep(&deshake->avctx);
427 }
428
429 static int filter_frame(AVFilterLink *link, AVFrame *in)
430 {
431     DeshakeContext *deshake = link->dst->priv;
432     AVFilterLink *outlink = link->dst->outputs[0];
433     AVFrame *out;
434     Transform t = {{0},0}, orig = {{0},0};
435     float matrix_y[9], matrix_uv[9];
436     float alpha = 2.0 / deshake->refcount;
437     char tmp[256];
438     int ret = 0;
439
440     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
441     if (!out) {
442         av_frame_free(&in);
443         return AVERROR(ENOMEM);
444     }
445     av_frame_copy_props(out, in);
446
447     if (deshake->opencl && CONFIG_OPENCL) {
448         ret = ff_opencl_deshake_process_inout_buf(link->dst,in, out);
449         if (ret < 0)
450             return ret;
451     }
452
453     if (deshake->cx < 0 || deshake->cy < 0 || deshake->cw < 0 || deshake->ch < 0) {
454         // Find the most likely global motion for the current frame
455         find_motion(deshake, (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0], in->data[0], link->w, link->h, in->linesize[0], &t);
456     } else {
457         uint8_t *src1 = (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0];
458         uint8_t *src2 = in->data[0];
459
460         deshake->cx = FFMIN(deshake->cx, link->w);
461         deshake->cy = FFMIN(deshake->cy, link->h);
462
463         if ((unsigned)deshake->cx + (unsigned)deshake->cw > link->w) deshake->cw = link->w - deshake->cx;
464         if ((unsigned)deshake->cy + (unsigned)deshake->ch > link->h) deshake->ch = link->h - deshake->cy;
465
466         // Quadword align right margin
467         deshake->cw &= ~15;
468
469         src1 += deshake->cy * in->linesize[0] + deshake->cx;
470         src2 += deshake->cy * in->linesize[0] + deshake->cx;
471
472         find_motion(deshake, src1, src2, deshake->cw, deshake->ch, in->linesize[0], &t);
473     }
474
475
476     // Copy transform so we can output it later to compare to the smoothed value
477     orig.vector.x = t.vector.x;
478     orig.vector.y = t.vector.y;
479     orig.angle = t.angle;
480     orig.zoom = t.zoom;
481
482     // Generate a one-sided moving exponential average
483     deshake->avg.vector.x = alpha * t.vector.x + (1.0 - alpha) * deshake->avg.vector.x;
484     deshake->avg.vector.y = alpha * t.vector.y + (1.0 - alpha) * deshake->avg.vector.y;
485     deshake->avg.angle = alpha * t.angle + (1.0 - alpha) * deshake->avg.angle;
486     deshake->avg.zoom = alpha * t.zoom + (1.0 - alpha) * deshake->avg.zoom;
487
488     // Remove the average from the current motion to detect the motion that
489     // is not on purpose, just as jitter from bumping the camera
490     t.vector.x -= deshake->avg.vector.x;
491     t.vector.y -= deshake->avg.vector.y;
492     t.angle -= deshake->avg.angle;
493     t.zoom -= deshake->avg.zoom;
494
495     // Invert the motion to undo it
496     t.vector.x *= -1;
497     t.vector.y *= -1;
498     t.angle *= -1;
499
500     // Write statistics to file
501     if (deshake->fp) {
502         snprintf(tmp, 256, "%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n", orig.vector.x, deshake->avg.vector.x, t.vector.x, orig.vector.y, deshake->avg.vector.y, t.vector.y, orig.angle, deshake->avg.angle, t.angle, orig.zoom, deshake->avg.zoom, t.zoom);
503         fwrite(tmp, sizeof(char), strlen(tmp), deshake->fp);
504     }
505
506     // Turn relative current frame motion into absolute by adding it to the
507     // last absolute motion
508     t.vector.x += deshake->last.vector.x;
509     t.vector.y += deshake->last.vector.y;
510     t.angle += deshake->last.angle;
511     t.zoom += deshake->last.zoom;
512
513     // Shrink motion by 10% to keep things centered in the camera frame
514     t.vector.x *= 0.9;
515     t.vector.y *= 0.9;
516     t.angle *= 0.9;
517
518     // Store the last absolute motion information
519     deshake->last.vector.x = t.vector.x;
520     deshake->last.vector.y = t.vector.y;
521     deshake->last.angle = t.angle;
522     deshake->last.zoom = t.zoom;
523
524     // Generate a luma transformation matrix
525     avfilter_get_matrix(t.vector.x, t.vector.y, t.angle, 1.0 + t.zoom / 100.0, matrix_y);
526     // Generate a chroma transformation matrix
527     avfilter_get_matrix(t.vector.x / (link->w / CHROMA_WIDTH(link)), t.vector.y / (link->h / CHROMA_HEIGHT(link)), t.angle, 1.0 + t.zoom / 100.0, matrix_uv);
528     // Transform the luma and chroma planes
529     ret = deshake->transform(link->dst, link->w, link->h, CHROMA_WIDTH(link), CHROMA_HEIGHT(link),
530                              matrix_y, matrix_uv, INTERPOLATE_BILINEAR, deshake->edge, in, out);
531
532     // Cleanup the old reference frame
533     av_frame_free(&deshake->ref);
534
535     if (ret < 0)
536         return ret;
537
538     // Store the current frame as the reference frame for calculating the
539     // motion of the next frame
540     deshake->ref = in;
541
542     return ff_filter_frame(outlink, out);
543 }
544
545 static const AVFilterPad deshake_inputs[] = {
546     {
547         .name         = "default",
548         .type         = AVMEDIA_TYPE_VIDEO,
549         .filter_frame = filter_frame,
550         .config_props = config_props,
551     },
552     { NULL }
553 };
554
555 static const AVFilterPad deshake_outputs[] = {
556     {
557         .name = "default",
558         .type = AVMEDIA_TYPE_VIDEO,
559     },
560     { NULL }
561 };
562
563 static const char *const shorthand[] = {
564     "x", "y", "w", "h", "rx", "ry", "edge",
565     "blocksize", "contrast", "search", "filename",
566     NULL
567 };
568
569 AVFilter avfilter_vf_deshake = {
570     .name          = "deshake",
571     .description   = NULL_IF_CONFIG_SMALL("Stabilize shaky video."),
572     .priv_size     = sizeof(DeshakeContext),
573     .init          = init,
574     .uninit        = uninit,
575     .query_formats = query_formats,
576     .inputs        = deshake_inputs,
577     .outputs       = deshake_outputs,
578     .priv_class    = &deshake_class,
579     .shorthand     = shorthand,
580 };