Repository: opencv
Issue: Invalid memory access when writing CV_8U FFV1 videos
Labels: wontfix,category: 3rdparty

### System Information

OpenCV version: 4.7.0
OS: Ubuntu 22.04.2
Compiler: GCC 11.4.0

### Detailed description

Bad memory access when writing CV_8U images to videos with FFV1 codec. The valgrind report shows the errors, executable is occasionally crashing.

```
==12468== Memcheck, a memory error detector
==12468== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==12468== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==12468== Command: ./bin/app
==12468== 
==12468== Warning: set address range perms: large range [0xb6a4000, 0x24e5b000) (defined)
==12468== Invalid read of size 8
==12468==    at 0x283A1A06: av_image_copy (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
==12468==    by 0x2838FB52: av_frame_copy (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
==12468==    by 0x2838FDB7: av_frame_ref (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
==12468==    by 0x26F96F3A: avcodec_send_frame (in /usr/lib/x86_64-linux-gnu/libavcodec.so.58.134.100)
==12468==    by 0x4F3E3AA: icv_av_write_frame_FFMPEG(AVFormatContext*, AVStream*, AVCodecContext*, unsigned char*, unsigned int, AVFrame*, int) [clone .constprop.0] (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x4F3EDDE: CvVideoWriter_FFMPEG::writeFrame(unsigned char const*, int, int, int, int, int) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x4F3F514: cv::(anonymous namespace)::CvVideoWriter_FFMPEG_proxy::write(cv::_InputArray const&) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x4EFAF77: cv::VideoWriter::write(cv::_InputArray const&) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x11EAD6: main (in /home/jenna/dev/maestro/build/bin/maestro-exovision)
==12468==  Address 0x38cc8208 is 0 bytes after a block of size 921,672 alloc'd
==12468==    at 0x484A899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==12468==    by 0x5014158: cv::fastMalloc(unsigned long) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_core.so.4.7.0)
==12468==    by 0x511DADB: cv::Mat::create(int, int const*, int) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_core.so.4.7.0)
==12468==    by 0x511DE46: cv::Mat::create(int, int, int) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/moonsurgical/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_core.so.4.7.0)
==12468==    by 0x511DF1E: cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_core.so.4.7.0)
==12468==    by 0x11EAA6: main (in /home/jenna/dev/maestro/build/bin/app)
==12468== 
==12468== Invalid read of size 8
==12468==    at 0x283A19E8: av_image_copy (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
==12468==    by 0x2838FB52: av_frame_copy (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
==12468==    by 0x2838FDB7: av_frame_ref (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100)
==12468==    by 0x26F96F3A: avcodec_send_frame (in /usr/lib/x86_64-linux-gnu/libavcodec.so.58.134.100)
==12468==    by 0x4F3E3AA: icv_av_write_frame_FFMPEG(AVFormatContext*, AVStream*, AVCodecContext*, unsigned char*, unsigned int, AVFrame*, int) [clone .constprop.0] (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x4F3EDDE: CvVideoWriter_FFMPEG::writeFrame(unsigned char const*, int, int, int, int, int) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x4F3F514: cv::(anonymous namespace)::CvVideoWriter_FFMPEG_proxy::write(cv::_InputArray const&) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/moonsurgical/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x4EFAF77: cv::VideoWriter::write(cv::_InputArray const&) (in /home/jenna/.conan/data/opencv/4.7.0-pck-a/company/stable/package/062863c92a5a0a247840166e9f84ebe8d10786b9/lib/libopencv_videoio.so.4.7.0)
==12468==    by 0x11EAD6: main (in /home/jenna/dev/maestro/build/bin/app)
==12468==  Address 0x37d52e78 is 888 bytes inside an unallocated block of size 914,656 in arena "client"

```

### Steps to reproduce

```
int main(int argc, char** argv)
{
   const auto l_codec = cv::VideoWriter::fourcc('F', 'F', 'V', '1');
   cv::VideoWriter l_videoWriter("/data/test.avi", cv::CAP_FFMPEG, l_codec, 6, cv::Size(1280, 720), false);

   for (int i = 0; i < 100; ++i)
   {
      cv::Mat l_image(720, 1280, CV_8UC1, cv::Scalar(255));
      l_videoWriter.write(l_image);
   }
   l_videoWriter.release();

   return 0;
}
```

### Issue submission checklist

- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [ ] I updated to the latest OpenCV version and the issue is still there
- [X] There is reproducer code and related data files (videos, images, onnx, etc)