1
0

Rvd.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef RVD_HPP
  2. #define RVD_HPP
  3. #pragma warning(disable:4786)
  4. #include "FourCC.hpp"
  5. //#include <io.h>
  6. #include <windows.h>
  7. #include <exception>
  8. #include <cstdlib>
  9. #include <cstddef>
  10. #include <string>
  11. #include <map>
  12. namespace Rvd
  13. {
  14. class FileError : public exception
  15. {
  16. public:
  17. explicit FileError(const char*);
  18. explicit FileError(DWORD);
  19. const char* what() const;
  20. private:
  21. std::string message;
  22. };
  23. bool nameIsOK(const char* name);
  24. enum Format { rgb24, rgb32, yuv12 };
  25. const char* asStr(Format f);
  26. struct Header
  27. {
  28. FourCC code;
  29. __int32 width;
  30. __int32 height;
  31. __int32 blockSize;
  32. FourCC compression;
  33. __int32 unknown; //frame count?
  34. __int32 rate;
  35. __int32 scale;
  36. __int8 pad[480];
  37. };
  38. typedef unsigned __int64 size_type;
  39. size_type estimatedFileSize(
  40. int width,
  41. int height,
  42. Format format,
  43. int frameCount);
  44. class File
  45. {
  46. public:
  47. enum mode_t {in, out, inout};
  48. File();
  49. File(const char* name, mode_t mode);
  50. ~File();
  51. void open(const char* name, mode_t mode, DWORD flags = 0);
  52. void close();
  53. void setFormat(Format);
  54. void setWidth(int w);
  55. void setHeight(int h);
  56. void setRate(int rate);
  57. void setScale(int scale);
  58. void reset(int frameNum) const;
  59. void advance(int count) const;
  60. void read(void* frame) const;
  61. void write(const void* frame);
  62. void writeHeader();
  63. bool isOpen() const;
  64. bool eof() const;
  65. mode_t mode() const;
  66. const char* name() const;
  67. int frameNum() const;
  68. int frameCount() const;
  69. bool frameCountIsOK() const;
  70. Format format() const;
  71. const FourCC compression() const;
  72. int width() const;
  73. int height() const;
  74. int rate() const;
  75. int scale() const;
  76. size_t frameSize() const;
  77. size_t paddedFrameSize() const;
  78. private:
  79. File& operator=(const File&);
  80. File(const File&);
  81. //int file;
  82. HANDLE m_handle;
  83. mutable int frameNum_;
  84. int frameCount_;
  85. bool frameCountIsOK_;
  86. char name_[_MAX_PATH];
  87. mode_t mode_;
  88. Header* const m_header;
  89. Format format_;
  90. size_t frameSize_;
  91. size_t paddedFrameSize_;
  92. size_t paddingSize_;
  93. bool unbuffered;
  94. };
  95. class MappedFile
  96. {
  97. public:
  98. MappedFile();
  99. MappedFile(const char* name);
  100. ~MappedFile();
  101. void open(const char* name);
  102. void close();
  103. bool isOpen() const;
  104. const char* name() const;
  105. int frameCount() const;
  106. Format format() const;
  107. int width() const;
  108. int height() const;
  109. int rate() const;
  110. int scale() const;
  111. size_t frameSize() const;
  112. void* map(int frameNum) const;
  113. void unmap(int frameNum) const;
  114. void unmap() const;
  115. bool isMapped(int frameNum) const;
  116. private:
  117. MappedFile(const MappedFile&);
  118. MappedFile& operator=(const MappedFile&);
  119. void init();
  120. Header header;
  121. Format m_format;
  122. size_t m_frameSize;
  123. size_t m_paddedFrameSize;
  124. std::string m_name;
  125. HANDLE file;
  126. HANDLE mapping;
  127. DWORD allocationGranularity;
  128. int m_frameCount;
  129. struct ViewInfo
  130. {
  131. void* view;
  132. void* frame;
  133. };
  134. typedef std::map<int, ViewInfo> Views;
  135. mutable Views views;
  136. };
  137. }
  138. #endif