home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Audio / Audio.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  6.7 KB  |  277 lines

  1.  
  2. /* 
  3.  *  Audio.h
  4.  *
  5.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  6.  *
  7.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  8.  *    
  9.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2, or (at your option)
  12.  *  any later version.
  13.  *   
  14.  *  FlasKMPEG is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *   
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with GNU Make; see the file COPYING.  If not, write to
  21.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  22.  *
  23.  */
  24.  
  25. #if !defined(AFX_AUDIO_H__8B6E2FE2_9AC7_11D3_8C3D_00000100CF13__INCLUDED_)
  26. #define AFX_AUDIO_H__8B6E2FE2_9AC7_11D3_8C3D_00000100CF13__INCLUDED_
  27.  
  28.  
  29. #if _MSC_VER > 1000
  30. #pragma once
  31. #endif // _MSC_VER > 1000
  32.  
  33. #include "..\Demux\Demux.h"
  34. #include ".\Resampler\Resampler.h"
  35. #include ".\AudFrameBuffer.h"
  36. #include "A52\CA52Dec.h"    
  37. #include "MPEG\MPEGDec.h"
  38. #include ".\Utils\AudioCompressor.h"
  39.  
  40. #define  AUDIO_BUFFER_SIZE 0x80000
  41. #define  PCM_BUFFER_SIZE 96000
  42. #define AC3_PCM_SAMPLES 1536 //2 channels * 16 bit * 1536 samples
  43. #define   PCM_1SECOND_SIZE 256000
  44. #define   SEQUENCE_END_CODE       0x1B7
  45. #define   AC3_SYNC_WORD    0x0B77
  46. #define   MPEG_SYNC_WORD   0x0FFF
  47.  
  48.  
  49.  
  50. // Format defines
  51. #define  AC3_FORMAT  0
  52. #define  MPEG_FORMAT 1
  53.  
  54. enum AudioFormat{ Ac3=0, MpegAudio , Lpcm, Dts, Sdds, UNknown};
  55.  
  56. //Audio start options
  57. #define NO_AUDIO        0
  58. #define DSC                  1
  59. #define DO_AUDIO        2
  60. #define AUDIO_PREVIEW     4
  61. #define AUDIO_DONT_NORMALIZE 8
  62.  
  63. #ifndef ABS
  64. #define ABS(x)         ( (x>0)? x: -x)
  65. #endif
  66.  
  67. #define CLK_ERROR(x,y) (ABS( (i64)x - (i64)y) )
  68. #define CLK_THRESHOLD       250
  69. #define CLK_THRESHOLD_SPAN  1000
  70. struct TTimeSpan{
  71.     i64  start;
  72.     i64  end;
  73. };
  74.  
  75. struct PESdata{
  76.     PESinfo         pInfo;
  77.      ui8           *data;
  78.     ui32            datasize;
  79. };
  80.  
  81. #define MAX_AUDIO_TRACKS 16
  82. struct TAudioInfo
  83. {
  84.   AudioFormat format;
  85.   int bit_rate;
  86.   int channels;
  87.   int subwoofer;
  88.   int sample_rate;
  89. };
  90.  
  91.  
  92. typedef struct tagAudioTrack
  93. {
  94.   ui8         nStreamId;    // Indicates stream Id
  95.   ui8         nSubStreamId; // Indicates substream Id
  96. } TAudioTrack;
  97.  
  98. #if 0
  99. typedef struct tagAudioIds
  100. {
  101.   TAudioTrack vTracks[MAX_AUDIO_TRACKS];
  102.   int   nCount;
  103.   int   nSelected;
  104. } TAudioTracks;
  105. #endif 
  106.  
  107. struct TReadSpanRunState{
  108.     bool    firstTime;
  109.     bool    frame_in_course;
  110.     bool    header_in_course;
  111.  
  112.     ui64    lastSCR;
  113.     ui32    lastPackBytes;
  114.     ui32    lastMuxrate;
  115.  
  116.     TAudFramePresInfo frame_pInfo;
  117.  
  118.     ui32    frame_size;
  119.     ui32    frame_remaining_bytes;
  120.     ui8     frame_data[65000];
  121.     ui32    out_ptr;
  122.     ui32    in_ptr;
  123.  
  124.      i64    delta;
  125.     PESdata PES;
  126. };
  127.  
  128. // Maximum range 1200 = +/- 12 dB
  129. #define TAUDIOPROPERTIES_MCHANNEL_MAX_RANGE 1200 
  130. struct TAudioProperties
  131. {
  132.   // The following properties can be altered during the conversion.
  133.   bool drc;                 // The Dynamic Range Compression is on
  134.   int  drc_value;           // Value of the Dynamic Range Compression
  135.   bool multichannel_volume; // Multichannel audio is being modified in volume
  136.   int  rear;                // Values for multichannel audio volumes
  137.   int  front;
  138.   int  center;
  139.   bool dolby_surround_downmix;
  140.   bool normalize;           // Is normalizing
  141.   int  normalize_value;     // Value to normalize
  142.   TAudioTrack sAudioTrack;  // Track to play
  143. };
  144.  
  145. struct TFrameSize
  146. {
  147.     ui16 bit_rate;
  148.     ui16 frm_size[3];
  149. };
  150.  
  151.  
  152.  
  153. class Audio: public CDemux, public CDataSource
  154. {
  155.  
  156. public:
  157.   // Returns basic info of the current stream. Returns NULL if the info is not available.
  158.   TAudioInfo * GetAudioInfo();
  159.  
  160.   // This must be serialize with calls to GetSamples()
  161.   void SetTrack(TAudioTrack *pAudioTrack);
  162.  
  163.     int GetSamples(int frame, short **buffer, int nSamples);
  164.   void GetAudioProperties(TAudioProperties *pAudProps)
  165.   {
  166.     *pAudProps = m_sAudioProperties;
  167.   }
  168.   void SetAudioProperties(TAudioProperties *pAudProps)
  169.   {
  170.     m_sAudioProperties = *pAudProps;
  171.   }
  172.  
  173.   // This is the sample rate of the input
  174.   // audio. We don't allow multiple streams
  175.   // with different sample rates. Unlikely to happen though.
  176.     int sampleRate;
  177.  
  178.  
  179.     int GetAudioDSC(i64 PTS, i64 videoStreamPos);
  180.     int GetAudio();
  181.  
  182.     int Stop();
  183.     int Start(char* inputFile, int audioMode);
  184.     bool Start(int out_sFreq, int audioMode, TAudioProperties *pAudProps );
  185.  
  186.     int SetAudioMode(int audioMode); 
  187.   
  188.   AudioFormat GetFormat() { return m_nFormat; }
  189.  
  190.     int read(char *buffer);
  191.  
  192.   bool Init( LPTWorkingMism pMismInfo );
  193.     Audio();
  194.     ~Audio();
  195.   
  196. private:
  197.   bool SetTrackIdsAndFormat(TAudioTrack *pTrack);
  198.   // Audio format in progress
  199.   AudioFormat m_nFormat;
  200.   // Stream in progress
  201.   ui8 m_nSubStreamID;
  202.   ui8 m_nStreamID;
  203.  
  204.     int decodeFrame( CAudioFrame *frame ,short *pcm_samples);
  205.     int ParseHeader(ui32 headers_found);
  206.     int FindHeader(ui32 headers_found);
  207.     int RetrieveFrameData();
  208.     int ReadSpanInit();
  209.     void ParseFrameData();
  210.  
  211.   //bool GetTrackProperties(TAudioTrack *pTrack);
  212.   bool GetPropertiesFromFrame(AudioFormat nFormat, ui8* pFrame, TAudioInfo *pTrackInfo);
  213.   void Normalize(i16 *pData, ui32 nDataSize, float gain);
  214.  
  215.     CA52Dec  *m_pA52Dec;
  216.     CMPEGDec *MPEGDec;
  217.  
  218.     CAudioCompressor AC;
  219.  
  220.   TAudioProperties m_sAudioProperties;
  221.  
  222.   bool  m_bIsPreview;
  223.   bool  m_bDontNormalize;
  224.     int                 ReadSpan( TTimeSpan *span, i64 time);
  225.     TReadSpanRunState   read_state;
  226.  
  227.     int audioMode;
  228.     
  229.  
  230.   bool m_bAudioInfoIsValid;
  231.   TAudioInfo    m_sAudioInfo;
  232.     CAsyncBuffer *decoded_samples_buffer;
  233.     CAsyncBuffer *ResamplerBuffer;
  234.     CAsyncBuffer *compressed_samples_buffer;
  235.     CAsyncBuffer *destination_buffer;
  236.  
  237.     CResampler          *resampler;
  238.     CAudFrameBuffer     *frameFIFO;
  239.  
  240.     bool firstPTSfound;
  241.     i64 prevSysClk;
  242.  
  243.     //read() variables. Cannot be static because they can't be shared
  244.     //                  between object instances
  245.     int   eof_flag;
  246.     bool  frame_in_progress;
  247.     ui32  fip_remaining_bytes;
  248.     short temp_decoded_frame[1536*2];
  249.     ui32  temp_decoded_ptr;
  250.      i64  aud_clk;
  251.     ui32  tot_n_samples;
  252.     // GetAudioDSC()
  253.     i64 firstPTS;
  254.  
  255.     // ParseHeader()
  256.     int header_pos;
  257.     ui8 hdr2, hdr3, mpeg_header[3];
  258.  
  259.     // MPEG audio stuff
  260.     ui8  bit_rate_index;
  261.   ui8  mode;
  262.     ui32 bitrate;
  263.     ui32 mpeg_layer;
  264.     ui8  sampling_frequency;
  265.     ui8  padding_bit;
  266.     ui32 N;
  267.  
  268.  
  269.     bool   justStarted;
  270.     bool   stopDecoder;
  271.     bool   decoderStopped;
  272.     char   buffer[AUDIO_BUFFER_SIZE];        
  273.     FILE            *hOutputFile;
  274. };
  275.  
  276. #endif // !defined(AFX_AUDIO_H__8B6E2FE2_9AC7_11D3_8C3D_00000100CF13__INCLUDED_)
  277.