"Unable to request 2 buffers: Cannot allocate memory" with 2 64mp cameras

RPI 5, 64-bit bookworm desktop
libcamera 0.1.0.2
2x64MP arducam autofocus cameras

pi@pi5:~ $ cat /proc/meminfo | grep Cma
CmaTotal:         655360 kB
CmaFree:          638880 kB

Hi, I get this error when i try to use two 64mp cameras from my c++ app

[3:15:25.513588021] [18902] ERROR V4L2 v4l2_videodevice.cpp:1241 /dev/video34[42:cap]: Unable to request 2 buffers: Cannot allocate memory
[3:15:25.513637410] [18902] ERROR RPI pipeline_base.cpp:683 Failed to allocate buffers

I created this simple example to demonstrate the error

if you uncomment the lines 54, 55 the error does not occur. I pinned the error to the line camera_release() in the closeCamera() method.

“Unable to request 2 buffers: Cannot allocate memory” - but i explicitly tell the cameras to use only 1 buffer in the configureStill method, why is it trying to allocate two buffers for both cameras? even if i put buffercount to 0 i still get error text “Unable to request 2 buffers”.
Also since i use YUV420 then each buffer should be 80MB so 4x80=320MB should be still ok since i have over 600MB free CMA memory. Or is this v4l_videodevice.cpp using rgb buffer? in this case 4x190MB = 760MB yes i would be running out of memory.

if i run 2xlibcamera-jpeg or libcamera-still with --encoding yuv420 in parallel in different command prompts i dont get this error.

whats going on here and how to fix this? why isnt the buffercount i give taken into account?

found these two threads that explain in detail whats going on with memory. seems that there is no fix for this though and only way to capture two images in parallel with such high res is still to use two separate RPIs each having 1 separate camera.
https://forums.raspberrypi.com/viewtopic.php?t=338949
https://forums.raspberrypi.com/viewtopic.php?t=339182
i will try the lite OS though instead of desktop

Merry Christmas Henri,
Thank you for your contribution to our forum!
Regarding your problem, though you are using Pi 5(especially you try to use two cameras), 64mp cameras will take up too much memory so that the error “Cannot allocate memory” always occus.
Changing to lite version may be the final choice…

tx, this seems to be hardware limitation. CMA memory is under DMA_ZONE which in turn can be max 1GB. I was about to look into changing DMA_ZONE memory range in raspberry pi os source code and then recompile the kernel, but then i stumbled to this sentence in documentation that was in the source code:

Zones
=====

Often hardware poses restrictions on how different physical memory
ranges can be accessed. In some cases, devices cannot perform DMA to
all the addressable memory. In other cases, the size of the physical
memory exceeds the maximal addressable size of virtual memory and
special actions are required to access portions of the memory. Linux
groups memory pages into `zones` according to their possible
usage. For example, ZONE_DMA will contain memory that can be used by
devices for DMA, ZONE_HIGHMEM will contain memory that is not
permanently mapped into kernel's address space and ZONE_NORMAL will
contain normally addressed pages.

The actual layout of the memory zones is hardware dependent as not all
architectures define all zones, and requirements for DMA are different
for different platforms.

What i have concluded is that there is a ton of wires coming from camera that go through some sort of multiplexer or controller or something like that map the memory to those wires based on CMA code so the peripheral device(camera in our case) can very fast dump its frame and theres certain fixed amount of wires going from that controller to the ram thats why we cant make DMA_ZONE greater than 1GB on raspberry pi.

My conclusion could be wrong though since i never found the place where to set DMA_ZONE memory ranges. So for those willing to go into that rabbit hole i found a post on this topic: [PATCH] arm64: make CONFIG_ZONE_DMA user settable

Just wondering, are there alternatives to raspberry pi that would work with 64mp arducam? I could potentially try those out and see if there are same hardware restrictions.

did some more digging i think its defined in this file https://github.com/raspberrypi/linux/blob/rpi-6.1.y/arch/arm64/mm/init.c
at rows 220-230

static void __init zone_sizes_init(void)
{
	unsigned long max_zone_pfns[MAX_NR_ZONES]  = {0};
	unsigned int __maybe_unused acpi_zone_dma_bits;
	unsigned int __maybe_unused dt_zone_dma_bits;
	phys_addr_t __maybe_unused dma32_phys_limit = max_zone_phys(32);

#ifdef CONFIG_ZONE_DMA
	acpi_zone_dma_bits = fls64(acpi_iort_dma_get_max_cpu_address());
	dt_zone_dma_bits = fls64(of_dma_get_max_cpu_address(NULL));
	zone_dma_bits = min3(32U, dt_zone_dma_bits, acpi_zone_dma_bits);
	arm64_dma_phys_limit = max_zone_phys(zone_dma_bits);
	max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit);
#endif
#ifdef CONFIG_ZONE_DMA32
	max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
	if (!arm64_dma_phys_limit)
		arm64_dma_phys_limit = dma32_phys_limit;
#endif
	max_zone_pfns[ZONE_NORMAL] = max_pfn;

	free_area_init(max_zone_pfns);
}

now need to find where arm64_dma_phys_limit gets its value

lines 58-91

/*
 * If the corresponding config options are enabled, we create both ZONE_DMA
 * and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory
 * unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4).
 * In such case, ZONE_DMA32 covers the rest of the 32-bit addressable memory,
 * otherwise it is empty.
 *
 * Memory reservation for crash kernel either done early or deferred
 * depending on DMA memory zones configs (ZONE_DMA) --
 *
 * In absence of ZONE_DMA configs arm64_dma_phys_limit initialized
 * here instead of max_zone_phys().  This lets early reservation of
 * crash kernel memory which has a dependency on arm64_dma_phys_limit.
 * Reserving memory early for crash kernel allows linear creation of block
 * mappings (greater than page-granularity) for all the memory bank rangs.
 * In this scheme a comparatively quicker boot is observed.
 *
 * If ZONE_DMA configs are defined, crash kernel memory reservation
 * is delayed until DMA zone memory range size initialization performed in
 * zone_sizes_init().  The defer is necessary to steer clear of DMA zone
 * memory range to avoid overlap allocation.  So crash kernel memory boundaries
 * are not known when mapping all bank memory ranges, which otherwise means
 * not possible to exclude crash kernel range from creating block mappings
 * so page-granularity mappings are created for the entire memory range.
 * Hence a slightly slower boot is observed.
 *
 * Note: Page-granularity mappings are necessary for crash kernel memory
 * range for shrinking its size via /sys/kernel/kexec_crash_size interface.
 */
#if IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32)
phys_addr_t __ro_after_init arm64_dma_phys_limit;
#else
phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1;
#endif

"If the corresponding config options are enabled, we create both ZONE_DMA

  • and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory
  • unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4)."

30-bits is 1GB go figure xD

i wonder could we ask the raspberry pi guys wether pi5 also is limited to 30-bits. some forum u could reccommend? or some alternative to raspberry pi that does not have such restriction? what other hardware does arducam 64mp cameras work on?

also seems like this dude managed to squeeze 848MB for CMA by playing around with device-tree adresses. its worth investigationg Maximizing CMA memory size problem - Page 2 - Raspberry Pi Forums look at his last post

Arduino, Raspberry Pi, NVIDIA Jetson, Rockchip, NXP, and more - from arducam website. would need to contact these companies and ask wether they have 30-bit DMA_ZONE limit

i also made post to raspberry pi forums on the topic of does pi5 have ZONE_DMA limited to 30 bits Question: Is Pi5 ZONE_DMA also limited to 30-bit addressable memory? - Raspberry Pi Forums
and to nvidia forums about jetson Is ZONE_DMA limit 30 or 32-bits on Jetson? - Jetson Nano - NVIDIA Developer Forums

https://www.marcusfolkesson.se/blog/contiguous-memory-allocator/
also i reccommend this post - this guy says he got Pi4 CMA to 900MiB!!!

pi@pi4:~ $ sudo cat /proc/iomem
00000000-3b2fffff : System RAM (960 MiB)
00000000-00000fff : reserved (0.00391 MiB)

~2MiB hole here

00210000-0122ffff : Kernel code (16.1875 MiB)
01230000-0165ffff : reserved (4.34375 MiB)
01660000-0199ffff : Kernel data (2.625 MiB)

~10MiB hole

02400000-2e3fffff : reserved (705.5 MiB) <— CMA

hole 2E400000 - 2E559FFF 0.07 MiB

2e55a000-2e567fff : reserved (0.00488 MiB)
2e568000-2effffff : reserved (10 MiB)

big hole ~137 MB 2f000000 - 372fffff

37300000-3b2fffff : reserved (64.25 MiB)

pi@pi4:/ $ sudo cat /proc/meminfo
CmaTotal: 720896 kB
CmaFree: 705376 kB

if im able to find whats allocated at
2e55a000-2e567fff : reserved (0.00488 MiB)
and
2e568000-2effffff : reserved (10 MiB)

i might be able to allocate somewhere else and then i can have cma up to 848MiB. The small one 0.00488 MiB i think is the device_tree they mention in the raspberry pi forum
and can be moved with setting
device_tree_address=0x2000000
device_tree_end=0x20FFFFF
in the /boot/config.txt just use adresses suitable for your case
but the big one 10MB they dont even mention in their thread. they dont have it in their iomem https://forums.raspberrypi.com/viewtopic.php?t=355799&start=25

I looked at whats in that address

pi@pi4:~ $ dmesg | grep -i 2e568000
[ 0.000000] reserved[0x5] [0x000000002e568000-0x000000002effffff], 0x0000000000a98000 bytes flags: 0x0
[ 0.765971] memblock_phys_free: [0x000000002e568000-0x000000002effffff] free_initrd_mem+0x34/0x5c

what is it?

i did add memblock=debug to /boot/cmdline.txt but i dont see any more information

Hey, I think I managed to get around 3GB+ recognized on the CMAd by adding cma=3072M at the end of the /boot/cmdlinet.txt Somewhere between 3GB-4GB seems to be the maximum. I messed up the boot, flashed it again with the lite 64-bit OS, plugged it into pi5 right away, and now, after editing the cmdline.txt and rebooting, it shows that there’s indeed that much CMAd available.

CmaTotal:        3145728 kB
CmaFree:         3135392 kB

I probably couldn’t get it before because after flashing it, I used that stick on a pi4 earlier. Somehow, after the first boot configuration there, it ended up recognizing only 1GB. Have a great end to the old year!

Unfortunately after setting CMA that large i cant use keyboard nor do i see cameras being attached with libcamera-still --list-cameras

I managed to increase CMA to 3072MB. Instructions here

https://forums.raspberrypi.com/viewtopic.php?p=2173568#p2173568

running these two commands in separate terminals works fine
libcamera-still --camera 0 --width 9152 --height 6944 -o test0.jpg -n -t 1 --encoding yuv420
libcamera-still --camera 1 --width 9152 --height 6944 -o test1.jpg -n -t 1 --encoding yuv420

but the with bgr still get error in second terminal when running
libcamera-still --camera 0 --width 9152 --height 6944 -o test0.jpg -n -t 1 --encoding rgb
libcamera-still --camera 1 --width 9152 --height 6944 -o test1.jpg -n -t 1 --encoding rgb

pi@pi:~ $ libcamera-still --camera 1 --width 9152 --height 6944 -o test1.jpg -n                                                                                                                                    -t 1 --encoding rgb
[0:05:20.147690394] [1079]  INFO Camera camera_manager.cpp:284 libcamera v0.1.0+                                                                                                                                   146-cff216fa-dirty (2023-12-05T09:54:10+00:00)
[0:05:20.174573716] [1091]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a25                                                                                                                                   8644a 22-11-2023 (22:03:31)
[0:05:20.179246561] [1091]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:265 No static properties available for 'arducam_64mp'
[0:05:20.179264321] [1091]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:267 Please consider updating the camera sensor properties database
[0:05:20.208772133] [1091]  INFO RPI pisp.cpp:1113 Registered camera /base/axi/p                                                                                                                                   cie@120000/rp1/i2c@88000/arducam_64mp@1a to CFE device /dev/media0 and ISP devic                                                                                                                                   e /dev/media2 using PiSP variant BCM2712_C0
[0:05:20.208854803] [1091]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a25                                                                                                                                   8644a 22-11-2023 (22:03:31)
[0:05:20.209826710] [1091]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:265 No static properties available for 'arducam_64mp'
[0:05:20.209843859] [1091]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:267 Please consider updating the camera sensor properties database
[0:05:20.227270061] [1091]  INFO RPI pisp.cpp:1113 Registered camera /base/axi/p                                                                                                                                   cie@120000/rp1/i2c@80000/arducam_64mp@1a to CFE device /dev/media1 and ISP devic                                                                                                                                   e /dev/media3 using PiSP variant BCM2712_C0
[0:05:20.228874734] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format Y16
[0:05:20.228911846] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format RGB6
[0:05:20.228921495] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format BGR6
[0:05:20.228931402] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format PC1M
Mode selection for 4576:3472:12:P
    SRGGB10_CSI2P,1280x720/0 - Score: 13268.4
    SRGGB10_CSI2P,1920x1080/0 - Score: 11268.4
    SRGGB10_CSI2P,2312x1736/0 - Score: 9005.18
    SRGGB10_CSI2P,3840x2160/0 - Score: 5268.43
    SRGGB10_CSI2P,4624x3472/0 - Score: 1017.18
    SRGGB10_CSI2P,8000x6000/0 - Score: 2493.76
    SRGGB10_CSI2P,9152x6944/0 - Score: 3012
Stream configuration adjusted
[0:05:20.229361826] [1079]  INFO Camera camera.cpp:1183 configuring streams: (0)                                                                                                                                    4576x3472-YUV420 (1) 4624x3472-RGGB16_PISP_COMP1
[0:05:20.229479960] [1091]  INFO RPI pisp.cpp:1397 Sensor: /base/axi/pcie@120000                                                                                                                                   /rp1/i2c@80000/arducam_64mp@1a - Selected sensor format: 4624x3472-SRGGB10_1X10                                                                                                                                    - Selected CFE format: 4624x3472-PC1R
[0:05:21.516236624] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format Y16
[0:05:21.516286348] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format RGB6
[0:05:21.516295830] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format BGR6
[0:05:21.516306294] [1079]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format PC1M
Mode selection for 9152:6944:12:P
    SRGGB10_CSI2P,1280x720/0 - Score: 29364.4
    SRGGB10_CSI2P,1920x1080/0 - Score: 27364.4
    SRGGB10_CSI2P,2312x1736/0 - Score: 25101.2
    SRGGB10_CSI2P,3840x2160/0 - Score: 21364.4
    SRGGB10_CSI2P,4624x3472/0 - Score: 17005.2
    SRGGB10_CSI2P,8000x6000/0 - Score: 5197.76
    SRGGB10_CSI2P,9152x6944/0 - Score: 1000
Stream configuration adjusted
[0:05:21.516770662] [1079]  INFO Camera camera.cpp:1183 configuring streams: (0)                                                                                                                                    9152x6944-BGR888 (1) 9152x6944-RGGB16_PISP_COMP1
[0:05:21.553124322] [1091]  INFO RPI pisp.cpp:1397 Sensor: /base/axi/pcie@120000                                                                                                                                   /rp1/i2c@80000/arducam_64mp@1a - Selected sensor format: 9152x6944-SRGGB10_1X10                                                                                                                                    - Selected CFE format: 9152x6944-PC1R
Still capture image received

pi@pi:~ $ libcamera-still --camera 0 --width 9152 --height 6944 -o test0.jpg -n                                                                                                                                    -t 1 --encoding rgb
[0:05:20.147690412] [1080]  INFO Camera camera_manager.cpp:284 libcamera v0.1.0+                                                                                                                                   146-cff216fa-dirty (2023-12-05T09:54:10+00:00)
[0:05:20.174573475] [1090]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a25                                                                                                                                   8644a 22-11-2023 (22:03:31)
[0:05:20.179246932] [1090]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:265 No static properties available for 'arducam_64mp'
[0:05:20.179264210] [1090]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:267 Please consider updating the camera sensor properties database
[0:05:20.208869396] [1090]  INFO RPI pisp.cpp:1113 Registered camera /base/axi/p                                                                                                                                   cie@120000/rp1/i2c@88000/arducam_64mp@1a to CFE device /dev/media0 and ISP devic                                                                                                                                   e /dev/media2 using PiSP variant BCM2712_C0
[0:05:20.209002438] [1090]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a25                                                                                                                                   8644a 22-11-2023 (22:03:31)
[0:05:20.209916973] [1090]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:265 No static properties available for 'arducam_64mp'
[0:05:20.209941474] [1090]  WARN CameraSensorProperties camera_sensor_properties                                                                                                                                   .cpp:267 Please consider updating the camera sensor properties database
[0:05:20.227270098] [1090]  INFO RPI pisp.cpp:1113 Registered camera /base/axi/p                                                                                                                                   cie@120000/rp1/i2c@80000/arducam_64mp@1a to CFE device /dev/media1 and ISP devic                                                                                                                                   e /dev/media3 using PiSP variant BCM2712_C0
[0:05:20.228876123] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format Y16
[0:05:20.228921069] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format RGB6
[0:05:20.228935143] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format BGR6
[0:05:20.228957366] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format PC1M
Mode selection for 4576:3472:12:P
    SRGGB10_CSI2P,1280x720/0 - Score: 13268.4
    SRGGB10_CSI2P,1920x1080/0 - Score: 11268.4
    SRGGB10_CSI2P,2312x1736/0 - Score: 9005.18
    SRGGB10_CSI2P,3840x2160/0 - Score: 5268.43
    SRGGB10_CSI2P,4624x3472/0 - Score: 1017.18
    SRGGB10_CSI2P,8000x6000/0 - Score: 2493.76
    SRGGB10_CSI2P,9152x6944/0 - Score: 3012
Stream configuration adjusted
[0:05:20.229547352] [1080]  INFO Camera camera.cpp:1183 configuring streams: (0)                                                                                                                                    4576x3472-YUV420 (1) 4624x3472-RGGB16_PISP_COMP1
[0:05:20.229807343] [1090]  INFO RPI pisp.cpp:1397 Sensor: /base/axi/pcie@120000                                                                                                                                   /rp1/i2c@88000/arducam_64mp@1a - Selected sensor format: 4624x3472-SRGGB10_1X10                                                                                                                                    - Selected CFE format: 4624x3472-PC1R
[0:05:21.501825562] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format Y16
[0:05:21.501889379] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format RGB6
[0:05:21.501898787] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format BGR6
[0:05:21.501913158] [1080]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2                                                                                                                                    pixel format PC1M
Mode selection for 9152:6944:12:P
    SRGGB10_CSI2P,1280x720/0 - Score: 29364.4
    SRGGB10_CSI2P,1920x1080/0 - Score: 27364.4
    SRGGB10_CSI2P,2312x1736/0 - Score: 25101.2
    SRGGB10_CSI2P,3840x2160/0 - Score: 21364.4
    SRGGB10_CSI2P,4624x3472/0 - Score: 17005.2
    SRGGB10_CSI2P,8000x6000/0 - Score: 5197.76
    SRGGB10_CSI2P,9152x6944/0 - Score: 1000
Stream configuration adjusted
[0:05:21.502521606] [1080]  INFO Camera camera.cpp:1183 configuring streams: (0)                                                                                                                                    9152x6944-BGR888 (1) 9152x6944-RGGB16_PISP_COMP1
[0:05:21.517791033] [1090]  INFO RPI pisp.cpp:1397 Sensor: /base/axi/pcie@120000                                                                                                                                   /rp1/i2c@88000/arducam_64mp@1a - Selected sensor format: 9152x6944-SRGGB10_1X10                                                                                                                                    - Selected CFE format: 9152x6944-PC1R
[0:05:22.949701675] [1090] ERROR V4L2 v4l2_videodevice.cpp:1697 /dev/video21[25:                                                                                                                                   out]: Failed to queue buffer 0: Invalid argument
[0:05:22.949775196] [1090] ERROR RPISTREAM rpi_stream.cpp:276 Failed to queue bu                                                                                                                                   ffer for ISP TDN Input
[0:05:22.949815364] [1090] ERROR V4L2 v4l2_videodevice.cpp:1697 /dev/video26[26:                                                                                                                                   cap]: Failed to queue buffer 0: Invalid argument
[0:05:22.949826531] [1090] ERROR RPISTREAM rpi_stream.cpp:276 Failed to queue bu                                                                                                                                   ffer for ISP TDN Output
[0:05:22.949861143] [1090] ERROR V4L2 v4l2_videodevice.cpp:1697 /dev/video22[27:                                                                                                                                   out]: Failed to queue buffer 0: Invalid argument
[0:05:22.949872551] [1090] ERROR RPISTREAM rpi_stream.cpp:276 Failed to queue bu                                                                                                                                   ffer for ISP Stitch Input
[0:05:22.949905089] [1090] ERROR V4L2 v4l2_videodevice.cpp:1697 /dev/video27[28:                                                                                                                                   cap]: Failed to queue buffer 0: Invalid argument
[0:05:22.949914923] [1090] ERROR RPISTREAM rpi_stream.cpp:276 Failed to queue bu                                                                                                                                   ffer for ISP Stitch Output
                                                                                                                                   

looking at log of last camera there is
[0:05:20.229547352] [1080] INFO Camera camera.cpp:1183 configuring streams: (0) 4576x3472-YUV420 (1) 4624x3472-RGGB16_PISP_COMP1

[0:05:21.502521606] [1080] INFO Camera camera.cpp:1183 configuring streams: (0) 9152x6944-BGR888 (1) 9152x6944-RGGB16_PISP_COMP1

Like why is it creating 4576x3472-YUV420, 4624x3472-RGGB16_PISP_COMP1, 9152x6944-RGGB16_PISP_COMP1 streams if im telling it to use res 9152x6944 and encoding rgb? Is it creating buffers for these also?

I think the CMA still isnt enough to create all these buffers since last address system reserves for cma is still withgin 1024M because the ZONE_DMA is limited to 30-bit on raspberry pi. The logs above i got when cma=960M48M


What i think might be happening lets say libcamera is creating 2 x (9152x6944 rgb888) buffer for each camera thats already 4 buffers and in my code i also make 1 buffer for each camera and that means 6 rgb buffers each requiring 192MB. 6x192MB=1152MB that is more then CMA can be give in 30-bit address space. whe i set CMA to exceed this address space cma=3072MB@48M then i get different error in second camera.

pi@pi:~ $ libcamera-still --camera 0 --width 8000 --height 6000 -o test0.jpg -n -t 1 --encoding rgb
[0:07:42.936795892] [1283]  INFO Camera camera_manager.cpp:284 libcamera v0.1.0+146-cff216fa-dirty (2023-12-05T09:54:10+00:00)
[0:07:42.961703713] [1293]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (22:03:31)
[0:07:42.962912846] [1293]  WARN CameraSensorProperties camera_sensor_properties.cpp:265 No static properties available for 'arducam_64mp'
[0:07:42.962928661] [1293]  WARN CameraSensorProperties camera_sensor_properties.cpp:267 Please consider updating the camera sensor properties database
[0:07:42.983811724] [1293]  INFO RPI pisp.cpp:1113 Registered camera /base/axi/pcie@120000/rp1/i2c@88000/arducam_64mp@1a to CFE device /dev/media0 and ISP device /dev/media3 using PiSP variant BCM2712_C0
[0:07:42.983888615] [1293]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (22:03:31)
[0:07:43.241684169] [1293] ERROR V4L2 v4l2_device.cpp:353 'arducam_64mp 4-001a': Unable to set controls: Device or resource busy
[0:07:43.241992804] [1293]  WARN CameraSensorProperties camera_sensor_properties.cpp:265 No static properties available for 'arducam_64mp'
[0:07:43.242005378] [1293]  WARN CameraSensorProperties camera_sensor_properties.cpp:267 Please consider updating the camera sensor properties database
[0:07:43.255837203] [1293]  INFO RPI pisp.cpp:1113 Registered camera /base/axi/pcie@120000/rp1/i2c@80000/arducam_64mp@1a to CFE device /dev/media1 and ISP device /dev/media4 using PiSP variant BCM2712_C0
[0:07:43.256455417] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format Y16
[0:07:43.256500696] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format RGB6
[0:07:43.256516678] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format BGR6
[0:07:43.256532326] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format PC1M
Mode selection for 4624:3468:12:P
    SRGGB10_CSI2P,1280x720/0 - Score: 13350.7
    SRGGB10_CSI2P,1920x1080/0 - Score: 11350.7
    SRGGB10_CSI2P,2312x1736/0 - Score: 9092.61
    SRGGB10_CSI2P,3840x2160/0 - Score: 5350.67
    SRGGB10_CSI2P,4624x3472/0 - Score: 1005.61
    SRGGB10_CSI2P,8000x6000/0 - Score: 2477
    SRGGB10_CSI2P,9152x6944/0 - Score: 3047.08
Stream configuration adjusted
[0:07:43.257506844] [1283]  INFO Camera camera.cpp:1183 configuring streams: (0) 4624x3468-YUV420 (1) 4624x3472-RGGB16_PISP_COMP1
[0:07:43.257649661] [1293]  INFO RPI pisp.cpp:1397 Sensor: /base/axi/pcie@120000/rp1/i2c@88000/arducam_64mp@1a - Selected sensor format: 4624x3472-SRGGB10_1X10 - Selected CFE format: 4624x3472-PC1R
[0:07:44.517548256] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format Y16
[0:07:44.517597424] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format RGB6
[0:07:44.517607516] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format BGR6
[0:07:44.517618239] [1283]  WARN V4L2 v4l2_pixelformat.cpp:338 Unsupported V4L2 pixel format PC1M
Mode selection for 8000:6000:12:P
    SRGGB10_CSI2P,1280x720/0 - Score: 25166.7
    SRGGB10_CSI2P,1920x1080/0 - Score: 23166.7
    SRGGB10_CSI2P,2312x1736/0 - Score: 20908.6
    SRGGB10_CSI2P,3840x2160/0 - Score: 17166.7
    SRGGB10_CSI2P,4624x3472/0 - Score: 12812.6
    SRGGB10_CSI2P,8000x6000/0 - Score: 1000
    SRGGB10_CSI2P,9152x6944/0 - Score: 1570.08
Stream configuration adjusted
[0:07:44.518218639] [1283]  INFO Camera camera.cpp:1183 configuring streams: (0) 8000x6000-BGR888 (1) 8000x6000-RGGB16_PISP_COMP1
[0:07:44.532071021] [1293]  INFO RPI pisp.cpp:1397 Sensor: /base/axi/pcie@120000/rp1/i2c@88000/arducam_64mp@1a - Selected sensor format: 8000x6000-SRGGB10_1X10 - Selected CFE format: 8000x6000-PC1R
[0:07:44.950899674] [1293] ERROR V4L2 v4l2_videodevice.cpp:1697 /dev/video25[23:cap]: Failed to queue buffer 0: Invalid argument
[0:07:44.950932138] [1293] ERROR RPISTREAM rpi_stream.cpp:276 Failed to queue buffer for ISP Output1
ERROR: Device timeout detected, attempting a restart!!!
[0:07:44.953530037] [1293] FATAL default v4l2_videodevice.cpp:1960 /dev/video25[23:cap]: assertion "cache_->isEmpty()" failed in streamOff()
Backtrace:
??? [0xfffffffff782ca44] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff782ca44])
??? [0xfffffffff7851bc0] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff7851bc0])
??? [0xfffffffff78015b4] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff78015b4])
??? [0xfffffffff734cdc0] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff734cdc0])
??? [0xfffffffff734f564] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff734f564])
??? [0xfffffffff73457f0] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff73457f0])
??? [0xfffffffff734eee4] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff734eee4])
??? [0xfffffffff77d2600] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff77d2600])
??? [0xfffffffff75be068] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff75be068])
??? [0xfffffffff73d18e0] (/usr/lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 [0xfffffffff73d18e0])

Through trial and error i figured out that maximum res i can give is 7445x6000 with BGR888 for parallel capture of two cameras

libcamera-still --camera 0 --width 7445 --height 6000 -o test0.jpg -n -t 1 --encoding rgb
libcamera-still --camera 1 --width 7445 --height 6000 -o test1.jpg -n -t 1 --encoding rgb

increasing the width to 7446 on both commands will create the same error in second camera log.

Is it possible for you to fix arducam-libcamera so that it wouldnt eat so much CMA and i could capture bgr888 at 9152x6944?

An idea popped in my head.
Lets say 1 buffer that driver or libcamera makes somewhere for each camera in and another 2 buffers the that are made in the user application. so 4 buffers in total
4xrgb7445x6000 is ~511.20MB … dual capture works
4xrgb7446x6000 is ~511.28MB … dual capture does not work anymore

as in the docs it is specified that when configuring CMA from /boot/config.txt the largest option is
dtoverlay=vc4-kms-v3d,cma-512

and 511.28MB is strangely close to 512

@Dion eventhough im able to increase CMA up to 972MB on rpi os lite with cma=972M@48M in the /boot/cmdline.txt, is it possible that arducam-libcamera or driver can still only use up to 512MB CMA? how to confirm or deny that? and what could be the cause of this?

libcamera-still --camera 0 --width 9152 --height 6944 -o test0.jpg -n -t 1 --encoding rgb
wait ~1.1…2 seconds
libcamera-still --camera 1 --width 9152 --height 6944 -o test1.jpg -n -t 1 --encoding rgb

it works!

1 Like

hey Henri,

I am having a similar issue with taking shots at the same time.

Just to confirm your findings:

  1. There is not enough memory to do 2 x 64 MP
  2. Solution is to take photos separately (1-2s gap)?
[2024-05-21 01:34:10,553: INFO/MainProcess] picam.tasks.take_photo[d2df0f60-bae2-46cd-b5cd-490863e1d4ba]: [35:31:26.986390843] [73820] e[1;32m INFO e[1;37mCamera e[1;34mcamera_manager.cpp:284 e[0mlibcamera v0.2.0+120-eb00c13d
[35:31:27.001053462] [73826] e[1;32m INFO e[1;37mRPI e[1;34mpisp.cpp:695 e[0mlibpisp version v1.0.5 999da5acb4f4 17-04-2024 (14:29:29)
[35:31:27.012116434] [73826] e[1;32m INFO e[1;37mRPI e[1;34mpisp.cpp:1154 e[0mRegistered camera /base/axi/pcie@120000/rp1/i2c@88000/ov64a40@36 to CFE device /dev/media0 and ISP device /dev/media2 using PiSP variant BCM2712_C0
[35:31:27.012190716] [73826] e[1;32m INFO e[1;37mRPI e[1;34mpisp.cpp:695 e[0mlibpisp version v1.0.5 999da5acb4f4 17-04-2024 (14:29:29)
[35:31:27.029294466] [73826] e[1;32m INFO e[1;37mRPI e[1;34mpisp.cpp:1154 e[0mRegistered camera /base/axi/pcie@120000/rp1/i2c@80000/ov64a40@36 to CFE device /dev/media1 and ISP device /dev/media3 using PiSP variant BCM2712_C0
[35:31:27.029842787] [73820] e[1;33m WARN e[1;37mV4L2 e[1;34mv4l2_pixelformat.cpp:344 e[0mUnsupported V4L2 pixel format RPBP
Mode selection for 4624:3472:12:P
    SBGGR10_CSI2P,1920x1080/0 - Score: 11359.2
    SBGGR10_CSI2P,2312x1736/0 - Score: 9096
    SBGGR10_CSI2P,3840x2160/0 - Score: 5359.24
    SBGGR10_CSI2P,4624x3472/0 - Score: 1000
    SBGGR10_CSI2P,8000x6000/0 - Score: 2476.58
    SBGGR10_CSI2P,9248x6944/0 - Score: 3024
Stream configuration adjusted
[35:31:27.030235713] [73820] e[1;32m INFO e[1;37mCamera e[1;34mcamera.cpp:1183 e[0mconfiguring streams: (0) 4624x3472-YUV420 (1) 4624x3472-BGGR_PISP_COMP1
[35:31:27.030334810] [73826] e[1;32m INFO e[1;37mRPI e[1;34mpisp.cpp:1450 e[0mSensor: /base/axi/pcie@120000/rp1/i2c@80000/ov64a40@36 - Selected sensor format: 4624x3472-SBGGR10_1X10 - Selected CFE format: 4624x3472-PC1B
[35:31:34.108684050] [73820] e[1;33m WARN e[1;37mV4L2 e[1;34mv4l2_pixelformat.cpp:344 e[0mUnsupported V4L2 pixel format RPBP
Mode selection for 9248:6944:12:P
    SBGGR10_CSI2P,1920x1080/0 - Score: 27551.2
    SBGGR10_CSI2P,2312x1736/0 - Score: 25288
    SBGGR10_CSI2P,3840x2160/0 - Score: 21551.2
    SBGGR10_CSI2P,4624x3472/0 - Score: 17192
    SBGGR10_CSI2P,8000x6000/0 - Score: 5384.58
    SBGGR10_CSI2P,9248x6944/0 - Score: 1000
Stream configuration adjusted
[35:31:34.109202648] [73820] e[1;32m INFO e[1;37mCamera e[1;34mcamera.cpp:1183 e[0mconfiguring streams: (0) 9248x6944-YUV420 (1) 9248x6944-BGGR_PISP_COMP1
[35:31:34.132700072] [73826] e[1;32m INFO e[1;37mRPI e[1;34mpisp.cpp:1450 e[0mSensor: /base/axi/pcie@120000/rp1/i2c@80000/ov64a40@36 - Selected sensor format: 9248x6944-SBGGR10_1X10 - Selected CFE format: 9248x6944-PC1B
[35:31:37.098461061] [73826] e[1;31mERROR e[1;37mV4L2 e[1;34mv4l2_videodevice.cpp:1697 e[0;32m/dev/video36[46:cap]: e[0mFailed to queue buffer 0: Invalid argument
[35:31:37.098491136] [73826] e[1;31mERROR e[1;37mRPISTREAM e[1;34mrpi_stream.cpp:276 e[0mFailed to queue buffer for ISP Stitch Output
[35:31:37.173005024] [73826] e[1;35mFATAL e[1;37mdefault e[1;34mv4l2_videodevice.cpp:1960 e[0;32m/dev/video36[46:cap]: e[0massertion "cache_->isEmpty()" failed in streamOff()
Backtrace:
libcamera::V4L2VideoDevice::streamOff()+0x19c (/usr/lib/aarch64-linux-gnu/libcamera.so.0.2.0 [0x00007fff2ec06cfc])
libcamera::RPi::PipelineHandlerBase::stopDevice(libcamera::Camera*)+0x44 (/usr/lib/aarch64-linux-gnu/libcamera.so.0.2.0 [0x00007fff2ec2fa24])
libcamera::PipelineHandler::stop(libcamera::Camera*)+0x40 (/usr/lib/aarch64-linux-gnu/libcamera.so.0.2.0 [0x00007fff2ebdc894])
libcamera::Object::message(libcamera::Message*)+0x78 (/usr/lib/aarch64-linux-gnu/libcamera-base.so.0.2.0 [0x00007fff2e6187a8])
libcamera::Thread::dispatchMessages(libcamera::Message::Type)+0x1c4 (/usr/lib/aarch64-linux-gnu/libcamera-base.so.0.2.0 [0x00007fff2e61adc8])
libcamera::EventDispatcherPoll::processEvents()+0x44 (/usr/lib/aarch64-linux-gnu/libcamera-base.so.0.2.0 [0x00007fff2e611a14])
libcamera::Thread::exec()+0x74 (/usr/lib/aarch64-linux-gnu/libcamera-base.so.0.2.0 [0x00007fff2e61a7b4])
libcamera::CameraManager::Private::run()+0xac (/usr/lib/aarch64-linux-gnu/libcamera.so.0.2.0 [0x00007fff2ebb81dc])
??? [0x00007fff2e91e9dc] (/usr/lib/aarch64-linux-gnu/libstdc++.so.6.0.30 [0x00007fff2e91e9dc])
start_thread+0x2b8 (./nptl/pthread_create.c:442)
thread_start+0xc (../sysdeps/unix/sysv/linux/aarch64/clone.S:82)

yes, or use yuv420 encoding then u can capture 2 images at the same time at max res

Thanks Henri.

What is the difference between YUV420 encoding vs just the default libcamera-still ?

I am saving all images as JPEG, is there a better / more efficient way than just using libcamera-still?

God created man so he would have somebody to talk to, then man killed god. 2000 years later man created ChatGPT so he would have someone to talk to…

YUV420 and RGB888 are two different color encoding formats used in image and video processing, and they have distinct characteristics and applications. Here’s a detailed comparison of the two:

YUV420 (also known as YUV 4:2:0)

  1. Color Model: YUV420 is based on the YUV color model, which separates the image into one luminance component (Y) and two chrominance components (U and V).
  2. Subsampling: The “420” indicates chroma subsampling. Specifically, it means that for every 4x2 block of pixels:
  • There are 4 Y samples (one for each pixel),
  • There is 1 U sample and 1 V sample (both shared by all 4 pixels).This reduces the amount of data required to represent the chrominance components by a factor of 4 compared to the full resolution.
  1. Data Size: YUV420 is more data-efficient than RGB888. Because of the subsampling, it requires less bandwidth and storage space.
  2. Applications: YUV420 is commonly used in video compression and broadcasting. It is particularly effective for reducing the size of video data while maintaining acceptable visual quality.
  3. Quality: YUV420 can exhibit color artifacts, especially in areas of high chrominance detail, due to the chroma subsampling.

RGB888

  1. Color Model: RGB888 uses the RGB color model, which directly represents colors as combinations of Red, Green, and Blue components.
  2. Sampling: Each color channel (R, G, B) is sampled at full resolution with 8 bits per channel. Hence, “888” means:
  • 8 bits for Red,
  • 8 bits for Green,
  • 8 bits for Blue,totaling 24 bits per pixel.
  1. Data Size: RGB888 requires more bandwidth and storage space compared to YUV420 because there is no subsampling. Each pixel is represented with full color information.
  2. Applications: RGB888 is often used in graphics applications, computer monitors, and any scenario where high color fidelity is critical. It is also used in many image formats (like BMP and PNG) and in uncompressed video.
  3. Quality: RGB888 provides high color fidelity and precision without the artifacts introduced by chroma subsampling. It is ideal for scenarios where maintaining the highest possible image quality is essential.

Summary

  • YUV420 is more efficient in terms of data size, making it suitable for video compression and streaming where bandwidth is a concern. It uses chroma subsampling, which can introduce some color artifacts but is generally acceptable for most video content.
  • RGB888 provides higher color fidelity and is used where quality is paramount, such as in graphics applications and image processing, but requires more storage and bandwidth due to the lack of subsampling.

Choosing between these formats depends on the specific requirements of the application, such as the need for compression efficiency versus the need for high color accuracy.

2 Likes