Hello:<br><br>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.<br><br>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. <br>
<br><a href="http://img43.imageshack.us/i/targetimg.jpg/">http://img43.imageshack.us/i/targetimg.jpg/</a><br><br>I have seen that there is some code available but it doesn't work  for me neither. Mainly, I have used this reference <a href="http://en.wikipedia.org/wiki/YUV">http://en.wikipedia.org/wiki/YUV</a> to know how to convert.<br>
<br>Could you give me some advice? <br><br><br>The code I am using is <br><br>int main(){<br>        FILE * fd = fopen("v1.yuv", "r");<br>        if(fd < 0){<br>                printf("can't open in file. error=%d(%s) \n", errno, strerror(errno));<br>
                exit(1);<br>        };<br><br>        FILE * fd_out = fopen("v1_i422_3.yuv", "w");<br>        if(fd_out < 0){<br>                printf("can't open out file. error=%d(%s) \n", errno, strerror(errno));<br>
                exit(1);<br>        };<br><br>        int width = 752;<br>        int height = 480;<br>        int count = width * height * 2;<br>        unsigned char * buf = (unsigned char *)malloc(count);<br>        unsigned char * buf_y = (unsigned char *)malloc(count/2);<br>
        unsigned char * buf_u = (unsigned char *)malloc(count/4);<br>        unsigned char * buf_v = (unsigned char *)malloc(count/4);<br>        int res = 1;<br>        int pos = 0;<br>        int wrb = 0;<br>        int pos_u = 0;<br>
        int pos_v = 0;<br>        int pos_y = 0;<br>        while(res > 0){<br>                res = fread((void*)buf, count, 1, fd );<br>                printf("bytes read=%d \n", res );<br>                if(res <= 0 )<br>
                        continue;<br><br>                pos_u = 0;<br>                pos_v = 0;<br>                pos_y = 0;<br>                for(int i = 0; i < height; i++ ){<br>                        for(int j = 0; j < width; j+=4 ){<br>
                                pos = i*width + j;<br><br>                                buf_y[pos_y] = buf[pos];<br>                                pos_y++;                        <br>                buf_y[pos_y] = buf[pos+2];<br>
                                pos_y++;                <br><br>                if( !(i%2) ){<br>                  buf_u[pos_u] = buf[pos+1];<br>                  pos_u++;<br>                  buf_v[pos_v] = buf[pos+3];<br>
                  pos_v++;                                <br>                };<br><br>                        };<br>                };<br>                wrb = fwrite(buf_y, count/2, 1, fd_out);<br>                if(wrb < 0){<br>
                        printf("can't write out file. error=%d(%s) \n", errno, strerror(errno));<br>                        exit(1);<br>                };<br>                fwrite(buf_u, count/4, 1, fd_out);<br>
                fwrite(buf_v, count/4, 1, fd_out);<br>        };<br>        fclose(fd);<br>        fclose(fd_out);<br>};<br><br><br>Regards Jorge<br>