You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use RGB24 format with TC358743 instead of UYVY, and noticed that it produces BGR sequence in the raw data instead of RGB. I used the blue fullscreen image as an HDMI source and made a stream dump that confirms this, getting something like [255, 0, 0] for first 3 bytes.
If you have a top secret datasheet under Toshiba NDA, you can check pages 36/261 and 37/261. This confirms that UYVY is implemented correctly, but RGB has the reverse sequence. Apparently, this bug went unnoticed because everyone was using UYVY. I need RGB to output the image in DRM and encode in M2M at the same time.
Steps to reproduce the behaviour
Use any capture software with TC358743 in RGB24 format and dump the raw image.
Device (s)
Raspberry Pi CM4, Raspberry Pi CM4 Lite
System
Arch Linux ARM, but it doesn't matter
version c14213886ae1e31951022b74ce776ce7ee7023ee (clean) (release) (start)
Linux pikvm 6.6.21-4-rpi #1 SMP Wed Mar 13 20:36:42 UTC 2024 armv7l GNU/Linux
Apparently, the problem is relevant for any version of kernel.
Logs
No response
Additional context
I found this bug when encoding JPEG in M2M and using libjpeg. It's the same with M2M H.264. Interestingly, because of the endianness, DRM_FORMAT_RGB888 is actually BGR, so the rendering of the TC* chip output in DRM was correct colors, while the encoding was not.
I don't know if it is possible to fix this with little blood without loss of performance, given that it looks like you need to use the reverse order to put bytes in memory after receiving from CSI. This is not a big problem and I can make a userspace workaround for this, but it would probably be better to fix it upstream if the fix is not complex or fatal to performance.
Life with component ordering is always complicated.
tc358743 is an upstream driver with relatively minimal changes. It was written by Cisco for some of their video conferencing kit. IIRC it was against an ST SoC, but it may have been TI.
They've defined that the media bus code is MEDIA_BUS_FMT_RGB888_1X24
Checking the definition for MEDIA_BUS_FMT_RGB888_1X24, it is putting B in the LSBs of the bus. CSI-2 does send the data LSB and blue first, so would make sense as a mapping,
As a cause for further confusion, the CSI-2 spec (I'm looking at v1.01.00 r0.04 2-Apr-2009) does include 12.2 RGB888 Data Reception
The RGB888 data format byte to 32-bit memory word mapping follows the generic CSI-2 rule.
Figure 110 that follows writes B0 into the LSB of a 32bit word.
Section 12.7 YUV422 8-bit Data Reception and figure 115 that follows says that format does NOT follow the generic CSI-2 rule, and whilst U1 is sent first on the wire, it is written into the MSB of a 32bit word.
However the whole of section 12 is marked as informative.
I can't see anything in the Unicam docs that imply it does implement the generic CSI-2 rule. I don't see why it would ever be implemented on a big endian system such as ARM, as it means that the byte order is totally messed if wanting to access individual components as bytes (memory order would be B2 R1 G1 B1 G3 B3 R2 G2!)
Looking at how MEDIA_BUS_FMT_RGB888_1X24 should map to a V4L2_PIX_FMT_xx define, comparing against V4L2_PIX_FMT_SRGGB16 and MEDIA_BUS_FMT_SRGGB16_1X16, I can see an argument that MEDIA_BUS_FMT_RGB888_1X24 should be V4L2_PIX_FMT_BGR24.
TC358743 is the only vaguely supported device on the Pi using the format (we don't support OV5640, ADV7511, ADV7605, or ADV7842), so swapping them would be of limited risk, but needs a chunk more studying. I don't see an equivalent in any other driver which would have given me more confidence in the change.
Thanks for researching this. It looks like total hell. I believe CISCO can use byte swapping in its userspace (or a format overriding, whatever), and it looks like this is the path I should follow right now to avoid headache.
Silly question I try to pipe a signal (HDMI. RGB) through the pi, TC358743 input and PI HDMI output the tricks with capssetter and bgr and rgb swap works as long I didn't scale, for example 1080p or 1080i to 1080p or 1080i works flawless but if I scale from 720 or to 720 and use videoconvert and videoscale the colorshift with capssetter did not work anymore can someone help me with that?
Seeing as there are several drivers that use RGB888_1X24, it would be easier to fix it in the Unicam driver (just not in the way that the first patch proposed). We can be a little easier going there as the Unicam driver has only just been merged to mainline, so in their view there are no/very few users of it.
If mainline did adopt that, then for downstream I'd be tempted to put in the same mapping twice, so both BGR888_1X24 and RGB888_1X24 map to V4L2_PIX_FMT_BGR24. The handling is generally dodgy, and the CSI2 spec specifies the layout that is V4L2_PIX_FMT_BGR24. If your random source device produces RGB24 then it's non-CSI2 compliant and shouldn't be using that CSI2 data ID.
Silly question I try to pipe a signal (HDMI. RGB) through the pi, TC358743 input and PI HDMI output the tricks with capssetter and bgr and rgb swap works as long I didn't scale, for example 1080p or 1080i to 1080p or 1080i works flawless but if I scale from 720 or to 720 and use videoconvert and videoscale the colorshift with capssetter did not work anymore can someone help me with that?
What's your full pipeline that you're trying to run? And which version of GStreamer?
Please don't do that, or at least provide a link between the two. Otherwise you just waste the time of those who might help - I wouldn't have needed to ask the above question as you've given the details in that thread.
Sorry for that. On the forum I hat to wait as it is moderated so I can't get the link to it instant and I wasn't near any device when it comes online so I hadn't adopt it in this issue in proper time.
If mainline did adopt that, then for downstream I'd be tempted to put in the same mapping twice, so both BGR888_1X24 and RGB888_1X24 map to V4L2_PIX_FMT_BGR24. The handling is generally dodgy, and the CSI2 spec specifies the layout that is V4L2_PIX_FMT_BGR24. If your random source device produces RGB24 then it's non-CSI2 compliant and shouldn't be using that CSI2 data ID.
Since I have no clue, so asking cautious.
from the link to the first patch, BGR and RGB both use MIPI_CSI2_DT_RGB888 is this correct?
FWIW there are a couple of threads upstream, but nothing resolved yet.
Hello, also experiencing this color-ordering issue with TC358743, anything resolved yet? In the meantime, is it possible to switch to UYVY on Pi5 without patching the driver?
Yes the big no-no @6by9 was talking about it changed the MBUS format the TC358743 used only changed the tc358743.c, I wrote a little script that puts everyting in the right order
#!/bin/bash
sudo apt install -y bc bison flex libssl-dev make git
git clone --depth=1 https://github.com/raspberrypi/linux
cd linux
sed -i 's#MEDIA_BUS_FMT_RGB888_1X24#MEDIA_BUS_FMT_BGR888_1X24#g' $HOME/linux/drivers/media/i2c/tc358743.c
KERNEL=kernel_2712
make bcm2712_defconfig
make -j6 Image.gz modules dtbs
make -j6 Image modules dtbs
sudo make -j6 modules_install
sudo cp /boot/firmware/$KERNEL.img /boot/firmware/$KERNEL-backup.img
sudo cp arch/arm64/boot/Image.gz /boot/firmware/$KERNEL.img
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/firmware/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/firmware/overlays/
sudo cp arch/arm64/boot/dts/overlays/README /boot/firmware/overlays/
It changed RGB888 to BGR888 inside the tc358743.c so it used the MBUS format BGR888 and then it compiles the kernel for the Pi5 and replace the existing one.
FWIW there are a couple of threads upstream, but nothing resolved yet.
Hello, also experiencing this color-ordering issue with TC358743, anything resolved yet? In the meantime, is it possible to switch to UYVY on Pi5 without patching the driver?
#7070 has cherry-picked the two patches that directly relate to this issue.
I haven't tested whether it works or not, but should advertise both RGB888 and BGR888 just to give backwards compatibility to apps that have RGB888_1X24 hard coded. It will WARN_ONCE if you use RGB888, and (as always) will deliver BGR888.
As a long-term fan of 6by9's work helping half of the internet to get HDMI-inputs into raspberries I'd happily like to confirm that the upstream patch #7070 fixes the "BGR is interpreted as RGB"-error!
It was commited for kernel version 6.12.52. When checking out kernel-sources yesterday, I got v6.12.53. After using the new kernel, it needed just a media-ctl initalization with format BGR888_1X24 (instead of RGB888_1X24) and the usage of format=BGR (instead of format=RGB) in gstreamer's caps.
[Tested on Raspberry Pi 5 with Rasperry Pi OS, release Debian GNU/Linux 13 (trixie).]
Activity
6by9 commentedon Mar 26, 2024
Life with component ordering is always complicated.
tc358743 is an upstream driver with relatively minimal changes. It was written by Cisco for some of their video conferencing kit. IIRC it was against an ST SoC, but it may have been TI.
They've defined that the media bus code is MEDIA_BUS_FMT_RGB888_1X24
Checking the definition for MEDIA_BUS_FMT_RGB888_1X24, it is putting B in the LSBs of the bus. CSI-2 does send the data LSB and blue first, so would make sense as a mapping,
As a cause for further confusion, the CSI-2 spec (I'm looking at v1.01.00 r0.04 2-Apr-2009) does include 12.2 RGB888 Data Reception
Figure 110 that follows writes B0 into the LSB of a 32bit word.
Section 12.7 YUV422 8-bit Data Reception and figure 115 that follows says that format does NOT follow the generic CSI-2 rule, and whilst U1 is sent first on the wire, it is written into the MSB of a 32bit word.
However the whole of section 12 is marked as informative.
I can't see anything in the Unicam docs that imply it does implement the generic CSI-2 rule. I don't see why it would ever be implemented on a big endian system such as ARM, as it means that the byte order is totally messed if wanting to access individual components as bytes (memory order would be
B2 R1 G1 B1 G3 B3 R2 G2!)Looking at how MEDIA_BUS_FMT_RGB888_1X24 should map to a V4L2_PIX_FMT_xx define, comparing against V4L2_PIX_FMT_SRGGB16 and MEDIA_BUS_FMT_SRGGB16_1X16, I can see an argument that MEDIA_BUS_FMT_RGB888_1X24 should be V4L2_PIX_FMT_BGR24.
TC358743 is the only vaguely supported device on the Pi using the format (we don't support OV5640, ADV7511, ADV7605, or ADV7842), so swapping them would be of limited risk, but needs a chunk more studying. I don't see an equivalent in any other driver which would have given me more confidence in the change.
6by9 commentedon Mar 26, 2024
And it wasn't unnoticed - there are various forum threads where GStreamer's capssetter is being used to swap bgr to rgb when using tc358743.
mdevaev commentedon Mar 26, 2024
Thanks for researching this. It looks like total hell. I believe CISCO can use byte swapping in its userspace (or a format overriding, whatever), and it looks like this is the path I should follow right now to avoid headache.
Userspace workaround for the wrong TC358743 RGB bytes ordering
video_stream: add RGB<->BGR swapping support
video_stream: add RGB<->BGR swapping support
MarkusLange commentedon Jun 27, 2025
Silly question I try to pipe a signal (HDMI. RGB) through the pi, TC358743 input and PI HDMI output the tricks with capssetter and bgr and rgb swap works as long I didn't scale, for example 1080p or 1080i to 1080p or 1080i works flawless but if I scale from 720 or to 720 and use videoconvert and videoscale the colorshift with capssetter did not work anymore can someone help me with that?
6by9 commentedon Jun 30, 2025
FWIW there are a couple of threads upstream, but nothing resolved yet.
https://lore.kernel.org/linux-media/20250606-rpi-unicam-rgb-bgr-fix-v1-1-9930b963f3eb@kernel.org/ - expected that MEDIA_BUS_FMT_BGR888_1X24 is used.
https://lore.kernel.org/linux-media/20250612-csi-bgr-rgb-v1-3-dc8a309118f8@kernel.org/ - changing to use that MBUS code, however it will break any existing userspace app that currently select MEDIA_BUS_FMT_RGB888_1X24 in their Media Controller setup (eg TC358743 on Pi5 I expect, so likely PiKVM). Userspace regressions are a big no-no.
Seeing as there are several drivers that use RGB888_1X24, it would be easier to fix it in the Unicam driver (just not in the way that the first patch proposed). We can be a little easier going there as the Unicam driver has only just been merged to mainline, so in their view there are no/very few users of it.
If mainline did adopt that, then for downstream I'd be tempted to put in the same mapping twice, so both BGR888_1X24 and RGB888_1X24 map to V4L2_PIX_FMT_BGR24. The handling is generally dodgy, and the CSI2 spec specifies the layout that is V4L2_PIX_FMT_BGR24. If your random source device produces RGB24 then it's non-CSI2 compliant and shouldn't be using that CSI2 data ID.
6by9 commentedon Jun 30, 2025
What's your full pipeline that you're trying to run? And which version of GStreamer?
Actually I see you've posted on the forums too - https://forums.raspberrypi.com/viewtopic.php?t=389417
Please don't do that, or at least provide a link between the two. Otherwise you just waste the time of those who might help - I wouldn't have needed to ask the above question as you've given the details in that thread.
MarkusLange commentedon Jul 2, 2025
Sorry for that. On the forum I hat to wait as it is moderated so I can't get the link to it instant and I wasn't near any device when it comes online so I hadn't adopt it in this issue in proper time.
MarkusLange commentedon Aug 29, 2025
Since I have no clue, so asking cautious.
from the link to the first patch, BGR and RGB both use MIPI_CSI2_DT_RGB888 is this correct?
superware commentedon Sep 26, 2025
Hello, also experiencing this color-ordering issue with TC358743, anything resolved yet? In the meantime, is it possible to switch to UYVY on Pi5 without patching the driver?
mdevaev commentedon Sep 26, 2025
@superware I just use a userspace workaround: pikvm/ustreamer@7015a26
MarkusLange commentedon Sep 28, 2025
@superware I used this:
Yes the big no-no @6by9 was talking about it changed the MBUS format the TC358743 used only changed the tc358743.c, I wrote a little script that puts everyting in the right order
It changed RGB888 to BGR888 inside the tc358743.c so it used the MBUS format BGR888 and then it compiles the kernel for the Pi5 and replace the existing one.
6by9 commentedon Sep 29, 2025
The latest upstream patch set at https://lore.kernel.org/linux-media/20250917-csi-bgr-rgb-v3-0-0145571b3aa4@kernel.org/ should do the trick.
I thought I'd sent a Reviewed-by tag, but it looks like I hadn't. I'm tempted to backport it as it is anyway.
6by9 commentedon Sep 29, 2025
#7070 has cherry-picked the two patches that directly relate to this issue.
I haven't tested whether it works or not, but should advertise both RGB888 and BGR888 just to give backwards compatibility to apps that have
RGB888_1X24hard coded. It will WARN_ONCE if you use RGB888, and (as always) will deliver BGR888.wetterfrosch commentedon Oct 20, 2025
As a long-term fan of 6by9's work helping half of the internet to get HDMI-inputs into raspberries I'd happily like to confirm that the upstream patch #7070 fixes the "BGR is interpreted as RGB"-error!
It was commited for kernel version 6.12.52. When checking out kernel-sources yesterday, I got v6.12.53. After using the new kernel, it needed just a media-ctl initalization with format
BGR888_1X24(instead ofRGB888_1X24) and the usage offormat=BGR(instead offormat=RGB) in gstreamer's caps.[Tested on Raspberry Pi 5 with Rasperry Pi OS, release Debian GNU/Linux 13 (trixie).]
Pics, or it didn't happen, before:

Aaand after:

Sending virtual 🌻, 🍫 and 🧀 to @6by9!
mdevaev commentedon Nov 7, 2025
I'm fine with this too. Thank you for your work @6by9. Let's close it and mark solved.