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