• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  LAME MP3 encoder for DirectShow
3  *  LAME encoder wrapper
4  *
5  *  Copyright (c) 2000-2005 Marie Orlova, Peter Gubanov, Vitaly Ivanov, Elecard Ltd.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
23 #if !defined(AFX_VITECENCODER_H__40DC8A44_B937_11D2_A381_A2FD7C37FA15__INCLUDED_)
24 #define AFX_VITECENCODER_H__40DC8A44_B937_11D2_A381_A2FD7C37FA15__INCLUDED_
25 
26 #if _MSC_VER >= 1000
27 #pragma once
28 #endif // _MSC_VER >= 1000
29 
30 #include <lame.h>
31 
32 
33 const unsigned int dwBitRateValue[2][14] =
34 {
35     {32,40,48,56,64,80,96,112,128,160,192,224,256,320},     // MPEG-1
36     {8,16,24,32,40,48,56,64,80,96,112,128,144,160}          // MPEG-2/2.5
37 };
38 /*
39 #define STEREO           0
40 #define JOINT_STEREO     1
41 #define DUAL_CHANNEL     2
42 #define MONO             3
43 */
44 
45 #define OUT_BUFFER_SIZE             16384
46 #define OUT_BUFFER_GUARD            8192
47 
48 #define OUT_BUFFER_MAX              (OUT_BUFFER_SIZE - OUT_BUFFER_GUARD)
49 
50 typedef struct {
51     DWORD   dwSampleRate;                   //SF in Hz
52     DWORD   dwBitrate;                      //BR in bit per second
53     vbr_mode    vmVariable;
54     DWORD   dwVariableMin;                  //specify a minimum allowed bitrate
55     DWORD   dwVariableMax;                  //specify a maximum allowed bitrate
56     DWORD   dwQuality;                      //Encoding quality
57     DWORD   dwVBRq;                         // VBR quality setting (0=highest quality, 9=lowest)
58     long    lLayer;                         //Layer: 1 or 2
59 
60     MPEG_mode ChMode;                       //Channel coding mode: see doc
61     DWORD   dwForceMS;
62 
63     DWORD   bCRCProtect;                    //Is CRC protection activated?
64     DWORD   bForceMono;
65     DWORD   bSetDuration;
66     DWORD   bCopyright;                     //Is the stream protected by copyright?
67     DWORD   bOriginal;                      //Is the stream an original?
68 
69     DWORD   dwPES;                          // PES header. Obsolete
70 
71     DWORD   dwEnforceVBRmin;
72     DWORD   dwVoiceMode;
73     DWORD   dwKeepAllFreq;
74     DWORD   dwStrictISO;
75     DWORD   dwNoShortBlock;
76     DWORD   dwXingTag;
77     DWORD   dwModeFixed;
78     DWORD   bSampleOverlap;
79 } MPEG_ENCODER_CONFIG;
80 
81 
82 class CEncoder
83 {
84 public:
85 
86     CEncoder();
87     virtual ~CEncoder();
88 
89     // Initialize encoder with PCM stream properties
90     HRESULT SetInputType(LPWAVEFORMATEX lpwfex, bool bJustCheck = FALSE);   // returns E_INVALIDARG if not supported
91     // GetInputType - returns current input type
GetInputType(WAVEFORMATEX * pwfex)92     HRESULT GetInputType(WAVEFORMATEX *pwfex)
93     {
94         if(m_bInpuTypeSet)
95         {
96             memcpy(pwfex, &m_wfex, sizeof(WAVEFORMATEX));
97             return S_OK;
98         }
99         else
100             return E_UNEXPECTED;
101     }
102 
103     // Set MPEG audio parameters
104     HRESULT SetOutputType(MPEG_ENCODER_CONFIG &mabsi);      // returns E_INVALIDARG if not supported or
105                                                             // not compatible with input type
106     // Return current MPEG audio settings
GetOutputType(MPEG_ENCODER_CONFIG * pmabsi)107     HRESULT GetOutputType(MPEG_ENCODER_CONFIG* pmabsi)
108     {
109         if (m_bOutpuTypeSet)
110         {
111             memcpy(pmabsi, &m_mabsi, sizeof(MPEG_ENCODER_CONFIG));
112             return S_OK;
113         }
114         else
115             return E_UNEXPECTED;
116     }
117 
118     // Set if output stream is a PES. Obsolete
SetPES(bool bPES)119     void SetPES(bool bPES)
120     {
121         m_mabsi.dwPES = false;//bPES;
122     }
123     // Is output stream a PES. Obsolete
IsPES()124     BOOL IsPES() const
125     {
126         return (BOOL)m_mabsi.dwPES;
127     }
128 
129     // Initialize encoder SDK
130     HRESULT Init();
131     // Close encoder SDK
132     HRESULT Close(IStream* pStream);
133 
134     // Encode media sample data
135     int Encode(const short * pdata, int data_size);
136     int GetFrame(const unsigned char ** pframe);
137 
138     // Returns block of a mp3 file, witch size integer multiples of cbAlign
139     int GetBlockAligned(const unsigned char ** pblock, int* piBufferSize, const long& cbAlign);
140 
141     HRESULT Finish();
142 
143 protected:
144     HRESULT updateLameTagFrame(IStream* pStream);
145     static HRESULT skipId3v2(IStream *pStream, size_t lametag_frame_size);
146     static HRESULT maybeSyncWord(IStream *pStream);
147     HRESULT SetDefaultOutputType(LPWAVEFORMATEX lpwfex);
148 
149     // Input media type
150     WAVEFORMATEX        m_wfex;
151 
152     // Output media type
153     MPEG_ENCODER_CONFIG m_mabsi;
154 
155     // Compressor private data
156     lame_global_flags * pgf;
157 
158     // Compressor miscelaneous state
159     BOOL                m_bInpuTypeSet;
160     BOOL                m_bOutpuTypeSet;
161 
162     BOOL                m_bFinished;
163     int                 m_frameCount;
164 
165     unsigned char *     m_outFrameBuf;
166     int                 m_outOffset;
167     int                 m_outReadOffset;
168 
169     CCritSec            m_lock;
170 };
171 
172 #endif // !defined(AFX_VITECENCODER_H__40DC8A44_B937_11D2_A381_A2FD7C37FA15__INCLUDED_)
173