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

Gopu Govindaswamy gopu at multicorewareinc.com
Fri Oct 25 09:16:46 CEST 2013


# HG changeset patch
# User Gopu Govindaswamy <gopu at multicorewareinc.com>
# Date 1382685398 -19800
# Node ID f93a9c2e6df892c358a6df2dc99ee3a8cb897837
# Parent  4125c74ff21d9b26ff697b30675460808953052b
input: read yuv input from stdin if filename is passed as "-"

diff -r 4125c74ff21d -r f93a9c2e6df8 source/input/yuv.cpp
--- a/source/input/yuv.cpp	Fri Oct 25 01:15:58 2013 -0500
+++ b/source/input/yuv.cpp	Fri Oct 25 12:46:38 2013 +0530
@@ -26,20 +26,26 @@
 #include "common.h"
 #include <stdio.h>
 #include <string.h>
+#include <iostream>
 
 using namespace x265;
 using namespace std;
 
 YUVInput::YUVInput(const char *filename)
 {
-    ifs.open(filename, ios::binary | ios::in);
+    ifs = NULL;
+    if (!strcmp(filename, "-"))
+        ifs = &cin;
+    else
+        ifs = new ifstream(filename, ios::binary | ios::in);
+
     width = height = 0;
     depth = 8;
     threadActive = false;
-    if (!ifs.fail())
+    if (ifs && !ifs->fail())
         threadActive = true;
     else
-        ifs.close();
+        if (ifs && ifs != &cin) delete ifs;
 #if defined ENABLE_THREAD
     head = 0;
     tail = 0;
@@ -48,7 +54,7 @@
 
 YUVInput::~YUVInput()
 {
-    ifs.close();
+    if (ifs && ifs != &cin) delete ifs;
 #if defined ENABLE_THREAD
     for (int i = 0; i < QUEUE_SIZE; i++)
     {
@@ -61,22 +67,25 @@
 
 int YUVInput::guessFrameCount()
 {
-    ifstream::pos_type cur = ifs.tellg();
+    if (!ifs) return -1;
+
+    ifstream::pos_type cur = ifs->tellg();
     if (cur < 0)
         return -1;
 
-    ifs.seekg(0, ios::end);
-    ifstream::pos_type size = ifs.tellg();
+    ifs->seekg(0, ios::end);
+    ifstream::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 * pixelbytes * 3 / 2));
 }
 
 void YUVInput::skipFrames(int numFrames)
 {
-    ifs.seekg(framesize * numFrames, ios::cur);
+    if (ifs)
+        ifs->seekg(framesize * numFrames, ios::cur);
 }
 
 void YUVInput::startReader()
@@ -97,7 +106,7 @@
         height < MIN_FRAME_HEIGHT || height > MAX_FRAME_HEIGHT)
     {
         threadActive = false;
-        ifs.close();
+        if (ifs && ifs != &cin) delete ifs;
     }
     else
     {
@@ -137,8 +146,9 @@
             break;
     }
 
-    ifs.read(buf[tail], framesize);
-    frameStat[tail] = ifs.good();
+    if (!ifs) return false;
+    ifs->read(buf[tail], framesize);
+    frameStat[tail] = ifs->good();
     if (!frameStat[tail])
         return false;
     tail = (tail + 1) % QUEUE_SIZE;
@@ -197,10 +207,11 @@
 
     pic.stride[1] = pic.stride[2] = pic.stride[0] >> 1;
 
-    ifs.read(buf, framesize);
+    if (!ifs) return false;
+    ifs->read(buf, framesize);
     PPAStopCpuEventFunc(read_yuv);
 
-    return ifs.good();
+    return ifs->good();
 }
 
 #endif // if defined ENABLE_THREAD
diff -r 4125c74ff21d -r f93a9c2e6df8 source/input/yuv.h
--- a/source/input/yuv.h	Fri Oct 25 01:15:58 2013 -0500
+++ b/source/input/yuv.h	Fri Oct 25 12:46:38 2013 +0530
@@ -71,7 +71,7 @@
     char* buf;
 #endif // if defined(ENABLE_THREAD)
 
-    std::ifstream ifs;
+    std::istream *ifs;
 
 public:
 
@@ -89,9 +89,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