video: Rename gst_video_codec_frame_set_hook() to gst_video_codec_frame_set_user_data()
[platform/upstream/gstreamer.git] / gst-libs / gst / video / gstvideoutils.c
1 /* GStreamer
2  * Copyright (C) 2008 David Schleef <ds@schleef.org>
3  * Copyright (C) 2012 Collabora Ltd.
4  *      Author : Edward Hervey <edward@collabora.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstvideoutils.h"
27
28 #include <string.h>
29
30 G_DEFINE_BOXED_TYPE (GstVideoCodecFrame, gst_video_codec_frame,
31     (GBoxedCopyFunc) gst_video_codec_frame_ref,
32     (GBoxedFreeFunc) gst_video_codec_frame_unref);
33
34 static void
35 _gst_video_codec_frame_free (GstVideoCodecFrame * frame)
36 {
37   g_return_if_fail (frame != NULL);
38
39   GST_DEBUG ("free frame %p", frame);
40
41   if (frame->input_buffer) {
42     gst_buffer_unref (frame->input_buffer);
43   }
44
45   if (frame->output_buffer) {
46     gst_buffer_unref (frame->output_buffer);
47   }
48
49   g_list_foreach (frame->events, (GFunc) gst_event_unref, NULL);
50   g_list_free (frame->events);
51
52   if (frame->user_data_destroy_notify)
53     frame->user_data_destroy_notify (frame->user_data);
54
55   g_slice_free (GstVideoCodecFrame, frame);
56 }
57
58 /**
59  * gst_video_codec_frame_set_user_data:
60  * @frame: a #GstVideoCodecFrame
61  * @user_data: private data
62  * @notify: (closure user_data): a #GDestroyNotify
63  *
64  * Sets @user_data on the frame and the #GDestroyNotify that will be called when
65  * the frame is freed. Allows to attach private data by the subclass to frames.
66  *
67  * If a @user_data was previously set, then the previous set @notify will be called
68  * before the @user_data is replaced.
69  */
70 void
71 gst_video_codec_frame_set_user_data (GstVideoCodecFrame * frame,
72     gpointer user_data, GDestroyNotify notify)
73 {
74   if (frame->user_data_destroy_notify)
75     frame->user_data_destroy_notify (frame->user_data);
76
77   frame->user_data = user_data;
78   frame->user_data_destroy_notify = notify;
79 }
80
81 /**
82  * gst_video_codec_frame_get_user_data:
83  * @frame: a #GstVideoCodecFrame
84  *
85  * Gets private data set on the frame by the subclass via
86  * gst_video_codec_frame_set_user_data() previously.
87  *
88  * Returns: (transfer none): The previously set user_data
89  */
90 gpointer
91 gst_video_codec_frame_get_user_data (GstVideoCodecFrame * frame)
92 {
93   return frame->user_data;
94 }
95
96 /**
97  * gst_video_codec_frame_ref:
98  * @frame: a #GstVideoCodecFrame
99  *
100  * Increases the refcount of the given frame by one.
101  *
102  * Returns: @buf
103  */
104 GstVideoCodecFrame *
105 gst_video_codec_frame_ref (GstVideoCodecFrame * frame)
106 {
107   g_return_val_if_fail (frame != NULL, NULL);
108
109   g_atomic_int_inc (&frame->ref_count);
110
111   return frame;
112 }
113
114 /**
115  * gst_video_codec_frame_unref:
116  * @frame: a #GstVideoCodecFrame
117  *
118  * Decreases the refcount of the frame. If the refcount reaches 0, the frame
119  * will be freed.
120  */
121 void
122 gst_video_codec_frame_unref (GstVideoCodecFrame * frame)
123 {
124   g_return_if_fail (frame != NULL);
125   g_return_if_fail (frame->ref_count > 0);
126
127   if (g_atomic_int_dec_and_test (&frame->ref_count)) {
128     _gst_video_codec_frame_free (frame);
129   }
130 }
131
132
133 /**
134  * gst_video_codec_state_ref:
135  * @state: a #GstVideoCodecState
136  *
137  * Increases the refcount of the given state by one.
138  *
139  * Returns: @buf
140  */
141 GstVideoCodecState *
142 gst_video_codec_state_ref (GstVideoCodecState * state)
143 {
144   g_return_val_if_fail (state != NULL, NULL);
145
146   g_atomic_int_inc (&state->ref_count);
147
148   return state;
149 }
150
151 static void
152 _gst_video_codec_state_free (GstVideoCodecState * state)
153 {
154   if (state->caps)
155     gst_caps_unref (state->caps);
156   if (state->codec_data)
157     gst_buffer_unref (state->codec_data);
158 }
159
160 /**
161  * gst_video_codec_state_unref:
162  * @state: a #GstVideoCodecState
163  *
164  * Decreases the refcount of the state. If the refcount reaches 0, the state
165  * will be freed.
166  */
167 void
168 gst_video_codec_state_unref (GstVideoCodecState * state)
169 {
170   g_return_if_fail (state != NULL);
171   g_return_if_fail (state->ref_count > 0);
172
173   if (g_atomic_int_dec_and_test (&state->ref_count)) {
174     _gst_video_codec_state_free (state);
175   }
176 }
177
178 G_DEFINE_BOXED_TYPE (GstVideoCodecState, gst_video_codec_state,
179     (GBoxedCopyFunc) gst_video_codec_state_ref,
180     (GBoxedFreeFunc) gst_video_codec_state_unref);