YUV Format Learning: NV12 and yuv420p format interchange

Source: Internet
Author: User

The conversion of NV12 and YUV420 is the conversion in the same sampling space, just the adjustment of the individual component position, as long as the arrangement of Y, U, V components is understood, it is easy to write.

The code is as follows:

/** yyyy yyyy UV UV-YYYY yyyy UU VV */void yuv420sp_to_yuv420p (unsigned char* yuv420sp, unsigned char* yuv420p,
    int width, int height) {int I, J;

    int y_size = width * height;
    unsigned char* y = yuv420sp;

    unsigned char* UV = yuv420sp + y_size;
    unsigned char* y_tmp = yuv420p;
    unsigned char* u_tmp = yuv420p + y_size;

    unsigned char* v_tmp = yuv420p + y_size * 5/4;

    Y memcpy (y_tmp, y, y_size);
        U for (j = 0, i = 0; j < y_size/2; j+=2, i++) {u_tmp[i] = uv[j];
    V_tmp[i] = uv[j+1]; }}/** yyyy yyyy uu VV, yyyy yyyy UV UV */void yuv420p_to_yuv420sp (unsigned char* yuv420p, unsigned char* yuv42
    0SP, int width, int height) {int I, J;

    int y_size = width * height;
    unsigned char* y = yuv420p;
    unsigned char* u = yuv420p + y_size;

    unsigned char* v = yuv420p + y_size * 5/4;
    unsigned char* y_tmp = yuv420sp;

    unsigned char* uv_tmp = yuv420sp + y_size; Y memcpy (y_tmp, y, y_size); 
        U for (j = 0, i = 0; j < y_size/2; j+=2, i++) {//Here you can adjust the position of U, V, into NV12 or NV21 #if uv_tmp[j] = u[i];
UV_TMP[J+1] = V[i];
        #else uv_tmp[j] = v[i];
UV_TMP[J+1] = U[i]; #endif}}


Postscript:

I wrote the things I had been studying for a long time, as if I had not written the idea. Before also tried to write a highly technical article, everywhere looking for information, find out, but later found that they are not that piece of material, and then directly affixed to the code, the theory of things instead of saying-because it is unclear, but also afraid to say wrong.

About YUV, initially heard in 6, 7 years ago the University, began to contact is 4, 5 years ago, and really understand its principles, but in recent years. The progress of learning is not slow. Write it, there is a confession. Also prepare yourself for the YUV player you are ready to implement.


Late 2015.8.5 Night



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.