/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

/*
 * Code is based on a52dec package that is
 * Copyrighted 2000-2001 by Michel Lespinasse <walken@zoy.org>
 * and copyrighted 1999-2000 by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 */


#ifndef NMM_AC3DECODENODE_HPP
#define NMM_AC3DECODENODE_HPP NMM_AC3DECODENODE_HPP

#include "nmm/nmm_prefix.hpp"

#include <inttypes.h>
#include "nmm/multimedia/GenericProcessorNode.hpp"
#include "nmm/multimedia/interfaces/ITrackImpl.hpp"
#include "nmm/multimedia/ISyncResetImpl.hpp"

#include "nmm/plugins/NMMRegisterPlugin.hpp"

extern "C" {
  #include <a52dec/a52.h>
}

namespace NMM {
/**
* In this node the methods convert and float_to_int a taken from a52dec that was part
* of liba52 by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
* a part of his decode_data function is found here as well
*/
  class AC3DecodeNode :
    public GenericProcessorNode,
    public ITrackImpl,
    public ISyncResetImpl
  {
  private:
    Buffer *old_in_buffer;
    Timestamp timestamp;     // The timestamp
    long int delay;          // The duration of one output buffer at the audio sink.
    
    Timestamp last_valid_ts; // last valid incoming ts
    long int samples;        // samples since last valid incoming ts

    /**
    * liba52 uses a internal state machine, this variable is part of
    * most liba52 function calls
    */
    a52_state_t* a52_state;

    /* buffer to hold temporary data */
    uint8_t   buf[3840];
    uint8_t*  bufptr;
    uint8_t*  bufpos;

    /* pointer to start and end position of input buffer */
    uint8_t* buffer_start;
    uint8_t* buffer_end;

    /* current block that is decoded */
    int block;

    /* bias and (volume) level adjust for decoding */
    sample_t  level, bias;

    /* number of audio channels to convert ac3-data to */
    int       channels;

    /* here the samplerate of the ac3 stream can be found */
    int sample_rate;

    /* Bit rate of ac3 stream */
    int bit_rate;

    /* special flags that some liba52 function calls need */
    int flags;
    
   public:
    AC3DecodeNode( const char * = "AC3DecodeNode", StreamQueue::Mode = StreamQueue::MODE_SUSPEND, int = 20);
    ~AC3DecodeNode();
    Message* processBuffer( Buffer *in_buffer);
    
  protected:
    virtual Result doInit();
    virtual Result doDeinit();
    virtual Result doInitOutput();
    virtual Result doDeinitOutput();
    virtual Result doActivate();
    virtual Result doFlush();
    
    /**
     * Resets the parameters of timestamp computation.
     * The stream_counter and the sync_time are set to 0.
     * At the moment this node only produces buffers with 256 samples at
     * a sample_rate of 48000, so the delay-value is set to 5.333333
     * milliseconds per buffer.
     */
    virtual Result syncReset();
    
    // ITrack interface
    virtual Result started(const Interval& overall_duration);
    virtual Result startTrack(const string& file_name);
    virtual Result endTrack();
    virtual Result finished();
    
  private:
    /**
     * converts 32 Bit sample to 16 bits (by Aaron Holtzman)
     * @return converted 16 bit value
     */
    int16_t convert (int32_t);

    /**
     * converts buffer of float values to 16 bit integers depending on some flags (by Aaron Holtzman)
     * @param _f Pointer to the decoded audio data
     * @param s16 Destination pointer where converted audio data should be copied;
     * the converted data can be directly send to the DSP
     * @param flags flags that influence convertion process like A52_MONO or A52_STEREO
     */
    void float_to_int(float*, int16_t*, int);

    /**
     * decodes one block of ac3 stream
     * @return Message buffer with decoded data
     */
    Message *decodeBlock();
  };

}

#include "nmm/nmm_suffix.hpp"

#endif //NMM_AC3DECODENODE_HPP



