Index: ext/ffmpeg/gstffmpeg.c
===================================================================
RCS file: /cvs/gstreamer/gst-ffmpeg/ext/ffmpeg/gstffmpeg.c,v
retrieving revision 1.27
diff -u -r1.27 gstffmpeg.c
--- ext/ffmpeg/gstffmpeg.c	13 Sep 2005 12:16:32 -0000	1.27
+++ ext/ffmpeg/gstffmpeg.c	23 Nov 2005 19:23:50 -0000
@@ -72,6 +72,8 @@
 gboolean _shut_up_I_am_probing = FALSE;
 #endif
 
+GMutex *libavcodec_mutex;
+
 static gboolean
 plugin_init (GstPlugin * plugin)
 {
@@ -82,6 +84,7 @@
 #endif
   av_register_all ();
 
+  libavcodec_mutex = g_mutex_new ();
   gst_ffmpegenc_register (plugin);
   gst_ffmpegdec_register (plugin);
 #if 0
Index: ext/ffmpeg/gstffmpeg.h
===================================================================
RCS file: /cvs/gstreamer/gst-ffmpeg/ext/ffmpeg/gstffmpeg.h,v
retrieving revision 1.4
diff -u -r1.4 gstffmpeg.h
--- ext/ffmpeg/gstffmpeg.h	13 Sep 2005 12:16:32 -0000	1.4
+++ ext/ffmpeg/gstffmpeg.h	23 Nov 2005 19:23:50 -0000
@@ -43,6 +43,7 @@
 extern gboolean _shut_up_I_am_probing;
 #endif
 
+extern GMutex *libavcodec_mutex;
 extern gboolean gst_ffmpegdemux_register (GstPlugin * plugin);
 extern gboolean gst_ffmpegdec_register (GstPlugin * plugin);
 extern gboolean gst_ffmpegenc_register (GstPlugin * plugin);
Index: ext/ffmpeg/gstffmpegdec.c
===================================================================
RCS file: /cvs/gstreamer/gst-ffmpeg/ext/ffmpeg/gstffmpegdec.c,v
retrieving revision 1.128
diff -u -r1.128 gstffmpegdec.c
--- ext/ffmpeg/gstffmpegdec.c	23 Nov 2005 14:53:06 -0000	1.128
+++ ext/ffmpeg/gstffmpegdec.c	23 Nov 2005 19:23:51 -0000
@@ -382,7 +382,11 @@
   }
 
   if (ffmpegdec->context->priv_data)
+  {
+    g_mutex_lock (libavcodec_mutex);
     avcodec_close (ffmpegdec->context);
+    g_mutex_unlock (libavcodec_mutex);
+  }
   ffmpegdec->opened = FALSE;
 
   if (ffmpegdec->context->palctrl) {
@@ -414,7 +418,12 @@
   GstFFMpegDecClass *oclass =
       (GstFFMpegDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
 
-  if (avcodec_open (ffmpegdec->context, oclass->in_plugin) < 0)
+  gint result;
+   
+  g_mutex_lock (libavcodec_mutex);
+  result = avcodec_open (ffmpegdec->context, oclass->in_plugin);
+  g_mutex_unlock (libavcodec_mutex);
+  if (result < 0)
     goto could_not_open;
 
   ffmpegdec->opened = TRUE;
Index: ext/ffmpeg/gstffmpegenc.c
===================================================================
RCS file: /cvs/gstreamer/gst-ffmpeg/ext/ffmpeg/gstffmpegenc.c,v
retrieving revision 1.78
diff -u -r1.78 gstffmpegenc.c
--- ext/ffmpeg/gstffmpegenc.c	23 Oct 2005 16:07:46 -0000	1.78
+++ ext/ffmpeg/gstffmpegenc.c	23 Nov 2005 19:23:51 -0000
@@ -276,7 +276,9 @@
 
   /* close old session */
   if (ffmpegenc->opened) {
+    g_mutex_lock (libavcodec_mutex);
     avcodec_close (ffmpegenc->context);
+    g_mutex_unlock (libavcodec_mutex);
     ffmpegenc->opened = FALSE;
   }
 
@@ -330,18 +332,28 @@
     * log handler */
   _shut_up_I_am_probing = TRUE;
   for (pixfmt = 0; pixfmt < PIX_FMT_NB; pixfmt++) {
+    gint result;
     ctx->pix_fmt = pixfmt;
-    if (avcodec_open (ctx, oclass->in_plugin) >= 0 &&
+    g_mutex_lock (libavcodec_mutex);
+    result = avcodec_open (ctx, oclass->in_plugin);
+    g_mutex_unlock (libavcodec_mutex);
+    if (result >= 0 &&
         ctx->pix_fmt == pixfmt) {
       ctx->width = -1;
       if (!caps)
         caps = gst_caps_new_empty ();
       gst_caps_append (caps,
           gst_ffmpeg_codectype_to_caps (oclass->in_plugin->type, ctx));
+      g_mutex_lock (libavcodec_mutex);
       avcodec_close (ctx);
+      g_mutex_unlock (libavcodec_mutex);
     }
     if (ctx->priv_data)
+    {
+      g_mutex_lock (libavcodec_mutex);
       avcodec_close (ctx);
+      g_mutex_unlock (libavcodec_mutex);
+    }
   }
   av_free (ctx);
   _shut_up_I_am_probing = FALSE;
@@ -359,6 +371,7 @@
 static gboolean
 gst_ffmpegenc_setcaps (GstPad * pad, GstCaps * caps)
 {
+  gint result;
   GstCaps *other_caps;
   GstCaps *allowed_caps;
   GstCaps *icaps;
@@ -369,7 +382,9 @@
 
   /* close old session */
   if (ffmpegenc->opened) {
+    g_mutex_lock (libavcodec_mutex);
     avcodec_close (ffmpegenc->context);
+    g_mutex_unlock (libavcodec_mutex);
     ffmpegenc->opened = FALSE;
   }
 
@@ -401,9 +416,16 @@
   pix_fmt = ffmpegenc->context->pix_fmt;
 
   /* open codec */
-  if (avcodec_open (ffmpegenc->context, oclass->in_plugin) < 0) {
+  g_mutex_lock (libavcodec_mutex);
+  result = avcodec_open (ffmpegenc->context, oclass->in_plugin);
+  g_mutex_unlock (libavcodec_mutex);
+  if (result < 0) {
     if (ffmpegenc->context->priv_data)
+    {
+      g_mutex_lock (libavcodec_mutex);
       avcodec_close (ffmpegenc->context);
+      g_mutex_unlock (libavcodec_mutex);
+    }
     GST_DEBUG ("ffenc_%s: Failed to open FFMPEG codec",
         oclass->in_plugin->name);
     return FALSE;
@@ -411,7 +433,9 @@
 
   /* is the colourspace correct? */
   if (pix_fmt != ffmpegenc->context->pix_fmt) {
+    g_mutex_lock (libavcodec_mutex);
     avcodec_close (ffmpegenc->context);
+    g_mutex_unlock (libavcodec_mutex);
     GST_DEBUG ("ffenc_%s: AV wants different colourspace (%d given, %d wanted)",
         oclass->in_plugin->name, pix_fmt, ffmpegenc->context->pix_fmt);
     return FALSE;
@@ -436,7 +460,9 @@
       ffmpegenc->context, TRUE);
 
   if (!other_caps) {
+    g_mutex_lock (libavcodec_mutex);
     avcodec_close (ffmpegenc->context);
+    g_mutex_unlock (libavcodec_mutex);
     GST_DEBUG ("Unsupported codec - no caps found");
     return FALSE;
   }
@@ -460,7 +486,9 @@
   }
 
   if (!gst_pad_set_caps (ffmpegenc->srcpad, icaps)) {
+    g_mutex_lock (libavcodec_mutex); 
     avcodec_close (ffmpegenc->context);
+    g_mutex_unlock (libavcodec_mutex);
     gst_caps_unref (icaps);
     return FALSE;
   }
@@ -689,7 +717,9 @@
   switch (transition) {
     case GST_STATE_CHANGE_PAUSED_TO_READY:
       if (ffmpegenc->opened) {
+        g_mutex_lock (libavcodec_mutex);
         avcodec_close (ffmpegenc->context);
+        g_mutex_unlock (libavcodec_mutex);
         ffmpegenc->opened = FALSE;
       }
       if (ffmpegenc->cache) {
