65 #define OFFSET(x) offsetof(DeshakeContext, x)
66 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
92 static int cmp(
const void *
a,
const void *
b)
94 return FFDIFFSIGN(*(
const double *)a, *(
const double *)b);
108 for (x = cut; x < count - cut; x++) {
112 return mean / (count - cut * 2);
127 int smallest = INT_MAX;
130 #define CMP(i, j) deshake->sad(src1 + cy * stride + cx, stride,\
131 src2 + (j) * stride + (i), stride)
135 for (y = -deshake->
ry; y <= deshake->ry; y++) {
136 for (x = -deshake->
rx; x <= deshake->rx; x++) {
137 diff =
CMP(cx - x, cy - y);
138 if (diff < smallest) {
147 for (y = -deshake->
ry + 1; y < deshake->ry; y += 2) {
148 for (x = -deshake->
rx + 1; x < deshake->rx; x += 2) {
149 diff =
CMP(cx - x, cy - y);
150 if (diff < smallest) {
162 for (y = tmp2 - 1; y <= tmp2 + 1; y++) {
163 for (x = tmp - 1; x <= tmp + 1; x++) {
164 if (x == tmp && y == tmp2)
167 diff =
CMP(cx - x, cy - y);
168 if (diff < smallest) {
177 if (smallest > 512) {
197 for (i = 0; i <= blocksize * 2; i++) {
199 for (j = 0; j <= 15; j++) {
200 pos = (y - i) * stride + (x - j);
201 if (src[pos] < lowest)
203 else if (src[pos] > highest) {
209 return highest - lowest;
219 a1 = atan2(y - cy, x - cx);
220 a2 = atan2(y - cy + shift->
y, x - cx + shift->
x);
224 return (diff >
M_PI) ? diff - 2 *
M_PI :
241 int count_max_value = 0;
245 int center_x = 0, center_y = 0;
251 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
252 for (y = 0; y < deshake->
ry * 2 + 1; y++) {
253 deshake->
counts[x][y] = 0;
259 for (y = deshake->
ry; y < height - deshake->ry - (deshake->
blocksize * 2); y += deshake->
blocksize * 2) {
261 for (x = deshake->
rx; x < width - deshake->rx - 16; x += 16) {
268 if (mv.
x != -1 && mv.
y != -1) {
269 deshake->
counts[mv.
x + deshake->
rx][mv.
y + deshake->
ry] += 1;
270 if (x > deshake->
rx && y > deshake->
ry)
284 if (t->
angle < 0.001)
291 for (y = deshake->
ry * 2; y >= 0; y--) {
292 for (x = 0; x < deshake->
rx * 2 + 1; x++) {
294 if (deshake->
counts[x][y] > count_max_value) {
295 t->
vec.
x = x - deshake->
rx;
296 t->
vec.
y = y - deshake->
ry;
297 count_max_value = deshake->
counts[x][y];
303 p_x = (center_x - width / 2.0);
304 p_y = (center_y - height / 2.0);
309 t->
vec.
x = av_clipf(t->
vec.
x, -deshake->
rx * 2, deshake->
rx * 2);
310 t->
vec.
y = av_clipf(t->
vec.
y, -deshake->
ry * 2, deshake->
ry * 2);
318 const float *matrix_y,
const float *matrix_uv,
323 const float *matrixs[3];
324 int plane_w[3], plane_h[3];
325 matrixs[0] = matrix_y;
326 matrixs[1] = matrixs[2] = matrix_uv;
328 plane_w[1] = plane_w[2] = cw;
330 plane_h[1] = plane_h[2] = ch;
332 for (i = 0; i < 3; i++) {
335 plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
355 if (deshake->
rx % 16) {
363 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);
367 if (deshake->
cx > 0) {
368 deshake->
cw += deshake->
cx - (deshake->
cx & ~15);
372 if (!CONFIG_OPENCL && deshake->
opencl) {
373 av_log(ctx,
AV_LOG_ERROR,
"OpenCL support was not enabled in this build, cannot be selected\n");
377 if (CONFIG_OPENCL && deshake->
opencl) {
383 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",
384 deshake->
cx, deshake->
cy, deshake->
cw, deshake->
ch,
419 if (CONFIG_OPENCL && deshake->
opencl) {
435 float matrix_y[9], matrix_uv[9];
450 if (CONFIG_OPENCL && deshake->
opencl) {
456 if (deshake->
cx < 0 || deshake->
cy < 0 || deshake->
cw < 0 || deshake->
ch < 0) {
466 if ((
unsigned)deshake->
cx + (
unsigned)deshake->
cw > link->
w) deshake->
cw = link->
w - deshake->
cx;
467 if ((
unsigned)deshake->
cy + (
unsigned)deshake->
ch > link->
h) deshake->
ch = link->
h - deshake->
cy;
480 orig.vec.x = t.
vec.
x;
481 orig.vec.y = t.
vec.
y;
482 orig.angle = t.
angle;
505 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);
506 fwrite(tmp,
sizeof(
char), strlen(tmp), deshake->
fp);
532 ret = deshake->
transform(link->
dst, link->
w, link->
h, chroma_width, chroma_height,
575 .priv_class = &deshake_class,
static int shift(int a, int b)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
int ry
Maximum vertical shift.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Main libavfilter public API header.
memory handling functions
int h
agreed upon image height
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
const char * name
Pad name.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
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 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 double clean_mean(double *values, int count)
Cleaned mean (cuts off 20% of values to remove outliers and then averages)
static void interpolate(float *out, float v1, float v2, int size)
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
int blocksize
Size of blocks to compare.
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
#define AV_LOG_VERBOSE
Detailed information.
A filter pad used for either input or output.
A link between two filters.
int refcount
Number of reference frames (defines averaging window)
int rx
Maximum horizontal shift.
int ff_opencl_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)
static double alpha(void *priv, double x, double y)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFILTER_DEFINE_CLASS(deshake)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
char * filename
Motion search detailed log filename.
void * priv
private data for use by the filter
static int filter_frame(AVFilterLink *link, AVFrame *in)
double x
Horizontal shift.
static int query_formats(AVFilterContext *ctx)
av_pixelutils_sad_fn sad
Sum of the absolute difference function.
static const AVFilterPad deshake_inputs[]
int contrast
Contrast threshold.
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int w
agreed upon image width
#define FFDIFFSIGN(x, y)
Comparator.
static double block_angle(int x, int y, int cx, int cy, IntMotionVector *shift)
Find the rotation for a given block.
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)
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
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...
int edge
Edge fill method.
int search
Motion search method.
static const AVFilterPad outputs[]
static const AVOption deshake_options[]
int format
agreed upon media format
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
static const int8_t mv[256][2]
Search all possible positions.
int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
static const AVFilterPad inputs[]
double * angles
< Scratch buffer for motion search
static int block_contrast(uint8_t *src, int x, int y, int stride, int blocksize)
Find the contrast of a given block.
AVFrame * ref
Previous frame.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Search most possible positions (faster)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
BYTE int const BYTE int int int height
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
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 int config_props(AVFilterLink *link)
static av_cold int init(AVFilterContext *ctx)
const char * name
Filter name.
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)
static int cmp(const void *a, const void *b)
AVFilterLink ** outputs
array of pointers to output links
int counts[2 *MAX_R+1][2 *MAX_R+1]
static enum AVPixelFormat pix_fmts[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int cw
Crop motion search to this box.
Transform last
Transform from last frame.
GLint GLenum GLboolean GLsizei stride
int ff_opencl_deshake_init(AVFilterContext *ctx)
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal and external API header
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const AVFilterPad deshake_outputs[]
AVFilterContext * dst
dest filter
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
AVPixelFormat
Pixel format.
#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 av_cold void uninit(AVFilterContext *ctx)
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
void ff_opencl_deshake_uninit(AVFilterContext *ctx)
#define AV_CEIL_RSHIFT(a, b)