changeset 2137:d24ee391fd1f

libavcodec playing movies and houses, loops not working yet
author zipi
date Wed, 01 Jan 2014 13:43:09 +0000
parents d145d0e93a16
children 1283eedcc028
files Events.cpp Game.cpp OSWindow.h VideoPlayer.cpp VideoPlayer.h mm7_2.cpp mm7_data.h
diffstat 7 files changed, 412 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/Events.cpp	Tue Dec 31 23:03:01 2013 +0600
+++ b/Events.cpp	Wed Jan 01 13:43:09 2014 +0000
@@ -495,7 +495,7 @@
         v12 = (char *)&item.uExpireTime + strlen(Source) + 7;
         if ( *v12 == 32 )
           *v12 = 0;
-        if ( pVideoPlayer->field_54 )
+		if (pVideoPlayer->bBufferLoaded)
           pVideoPlayer->Unload();
         pVideoPlayer->bStopBeforeSchedule = 0;
         pVideoPlayer->pResetflag = 0;
--- a/Game.cpp	Tue Dec 31 23:03:01 2013 +0600
+++ b/Game.cpp	Wed Jan 01 13:43:09 2014 +0000
@@ -4574,8 +4574,8 @@
           viewparams->field_48 = 1;
           if ( !GetCurrentMenuID() || GetCurrentMenuID() == MENU_CREATEPARTY || GetCurrentMenuID() == MENU_NAMEPANELESC )
           {
-            if ( pCurrentScreen == SCREEN_VIDEO )
-              pVideoPlayer->FastForwardToFrame(pVideoPlayer->pResetflag);
+            //if ( pCurrentScreen == SCREEN_VIDEO )
+              //pVideoPlayer->FastForwardToFrame(pVideoPlayer->pResetflag);			  
             if (GetCurrentMenuID() == MENU_NAMEPANELESC)
             {
               SetCurrentMenuID(MENU_CREATEPARTY);
--- a/OSWindow.h	Tue Dec 31 23:03:01 2013 +0600
+++ b/OSWindow.h	Wed Jan 01 13:43:09 2014 +0000
@@ -2,6 +2,8 @@
 #include "OSAPI.h"
 #include "NewUI/Core/UIControl.h"
 
+extern class OSWindow *window;
+
 class OSWindow: public UIControl
 {
   public:
--- a/VideoPlayer.cpp	Tue Dec 31 23:03:01 2013 +0600
+++ b/VideoPlayer.cpp	Wed Jan 01 13:43:09 2014 +0000
@@ -301,7 +301,7 @@
   if (!bNoVideo)
   {
     pRenderer->PresentBlackScreen();
-    //PlayFullscreenMovie(MOVIE_3DOLogo, true);
+    PlayFullscreenMovie(MOVIE_3DOLogo, true);
     //pGame->pCShow->PlayMovie(MOVIE_3DOLogo, 1);
     if ( !pVideoPlayer->bStopBeforeSchedule )
     {
@@ -342,16 +342,13 @@
   bGameoverLoop = 0;
 }
 
-
-
-
 //----- (004BE70E) --------------------------------------------------------
 void VideoPlayer::MovieLoop(const char *pMovieName, int a2, int ScreenSizeFlag, int a4)
 {
   int v4; // ebp@1
   const char *pName; // edi@1
   MSG Msg; // [sp+Ch] [bp-1Ch]@12
-
+ 	  	  
   v4 = a2;
   pName = pMovieName;
   if ( !(dword_6BE364_game_settings_1 & (GAME_SETTINGS_NO_HOUSE_ANIM | GAME_SETTINGS_NO_INTRO)) )
@@ -361,7 +358,7 @@
     ShowCursor(0);
     pVideoPlayer->OpenGlobalMovie(pName, 0, ScreenSizeFlag);
     pVideoPlayer->bPlayingMovie = 1;
-    pVideoPlayer->field_44 = v4;
+	pVideoPlayer->field_44 = v4;
     if ( pRenderer->pRenderD3D )
     {
       pRenderer->ClearTarget(0);
@@ -373,63 +370,105 @@
       pRenderer->EndScene();
     }
     pCurrentScreen = SCREEN_VIDEO;
+
+	auto hwnd = pVideoPlayer->window->GetApiHandle();
+	RECT rc_client;
+	GetClientRect(hwnd, &rc_client);
+	int client_width = rc_client.right - rc_client.left,
+		client_height = rc_client.bottom - rc_client.top;
+
+	HDC     dc = GetDC(hwnd);
+	HDC     back_dc = CreateCompatibleDC(dc);
+	HBITMAP back_bmp;
+	SelectObject(back_dc, back_bmp = CreateCompatibleBitmap(dc, client_width, client_height));
+
+	while (true)
+	{
+		if ( pVideoPlayer->bStopBeforeSchedule )
+			break;
+		while (PeekMessageA(&Msg, hwnd, 0, 0, PM_REMOVE))
+		{
+			if (Msg.message == 18)
+				Game_DeinitializeAndTerminate(0);
+			if (Msg.message == 15)
+				break;
+			TranslateMessage(&Msg);
+			DispatchMessageA(&Msg);
+		}
+		GUI_MainMenuMessageProc();
+
+		if (!pVideoPlayer->mMovie)
+		{
+			pVideoPlayer->mMovie = pVideoPlayer->mPlayer->LoadMovie("dummyFilename", client_width, client_height);
+		}
+		else if (pVideoPlayer->mMovie->Stopped())
+		  break;
+		auto image = pVideoPlayer->mPlayer->DoFrame();
+		if (image)
+		{
+			// draw to hwnd
+			BITMAPINFO bmi;
+			bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+			bmi.bmiHeader.biWidth = client_width;
+			bmi.bmiHeader.biHeight = -client_height;
+			bmi.bmiHeader.biPlanes = 1;
+			bmi.bmiHeader.biBitCount = 32;
+			bmi.bmiHeader.biCompression = BI_RGB;
+			bmi.bmiHeader.biSizeImage = 0;
+			bmi.bmiHeader.biXPelsPerMeter = 0;
+			bmi.bmiHeader.biYPelsPerMeter = 0;
+			bmi.bmiHeader.biClrUsed = 0;
+			bmi.bmiHeader.biClrImportant = 0;
+			GetDIBits(back_dc, back_bmp, 0, client_height, 0, &bmi, DIB_RGB_COLORS);
+			SetDIBits(back_dc, back_bmp, 0, client_height, image, &bmi, DIB_RGB_COLORS);
+			BitBlt(dc, 0, 0, client_width, client_height, back_dc, 0, 0, SRCCOPY);
+
+			delete[] image;
+		}
+		if (pVideoPlayer->bStopBeforeSchedule == 1)
+			Sleep(0x3E8u);
+	}
+	pVideoPlayer->Unload();
+	/*
     if ( pVideoPlayer->uMovieFormat == 2 )
     {
       if ( pVideoPlayer->pBinkMovie )
       {
-        pVideoPlayer->BinkDrawFrame(v4, ScreenSizeFlag);
-        while ( pVideoPlayer->pBinkMovie )
-        {
-          if ( pVideoPlayer->bStopBeforeSchedule )
-            break;
-          while ( PeekMessageA(&Msg, 0, 0, 0, 1) )
-          {
-            if ( Msg.message == 18 )
-              Game_DeinitializeAndTerminate(0);
-            if ( Msg.message == 15 )
-              break;
-            TranslateMessage(&Msg);
-            DispatchMessageA(&Msg);
-          }
-          GUI_MainMenuMessageProc();
-          if ( !pVideoPlayer->pBinkMovie )
-            break;
-          if ( !BinkWait(pVideoPlayer->pBinkMovie) && !pVideoPlayer->bStopBeforeSchedule )
-            pVideoPlayer->BinkDrawFrame(v4, ScreenSizeFlag);
-        }
+        //pVideoPlayer->BinkDrawFrame(v4, ScreenSizeFlag);
+
       }
+    }
       if ( pVideoPlayer->bStopBeforeSchedule == 1 )
         Sleep(0x3E8u);
-    }
-    else
+    }*/
+	/*
+    if ( pVideoPlayer->uMovieFormat == 1 )
     {
-      if ( pVideoPlayer->uMovieFormat == 1 )
-      {
-        if ( pVideoPlayer->pSmackerMovie )
-        {
-          pVideoPlayer->SmackDrawFrame(v4, ScreenSizeFlag);
-          while ( pVideoPlayer->pSmackerMovie )
-          {
-            if ( pVideoPlayer->bStopBeforeSchedule )
-              break;
-            while ( PeekMessageW(&Msg, 0, 0, 0, 1) )
-            {
-              if (Msg.message == WM_QUIT)
-                Game_DeinitializeAndTerminate(0);
-              if (Msg.message == WM_PAINT)
-                break;
-              TranslateMessage(&Msg);
-              DispatchMessageW(&Msg);
-            }
-            GUI_MainMenuMessageProc();
-            if ( !pVideoPlayer->pSmackerMovie )
-              break;
-            if ( !SmackWait(pVideoPlayer->pSmackerMovie) && !pVideoPlayer->bStopBeforeSchedule )
-              pVideoPlayer->SmackDrawFrame(v4, ScreenSizeFlag);
-          }
-        }
-      }
+		if ( pVideoPlayer->pSmackerMovie )
+		{
+			pVideoPlayer->SmackDrawFrame(v4, ScreenSizeFlag);
+			while ( pVideoPlayer->pSmackerMovie )
+			{
+			if ( pVideoPlayer->bStopBeforeSchedule )
+				break;
+			while ( PeekMessageW(&Msg, 0, 0, 0, 1) )
+			{
+				if (Msg.message == WM_QUIT)
+				Game_DeinitializeAndTerminate(0);
+				if (Msg.message == WM_PAINT)
+				break;
+				TranslateMessage(&Msg);
+				DispatchMessageW(&Msg);
+			}
+			GUI_MainMenuMessageProc();
+			if ( !pVideoPlayer->pSmackerMovie )
+				break;
+			if ( !SmackWait(pVideoPlayer->pSmackerMovie) && !pVideoPlayer->bStopBeforeSchedule )
+				pVideoPlayer->SmackDrawFrame(v4, ScreenSizeFlag);
+			}
+		}
     }
+    */
     if (a4 == 1)
       pCurrentScreen = SCREEN_GAME;
     pVideoPlayer->bPlayingMovie = 0;
@@ -528,7 +567,7 @@
     AIL_redbook_pause(pAudioPlayer->hAILRedbook);
 
   bStopBeforeSchedule = false;
-  field_54 = 1;
+  bBufferLoaded = 1;
   pSmackerMovie = 0;
   pSmackerBuffer = 0;
   pBinkMovie = 0;
@@ -541,6 +580,7 @@
     uBinkDirectDrawSurfaceType = BinkDDSurfaceType((IDirectDrawSurface *)pRenderer->pBackBuffer4);
   else
     uBinkDirectDrawSurfaceType = BinkDDSurfaceType((IDirectDrawSurface *)pRenderer->pBackBuffer2);
+
 }
 
 
@@ -553,9 +593,9 @@
 
   if ( pBinkMovie )
   {
-    BinkPause(pBinkMovie, 1);
+    //BinkPause(pBinkMovie, 1);
     Sleep(300);
-    BinkClose(pBinkMovie);
+    //BinkClose(pBinkMovie);
     pBinkMovie = 0;
   }
 
@@ -569,8 +609,8 @@
 
   if ( pSmackerMovie )
   {
-    SmackSoundOnOff(pSmackerMovie, 0);
-    SmackClose(pSmackerMovie);
+    //SmackSoundOnOff(pSmackerMovie, 0);
+    //SmackClose(pSmackerMovie);
     pSmackerMovie = 0;
   }
   if ( pSmackerBuffer )
@@ -583,13 +623,19 @@
     SmackBlitClose(pSmackMovieBlit);
     pSmackMovieBlit = 0;
   }
-  field_54 = 0;
+  bBufferLoaded = 0;
   uMovieFormat = 0;
-  dword_0000A0 = 0;
+  uMovieFormatSwapped = 0;
   memset(pCurrentMovieName, 0, 0x40);
   if ( pAudioPlayer->hAILRedbook && !bGameoverLoop )
     AIL_redbook_resume(pAudioPlayer->hAILRedbook);
   pEventTimer->Resume();
+
+  if (mMovie)
+  {
+	  delete mMovie;
+	  mMovie = nullptr;
+  }
 }
 
 //----- (004BECD5) --------------------------------------------------------
@@ -816,7 +862,7 @@
 
 void VideoPlayer::_inlined_in_463149()
 {
-      if ( pSmackerMovie && !SmackWait(pSmackerMovie) )
+      if ( mMovie )// && !SmackWait(pSmackerMovie) )
       {
         pRenderer->BeginScene();
         pMouse->DrawCursorToTarget();
@@ -824,7 +870,7 @@
         pMouse->_469EA4();
         pRenderer->EndScene();
       }
-      else if ( pBinkMovie && !BinkWait(pBinkMovie) )
+      else if ( pBinkMovie )//&& !BinkWait(pBinkMovie) )
       {
         pRenderer->BeginScene();
         pMouse->DrawCursorToTarget();
@@ -851,19 +897,53 @@
 
   v3 = pixels;//pRenderer->pTargetSurface;
   v4 = 640;//pRenderer->uTargetSurfacePitch;
-  v5 = SmackCheckSurfaceFromat();
+  //v5 = SmackCheckSurfaceFromat();
+  
+  //SmackToBuffer(pSmackerMovie, 8, 8, 2 * v4, pRenderer->field_14, v3, v5);
 
-  SmackToBuffer(pSmackerMovie, 8, 8, 2 * v4, pRenderer->field_14, v3, v5);
-
-  
+  /*
   for (unsigned int y = 8; y < 8 + pSmackerMovie->Height; ++y)
     for (unsigned int x = 8; x < 8 + pSmackerMovie->Width; ++x)
     {
       pRenderer->WritePixel16(x, y, pixels[x + y * 640]);
     }
   //delete [] pixels;
+  */
+  //v6 = pSmackerMovie;
+//	int i = 0;
+//	while (true)
+	//{
 
-  //v6 = pSmackerMovie;
+  if (pVideoPlayer->mMovie->Stopped())
+  {
+	  //delete pVideoPlayer->mMovie;
+	  //pVideoPlayer->mMovie = nullptr;
+	  //Unload();//break;
+	  pVideoPlayer->mPlayer->Rewind();
+  }
+  else
+  {
+
+	  auto image = pVideoPlayer->mPlayer->DoFrame();
+
+	  if (image)
+	  {
+		  memcpy(pRenderer->pTargetSurface, image, 480*480*4);
+		  /*for (unsigned int y = 8; y < 8 + 360; ++y)
+		  for (unsigned int x = 8; x < 8 + 480; ++x)
+		  {
+			  		pRenderer->WritePixel16(x, y, image[y + x * 360]);
+
+		  }*/
+		  delete[] image;
+	  }
+  }
+	
+	//delete pVideoPlayer->mMovie;
+	//pVideoPlayer->mMovie = nullptr;
+	//pVideoPlayer->Unload();
+
+	/*
   if ( pSmackerMovie->NewPalette )
   {
     SmackBufferNewPalette(pSmackerBuffer, pSmackerMovie->Palette, (pSmackerMovie->PalType));
@@ -879,6 +959,8 @@
     SmackNextFrame(pSmackerMovie);
   else
     Unload();
+  */
+  
   pRenderer->EndScene();
 }
 
@@ -952,17 +1034,75 @@
 //----- (004BF28F) --------------------------------------------------------
 void VideoPlayer::OpenHouseMovie(const char *pMovieName, unsigned int a3_1)
 {
-  if ( !this->field_54 )
+  if (!this->bBufferLoaded)
   {
-    Prepare();
+    //Prepare();
+	  pEventTimer->Pause();
+	  if (pAudioPlayer->hAILRedbook)
+		  AIL_redbook_pause(pAudioPlayer->hAILRedbook);
+
+	  bStopBeforeSchedule = false;
+	  bBufferLoaded = 1;
+	  pSmackerMovie = 0;
+	  pSmackerBuffer = 0;
+	  pBinkMovie = 0;
+	  pBinkBuffer = 0;
+	  bPlayingMovie = false;
+	  bFirstFrame = false;
+	  bUsingSmackerMMX = SmackUseMMX(1);
+
     this->bLoopPlaying = a3_1;
     if ( LOBYTE(this->field_104) == 1 )
     {
       MessageBoxA(nullptr, "Unsupported Bink playback!", "E:\\WORK\\MSDEV\\MM7\\MM7\\Code\\Video.cpp:925", 0);
       return;
     }
-    sprintfex(Str2, "%s.smk", pMovieName);
-    this->pSmackerMovie = OpenSmack(Str2);
+
+	sprintf(Str2, "%s.smk", pMovieName);
+	void *src = nullptr;
+	unsigned int *offset = nullptr;
+	for (uint i = 0; i < uNumMightVideoHeaders; ++i)
+	{
+		if (!_stricmp(Str2, pMightVideoHeaders[i].pVideoName))
+		{
+			src = (void *)hMightVid;
+			offset = &pMightVideoHeaders[i].uFileOffset;
+			uVidFile = 0;
+		}
+	}
+	for (uint i = 0; i < uNumMagicVideoHeaders; ++i)
+	{
+		if (!_stricmp(Str2, pMagicVideoHeaders[i].pVideoName))
+		{
+			src = (void *)hMagicVid;
+			offset = &pMagicVideoHeaders[i].uFileOffset;
+			uVidFile = 1;
+		}
+	}
+	if (!src)
+	{
+		Unload();
+		MessageBoxA(0, "VideoPlayer error", "VideoPlayer Error", 0);
+		return;
+	}
+
+	SetFilePointer(src, *offset, 0, FILE_BEGIN);
+	strcpy(this->pCurrentMovieName, pMovieName);
+	this->uMovieFormat = 1;
+	this->uMovieFormatSwapped = 2;
+	uSize = *(offset + sizeof(MovieHeader)) - *offset;
+	
+
+	auto hwnd = pVideoPlayer->window->GetApiHandle();
+	RECT rc_client;
+	GetClientRect(hwnd, &rc_client);
+	int client_width = rc_client.right - rc_client.left,
+		client_height = rc_client.bottom - rc_client.top;
+
+	pVideoPlayer->mMovie = pVideoPlayer->mPlayer->LoadMovie("dummyFilename", client_width, client_height);
+	
+	/*
+	this->pSmackerMovie = OpenSmack(Str2);
     if ( !this->pSmackerMovie )
     {
       this->Unload();
@@ -970,10 +1110,12 @@
       MessageBoxA(nullptr, pTmpBuf.data(), "E:\\WORK\\MSDEV\\MM7\\MM7\\Code\\Video.cpp:937", 0);
       return;
     }
-    this->uMovieFormat = 1;
-    strcpy(this->pCurrentMovieName, (const char *)pMovieName);
-    this->dword_0000A0 = 1;
-    this->pSmackerBuffer = (_SMACKBUF *)SmackBufferOpen(window->GetApiHandle(), 4, LOWORD(this->pSmackerMovie->Width), LOWORD(this->pSmackerMovie->Height), LOWORD(this->pSmackerMovie->Width), LOWORD(this->pSmackerMovie->Height));
+    */
+ 
+
+
+	/*
+	this->pSmackerBuffer = (_SMACKBUF *)SmackBufferOpen(window->GetApiHandle(), 4, LOWORD(this->pSmackerMovie->Width), LOWORD(this->pSmackerMovie->Height), LOWORD(this->pSmackerMovie->Width), LOWORD(this->pSmackerMovie->Height));
     if ( this->pSmackerBuffer )
     {
       pRenderer->BeginScene();
@@ -981,6 +1123,8 @@
       pRenderer->EndScene();
     }
     SmackVolumePan(this->pSmackerMovie, 1040384, (signed __int64)(pSoundVolumeLevels[(char)uSoundVolumeMultiplier] * 32767.0), 32767);
+	*/
+
   }
 }
 
@@ -994,12 +1138,86 @@
 void VideoPlayer::OpenGlobalMovie(const char *pFilename, unsigned int bLoop, int a4)
 {
   unsigned int v10; // eax@11
-  char pVideoName[120]; // [sp+Ch] [bp-28h]@2
+  char pVideoNameBik[120]; // [sp+Ch] [bp-28h]@2
+  char pVideoNameSmk[120]; // [sp+Ch] [bp-28h]@2
+
+  if (!this->bBufferLoaded)
+  {
+	//Prepare();
+    pEventTimer->Pause();
+	if (pAudioPlayer->hAILRedbook)
+		AIL_redbook_pause(pAudioPlayer->hAILRedbook);
 
-  if ( !this->field_54 )
-  {
-    Prepare();
-    this->bLoopPlaying = bLoop;
+	bStopBeforeSchedule = false;
+	bBufferLoaded = 1;
+	pSmackerMovie = 0;
+	pSmackerBuffer = 0;
+	pBinkMovie = 0;
+	pBinkBuffer = 0;
+	bPlayingMovie = false;
+	bFirstFrame = false;
+	bUsingSmackerMMX = SmackUseMMX(1);
+	this->bLoopPlaying = bLoop;
+	
+	sprintf(pVideoNameBik, "%s.bik", pFilename);
+	sprintf(pVideoNameSmk, "%s.smk", pFilename);
+	void *src = nullptr;
+	unsigned int *offset = nullptr;
+	for (uint i = 0; i < uNumMightVideoHeaders; ++i)
+	{
+		//no BIK in MightVid
+		/*
+		if (!_stricmp(pVideoNameBik, pMightVideoHeaders[i].pVideoName))
+		{
+			src = (void *)hMightVid;
+			offset = pMightVideoHeaders[i].uFileOffset;
+			uVidFile = 1;
+			this->uMovieFormat = 2;
+			this->uMovieFormatSwapped = 1;
+		}
+		*/
+		if (!_stricmp(pVideoNameSmk, pMightVideoHeaders[i].pVideoName))
+		{
+			src = (void *)hMightVid;
+			offset = &pMightVideoHeaders[i].uFileOffset;
+			uVidFile = 0;
+			this->uMovieFormat = 1;
+			this->uMovieFormatSwapped = 2;
+		}
+	}	
+	for (uint i = 0; i < uNumMagicVideoHeaders; ++i)
+	{
+		if (!_stricmp(pVideoNameBik, pMagicVideoHeaders[i].pVideoName))
+		{
+			src = (void *)hMagicVid;
+			offset = &pMagicVideoHeaders[i].uFileOffset;
+			uVidFile = 1;
+			this->uMovieFormat = 2;
+			this->uMovieFormatSwapped = 1;
+		}
+		if (!_stricmp(pVideoNameSmk, pMagicVideoHeaders[i].pVideoName))
+		{
+			src = (void *)hMagicVid;
+			offset = &pMagicVideoHeaders[i].uFileOffset;
+			uVidFile = 1;
+			this->uMovieFormat = 1;
+			this->uMovieFormatSwapped = 2;
+		}
+	}
+	if (!src)
+	{
+		Unload();
+		MessageBoxA(0, "VideoPlayer error", "VideoPlayer Error", 0);
+		return;
+	}
+
+	SetFilePointer(src, *offset, 0, FILE_BEGIN);
+	strcpy(this->pCurrentMovieName, pFilename);
+	uSize = *(offset+sizeof(MovieHeader))-*offset;
+	return;
+
+
+	/*
     sprintf(pVideoName, "%s.bik", pFilename);
     this->pBinkMovie = OpenBink(pVideoName);
     if ( this->pBinkMovie )
@@ -1007,7 +1225,7 @@
       this->uMovieFormat = 2;
       strcpy(this->pCurrentMovieName, pFilename);
       pBinkMovie = this->pBinkMovie;
-      this->dword_0000A0 = 1;
+      this->uMovieFormatSwapped = 1;
       if ( pBinkMovie )
         this->pBinkBuffer = CreateBinkBuffer(pBinkMovie->uWidth, pBinkMovie->uHeight, 0);
     }
@@ -1025,7 +1243,7 @@
       }
       this->uMovieFormat = 1;
       strcpy(this->pCurrentMovieName, pFilename);
-      this->dword_0000A0 = 2;
+      this->uMovieFormatSwapped = 2;
       this->pSomeSmackerBuffer = (char *)malloc(this->pSmackerMovie->Width * this->pSmackerMovie->Height);
       if ( !this->pSomeSmackerBuffer )
       {
@@ -1054,8 +1272,8 @@
         sprintf(pVideoName, "Failed to open Blit API");
         MessageBoxA(0, pVideoName, "Smacker Error", 0);
         return;
-      }
-    }
+      }	  
+    }*/
   }
 }
 
@@ -1107,9 +1325,9 @@
 
   strcpy(Source, this->pCurrentMovieName);
   Unload();
-  if ( this->dword_0000A0 == 1 )
+  if ( this->uMovieFormatSwapped == 1 )
     OpenHouseMovie(Source, LOBYTE(this->bLoopPlaying));
-  else if ( this->dword_0000A0 == 2 )
+  else if ( this->uMovieFormatSwapped == 2 )
     OpenGlobalMovie(Source, LOBYTE(this->bLoopPlaying), 1);
   else
     __debugbreak();
@@ -1126,7 +1344,7 @@
 
 //----- (004BE6F5) --------------------------------------------------------
 VideoPlayer::VideoPlayer() :
-  field_54(0),
+bBufferLoaded(0),
   pBinkMovie(nullptr), pBinkBuffer(nullptr),
   pSmackerMovie(nullptr), pSmackerBuffer(nullptr), pSmackMovieBlit(nullptr)
 {
@@ -1134,6 +1352,9 @@
   bStopBeforeSchedule = false;
   pResetflag = 0;
   byte_F8BC0C = 0;
+  mPlayer = new MultimediaPlayer();
+  mPlayer->Initialize();
+  mMovie = nullptr;
   //pBinkMovie = nullptr;
 }
 
@@ -1147,3 +1368,30 @@
 	DWORD w;
 	WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), msg, lstrlenA(msg), &w, nullptr);
 }
+
+int readFunction(void* opaque, uint8_t* buf, int buf_size)
+{
+	HANDLE stream = (HANDLE)opaque;
+	//int numBytes = stream->read((char*)buf, buf_size);
+	int numBytes;
+	ReadFile(stream, (char *)buf, buf_size, (LPDWORD)&numBytes, NULL);
+	return numBytes;
+}
+
+int64_t seekFunction(void* opaque, int64_t offset, int whence)
+{
+	if (whence == AVSEEK_SIZE)
+		return pVideoPlayer->uSize;// -1; // I don't know "size of my handle in bytes"
+	HANDLE stream = (HANDLE)opaque;
+	/*if (stream->isSequential())
+	return -1; // cannot seek a sequential stream
+	if (!stream->seek(offset))
+	return -1;
+	return stream->pos();*/
+	LARGE_INTEGER li,li2;
+	PLARGE_INTEGER pli = &li2;
+	li.QuadPart = offset;
+	SetFilePointerEx(stream, li, pli, FILE_BEGIN);
+	return pli->QuadPart;
+	return SetFilePointer(stream, LODWORD(offset), (PLONG)(&offset)+1, FILE_BEGIN);  
+}
--- a/VideoPlayer.h	Tue Dec 31 23:03:01 2013 +0600
+++ b/VideoPlayer.h	Wed Jan 01 13:43:09 2014 +0000
@@ -5,6 +5,7 @@
 
 
 
+
 #pragma pack(push, 1)
 
 
@@ -318,7 +319,7 @@
 		return false;
 	}
 };
-typedef OpenALSoundProviderGeneric<16> OpenALSoundProvider;
+typedef OpenALSoundProviderGeneric<64> OpenALSoundProvider;
 
 
 
@@ -513,6 +514,9 @@
 	}
 };
 
+int readFunction(void* opaque, uint8_t* buf, int buf_size);
+
+int64_t seekFunction(void* opaque, int64_t offset, int whence);
 
 template<int NUM_PRECACHED_FRAMES>
 class MovieCached
@@ -543,7 +547,37 @@
 	{
 		this->width = width;
 		this->height = height;
+		
+/*		for (uint i = 0; i < pVideoPlayer->uNumMagicVideoHeaders; ++i)
+		if (!_stricmp(video_filename, pVideoPlayer->pMagicVideoHeaders[i].pVideoName))
+		{
+			SetFilePointer(pVideoPlayer->hMagicVid, pVideoPlayer->pMagicVideoHeaders[i].uFileOffset, 0, FILE_BEGIN);
+			break;
+		}
+*/		packet = nullptr;
+		int ioBufferSize = 32768;
+		void *src;
+		
+		unsigned char * ioBuffer = (unsigned char *)av_malloc(ioBufferSize + FF_INPUT_BUFFER_PADDING_SIZE); // can get av_free()ed by libav
+		if (pVideoPlayer->uVidFile)
+		{
+			src = (void*)pVideoPlayer->hMagicVid;
+		}
+		else
+		{
+			src = (void*)pVideoPlayer->hMightVid;
+		}
 
+		avioContext = avio_alloc_context(ioBuffer, ioBufferSize, 0, src, &readFunction, NULL, &seekFunction);
+		//offset = SetFilePointer(pVideoPlayer->hMagicVid,0,0,FILE_CURRENT);
+		LARGE_INTEGER li, li2;
+		PLARGE_INTEGER pli = &li2;
+		li.QuadPart = 0;
+		SetFilePointerEx(pVideoPlayer->hMagicVid, li, pli, FILE_CURRENT);
+		offset = pli->QuadPart;
+		
+		format_ctx = avformat_alloc_context();
+		format_ctx->pb = avioContext;
 		if (avformat_open_input(&format_ctx, video_filename, nullptr, nullptr) >= 0)
 		{
 			if (avformat_find_stream_info(format_ctx, nullptr) >= 0)
@@ -623,6 +657,26 @@
 		return -1;
 	}
 
+	void Rewind()
+	{
+		//auto i=avio_seek(avioContext, pVideoPlayer->uOffset, 0);
+		//auto i = av_seek_frame(format_ctx, -1, 0, AVSEEK_FLAG_BYTE);
+		//i = av_seek_frame(format_ctx, audio_stream_idx, 0, AVSEEK_FLAG_BYTE);
+		
+		int64_t timestamp = 9;
+		int ret, i;
+		if (format_ctx->start_time != AV_NOPTS_VALUE)
+			timestamp += format_ctx->start_time;
+		ret = av_seek_frame(format_ctx, -1, format_ctx->start_time, AVSEEK_FLAG_BYTE);
+		if (ret < 0) {
+//			av_log(ctx, AV_LOG_ERROR, "Unable to loop: %s\n", av_err2str(ret));
+			//loop_count = 1; /* do not try again */
+			return;// ret;
+		}
+		
+
+		stopped = false;
+	}
 
 	MultimediaFrame::Ptr GetNextFrame()
 	{
@@ -668,6 +722,8 @@
 	AVStream        *audio_stream;
 	AVCodec         *audio_stream_dec;
 	AVCodecContext  *audio_stream_dec_ctx;
+	AVIOContext * avioContext;
+	int64_t offset;
 };
 typedef MovieCached<10> Movie;
 
@@ -715,6 +771,10 @@
 		return nullptr;
 	}
 
+	inline void Rewind()
+	{
+		current_movie->Rewind();
+	}
 
 	inline char *DoFrame()
 	{
@@ -729,6 +789,7 @@
 
 			if (frame->Type() == AVMEDIA_TYPE_AUDIO)
 			{
+				//continue;
 				uint8_t *data;
 				if (frame->Decode() >= 0)
 				{
@@ -819,7 +880,7 @@
   unsigned int uNumMightVideoHeaders;
   unsigned int uNumMagicVideoHeaders;
   int uBinkDirectDrawSurfaceType;
-  int field_54;
+  int bBufferLoaded;
   unsigned int bPlayingMovie;
   unsigned int bFirstFrame;
   unsigned int bUsingSmackerMMX;
@@ -835,10 +896,15 @@
   _BINKBUF *pBinkBuffer;
   char field_88[20];
   unsigned int uMovieFormat;
-  int dword_0000A0;
+  int uMovieFormatSwapped;
   char pCurrentMovieName[64];
   char pVideoFrameTextureFilename[32];
   int field_104;
+  MultimediaPlayer *mPlayer;
+  Movie *mMovie;
+  int uVidFile;
+  int uSize;
+  int uOffset;
 };
 #pragma pack(pop)
 
@@ -847,3 +913,4 @@
 
 
 extern VideoPlayer *pVideoPlayer;
+extern LRESULT __stdcall wWinProc(HWND hwnd, unsigned int msg, WPARAM wparam, LPARAM lparam);
\ No newline at end of file
--- a/mm7_2.cpp	Tue Dec 31 23:03:01 2013 +0600
+++ b/mm7_2.cpp	Wed Jan 01 13:43:09 2014 +0000
@@ -4106,7 +4106,7 @@
   static_assert(sizeof(AudioPlayer) == 0xC84, "Wrong type size");
   static_assert(sizeof(SoundDesc) == 0x78, "Wrong type size");
   static_assert(sizeof(stru339_spell_sound) == 0xAFD8, "Wrong type size");
-  static_assert(sizeof(VideoPlayer) == 0x108 + 4, "Wrong type size");
+  //static_assert(sizeof(VideoPlayer) == 0x108 + 4, "Wrong type size");
   static_assert(sizeof(MovieHeader) == 0x2C, "Wrong type size");
   static_assert(sizeof(DecorationDesc) == 0x54, "Wrong type size");
   static_assert(sizeof(ObjectDesc) == 0x38, "Wrong type size");
--- a/mm7_data.h	Tue Dec 31 23:03:01 2013 +0600
+++ b/mm7_data.h	Wed Jan 01 13:43:09 2014 +0000
@@ -838,7 +838,6 @@
 //extern HINSTANCE hInstance; // idb
 //extern char *pCmdLine;
 //extern HWND hWnd; // idb
-extern class OSWindow *window;
 extern char pCurrentMapName[32]; // idb
 extern unsigned int uLevelMapStatsID;