/******************************************************************************
Copyright (c) 2006 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:19 $
$LastChangedBy: sto $

Project:    AAC Decoder
Title:      MP4 parsing modules for the simple I/O application
Author:     CVN

Description:
            This file contains some modules used for parsing MP4 files
            within the simple I/O application

References:
            None

******************************************************************************/

#ifndef MP4AD_PARSING_PROCESS_H
#define MP4AD_PARSING_PROCESS_H

#include <stdio.h>
#include "adi_mp4ad_parser.h"
#include "adi_heaacv2_decoder.h"
#include "circular_buffer.h"

/****************************************************************************
 *      #defines                                                            *
 ****************************************************************************/



/****************************************************************************
 *      Typedefs/Enumerations                                               *
 ****************************************************************************/


/****************************************************************************
 *      Structure Declarations                                              *
 ****************************************************************************/
typedef struct {
    adi_mp4ad_instance_t*   parser_handle;
    int                     num_frames_to_read; // number of frames left in the current chunk
    FILE*                   input_file;         // file handle for input mp4 file
    int                     curr_stream_offset;	// Current file offset in stream
    int                     end_of_stream_encountered_flag;
} mp4_file_index_t;


/****************************************************************************
 *      Function Declarations                                               *
 ****************************************************************************/


/*
**
** Function:            copy_mp4ad_config
**
** Description:         Copies the audio configuration required to initialise
**                      the AAC decoder, parsed from an MP4 file
**
** Arguments:
**
**  requested_dec_config - decoder configuration structure
**  parser_config        - parser configuration structure which contains the pointers
**                          to the container information struct and the song tags struct
**                          filled by the parser
**  print_device         - file handle for printing out diagnostic info (disabled if NULL)
**
** Outputs:             requested_dec_config filled with parsed audio config
**
** Return value:        0 = success
**                      -1 = the file does not contain AAC-LC or HE-AAC audio, so decoder is not configured
**
**
*/
int copy_mp4ad_config(  adi_aacd_config_t* requested_dec_config,
                        adi_mp4ad_config_t* parser_config,
                        FILE *print_device);

/*
**
** Function:            perform_mp4ad_parsing_loop
**
** Description:         Performs the file parsing logic, where the input parser buffer is
**                      processed by the mp4 file parser module. 
**
** Arguments:
**
**   adi_mp4ad_parser_instance  - parser instance to use for parsing this file
**   parser_buffer              - circular buffer object to use for parsing this file
**   parser_output_status       - pointer to parser output status struct so user can access the output status of the parser
**   print_device               - file handle for printing out diagnostic info (disabled if NULL)
**
**
** Outputs:             parser_output_status
**
** Return value:        >0 = remaining number of bytes size of a large atom that needs to be skipped in the file
**                      0  = more data needs to be added to the parser buffer
**                      -1 = parser initialisation failed (cannot parse the file)
**                      -2 = parser buffer too small (file only partially parsed)
**                      -3 = ADI_MP4AD_INVALID_BITSTREAM returned by parser (cannot parse the file)
**                      -4 = fatal error returned by parser (cannot parse the file)
*/
int perform_mp4ad_parsing_loop(
        adi_mp4ad_instance_t*   adi_mp4ad_parser_instance,
        cbuffer_t*              parser_buffer,
    	adi_mp4ad_output_status_t* parser_output_status,
        FILE*                   print_device
);

/*
**
** Function:            init_mp4ad_file_access
**
** Description:         initialises the data structure used to store the
**                      state information required to access audio data chunks
**                      from a MP4 file.
**
** Arguments:
**
**   current_file_access_unit_state - data struct containing state info related to the access unit
**   adi_mp4ad_parser_instance      - parser instance to use for demultiplexing audio from this file
**   infile                         - file handle for the input mp4 audio file
**
**
** Outputs:             initialised data struct current_file_access_unit_state
**
** Return value:        none
**
**
*/
void init_mp4ad_file_access(
        mp4_file_index_t*       current_file_access_unit_state,
        adi_mp4ad_instance_t*   adi_mp4ad_parser_instance,
        FILE*                   infile
);

/*
**
** Function:            demultiplex_audio_data_from_mp4_file
**
** Description:         Fills the decoder buffer with data from the current 
**                      audio access unit in the MP4 file. If the end of the access unit
**                      is reached, then the location of the next audio access unit
**                      in the file is found.
**
** Arguments:
**
**   current_file_access_unit_state - data struct containing state info related to the access unit
**   decoder_buffer                 - circular buffer object to use for decoding this file
**   decoder_needs_more_data        - flag indicating if the decoder last returned ADI_AACD_NEED_MORE_INPUT
**   read_request                   - number of bytes requested  to be added to the decoder buffer
**   print_device                   - file handle for printing out diagnostic info (disabled if NULL)
**
**
** Outputs:             end-of-stream indicator (within current_file_access_unit_state)
**                      curr_stream_offset (within current_file_access_unit_state)
**                      number of bytes left to process in current audio access unit
**                           (within current_file_access_unit_state)
**
** Return value:        0  = success
**                      1  = left over data at end of access unit was flushed - resynch should be called 
**                      -1 = index tables do not exist - cannot read any audio data from file
**                      -2 = unknown problem preventing read of new data from next access unit
**
**
*/
int demultiplex_audio_data_from_mp4_file(
        mp4_file_index_t*       current_file_access_unit_state,
        cbuffer_t*              decoder_buffer,
        int                     decoder_needs_more_data,
        int                     read_request,
        FILE*                   print_device
);

/*
**
** Function:            perform_mp4_file_seeking
**
** Description:         Performs the file seeking logic, where the parser module is used
**                      to update the input file pointer and the current playback time.
**
** Arguments:
**   current_file_access_unit_state - data struct containing state info related to the access unit
**   seek_time_millisecs            - desired playback timestamp to seek to
**   curr_time_millisecs_ptr        - pointer to variable storing the current playback timestamp
**   decoder_buffer                 - circular buffer object used by system for decoding this mp4 file
**   print_device                   - file handle for printing out diagnostic info (disabled if NULL)
**
**
** Outputs:             *curr_time_millisecs_ptr - time stamp for data about to be decoded
**                      curr_stream_offset (within current_file_access_unit_state)
**                      number of bytes left to process in current audio access unit
**                           (within current_file_access_unit_state)
**
** Return value:        none
*/
void perform_mp4_file_seeking(  
        mp4_file_index_t*       current_file_access_unit_state,                        
    	int	                    seek_time_millisecs,
	    int*                    curr_time_millisecs_ptr,
        cbuffer_t*              decoder_buffer,                                        
        FILE*                   print_device                                           
);

#endif /* MP4AD_PARSING_PROCESS_H */

/*
**
** EOF: $HeadURL: svn://dingofiles.spd.analog.com/audio/branches/aac-decoder/3.0.x/example/file-formats/include/mp4ad_parsing_process.h $
**
*/
