/**********************************************************************
 * ISO MPEG Audio Subgroup Software Simulation Group (1996)
 * ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
 *
 * $Id: musicout.c,v 1.2 1996/03/28 03:13:37 rowlands Exp $
 *
 * $Log: musicout.c,v $
 * Revision 1.2  1996/03/28 03:13:37  rowlands
 * Merged layers 1-2 and layer 3 revisions
 *
 * Revision 1.1  1996/02/14 03:45:52  rowlands
 * Initial revision
 *
 * Received from FhG
 **********************************************************************/
/**********************************************************************
 *   date   programmers                comment                        *
 * 2/25/91  Douglas Wong        start of version 1.0 records          *
 * 3/06/91  Douglas Wong        rename setup.h to dedef.h             *
 *                              removed extraneous variables          *
 *                              removed window_samples (now part of   *
 *                              filter_samples)                       *
 * 3/07/91  Davis Pan           changed output file to "codmusic"     *
 * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
 *                              Incorporated new "out_fifo()" which   *
 *                              writes out last incomplete buffer.    *
 *                              Incorporated all AIFF routines which  *
 *                              are also compatible with SUN.         *
 *                              Incorporated user interface for       *
 *                              specifying sound file names.          *
 *                              Also incorporated user interface for  *
 *                              writing AIFF compatible sound files.  *
 * 27jun91  dpwe (Aware)        Added musicout and &sample_frames as  *
 *                              args to out_fifo (were glob refs).    *
 *                              Used new 'frame_params' struct.       *
 *                              Clean,simplify, track clipped output  *
 *                              and total bits/frame received.        *
 * 7/10/91  Earle Jennings      changed to floats to FLOAT            *
 *10/ 1/91  S.I. Sudharsanan,   Ported to IBM AIX platform.           *
 *          Don H. Lee,                                               *
 *          Peter W. Farrett                                          *
 *10/ 3/91  Don H. Lee          implemented CRC-16 error protection   *
 *                              newly introduced functions are        *
 *                              buffer_CRC and recover_CRC_error      *
 *                              Additions and revisions are marked    *
 *                              with "dhl" for clarity                *
 * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
 *                              important fixes involved changing     *
 *                              16-bit ints to long or unsigned in    *
 *                              bit alloc routines for quant of 65535 *
 *                              and passing proper function args.     *
 *                              Removed "Other Joint Stereo" option   *
 *                              and made bitrate be total channel     *
 *                              bitrate, irrespective of the mode.    *
 *                              Fixed many small bugs & reorganized.  *
 *19 aug 92 Soren H. Nielsen    Changed MS-DOS file name extensions.  *
 * 8/27/93 Seymour Shlien,      Fixes in Unix and MSDOS ports,        *
 *         Daniel Lauzon, and                                         *
 *         Bill Truerniet                                             *
 *--------------------------------------------------------------------*
 * 4/23/92  J. Pineda           Added code for layer III.  LayerIII   *
 *          Amit Gulati         decoding is currently performed in    *
 *                              two-passes for ease of sideinfo and   *
 *                              maindata buffering and decoding.      *
 *                              The second (computation) pass is      *
 *                              activated with "decode -3 <outfile>"  *
 * 10/25/92 Amit Gulati         Modified usage() for layerIII         *
 * 12/10/92 Amit Gulati         Changed processing order of re-order- *
 *                              -ing step.  Fixed adjustment of       *
 *                              main_data_end pointer to exclude      *
 *                              side information.                     *
 *  9/07/93 Toshiyuki Ishino    Integrated Layer III with Ver 3.9.    *
 *--------------------------------------------------------------------*
 * 11/20/93 Masahiro Iwadare    Integrated Layer III with Ver 4.0.    *
 *--------------------------------------------------------------------*
 *  7/14/94 Juergen Koller      Bug fixes in Layer III code           *
 *--------------------------------------------------------------------*
 * 08/11/94 IIS                 Bug fixes in Layer III code           *
 *--------------------------------------------------------------------*
 * 11/04/94 Jon Rowlands        Prototype fixes                       *
 *--------------------------------------------------------------------*
 *  7/12/95 Soeren H. Nielsen   Changes for LSF Layer I and II        *
 *--------------------------------------------------------------------*
 *  7/14/94 Juergen Koller      Bug fixes in Layer III code           *
 *--------------------------------------------------------------------*
 *     8/95 Roland Bitto        addapdet to MPEG 2                    *
 *--------------------------------------------------------------------*
 * 11/22/95 Heiko Purnhagen     skip ancillary data in bitstream      *
 *--------------------------------------------------------------------*
 * 12/16/96 Johan Hagman	Adapted for Solaris (mpeg3play 0.9)   *
 **********************************************************************/

#include        "common.h"
#include        "decoder.h"
/********************************************************************
 *
 *        This part contains the MPEG I decoder for Layers I & II.
 *
 *********************************************************************/

/****************************************************************
 *
 *        For MS-DOS user (Turbo c) change all instance of malloc
 *        to _farmalloc and free to _farfree. Compiler model hugh
 *        Also make sure all the pointer specified are changed to far.
 *
 *****************************************************************/
//typedef short	 PCMBUF[2][SSLIMIT][SBLIMIT];

 _declspec(dllexport) void poly(float hybridOut[32][18], short pcm_sample[SSLIMIT][SBLIMIT]);
 _declspec(dllexport) void poly(float hybridOut[32][18], short pcm_sample[SSLIMIT][SBLIMIT])

 {
/* Implementations */

    
    //static PCMBUF FAR	*pcm_sample;

    static int 		 ss, sb, /*ch,*/ i, j, k, stereo,
			 done = FALSE, clip;
    
    /*pcm_sample = (PCMBUF FAR *) mem_alloc((long) sizeof(PCMBUF),
                                              "PCM Sample");*/
    		//for (ch = 0; ch < 1; ch++) {
		    		    /* PolyPhase Input*/
		    static REAL polyPhaseIn[SBLIMIT];

#ifdef OPTIMIZE
		    for (ss = 0; ss < SSLIMIT; ss++) {	/* Polyphase synthesis*/
			for (sb = 0; sb < SBLIMIT; sb++) {
			    /* Perform frequency inversion for polyphase*/
			    if ((ss % 2) && (sb % 2))
				polyPhaseIn[sb] = -hybridOut[sb][ss];
			    else
				polyPhaseIn[sb] = hybridOut[sb][ss];
			}
			clip += SubBandSynthesis(polyPhaseIn, &(pcm_sample[ss][0]));
		    }
#else
		    /* Frequency inversion for polyphase*/
		    for (ss = 0; ss < SSLIMIT; ss++)		/* 18*/
			for (sb = 0; sb < SBLIMIT; sb++)	/* 32*/
			    if ((ss%2) && (sb%2))
				hybridOut[sb][ss] = -hybridOut[sb][ss];

		    for (ss = 0; ss < SSLIMIT; ss++) {	/* Polyphase synthesis*/
			for (sb = 0; sb < SBLIMIT; sb++)
			    polyPhaseIn[sb] = hybridOut[sb][ss];
			clip += SubBandSynthesis (polyPhaseIn, &(pcm_sample[ss][0]));
		    }
#endif	/* OPTIMIZE */
		//}
	}