/*
   justdoit.c
*/

#include "videoport.h"

int main(int argc, char **argv)
{
  videoport p;
  int direction = (tolower(*argv[1]) == 'v') ?
    VIDEOPORT_DIRECTION_VM : VIDEOPORT_DIRECTION_MV;
  char *filename = argv[2];
  int issue_count;
  diskperformance diskperf = diskperformance_open();
  int fd;
  int nbytes;
  
  NC(p = videoport_open(direction, 20, 0));

  if (direction == VIDEOPORT_DIRECTION_VM)
    {
      printf("writing input video to file %s\n", filename);
      OC(fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, 0644));
    }
  else /* direction == VIDEOPORT_DIRECTION_MV */
    {
      printf("reading video from file %s for output\n", filename);
      OC(fd = open(filename, O_RDONLY, 0644));
    }

  nbytes = p->bytes_per_pixel * p->xsize * p->ysize;

  issue_count = 0;
  diskperformance_datapoint(diskperf, 0, p);

  for(;;)
    {
      int rc;
      videofield *f;
      
      videoport_wait_for_space_or_data(p);
      f = videoport_get_one_field(p);

      if (direction == VIDEOPORT_DIRECTION_VM)
        {
          OC(rc = write(fd, f->pixels, nbytes));
        }
      else
        {
          OC(rc = read(fd, f->pixels, nbytes));
        }
      if (rc != nbytes) error_exit("EOF or out of disk space");

      ++issue_count;
      diskperformance_datapoint(diskperf,
                                ((off64_t)issue_count) * nbytes, p);

      videoport_put_one_field(p, f);
    }
}

  

