Updated FAAD binary to v2.8.6 (2017-10-10), compiled with ICL 18.0 and MSVC 15.5.
[LameXP.git] / etc / Patches / FAAD-v2.8.6-Win32-Unicode-Support.diff
blob96a11706fba1bf81b8cb8fd8efd7af7dfb5c8f4d
1 frontend/audio.c | 7 +-
2 frontend/main.c | 34 ++++++---
3 frontend/mp4read.c | 7 +-
4 frontend/unicode_support.c | 172 +++++++++++++++++++++++++++++++++++++++++++++
5 frontend/unicode_support.h | 50 +++++++++++++
6 5 files changed, 258 insertions(+), 12 deletions(-)
8 diff --git a/frontend/audio.c b/frontend/audio.c
9 index 9ab7a2a..0d94c18 100644
10 --- a/frontend/audio.c
11 +++ b/frontend/audio.c
12 @@ -38,6 +38,9 @@
13 #include <neaacdec.h>
14 #include <stdint.h>
16 +#ifdef _WIN32
17 +#include "unicode_support.h"
18 +#endif
19 #include "audio.h"
22 @@ -74,13 +77,13 @@ audio_file *open_audio_file(char *infile, int samplerate, int channels,
23 if(infile[0] == '-')
25 #ifdef _WIN32
26 - setmode(fileno(stdout), O_BINARY);
27 + _setmode(_fileno(stdout), O_BINARY);
28 #endif
29 aufile->sndfile = stdout;
30 aufile->toStdio = 1;
31 } else {
32 aufile->toStdio = 0;
33 - aufile->sndfile = fopen(infile, "wb");
34 + aufile->sndfile = faad_fopen(infile, "wb");
37 if (aufile->sndfile == NULL)
38 diff --git a/frontend/main.c b/frontend/main.c
39 index deaa039..d433253 100644
40 --- a/frontend/main.c
41 +++ b/frontend/main.c
42 @@ -35,9 +35,11 @@
43 #ifdef _WIN32
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 +#include <io.h>
47 #ifndef __MINGW32__
48 #define off_t __int64
49 #endif
50 +#include "unicode_support.h"
51 #else
52 #include <time.h>
53 #endif
54 @@ -479,7 +481,7 @@ static int decodeAACfile(char *aacfile, char *sndfile, char *adts_fn, int to_std
56 if (adts_out)
58 - adtsFile = fopen(adts_fn, "wb");
59 + adtsFile = faad_fopen(adts_fn, "wb");
60 if (adtsFile == NULL)
62 faad_fprintf(stderr, "Error opening file: %s\n", adts_fn);
63 @@ -491,12 +493,12 @@ static int decodeAACfile(char *aacfile, char *sndfile, char *adts_fn, int to_std
65 b.infile = stdin;
66 #ifdef _WIN32
67 - setmode(fileno(stdin), O_BINARY);
68 + _setmode(_fileno(stdin), O_BINARY);
69 #endif
71 } else
73 - b.infile = fopen(aacfile, "rb");
74 + b.infile = faad_fopen(aacfile, "rb");
75 if (b.infile == NULL)
77 /* unable to open file */
78 @@ -833,7 +835,7 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
80 if (adts_out)
82 - adtsFile = fopen(adts_fn, "wb");
83 + adtsFile = faad_fopen(adts_fn, "wb");
84 if (adtsFile == NULL)
86 faad_fprintf(stderr, "Error opening file: %s\n", adts_fn);
87 @@ -953,7 +955,7 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
88 outputFormat, fileType, aacChannelConfig2wavexChannelMask(&frameInfo));
89 } else {
90 #ifdef _WIN32
91 - setmode(fileno(stdout), O_BINARY);
92 + _setmode(_fileno(stdout), O_BINARY);
93 #endif
94 aufile = open_audio_file("-", frameInfo.samplerate, frameInfo.channels,
95 outputFormat, fileType, aacChannelConfig2wavexChannelMask(&frameInfo));
96 @@ -1007,7 +1009,7 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
97 return frameInfo.error;
100 -int main(int argc, char *argv[])
101 +static int faad_main(int argc, char *argv[])
103 int result;
104 int infoOnly = 0;
105 @@ -1265,13 +1267,13 @@ int main(int argc, char *argv[])
106 readFromStdin = 1;
107 hMP4File = stdin;
108 #ifdef _WIN32
109 - setmode(fileno(stdin), O_BINARY);
110 + _setmode(_fileno(stdin), O_BINARY);
111 #endif
113 } else {
115 mp4file = 0;
116 - hMP4File = fopen(aacFileName, "rb");
117 + hMP4File = faad_fopen(aacFileName, "rb");
118 if (!hMP4File)
120 faad_fprintf(stderr, "Error opening file: %s\n", aacFileName);
121 @@ -1338,3 +1340,19 @@ int main(int argc, char *argv[])
123 return 0;
126 +int main(int argc, char *argv[])
128 +#ifdef _WIN32
129 + int argc_utf8, exit_code;
130 + char **argv_utf8;
131 + init_console_utf8(stderr);
132 + init_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
133 + exit_code = faad_main(argc_utf8, argv_utf8);
134 + free_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
135 + uninit_console_utf8();
136 + return exit_code;
137 +#else
138 + return faad_main(argc, argv);
139 +#endif
141 \ No newline at end of file
142 diff --git a/frontend/mp4read.c b/frontend/mp4read.c
143 index c8ca530..be8d591 100644
144 --- a/frontend/mp4read.c
145 +++ b/frontend/mp4read.c
146 @@ -24,6 +24,9 @@
147 #include <time.h>
148 #include <limits.h>
150 +#ifdef _WIN32
151 +#include "unicode_support.h"
152 +#endif
153 #include "mp4read.h"
155 enum ATOM_TYPE
156 @@ -939,7 +942,7 @@ int mp4read_seek(int framenum)
158 static void mp4info(void)
160 - fprintf(stderr, "Modification Time:\t\%s", mp4time(mp4config.mtime));
161 + fprintf(stderr, "Modification Time:\t\t%s", mp4time(mp4config.mtime));
162 fprintf(stderr, "Samplerate:\t\t%d\n", mp4config.samplerate);
163 fprintf(stderr, "Total samples:\t\t%d\n", mp4config.samples);
164 fprintf(stderr, "Total channels:\t\t%d\n", mp4config.channels);
165 @@ -970,7 +973,7 @@ int mp4read_open(char *name)
167 mp4read_close();
169 - g_fin = fopen(name, "rb");
170 + g_fin = faad_fopen(name, "rb");
171 if (!g_fin)
172 return ERR_FAIL;
174 diff --git a/frontend/unicode_support.c b/frontend/unicode_support.c
175 new file mode 100644
176 index 0000000..96ea5a4
177 --- /dev/null
178 +++ b/frontend/unicode_support.c
179 @@ -0,0 +1,172 @@
180 +/* Copyright (c) 2004-2012 LoRd_MuldeR <mulder2@gmx.de>
181 + File: unicode_support.c
183 + This file was originally part of a patch included with LameXP,
184 + released under the same license as the original audio tools.
186 + Redistribution and use in source and binary forms, with or without
187 + modification, are permitted provided that the following conditions
188 + are met:
190 + - Redistributions of source code must retain the above copyright
191 + notice, this list of conditions and the following disclaimer.
193 + - Redistributions in binary form must reproduce the above copyright
194 + notice, this list of conditions and the following disclaimer in the
195 + documentation and/or other materials provided with the distribution.
197 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
199 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
200 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
201 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
202 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
203 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
204 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
205 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
206 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
207 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
209 +#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
211 +#include "unicode_support.h"
213 +#include <windows.h>
214 +#include <io.h>
216 +static UINT g_old_output_cp = ((UINT)-1);
218 +char *utf16_to_utf8(const wchar_t *input)
220 + char *Buffer;
221 + int BuffSize = 0, Result = 0;
223 + BuffSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
224 + Buffer = (char*) malloc(sizeof(char) * BuffSize);
225 + if(Buffer)
227 + Result = WideCharToMultiByte(CP_UTF8, 0, input, -1, Buffer, BuffSize, NULL, NULL);
230 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
233 +char *utf16_to_ansi(const wchar_t *input)
235 + char *Buffer;
236 + int BuffSize = 0, Result = 0;
238 + BuffSize = WideCharToMultiByte(CP_ACP, 0, input, -1, NULL, 0, NULL, NULL);
239 + Buffer = (char*) malloc(sizeof(char) * BuffSize);
240 + if(Buffer)
242 + Result = WideCharToMultiByte(CP_ACP, 0, input, -1, Buffer, BuffSize, NULL, NULL);
245 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
248 +wchar_t *utf8_to_utf16(const char *input)
250 + wchar_t *Buffer;
251 + int BuffSize = 0, Result = 0;
253 + BuffSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
254 + Buffer = (wchar_t*) malloc(sizeof(wchar_t) * BuffSize);
255 + if(Buffer)
257 + Result = MultiByteToWideChar(CP_UTF8, 0, input, -1, Buffer, BuffSize);
260 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
263 +void init_commandline_arguments_utf8(int *argc, char ***argv)
265 + int i, nArgs;
266 + LPWSTR *szArglist;
268 + szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
270 + if(NULL == szArglist)
272 + fprintf(stderr, "\nFATAL: CommandLineToArgvW failed\n\n");
273 + exit(-1);
276 + *argv = (char**) malloc(sizeof(char*) * nArgs);
277 + *argc = nArgs;
279 + if(NULL == *argv)
281 + fprintf(stderr, "\nFATAL: Malloc failed\n\n");
282 + exit(-1);
285 + for(i = 0; i < nArgs; i++)
287 + (*argv)[i] = utf16_to_utf8(szArglist[i]);
288 + if(NULL == (*argv)[i])
290 + fprintf(stderr, "\nFATAL: utf16_to_utf8 failed\n\n");
291 + exit(-1);
295 + LocalFree(szArglist);
298 +void free_commandline_arguments_utf8(int *argc, char ***argv)
300 + int i = 0;
302 + if(*argv != NULL)
304 + for(i = 0; i < *argc; i++)
306 + if((*argv)[i] != NULL)
308 + free((*argv)[i]);
309 + (*argv)[i] = NULL;
312 + free(*argv);
313 + *argv = NULL;
317 +FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8)
319 + FILE *ret = NULL;
320 + wchar_t *filename_utf16 = utf8_to_utf16(filename_utf8);
321 + wchar_t *mode_utf16 = utf8_to_utf16(mode_utf8);
323 + if(filename_utf16 && mode_utf16)
325 + ret = _wfopen(filename_utf16, mode_utf16);
328 + if(filename_utf16) free(filename_utf16);
329 + if(mode_utf16) free(mode_utf16);
331 + return ret;
334 +void init_console_utf8(FILE *const stream)
336 + if (_isatty(_fileno(stream)))
338 + g_old_output_cp = GetConsoleOutputCP();
339 + SetConsoleOutputCP(CP_UTF8);
343 +void uninit_console_utf8(void)
345 + if(g_old_output_cp != ((UINT)-1))
347 + SetConsoleOutputCP(g_old_output_cp);
351 +#endif
352 \ No newline at end of file
353 diff --git a/frontend/unicode_support.h b/frontend/unicode_support.h
354 new file mode 100644
355 index 0000000..eb21877
356 --- /dev/null
357 +++ b/frontend/unicode_support.h
358 @@ -0,0 +1,50 @@
359 +/* Copyright (c) 2004-2012 LoRd_MuldeR <mulder2@gmx.de>
360 + File: unicode_support.h
362 + This file was originally part of a patch included with LameXP,
363 + released under the same license as the original audio tools.
365 + Redistribution and use in source and binary forms, with or without
366 + modification, are permitted provided that the following conditions
367 + are met:
369 + - Redistributions of source code must retain the above copyright
370 + notice, this list of conditions and the following disclaimer.
372 + - Redistributions in binary form must reproduce the above copyright
373 + notice, this list of conditions and the following disclaimer in the
374 + documentation and/or other materials provided with the distribution.
376 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
377 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
378 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
379 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
380 + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
381 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
382 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
383 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
384 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
385 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
386 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
388 +#ifndef UNICODE_SUPPORT_H_INCLUDED
389 +#define UNICODE_SUPPORT_H_INCLUDED
391 +#include <stdio.h>
393 +char *utf16_to_utf8(const wchar_t *input);
394 +char *utf16_to_ansi(const wchar_t *input);
395 +wchar_t *utf8_to_utf16(const char *input);
396 +void init_commandline_arguments_utf8(int *argc, char ***argv);
397 +void free_commandline_arguments_utf8(int *argc, char ***argv);
398 +FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8);
399 +void init_console_utf8(FILE *const stream);
400 +void uninit_console_utf8(void);
402 +#ifdef _WIN32
403 +#define faad_fopen(X,Y) fopen_utf8((X),(Y))
404 +#else
405 +#define faad_fopen(X,Y) fopen((X),(Y))
406 +#endif
408 +#endif //UNICODE_SUPPORT_H_INCLUDED