Cykooz / libheif-rs

Safe wrapper to libheif-sys for parsing heif/heic files
MIT License
34 stars 11 forks source link

Expose heif_context_set_max_decoding_threads config? #12

Closed chadaustin closed 1 year ago

chadaustin commented 1 year ago

First of all, I want to say thanks for this library. I'm writing a Rust program to compute rotation-independent perceptual hashes of the photos on my NAS, and libheif-rs has been very helpful.

When profiling on Windows with Superluminal, I noticed libheif is allocating threads per image. I'm already doing my own thread pools, so I'd prefer to have libheif not allocate threads.

I haven't gotten a chance to do a local fork and patch yet, but if you are interested in exposing heif_context_set_max_decoding_threads, that would be useful!

Cykooz commented 1 year ago

As far as I understood from the libheif source code, the heif_context_set_max_decoding_threads function only applies to RGB images that consists of smaller images (tiles).

// If the maximum threads number is set to 0, the image tiles 
// are decoded in the main thread. This is different from setting it to 1, which will 
// generate a single background thread to decode the tiles.
// Note that this setting only affects libheif itself. The codecs itself may still use 
// multi-threaded decoding. You can use it, for example, in cases where you are 
// decoding several images in parallel anyway you thus want
// to minimize parallelism in each decoder.
LIBHEIF_API
void heif_context_set_max_decoding_threads(struct heif_context* ctx, int max_threads);

heif_context_set_max_decoding_threads function set value of HeifContext::m_max_decoding_threads variable. But this variable is used only in one method:

// This function only works with RGB images.
Error HeifContext::decode_full_grid_image(heif_item_id ID,
                                          std::shared_ptr<HeifPixelImage>& img,
                                          const std::vector<uint8_t>& grid_data) const

I can add set_max_decoding_threads() method into HeifContext but I'm not sure if that helps you.

Cykooz commented 1 year ago

I have added this method in the new release

chadaustin commented 1 year ago

Thank you! I tried it out, and you were correct. The threads are coming from libde265, and single-threaded decoding is not currently supported: https://github.com/strukturag/libde265/wiki/Decoder-API-Tutorial#writing-a-simple-video-decoder

No problem - I can deal with the confusing profiler reports for now and can write a patch for libde265 later if necessary.

Thanks again!