--- gstvaapiencoder.c	2016-11-17 12:18:13.000000000 +0200
+++ gstvaapiencoder_fixed.c	2018-06-08 11:35:40.174258759 +0300
@@ -234,13 +234,23 @@
 
   g_mutex_lock (&encoder->mutex);
   do {
+    int try_count = 0;
+
     codedbuf_proxy = gst_vaapi_coded_buffer_proxy_new_from_pool (pool);
     if (codedbuf_proxy)
       break;
 
-    /* Wait for a free coded buffer to become available */
-    g_cond_wait (&encoder->codedbuf_free, &encoder->mutex);
-    codedbuf_proxy = gst_vaapi_coded_buffer_proxy_new_from_pool (pool);
+    while (try_count < 5)
+    {
+      /* Wait for a free coded buffer to become available */
+      g_cond_wait (&encoder->codedbuf_free, &encoder->mutex);
+      codedbuf_proxy = gst_vaapi_coded_buffer_proxy_new_from_pool (pool);
+      if (codedbuf_proxy)
+      {
+          break;
+      }
+      try_count++;
+    }
   } while (0);
   g_mutex_unlock (&encoder->mutex);
   if (!codedbuf_proxy)
