shithub: libvpx

Download patch

ref: fd31755ea19c78569090b130faf25d5f5ce8514a
parent: 018fd12cf61987256a646ab3126a36c409bb9544
parent: 096224ffc2cb67bcaefad6315cee83ac37b7cbbf
author: Deb Mukherjee <debargha@google.com>
date: Fri Jul 18 12:30:06 EDT 2014

Merge "Use custom mkstemp() to fix Win issue in y4m_test"

--- a/test/video_source.h
+++ b/test/video_source.h
@@ -10,6 +10,9 @@
 #ifndef TEST_VIDEO_SOURCE_H_
 #define TEST_VIDEO_SOURCE_H_
 
+#if defined(_WIN32)
+#include <windows.h>
+#endif
 #include <cstdio>
 #include <cstdlib>
 #include <string>
@@ -55,9 +58,69 @@
   return fopen(path_to_source.c_str(), "wb");
 }
 
-static FILE *OpenTempOutFile() {
-  return tmpfile();
+static std::string GetTempOutFilename() {
+  std::string basename;
+#if defined(_WIN32)
+  char fname[MAX_PATH];
+  // Assume for now that the filename generated is unique per process
+  const UINT ret = GetTempFileNameA(
+      GetDataPath().c_str(), "lvx", 0, fname);
+  if (ret != 0) {
+    const char *slash = strrchr(fname, '\\');
+    if (slash == NULL) slash = strrchr(fname, '/');
+    if (slash == NULL)
+      basename.assign(fname);
+    else
+      basename.assign(slash + 1);
+  } else {
+    basename.clear();
+  }
+#else
+  char fname[256];
+  const std::string templ = GetDataPath() + "/libvpx_test_XXXXXX";
+  strncpy(fname, templ.c_str(), templ.size());
+  fname[templ.size()] = '\0';
+  const int fd = mkstemp(fname);
+  if (fd != -1) {
+    close(fd);
+    basename.assign(strrchr(fname, '/') + 1);
+  } else {
+    basename.clear();
+  }
+#endif
+  return basename;
 }
+
+class TempOutFile {
+ public:
+  TempOutFile() {
+    file_name_ = GetTempOutFilename();
+    file_ = OpenTestOutFile(file_name_);
+  }
+  ~TempOutFile() {
+    CloseFile();
+    if (!file_name_.empty()) {
+      const std::string path_to_source = GetDataPath() + "/" + file_name_;
+      EXPECT_EQ(0, remove(path_to_source.c_str()));
+    }
+  }
+  FILE *file() {
+    return file_;
+  }
+  const std::string& file_name() {
+    return file_name_;
+  }
+  void CloseFile() {
+    if (file_) {
+      fclose(file_);
+      file_ = NULL;
+    }
+  }
+
+ protected:
+  FILE *file_;
+  std::string file_name_;
+};
 
 // Abstract base class for test video sources, which provide a stream of
 // vpx_image_t images with associated timestamps and duration.
--- a/test/y4m_test.cc
+++ b/test/y4m_test.cc
@@ -138,14 +138,11 @@
 class Y4mVideoWriteTest
     : public Y4mVideoSourceTest {
  protected:
-  Y4mVideoWriteTest() : Y4mVideoSourceTest() {}
+  Y4mVideoWriteTest() {}
 
-  virtual void ReplaceInputFp(FILE *input_file) {
+  virtual ~Y4mVideoWriteTest() {
     CloseSource();
-    frame_ = 0;
-    input_file_ = input_file;
-    rewind(input_file_);
-    ReadSourceToStart();
+    delete tmpfile_;
   }
 
   // Writes out a y4m file and then reads it back
@@ -153,20 +150,21 @@
     ASSERT_TRUE(input_file_ != NULL);
     char buf[Y4M_BUFFER_SIZE] = {0};
     const struct VpxRational framerate = {y4m_.fps_n, y4m_.fps_d};
-    FILE *out_file = libvpx_test::OpenTempOutFile();
-    ASSERT_TRUE(out_file != NULL);
+    tmpfile_ = new libvpx_test::TempOutFile;
+    ASSERT_TRUE(tmpfile_->file() != NULL);
     y4m_write_file_header(buf, sizeof(buf),
                           kWidth, kHeight,
                           &framerate, y4m_.vpx_fmt,
                           y4m_.bit_depth);
-    fputs(buf, out_file);
+    fputs(buf, tmpfile_->file());
     for (unsigned int i = start_; i < limit_; i++) {
       y4m_write_frame_header(buf, sizeof(buf));
-      fputs(buf, out_file);
-      write_image_file(img(), out_file);
+      fputs(buf, tmpfile_->file());
+      write_image_file(img(), tmpfile_->file());
       Next();
     }
-    ReplaceInputFp(out_file);
+    tmpfile_->CloseFile();
+    Y4mVideoSourceTest::Init(tmpfile_->file_name(), limit_);
   }
 
   virtual void Init(const std::string &file_name, int limit) {
@@ -173,6 +171,7 @@
     Y4mVideoSourceTest::Init(file_name, limit);
     WriteY4mAndReadBack();
   }
+  libvpx_test::TempOutFile *tmpfile_;
 };
 
 TEST_P(Y4mVideoWriteTest, WriteTest) {
--- a/y4menc.c
+++ b/y4menc.c
@@ -48,6 +48,7 @@
               "C420p16 XYSCSS=420P16\n";
       break;
     default:
+      color = NULL;
       assert(0);
   }
   return snprintf(buf, len, "YUV4MPEG2 W%u H%u F%u:%u I%c %s", width, height,