add caps for video/x-h264 encoder and remove unused source. 52/33752/1
authorKitae Kim <kt920.kim@samsung.com>
Wed, 14 Jan 2015 09:42:03 +0000 (18:42 +0900)
committerKitae Kim <kt920.kim@samsung.com>
Wed, 14 Jan 2015 09:42:03 +0000 (18:42 +0900)
Change-Id: I457cb8fab6dbcc23607c1aad947b65f2a2ebc8f8
Signed-off-by: Kitae Kim <kt920.kim@samsung.com>
src/gstmaruenc.c
src/gstmaruutils.c

index 11d00d1..9f8492e 100644 (file)
@@ -102,30 +102,30 @@ GstStateChangeReturn gst_maruenc_change_state (GstElement *element, GstStateChan
 static void
 gst_maruenc_base_init (GstMaruEncClass *klass)
 {
-    GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
-    GstPadTemplate *sinktempl = NULL, *srctempl = NULL;
-    GstCaps *sinkcaps = NULL, *srccaps = NULL;
-    CodecElement *codec;
-    gchar *longname, *classification, *description;
-
-    codec =
-        (CodecElement *)g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
-         GST_MARUENC_PARAMS_QDATA);
+  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
+  GstPadTemplate *sinktempl = NULL, *srctempl = NULL;
+  GstCaps *sinkcaps = NULL, *srccaps = NULL;
+  CodecElement *codec;
+  gchar *longname, *classification, *description;
 
-    longname = g_strdup_printf ("%s Encoder", codec->longname);
-    classification = g_strdup_printf ("Codec/Encoder/%s",
-            (codec->media_type == AVMEDIA_TYPE_VIDEO) ? "Video" : "Audio");
-    description = g_strdup_printf ("%s Encoder", codec->name);
+  codec =
+    (CodecElement *)g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
+        GST_MARUENC_PARAMS_QDATA);
 
-    gst_element_class_set_details_simple (element_class,
-            longname,
-            classification,
-            description,
-            "Kitae Kim <kt920.kim@samsung.com>");
+  longname = g_strdup_printf ("%s Encoder", codec->longname);
+  classification = g_strdup_printf ("Codec/Encoder/%s",
+      (codec->media_type == AVMEDIA_TYPE_VIDEO) ? "Video" : "Audio");
+  description = g_strdup_printf ("%s Encoder", codec->name);
 
-    g_free (longname);
-    g_free (classification);
+  gst_element_class_set_details_simple (element_class,
+      longname,
+      classification,
+      description,
+      "Erik Walthinsen <omega@cse.ogi.edu>");
 
+  g_free (longname);
+  g_free (classification);
+  g_free (description);
 
   if (!(srccaps = gst_maru_codecname_to_caps (codec->name, NULL, TRUE))) {
     GST_DEBUG ("Couldn't get source caps for encoder '%s'", codec->name);
@@ -259,10 +259,6 @@ gst_maruenc_init (GstMaruEnc *maruenc)
   maruenc->opened = FALSE;
 
   maruenc->dev = g_malloc0 (sizeof(CodecDevice));
-  if (!maruenc->dev) {
-    GST_ERROR_OBJECT (maruenc, "failed to allocate CodecDevice");
-    // TODO: error handling
-  }
 
   if (oclass->codec->media_type == AVMEDIA_TYPE_VIDEO) {
     gst_pad_set_chain_function (maruenc->sinkpad, gst_maruenc_chain_video);
@@ -307,9 +303,6 @@ gst_maruenc_finalize (GObject *object)
   }
 
   g_queue_free (maruenc->delay);
-#if 0
-  g_free (maruenc->filename);
-#endif
 
   g_object_unref (maruenc->adapter);
 
@@ -326,7 +319,6 @@ gst_maruenc_get_possible_sizes (GstMaruEnc *maruenc, GstPad *pad,
   guint i;
 
   othercaps = gst_pad_peer_get_caps (maruenc->srcpad);
-
   if (!othercaps) {
     return gst_caps_copy (caps);
   }
@@ -409,6 +401,47 @@ gst_maruenc_getcaps (GstPad *pad)
     return caps;
   }
 
+  if (!strcmp(oclass->codec->name, "libx264")) {
+    GstPad *peer;
+
+    peer = gst_pad_get_peer (maruenc->srcpad);
+    if (peer) {
+      const GstCaps *templcaps;
+      GstCaps *peercaps;
+      guint i, n;
+
+      peercaps = gst_pad_get_caps (peer);
+
+      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 (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;
+    }
+
+    return caps;
+  }
+
   // cached
   if (oclass->sinkcaps) {
     caps = gst_maruenc_get_possible_sizes (maruenc, pad, oclass->sinkcaps);
@@ -450,10 +483,6 @@ gst_maruenc_getcaps (GstPad *pad)
     }
 
     ctx = g_malloc0 (sizeof(CodecContext));
-    if (!ctx) {
-      GST_DEBUG_OBJECT (maruenc, "no context");
-      break;
-    }
 
     ctx->video.width = DEFAULT_WIDTH;
     ctx->video.height = DEFAULT_HEIGHT;
@@ -461,7 +490,6 @@ gst_maruenc_getcaps (GstPad *pad)
     ctx->video.fps_d = 25;
     ctx->video.ticks_per_frame = 1;
     ctx->bit_rate = DEFAULT_VIDEO_BITRATE;
-
     ctx->video.pix_fmt = pixfmt;
 
     GST_DEBUG ("Attempting to open codec");
@@ -484,11 +512,6 @@ gst_maruenc_getcaps (GstPad *pad)
       GST_DEBUG_OBJECT (maruenc, "Opening codec failed with pixfmt: %d", pixfmt);
     }
 
-#if 0
-    if (ctx->priv_data) {
-      gst_maru_avcodec_close (ctx, maruenc->dev);
-    }
-#endif
     g_free (ctx);
   }
 
@@ -533,29 +556,6 @@ gst_maruenc_setcaps (GstPad *pad, GstCaps *caps)
   GST_DEBUG_OBJECT (maruenc, "Setting context to bitrate %lu, gop_size %d",
       maruenc->bitrate, maruenc->gop_size);
 
-#if 0
-
-  // user defined properties
-  maruenc->context->gop_size = maruenc->gop_size;
-  maruenc->context->lmin = (maruenc->lmin * FF_QP2LAMBDA + 0.5);
-  maruenc->context->lmax = (maruenc->lmax * FF_QP2LAMBDA + 0.5);
-
-  // some other defaults
-  maruenc->context->b_frame_strategy = 0;
-  maruenc->context->coder_type = 0;
-  maruenc->context->context_model = 0;
-  maruenc->context->scenechange_threshold = 0;
-  maruenc->context->inter_threshold = 0;
-
-  if (maruenc->interlaced) {
-    maruenc->context->flags |=
-      CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME;
-    maruenc->picture->interlaced_frame = TRUE;
-
-    maruenc->picture->top_field_first = TRUE;
-  }
-#endif
-
   gst_maru_caps_with_codectype (oclass->codec->media_type, caps, maruenc->context);
 
   if (!maruenc->context->video.fps_d) {
@@ -705,8 +705,7 @@ gst_maruenc_chain_video (GstPad *pad, GstBuffer *buffer)
     interface->encode_video (maruenc->context, maruenc->working_buf,
                 maruenc->working_buf_size, GST_BUFFER_DATA (buffer),
                 GST_BUFFER_SIZE (buffer), GST_BUFFER_TIMESTAMP (buffer),
-                &coded_frame, &is_keyframe,
-                maruenc->dev);
+                &coded_frame, &is_keyframe, maruenc->dev);
 
   if (ret_size < 0) {
     GstMaruEncClass *oclass =
@@ -760,12 +759,13 @@ gst_maruenc_chain_video (GstPad *pad, GstBuffer *buffer)
        * - this unit cannot be decoded independently.
        */
       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
+    } else {
+      GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
     }
   } else {
     GST_WARNING_OBJECT (maruenc, "codec did not provide keyframe info");
   }
   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (maruenc->srcpad));
-
   gst_buffer_unref (buffer);
 
 #if 0
index c6fd4e2..00bbc5e 100644 (file)
@@ -401,6 +401,10 @@ gst_maru_caps_to_smpfmt (const GstCaps *caps, CodecContext *ctx, gboolean raw)
   }
 
   name = gst_structure_get_name (str);
+  if (!name) {
+    GST_ERROR ("Couldn't get audio sample format from caps %" GST_PTR_FORMAT, caps);
+    return;
+  }
 
   if (!strcmp (name, "audio/x-raw-float")) {
     if (gst_structure_get_int (str, "width", &width) &&
@@ -472,6 +476,10 @@ gst_maru_caps_with_codecname (const char *name, int media_type,
 
   if ((strcmp (name, "mpeg4") == 0)) {
     const gchar *mime = gst_structure_get_name (structure);
+    if (!mime) {
+      GST_ERROR ("Couldn't get mime type from caps %" GST_PTR_FORMAT, caps);
+      return;
+    }
 
     if (!strcmp (mime, "video/x-divx")) {
       ctx->codec_tag = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
@@ -512,11 +520,15 @@ gst_maru_caps_to_codecname (const GstCaps *caps,
 {
   const gchar *mimetype;
   const GstStructure *str;
-  int media_type;
+  int media_type = AVMEDIA_TYPE_UNKNOWN;
 
   str = gst_caps_get_structure (caps, 0);
 
   mimetype = gst_structure_get_name (str);
+  if (!mimetype) {
+    GST_ERROR ("Couldn't get mimetype from caps %" GST_PTR_FORMAT, caps);
+    return;
+  }
 
   if (!strcmp (mimetype, "video/x-h263")) {
     const gchar *h263version = gst_structure_get_string (str, "h263version");
@@ -649,15 +661,6 @@ gst_maru_caps_to_codecname (const GstCaps *caps,
   }
 
   if (context != NULL) {
-#if 0
-    if (video == TRUE) {
-      context->codec_type = CODEC_TYPE_VIDEO;
-    } else if (audio == TRUE) {
-      context->codec_type = CODEC_TYPE_AUDIO;
-    } else {
-      context->codec_type = CODEC_TYPE_UNKNOWN;
-    }
-#endif
     gst_maru_caps_with_codecname (codec_name, media_type, caps, context);
   }
 
@@ -1112,8 +1115,8 @@ gst_maru_codecname_to_caps (const char *name, CodecContext *ctx, gboolean encode
             "video/x-3ivx", NULL));
       }
     }
-  } else if (strcmp (name, "h264") == 0) {
-      caps = gst_maru_video_caps_new (ctx, name, "video/x-h264", NULL);
+  } else if ((strcmp (name, "h264") == 0) || (strcmp (name, "libx264") == 0)) {
+    caps = gst_maru_video_caps_new (ctx, name, "video/x-h264", NULL);
   } else if (g_str_has_prefix(name, "msmpeg4")) {
     // msmpeg4v1,m msmpeg4v2, msmpeg4
     gint version;
@@ -1219,6 +1222,8 @@ gst_maru_codecname_to_caps (const char *name, CodecContext *ctx, gboolean encode
 
   if (caps != NULL) {
     if (ctx && ctx->codecdata_size > 0) {
+      GST_DEBUG ("codec_data size %d", ctx->codecdata_size);
+
       GstBuffer *data = gst_buffer_new_and_alloc (ctx->codecdata_size);
 
       memcpy (GST_BUFFER_DATA(data), ctx->codecdata, ctx->codecdata_size);