Reusing Mat memory as much as possible when decoding a sequence of images

After some more research, I now see a couple of options to do what I need:

  1. Replicate some of the logic in imdecode_() to look ahead and figure out the size and type of the image. Then create a Mat header with the same size and type, point that header to the pinned data I already have and know is large enough, and use this Mat as the dst parameter in imdecode().

  2. Create a custom allocator inheriting the cv::MatAllocator() abstract class like here, instantiate this class with some pinned memory, and set the instance as the allocator of the dst Mat. The custom allocator will allocate new memory only if the requested memory is larger than the memory it already has.

Option (1) looks straightforward, but limited to imdecode() only. Option (2) may not be as straightforward, but I can use it to change the behavior of any Mat::create() call on the Mat to which it’s applied, meaning it’s not limited to imdecode().

Thoughts or recommendations?

P.S. I found an old post that asked for the same feature I’m seeking: Does Mat::create() reallocate when new size is smaller? - OpenCV Q&A Forum