Repository: opencv
Issue: AVFoundation as the VideoCapture backend API will `read()` out weird frame data.
Labels: bug,category: videoio,platform: ios/osx

### System Information

- OpenCV version: 4.8.0
- Operating System / Platform: macOS 13.3.1
- Compiler & compiler version:
  ```
  Apple clang version 14.0.3 (clang-1403.0.22.14.1)
  Target: arm64-apple-darwin22.4.0
  Thread model: posix
  InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
  ```

OpenCV build info (Installed from Conda):
```
General configuration for OpenCV 4.8.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/work/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2023-07-22T12:05:15Z
    Host:                        Darwin 20.6.0 x86_64
    Target:                      Darwin 20.0.0 arm64
    CMake:                       3.26.4
    CMake generator:             Ninja
    CMake build tool:            /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_build_env/bin/ninja
    Configuration:               Release

  CPU/HW features:
    Baseline:                    NEON FP16
      required:                  NEON
      disabled:                  VFPV3

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                17
    C++ Compiler:                /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_build_env/bin/arm64-apple-darwin20.0.0-clang++  (ver 15.0.7)
    C++ flags (Release):         -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -fmessage-length=0 -isystem /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/include -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/work=/usr/local/src/conda/libopencv-4.8.0 -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold=/usr/local/src/conda-prefix   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -fmessage-length=0 -isystem /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/include -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/work=/usr/local/src/conda/libopencv-4.8.0 -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold=/usr/local/src/conda-prefix   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -g  -DDEBUG -D_DEBUG
    C Compiler:                  /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_build_env/bin/arm64-apple-darwin20.0.0-clang
    C flags (Release):           -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -isystem /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/include -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/work=/usr/local/src/conda/libopencv-4.8.0 -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold=/usr/local/src/conda-prefix   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -isystem /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/include -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/work=/usr/local/src/conda/libopencv-4.8.0 -fdebug-prefix-map=/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold=/usr/local/src/conda-prefix   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-deprecated-enum-enum-conversion -Wno-deprecated-anon-enum-enum-conversion -fdiagnostics-show-option -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections    -g  -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,-pie -Wl,-headerpad_max_install_names -Wl,-dead_strip_dylibs -Wl,-rpath,/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib -L/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib  -Wl,-dead_strip
    Linker flags (Debug):        -Wl,-pie -Wl,-headerpad_max_install_names -Wl,-dead_strip_dylibs -Wl,-rpath,/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib -L/Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib  -Wl,-dead_strip
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 alphamat aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv java julia matlab ovis python2 sfm ts viz
    Applications:                apps
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:                           COCOA
    Cocoa:                       YES

  Media I/O:
    ZLib:                        /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/libz.dylib (ver 1.2.13)
    JPEG:                        /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/libjpeg.dylib (ver 80)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/libpng.dylib (ver 1.6.39)
    TIFF:                        /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/libtiff.dylib (ver 42 / 4.5.1)
    JPEG 2000:                   /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/libjasper.dylib (ver 4.0.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    FFMPEG:                      YES
      avcodec:                   YES (60.3.100)
      avformat:                  YES (60.3.100)
      avutil:                    YES (58.2.100)
      swscale:                   YES (7.1.100)
      avresample:                NO
    AVFoundation:                YES

  Parallel framework:            GCD

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      YES (lapack cblas)
    OpenVINO:                    YES (2023.0.1)
    Eigen:                       YES (ver 3.4.0)
    Custom HAL:                  YES (carotene (ver 0.0.1))
    Protobuf:                    /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/libprotobuf.dylib (3.21.12)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  Python 3:
    Interpreter:                 /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/bin/python (ver 3.11.4)
    Libraries:                   /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/libpython3.11.dylib (ver 3.11.4)
    numpy:                       /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_build_env/venv/lib/python3.11/site-packages/numpy/core/include (ver undefined - cannot be probed because of the cross-compilation)
    install path:                /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/python3.11/site-packages

  Python (for build):            /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/bin/python

  Install to:                    /Users/runner/miniforge3/conda-bld/libopencv_1690027095802/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold
-----------------------------------------------------------------
```

### Detailed description

Here are two problem with `cv::CAP_AVFOUNDATION` as the backend API of VideoCapture:

1. For some video files, the first frame read out is all-zero.
2. The frame data is different from `cv::CAP_FFMPEG` as the backend API. I need to align it with the Linux OpenCV, which uses `cv::CAP_FFMPEG` as the backend.

I'm trying to use OpenCV on iOS, which only provide `cv::CAP_AVFOUNDATION` as the VideoCapture backend API. And I found it has these issues listed above.

### Steps to reproduce

For the first problem, here is the test video:

https://github.com/opencv/opencv/assets/22856433/9c1ea094-0c06-4027-acb4-49e0b858d511

Read first 10 frames and print the sum of each frame:
```cpp
int main() {
    cout << "starting..." << endl;
    string video_file = "/path/to/test.mp4";
    cv::VideoCapture cap(video_file, cv::CAP_AVFOUNDATION);
    std::cout << "VideoCapture backend name: " << cap.getBackendName() << std::endl;
    cv::Mat frame;
    for (int i = 0; i < 10; i++) {
        if (cap.read(frame)) {
            std::cout << "sum: " << cv::sum(frame) << std::endl;
        }
    }
    return 0;
}
```

Output:
```
starting...
VideoCapture backend name: AVFOUNDATION
sum: [0, 0, 0, 0]
sum: [1.95294e+08, 2.23108e+08, 2.66073e+08, 0]
sum: [1.9607e+08, 2.24322e+08, 2.67879e+08, 0]
sum: [1.98047e+08, 2.27122e+08, 2.71757e+08, 0]
sum: [1.98706e+08, 2.28026e+08, 2.72807e+08, 0]
sum: [1.97659e+08, 2.26568e+08, 2.70235e+08, 0]
sum: [1.96755e+08, 2.25757e+08, 2.68343e+08, 0]
sum: [1.97153e+08, 2.26161e+08, 2.69008e+08, 0]
sum: [1.96985e+08, 2.26974e+08, 2.69596e+08, 0]
sum: [1.96676e+08, 2.277e+08, 2.69989e+08, 0]
```

You can see that the first frame read successfully **but the content is all-zero**.

Switch to `FFMPEG` as the backend, the sum looks fine:

```
starting...
VideoCapture backend name: FFMPEG
sum: [1.92925e+08, 2.20209e+08, 2.62964e+08, 0]
sum: [1.93731e+08, 2.21416e+08, 2.64778e+08, 0]
sum: [1.95721e+08, 2.24181e+08, 2.68668e+08, 0]
sum: [1.96386e+08, 2.25072e+08, 2.69736e+08, 0]
sum: [1.95348e+08, 2.2361e+08, 2.67181e+08, 0]
sum: [1.94442e+08, 2.22798e+08, 2.65309e+08, 0]
sum: [1.94855e+08, 2.23208e+08, 2.66002e+08, 0]
sum: [1.94669e+08, 2.23991e+08, 2.66607e+08, 0]
sum: [1.94349e+08, 2.24715e+08, 2.67001e+08, 0]
sum: [1.95217e+08, 2.25232e+08, 2.67725e+08, 0]
```

And you can see **the sum is different from `AVFOUNDATION` as the backend**.

---

BTW, **can I compile a OpenCV iOS Framework from source that uses `FFMPEG` as the VideoCapture backend**? This way I can align the result with Linux version.

I read [OpenCV: Installation in iOS](https://docs.opencv.org/3.4/d5/da3/tutorial_ios_install.html) but it didn't told me how to enable FFMPEG on iOS. I also found this in CMakeLists.txt:

```cmake
OCV_OPTION(WITH_FFMPEG "Include FFMPEG support" (NOT ANDROID)
  VISIBLE_IF NOT IOS AND NOT WINRT
  VERIFY HAVE_FFMPEG)
```

So, the FFMPEG backend is not supported on iOS?

### 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
- [X] 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)