Skip to content

Add AVIF support through libavif.#23596

Merged
asmorkalov merged 12 commits intoopencv:4.xfrom
vrabaud:libavif
Jun 9, 2023
Merged

Add AVIF support through libavif.#23596
asmorkalov merged 12 commits intoopencv:4.xfrom
vrabaud:libavif

Conversation

@vrabaud
Copy link
Copy Markdown
Contributor

@vrabaud vrabaud commented May 9, 2023

This is to fix #19271
Extra: opencv/opencv_extra#1069

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

@vrabaud
Copy link
Copy Markdown
Contributor Author

vrabaud commented May 11, 2023

I fixed the EXIF that was not handled and I set the output buffers to 16 bit when reading 10/12 bit. We lose the bit depth information though.

@asmorkalov
Copy link
Copy Markdown
Contributor

asmorkalov commented May 22, 2023

Build with default Ubuntu 22.04 libavif package fails with compilation error:

[ 90%] Building CXX object modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/grfmt_avif.cpp.o
/home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp: In constructor ‘cv::AvifDecoder::AvifDecoder()’:
/home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:139:27: error: ‘avifDecoder’ {aka ‘struct avifDecoder’} has no member named ‘allowIncremental’
  139 |   if (decoder_) decoder_->allowIncremental = AVIF_TRUE;
      |                           ^~~~~~~~~~~~~~~~
make[3]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/build.make:104: modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/grfmt_avif.cpp.o] Error 1

Version info:

arina@arina-pc:~$ apt show libavif-dev
Package: libavif-dev
Version: 0.9.3-3
Priority: optional
Section: universe/libdevel
Source: libavif
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Multimedia Maintainers <debian-multimedia@lists.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 87,0 kB
Depends: libavif13 (= 0.9.3-3)
Homepage: https://github.com/AOMediaCodec/libavif
Download-Size: 21,0 kB
APT-Manual-Installed: yes
APT-Sources: http://ru.archive.ubuntu.com/ubuntu jammy/universe amd64 Packages
Description: Library for handling .avif files (development files)
 This library aims to be a friendly, portable C implementation of the AV1
 Image File Format as described in https://aomediacodec.github.io/av1-avif/.
 .
 This package provides the development files.

@asmorkalov asmorkalov added this to the 4.9.0 milestone May 30, 2023
@vrabaud
Copy link
Copy Markdown
Contributor Author

vrabaud commented Jun 1, 2023

I split the tests and fixed the compilation on Ubuntu (that API is also useless for us). I added a test for EXIF (that only works with libavif HEAD).

@asmorkalov
Copy link
Copy Markdown
Contributor

asmorkalov commented Jun 2, 2023

@vrabaud Thanks a lot for the update. Looks like you forgot about opencv_extra patch:

[ RUN      ] Imgcodecs_Hdr.regression
imread_('/home/arina/Projects/opencv_extra/testdata/highgui//readwrite/rle.hdr'): can't read header: OpenCV(4.7.0-dev) /home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:183: error: (-212:Parsing error)  BMFF parsing failed in function 'readHeader'

/home/arina/Projects/opencv/modules/imgcodecs/test/test_grfmt.cpp:373: Failure
Value of: img_rle.empty()
  Actual: true
Expected: false
Could not open /home/arina/Projects/opencv_extra/testdata/highgui//readwrite/rle.hdr
[  FAILED  ] Imgcodecs_Hdr.regression (0 ms)
[ RUN      ] Imgcodecs_Hdr.regression_imencode
imread_('/home/arina/Projects/opencv_extra/testdata/highgui//readwrite/rle.hdr'): can't read header: OpenCV(4.7.0-dev) /home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:183: error: (-212:Parsing error)  BMFF parsing failed in function 'readHeader'

/home/arina/Projects/opencv/modules/imgcodecs/test/test_grfmt.cpp:396: Failure
Value of: img_ref.empty()
  Actual: true
Expected: false
Could not open /home/arina/Projects/opencv_extra/testdata/highgui//readwrite/rle.hdr
[  FAILED  ] Imgcodecs_Hdr.regression_imencode (0 ms)
....
[ RUN      ] All/Imgcodecs_FileMode.regression/55, where GetParam() = ("readwrite/rle.hdr", -1)
imread_('/home/arina/Projects/opencv_extra/testdata/highgui/readwrite/rle.hdr'): can't read header: OpenCV(4.7.0-dev) /home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:183: error: (-212:Parsing error)  BMFF parsing failed in function 'readHeader'

/home/arina/Projects/opencv/modules/imgcodecs/test/test_grfmt.cpp:57: Failure
Value of: single.empty()
  Actual: true
Expected: false
[  FAILED  ] All/Imgcodecs_FileMode.regression/55, where GetParam() = ("readwrite/rle.hdr", -1) (0 ms)
[ RUN      ] All/Imgcodecs_FileMode.regression/56, where GetParam() = ("readwrite/rle.hdr", 0)
imread_('/home/arina/Projects/opencv_extra/testdata/highgui/readwrite/rle.hdr'): can't read header: OpenCV(4.7.0-dev) /home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:183: error: (-212:Parsing error)  BMFF parsing failed in function 'readHeader'

/home/arina/Projects/opencv/modules/imgcodecs/test/test_grfmt.cpp:57: Failure
Value of: single.empty()
  Actual: true
Expected: false
[  FAILED  ] All/Imgcodecs_FileMode.regression/56, where GetParam() = ("readwrite/rle.hdr", 0) (0 ms)
[ RUN      ] All/Imgcodecs_FileMode.regression/57, where GetParam() = ("readwrite/rle.hdr", 1)
imread_('/home/arina/Projects/opencv_extra/testdata/highgui/readwrite/rle.hdr'): can't read header: OpenCV(4.7.0-dev) /home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:183: error: (-212:Parsing error)  BMFF parsing failed in function 'readHeader'

/home/arina/Projects/opencv/modules/imgcodecs/test/test_grfmt.cpp:57: Failure
Value of: single.empty()
  Actual: true
Expected: false
[  FAILED  ] All/Imgcodecs_FileMode.regression/57, where GetParam() = ("readwrite/rle.hdr", 1) (0 ms)
[ RUN      ] All/Imgcodecs_FileMode.regression/58, where GetParam() = ("readwrite/rle.hdr", 2)
imread_('/home/arina/Projects/opencv_extra/testdata/highgui/readwrite/rle.hdr'): can't read header: OpenCV(4.7.0-dev) /home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:183: error: (-212:Parsing error)  BMFF parsing failed in function 'readHeader'

/home/arina/Projects/opencv/modules/imgcodecs/test/test_grfmt.cpp:57: Failure
Value of: single.empty()
  Actual: true
Expected: false
[  FAILED  ] All/Imgcodecs_FileMode.regression/58, where GetParam() = ("readwrite/rle.hdr", 2) (1 ms)
[ RUN      ] All/Imgcodecs_FileMode.regression/59, where GetParam() = ("readwrite/rle.hdr", 4)
imread_('/home/arina/Projects/opencv_extra/testdata/highgui/readwrite/rle.hdr'): can't read header: OpenCV(4.7.0-dev) /home/arina/Projects/opencv/modules/imgcodecs/src/grfmt_avif.cpp:183: error: (-212:Parsing error)  BMFF parsing failed in function 'readHeader'

/home/arina/Projects/opencv/modules/imgcodecs/test/test_grfmt.cpp:57: Failure
Value of: single.empty()
  Actual: true
Expected: false
[  FAILED  ] All/Imgcodecs_FileMode.regression/59, where GetParam() = ("readwrite/rle.hdr", 4) (0 ms)
[----------] 60 tests from All/Imgcodecs_FileMode (36845 ms total)

@asmorkalov asmorkalov self-assigned this Jun 2, 2023
@vrabaud vrabaud force-pushed the libavif branch 2 times, most recently from 2423ae9 to bd40e00 Compare June 2, 2023 13:47
@vrabaud
Copy link
Copy Markdown
Contributor Author

vrabaud commented Jun 2, 2023

All imgcodecs tests now pass.

@asmorkalov
Copy link
Copy Markdown
Contributor

All tests passed on my side with Ubuntu 22.04. Thanks a lot! I'll try to look into code in details for the next several days and provide feedback. I also added Ubuntu 22.04 config with libavif on board to CI in opencv/ci-gha-workflow#101 and opencv-infrastructure/opencv-gha-dockerfile#18

@vrabaud
Copy link
Copy Markdown
Contributor Author

vrabaud commented Jun 8, 2023

(the CI error seems unrelated: DNN with CUDA error)

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@asmorkalov asmorkalov modified the milestones: 4.9.0, 4.8.0 Jun 8, 2023
@asmorkalov
Copy link
Copy Markdown
Contributor

@vrabaud We discussed the solution on core team meeting and decided to disable libavif by default for now and treat it's support as experimental till 4.9.0. Could you disable the option by default? I'll add CI configuration a bit later after the release.

@vrabaud
Copy link
Copy Markdown
Contributor Author

vrabaud commented Jun 9, 2023

@asmorkalov: sure, done. WITH_AVIF is set to OFF by default.

@asmorkalov asmorkalov merged commit 472aad4 into opencv:4.x Jun 9, 2023
@asmorkalov asmorkalov mentioned this pull request Jul 12, 2023
@umbertov
Copy link
Copy Markdown

@asmorkalov @vrabaud Hey, I was reading cmake files and noticed that AVIF support relies on headers & libraries either being present on the system, or built separately and pointed at by the user, while many other 3rd party components like jpeg, png, webp and more have an option (like BUILD_JPEG for libjpeg, for instance) to be built together with opencv and being statically linked into the libopencv_*.so libraries. I assume that they're being statically linked since ldd shows no references to e.g. libjpeg.

Is a BUILD_AVIF option not implemented just because of time & resources reasons, or because of weird licensing issues? Because if it is just due to the former, i might try to work on implementing it.
Thanks in advance for any answer!

@asmorkalov
Copy link
Copy Markdown
Contributor

Major issue is mostly engineering. AVIF has a lot of own dependencies that we do not want to bring into OpenCV. Licenses for AVIF dependence sub-tree is open issue too.

@vrabaud
Copy link
Copy Markdown
Contributor Author

vrabaud commented Oct 26, 2023

Licenses are fine: libavif is BSD 2 clause.
Now, it is a thin wrapper around any encoding and/or decoding library.
If we pull in libaom, it is 400 files, dav1d (only for decoding) 120. That is a lot to add to the third_party directory: that was the only rational not to include it.

thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
Add AVIF support through libavif. opencv#23596

This is to fix opencv#19271
Extra: opencv/opencv_extra#1069

### Pull Request Readiness Checklist

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
Add AVIF support through libavif. opencv#23596

This is to fix opencv#19271
Extra: opencv/opencv_extra#1069

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 30, 2024
Add AVIF support through libavif. opencv#23596

This is to fix opencv#19271
Extra: opencv/opencv_extra#1069

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
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.

Support AVIF for image reading and writing

3 participants