[x265] input: Allow Unicode filenames (input files) in Windows

Mateusz mateuszb at poczta.onet.pl
Wed May 11 10:10:26 CEST 2016


-------------- next part --------------
# HG changeset patch
# User Ma0 <mateuszb at poczta.onet.pl>
# Date 1462954071 -7200
#      Wed May 11 10:07:51 2016 +0200
# Node ID 081f36816156a813af6e3982ffc1dc10c9919e58
# Parent  a5362b9533f6a5b77740b4e8f97dba2555b6f929
input: Allow Unicode filenames (input files) in Windows

diff -r a5362b9533f6 -r 081f36816156 source/common/common.h
--- a/source/common/common.h	Wed May 04 21:08:09 2016 +0000
+++ b/source/common/common.h	Wed May 11 10:07:51 2016 +0200
@@ -77,6 +77,7 @@
 
 #if defined(__MINGW32__)
 #define fseeko fseeko64
+#define ftello ftello64
 #endif
 
 #elif defined(_MSC_VER)
@@ -85,6 +86,7 @@
 #define ALIGN_VAR_16(T, var) __declspec(align(16)) T var
 #define ALIGN_VAR_32(T, var) __declspec(align(32)) T var
 #define fseeko _fseeki64
+#define ftello _ftelli64
 
 #endif // if defined(__GNUC__)
 
diff -r a5362b9533f6 -r 081f36816156 source/input/y4m.cpp
--- a/source/input/y4m.cpp	Wed May 04 21:08:09 2016 +0000
+++ b/source/input/y4m.cpp	Wed May 11 10:07:51 2016 +0200
@@ -60,15 +60,15 @@
     ifs = NULL;
     if (!strcmp(info.filename, "-"))
     {
-        ifs = &cin;
+        ifs = stdin;
 #if _WIN32
         setmode(fileno(stdin), O_BINARY);
 #endif
     }
     else
-        ifs = new ifstream(info.filename, ios::binary | ios::in);
+        ifs = x265_fopen(info.filename, "rb");
 
-    if (ifs && ifs->good() && parseHeader())
+    if (ifs && !ferror(ifs) && parseHeader())
     {
         int pixelbytes = depth > 8 ? 2 : 1;
         for (int i = 0; i < x265_cli_csps[colorSpace].planes; i++)
@@ -91,8 +91,8 @@
     }
     if (!threadActive)
     {
-        if (ifs && ifs != &cin)
-            delete ifs;
+        if (ifs && ifs != stdin)
+            fclose(ifs);
         ifs = NULL;
         return;
     }
@@ -110,9 +110,9 @@
     size_t estFrameSize = framesize + strlen(header) + 1; /* assume basic FRAME\n headers */
 
     /* try to estimate frame count, if this is not stdin */
-    if (ifs != &cin)
+    if (ifs != stdin)
     {
-        istream::pos_type cur = ifs->tellg();
+        int64_t cur = ftello(ifs);
 
 #if defined(_MSC_VER) && _MSC_VER < 1700
         /* Older MSVC versions cannot handle 64bit file sizes properly, so go native */
@@ -129,9 +129,9 @@
 #else // if defined(_MSC_VER) && _MSC_VER < 1700
         if (cur >= 0)
         {
-            ifs->seekg(0, ios::end);
-            istream::pos_type size = ifs->tellg();
-            ifs->seekg(cur, ios::beg);
+            fseeko(ifs, 0, SEEK_END);
+            int64_t size = ftello(ifs);
+            fseeko(ifs, cur, SEEK_SET);
             if (size > 0)
                 info.frameCount = (int)((size - cur) / estFrameSize);
         }
@@ -141,18 +141,18 @@
     if (info.skipFrames)
     {
 #if X86_64
-        ifs->seekg((uint64_t)estFrameSize * info.skipFrames, ios::cur);
+        fseeko(ifs, (int64_t)estFrameSize * info.skipFrames, SEEK_CUR);
 #else
         for (int i = 0; i < info.skipFrames; i++)
-            ifs->ignore(estFrameSize);
+            fseeko(ifs, estFrameSize, SEEK_CUR);
 #endif
     }
 }
 
 Y4MInput::~Y4MInput()
 {
-    if (ifs && ifs != &cin)
-        delete ifs;
+    if (ifs && ifs != stdin)
+        fclose(ifs);
 
     for (int i = 0; i < QUEUE_SIZE; i++)
         X265_FREE(buf[i]);
@@ -174,23 +174,23 @@
     int csp = 0;
     int d = 0;
 
-    while (ifs->good())
+    while (!ferror(ifs))
     {
         // Skip Y4MPEG string
-        int c = ifs->get();
-        while (ifs->good() && (c != ' ') && (c != '\n'))
-            c = ifs->get();
+        int c = fgetc(ifs);
+        while (!ferror(ifs) && (c != ' ') && (c != '\n'))
+            c = fgetc(ifs);
 
-        while (c == ' ' && ifs->good())
+        while (c == ' ' && !ferror(ifs))
         {
             // read parameter identifier
-            switch (ifs->get())
+            switch (fgetc(ifs))
             {
             case 'W':
                 width = 0;
-                while (ifs->good())
+                while (!ferror(ifs))
                 {
-                    c = ifs->get();
+                    c = fgetc(ifs);
 
                     if (c == ' ' || c == '\n')
                         break;
@@ -201,9 +201,9 @@
 
             case 'H':
                 height = 0;
-                while (ifs->good())
+                while (!ferror(ifs))
                 {
-                    c = ifs->get();
+                    c = fgetc(ifs);
                     if (c == ' ' || c == '\n')
                         break;
                     else
@@ -214,15 +214,15 @@
             case 'F':
                 rateNum = 0;
                 rateDenom = 0;
-                while (ifs->good())
+                while (!ferror(ifs))
                 {
-                    c = ifs->get();
+                    c = fgetc(ifs);
                     if (c == '.')
                     {
                         rateDenom = 1;
-                        while (ifs->good())
+                        while (!ferror(ifs))
                         {
-                            c = ifs->get();
+                            c = fgetc(ifs);
                             if (c == ' ' || c == '\n')
                                 break;
                             else
@@ -235,9 +235,9 @@
                     }
                     else if (c == ':')
                     {
-                        while (ifs->good())
+                        while (!ferror(ifs))
                         {
-                            c = ifs->get();
+                            c = fgetc(ifs);
                             if (c == ' ' || c == '\n')
                                 break;
                             else
@@ -253,14 +253,14 @@
             case 'A':
                 sarWidth = 0;
                 sarHeight = 0;
-                while (ifs->good())
+                while (!ferror(ifs))
                 {
-                    c = ifs->get();
+                    c = fgetc(ifs);
                     if (c == ':')
                     {
-                        while (ifs->good())
+                        while (!ferror(ifs))
                         {
-                            c = ifs->get();
+                            c = fgetc(ifs);
                             if (c == ' ' || c == '\n')
                                 break;
                             else
@@ -276,18 +276,18 @@
             case 'C':
                 csp = 0;
                 d = 0;
-                while (ifs->good())
+                while (!ferror(ifs))
                 {
-                    c = ifs->get();
+                    c = fgetc(ifs);
 
                     if (c <= '9' && c >= '0')
                         csp = csp * 10 + (c - '0');
                     else if (c == 'p')
                     {
                         // example: C420p16
-                        while (ifs->good())
+                        while (!ferror(ifs))
                         {
-                            c = ifs->get();
+                            c = fgetc(ifs);
 
                             if (c <= '9' && c >= '0')
                                 d = d * 10 + (c - '0');
@@ -306,10 +306,10 @@
                 break;
 
             default:
-                while (ifs->good())
+                while (!ferror(ifs))
                 {
                     // consume this unsupported configuration word
-                    c = ifs->get();
+                    c = fgetc(ifs);
                     if (c == ' ' || c == '\n')
                         break;
                 }
@@ -354,26 +354,25 @@
 
 bool Y4MInput::populateFrameQueue()
 {
-    if (!ifs || ifs->fail())
+    if (!ifs || ferror(ifs))
         return false;
 
     /* strip off the FRAME header */
     char hbuf[sizeof(header)];
 
-    ifs->read(hbuf, strlen(header));
-    if (ifs->eof())
+    if (fread(hbuf, strlen(header), 1, ifs) != 1)
         return false;
 
-    if (!ifs->good() || memcmp(hbuf, header, strlen(header)))
+    if (ferror(ifs) || memcmp(hbuf, header, strlen(header)))
     {
         x265_log(NULL, X265_LOG_ERROR, "y4m: frame header missing\n");
         return false;
     }
 
     /* consume bytes up to line feed */
-    int c = ifs->get();
-    while (c != '\n' && ifs->good())
-        c = ifs->get();
+    int c = fgetc(ifs);
+    while (c != '\n' && !ferror(ifs))
+        c = fgetc(ifs);
 
     /* wait for room in the ring buffer */
     int written = writeCount.get();
@@ -386,8 +385,7 @@
     }
 
     ProfileScopeEvent(frameRead);
-    ifs->read(buf[written % QUEUE_SIZE], framesize);
-    if (ifs->good())
+    if (fread(buf[written % QUEUE_SIZE], framesize, 1, ifs) == 1)
     {
         writeCount.incr();
         return true;
diff -r a5362b9533f6 -r 081f36816156 source/input/y4m.h
--- a/source/input/y4m.h	Wed May 04 21:08:09 2016 +0000
+++ b/source/input/y4m.h	Wed May 11 10:07:51 2016 +0200
@@ -63,7 +63,7 @@
 
     char* buf[QUEUE_SIZE];
 
-    std::istream *ifs;
+    FILE *ifs;
 
     bool parseHeader();
 
@@ -79,9 +79,9 @@
 
     void release();
 
-    bool isEof() const            { return ifs && ifs->eof();  }
+    bool isEof() const            { return ifs && feof(ifs); }
 
-    bool isFail()                 { return !(ifs && !ifs->fail() && threadActive); }
+    bool isFail()                 { return !(ifs && !ferror(ifs) && threadActive); }
 
     void startReader();
 
diff -r a5362b9533f6 -r 081f36816156 source/input/yuv.cpp
--- a/source/input/yuv.cpp	Wed May 04 21:08:09 2016 +0000
+++ b/source/input/yuv.cpp	Wed May 11 10:07:51 2016 +0200
@@ -68,20 +68,20 @@
 
     if (!strcmp(info.filename, "-"))
     {
-        ifs = &cin;
+        ifs = stdin;
 #if _WIN32
         setmode(fileno(stdin), O_BINARY);
 #endif
     }
     else
-        ifs = new ifstream(info.filename, ios::binary | ios::in);
+        ifs = x265_fopen(info.filename, "rb");
 
-    if (ifs && ifs->good())
+    if (ifs && !ferror(ifs))
         threadActive = true;
     else
     {
-        if (ifs && ifs != &cin)
-            delete ifs;
+        if (ifs && ifs != stdin)
+            fclose(ifs);
         ifs = NULL;
         return;
     }
@@ -100,9 +100,9 @@
     info.frameCount = -1;
 
     /* try to estimate frame count, if this is not stdin */
-    if (ifs != &cin)
+    if (ifs != stdin)
     {
-        istream::pos_type cur = ifs->tellg();
+        int64_t cur = ftello(ifs);
 
 #if defined(_MSC_VER) && _MSC_VER < 1700
         /* Older MSVC versions cannot handle 64bit file sizes properly, so go native */
@@ -119,9 +119,9 @@
 #else // if defined(_MSC_VER) && _MSC_VER < 1700
         if (cur >= 0)
         {
-            ifs->seekg(0, ios::end);
-            istream::pos_type size = ifs->tellg();
-            ifs->seekg(cur, ios::beg);
+            fseeko(ifs, 0, SEEK_END);
+            int64_t size = ftello(ifs);
+            fseeko(ifs, cur, SEEK_SET);
             if (size > 0)
                 info.frameCount = (int)((size - cur) / framesize);
         }
@@ -131,18 +131,18 @@
     if (info.skipFrames)
     {
 #if X86_64
-        ifs->seekg((uint64_t)framesize * info.skipFrames, ios::cur);
+        fseeko(ifs, (int64_t)framesize * info.skipFrames, SEEK_CUR);
 #else
         for (int i = 0; i < info.skipFrames; i++)
-            ifs->ignore(framesize);
+            fseeko(ifs, framesize, SEEK_CUR);
 #endif
     }
 }
 
 YUVInput::~YUVInput()
 {
-    if (ifs && ifs != &cin)
-        delete ifs;
+    if (ifs && ifs != stdin)
+        fclose(ifs);
     for (int i = 0; i < QUEUE_SIZE; i++)
         X265_FREE(buf[i]);
 }
@@ -178,7 +178,7 @@
 
 bool YUVInput::populateFrameQueue()
 {
-    if (!ifs || ifs->fail())
+    if (!ifs || ferror(ifs))
         return false;
 
     /* wait for room in the ring buffer */
@@ -193,8 +193,7 @@
     }
 
     ProfileScopeEvent(frameRead);
-    ifs->read(buf[written % QUEUE_SIZE], framesize);
-    if (ifs->good())
+    if (fread(buf[written % QUEUE_SIZE], framesize, 1, ifs) == 1)
     {
         writeCount.incr();
         return true;
diff -r a5362b9533f6 -r 081f36816156 source/input/yuv.h
--- a/source/input/yuv.h	Wed May 04 21:08:09 2016 +0000
+++ b/source/input/yuv.h	Wed May 11 10:07:51 2016 +0200
@@ -55,7 +55,7 @@
 
     char* buf[QUEUE_SIZE];
 
-    std::istream *ifs;
+    FILE *ifs;
 
     int guessFrameCount();
 
@@ -71,9 +71,9 @@
 
     void release();
 
-    bool isEof() const                            { return ifs && ifs->eof();  }
+    bool isEof() const                            { return ifs && feof(ifs); }
 
-    bool isFail()                                 { return !(ifs && !ifs->fail() && threadActive); }
+    bool isFail()                                 { return !(ifs && !ferror(ifs) && threadActive); }
 
     void startReader();
 


More information about the x265-devel mailing list