avfilter/vf_guided: fix reallocation of memory per every frame's plane processing

This commit is contained in:
Paul B Mahol 2022-05-02 23:44:21 +02:00
parent 17a4237a05
commit 810c508956
1 changed files with 62 additions and 48 deletions

View File

@ -59,6 +59,20 @@ typedef struct GuidedContext {
int planewidth[4];
int planeheight[4];
float *I;
float *II;
float *P;
float *IP;
float *meanI;
float *meanII;
float *meanP;
float *meanIP;
float *A;
float *B;
float *meanA;
float *meanB;
int (*box_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
} GuidedContext;
@ -197,38 +211,19 @@ static int guided_##name(AVFilterContext *ctx, GuidedContext *s,
\
ThreadData t; \
const int nb_threads = ff_filter_get_nb_threads(ctx); \
float *I; \
float *II; \
float *P; \
float *IP; \
float *meanI; \
float *meanII; \
float *meanP; \
float *meanIP; \
float *A; \
float *B; \
float *meanA; \
float *meanB; \
float *I = s->I; \
float *II = s->II; \
float *P = s->P; \
float *IP = s->IP; \
float *meanI = s->meanI; \
float *meanII = s->meanII; \
float *meanP = s->meanP; \
float *meanIP = s->meanIP; \
float *A = s->A; \
float *B = s->B; \
float *meanA = s->meanA; \
float *meanB = s->meanB; \
\
I = av_calloc(w * h, sizeof(float)); \
II = av_calloc(w * h, sizeof(float)); \
P = av_calloc(w * h, sizeof(float)); \
IP = av_calloc(w * h, sizeof(float)); \
meanI = av_calloc(w * h, sizeof(float)); \
meanII = av_calloc(w * h, sizeof(float)); \
meanP = av_calloc(w * h, sizeof(float)); \
meanIP = av_calloc(w * h, sizeof(float)); \
\
A = av_calloc(w * h, sizeof(float)); \
B = av_calloc(w * h, sizeof(float)); \
meanA = av_calloc(w * h, sizeof(float)); \
meanB = av_calloc(w * h, sizeof(float)); \
\
if (!I || !II || !P || !IP || !meanI || !meanII || !meanP || \
!meanIP || !A || !B || !meanA || !meanB) { \
ret = AVERROR(ENOMEM); \
goto end; \
} \
for (int i = 0;i < h;i++) { \
for (int j = 0;j < w;j++) { \
int x = i * w + j; \
@ -280,19 +275,7 @@ static int guided_##name(AVFilterContext *ctx, GuidedContext *s,
meanB[x] * maxval; \
} \
} \
end: \
av_freep(&I); \
av_freep(&II); \
av_freep(&P); \
av_freep(&IP); \
av_freep(&meanI); \
av_freep(&meanII); \
av_freep(&meanP); \
av_freep(&meanIP); \
av_freep(&A); \
av_freep(&B); \
av_freep(&meanA); \
av_freep(&meanB); \
\
return ret; \
}
@ -352,11 +335,10 @@ static int process_frame(FFFrameSync *fs)
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
GuidedContext *s = ctx->priv;
AVFilterLink *mainlink = ctx->inputs[0];
FFFrameSyncIn *in;
int ret;
int w, h, ret;
if (s->guidance == ON) {
if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
@ -366,12 +348,30 @@ static int config_output(AVFilterLink *outlink)
}
}
outlink->w = mainlink->w;
outlink->h = mainlink->h;
outlink->w = w = mainlink->w;
outlink->h = h = mainlink->h;
outlink->time_base = mainlink->time_base;
outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
outlink->frame_rate = mainlink->frame_rate;
s->I = av_calloc(w * h, sizeof(*s->I));
s->II = av_calloc(w * h, sizeof(*s->II));
s->P = av_calloc(w * h, sizeof(*s->P));
s->IP = av_calloc(w * h, sizeof(*s->IP));
s->meanI = av_calloc(w * h, sizeof(*s->meanI));
s->meanII = av_calloc(w * h, sizeof(*s->meanII));
s->meanP = av_calloc(w * h, sizeof(*s->meanP));
s->meanIP = av_calloc(w * h, sizeof(*s->meanIP));
s->A = av_calloc(w * h, sizeof(*s->A));
s->B = av_calloc(w * h, sizeof(*s->B));
s->meanA = av_calloc(w * h, sizeof(*s->meanA));
s->meanB = av_calloc(w * h, sizeof(*s->meanA));
if (!s->I || !s->II || !s->P || !s->IP || !s->meanI || !s->meanII || !s->meanP ||
!s->meanIP || !s->A || !s->B || !s->meanA || !s->meanB)
return AVERROR(ENOMEM);
if (s->guidance == OFF)
return 0;
@ -460,6 +460,20 @@ static av_cold void uninit(AVFilterContext *ctx)
GuidedContext *s = ctx->priv;
if (s->guidance == ON)
ff_framesync_uninit(&s->fs);
av_freep(&s->I);
av_freep(&s->II);
av_freep(&s->P);
av_freep(&s->IP);
av_freep(&s->meanI);
av_freep(&s->meanII);
av_freep(&s->meanP);
av_freep(&s->meanIP);
av_freep(&s->A);
av_freep(&s->B);
av_freep(&s->meanA);
av_freep(&s->meanB);
return;
}