x264enc: Select stream-format based on caps
authorThiago Santos <thiago.sousa.santos@collabora.co.uk>
Tue, 14 Jun 2011 02:24:27 +0000 (23:24 -0300)
committerNikhil Devshatwar <a0132237@ti.com>
Fri, 17 May 2013 09:40:35 +0000 (15:10 +0530)
Makes x264 select its stream-format based on what's available
on caps, the user selected option will be chosen as a fallback
when both options are available.

https://bugzilla.gnome.org/show_bug.cgi?id=644233

ext/x264/gstx264enc.c
ext/x264/gstx264enc.h

index f3377bda982a9bbc9afc628b9a4be8e3ae102ae9..f8cde40f8a8d88b1e1dac50a91bf767ac7e91eb5 100644 (file)
@@ -218,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,
@@ -940,6 +947,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;
@@ -1445,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);
@@ -1566,6 +1581,7 @@ gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
     GstStructure *s;
     const gchar *profile;
     const gchar *level;
+    const gchar *bytestream;
 
     if (gst_caps_is_empty (allowed_caps)) {
       gst_caps_unref (allowed_caps);
@@ -1631,6 +1647,18 @@ gst_x264_enc_sink_set_caps (GstPad * pad, GstCaps * caps)
       }
     }
 
+    bytestream = gst_structure_get_string (s, "stream-format");
+    encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_FROM_PROPERTY;
+    if (bytestream) {
+      if (!strcmp (bytestream, "avc")) {
+        encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_AVC;
+      } else if (!strcmp (profile, "byte-stream")) {
+        encoder->current_byte_stream = GST_X264_ENC_STREAM_FORMAT_BYTE_STREAM;
+      } else {
+        /* means we have both in caps and _FROM_PROPERTY should be the option */
+      }
+    }
+
     gst_caps_unref (allowed_caps);
   }
 
@@ -1904,7 +1932,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);
index e779a5e893f079d5ea793382844790b7514ac25f..a2f1145b97c5c1bd53c3ac2fa71544d395bccb37 100644 (file)
@@ -52,6 +52,7 @@ struct _GstX264Enc
 
   x264_t *x264enc;
   x264_param_t x264param;
+  gint current_byte_stream;
 
   /* properties */
   guint threads;