[x265] [PATCH] input: read y4m input from stdin if filename is passed as "-"

Gopu Govindaswamy gopu at multicorewareinc.com
Mon Oct 28 07:42:19 CET 2013


# HG changeset patch
# User Gopu Govindaswamy <gopu at multicorewareinc.com>
# Date 1382942530 -19800
# Node ID 4327bc0b1bce6cbbeb28f4ef81063b63802b3a9a
# Parent  ef2428fd32feddd60168f3430c50f4d7e6f02741
input: read y4m input from stdin if filename is passed as "-"

diff -r ef2428fd32fe -r 4327bc0b1bce source/input/y4m.cpp
--- a/source/input/y4m.cpp	Mon Oct 28 00:08:06 2013 -0500
+++ b/source/input/y4m.cpp	Mon Oct 28 12:12:10 2013 +0530
@@ -26,6 +26,15 @@
 #include "common.h"
 #include <stdio.h>
 #include <string.h>
+#include <iostream>
+
+#if WIN32
+#include "io.h"
+#include "fcntl.h"
+#if defined(_MSC_VER)
+#pragma warning(disable: 4996) // POSIX setmode and fileno deprecated
+#endif
+#endif
 
 using namespace x265;
 using namespace std;
@@ -40,9 +49,19 @@
     buf = NULL;
 #endif
 
-    ifs.open(filename, ios::binary | ios::in);
+    ifs = NULL;
+    if (!strcmp(filename, "-"))
+    {
+        ifs = &cin;
+#if WIN32
+        setmode(fileno(stdin), O_BINARY);
+#endif
+    }
+    else
+        ifs = new ifstream(filename, ios::binary | ios::in);
+
     threadActive = false;
-    if (!ifs.fail())
+    if (ifs && !ifs->fail())
     {
         if (parseHeader())
         {
@@ -64,13 +83,17 @@
 #endif // if defined(ENABLE_THREAD)
         }
     }
-    if (!threadActive)
-        ifs.close();
+    if (!threadActive && ifs && ifs != &cin)
+    {
+        delete ifs;
+        ifs = NULL;
+    }
 }
 
 Y4MInput::~Y4MInput()
 {
-    ifs.close();
+    if (ifs && ifs != &cin)
+        delete ifs;
 #if defined(ENABLE_THREAD)
     for (int i = 0; i < QUEUE_SIZE; i++)
     {
@@ -84,6 +107,9 @@
 
 bool Y4MInput::parseHeader()
 {
+    if (!ifs)
+        return false;
+
     width = 0;
     height = 0;
     rateNum = 0;
@@ -92,22 +118,22 @@
     while (ifs)
     {
         // Skip Y4MPEG string
-        int c = ifs.get();
-        while (!ifs.eof() && (c != ' ') && (c != '\n'))
+        int c = ifs->get();
+        while (!ifs->eof() && (c != ' ') && (c != '\n'))
         {
-            c = ifs.get();
+            c = ifs->get();
         }
 
         while (c == ' ' && ifs)
         {
             // read parameter identifier
-            switch (ifs.get())
+            switch (ifs->get())
             {
             case 'W':
                 width = 0;
                 while (ifs)
                 {
-                    c = ifs.get();
+                    c = ifs->get();
 
                     if (c == ' ' || c == '\n')
                     {
@@ -125,7 +151,7 @@
                 height = 0;
                 while (ifs)
                 {
-                    c = ifs.get();
+                    c = ifs->get();
                     if (c == ' ' || c == '\n')
                     {
                         break;
@@ -143,13 +169,13 @@
                 rateDenom = 0;
                 while (ifs)
                 {
-                    c = ifs.get();
+                    c = ifs->get();
                     if (c == '.')
                     {
                         rateDenom = 1;
                         while (ifs)
                         {
-                            c = ifs.get();
+                            c = ifs->get();
                             if (c == ' ' || c == '\n')
                             {
                                 break;
@@ -167,7 +193,7 @@
                     {
                         while (ifs)
                         {
-                            c = ifs.get();
+                            c = ifs->get();
                             if (c == ' ' || c == '\n')
                             {
                                 break;
@@ -190,7 +216,7 @@
                 while (ifs)
                 {
                     // consume this unsupported configuration word
-                    c = ifs.get();
+                    c = ifs->get();
                     if (c == ' ' || c == '\n')
                         break;
                 }
@@ -217,25 +243,33 @@
 
 int Y4MInput::guessFrameCount()
 {
-    istream::pos_type cur = ifs.tellg();
+    if (!ifs || ifs == &cin) 
+        return -1;
+    istream::pos_type cur = ifs->tellg();
     if (cur < 0)
         return -1;
 
-    ifs.seekg(0, ios::end);
-    istream::pos_type size = ifs.tellg();
+    ifs->seekg(0, ios::end);
+    istream::pos_type size = ifs->tellg();
     if (size < 0)
         return -1;
-    ifs.seekg(cur, ios::beg);
+    ifs->seekg(cur, ios::beg);
 
     return (int)((size - cur) / ((width * height * 3 / 2) + strlen(header) + 1));
 }
 
 void Y4MInput::skipFrames(int numFrames)
 {
-    const size_t count = (width * height * 3 / 2) + strlen(header);
-    for (int i = 0; i < numFrames; i++)
+    const size_t count = (width * height * 3 / 2) + strlen(header) + 1;
+    if (ifs && numFrames)
     {
-        ifs.ignore(count);
+        if (ifs == &cin)
+        {
+            for (int i = 0; i < numFrames; i++)
+                ifs->ignore(count);
+        }
+        else
+            ifs->seekg(count * numFrames, ios::cur);
     }
 }
 
@@ -296,8 +330,10 @@
 {
     /* strip off the FRAME header */
     char hbuf[sizeof(header)];
+    if (!ifs)
+        return false;
 
-    ifs.read(hbuf, strlen(header));
+    ifs->read(hbuf, strlen(header));
     if (!ifs || memcmp(hbuf, header, strlen(header)))
     {
         if (ifs)
@@ -305,10 +341,10 @@
         return false;
     }
     /* consume bytes up to line feed */
-    int c = ifs.get();
+    int c = ifs->get();
     while (c != '\n' && !ifs)
     {
-        c = ifs.get();
+        c = ifs->get();
     }
 
     const size_t count = width * height * 3 / 2;
@@ -319,11 +355,11 @@
             return false;
     }
 
-    ifs.read(buf[tail], count);
-    frameStat[tail] = !ifs.fail();
+    ifs->read(buf[tail], count);
+    frameStat[tail] = !ifs->fail();
     tail = (tail + 1) % QUEUE_SIZE;
     notEmpty.trigger();
-    return !ifs.fail();
+    return !ifs->fail();
 }
 
 #else // if defined(ENABLE_THREAD)
@@ -333,7 +369,11 @@
 
     /* strip off the FRAME header */
     char hbuf[sizeof(header)];
-    ifs.read(hbuf, strlen(header));
+
+    if (!ifs)
+        return false;
+
+    ifs->read(hbuf, strlen(header));
     if (!ifs || memcmp(hbuf, header, strlen(header)))
     {
         x265_log(NULL, X265_LOG_ERROR, "y4m: frame header missing\n");
@@ -341,10 +381,10 @@
     }
 
     /* consume bytes up to line feed */
-    int c = ifs.get();
+    int c = ifs->get();
     while (c != '\n' && !ifs)
     {
-        c = ifs.get();
+        c = ifs->get();
     }
 
     const size_t count = width * height * 3 / 2;
@@ -361,10 +401,10 @@
 
     pic.stride[1] = pic.stride[2] = pic.stride[0] >> 1;
 
-    ifs.read(buf, count);
+    ifs->read(buf, count);
     PPAStopCpuEventFunc(read_yuv);
 
-    return !ifs.fail();
+    return !ifs->fail();
 }
 
 #endif // if defined(ENABLE_THREAD)
diff -r ef2428fd32fe -r 4327bc0b1bce source/input/y4m.h
--- a/source/input/y4m.h	Mon Oct 28 00:08:06 2013 -0500
+++ b/source/input/y4m.h	Mon Oct 28 12:12:10 2013 +0530
@@ -68,7 +68,7 @@
 #else // if defined(ENABLE_THREAD)
     char *buf;
 #endif // if defined(ENABLE_THREAD)
-    std::ifstream ifs;
+    std::istream *ifs;
 
     bool parseHeader();
 
@@ -88,9 +88,9 @@
 
     int getHeight() const                         { return height; }
 
-    bool isEof() const                            { return ifs.eof(); }
+    bool isEof() const                            { return (ifs && ifs->eof()); }
 
-    bool isFail()                                 { return !(ifs.is_open() && threadActive); }
+    bool isFail()                                 { return !(ifs && !ifs->fail() && threadActive); }
 
     void startReader();
 


More information about the x265-devel mailing list