/*
    Copyright 2003 Matthew Marjanovic <maddog@mir.com>

    This file is part of y4mscaler.

    y4mscaler is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    y4mscaler is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with y4mscaler; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#include "y4m-config.h"
#include "debug.h"

#include <stdio.h>

#include "ysStreamInfo.H"




int ysStreamInfo::parse_dimensions(const char *s)
{
  int sx, sy;
  if (sscanf(s, "%ux%u", &sx, &sy) == 2) {
    x_size(sx);
    y_size(sy);
    return 0;
  }
  return 1;
}


int ysStreamInfo::x_alignment() const
{
  //  return 2;
  return _subsampling.ratio().x().denominator();
}


int ysStreamInfo::y_alignment() const
{
  if (interlace() != Y4M_ILACE_NONE)
    //    return 4;
    return _subsampling.ratio().y().denominator() * 2;
  else
    //    return 2;
    return _subsampling.ratio().y().denominator();
}



const char *ilace_to_string(int i)
{
  switch (i) {
  case Y4M_UNKNOWN:             return "unknown";  break;
  case Y4M_ILACE_NONE:          return "progressive";  break;
  case Y4M_ILACE_TOP_FIRST:     return "top-field-first";  break;
  case Y4M_ILACE_BOTTOM_FIRST:  return "bottom-field-first";  break;
  default:
    return "?????";
    break;
  }
}



int ysStreamInfo::read_stream_header(int fdin)
{
  int retval = y4m_read_stream_header(fdin, &_streaminfo);

  if (retval == Y4M_OK) {
    /* try to find a chromass xtag... */
    y4m_xtag_list_t *xtags = y4m_si_xtags(&_streaminfo);
    const char *tag = NULL;
    int n;
    for (n = y4m_xtag_count(xtags) - 1; n >= 0; n--) {
      tag = y4m_xtag_get(xtags, n);
      if (!strncmp("XYSCSS=", tag, 7)) break;
    }
    if ((tag == NULL) || (n < 0)) { 
      /*  no tag: default is 4:2:0/JPEG  */
      _subsampling.mode(ysSubsampling::SS_420_JPEG);
    } else {
      /* parse the tag */
      tag += 7;
      if (!strcmp("444", tag)) {
	_subsampling.mode(ysSubsampling::SS_444);
      } else if (!strcmp("422", tag)) {
	_subsampling.mode(ysSubsampling::SS_422);
      } else if (!strcmp("420JPEG", tag)) {
	_subsampling.mode(ysSubsampling::SS_420_JPEG);
      } else if (!strcmp("420MPEG2", tag)) {
	_subsampling.mode(ysSubsampling::SS_420_MPEG2);
      } else if (!strcmp("420PALDV", tag)) {
	_subsampling.mode(ysSubsampling::SS_420_PALDV);
	/////      } else if (!strcmp("411NTSCDV", tag)) {
	////	_subsampling.mode(ysSubsampling::SS_411_NTSCDV);
      } else if (!strcmp("411", tag)) {
	_subsampling.mode(ysSubsampling::SS_411);
      } else {
	/////	some_error_here;
      }
    }
  }
  return retval;
}


int ysStreamInfo::write_stream_header(int fdout)
{
  y4m_xtag_list_t *xtags = y4m_si_xtags(&_streaminfo);
  /* remove any old chromass tags */
  for (int n = 0; n < y4m_xtag_count(xtags); n++) {
    const char *tag = y4m_xtag_get(xtags, n);
    if (!strncmp("XYSCSS=", tag, 7)) 
      y4m_xtag_remove(xtags, n);
  }
  /* add a new chromass tag */
  switch (_subsampling.mode()) {
  case ysSubsampling::SS_444:
    y4m_xtag_add(xtags, "XYSCSS=444");
    break;
  case ysSubsampling::SS_422:
    y4m_xtag_add(xtags, "XYSCSS=422");
    break;
  case ysSubsampling::SS_420_MPEG2: 
    y4m_xtag_add(xtags, "XYSCSS=420MPEG2");
    break;
  case ysSubsampling::SS_420_PALDV:
    y4m_xtag_add(xtags, "XYSCSS=420PALDV");
    break;
  case ysSubsampling::SS_411:
    y4m_xtag_add(xtags, "XYSCSS=411");
    break;
  default:
  case ysSubsampling::SS_420_JPEG:
    y4m_xtag_add(xtags, "XYSCSS=420JPEG");
    break;
  }
  /* write that header! */
  return y4m_write_stream_header(fdout, &_streaminfo);
}

