/* Reference_IDCT.c, Inverse Discrete Fourier Transform, double precision          */

/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */

/*
 * Disclaimer of Warranty
 *
 * These software programs are available to the user without any license fee or
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
 * any and all warranties, whether express, implied, or statuary, including any
 * implied warranties or merchantability or of fitness for a particular
 * purpose.  In no event shall the copyright-holder be liable for any
 * incidental, punitive, or consequential damages of any kind whatsoever
 * arising from the use of these programs.
 *
 * This disclaimer of warranty extends to the user of these programs and user's
 * customers, employees, agents, transferees, successors, and assigns.
 *
 * The MPEG Software Simulation Group does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any third-party
 * patents.
 *
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
 * are subject to royalty fees to patent holders.  Many of these patents are
 * general enough such that they are unavoidable regardless of implementation
 * design.
 *
 * MPEG2AVI
 * --------
 *  v0.16B33 renamed functions to init_idct_ref64() and idct_ref64()
 *  v0.16B3  changed variable declarations to reduce runtime overhead
 *  v0.16B22 added emms statement to clear FPU state from MMX-stuff
 */

/*  Perform IEEE 1180 reference (64-bit floating point, separable 8x1
 *  direct matrix multiply) Inverse Discrete Cosine Transform
*/


/* Here we use math.h to generate constants.  Compiler results may
   vary a little */

#include <math.h>

#include "config.h"

#ifndef PI
# ifdef M_PI
#  define PI M_PI
# else
#  define PI 3.14159265358979323846
# endif
#endif

/* global declarations */
void init_idct_ref64 _ANSI_ARGS_((void));
void idct_ref64 _ANSI_ARGS_((short *block));

/* private data */
static double tmp[64];

/* cosine transform matrix for 8x1 IDCT */
static double c[8][8];

/* initialize DCT coefficient matrix */

void init_idct_ref64()
{
  int freq, time;
  double scale;

  for (freq=0; freq < 8; freq++)
  {
    scale = (freq == 0) ? sqrt(0.125) : 0.5;
    for (time=0; time<8; time++)
      c[time][freq] = scale*cos((PI/8.0)*freq*(time + 0.5));
  }
}

/* perform IDCT matrix multiply for 8x8 coefficient block */

void idct_ref64(block)
short *block;
{
  static int i, j, k, v;
  static double partial_product;
//
  static double *ptmp; /* pointer to tmp[] */
  static short *pblock; /* pointer to block[] */

//PX3  __asm emms;	// v0.16B22 due to MMX calls

  ptmp = tmp;
  for (i=0; i<8; i++)
  {
	pblock = &block[8*i];
	ptmp = &tmp[8*i]; /* sizeof( double ) */
    for (j=0; j<8; j++)
    {
      partial_product = 0.0;
	  
      for (k=0; k<8; k++)
        partial_product+= c[j][k]*pblock[k];
		//partial_product+= c[k][j]*block[k+8*i];

      ptmp[0] = partial_product;
	  ++ptmp;
	  //tmp[8*i+j] = partial_product;
    }

	
  }

  /* Transpose operation is integrated into address mapping by switching 
     loop order of i and j */

  ptmp = tmp;

  for (j=0; j<8; j++)
  {
	pblock = &block[j];
    for (i=0; i<8; i++)
    {
      partial_product = 0.0;

	  ptmp = &tmp[ j ];
      for (k=0; k<8; k++)
	  {
        partial_product+= c[i][k]*(ptmp[ 8*k ]);
		//ptmp += 8*k;
	  }

      v = (int) floor(partial_product+0.5);
      pblock[8*i] = (v<-256) ? -256 : ((v>255) ? 255 : v);
	  //pblock += 8*i;
    }
  }
}
