1 /*
2  * Musepack audio compression
3  * Copyright (c) 2005-2009, The Musepack Development Team
4  * Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifndef MPCENC_MPCENC_H
22 #define MPCENC_MPCENC_H
23 
24 #include "libmpcenc.h"
25 #include "libmpcpsy.h"
26 #include <mpc/datatypes.h>
27 #include <mpc/minimax.h>
28 
29 //// optimization/feature defines //////////////////////////////////
30 #ifndef NOT_INCLUDE_CONFIG_H
31 # include "config.h"
32 #endif
33 
34 //// portable system includes //////////////////////////////////////
35 #include <stddef.h>
36 #include <math.h>
37 
38 //// system dependent system includes //////////////////////////////
39 // low level I/O, where are prototypes and constants?
40 #if   defined _WIN32  ||  defined __TURBOC__  ||  defined __ZTC__  ||  defined _MSC_VER
41 # include <io.h>
42 # include <fcntl.h>
43 # include <windows.h>
44 #elif defined __unix__  ||  defined __linux__  ||  defined __APPLE__
45 # include <unistd.h>
46 # include <locale.h>
47 # include <langinfo.h>
48 #else
49 // .... add Includes for new Operating System here (with prefix: #elif defined)
50 # include <unistd.h>
51 #endif
52 
53 #if   defined __linux__
54 #  include <fpu_control.h>
55 #elif defined __FreeBSD__
56 # include <machine/floatingpoint.h>
57 #elif defined _MSC_VER
58 # include <float.h>
59 #endif
60 
61 
62 #if !defined(__APPLE__)
63 // use optimized assembler routines for Pentium III/K6-2/Athlon (only 32 bit OS, Intel x86 and no MAKE_xxBITS)
64 // you need the NASM assembler on your system, the program becomes a little bit larger and decoding
65 // on AMD K6-2 (x3), AMD K6-III (x3), AMD Duron (x1.7), AMD Athlon (x1.7), Pentium III (x2) and Pentium 4 (x1.8) becomes faster
66 #define USE_ASM
67 
68 #endif
69 
70 // Use termios for reading values from keyboard without echo and ENTER
71 #define USE_TERMIOS
72 
73 // make debug output in tags.c stfu
74 #define STFU
75 
76 #if INT_MAX < 2147483647L
77 # undef USE_ASM
78 #endif
79 
80 #ifndef O_BINARY
81 # ifdef _O_BINARY
82 #  define O_BINARY              _O_BINARY
83 # else
84 #  define O_BINARY              0
85 # endif
86 #endif
87 
88 #if defined _WIN32  ||  defined __TURBOC__
89 # define strncasecmp(__s1,__s2,__n) strnicmp ((__s1), (__s2), (__n))
90 # define strcasecmp(__s1,__s2)      stricmp  ((__s1), (__s2))
91 #endif
92 
93 #if defined _WIN32
94 # include <direct.h>
95 # define snprintf                   _snprintf
96 # define getcwd(__buff,__len)       _getcwd ((__buff), (__len))
97 #endif
98 
99 //// Binary/Low-Level-IO ///////////////////////////////////////////
100 //
101 
102 #if   defined __BORLANDC__  ||  defined _WIN32
103 # define FILENO(__fp)          _fileno ((__fp))
104 #elif defined __CYGWIN__  ||  defined __TURBOC__  ||  defined __unix__  ||  defined __EMX__  ||  defined _MSC_VER
105 # define FILENO(__fp)          fileno  ((__fp))
106 #else
107 # define FILENO(__fp)          fileno  ((__fp))
108 #endif
109 
110 
111 //
112 // If we have access to a file via file name, we can open the file with an
113 // additional "b" or a O_BINARY within the (f)open function to get a
114 // transparent untranslated data stream which is necessary for audio bitstream
115 // data and also for PCM data. If we are working with
116 // stdin/stdout/FILENO_STDIN/FILENO_STDOUT we can't open the file with these
117 // attributes, because the files are already open. So we need a non
118 // standardized sequence to switch to this mode (not necessary for Unix).
119 // Mostly the sequence is the same for incoming and outgoing streams, but only
120 // mostly so we need one for IN and one for OUT.
121 // Macros are called with the file pointer and you get back the untransalted file
122 // pointer which can be equal or different from the original.
123 //
124 
125 #if   defined __EMX__
126 # define SETBINARY_IN(__fp)     (_fsetmode ( (__fp), "b" ), (__fp))
127 # define SETBINARY_OUT(__fp)    (_fsetmode ( (__fp), "b" ), (__fp))
128 #elif defined __TURBOC__ || defined __BORLANDC__
129 # define SETBINARY_IN(__fp)     (setmode   ( FILENO ((__fp)),  O_BINARY ), (__fp))
130 # define SETBINARY_OUT(__fp)    (setmode   ( FILENO ((__fp)),  O_BINARY ), (__fp))
131 #elif defined __CYGWIN__
132 # define SETBINARY_IN(__fp)     (setmode   ( FILENO ((__fp)), _O_BINARY ), (__fp))
133 # define SETBINARY_OUT(__fp)    (setmode   ( FILENO ((__fp)), _O_BINARY ), (__fp))
134 #elif defined _WIN32
135 # define SETBINARY_IN(__fp)     (_setmode  ( FILENO ((__fp)), _O_BINARY ), (__fp))
136 # define SETBINARY_OUT(__fp)    (_setmode  ( FILENO ((__fp)), _O_BINARY ), (__fp))
137 #elif defined _MSC_VER
138 # define SETBINARY_IN(__fp)     (setmode   ( FILENO ((__fp)),  O_BINARY ), (__fp))
139 # define SETBINARY_OUT(__fp)    (setmode   ( FILENO ((__fp)),  O_BINARY ), (__fp))
140 #elif defined __unix__
141 # define SETBINARY_IN(__fp)     (__fp)
142 # define SETBINARY_OUT(__fp)    (__fp)
143 #elif 0
144 # define SETBINARY_IN(__fp)     (freopen   ( NULL, "rb", (__fp) ), (__fp))
145 # define SETBINARY_OUT(__fp)    (freopen   ( NULL, "wb", (__fp) ), (__fp))
146 #else
147 # define SETBINARY_IN(__fp)     (__fp)
148 # define SETBINARY_OUT(__fp)    (__fp)
149 #endif
150 
151 // file I/O using ANSI buffered file I/O via file pointer FILE* (fopen, fread, fwrite, fclose)
152 #define READ(fp,ptr,len)       fread  (ptr, 1, len, fp)     // READ    returns -1 or 0 on error/EOF, otherwise > 0
153 #define READ1(fp,ptr)          fread  (ptr, 1, 1, fp)       // READ    returns -1 or 0 on error/EOF, otherwise > 0
154 
155 #ifdef _WIN32
156 # define POPEN_READ_BINARY_OPEN(cmd)    _popen ((cmd), "rb")
157 #else
158 # define POPEN_READ_BINARY_OPEN(cmd)    popen ((cmd), "r")
159 #endif
160 
161 // Path separator
162 #if defined __unix__  ||  defined __bsdi__  ||  defined __FreeBSD__  ||  defined __OpenBSD__  ||  defined __NetBSD__  ||  defined __APPLE__ || defined __DragonFly__
163 # define PATH_SEP               '/'
164 # define DRIVE_SEP              '\0'
165 # define EXE_EXT                ""
166 # define DEV_NULL               "/dev/null"
167 # define ENVPATH_SEP            ':'
168 #elif defined _WIN32  ||  defined __TURBOC__  ||  defined __ZTC__  ||  defined _MSC_VER
169 # define PATH_SEP               '\\'
170 # define DRIVE_SEP              ':'
171 # define EXE_EXT                ".exe"
172 # define DEV_NULL               "\\nul"
173 # define ENVPATH_SEP            ';'
174 #else
175 # define PATH_SEP               '/'         // Amiga: C:/
176 # define DRIVE_SEP              ':'
177 # define EXE_EXT                ""
178 # define DEV_NULL               "nul"
179 # define ENVPATH_SEP            ';'
180 #endif
181 
182 #ifdef _WIN32
183 # define TitleBar(text)   SetConsoleTitle (text)
184 #else
185 # define TitleBar(text)   (void) (text)
186 #endif
187 
188 
189 //// constants /////////////////////////////////////////////////////
190 #define DECODER_DELAY    (512 - 32 + 1)
191 #define BLK_SIZE         (36 * 32)
192 
193 
194 //// procedures/functions //////////////////////////////////////////
195 // pipeopen.c
196 FILE*      pipeopen                   ( const char* command, const char* filename );
197 
198 // stderr.c
199 void       SetStderrSilent            ( mpc_bool_t state );
200 mpc_bool_t GetStderrSilent            ( void );
201 int mpc_cdecl  stderr_printf              ( const char* format, ... );
202 
203 // quant.h
204 #define SCFfac              0.832980664785f     // = SCF[n-1]/SCF[n]
205 
206 // wave_in.h
207 typedef struct {
208     FILE*         fp;                   // File pointer to read data
209     mpc_size_t    PCMOffset;            // File offset of PCM data
210     long double   SampleFreq;           // Sample frequency in Hz
211     mpc_uint_t    BitsPerSample;        // used bits per sample, 8*BytesPerSample-7 <= BitsPerSample <= BytesPerSample
212     mpc_uint_t    BytesPerSample;       // allocated bytes per sample
213     mpc_uint_t    Channels;             // Number of channels, 1...8
214 	mpc_size_t    PCMBytes;             // PCM Samples (in 8 bit units)
215 	mpc_size_t    PCMSamples;           // PCM Samples per Channel
216     mpc_bool_t    raw;                  // raw: headerless format
217 } wave_t;
218 
219 typedef float SCFTriple [3];
220 
221 // FIXME : put in lib header
222 
223 void   Init_FFT      ( PsyModel* );
224 
225 // FIXME : put in lib header
226 void   Init_Psychoakustik       ( PsyModel* );
227 SMRTyp Psychoakustisches_Modell ( PsyModel *, const int, const PCMDataTyp*, int* TransientL, int* TransientR );
228 void   TransientenCalc          ( int* Transient, const int* TransientL, const int* TransientR );
229 void   RaiseSMR                 ( PsyModel*, const int, SMRTyp* );
230 void   MS_LR_Entscheidung       ( const int, unsigned char* MS, SMRTyp*, SubbandFloatTyp* );
231 void   Init_Psychoakustiktabellen ( PsyModel* );
232 
233 void   NS_Analyse (PsyModel*, const int, const unsigned char* MS, const SMRTyp, const int* Transient );
234 
235 void   Analyse_Filter(const PCMDataTyp*, SubbandFloatTyp*, const int);
236 void   Analyse_Init ( float Left, float Right, SubbandFloatTyp* out, const int MaxBand );
237 
238 void SetQualityParams (PsyModel *, float );
239 int TestProfileParams ( PsyModel* );
240 
241 extern const float  Butfly    [7];              // Antialiasing to calculate the subband powers
242 extern const float  InvButfly [7];              // Antialiasing to calculate the masking thresholds
243 extern const float  iw        [PART_LONG];      // inverse partition-width for long
244 extern const float  iw_short  [PART_SHORT];     // inverse partition-width for short
245 extern const int    wl        [PART_LONG];      // w_low  for long
246 extern const int    wl_short  [PART_SHORT];     // w_low  for short
247 extern const int    wh        [PART_LONG];      // w_high for long
248 extern const int    wh_short  [PART_SHORT];     // w_high for short
249 
250 
251 // quant.c
252 extern float __invSCF [128 + 6];        // tabulated scalefactors (inverted)
253 #define invSCF  (__invSCF + 6)
254 
255 float  ISNR_Schaetzer                  ( const float* samples, const float comp, const int res);
256 float  ISNR_Schaetzer_Trans            ( const float* samples, const float comp, const int res);
257 void   QuantizeSubband                 ( unsigned int* qu_output, const float* input, const int res, float* errors, const int maxNsOrder );
258 void   QuantizeSubbandWithNoiseShaping ( unsigned int* qu_output, const float* input, const int res, float* errors, const float* FIR );
259 
260 void   NoiseInjectionComp ( void );
261 
262 // keyboard.c
263 int    WaitKey      ( void );
264 int    CheckKeyKeep ( void );
265 int    CheckKey     ( void );
266 
267 
268 // regress.c
269 void    Regression       ( float* const _r, float* const _b, const float* p, const float* q );
270 
271 
272 // tags.c
273 void    Init_Tags        ( void );
274 int     FinalizeTags     ( FILE* fp, unsigned int Version, unsigned int flags );
275 int     addtag           ( const char* key, size_t keylen, const char* value, size_t valuelen, int converttoutf8, int flags );
276 int     gettag           ( const char* key, char* dst, size_t len );
277 int     CopyTags         ( const char* filename );
278 
279 
280 // wave_in.c
281 int     Open_WAV_Header  ( wave_t* type, const char* name );
282 size_t  Read_WAV_Samples ( wave_t* t, const size_t RequestedSamples, PCMDataTyp* data, const ptrdiff_t offset, const float scalel, const float scaler, int* Silence );
283 int     Read_WAV_Header  ( wave_t* type );
284 
285 
286 // winmsg.c
287 #ifdef _WIN32
288 #define WIN32_MESSAGES      1                   // support Windows-Messaging to Frontend
289 int    SearchForFrontend   ( void );
290 void   SendQuitMessage     ( void );
291 void   SendModeMessage     ( const int );
292 void   SendStartupMessage  ( const char*, const int);
293 void   SendProgressMessage ( const int, const float, const float );
294 #else
295 # define WIN32_MESSAGES                 0
296 # define SearchForFrontend()            (0)
297 # define SendQuitMessage()              (void)0
298 # define SendModeMessage(x)             (void)0
299 # define SendStartupMessage(x,y)        (void)0
300 # define SendProgressMessage(x,y,z)     (void)0
301 #endif /* _WIN32 */
302 
303 
304 #define MPPENC_DENORMAL_FIX_BASE ( 32. * 1024. /* normalized sample value range */ / ( (float) (1 << 24 /* first bit below 32-bit PCM range */ ) ) )
305 #define MPPENC_DENORMAL_FIX_LEFT ( MPPENC_DENORMAL_FIX_BASE )
306 #define MPPENC_DENORMAL_FIX_RIGHT ( MPPENC_DENORMAL_FIX_BASE * 0.5f )
307 
308 #ifndef LAST_HUFFMAN
309 # define LAST_HUFFMAN    7
310 #endif
311 
312 #endif /* MPCENC_MPCENC_H */
313 
314 /* end of mpcenc.h */
315