YUY2 (YUYV) Transfer YUV420 source analysis

Source: Internet
Author: User

This article records their own learning process, the program for downloading the source code online.

Video surveillance, with the ARM11 of the hardware encoder, because it requires the input of the video for the YUV420 format, so need to their own USB camera output of the YUY2 format to convert.

The YUY2 is the same as the internal arrangement of YUV422. Theoretical analysis, can refer to this article: http://blog.csdn.net/searchsun/article/details/2443867

SOURCE Ideas:

1, YUV has packaged (packed) format and plane (planar) format. YUY2 is the packed format, and YUV420 is the planar format, which needs to be converted.

Extracts each y, U, v component from the original image and puts it into the array to synthesize a new YUV420 format from these components.

2, YUV420 compared to YUY2, there is no change on the Y-component, the rows of U and V are extracted.

3. Image with Width * height of length and width. If the format is YUV444, the image size is width * Height * 3

If the format is YUV422, the image size is width * Height *

If the format is YUV420, the image size is width * Height * 1.5

This article will use: 6*8 image for illustrative purposes.

//========================================================================

The source code is as follows: In addition to the picture, the text that you add will exist in the form of annotations.

//========================================================================

#include <stdio.h> #include <string.h>
#include <stdlib.h>
#include <limits. H>

Src_filename out_filename src_width src_height Frameno
int main (int argc, char* argv[])
{
Char src_filename[255];
Char out_filename[255];

int Src_width, src_height;
Src_width = src_height = 0;

int framenum = 65535;

int src_size, out_size, tem_size;
src_size = out_size = tem_size = 0;

unsigned char * src_buf, *out_buf, *tem_buf;

file* In_file, *out_file;

unsigned char *y, *u, *v;
unsigned char * Y2, *U2, *v2;

unsigned char * p=null;

if (argc>5)//Determine if the input parameter is 5.
{
strcpy (Src_filename, argv[1]);
strcpy (Out_filename, argv[2]);
Src_width = Atoi (argv[3]);
Src_height = Atoi (argv[4]);
Framenum = Atoi (argv[5]);
}
Else
{
strcpy (Src_filename, argv[1]);
strcpy (Out_filename, argv[2]);
Src_width = Atoi (argv[3]);
Src_height = Atoi (argv[4]);
}


Src_size = (Src_width * src_height) << 1; Source picture, format YUY2, size Src_width * src_height
Src_buf = (unsigned char *) malloc (src_size*sizeof (char));
memset (src_buf, 0, src_size);




Tem_size = (Src_width * src_height) << 1; This opens a piece of area that is the same size as the source graph. In this paper, the packed form of YUY2 format data is first transformed

As a form of planar, and store this data in the open memory here. In general, the area here has temporary storage effect.

Build YUV420 Data Services for post-order. Tem_buf = (unsigned char *) malloc (tem_size*sizeof (char));
memset (tem_buf, 0, tem_size);


Out_size = src_width*src_height * 1.5; Open Src_width*src_height * 1.5 size area for saving YUV420 data.
Out_buf = (unsigned char *) malloc (out_size*sizeof (char));
memset (out_buf, 0, out_size);


In_file = fopen (Src_filename, "RB");
if (!in_file)
{
printf ("Cannot open input file.");
return 0;
}


Out_file = fopen (Out_filename, "WB");
if (!out_file)
{
printf ("Cannot write file./n");
return 0;
}


while (framenum>0 &&!feof (in_file))
{
Read out a frame of data
if (Fread (Src_buf, src_size, 1, In_file) <= 0)
printf ("Cannot read from input file.");
p = src_buf; Source Map Data Header address

Determine the first address of each component of Y, U, V in the temporary storage area. Y = Tem_buf;
U = Y + src_width*src_height;
V = U + (src_width*src_height>>1); Y U V =4:2; 2

Determine the first address of each component of Y, U, V in the YUV420 storage area. Y2 = Out_buf;
U2 = Y2 + src_width*src_height; Length and width reflect the number of pixels, each pixel must contain the Y component, so to reserve a long * wide space for the Y component storage.
V2 = U2 + (src_width*src_height>>2);

/* Changed from packaged yuyv to tablet yuv*/
int k, J;
for (k=0; k<src_height; ++k)
{
for (j=0; j< (src_width>>1); ++j)
{
Y[J*2] = P[4*j];
U[J] = p[4*j+1];
Y[J*2+1] = p[4*j+2];
V[J] = p[4*j+3];
}
p = p + src_width*2;

y = y + src_width;
u = U + (src_width>>1);
v = v + (src_width>>1);

}

Packed format source graph (take 6*8 image as an example)


After for loop becomes planar form



Reset
Y = Tem_buf;
U = Y + src_width*src_height;
V = U + (src_width*src_height>>1);

int l;
for (l=0; l<src_height/2; ++l)
{
memcpy (U2, U, src_width>>1);
memcpy (V2, V, src_width>>1);

U2 = U2 + (src_width>>1);
V2 = V2 + (src_width>>1);

u = U + (src_width);
v = v + (src_width);
}

memcpy (Y2, Y, src_width*src_height);

Finally get:


   fwrite (out_buf, sizeof (char), out_size, out_file);
   printf (".");


   frameNum--;
}

Fflush (out_file);

Free (src_buf); src_buf=null;
Free (tem_buf); tem_buf=null;
Free (out_buf); out_buf=null;


Fclose (in_file);
Fclose (Out_file);

return 0;
}

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.