changeset 2361:d6bba1e6249d

for eliminate memory flow in video
author Ritor1
date Sat, 19 Apr 2014 23:56:01 +0600
parents 63ad4e3606f4
children 7966fc2949a7 43f29cb2e6ed 14c05b876218
files MediaPlayer.cpp
diffstat 1 files changed, 17 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/MediaPlayer.cpp	Wed Apr 16 17:36:47 2014 +0600
+++ b/MediaPlayer.cpp	Sat Apr 19 23:56:01 2014 +0600
@@ -643,6 +643,8 @@
 
   //Освободить память выделенную для фрейма
   avcodec_free_frame(&frame);
+  delete frame;
+  av_free_packet(packet);
   delete packet;
 
   return true;
@@ -756,16 +758,6 @@
 class Movie: public Media::IMovie
 {
   public:
-	int              audio_stream_idx;
-	AVStream        *audio_stream;//содержат информацию о аудио потоке
-	AVCodec         *audio_stream_dec;
-	AVCodecContext  *audio_stream_dec_ctx;
-
-	int              video_stream_idx;
-	AVStream        *video_stream;//содержат информацию о видео потоке
-	AVCodec         *video_stream_dec;
-	AVCodecContext  *video_stream_dec_ctx;
-
     inline Movie()
     {
       this->movie_filename[0] = 0;
@@ -782,7 +774,7 @@
       memset(last_resampled_frame_data, 0, sizeof(last_resampled_frame_data));
       memset(last_resampled_frame_linesize, 0, sizeof(last_resampled_frame_linesize));
 
-		decoding_packet = nullptr;
+      decoding_packet = nullptr;
 		ioBuffer = nullptr;
 		format_ctx = nullptr;
 		avioContext = nullptr;
@@ -797,6 +789,7 @@
     {
       audio.Release();
       video.Release();
+
       if (format_ctx)
       {
 		// Close the video file
@@ -811,10 +804,15 @@
 	  }
 	  if (ioBuffer)
 	  {
-		av_free(ioBuffer);
+		//av_free(ioBuffer);
 		ioBuffer = nullptr;
       }
+	  av_free_packet(decoding_packet);
 	  delete decoding_packet;
+      avcodec_free_frame(&decoding_frame);
+	  delete decoding_frame;
+      if (last_resampled_frame_data[0])
+        av_freep(&last_resampled_frame_data[0]);
     }
 
     bool Load(const wchar_t *filename, int dst_width, int dst_height, int cache_ms)	//Загрузка
@@ -928,18 +926,13 @@
         {
           //Now seek back to the beginning of the stream
           if (video.dec_ctx->frame_number >= video.stream->duration - 1 )
-		  {
-          //  if (av_seek_frame(format_ctx, video.stream_idx, format_ctx->start_time, AVSEEK_FLAG_BACKWARD) < 0)
-          //    fprintf(stderr, "%s: error while seeking\n", format_ctx->filename);		  
-		  //}
-          end_current_file = true;
-		 // return;
-		  }
+            end_current_file = true;
         }
         if (av_read_frame(format_ctx, avpacket) < 0) //воспроизведение завершено
         {
           // probably movie is finished
           end_current_file = true;
+          av_free_packet(avpacket);
 		  return;
         }
 		// Is this a packet from the video stream?
@@ -980,6 +973,7 @@
         AVPixelFormat  rescaled_format = AV_PIX_FMT_RGB32;
         uint8_t       *rescaled_data[4] = {nullptr, nullptr, nullptr, nullptr};
         int            rescaled_linesize[4] = {0, 0, 0, 0};
+
         if (av_image_alloc(rescaled_data, rescaled_linesize, current_movie_width, current_movie_height, rescaled_format, 1) >= 0)
         {
           // создание контекста для преобразования
@@ -996,10 +990,14 @@
           last_resampled_frame_num = desired_frame_number;
           memcpy(last_resampled_frame_data, rescaled_data, sizeof(rescaled_data));
           memcpy(last_resampled_frame_linesize, rescaled_linesize, sizeof(rescaled_linesize));
+          //вроде должен быть av_freep(&rescaled_data[0]);
         }
       }
       else
         memset(dst_surface, 0, width * current_movie_height * 4);
+
+      // Free the packet that was allocated by av_read_frame
+      av_free_packet(avpacket);
     }
 
     virtual void Play()