Tizen 2.0 Release
[framework/multimedia/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapiutils.c
1 /*
2  *  gstvaapiutils.c - VA-API utilities
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *  Copyright (C) 2011-2012 Intel Corporation
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 "gstvaapicompat.h"
25 #include "gstvaapiutils.h"
26 #include "gstvaapisurface.h"
27 #include <stdio.h>
28 #include <stdarg.h>
29
30 #define DEBUG 1
31 #include "gstvaapidebug.h"
32
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)
39
40 /* Check VA status for success or print out an error */
41 gboolean
42 vaapi_check_status(VAStatus status, const char *msg)
43 {
44     if (status != VA_STATUS_SUCCESS) {
45         GST_DEBUG("%s: %s", msg, vaErrorStr(status));
46         return FALSE;
47     }
48     return TRUE;
49 }
50
51 /* Maps VA buffer */
52 void *
53 vaapi_map_buffer(VADisplay dpy, VABufferID buf_id)
54 {
55     VAStatus status;
56     void *data = NULL;
57
58     status = vaMapBuffer(dpy, buf_id, &data);
59     if (!vaapi_check_status(status, "vaMapBuffer()"))
60         return NULL;
61     return data;
62 }
63
64 /* Unmaps VA buffer */
65 void
66 vaapi_unmap_buffer(VADisplay dpy, VABufferID buf_id, void **pbuf)
67 {
68     VAStatus status;
69
70     if (pbuf)
71         *pbuf = NULL;
72
73     status = vaUnmapBuffer(dpy, buf_id);
74     if (!vaapi_check_status(status, "vaUnmapBuffer()"))
75         return;
76 }
77
78 /* Creates and maps VA buffer */
79 gboolean
80 vaapi_create_buffer(
81     VADisplay     dpy,
82     VAContextID   ctx,
83     int           type,
84     unsigned int  size,
85     gconstpointer buf,
86     VABufferID   *buf_id_ptr,
87     gpointer     *mapped_data
88 )
89 {
90     VABufferID buf_id;
91     VAStatus status;
92     gpointer data = (gpointer)buf;
93
94     status = vaCreateBuffer(dpy, ctx, type, size, 1, data, &buf_id);
95     if (!vaapi_check_status(status, "vaCreateBuffer()"))
96         return FALSE;
97
98     if (mapped_data) {
99         data = vaapi_map_buffer(dpy, buf_id);
100         if (!data)
101             goto error;
102         *mapped_data = data;
103     }
104
105     *buf_id_ptr = buf_id;
106     return TRUE;
107
108 error:
109     vaapi_destroy_buffer(dpy, &buf_id);
110     return FALSE;
111 }
112
113 /* Destroy VA buffer */
114 void
115 vaapi_destroy_buffer(VADisplay dpy, VABufferID *buf_id_ptr)
116 {
117     if (!buf_id_ptr || *buf_id_ptr == VA_INVALID_ID)
118         return;
119
120     vaDestroyBuffer(dpy, *buf_id_ptr);
121     *buf_id_ptr = VA_INVALID_ID;
122 }
123
124 /* Return a string representation of a VAProfile */
125 const char *string_of_VAProfile(VAProfile profile)
126 {
127     switch (profile) {
128 #define MAP(profile) \
129         STRCASEP(VAProfile, profile)
130         MAP(MPEG2Simple);
131         MAP(MPEG2Main);
132         MAP(MPEG4Simple);
133         MAP(MPEG4AdvancedSimple);
134         MAP(MPEG4Main);
135 #if VA_CHECK_VERSION(0,32,0)
136         MAP(JPEGBaseline);
137         MAP(H263Baseline);
138         MAP(H264ConstrainedBaseline);
139 #endif
140         MAP(H264Baseline);
141         MAP(H264Main);
142         MAP(H264High);
143         MAP(VC1Simple);
144         MAP(VC1Main);
145         MAP(VC1Advanced);
146 #undef MAP
147     default: break;
148     }
149     return "<unknown>";
150 }
151
152 /* Return a string representation of a VAEntrypoint */
153 const char *string_of_VAEntrypoint(VAEntrypoint entrypoint)
154 {
155     switch (entrypoint) {
156 #define MAP(entrypoint) \
157         STRCASEP(VAEntrypoint, entrypoint)
158         MAP(VLD);
159         MAP(IZZ);
160         MAP(IDCT);
161         MAP(MoComp);
162         MAP(Deblocking);
163 #undef MAP
164     default: break;
165     }
166     return "<unknown>";
167 }
168
169 /* Return a string representation of a VADisplayAttributeType */
170 const char *
171 string_of_VADisplayAttributeType(VADisplayAttribType attribute_type)
172 {
173     switch (attribute_type) {
174 #define MAP(attribute_type) \
175         STRCASEP(VADisplayAttrib, attribute_type)
176         MAP(Brightness);
177         MAP(Contrast);
178         MAP(Hue);
179         MAP(Saturation);
180         MAP(BackgroundColor);
181 #if !VA_CHECK_VERSION(0,34,0)
182         MAP(DirectSurface);
183 #endif
184         MAP(Rotation);
185         MAP(OutofLoopDeblock);
186 #if VA_CHECK_VERSION(0,31,1) && !VA_CHECK_VERSION(0,34,0)
187         MAP(BLEBlackMode);
188         MAP(BLEWhiteMode);
189         MAP(BlueStretch);
190         MAP(SkinColorCorrection);
191 #endif
192         MAP(CSCMatrix);
193         MAP(BlendColor);
194         MAP(OverlayAutoPaintColorKey);
195         MAP(OverlayColorKey);
196         MAP(RenderMode);
197         MAP(RenderDevice);
198         MAP(RenderRect);
199 #undef MAP
200     default: break;
201     }
202     return "<unknown>";
203 }
204
205 /**
206  * from_GstVaapiSurfaceRenderFlags:
207  * @flags: the #GstVaapiSurfaceRenderFlags
208  *
209  * Converts #GstVaapiSurfaceRenderFlags to flags suitable for
210  * vaPutSurface().
211  */
212 guint
213 from_GstVaapiSurfaceRenderFlags(guint flags)
214 {
215     guint va_fields = 0, va_csc = 0;
216
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;
223
224 #ifdef VA_SRC_BT601
225     if (flags & GST_VAAPI_COLOR_STANDARD_ITUR_BT_601)
226         va_csc = VA_SRC_BT601;
227 #endif
228 #ifdef VA_SRC_BT709
229     if (flags & GST_VAAPI_COLOR_STANDARD_ITUR_BT_709)
230         va_csc = VA_SRC_BT709;
231 #endif
232
233     return va_fields|va_csc;
234 }
235
236 /**
237  * to_GstVaapiSurfaceStatus:
238  * @flags: the #GstVaapiSurfaceStatus flags to translate
239  *
240  * Converts vaQuerySurfaceStatus() @flags to #GstVaapiSurfaceStatus
241  * flags.
242  *
243  * Return value: the #GstVaapiSurfaceStatus flags
244  */
245 guint
246 to_GstVaapiSurfaceStatus(guint va_flags)
247 {
248     guint flags;
249     const guint va_flags_mask = (VASurfaceReady|
250                                  VASurfaceRendering|
251                                  VASurfaceDisplaying);
252
253     /* Check for core status */
254     switch (va_flags & va_flags_mask) {
255     case VASurfaceReady:
256         flags = GST_VAAPI_SURFACE_STATUS_IDLE;
257         break;
258     case VASurfaceRendering:
259         flags = GST_VAAPI_SURFACE_STATUS_RENDERING;
260         break;
261     case VASurfaceDisplaying:
262         flags = GST_VAAPI_SURFACE_STATUS_DISPLAYING;
263         break;
264     default:
265         flags = 0;
266         break;
267     }
268
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;
273 #endif
274     return flags;
275 }
276
277 /* Translate GstVaapiRotation value to VA-API rotation value */
278 guint
279 from_GstVaapiRotation(guint value)
280 {
281     switch (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;
286     }
287     GST_ERROR("unsupported GstVaapiRotation value %d", value);
288     return VA_ROTATION_NONE;
289 }
290
291 /* Translate VA-API rotation value to GstVaapiRotation value */
292 guint
293 to_GstVaapiRotation(guint value)
294 {
295     switch (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;
300     }
301     GST_ERROR("unsupported VA-API rotation value %d", value);
302     return GST_VAAPI_ROTATION_0;
303 }
304
305 guint
306 from_GstVaapiRateControl(guint value)
307 {
308     switch (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;
316 #endif
317     }
318     GST_ERROR("unsupported GstVaapiRateControl value %d", value);
319     return VA_RC_NONE;
320 }
321
322 guint
323 to_GstVaapiRateControl(guint value)
324 {
325     switch (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;
333 #endif
334     }
335     GST_ERROR("unsupported VA-API Rate Control value %d", value);
336     return GST_VAAPI_RATECONTROL_NONE;
337 }
338
339 const char *
340 string_of_VARateControl(guint rate_control)
341 {
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";
350 #endif
351     default: break;
352     }
353     return "<unknown>";
354 }