[x265] [PATCH] input: change from ifstream to stdio stream

Ashok Kumar Mishra ashok at multicorewareinc.com
Mon Jan 15 20:45:52 CET 2018


On Sat, Jan 13, 2018 at 7:43 AM, Ma0 <mateuszb at poczta.onet.pl> wrote:

> # HG changeset patch
> # User Ma0 <mateuszb at poczta.onet.pl>
> # Date 1515808050 -3600
> #      Sat Jan 13 02:47:30 2018 +0100
> # Node ID 5066d1a0dfa3a7a5aef54c26b93b03595b3e3508
> # Parent  2f3c4158cf3553030920708271bc43cdc79932a3
> input: change from ifstream to stdio stream
>
> diff -r 2f3c4158cf35 -r 5066d1a0dfa3 source/common/common.h
> --- a/source/common/common.h    Thu Jan 04 12:37:01 2018 +0530
> +++ b/source/common/common.h    Sat Jan 13 02:47:30 2018 +0100
> @@ -78,6 +78,7 @@
>
>  #if defined(__MINGW32__)
>  #define fseeko fseeko64
> +#define ftello ftello64
>  #endif
>
>  #elif defined(_MSC_VER)
> @@ -87,6 +88,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 2f3c4158cf35 -r 5066d1a0dfa3 source/input/y4m.cpp
> --- a/source/input/y4m.cpp      Thu Jan 04 12:37:01 2018 +0530
> +++ b/source/input/y4m.cpp      Sat Jan 13 02:47:30 2018 +0100
> @@ -39,7 +39,7 @@
>  using namespace X265_NS;
>  using namespace std;
>
> -static const char header[] = "FRAME";
> +static const char header[] = {'F','R','A','M','E'};
>
>  Y4MInput::Y4MInput(InputFileInfo& info)
>  {
> @@ -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;
>      }
> @@ -107,59 +107,38 @@
>      info.depth = depth;
>      info.frameCount = -1;
>
> -    size_t estFrameSize = framesize + strlen(header) + 1; /* assume basic
> FRAME\n headers */
> +    size_t estFrameSize = framesize + sizeof(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 */
> -        HANDLE hFile = CreateFileA(info.filename, GENERIC_READ,
> -                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
> NULL, OPEN_EXISTING,
> -                                   FILE_ATTRIBUTE_NORMAL, NULL);
> -        if (hFile != INVALID_HANDLE_VALUE)
> -        {
> -            LARGE_INTEGER size;
> -            if (GetFileSizeEx(hFile, &size))
> -                info.frameCount = (int)((size.QuadPart - (int64_t)cur) /
> estFrameSize);
> -            CloseHandle(hFile);
> -        }
> -#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);
>          }
> -#endif // if defined(_MSC_VER) && _MSC_VER < 1700
>      }
>
>      if (info.skipFrames)
>      {
> -#if X86_64
> -        if (ifs != &cin)
> -            ifs->seekg((uint64_t)estFrameSize * info.skipFrames,
> ios::cur);
> +        if (ifs != stdin)
> +            fseeko(ifs, (int64_t)estFrameSize * info.skipFrames,
> SEEK_CUR);
>          else
>              for (int i = 0; i < info.skipFrames; i++)
> -            {
> -                ifs->read(buf[0], estFrameSize - framesize);
> -                ifs->read(buf[0], framesize);
> -            }
> -#else
> -        for (int i = 0; i < info.skipFrames; i++)
> -            ifs->ignore(estFrameSize);
> -#endif
> +                if (fread(buf[0], estFrameSize - framesize, 1, ifs) +
> fread(buf[0], framesize, 1, ifs) != 2)
> +                    break;
>      }
>  }
>
>  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]);
> @@ -181,24 +160,22 @@
>      int csp = 0;
>      int d = 0;
>
> -    while (ifs->good())
> +    int c;
> +    while ((c = fgetc(ifs)) != EOF)
>      {
>          // Skip Y4MPEG string
> -        int c = ifs->get();
> -        while (ifs->good() && (c != ' ') && (c != '\n'))
> -            c = ifs->get();
> +        while ((c != EOF) && (c != ' ') && (c != '\n'))
> +            c = fgetc(ifs);
>
> -        while (c == ' ' && ifs->good())
> +        while (c == ' ')
>          {
>              // read parameter identifier
> -            switch (ifs->get())
> +            switch (fgetc(ifs))
>              {
>              case 'W':
>                  width = 0;
> -                while (ifs->good())
> +                while ((c = fgetc(ifs)) != EOF)
>                  {
> -                    c = ifs->get();
> -
>                      if (c == ' ' || c == '\n')
>                          break;
>                      else
> @@ -208,9 +185,8 @@
>
>              case 'H':
>                  height = 0;
> -                while (ifs->good())
> +                while ((c = fgetc(ifs)) != EOF)
>                  {
> -                    c = ifs->get();
>                      if (c == ' ' || c == '\n')
>                          break;
>                      else
> @@ -221,15 +197,13 @@
>              case 'F':
>                  rateNum = 0;
>                  rateDenom = 0;
> -                while (ifs->good())
> +                while ((c = fgetc(ifs)) != EOF)
>                  {
> -                    c = ifs->get();
>                      if (c == '.')
>                      {
>                          rateDenom = 1;
> -                        while (ifs->good())
> +                        while ((c = fgetc(ifs)) != EOF)
>                          {
> -                            c = ifs->get();
>                              if (c == ' ' || c == '\n')
>                                  break;
>                              else
> @@ -242,9 +216,8 @@
>                      }
>                      else if (c == ':')
>                      {
> -                        while (ifs->good())
> +                        while ((c = fgetc(ifs)) != EOF)
>                          {
> -                            c = ifs->get();
>                              if (c == ' ' || c == '\n')
>                                  break;
>                              else
> @@ -260,14 +233,12 @@
>              case 'A':
>                  sarWidth = 0;
>                  sarHeight = 0;
> -                while (ifs->good())
> +                while ((c = fgetc(ifs)) != EOF)
>                  {
> -                    c = ifs->get();
>                      if (c == ':')
>                      {
> -                        while (ifs->good())
> +                        while ((c = fgetc(ifs)) != EOF)
>                          {
> -                            c = ifs->get();
>                              if (c == ' ' || c == '\n')
>                                  break;
>                              else
> @@ -283,19 +254,15 @@
>              case 'C':
>                  csp = 0;
>                  d = 0;
> -                while (ifs->good())
> +                while ((c = fgetc(ifs)) != EOF)
>                  {
> -                    c = ifs->get();
> -
>                      if (c <= 'o' && c >= '0')
>                          csp = csp * 10 + (c - '0');
>                      else if (c == 'p')
>                      {
>                          // example: C420p16
> -                        while (ifs->good())
> +                        while ((c = fgetc(ifs)) != EOF)
>                          {
> -                            c = ifs->get();
> -
>                              if (c <= '9' && c >= '0')
>                                  d = d * 10 + (c - '0');
>                              else
> @@ -330,10 +297,9 @@
>                  break;
>
>              default:
> -                while (ifs->good())
> +                while ((c = fgetc(ifs)) != EOF)
>                  {
>                      // consume this unsupported configuration word
> -                    c = ifs->get();
>                      if (c == ' ' || c == '\n')
>                          break;
>                  }
> @@ -378,26 +344,24 @@
>
>  bool Y4MInput::populateFrameQueue()
>  {
> -    if (!ifs || ifs->fail())
> +    if (!ifs || ferror(ifs))
>          return false;
>
> -    /* strip off the FRAME header */
> -    char hbuf[sizeof(header)];
> +    /* strip off the FRAME\n header */
> +    char hbuf[sizeof(header) + 1];
>
> -    ifs->read(hbuf, strlen(header));
> -    if (ifs->eof())
> -        return false;
> -
> -    if (!ifs->good() || memcmp(hbuf, header, strlen(header)))
> +    if (fread(hbuf, sizeof(hbuf), 1, ifs) != 1 || memcmp(hbuf, header,
> sizeof(header)))
>      {
> -        x265_log(NULL, X265_LOG_ERROR, "y4m: frame header missing\n");
> +        if (!feof(ifs))
> +            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 = hbuf[sizeof(header)];
> +    while (c != '\n')
> +        if ((c = fgetc(ifs)) == EOF)
> +            break;
>
>      /* wait for room in the ring buffer */
>      int written = writeCount.get();
> @@ -410,8 +374,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 2f3c4158cf35 -r 5066d1a0dfa3 source/input/y4m.h
> --- a/source/input/y4m.h        Thu Jan 04 12:37:01 2018 +0530
> +++ b/source/input/y4m.h        Sat Jan 13 02:47:30 2018 +0100
> @@ -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 2f3c4158cf35 -r 5066d1a0dfa3 source/input/yuv.cpp
> --- a/source/input/yuv.cpp      Thu Jan 04 12:37:01 2018 +0530
> +++ b/source/input/yuv.cpp      Sat Jan 13 02:47:30 2018 +0100
> @@ -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,53 +100,35 @@
>      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 */
> -        HANDLE hFile = CreateFileA(info.filename, GENERIC_READ,
> -                                   FILE_SHARE_READ | FILE_SHARE_WRITE,
> NULL, OPEN_EXISTING,
> -                                   FILE_ATTRIBUTE_NORMAL, NULL);
> -        if (hFile != INVALID_HANDLE_VALUE)
> -        {
> -            LARGE_INTEGER size;
> -            if (GetFileSizeEx(hFile, &size))
> -                info.frameCount = (int)((size.QuadPart - (int64_t)cur) /
> framesize);
> -            CloseHandle(hFile);
> -        }
> -#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);
>          }
> -#endif // if defined(_MSC_VER) && _MSC_VER < 1700
>      }
>
>      if (info.skipFrames)
>      {
> -#if X86_64
> -        if (ifs != &cin)
> -            ifs->seekg((uint64_t)framesize * info.skipFrames, ios::cur);
> +        if (ifs != stdin)
> +            fseeko(ifs, (int64_t)framesize * info.skipFrames, SEEK_CUR);
>          else
>              for (int i = 0; i < info.skipFrames; i++)
> -                ifs->read(buf[0], framesize);
> -#else
> -        for (int i = 0; i < info.skipFrames; i++)
> -            ifs->ignore(framesize);
> -#endif
> +                if (fread(buf[0], framesize, 1, ifs) != 1)
> +                    break;
>      }
>  }
>
>  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]);
>  }
> @@ -182,7 +164,7 @@
>
>  bool YUVInput::populateFrameQueue()
>  {
> -    if (!ifs || ifs->fail())
> +    if (!ifs || ferror(ifs))
>          return false;
>
>      /* wait for room in the ring buffer */
> @@ -197,8 +179,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 2f3c4158cf35 -r 5066d1a0dfa3 source/input/yuv.h
> --- a/source/input/yuv.h        Thu Jan 04 12:37:01 2018 +0530
> +++ b/source/input/yuv.h        Sat Jan 13 02:47:30 2018 +0100
> @@ -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();
>
> _______________________________________________
> x265-devel mailing list
> x265-devel at videolan.org
> https://mailman.videolan.org/listinfo/x265-devel
>

Looks good to me, can be pushed to default branch after smoke test.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20180116/0ad2ca77/attachment-0001.html>


More information about the x265-devel mailing list