libs: refine GstVaapiMiniObject.
authorGwenole Beauchesne <gwenole.beauchesne@intel.com>
Tue, 30 Apr 2013 08:28:30 +0000 (10:28 +0200)
committerGwenole Beauchesne <gwenole.beauchesne@intel.com>
Tue, 7 May 2013 15:51:27 +0000 (17:51 +0200)
Drop support for user-defined data since this capability was not used
so far and GstVaapiMiniObject represents the smallest reference counted
object type. Add missing GST_VAAPI_MINI_OBJECT_CLASS() helper macro.

Besides, since GstVaapiMiniObject is a libgstvaapi internal object, it
is also possible to further simplify the layout of the object. i.e. merge
GstVaapiMiniObjectBase into GstVaapiMiniObject.

gst-libs/gst/vaapi/gstvaapicodec_objects.c
gst-libs/gst/vaapi/gstvaapidecoder_dpb.c
gst-libs/gst/vaapi/gstvaapidecoder_h264.c
gst-libs/gst/vaapi/gstvaapidecoder_mpeg2.c
gst-libs/gst/vaapi/gstvaapiminiobject.c
gst-libs/gst/vaapi/gstvaapiminiobject.h

index 3279243..1cd6583 100644 (file)
@@ -42,7 +42,7 @@ const GstVaapiCodecObjectClass *
 gst_vaapi_codec_object_get_class(GstVaapiCodecObject *object)
 {
     return (const GstVaapiCodecObjectClass *)
-        gst_vaapi_mini_object_get_class(GST_VAAPI_MINI_OBJECT(object));
+        GST_VAAPI_MINI_OBJECT_GET_CLASS(object);
 }
 
 static gboolean
index 047fa24..c8c2c78 100644 (file)
@@ -29,8 +29,7 @@
     ((GstVaapiDpbClass *)(klass))
 
 #define GST_VAAPI_DPB_GET_CLASS(obj) \
-    GST_VAAPI_DPB_CLASS(gst_vaapi_mini_object_get_class( \
-                            GST_VAAPI_MINI_OBJECT(obj)))
+    GST_VAAPI_DPB_CLASS(GST_VAAPI_MINI_OBJECT_GET_CLASS(obj))
 
 /**
  * GstVaapiDpb:
index aa61aeb..aff30c4 100644 (file)
@@ -57,6 +57,7 @@ typedef struct _GstVaapiPictureH264             GstVaapiPictureH264;
     ((GstVaapiParserInfoH264 *)(obj))
 
 struct _GstVaapiParserInfoH264 {
+    GstVaapiMiniObject  parent_instance;
     GstH264NalUnit      nalu;
     union {
         GstH264SPS      sps;
index b320248..ecc9d7f 100644 (file)
@@ -258,6 +258,7 @@ struct _GstMpegVideoSliceHdr {
 
 typedef struct _GstVaapiParserInfoMpeg2 GstVaapiParserInfoMpeg2;
 struct _GstVaapiParserInfoMpeg2 {
+    GstVaapiMiniObject  parent_instance;
     GstMpegVideoPacket  packet;
     guint8              extension_type; /* for Extension packets */
     union {
index 081df1d..477d00a 100644 (file)
 #include <string.h>
 #include "gstvaapiminiobject.h"
 
-typedef struct _GstVaapiMiniObjectBase GstVaapiMiniObjectBase;
-struct _GstVaapiMiniObjectBase {
-    gconstpointer       object_class;
-    gint                ref_count;
-    GDestroyNotify      user_data_destroy_notify;
-};
-
-static inline GstVaapiMiniObjectBase *
-object2base(GstVaapiMiniObject *object)
-{
-    return GSIZE_TO_POINTER(GPOINTER_TO_SIZE(object) -
-        sizeof(GstVaapiMiniObjectBase));
-}
-
-static inline GstVaapiMiniObject *
-base2object(GstVaapiMiniObjectBase *base_object)
-{
-    return GSIZE_TO_POINTER(GPOINTER_TO_SIZE(base_object) +
-        sizeof(GstVaapiMiniObjectBase));
-}
-
 static void
 gst_vaapi_mini_object_free(GstVaapiMiniObject *object)
 {
-    GstVaapiMiniObjectBase * const base_object = object2base(object);
-    const GstVaapiMiniObjectClass * const klass = base_object->object_class;
+    const GstVaapiMiniObjectClass * const klass = object->object_class;
 
-    g_atomic_int_inc(&base_object->ref_count);
+    g_atomic_int_inc(&object->ref_count);
 
     if (klass->finalize)
         klass->finalize(object);
 
-    if (G_LIKELY(g_atomic_int_dec_and_test(&base_object->ref_count))) {
-        if (object->user_data && base_object->user_data_destroy_notify)
-            base_object->user_data_destroy_notify(object->user_data);
-        g_slice_free1(sizeof(*base_object) + klass->size, base_object);
-    }
-}
-
-const GstVaapiMiniObjectClass *
-gst_vaapi_mini_object_get_class(GstVaapiMiniObject *object)
-{
-    g_return_val_if_fail(object != NULL, NULL);
-
-    return object2base(object)->object_class;
+    if (G_LIKELY(g_atomic_int_dec_and_test(&object->ref_count)))
+        g_slice_free1(klass->size, object);
 }
 
 /**
@@ -87,28 +54,23 @@ GstVaapiMiniObject *
 gst_vaapi_mini_object_new(const GstVaapiMiniObjectClass *object_class)
 {
     GstVaapiMiniObject *object;
-    GstVaapiMiniObjectBase *base_object;
 
     static const GstVaapiMiniObjectClass default_object_class = {
         .size = sizeof(GstVaapiMiniObject),
     };
 
-    if (!object_class)
+    if (G_UNLIKELY(!object_class))
         object_class = &default_object_class;
 
     g_return_val_if_fail(object_class->size >= sizeof(*object), NULL);
 
-    base_object = g_slice_alloc(sizeof(*base_object) + object_class->size);
-    if (!base_object)
+    object = g_slice_alloc(object_class->size);
+    if (!object)
         return NULL;
 
-    object = base2object(base_object);
+    object->object_class = object_class;
+    object->ref_count = 1;
     object->flags = 0;
-    object->user_data = 0;
-
-    base_object->object_class = object_class;
-    base_object->ref_count = 1;
-    base_object->user_data_destroy_notify = NULL;
     return object;
 }
 
@@ -132,7 +94,7 @@ gst_vaapi_mini_object_new0(const GstVaapiMiniObjectClass *object_class)
     if (!object)
         return NULL;
 
-    object_class = object2base(object)->object_class;
+    object_class = object->object_class;
 
     sub_size = object_class->size - sizeof(*object);
     if (sub_size > 0)
@@ -153,7 +115,7 @@ gst_vaapi_mini_object_ref(GstVaapiMiniObject *object)
 {
     g_return_val_if_fail(object != NULL, NULL);
 
-    g_atomic_int_inc(&object2base(object)->ref_count);
+    g_atomic_int_inc(&object->ref_count);
     return object;
 }
 
@@ -168,9 +130,9 @@ void
 gst_vaapi_mini_object_unref(GstVaapiMiniObject *object)
 {
     g_return_if_fail(object != NULL);
-    g_return_if_fail(object2base(object)->ref_count > 0);
+    g_return_if_fail(object->ref_count > 0);
 
-    if (g_atomic_int_dec_and_test(&object2base(object)->ref_count))
+    if (g_atomic_int_dec_and_test(&object->ref_count))
         gst_vaapi_mini_object_free(object);
 }
 
@@ -206,48 +168,3 @@ gst_vaapi_mini_object_replace(GstVaapiMiniObject **old_object_ptr,
     if (old_object)
         gst_vaapi_mini_object_unref(old_object);
 }
-
-/**
- * gst_vaapi_mini_object_get_user_data:
- * @object: a #GstVaapiMiniObject
- *
- * Gets user-provided data set on the object via a previous call to
- * gst_vaapi_mini_object_set_user_data().
- *
- * Returns: (transfer none): The previously set user_data
- */
-gpointer
-gst_vaapi_mini_object_get_user_data(GstVaapiMiniObject *object)
-{
-    g_return_val_if_fail(object != NULL, NULL);
-
-    return object->user_data;
-}
-
-/**
- * gst_vaapi_mini_object_set_user_data:
- * @object: a #GstVaapiMiniObject
- * @user_data: user-provided data
- * @destroy_notify: (closure user_data): a #GDestroyNotify
- *
- * Sets @user_data on the object and the #GDestroyNotify that will be
- * called when the data is freed.
- *
- * If some @user_data was previously set, then the former @destroy_notify
- * function will be called before the @user_data is replaced.
- */
-void
-gst_vaapi_mini_object_set_user_data(GstVaapiMiniObject *object,
-    gpointer user_data, GDestroyNotify destroy_notify)
-{
-    GstVaapiMiniObjectBase *base_object;
-
-    g_return_if_fail(object != NULL);
-
-    base_object = object2base(object);
-    if (object->user_data && base_object->user_data_destroy_notify)
-        base_object->user_data_destroy_notify(object->user_data);
-
-    object->user_data = user_data;
-    base_object->user_data_destroy_notify = destroy_notify;
-}
index 4cf27d9..900b232 100644 (file)
@@ -39,13 +39,22 @@ typedef struct _GstVaapiMiniObjectClass GstVaapiMiniObjectClass;
     ((GstVaapiMiniObject *)(object))
 
 /**
+ * GST_VAAPI_MINI_OBJECT_CLASS:
+ * @klass: a #GstVaapiMiniObjectClass
+ *
+ * Casts the @klass to a #GstVaapiMiniObjectClass
+ */
+#define GST_VAAPI_MINI_OBJECT_CLASS(klass) \
+    ((GstVaapiMiniObjectClass *)(klass))
+
+/**
  * GST_VAAPI_MINI_OBJECT_GET_CLASS:
  * @object: a #GstVaapiMiniObject
  *
  * Retrieves the #GstVaapiMiniObjectClass associated with the @object
  */
 #define GST_VAAPI_MINI_OBJECT_GET_CLASS(object) \
-    gst_vaapi_mini_object_get_class(GST_VAAPI_MINI_OBJECT(object))
+    (GST_VAAPI_MINI_OBJECT(object)->object_class)
 
 /**
  * GST_VAAPI_MINI_OBJECT_FLAGS:
@@ -88,17 +97,20 @@ typedef struct _GstVaapiMiniObjectClass GstVaapiMiniObjectClass;
 
 /**
  * GstVaapiMiniObject:
+ * @object_class: the #GstVaapiMiniObjectClass
+ * @ref_count: the object reference count that should be manipulated
+ *   through gst_vaapi_mini_object_ref() et al. helpers
  * @flags: set of flags that should be manipulated through
  *   GST_VAAPI_MINI_OBJECT_FLAG_*() functions
- * @user_data: user-provided data from gst_vaapi_mini_object_set_user_data()
  *
  * A #GstVaapiMiniObject represents a minimal reference counted data
  * structure that can hold a set of flags and user-provided data.
  */
 struct _GstVaapiMiniObject {
     /*< private >*/
+    gconstpointer       object_class;
+    volatile gint       ref_count;
     guint               flags;
-    gpointer            user_data;
 };
 
 /**
@@ -117,10 +129,6 @@ struct _GstVaapiMiniObjectClass {
 };
 
 G_GNUC_INTERNAL
-const GstVaapiMiniObjectClass *
-gst_vaapi_mini_object_get_class(GstVaapiMiniObject *object) G_GNUC_CONST;
-
-G_GNUC_INTERNAL
 GstVaapiMiniObject *
 gst_vaapi_mini_object_new(const GstVaapiMiniObjectClass *object_class);
 
@@ -141,15 +149,6 @@ void
 gst_vaapi_mini_object_replace(GstVaapiMiniObject **old_object_ptr,
     GstVaapiMiniObject *new_object);
 
-G_GNUC_INTERNAL
-gpointer
-gst_vaapi_mini_object_get_user_data(GstVaapiMiniObject *object);
-
-G_GNUC_INTERNAL
-void
-gst_vaapi_mini_object_set_user_data(GstVaapiMiniObject *object,
-    gpointer user_data, GDestroyNotify destroy_notify);
-
 G_END_DECLS
 
 #endif /* GST_VAAPI_MINI_OBJECT_H */