my task read yuv file , each component(y,cb,cr) of it, i'm appending data , storing file. have tried below code:
#include <stdio.h> #include <stdlib.h> void main() { file *fp=fopen("traffic_1920x1080.yuv","rb"); file *myyuv=fopen("traffic_1920x1088.yuv","ab"); int count=0; unsigned char *y=(unsigned char*)malloc(sizeof(unsigned char)*1920*1080); unsigned char *u=(unsigned char*)malloc(sizeof(unsigned char)*(1920/2)*(1080/2)); unsigned char *v=(unsigned char*)malloc(sizeof(unsigned char)*(1920/2)*(1080/2)); unsigned char ypad[1920*8]; unsigned char upad[(1920/2)*4]; unsigned char vpad[(1920/2)*4]; for(int i=0;i<(1920/2)*4;i++) { ypad[i]=255; upad[i]=128; vpad[i]=128; } for(int i=(1920/2)*4;i<1920*8;i++) ypad[i]=255; while (!feof(fp)) { fread(y,sizeof(unsigned char),1920*1080,fp); fread(u,sizeof(unsigned char),1920/2*1080/2,fp); fread(v,sizeof(unsigned char),1920/2*1080/2,fp); fwrite(y, sizeof(unsigned char),1920*1080,myyuv); fwrite(ypad,sizeof(unsigned char),1920*8,myyuv); fwrite(u,sizeof(unsigned char),1920/2*1080/2,myyuv); fwrite(upad,sizeof(unsigned char),1920/2*4,myyuv); fwrite(v,sizeof(unsigned char),1920/2*1080/2,myyuv); fwrite(vpad,sizeof(unsigned char),1920/2*4,myyuv); printf("frame %d created\r",count); y+=1920*1080; u+=1920/2*1080/2; v+=1920/2*1080/2; count ++; } free(y); free(u); free(v); fclose(fp); fclose(myyuv); }
howevr above code works fine first loop, in second loop exception
access violation writing location 0x0092f000.
at line fwrite(y, sizeof(unsigned char),1920*1080,myyuv);
is problem in pointer increment? or else? please reply. in advance.
these increments:
y+=1920*1080; u+=1920/2*1080/2; v+=1920/2*1080/2;
will increment pointers past end of allocated memory. example, y
points start of 1920*1080 bytes of allocated memory. increasing makes point past end of memory. results in reading/writing to/from unallocated memory. that's why access violation.
i don't see reason pointers incremented @ all.
other that, code should check error conditions (did fopen() succeed, etc.)
Comments
Post a Comment