FFmpeg  2.1.1
vf_deshake.c
Go to the documentation of this file.
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 #define MAX_R 64
72 
73 static const AVOption deshake_options[] = {
74  { "x", "set x for the rectangular search area", OFFSET(cx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
75  { "y", "set y for the rectangular search area", OFFSET(cy), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
76  { "w", "set width for the rectangular search area", OFFSET(cw), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
77  { "h", "set height for the rectangular search area", OFFSET(ch), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
78  { "rx", "set x for the rectangular search area", OFFSET(rx), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
79  { "ry", "set y for the rectangular search area", OFFSET(ry), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
80  { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=FILL_MIRROR}, FILL_BLANK, FILL_COUNT-1, FLAGS, "edge"},
81  { "blank", "fill zeroes at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_BLANK}, INT_MIN, INT_MAX, FLAGS, "edge" },
82  { "original", "original image at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_ORIGINAL}, INT_MIN, INT_MAX, FLAGS, "edge" },
83  { "clamp", "extruded edge value at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_CLAMP}, INT_MIN, INT_MAX, FLAGS, "edge" },
84  { "mirror", "mirrored edge at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_MIRROR}, INT_MIN, INT_MAX, FLAGS, "edge" },
85  { "blocksize", "set motion search blocksize", OFFSET(blocksize), AV_OPT_TYPE_INT, {.i64=8}, 4, 128, .flags = FLAGS },
86  { "contrast", "set contrast threshold for blocks", OFFSET(contrast), AV_OPT_TYPE_INT, {.i64=125}, 1, 255, .flags = FLAGS },
87  { "search", "set search strategy", OFFSET(search), AV_OPT_TYPE_INT, {.i64=EXHAUSTIVE}, EXHAUSTIVE, SEARCH_COUNT-1, FLAGS, "smode" },
88  { "exhaustive", "exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
89  { "less", "less exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=SMART_EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
90  { "filename", "set motion search detailed log file name", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
91  { "opencl", "use OpenCL filtering capabilities", OFFSET(opencl), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = FLAGS },
92  { NULL }
93 };
94 
95 AVFILTER_DEFINE_CLASS(deshake);
96 
97 static int cmp(const double *a, const double *b)
98 {
99  return *a < *b ? -1 : ( *a > *b ? 1 : 0 );
100 }
101 
102 /**
103  * Cleaned mean (cuts off 20% of values to remove outliers and then averages)
104  */
105 static double clean_mean(double *values, int count)
106 {
107  double mean = 0;
108  int cut = count / 5;
109  int x;
110 
111  qsort(values, count, sizeof(double), (void*)cmp);
112 
113  for (x = cut; x < count - cut; x++) {
114  mean += values[x];
115  }
116 
117  return mean / (count - cut * 2);
118 }
119 
120 /**
121  * Find the most likely shift in motion between two frames for a given
122  * macroblock. Test each block against several shifts given by the rx
123  * and ry attributes. Searches using a simple matrix of those shifts and
124  * chooses the most likely shift by the smallest difference in blocks.
125  */
126 static void find_block_motion(DeshakeContext *deshake, uint8_t *src1,
127  uint8_t *src2, int cx, int cy, int stride,
129 {
130  int x, y;
131  int diff;
132  int smallest = INT_MAX;
133  int tmp, tmp2;
134 
135  #define CMP(i, j) deshake->c.sad[0](deshake, src1 + cy * stride + cx, \
136  src2 + (j) * stride + (i), stride, \
137  deshake->blocksize)
138 
139  if (deshake->search == EXHAUSTIVE) {
140  // Compare every possible position - this is sloooow!
141  for (y = -deshake->ry; y <= deshake->ry; y++) {
142  for (x = -deshake->rx; x <= deshake->rx; x++) {
143  diff = CMP(cx - x, cy - y);
144  if (diff < smallest) {
145  smallest = diff;
146  mv->x = x;
147  mv->y = y;
148  }
149  }
150  }
151  } else if (deshake->search == SMART_EXHAUSTIVE) {
152  // Compare every other possible position and find the best match
153  for (y = -deshake->ry + 1; y < deshake->ry; y += 2) {
154  for (x = -deshake->rx + 1; x < deshake->rx; x += 2) {
155  diff = CMP(cx - x, cy - y);
156  if (diff < smallest) {
157  smallest = diff;
158  mv->x = x;
159  mv->y = y;
160  }
161  }
162  }
163 
164  // Hone in on the specific best match around the match we found above
165  tmp = mv->x;
166  tmp2 = mv->y;
167 
168  for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
169  for (x = tmp - 1; x <= tmp + 1; x++) {
170  if (x == tmp && y == tmp2)
171  continue;
172 
173  diff = CMP(cx - x, cy - y);
174  if (diff < smallest) {
175  smallest = diff;
176  mv->x = x;
177  mv->y = y;
178  }
179  }
180  }
181  }
182 
183  if (smallest > 512) {
184  mv->x = -1;
185  mv->y = -1;
186  }
187  emms_c();
188  //av_log(NULL, AV_LOG_ERROR, "%d\n", smallest);
189  //av_log(NULL, AV_LOG_ERROR, "Final: (%d, %d) = %d x %d\n", cx, cy, mv->x, mv->y);
190 }
191 
192 /**
193  * Find the contrast of a given block. When searching for global motion we
194  * really only care about the high contrast blocks, so using this method we
195  * can actually skip blocks we don't care much about.
196  */
197 static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
198 {
199  int highest = 0;
200  int lowest = 255;
201  int i, j, pos;
202 
203  for (i = 0; i <= blocksize * 2; i++) {
204  // We use a width of 16 here to match the libavcodec sad functions
205  for (j = 0; j <= 15; j++) {
206  pos = (y - i) * stride + (x - j);
207  if (src[pos] < lowest)
208  lowest = src[pos];
209  else if (src[pos] > highest) {
210  highest = src[pos];
211  }
212  }
213  }
214 
215  return highest - lowest;
216 }
217 
218 /**
219  * Find the rotation for a given block.
220  */
221 static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
222 {
223  double a1, a2, diff;
224 
225  a1 = atan2(y - cy, x - cx);
226  a2 = atan2(y - cy + shift->y, x - cx + shift->x);
227 
228  diff = a2 - a1;
229 
230  return (diff > M_PI) ? diff - 2 * M_PI :
231  (diff < -M_PI) ? diff + 2 * M_PI :
232  diff;
233 }
234 
235 /**
236  * Find the estimated global motion for a scene given the most likely shift
237  * for each block in the frame. The global motion is estimated to be the
238  * same as the motion from most blocks in the frame, so if most blocks
239  * move one pixel to the right and two pixels down, this would yield a
240  * motion vector (1, -2).
241  */
242 static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
243  int width, int height, int stride, Transform *t)
244 {
245  int x, y;
246  IntMotionVector mv = {0, 0};
247  int counts[2*MAX_R+1][2*MAX_R+1];
248  int count_max_value = 0;
249  int contrast;
250 
251  int pos;
252  double *angles = av_malloc(sizeof(*angles) * width * height / (16 * deshake->blocksize));
253  int center_x = 0, center_y = 0;
254  double p_x, p_y;
255 
256  // Reset counts to zero
257  for (x = 0; x < deshake->rx * 2 + 1; x++) {
258  for (y = 0; y < deshake->ry * 2 + 1; y++) {
259  counts[x][y] = 0;
260  }
261  }
262 
263  pos = 0;
264  // Find motion for every block and store the motion vector in the counts
265  for (y = deshake->ry; y < height - deshake->ry - (deshake->blocksize * 2); y += deshake->blocksize * 2) {
266  // We use a width of 16 here to match the libavcodec sad functions
267  for (x = deshake->rx; x < width - deshake->rx - 16; x += 16) {
268  // If the contrast is too low, just skip this block as it probably
269  // won't be very useful to us.
270  contrast = block_contrast(src2, x, y, stride, deshake->blocksize);
271  if (contrast > deshake->contrast) {
272  //av_log(NULL, AV_LOG_ERROR, "%d\n", contrast);
273  find_block_motion(deshake, src1, src2, x, y, stride, &mv);
274  if (mv.x != -1 && mv.y != -1) {
275  counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
276  if (x > deshake->rx && y > deshake->ry)
277  angles[pos++] = block_angle(x, y, 0, 0, &mv);
278 
279  center_x += mv.x;
280  center_y += mv.y;
281  }
282  }
283  }
284  }
285 
286  if (pos) {
287  center_x /= pos;
288  center_y /= pos;
289  t->angle = clean_mean(angles, pos);
290  if (t->angle < 0.001)
291  t->angle = 0;
292  } else {
293  t->angle = 0;
294  }
295 
296  // Find the most common motion vector in the frame and use it as the gmv
297  for (y = deshake->ry * 2; y >= 0; y--) {
298  for (x = 0; x < deshake->rx * 2 + 1; x++) {
299  //av_log(NULL, AV_LOG_ERROR, "%5d ", counts[x][y]);
300  if (counts[x][y] > count_max_value) {
301  t->vector.x = x - deshake->rx;
302  t->vector.y = y - deshake->ry;
303  count_max_value = counts[x][y];
304  }
305  }
306  //av_log(NULL, AV_LOG_ERROR, "\n");
307  }
308 
309  p_x = (center_x - width / 2);
310  p_y = (center_y - height / 2);
311  t->vector.x += (cos(t->angle)-1)*p_x - sin(t->angle)*p_y;
312  t->vector.y += sin(t->angle)*p_x + (cos(t->angle)-1)*p_y;
313 
314  // Clamp max shift & rotation?
315  t->vector.x = av_clipf(t->vector.x, -deshake->rx * 2, deshake->rx * 2);
316  t->vector.y = av_clipf(t->vector.y, -deshake->ry * 2, deshake->ry * 2);
317  t->angle = av_clipf(t->angle, -0.1, 0.1);
318 
319  //av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
320  av_free(angles);
321 }
322 
324  int width, int height, int cw, int ch,
325  const float *matrix_y, const float *matrix_uv,
327  enum FillMethod fill, AVFrame *in, AVFrame *out)
328 {
329  int i = 0, ret = 0;
330  const float *matrixs[3];
331  int plane_w[3], plane_h[3];
332  matrixs[0] = matrix_y;
333  matrixs[1] = matrixs[2] = matrix_uv;
334  plane_w[0] = width;
335  plane_w[1] = plane_w[2] = cw;
336  plane_h[0] = height;
337  plane_h[1] = plane_h[2] = ch;
338 
339  for (i = 0; i < 3; i++) {
340  // Transform the luma and chroma planes
341  ret = avfilter_transform(in->data[i], out->data[i], in->linesize[i], out->linesize[i],
342  plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
343  if (ret < 0)
344  return ret;
345  }
346  return ret;
347 }
348 
349 static av_cold int init(AVFilterContext *ctx)
350 {
351  int ret;
352  DeshakeContext *deshake = ctx->priv;
353 
354  deshake->refcount = 20; // XXX: add to options?
355  deshake->blocksize /= 2;
356  deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
357 
358  if (deshake->rx % 16) {
359  av_log(ctx, AV_LOG_ERROR, "rx must be a multiple of 16\n");
360  return AVERROR_PATCHWELCOME;
361  }
362 
363  if (deshake->filename)
364  deshake->fp = fopen(deshake->filename, "w");
365  if (deshake->fp)
366  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);
367 
368  // Quadword align left edge of box for MMX code, adjust width if necessary
369  // to keep right margin
370  if (deshake->cx > 0) {
371  deshake->cw += deshake->cx - (deshake->cx & ~15);
372  deshake->cx &= ~15;
373  }
374  deshake->transform = deshake_transform_c;
375  if (!CONFIG_OPENCL && deshake->opencl) {
376  av_log(ctx, AV_LOG_ERROR, "OpenCL support was not enabled in this build, cannot be selected\n");
377  return AVERROR(EINVAL);
378  }
379 
380  if (CONFIG_OPENCL && deshake->opencl) {
381  deshake->transform = ff_opencl_transform;
382  ret = ff_opencl_deshake_init(ctx);
383  if (ret < 0)
384  return ret;
385  }
386  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",
387  deshake->cx, deshake->cy, deshake->cw, deshake->ch,
388  deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
389 
390  return 0;
391 }
392 
394 {
395  static const enum AVPixelFormat pix_fmts[] = {
399  };
400 
402 
403  return 0;
404 }
405 
406 static int config_props(AVFilterLink *link)
407 {
408  DeshakeContext *deshake = link->dst->priv;
409 
410  deshake->ref = NULL;
411  deshake->last.vector.x = 0;
412  deshake->last.vector.y = 0;
413  deshake->last.angle = 0;
414  deshake->last.zoom = 0;
415 
416  deshake->avctx = avcodec_alloc_context3(NULL);
417  avpriv_dsputil_init(&deshake->c, deshake->avctx);
418 
419  return 0;
420 }
421 
422 static av_cold void uninit(AVFilterContext *ctx)
423 {
424  DeshakeContext *deshake = ctx->priv;
425  if (CONFIG_OPENCL && deshake->opencl) {
427  }
428  av_frame_free(&deshake->ref);
429  if (deshake->fp)
430  fclose(deshake->fp);
431  if (deshake->avctx)
432  avcodec_close(deshake->avctx);
433  av_freep(&deshake->avctx);
434 }
435 
436 static int filter_frame(AVFilterLink *link, AVFrame *in)
437 {
438  DeshakeContext *deshake = link->dst->priv;
439  AVFilterLink *outlink = link->dst->outputs[0];
440  AVFrame *out;
441  Transform t = {{0},0}, orig = {{0},0};
442  float matrix_y[9], matrix_uv[9];
443  float alpha = 2.0 / deshake->refcount;
444  char tmp[256];
445  int ret = 0;
446 
447  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
448  if (!out) {
449  av_frame_free(&in);
450  return AVERROR(ENOMEM);
451  }
452  av_frame_copy_props(out, in);
453 
454  if (CONFIG_OPENCL && deshake->opencl) {
455  ret = ff_opencl_deshake_process_inout_buf(link->dst,in, out);
456  if (ret < 0)
457  return ret;
458  }
459 
460  if (deshake->cx < 0 || deshake->cy < 0 || deshake->cw < 0 || deshake->ch < 0) {
461  // Find the most likely global motion for the current frame
462  find_motion(deshake, (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0], in->data[0], link->w, link->h, in->linesize[0], &t);
463  } else {
464  uint8_t *src1 = (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0];
465  uint8_t *src2 = in->data[0];
466 
467  deshake->cx = FFMIN(deshake->cx, link->w);
468  deshake->cy = FFMIN(deshake->cy, link->h);
469 
470  if ((unsigned)deshake->cx + (unsigned)deshake->cw > link->w) deshake->cw = link->w - deshake->cx;
471  if ((unsigned)deshake->cy + (unsigned)deshake->ch > link->h) deshake->ch = link->h - deshake->cy;
472 
473  // Quadword align right margin
474  deshake->cw &= ~15;
475 
476  src1 += deshake->cy * in->linesize[0] + deshake->cx;
477  src2 += deshake->cy * in->linesize[0] + deshake->cx;
478 
479  find_motion(deshake, src1, src2, deshake->cw, deshake->ch, in->linesize[0], &t);
480  }
481 
482 
483  // Copy transform so we can output it later to compare to the smoothed value
484  orig.vector.x = t.vector.x;
485  orig.vector.y = t.vector.y;
486  orig.angle = t.angle;
487  orig.zoom = t.zoom;
488 
489  // Generate a one-sided moving exponential average
490  deshake->avg.vector.x = alpha * t.vector.x + (1.0 - alpha) * deshake->avg.vector.x;
491  deshake->avg.vector.y = alpha * t.vector.y + (1.0 - alpha) * deshake->avg.vector.y;
492  deshake->avg.angle = alpha * t.angle + (1.0 - alpha) * deshake->avg.angle;
493  deshake->avg.zoom = alpha * t.zoom + (1.0 - alpha) * deshake->avg.zoom;
494 
495  // Remove the average from the current motion to detect the motion that
496  // is not on purpose, just as jitter from bumping the camera
497  t.vector.x -= deshake->avg.vector.x;
498  t.vector.y -= deshake->avg.vector.y;
499  t.angle -= deshake->avg.angle;
500  t.zoom -= deshake->avg.zoom;
501 
502  // Invert the motion to undo it
503  t.vector.x *= -1;
504  t.vector.y *= -1;
505  t.angle *= -1;
506 
507  // Write statistics to file
508  if (deshake->fp) {
509  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);
510  fwrite(tmp, sizeof(char), strlen(tmp), deshake->fp);
511  }
512 
513  // Turn relative current frame motion into absolute by adding it to the
514  // last absolute motion
515  t.vector.x += deshake->last.vector.x;
516  t.vector.y += deshake->last.vector.y;
517  t.angle += deshake->last.angle;
518  t.zoom += deshake->last.zoom;
519 
520  // Shrink motion by 10% to keep things centered in the camera frame
521  t.vector.x *= 0.9;
522  t.vector.y *= 0.9;
523  t.angle *= 0.9;
524 
525  // Store the last absolute motion information
526  deshake->last.vector.x = t.vector.x;
527  deshake->last.vector.y = t.vector.y;
528  deshake->last.angle = t.angle;
529  deshake->last.zoom = t.zoom;
530 
531  // Generate a luma transformation matrix
532  avfilter_get_matrix(t.vector.x, t.vector.y, t.angle, 1.0 + t.zoom / 100.0, matrix_y);
533  // Generate a chroma transformation matrix
534  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);
535  // Transform the luma and chroma planes
536  ret = deshake->transform(link->dst, link->w, link->h, CHROMA_WIDTH(link), CHROMA_HEIGHT(link),
537  matrix_y, matrix_uv, INTERPOLATE_BILINEAR, deshake->edge, in, out);
538 
539  // Cleanup the old reference frame
540  av_frame_free(&deshake->ref);
541 
542  if (ret < 0)
543  return ret;
544 
545  // Store the current frame as the reference frame for calculating the
546  // motion of the next frame
547  deshake->ref = in;
548 
549  return ff_filter_frame(outlink, out);
550 }
551 
552 static const AVFilterPad deshake_inputs[] = {
553  {
554  .name = "default",
555  .type = AVMEDIA_TYPE_VIDEO,
556  .filter_frame = filter_frame,
557  .config_props = config_props,
558  },
559  { NULL }
560 };
561 
562 static const AVFilterPad deshake_outputs[] = {
563  {
564  .name = "default",
565  .type = AVMEDIA_TYPE_VIDEO,
566  },
567  { NULL }
568 };
569 
571  .name = "deshake",
572  .description = NULL_IF_CONFIG_SMALL("Stabilize shaky video."),
573  .priv_size = sizeof(DeshakeContext),
574  .init = init,
575  .uninit = uninit,
577  .inputs = deshake_inputs,
578  .outputs = deshake_outputs,
579  .priv_class = &deshake_class,
580 };
#define AVERROR_PATCHWELCOME
static int shift(int a, int b)
Definition: sonic.c:78
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
#define OFFSET(x)
Definition: vf_deshake.c:68
AVOption.
Definition: opt.h:253
int ry
Maximum vertical shift.
Definition: deshake.h:79
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
#define FLAGS
Definition: vf_deshake.c:69
int y
Vertical shift.
Definition: deshake.h:41
planar YUV 4:2:2, 16bpp, (1 Cr &amp; Cb sample per 2x1 Y samples)
Definition: avcodec.h:4538
const char * b
Definition: vf_curves.c:105
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define a1
Definition: regdef.h:47
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
Pixel format.
Definition: avcodec.h:4533
double angle
Angle of rotation.
Definition: deshake.h:51
int stride
Definition: mace.c:144
#define av_cold
Definition: avcodec.h:653
AVFilter avfilter_vf_deshake
Definition: vf_deshake.c:570
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
InterpolateMethod
Definition: transform.h:39
const char * name
Pad name.
Definition: internal.h:66
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
#define M_PI
Definition: mathematics.h:46
AVCodecContext * avctx
Definition: deshake.h:84
double zoom
Zoom percentage.
Definition: deshake.h:52
uint8_t
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only &quot;metadata&quot; fields from src to dst.
Definition: frame.c:446
static void find_block_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2, int cx, int cy, int stride, IntMotionVector *mv)
Find the most likely shift in motion between two frames for a given macroblock.
Definition: vf_deshake.c:126
#define emms_c()
Definition: internal.h:49
int avfilter_transform(const uint8_t *src, uint8_t *dst, int src_stride, int dst_stride, int width, int height, const float *matrix, enum InterpolateMethod interpolate, enum FillMethod fill)
Do an affine transformation with the given interpolation method.
Definition: transform.c:149
static double clean_mean(double *values, int count)
Cleaned mean (cuts off 20% of values to remove outliers and then averages)
Definition: vf_deshake.c:105
static void interpolate(float *out, float v1, float v2, int size)
Definition: twinvq.c:84
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:151
int blocksize
Size of blocks to compare.
Definition: deshake.h:81
#define AV_LOG_VERBOSE
Detailed information.
Definition: avcodec.h:4163
FILE * fp
Definition: deshake.h:88
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:531
#define CONFIG_OPENCL
Definition: config.h:367
A filter pad used for either input or output.
Definition: internal.h:60
int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: utils.c:2505
int refcount
Number of reference frames (defines averaging window)
Definition: deshake.h:87
int rx
Maximum horizontal shift.
Definition: deshake.h:78
int ff_opencl_transform(AVFilterContext *ctx, int width, int height, int cw, int ch, const float *matrix_y, const float *matrix_uv, enum InterpolateMethod interpolate, enum FillMethod fill, AVFrame *in, AVFrame *out)
static double alpha(void *priv, double x, double y)
Definition: vf_geq.c:98
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
#define MAX_R
Definition: vf_deshake.c:71
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: avcodec.h:4548
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
char * filename
Motion search detailed log filename.
Definition: deshake.h:94
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: avcodec.h:4570
planar YUV 4:1:1, 12bpp, (1 Cr &amp; Cb sample per 4x1 Y samples)
Definition: avcodec.h:4541
static int filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_deshake.c:436
double x
Horizontal shift.
Definition: deshake.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
static int query_formats(AVFilterContext *ctx)
Definition: vf_deshake.c:393
static const AVFilterPad deshake_inputs[]
Definition: vf_deshake.c:552
int contrast
Contrast threshold.
Definition: deshake.h:82
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
Find the rotation for a given block.
Definition: vf_deshake.c:221
static int deshake_transform_c(AVFilterContext *ctx, int width, int height, int cw, int ch, const float *matrix_y, const float *matrix_uv, enum InterpolateMethod interpolate, enum FillMethod fill, AVFrame *in, AVFrame *out)
Definition: vf_deshake.c:323
FillMethod
Definition: transform.h:51
float y
ret
Definition: avfilter.c:961
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2, int width, int height, int stride, Transform *t)
Find the estimated global motion for a scene given the most likely shift for each block in the frame...
Definition: vf_deshake.c:242
#define a2
Definition: regdef.h:48
int edge
Edge fill method.
Definition: deshake.h:80
#define FFMIN(a, b)
Definition: avcodec.h:925
int search
Motion search method.
Definition: deshake.h:83
DSPContext c
Context providing optimized SAD methods.
Definition: deshake.h:85
#define CHROMA_HEIGHT(link)
Definition: vf_deshake.c:66
static const AVOption deshake_options[]
Definition: vf_deshake.c:73
double y
Vertical shift.
Definition: deshake.h:46
Main libavfilter public API header.
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: avcodec.h:4546
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
static const int8_t mv[256][2]
Definition: 4xm.c:73
Search all possible positions.
Definition: deshake.h:34
int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
planar YUV 4:4:0 (1 Cr &amp; Cb sample per 1x2 Y samples)
Definition: avcodec.h:4569
static int width
Definition: utils.c:158
AVS_Value src
Definition: avisynth_c.h:523
static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
Find the contrast of a given block.
Definition: vf_deshake.c:197
AVFrame * ref
Previous frame.
Definition: deshake.h:77
Transform avg
Definition: deshake.h:89
Search most possible positions (faster)
Definition: deshake.h:35
#define CHROMA_WIDTH(link)
Definition: vf_deshake.c:65
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
Filter definition.
Definition: avfilter.h:464
static int config_props(AVFilterLink *link)
Definition: vf_deshake.c:406
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
planar YUV 4:4:4, 24bpp, (1 Cr &amp; Cb sample per 1x1 Y samples)
Definition: avcodec.h:4539
static av_cold int init(AVFilterContext *ctx)
Definition: vf_deshake.c:349
int(* transform)(AVFilterContext *ctx, int width, int height, int cw, int ch, const float *matrix_y, const float *matrix_uv, enum InterpolateMethod interpolate, enum FillMethod fill, AVFrame *in, AVFrame *out)
Definition: deshake.h:99
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define snprintf
Definition: snprintf.h:34
#define CMP(i, j)
static int cmp(const double *a, const double *b)
Definition: vf_deshake.c:97
int cw
Crop motion search to this box.
Definition: deshake.h:90
Transform last
Transform from last frame.
Definition: deshake.h:86
int ff_opencl_deshake_init(AVFilterContext *ctx)
static float t
Definition: muxing.c:123
MotionVector vector
Motion vector.
Definition: deshake.h:50
DSP utils.
int x
Horizontal shift.
Definition: deshake.h:40
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static const AVFilterPad deshake_outputs[]
Definition: vf_deshake.c:562
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
void avfilter_get_matrix(float x_shift, float y_shift, float angle, float zoom, float *matrix)
Get an affine transformation matrix from a given translation, rotation, and zoom factor.
Definition: transform.c:106
#define AVERROR(e)
An instance of a filter.
Definition: avfilter.h:627
void INT64 INT64 count
Definition: avisynth_c.h:594
av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2947
internal API functions
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: avcodec.h:4547
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_deshake.c:422
planar YUV 4:1:0, 9bpp, (1 Cr &amp; Cb sample per 4x4 Y samples)
Definition: avcodec.h:4540
void ff_opencl_deshake_uninit(AVFilterContext *ctx)