0

My task is to read a yuv file and to each component(Y,Cb,Cr) of it, I'm appending some data and storing it into another file. I have tried the 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 the above code works fine for the first loop, but in the second loop i get an exception

Access violation writing location 0x0092f000.

at line fwrite(y, sizeof(unsigned char),1920*1080,myYUV);

Is this a problem in pointer increment? or it is something else? Please reply. Thanks in advance.

Zax
  • 2,870
  • 7
  • 52
  • 76

1 Answers1

3

These increments:

y+=1920*1080;
u+=1920/2*1080/2;
v+=1920/2*1080/2;

will increment the pointers past the end of the allocated memory. For example, y points to the start of 1920*1080 bytes of allocated memory. Increasing it by that much makes it point past the end of that memory. This results in reading/writing to/from unallocated memory. That's why you get an access violation.

I don't actually see a reason for those pointers to be incremented at all.

Other than that, your code should check for error conditions (did fopen() succeed, etc.)

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • Each time i read 1920*1080 bytes and write them to myYUV, inorder to access the next set of data i should increment the y ptr. Correct me if i'm wrong. – Zax Jul 15 '13 at 07:30
  • Well, then you need a bigger buffer, because the first time you increment, you are going past the end of the buffer you have set aside. – Mats Petersson Jul 15 '13 at 07:31
  • @Zax What purpose does that serve? You write the data out to a file and then read new data over the old one (which is no longer needed since it's been written to the file already.) There's no reason to increment the pointers. – Nikos C. Jul 15 '13 at 07:31
  • as you can see the code, i am padding additional data to the new file after every fwrite from y,u,v. All my intention is to make a 1920x1080 resolution file to 1920x1088. – Zax Jul 15 '13 at 07:42
  • @Zax Are you sure you need a while loop at all then? In each loop, you're reading and writing a complete image. If you only have one image, there's no need for the loop at all. If you do have multiple images, but you still need the old ones, then allocate more memory. But your code suggests that the old data is not needed, thus no need to increment the pointers. – Nikos C. Jul 15 '13 at 07:50