/******************************************************************************
Copyright (c) 2006 Analog Devices, Inc.  All Rights Reserved.  This software is
proprietary and confidential to Analog Devices, Inc. and its licensors.
*******************************************************************************/
/**
 * @file adi_mp3_dec.h
 * @MP3 decoder API
 *
 *	Description:	MP3 decoder header file.
 *	Copyright:		Analog Devices (c) 2006
 *	Owner:			Analog Devices
 *
 *	Revision:
 *
 *					February 07,2006 	Initial Revision
 *
 */

#ifndef ADI_MP3_DEC__H
#define ADI_MP3_DEC__H

#include    "adi_basic_types.h"

typedef int8_t  adi_mp3_dec_bitstream_data_t;
typedef int16_t adi_mp3_dec_audio_data_t;

#define false 0
#define true 1

//==========================================================
//	adi_mp3dec_config_item_t
// =========================================================

typedef enum{
	NOMORE_DATA_WITHAPP		= 1,
	USE_CIRCULAR_BUFFER		= 2,
	INPUT_BUFFER_LENGTH		= 3,
	ALL_CONFIG_PARAMS       = 4
}adi_mp3_dec_config_item_t;

//==========================================================
//	adi_mp3dec_frame_properties_t
// =========================================================
typedef struct
{
	/* Sampling rate */
    int32_t nSampleRate;
	int32_t iBitRate;
	int32_t nMpegType;
	int32_t nLayerNum;
	int32_t num_output_channels;
	int32_t isvbr;
	uint32_t iFrameDuration;
	uint32_t iCurrentFrameSize;
	uint32_t avg_bitrate_in_bytes_persec;
}adi_mp3_dec_frame_properties_t;

//==========================================================
//	adi_mp3dec_config_t
// =========================================================

typedef struct {
	int32_t  noMoreDataWithApp;
	int32_t  use_circular_buffer;
	int32_t  input_buffer_length;
	int32_t  max_bytes_to_search_for_sync;
	adi_mp3_dec_bitstream_data_t *input_buffer_base_addr;
	adi_mp3_dec_frame_properties_t *frame_properties;
} adi_mp3_dec_config_t;


//==========================================================
//	adi_mp3_dec_memory_pool_t
// =========================================================
typedef struct {
	void *onchip_block0;
	void *onchip_block1;
	void *onchip;
	void *offchip;
} adi_mp3_dec_memory_pool_t;


//==========================================================
//	adi_mp3_dec_instance_t
// =========================================================
typedef void adi_mp3_dec_instance_t;


//==========================================================
//	di_mp3_dec_outputinstance_t
// =========================================================
typedef struct {
	int32_t frame_completed;
	int32_t num_output_channels;
    int32_t num_output_samples_per_channel;
	adi_mp3_dec_bitstream_data_t *dec_read_ptr;
	adi_mp3_dec_frame_properties_t *frame_properties_ptr;
	int32_t num_input_bytes_consumed;
} adi_mp3_dec_output_status_t;


//==========================================================
//	MP3 decoder memory requirements
// =========================================================
#define ADI_MP3_DEC_MAX_NUM_CH		2
#define ADI_MP3_DEC_MAX_OUTBUFSIZE	(1152*ADI_MP3_DEC_MAX_NUM_CH)

#define ADI_MP3_DEC_MIN_BITSTREAM_BUFSIZE			1440 // bytes

// The following sizes are all in 32-bit words
#define ADI_MP3_DEC_STATE_ON_CHIP_BLOCK0		 	2183
#define ADI_MP3_DEC_STATE_ON_CHIP_BLOCK1		 	1090
#define ADI_MP3_DEC_STATE_ON_CHIP					2
#define ADI_MP3_DEC_STATE_OFF_CHIP					2

#define ADI_MP3_DEC_SCRATCH_ON_CHIP_BLOCK0			1941
#define ADI_MP3_DEC_SCRATCH_ON_CHIP_BLOCK1			2
#define ADI_MP3_DEC_SCRATCH_ON_CHIP					2
#define ADI_MP3_DEC_SCRATCH_OFF_CHIP				2


//==========================================================
//	MP3 decoder error Codes
//==========================================================
#define ADI_MP3_DEC_SUCCESS						0x100
#define ADI_MP3_DEC_FREE_FORMAT					0x101
#define ADI_MP3_DEC_NEED_MORE_DATA				0x102
#define ADI_MP3_DEC_STREAM_COMPLETE				0x103
#define ADI_MP3_DEC_INVALID_CONFIG_PARAMS		0x104
#define ADI_MP3_DEC_INVALID_FILE				0x105
#define ADI_MP3_DEC_TIMEOUT_REACHED             0x106

//==========================================================
//	Revision number defines
//==========================================================
#define ADI_MP3D_LIBRARY_INFO "ADI_MP3_Decoder_Rev_2.0.6"
#define adi_mp3_dec_create adi_mp3_dec_create_2_0


//==========================================================
//	Section defines, used internally
//==========================================================
#define SEG_MP3_FAST_CODE		    section("MP3_code")
#define SEG_MP3_FAST_CONST			section("MP3_const")
#define SEG_MP3_FAST_CONST0			section("MP3_const0")
#define SEG_MP3_FAST_CONST1			section("MP3_const1")
#define SEG_MP3_SLOW_CODE		    section("MP3_slowcode")
#define SEG_MP3_SLOW_CONST			section("MP3_slowconst")


//==========================================================
//	Miscellaneous definitions
//==========================================================
/* This is to set how many bytes the decoder should search for
   the sync marker before giving up. Note that the user can
   set any search range value other than this one. */
#define ADI_MP3_DEC_THRESHOLD_FOR_INPUT	(128*1024)



//==========================================================
//	function prototypes
//==========================================================
adi_mp3_dec_instance_t *adi_mp3_dec_create(
    adi_mp3_dec_memory_pool_t *state,
	adi_mp3_dec_memory_pool_t *scratch
);

int32_t adi_mp3_dec_init(
    adi_mp3_dec_instance_t *decoder_instance,
    const adi_mp3_dec_config_t *requested_dec_config,
	adi_mp3_dec_config_t *actual_decoder_config
);

void adi_mp3_dec_reconfigure(
    adi_mp3_dec_instance_t *decoder_instance,
	const adi_mp3_dec_config_item_t config_item,
	const int32_t config_value,
	adi_mp3_dec_config_t *actual_decoder_config
);

void adi_mp3_dec_resynch(
    adi_mp3_dec_instance_t *decoder_instance
);

int32_t adi_mp3_decoder(
    void *dec_instance,
	const int32_t num_input_bytes,
	const adi_mp3_dec_bitstream_data_t *input_bitstream,
	adi_mp3_dec_output_status_t *dec_output_status,
    adi_mp3_dec_audio_data_t *output_pcm_channels[]
);

int MP3_Decode_File(char *filename);

#endif // #ifndef ADI_MP3_DEC__H
