/* 
 *  syncing.txt
 *
 *	Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
 *
 *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
 *	
 *  FlasKMPEG 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; either version 2, or (at your option)
 *  any later version.
 *   
 *  FlasKMPEG 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 GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */


Syncing audio...


Goal:  
       Achieve tight syncronization taking MPEG timestamps as reference

Requirements:
       Routine must output raw PCM samples that follow the timing
	   info in the input MPEG stream.


Procedure overview:
    -The process start when some routines ask for some PCM samples
	 from the input stream.
	-From the number of PCM samples demanded we can know how much time
	 of the imput stream we have to parse. 
	-After parsing the input data we get possible audio frames ready
	 to decode, that can carry associated PTS. (presentation time stamps)
	-Taking the input stream clock as reference we decode the found frames
	 into PCM samples, and fill the possible gaps with no sound,
	 or overlap the audio frames if their PTS collide. 


Data structures:

struct TTimeSpan{
	ui64  start;
	ui64  end;
}

CAudFrameBuffer  frameFIFO;

ReadSpan( TTimeSpan *span ):
   This function calls ReadLPES() to retrieve PES packets from the
   input stream. 
   The funcion adds the frames it founds to the frame FIFO, and 
   updates the span structure with new values that indicate
   the lenght of the processed span.

ProcessSpan( TTimeSpan *span );
	This function decodes to PCM samples the span taking the frames from the FIFO
	It also removes the already used audio frames from the fifo.

ui32 GetPCMSamples( ui32 samples )
	This function is a wrapper that calls ProcessSpan and ReadSpan.
	samples indicates the number of samples we want, the return value
	indicates the number of samples we got back. 



Further talk
------------
	   Clock discontinuities
	   ---------------------
	     Is very usual to find MPEG streams in which the SCR clock embedded in the
		 stream is reset'd right at the middle.
		   Clock discontinuities are handled at the Demuxer level though the ReadLPES()
		 function (Read Linear PES()). The funcion is similar to ReadPES() with the exception
		 that the time presented to the upper layers (PTSs and SCRs) always grows and never resets.

	      

