[FFmpeg-cvslog] avfilter/vf_mix: add fast filtering in case all tmix weights are same

Paul B Mahol git at videolan.org
Fri Jun 2 19:56:38 EEST 2023


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Fri Jun  2 01:08:56 2023 +0200| [9a9b56c8706957267c57a0c97db890b170b2bb9b] | committer: Paul B Mahol

avfilter/vf_mix: add fast filtering in case all tmix weights are same

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=9a9b56c8706957267c57a0c97db890b170b2bb9b
---

 libavfilter/vf_mix.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 73 insertions(+), 2 deletions(-)

diff --git a/libavfilter/vf_mix.c b/libavfilter/vf_mix.c
index 70fe90ea30..446df2c326 100644
--- a/libavfilter/vf_mix.c
+++ b/libavfilter/vf_mix.c
@@ -43,8 +43,10 @@ typedef struct MixContext {
     float scale;
     float wfactor;
 
+    int fast;
     int tmix;
     int nb_frames;
+    int nb_unique_frames;
 
     int depth;
     int max;
@@ -53,6 +55,8 @@ typedef struct MixContext {
     int linesizes[4];
     int height[4];
 
+    uint8_t *sum[4];
+
     uint8_t **data;
     int *linesize;
 
@@ -81,6 +85,7 @@ static int parse_weights(AVFilterContext *ctx)
     char *p, *arg, *saveptr = NULL;
     int i, last = 0;
 
+    s->fast = 1;
     s->wfactor = 0.f;
     p = s->weights_str;
     for (i = 0; i < s->nb_inputs; i++) {
@@ -93,6 +98,8 @@ static int parse_weights(AVFilterContext *ctx)
             return AVERROR(EINVAL);
         }
         s->wfactor += s->weights[i];
+        if (i > 0)
+            s->fast &= s->weights[i] == s->weights[0];
         last = i;
     }
 
@@ -103,6 +110,8 @@ static int parse_weights(AVFilterContext *ctx)
     if (s->scale == 0) {
         s->wfactor = 1 / s->wfactor;
     } else {
+        if (s->scale != 1.f / s->wfactor)
+            s->fast = 0;
         s->wfactor = s->scale;
     }
 
@@ -145,13 +154,52 @@ typedef struct ThreadData {
     AVFrame **in, *out;
 } ThreadData;
 
+#define FAST_TMIX_SLICE(type, stype, round)                                                     \
+    for (int p = 0; p < s->nb_planes; p++) {                                                    \
+        const int slice_start = (s->height[p] * jobnr) / nb_jobs;                               \
+        const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;                             \
+        const int width = s->linesizes[p] / sizeof(type);                                       \
+        stype *sum = (stype *)(s->sum[p] + slice_start * s->linesizes[p] * 2);                  \
+        type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);                    \
+        const ptrdiff_t sum_linesize = (s->linesizes[p] * 2) / sizeof(stype);                   \
+        const ptrdiff_t dst_linesize = out->linesize[p] / sizeof(type);                         \
+        const int idx = FFMAX(0, nb_inputs - nb_unique);                                        \
+        const ptrdiff_t src_linesize[2] = { in[idx]->linesize[p],                               \
+                                            in[nb_inputs-1]->linesize[p] };                     \
+        const type *src[2];                                                                     \
+                                                                                                \
+        if (!((1 << p) & s->planes)) {                                                          \
+            av_image_copy_plane((uint8_t *)dst, out->linesize[p],                               \
+                                in[0]->data[p] + slice_start * in[0]->linesize[p],              \
+                                in[0]->linesize[p],                                             \
+                                s->linesizes[p], slice_end - slice_start);                      \
+            continue;                                                                           \
+        }                                                                                       \
+                                                                                                \
+        src[0] = (const type *)(in[idx]->data[p] + slice_start*src_linesize[0]);                \
+        src[1] = (const type *)(in[nb_inputs-1]->data[p] + slice_start * src_linesize[1]);      \
+                                                                                                \
+        for (int y = slice_start; y < slice_end; y++) {                                         \
+            for (int x = 0; x < width; x++) {                                                   \
+                sum[x] += src[1][x] * (1 + (nb_inputs - 1) * (idx == (nb_inputs - 1)));         \
+                dst[x] = (sum[x] + (round)) / nb_inputs;                                        \
+                sum[x] -= src[0][x];                                                            \
+            }                                                                                   \
+                                                                                                \
+            dst    += dst_linesize;                                                             \
+            sum    += sum_linesize;                                                             \
+            src[0] += src_linesize[0] / sizeof(type);                                           \
+            src[1] += src_linesize[1] / sizeof(type);                                           \
+        }                                                                                       \
+    }
+
 #define MIX_SLICE(type, fun, clip)                                                              \
     for (int p = 0; p < s->nb_planes; p++) {                                                    \
         const int slice_start = (s->height[p] * jobnr) / nb_jobs;                               \
         const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs;                             \
         const int width = s->linesizes[p] / sizeof(type);                                       \
         type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]);                    \
-        ptrdiff_t dst_linesize = out->linesize[p] / sizeof(type);                               \
+        const ptrdiff_t dst_linesize = out->linesize[p] / sizeof(type);                         \
                                                                                                 \
         if (!((1 << p) & s->planes)) {                                                          \
             av_image_copy_plane((uint8_t *)dst, out->linesize[p],                               \
@@ -200,10 +248,23 @@ static int mix_frames(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     const float *weights = s->weights;
     uint8_t **srcf = s->data + jobnr * s->nb_inputs;
     int *linesize = s->linesize + jobnr * s->nb_inputs;
+    const int nb_unique = s->nb_unique_frames;
     const int nb_inputs = s->nb_inputs;
     const float wfactor = s->wfactor;
     const int max = s->max;
 
+    if (s->tmix && s->fast) {
+        if (s->depth <= 8) {
+            FAST_TMIX_SLICE(uint8_t, uint16_t, nb_inputs >> 1)
+        } else if (s->depth <= 16) {
+            FAST_TMIX_SLICE(uint16_t, uint32_t, nb_inputs >> 1)
+        } else {
+            FAST_TMIX_SLICE(float, float, 0.f)
+        }
+
+        return 0;
+    }
+
     if (s->depth <= 8) {
         MIX_SLICE(uint8_t, lrintf, CLIP8)
     } else if (s->depth <= 16) {
@@ -294,8 +355,14 @@ static int config_output(AVFilterLink *outlink)
     if (!s->linesize)
         return AVERROR(ENOMEM);
 
-    if (s->tmix)
+    if (s->tmix) {
+        for (int p = 0; p < s->nb_planes; p++) {
+            s->sum[p] = av_calloc(s->linesizes[p], s->height[p] * sizeof(*s->sum) * 2);
+            if (!s->sum[p])
+                return AVERROR(ENOMEM);
+        }
         return 0;
+    }
 
     outlink->w          = width;
     outlink->h          = height;
@@ -335,6 +402,8 @@ static av_cold void uninit(AVFilterContext *ctx)
     av_freep(&s->linesize);
 
     if (s->tmix) {
+        for (i = 0; i < 4; i++)
+            av_freep(&s->sum[i]);
         for (i = 0; i < s->nb_frames && s->frames; i++)
             av_frame_free(&s->frames[i]);
     }
@@ -418,6 +487,7 @@ static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in)
     if (s->nb_frames < s->nb_inputs) {
         s->frames[s->nb_frames] = in;
         s->nb_frames++;
+        s->nb_unique_frames++;
         while (s->nb_frames < s->nb_inputs) {
             s->frames[s->nb_frames] = av_frame_clone(s->frames[s->nb_frames - 1]);
             if (!s->frames[s->nb_frames])
@@ -425,6 +495,7 @@ static int tmix_filter_frame(AVFilterLink *inlink, AVFrame *in)
             s->nb_frames++;
         }
     } else {
+        s->nb_unique_frames = FFMIN(s->nb_unique_frames + 1, s->nb_inputs);
         av_frame_free(&s->frames[0]);
         memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1));
         s->frames[s->nb_inputs - 1] = in;



More information about the ffmpeg-cvslog mailing list