瀏覽代碼

pull _convert_ctx out of loop

David Rose 14 年之前
父節點
當前提交
67dc3fabfa
共有 2 個文件被更改,包括 38 次插入21 次删除
  1. 35 21
      panda/src/movies/ffmpegVideoCursor.cxx
  2. 3 0
      panda/src/movies/ffmpegVideoCursor.h

+ 35 - 21
panda/src/movies/ffmpegVideoCursor.cxx

@@ -52,6 +52,7 @@ FfmpegVideoCursor() :
   _packet1(NULL),
   _format_ctx(NULL),
   _video_ctx(NULL),
+  _convert_ctx(NULL),
   _video_index(-1),
   _frame(NULL),
   _frame_out(NULL),
@@ -133,14 +134,20 @@ init_from(FfmpegVideo *source) {
       cleanup();
       return;
     }
-  }
 
-  _size_x = _video_ctx->width;
-  _size_y = _video_ctx->height;
-  _num_components = 3; // Don't know how to implement RGBA movies yet.
-  _length = (_format_ctx->duration * 1.0) / AV_TIME_BASE;
-  _can_seek = true;
-  _can_seek_fast = true;
+    _size_x = _video_ctx->width;
+    _size_y = _video_ctx->height;
+    _num_components = 3; // Don't know how to implement RGBA movies yet.
+    _length = (_format_ctx->duration * 1.0) / AV_TIME_BASE;
+    _can_seek = true;
+    _can_seek_fast = true;
+
+#ifdef HAVE_SWSCALE
+    _convert_ctx = sws_getContext(_size_x, _size_y,
+                                  _video_ctx->pix_fmt, _size_x, _size_y,
+                                  PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
+#endif  // HAVE_SWSCALE
+  }
 
   _frame = avcodec_alloc_frame();
   _frame_out = avcodec_alloc_frame();
@@ -181,6 +188,7 @@ FfmpegVideoCursor(FfmpegVideo *src) :
   _packet1(NULL),
   _format_ctx(NULL),
   _video_ctx(NULL),
+  _convert_ctx(NULL),
   _video_index(-1),
   _frame(NULL),
   _frame_out(NULL),
@@ -493,15 +501,25 @@ cleanup() {
     _packet1 = NULL;
   }
 
-  if ((_video_ctx)&&(_video_ctx->codec)) {
+  {
     MutexHolder av_holder(_av_lock);
-    avcodec_close(_video_ctx);
-  }
-  _video_ctx = NULL;
-
-  if (_format_ctx) {
-    _ffvfile.close();
-    _format_ctx = NULL;
+    
+#ifdef HAVE_SWSCALE
+    if (_convert_ctx != NULL) {
+      sws_freeContext(_convert_ctx);
+    }
+    _convert_ctx = NULL;
+#endif  // HAVE_SWSCALE
+    
+    if ((_video_ctx)&&(_video_ctx->codec)) {
+      avcodec_close(_video_ctx);
+    }
+    _video_ctx = NULL;
+    
+    if (_format_ctx) {
+      _ffvfile.close();
+      _format_ctx = NULL;
+    }
   }
 
   _video_index = -1;
@@ -927,13 +945,9 @@ export_frame(MovieVideoCursor::Buffer *buffer) {
   buffer->_begin_time = _begin_time;
   buffer->_end_time = _end_time;
 #ifdef HAVE_SWSCALE
-  struct SwsContext *convert_ctx = sws_getContext(_size_x, _size_y,
-                                                  _video_ctx->pix_fmt, _size_x, _size_y,
-                                                  PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
-  nassertv(convert_ctx != NULL);
-  sws_scale(convert_ctx, _frame->data, _frame->linesize,
+  nassertv(_convert_ctx != NULL);
+  sws_scale(_convert_ctx, _frame->data, _frame->linesize,
             0, _size_y, _frame_out->data, _frame_out->linesize);
-  sws_freeContext(convert_ctx);
 #else
   img_convert((AVPicture *)_frame_out, PIX_FMT_BGR24, 
               (AVPicture *)_frame, _video_ctx->pix_fmt, _size_x, _size_y);

+ 3 - 0
panda/src/movies/ffmpegVideoCursor.h

@@ -33,6 +33,7 @@ struct AVCodecContext;
 struct AVStream;
 struct AVPacket;
 struct AVFrame;
+struct SwsContext;
 
 ////////////////////////////////////////////////////////////////////
 //       Class : FfmpegVideoCursor
@@ -116,6 +117,8 @@ private:
   double _packet_time;
   AVFormatContext *_format_ctx;
   AVCodecContext *_video_ctx;
+  SwsContext *_convert_ctx;
+
   FfmpegVirtualFile _ffvfile;
   int _video_index;
   double _video_timebase;