add buildrequire of gl
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapicodedbufferproxy.c
1 /*
2  *  gstvaapicodedbufferproxy.c - VA coded buffer proxy
3  *
4  *  Copyright (C) 2013 Intel Corporation
5  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22
23 #include "sysdeps.h"
24 #include "gstvaapicodedbufferproxy.h"
25 #include "gstvaapicodedbufferproxy_priv.h"
26 #include "gstvaapivideopool_priv.h"
27
28 #define DEBUG 1
29 #include "gstvaapidebug.h"
30
31 static void
32 coded_buffer_proxy_set_user_data (GstVaapiCodedBufferProxy * proxy,
33     gpointer user_data, GDestroyNotify destroy_func)
34 {
35   if (proxy->user_data_destroy)
36     proxy->user_data_destroy (proxy->user_data);
37
38   proxy->user_data = user_data;
39   proxy->user_data_destroy = destroy_func;
40 }
41
42 static void
43 coded_buffer_proxy_finalize (GstVaapiCodedBufferProxy * proxy)
44 {
45   if (proxy->buffer) {
46     if (proxy->pool)
47       gst_vaapi_video_pool_put_object (proxy->pool, proxy->buffer);
48     gst_vaapi_object_unref (proxy->buffer);
49     proxy->buffer = NULL;
50   }
51   gst_vaapi_video_pool_replace (&proxy->pool, NULL);
52   coded_buffer_proxy_set_user_data (proxy, NULL, NULL);
53
54   /* Notify the user function that the object is now destroyed */
55   if (proxy->destroy_func)
56     proxy->destroy_func (proxy->destroy_data);
57 }
58
59 static inline const GstVaapiMiniObjectClass *
60 gst_vaapi_coded_buffer_proxy_class (void)
61 {
62   static const GstVaapiMiniObjectClass GstVaapiCodedBufferProxyClass = {
63     sizeof (GstVaapiCodedBufferProxy),
64     (GDestroyNotify)coded_buffer_proxy_finalize
65   };
66   return &GstVaapiCodedBufferProxyClass;
67 }
68
69 /**
70  * gst_vaapi_coded_buffer_proxy_new_from_pool:
71  * @pool: a #GstVaapiCodedBufferPool
72  *
73  * Allocates a new coded buffer from the supplied @pool and creates
74  * the wrapped coded buffer proxy object from it. When the last
75  * reference to the proxy object is released, then the underlying VA
76  * coded buffer is pushed back to its parent pool.
77  *
78  * Returns: The same newly allocated @proxy object, or %NULL on error
79  */
80 GstVaapiCodedBufferProxy *
81 gst_vaapi_coded_buffer_proxy_new_from_pool (GstVaapiCodedBufferPool * pool)
82 {
83   GstVaapiCodedBufferProxy *proxy;
84
85   g_return_val_if_fail (pool != NULL, NULL);
86   g_return_val_if_fail (GST_VAAPI_VIDEO_POOL (pool)->object_type ==
87       GST_VAAPI_VIDEO_POOL_OBJECT_TYPE_CODED_BUFFER, NULL);
88
89   proxy = (GstVaapiCodedBufferProxy *)
90       gst_vaapi_mini_object_new (gst_vaapi_coded_buffer_proxy_class ());
91   if (!proxy)
92     return NULL;
93
94   proxy->destroy_func = NULL;
95   proxy->user_data_destroy = NULL;
96   proxy->pool = gst_vaapi_video_pool_ref (pool);
97   proxy->buffer = gst_vaapi_video_pool_get_object (proxy->pool);
98   if (!proxy->buffer)
99     goto error;
100   gst_vaapi_object_ref (proxy->buffer);
101   return proxy;
102
103 error:
104   gst_vaapi_coded_buffer_proxy_unref (proxy);
105   return NULL;
106 }
107
108 /**
109  * gst_vaapi_coded_buffer_proxy_ref:
110  * @proxy: a #GstVaapiCodedBufferProxy
111  *
112  * Atomically increases the reference count of the given @proxy by one.
113  *
114  * Returns: The same @proxy argument
115  */
116 GstVaapiCodedBufferProxy *
117 gst_vaapi_coded_buffer_proxy_ref (GstVaapiCodedBufferProxy * proxy)
118 {
119   g_return_val_if_fail (proxy != NULL, NULL);
120
121   return GST_VAAPI_CODED_BUFFER_PROXY (gst_vaapi_mini_object_ref
122       (GST_VAAPI_MINI_OBJECT (proxy)));
123 }
124
125 /**
126  * gst_vaapi_coded_buffer_proxy_unref:
127  * @proxy: a #GstVaapiCodedBufferProxy
128  *
129  * Atomically decreases the reference count of the @proxy by one. If
130  * the reference count reaches zero, the object will be free'd.
131  */
132 void
133 gst_vaapi_coded_buffer_proxy_unref (GstVaapiCodedBufferProxy * proxy)
134 {
135   g_return_if_fail (proxy != NULL);
136
137   gst_vaapi_mini_object_unref (GST_VAAPI_MINI_OBJECT (proxy));
138 }
139
140 /**
141  * gst_vaapi_coded_buffer_proxy_replace:
142  * @old_proxy_ptr: a pointer to a #GstVaapiCodedBufferProxy
143  * @new_proxy: a #GstVaapiCodedBufferProxy
144  *
145  * Atomically replaces the proxy object held in @old_proxy_ptr with
146  * @new_proxy. This means that @old_proxy_ptr shall reference a valid
147  * object. However, @new_proxy can be NULL.
148  */
149 void
150 gst_vaapi_coded_buffer_proxy_replace (GstVaapiCodedBufferProxy ** old_proxy_ptr,
151     GstVaapiCodedBufferProxy * new_proxy)
152 {
153   g_return_if_fail (old_proxy_ptr != NULL);
154
155   gst_vaapi_mini_object_replace ((GstVaapiMiniObject **) old_proxy_ptr,
156       GST_VAAPI_MINI_OBJECT (new_proxy));
157 }
158
159 /**
160  * gst_vaapi_coded_buffer_proxy_get_buffer:
161  * @proxy: a #GstVaapiCodedBufferProxy
162  *
163  * Returns the #GstVaapiCodedBuffer stored in the @proxy.
164  *
165  * Return value: the #GstVaapiCodedBuffer, or %NULL if an error occurred
166  */
167 GstVaapiCodedBuffer *
168 gst_vaapi_coded_buffer_proxy_get_buffer (GstVaapiCodedBufferProxy * proxy)
169 {
170   g_return_val_if_fail (proxy != NULL, NULL);
171
172   return GST_VAAPI_CODED_BUFFER_PROXY_BUFFER (proxy);
173 }
174
175 /**
176  * gst_vaapi_coded_buffer_proxy_get_buffer_size:
177  * @proxy: a #GstVaapiCodedBufferProxy
178  *
179  * Returns the size of the underlying #GstVaapiCodedBuffer object
180  * stored in the @proxy.
181  *
182  * Return value: the underlying #GstVaapiCodedBuffer size, or -1 if an
183  *   error occurred
184  */
185 gssize
186 gst_vaapi_coded_buffer_proxy_get_buffer_size (GstVaapiCodedBufferProxy * proxy)
187 {
188   g_return_val_if_fail (proxy != NULL, -1);
189
190   return GST_VAAPI_CODED_BUFFER_PROXY_BUFFER_SIZE (proxy);
191 }
192
193 /**
194  * gst_vaapi_coded_buffer_proxy_set_destroy_notify:
195  * @proxy: a @GstVaapiCodedBufferProxy
196  * @destroy_func: a #GDestroyNotify function
197  * @user_data: some extra data to pass to the @destroy_func function
198  *
199  * Sets @destroy_func as the function to call when the coded buffer
200  * @proxy was released. At this point, the proxy object is considered
201  * released, i.e. the underlying data storage is no longer valid and
202  * the callback function shall not expect anything from that.
203  */
204 void
205 gst_vaapi_coded_buffer_proxy_set_destroy_notify (GstVaapiCodedBufferProxy *
206     proxy, GDestroyNotify destroy_func, gpointer user_data)
207 {
208   g_return_if_fail (proxy != NULL);
209
210   proxy->destroy_func = destroy_func;
211   proxy->destroy_data = user_data;
212 }
213
214 /**
215  * gst_vaapi_coded_buffer_proxy_get_user_data:
216  * @proxy: a #GstVaapiCodedBufferProxy
217  *
218  * Gets private data previously set on the VA coded buffer proxy
219  * object through the gst_vaapi_coded_buffer_proxy_set_user_data()
220  * function.
221  *
222  * Return value: the previously set user-data
223  */
224 gpointer
225 gst_vaapi_coded_buffer_proxy_get_user_data (GstVaapiCodedBufferProxy * proxy)
226 {
227   g_return_val_if_fail (proxy != NULL, NULL);
228
229   return proxy->user_data;
230 }
231
232 /**
233  * gst_vaapi_coded_buffer_proxy_set_user_data:
234  * @proxy: a #GstVaapiCodedBufferProxy
235  * @user_data: user-defined data
236  * @destroy_func: a #GDestroyNotify
237  *
238  * Sets @user_data on the VA coded buffer proxy object and the
239  * #GDestroyNotify function that will be called when the coded buffer
240  * proxy object is released.
241  *
242  * If a @user_data was previously set, then the previously set
243  * @destroy_func function, if any, will be called before the
244  * @user_data is replaced.
245  */
246 void
247 gst_vaapi_coded_buffer_proxy_set_user_data (GstVaapiCodedBufferProxy * proxy,
248     gpointer user_data, GDestroyNotify destroy_func)
249 {
250   g_return_if_fail (proxy != NULL);
251
252   coded_buffer_proxy_set_user_data (proxy, user_data, destroy_func);
253 }