PR #26398 opened by ksu-gitreaper807 URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26398 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26398.patch
# Summary of changes This WIP proposes exploring a fused Bayer demosaic-to-grayscale path that avoids materializing an intermediate RGB24 frame. The code under tools/bayer-gray-prototype is standalone research scaffolding. It is not integrated into libswscale and does not alter production conversion dispatch. It currently handles full-frame, unscaled conversions from the four 8-bit Bayer patterns to GRAY8, for even dimensions and default settings. Other cases use the existing converter. AVX-512 source is included, but its dispatch remains disabled and it has not yet been run on AVX-512 hardware. Preliminary byte-for-byte comparisons were run against libswscale on x86-64, AArch64 QEMU, and big-endian PPC64 QEMU. The PPC64 run exercised the reference fallback, not a PPC SIMD implementation. Early synthetic x86-64 and Samsung S20 FE measurements show potential, but are not yet repeatable evidence of a general speedup. Reproduction instructions and results are in the prototype README. Further work before this is ready for integration includes GCC Compile Farm validation, an AVX-512 hardware run, broader edge-case and API coverage, review of the fast-path eligibility conditions, integrated FATE/checkasm tests, and repeatable benchmarks on target hardware. I am opening this as WIP to get early feedback on the approach and whether it is suitable for FFmpeg. >From fa70a36cd6a6463786e6cf12bba1b1120dd45130 Mon Sep 17 00:00:00 2001 From: ksu-gitreaper807 <[email protected]> Date: Fri, 27 Nov 2026 13:18:52 +0000 Subject: [PATCH 1/2] WIP: add Bayer grayscale fusion prototype --- tools/bayer-gray-prototype/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tools/bayer-gray-prototype/README.md diff --git a/tools/bayer-gray-prototype/README.md b/tools/bayer-gray-prototype/README.md new file mode 100644 index 00000000000..bf6dc95b933 --- /dev/null +++ b/tools/bayer-gray-prototype/README.md @@ -0,0 +1,9 @@ +# Bayer to grayscale fused conversion prototype + +This is a work in progress for discussion. It evaluates fusing Bayer interpolation with grayscale conversion so that the temporary RGB24 image is not written and read back. It is not wired into libswscale, does not change FFmpeg's public API, and is not ready to merge. + +The prototype covers only unscaled full-frame conversion from the four 8-bit Bayer patterns to GRAY8, with even dimensions and default conversion settings. Other cases are outside the prototype's fast path. The portable scalar, x86 AVX2, and AArch64 NEON implementations are included. The AVX-512 implementation is experimental and is disabled by default; it has not been executed on AVX-512 hardware. Architectures without an implemented path continue to use the reference libswscale conversion in the standalone harness. + +Initial local validation compared output bytes against libswscale on x86-64, AArch64 QEMU, and big-endian PPC64 QEMU. Synthetic x86 and Samsung S20 FE measurements were encouraging, but they do not establish a general speedup. Additional GCC Compile Farm validation, AVX-512 execution, integrated FATE coverage, broader input/API coverage, and repeatable benchmarks on target hardware remain outstanding. Results and reproduction scripts: https://github.com/ksu-gitreaper807/ffmpeg-bayer-cfarm-validation + +The standalone harness in `bayer_gray_dispatch.c` needs to be compiled against a configured FFmpeg build. It is research scaffolding only; the integration design and eligibility checks need review before any production dispatch is added. \ No newline at end of file -- 2.54.1 >From 0de0e566ed4257a7bf8332a2b14d5e1042c3ca10 Mon Sep 17 00:00:00 2001 From: ksu-gitreaper807 <[email protected]> Date: Fri, 27 Nov 2026 13:19:56 +0000 Subject: [PATCH 2/2] WIP: add fused Bayer grayscale implementations --- .../bayer-gray-prototype/bayer_gray_avx512.h | 31 +++++ .../bayer_gray_dispatch.c | 71 +++++++++++ .../bayer_gray_fused_patterns.c | 110 ++++++++++++++++++ tools/bayer-gray-prototype/bayer_gray_neon.h | 24 ++++ 4 files changed, 236 insertions(+) create mode 100644 tools/bayer-gray-prototype/bayer_gray_avx512.h create mode 100644 tools/bayer-gray-prototype/bayer_gray_dispatch.c create mode 100644 tools/bayer-gray-prototype/bayer_gray_fused_patterns.c create mode 100644 tools/bayer-gray-prototype/bayer_gray_neon.h diff --git a/tools/bayer-gray-prototype/bayer_gray_avx512.h b/tools/bayer-gray-prototype/bayer_gray_avx512.h new file mode 100644 index 00000000000..ad85925a5b1 --- /dev/null +++ b/tools/bayer-gray-prototype/bayer_gray_avx512.h @@ -0,0 +1,31 @@ +#if HAS_AVX2_IMPL +#define AVX512 __attribute__((target("avx512f,avx2"))) +static AVX512 inline __m512i load16(const uint8_t *p) { __m256i v=_mm256_loadu_si256((const __m256i*)p); return _mm512_cvtepu16_epi32(_mm256_and_si256(v,_mm256_set1_epi16(255))); } +static AVX512 inline __m512i add512(__m512i a,__m512i b){return _mm512_add_epi32(a,b);} +static AVX512 inline __m512i avg2_512(__m512i a,__m512i b){return _mm512_srli_epi32(add512(a,b),1);} +static AVX512 inline __m512i avg4_512(__m512i a,__m512i b,__m512i c,__m512i d){return _mm512_srli_epi32(add512(add512(a,b),add512(c,d)),2);} +static AVX512 inline __m512i vg512(__m512i r,__m512i g,__m512i b){ + __m512i v=add512(add512(_mm512_mullo_epi32(r,_mm512_set1_epi32(8414)),_mm512_mullo_epi32(g,_mm512_set1_epi32(16519))),_mm512_mullo_epi32(b,_mm512_set1_epi32(3208))); + v=_mm512_srli_epi32(add512(v,_mm512_set1_epi32(524544)),9);v=_mm512_min_epi32(_mm512_slli_epi32(v,1),_mm512_set1_epi32(32767)); + v=_mm512_srai_epi32(add512(_mm512_mullo_epi32(v,_mm512_set1_epi32(19078)),_mm512_set1_epi32(-39084288)),14); + v=_mm512_srai_epi32(add512(v,_mm512_set1_epi32(64)),7);return _mm512_max_epi32(_mm512_setzero_si512(),_mm512_min_epi32(v,_mm512_set1_epi32(255))); +} +static AVX512 void blocks16(const uint8_t*s,ptrdiff_t st,uint8_t*d,ptrdiff_t dt,int fmt){ + const uint8_t *a=s-st,*b=s,*c=s+st,*e=s+2*st; + __m512i am=load16(a-1),a0=load16(a),ap=load16(a+1),bm=load16(b-1),b0=load16(b),bp=load16(b+1),b2=load16(b+2),cm=load16(c-1),c0=load16(c),cp=load16(c+1),c2=load16(c+2),e0=load16(e),ep=load16(e+1),e2=load16(e+2); + __m512i r0,r1,r2,r3,g0,g1,g2,g3,z0,z1,z2,z3; + if(fmt<2){r0=avg4_512(am,ap,cm,cp);g0=avg4_512(a0,bm,bp,c0);z0=b0; + r1=avg2_512(ap,cp);g1=bp;z1=avg2_512(b0,b2);r2=avg2_512(cm,cp);g2=c0;z2=avg2_512(b0,e0); + r3=cp;g3=avg4_512(bp,c0,c2,ep);z3=avg4_512(b0,b2,e0,e2);} + else {__m512i a2=load16(a+2),em=load16(e-1); + r0=avg2_512(a0,c0);g0=b0;z0=avg2_512(bm,bp); + r1=avg4_512(a0,a2,c0,c2);g1=avg4_512(ap,b0,b2,cp);z1=bp; + r2=c0;g2=avg4_512(b0,cm,cp,e0);z2=avg4_512(bm,bp,em,ep); + r3=avg2_512(c0,c2);g3=cp;z3=avg2_512(bp,ep);} + int swap=fmt==0||fmt==3; + __m512i y00=vg512(swap?z0:r0,g0,swap?r0:z0),y01=vg512(swap?z1:r1,g1,swap?r1:z1); + __m512i y10=vg512(swap?z2:r2,g2,swap?r2:z2),y11=vg512(swap?z3:r3,g3,swap?r3:z3); + int t0[16],t1[16],t2[16],t3[16];_mm512_storeu_si512((__m512i*)t0,y00);_mm512_storeu_si512((__m512i*)t1,y01);_mm512_storeu_si512((__m512i*)t2,y10);_mm512_storeu_si512((__m512i*)t3,y11); + for(int i=0;i<16;i++){d[2*i]=t0[i];d[2*i+1]=t1[i];d[dt+2*i]=t2[i];d[dt+2*i+1]=t3[i];} +} +#endif diff --git a/tools/bayer-gray-prototype/bayer_gray_dispatch.c b/tools/bayer-gray-prototype/bayer_gray_dispatch.c new file mode 100644 index 00000000000..28c5085fe7d --- /dev/null +++ b/tools/bayer-gray-prototype/bayer_gray_dispatch.c @@ -0,0 +1,71 @@ +/* Standalone experiment: runtime dispatch with conservative libswscale fallback. + * Not an FFmpeg patch; compile against the same FFmpeg build as the reference. + */ +#define main patterns_original_main +#include "bayer_gray_fused_patterns.c" +#undef main +#include "bayer_gray_avx512.h" +#include "bayer_gray_neon.h" +typedef enum { REF, X86_AVX2, X86_AVX512, ARM_NEON } Route; +typedef struct {SwsContext *reference;int w,h,dw,dh,fmt,allowed;Route route;} Dispatch; +static const char *route_name(Route r){switch(r){case X86_AVX2:return "avx2";case X86_AVX512:return "avx512";case ARM_NEON:return "neon";default:return "reference";}} +static int format_id(enum AVPixelFormat f){switch(f){case AV_PIX_FMT_BAYER_RGGB8:return 0;case AV_PIX_FMT_BAYER_BGGR8:return 1;case AV_PIX_FMT_BAYER_GBRG8:return 2;case AV_PIX_FMT_BAYER_GRBG8:return 3;default:return -1;}} +static Route select_cpu(int w,int h){int f=av_get_cpu_flags(); if(h<6)return REF; +#if HAS_AVX2_IMPL && defined(DISPATCH_TEST_HOOKS) + if(getenv("DISPATCH_TEST_HOST_AVX2") && __builtin_cpu_supports("avx2"))f|=AV_CPU_FLAG_AVX2; +#endif +#if HAS_AVX2_IMPL +#if defined(DISPATCH_ENABLE_UNVERIFIED_AVX512) + if(w>=36&&(f&AV_CPU_FLAG_AVX512))return X86_AVX512; +#endif + if(w>=20&&(f&AV_CPU_FLAG_AVX2))return X86_AVX2; +#elif HAS_NEON_IMPL + if(w>=12&&(f&AV_CPU_FLAG_NEON))return ARM_NEON; +#else + (void)f; +#endif + return REF;} +static int dispatch_init(Dispatch*p,int w,int h,int dw,int dh,enum AVPixelFormat sf,enum AVPixelFormat df,int flags,int nondefault){ + memset(p,0,sizeof(*p));p->w=w;p->h=h;p->dw=dw;p->dh=dh;p->fmt=format_id(sf); + p->reference=sws_getContext(w,h,sf,dw,dh,df,flags,NULL,NULL,NULL);if(!p->reference)return -1; + p->allowed=p->fmt>=0&&df==AV_PIX_FMT_GRAY8&&w==dw&&h==dh&&w>=4&&h>=4&&!(w&1)&&!(h&1)&&flags==SWS_BILINEAR&&!nondefault; + p->route=p->allowed?select_cpu(w,h):REF;return 0; +} +/* Custom colorspace settings must go through this wrapper, invalidating fast path. */ +static int dispatch_set_colorspace(Dispatch*p,const int inv[4],int src_range,const int out[4],int dst_range,int bright,int contrast,int saturation){p->allowed=0;p->route=REF;return sws_setColorspaceDetails(p->reference,inv,src_range,out,dst_range,bright,contrast,saturation);} +static int process(Dispatch*p,const uint8_t *src,ptrdiff_t ss,uint8_t *dst,ptrdiff_t ds){int count=0; + for(int y=0;y<p->h;y+=2){int edge=y==0||y==p->h-2;for(int x=0;x<p->w;){ +#if HAS_AVX2_IMPL + if(p->route==X86_AVX512&&!edge&&x>=2&&x+34<=p->w){blocks16(src+(ptrdiff_t)y*ss+x,ss,dst+(ptrdiff_t)y*ds+x,ds,p->fmt);x+=32;count++;continue;} + if(p->route==X86_AVX2&&!edge&&x>=2&&x+18<=p->w){blocks8(src+(ptrdiff_t)y*ss+x,ss,dst+(ptrdiff_t)y*ds+x,ds,p->fmt);x+=16;count++;continue;} +#endif +#if HAS_NEON_IMPL + if(p->route==ARM_NEON&&!edge&&x>=2&&x+10<=p->w){blocks4_neon(src+(ptrdiff_t)y*ss+x,ss,dst+(ptrdiff_t)y*ds+x,ds,p->fmt);x+=8;count++;continue;} +#endif + block(src+(ptrdiff_t)y*ss+x,ss,dst+(ptrdiff_t)y*ds+x,ds,edge||x==0||x==p->w-2,p->fmt);x+=2;}} + return count; +} +static int dispatch_scale(Dispatch*p,const uint8_t*const src[4],const int ss[4],int y,int h,uint8_t*const dst[4],const int ds[4],int*vector_count){ + /* A non-full-frame submission permanently disables the optimization for this context. */ + if(y!=0||h!=p->h){p->allowed=0;p->route=REF;} + if(!p->allowed||p->route==REF){if(vector_count)*vector_count=0;return sws_scale(p->reference,src,ss,y,h,dst,ds);} + int n=process(p,src[0],ss[0],dst[0],ds[0]);if(vector_count)*vector_count=n;return p->h; +} +static void dispatch_free(Dispatch*p){sws_freeContext(p->reference);p->reference=NULL;} +#ifndef DISPATCH_NO_MAIN +int main(int argc,char**argv){ + if(argc!=8){fprintf(stderr,"usage: fmt width height iterations seed auto|reference normal|flip\n");return 2;} + int fmt=atoi(argv[1]),w=atoi(argv[2]),h=atoi(argv[3]),n=atoi(argv[4]),seed=atoi(argv[5]),flip=!strcmp(argv[7],"flip");if(fmt<0||fmt>3||w<4||h<4||(w&1)||(h&1)||n<1)return 2; + enum AVPixelFormat fmts[]={AV_PIX_FMT_BAYER_RGGB8,AV_PIX_FMT_BAYER_BGGR8,AV_PIX_FMT_BAYER_GBRG8,AV_PIX_FMT_BAYER_GRBG8}; + uint8_t*s[4]={0},*a[4]={0},*b[4]={0};int ss[4],as[4],bs[4];if(av_image_alloc(s,ss,w,h,fmts[fmt],64)<0||av_image_alloc(a,as,w,h,AV_PIX_FMT_GRAY8,64)<0||av_image_alloc(b,bs,w,h,AV_PIX_FMT_GRAY8,64)<0)return 3; + if(getenv("DISPATCH_FORCE_NO_SIMD"))av_force_cpu_flags(0); + SwsContext*c=sws_getContext(w,h,fmts[fmt],w,h,AV_PIX_FMT_GRAY8,SWS_BILINEAR,0,0,0);Dispatch p;if(!c||dispatch_init(&p,w,h,w,h,fmts[fmt],AV_PIX_FMT_GRAY8,SWS_BILINEAR,0))return 4; + if(!strcmp(argv[6],"reference"))p.route=REF;else if(strcmp(argv[6],"auto"))return 2; + int stride=flip?-ss[0]:ss[0];const uint8_t*in[4]={s[0]+(flip?(ptrdiff_t)(h-1)*ss[0]:0),0,0,0};int is[4]={stride,0,0,0};uint32_t state=seed+1;int vectors=0; + for(int frame=0;frame<5;frame++){for(int y=0;y<h;y++)for(int x=0;x<w;x++){state=state*1664525u+1013904223u;s[0][(ptrdiff_t)y*ss[0]+x]=state>>24;} + memset(b[0],0xa5,(size_t)bs[0]*h);if(sws_scale(c,in,is,0,h,a,as)!=h||dispatch_scale(&p,in,is,0,h,b,bs,&vectors)!=h)return 5; + for(int y=0;y<h;y++){for(int x=0;x<w;x++)if(a[0][(ptrdiff_t)y*as[0]+x]!=b[0][(ptrdiff_t)y*bs[0]+x]){fprintf(stderr,"MISMATCH fmt=%d w=%d h=%d frame=%d x=%d y=%d route=%s expected=%u got=%u\n",fmt,w,h,frame,x,y,route_name(p.route),a[0][(ptrdiff_t)y*as[0]+x],b[0][(ptrdiff_t)y*bs[0]+x]);return 6;}for(int x=w;x<bs[0]&&p.route!=REF;x++)if(b[0][(ptrdiff_t)y*bs[0]+x]!=0xa5)return 7;}} + double base=0,fast=0;for(int pass=0;pass<2;pass++){double t=now();for(int i=0;i<n;i++)if(pass)dispatch_scale(&p,in,is,0,h,b,bs,NULL);else sws_scale(c,in,is,0,h,a,as);t=now()-t;if(pass)fast=t;else base=t;} + printf("PASS fmt=%d size=%dx%d route=%s vectors=%d frames=5 baseline_ms=%.6f dispatch_ms=%.6f\n",fmt,w,h,route_name(p.route),vectors,1000*base/n,1000*fast/n);dispatch_free(&p);sws_freeContext(c);av_free(s[0]);av_free(a[0]);av_free(b[0]);return 0; +} +#endif diff --git a/tools/bayer-gray-prototype/bayer_gray_fused_patterns.c b/tools/bayer-gray-prototype/bayer_gray_fused_patterns.c new file mode 100644 index 00000000000..783bfe071ac --- /dev/null +++ b/tools/bayer-gray-prototype/bayer_gray_fused_patterns.c @@ -0,0 +1,110 @@ +/* Research prototype: default-range, unscaled 8-bit Bayer -> GRAY8, even dimensions. + * Independent from FFmpeg dispatch; compare against a real sws_scale context. + * Bayer interpolation follows libswscale/bayer_template.c (LGPL 2.1+). + */ +#define _POSIX_C_SOURCE 200809L +#include <stdint.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#include "libavutil/cpu.h" +#include "libavutil/imgutils.h" +#include "libavutil/mem.h" +#include "libswscale/swscale.h" +#if (defined(__x86_64__) || defined(__i386__)) && (defined(__GNUC__) || defined(__clang__)) +#include <immintrin.h> +#define HAS_AVX2_IMPL 1 +#else +#define HAS_AVX2_IMPL 0 +#endif +static inline uint8_t gray(int r,int g,int b) { + int y=(8414*r+16519*g+3208*b+524544)>>9; + y*=2; if(y>32767)y=32767; + int scaled=y*19078-39084288; + y=scaled>=0?scaled>>14:-(((-scaled)+16383)>>14); + y=(y+64)>>7;return y<0?0:y>255?255:y; +} +/* Channel ordering follows the corresponding FFmpeg Bayer template. */ +static void block(const uint8_t*s,ptrdiff_t st,uint8_t*d,ptrdiff_t dt,int copy,int fmt) { + const uint8_t *a=copy?s:s-st,*b=s,*c=s+st,*e=copy?s:s+2*st; + int r[4],g[4],z[4]; + if(fmt<2) { + if(copy){for(int i=0;i<4;i++)r[i]=c[1],z[i]=b[0];g[0]=g[3]=(b[1]+c[0])>>1;g[1]=b[1];g[2]=c[0];} + else {r[0]=(a[-1]+a[1]+c[-1]+c[1])>>2;g[0]=(a[0]+b[-1]+b[1]+c[0])>>2;z[0]=b[0]; + r[1]=(a[1]+c[1])>>1;g[1]=b[1];z[1]=(b[0]+b[2])>>1; + r[2]=(c[-1]+c[1])>>1;g[2]=c[0];z[2]=(b[0]+e[0])>>1; + r[3]=c[1];g[3]=(b[1]+c[0]+c[2]+e[1])>>2;z[3]=(b[0]+b[2]+e[0]+e[2])>>2;} + } else { + if(copy){for(int i=0;i<4;i++)r[i]=c[0],z[i]=b[1];g[0]=b[0];g[3]=c[1];g[1]=g[2]=(b[0]+c[1])>>1;} + else {r[0]=(a[0]+c[0])>>1;g[0]=b[0];z[0]=(b[-1]+b[1])>>1; + r[1]=(a[0]+a[2]+c[0]+c[2])>>2;g[1]=(a[1]+b[0]+b[2]+c[1])>>2;z[1]=b[1]; + r[2]=c[0];g[2]=(b[0]+c[-1]+c[1]+e[0])>>2;z[2]=(b[-1]+b[1]+e[-1]+e[1])>>2; + r[3]=(c[0]+c[2])>>1;g[3]=c[1];z[3]=(b[1]+e[1])>>1;} + } + for(int i=0;i<4;i++){int swap=fmt==0||fmt==3;d[(i/2)*dt+i%2]=swap?gray(z[i],g[i],r[i]):gray(r[i],g[i],z[i]);} +} +#if HAS_AVX2_IMPL +#define AVX __attribute__((target("avx2"))) +static AVX inline __m256i load8(const uint8_t *p){__m128i x=_mm_loadu_si128((const __m128i*)p);return _mm256_cvtepu16_epi32(_mm_and_si128(x,_mm_set1_epi16(255)));} +static AVX inline __m256i add2(__m256i a,__m256i b){return _mm256_add_epi32(a,b);} +static AVX inline __m256i avg2(__m256i a,__m256i b){return _mm256_srli_epi32(add2(a,b),1);} +static AVX inline __m256i avg4(__m256i a,__m256i b,__m256i c,__m256i d){return _mm256_srli_epi32(add2(add2(a,b),add2(c,d)),2);} +static AVX inline __m256i vg(__m256i r,__m256i g,__m256i b){ + __m256i v=add2(add2(_mm256_mullo_epi32(r,_mm256_set1_epi32(8414)),_mm256_mullo_epi32(g,_mm256_set1_epi32(16519))),_mm256_mullo_epi32(b,_mm256_set1_epi32(3208))); + v=_mm256_srli_epi32(add2(v,_mm256_set1_epi32(524544)),9);v=_mm256_min_epi32(_mm256_slli_epi32(v,1),_mm256_set1_epi32(32767)); + v=_mm256_srai_epi32(add2(_mm256_mullo_epi32(v,_mm256_set1_epi32(19078)),_mm256_set1_epi32(-39084288)),14); + v=_mm256_srai_epi32(add2(v,_mm256_set1_epi32(64)),7);return _mm256_max_epi32(_mm256_setzero_si256(),_mm256_min_epi32(v,_mm256_set1_epi32(255))); +} +static AVX void blocks8(const uint8_t*s,ptrdiff_t st,uint8_t*d,ptrdiff_t dt,int fmt){ + const uint8_t *a=s-st,*b=s,*c=s+st,*e=s+2*st; + __m256i am=load8(a-1),a0=load8(a),ap=load8(a+1),bm=load8(b-1),b0=load8(b),bp=load8(b+1),b2=load8(b+2),cm=load8(c-1),c0=load8(c),cp=load8(c+1),c2=load8(c+2),e0=load8(e),ep=load8(e+1),e2=load8(e+2); + __m256i r0,r1,r2,r3,g0,g1,g2,g3,z0,z1,z2,z3; + if(fmt<2){r0=avg4(am,ap,cm,cp);g0=avg4(a0,bm,bp,c0);z0=b0; + r1=avg2(ap,cp);g1=bp;z1=avg2(b0,b2);r2=avg2(cm,cp);g2=c0;z2=avg2(b0,e0); + r3=cp;g3=avg4(bp,c0,c2,ep);z3=avg4(b0,b2,e0,e2);} + else {__m256i a2=load8(a+2),em=load8(e-1); + r0=avg2(a0,c0);g0=b0;z0=avg2(bm,bp); + r1=avg4(a0,a2,c0,c2);g1=avg4(ap,b0,b2,cp);z1=bp; + r2=c0;g2=avg4(b0,cm,cp,e0);z2=avg4(bm,bp,em,ep); + r3=avg2(c0,c2);g3=cp;z3=avg2(bp,ep);} + int swap=fmt==0||fmt==3; + __m256i y00=vg(swap?z0:r0,g0,swap?r0:z0),y01=vg(swap?z1:r1,g1,swap?r1:z1); + __m256i y10=vg(swap?z2:r2,g2,swap?r2:z2),y11=vg(swap?z3:r3,g3,swap?r3:z3); + int t0[8],t1[8],t2[8],t3[8];_mm256_storeu_si256((__m256i*)t0,y00);_mm256_storeu_si256((__m256i*)t1,y01);_mm256_storeu_si256((__m256i*)t2,y10);_mm256_storeu_si256((__m256i*)t3,y11); + for(int i=0;i<8;i++){d[2*i]=t0[i];d[2*i+1]=t1[i];d[dt+2*i]=t2[i];d[dt+2*i+1]=t3[i];} +} +#endif +static int fused_fmt(const uint8_t*s,ptrdiff_t ss,uint8_t*d,ptrdiff_t ds,int w,int h,int simd,int fmt){ + int vectors=0;for(int y=0;y<h;y+=2){int border=y==0||y==h-2;int x=0;for(;x<w;){ +#if HAS_AVX2_IMPL + if(simd&&!border&&x>=2&&x+18<=w){blocks8(s+(ptrdiff_t)y*ss+x,ss,d+(ptrdiff_t)y*ds+x,ds,fmt);x+=16;vectors++;continue;} +#endif + block(s+(ptrdiff_t)y*ss+x,ss,d+(ptrdiff_t)y*ds+x,ds,border||x==0||x==w-2,fmt);x+=2;}} + return vectors; +} +static int fused(const uint8_t*s,ptrdiff_t ss,uint8_t*d,ptrdiff_t ds,int w,int h,int simd){return fused_fmt(s,ss,d,ds,w,h,simd,0);} +static double now(void){struct timespec t;clock_gettime(CLOCK_MONOTONIC,&t);return t.tv_sec+t.tv_nsec*1e-9;} +int main(int argc,char**argv){ + if(argc!=8){fprintf(stderr,"usage: width height iterations seed scalar|simd normal|flip flags\n");return 2;} + int w=atoi(argv[1]),h=atoi(argv[2]),n=atoi(argv[3]),seed=atoi(argv[4]),simd=!strcmp(argv[5],"simd"),flip=!strcmp(argv[6],"flip"),flags=strtol(argv[7],0,0); + if(w<4||h<4||(w&1)||(h&1)||w>32768||h>32768||n<1||(!simd&&strcmp(argv[5],"scalar")))return 2; + int supported=!!(av_get_cpu_flags()&AV_CPU_FLAG_AVX2); +#if HAS_AVX2_IMPL + if(getenv("FUSED_TEST_HOST_AVX2"))supported=!!__builtin_cpu_supports("avx2"); +#endif + if(simd&&(!HAS_AVX2_IMPL||!supported)){fprintf(stderr,"AVX2 unavailable\n");return 77;} + const char *fmt_env=getenv("FUSED_BAYER_FORMAT"); int fmt=fmt_env?atoi(fmt_env):0; + const enum AVPixelFormat formats[4]={AV_PIX_FMT_BAYER_RGGB8,AV_PIX_FMT_BAYER_BGGR8,AV_PIX_FMT_BAYER_GBRG8,AV_PIX_FMT_BAYER_GRBG8};if(fmt<0||fmt>3)return 2; + uint8_t *s[4]={0},*a[4]={0},*b[4]={0};int ss[4],as[4],bs[4];if(av_image_alloc(s,ss,w,h,formats[fmt],64)<0||av_image_alloc(a,as,w,h,AV_PIX_FMT_GRAY8,64)<0||av_image_alloc(b,bs,w,h,AV_PIX_FMT_GRAY8,64)<0)return 3; + if(getenv("FUSED_BASE_CPU_SCALAR"))av_force_cpu_flags(0); + SwsContext*c=sws_getContext(w,h,formats[fmt],w,h,AV_PIX_FMT_GRAY8,SWS_BILINEAR|flags,0,0,0);if(!c)return 4; + uint32_t state=(uint32_t)seed+1;const uint8_t *sp=s[0]+(flip?(ptrdiff_t)(h-1)*ss[0]:0);int stride=flip?-ss[0]:ss[0],vec=0; + int pattern=getenv("FUSED_PATTERN")?atoi(getenv("FUSED_PATTERN")):0; + for(int frame=0;frame<5;frame++){for(int y=0;y<h;y++)for(int x=0;x<w;x++){state=state*1664525u+1013904223u;s[0][(ptrdiff_t)y*ss[0]+x]=pattern==1?0:pattern==2?255:pattern==3?((x+y)&1?255:0):pattern==4?(x&1?255:0):pattern==5?(y&1?255:0):pattern==6?(x+y+frame)&255:state>>24;} + memset(a[0],0xa5,(size_t)as[0]*h);memset(b[0],0xa5,(size_t)bs[0]*h);const uint8_t*in[4]={sp,0,0,0};int is[4]={stride,0,0,0};if(sws_scale(c,in,is,0,h,a,as)!=h)return 5; + vec=fused_fmt(sp,stride,b[0],bs[0],w,h,simd,fmt);for(int y=0;y<h;y++)for(int x=0;x<w;x++)if(a[0][(ptrdiff_t)y*as[0]+x]!=b[0][(ptrdiff_t)y*bs[0]+x]){fprintf(stderr,"mismatch frame=%d x=%d y=%d baseline=%u fused=%u vectors=%d\n",frame,x,y,a[0][(ptrdiff_t)y*as[0]+x],b[0][(ptrdiff_t)y*bs[0]+x],vec);return 6;} + for(int y=0;y<h;y++)for(int x=w;x<bs[0];x++)if(b[0][(ptrdiff_t)y*bs[0]+x]!=0xa5){fprintf(stderr,"padding modified\n");return 7;}} + double base=0,fast=0;const uint8_t*in[4]={sp,0,0,0};int is[4]={stride,0,0,0};for(int pass=0;pass<2;pass++){int which=(pass+(seed&1))&1;double t=now();for(int i=0;i<n;i++){if(which)fused_fmt(sp,stride,b[0],bs[0],w,h,simd,fmt);else if(sws_scale(c,in,is,0,h,a,as)!=h)return 8;}t=now()-t;if(which)fast=t;else base=t;} + printf("%dx%d %s flip=%d flags=%d frames=5 match=yes vectors=%d baseline_ms=%.6f fused_ms=%.6f delta_pct=%.2f\n",w,h,argv[5],flip,flags,vec,1000*base/n,1000*fast/n,100*(fast/base-1));sws_freeContext(c);av_free(s[0]);av_free(a[0]);av_free(b[0]);return 0; +} diff --git a/tools/bayer-gray-prototype/bayer_gray_neon.h b/tools/bayer-gray-prototype/bayer_gray_neon.h new file mode 100644 index 00000000000..88437c6303b --- /dev/null +++ b/tools/bayer-gray-prototype/bayer_gray_neon.h @@ -0,0 +1,24 @@ +#if defined(__aarch64__) +#include <arm_neon.h> +#define HAS_NEON_IMPL 1 +static inline uint32x4_t nload(const uint8_t *p){uint8x8_t x=vld1_u8(p);uint8x8_t even=vuzp1_u8(x,x);return vmovl_u16(vget_low_u16(vmovl_u8(even)));} +static inline uint32x4_t n2(uint32x4_t a,uint32x4_t b){return vshrq_n_u32(vaddq_u32(a,b),1);} +static inline uint32x4_t n4(uint32x4_t a,uint32x4_t b,uint32x4_t c,uint32x4_t d){return vshrq_n_u32(vaddq_u32(vaddq_u32(a,b),vaddq_u32(c,d)),2);} +static inline int32x4_t ng(uint32x4_t r,uint32x4_t g,uint32x4_t b){ + uint32x4_t v=vmlaq_n_u32(vmlaq_n_u32(vmulq_n_u32(r,8414),g,16519),b,3208); + v=vshrq_n_u32(vaddq_u32(v,vdupq_n_u32(524544)),9);v=vminq_u32(vshlq_n_u32(v,1),vdupq_n_u32(32767)); + int32x4_t z=vreinterpretq_s32_u32(v);z=vshrq_n_s32(vsubq_s32(vmulq_n_s32(z,19078),vdupq_n_s32(39084288)),14); + z=vshrq_n_s32(vaddq_s32(z,vdupq_n_s32(64)),7);return vmaxq_s32(vdupq_n_s32(0),vminq_s32(z,vdupq_n_s32(255))); +} +static void blocks4_neon(const uint8_t*s,ptrdiff_t st,uint8_t*d,ptrdiff_t dt,int fmt){ + const uint8_t *a=s-st,*b=s,*c=s+st,*e=s+2*st; + uint32x4_t am=nload(a-1),a0=nload(a),ap=nload(a+1),bm=nload(b-1),b0=nload(b),bp=nload(b+1),b2=nload(b+2),cm=nload(c-1),c0=nload(c),cp=nload(c+1),c2=nload(c+2),e0=nload(e),ep=nload(e+1),e2=nload(e+2); + uint32x4_t r0,r1,r2,r3,g0,g1,g2,g3,z0,z1,z2,z3; + if(fmt<2){r0=n4(am,ap,cm,cp);g0=n4(a0,bm,bp,c0);z0=b0;r1=n2(ap,cp);g1=bp;z1=n2(b0,b2);r2=n2(cm,cp);g2=c0;z2=n2(b0,e0);r3=cp;g3=n4(bp,c0,c2,ep);z3=n4(b0,b2,e0,e2);} + else {uint32x4_t a2=nload(a+2),em=nload(e-1);r0=n2(a0,c0);g0=b0;z0=n2(bm,bp);r1=n4(a0,a2,c0,c2);g1=n4(ap,b0,b2,cp);z1=bp;r2=c0;g2=n4(b0,cm,cp,e0);z2=n4(bm,bp,em,ep);r3=n2(c0,c2);g3=cp;z3=n2(bp,ep);} + int swap=fmt==0||fmt==3;int32x4_t y0=ng(swap?z0:r0,g0,swap?r0:z0),y1=ng(swap?z1:r1,g1,swap?r1:z1),y2=ng(swap?z2:r2,g2,swap?r2:z2),y3=ng(swap?z3:r3,g3,swap?r3:z3); + int t0[4],t1[4],t2[4],t3[4];vst1q_s32(t0,y0);vst1q_s32(t1,y1);vst1q_s32(t2,y2);vst1q_s32(t3,y3);for(int i=0;i<4;i++){d[2*i]=t0[i];d[2*i+1]=t1[i];d[dt+2*i]=t2[i];d[dt+2*i+1]=t3[i];} +} +#else +#define HAS_NEON_IMPL 0 +#endif -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
