/* ***** BEGIN LICENSE BLOCK *****

 *
 * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
 *
 * The contents of this file, and the files included with this file, are
 * subject to the current version of the RealNetworks Public Source License
 * Version 1.0 (the "RPSL") available at
 * http://www.helixcommunity.org/content/rpsl unless you have licensed
 * the file under the RealNetworks Community Source License Version 1.0
 * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
 * in which case the RCSL will apply. You may also obtain the license terms
 * directly from RealNetworks.  You may not use this file except in
 * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
 * applicable to this file, the RCSL.  Please see the applicable RPSL or
 * RCSL for the rights, obligations and limitations governing use of the
 * contents of the file.
 *
 * This file is part of the Helix DNA Technology. RealNetworks is the
 * developer of the Original Code and owns the copyrights in the portions
 * it created.
 *
 * This file, and the files included with this file, is distributed and made
 * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 *
 * Technology Compatibility Kit Test Suite(s) Location:
 *    http://www.helixcommunity.org/content/tck
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */

/**************************************************************************************
 * Fixed-point MP3 decoder
 * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
 * June 2003
 *
 * buffers.c - allocation and freeing of internal MP3 decoder buffers
 *
 * All memory allocation for the codec is done in this file, so if you don't want
 *  to use other the default system malloc() and free() for heap management this is
 *  the only file you'll need to change.
 **************************************************************************************/

#include "stdio.h"
#include "stdlib.h"
#include "coder.h"
#include "fakelibc.h"

/**************************************************************************************
 * Function:    ClearBuffer
 *
 * Description: fill buffer with 0's
 *
 * Inputs:      pointer to buffer
 *              number of bytes to fill with 0
 *
 * Outputs:     cleared buffer
 *
 * Return:      none
 *
 * Notes:       slow, platform-independent equivalent to memset(buf, 0, nBytes)
 **************************************************************************************/
static void ClearBuffer(void *buf, int nBytes)
{
	int i;
	unsigned char *cbuf = (unsigned char *)buf;

	for (i = 0; i < nBytes; i++)
		cbuf[i] = 0;

	return;
}

/**************************************************************************************
 * Function:    AllocateBuffers
 *
 * Description: allocate all the memory needed for the MP3 decoder
 *
 * Inputs:      none
 *
 * Outputs:     none
 *
 * Return:      pointer to MP3DecInfo structure (initialized with pointers to all
 *                the internal buffers needed for decoding, all other members of
 *                MP3DecInfo structure set to 0)
 *
 * Notes:       if one or more mallocs fail, function frees any buffers already
 *                allocated before returning
 **************************************************************************************/
extern char g_decoder_pool[];
MP3DecInfo *AllocateBuffers(void)
{
	MP3DecInfo *mp3DecInfo;
	FrameHeader *fh;
	SideInfo *si;
	ScaleFactorInfo *sfi;
	HuffmanInfo *hi;
	DequantInfo *di;
	IMDCTInfo *mi;
	SubbandInfo *sbi;

//  mp3DecInfo = (MP3DecInfo *)uni_malloc(sizeof(MP3DecInfo));
//  if (!mp3DecInfo)
//    return 0;
//  ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));
//  
//  fh =  (FrameHeader *)     uni_malloc(sizeof(FrameHeader));
//  si =  (SideInfo *)        uni_malloc(sizeof(SideInfo));
//  sfi = (ScaleFactorInfo *) uni_malloc(sizeof(ScaleFactorInfo));
//  hi =  (HuffmanInfo *)     uni_malloc(sizeof(HuffmanInfo));
//  di =  (DequantInfo *)     uni_malloc(sizeof(DequantInfo));
//  mi =  (IMDCTInfo *)       uni_malloc(sizeof(IMDCTInfo));
//  sbi = (SubbandInfo *)     uni_malloc(sizeof(SubbandInfo));

	mp3DecInfo = (MP3DecInfo *)g_decoder_pool;
	if (!mp3DecInfo)
		return 0;
	ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));
	fh =  (FrameHeader *)     ((char *)mp3DecInfo + sizeof(MP3DecInfo));
	si =  (SideInfo *)        ((char *)fh + sizeof(FrameHeader));
	sfi = (ScaleFactorInfo *) ((char *)si + sizeof(SideInfo));
	hi =  (HuffmanInfo *)     ((char *)sfi + sizeof(ScaleFactorInfo));
	di =  (DequantInfo *)     ((char *)hi + sizeof(HuffmanInfo));
	mi =  (IMDCTInfo *)       ((char *)di + sizeof(DequantInfo));
	sbi = (SubbandInfo *)     ((char *)mi + sizeof(IMDCTInfo));

	mp3DecInfo->FrameHeaderPS =     (void *)fh;
	mp3DecInfo->SideInfoPS =        (void *)si;
	mp3DecInfo->ScaleFactorInfoPS = (void *)sfi;
	mp3DecInfo->HuffmanInfoPS =     (void *)hi;
	mp3DecInfo->DequantInfoPS =     (void *)di;
	mp3DecInfo->IMDCTInfoPS =       (void *)mi;
	mp3DecInfo->SubbandInfoPS =     (void *)sbi;

	if (!fh || !si || !sfi || !hi || !di || !mi || !sbi) {
		FreeBuffers(mp3DecInfo);	/* safe to call - only frees memory that was successfully allocated */
		return 0;
	}

	/* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
	ClearBuffer(fh,  sizeof(FrameHeader));
	ClearBuffer(si,  sizeof(SideInfo));
	ClearBuffer(sfi, sizeof(ScaleFactorInfo));
	ClearBuffer(hi,  sizeof(HuffmanInfo));
	ClearBuffer(di,  sizeof(DequantInfo));
	ClearBuffer(mi,  sizeof(IMDCTInfo));
	ClearBuffer(sbi, sizeof(SubbandInfo));

#if 0
      printf("MP3DecInfo Size: %lu\n", sizeof(MP3DecInfo));
      printf("FrameHeader Size: %lu\n", sizeof(FrameHeader));
      printf("SideInfo Size: %lu\n", sizeof(SideInfo));
      printf("ScaleFactorInfo Size: %lu\n", sizeof(ScaleFactorInfo));
      printf("HuffmanInfo Size: %lu\n", sizeof(HuffmanInfo));
      printf("DequantInfo Size: %lu\n", sizeof(DequantInfo));
      printf("IMDCTInfo Size: %lu\n", sizeof(IMDCTInfo));
      printf("SubbandInfo Size: %lu\n", sizeof(SubbandInfo));
      printf("Decoder total Size: %lu\n", \
              sizeof(MP3DecInfo) + sizeof(FrameHeader) + sizeof(SideInfo) + sizeof(ScaleFactorInfo) \
              + sizeof(HuffmanInfo) + sizeof(DequantInfo) + sizeof(IMDCTInfo) + sizeof(SubbandInfo));
#endif

	return mp3DecInfo;
}

#define SAFE_FREE(x)	{if (x)	uni_free(x);	(x) = 0;}	/* helper macro */

/**************************************************************************************
 * Function:    FreeBuffers
 *
 * Description: frees all the memory used by the MP3 decoder
 *
 * Inputs:      pointer to initialized MP3DecInfo structure
 *
 * Outputs:     none
 *
 * Return:      none
 *
 * Notes:       safe to call even if some buffers were not allocated (uses SAFE_FREE)
 **************************************************************************************/
void FreeBuffers(MP3DecInfo *mp3DecInfo)
{
//	if (!mp3DecInfo)
//		return;
//
//	SAFE_FREE(mp3DecInfo->FrameHeaderPS);
//	SAFE_FREE(mp3DecInfo->SideInfoPS);
//	SAFE_FREE(mp3DecInfo->ScaleFactorInfoPS);
//	SAFE_FREE(mp3DecInfo->HuffmanInfoPS);
//	SAFE_FREE(mp3DecInfo->DequantInfoPS);
//	SAFE_FREE(mp3DecInfo->IMDCTInfoPS);
//	SAFE_FREE(mp3DecInfo->SubbandInfoPS);
//
//	SAFE_FREE(mp3DecInfo);
}

static FrameHeader _frameHeader;
static SideInfo _sideInfo;
static ScaleFactorInfo _scaleFactorInfo;
static HuffmanInfo _huffmanInfo;
static DequantInfo _dequantInfo;
static IMDCTInfo _iMDCTinfo;
static SubbandInfo _subbandInfo;
static MP3DecInfo _mp3DecInfo;

MP3DecInfo *InitBuffers(void)
{
    MP3DecInfo *mp3DecInfo;
    FrameHeader *fh;
    SideInfo *si;
    ScaleFactorInfo *sfi;
    HuffmanInfo *hi;
    DequantInfo *di;
    IMDCTInfo *mi;
    SubbandInfo *sbi;

    mp3DecInfo = &_mp3DecInfo;
    ClearBuffer(mp3DecInfo, sizeof(MP3DecInfo));

    fh =  &_frameHeader;
    si =  &_sideInfo;
    sfi = &_scaleFactorInfo;
    hi =  &_huffmanInfo;
    di =  &_dequantInfo;
    mi =  &_iMDCTinfo;
    sbi = &_subbandInfo;
    //sbi = NULL;

    mp3DecInfo->FrameHeaderPS =     (void *)fh;
    mp3DecInfo->SideInfoPS =        (void *)si;
    mp3DecInfo->ScaleFactorInfoPS = (void *)sfi;
    mp3DecInfo->HuffmanInfoPS =     (void *)hi;
    mp3DecInfo->DequantInfoPS =     (void *)di;
    mp3DecInfo->IMDCTInfoPS =       (void *)mi;
    mp3DecInfo->SubbandInfoPS =     (void *)sbi;

    /* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
    ClearBuffer(fh,  sizeof(FrameHeader));
    ClearBuffer(si,  sizeof(SideInfo));
    ClearBuffer(sfi, sizeof(ScaleFactorInfo));
    ClearBuffer(hi,  sizeof(HuffmanInfo));
    ClearBuffer(di,  sizeof(DequantInfo));
    ClearBuffer(mi,  sizeof(IMDCTInfo));
    ClearBuffer(sbi, sizeof(SubbandInfo));

#if 0
    printf("MP3DecInfo Size: %lu\n", sizeof(MP3DecInfo));
    printf("FrameHeader Size: %lu\n", sizeof(FrameHeader));
    printf("SideInfo Size: %lu\n", sizeof(SideInfo));
    printf("ScaleFactorInfo Size: %lu\n", sizeof(ScaleFactorInfo));
    printf("HuffmanInfo Size: %lu\n", sizeof(HuffmanInfo));
    printf("DequantInfo Size: %lu\n", sizeof(DequantInfo));
    printf("IMDCTInfo Size: %lu\n", sizeof(IMDCTInfo));
    printf("SubbandInfo Size: %lu\n", sizeof(SubbandInfo));
    printf("Decoder total Size: %lu\n", \
            sizeof(MP3DecInfo) + sizeof(FrameHeader) + sizeof(SideInfo) + sizeof(ScaleFactorInfo) \
            + sizeof(HuffmanInfo) + sizeof(DequantInfo) + sizeof(IMDCTInfo) + sizeof(SubbandInfo));
#endif

    return mp3DecInfo;
}

