ececae9c3eb305ee9642abb53498d1a5481b3935
[platform/upstream/gstreamer.git] / subprojects / gstreamer-vaapi / 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_coded_buffer_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 (GST_VAAPI_VIDEO_POOL (pool));
97   proxy->buffer = gst_vaapi_video_pool_get_object (proxy->pool);
98   if (!proxy->buffer)
99     goto error;
100   gst_mini_object_ref (GST_MINI_OBJECT_CAST (proxy->buffer));
101   return proxy;
102
103   /* ERRORS */
104 error:
105   {
106     gst_vaapi_coded_buffer_proxy_unref (proxy);
107     return NULL;
108   }
109 }
110
111 /**
112  * gst_vaapi_coded_buffer_proxy_ref:
113  * @proxy: a #GstVaapiCodedBufferProxy
114  *
115  * Atomically increases the reference count of the given @proxy by one.
116  *
117  * Returns: The same @proxy argument
118  */
119 GstVaapiCodedBufferProxy *
120 gst_vaapi_coded_buffer_proxy_ref (GstVaapiCodedBufferProxy * proxy)
121 {
122   g_return_val_if_fail (proxy != NULL, NULL);
123
124   return GST_VAAPI_CODED_BUFFER_PROXY (gst_vaapi_mini_object_ref
125       (GST_VAAPI_MINI_OBJECT (proxy)));
126 }
127
128 /**
129  * gst_vaapi_coded_buffer_proxy_unref:
130  * @proxy: a #GstVaapiCodedBufferProxy
131  *
132  * Atomically decreases the reference count of the @proxy by one. If
133  * the reference count reaches zero, the object will be free'd.
134  */
135 void
136 gst_vaapi_coded_buffer_proxy_unref (GstVaapiCodedBufferProxy * proxy)
137 {
138   g_return_if_fail (proxy != NULL);
139
140   gst_vaapi_mini_object_unref (GST_VAAPI_MINI_OBJECT (proxy));
141 }
142
143 /**
144  * gst_vaapi_coded_buffer_proxy_replace:
145  * @old_proxy_ptr: a pointer to a #GstVaapiCodedBufferProxy
146  * @new_proxy: a #GstVaapiCodedBufferProxy
147  *
148  * Atomically replaces the proxy object held in @old_proxy_ptr with
149  * @new_proxy. This means that @old_proxy_ptr shall reference a valid
150  * object. However, @new_proxy can be NULL.
151  */
152 void
153 gst_vaapi_coded_buffer_proxy_replace (GstVaapiCodedBufferProxy ** old_proxy_ptr,
154     GstVaapiCodedBufferProxy * new_proxy)
155 {
156   g_return_if_fail (old_proxy_ptr != NULL);
157
158   gst_vaapi_mini_object_replace ((GstVaapiMiniObject **) old_proxy_ptr,
159       GST_VAAPI_MINI_OBJECT (new_proxy));
160 }
161
162 /**
163  * gst_vaapi_coded_buffer_proxy_get_buffer:
164  * @proxy: a #GstVaapiCodedBufferProxy
165  *
166  * Returns the #GstVaapiCodedBuffer stored in the @proxy.
167  *
168  * Return value: the #GstVaapiCodedBuffer, or %NULL if an error occurred
169  */
170 GstVaapiCodedBuffer *
171 gst_vaapi_coded_buffer_proxy_get_buffer (GstVaapiCodedBufferProxy * proxy)
172 {
173   g_return_val_if_fail (proxy != NULL, NULL);
174
175   return GST_VAAPI_CODED_BUFFER_PROXY_BUFFER (proxy);
176 }
177
178 /**
179  * gst_vaapi_coded_buffer_proxy_get_buffer_size:
180  * @proxy: a #GstVaapiCodedBufferProxy
181  *
182  * Returns the size of the underlying #GstVaapiCodedBuffer object
183  * stored in the @proxy.
184  *
185  * Return value: the underlying #GstVaapiCodedBuffer size, or -1 if an
186  *   error occurred
187  */
188 gssize
189 gst_vaapi_coded_buffer_proxy_get_buffer_size (GstVaapiCodedBufferProxy * proxy)
190 {
191   g_return_val_if_fail (proxy != NULL, -1);
192
193   return GST_VAAPI_CODED_BUFFER_PROXY_BUFFER_SIZE (proxy);
194 }
195
196 /**
197  * gst_vaapi_coded_buffer_proxy_set_destroy_notify:
198  * @proxy: a @GstVaapiCodedBufferProxy
199  * @destroy_func: a #GDestroyNotify function
200  * @user_data: some extra data to pass to the @destroy_func function
201  *
202  * Sets @destroy_func as the function to call when the coded buffer
203  * @proxy was released. At this point, the proxy object is considered
204  * released, i.e. the underlying data storage is no longer valid and
205  * the callback function shall not expect anything from that.
206  */
207 void
208 gst_vaapi_coded_buffer_proxy_set_destroy_notify (GstVaapiCodedBufferProxy *
209     proxy, GDestroyNotify destroy_func, gpointer user_data)
210 {
211   g_return_if_fail (proxy != NULL);
212
213   proxy->destroy_func = destroy_func;
214   proxy->destroy_data = user_data;
215 }
216
217 /**
218  * gst_vaapi_coded_buffer_proxy_get_user_data:
219  * @proxy: a #GstVaapiCodedBufferProxy
220  *
221  * Gets private data previously set on the VA coded buffer proxy
222  * object through the gst_vaapi_coded_buffer_proxy_set_user_data()
223  * function.
224  *
225  * Return value: the previously set user-data
226  */
227 gpointer
228 gst_vaapi_coded_buffer_proxy_get_user_data (GstVaapiCodedBufferProxy * proxy)
229 {
230   g_return_val_if_fail (proxy != NULL, NULL);
231
232   return proxy->user_data;
233 }
234
235 /**
236  * gst_vaapi_coded_buffer_proxy_set_user_data:
237  * @proxy: a #GstVaapiCodedBufferProxy
238  * @user_data: user-defined data
239  * @destroy_func: a #GDestroyNotify
240  *
241  * Sets @user_data on the VA coded buffer proxy object and the
242  * #GDestroyNotify function that will be called when the coded buffer
243  * proxy object is released.
244  *
245  * If a @user_data was previously set, then the previously set
246  * @destroy_func function, if any, will be called before the
247  * @user_data is replaced.
248  */
249 void
250 gst_vaapi_coded_buffer_proxy_set_user_data (GstVaapiCodedBufferProxy * proxy,
251     gpointer user_data, GDestroyNotify destroy_func)
252 {
253   g_return_if_fail (proxy != NULL);
254
255   coded_buffer_proxy_set_user_data (proxy, user_data, destroy_func);
256 }