summaryrefslogtreecommitdiff
path: root/gst-libs/gst/video/gstvideoutils.c
blob: 03dffcd8fcf741dfb1a4436ab81c0b8380bab04c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* GStreamer
 * Copyright (C) 2008 David Schleef <ds@schleef.org>
 * Copyright (C) 2012 Collabora Ltd.
 *	Author : Edward Hervey <edward@collabora.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/video/video.h>
#include "gstvideoutils.h"

/**
 * SECTION:gstvideoutils
 * @title: GstVideo Codec utility function
 * @short_description: Extra utility functions for video codecs
 */

#include <string.h>

G_DEFINE_BOXED_TYPE (GstVideoCodecFrame, gst_video_codec_frame,
    (GBoxedCopyFunc) gst_video_codec_frame_ref,
    (GBoxedFreeFunc) gst_video_codec_frame_unref);

static void
_gst_video_codec_frame_free (GstVideoCodecFrame * frame)
{
  g_return_if_fail (frame != NULL);

  GST_DEBUG ("free frame %p", frame);

  if (frame->input_buffer) {
    gst_buffer_unref (frame->input_buffer);
  }

  if (frame->output_buffer) {
    gst_buffer_unref (frame->output_buffer);
  }

  g_list_free_full (frame->events, (GDestroyNotify) gst_event_unref);
  frame->events = NULL;

  if (frame->user_data_destroy_notify)
    frame->user_data_destroy_notify (frame->user_data);

  g_slice_free (GstVideoCodecFrame, frame);
}

/**
 * gst_video_codec_frame_set_user_data:
 * @frame: a #GstVideoCodecFrame
 * @user_data: private data
 * @notify: (closure user_data): a #GDestroyNotify
 *
 * Sets @user_data on the frame and the #GDestroyNotify that will be called when
 * the frame is freed. Allows to attach private data by the subclass to frames.
 *
 * If a @user_data was previously set, then the previous set @notify will be called
 * before the @user_data is replaced.
 */
void
gst_video_codec_frame_set_user_data (GstVideoCodecFrame * frame,
    gpointer user_data, GDestroyNotify notify)
{
  if (frame->user_data_destroy_notify)
    frame->user_data_destroy_notify (frame->user_data);

  frame->user_data = user_data;
  frame->user_data_destroy_notify = notify;
}

/**
 * gst_video_codec_frame_get_user_data:
 * @frame: a #GstVideoCodecFrame
 *
 * Gets private data set on the frame by the subclass via
 * gst_video_codec_frame_set_user_data() previously.
 *
 * Returns: (transfer none): The previously set user_data
 */
gpointer
gst_video_codec_frame_get_user_data (GstVideoCodecFrame * frame)
{
  return frame->user_data;
}

/**
 * gst_video_codec_frame_ref:
 * @frame: a #GstVideoCodecFrame
 *
 * Increases the refcount of the given frame by one.
 *
 * Returns: @buf
 */
GstVideoCodecFrame *
gst_video_codec_frame_ref (GstVideoCodecFrame * frame)
{
  g_return_val_if_fail (frame != NULL, NULL);

  GST_TRACE ("%p ref %d->%d", frame, frame->ref_count, frame->ref_count + 1);

  g_atomic_int_inc (&frame->ref_count);

  return frame;
}

/**
 * gst_video_codec_frame_unref:
 * @frame: a #GstVideoCodecFrame
 *
 * Decreases the refcount of the frame. If the refcount reaches 0, the frame
 * will be freed.
 */
void
gst_video_codec_frame_unref (GstVideoCodecFrame * frame)
{
  g_return_if_fail (frame != NULL);
  g_return_if_fail (frame->ref_count > 0);

  GST_TRACE ("%p unref %d->%d", frame, frame->ref_count, frame->ref_count - 1);

  if (g_atomic_int_dec_and_test (&frame->ref_count)) {
    _gst_video_codec_frame_free (frame);
  }
}


/**
 * gst_video_codec_state_ref:
 * @state: a #GstVideoCodecState
 *
 * Increases the refcount of the given state by one.
 *
 * Returns: @buf
 */
GstVideoCodecState *
gst_video_codec_state_ref (GstVideoCodecState * state)
{
  g_return_val_if_fail (state != NULL, NULL);

  GST_TRACE ("%p ref %d->%d", state, state->ref_count, state->ref_count + 1);

  g_atomic_int_inc (&state->ref_count);

  return state;
}

static void
_gst_video_codec_state_free (GstVideoCodecState * state)
{
  GST_DEBUG ("free state %p", state);

  if (state->caps)
    gst_caps_unref (state->caps);
  if (state->allocation_caps)
    gst_caps_unref (state->allocation_caps);
  if (state->codec_data)
    gst_buffer_unref (state->codec_data);
  if (state->mastering_display_info)
    g_slice_free (GstVideoMasteringDisplayInfo, state->mastering_display_info);
  if (state->content_light_level)
    g_slice_free (GstVideoContentLightLevel, state->content_light_level);
  g_slice_free (GstVideoCodecState, state);
}

/**
 * gst_video_codec_state_unref:
 * @state: a #GstVideoCodecState
 *
 * Decreases the refcount of the state. If the refcount reaches 0, the state
 * will be freed.
 */
void
gst_video_codec_state_unref (GstVideoCodecState * state)
{
  g_return_if_fail (state != NULL);
  g_return_if_fail (state->ref_count > 0);

  GST_TRACE ("%p unref %d->%d", state, state->ref_count, state->ref_count - 1);

  if (g_atomic_int_dec_and_test (&state->ref_count)) {
    _gst_video_codec_state_free (state);
  }
}

G_DEFINE_BOXED_TYPE (GstVideoCodecState, gst_video_codec_state,
    (GBoxedCopyFunc) gst_video_codec_state_ref,
    (GBoxedFreeFunc) gst_video_codec_state_unref);