You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-08-04 22:03:09 +02:00
avfilter/vf_blackdetect: add AVX2 SIMD version
Requested by a user. Even with autovectorization enabled, the compiler performs a quite poor job of optimizing this function, due to not being able to take advantage of the pmaxub + pcmpeqb trick for counting the number of pixels less than or equal-to a threshold. blackdetect8_c: 4625.0 ( 1.00x) blackdetect8_avx2: 155.1 (29.83x) blackdetect16_c: 2529.4 ( 1.00x) blackdetect16_avx2: 163.6 (15.46x)
This commit is contained in:
@@ -33,6 +33,7 @@
|
|||||||
#include "filters.h"
|
#include "filters.h"
|
||||||
#include "formats.h"
|
#include "formats.h"
|
||||||
#include "video.h"
|
#include "video.h"
|
||||||
|
#include "vf_blackdetect.h"
|
||||||
|
|
||||||
typedef struct BlackDetectContext {
|
typedef struct BlackDetectContext {
|
||||||
const AVClass *class;
|
const AVClass *class;
|
||||||
@@ -53,6 +54,8 @@ typedef struct BlackDetectContext {
|
|||||||
int depth;
|
int depth;
|
||||||
int nb_threads;
|
int nb_threads;
|
||||||
unsigned int *counter;
|
unsigned int *counter;
|
||||||
|
|
||||||
|
ff_blackdetect_fn func;
|
||||||
} BlackDetectContext;
|
} BlackDetectContext;
|
||||||
|
|
||||||
#define OFFSET(x) offsetof(BlackDetectContext, x)
|
#define OFFSET(x) offsetof(BlackDetectContext, x)
|
||||||
@@ -133,6 +136,7 @@ static int config_input(AVFilterLink *inlink)
|
|||||||
s->time_base = inlink->time_base;
|
s->time_base = inlink->time_base;
|
||||||
s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
|
s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
|
||||||
s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
|
s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
|
||||||
|
s->func = ff_blackdetect_get_fn(depth);
|
||||||
if (!s->counter)
|
if (!s->counter)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
|
|
||||||
@@ -160,37 +164,16 @@ static int black_counter(AVFilterContext *ctx, void *arg,
|
|||||||
int jobnr, int nb_jobs)
|
int jobnr, int nb_jobs)
|
||||||
{
|
{
|
||||||
BlackDetectContext *s = ctx->priv;
|
BlackDetectContext *s = ctx->priv;
|
||||||
const unsigned int threshold = s->pixel_black_th_i;
|
const AVFrame *in = arg;
|
||||||
unsigned int *counterp = &s->counter[jobnr];
|
|
||||||
AVFrame *in = arg;
|
|
||||||
const int plane = s->alpha ? 3 : 0;
|
const int plane = s->alpha ? 3 : 0;
|
||||||
const int linesize = in->linesize[plane];
|
const int linesize = in->linesize[plane];
|
||||||
const int w = in->width;
|
|
||||||
const int h = in->height;
|
const int h = in->height;
|
||||||
const int start = (h * jobnr) / nb_jobs;
|
const int start = (h * jobnr) / nb_jobs;
|
||||||
const int end = (h * (jobnr+1)) / nb_jobs;
|
const int end = (h * (jobnr+1)) / nb_jobs;
|
||||||
const int size = end - start;
|
|
||||||
unsigned int counter = 0;
|
|
||||||
|
|
||||||
if (s->depth == 8) {
|
s->counter[jobnr] = s->func(in->data[plane] + start * linesize,
|
||||||
const uint8_t *p = in->data[plane] + start * linesize;
|
linesize, in->width, end - start,
|
||||||
|
s->pixel_black_th_i);
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
for (int x = 0; x < w; x++)
|
|
||||||
counter += p[x] <= threshold;
|
|
||||||
p += linesize;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const uint16_t *p = (const uint16_t *)(in->data[plane] + start * linesize);
|
|
||||||
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
for (int x = 0; x < w; x++)
|
|
||||||
counter += p[x] <= threshold;
|
|
||||||
p += linesize / 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*counterp = counter;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
71
libavfilter/vf_blackdetect.h
Normal file
71
libavfilter/vf_blackdetect.h
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of FFmpeg.
|
||||||
|
*
|
||||||
|
* FFmpeg is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* FFmpeg is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with FFmpeg; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef AVFILTER_VF_BLACKDETECT_H
|
||||||
|
#define AVFILTER_VF_BLACKDETECT_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef unsigned (*ff_blackdetect_fn)(const uint8_t *src, ptrdiff_t stride,
|
||||||
|
ptrdiff_t width, ptrdiff_t height,
|
||||||
|
unsigned threshold);
|
||||||
|
|
||||||
|
ff_blackdetect_fn ff_blackdetect_get_fn_x86(int depth);
|
||||||
|
|
||||||
|
static unsigned count_pixels8_c(const uint8_t *src, ptrdiff_t stride,
|
||||||
|
ptrdiff_t width, ptrdiff_t height,
|
||||||
|
unsigned threshold)
|
||||||
|
{
|
||||||
|
unsigned int counter = 0;
|
||||||
|
while (height--) {
|
||||||
|
for (int x = 0; x < width; x++)
|
||||||
|
counter += src[x] <= threshold;
|
||||||
|
src += stride;
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned count_pixels16_c(const uint8_t *src, ptrdiff_t stride,
|
||||||
|
ptrdiff_t width, ptrdiff_t height,
|
||||||
|
unsigned threshold)
|
||||||
|
{
|
||||||
|
unsigned int counter = 0;
|
||||||
|
while (height--) {
|
||||||
|
const uint16_t *src16 = (const uint16_t *) src;
|
||||||
|
for (int x = 0; x < width; x++)
|
||||||
|
counter += src16[x] <= threshold;
|
||||||
|
src += stride;
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline ff_blackdetect_fn ff_blackdetect_get_fn(int depth)
|
||||||
|
{
|
||||||
|
ff_blackdetect_fn fn = NULL;
|
||||||
|
#if ARCH_X86
|
||||||
|
fn = ff_blackdetect_get_fn_x86(depth);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (!fn)
|
||||||
|
fn = depth == 8 ? count_pixels8_c : count_pixels16_c;
|
||||||
|
return fn;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* AVFILTER_VF_BLACKDETECT_H */
|
@@ -3,6 +3,7 @@ OBJS-$(CONFIG_SCENE_SAD) += x86/scene_sad_init.o
|
|||||||
OBJS-$(CONFIG_AFIR_FILTER) += x86/af_afir_init.o
|
OBJS-$(CONFIG_AFIR_FILTER) += x86/af_afir_init.o
|
||||||
OBJS-$(CONFIG_ANLMDN_FILTER) += x86/af_anlmdn_init.o
|
OBJS-$(CONFIG_ANLMDN_FILTER) += x86/af_anlmdn_init.o
|
||||||
OBJS-$(CONFIG_ATADENOISE_FILTER) += x86/vf_atadenoise_init.o
|
OBJS-$(CONFIG_ATADENOISE_FILTER) += x86/vf_atadenoise_init.o
|
||||||
|
OBJS-$(CONFIG_BLACKDETECT_FILTER) += x86/vf_blackdetect_init.o
|
||||||
OBJS-$(CONFIG_BLEND_FILTER) += x86/vf_blend_init.o
|
OBJS-$(CONFIG_BLEND_FILTER) += x86/vf_blend_init.o
|
||||||
OBJS-$(CONFIG_BWDIF_FILTER) += x86/vf_bwdif_init.o
|
OBJS-$(CONFIG_BWDIF_FILTER) += x86/vf_bwdif_init.o
|
||||||
OBJS-$(CONFIG_COLORSPACE_FILTER) += x86/colorspacedsp_init.o
|
OBJS-$(CONFIG_COLORSPACE_FILTER) += x86/colorspacedsp_init.o
|
||||||
@@ -49,6 +50,7 @@ X86ASM-OBJS-$(CONFIG_SCENE_SAD) += x86/scene_sad.o
|
|||||||
X86ASM-OBJS-$(CONFIG_AFIR_FILTER) += x86/af_afir.o
|
X86ASM-OBJS-$(CONFIG_AFIR_FILTER) += x86/af_afir.o
|
||||||
X86ASM-OBJS-$(CONFIG_ANLMDN_FILTER) += x86/af_anlmdn.o
|
X86ASM-OBJS-$(CONFIG_ANLMDN_FILTER) += x86/af_anlmdn.o
|
||||||
X86ASM-OBJS-$(CONFIG_ATADENOISE_FILTER) += x86/vf_atadenoise.o
|
X86ASM-OBJS-$(CONFIG_ATADENOISE_FILTER) += x86/vf_atadenoise.o
|
||||||
|
X86ASM-OBJS-$(CONFIG_BLACKDETECT_FILTER) += x86/vf_blackdetect.o
|
||||||
X86ASM-OBJS-$(CONFIG_BLEND_FILTER) += x86/vf_blend.o
|
X86ASM-OBJS-$(CONFIG_BLEND_FILTER) += x86/vf_blend.o
|
||||||
X86ASM-OBJS-$(CONFIG_BWDIF_FILTER) += x86/vf_bwdif.o
|
X86ASM-OBJS-$(CONFIG_BWDIF_FILTER) += x86/vf_bwdif.o
|
||||||
X86ASM-OBJS-$(CONFIG_COLORSPACE_FILTER) += x86/colorspacedsp.o
|
X86ASM-OBJS-$(CONFIG_COLORSPACE_FILTER) += x86/colorspacedsp.o
|
||||||
|
73
libavfilter/x86/vf_blackdetect.asm
Normal file
73
libavfilter/x86/vf_blackdetect.asm
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
;*****************************************************************************
|
||||||
|
;* x86-optimized functions for blackdetect filter
|
||||||
|
;*
|
||||||
|
;* Copyright (C) 2025 Niklas Haas
|
||||||
|
;*
|
||||||
|
;* This file is part of FFmpeg.
|
||||||
|
;*
|
||||||
|
;* FFmpeg is free software; you can redistribute it and/or
|
||||||
|
;* modify it under the terms of the GNU Lesser General Public
|
||||||
|
;* License as published by the Free Software Foundation; either
|
||||||
|
;* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
;*
|
||||||
|
;* FFmpeg is distributed in the hope that it will be useful,
|
||||||
|
;* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
;* Lesser General Public License for more details.
|
||||||
|
;*
|
||||||
|
;* You should have received a copy of the GNU Lesser General Public
|
||||||
|
;* License along with FFmpeg; if not, write to the Free Software
|
||||||
|
;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
;*****************************************************************************
|
||||||
|
|
||||||
|
%include "libavutil/x86/x86util.asm"
|
||||||
|
|
||||||
|
SECTION .text
|
||||||
|
|
||||||
|
%macro count_pixels_fn 1 ; depth
|
||||||
|
cglobal blackdetect_%1, 5, 7, 2, src, stride, width, height, threshold
|
||||||
|
movd xm1, thresholdd
|
||||||
|
%if %1 == 8
|
||||||
|
vpbroadcastb m1, xm1
|
||||||
|
%else
|
||||||
|
vpbroadcastw m1, xm1
|
||||||
|
shl widthq, 1
|
||||||
|
%endif
|
||||||
|
add srcq, widthq
|
||||||
|
neg widthq
|
||||||
|
xor r4, r4
|
||||||
|
mov r5, widthq
|
||||||
|
jmp .start
|
||||||
|
.loop:
|
||||||
|
popcnt r6d, r6d
|
||||||
|
add r4, r6
|
||||||
|
.start:
|
||||||
|
movu m0, [srcq + r5]
|
||||||
|
%if %1 == 8
|
||||||
|
pmaxub m0, m1
|
||||||
|
pcmpeqb m0, m1
|
||||||
|
%else
|
||||||
|
pmaxuw m0, m1
|
||||||
|
pcmpeqw m0, m1
|
||||||
|
%endif
|
||||||
|
pmovmskb r6d, m0
|
||||||
|
add r5, mmsize
|
||||||
|
jl .loop
|
||||||
|
; handle tail by shifting away unused high elements
|
||||||
|
shlx r6d, r6d, r5d
|
||||||
|
popcnt r6d, r6d
|
||||||
|
add r4, r6
|
||||||
|
add srcq, strideq
|
||||||
|
mov r5, widthq
|
||||||
|
dec heightq
|
||||||
|
jg .start
|
||||||
|
%if %1 > 8
|
||||||
|
shr r4, 1
|
||||||
|
%endif
|
||||||
|
mov rax, r4
|
||||||
|
RET
|
||||||
|
%endmacro
|
||||||
|
|
||||||
|
INIT_YMM avx2
|
||||||
|
count_pixels_fn 8
|
||||||
|
count_pixels_fn 16
|
34
libavfilter/x86/vf_blackdetect_init.c
Normal file
34
libavfilter/x86/vf_blackdetect_init.c
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Niklas Haas
|
||||||
|
*
|
||||||
|
* This file is part of FFmpeg.
|
||||||
|
*
|
||||||
|
* FFmpeg is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* FFmpeg is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with FFmpeg; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "libavutil/attributes.h"
|
||||||
|
#include "libavutil/x86/cpu.h"
|
||||||
|
#include "libavfilter/vf_blackdetect.h"
|
||||||
|
|
||||||
|
unsigned ff_blackdetect_8_avx2(const uint8_t *, ptrdiff_t, ptrdiff_t, ptrdiff_t, unsigned);
|
||||||
|
unsigned ff_blackdetect_16_avx2(const uint8_t *, ptrdiff_t, ptrdiff_t, ptrdiff_t, unsigned);
|
||||||
|
|
||||||
|
av_cold ff_blackdetect_fn ff_blackdetect_get_fn_x86(int depth)
|
||||||
|
{
|
||||||
|
int cpu_flags = av_get_cpu_flags();
|
||||||
|
if (EXTERNAL_AVX2(cpu_flags))
|
||||||
|
return depth == 8 ? ff_blackdetect_8_avx2 : ff_blackdetect_16_avx2;
|
||||||
|
return NULL;
|
||||||
|
}
|
Reference in New Issue
Block a user