Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CryVideoWebM.cpp
- /*************************************************************************
- Cry Video Source File.
- Copyright (C), Marwin Misselhorn 2012
- -------------------------------------------------------------------------
- *************************************************************************/
- #include <CryModuleDefs.h>
- #define eCryModule eCryM_Game
- // Insert your headers here
- #include <platform.h>
- #include <memory>
- #include <smartptr.h>
- #include <ISystem.h>
- #include <Cry_Math.h>
- #ifndef WEBMDLL_EXPORTS
- #define WEBMDLL_EXPORTS
- #endif
- #ifdef WEBMDLL_EXPORTS
- #define GAME_API DLL_EXPORT
- #else
- #define GAME_API
- #endif
- #pragma warning(disable: 4018) // conditional expression is constant
- //Cry Video MOD:
- #include <ICryVideoWebM.h>
- #include "CryVideoWebM.h"
- #include "CryFunctions.h"
- #include "ICryPak.h"
- #include <windows.h>
- #include <sstream>
- using namespace std;
- // We need shell api for Current Root Extraction.
- #include "shlwapi.h"
- #pragma comment(lib, "shlwapi.lib")
- CCryVideoWebM *g_pCryVideoWebM = 0;
- stringstream YUVStream(ios_base::in|ios_base::out|ios_base::binary);
- long YSize;
- long UVSize;
- long YUVSize;
- //////////////////////////////////////////////////////////////////////////
- // Initializes Root folder of the game.
- //////////////////////////////////////////////////////////////////////////
- void InitRootDir()
- {
- #ifdef WIN32
- WCHAR szExeFileName[_MAX_PATH];
- GetModuleFileNameW( GetModuleHandle(NULL), szExeFileName, sizeof(szExeFileName));
- PathRemoveFileSpecW(szExeFileName);
- // Remove Bin32/Bin64 folder/
- WCHAR* lpPath = StrStrIW(szExeFileName,L"\\Bin32");
- if (lpPath)
- *lpPath = 0;
- lpPath = StrStrIW(szExeFileName,L"\\Bin64");
- if (lpPath)
- *lpPath = 0;
- SetCurrentDirectoryW( szExeFileName );
- #endif
- }
- CCryVideoWebM::CCryVideoWebM()
- {
- started = false;
- g_pCryVideoWebM = this;
- InitRootDir();
- }
- CCryVideoWebM::~CCryVideoWebM()
- {
- g_pCryVideoWebM = 0;
- CloseWebMFile();
- started = false;
- }
- bool CCryVideoWebM::Init(ISystem *pSystem, IGameFramework *pGameFramework)
- {
- started = false;
- gEnv = pSystem->GetGlobalEnvironment();
- m_pFramework = pGameFramework;
- CryFunctions cryFunctions;
- started = true;
- return true;
- }
- bool CCryVideoWebM::LoadWebMFile(const char* fileName, bool swapuv)
- {
- int flipuv = 0;
- if (swapuv)
- {
- flipuv = 1;
- CryLogAlways("Load WebM File DX11");
- }
- else
- {
- CryLogAlways("Load WebM File DX9");
- }
- int result = LoadFileWebM(fileName, flipuv);
- CryLogAlways("Load WebM File --> Result:%i", result);
- if (result == 1)
- {
- YSize = GetVideoWidth()*GetVideoHeight();
- UVSize = YSize / 4;
- YUVSize = YSize + (UVSize*2);
- return true;
- }
- return false;
- }
- bool CCryVideoWebM::CloseWebMFile()
- {
- CryLogAlways("Close WebM File");
- int result = CloseFileWebM();
- CryLogAlways("Close WebM File --> Result:%i", result);
- YUVStream.str("");
- YUVStream.clear();
- if (result == 0)
- return true;
- return false;
- }
- bool CCryVideoWebM::GetNextFrame()
- {
- //CryLogAlways("Get Next Frame VPX");
- int result = ReadNextFrameWebM();
- //CryLogAlways("Result: %i", result);
- if (result == 1)
- return true;
- return false;
- }
- bool CCryVideoWebM::RetrieveNextFrame(char* yPlane, char* uPlane, char* vPlane, bool oneBuffer, char* yuvPlane)
- {
- YUVStream.str("");
- YUVStream.clear();
- int result = GetNextFrameWebM();
- if (result == 1)
- {
- //read content of frame
- if (oneBuffer)
- {
- YUVStream.read(yuvPlane,YUVSize);
- }
- else
- {
- YUVStream.read(yPlane,YSize);
- YUVStream.read(uPlane,UVSize);
- YUVStream.read(vPlane,UVSize);
- }
- return true;
- }
- return false;
- }
- int CCryVideoWebM::GetVideoWidth()
- {
- int width = GetVideoWidthWebM();
- return width;
- }
- int CCryVideoWebM::GetVideoHeight()
- {
- int height = GetVideoHeightWebM();
- return height;
- }
- float CCryVideoWebM::GetVideoFrameRate()
- {
- int frameNum = GetVideoFrameNumWebM();
- int frameDen = GetVideoFrameDenWebM();
- float frameRate = (float)frameNum / (float)frameDen;
- return frameRate;
- }
- void CCryVideoWebM::Shutdown()
- {
- this->~CCryVideoWebM();
- }
- void CCryVideoWebM::GetMemoryStatistics(ICrySizer * s) const
- {
- s->Add(*this);
- }
- CryFunctions::CryFunctions() : a_(0) { }
- FILE* CryOpenFile(char* fileName, char* mode)
- {
- FILE* file = gEnv->pCryPak->FOpen(fileName, mode);
- return file;
- }
- int CryCloseFile(FILE* handle)
- {
- int result = -2;
- CryLogAlways("Close CryPak File");
- if (handle)
- {
- result = gEnv->pCryPak->FClose(handle);
- }
- CryLogAlways("Close CryPak File --> Result:%i", result);
- return result;
- }
- size_t CryReadFile(void* data, size_t elems, size_t length, FILE* handle)
- {
- return (gEnv->pCryPak->FReadRaw(data, length, elems, handle));
- }
- size_t CryWriteFile(const void* data, size_t elems, size_t length, FILE* handle)
- {
- return (gEnv->pCryPak->FWrite(data, length, elems, handle));
- }
- int CrySeekFile(FILE* handle, long seek, int mode)
- {
- return ((int)gEnv->pCryPak->FSeek(handle, seek, mode));
- }
- void CryFileRewind(FILE* handle)
- {
- gEnv->pCryPak->FSeek(handle, 0L, SEEK_SET);
- }
- long CryTellFile(FILE* handle)
- {
- return(gEnv->pCryPak->FTell(handle));
- }
- int CryFileError(FILE* handle)
- {
- return(gEnv->pCryPak->FError(handle));
- }
- int CryFileEOF(FILE* handle)
- {
- return(gEnv->pCryPak->FEof(handle));
- }
- size_t CryFileGetSize(FILE* handle)
- {
- return(gEnv->pCryPak->FGetSize(handle));
- }
- int WriteToYUVStream(const void* data, int count)
- {
- YUVStream.write((const char*)data, count);
- return 1;
- }
- void VPXLog(const char* text)
- {
- CryLogAlways("[VPX] - %s", text);
- }
- vpxdec.c
- int ReadNextFrameWebM()
- {
- //Just read frame and decode it, not retrieve it yet
- int result = read_frame(&input, &buf, &buf_sz, &buf_alloc_sz);
- int corrupted;
- /* Decode file */
- if (vpx_codec_decode(&decoder, buf, buf_sz, NULL, 0))
- {
- VPXLog("Failed to decode frame!");
- return 0;
- }
- if (vpx_codec_control(&decoder, VP8D_GET_FRAME_CORRUPTED, &corrupted))
- {
- VPXLog("Warning: Corrupted frame detected!");
- }
- //VPXLog("Frame read!");
- if (result == 0)
- {
- return 1;
- }
- return 0;
- }
- int GetNextFrameWebM()
- {
- //Retrieve frame
- vpx_codec_iter_t iter = NULL;
- img = vpx_codec_get_frame(&decoder, &iter);
- //if ((img = vpx_codec_get_frame(&decoder, &iter)))
- //++frame_out;
- if (img)
- {
- uint8_t *buf;
- unsigned int y;
- buf = img->planes[VPX_PLANE_Y];
- for (y = 0; y < img->d_h; y++)
- {
- WriteToYUVStream(buf, img->d_w);
- buf += img->stride[VPX_PLANE_Y];
- }
- buf = img->planes[flipuv?VPX_PLANE_V:VPX_PLANE_U];
- for (y = 0; y < (1 + img->d_h) / 2; y++)
- {
- WriteToYUVStream(buf, (1 + img->d_w) / 2);
- buf += img->stride[VPX_PLANE_U];
- }
- buf = img->planes[flipuv?VPX_PLANE_U:VPX_PLANE_V];
- for (y = 0; y < (1 + img->d_h) / 2; y++)
- {
- WriteToYUVStream(buf, (1 + img->d_w) / 2);
- buf += img->stride[VPX_PLANE_V];
- }
- return 1;
- }
- CloseFileWebM();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement