mpromonet / v4l2rtspserver

RTSP Server for V4L2 device capture supporting HEVC/H264/JPEG/VP8/VP9
The Unlicense
1.86k stars 428 forks source link

Image garbled at high resolution #237

Open camaro4life18 opened 3 years ago

camaro4life18 commented 3 years ago

When I set the resolution for the stream at the camera maximum, it comes out all garbled. Here is the supported formats for my camera: `v4l2-ctl --list-formats-ext ioctl: VIDIOC_ENUM_FMT Type: Video Capture

    [0]: 'YUYV' (YUYV 4:2:2)
            Size: Discrete 3264x2448
                    Interval: Discrete 0.500s (2.000 fps)
            Size: Discrete 2592x1944
                    Interval: Discrete 0.500s (2.000 fps)
            Size: Discrete 2048x1536
                    Interval: Discrete 0.500s (2.000 fps)
            Size: Discrete 1920x1080
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 1600x1200
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 1280x960
                    Interval: Discrete 0.200s (5.000 fps)
            Size: Discrete 1280x720
                    Interval: Discrete 0.067s (15.000 fps)
            Size: Discrete 800x600
                    Interval: Discrete 0.050s (20.000 fps)
            Size: Discrete 640x480
                    Interval: Discrete 0.050s (20.000 fps)
    [1]: 'MJPG' (Motion-JPEG, compressed)
            Size: Discrete 3264x2448
                    Interval: Discrete 0.067s (15.000 fps)
            Size: Discrete 2592x1944
                    Interval: Discrete 0.067s (15.000 fps)
            Size: Discrete 2048x1536
                    Interval: Discrete 0.067s (15.000 fps)
            Size: Discrete 1920x1080
                    Interval: Discrete 0.033s (30.000 fps)
            Size: Discrete 1600x1200
                    Interval: Discrete 0.033s (30.000 fps)
            Size: Discrete 1280x960
                    Interval: Discrete 0.033s (30.000 fps)
            Size: Discrete 1280x720
                    Interval: Discrete 0.033s (30.000 fps)
            Size: Discrete 800x600
                    Interval: Discrete 0.033s (30.000 fps)
            Size: Discrete 640x480
                    Interval: Discrete 0.033s (30.000 fps)

`

Then when I run the command...

v4l2rtspserver -W 3264 -H 2448 -F 15 -P 8554 -fMJPG /dev/video0 2021-02-25 14:50:43,585 [NOTICE] - /home/pi/v4l2rtspserver/main.cpp:385 Version: 0.2.1-17-g6005c2f live555 version:2021.02.11 2021-02-25 14:50:43,586 [NOTICE] - /home/pi/v4l2rtspserver/main.cpp:426 Create V4L2 Source.../dev/video0 2021-02-25 14:50:43,677 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2Device.cpp:133 driver:uvcvideo capabilities:84a00001 mandatory:4000001 2021-02-25 14:50:43,677 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2Device.cpp:136 /dev/video0 support capture 2021-02-25 14:50:43,677 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2Device.cpp:139 /dev/video0 support streaming 2021-02-25 14:50:43,693 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2Device.cpp:215 /dev/video0:MJPG size:3264x2448 bufferSize:15981133 2021-02-25 14:50:43,709 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2Device.cpp:236 fps:1/15 2021-02-25 14:50:43,709 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2Device.cpp:237 nbBuffer:0 2021-02-25 14:50:43,709 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2MmapDevice.cpp:49 Device /dev/video0 2021-02-25 14:50:43,919 [NOTICE] - /home/pi/v4l2rtspserver/v4l2wrapper/src/V4l2MmapDevice.cpp:73 Device /dev/video0 nb buffer:10 2021-02-25 14:50:44,005 [NOTICE] - /home/pi/v4l2rtspserver/main.cpp:449 Create Source .../dev/video0 2021-02-25 14:50:44,005 [NOTICE] - /home/pi/v4l2rtspserver/inc/V4l2RTSPServer.h:84 Play this stream using the URL "rtsp://192.168.1.180:8554/unicast" 2021-02-25 14:50:44,005 [NOTICE] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:93 begin thread RTCPInstance::RTCPInstance error: totSessionBW parameter should not be zero!

This image looks distorted and the resolution on the client comes back as 1216x400

image

camaro4life18 commented 3 years ago

Digging into my own issue, I ran with -vv and got the follow logs...

2021-02-26 12:18:02,014 [DEBUG ] - /home/pi/v4l2rtspserver/src/MJPEGVideoSource.cpp:26 SOF length:17 2021-02-26 12:18:02,014 [INFO ] - /home/pi/v4l2rtspserver/src/MJPEGVideoSource.cpp:42 width:1216 height:400 type:0 precision:8 2021-02-26 12:18:02,014 [DEBUG ] - /home/pi/v4l2rtspserver/src/MJPEGVideoSource.cpp:49 DQT length:132 2021-02-26 12:18:02,014 [DEBUG ] - /home/pi/v4l2rtspserver/src/MJPEGVideoSource.cpp:57 Quantization table idx:0 precision:0 size:64 total size:64 2021-02-26 12:18:02,014 [DEBUG ] - /home/pi/v4l2rtspserver/src/MJPEGVideoSource.cpp:77 SOS length:12 2021-02-26 12:18:02,014 [DEBUG ] - /home/pi/v4l2rtspserver/src/MJPEGVideoSource.cpp:87 headerSize:589 2021-02-26 12:18:02,018 [DEBUG ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:105 waitingFrame delay:66ms 2021-02-26 12:18:02,019 [INFO ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:29 intv_sec:1614359882 fps:15 bandwidth:45002kbps 2021-02-26 12:18:02,019 [DEBUG ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:213 getNextFrame timestamp:1614359882.18177 size:384016 diff:1ms 2021-02-26 12:18:02,019 [DEBUG ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:245 queueFrame timestamp:1614359882.18177 size:384016 diff:1ms 2021-02-26 12:18:02,084 [DEBUG ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:105 waitingFrame delay:66ms 2021-02-26 12:18:02,085 [DEBUG ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:213 getNextFrame timestamp:1614359882.84836 size:383872 diff:1ms 2021-02-26 12:18:02,085 [DEBUG ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:245 queueFrame timestamp:1614359882.84836 size:383872 diff:1ms 2021-02-26 12:18:02,139 [DEBUG ] - /home/pi/v4l2rtspserver/src/DeviceSource.cpp:162 deliverFrame timestamp:1614359882.139887 size:384016 diff:121ms queue:1

In those logs you can see the resolution is getting shrunk. I dug in the code to see where that output is coming from and traced some of the code back to DeviceSource.cpp line 150 that is reducing the size to fit in fMaxSize. So it looks like live555 is reducing the image size to fit its buffer.

I'm not familiar with this code and how live555 and ffmpeg work, but this definitely seems like the problem for such a high resolution. It also looks like i'm not the only one to ever run into this issue.... https://stackoverflow.com/questions/20510484/live555-fmaxsize-and-ffmpeg

mpromonet commented 3 years ago

Hi camaro4life18,

This project doesnot use ffmpeg, it doesnot compress images. The MJEG streaming is not implemented in live555 and this project brings a naive implementation of it. Depending on the JPEG image, it may work or not. It seems size if not correctly decode, you should look to your JPEG frame regarding https://github.com/mpromonet/v4l2rtspserver/blob/master/src/MJPEGVideoSource.cpp

Note that JPEG RTP are limited by the https://tools.ietf.org/html/rfc2435 :

3.1.5.  Width: 8 bits

   This field encodes the width of the image in 8-pixel multiples (e.g.,
   a width of 40 denotes an image 320 pixels wide).  The maximum width
   is 2040 pixels.

3.1.6.  Height: 8 bits

   This field encodes the height of the image in 8-pixel multiples
   (e.g., a height of 30 denotes an image 240 pixels tall). When
   encoding interlaced video, this is the height of a video field, since
   fields are individually JPEG encoded. The maximum height is 2040
   pixels.

Then 3264x2448 is not possible.

Best Regards, Michel.