00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00052 #include "avfilter.h"
00053 #include "formats.h"
00054 #include "video.h"
00055 #include "libavutil/common.h"
00056 #include "libavutil/mem.h"
00057 #include "libavutil/pixdesc.h"
00058 #include "libavcodec/dsputil.h"
00059
00060 #include "transform.h"
00061
00062 #define CHROMA_WIDTH(link) -((-link->w) >> av_pix_fmt_descriptors[link->format].log2_chroma_w)
00063 #define CHROMA_HEIGHT(link) -((-link->h) >> av_pix_fmt_descriptors[link->format].log2_chroma_h)
00064
00065 enum SearchMethod {
00066 EXHAUSTIVE,
00067 SMART_EXHAUSTIVE,
00068 SEARCH_COUNT
00069 };
00070
00071 typedef struct {
00072 int x;
00073 int y;
00074 } IntMotionVector;
00075
00076 typedef struct {
00077 double x;
00078 double y;
00079 } MotionVector;
00080
00081 typedef struct {
00082 MotionVector vector;
00083 double angle;
00084 double zoom;
00085 } Transform;
00086
00087 typedef struct {
00088 AVClass av_class;
00089 AVFilterBufferRef *ref;
00090 int rx;
00091 int ry;
00092 enum FillMethod edge;
00093 int blocksize;
00094 int contrast;
00095 enum SearchMethod search;
00096 AVCodecContext *avctx;
00097 DSPContext c;
00098 Transform last;
00099 int refcount;
00100 FILE *fp;
00101 Transform avg;
00102 int cw;
00103 int ch;
00104 int cx;
00105 int cy;
00106 } DeshakeContext;
00107
00108 static int cmp(const double *a, const double *b)
00109 {
00110 return *a < *b ? -1 : ( *a > *b ? 1 : 0 );
00111 }
00112
00116 static double clean_mean(double *values, int count)
00117 {
00118 double mean = 0;
00119 int cut = count / 5;
00120 int x;
00121
00122 qsort(values, count, sizeof(double), (void*)cmp);
00123
00124 for (x = cut; x < count - cut; x++) {
00125 mean += values[x];
00126 }
00127
00128 return mean / (count - cut * 2);
00129 }
00130
00137 static void find_block_motion(DeshakeContext *deshake, uint8_t *src1,
00138 uint8_t *src2, int cx, int cy, int stride,
00139 IntMotionVector *mv)
00140 {
00141 int x, y;
00142 int diff;
00143 int smallest = INT_MAX;
00144 int tmp, tmp2;
00145
00146 #define CMP(i, j) deshake->c.sad[0](deshake, src1 + cy * stride + cx, \
00147 src2 + (j) * stride + (i), stride, \
00148 deshake->blocksize)
00149
00150 if (deshake->search == EXHAUSTIVE) {
00151
00152 for (y = -deshake->ry; y <= deshake->ry; y++) {
00153 for (x = -deshake->rx; x <= deshake->rx; x++) {
00154 diff = CMP(cx - x, cy - y);
00155 if (diff < smallest) {
00156 smallest = diff;
00157 mv->x = x;
00158 mv->y = y;
00159 }
00160 }
00161 }
00162 } else if (deshake->search == SMART_EXHAUSTIVE) {
00163
00164 for (y = -deshake->ry + 1; y < deshake->ry - 2; y += 2) {
00165 for (x = -deshake->rx + 1; x < deshake->rx - 2; x += 2) {
00166 diff = CMP(cx - x, cy - y);
00167 if (diff < smallest) {
00168 smallest = diff;
00169 mv->x = x;
00170 mv->y = y;
00171 }
00172 }
00173 }
00174
00175
00176 tmp = mv->x;
00177 tmp2 = mv->y;
00178
00179 for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
00180 for (x = tmp - 1; x <= tmp + 1; x++) {
00181 if (x == tmp && y == tmp2)
00182 continue;
00183
00184 diff = CMP(cx - x, cy - y);
00185 if (diff < smallest) {
00186 smallest = diff;
00187 mv->x = x;
00188 mv->y = y;
00189 }
00190 }
00191 }
00192 }
00193
00194 if (smallest > 512) {
00195 mv->x = -1;
00196 mv->y = -1;
00197 }
00198 emms_c();
00199
00200
00201 }
00202
00208 static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
00209 {
00210 int highest = 0;
00211 int lowest = 0;
00212 int i, j, pos;
00213
00214 for (i = 0; i <= blocksize * 2; i++) {
00215
00216 for (j = 0; i <= 15; i++) {
00217 pos = (y - i) * stride + (x - j);
00218 if (src[pos] < lowest)
00219 lowest = src[pos];
00220 else if (src[pos] > highest) {
00221 highest = src[pos];
00222 }
00223 }
00224 }
00225
00226 return highest - lowest;
00227 }
00228
00232 static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
00233 {
00234 double a1, a2, diff;
00235
00236 a1 = atan2(y - cy, x - cx);
00237 a2 = atan2(y - cy + shift->y, x - cx + shift->x);
00238
00239 diff = a2 - a1;
00240
00241 return (diff > M_PI) ? diff - 2 * M_PI :
00242 (diff < -M_PI) ? diff + 2 * M_PI :
00243 diff;
00244 }
00245
00253 static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
00254 int width, int height, int stride, Transform *t)
00255 {
00256 int x, y;
00257 IntMotionVector mv = {0, 0};
00258 int counts[128][128];
00259 int count_max_value = 0;
00260 int contrast;
00261
00262 int pos;
00263 double *angles = av_malloc(sizeof(*angles) * width * height / (16 * deshake->blocksize));
00264 int center_x = 0, center_y = 0;
00265 double p_x, p_y;
00266
00267
00268 for (x = 0; x < deshake->rx * 2 + 1; x++) {
00269 for (y = 0; y < deshake->ry * 2 + 1; y++) {
00270 counts[x][y] = 0;
00271 }
00272 }
00273
00274 pos = 0;
00275
00276 for (y = deshake->ry; y < height - deshake->ry - (deshake->blocksize * 2); y += deshake->blocksize * 2) {
00277
00278 for (x = deshake->rx; x < width - deshake->rx - 16; x += 16) {
00279
00280
00281 contrast = block_contrast(src2, x, y, stride, deshake->blocksize);
00282 if (contrast > deshake->contrast) {
00283
00284 find_block_motion(deshake, src1, src2, x, y, stride, &mv);
00285 if (mv.x != -1 && mv.y != -1) {
00286 counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
00287 if (x > deshake->rx && y > deshake->ry)
00288 angles[pos++] = block_angle(x, y, 0, 0, &mv);
00289
00290 center_x += mv.x;
00291 center_y += mv.y;
00292 }
00293 }
00294 }
00295 }
00296
00297 if (pos) {
00298 center_x /= pos;
00299 center_y /= pos;
00300 t->angle = clean_mean(angles, pos);
00301 if (t->angle < 0.001)
00302 t->angle = 0;
00303 } else {
00304 t->angle = 0;
00305 }
00306
00307
00308 for (y = deshake->ry * 2; y >= 0; y--) {
00309 for (x = 0; x < deshake->rx * 2 + 1; x++) {
00310
00311 if (counts[x][y] > count_max_value) {
00312 t->vector.x = x - deshake->rx;
00313 t->vector.y = y - deshake->ry;
00314 count_max_value = counts[x][y];
00315 }
00316 }
00317
00318 }
00319
00320 p_x = (center_x - width / 2);
00321 p_y = (center_y - height / 2);
00322 t->vector.x += (cos(t->angle)-1)*p_x - sin(t->angle)*p_y;
00323 t->vector.y += sin(t->angle)*p_x + (cos(t->angle)-1)*p_y;
00324
00325
00326 t->vector.x = av_clipf(t->vector.x, -deshake->rx * 2, deshake->rx * 2);
00327 t->vector.y = av_clipf(t->vector.y, -deshake->ry * 2, deshake->ry * 2);
00328 t->angle = av_clipf(t->angle, -0.1, 0.1);
00329
00330
00331 av_free(angles);
00332 }
00333
00334 static av_cold int init(AVFilterContext *ctx, const char *args)
00335 {
00336 DeshakeContext *deshake = ctx->priv;
00337 char filename[256] = {0};
00338
00339 deshake->rx = 16;
00340 deshake->ry = 16;
00341 deshake->edge = FILL_MIRROR;
00342 deshake->blocksize = 8;
00343 deshake->contrast = 125;
00344 deshake->search = EXHAUSTIVE;
00345 deshake->refcount = 20;
00346
00347 deshake->cw = -1;
00348 deshake->ch = -1;
00349 deshake->cx = -1;
00350 deshake->cy = -1;
00351
00352 if (args) {
00353 sscanf(args, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%255s",
00354 &deshake->cx, &deshake->cy, &deshake->cw, &deshake->ch,
00355 &deshake->rx, &deshake->ry, (int *)&deshake->edge,
00356 &deshake->blocksize, &deshake->contrast, (int *)&deshake->search, filename);
00357
00358 deshake->blocksize /= 2;
00359
00360 deshake->rx = av_clip(deshake->rx, 0, 64);
00361 deshake->ry = av_clip(deshake->ry, 0, 64);
00362 deshake->edge = av_clip(deshake->edge, FILL_BLANK, FILL_COUNT - 1);
00363 deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
00364 deshake->contrast = av_clip(deshake->contrast, 1, 255);
00365 deshake->search = av_clip(deshake->search, EXHAUSTIVE, SEARCH_COUNT - 1);
00366
00367 }
00368 if (*filename)
00369 deshake->fp = fopen(filename, "w");
00370 if (deshake->fp)
00371 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);
00372
00373
00374
00375 if (deshake->cx > 0) {
00376 deshake->cw += deshake->cx - (deshake->cx & ~15);
00377 deshake->cx &= ~15;
00378 }
00379
00380 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",
00381 deshake->cx, deshake->cy, deshake->cw, deshake->ch,
00382 deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
00383
00384 return 0;
00385 }
00386
00387 static int query_formats(AVFilterContext *ctx)
00388 {
00389 enum PixelFormat pix_fmts[] = {
00390 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_YUV410P,
00391 PIX_FMT_YUV411P, PIX_FMT_YUV440P, PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P,
00392 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P, PIX_FMT_NONE
00393 };
00394
00395 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00396
00397 return 0;
00398 }
00399
00400 static int config_props(AVFilterLink *link)
00401 {
00402 DeshakeContext *deshake = link->dst->priv;
00403
00404 deshake->ref = NULL;
00405 deshake->last.vector.x = 0;
00406 deshake->last.vector.y = 0;
00407 deshake->last.angle = 0;
00408 deshake->last.zoom = 0;
00409
00410 deshake->avctx = avcodec_alloc_context3(NULL);
00411 dsputil_init(&deshake->c, deshake->avctx);
00412
00413 return 0;
00414 }
00415
00416 static av_cold void uninit(AVFilterContext *ctx)
00417 {
00418 DeshakeContext *deshake = ctx->priv;
00419
00420 avfilter_unref_buffer(deshake->ref);
00421 if (deshake->fp)
00422 fclose(deshake->fp);
00423 if (deshake->avctx)
00424 avcodec_close(deshake->avctx);
00425 av_freep(&deshake->avctx);
00426 }
00427
00428 static int end_frame(AVFilterLink *link)
00429 {
00430 DeshakeContext *deshake = link->dst->priv;
00431 AVFilterBufferRef *in = link->cur_buf;
00432 AVFilterBufferRef *out = link->dst->outputs[0]->out_buf;
00433 Transform t = {{0},0}, orig = {{0},0};
00434 float matrix[9];
00435 float alpha = 2.0 / deshake->refcount;
00436 char tmp[256];
00437
00438 link->cur_buf = NULL;
00439 if (deshake->cx < 0 || deshake->cy < 0 || deshake->cw < 0 || deshake->ch < 0) {
00440
00441 find_motion(deshake, (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0], in->data[0], link->w, link->h, in->linesize[0], &t);
00442 } else {
00443 uint8_t *src1 = (deshake->ref == NULL) ? in->data[0] : deshake->ref->data[0];
00444 uint8_t *src2 = in->data[0];
00445
00446 deshake->cx = FFMIN(deshake->cx, link->w);
00447 deshake->cy = FFMIN(deshake->cy, link->h);
00448
00449 if ((unsigned)deshake->cx + (unsigned)deshake->cw > link->w) deshake->cw = link->w - deshake->cx;
00450 if ((unsigned)deshake->cy + (unsigned)deshake->ch > link->h) deshake->ch = link->h - deshake->cy;
00451
00452
00453 deshake->cw &= ~15;
00454
00455 src1 += deshake->cy * in->linesize[0] + deshake->cx;
00456 src2 += deshake->cy * in->linesize[0] + deshake->cx;
00457
00458 find_motion(deshake, src1, src2, deshake->cw, deshake->ch, in->linesize[0], &t);
00459 }
00460
00461
00462
00463 orig.vector.x = t.vector.x;
00464 orig.vector.y = t.vector.y;
00465 orig.angle = t.angle;
00466 orig.zoom = t.zoom;
00467
00468
00469 deshake->avg.vector.x = alpha * t.vector.x + (1.0 - alpha) * deshake->avg.vector.x;
00470 deshake->avg.vector.y = alpha * t.vector.y + (1.0 - alpha) * deshake->avg.vector.y;
00471 deshake->avg.angle = alpha * t.angle + (1.0 - alpha) * deshake->avg.angle;
00472 deshake->avg.zoom = alpha * t.zoom + (1.0 - alpha) * deshake->avg.zoom;
00473
00474
00475
00476 t.vector.x -= deshake->avg.vector.x;
00477 t.vector.y -= deshake->avg.vector.y;
00478 t.angle -= deshake->avg.angle;
00479 t.zoom -= deshake->avg.zoom;
00480
00481
00482 t.vector.x *= -1;
00483 t.vector.y *= -1;
00484 t.angle *= -1;
00485
00486
00487 if (deshake->fp) {
00488 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);
00489 fwrite(tmp, sizeof(char), strlen(tmp), deshake->fp);
00490 }
00491
00492
00493
00494 t.vector.x += deshake->last.vector.x;
00495 t.vector.y += deshake->last.vector.y;
00496 t.angle += deshake->last.angle;
00497 t.zoom += deshake->last.zoom;
00498
00499
00500 t.vector.x *= 0.9;
00501 t.vector.y *= 0.9;
00502 t.angle *= 0.9;
00503
00504
00505 deshake->last.vector.x = t.vector.x;
00506 deshake->last.vector.y = t.vector.y;
00507 deshake->last.angle = t.angle;
00508 deshake->last.zoom = t.zoom;
00509
00510
00511 avfilter_get_matrix(t.vector.x, t.vector.y, t.angle, 1.0 + t.zoom / 100.0, matrix);
00512
00513
00514 avfilter_transform(in->data[0], out->data[0], in->linesize[0], out->linesize[0], link->w, link->h, matrix, INTERPOLATE_BILINEAR, deshake->edge);
00515
00516
00517 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);
00518
00519
00520 avfilter_transform(in->data[1], out->data[1], in->linesize[1], out->linesize[1], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), matrix, INTERPOLATE_BILINEAR, deshake->edge);
00521 avfilter_transform(in->data[2], out->data[2], in->linesize[2], out->linesize[2], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), matrix, INTERPOLATE_BILINEAR, deshake->edge);
00522
00523
00524
00525 if (deshake->ref != NULL)
00526 avfilter_unref_buffer(deshake->ref);
00527
00528
00529 deshake->ref = in;
00530
00531
00532 ff_draw_slice(link->dst->outputs[0], 0, link->h, 1);
00533 return ff_end_frame(link->dst->outputs[0]);
00534 }
00535
00536 static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00537 {
00538 return 0;
00539 }
00540
00541 AVFilter avfilter_vf_deshake = {
00542 .name = "deshake",
00543 .description = NULL_IF_CONFIG_SMALL("Stabilize shaky video."),
00544
00545 .priv_size = sizeof(DeshakeContext),
00546
00547 .init = init,
00548 .uninit = uninit,
00549 .query_formats = query_formats,
00550
00551 .inputs = (const AVFilterPad[]) {{ .name = "default",
00552 .type = AVMEDIA_TYPE_VIDEO,
00553 .draw_slice = draw_slice,
00554 .end_frame = end_frame,
00555 .config_props = config_props,
00556 .min_perms = AV_PERM_READ | AV_PERM_PRESERVE, },
00557 { .name = NULL}},
00558
00559 .outputs = (const AVFilterPad[]) {{ .name = "default",
00560 .type = AVMEDIA_TYPE_VIDEO, },
00561 { .name = NULL}},
00562 };