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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/video/video.h>
27 #include "gstvideoutils.h"
28 
29 /**
30  * SECTION:gstvideoutils
31  * @title: GstVideo Codec utility function
32  * @short_description: Extra utility functions for video codecs
33  */
34 
35 #include <string.h>
36 
37 G_DEFINE_BOXED_TYPE (GstVideoCodecFrame, gst_video_codec_frame,
38     (GBoxedCopyFunc) gst_video_codec_frame_ref,
39     (GBoxedFreeFunc) gst_video_codec_frame_unref);
40 
41 static void
_gst_video_codec_frame_free(GstVideoCodecFrame * frame)42 _gst_video_codec_frame_free (GstVideoCodecFrame * frame)
43 {
44   g_return_if_fail (frame != NULL);
45 
46   GST_DEBUG ("free frame %p", frame);
47 
48   if (frame->input_buffer) {
49     gst_buffer_unref (frame->input_buffer);
50   }
51 
52   if (frame->output_buffer) {
53     gst_buffer_unref (frame->output_buffer);
54   }
55 
56   g_list_free_full (frame->events, (GDestroyNotify) gst_event_unref);
57   frame->events = NULL;
58 
59   if (frame->user_data_destroy_notify)
60     frame->user_data_destroy_notify (frame->user_data);
61 
62   g_slice_free (GstVideoCodecFrame, frame);
63 }
64 
65 /**
66  * gst_video_codec_frame_set_user_data:
67  * @frame: a #GstVideoCodecFrame
68  * @user_data: private data
69  * @notify: (closure user_data): a #GDestroyNotify
70  *
71  * Sets @user_data on the frame and the #GDestroyNotify that will be called when
72  * the frame is freed. Allows to attach private data by the subclass to frames.
73  *
74  * If a @user_data was previously set, then the previous set @notify will be called
75  * before the @user_data is replaced.
76  */
77 void
gst_video_codec_frame_set_user_data(GstVideoCodecFrame * frame,gpointer user_data,GDestroyNotify notify)78 gst_video_codec_frame_set_user_data (GstVideoCodecFrame * frame,
79     gpointer user_data, GDestroyNotify notify)
80 {
81   if (frame->user_data_destroy_notify)
82     frame->user_data_destroy_notify (frame->user_data);
83 
84   frame->user_data = user_data;
85   frame->user_data_destroy_notify = notify;
86 }
87 
88 /**
89  * gst_video_codec_frame_get_user_data:
90  * @frame: a #GstVideoCodecFrame
91  *
92  * Gets private data set on the frame by the subclass via
93  * gst_video_codec_frame_set_user_data() previously.
94  *
95  * Returns: (transfer none): The previously set user_data
96  */
97 gpointer
gst_video_codec_frame_get_user_data(GstVideoCodecFrame * frame)98 gst_video_codec_frame_get_user_data (GstVideoCodecFrame * frame)
99 {
100   return frame->user_data;
101 }
102 
103 /**
104  * gst_video_codec_frame_ref:
105  * @frame: a #GstVideoCodecFrame
106  *
107  * Increases the refcount of the given frame by one.
108  *
109  * Returns: @buf
110  */
111 GstVideoCodecFrame *
gst_video_codec_frame_ref(GstVideoCodecFrame * frame)112 gst_video_codec_frame_ref (GstVideoCodecFrame * frame)
113 {
114   g_return_val_if_fail (frame != NULL, NULL);
115 
116   GST_TRACE ("%p ref %d->%d", frame, frame->ref_count, frame->ref_count + 1);
117 
118   g_atomic_int_inc (&frame->ref_count);
119 
120   return frame;
121 }
122 
123 /**
124  * gst_video_codec_frame_unref:
125  * @frame: a #GstVideoCodecFrame
126  *
127  * Decreases the refcount of the frame. If the refcount reaches 0, the frame
128  * will be freed.
129  */
130 void
gst_video_codec_frame_unref(GstVideoCodecFrame * frame)131 gst_video_codec_frame_unref (GstVideoCodecFrame * frame)
132 {
133   g_return_if_fail (frame != NULL);
134   g_return_if_fail (frame->ref_count > 0);
135 
136   GST_TRACE ("%p unref %d->%d", frame, frame->ref_count, frame->ref_count - 1);
137 
138   if (g_atomic_int_dec_and_test (&frame->ref_count)) {
139     _gst_video_codec_frame_free (frame);
140   }
141 }
142 
143 
144 /**
145  * gst_video_codec_state_ref:
146  * @state: a #GstVideoCodecState
147  *
148  * Increases the refcount of the given state by one.
149  *
150  * Returns: @buf
151  */
152 GstVideoCodecState *
gst_video_codec_state_ref(GstVideoCodecState * state)153 gst_video_codec_state_ref (GstVideoCodecState * state)
154 {
155   g_return_val_if_fail (state != NULL, NULL);
156 
157   GST_TRACE ("%p ref %d->%d", state, state->ref_count, state->ref_count + 1);
158 
159   g_atomic_int_inc (&state->ref_count);
160 
161   return state;
162 }
163 
164 static void
_gst_video_codec_state_free(GstVideoCodecState * state)165 _gst_video_codec_state_free (GstVideoCodecState * state)
166 {
167   GST_DEBUG ("free state %p", state);
168 
169   if (state->caps)
170     gst_caps_unref (state->caps);
171   if (state->allocation_caps)
172     gst_caps_unref (state->allocation_caps);
173   if (state->codec_data)
174     gst_buffer_unref (state->codec_data);
175   g_slice_free (GstVideoCodecState, state);
176 }
177 
178 /**
179  * gst_video_codec_state_unref:
180  * @state: a #GstVideoCodecState
181  *
182  * Decreases the refcount of the state. If the refcount reaches 0, the state
183  * will be freed.
184  */
185 void
gst_video_codec_state_unref(GstVideoCodecState * state)186 gst_video_codec_state_unref (GstVideoCodecState * state)
187 {
188   g_return_if_fail (state != NULL);
189   g_return_if_fail (state->ref_count > 0);
190 
191   GST_TRACE ("%p unref %d->%d", state, state->ref_count, state->ref_count - 1);
192 
193   if (g_atomic_int_dec_and_test (&state->ref_count)) {
194     _gst_video_codec_state_free (state);
195   }
196 }
197 
198 G_DEFINE_BOXED_TYPE (GstVideoCodecState, gst_video_codec_state,
199     (GBoxedCopyFunc) gst_video_codec_state_ref,
200     (GBoxedFreeFunc) gst_video_codec_state_unref);
201