]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blobdiff - ext/x264/gstx264enc.c
x264enc: remove leaking "optimization"
[glsdk/gst-plugins-ugly0-10.git] / ext / x264 / gstx264enc.c
index 70e398194cfe311d3338d64f7c8b1e9a1458c336..2be3dd3b9ca7d5646b3b5f23968763be9ebd81cf 100644 (file)
  * of the encoding.  This will similarly be the case if this target bitrate
  * is to obtained in multiple (2 or 3) pass encoding.
  * Alternatively, one may choose to perform Constant Quantizer or Quality encoding,
- * in which case the #GstX264Enc:quantizer property controls much of the outcome.
+ * in which case the #GstX264Enc:quantizer property controls much of the outcome, in that case #GstX264Enc:bitrate is the maximum bitrate.
  *
  * The H264 profile that is eventually used depends on a few settings.
  * If #GstX264Enc:dct8x8 is enabled, then High profile is used.
  * Otherwise, if #GstX264Enc:cabac entropy coding is enabled or #GstX264Enc:bframes
  * are allowed, then Main Profile is in effect, and otherwise Baseline profile
- * applies.  No profile is imposed by default, which is fine for most software
- * players and settings, but in some cases (e.g. hardware platforms) a more
- * restricted profile/level may be necessary.
+ * applies.  The main profile is imposed by default,
+ * which is fine for most software players and settings,
+ * but in some cases (e.g. hardware platforms) a more restricted profile/level
+ * may be necessary. The recommended way to set a profile is to set it in the
+ * downstream caps.
  *
  * If a preset/tuning are specified then these will define the default values and
  * the property defaults will be ignored. After this the option-string property is
  * applied, followed by the user-set properties, fast first pass restrictions and
  * finally the profile restrictions.
  *
+ * <note>Some settings, including the default settings, may lead to quite
+ * some latency (i.e. frame buffering) in the encoder. This may cause problems
+ * with pipeline stalling in non-trivial pipelines, because the encoder latency
+ * is often considerably higher than the default size of a simple queue
+ * element. Such problems are caused by one of the queues in the other
+ * non-x264enc streams/branches filling up and blocking upstream. They can
+ * be fixed by relaxing the default time/size/buffer limits on the queue
+ * elements in the non-x264 branches, or using a (single) multiqueue element
+ * for all branches. Also see the last example below.
+ * </note>
+ *
  * <refsect2>
  * <title>Example pipeline</title>
  * |[
  * ]| This example pipeline will encode a test video source to H264 using fixed
  * quantization, and muxes it in a Matroska container.
  * |[
- * gst-launch -v videotestsrc num-buffers=1000 ! x264enc pass=5 quantizer=25 speed-preset=6 profile=1 ! \
+ * gst-launch -v videotestsrc num-buffers=1000 ! x264enc pass=5 quantizer=25 speed-preset=6 ! video/x-h264, profile=baseline ! \
  *   qtmux ! filesink location=videotestsrc.mov
  * ]| This example pipeline will encode a test video source to H264 using
  * constant quality at around Q25 using the 'medium' speed/quality preset and
  * restricting the options used so that the output is H.264 Baseline Profile
  * compliant and finally multiplexing the output in Quicktime mov format.
+ * |[
+ * gst-launch -v videotestsrc num-buffers=1000 ! tee name=t ! queue ! xvimagesink \
+ *   t. ! queue ! x264enc rc-lookahead=5 ! fakesink
+ * ]| This example pipeline will encode a test video source to H264 while
+ * displaying the input material at the same time.  As mentioned above,
+ * specific settings are needed in this case to avoid pipeline stalling.
+ * Depending on goals and context, other approaches are possible, e.g.
+ * tune=zerolatency might be configured, or queue sizes increased.
  * </refsect2>
  */
 
@@ -75,6 +96,8 @@
 
 #include "gstx264enc.h"
 
+#include <gst/pbutils/pbutils.h>
+
 #if X264_BUILD >= 71
 #define X264_DELAYED_FRAMES_API
 #endif
 #define X264_MB_RC
 #endif
 
+#if X264_BUILD >= 78
+/* b-pyramid was available before but was changed from boolean here */
+#define X264_B_PYRAMID
+#endif
+
 #if X264_BUILD >= 80
 #define X264_ENH_THREADING
 #endif
@@ -190,6 +218,13 @@ static GString *x264enc_defaults;
 #define ARG_PSY_TUNE_DEFAULT           0        /* no psy tuning */
 #define ARG_TUNE_DEFAULT               0        /* no tuning */
 
+enum
+{
+  GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY,
+  GST_X264_ENC_STREAM_FORMAT_AVC,
+  GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM
+};
+
 enum
 {
   GST_X264_ENC_PASS_CBR = 0,
@@ -208,7 +243,7 @@ gst_x264_enc_pass_get_type (void)
 
   static const GEnumValue pass_types[] = {
     {GST_X264_ENC_PASS_CBR, "Constant Bitrate Encoding", "cbr"},
-    {GST_X264_ENC_PASS_QUANT, "Constant Quantizer", "quant"},
+    {GST_X264_ENC_PASS_QUANT, "Constant Quantizer (debugging only)", "quant"},
     {GST_X264_ENC_PASS_QUAL, "Constant Quality", "qual"},
     {GST_X264_ENC_PASS_PASS1, "VBR Encoding - Pass 1", "pass1"},
     {GST_X264_ENC_PASS_PASS2, "VBR Encoding - Pass 2", "pass2"},
@@ -341,7 +376,7 @@ static const GFlagsValue tune_types[] = {
   {0x0, "No tuning", "none"},
   {0x1, "Still image", "stillimage"},
   {0x2, "Fast decode", "fastdecode"},
-  {0x4, "Zero latency", "zerolatency"},
+  {0x4, "Zero latency (requires constant framerate)", "zerolatency"},
   {0, NULL, NULL},
 };
 
@@ -352,7 +387,7 @@ gst_x264_enc_tune_get_type (void)
   static GType tune_type = 0;
 
   if (!tune_type) {
-    tune_type = g_flags_register_static ("GstX264EncTune", tune_types);
+    tune_type = g_flags_register_static ("GstX264EncTune", tune_types + 1);
   }
   return tune_type;
 }
@@ -396,7 +431,7 @@ gst_x264_enc_build_tunings_string (GstX264Enc * x264enc)
 {
   int i = 1;
 
-  if (x264enc->tunings && x264enc->tunings->len)
+  if (x264enc->tunings)
     g_string_free (x264enc->tunings, TRUE);
 
   if (x264enc->psy_tune) {
@@ -425,7 +460,7 @@ static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS ("video/x-raw-yuv, "
-        "format = (fourcc) I420, "
+        "format = (fourcc) { I420, YV12 }, "
         "framerate = (fraction) [0, MAX], "
         "width = (int) [ 16, MAX ], " "height = (int) [ 16, MAX ]")
     );
@@ -437,7 +472,9 @@ static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
         "framerate = (fraction) [0/1, MAX], "
         "width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ], "
         "stream-format = (string) { byte-stream, avc }, "
-        "alignment = (string) { au }")
+        "alignment = (string) { au }, "
+        "profile = (string) { high-10, high, main, constrained-baseline, "
+        "high-10-intra }")
     );
 
 static void gst_x264_enc_finalize (GObject * object);
@@ -447,6 +484,7 @@ static gboolean gst_x264_enc_init_encoder (GstX264Enc * encoder);
 static void gst_x264_enc_close_encoder (GstX264Enc * encoder);
 
 static gboolean gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps);
+static GstCaps *gst_x264_enc_sink_get_caps (GstPad * pad);
 static gboolean gst_x264_enc_sink_event (GstPad * pad, GstEvent * event);
 static gboolean gst_x264_enc_src_event (GstPad * pad, GstEvent * event);
 static GstFlowReturn gst_x264_enc_chain (GstPad * pad, GstBuffer * buf);
@@ -487,10 +525,8 @@ gst_x264_enc_base_init (gpointer g_class)
       "Josef Zlomek <josef.zlomek@itonis.tv>, "
       "Mark Nauwelaerts <mnauw@users.sf.net>");
 
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&src_factory));
-  gst_element_class_add_pad_template (element_class,
-      gst_static_pad_template_get (&sink_factory));
+  gst_element_class_add_static_pad_template (element_class, &src_factory);
+  gst_element_class_add_static_pad_template (element_class, &sink_factory);
 }
 
 /* don't forget to free the string after use */
@@ -541,18 +577,23 @@ gst_x264_enc_class_init (GstX264EncClass * klass)
   g_object_class_install_property (gobject_class, ARG_PASS,
       g_param_spec_enum ("pass", "Encoding pass/type",
           "Encoding pass/type", GST_X264_ENC_PASS_TYPE,
-          ARG_PASS_DEFAULT, G_PARAM_READWRITE));
+          ARG_PASS_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, ARG_QUANTIZER,
       g_param_spec_uint ("quantizer", "Constant Quantizer",
           "Constant quantizer or quality to apply",
-          1, 50, ARG_QUANTIZER_DEFAULT, G_PARAM_READWRITE));
+          1, 50, ARG_QUANTIZER_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, ARG_BITRATE,
       g_param_spec_uint ("bitrate", "Bitrate", "Bitrate in kbit/sec", 1,
-          100 * 1024, ARG_BITRATE_DEFAULT, G_PARAM_READWRITE));
+          100 * 1024, ARG_BITRATE_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
+          GST_PARAM_MUTABLE_PLAYING));
   g_object_class_install_property (gobject_class, ARG_VBV_BUF_CAPACITY,
       g_param_spec_uint ("vbv-buf-capacity", "VBV buffer capacity",
           "Size of the VBV buffer in milliseconds",
-          300, 10000, ARG_VBV_BUF_CAPACITY_DEFAULT, G_PARAM_READWRITE));
+          0, 10000, ARG_VBV_BUF_CAPACITY_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
+          GST_PARAM_MUTABLE_PLAYING));
 
 #ifdef X264_PRESETS
   g_object_class_install_property (gobject_class, ARG_SPEED_PRESET,
@@ -560,31 +601,37 @@ gst_x264_enc_class_init (GstX264EncClass * klass)
           "Preset name for speed/quality tradeoff options (can affect decode "
           "compatibility - impose restrictions separately for your target decoder)",
           GST_X264_ENC_SPEED_PRESET_TYPE, ARG_SPEED_PRESET_DEFAULT,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, ARG_PSY_TUNE,
       g_param_spec_enum ("psy-tune", "Psychovisual tuning preset",
           "Preset name for psychovisual tuning options",
-          GST_X264_ENC_PSY_TUNE_TYPE, ARG_PSY_TUNE_DEFAULT, G_PARAM_READWRITE));
+          GST_X264_ENC_PSY_TUNE_TYPE, ARG_PSY_TUNE_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, ARG_TUNE,
       g_param_spec_flags ("tune", "Content tuning preset",
           "Preset name for non-psychovisual tuning options",
-          GST_X264_ENC_TUNE_TYPE, ARG_TUNE_DEFAULT, G_PARAM_READWRITE));
+          GST_X264_ENC_TUNE_TYPE, ARG_TUNE_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, ARG_PROFILE,
       g_param_spec_enum ("profile", "H.264 profile",
           "Apply restrictions to meet H.264 Profile constraints. This will "
-          "override other properties if necessary.",
-          GST_X264_ENC_PROFILE_TYPE, ARG_PROFILE_DEFAULT, G_PARAM_READWRITE));
+          "override other properties if necessary. This will only be used "
+          "if downstream elements do not specify a profile in their caps (DEPRECATED)",
+          GST_X264_ENC_PROFILE_TYPE, ARG_PROFILE_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 #endif /* X264_PRESETS */
   g_object_class_install_property (gobject_class, ARG_OPTION_STRING,
       g_param_spec_string ("option-string", "Option string",
           "String of x264 options (overridden by element properties)",
-          ARG_OPTION_STRING_DEFAULT, G_PARAM_READWRITE));
+          ARG_OPTION_STRING_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
   /* options for which we _do_ use string equivalents */
   g_object_class_install_property (gobject_class, ARG_THREADS,
       g_param_spec_uint ("threads", "Threads",
           "Number of threads used by the codec (0 for automatic)",
-          0, 4, ARG_THREADS_DEFAULT, G_PARAM_READWRITE));
+          0, 4, ARG_THREADS_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   /* NOTE: this first string append doesn't require the ':' delimiter but the
    * rest do */
   g_string_append_printf (x264enc_defaults, "threads=%d", ARG_THREADS_DEFAULT);
@@ -592,54 +639,60 @@ gst_x264_enc_class_init (GstX264EncClass * klass)
   g_object_class_install_property (gobject_class, ARG_SLICED_THREADS,
       g_param_spec_boolean ("sliced-threads", "Sliced Threads",
           "Low latency but lower efficiency threading",
-          ARG_SLICED_THREADS_DEFAULT, G_PARAM_READWRITE));
+          ARG_SLICED_THREADS_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":sliced-threads=%d",
       ARG_SLICED_THREADS_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_SYNC_LOOKAHEAD,
       g_param_spec_int ("sync-lookahead", "Sync Lookahead",
           "Number of buffer frames for threaded lookahead (-1 for automatic)",
-          -1, 250, ARG_SYNC_LOOKAHEAD_DEFAULT, G_PARAM_READWRITE));
+          -1, 250, ARG_SYNC_LOOKAHEAD_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":sync-lookahead=%d",
       ARG_SYNC_LOOKAHEAD_DEFAULT);
 #endif
   g_object_class_install_property (gobject_class, ARG_STATS_FILE,
       g_param_spec_string ("stats-file", "Stats File",
           "Filename for multipass statistics (deprecated, use multipass-cache-file)",
-          ARG_STATS_FILE_DEFAULT, G_PARAM_READWRITE));
+          ARG_STATS_FILE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_object_class_install_property (gobject_class, ARG_MULTIPASS_CACHE_FILE,
       g_param_spec_string ("multipass-cache-file", "Multipass Cache File",
           "Filename for multipass cache file",
-          ARG_MULTIPASS_CACHE_FILE_DEFAULT, G_PARAM_READWRITE));
+          ARG_MULTIPASS_CACHE_FILE_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":stats=%s",
       ARG_MULTIPASS_CACHE_FILE_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_BYTE_STREAM,
       g_param_spec_boolean ("byte-stream", "Byte Stream",
           "Generate byte stream format of NALU", ARG_BYTE_STREAM_DEFAULT,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":annexb=%d",
       ARG_BYTE_STREAM_DEFAULT);
 #ifdef X264_INTRA_REFRESH
   g_object_class_install_property (gobject_class, ARG_INTRA_REFRESH,
       g_param_spec_boolean ("intra-refresh", "Intra Refresh",
           "Use Periodic Intra Refresh instead of IDR frames",
-          ARG_INTRA_REFRESH_DEFAULT, G_PARAM_READWRITE));
+          ARG_INTRA_REFRESH_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":intra-refresh=%d",
       ARG_INTRA_REFRESH_DEFAULT);
 #endif
   g_object_class_install_property (gobject_class, ARG_ME,
       g_param_spec_enum ("me", "Motion Estimation",
           "Integer pixel motion estimation method", GST_X264_ENC_ME_TYPE,
-          ARG_ME_DEFAULT, G_PARAM_READWRITE));
+          ARG_ME_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":me=%s",
       x264_motion_est_names[ARG_ME_DEFAULT]);
   g_object_class_install_property (gobject_class, ARG_SUBME,
       g_param_spec_uint ("subme", "Subpixel Motion Estimation",
-          "Subpixel motion estimation and partition decision quality: 1=fast, 6=best",
-          1, 6, ARG_SUBME_DEFAULT, G_PARAM_READWRITE));
+          "Subpixel motion estimation and partition decision quality: 1=fast, 10=best",
+          1, 10, ARG_SUBME_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":subme=%d", ARG_SUBME_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_ANALYSE,
       g_param_spec_flags ("analyse", "Analyse", "Partitions to consider",
-          GST_X264_ENC_ANALYSE_TYPE, ARG_ANALYSE_DEFAULT, G_PARAM_READWRITE));
+          GST_X264_ENC_ANALYSE_TYPE, ARG_ANALYSE_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   partitions = gst_x264_enc_build_partitions (ARG_ANALYSE_DEFAULT);
   if (partitions) {
     g_string_append_printf (x264enc_defaults, ":partitions=%s", partitions);
@@ -648,106 +701,121 @@ gst_x264_enc_class_init (GstX264EncClass * klass)
   g_object_class_install_property (gobject_class, ARG_DCT8x8,
       g_param_spec_boolean ("dct8x8", "DCT8x8",
           "Adaptive spatial transform size", ARG_DCT8x8_DEFAULT,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":8x8dct=%d", ARG_DCT8x8_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_REF,
       g_param_spec_uint ("ref", "Reference Frames",
           "Number of reference frames",
-          1, 12, ARG_REF_DEFAULT, G_PARAM_READWRITE));
+          1, 12, ARG_REF_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":ref=%d", ARG_REF_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_BFRAMES,
       g_param_spec_uint ("bframes", "B-Frames",
           "Number of B-frames between I and P",
-          0, 4, ARG_BFRAMES_DEFAULT, G_PARAM_READWRITE));
+          0, 4, ARG_BFRAMES_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":bframes=%d", ARG_BFRAMES_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_B_ADAPT,
       g_param_spec_boolean ("b-adapt", "B-Adapt",
           "Automatically decide how many B-frames to use",
-          ARG_B_ADAPT_DEFAULT, G_PARAM_READWRITE));
+          ARG_B_ADAPT_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":b-adapt=%d", ARG_B_ADAPT_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_B_PYRAMID,
       g_param_spec_boolean ("b-pyramid", "B-Pyramid",
           "Keep some B-frames as references", ARG_B_PYRAMID_DEFAULT,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+#ifdef X264_B_PYRAMID
+  g_string_append_printf (x264enc_defaults, ":b-pyramid=%s",
+      x264_b_pyramid_names[ARG_B_PYRAMID_DEFAULT]);
+#else
   g_string_append_printf (x264enc_defaults, ":b-pyramid=%d",
       ARG_B_PYRAMID_DEFAULT);
+#endif /* X264_B_PYRAMID */
   g_object_class_install_property (gobject_class, ARG_WEIGHTB,
       g_param_spec_boolean ("weightb", "Weighted B-Frames",
           "Weighted prediction for B-frames", ARG_WEIGHTB_DEFAULT,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":weightb=%d", ARG_WEIGHTB_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_SPS_ID,
       g_param_spec_uint ("sps-id", "SPS ID",
           "SPS and PPS ID number",
-          0, 31, ARG_SPS_ID_DEFAULT, G_PARAM_READWRITE));
+          0, 31, ARG_SPS_ID_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":sps-id=%d", ARG_SPS_ID_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_AU_NALU,
       g_param_spec_boolean ("aud", "AUD",
           "Use AU (Access Unit) delimiter", ARG_AU_NALU_DEFAULT,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":aud=%d", ARG_AU_NALU_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_TRELLIS,
       g_param_spec_boolean ("trellis", "Trellis quantization",
           "Enable trellis searched quantization", ARG_TRELLIS_DEFAULT,
-          G_PARAM_READWRITE));
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":trellis=%d", ARG_TRELLIS_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_KEYINT_MAX,
       g_param_spec_uint ("key-int-max", "Key-frame maximal interval",
           "Maximal distance between two key-frames (0 for automatic)",
-          0, G_MAXINT, ARG_KEYINT_MAX_DEFAULT, G_PARAM_READWRITE));
+          0, G_MAXINT, ARG_KEYINT_MAX_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":keyint=%d",
       ARG_KEYINT_MAX_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_CABAC,
       g_param_spec_boolean ("cabac", "Use CABAC", "Enable CABAC entropy coding",
-          ARG_CABAC_DEFAULT, G_PARAM_READWRITE));
+          ARG_CABAC_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":cabac=%d", ARG_CABAC_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_QP_MIN,
       g_param_spec_uint ("qp-min", "Minimum Quantizer",
-          "Minimum quantizer", 1, 51, ARG_QP_MIN_DEFAULT, G_PARAM_READWRITE));
+          "Minimum quantizer", 1, 51, ARG_QP_MIN_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":qpmin=%d", ARG_QP_MIN_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_QP_MAX,
       g_param_spec_uint ("qp-max", "Maximum Quantizer",
-          "Maximum quantizer", 1, 51, ARG_QP_MAX_DEFAULT, G_PARAM_READWRITE));
+          "Maximum quantizer", 1, 51, ARG_QP_MAX_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":qpmax=%d", ARG_QP_MAX_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_QP_STEP,
       g_param_spec_uint ("qp-step", "Maximum Quantizer Difference",
           "Maximum quantizer difference between frames",
-          1, 50, ARG_QP_STEP_DEFAULT, G_PARAM_READWRITE));
+          1, 50, ARG_QP_STEP_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":qpstep=%d", ARG_QP_STEP_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_IP_FACTOR,
       g_param_spec_float ("ip-factor", "IP-Factor",
           "Quantizer factor between I- and P-frames",
-          0, 2, ARG_IP_FACTOR_DEFAULT, G_PARAM_READWRITE));
+          0, 2, ARG_IP_FACTOR_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":ip-factor=%f",
       ARG_IP_FACTOR_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_PB_FACTOR,
       g_param_spec_float ("pb-factor", "PB-Factor",
           "Quantizer factor between P- and B-frames", 0, 2,
-          ARG_PB_FACTOR_DEFAULT, G_PARAM_READWRITE));
+          ARG_PB_FACTOR_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":pb-factor=%f",
       ARG_PB_FACTOR_DEFAULT);
 #ifdef X264_MB_RC
   g_object_class_install_property (gobject_class, ARG_RC_MB_TREE,
       g_param_spec_boolean ("mb-tree", "Macroblock Tree",
           "Macroblock-Tree ratecontrol",
-          ARG_RC_MB_TREE_DEFAULT, G_PARAM_READWRITE));
+          ARG_RC_MB_TREE_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":mbtree=%d",
       ARG_RC_MB_TREE_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_RC_LOOKAHEAD,
       g_param_spec_int ("rc-lookahead", "Rate Control Lookahead",
           "Number of frames for frametype lookahead", 0, 250,
-          ARG_RC_LOOKAHEAD_DEFAULT, G_PARAM_READWRITE));
+          ARG_RC_LOOKAHEAD_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":rc-lookahead=%d",
       ARG_RC_LOOKAHEAD_DEFAULT);
 #endif
   g_object_class_install_property (gobject_class, ARG_NR,
       g_param_spec_uint ("noise-reduction", "Noise Reduction",
           "Noise reduction strength",
-          0, 100000, ARG_NR_DEFAULT, G_PARAM_READWRITE));
+          0, 100000, ARG_NR_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":nr=%d", ARG_NR_DEFAULT);
   g_object_class_install_property (gobject_class, ARG_INTERLACED,
       g_param_spec_boolean ("interlaced", "Interlaced",
-          "Interlaced material", ARG_INTERLACED_DEFAULT, G_PARAM_READWRITE));
+          "Interlaced material", ARG_INTERLACED_DEFAULT,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
   g_string_append_printf (x264enc_defaults, ":interlaced=%d",
       ARG_INTERLACED_DEFAULT);
 
@@ -800,6 +868,8 @@ gst_x264_enc_init (GstX264Enc * encoder, GstX264EncClass * klass)
   encoder->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
   gst_pad_set_setcaps_function (encoder->sinkpad,
       GST_DEBUG_FUNCPTR (gst_x264_enc_sink_set_caps));
+  gst_pad_set_getcaps_function (encoder->sinkpad,
+      GST_DEBUG_FUNCPTR (gst_x264_enc_sink_get_caps));
   gst_pad_set_event_function (encoder->sinkpad,
       GST_DEBUG_FUNCPTR (gst_x264_enc_sink_event));
   gst_pad_set_chain_function (encoder->sinkpad,
@@ -875,6 +945,7 @@ gst_x264_enc_reset (GstX264Enc * encoder)
   encoder->x264enc = NULL;
   encoder->width = 0;
   encoder->height = 0;
+  encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY;
 
   GST_OBJECT_LOCK (encoder);
   encoder->i_type = X264_TYPE_AUTO;
@@ -1079,6 +1150,10 @@ gst_x264_enc_init_encoder (GstX264Enc * encoder)
     case GST_X264_ENC_PASS_QUAL:
       encoder->x264param.rc.i_rc_method = X264_RC_CRF;
       encoder->x264param.rc.f_rf_constant = encoder->quantizer;
+      encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
+      encoder->x264param.rc.i_vbv_buffer_size
+          = encoder->x264param.rc.i_vbv_max_bitrate
+          * encoder->vbv_buf_capacity / 1000;
       break;
     case GST_X264_ENC_PASS_CBR:
     case GST_X264_ENC_PASS_PASS1:
@@ -1088,8 +1163,8 @@ gst_x264_enc_init_encoder (GstX264Enc * encoder)
       encoder->x264param.rc.i_rc_method = X264_RC_ABR;
       encoder->x264param.rc.i_bitrate = encoder->bitrate;
       encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
-      encoder->x264param.rc.i_vbv_buffer_size
-          encoder->x264param.rc.i_vbv_max_bitrate
+      encoder->x264param.rc.i_vbv_buffer_size =
+          encoder->x264param.rc.i_vbv_max_bitrate
           * encoder->vbv_buf_capacity / 1000;
       pass = encoder->pass & 0xF;
       break;
@@ -1126,15 +1201,66 @@ gst_x264_enc_init_encoder (GstX264Enc * encoder)
       break;
   }
 
+#if X264_BUILD >= 81 && X264_BUILD < 106
+  /* When vfr is disabled, libx264 ignores buffer timestamps. This causes
+   * issues with rate control in libx264 with our nanosecond timebase. This
+   * has been fixed upstream in libx264 but this workaround is required for
+   * pre-fix versions. */
+  if (!encoder->x264param.b_vfr_input) {
+    if (encoder->x264param.i_fps_num == 0) {
+      GST_ELEMENT_ERROR (encoder, STREAM, ENCODE,
+          ("Constant framerate is required."),
+          ("The framerate caps (%d/%d) indicate VFR but VFR is disabled in libx264. (Is the zerolatency tuning in use?)",
+              encoder->x264param.i_fps_num, encoder->x264param.i_fps_den));
+      return FALSE;
+    }
+    encoder->x264param.i_timebase_num = encoder->x264param.i_fps_den;
+    encoder->x264param.i_timebase_den = encoder->x264param.i_fps_num;
+  }
+#endif
+
 #ifdef X264_PRESETS
-  if (encoder->profile
-      && x264_param_apply_profile (&encoder->x264param,
-          x264_profile_names[encoder->profile - 1])) {
-    GST_WARNING_OBJECT (encoder, "Bad profile name: %s",
-        x264_profile_names[encoder->profile - 1]);
+  if (encoder->peer_profile) {
+    if (x264_param_apply_profile (&encoder->x264param, encoder->peer_profile))
+      GST_WARNING_OBJECT (encoder, "Bad downstream profile name: %s",
+          encoder->peer_profile);
+  } else if (encoder->profile) {
+    if (x264_param_apply_profile (&encoder->x264param,
+            x264_profile_names[encoder->profile - 1]))
+      GST_WARNING_OBJECT (encoder, "Bad profile name: %s",
+          x264_profile_names[encoder->profile - 1]);
   }
 #endif /* X264_PRESETS */
 
+  /* If using an intra profile, all frames are intra frames */
+  if (encoder->peer_intra_profile)
+    encoder->x264param.i_keyint_max = encoder->x264param.i_keyint_min = 1;
+
+  /* Enforce level limits if they were in the caps */
+  if (encoder->peer_level) {
+    encoder->x264param.i_level_idc = encoder->peer_level->level_idc;
+
+    encoder->x264param.rc.i_bitrate = MIN (encoder->x264param.rc.i_bitrate,
+        encoder->peer_level->bitrate);
+    encoder->x264param.rc.i_vbv_max_bitrate =
+        MIN (encoder->x264param.rc.i_vbv_max_bitrate,
+        encoder->peer_level->bitrate);
+    encoder->x264param.rc.i_vbv_buffer_size =
+        MIN (encoder->x264param.rc.i_vbv_buffer_size, encoder->peer_level->cpb);
+    encoder->x264param.analyse.i_mv_range =
+        MIN (encoder->x264param.analyse.i_mv_range,
+        encoder->peer_level->mv_range);
+
+    if (encoder->peer_level->frame_only) {
+      encoder->x264param.b_interlaced = FALSE;
+#if X264_BUILD >= 95
+      encoder->x264param.b_fake_interlaced = FALSE;
+#endif
+    }
+  }
+
+  encoder->reconfig = FALSE;
+
   GST_OBJECT_UNLOCK (encoder);
 
   encoder->x264enc = x264_encoder_open (&encoder->x264param);
@@ -1165,6 +1291,42 @@ gst_x264_enc_close_encoder (GstX264Enc * encoder)
   }
 }
 
+static gboolean
+gst_x264_enc_set_profile_and_level (GstX264Enc * encoder, GstCaps * caps)
+{
+  x264_nal_t *nal;
+  int i_nal;
+  int header_return;
+  gint sps_ni = 0;
+  guint8 *sps;
+
+
+  header_return = x264_encoder_headers (encoder->x264enc, &nal, &i_nal);
+  if (header_return < 0) {
+    GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x264 header failed."),
+        ("x264_encoder_headers return code=%d", header_return));
+    return FALSE;
+  }
+
+  /* old x264 returns SEI, SPS and PPS, newer one has SEI last */
+  if (i_nal == 3 && nal[sps_ni].i_type != 7)
+    sps_ni = 1;
+
+  /* old style API: nal's are not encapsulated, and have no sync/size prefix,
+   * new style API: nal's are encapsulated, and have 4-byte size prefix */
+#ifndef X264_ENC_NALS
+  sps = nal[sps_ni].p_payload;
+#else
+  sps = nal[sps_ni].p_payload + 4;
+  /* skip NAL unit type */
+  sps++;
+#endif
+
+  gst_codec_utils_h264_caps_set_level_and_profile (caps, sps, 3);
+
+  return TRUE;
+}
+
 /*
  * Returns: Buffer with the stream headers.
  */
@@ -1291,7 +1453,14 @@ gst_x264_enc_set_src_caps (GstX264Enc * encoder, GstPad * pad, GstCaps * caps)
 
   structure = gst_caps_get_structure (outcaps, 0);
 
-  if (!encoder->byte_stream) {
+  if (encoder->current_byte_stream == GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY) {
+    if (encoder->byte_stream) {
+      encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM;
+    } else {
+      encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_AVC;
+    }
+  }
+  if (encoder->current_byte_stream == GST_X264_ENC_STREAM_FORMAT_AVC) {
     buf = gst_x264_enc_header_buf (encoder);
     if (buf != NULL) {
       gst_caps_set_simple (outcaps, "codec_data", GST_TYPE_BUFFER, buf, NULL);
@@ -1304,6 +1473,11 @@ gst_x264_enc_set_src_caps (GstX264Enc * encoder, GstPad * pad, GstCaps * caps)
   }
   gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
 
+  if (!gst_x264_enc_set_profile_and_level (encoder, outcaps)) {
+    gst_caps_unref (outcaps);
+    return FALSE;
+  }
+
   res = gst_pad_set_caps (pad, outcaps);
   gst_caps_unref (outcaps);
 
@@ -1314,15 +1488,18 @@ static gboolean
 gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
 {
   GstX264Enc *encoder = GST_X264_ENC (GST_OBJECT_PARENT (pad));
+  GstVideoFormat format;
   gint width, height;
   gint fps_num, fps_den;
   gint par_num, par_den;
   gint i;
+  GstCaps *peer_caps;
+  const GstCaps *template_caps;
+  GstCaps *allowed_caps = NULL;
+  gboolean level_ok = TRUE;
 
   /* get info from caps */
-  /* only I420 supported for now; so apparently claims x264enc ? */
-  if (!gst_video_format_parse_caps (caps, &encoder->format, &width, &height) ||
-      encoder->format != GST_VIDEO_FORMAT_I420)
+  if (!gst_video_format_parse_caps (caps, &format, &width, &height))
     return FALSE;
   if (!gst_video_parse_caps_framerate (caps, &fps_num, &fps_den))
     return FALSE;
@@ -1331,8 +1508,8 @@ gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
     par_den = 1;
   }
 
-  /* If the encoder is initialized, do not
-     reinitialize it again if not necessary */
+  /* If the encoder is initialized, do not reinitialize it again if not
+   * necessary */
   if (encoder->x264enc) {
     if (width == encoder->width && height == encoder->height
         && fps_num == encoder->fps_num && fps_den == encoder->fps_den
@@ -1346,6 +1523,7 @@ gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
   }
 
   /* store input description */
+  encoder->format = format;
   encoder->width = width;
   encoder->height = height;
   encoder->fps_num = fps_num;
@@ -1353,17 +1531,143 @@ gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
   encoder->par_num = par_num;
   encoder->par_den = par_den;
 
-  /* prepare a cached image description  */
+  /* prepare a cached image description */
   encoder->image_size = gst_video_format_get_size (encoder->format, width,
       height);
   for (i = 0; i < 3; ++i) {
-    /* only offsets now, is shifted later */
+    /* only offsets now, is shifted later. Offsets will be for Y, U, V so we
+     * can just feed YV12 as I420 to the decoder later */
     encoder->offset[i] = gst_video_format_get_component_offset (encoder->format,
         i, width, height);
     encoder->stride[i] = gst_video_format_get_row_stride (encoder->format,
         i, width);
   }
 
+  encoder->peer_profile = NULL;
+  encoder->peer_intra_profile = FALSE;
+  encoder->peer_level = NULL;
+
+  /* FIXME: Remove THIS bit in 0.11 when the profile property is removed */
+  peer_caps = gst_pad_peer_get_caps_reffed (encoder->srcpad);
+  if (peer_caps) {
+    gint i;
+    gboolean has_profile_or_level_or_format = FALSE;
+
+    for (i = 0; i < gst_caps_get_size (peer_caps); i++) {
+      GstStructure *s = gst_caps_get_structure (peer_caps, i);
+
+      if (gst_structure_has_name (s, "video/x-h264") &&
+          (gst_structure_has_field (s, "profile") ||
+              gst_structure_has_field (s, "level") ||
+              gst_structure_has_field (s, "stream-format"))) {
+        has_profile_or_level_or_format = TRUE;
+        break;
+      }
+    }
+
+    if (has_profile_or_level_or_format) {
+      template_caps = gst_pad_get_pad_template_caps (encoder->srcpad);
+
+      allowed_caps = gst_caps_intersect (peer_caps, template_caps);
+    }
+
+    gst_caps_unref (peer_caps);
+  }
+
+  /* Replace the bit since FIXME with this
+   * allowed_caps = gst_pad_get_allowed_caps (encoder->srcpad);
+   */
+
+  if (allowed_caps) {
+    GstStructure *s;
+    const gchar *profile;
+    const gchar *level;
+    const gchar *stream_format;
+
+    if (gst_caps_is_empty (allowed_caps)) {
+      gst_caps_unref (allowed_caps);
+      return FALSE;
+    }
+
+    allowed_caps = gst_caps_make_writable (allowed_caps);
+    gst_pad_fixate_caps (encoder->srcpad, allowed_caps);
+    s = gst_caps_get_structure (allowed_caps, 0);
+
+    profile = gst_structure_get_string (s, "profile");
+    if (profile) {
+      if (!strcmp (profile, "constrained-baseline")) {
+        encoder->peer_profile = "baseline";
+      } else if (!strcmp (profile, "high-10-intra")) {
+        encoder->peer_intra_profile = TRUE;
+        encoder->peer_profile = "high10";
+      } else if (!strcmp (profile, "high-10")) {
+        encoder->peer_profile = "high10";
+      } else if (!strcmp (profile, "high")) {
+        encoder->peer_profile = "high";
+      } else if (!strcmp (profile, "main")) {
+        encoder->peer_profile = "main";
+      } else {
+        g_assert_not_reached ();
+      }
+    }
+
+    level = gst_structure_get_string (s, "level");
+    if (level) {
+      int level_idc = gst_codec_utils_h264_get_level_idc (level);
+
+      if (level_idc) {
+        gint i;
+
+        for (i = 0; x264_levels[i].level_idc; i++) {
+          if (level_idc == x264_levels[i].level_idc) {
+            int mb_width = (width + 15) / 16;
+            int mb_height = (height + 15) / 16;
+            int mbs = mb_width * mb_height;
+
+            if (x264_levels[i].frame_size < mbs ||
+                x264_levels[i].frame_size * 8 < mb_width * mb_width ||
+                x264_levels[i].frame_size * 8 < mb_height * mb_height) {
+              GST_WARNING_OBJECT (encoder,
+                  "Frame size larger than level %s allows", level);
+              level_ok = FALSE;
+              break;
+            }
+
+            if (fps_den &&
+                x264_levels[i].mbps < (gint64) mbs * fps_num / fps_den) {
+              GST_WARNING_OBJECT (encoder,
+                  "Macroblock rate higher than level %s allows", level);
+              level_ok = FALSE;
+              break;
+            }
+
+            encoder->peer_level = &x264_levels[i];
+            break;
+          }
+        }
+      }
+    }
+
+    stream_format = gst_structure_get_string (s, "stream-format");
+    encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY;
+    if (stream_format) {
+      if (!strcmp (stream_format, "avc")) {
+        encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_AVC;
+        g_string_append_printf (encoder->option_string, ":annexb=0");
+      } else if (!strcmp (stream_format, "byte-stream")) {
+        encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM;
+        g_string_append_printf (encoder->option_string, ":annexb=1");
+      } else {
+        /* means we have both in caps and _FROM_PROPERTY should be the option */
+      }
+    }
+
+    gst_caps_unref (allowed_caps);
+  }
+
+  if (!level_ok)
+    return FALSE;
+
   if (!gst_x264_enc_init_encoder (encoder))
     return FALSE;
 
@@ -1375,6 +1679,59 @@ gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
   return TRUE;
 }
 
+static GstCaps *
+gst_x264_enc_sink_get_caps (GstPad * pad)
+{
+  GstX264Enc *encoder;
+  GstPad *peer;
+  GstCaps *caps;
+
+  encoder = GST_X264_ENC (gst_pad_get_parent (pad));
+  if (!encoder)
+    return gst_caps_new_empty ();
+
+  peer = gst_pad_get_peer (encoder->srcpad);
+  if (peer) {
+    const GstCaps *templcaps;
+    GstCaps *peercaps;
+    guint i, n;
+
+    peercaps = gst_pad_get_caps (peer);
+
+    /* Translate peercaps to YUV */
+    peercaps = gst_caps_make_writable (peercaps);
+    n = gst_caps_get_size (peercaps);
+    for (i = 0; i < n; i++) {
+      GstStructure *s = gst_caps_get_structure (peercaps, i);
+
+      gst_structure_set_name (s, "video/x-raw-yuv");
+      gst_structure_remove_field (s, "stream-format");
+      gst_structure_remove_field (s, "alignment");
+    }
+
+    templcaps = gst_pad_get_pad_template_caps (pad);
+
+    caps = gst_caps_intersect (peercaps, templcaps);
+    gst_caps_unref (peercaps);
+    gst_object_unref (peer);
+    peer = NULL;
+  } else {
+    caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
+  }
+
+  /* If we already have caps return them */
+  if (GST_PAD_CAPS (pad) && gst_caps_can_intersect (GST_PAD_CAPS (pad), caps)) {
+    GstCaps *tmpcaps = gst_caps_copy (GST_PAD_CAPS (pad));
+
+    gst_caps_merge (tmpcaps, caps);
+    caps = tmpcaps;
+  }
+
+  gst_object_unref (encoder);
+
+  return caps;
+}
+
 static gboolean
 gst_x264_enc_src_event (GstPad * pad, GstEvent * event)
 {
@@ -1424,8 +1781,23 @@ gst_x264_enc_sink_event (GstPad * pad, GstEvent * event)
     case GST_EVENT_EOS:
       gst_x264_enc_flush_frames (encoder, TRUE);
       break;
+    case GST_EVENT_TAG:{
+      GstTagList *tags = NULL;
+
+      event =
+          GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));
+
+      gst_event_parse_tag (event, &tags);
+      /* drop codec/video-codec and replace encoder/encoder-version */
+      gst_tag_list_remove_tag (tags, GST_TAG_VIDEO_CODEC);
+      gst_tag_list_remove_tag (tags, GST_TAG_CODEC);
+      gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, "x264",
+          GST_TAG_ENCODER_VERSION, X264_BUILD, NULL);
+      /* push is done below */
+      break;
       /* no flushing if flush received,
        * buffers in encoder are considered (in the) past */
+    }
     case GST_EVENT_CUSTOM_DOWNSTREAM:{
       const GstStructure *s;
       s = gst_event_get_structure (event);
@@ -1523,7 +1895,6 @@ gst_x264_enc_encode_frame (GstX264Enc * encoder, x264_picture_t * pic_in,
 #endif
   int encoder_return;
   GstFlowReturn ret;
-  GstClockTime timestamp;
   GstClockTime duration;
   guint8 *data;
   GstEvent *forcekeyunit_event = NULL;
@@ -1531,6 +1902,14 @@ gst_x264_enc_encode_frame (GstX264Enc * encoder, x264_picture_t * pic_in,
   if (G_UNLIKELY (encoder->x264enc == NULL))
     return GST_FLOW_NOT_NEGOTIATED;
 
+  GST_OBJECT_LOCK (encoder);
+  if (encoder->reconfig) {
+    encoder->reconfig = FALSE;
+    if (x264_encoder_reconfig (encoder->x264enc, &encoder->x264param) < 0)
+      GST_WARNING_OBJECT (encoder, "Could not reconfigure");
+  }
+  GST_OBJECT_UNLOCK (encoder);
+
   encoder_return = x264_encoder_encode (encoder->x264enc,
       &nal, i_nal, pic_in, &pic_out);
 
@@ -1556,7 +1935,9 @@ gst_x264_enc_encode_frame (GstX264Enc * encoder, x264_picture_t * pic_in,
 
     nal_size =
         x264_nal_encode (encoder->buffer + i_size + 4, &i_data, 0, &nal[i]);
-    if (encoder->byte_stream)
+    g_assert (encoder->current_byte_stream !=
+        GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY);
+    if (encoder->current_byte_stream == GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM)
       GST_WRITE_UINT32_BE (encoder->buffer + i_size, 1);
     else
       GST_WRITE_UINT32_BE (encoder->buffer + i_size, nal_size);
@@ -1571,7 +1952,6 @@ gst_x264_enc_encode_frame (GstX264Enc * encoder, x264_picture_t * pic_in,
 
   in_buf = g_queue_pop_head (encoder->delay);
   if (in_buf) {
-    timestamp = GST_BUFFER_TIMESTAMP (in_buf);
     duration = GST_BUFFER_DURATION (in_buf);
     gst_buffer_unref (in_buf);
   } else {
@@ -1669,6 +2049,35 @@ out:
   return ret;
 }
 
+
+
+static void
+gst_x264_enc_reconfig (GstX264Enc * encoder)
+{
+  switch (encoder->pass) {
+    case GST_X264_ENC_PASS_QUAL:
+      encoder->x264param.rc.f_rf_constant = encoder->quantizer;
+      encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
+      encoder->x264param.rc.i_vbv_buffer_size
+          = encoder->x264param.rc.i_vbv_max_bitrate
+          * encoder->vbv_buf_capacity / 1000;
+      break;
+    case GST_X264_ENC_PASS_CBR:
+    case GST_X264_ENC_PASS_PASS1:
+    case GST_X264_ENC_PASS_PASS2:
+    case GST_X264_ENC_PASS_PASS3:
+    default:
+      encoder->x264param.rc.i_bitrate = encoder->bitrate;
+      encoder->x264param.rc.i_vbv_max_bitrate = encoder->bitrate;
+      encoder->x264param.rc.i_vbv_buffer_size
+          = encoder->x264param.rc.i_vbv_max_bitrate
+          * encoder->vbv_buf_capacity / 1000;
+      break;
+  }
+
+  encoder->reconfig = TRUE;
+}
+
 static void
 gst_x264_enc_set_property (GObject * object, guint prop_id,
     const GValue * value, GParamSpec * pspec)
@@ -1683,8 +2092,10 @@ gst_x264_enc_set_property (GObject * object, guint prop_id,
   GST_OBJECT_LOCK (encoder);
   /* state at least matters for sps, bytestream, pass,
    * and so by extension ... */
+
   state = GST_STATE (encoder);
-  if (state != GST_STATE_READY && state != GST_STATE_NULL)
+  if ((state != GST_STATE_READY && state != GST_STATE_NULL) &&
+      !(pspec->flags & GST_PARAM_MUTABLE_PLAYING))
     goto wrong_state;
 
   switch (prop_id) {
@@ -1693,12 +2104,15 @@ gst_x264_enc_set_property (GObject * object, guint prop_id,
       break;
     case ARG_QUANTIZER:
       encoder->quantizer = g_value_get_uint (value);
+      gst_x264_enc_reconfig (encoder);
       break;
     case ARG_BITRATE:
       encoder->bitrate = g_value_get_uint (value);
+      gst_x264_enc_reconfig (encoder);
       break;
     case ARG_VBV_BUF_CAPACITY:
       encoder->vbv_buf_capacity = g_value_get_uint (value);
+      gst_x264_enc_reconfig (encoder);
       break;
     case ARG_SPEED_PRESET:
       encoder->speed_preset = g_value_get_enum (value);
@@ -1788,8 +2202,13 @@ gst_x264_enc_set_property (GObject * object, guint prop_id,
       break;
     case ARG_B_PYRAMID:
       encoder->b_pyramid = g_value_get_boolean (value);
+#ifdef X264_B_PYRAMID
+      g_string_append_printf (encoder->option_string, ":b-pyramid=%s",
+          x264_b_pyramid_names[encoder->b_pyramid]);
+#else
       g_string_append_printf (encoder->option_string, ":b-pyramid=%d",
           encoder->b_pyramid);
+#endif /* X264_B_PYRAMID */
       break;
     case ARG_WEIGHTB:
       encoder->weightb = g_value_get_boolean (value);
@@ -1876,7 +2295,7 @@ gst_x264_enc_set_property (GObject * object, guint prop_id,
   /* ERROR */
 wrong_state:
   {
-    GST_DEBUG_OBJECT (encoder, "setting property in wrong state");
+    GST_WARNING_OBJECT (encoder, "setting property in wrong state");
     GST_OBJECT_UNLOCK (encoder);
   }
 }