CIF Video Format

 

This is the simplest video file format in YUV color space. The size of this format is static(352x288). It is also a 4:2:2 YUV format. So Y, U and V matrices are not same. YUV order is as follows:

                     Y: 352x288
                     U: 176x144
                     V: 176x144

If you want to use CIF format, you will need to convert to RGB. Below you can find the conversion source code.
 

CIF Read and Write Source Code in C

For memory allocation see Dynamic Memory Allocation page.
 

/*

 *   Give a reference to this site if you like to use this code.

 *
 *                               Written by Chasan Chouse, 2004

 */

/*
  Returns number of frames
*/

long
 CIF_Get_Frame_Info(char FileName[])
{
    long szf;
    FILE *f;

    f = fopen(FileName, "rb");
    szf = filesize(f);
    fclose(f);
    return (szf / 152064); // one frame=152064
}

/*
  CIF format is 352x288
  YUV order is as follows:
    Y: 352x288
    U: 176x144
    V: 176x144
  New order will be
    Y: 352x288
    U: 352x288
    V: 352x288

    No Header or other info.

*/
int CIF_Read_Data(char FileName[],
                  long FrameNo,
                  UCHAR **Y, // Y must be 352x288
                  UCHAR **U, // U must be 352x288
                  UCHAR **V) // V must be 352x288
{
    long i, j, szf, NofFrames;
    long szOneFrame = 152064// Y:352x288, U:176x144, V:176x144
    UCHAR pix;
    FILE *f;

    f = fopen(FileName, "rb");
    szf = filesize(f);
    NofFrames = szf / szOneFrame;
    if (FrameNo >= NofFrames)
    {
        printf("ERROR! Frame number must be between %ls and %ld."0, NofFrames-1);
        return false;
    }
    fseek(f, szOneFrame*FrameNo, SEEK_SET);
    /*Read Y values*/
    for(i=0;i<288;i++)
    {
        for(j=0;j<352;j++)
        {
            Y[i][j] = (UCHAR)fgetc(f);
        }
    }
    /*Read U values and expand them*/
    for(i=0;i<144;i++)
    {
        for(j=0;j<176;j++)
        {
            pix = (UCHAR)fgetc(f);
            U[i<<1][j<<1] = pix;        // 0,0
            U[i<<1][(j<<1)+1] = pix;    // 0,1
            U[(i<<1)+1][j<<1] = pix;    // 1,0
            U[(i<<1)+1][(j<<1)+1] = pix;// 1,1
        }
    }
    /*Read V values and expand them*/
    for(i=0;i<144;i++)
    {
        for(j=0;j<176;j++)
        {
            pix = (UCHAR)fgetc(f);
            V[i<<1][j<<1] = pix;        // 0,0
            V[i<<1][(j<<1)+1] = pix;    // 0,1
            V[(i<<1)+1][j<<1] = pix;    // 1,0
            V[(i<<1)+1][(j<<1)+1] = pix;// 1,1
        }
    }
    fclose(f);
    return false;
}
 

/*
  If the it's the first frame file must be created.
  Other frames will be appended.

/*
int CIF_Write_Data(char FileName[],
                   int mode,  // 0: first frame 1: next frames, append
                   UCHAR **Y, // Y must be 352x288
                   UCHAR **U, // U must be 352x288
                   UCHAR **V) // V must be 352x288
{
    long i, j;
    long szOneFrame = 152064// Y:352x288, U:176x144, V:176x144
    UCHAR pix;
    FILE *f;

    if (mode == 0// file will be created
    {
        if( (f = fopen(FileName, "rb+")) ) // check if file exists
        {
            fclose(f);
            DispError("ERROR! File exists\n");
            return false;
        }
        else // OK! Create the file
            f = fopen(FileName, "wb");
    }
    else // append
    {
        f = fopen(FileName, "rb+");
        fseek(f, 0L, SEEK_END);
    }
    /*Write Y values*/
    for(i=0;i<288;i++)
    {
        for(j=0;j<352;j++)
        {
             fputc((int)Y[i][j], f);
        }
    }
    /*Write U values*/
    for(i=0;i<144;i++)
    {
        for(j=0;j<176;j++)
        {
            /*Average of four pixel values will be written as U*/
            pix  = U[i<<1][j<<1];
            pix += U[i<<1][(j<<1)+1];
            pix += U[(i<<1)+1][j<<1];
            pix += U[(i<<1)+1][(j<<1)+1];
            pix = pix >> 2// pix/4
            fputc(pix, f);
        }
    }
    /*Write V values*/
    for(i=0;i<144;i++)
    {
        for(j=0;j<176;j++)
        {
            /*Average of four pixel values will be written as V*/
            pix  = V[i<<1][j<<1];
            pix += V[i<<1][(j<<1)+1];
            pix += V[(i<<1)+1][j<<1];
            pix += V[(i<<1)+1][(j<<1)+1];
            pix = pix >> 2// pix/4
            fputc(pix, f);
        }
    }

    fclose(f);
    return false;
}

 

Copyright by Chasan Chouse