diff VideoPlayer.h @ 2142:ca548138d6aa

some code cleaning in video playing, loops play but there is a memory leak
author zipi
date Thu, 02 Jan 2014 18:25:42 +0000
parents d24ee391fd1f
children 0b2eea6f80c9
line wrap: on
line diff
--- a/VideoPlayer.h	Thu Jan 02 17:57:09 2014 +0600
+++ b/VideoPlayer.h	Thu Jan 02 18:25:42 2014 +0000
@@ -522,8 +522,13 @@
 class MovieCached
 {
 public:
-	bool Stopped() const { return stopped; }
-
+	bool Stopped() { return stopped; }
+	int GetWidth() { return width; }
+	int GetHeight() { return height; }
+	inline ~MovieCached()
+	{
+		Release();
+	}
 protected:
 	friend class MultimediaPlayer;
 	inline MovieCached(OpenALSoundProvider *sound_provider)
@@ -541,6 +546,23 @@
 		this->audio_stream = nullptr;
 		this->audio_stream_dec = nullptr;
 		this->audio_stream_dec_ctx = nullptr;
+
+		packet = nullptr;
+		ioBuffer = nullptr;
+		format_ctx = nullptr;
+		avioContext = nullptr;
+	}
+
+	bool LoadFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
+	{
+		if (!ioBuffer)
+			ioBuffer = (unsigned char *)av_malloc(16384 + FF_INPUT_BUFFER_PADDING_SIZE); // can get av_free()ed by libav
+		if (!avioContext)
+			avioContext = avio_alloc_context(ioBuffer, 16384, 0, h, &readFunction, NULL, &seekFunction);
+		if (!format_ctx)
+			format_ctx = avformat_alloc_context();
+		format_ctx->pb = avioContext;
+		return Load("dummyFilename", width, height);
 	}
 
 	bool Load(const char *video_filename, int width, int height)
@@ -548,42 +570,12 @@
 		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)
 			{
 				av_dump_format(format_ctx, 0, video_filename, 0);
-
+				
 				video_stream_idx = OpenStream(AVMEDIA_TYPE_VIDEO, &video_stream, &video_stream_dec, &video_stream_dec_ctx);
 				if (video_stream_idx < 0)
 					return Release(), false;
@@ -603,11 +595,6 @@
 
 	bool Release()
 	{
-		if (format_ctx)
-		{
-			avformat_free_context(format_ctx);
-			format_ctx = nullptr;
-		}
 		if (packet)
 		{
 			av_free_packet(packet);
@@ -630,6 +617,17 @@
 			audio_stream = nullptr;
 			audio_stream_dec = nullptr;
 			avcodec_close(audio_stream_dec_ctx);
+
+		}
+		if (avioContext)
+		{
+			av_free(avioContext);
+			avioContext = nullptr;
+		}
+		if (format_ctx)
+		{
+			avformat_free_context(format_ctx);
+			format_ctx = nullptr;
 		}
 		return true;
 	}
@@ -657,27 +655,6 @@
 		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()
 	{
 		packet->data = nullptr;
@@ -722,8 +699,8 @@
 	AVStream        *audio_stream;
 	AVCodec         *audio_stream_dec;
 	AVCodecContext  *audio_stream_dec_ctx;
-	AVIOContext * avioContext;
-	int64_t offset;
+	unsigned char * ioBuffer;
+	AVIOContext *avioContext;
 };
 typedef MovieCached<10> Movie;
 
@@ -754,6 +731,21 @@
 		return true;
 	}
 
+	Movie *LoadMovieFromLOD(HANDLE h, int readFunction(void*, uint8_t*, int), int64_t seekFunction(void*, int64_t, int), int width, int height)
+	{
+		auto movie = new Movie(sound_provider);
+		if (movie)
+		{
+			if (movie->LoadFromLOD(h, readFunction, seekFunction, width, height))
+			{
+				current_movie_width = width;
+				current_movie_height = height;
+				return current_movie = movie;
+			}
+			delete movie;
+		}
+		return nullptr;
+	}
 
 	Movie *LoadMovie(const char *filename, int width, int height)
 	{
@@ -771,11 +763,6 @@
 		return nullptr;
 	}
 
-	inline void Rewind()
-	{
-		current_movie->Rewind();
-	}
-
 	inline char *DoFrame()
 	{
 		if (!current_movie)
@@ -865,13 +852,13 @@
   _BINKBUF *CreateBinkBuffer(unsigned int uWidth, unsigned int uHeight, char a4);
   void _inlined_in_463149();
 
-  static void MovieLoop(const char *pMovieName, int a2, int a3, int a4);
+  void MovieLoop(const char *pMovieName, int a2, int a3, int a4);
 
 
   RGBTexture pVideoFrame;
-  struct _SMACK *pSmackerMovie;
-  struct _SMACKBUF *pSmackerBuffer;
-  char *pSomeSmackerBuffer;
+  //struct _SMACK *pSmackerMovie;
+  //struct _SMACKBUF *pSmackerBuffer;
+  //char *pSomeSmackerBuffer;
   int field_34;
   MovieHeader *pMightVideoHeaders;
   MovieHeader *pMagicVideoHeaders;
@@ -892,19 +879,23 @@
   struct _SMACKBLIT *pSmackMovieBlit;
   HANDLE hMightVid;
   HANDLE hMagicVid;
-  _BINK *pBinkMovie;
-  _BINKBUF *pBinkBuffer;
+  //_BINK *pBinkMovie;
+  //_BINKBUF *pBinkBuffer;
   char field_88[20];
   unsigned int uMovieFormat;
   int uMovieFormatSwapped;
   char pCurrentMovieName[64];
   char pVideoFrameTextureFilename[32];
   int field_104;
-  MultimediaPlayer *mPlayer;
-  Movie *mMovie;
-  int uVidFile;
+  MultimediaPlayer *pPlayer;
+  Movie *pMovie;
+  HANDLE hVidFile;
   int uSize;
   int uOffset;
+  void UpdatePalette();
+  static int readFunction(void *, uint8_t *, int);
+  static int64_t seekFunction(void *, int64_t, int);
+  void LoadMovie(const char *);
 };
 #pragma pack(pop)