-1

I'm doing some computing on raw video YUV file. The idea is to move a 8x8 window accross the entire frame, pixel by pixel. Here's my code:

    while ( (frameNumber < maxFrame) && 
            (fread_s(frame1, frameSize, 1, frameSize, file1)== frameSize) &&
            (fread_s(frame2, frameSize, 1, frameSize, file2)) == frameSize)
{
    unsigned char *p1 = frame1;     //pointer to indicate start of a window
    unsigned char *p2 = frame2;  
    unsigned char *p8_1 = NULL;     //pointer used to navigate accross 8x8window
    unsigned char *p8_2 = NULL;

    for (int i = 0; i < countY; i += stepSize)
    {
        p1 += i*width;                       //move to next line
        p2 += i*width;

        for (int j = 0; j < countX; j += stepSize)
        {



            meanLuma1 = 0;
            meanLuma2 = 0;

            //8x8 window loop

            for (int k = 0; k < windowSize; k++)
            {
                p8_1 = p1 + k*width;             //move to next line of a window
                p8_2 = p2 + k*width;

                for (int l = 0; l < windowSize; l++)
                {
                    meanLuma1 += *p8_1;       
                    meanLuma2 += *p8_2;    //Access violation here at i=60, others are 0
                    ++p8_1;
                    ++p8_2;

                }

            }
            meanLuma1 = meanLuma1 / (windowSize*windowSize);
            meanLuma2 = meanLuma2 / (windowSize*windowSize);

            ++p1;
            ++p2;

        }

I keep getting access violation exception on p8_2 (i=60 ; j,k,l=0). I think it's weird, that p8_1 reads its value successfully but p8_2 does not, because both files have same size and dimensions. Variable states are following:

width=352;
height=288;
stepSize=4;
windowSize=8;

And I'm computing maximum number of steps like following:

    int countX = ((width - windowSize) / stepSize)+1;
    int countY = ((height - windowSize) / stepSize)+1;

Second strangeness is that if I set windowSize = 16 and stepSize = 8 , it compiles successfully.

Don't you see any obvious issues? I'm struggling with finding the bug for ages.

xbilek18
  • 85
  • 1
  • 11

1 Answers1

1

Seems here is an error in lines:

p1 += i*width;                     //move to next line
p2 += i*width;

You move on too far in arithmetical progression on each step. Try:

p1 += width;                       //move to next line
p2 += width;

or

p1 += stepSize*width;              //move to next line
p2 += stepSize*width;

(depends on width and stepSize measure units).

s.cpp
  • 91
  • 1
  • 3