Repository: opencv
Issue: MSMF video capture backend doesn't respect setting output format other than default BGR
Labels: priority: low,category: videoio

##### System information (version)
<!-- Example
- OpenCV => 4.2
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2017
-->

- OpenCV => 4.4 (tested, but looks like at least 3.4+)
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 2019 (doesn't matter)

##### Detailed description

Was trying to remove unnecessary color conversion work done on CPU while processing 4k video file and found that you can't actually change output Mat to anything than BGR, even though backend has that ability.

As you can see in [source code](https://github.com/opencv/opencv/blob/5ac0712cf1f25af2224afd1776ca9476e39f85d8/modules/videoio/src/cap_msmf.cpp#L633) variable `outputFormat` is initialized with value `CV_CAP_MODE_BGR` and video stream is configured with that value. However backend [supports changing output format via CV_CAP_PROP_FOURCC property](https://github.com/opencv/opencv/blob/5ac0712cf1f25af2224afd1776ca9476e39f85d8/modules/videoio/src/cap_msmf.cpp#L1311) and CV_CAP_PROP_FORMAT (both do the same thing).

Setting this properties calls `configureOutput` method that uses provided new format to fill MediaType structure and initialize all MediaFoundation related work.
However this method doesn't update `outputFormat` variable at all, so even after you changed MF's output type to YUYV, `retrieveFrame` method [still does switch on old value](https://github.com/opencv/opencv/blob/5ac0712cf1f25af2224afd1776ca9476e39f85d8/modules/videoio/src/cap_msmf.cpp#L1052) that never changed after object construction. And returned Mat is always 8UC3 with fake BGR no matter what.

I actually have no idea why this didn't crash immediately after I'd called it, result frame was like vertical split of original.

Changing [this line](https://github.com/opencv/opencv/blob/5ac0712cf1f25af2224afd1776ca9476e39f85d8/modules/videoio/src/cap_msmf.cpp#L820) to 
```.cpp
if (initStream(dwStreamIndex, newFormat))
{
    outputFormat = outFormat;
    return true;
}
return false;
```
fixes bug.

##### Steps to reproduce
I'm working with C# interop, so don't have exact C++ version, but pseudo code should be like this
```.cpp
#define CV_CAP_MODE_YUYV CV_FOURCC_MACRO('Y', 'U', 'Y', 'V')

cv::VideoCapture video(_path, cv::CAP_MSMF);
video.set(CV_CAP_PROP_FOURCC, CV_CAP_MODE_YUYV);
cv::Mat frame;
video.read(frame);
CV_Assert(frame.type() == CV_8UC2)
```



##### Issue submission checklist

 - [x] I report the issue, it's not a question
   <!--
   OpenCV team works with answers.opencv.org, Stack Overflow and other communities
   to discuss problems. Tickets with question without real issue statement will be
   closed.
   -->
 - [x] I checked the problem with documentation, FAQ, open issues,
       answers.opencv.org, Stack Overflow, etc and have not found solution
   <!--
   Places to check:
   * OpenCV documentation: https://docs.opencv.org
   * FAQ page: https://github.com/opencv/opencv/wiki/FAQ
   * OpenCV forum: https://answers.opencv.org
   * OpenCV issue tracker: https://github.com/opencv/opencv/issues?q=is%3Aissue
   * Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
   -->
 - [x] I updated to latest OpenCV version and the issue is still there
   <!--
   master branch for OpenCV 4.x and 3.4 branch for OpenCV 3.x releases.
   OpenCV team supports only latest release for each branch.
   The ticket is closed, if the problem is not reproduced with modern version.
   -->
 - [x] There is reproducer code and related data files: videos, images, onnx, etc
   <!--
   The best reproducer -- test case for OpenCV that we can add to the library.
   Recommendations for media files and binary files:
   * Try to reproduce the issue with images and videos in opencv_extra repository
     to reduce attachment size
   * Use PNG for images, if you report some CV related bug, but not image reader
     issue
   * Attach the image as archive to the ticket, if you report some reader issue.
     Image hosting services compress images and it breaks the repro code.
   * Provide ONNX file for some public model or ONNX file with with random weights,
     if you report ONNX parsing or handling issue. Architecture details diagram
     from netron tool can be very useful too. See https://lutzroeder.github.io/netron/
   -->