--- mplayer_base/mp_msg.h	2008-03-08 02:46:16.000000000 +0300
+++ mplayer/mp_msg.h	2008-04-20 05:51:56.000000000 +0400
@@ -127,6 +127,21 @@
 #   endif
 #endif /* __GNUC__ */
 
+#ifdef USE_ICONV
+#include <iconv.h>
+
+/* Recode filename from <tocode> to <fromcode> charset.
+ *
+ * *icnv -- iconv handler, unique for caller;
+ * *buf  -- buffer for recoded result, must be allocated outside,
+ *          but may be grown inside the function;
+ * *buf_size -- size of buffer to use. */
+const char* mp_recode_to(iconv_t* const icnv, 
+    const char* const filename, 
+    const char* const tocode, const char* const fromcode,
+    char** const buf, size_t* const buf_size);
+#endif /* USE_ICONV */
+
 const char* filename_recode(const char* filename);
 
 #endif /* MPLAYER_MP_MSG_H */
--- mplayer_base/mp_msg.c	2008-04-12 20:24:22.000000000 +0400
+++ mplayer/mp_msg.c	2008-04-20 05:53:40.000000000 +0400
@@ -33,7 +33,9 @@
 char *mp_msg_charset = NULL;
 static char *old_charset = NULL;
 static iconv_t msgiconv;
-#endif
+char *recoded_filename;
+size_t recoded_filename_size = MSGSIZE_MAX;
+#endif /* USE_ICONV */
 
 const char* filename_recode(const char* filename)
 {
@@ -41,30 +43,66 @@
     return filename;
 #else
     static iconv_t inv_msgiconv = (iconv_t)(-1);
-    static char recoded_filename[MSGSIZE_MAX];
+    if (mp_msg_charset && strcasecmp(mp_msg_charset, "noconv"))
+        return mp_recode_to(&inv_msgiconv, 
+                filename, MSG_CHARSET, mp_msg_charset,
+                &recoded_filename, &recoded_filename_size);
+    else
+        return filename;
+#endif
+}
+
+#ifdef USE_ICONV
+// recode filename from <tocode> to <fromcode> charset
+const char* mp_recode_to(iconv_t* const icnv, 
+    const char* const filename, 
+    const char* const tocode, const char* const fromcode,
+    char** const buf, size_t* const buf_size)
+{
     size_t filename_len, max_path;
     char* precoded;
-    if (!mp_msg_charset ||
-        !strcasecmp(mp_msg_charset, MSG_CHARSET) ||
-        !strcasecmp(mp_msg_charset, "noconv"))
+    if (!strcasecmp(fromcode, tocode))
         return filename;
-    if (inv_msgiconv == (iconv_t)(-1)) {
-        inv_msgiconv = iconv_open(MSG_CHARSET, mp_msg_charset);
-        if (inv_msgiconv == (iconv_t)(-1))
+    if (*icnv == (iconv_t)(-1)) {
+        *icnv = iconv_open(tocode, fromcode);
+        if (*icnv == (iconv_t)(-1)) {
+            mp_msg(MSGT_GLOBAL, MSGL_ERR,
+                   "iconv: conversion from %s to %s failed because of %s\n",
+                   fromcode, tocode, strerror(errno)); 
             return filename;
+        }
     }
+
     filename_len = strlen(filename);
-    max_path = MSGSIZE_MAX - 4;
-    precoded = recoded_filename;
-    if (iconv(inv_msgiconv, &filename, &filename_len,
-              &precoded, &max_path) == (size_t)(-1) && errno == E2BIG) {
-        precoded[0] = precoded[1] = precoded[2] = '.';
-        precoded += 3;
+    max_path = *buf_size - 1;
+    precoded = *buf;
+    while(1) {
+        if (iconv(*icnv, &filename, &filename_len,
+                  &precoded, &max_path) == (size_t)(-1)) {
+            // if buffer size is not enough, sufficient memory 
+            // must be allocated; 
+            if (errno == E2BIG) {
+                unsigned int diff = precoded - *buf;
+                // double buffer
+                max_path += *buf_size;
+                *buf_size <<= 1;
+                *buf = realloc(*buf, *buf_size);
+                precoded = (unsigned int)(*buf) + diff;
+            }
+            else {
+                mp_msg(MSGT_GLOBAL, MSGL_ERR,
+                       "iconv: conversion from %s to %s failed because of %s\n",
+                       fromcode, tocode, strerror(errno)); 
+                break;
+            }
+        }
+        else
+            break;
     }
     *precoded = '\0';
-    return recoded_filename;
-#endif
+    return *buf;
 }
+#endif /* USE_ICONV */
 
 void mp_msg_init(void){
     int i;
@@ -77,6 +115,9 @@
     mp_msg_charset = getenv("MPLAYER_CHARSET");
     if (!mp_msg_charset)
       mp_msg_charset = get_term_charset();
+    // allocate initial buffer for filename conversion 
+    // via filename_recode()
+    recoded_filename = malloc(recoded_filename_size);
 #endif
 }
 
