NSV.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #if !defined(NSV_HPP)
  2. #define NSV_HPP
  3. //______________________________________________________________________________
  4. //
  5. // NSV.hpp
  6. // NSV File I/O Classes
  7. #include <FourCC.hpp>
  8. #include <exception>
  9. #include <string>
  10. #include <vector>
  11. #include <fstream>
  12. #include <iosfwd>
  13. namespace NSV
  14. {
  15. #if (defined(__POWERPC__) || defined(__APPLE__))
  16. typedef long long INT64;
  17. typedef long long POS_TYPE;
  18. typedef std::ios IOS_BASE;
  19. #elif (defined(__linux) || defined(linux))
  20. typedef long long INT64; // Assumes WIN32
  21. typedef std::ios::pos_type POS_TYPE;
  22. typedef std::ios_base IOS_BASE;
  23. #else
  24. typedef __int64 INT64; // Assumes WIN32
  25. typedef std::ios::pos_type POS_TYPE;
  26. typedef std::ios_base IOS_BASE;
  27. #endif
  28. enum
  29. {
  30. FrameRates = 256
  31. };
  32. double frameRate(int nFrameRate);
  33. int frameRate(unsigned int uiRateNum, unsigned int uiRateDenom);
  34. bool video(FourCC fccNSV);
  35. bool audio(FourCC fccNSV);
  36. FourCC videoFormat(FourCC fccCompression);
  37. FourCC audioFormat(unsigned short wFormatTag);
  38. FourCC videoCompression(FourCC fccNSV);
  39. int videoBitCount(FourCC fccNSV);
  40. unsigned short audioFormatTag(FourCC fccNSV);
  41. //--------------------------------------
  42. class Exception : public std::exception
  43. {
  44. public:
  45. Exception();
  46. Exception(const std::string& strException);
  47. Exception(const Exception& e);
  48. ~Exception() throw();
  49. Exception& operator=(const Exception& e);
  50. const char* what() const throw();
  51. private:
  52. std::string m_strException;
  53. };
  54. //--------------------------------------
  55. class IndexEntry
  56. {
  57. public:
  58. IndexEntry(POS_TYPE posOffset = 0, size_t sizeData = -1, bool bKeyFrame = false) :
  59. m_posOffset(posOffset),
  60. m_sizeData(sizeData),
  61. m_bKeyFrame(bKeyFrame)
  62. {
  63. }
  64. //private:
  65. POS_TYPE m_posOffset;
  66. size_t m_sizeData;
  67. bool m_bKeyFrame;
  68. };
  69. typedef std::vector<IndexEntry> Index;
  70. //--------------------------------------
  71. class FileHeader
  72. {
  73. public:
  74. friend std::ostream& operator<<(std::ostream& os, const FileHeader& fh);
  75. FileHeader() :
  76. m_fccSignature(0UL),
  77. m_sizeHeader(0),
  78. m_sizeFile(0),
  79. m_iFileSize_ms(0),
  80. m_sizeMetaData(0),
  81. m_nTOCAlloc(0),
  82. m_nTOCSize(0),
  83. m_strMetaData(),
  84. m_index()
  85. {
  86. }
  87. //private:
  88. FourCC m_fccSignature;
  89. size_t m_sizeHeader;
  90. size_t m_sizeFile;
  91. int m_iFileSize_ms;
  92. size_t m_sizeMetaData;
  93. int m_nTOCAlloc;
  94. int m_nTOCSize;
  95. std::string m_strMetaData;
  96. std::vector<size_t> m_index;
  97. };
  98. //--------------------------------------
  99. class FrameHeader
  100. {
  101. public:
  102. friend std::ostream& operator<<(std::ostream& os, const FrameHeader& fh);
  103. FrameHeader() :
  104. m_fccSignature(0UL),
  105. m_fccVideo(0UL),
  106. m_fccAudio(0UL),
  107. m_iWidth(0),
  108. m_iHeight(0),
  109. m_iFrameRate(0),
  110. m_iSyncOffset_ms(0),
  111. m_bKeyFrame(false)
  112. {
  113. }
  114. //private:
  115. FourCC m_fccSignature;
  116. FourCC m_fccVideo;
  117. FourCC m_fccAudio;
  118. int m_iWidth;
  119. int m_iHeight;
  120. int m_iFrameRate;
  121. int m_iSyncOffset_ms;
  122. bool m_bKeyFrame;
  123. };
  124. class Stream;
  125. //--------------------------------------
  126. class File
  127. {
  128. friend std::ostream& operator<<(std::ostream& os, const File& f);
  129. public:
  130. File();
  131. ~File();
  132. // File header
  133. void header(const std::string& strMetaData, int nIndexEntries);
  134. void metaData(const std::string& strMetaData);
  135. const std::string& metaData() const;
  136. void indexEntry(int nEntry, POS_TYPE posOffset, size_t sizeData = -1, bool bKeyFrame = false);
  137. void appendIndexEntry(POS_TYPE posOffset, size_t sizeData = -1, bool bKeyFrame = false);
  138. Index& index(); // Relative index
  139. int indexEntriesUsed() const;
  140. // Synchronization frame header
  141. FourCC videoFormat() const;
  142. FourCC audioFormat() const;
  143. void size(int iWidth, int iHeight);
  144. int width()const;
  145. int height() const;
  146. void frameRate(unsigned char ucFrameRate);
  147. unsigned char frameRate() const;
  148. void rate(unsigned int uiRateNum, unsigned int uiRateDenom);
  149. unsigned int rateNum() const;
  150. unsigned int rateDenom() const;
  151. void syncOffset(int iSyncOffset_ms);
  152. int syncOffset() const;
  153. void finalizeSyncOffset();
  154. void frames(INT64 nFrames);
  155. INT64 frames() const;
  156. Stream& newStream(FourCC fccDataType, unsigned int uiRateNum = 0, unsigned int uiRateDenom = 0, INT64 nSamples = -1);
  157. int streams() const;
  158. int streamsAux() const;
  159. Stream& stream(int nStream);
  160. const Stream& stream(int nStream) const;
  161. int streamVideo() const;
  162. int streamAudio() const;
  163. int streamAux(int nAux) const;
  164. void dataOffset(POS_TYPE posDataOffset);
  165. POS_TYPE dataOffset();
  166. private:
  167. File(const File& f); // Not implemented
  168. File& operator=(const File& f); // Not implemented
  169. std::string m_strMetaData;
  170. Index m_index;
  171. int m_nIndexEntriesUsed;
  172. int m_iWidth;
  173. int m_iHeight;
  174. unsigned int m_uiRateNum;
  175. unsigned int m_uiRateDenom;
  176. unsigned char m_ucFrameRate;
  177. int m_iSyncOffset_ms;
  178. std::vector<Stream> m_vStream;
  179. POS_TYPE m_posDataOffset;
  180. int m_nStreamVideo;
  181. int m_nStreamAudio;
  182. INT64 m_nFrames;
  183. };
  184. //--------------------------------------
  185. class Stream
  186. {
  187. friend std::ostream& operator<<(std::ostream& os, const Stream& s);
  188. public:
  189. Stream(File* pFile, int nStream, FourCC fccDataType, unsigned int uiRateNum, unsigned int uiRateDenom, INT64 nSamples);
  190. Stream(const Stream& s);
  191. ~Stream();
  192. Stream& operator=(const Stream& s);
  193. void open();
  194. void close();
  195. int stream() const;
  196. FourCC type() const; // Returns AVI types, vids, auds, ...
  197. FourCC dataType() const;
  198. void samples(INT64 nSamples);
  199. INT64 samples() const;
  200. void sample(INT64 nSample);
  201. INT64 sample() const;
  202. void rate(unsigned int uiRateNum, unsigned int uiRateDenom);
  203. unsigned int rateNum() const;
  204. unsigned int rateDenom() const;
  205. void dataSize(size_t sizeData);
  206. void data(const unsigned char* pData, size_t sizeData);
  207. unsigned char* data();
  208. size_t dataSize() const;
  209. void keyFrame(bool bKeyFrame);
  210. bool keyFrame() const;
  211. private:
  212. File* m_pFile;
  213. int m_nStream;
  214. FourCC m_fccDataType;
  215. INT64 m_nSamples;
  216. INT64 m_nSample;
  217. unsigned int m_uiRateNum;
  218. unsigned int m_uiRateDenom;
  219. unsigned char* m_pBuffer;
  220. size_t m_sizeBuffer;
  221. size_t m_sizeData;
  222. bool m_bKeyFrame;
  223. };
  224. /*
  225. //--------------------------------------
  226. class Reader
  227. {
  228. public:
  229. Reader(File& f);
  230. Reader(File& f, const std::string& strFile);
  231. ~Reader();
  232. void open(const std::string& strFile);
  233. void open();
  234. void close();
  235. File& file();
  236. const std::string& fileName() const;
  237. void readFileHeader();
  238. void readFrame();
  239. void readFrameInfo();
  240. void readFrameHeader();
  241. void readPayload();
  242. void readPayloadInfo();
  243. void readPayloadHeader(int& nAux, int& iAuxPlusVideo, int& iAudio);
  244. INT64 frames() const;
  245. INT64 frame() const;
  246. void seek(INT64 nFrame);
  247. void readSample(int nStream, unsigned char* pData, size_t sizeDataMax, size_t& sizeData, bool& bKeyFrame);
  248. bool eof();
  249. const FileHeader& fileHeader() const;
  250. const FrameHeader& frameHeader() const;
  251. private:
  252. Reader(const Reader& r); // Not implemented
  253. Reader& operator=(const Reader& r); // Not implemented
  254. short read_i16();
  255. unsigned short read_ui16();
  256. int read_i32();
  257. unsigned int read_ui32();
  258. File& m_file;
  259. std::string m_strFile;
  260. std::ifstream m_ifs;
  261. FileHeader m_fileHeader;
  262. FrameHeader m_frameHeader;
  263. bool m_bFrameHeader;
  264. INT64 m_nFrame;
  265. };
  266. */
  267. //--------------------------------------
  268. class Writer
  269. {
  270. public:
  271. Writer(File& f);
  272. Writer(File& f, const std::string& strFile);
  273. ~Writer();
  274. void open(const std::string& strFile);
  275. void open();
  276. void close();
  277. File& file();
  278. const std::string& fileName() const;
  279. INT64 frame() const;
  280. void seek(POS_TYPE posOffset);
  281. void header(const std::string& strMetaData, int nIndexEntries);
  282. void sample(int nStream, const unsigned char* pData, size_t sizeData, bool bKeyframe);
  283. void writeFrame();
  284. private:
  285. Writer(const Writer& w); // Not implemented
  286. Writer& operator=(const Writer& w); // Not implemented
  287. void writeFileHeader();
  288. void writeFrameHeader(bool bKeyFrame);
  289. void writePayloadHeader();
  290. void write_i16(short i16);
  291. void write_ui16(unsigned short ui16);
  292. void write_i32(int i32);
  293. void write_ui32(unsigned int ui32);
  294. File& m_file;
  295. std::string m_strFile;
  296. std::ofstream m_ofs;
  297. INT64 m_nFrame;
  298. bool m_bKeyFrame;
  299. };
  300. } // namespace NSV
  301. #endif // NSV_HPP