Blame gst/gstformat.c

Packit f546b1
/* GStreamer
Packit f546b1
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
Packit f546b1
 *                    2000 Wim Taymans <wim.taymans@chello.be>
Packit f546b1
 *                    2005 Wim Taymans <wim@fluendo.com>
Packit f546b1
 *
Packit f546b1
 * gstformat.c: GstFormat registration
Packit f546b1
 *
Packit f546b1
 * This library is free software; you can redistribute it and/or
Packit f546b1
 * modify it under the terms of the GNU Library General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2 of the License, or (at your option) any later version.
Packit f546b1
 *
Packit f546b1
 * This library is distributed in the hope that it will be useful,
Packit f546b1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f546b1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f546b1
 * Library General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Library General Public
Packit f546b1
 * License along with this library; if not, write to the
Packit f546b1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit f546b1
 * Boston, MA 02110-1301, USA.
Packit f546b1
 */
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * SECTION:gstformat
Packit f546b1
 * @title: GstFormat
Packit f546b1
 * @short_description: Dynamically register new data formats
Packit f546b1
 * @see_also: #GstPad, #GstElement
Packit f546b1
 *
Packit f546b1
 * GstFormats functions are used to register a new format to the gstreamer
Packit f546b1
 * core.  Formats can be used to perform seeking or conversions/query
Packit f546b1
 * operations.
Packit f546b1
 */
Packit f546b1
Packit f546b1
Packit f546b1
#include "gst_private.h"
Packit f546b1
#include <string.h>
Packit f546b1
#include "gstformat.h"
Packit f546b1
#include "gstenumtypes.h"
Packit f546b1
Packit f546b1
static GMutex mutex;
Packit f546b1
static GList *_gst_formats = NULL;
Packit f546b1
static GHashTable *_nick_to_format = NULL;
Packit f546b1
static GHashTable *_format_to_nick = NULL;
Packit f546b1
static guint32 _n_values = 1;   /* we start from 1 because 0 reserved for UNDEFINED */
Packit f546b1
Packit f546b1
static GstFormatDefinition standard_definitions[] = {
Packit f546b1
  {GST_FORMAT_DEFAULT, "default", "Default format for the media type", 0},
Packit f546b1
  {GST_FORMAT_BYTES, "bytes", "Bytes", 0},
Packit f546b1
  {GST_FORMAT_TIME, "time", "Time", 0},
Packit f546b1
  {GST_FORMAT_BUFFERS, "buffers", "Buffers", 0},
Packit f546b1
  {GST_FORMAT_PERCENT, "percent", "Percent", 0},
Packit f546b1
  {GST_FORMAT_UNDEFINED, NULL, NULL, 0}
Packit f546b1
};
Packit f546b1
Packit f546b1
void
Packit f546b1
_priv_gst_format_initialize (void)
Packit f546b1
{
Packit f546b1
  GstFormatDefinition *standards = standard_definitions;
Packit f546b1
Packit f546b1
  g_mutex_lock (&mutex);
Packit f546b1
  if (_nick_to_format == NULL) {
Packit f546b1
    _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
Packit f546b1
    _format_to_nick = g_hash_table_new (NULL, NULL);
Packit f546b1
  }
Packit f546b1
Packit f546b1
  while (standards->nick) {
Packit f546b1
    standards->quark = g_quark_from_static_string (standards->nick);
Packit f546b1
    g_hash_table_insert (_nick_to_format, (gpointer) standards->nick,
Packit f546b1
        standards);
Packit f546b1
    g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
Packit f546b1
        standards);
Packit f546b1
Packit f546b1
    _gst_formats = g_list_append (_gst_formats, standards);
Packit f546b1
    standards++;
Packit f546b1
    _n_values++;
Packit f546b1
  }
Packit f546b1
  /* getting the type registers the enum */
Packit f546b1
  g_type_class_ref (gst_format_get_type ());
Packit f546b1
  g_mutex_unlock (&mutex);
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_format_get_name:
Packit f546b1
 * @format: a #GstFormat
Packit f546b1
 *
Packit f546b1
 * Get a printable name for the given format. Do not modify or free.
Packit f546b1
 *
Packit f546b1
 * Returns: (nullable): a reference to the static name of the format
Packit f546b1
 * or %NULL if the format is unknown.
Packit f546b1
 */
Packit f546b1
const gchar *
Packit f546b1
gst_format_get_name (GstFormat format)
Packit f546b1
{
Packit f546b1
  const GstFormatDefinition *def;
Packit f546b1
  const gchar *result;
Packit f546b1
Packit f546b1
  if ((def = gst_format_get_details (format)) != NULL)
Packit f546b1
    result = def->nick;
Packit f546b1
  else
Packit f546b1
    result = NULL;
Packit f546b1
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_format_to_quark:
Packit f546b1
 * @format: a #GstFormat
Packit f546b1
 *
Packit f546b1
 * Get the unique quark for the given format.
Packit f546b1
 *
Packit f546b1
 * Returns: the quark associated with the format or 0 if the format
Packit f546b1
 * is unknown.
Packit f546b1
 */
Packit f546b1
GQuark
Packit f546b1
gst_format_to_quark (GstFormat format)
Packit f546b1
{
Packit f546b1
  const GstFormatDefinition *def;
Packit f546b1
  GQuark result;
Packit f546b1
Packit f546b1
  if ((def = gst_format_get_details (format)) != NULL)
Packit f546b1
    result = def->quark;
Packit f546b1
  else
Packit f546b1
    result = 0;
Packit f546b1
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_format_register:
Packit f546b1
 * @nick: The nick of the new format
Packit f546b1
 * @description: The description of the new format
Packit f546b1
 *
Packit f546b1
 * Create a new GstFormat based on the nick or return an
Packit f546b1
 * already registered format with that nick.
Packit f546b1
 *
Packit f546b1
 * Returns: A new GstFormat or an already registered format
Packit f546b1
 * with the same nick.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
GstFormat
Packit f546b1
gst_format_register (const gchar * nick, const gchar * description)
Packit f546b1
{
Packit f546b1
  GstFormatDefinition *format;
Packit f546b1
  GstFormat query;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (nick != NULL, GST_FORMAT_UNDEFINED);
Packit f546b1
  g_return_val_if_fail (description != NULL, GST_FORMAT_UNDEFINED);
Packit f546b1
Packit f546b1
  query = gst_format_get_by_nick (nick);
Packit f546b1
  if (query != GST_FORMAT_UNDEFINED)
Packit f546b1
    return query;
Packit f546b1
Packit f546b1
  g_mutex_lock (&mutex);
Packit f546b1
  format = g_slice_new (GstFormatDefinition);
Packit f546b1
  format->value = (GstFormat) _n_values;
Packit f546b1
  format->nick = g_strdup (nick);
Packit f546b1
  format->description = g_strdup (description);
Packit f546b1
  format->quark = g_quark_from_static_string (format->nick);
Packit f546b1
Packit f546b1
  g_hash_table_insert (_nick_to_format, (gpointer) format->nick, format);
Packit f546b1
  g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
Packit f546b1
      format);
Packit f546b1
  _gst_formats = g_list_append (_gst_formats, format);
Packit f546b1
  _n_values++;
Packit f546b1
  g_mutex_unlock (&mutex);
Packit f546b1
Packit f546b1
  return format->value;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_format_get_by_nick:
Packit f546b1
 * @nick: The nick of the format
Packit f546b1
 *
Packit f546b1
 * Return the format registered with the given nick.
Packit f546b1
 *
Packit f546b1
 * Returns: The format with @nick or GST_FORMAT_UNDEFINED
Packit f546b1
 * if the format was not registered.
Packit f546b1
 */
Packit f546b1
GstFormat
Packit f546b1
gst_format_get_by_nick (const gchar * nick)
Packit f546b1
{
Packit f546b1
  GstFormatDefinition *format;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (nick != NULL, GST_FORMAT_UNDEFINED);
Packit f546b1
Packit f546b1
  g_mutex_lock (&mutex);
Packit f546b1
  format = g_hash_table_lookup (_nick_to_format, nick);
Packit f546b1
  g_mutex_unlock (&mutex);
Packit f546b1
Packit f546b1
  if (format != NULL)
Packit f546b1
    return format->value;
Packit f546b1
  else
Packit f546b1
    return GST_FORMAT_UNDEFINED;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_formats_contains:
Packit f546b1
 * @formats: (array zero-terminated=1): The format array to search
Packit f546b1
 * @format: the format to find
Packit f546b1
 *
Packit f546b1
 * See if the given format is inside the format array.
Packit f546b1
 *
Packit f546b1
 * Returns: %TRUE if the format is found inside the array
Packit f546b1
 */
Packit f546b1
gboolean
Packit f546b1
gst_formats_contains (const GstFormat * formats, GstFormat format)
Packit f546b1
{
Packit f546b1
  if (!formats)
Packit f546b1
    return FALSE;
Packit f546b1
Packit f546b1
  while (*formats) {
Packit f546b1
    if (*formats == format)
Packit f546b1
      return TRUE;
Packit f546b1
Packit f546b1
    formats++;
Packit f546b1
  }
Packit f546b1
  return FALSE;
Packit f546b1
}
Packit f546b1
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_format_get_details:
Packit f546b1
 * @format: The format to get details of
Packit f546b1
 *
Packit f546b1
 * Get details about the given format.
Packit f546b1
 *
Packit f546b1
 * Returns: (nullable): The #GstFormatDefinition for @format or %NULL
Packit f546b1
 * on failure.
Packit f546b1
 *
Packit f546b1
 * MT safe.
Packit f546b1
 */
Packit f546b1
const GstFormatDefinition *
Packit f546b1
gst_format_get_details (GstFormat format)
Packit f546b1
{
Packit f546b1
  const GstFormatDefinition *result;
Packit f546b1
Packit f546b1
  g_mutex_lock (&mutex);
Packit f546b1
  result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
Packit f546b1
  g_mutex_unlock (&mutex);
Packit f546b1
Packit f546b1
  return result;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_format_iterate_definitions:
Packit f546b1
 *
Packit f546b1
 * Iterate all the registered formats. The format definition is read
Packit f546b1
 * only.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full): a GstIterator of #GstFormatDefinition.
Packit f546b1
 */
Packit f546b1
GstIterator *
Packit f546b1
gst_format_iterate_definitions (void)
Packit f546b1
{
Packit f546b1
  GstIterator *result;
Packit f546b1
Packit f546b1
  g_mutex_lock (&mutex);
Packit f546b1
  /* FIXME: register a boxed type for GstFormatDefinition */
Packit f546b1
  result = gst_iterator_new_list (G_TYPE_POINTER,
Packit f546b1
      &mutex, &_n_values, &_gst_formats, NULL, NULL);
Packit f546b1
  g_mutex_unlock (&mutex);
Packit f546b1
Packit f546b1
  return result;
Packit f546b1
}