New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any chance of supporting HEIF? #671
Comments
Nokia provide a reference implementation for the HEIF container format but I suspect it's the patent-encumbered HEVC codec it can contain that might be of more interest. In terms of "hotness", the AV1 codec from the Alliance for Open Media, a group of tech companies that don't have HEVC-related patents, will probably become more widespread. (From libvips' point of view I guess this would have to involve exporting an image as a single frame "video".) |
I agree, it's hard to see a patented codec getting much use. There's a free implementation in ffmpeg: https://trac.ffmpeg.org/wiki/Encode/H.265 But there will be a range of potential legal issues using it in various parts of the world. |
Hrm, hadn't thought of the legal issues, was just thinking technically. Looking into that ffmpeg implementation, though... may be useful. |
There's a free loader here which would be easy to adapt: https://github.com/monostream/tifig Though as noted it would need to be an optional feature. |
It looks like https://github.com/monostream/tifig/tree/master/lib uses the same Nokia reference implementation under the hood. The way patents are discussed in its licence leads me to suspect it is incompatible with the LGPL :( Has GPAC been considered? It is LGPL, supports HEIF and https://github.com/gpac/gpac |
My impression was that the Nokia library just handled the container format and that the patented codec stuff was all in libav (ffmpeg). I'm very unsure though. @monostefan, do you have an opinion on this issue? GPAC sounds interesting, I'll have a look. |
You're right @lovell, tifig uses the Nokia library under the hood. We are building this tool because we need something to generate thumbnails from HEIF images in the cloud storage service we're working on and it seems to us that there really isn't any such (open source) tool around yet. At least nothing that runs in a halfway decent amount of time and is able to leverage the 320x240 thumbnails stored in every HEIF image created on a iOS 11 device. As far as I know, not even gpac does that. I'm not really and expert on the HEIF licensing issue and I'm certainly not a lawyer. However, what I can tell you is that the Nokia library doesn't have any HEVC encoder or decoder built in. All it does is extracting encoded streams from or pack them into a HEIF container. You can use any free implementation for that such as ffmpeg/libav or libde265. The remaining question is what the license of Nokias library want's to tell us. As far as I understand it, there are three important points:
|
Thanks @monostefan, it'd be worth you getting a lawyer to check the nokiatech/heif licence if this is going to be used in a commercial product due to the explicit "non-commercial purposes..." clause. It looks like ImageMagick might be going with libde265 plus custom demux logic - see ImageMagick/ImageMagick#507 |
That IM issue is interesting, thanks @lovell! The mux/demux stuff seems rather simple, fortunately. Nokia's non-commercial clause is extremely irritating. libde265 looks great. |
Oh and yeah, as @lovell pointed out, there's that paragraph in @nokiatech's license that restricts its use to non-commercial applications, defined as evaluation, testing and academic research exclusively. Basically this makes the whole thing unusable as others have pointed out. How wonderful would it be if Nokia sues us for using a library they created to to support an image format they invented ;). I doubt that this will ever happen though. Nevertheless, I will consult with our legal team... On the other hand, it shouldn't be too hard to write or use a Base Media File Format parser to get the necessary bytes out of a HEIC file (a HEIF image using HEVC encoding). The next challenge then would be to support the entire standard. HEIF not only supports other encodings like AVC or JPEG, there's also a plethora of image types, like single images, image collections (burst shots), animated image sequences, images derived from auxiliary images, grid and single stream based images and many more. |
As predicted there's a draft AV1-based image format called AVIF being proposed by Netflix - see https://github.com/AOMediaCodec/av1-avif - but it still requires a HEIF container. The relatively new https://github.com/strukturag/libheif has a straightforward API, is LGPL, is available for Debian, relies on the same libde265 as ImageMagick and is plugin-based for possible future AVIF support. |
How is this not a higher priority? HEIC is the default on iOS 11 and on. Meaning you have millions of devices that can't upload to web-servers. Sharp and libvips are awesome but it seems crazy that libvips still doesn't support the new iOS default. |
@jrobber I'm sure a pull request to add a libheif-based loader and saver to libvips would be most welcome. Happy to help review if you're able. |
@lovell I've never done this kind of code before. If you're able to point me in a starting direction and give me some high level steps I might take a crack at it. But at the same time. I assume that if it was that easy someone else would have done it. |
I would start from one of the existing loaders and modify it. For example, the NIfTI loader is here: https://github.com/libvips/libvips/blob/master/libvips/foreign/niftiload.c It's not especially easy, unfortunately :( You need to know about gobject, libvips, libheif and of course HEIC itself. |
@jcupitt Thanks! Unfortunately C is my kryptonite. I'm happy to contribute financially to someone who wants to do this, but in the mean time I'm going to have to put a 2nd image processor in and move forward with my project. |
I had a quick hack and added https://github.com/libvips/libvips/tree/add-heifload It works on simple RGB images, but does not have any options, and does not support any metadata, other than the very basic width / height / etc. I'll try to get it working on the Nokia test images, and add metadata support. |
It now loads at least something from everything in the nokia test set, and supports exif metadata. Does anyone have a test image with XMP or IPTC metadata? It doesn't look like libheif supports gif-style animated images yet, but it should support images with alpha, and image collections. I'm unsure about tiled image support. |
@jcupitt here's an image taken with the Lightroom app on iPhone XS: https://www.dropbox.com/s/oe7hxfuzzaqbj3l/photo%20for%20jcupitt%20on%20GitHub.HEIC?dl=0 This is what Finder shows: exiv2 doesn't report any metadata. |
@rayshan thanks for sharing. See Exiv2/exiv2#318. If you need metadata via node, ExifTool-vendored pulls out 125 tags from your file, fwiw (because ExifTool rocks!) |
Oh nice! Thanks @rayshan. I now see:
|
I'll try to add multi-page read too. An example with XMP metadata would be great, if anyone knows of one. |
The iOS Camera app also doesn't write anything to XMP. ExifTool only has read-only support for HEIF / HEIC currently so I couldn't copy the tags over. I found this link on Google: https://winaero.com/blog/heif-heic-images-windows-10/. The samples in the post have XMP metadata: https://1drv.ms/u/s!AuKNsLlK8k5jih-WGHX1J0UnOffb |
Oh, nice, I searched but failed to find an XMP example. I'll test that this weekend. Thanks! |
Still to do:
The WONTFIX is probably:
|
OK, I think it's pretty much done. Would anyone be able to try it out? Are there any obvious missing features? The |
I removed I added a Benchmarks! With no stored thumbnails, it's about 0.5s for a typical smartphone image:
It's obviously quick if there are thumbnails available:
|
Would @lovell be the perfect tester? I'm a loyal sharp user :) |
@jcupitt Amazing, thank you John, I'll try to take a look soon. @rayshan Based on what John's demonstrated here I'd expect sharp to "just work" reading HEIC images when compiled against a version of libvips that supports libheif/libde265. Writing HEIC images would require a change to sharp to expose the setting of output options. |
Docs, if anyone is curious: https://github.com/libvips/libvips/blob/add-heifload/libvips/foreign/heifload.c#L810 https://github.com/libvips/libvips/blob/add-heifload/libvips/foreign/heifsave.c#L569 The reduction in filesize is impressive, and larger than I'd expected.
First the original (131kb, Q75 jpg): A Q10 heic, 7.5kb: Q30 heic, 31kb: Q50 heic, 105kb: So Q50 gives about the same size as Q75 jpg. Q10 looks awful, but the size is just tiny. Q30 looks roughly same (to my terrible old eyes) as Q75 jpg, but is more than 4x smaller. |
I forgot to say, those image are probably larger than shown, so you are seeing your browser downsize. Open each one in a separate tab, make sure you are zoomed in, and try flipping between tabs. |
Just to confirm, libheif is NOT available in most Linux distro (probably because of patent issue) |
I think (!?) there are no patent issues around libheif itself. It uses x265 and libde265 for encode / decode, and they are the things that infringe. Users in countries where software patents are a thing would need to make separate arrangements. It's in Debian sid, for example, though not yet in Ubuntu. https://packages.debian.org/sid/libheif-dev I agree that including it in binaries we distribute would need thought. Users would need to be aware of the patent problems. They are also GPL, of course, so the same restrictions that libpoppler faces would apply. |
Same problem, as dependency are not available... |
HEVC patent licensing is sadly a fragmented mess that could bring about its demise. Of the three(?!) patent pools, HEVC Advance is the most progressive and allows royalty-free software-only implementations such as libde265. It is likely that libde265 is impacted by patents from other pools (MPEG LA, Velos) and individual holders (Technicolor, Intel etc.) so I would seek legal advice before distributing binaries of libde265. Ubuntu 18.04+ provides libheif and Ubuntu 16.04+ provides libde265; both are LGPL. Allowing libvips to read and write the patent-free HEIF container via libheif provides the gateway to future royalty-free encoding formats such as AVIF. |
Ah, I'm sure I saw a page saying de265 was GPL, but you're right, it's LGPL3, that's good. Let's merge to master, it'll make testing simpler as we get ready for 8.8. Any problems, please open a new issue. Thanks! |
Thanks for the great work! If I want to convert HEIC to jpeg, is
|
The test image is from https://trac.ffmpeg.org/attachment/ticket/6521/IMG_4479.HEIC. |
Hi @leslie-wang, yes, I see a crash here too, it looks like libvips is asking for RGB, but libheif is handing over RGBA. I'll have a look. |
I've opened a new issue #1247, let's move there. |
In case anyone comes across this old issue, libvips now has full avif support via libheif and aom. |
This may be the new hotness very soon
The text was updated successfully, but these errors were encountered: