/******************************************************************************
Copyright (c) 2007 Analog Devices, Inc.  All Rights Reserved.  This software is
proprietary and confidential to Analog Devices, Inc. and its licensors.
*******************************************************************************

$Revision: 1.1 $
$Date: 2008/03/27 03:03:18 $
$LastChangedBy: sto $

Project:    MP4 Audio File Parser
Title:      MP4 Audio File Parser header
Author:     CFJ

Description:
            MP4 Audio File Parser API header file

References:
            None

******************************************************************************/
#ifndef __ADI_MP4AD_PARSER_H__
#define __ADI_MP4AD_PARSER_H__


///////////////
// Macros

// Instance memory object sizes required
#define ADI_MP4AD_FASTB0_PRIO0_STATE_MEM_NUMBYTES   (304)

// Make API include version, so link will fail if versions do not match!!
#define adi_mp4ad_create adi_mp4ad_create_2_0

// MPEG-4 audio object type identification numbers for AAC-LC or HE-AAC bitstreams
#define ADI_MP4AD_AACLC_MPEG4_OBJECT    2
#define ADI_MP4AD_SBR_MPEG4_OBJECT      5

// Channel element identification in PCE
#define ADI_MP4AD_ELEMENT_IS_FRONT      0x10
#define ADI_MP4AD_ELEMENT_IS_SIDE       0x20
#define ADI_MP4AD_ELEMENT_IS_BACK       0x30
#define ADI_MP4AD_ELEMENT_IS_LFE        0x40
#define ADI_MP4AD_ELEMENT_IS_CPE        0x80
#define ADI_MP4AD_ELEMENT_TYPE_MASK     0x70
#define ADI_MP4AD_ELEMENT_TAG_MASK      0x0F

#define ADI_MP4AD_MAX_CH_ELE_NUM 68       // Maximum number of channel elements encoded in AAC payload

// enums

typedef enum
{
	ADI_MP4AD_SUCCESS 				    = 0,
	ADI_MP4AD_TERMINAL_FAILURE		    = 0xFFFF,
	ADI_MP4AD_NEED_MORE_INPUT		    = 0x0001,
	ADI_MP4AD_INVALID_CONFIG_PARAMS	    = 0X0002,
	ADI_MP4AD_OPERATION_FAILED		    = 0x0006,
	ADI_MP4AD_INVALID_BITSTREAM		    = 0x0012,
	ADI_MP4AD_STREAM_COMPLETE		    = 0x0013,
	ADI_MP4AD_SKIP_DATA_REQUEST         = 0x1104,
	ADI_MP4AD_UNKNOWN_OBJECT		    = 0x1105,
	ADI_MP4AD_SEEK_IGNORED			    = 0x1106,
	ADI_MP4AD_SEEK_TABLES_NOT_ALLOCATED = 0x1107
} adi_mp4ad_return_code_t;

typedef enum
{
	ADI_MP4AD_ALL_RECONFIG_PARAMS = 0,
} adi_mp4ad_config_item_t;


//////////////////
// Typedefs
typedef struct adi_mp4ad_t adi_mp4ad_instance_t;

typedef unsigned char adi_mp4ad_bitstream_data_t;

typedef struct  {
    unsigned int    version_num;
    char*           version_text;
} adi_mp4ad_module_info_t;

typedef struct
{
	int								max_num_bytes;
	char* 							tag_data;
	int								tag_length_num_bytes;
} adi_mp4ad_tag_text_t;

typedef struct
{
	adi_mp4ad_tag_text_t	song_title;
	adi_mp4ad_tag_text_t	artist;
	adi_mp4ad_tag_text_t	album;
	adi_mp4ad_tag_text_t	year;
	adi_mp4ad_tag_text_t	comments;
	adi_mp4ad_tag_text_t	genre;
} adi_mp4ad_metadata_t;


// Fixed length data types
#ifndef __ADSPBLACKFIN__
#error "Data types are specific to Blackfin only"
#endif
typedef unsigned char				adi_mp4ad_uint8;
typedef char						adi_mp4ad_int8;
typedef unsigned short				adi_mp4ad_uint16;
typedef short						adi_mp4ad_int16;
typedef unsigned int				adi_mp4ad_uint32;
typedef int							adi_mp4ad_int32;
typedef unsigned long int			adi_mp4ad_uint64;
typedef long int					adi_mp4ad_int64;


#define ADI_MP4AD_MAX_COMPATIBLE_BRANDS		(8)
typedef struct
{
	adi_mp4ad_uint32	major_brand;
	adi_mp4ad_uint32	minor_brand;
	adi_mp4ad_uint64	compatible_brands[ADI_MP4AD_MAX_COMPATIBLE_BRANDS];
	int					is_parsed;
} adi_mp4ad_ftyp_t;


typedef struct
{
	adi_mp4ad_uint64	creation_time;
	adi_mp4ad_uint64	modification_time;
	adi_mp4ad_uint32	timescale;
	adi_mp4ad_uint64	duration;
	adi_mp4ad_uint32	rate;
	adi_mp4ad_uint16	volume;
	adi_mp4ad_uint16	bits;
	adi_mp4ad_uint32	matrix[9];
	adi_mp4ad_uint32	next_track_ID;
} adi_mp4ad_mvhd_t;

typedef struct
{
	adi_mp4ad_uint64	creation_time;
	adi_mp4ad_uint64	modification_time;
	adi_mp4ad_uint32	track_ID;
	adi_mp4ad_uint64	duration;
	adi_mp4ad_uint16	layer;
	adi_mp4ad_uint16	alternate_group;
	adi_mp4ad_uint16	volume;
	adi_mp4ad_uint32	matrix[9];
	adi_mp4ad_uint32	width;
	adi_mp4ad_uint32	height;
} adi_mp4ad_tkhd_t;

typedef struct
{
	adi_mp4ad_uint64	creation_time;
	adi_mp4ad_uint64	modification_time;
	adi_mp4ad_uint32	timescale;
	adi_mp4ad_uint64	duration;
	adi_mp4ad_uint8		language[3];
	int					is_parsed;
} adi_mp4ad_mdhd_t;


#define ADI_MP4AD_HDLR_NAME_MAX_CHARS		(32)
typedef struct
{
	adi_mp4ad_uint32	handler_type;
	adi_mp4ad_uint8		name[ADI_MP4AD_HDLR_NAME_MAX_CHARS];
} adi_mp4ad_hdlr_t;

typedef struct
{
	adi_mp4ad_uint16	balance;
} adi_mp4ad_smhd_t;

typedef struct
{
	adi_mp4ad_uint16	entry_count;
} adi_mp4ad_dref_t;

typedef struct
{
	adi_mp4ad_uint32	entry_count;
	adi_mp4ad_uint16	channelcount;
	adi_mp4ad_uint16	samplesize;
	adi_mp4ad_uint32	samplerate;
} adi_mp4ad_stsd_t;

typedef struct
{
	int					is_parsed;				// Has this atom been parsed?
	// Elementary Stream Descriptor elements (14496-1:2004 sec 7.2.6.5)
	adi_mp4ad_uint32	tag;
	// Decoder Config Descriptor elements (14496-1:2004 sec 7.2.6.6)
	adi_mp4ad_uint8		object_type_indication;
	adi_mp4ad_uint32    max_bitrate_bps;
	adi_mp4ad_uint32    avg_bitrate_bps;
	// Audio Specific Config elements (14496-3:2005 sec 1.6)
	adi_mp4ad_uint32	audio_object_type;
	adi_mp4ad_uint32	sampling_frequency_index;
	adi_mp4ad_uint32	sampling_frequency;
	adi_mp4ad_uint32	channel_configuration;
	adi_mp4ad_int32		sbr_present_flag;
	adi_mp4ad_uint32	sbr_sampling_frequency_index;
	adi_mp4ad_uint32	sbr_sampling_frequency;
	// GA Specific Config parameters (14496-3:2005 sec 4.4.1)
	adi_mp4ad_uint32	frame_length_flag;
	adi_mp4ad_uint32	extension_flag;
	// Program Config Element parameters (14496-3:2005 sec 4.4.1.1)
	adi_mp4ad_uint32	object_type;
	adi_mp4ad_uint32	sampfreq_idx;
	adi_mp4ad_uint32	num_front_channel_elements;
	adi_mp4ad_uint32	num_side_channel_elements;
	adi_mp4ad_uint32	num_back_channel_elements;
	adi_mp4ad_uint32	num_lfe_channel_elements;
	adi_mp4ad_uint32	num_assoc_data_elements;
	adi_mp4ad_uint32	num_valid_cc_elements;
	adi_mp4ad_uint32	mono_mixdown_present;
	adi_mp4ad_uint32	stereo_mixdown_present;
	adi_mp4ad_uint32	matrix_mixdown_idx_present;
	adi_mp4ad_uint32	valid_cc_element_tag_select[16];
	adi_mp4ad_uint32	comment_field_bytes;
	adi_mp4ad_uint32	channel;
	adi_mp4ad_int8      pce_tag;
	adi_mp4ad_int8      channel_element_id[ADI_MP4AD_MAX_CH_ELE_NUM]; // 4-bit channel element type + 4-bit channel element tag

} adi_mp4ad_esds_t;

typedef struct
{
	adi_mp4ad_ftyp_t							ftyp;				// 4.3
	adi_mp4ad_mvhd_t							mvhd;				// 8.3
	adi_mp4ad_tkhd_t							tkhd;				// 8.3
	adi_mp4ad_mdhd_t							mdhd;				// 8.8
	adi_mp4ad_hdlr_t							hdlr;				// 8.9
	adi_mp4ad_smhd_t							smhd;				// 8.11
	adi_mp4ad_dref_t							dref;				// 8.12
	adi_mp4ad_stsd_t							stsd;				// 8.16
	adi_mp4ad_esds_t							esds;				//
} adi_mp4ad_container_info_t;

typedef enum
{
	ADI_MP4AD_WARNING_NONE					= 0x0000,	// No warning
	ADI_MP4AD_WARNING_INDEX_MEMORY_TOO_SMALL= 0x0001,   // Table size for indexing information is too small
	ADI_MP4AD_WARNING_BRANDS_NOT_LISTED     = 0x0020,   // some compatible brands could not be stored
	ADI_MP4AD_WARNING_DRM_PROTECTED         = 0x1000    // Apple DRM protected content
} adi_mp4ad_warning_info_bitfield_t;

// Decoder output status
typedef struct
{
	unsigned int						warning_info;
	adi_mp4ad_bitstream_data_t*			unconsumed_input_data_ptr;
	long int							num_input_bytes_consumed;
	int									next_object_length_num_bytes;
	int									next_object_id;
	long int 							data_unit_stream_offset;
	long int 							data_unit_time_ms;
    int                                 data_unit_num_frames;
	int                                 index_mem_used_bytes;
} adi_mp4ad_output_status_t;

// API structures
// This structure contains pointers to memory pools that need to be passed to
// the parser via the function adi_mp4ad_create()
typedef struct
{
    struct
    {
        void *prio0;
    } fastb0;
} adi_mp4ad_state_mem_t;

typedef struct
{
} adi_mp4ad_scratch_mem_t;

typedef struct
{
	adi_mp4ad_state_mem_t		state;
	adi_mp4ad_scratch_mem_t		scratch;
} adi_mp4ad_mem_t;

typedef struct
{
	adi_mp4ad_bitstream_data_t* circular_buffer_base;
	int 						circular_buffer_length;
	unsigned int				use_circular_bitstream_buffer;
	adi_mp4ad_metadata_t*		text_fields_ptr;
	adi_mp4ad_container_info_t*	container_info_ptr;
	void*						index_mem_ptr;
	int							index_mem_num_bytes;
} adi_mp4ad_config_t;


//////////////////
//  Glocal Constants Extern Declarations
extern const adi_mp4ad_module_info_t adi_mp4ad_module_info;


//////////////////
// Parser API Prototypes
#if defined(__cplusplus)
extern "C" { /* this is added to support integration into C++ systems */
#endif

adi_mp4ad_instance_t* adi_mp4ad_create (
	adi_mp4ad_mem_t* per_instance_mem_blocks
);

adi_mp4ad_return_code_t adi_mp4ad_init (
	adi_mp4ad_instance_t *parser_instance,
	adi_mp4ad_config_t *config_params
);


adi_mp4ad_return_code_t adi_mp4ad_reconfigure (
    adi_mp4ad_instance_t *instance_handle,
    adi_mp4ad_config_item_t config_item,
    adi_mp4ad_config_t *config_params
);


// Parses variables from the bitstream
adi_mp4ad_return_code_t adi_mp4ad_parser (
	adi_mp4ad_instance_t *parser,
	int length,
	adi_mp4ad_bitstream_data_t *bytes,
	adi_mp4ad_output_status_t *status
);

adi_mp4ad_return_code_t adi_mp4ad_seek (
	adi_mp4ad_instance_t *parser,
	int seek_time_ms,
	int curr_time_ms,
	int curr_stream_byte_offset,
	adi_mp4ad_output_status_t *status
);

adi_mp4ad_return_code_t adi_mp4ad_get_next_audio_data_unit (
	adi_mp4ad_instance_t *parser,
	adi_mp4ad_output_status_t *status
);


#if defined(__cplusplus)
}
#endif

#endif // __ADI_MP4AD_PARSER_H__


/*
**
** EOF: $HeadURL: svn://dingofiles.spd.analog.com/audio/trunk/mp4a-parser/include/adi_mp4ad_parser.h $
**
*/
