1 /*
2  *  gstvaapipixmap.c - Pixmap abstraction
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 /**
24  * SECTION:gstvaapipixmap
25  * @short_description: Pixmap abstraction
26  */
27 
28 #include "sysdeps.h"
29 #include "gstvaapipixmap.h"
30 #include "gstvaapipixmap_priv.h"
31 #include "gstvaapisurface_priv.h"
32 
33 #define DEBUG 1
34 #include "gstvaapidebug.h"
35 
36 static inline GstVaapiPixmap *
gst_vaapi_pixmap_new_internal(const GstVaapiPixmapClass * pixmap_class,GstVaapiDisplay * display)37 gst_vaapi_pixmap_new_internal (const GstVaapiPixmapClass * pixmap_class,
38     GstVaapiDisplay * display)
39 {
40   g_assert (pixmap_class->create != NULL);
41   g_assert (pixmap_class->render != NULL);
42 
43   return gst_vaapi_object_new (GST_VAAPI_OBJECT_CLASS (pixmap_class), display);
44 }
45 
46 GstVaapiPixmap *
gst_vaapi_pixmap_new(const GstVaapiPixmapClass * pixmap_class,GstVaapiDisplay * display,GstVideoFormat format,guint width,guint height)47 gst_vaapi_pixmap_new (const GstVaapiPixmapClass * pixmap_class,
48     GstVaapiDisplay * display, GstVideoFormat format, guint width, guint height)
49 {
50   GstVaapiPixmap *pixmap;
51 
52   g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN &&
53       format != GST_VIDEO_FORMAT_ENCODED, NULL);
54   g_return_val_if_fail (width > 0, NULL);
55   g_return_val_if_fail (height > 0, NULL);
56 
57   pixmap = gst_vaapi_pixmap_new_internal (pixmap_class, display);
58   if (!pixmap)
59     return NULL;
60 
61   pixmap->format = format;
62   pixmap->width = width;
63   pixmap->height = height;
64   if (!pixmap_class->create (pixmap))
65     goto error;
66   return pixmap;
67 
68   /* ERRORS */
69 error:
70   {
71     gst_vaapi_pixmap_unref (pixmap);
72     return NULL;
73   }
74 }
75 
76 GstVaapiPixmap *
gst_vaapi_pixmap_new_from_native(const GstVaapiPixmapClass * pixmap_class,GstVaapiDisplay * display,gpointer native_pixmap)77 gst_vaapi_pixmap_new_from_native (const GstVaapiPixmapClass * pixmap_class,
78     GstVaapiDisplay * display, gpointer native_pixmap)
79 {
80   GstVaapiPixmap *pixmap;
81 
82   pixmap = gst_vaapi_pixmap_new_internal (pixmap_class, display);
83   if (!pixmap)
84     return NULL;
85 
86   GST_VAAPI_OBJECT_ID (pixmap) = GPOINTER_TO_SIZE (native_pixmap);
87   pixmap->use_foreign_pixmap = TRUE;
88   if (!pixmap_class->create (pixmap))
89     goto error;
90   return pixmap;
91 
92   /* ERRORS */
93 error:
94   {
95     gst_vaapi_pixmap_unref (pixmap);
96     return NULL;
97   }
98 }
99 
100 /**
101  * gst_vaapi_pixmap_ref:
102  * @pixmap: a #GstVaapiPixmap
103  *
104  * Atomically increases the reference count of the given @pixmap by one.
105  *
106  * Returns: The same @pixmap argument
107  */
108 GstVaapiPixmap *
gst_vaapi_pixmap_ref(GstVaapiPixmap * pixmap)109 gst_vaapi_pixmap_ref (GstVaapiPixmap * pixmap)
110 {
111   return gst_vaapi_object_ref (GST_VAAPI_OBJECT (pixmap));
112 }
113 
114 /**
115  * gst_vaapi_pixmap_unref:
116  * @pixmap: a #GstVaapiPixmap
117  *
118  * Atomically decreases the reference count of the @pixmap by one. If
119  * the reference count reaches zero, the pixmap will be free'd.
120  */
121 void
gst_vaapi_pixmap_unref(GstVaapiPixmap * pixmap)122 gst_vaapi_pixmap_unref (GstVaapiPixmap * pixmap)
123 {
124   gst_vaapi_object_unref (GST_VAAPI_OBJECT (pixmap));
125 }
126 
127 /**
128  * gst_vaapi_pixmap_replace:
129  * @old_pixmap_ptr: a pointer to a #GstVaapiPixmap
130  * @new_pixmap: a #GstVaapiPixmap
131  *
132  * Atomically replaces the pixmap pixmap held in @old_pixmap_ptr with
133  * @new_pixmap. This means that @old_pixmap_ptr shall reference a
134  * valid pixmap. However, @new_pixmap can be NULL.
135  */
136 void
gst_vaapi_pixmap_replace(GstVaapiPixmap ** old_pixmap_ptr,GstVaapiPixmap * new_pixmap)137 gst_vaapi_pixmap_replace (GstVaapiPixmap ** old_pixmap_ptr,
138     GstVaapiPixmap * new_pixmap)
139 {
140   gst_vaapi_object_replace ((GstVaapiObject **) (old_pixmap_ptr),
141       GST_VAAPI_OBJECT (new_pixmap));
142 }
143 
144 /**
145  * gst_vaapi_pixmap_get_display:
146  * @pixmap: a #GstVaapiPixmap
147  *
148  * Returns the #GstVaapiDisplay this @pixmap is bound to.
149  *
150  * Return value: the parent #GstVaapiDisplay object
151  */
152 GstVaapiDisplay *
gst_vaapi_pixmap_get_display(GstVaapiPixmap * pixmap)153 gst_vaapi_pixmap_get_display (GstVaapiPixmap * pixmap)
154 {
155   g_return_val_if_fail (pixmap != NULL, NULL);
156 
157   return GST_VAAPI_OBJECT_DISPLAY (pixmap);
158 }
159 
160 /**
161  * gst_vaapi_pixmap_get_format:
162  * @pixmap: a #GstVaapiPixmap
163  *
164  * Retrieves the format of a #GstVaapiPixmap.
165  *
166  * Return value: the format of the @pixmap
167  */
168 GstVideoFormat
gst_vaapi_pixmap_get_format(GstVaapiPixmap * pixmap)169 gst_vaapi_pixmap_get_format (GstVaapiPixmap * pixmap)
170 {
171   g_return_val_if_fail (pixmap != NULL, GST_VIDEO_FORMAT_UNKNOWN);
172 
173   return GST_VAAPI_PIXMAP_FORMAT (pixmap);
174 }
175 
176 /**
177  * gst_vaapi_pixmap_get_width:
178  * @pixmap: a #GstVaapiPixmap
179  *
180  * Retrieves the width of a #GstVaapiPixmap.
181  *
182  * Return value: the width of the @pixmap, in pixels
183  */
184 guint
gst_vaapi_pixmap_get_width(GstVaapiPixmap * pixmap)185 gst_vaapi_pixmap_get_width (GstVaapiPixmap * pixmap)
186 {
187   g_return_val_if_fail (pixmap != NULL, 0);
188 
189   return GST_VAAPI_PIXMAP_WIDTH (pixmap);
190 }
191 
192 /**
193  * gst_vaapi_pixmap_get_height:
194  * @pixmap: a #GstVaapiPixmap
195  *
196  * Retrieves the height of a #GstVaapiPixmap
197  *
198  * Return value: the height of the @pixmap, in pixels
199  */
200 guint
gst_vaapi_pixmap_get_height(GstVaapiPixmap * pixmap)201 gst_vaapi_pixmap_get_height (GstVaapiPixmap * pixmap)
202 {
203   g_return_val_if_fail (pixmap != NULL, 0);
204 
205   return GST_VAAPI_PIXMAP_HEIGHT (pixmap);
206 }
207 
208 /**
209  * gst_vaapi_pixmap_get_size:
210  * @pixmap: a #GstVaapiPixmap
211  * @width: return location for the width, or %NULL
212  * @height: return location for the height, or %NULL
213  *
214  * Retrieves the dimensions of a #GstVaapiPixmap.
215  */
216 void
gst_vaapi_pixmap_get_size(GstVaapiPixmap * pixmap,guint * width,guint * height)217 gst_vaapi_pixmap_get_size (GstVaapiPixmap * pixmap, guint * width,
218     guint * height)
219 {
220   g_return_if_fail (pixmap != NULL);
221 
222   if (width)
223     *width = GST_VAAPI_PIXMAP_WIDTH (pixmap);
224 
225   if (height)
226     *height = GST_VAAPI_PIXMAP_HEIGHT (pixmap);
227 }
228 
229 /**
230  * gst_vaapi_pixmap_put_surface:
231  * @pixmap: a #GstVaapiPixmap
232  * @surface: a #GstVaapiSurface
233  * @crop_rect: the video cropping rectangle, or %NULL if the entire
234  *   surface is to be used.
235  * @flags: postprocessing flags. See #GstVaapiSurfaceRenderFlags
236  *
237  * Renders the whole @surface, or a cropped region defined with
238  * @crop_rect, into the @pixmap, while scaling to fit the target
239  * pixmap. The @flags specify how de-interlacing (if needed), color
240  * space conversion, scaling and other postprocessing transformations
241  * are performed.
242  *
243  * Return value: %TRUE on success
244  */
245 gboolean
gst_vaapi_pixmap_put_surface(GstVaapiPixmap * pixmap,GstVaapiSurface * surface,const GstVaapiRectangle * crop_rect,guint flags)246 gst_vaapi_pixmap_put_surface (GstVaapiPixmap * pixmap,
247     GstVaapiSurface * surface, const GstVaapiRectangle * crop_rect, guint flags)
248 {
249   GstVaapiRectangle src_rect;
250 
251   g_return_val_if_fail (pixmap != NULL, FALSE);
252   g_return_val_if_fail (surface != NULL, FALSE);
253 
254   if (!crop_rect) {
255     src_rect.x = 0;
256     src_rect.y = 0;
257     src_rect.width = GST_VAAPI_SURFACE_WIDTH (surface);
258     src_rect.height = GST_VAAPI_SURFACE_HEIGHT (surface);
259     crop_rect = &src_rect;
260   }
261   return GST_VAAPI_PIXMAP_GET_CLASS (pixmap)->render (pixmap, surface,
262       crop_rect, flags);
263 }
264