video_source.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef VPX_TEST_VIDEO_SOURCE_H_
  11. #define VPX_TEST_VIDEO_SOURCE_H_
  12. #if defined(_WIN32)
  13. #undef NOMINMAX
  14. #define NOMINMAX
  15. #ifndef WIN32_LEAN_AND_MEAN
  16. #define WIN32_LEAN_AND_MEAN
  17. #endif
  18. #include <windows.h>
  19. #endif
  20. #include <cstdio>
  21. #include <cstdlib>
  22. #include <string>
  23. #include "test/acm_random.h"
  24. #include "vpx/vpx_encoder.h"
  25. namespace libvpx_test {
  26. // Helper macros to ensure LIBVPX_TEST_DATA_PATH is a quoted string.
  27. // These are undefined right below GetDataPath
  28. // NOTE: LIBVPX_TEST_DATA_PATH MUST NOT be a quoted string before
  29. // Stringification or the GetDataPath will fail at runtime
  30. #define TO_STRING(S) #S
  31. #define STRINGIFY(S) TO_STRING(S)
  32. // A simple function to encapsulate cross platform retrieval of test data path
  33. static std::string GetDataPath() {
  34. const char *const data_path = getenv("LIBVPX_TEST_DATA_PATH");
  35. if (data_path == NULL) {
  36. #ifdef LIBVPX_TEST_DATA_PATH
  37. // In some environments, we cannot set environment variables
  38. // Instead, we set the data path by using a preprocessor symbol
  39. // which can be set from make files
  40. return STRINGIFY(LIBVPX_TEST_DATA_PATH);
  41. #else
  42. return ".";
  43. #endif
  44. }
  45. return data_path;
  46. }
  47. // Undefining stringification macros because they are not used elsewhere
  48. #undef TO_STRING
  49. #undef STRINGIFY
  50. inline FILE *OpenTestDataFile(const std::string &file_name) {
  51. const std::string path_to_source = GetDataPath() + "/" + file_name;
  52. return fopen(path_to_source.c_str(), "rb");
  53. }
  54. static FILE *GetTempOutFile(std::string *file_name) {
  55. file_name->clear();
  56. #if defined(_WIN32)
  57. char fname[MAX_PATH];
  58. char tmppath[MAX_PATH];
  59. if (GetTempPathA(MAX_PATH, tmppath)) {
  60. // Assume for now that the filename generated is unique per process
  61. if (GetTempFileNameA(tmppath, "lvx", 0, fname)) {
  62. file_name->assign(fname);
  63. return fopen(fname, "wb+");
  64. }
  65. }
  66. return NULL;
  67. #else
  68. return tmpfile();
  69. #endif
  70. }
  71. class TempOutFile {
  72. public:
  73. TempOutFile() { file_ = GetTempOutFile(&file_name_); }
  74. ~TempOutFile() {
  75. CloseFile();
  76. if (!file_name_.empty()) {
  77. EXPECT_EQ(0, remove(file_name_.c_str()));
  78. }
  79. }
  80. FILE *file() { return file_; }
  81. const std::string &file_name() { return file_name_; }
  82. protected:
  83. void CloseFile() {
  84. if (file_) {
  85. fclose(file_);
  86. file_ = NULL;
  87. }
  88. }
  89. FILE *file_;
  90. std::string file_name_;
  91. };
  92. // Abstract base class for test video sources, which provide a stream of
  93. // vpx_image_t images with associated timestamps and duration.
  94. class VideoSource {
  95. public:
  96. virtual ~VideoSource() {}
  97. // Prepare the stream for reading, rewind/open as necessary.
  98. virtual void Begin() = 0;
  99. // Advance the cursor to the next frame
  100. virtual void Next() = 0;
  101. // Get the current video frame, or NULL on End-Of-Stream.
  102. virtual vpx_image_t *img() const = 0;
  103. // Get the presentation timestamp of the current frame.
  104. virtual vpx_codec_pts_t pts() const = 0;
  105. // Get the current frame's duration
  106. virtual unsigned long duration() const = 0;
  107. // Get the timebase for the stream
  108. virtual vpx_rational_t timebase() const = 0;
  109. // Get the current frame counter, starting at 0.
  110. virtual unsigned int frame() const = 0;
  111. // Get the current file limit.
  112. virtual unsigned int limit() const = 0;
  113. };
  114. class DummyVideoSource : public VideoSource {
  115. public:
  116. DummyVideoSource()
  117. : img_(NULL), limit_(100), width_(80), height_(64),
  118. format_(VPX_IMG_FMT_I420) {
  119. ReallocImage();
  120. }
  121. virtual ~DummyVideoSource() { vpx_img_free(img_); }
  122. virtual void Begin() {
  123. frame_ = 0;
  124. FillFrame();
  125. }
  126. virtual void Next() {
  127. ++frame_;
  128. FillFrame();
  129. }
  130. virtual vpx_image_t *img() const { return (frame_ < limit_) ? img_ : NULL; }
  131. // Models a stream where Timebase = 1/FPS, so pts == frame.
  132. virtual vpx_codec_pts_t pts() const { return frame_; }
  133. virtual unsigned long duration() const { return 1; }
  134. virtual vpx_rational_t timebase() const {
  135. const vpx_rational_t t = { 1, 30 };
  136. return t;
  137. }
  138. virtual unsigned int frame() const { return frame_; }
  139. virtual unsigned int limit() const { return limit_; }
  140. void set_limit(unsigned int limit) { limit_ = limit; }
  141. void SetSize(unsigned int width, unsigned int height) {
  142. if (width != width_ || height != height_) {
  143. width_ = width;
  144. height_ = height;
  145. ReallocImage();
  146. }
  147. }
  148. void SetImageFormat(vpx_img_fmt_t format) {
  149. if (format_ != format) {
  150. format_ = format;
  151. ReallocImage();
  152. }
  153. }
  154. protected:
  155. virtual void FillFrame() {
  156. if (img_) memset(img_->img_data, 0, raw_sz_);
  157. }
  158. void ReallocImage() {
  159. vpx_img_free(img_);
  160. img_ = vpx_img_alloc(NULL, format_, width_, height_, 32);
  161. raw_sz_ = ((img_->w + 31) & ~31) * img_->h * img_->bps / 8;
  162. }
  163. vpx_image_t *img_;
  164. size_t raw_sz_;
  165. unsigned int limit_;
  166. unsigned int frame_;
  167. unsigned int width_;
  168. unsigned int height_;
  169. vpx_img_fmt_t format_;
  170. };
  171. class RandomVideoSource : public DummyVideoSource {
  172. public:
  173. RandomVideoSource(int seed = ACMRandom::DeterministicSeed())
  174. : rnd_(seed), seed_(seed) {}
  175. protected:
  176. // Reset the RNG to get a matching stream for the second pass
  177. virtual void Begin() {
  178. frame_ = 0;
  179. rnd_.Reset(seed_);
  180. FillFrame();
  181. }
  182. // 15 frames of noise, followed by 15 static frames. Reset to 0 rather
  183. // than holding previous frames to encourage keyframes to be thrown.
  184. virtual void FillFrame() {
  185. if (img_) {
  186. if (frame_ % 30 < 15) {
  187. for (size_t i = 0; i < raw_sz_; ++i) img_->img_data[i] = rnd_.Rand8();
  188. } else {
  189. memset(img_->img_data, 0, raw_sz_);
  190. }
  191. }
  192. }
  193. ACMRandom rnd_;
  194. int seed_;
  195. };
  196. // Abstract base class for test video sources, which provide a stream of
  197. // decompressed images to the decoder.
  198. class CompressedVideoSource {
  199. public:
  200. virtual ~CompressedVideoSource() {}
  201. virtual void Init() = 0;
  202. // Prepare the stream for reading, rewind/open as necessary.
  203. virtual void Begin() = 0;
  204. // Advance the cursor to the next frame
  205. virtual void Next() = 0;
  206. virtual const uint8_t *cxdata() const = 0;
  207. virtual size_t frame_size() const = 0;
  208. virtual unsigned int frame_number() const = 0;
  209. };
  210. } // namespace libvpx_test
  211. #endif // VPX_TEST_VIDEO_SOURCE_H_