2 * gstvaapiutils.c - VA-API utilities
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2012 Intel Corporation
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.
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.
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
24 #include "gstvaapicompat.h"
25 #include "gstvaapiutils.h"
26 #include "gstvaapisurface.h"
31 #include "gstvaapidebug.h"
33 #define CONCAT(a, b) CONCAT_(a, b)
34 #define CONCAT_(a, b) a##b
35 #define STRINGIFY(x) STRINGIFY_(x)
36 #define STRINGIFY_(x) #x
37 #define STRCASEP(p, x) STRCASE(CONCAT(p, x))
38 #define STRCASE(x) case x: return STRINGIFY(x)
40 /* Check VA status for success or print out an error */
42 vaapi_check_status(VAStatus status, const char *msg)
44 if (status != VA_STATUS_SUCCESS) {
45 GST_DEBUG("%s: %s", msg, vaErrorStr(status));
53 vaapi_map_buffer(VADisplay dpy, VABufferID buf_id)
58 status = vaMapBuffer(dpy, buf_id, &data);
59 if (!vaapi_check_status(status, "vaMapBuffer()"))
64 /* Unmaps VA buffer */
66 vaapi_unmap_buffer(VADisplay dpy, VABufferID buf_id, void **pbuf)
73 status = vaUnmapBuffer(dpy, buf_id);
74 if (!vaapi_check_status(status, "vaUnmapBuffer()"))
78 /* Creates and maps VA buffer */
86 VABufferID *buf_id_ptr,
92 gpointer data = (gpointer)buf;
94 status = vaCreateBuffer(dpy, ctx, type, size, 1, data, &buf_id);
95 if (!vaapi_check_status(status, "vaCreateBuffer()"))
99 data = vaapi_map_buffer(dpy, buf_id);
105 *buf_id_ptr = buf_id;
109 vaapi_destroy_buffer(dpy, &buf_id);
113 /* Destroy VA buffer */
115 vaapi_destroy_buffer(VADisplay dpy, VABufferID *buf_id_ptr)
117 if (!buf_id_ptr || *buf_id_ptr == VA_INVALID_ID)
120 vaDestroyBuffer(dpy, *buf_id_ptr);
121 *buf_id_ptr = VA_INVALID_ID;
124 /* Return a string representation of a VAProfile */
125 const char *string_of_VAProfile(VAProfile profile)
128 #define MAP(profile) \
129 STRCASEP(VAProfile, profile)
133 MAP(MPEG4AdvancedSimple);
135 #if VA_CHECK_VERSION(0,32,0)
138 MAP(H264ConstrainedBaseline);
152 /* Return a string representation of a VAEntrypoint */
153 const char *string_of_VAEntrypoint(VAEntrypoint entrypoint)
155 switch (entrypoint) {
156 #define MAP(entrypoint) \
157 STRCASEP(VAEntrypoint, entrypoint)
169 /* Return a string representation of a VADisplayAttributeType */
171 string_of_VADisplayAttributeType(VADisplayAttribType attribute_type)
173 switch (attribute_type) {
174 #define MAP(attribute_type) \
175 STRCASEP(VADisplayAttrib, attribute_type)
180 MAP(BackgroundColor);
181 #if !VA_CHECK_VERSION(0,34,0)
185 MAP(OutofLoopDeblock);
186 #if VA_CHECK_VERSION(0,31,1) && !VA_CHECK_VERSION(0,34,0)
190 MAP(SkinColorCorrection);
194 MAP(OverlayAutoPaintColorKey);
195 MAP(OverlayColorKey);
206 * from_GstVaapiSurfaceRenderFlags:
207 * @flags: the #GstVaapiSurfaceRenderFlags
209 * Converts #GstVaapiSurfaceRenderFlags to flags suitable for
213 from_GstVaapiSurfaceRenderFlags(guint flags)
215 guint va_fields = 0, va_csc = 0;
217 if (flags & GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD)
218 va_fields |= VA_TOP_FIELD;
219 if (flags & GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD)
220 va_fields |= VA_BOTTOM_FIELD;
221 if ((va_fields ^ (VA_TOP_FIELD|VA_BOTTOM_FIELD)) == 0)
222 va_fields = VA_FRAME_PICTURE;
225 if (flags & GST_VAAPI_COLOR_STANDARD_ITUR_BT_601)
226 va_csc = VA_SRC_BT601;
229 if (flags & GST_VAAPI_COLOR_STANDARD_ITUR_BT_709)
230 va_csc = VA_SRC_BT709;
233 return va_fields|va_csc;
237 * to_GstVaapiSurfaceStatus:
238 * @flags: the #GstVaapiSurfaceStatus flags to translate
240 * Converts vaQuerySurfaceStatus() @flags to #GstVaapiSurfaceStatus
243 * Return value: the #GstVaapiSurfaceStatus flags
246 to_GstVaapiSurfaceStatus(guint va_flags)
249 const guint va_flags_mask = (VASurfaceReady|
251 VASurfaceDisplaying);
253 /* Check for core status */
254 switch (va_flags & va_flags_mask) {
256 flags = GST_VAAPI_SURFACE_STATUS_IDLE;
258 case VASurfaceRendering:
259 flags = GST_VAAPI_SURFACE_STATUS_RENDERING;
261 case VASurfaceDisplaying:
262 flags = GST_VAAPI_SURFACE_STATUS_DISPLAYING;
269 /* Check for encoder status */
270 #if VA_CHECK_VERSION(0,30,0)
271 if (va_flags & VASurfaceSkipped)
272 flags |= GST_VAAPI_SURFACE_STATUS_SKIPPED;
277 /* Translate GstVaapiRotation value to VA-API rotation value */
279 from_GstVaapiRotation(guint value)
282 case GST_VAAPI_ROTATION_0: return VA_ROTATION_NONE;
283 case GST_VAAPI_ROTATION_90: return VA_ROTATION_90;
284 case GST_VAAPI_ROTATION_180: return VA_ROTATION_180;
285 case GST_VAAPI_ROTATION_270: return VA_ROTATION_270;
287 GST_ERROR("unsupported GstVaapiRotation value %d", value);
288 return VA_ROTATION_NONE;
291 /* Translate VA-API rotation value to GstVaapiRotation value */
293 to_GstVaapiRotation(guint value)
296 case VA_ROTATION_NONE: return GST_VAAPI_ROTATION_0;
297 case VA_ROTATION_90: return GST_VAAPI_ROTATION_90;
298 case VA_ROTATION_180: return GST_VAAPI_ROTATION_180;
299 case VA_ROTATION_270: return GST_VAAPI_ROTATION_270;
301 GST_ERROR("unsupported VA-API rotation value %d", value);
302 return GST_VAAPI_ROTATION_0;
306 from_GstVaapiRateControl(guint value)
309 case GST_VAAPI_RATECONTROL_NONE: return VA_RC_NONE;
310 case GST_VAAPI_RATECONTROL_CBR: return VA_RC_CBR;
311 case GST_VAAPI_RATECONTROL_VBR: return VA_RC_VBR;
312 case GST_VAAPI_RATECONTROL_VCM: return VA_RC_VCM;
313 #if VA_CHECK_VERSION(0,34,0)
314 case GST_VAAPI_RATECONTROL_CQP: return VA_RC_CQP;
315 case GST_VAAPI_RATECONTROL_VBR_CONSTRAINED: return VA_RC_VBR_CONSTRAINED;
318 GST_ERROR("unsupported GstVaapiRateControl value %d", value);
323 to_GstVaapiRateControl(guint value)
326 case VA_RC_NONE: return GST_VAAPI_RATECONTROL_NONE;
327 case VA_RC_CBR: return GST_VAAPI_RATECONTROL_CBR;
328 case VA_RC_VBR: return GST_VAAPI_RATECONTROL_VBR;
329 case VA_RC_VCM: return GST_VAAPI_RATECONTROL_VCM;
330 #if VA_CHECK_VERSION(0,34,0)
331 case VA_RC_CQP: return GST_VAAPI_RATECONTROL_CQP;
332 case VA_RC_VBR_CONSTRAINED: return GST_VAAPI_RATECONTROL_VBR_CONSTRAINED;
335 GST_ERROR("unsupported VA-API Rate Control value %d", value);
336 return GST_VAAPI_RATECONTROL_NONE;
340 string_of_VARateControl(guint rate_control)
342 switch (rate_control) {
343 case VA_RC_NONE: return "VA_RC_NONE";
344 case VA_RC_CBR: return "VA_RC_CBR";
345 case VA_RC_VBR: return "VA_RC_VBR";
346 case VA_RC_VCM: return "VA_RC_VCM";
347 #if VA_CHECK_VERSION(0,34,0)
348 case VA_RC_CQP: return "VA_RC_CQP";
349 case VA_RC_VBR_CONSTRAINED: return "VA_RC_VBR_CONSTRAINED";