Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@FantasqueX
Copy link
Contributor

Zlib-ng is zlib replacement with optimizations for "next generation" systems. Its optimization may benifits image library decode and encode speed such as libpng. In our tests, if using zlib-ng and libpng combination on a x86_64 machine with AVX2, the time of imdecode amd imencode will drop 20% approximately. This patch enables zlib-ng's optimization if CV_DISABLE_OPTIMIZATION is OFF. Since Zlib-ng can dispatch intrinsics on the fly, port work is much easier.

Related discussion: #22573

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

FantasqueX added a commit to FantasqueX/opencv that referenced this pull request Dec 28, 2023
在只开启 AVX2 intrinsics 时,png 格式 `imdecodemulti` 和 `imencodemulti`
耗时会减少约 20%。没有启用 name mangling,因为 openexr
会编译不过。经验证,在 Windows 和 Linux 下,zlib 的符号不会导出。

上游 PR:opencv#24782
Zlib-ng is zlib replacement with optimizations for "next generation" systems. Its optimization may benifits image library decode and encode speed such as libpng. In our tests, if using zlib-ng and libpng combination on a x86_64 machine with AVX2, the time of `imdecode` amd `imencode` will drop 20% approximately. This patch enables zlib-ng's optimization if `CV_DISABLE_OPTIMIZATION` is OFF. Since Zlib-ng can dispatch intrinsics on the fly, port work is much easier.

Related discussion: opencv#22573
@FantasqueX
Copy link
Contributor Author

Hi, I've updated zlib-ng to 2.1.6. Could you please review this patch? @asmorkalov @opencv-alalek

@FantasqueX
Copy link
Contributor Author

Gently ping~

@asmorkalov
Copy link
Contributor

I apologize for the delay. Zlib is used py OpenCV itself, libpng, QT and other our 3rdparties. I'm looking on different options for OpenCV-Python package to re-use the same implementation and exclude conflicts.

@asmorkalov
Copy link
Contributor

ARM v7 build is broken:

[ 11%] Building C object 3rdparty/zlib-ng/CMakeFiles/zlib.dir/arch/arm/slide_hash_armv6.c.o
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c: In function ‘slide_hash_chain’:
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:13:16: error: unknown type name ‘uint16x2_t’
     Z_REGISTER uint16x2_t v;
                ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:14:5: error: unknown type name ‘uint16x2_t’
     uint16x2_t p0, p1, p2, p3;
     ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:23:24: error: ‘uint16x2_t’ undeclared (first use in this function)
     n = size / (sizeof(uint16x2_t) * 4);
                        ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:23:24: note: each undeclared identifier is reported only once for each function it appears in
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:25:23: error: expected ‘)’ before ‘uint16x2_t’
         p0 = *((const uint16x2_t *)(table));
                       ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:25:9: warning: type defaults to ‘int’ in type name [-Wimplicit-int]
         p0 = *((const uint16x2_t *)(table));
         ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:25:14: error: invalid type argument of unary ‘*’ (have ‘int’)
         p0 = *((const uint16x2_t *)(table));
              ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:26:23: error: expected ‘)’ before ‘uint16x2_t’
         p1 = *((const uint16x2_t *)(table+2));
                       ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:26:9: warning: type defaults to ‘int’ in type name [-Wimplicit-int]
         p1 = *((const uint16x2_t *)(table+2));
         ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:26:14: error: invalid type argument of unary ‘*’ (have ‘int’)
         p1 = *((const uint16x2_t *)(table+2));
              ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:27:23: error: expected ‘)’ before ‘uint16x2_t’
         p2 = *((const uint16x2_t *)(table+4));
                       ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:27:9: warning: type defaults to ‘int’ in type name [-Wimplicit-int]
         p2 = *((const uint16x2_t *)(table+4));
         ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:27:14: error: invalid type argument of unary ‘*’ (have ‘int’)
         p2 = *((const uint16x2_t *)(table+4));
              ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:28:23: error: expected ‘)’ before ‘uint16x2_t’
         p3 = *((const uint16x2_t *)(table+6));
                       ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:28:9: warning: type defaults to ‘int’ in type name [-Wimplicit-int]
         p3 = *((const uint16x2_t *)(table+6));
         ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:28:14: error: invalid type argument of unary ‘*’ (have ‘int’)
         p3 = *((const uint16x2_t *)(table+6));
              ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:29:14: warning: implicit declaration of function ‘__uqsub16’ [-Wimplicit-function-declaration]
         p0 = __uqsub16(p0, v);
              ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:33:24: error: expected expression before ‘)’ token
         *((uint16x2_t *)(table)) = p0;
                        ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:34:24: error: expected expression before ‘)’ token
         *((uint16x2_t *)(table+2)) = p1;
                        ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:35:24: error: expected expression before ‘)’ token
         *((uint16x2_t *)(table+4)) = p2;
                        ^
/home/ubuntu/Projects/opencv/3rdparty/zlib-ng/arch/arm/slide_hash_armv6.c:36:24: error: expected expression before ‘)’ token
         *((uint16x2_t *)(table+6)) = p3;
                        ^
3rdparty/zlib-ng/CMakeFiles/zlib.dir/build.make:507: recipe for target '3rdparty/zlib-ng/CMakeFiles/zlib.dir/arch/arm/slide_hash_armv6.c.o' failed

OS: ubuntu 16.04, GCC 5.4.0, CMake: cmake -DCPU_BASELINE=NEON -DWITH_ZLIB_NG=ON -DBUILD_PNG=ON ../opencv

@asmorkalov
Copy link
Contributor

Performance numbers for libpng on Intel i5-1135G7, Ubuntu 20.04:

Geometric mean (ms)

Name of Test 4.x-1  patched-1 patched-1 
                                  vs    
                                4.x-1   
                              (x-factor)
Decode::JPEG 43.926  44.043      1.00   
Encode::JPEG 38.879  38.880      1.00   
decode::PNG  23.325  12.710      1.84   
encode::PNG  49.257  30.369      1.62 

@asmorkalov
Copy link
Contributor

The same configuration with Spng:

Geometric mean (ms)

Name of Test 4.x-1  patched-1 patched-1 
                                  vs    
                                4.x-1   
                              (x-factor)
Decode::JPEG 43.528  43.789      0.99   
Encode::JPEG 39.319  38.999      1.01   
decode::PNG  19.943  13.462      1.48   
encode::PNG  64.591  46.950      1.38 

@asmorkalov asmorkalov added this to the 4.10.0 milestone Jan 23, 2024
@FantasqueX
Copy link
Contributor Author

Thanks for testing. For Armv7 build failure, opencv's zlib-ng build option is -DZLIB_COMPAT=ON -DZLIB_ENABLE_TESTS=OFF -DWITH_GTEST=OFF . We should test whther zlib-ng 2.1.6 can be built with those options on Armv7. Since I dont't have an Armv7 environment, it'll take some time to investigate the build failure. I'll have a try with qemu-system.

@asmorkalov
Copy link
Contributor

I made experiments with arm and found out that root CmakeLists.txt in zlib was migrated incorrectly. I'll return back to the PR soon.

@FantasqueX
Copy link
Contributor Author

Sorry for late response. I encountered some difficulties using qemu-system. So, I tested my patch on raspberry pi 2b which is the latest armv7 device AFAIK. I cannot reproduce the build failure.

OS: raspberry pi OS based on debian 11, GCC 10.2.1-6+rpi1, CMake: cmake -GNinja -DCPU_BASELINE=NEON -DWITH_ZLIB_NG=ON -DBUILD_PNG=ON ..

@FantasqueX
Copy link
Contributor Author

@asmorkalov Gently ping. It's been a month since I heard from you last time. Is there anything I can do to make this PR be merged sooner?

@asmorkalov
Copy link
Contributor

I stuck a bit with the NEON issue in some configurations. I'll try to fix it and propose a solution this week. The PR is definitely useful and will be merged.

@asmorkalov
Copy link
Contributor

The compilation issue is compiler dependent. I see the same errors with GCC 5.4 in Ubuntu 16.04, GCC 7.5 and 8.4, but not with GCC 10.2 on Raspberry Pi.

@asmorkalov
Copy link
Contributor

Discussed the PR on the OpenCV Core team meeting. Decided to merge the PR as is. WITH_ZLIB_NG option is disabled by default. NEON issue with old compilers will be fixed later with another patch.

@asmorkalov asmorkalov merged commit 6bf758e into opencv:4.x May 3, 2024
@mshabunin mshabunin mentioned this pull request Jun 14, 2024
@FantasqueX FantasqueX deleted the 4.x-zlib-ng branch June 28, 2024 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants