How to correctly clean internal queue of buffers on capture/output planes?

Is it possible to flush the buffers if they are queued inside the decoder (encoder) before closing the device or this will lead to memory leaks?

How critical is this? What are the possible scenarios for flushing buffers if the device is open in non-blocking mode?

After the call

v4l2_ioctl (fd, VIDIOC_REQBUFS, & reqbufs);

when reqbufs.count = 0 all buffers can be freed safely, regardless of whether it is queued. This is true?

Is there a difference in the behavior of the buffers MMAP and DMA memory type?

Please check 00_video_decode for how EOS is handled. There are blocking mode and non-blocking mode and EOS handling is slightliy different.

For V4L2_MEMORY_MMAP, buffers are allocated/destroyed in decoder.
For V4L2_MEMORY_DMABUF, buffers are allocated/destroyed in application.