Go to the documentation of this file.
64 #define OFFSET(x) offsetof(DeshakeContext, x)
65 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
91 static int cmp(
const void *
a,
const void *
b)
93 return FFDIFFSIGN(*(
const double *)
a, *(
const double *)
b);
107 for (x = cut; x < count - cut; x++) {
111 return mean / (count - cut * 2);
121 uint8_t *src2,
int cx,
int cy,
int stride,
126 int smallest = INT_MAX;
129 #define CMP(i, j) deshake->sad(src1 + cy * stride + cx, stride,\
130 src2 + (j) * stride + (i), stride)
134 for (y = -deshake->
ry; y <= deshake->ry; y++) {
135 for (x = -deshake->
rx; x <= deshake->rx; x++) {
137 if (
diff < smallest) {
146 for (y = -deshake->
ry + 1; y < deshake->ry; y += 2) {
147 for (x = -deshake->
rx + 1; x < deshake->rx; x += 2) {
149 if (
diff < smallest) {
161 for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
162 for (x =
tmp - 1; x <=
tmp + 1; x++) {
163 if (x ==
tmp && y == tmp2)
167 if (
diff < smallest) {
176 if (smallest > 512) {
196 for (
i = 0;
i <= blocksize * 2;
i++) {
198 for (j = 0; j <= 15; j++) {
202 else if (
src[
pos] > highest) {
208 return highest - lowest;
218 a1 = atan2(y - cy, x - cx);
240 int count_max_value = 0;
244 int center_x = 0, center_y = 0;
250 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
251 for (y = 0; y < deshake->
ry * 2 + 1; y++) {
252 deshake->
counts[x][y] = 0;
258 for (y = deshake->
ry; y < height - deshake->ry - (deshake->
blocksize * 2); y += deshake->
blocksize * 2) {
260 for (x = deshake->
rx; x < width - deshake->rx - 16; x += 16) {
267 if (
mv.x != -1 &&
mv.y != -1) {
269 if (x > deshake->
rx && y > deshake->
ry)
283 if (t->
angle < 0.001)
290 for (y = deshake->
ry * 2; y >= 0; y--) {
291 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
293 if (deshake->
counts[x][y] > count_max_value) {
294 t->
vec.
x = x - deshake->
rx;
295 t->
vec.
y = y - deshake->
ry;
296 count_max_value = deshake->
counts[x][y];
302 p_x = (center_x -
width / 2.0);
303 p_y = (center_y -
height / 2.0);
317 const float *matrix_y,
const float *matrix_uv,
322 const float *matrixs[3];
323 int plane_w[3], plane_h[3];
324 matrixs[0] = matrix_y;
325 matrixs[1] = matrixs[2] = matrix_uv;
327 plane_w[1] = plane_w[2] = cw;
329 plane_h[1] = plane_h[2] = ch;
331 for (
i = 0;
i < 3;
i++) {
334 out->linesize[
i], plane_w[
i], plane_h[
i],
350 if (deshake->
rx % 16) {
358 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);
362 if (deshake->
cx > 0) {
363 deshake->
cw += deshake->
cx - (deshake->
cx & ~15);
368 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",
369 deshake->
cx, deshake->
cy, deshake->
cw, deshake->
ch,
410 float matrix_y[9], matrix_uv[9];
418 float transform_zoom;
432 if (deshake->
cx < 0 || deshake->
cy < 0 || deshake->
cw < 0 || deshake->
ch < 0) {
437 uint8_t *src2 = in->
data[0];
442 if ((
unsigned)deshake->
cx + (
unsigned)deshake->
cw >
link->w) deshake->
cw =
link->w - deshake->
cx;
443 if ((
unsigned)deshake->
cy + (
unsigned)deshake->
ch >
link->h) deshake->
ch =
link->h - deshake->
cy;
456 orig.vec.x = t.
vec.
x;
457 orig.vec.y = t.
vec.
y;
458 orig.angle = t.
angle;
481 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);
482 fwrite(
tmp, 1, strlen(
tmp), deshake->
fp);
503 transform_zoom = 1.0 + t.
zoom / 100.0;
554 .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
@ SMART_EXHAUSTIVE
Search most possible positions (faster)
int ry
Maximum vertical shift.
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)
#define FILTER_PIXFMTS_ARRAY(array)
static const int8_t mv[256][2]
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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 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.
@ 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_INPUTS(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.
#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...
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.
int counts[2 *MAX_R+1][2 *MAX_R+1]
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
double * angles
< Scratch buffer for motion search
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.
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)
@ EXHAUSTIVE
Search all possible positions.
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_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)
static int shift(int a, int b)
static const AVFilterPad deshake_outputs[]
@ 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 av_always_inline int diff(const uint32_t a, const uint32_t b)
static const int16_t alpha[]
int rx
Maximum horizontal shift.
#define FILTER_OUTPUTS(array)
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)
av_pixelutils_sad_fn sad
Sum of the absolute difference function.