Updated FAAD decoder to v2.7 from CVS in order to include latest libFAAD fixes (2016...
[LameXP.git] / etc / Patches / deprecated / FAAD-v2.7-UTF8+Flush+FixBufferOverrun.V2.diff
blob085be8dfaaeb816fe2103a9462a6343c440fe5c0
1 frontend/audio.c | 11 +++++-
2 frontend/main.c | 89 ++++++++++++++++++++++++++++++++---------
3 frontend/unicode_support.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++
4 frontend/unicode_support.h | 21 ++++++++++
5 include/neaacdec.h | 6 +--
6 5 files changed, 202 insertions(+), 23 deletions(-)
8 diff --git a/frontend/audio.c b/frontend/audio.c
9 index 067ac20..a502c6e 100644
10 --- a/frontend/audio.c
11 +++ b/frontend/audio.c
12 @@ -37,11 +37,14 @@
13 #include <math.h>
14 #include <neaacdec.h>
15 #include "audio.h"
16 +#include "unicode_support.h"
19 audio_file *open_audio_file(char *infile, int samplerate, int channels,
20 int outputFormat, int fileType, long channelMask)
22 + wchar_t *fileNameW;
24 audio_file *aufile = malloc(sizeof(audio_file));
26 aufile->outputFormat = outputFormat;
27 @@ -78,7 +81,13 @@ audio_file *open_audio_file(char *infile, int samplerate, int channels,
28 aufile->toStdio = 1;
29 } else {
30 aufile->toStdio = 0;
31 - aufile->sndfile = fopen(infile, "wb");
32 + aufile->sndfile = NULL;
33 + fileNameW = utf8_to_utf16(infile);
34 + if(fileNameW)
35 + {
36 + aufile->sndfile = _wfopen(fileNameW, L"wb");
37 + free(fileNameW);
38 + }
41 if (aufile->sndfile == NULL)
42 diff --git a/frontend/main.c b/frontend/main.c
43 index 04e2058..01a5b7a 100644
44 --- a/frontend/main.c
45 +++ b/frontend/main.c
46 @@ -44,11 +44,13 @@
47 #include <stdlib.h>
48 #include <string.h>
49 #include <getopt.h>
50 +#include <io.h>
52 #include <neaacdec.h>
53 #include <mp4ff.h>
55 #include "audio.h"
56 +#include "unicode_support.h"
58 #ifndef min
59 #define min(a,b) ( (a) < (b) ? (a) : (b) )
60 @@ -71,6 +73,8 @@ static void faad_fprintf(FILE *stream, const char *fmt, ...)
61 vfprintf(stream, fmt, ap);
63 va_end(ap);
65 + fflush(stream);
69 @@ -443,7 +447,7 @@ static int decodeAACfile(char *aacfile, char *sndfile, char *adts_fn, int to_std
70 NeAACDecFrameInfo frameInfo;
71 NeAACDecConfigurationPtr config;
73 - char percents[200];
74 + char percents[300];
75 int percent, old_percent = -1;
76 int bread, fileread;
77 int header_type = 0;
78 @@ -456,11 +460,19 @@ static int decodeAACfile(char *aacfile, char *sndfile, char *adts_fn, int to_std
80 aac_buffer b;
82 + wchar_t *fileNameW;
84 memset(&b, 0, sizeof(aac_buffer));
86 if (adts_out)
88 - adtsFile = fopen(adts_fn, "wb");
89 + adtsFile = NULL;
90 + fileNameW = utf8_to_utf16(adts_fn);
91 + if(fileNameW)
92 + {
93 + adtsFile = _wfopen(fileNameW, L"wb");
94 + free(fileNameW);
95 + }
96 if (adtsFile == NULL)
98 faad_fprintf(stderr, "Error opening file: %s\n", adts_fn);
99 @@ -470,20 +482,26 @@ static int decodeAACfile(char *aacfile, char *sndfile, char *adts_fn, int to_std
101 if (0 == strcmp(aacfile, "-"))
103 - b.infile = stdin;
104 + b.infile = stdin;
105 #ifdef _WIN32
106 setmode(fileno(stdin), O_BINARY);
107 #endif
109 - } else
111 + else
113 - b.infile = fopen(aacfile, "rb");
114 - if (b.infile == NULL)
116 - /* unable to open file */
117 - faad_fprintf(stderr, "Error opening file: %s\n", aacfile);
118 - return 1;
120 + b.infile = NULL;
121 + fileNameW = utf8_to_utf16(aacfile);
122 + if(fileNameW)
124 + b.infile = _wfopen(fileNameW, L"rb");
125 + free(fileNameW);
127 + if (b.infile == NULL)
129 + /* unable to open file */
130 + faad_fprintf(stderr, "Error opening file: %s\n", aacfile);
131 + return 1;
135 retval = fseek(b.infile, 0, SEEK_END);
136 @@ -708,7 +726,7 @@ static int decodeAACfile(char *aacfile, char *sndfile, char *adts_fn, int to_std
137 if (percent > old_percent)
139 old_percent = percent;
140 - sprintf(percents, "%d%% decoding %s.", percent, aacfile);
141 + _snprintf_s(percents, 300, _TRUNCATE, "[%d%%] decoding %s.", percent, aacfile);
142 faad_fprintf(stderr, "%s\r", percents);
143 #ifdef _WIN32
144 SetConsoleTitle(percents);
145 @@ -810,7 +828,7 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
146 unsigned char *buffer;
147 int buffer_size;
149 - char percents[200];
150 + char percents[300];
151 int percent, old_percent = -1;
153 int first_time = 1;
154 @@ -821,6 +839,7 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
155 unsigned int framesize;
156 unsigned long timescale;
158 + wchar_t *fileNameW;
160 /* initialise the callback structure */
161 mp4ff_callback_t *mp4cb = malloc(sizeof(mp4ff_callback_t));
162 @@ -830,7 +849,14 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
163 return 1;
166 - mp4File = fopen(mp4file, "rb");
167 + mp4File = NULL;
168 + fileNameW = utf8_to_utf16(mp4file);
169 + if(fileNameW)
171 + mp4File = _wfopen(fileNameW, L"rb");
172 + free(fileNameW);
175 mp4cb->read = read_callback;
176 mp4cb->seek = seek_callback;
177 mp4cb->user_data = mp4File;
178 @@ -847,7 +873,13 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
180 if (adts_out)
182 - adtsFile = fopen(adts_fn, "wb");
183 + adtsFile = NULL;
184 + fileNameW = utf8_to_utf16(adts_fn);
185 + if(fileNameW)
187 + adtsFile = _wfopen(fileNameW, L"wb");
188 + free(fileNameW);
190 if (adtsFile == NULL)
192 faad_fprintf(stderr, "Error opening file: %s\n", adts_fn);
193 @@ -1053,7 +1085,7 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
194 if (percent > old_percent)
196 old_percent = percent;
197 - sprintf(percents, "%d%% decoding %s.", percent, mp4file);
198 + _snprintf_s(percents, 300, _TRUNCATE, "[%d%%] decoding %s.", percent, mp4file);
199 faad_fprintf(stderr, "%s\r", percents);
200 #ifdef _WIN32
201 SetConsoleTitle(percents);
202 @@ -1091,7 +1123,7 @@ static int decodeMP4file(char *mp4file, char *sndfile, char *adts_fn, int to_std
203 return frameInfo.error;
206 -int main(int argc, char *argv[])
207 +int faad_main(int argc, char *argv[])
209 int result;
210 int infoOnly = 0;
211 @@ -1115,6 +1147,7 @@ int main(int argc, char *argv[])
212 unsigned char header[8];
213 float length = 0;
214 FILE *hMP4File;
215 + wchar_t *fileNameW;
217 /* System dependant types */
218 #ifdef _WIN32
219 @@ -1345,7 +1378,13 @@ int main(int argc, char *argv[])
220 } else {
222 mp4file = 0;
223 - hMP4File = fopen(aacFileName, "rb");
224 + hMP4File = NULL;
225 + fileNameW = utf8_to_utf16(aacFileName);
226 + if(fileNameW)
228 + hMP4File = _wfopen(fileNameW, L"rb");
229 + free(fileNameW);
231 if (!hMP4File)
233 faad_fprintf(stderr, "Error opening file: %s\n", aacFileName);
234 @@ -1408,3 +1447,15 @@ int main(int argc, char *argv[])
236 return 0;
239 +int wmain(int argc, wchar_t **argv_utf16)
241 + int result = 0;
242 + char **argv_utf8 = NULL;
244 + init_commandline_arguments_utf8(argc, &argv_utf8, argv_utf16);
245 + result = faad_main(argc, argv_utf8);
246 + free_commandline_arguments_utf8(argc, &argv_utf8);
248 + return result;
250 diff --git a/frontend/unicode_support.c b/frontend/unicode_support.c
251 new file mode 100644
252 index 0000000..21ecd5c
253 --- /dev/null
254 +++ b/frontend/unicode_support.c
255 @@ -0,0 +1,98 @@
256 +#include "unicode_support.h"
258 +#include <stdio.h>
259 +#include <windows.h>
261 +char *utf16_to_utf8(const wchar_t *input)
263 + char *Buffer;
264 + int BuffSize, Result;
266 + BuffSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL);
267 + Buffer = (char*) malloc(sizeof(char) * BuffSize);
269 + if(!Buffer)
271 + fprintf(stderr, "Error in utf16_to_utf8: Memory allocation failed!\n");
272 + return NULL;
275 + Result = WideCharToMultiByte(CP_UTF8, 0, input, -1, Buffer, BuffSize, NULL, NULL);
276 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
279 +wchar_t *utf8_to_utf16(const char *input)
281 + wchar_t *Buffer;
282 + int BuffSize, Result;
284 + BuffSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0);
285 + Buffer = (wchar_t*) malloc(sizeof(wchar_t) * BuffSize);
287 + if(!Buffer)
289 + fprintf(stderr, "Error in utf8_to_utf16: Memory allocation failed!\n");
290 + return NULL;
293 + Result = MultiByteToWideChar(CP_UTF8, 0, input, -1, Buffer, BuffSize);
294 + return ((Result > 0) && (Result <= BuffSize)) ? Buffer : NULL;
297 +void init_commandline_arguments_utf8(int argc, char ***argv_utf8, wchar_t **argv_utf16)
299 + int i = 0;
301 + *argv_utf8 = (char**) malloc(argc * sizeof(char*));
302 + if(!(*argv_utf8))
304 + fprintf(stderr, "Error in init_commandline_arguments_utf8: Memory allocation failed!\n");
305 + exit(-1);
308 + for(i = 0; i < argc; i++)
310 + (*argv_utf8)[i] = utf16_to_utf8(argv_utf16[i]);
311 + if(!(*argv_utf8)[i])
313 + fprintf(stderr, "Error in init_commandline_arguments_utf8: Memory allocation failed!\n");
314 + exit(-1);
319 +void free_commandline_arguments_utf8(int argc, char ***argv_utf8)
321 + int i = 0;
323 + if(*argv_utf8 != NULL)
325 + for(i = 0; i < argc; i++)
327 + if((*argv_utf8)[i] != NULL)
329 + free((*argv_utf8)[i]);
330 + (*argv_utf8)[i] = NULL;
333 + free(*argv_utf8);
334 + *argv_utf8 = NULL;
338 +FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8)
340 + FILE *ret = NULL;
341 + wchar_t *filename_utf16 = utf8_to_utf16(filename_utf8);
342 + wchar_t *mode_utf16 = utf8_to_utf16(mode_utf8);
344 + if(filename_utf16 && mode_utf16)
346 + ret = _wfopen(filename_utf16, mode_utf16);
349 + if(filename_utf16) free(filename_utf16);
350 + if(mode_utf16) free(mode_utf16);
352 + return ret;
354 diff --git a/frontend/unicode_support.h b/frontend/unicode_support.h
355 new file mode 100644
356 index 0000000..cc13fd9
357 --- /dev/null
358 +++ b/frontend/unicode_support.h
359 @@ -0,0 +1,21 @@
360 +#ifndef UNICODE_SUPPORT_H_INCLUDED
361 +#define UNICODE_SUPPORT_H_INCLUDED
363 +#include <ctype.h>
364 +#include <stdio.h>
365 +#include <stdlib.h>
367 +#ifdef __cplusplus
368 +extern "C" {
369 +#endif
371 +char *utf16_to_utf8(const wchar_t *input);
372 +wchar_t *utf8_to_utf16(const char *input);
373 +void init_commandline_arguments_utf8(int argc, char ***argv_utf8, wchar_t **argv_utf16);
374 +void free_commandline_arguments_utf8(int argc, char ***argv_utf8);
375 +FILE *fopen_utf8(const char *filename_utf8, const char *mode_utf8);
377 +#ifdef __cplusplus
379 +#endif
380 +#endif
381 \ No newline at end of file
382 diff --git a/include/neaacdec.h b/include/neaacdec.h
383 index 610a00b..7904175 100644
384 --- a/include/neaacdec.h
385 +++ b/include/neaacdec.h
386 @@ -202,7 +202,7 @@ typedef struct NeAACDecFrameInfo
387 unsigned char ps;
388 } NeAACDecFrameInfo;
390 -char NEAACDECAPI *NeAACDecGetErrorMessage(unsigned char errcode);
391 +char* NEAACDECAPI NeAACDecGetErrorMessage(unsigned char errcode);
393 unsigned long NEAACDECAPI NeAACDecGetCapabilities(void);
395 @@ -235,12 +235,12 @@ void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hDecoder, long frame);
397 void NEAACDECAPI NeAACDecClose(NeAACDecHandle hDecoder);
399 -void NEAACDECAPI *NeAACDecDecode(NeAACDecHandle hDecoder,
400 +void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hDecoder,
401 NeAACDecFrameInfo *hInfo,
402 unsigned char *buffer,
403 unsigned long buffer_size);
405 -void NEAACDECAPI *NeAACDecDecode2(NeAACDecHandle hDecoder,
406 +void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hDecoder,
407 NeAACDecFrameInfo *hInfo,
408 unsigned char *buffer,
409 unsigned long buffer_size,