You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-08-10 06:10:52 +02:00
vf_deshake: Avoid doing a malloc+free for every single frame.
Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>
This commit is contained in:
@@ -76,6 +76,8 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
const AVClass *class;
|
const AVClass *class;
|
||||||
int counts[2*MAX_R+1][2*MAX_R+1]; /// < Scratch buffer for motion search
|
int counts[2*MAX_R+1][2*MAX_R+1]; /// < Scratch buffer for motion search
|
||||||
|
double *angles; ///< Scratch buffer for block angles
|
||||||
|
unsigned angles_size;
|
||||||
AVFrame *ref; ///< Previous frame
|
AVFrame *ref; ///< Previous frame
|
||||||
int rx; ///< Maximum horizontal shift
|
int rx; ///< Maximum horizontal shift
|
||||||
int ry; ///< Maximum vertical shift
|
int ry; ///< Maximum vertical shift
|
||||||
|
@@ -244,10 +244,11 @@ static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
|
|||||||
int contrast;
|
int contrast;
|
||||||
|
|
||||||
int pos;
|
int pos;
|
||||||
double *angles = av_malloc_array(width * height / (16 * deshake->blocksize), sizeof(*angles));
|
|
||||||
int center_x = 0, center_y = 0;
|
int center_x = 0, center_y = 0;
|
||||||
double p_x, p_y;
|
double p_x, p_y;
|
||||||
|
|
||||||
|
av_fast_malloc(&deshake->angles, &deshake->angles_size, width * height / (16 * deshake->blocksize) * sizeof(*deshake->angles));
|
||||||
|
|
||||||
// Reset counts to zero
|
// Reset counts to zero
|
||||||
for (x = 0; x < deshake->rx * 2 + 1; x++) {
|
for (x = 0; x < deshake->rx * 2 + 1; x++) {
|
||||||
for (y = 0; y < deshake->ry * 2 + 1; y++) {
|
for (y = 0; y < deshake->ry * 2 + 1; y++) {
|
||||||
@@ -269,7 +270,7 @@ static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
|
|||||||
if (mv.x != -1 && mv.y != -1) {
|
if (mv.x != -1 && mv.y != -1) {
|
||||||
deshake->counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
|
deshake->counts[mv.x + deshake->rx][mv.y + deshake->ry] += 1;
|
||||||
if (x > deshake->rx && y > deshake->ry)
|
if (x > deshake->rx && y > deshake->ry)
|
||||||
angles[pos++] = block_angle(x, y, 0, 0, &mv);
|
deshake->angles[pos++] = block_angle(x, y, 0, 0, &mv);
|
||||||
|
|
||||||
center_x += mv.x;
|
center_x += mv.x;
|
||||||
center_y += mv.y;
|
center_y += mv.y;
|
||||||
@@ -281,7 +282,7 @@ static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
|
|||||||
if (pos) {
|
if (pos) {
|
||||||
center_x /= pos;
|
center_x /= pos;
|
||||||
center_y /= pos;
|
center_y /= pos;
|
||||||
t->angle = clean_mean(angles, pos);
|
t->angle = clean_mean(deshake->angles, pos);
|
||||||
if (t->angle < 0.001)
|
if (t->angle < 0.001)
|
||||||
t->angle = 0;
|
t->angle = 0;
|
||||||
} else {
|
} else {
|
||||||
@@ -312,7 +313,6 @@ static void find_motion(DeshakeContext *deshake, uint8_t *src1, uint8_t *src2,
|
|||||||
t->angle = av_clipf(t->angle, -0.1, 0.1);
|
t->angle = av_clipf(t->angle, -0.1, 0.1);
|
||||||
|
|
||||||
//av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
|
//av_log(NULL, AV_LOG_ERROR, "%d x %d\n", avg->x, avg->y);
|
||||||
av_free(angles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int deshake_transform_c(AVFilterContext *ctx,
|
static int deshake_transform_c(AVFilterContext *ctx,
|
||||||
@@ -422,6 +422,8 @@ static av_cold void uninit(AVFilterContext *ctx)
|
|||||||
ff_opencl_deshake_uninit(ctx);
|
ff_opencl_deshake_uninit(ctx);
|
||||||
}
|
}
|
||||||
av_frame_free(&deshake->ref);
|
av_frame_free(&deshake->ref);
|
||||||
|
av_freep(&deshake->angles);
|
||||||
|
deshake->angles_size = 0;
|
||||||
if (deshake->fp)
|
if (deshake->fp)
|
||||||
fclose(deshake->fp);
|
fclose(deshake->fp);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user