Yuyv encoding to H.264: Linux video collection and encoding (2)

Source: Internet
Author: User

You can use v4l2 to successfully collect the yuyv video and encode it into H. 264 file, because yuv420 was previously encoded as H. 264 program, so I think yuv422 is encoded into H. 264 is also similar and should be quite simple.

So I am very happy to write the encoding function. Encoding yuv420 I am referring to this article and this blog post. I am now ready to start. I feel that Maria is waving to me, I think I'm about to succeed.
Pai_^

However, I know that, as a programmer, I will be embarrassed if I don't encounter any problems or difficulties.

Yes, I have encountered difficulties. The videos I coded are flowers, red, green, and like spring!


Because the YUV code of yuv420 is also scanned by line, its YUV sequence is as follows:

Yyyy

Yyyy

Uvuv

So I naively thought that the yuv422 stored the same sequence, because they all had an interline scan.

Later, we learned that the YV scan sequence is not like this. The yuv422 sequence should be like the following:

Yu YV

That is to say, UV is indeed cross-storage, but it must be crossed with Y.

Once you know the storage sequence, it's easy to encode it into H.264.

See my code below:

Char * Y = en-> picture-> IMG. plane [0]; char * u = en-> picture-> IMG. plane [1]; char * V = en-> picture-> IMG. plane [2]; int is_y = 1, is_u = 1; int y_index = 0, u_index = 0, v_index = 0; int yuv422_length = 2 * En-> param-> I _width * En-> param-> I _height; // The sequence is Yu YV, length width * height * 2 bytes of A yuv422 frame for (I = 0; I <yuv422_length; ++ I) {If (is_y) {* (Y + y_index) = * (in + I); ++ y_index; is_y = 0;} else {If (is_u) {* (U + u_index) = * (in + I ); ++ u_index; is_u = 0;} else {* (V + v_index) = * (in + I); ++ v_index; is_u = 1;} is_y = 1 ;}}

Note: In is the pointer to the yuv422 sequence, that is, the starting address of yuv422.

We only need

Y is assigned to plane [0].

U assigned to plane [1]

V is assigned to plane [2] to encode it.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.