Skip to content

[BUG] MJPEGEncoder - TypeError: memoryview: casts are restricted to C-contiguous views #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
signag opened this issue Feb 20, 2024 · 5 comments

Comments

@signag
Copy link

signag commented Feb 20, 2024

Hello,

Bug Description

I am working on a web server for controlling Raspberry Pi cameras with picamera2 (raspi-cam-srv)
This includes an MJPEG live stream.

Working with camera module 2, I try to use several stream sizes which are available in the 7 sensor modes.
The following sizes have been tested:
(640, 480) - OK
(1640, 1232) - NOK
(1920, 1080) - OK
(3280, 2464) - NOK
For stream sizes marked above with NOK, I get the following exception:

Exception during process_requests()
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/picamera2/previews/null_preview.py", line 85, in handle_request
    picam2.process_requests(self)
  File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1265, in process_requests
    encoder.encode(encoder.name, req)
  File "/usr/lib/python3/dist-packages/picamera2/encoders/encoder.py", line 210, in encode
    self._encode(stream, request)
  File "/usr/lib/python3/dist-packages/picamera2/encoders/libav_mjpeg_encoder.py", line 81, in _encode
    frame = av.VideoFrame.from_ndarray(m.array, format=self._av_input_format, width=self.width)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "av/video/frame.pyx", line 463, in av.video.frame.VideoFrame.from_ndarray
  File "av/video/frame.pyx", line 44, in av.video.frame.copy_array_to_plane
TypeError: memoryview: casts are restricted to C-contiguous views

To Reproduce

To reproduce the issue, I have attached the following test program which is an excerpt of the streaming server of raspiCamSrv.
testPicamera2MJPEGEncoder.py.txt

Expected behaviour

The MJPEGEncoder should support all stream sizes of the available sensor modes.

Console Output, Screenshots

This is the output obtained when running the test program:
testOutput.txt

Hardware

RPi 5
Attached cameras:

  • (0): module 2 camera ("Model": "imx219")
  • (1): module 3 camera ("Model": "imx708")

OS: Bookworm
Debian version: 12.5

Additional Context

The above error does not occur when using the different stream sizes with camera module 3.

@davidplowman
Copy link
Collaborator

Hi, thanks for the report.

Yes, there do seem to be some problems passing images to libav when there is padding on the end of each row of pixels (as hardware often requires for alignment purposes). Specifically it's the PyAV wrapper that doesn't like non-contiguous arrays. Doing yet another copy to make them contiguous seems quite painful, so it would probably be better to patch PyAV to cope - I'll have a look.

@davidplowman
Copy link
Collaborator

OK, I think the top commit here https://github.com/davidplowman/PyAV should do it. Obviously that'll have to get built into a package and distributed to our apt repo.

@signag
Copy link
Author

signag commented Feb 21, 2024

Hi,
I compiled your latest PyAV from https://github.com/davidplowman/PyAV and deployed av to my virtual environment.

Now all stream sizes, listed in the sensor modes, can be successfully configured.
I tested also some extreme ones like (3280, 52) which work too.

Thank you very much

@davidplowman
Copy link
Collaborator

Thanks for the confirmation. I'll get the version in our apt repository updated.

@davidplowman
Copy link
Collaborator

I think the apt repo for Bookworm has also been updated now if that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants