[Libav-user] Convert from YUYV to YUV420P

Jorge gusa.mlbb at gmail.com
Mon Apr 4 10:45:40 CEST 2011


Hello:

I am trying to convert from YUYV to YUV420P. I haven't seen any function so
I decided to do on my own. But I have the problem that it doesn't seem to
work.

To test if my function works I have took a picture from the camera and I
have convert it to YUV420P format, but the result is not good.

http://img43.imageshack.us/i/targetimg.jpg/

I have seen that there is some code available but it doesn't work  for me
neither. Mainly, I have used this reference
http://en.wikipedia.org/wiki/YUVto know how to convert.

Could you give me some advice?


The code I am using is

int main(){
        FILE * fd = fopen("v1.yuv", "r");
        if(fd < 0){
                printf("can't open in file. error=%d(%s) \n", errno,
strerror(errno));
                exit(1);
        };

        FILE * fd_out = fopen("v1_i422_3.yuv", "w");
        if(fd_out < 0){
                printf("can't open out file. error=%d(%s) \n", errno,
strerror(errno));
                exit(1);
        };

        int width = 752;
        int height = 480;
        int count = width * height * 2;
        unsigned char * buf = (unsigned char *)malloc(count);
        unsigned char * buf_y = (unsigned char *)malloc(count/2);
        unsigned char * buf_u = (unsigned char *)malloc(count/4);
        unsigned char * buf_v = (unsigned char *)malloc(count/4);
        int res = 1;
        int pos = 0;
        int wrb = 0;
        int pos_u = 0;
        int pos_v = 0;
        int pos_y = 0;
        while(res > 0){
                res = fread((void*)buf, count, 1, fd );
                printf("bytes read=%d \n", res );
                if(res <= 0 )
                        continue;

                pos_u = 0;
                pos_v = 0;
                pos_y = 0;
                for(int i = 0; i < height; i++ ){
                        for(int j = 0; j < width; j+=4 ){
                                pos = i*width + j;

                                buf_y[pos_y] = buf[pos];
                                pos_y++;
                buf_y[pos_y] = buf[pos+2];
                                pos_y++;

                if( !(i%2) ){
                  buf_u[pos_u] = buf[pos+1];
                  pos_u++;
                  buf_v[pos_v] = buf[pos+3];
                  pos_v++;
                };

                        };
                };
                wrb = fwrite(buf_y, count/2, 1, fd_out);
                if(wrb < 0){
                        printf("can't write out file. error=%d(%s) \n",
errno, strerror(errno));
                        exit(1);
                };
                fwrite(buf_u, count/4, 1, fd_out);
                fwrite(buf_v, count/4, 1, fd_out);
        };
        fclose(fd);
        fclose(fd_out);
};


Regards Jorge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20110404/811b864e/attachment.html>


More information about the Libav-user mailing list