117#define OFFSET(x) offsetof(DeshakeContext, x)
118#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
144static int cmp(
const void *
a,
const void *
b)
146 return FFDIFFSIGN(*(
const double *)
a, *(
const double *)
b);
160 for (x = cut; x < count - cut; x++) {
164 return mean / (count - cut * 2);
179 int smallest = INT_MAX;
182 #define CMP(i, j) deshake->sad(src1 + cy * stride + cx, stride,\
183 src2 + (j) * stride + (i), stride)
187 for (y = -deshake->
ry; y <= deshake->ry; y++) {
188 for (x = -deshake->
rx; x <= deshake->rx; x++) {
190 if (
diff < smallest) {
199 for (y = -deshake->
ry + 1; y < deshake->ry; y += 2) {
200 for (x = -deshake->
rx + 1; x < deshake->rx; x += 2) {
202 if (
diff < smallest) {
214 for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
215 for (x =
tmp - 1; x <=
tmp + 1; x++) {
216 if (x ==
tmp && y == tmp2)
220 if (
diff < smallest) {
229 if (smallest > 512) {
248 for (
i = 0;
i <= blocksize * 2;
i++) {
250 for (j = 0; j <= 15; j++) {
254 else if (
src[
pos] > highest) {
260 return highest - lowest;
270 a1 = atan2(y - cy, x - cx);
292 int count_max_value = 0;
296 int center_x = 0, center_y = 0;
302 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
303 for (y = 0; y < deshake->
ry * 2 + 1; y++) {
304 deshake->
counts[x][y] = 0;
310 for (y = deshake->
ry; y < height - deshake->ry - (deshake->
blocksize * 2); y += deshake->
blocksize * 2) {
312 for (x = deshake->
rx; x < width - deshake->rx - 16; x += 16) {
319 if (
mv.x != -1 &&
mv.y != -1) {
321 if (x > deshake->
rx && y > deshake->
ry)
335 if (t->
angle < 0.001)
342 for (y = deshake->
ry * 2; y >= 0; y--) {
343 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
345 if (deshake->
counts[x][y] > count_max_value) {
346 t->
vec.
x = x - deshake->
rx;
347 t->
vec.
y = y - deshake->
ry;
348 count_max_value = deshake->
counts[x][y];
354 p_x = (center_x -
width / 2.0);
355 p_y = (center_y -
height / 2.0);
369 const float *matrix_y,
const float *matrix_uv,
374 const float *matrixs[3];
375 int plane_w[3], plane_h[3];
376 matrixs[0] = matrix_y;
377 matrixs[1] = matrixs[2] = matrix_uv;
379 plane_w[1] = plane_w[2] = cw;
381 plane_h[1] = plane_h[2] = ch;
383 for (
i = 0;
i < 3;
i++) {
386 out->linesize[
i], plane_w[
i], plane_h[
i],
402 if (deshake->
rx % 16) {
410 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", 1, 104, deshake->
fp);
414 if (deshake->
cx > 0) {
415 deshake->
cw += deshake->
cx - (deshake->
cx & ~15);
420 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",
421 deshake->
cx, deshake->
cy, deshake->
cw, deshake->
ch,
462 float matrix_y[9], matrix_uv[9];
470 float transform_zoom;
486 if (deshake->
cx < 0 || deshake->
cy < 0 || deshake->
cw < 0 || deshake->
ch < 0) {
496 if ((
unsigned)deshake->
cx + (
unsigned)deshake->
cw > link->
w) deshake->
cw = link->
w - deshake->
cx;
497 if ((
unsigned)deshake->
cy + (
unsigned)deshake->
ch > link->
h) deshake->
ch = link->
h - deshake->
cy;
510 orig.vec.x = t.
vec.
x;
511 orig.vec.y = t.
vec.
y;
512 orig.angle = t.
angle;
535 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);
536 fwrite(
tmp, 1, strlen(
tmp), deshake->
fp);
557 transform_zoom = 1.0 + t.
zoom / 100.0;
562 ff_get_matrix(t.
vec.
x / (link->
w / chroma_width), t.
vec.
y / (link->
h / chroma_height), t.
angle, transform_zoom, transform_zoom, matrix_uv);
564 ret = deshake->
transform(link->
dst, link->
w, link->
h, chroma_width, chroma_height,
595 .p.priv_class = &deshake_class,
const FFFilter ff_vf_deshake
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
common internal and external API header
#define AV_CEIL_RSHIFT(a, b)
static int aligned(int val)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
static const int16_t alpha[]
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static const int8_t mv[256][2]
static int shift(int a, int b)
#define FILTER_INPUTS(array)
#define FILTER_OUTPUTS(array)
#define FILTER_PIXFMTS_ARRAY(array)
#define AVFILTER_DEFINE_CLASS(fname)
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
Get a potentially optimized pointer to a Sum-of-absolute-differences function (see the av_pixelutils_...
static enum AVPixelFormat pix_fmts[]
#define FFDIFFSIGN(x, y)
Comparator.
Memory handling functions.
static av_always_inline int cmp(MPVEncContext *const s, const int x, const int y, const int subx, const int suby, const int size, const int h, int ref_index, int src_index, me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags)
compares a block (either a full macroblock or a partition thereof) against a proposed motion-compensa...
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
int(* av_pixelutils_sad_fn)(const uint8_t *src1, ptrdiff_t stride1, const uint8_t *src2, ptrdiff_t stride2)
Sum of abs(src1[x] - src2[x])
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
#define AV_QSORT(p, num, type, cmp)
Quicksort This sort is fast, and fully inplace but not stable and it is possible to construct input t...
static int config_props(AVBitStreamFilterLink *link)
Describe the class of an AVClass context structure.
void * priv
private data for use by the filter
AVFilterLink ** outputs
array of pointers to output links
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
This structure describes decoded (raw) audio or video data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
av_pixelutils_sad_fn sad
Sum of the absolute difference function.
double * angles
Scratch buffer for block angles.
int cw
Crop motion search to this box.
AVFrame * ref
Previous frame.
Transform last
Transform from last frame.
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)
int edge
Edge fill method.
int blocksize
Size of blocks to compare.
int rx
Maximum horizontal shift.
int counts[2 *MAX_R+1][2 *MAX_R+1]
Scratch buffer for motion search.
int ry
Maximum vertical shift.
char * filename
Motion search detailed log filename.
int search
Motion search method.
int refcount
Number of reference frames (defines averaging window)
int contrast
Contrast threshold.
static AVFormatContext * ctx
static void interpolate(float *out, float v1, float v2, int size)
static const AVOption deshake_options[]
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)
static double clean_mean(double *values, int count)
Cleaned mean (cuts off 20% of values to remove outliers and then averages)
static int filter_frame(AVFilterLink *link, AVFrame *in)
static const AVFilterPad deshake_inputs[]
static int cmp(const void *a, const void *b)
static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
Find the rotation for a given block.
static av_cold void uninit(AVFilterContext *ctx)
@ SMART_EXHAUSTIVE
Search most possible positions (faster)
@ EXHAUSTIVE
Search all possible positions.
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.
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.
static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
Find the contrast of a given block.
static int config_props(AVFilterLink *link)
static float search(FOCContext *foc, int pass, int maxpass, int xmin, int xmax, int ymin, int ymax, int *best_x, int *best_y, float best_score)
static float mean(const float *input, int size)
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double a2(void *priv, double x, double y)
static double a1(void *priv, double x, double y)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.