//==============================================================================
//
// templ_yuv.c
//
// template for CIF yuv 4:2:0 video file processing
//
// developer: Henry Guennadi Levkin
//
// first argument in command line: video file name
// second and third arguments: width and height of luma
//==============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned char byte;

//------------------------------------------------------------------------------
int gnW = 352;  // CIF
int gnH = 288;

byte* gpLu;     // current frame
byte* gpCb;
byte* gpCr;

byte* gpLuPrev; // previous frame
byte* gpCbPrev;
byte* gpCrPrev;

byte* gpLuRes;  // result frame
byte* gpCbRes;
byte* gpCrRes;

//------------------------------------------------------------------------------
int main(int nArgs, char** ppArg)
{
  int nFrame = 0; // current processing frame
  int nRead;

  char filename[256];  // input file
  FILE* pFile;
  
  char filename2[256]; // output file
  FILE* pFile2;
  int nPicSize;

  if(nArgs==1)
  {
  }
  else if(nArgs==2)
  {
    strcpy(filename, ppArg[1]);
  }
  else if(nArgs==4)
  {
    strcpy(filename, ppArg[1]);
    gnW = atoi(ppArg[2]);
    gnH = atoi(ppArg[3]);
  }
  else
  {
  }

  pFile = fopen(filename, "rb");
  if(pFile == NULL)
  {
    puts("File can't be open!");
    return 1;
  }

  strcpy(filename2, "result.yuv");
  pFile2 = fopen(filename2, "wb");
  
  // memory allocation
  nPicSize = gnW * gnH * 3 / 2;

  // current frame
  gpLu = (byte*) malloc(nPicSize);
  gpCb = gpLu + gnW * gnH;
  gpCr = gpLu + gnW * gnH * 5 / 4;

  // previous frame
  gpLuPrev = (byte*) malloc(nPicSize);
  gpCbPrev = gpLuPrev + gnW * gnH;
  gpCrPrev = gpLuPrev + gnW * gnH * 5 / 4;

  // result frame
  gpLuRes = (byte*) malloc(nPicSize);
  gpCbRes = gpLuRes + gnW * gnH;
  gpCrRes = gpLuRes + gnW * gnH * 5 / 4;

  // reading/processing frames loop

  while(1)
  {
    if(nFrame != 0)
    {
      memcpy(gpLuPrev, gpLu, nPicSize);
    }

    nRead = fread(gpLu, 1, nPicSize, pFile);
    if(nRead < nPicSize) break;

    // processing
    printf("frame# %d:\n",nFrame);
    
    memcpy(gpLuRes, gpLu, nPicSize); // just coping!
    
    // saving result
    fwrite(gpLuRes, 1, nPicSize, pFile2);

    //---------------
    nFrame++;
  }

  // ------------------
  free(gpLuRes);
  free(gpLuPrev);
  free(gpLu);

  fclose(pFile2);
  fclose(pFile);

  return 0;
}

