9

For a c++ project, I need to open and show a HEIF (.heic) image. What I know (if I'm right) is that the HEIF images are based on the ffmpeg standard, and requires a H265 codec to be read.

I found several open-source H265 codecs:

I can open and show H265 encoded video files with each of them, but I'm unable to simply open, show or convert a .heic image. All of them return an error, or just do nothing.

To be honest I'm a little puzzled, because the HEIF standard seem to be a well kept secret. I'm unable to find a relevant info that could allow me to walk to a solution. Those I found are just tricks and workarounds, like e.g. forcing the device (I'm speaking here about the Apple iPhone using the new iOS11) to generate a jpg image instead of a heic, or using a third party application like dr.fone. Of course these solutions are irrelevant for me.

So, somebody can tell me which codec I should use with a .heif image, and how I can use it to open it? Or are there open source libraries or examples that allow to manipulate this type of image file? Somebody can point me to the good direction?

Jean-Milost Reymond
  • 1,833
  • 1
  • 15
  • 36
  • FFMPEG still does not support HEIF. A bug report is open to handle this. https://trac.ffmpeg.org/ticket/6521 This should be a comment and not a response, but I'm not allowed to comment. – avibrazil Sep 26 '17 at 23:56
  • 1
    Any good news concerning FFMPEG compatibility with HEIC pictures ? – bgcode Mar 28 '18 at 12:39
  • 1
    well, for my part I finally got a result using the libde265 library (http://www.libde265.org/). However it was strongly limited: In all my samples I could just open this video: https://github.com/strukturag/libde265.js/blob/master/demo/spreedmovie.hevc. I don't know if any FFMPEG library now supports the HEIV/HEVC format correctly. All I know is that my company found a way to do that, using several of our internal SDK to read the HEIV/HEVC media content and send it to the FFMPEG decoder, and this resulted among other to a Windows plugin, available here: https://www.copytrans.net/download/ – Jean-Milost Reymond Mar 29 '18 at 13:27

2 Answers2

4

libheif seems to be a pretty active LGPL library for HEIF, with a C API. From the README:

The library has a C API for easy integration and wide language support. Note that the API is still work in progress and may still change.

The decoder automatically supports both HEIF and AVIF through the same API. No changes are required to existing code to support AVIF. The encoder can be switched between HEIF and AVIF simply by setting heif_compression_HEVC or heif_compression_AV1 to heif_context_get_encoder_for_format().

Loading the primary image in an HEIF file is as easy as this:

heif_context* ctx = heif_context_alloc();
heif_context_read_from_file(ctx, input_filename, nullptr);

// get a handle to the primary image
heif_image_handle* handle;
heif_context_get_primary_image_handle(ctx, &handle);

// decode the image and convert colorspace to RGB, saved as 24bit interleaved
heif_image* img;
heif_decode_image(handle, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGB, nullptr);

int stride;
const uint8_t* data = heif_image_get_plane_readonly(img, heif_channel_interleaved, &stride);

A nice, Emscripten-powered demo is available which actually allows you to load and view HEIF files directly inside a browser.

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
  • 3
    Thank you for sharing this info. If a such a library had existed when I had to solve this question, I would probably have used it, but unfortunately in my case I already implemented my own library. But it's a good point to know that an opensource alternative exists now. – Jean-Milost Reymond Feb 08 '19 at 20:51
  • Is heif_pixel_image_get_plane_readonly() deprecated? It showing undefined for me. – ihsan Dec 14 '22 at 13:12
  • Looks like it's changed to `heif_image_get_plane_readonly()` (no `_pixel`). I've updated my README excerpt. – mwfearnley Dec 14 '22 at 13:28
  • Hey i cant get libheif to install properly, When using cmake, how do i add libde265? Without that, heif_decode_image() returns unsupported codec. How do i set up libde265 to work with libheif when using cmake to build? – ihsan Dec 15 '22 at 09:11
3

An example viewer specific for HEIF from Nokia is available here. I also tested directly with FFmpeg and it's able to open (play/decompress) the provided conformance files.

Ronald S. Bultje
  • 10,828
  • 26
  • 47
  • 1
    Thanks for the links, I will do new tests with the conformance files. For the Nokia SDK, I already tried to use it, but without success, and the very restrictive license of the product discouraged me to push further my research. Anyway it's just a parser that allows to open a .heic file and transmit its stream to a codec, or I'm wrong? – Jean-Milost Reymond Sep 14 '17 at 12:45
  • So, the question may be now: "how to transmit a such stream to a codec". First of all, which codec can I use for that? (of course, a part of the answer should be in the conformance files, that I will test as soon as possible) – Jean-Milost Reymond Sep 14 '17 at 13:22
  • 1
    You mean when using Nokia's SDK? I would assume they'd want you to use hm for that, but it's obviously up to you, any decoder (even FFmpeg or de265) would work for that. – Ronald S. Bultje Sep 14 '17 at 17:58