Skip to content
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

Add support for YU12 #281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add support for YU12 #281

wants to merge 2 commits into from

Conversation

fcharron
Copy link

Inspired by https://github.com/pranjalv123/mjpg-streamer-yu12, I added YU12 (Planar YUV420) support.

More info about the YU12 format at https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/pixfmt-yuv420.html.

In summary, all Y's of the frame are located at the beginning of the buffer, followed by all U's, then all V's. Besides that special buffer format, the configuration (number and display location of Y, U and V components) is exactly the same as regular YUV.

Tested on latest official Octopi (https://github.com/guysoft/OctoPi)

@jacksonliam
Copy link
Owner

Thanks for this! I don't have any hardware I can test it with but doesn't look like it should affect anything else so will get this merged in when I get a minute!

@juanesf
Copy link

juanesf commented May 3, 2021

This probably works for ov5640 in allwinner

@jquievreux
Copy link

@fcharron work well thank you and @jacksonliam thank you too

@ssotangkur
Copy link

Can we get this merged?

@beckmx
Copy link

beckmx commented Jan 10, 2023

@fcharron hello there, I want to add nv12 4:2:0 support, I noticed with this you add yuv12, I was wondering if with your PR the nv12 can be interpreted or do I need additional conversion?

@fcharron
Copy link
Author

@fcharron hello there, I want to add nv12 4:2:0 support, I noticed with this you add yuv12, I was wondering if with your PR the nv12 can be interpreted or do I need additional conversion?

Hi @beckmx, while YV12/YU12 (https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/pixfmt-yuv420.html) and NV12/NV21 (https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/pixfmt-nv12.html) contain the exact same data, it is not organized in the same order in memory. As such, you would need an additional conversion to go from one to the other, but it should be a pretty simple one.

@fcharron
Copy link
Author

fcharron commented Jan 10, 2023

Can we get this merged?

@ssotangkur I don't see any problem with it. I figure if anyone else confirms it works as expected, the project owner would be satisfied that it works fine. Seems like a couple of people already did confirm it worked... @jacksonliam ?

@beckmx
Copy link

beckmx commented Jan 11, 2023

@fcharron to be honest with u, I read the docs and also understood the same, but I am not that good on these topics

@beckmx
Copy link

beckmx commented Jan 12, 2023

@fcharron I forked from your repo and added nv12, it worked, or kinda, if you have sometime to checkout, looks like the color of the image looks pink 🥲

@PyroSA
Copy link

PyroSA commented Jan 27, 2023

@beckmx - where's your branch? I have an oak-d that exposes NV12, so could have a look.

@beckmx
Copy link

beckmx commented Jan 27, 2023

@PyroSA https://github.com/beckmx/vita-streamer there u go, I created a yt video in case u want to see the problem

@PyroSA
Copy link

PyroSA commented Jan 28, 2023

@beckmx

I think your colour conversion is incomplete.
u and v is shifted, and you need to convert to RGB.

            for(x = 0; x < vd->width; x++) {
                int r, g, b;
                int yc, uc, vc;

                // Gather YUV                
                yc = yplane[x + y * vd->width] << 8; // pre-shift
                uc = uplane[x / uvdiv + y / uvdiv * uvwidth] - 128;
                vc = vplane[x / uvdiv + y / uvdiv * uvwidth] - 128;

                // Convert to RGB
                r = yc + (359 * vc) >> 8;
                g = yc - (88 * uc) - (183 * vc) >> 8;
                b = yc + (454 * uc) >> 8;

                // Clamp values
                *(ptr++) = (r > 255) ? 255 : ((r < 0) ? 0 : r);
                *(ptr++) = (g > 255) ? 255 : ((g < 0) ? 0 : g);
                *(ptr++) = (b > 255) ? 255 : ((b < 0) ? 0 : b);
            }

That was enough to get mine looking accurate.

(Fixed focus lens, yellow-blue-red at bottom)
image

And boom - @alex-luxonis's uvc output becomes an http stream - love it!

@beckmx
Copy link

beckmx commented Jan 28, 2023

@PyroSA awesome, I could easily port this to openwrt :)

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

Successfully merging this pull request may close these issues.

7 participants