Blame gst-libs/gst/video/gstvideoutils.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) 2008 David Schleef <ds@schleef.org>
Packit 971217
 * Copyright (C) 2012 Collabora Ltd.
Packit 971217
 *	Author : Edward Hervey <edward@collabora.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/video/video.h>
Packit 971217
#include "gstvideoutils.h"
Packit 971217
Packit 971217
#include <string.h>
Packit 971217
Packit 971217
G_DEFINE_BOXED_TYPE (GstVideoCodecFrame, gst_video_codec_frame,
Packit 971217
    (GBoxedCopyFunc) gst_video_codec_frame_ref,
Packit 971217
    (GBoxedFreeFunc) gst_video_codec_frame_unref);
Packit 971217
Packit 971217
static void
Packit 971217
_gst_video_codec_frame_free (GstVideoCodecFrame * frame)
Packit 971217
{
Packit 971217
  g_return_if_fail (frame != NULL);
Packit 971217
Packit 971217
  GST_DEBUG ("free frame %p", frame);
Packit 971217
Packit 971217
  if (frame->input_buffer) {
Packit 971217
    gst_buffer_unref (frame->input_buffer);
Packit 971217
  }
Packit 971217
Packit 971217
  if (frame->output_buffer) {
Packit 971217
    gst_buffer_unref (frame->output_buffer);
Packit 971217
  }
Packit 971217
Packit 971217
  g_list_free_full (frame->events, (GDestroyNotify) gst_event_unref);
Packit 971217
  frame->events = NULL;
Packit 971217
Packit 971217
  if (frame->user_data_destroy_notify)
Packit 971217
    frame->user_data_destroy_notify (frame->user_data);
Packit 971217
Packit 971217
  g_slice_free (GstVideoCodecFrame, frame);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_codec_frame_set_user_data:
Packit 971217
 * @frame: a #GstVideoCodecFrame
Packit 971217
 * @user_data: private data
Packit 971217
 * @notify: (closure user_data): a #GDestroyNotify
Packit 971217
 *
Packit 971217
 * Sets @user_data on the frame and the #GDestroyNotify that will be called when
Packit 971217
 * the frame is freed. Allows to attach private data by the subclass to frames.
Packit 971217
 *
Packit 971217
 * If a @user_data was previously set, then the previous set @notify will be called
Packit 971217
 * before the @user_data is replaced.
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_video_codec_frame_set_user_data (GstVideoCodecFrame * frame,
Packit 971217
    gpointer user_data, GDestroyNotify notify)
Packit 971217
{
Packit 971217
  if (frame->user_data_destroy_notify)
Packit 971217
    frame->user_data_destroy_notify (frame->user_data);
Packit 971217
Packit 971217
  frame->user_data = user_data;
Packit 971217
  frame->user_data_destroy_notify = notify;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_codec_frame_get_user_data:
Packit 971217
 * @frame: a #GstVideoCodecFrame
Packit 971217
 *
Packit 971217
 * Gets private data set on the frame by the subclass via
Packit 971217
 * gst_video_codec_frame_set_user_data() previously.
Packit 971217
 *
Packit 971217
 * Returns: (transfer none): The previously set user_data
Packit 971217
 */
Packit 971217
gpointer
Packit 971217
gst_video_codec_frame_get_user_data (GstVideoCodecFrame * frame)
Packit 971217
{
Packit 971217
  return frame->user_data;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_codec_frame_ref:
Packit 971217
 * @frame: a #GstVideoCodecFrame
Packit 971217
 *
Packit 971217
 * Increases the refcount of the given frame by one.
Packit 971217
 *
Packit 971217
 * Returns: @buf
Packit 971217
 */
Packit 971217
GstVideoCodecFrame *
Packit 971217
gst_video_codec_frame_ref (GstVideoCodecFrame * frame)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (frame != NULL, NULL);
Packit 971217
Packit 971217
  GST_TRACE ("%p ref %d->%d", frame, frame->ref_count, frame->ref_count + 1);
Packit 971217
Packit 971217
  g_atomic_int_inc (&frame->ref_count);
Packit 971217
Packit 971217
  return frame;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_codec_frame_unref:
Packit 971217
 * @frame: a #GstVideoCodecFrame
Packit 971217
 *
Packit 971217
 * Decreases the refcount of the frame. If the refcount reaches 0, the frame
Packit 971217
 * will be freed.
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_video_codec_frame_unref (GstVideoCodecFrame * frame)
Packit 971217
{
Packit 971217
  g_return_if_fail (frame != NULL);
Packit 971217
  g_return_if_fail (frame->ref_count > 0);
Packit 971217
Packit 971217
  GST_TRACE ("%p unref %d->%d", frame, frame->ref_count, frame->ref_count - 1);
Packit 971217
Packit 971217
  if (g_atomic_int_dec_and_test (&frame->ref_count)) {
Packit 971217
    _gst_video_codec_frame_free (frame);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_codec_state_ref:
Packit 971217
 * @state: a #GstVideoCodecState
Packit 971217
 *
Packit 971217
 * Increases the refcount of the given state by one.
Packit 971217
 *
Packit 971217
 * Returns: @buf
Packit 971217
 */
Packit 971217
GstVideoCodecState *
Packit 971217
gst_video_codec_state_ref (GstVideoCodecState * state)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (state != NULL, NULL);
Packit 971217
Packit 971217
  GST_TRACE ("%p ref %d->%d", state, state->ref_count, state->ref_count + 1);
Packit 971217
Packit 971217
  g_atomic_int_inc (&state->ref_count);
Packit 971217
Packit 971217
  return state;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_gst_video_codec_state_free (GstVideoCodecState * state)
Packit 971217
{
Packit 971217
  GST_DEBUG ("free state %p", state);
Packit 971217
Packit 971217
  if (state->caps)
Packit 971217
    gst_caps_unref (state->caps);
Packit 971217
  if (state->allocation_caps)
Packit 971217
    gst_caps_unref (state->allocation_caps);
Packit 971217
  if (state->codec_data)
Packit 971217
    gst_buffer_unref (state->codec_data);
Packit 971217
  g_slice_free (GstVideoCodecState, state);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_codec_state_unref:
Packit 971217
 * @state: a #GstVideoCodecState
Packit 971217
 *
Packit 971217
 * Decreases the refcount of the state. If the refcount reaches 0, the state
Packit 971217
 * will be freed.
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_video_codec_state_unref (GstVideoCodecState * state)
Packit 971217
{
Packit 971217
  g_return_if_fail (state != NULL);
Packit 971217
  g_return_if_fail (state->ref_count > 0);
Packit 971217
Packit 971217
  GST_TRACE ("%p unref %d->%d", state, state->ref_count, state->ref_count - 1);
Packit 971217
Packit 971217
  if (g_atomic_int_dec_and_test (&state->ref_count)) {
Packit 971217
    _gst_video_codec_state_free (state);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
G_DEFINE_BOXED_TYPE (GstVideoCodecState, gst_video_codec_state,
Packit 971217
    (GBoxedCopyFunc) gst_video_codec_state_ref,
Packit 971217
    (GBoxedFreeFunc) gst_video_codec_state_unref);