Go to the documentation of this file.
117 #define OFFSET(x) offsetof(DeshakeContext, x)
118 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
144 static 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;
601 .priv_class = &deshake_class,
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
AVPixelFormat
Pixel format.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int ry
Maximum vertical shift.
#define FILTER_PIXFMTS_ARRAY(array)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
static const int8_t mv[256][2]
double x
Horizontal shift.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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])
#define FILTER_INPUTS(array)
This structure describes decoded (raw) audio or video data.
static const AVOption deshake_options[]
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)
#define AV_LOG_VERBOSE
Detailed information.
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
static const AVFilterPad deshake_inputs[]
const char * name
Filter name.
A link between two filters.
static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
Find the contrast of a given block.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int blocksize
Size of blocks to compare.
AVFILTER_DEFINE_CLASS(deshake)
Transform last
Transform from last frame.
const AVFilter ff_vf_deshake
static av_cold int init(AVFilterContext *ctx)
static double a2(void *priv, double x, double y)
static int cmp(const void *a, const void *b)
A filter pad used for either input or output.
#define FFDIFFSIGN(x, y)
Comparator.
static int aligned(int val)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
int refcount
Number of reference frames (defines averaging window)
#define AV_CEIL_RSHIFT(a, b)
AVFrame * ref
Previous frame.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FILTER_OUTPUTS(array)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
char * filename
Motion search detailed log filename.
Describe the class of an AVClass context structure.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
@ SMART_EXHAUSTIVE
Search most possible positions (faster)
static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
Find the rotation for a given block.
int cw
Crop motion search to this box.
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 double clean_mean(double *values, int count)
Cleaned mean (cuts off 20% of values to remove outliers and then averages)
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 enum AVPixelFormat pix_fmts[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static int shift(int a, int b)
int counts[2 *MAX_R+1][2 *MAX_R+1]
Scratch buffer for motion search.
@ EXHAUSTIVE
Search all possible positions.
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
double * angles
Scratch buffer for block angles.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
static void interpolate(float *out, float v1, float v2, int size)
static int filter_frame(AVFilterLink *link, AVFrame *in)
static int config_props(AVFilterLink *link)
#define i(width, name, range_min, range_max)
#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...
int w
agreed upon image width
int contrast
Contrast threshold.
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
int search
Motion search method.
const char * name
Pad name.
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
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 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 av_cold void uninit(AVFilterContext *ctx)
int h
agreed upon image height
int edge
Edge fill method.
@ AV_OPT_TYPE_INT
Underlying C type is int.
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_...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return values
static float mean(const float *input, int size)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static const int16_t alpha[]
int rx
Maximum horizontal shift.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
static double a1(void *priv, double x, double y)
@ 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...
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
av_pixelutils_sad_fn sad
Sum of the absolute difference function.