Advertisement
dragonbane

[CryVideo] CryVideoWebM.dll

Feb 24th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.11 KB | None | 0 0
  1. CryVideoWebM.cpp
  2.  
  3. /*************************************************************************
  4.   Cry Video Source File.
  5.   Copyright (C), Marwin Misselhorn 2012
  6.  -------------------------------------------------------------------------
  7. *************************************************************************/
  8.  
  9. #include <CryModuleDefs.h>
  10. #define eCryModule eCryM_Game
  11.  
  12. // Insert your headers here
  13. #include <platform.h>
  14. #include <memory>
  15. #include <smartptr.h>
  16. #include <ISystem.h>
  17.  
  18. #include <Cry_Math.h>
  19.  
  20.  
  21. #ifndef WEBMDLL_EXPORTS
  22. #define WEBMDLL_EXPORTS
  23. #endif
  24.  
  25. #ifdef WEBMDLL_EXPORTS
  26. #define GAME_API DLL_EXPORT
  27. #else
  28. #define GAME_API
  29. #endif
  30.  
  31. #pragma warning(disable: 4018)  // conditional expression is constant
  32.  
  33. //Cry Video MOD:
  34. #include <ICryVideoWebM.h>
  35. #include "CryVideoWebM.h"
  36. #include "CryFunctions.h"
  37. #include "ICryPak.h"
  38.  
  39. #include <windows.h>
  40. #include <sstream>
  41.  
  42.  
  43. using namespace std;
  44.  
  45. // We need shell api for Current Root Extraction.
  46. #include "shlwapi.h"
  47. #pragma comment(lib, "shlwapi.lib")
  48.  
  49. CCryVideoWebM *g_pCryVideoWebM = 0;
  50. stringstream YUVStream(ios_base::in|ios_base::out|ios_base::binary);
  51. long YSize;
  52. long UVSize;
  53. long YUVSize;
  54.  
  55. //////////////////////////////////////////////////////////////////////////
  56. // Initializes Root folder of the game.
  57. //////////////////////////////////////////////////////////////////////////
  58. void InitRootDir()
  59. {
  60. #ifdef WIN32
  61.     WCHAR szExeFileName[_MAX_PATH];
  62.  
  63.     GetModuleFileNameW( GetModuleHandle(NULL), szExeFileName, sizeof(szExeFileName));
  64.     PathRemoveFileSpecW(szExeFileName);
  65.  
  66.     // Remove Bin32/Bin64 folder/
  67.     WCHAR* lpPath = StrStrIW(szExeFileName,L"\\Bin32");
  68.     if (lpPath)
  69.         *lpPath = 0;
  70.     lpPath = StrStrIW(szExeFileName,L"\\Bin64");
  71.     if (lpPath)
  72.         *lpPath = 0;
  73.  
  74.     SetCurrentDirectoryW( szExeFileName );
  75.  
  76. #endif
  77. }
  78.  
  79. CCryVideoWebM::CCryVideoWebM()
  80. {
  81.     started = false;
  82.     g_pCryVideoWebM = this;
  83.  
  84.     InitRootDir();
  85. }
  86.  
  87. CCryVideoWebM::~CCryVideoWebM()
  88. {
  89.     g_pCryVideoWebM = 0;
  90.  
  91.     CloseWebMFile();
  92.  
  93.     started = false;
  94. }
  95.  
  96. bool CCryVideoWebM::Init(ISystem *pSystem, IGameFramework *pGameFramework)
  97. {
  98.     started = false;
  99.  
  100.     gEnv = pSystem->GetGlobalEnvironment();
  101.     m_pFramework = pGameFramework;
  102.  
  103.     CryFunctions cryFunctions;
  104.  
  105.     started = true;
  106.  
  107.     return true;
  108. }
  109.  
  110. bool CCryVideoWebM::LoadWebMFile(const char* fileName, bool swapuv)
  111. {
  112.     int flipuv = 0;
  113.  
  114.     if (swapuv)
  115.     {
  116.         flipuv = 1;
  117.         CryLogAlways("Load WebM File DX11");
  118.     }
  119.     else
  120.     {
  121.         CryLogAlways("Load WebM File DX9");
  122.     }
  123.  
  124.     int result = LoadFileWebM(fileName, flipuv);
  125.     CryLogAlways("Load WebM File --> Result:%i", result);
  126.  
  127.     if (result == 1)
  128.     {
  129.         YSize = GetVideoWidth()*GetVideoHeight();
  130.         UVSize = YSize / 4;
  131.         YUVSize = YSize + (UVSize*2);
  132.        
  133.         return true;
  134.     }
  135.  
  136.     return false;
  137. }
  138.  
  139. bool CCryVideoWebM::CloseWebMFile()
  140. {
  141.     CryLogAlways("Close WebM File");
  142.     int result = CloseFileWebM();
  143.     CryLogAlways("Close WebM File --> Result:%i", result);
  144.  
  145.     YUVStream.str("");
  146.     YUVStream.clear();
  147.  
  148.     if (result == 0)
  149.         return true;
  150.    
  151.     return false;
  152. }
  153.  
  154. bool CCryVideoWebM::GetNextFrame()
  155. {
  156.     //CryLogAlways("Get Next Frame VPX");
  157.     int result = ReadNextFrameWebM();
  158.  
  159.     //CryLogAlways("Result: %i", result);
  160.  
  161.     if (result == 1)
  162.         return true;
  163.  
  164.     return false;
  165. }
  166.  
  167. bool CCryVideoWebM::RetrieveNextFrame(char* yPlane, char* uPlane, char* vPlane, bool oneBuffer, char* yuvPlane)
  168. {
  169.     YUVStream.str("");
  170.     YUVStream.clear();
  171.  
  172.     int result = GetNextFrameWebM();
  173.  
  174.     if (result == 1)
  175.     {
  176.         //read content of frame
  177.         if (oneBuffer)
  178.         {
  179.             YUVStream.read(yuvPlane,YUVSize);
  180.         }
  181.         else
  182.         {
  183.             YUVStream.read(yPlane,YSize);
  184.             YUVStream.read(uPlane,UVSize);
  185.             YUVStream.read(vPlane,UVSize);
  186.         }
  187.  
  188.         return true;
  189.     }
  190.  
  191.     return false;
  192. }
  193.  
  194. int CCryVideoWebM::GetVideoWidth()
  195. {
  196.     int width = GetVideoWidthWebM();
  197.    
  198.     return width;
  199. }
  200.  
  201. int CCryVideoWebM::GetVideoHeight()
  202. {
  203.     int height = GetVideoHeightWebM();
  204.    
  205.     return height;
  206. }
  207.  
  208. float CCryVideoWebM::GetVideoFrameRate()
  209. {
  210.     int frameNum = GetVideoFrameNumWebM();
  211.     int frameDen = GetVideoFrameDenWebM();
  212.  
  213.     float frameRate = (float)frameNum / (float)frameDen;
  214.     return frameRate;
  215. }
  216.  
  217. void CCryVideoWebM::Shutdown()
  218. {
  219.     this->~CCryVideoWebM();
  220. }
  221.  
  222. void CCryVideoWebM::GetMemoryStatistics(ICrySizer * s) const
  223. {
  224.     s->Add(*this);
  225. }
  226.  
  227. CryFunctions::CryFunctions() : a_(0) { }
  228.  
  229.  
  230. FILE* CryOpenFile(char* fileName, char* mode)
  231. {
  232.   FILE* file = gEnv->pCryPak->FOpen(fileName, mode);
  233.  
  234.   return file;
  235. }
  236. int CryCloseFile(FILE* handle)
  237. {
  238.     int result = -2;
  239.    
  240.     CryLogAlways("Close CryPak File");
  241.     if (handle)
  242.     {
  243.         result = gEnv->pCryPak->FClose(handle);
  244.     }
  245.     CryLogAlways("Close CryPak File --> Result:%i", result);
  246.    
  247.     return result;
  248. }
  249. size_t CryReadFile(void* data, size_t elems, size_t length, FILE* handle)
  250. {
  251.     return (gEnv->pCryPak->FReadRaw(data, length, elems, handle));
  252. }
  253. size_t CryWriteFile(const void* data, size_t elems, size_t length, FILE* handle)
  254. {
  255.     return (gEnv->pCryPak->FWrite(data, length, elems, handle));
  256. }
  257. int CrySeekFile(FILE* handle, long seek, int mode)
  258. {
  259.     return ((int)gEnv->pCryPak->FSeek(handle, seek, mode));
  260. }
  261. void CryFileRewind(FILE* handle)
  262. {
  263.     gEnv->pCryPak->FSeek(handle, 0L, SEEK_SET);
  264. }
  265. long CryTellFile(FILE* handle)
  266. {
  267.     return(gEnv->pCryPak->FTell(handle));
  268. }
  269. int CryFileError(FILE* handle)
  270. {
  271.     return(gEnv->pCryPak->FError(handle));
  272. }
  273. int CryFileEOF(FILE* handle)
  274. {
  275.     return(gEnv->pCryPak->FEof(handle));
  276. }
  277.  
  278. size_t CryFileGetSize(FILE* handle)
  279. {
  280.     return(gEnv->pCryPak->FGetSize(handle));
  281. }
  282.  
  283. int WriteToYUVStream(const void* data, int count)
  284. {
  285.     YUVStream.write((const char*)data, count);
  286.    
  287.     return 1;
  288. }
  289.  
  290. void VPXLog(const char* text)
  291. {
  292.     CryLogAlways("[VPX] - %s", text);
  293. }
  294.  
  295.  
  296.  
  297. vpxdec.c
  298.  
  299. int ReadNextFrameWebM()
  300. {
  301.     //Just read frame and decode it, not retrieve it yet
  302.     int result = read_frame(&input, &buf, &buf_sz, &buf_alloc_sz);
  303.     int corrupted;
  304.    
  305.     /* Decode file */
  306.     if (vpx_codec_decode(&decoder, buf, buf_sz, NULL, 0))
  307.     {
  308.        VPXLog("Failed to decode frame!");
  309.  
  310.        return 0;
  311.     }
  312.     if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted))
  313.     {
  314.         VPXLog("Warning: Corrupted frame detected!");
  315.     }
  316.  
  317.     //VPXLog("Frame read!");
  318.  
  319.     if (result == 0)
  320.     {
  321.        return 1;  
  322.     }
  323.  
  324.     return 0;
  325. }
  326.  
  327. int GetNextFrameWebM()
  328. {
  329.     //Retrieve frame
  330.     vpx_codec_iter_t iter = NULL;
  331.  
  332.     img = vpx_codec_get_frame(&decoder, &iter);
  333.  
  334.      //if ((img = vpx_codec_get_frame(&decoder, &iter)))
  335.            //++frame_out;
  336.  
  337.     if (img)
  338.     {
  339.         uint8_t *buf;
  340.         unsigned int y;
  341.  
  342.         buf = img->planes[VPX_PLANE_Y];
  343.        
  344.         for (y = 0; y < img->d_h; y++)
  345.         {
  346.            WriteToYUVStream(buf, img->d_w);
  347.            buf += img->stride[VPX_PLANE_Y];
  348.         }
  349.  
  350.         buf = img->planes[flipuv?VPX_PLANE_V:VPX_PLANE_U];
  351.  
  352.         for (y = 0; y < (1 + img->d_h) / 2; y++)
  353.         {
  354.            WriteToYUVStream(buf, (1 + img->d_w) / 2);
  355.            buf += img->stride[VPX_PLANE_U];
  356.         }
  357.  
  358.         buf = img->planes[flipuv?VPX_PLANE_U:VPX_PLANE_V];
  359.  
  360.         for (y = 0; y < (1 + img->d_h) / 2; y++)
  361.         {
  362.            WriteToYUVStream(buf, (1 + img->d_w) / 2);
  363.            buf += img->stride[VPX_PLANE_V];
  364.         }
  365.  
  366.         return 1;
  367.      }
  368.  
  369.     CloseFileWebM();
  370.  
  371.     return 0;
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement