+#ifdef _WIN32
+#define CONSOLE_TITLE_SIZE 200
+static wchar_t org_console_title[CONSOLE_TITLE_SIZE] = L"";
+
+void x264_cli_set_console_title( const char *title )
+{
+ wchar_t title_utf16[CONSOLE_TITLE_SIZE];
+ if( utf8_to_utf16( title, title_utf16 ) )
+ SetConsoleTitleW( title_utf16 );
+}
+
+static int utf16_to_ansi( const wchar_t *utf16, char *ansi, int size )
+{
+ int invalid;
+ return WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, utf16, -1, ansi, size, NULL, &invalid ) && !invalid;
+}
+
+/* Some external libraries doesn't support Unicode in filenames,
+ * as a workaround we can try to get an ANSI filename instead. */
+int x264_ansi_filename( const char *filename, char *ansi_filename, int size, int create_file )
+{
+ wchar_t filename_utf16[MAX_PATH];
+ if( utf8_to_utf16( filename, filename_utf16 ) )
+ {
+ if( create_file )
+ {
+ /* Create the file using the Unicode filename if it doesn't already exist. */
+ FILE *fh = _wfopen( filename_utf16, L"ab" );
+ if( fh )
+ fclose( fh );
+ }
+
+ /* Check if the filename already is valid ANSI. */
+ if( utf16_to_ansi( filename_utf16, ansi_filename, size ) )
+ return 1;
+
+ /* Check for a legacy 8.3 short filename. */
+ int short_length = GetShortPathNameW( filename_utf16, filename_utf16, MAX_PATH );
+ if( short_length > 0 && short_length < MAX_PATH )
+ if( utf16_to_ansi( filename_utf16, ansi_filename, size ) )
+ return 1;
+ }
+ return 0;
+}
+
+/* Retrieve command line arguments as UTF-8. */
+static int get_argv_utf8( int *argc_ptr, char ***argv_ptr )
+{
+ int ret = 0;
+ wchar_t **argv_utf16 = CommandLineToArgvW( GetCommandLineW(), argc_ptr );
+ if( argv_utf16 )
+ {
+ int argc = *argc_ptr;
+ int offset = (argc+1) * sizeof(char*);
+ int size = offset;
+
+ for( int i = 0; i < argc; i++ )
+ size += WideCharToMultiByte( CP_UTF8, 0, argv_utf16[i], -1, NULL, 0, NULL, NULL );
+
+ char **argv = *argv_ptr = malloc( size );
+ if( argv )
+ {
+ for( int i = 0; i < argc; i++ )
+ {
+ argv[i] = (char*)argv + offset;
+ offset += WideCharToMultiByte( CP_UTF8, 0, argv_utf16[i], -1, argv[i], size-offset, NULL, NULL );
+ }
+ argv[argc] = NULL;
+ ret = 1;
+ }
+ LocalFree( argv_utf16 );
+ }
+ return ret;
+}
+#endif
+