Repository: opencv
Issue: `cv::VideoCapture` with `cv::CAP_V4L2` backend returns incomplete JPEG data
Labels: bug,category: videoio(camera),category: 3rdparty

### System Information

OpenCV version: 4.6.0
Operating System / Platform: Ubuntu 18.04
Compiler & compiler version: GCC 7.5.0
Camera: eMeet C970 

### Detailed description

`cv::VideoCapture` with `cv::CAP_V4L2` backend returns no JPEG End Of Image (`0xFFD9`).
This happens not always.
I modified `CvCaptureCAM_V4L::read_frame_v4l2()` to check JPEG data is correct.

```cpp
bool CvCaptureCAM_V4L::read_video_frame_v4l2() {
    //(snip)
    //We shouldn't use this buffer in the queue while not retrieve frame from it.
    buffers[buf.index].buffer = buf;
    bufferIndex = buf.index;

    //Check JPEG EOI if palette is MJPEG
    bool eoi_found = false;
    if (palette == V4L2_PIX_FMT_MJPEG) {
        auto ptr = reinterpret_cast<unsigned char*>(buffers[buf.index].start);
        auto length = buffers[buf.index].length;
        while (length > 2) {
            auto eoi0 = ptr[length - 2];
            auto eoi1 = ptr[length - 1];
            if (eoi0 == 0xFF && eoi1 == 0xD9) {
              eoi_found = true;
              break;
            }
            length--;
        }
        if (!eoi_found) {
            CV_LOG_ERROR(NULL, "No EOI on captured JPEG");
        }
    }

    //set timestamp in capture struct to be timestamp of most recent frame
    timestamp = buf.timestamp;
    return true;
}
```
But captured JPEG data was correct.

### Steps to reproduce

```cpp
#include <opencv2/videoio.hpp>
#include <iostream>

int main(){
  cv::VideoCapture input(0, cv::CAP_V4L2);
  if (!input.isOpened()) {
    std::cerr << "Failed to open /dev/video0" << std::endl;
    return EXIT_FAILURE;
  }
  input.set(cv::CAP_PROP_CONVERT_RGB, false);
  input.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
  cv::Mat frame;
  uint32_t frame_cnt = 0;
  while(true) {
    if (!input.read(frame)){
      std::cerr << "Failed to capture frame" << std::endl;
    }
    auto length = frame.cols;
    auto ptr = frame.data;
    bool eoi_found = false;
    while (length > 2) {
      auto eoi0 = ptr[length - 2];
      auto eoi1 = ptr[length - 1];
      if (eoi0 == 0xFF && eoi1 == 0xD9) {
        eoi_found = true;
        break;
      }
      length--;
    }
    std::cout << frame_cnt << ": " << frame.size();

    if (!eoi_found) {
      std::cout << " !NO JPEG EOI!";
    }
    std::cout << std::endl;
    frame_cnt++;
  }
}
```

This example shows that incomplete JPEG data was retrieved.

```shell
$ ./build/reproduce
(snip)
126: [41955 x 1]
127: [41955 x 1] !NO JPEG EOI!
(snip)
310: [41947 x 1]
311: [41947 x 1] !NO JPEG EOI!
```

Incomplete frame had same size as previous frame, but frame data was not same.
It seems that frame data was new one, but frame data size is old.

### 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
- [ ] There is reproducer code and related data files (videos, images, onnx, etc)