Skip to content

Commit a437de9

Browse files
committed
AviSynth: convert input filenames to UTF-8 (LWLibavVideo/AudioSource); close #30
1 parent bf66e55 commit a437de9

File tree

2 files changed

+94
-8
lines changed

2 files changed

+94
-8
lines changed

AviSynth/libavsmash_source.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ LSMASHVideoSource::LSMASHVideoSource
236236
prepare_video_decoding( vdhp, vohp, format_ctx.get(), threads, direct_rendering, pixel_format, vi, env );
237237
lsmash_discard_boxes( libavsmash_video_get_root( vdhp ) );
238238

239-
has_at_least_v8 = true;
240-
try { env->CheckVersion(8); }
241-
catch (const AvisynthError&) { has_at_least_v8 = false; }
239+
has_at_least_v8 = env->FunctionExists("propShow");
242240

243241
av_frame = libavsmash_video_get_frame_buffer(vdhp);
244242
int num = av_frame->sample_aspect_ratio.num;

AviSynth/lwlibav_source.cpp

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ extern "C"
4141
#include "lwlibav_source.h"
4242
#include "../common/lwlibav_video_internal.h"
4343

44+
#ifdef _WIN32
45+
#include <windows.h>
46+
#endif
47+
4448
#ifdef _MSC_VER
4549
#pragma warning( disable:4996 )
4650
#endif
@@ -172,9 +176,7 @@ LWLibavVideoSource::LWLibavVideoSource
172176
/* */
173177
prepare_video_decoding( vdhp, vohp, direct_rendering, pixel_format, env );
174178

175-
has_at_least_v8 = true;
176-
try { env->CheckVersion(8); }
177-
catch (const AvisynthError&) { has_at_least_v8 = false; }
179+
has_at_least_v8 = env->FunctionExists("propShow");
178180

179181
av_frame = lwlibav_video_get_frame_buffer(vdhp);
180182
int num = av_frame->sample_aspect_ratio.num;
@@ -373,9 +375,66 @@ static void set_av_log_level( int level )
373375
av_log_set_level( AV_LOG_TRACE );
374376
}
375377

378+
#ifdef WIN32
379+
static AVS_FORCEINLINE int ansi_to_wchar(const char* filename_ansi, wchar_t** filename_w)
380+
{
381+
int num_chars;
382+
num_chars = MultiByteToWideChar(CP_ACP, 0, filename_ansi, -1, NULL, 0);
383+
if (num_chars <= 0) {
384+
*filename_w = NULL;
385+
return 0;
386+
}
387+
*filename_w = (wchar_t*)av_calloc(num_chars, sizeof(wchar_t));
388+
if (!*filename_w) {
389+
errno = ENOMEM;
390+
return -1;
391+
}
392+
MultiByteToWideChar(CP_ACP, 0, filename_ansi, -1, *filename_w, num_chars);
393+
return 0;
394+
}
395+
396+
static AVS_FORCEINLINE int wchar_to_utf8(const wchar_t* filename_w, char** filename)
397+
{
398+
int num_chars = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, filename_w, -1, NULL, 0, NULL, NULL);
399+
if (num_chars <= 0) {
400+
*filename = NULL;
401+
return 0;
402+
}
403+
*filename = (char*)av_malloc_array(num_chars, sizeof * filename);
404+
if (!*filename) {
405+
errno = ENOMEM;
406+
return -1;
407+
}
408+
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, filename_w, -1, *filename, num_chars, NULL, NULL);
409+
return 0;
410+
}
411+
412+
static AVS_FORCEINLINE int ansi_to_utf8(const char* filename_ansi, char** filename)
413+
{
414+
wchar_t* filename_w = NULL;
415+
int ret = -1;
416+
if (ansi_to_wchar(filename_ansi, &filename_w))
417+
return -1;
418+
419+
if (!filename_w) {
420+
*filename = NULL;
421+
return 0;
422+
}
423+
424+
ret = wchar_to_utf8(filename_w, filename);
425+
av_free(filename_w);
426+
return ret;
427+
}
428+
#endif
429+
376430
AVSValue __cdecl CreateLWLibavVideoSource( AVSValue args, void *user_data, IScriptEnvironment *env )
377431
{
378-
const char *source = args[0].AsString();
432+
#ifdef WIN32
433+
const char *source_
434+
#else
435+
const char *source
436+
#endif
437+
= args[0].AsString();
379438
int stream_index = args[1].AsInt( -1 );
380439
int threads = args[2].AsInt( 0 );
381440
int no_create_index = args[3].AsBool( true ) ? 0 : 1;
@@ -399,6 +458,18 @@ AVSValue __cdecl CreateLWLibavVideoSource( AVSValue args, void *user_data, IScri
399458
int ff_loglevel = args[15].AsInt( 0 );
400459
const char* cdir = args[16].AsString( nullptr );
401460
const bool progress = args[17].AsBool(true);
461+
462+
#ifdef WIN32
463+
char* tmp = nullptr;
464+
if (ansi_to_utf8(source_, &tmp))
465+
env->ThrowError("LWLibavVideoSource: Cannot allocate filename");
466+
if (!tmp)
467+
env->ThrowError("LWLibavVideoSource: Cannot retrieve num_chars");
468+
469+
const char* source = tmp;
470+
av_free(tmp);
471+
#endif
472+
402473
/* Set LW-Libav options. */
403474
lwlibav_option_t opt;
404475
opt.file_path = source;
@@ -427,7 +498,12 @@ AVSValue __cdecl CreateLWLibavVideoSource( AVSValue args, void *user_data, IScri
427498

428499
AVSValue __cdecl CreateLWLibavAudioSource( AVSValue args, void *user_data, IScriptEnvironment *env )
429500
{
430-
const char *source = args[0].AsString();
501+
#ifdef WIN32
502+
const char* source_
503+
#else
504+
const char* source
505+
#endif
506+
= args[0].AsString();
431507
int stream_index = args[1].AsInt( -1 );
432508
int no_create_index = args[2].AsBool( true ) ? 0 : 1;
433509
const char *index_file_path = args[3].AsString( nullptr );
@@ -439,6 +515,18 @@ AVSValue __cdecl CreateLWLibavAudioSource( AVSValue args, void *user_data, IScri
439515
const char* cdir = args[9].AsString( nullptr );
440516
double drc = args[10].AsFloatf( 1.0f );
441517
const bool progress = args[11].AsBool(true);
518+
519+
#ifdef WIN32
520+
char* tmp = nullptr;
521+
if (ansi_to_utf8(source_, &tmp))
522+
env->ThrowError("LWLibavAudioSource: Cannot allocate filename");
523+
if (!tmp)
524+
env->ThrowError("LWLibavAudioSource: Cannot retrieve num_chars");
525+
526+
const char* source = tmp;
527+
av_free(tmp);
528+
#endif
529+
442530
/* Set LW-Libav options. */
443531
lwlibav_option_t opt;
444532
opt.file_path = source;

0 commit comments

Comments
 (0)