Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / sys / msdk / msdk_libva.c
1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2016, Oblong Industries, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <va/va_drm.h>
36 #include "msdk.h"
37 #include "msdk_libva.h"
38
39 struct fourcc_map
40 {
41   mfxU32 mfx_fourcc;
42   guint32 va_fourcc;
43 };
44
45 struct rt_map
46 {
47   mfxU32 mfx_rt_format;
48   guint32 va_rt_format;
49 };
50
51 #define FOURCC_MFX_TO_VA(MFX, VA) \
52     { MFX_FOURCC_##MFX, VA_FOURCC_##VA }
53
54 #define RT_MFX_TO_VA(MFX, VA) \
55     { MFX_CHROMAFORMAT_##MFX, VA_RT_FORMAT_##VA }
56
57 static const struct fourcc_map gst_msdk_fourcc_mfx_to_va[] = {
58   FOURCC_MFX_TO_VA (NV12, NV12),
59   FOURCC_MFX_TO_VA (YUY2, YUY2),
60   FOURCC_MFX_TO_VA (UYVY, UYVY),
61   FOURCC_MFX_TO_VA (YV12, YV12),
62   FOURCC_MFX_TO_VA (RGB4, ARGB),
63   FOURCC_MFX_TO_VA (P8, P208),
64   FOURCC_MFX_TO_VA (P010, P010),
65 #if (MFX_VERSION >= 1031)
66   FOURCC_MFX_TO_VA (P016, P016),
67   FOURCC_MFX_TO_VA (Y216, Y216),
68   FOURCC_MFX_TO_VA (Y416, Y416),
69 #endif
70 #if (MFX_VERSION >= 1028)
71   FOURCC_MFX_TO_VA (RGB565, RGB565),
72 #endif
73   FOURCC_MFX_TO_VA (AYUV, AYUV),
74 #if VA_CHECK_VERSION(1, 4, 1)
75   FOURCC_MFX_TO_VA (A2RGB10, A2R10G10B10),
76 #endif
77 #if ((MFX_VERSION >= 1027) && VA_CHECK_VERSION(1, 2, 0))
78   FOURCC_MFX_TO_VA (Y210, Y210),
79   FOURCC_MFX_TO_VA (Y410, Y410),
80 #endif
81   FOURCC_MFX_TO_VA (BGR4, ABGR),
82
83 #if (MFX_VERSION >= 2004)
84   FOURCC_MFX_TO_VA (RGBP, RGBP),
85   FOURCC_MFX_TO_VA (BGRP, BGRP),
86 #endif
87
88   {0, 0}
89 };
90
91 static const struct rt_map gst_msdk_rt_mfx_to_va[] = {
92   RT_MFX_TO_VA (YUV420, YUV420),
93   RT_MFX_TO_VA (YUV422, YUV422),
94   RT_MFX_TO_VA (YUV444, YUV444),
95   {0, 0}
96 };
97
98 mfxStatus
99 gst_msdk_get_mfx_status_from_va_status (VAStatus va_res)
100 {
101   mfxStatus mfxRes = MFX_ERR_NONE;
102
103   switch (va_res) {
104     case VA_STATUS_SUCCESS:
105       mfxRes = MFX_ERR_NONE;
106       break;
107     case VA_STATUS_ERROR_ALLOCATION_FAILED:
108       mfxRes = MFX_ERR_MEMORY_ALLOC;
109       break;
110     case VA_STATUS_ERROR_ATTR_NOT_SUPPORTED:
111     case VA_STATUS_ERROR_UNSUPPORTED_PROFILE:
112     case VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT:
113     case VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT:
114     case VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE:
115     case VA_STATUS_ERROR_FLAG_NOT_SUPPORTED:
116     case VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED:
117       mfxRes = MFX_ERR_UNSUPPORTED;
118       break;
119     case VA_STATUS_ERROR_INVALID_DISPLAY:
120     case VA_STATUS_ERROR_INVALID_CONFIG:
121     case VA_STATUS_ERROR_INVALID_CONTEXT:
122     case VA_STATUS_ERROR_INVALID_SURFACE:
123     case VA_STATUS_ERROR_INVALID_BUFFER:
124     case VA_STATUS_ERROR_INVALID_IMAGE:
125     case VA_STATUS_ERROR_INVALID_SUBPICTURE:
126       mfxRes = MFX_ERR_NOT_INITIALIZED;
127       break;
128     case VA_STATUS_ERROR_INVALID_PARAMETER:
129       mfxRes = MFX_ERR_INVALID_VIDEO_PARAM;
130       break;
131     default:
132       mfxRes = MFX_ERR_UNKNOWN;
133       break;
134   }
135   return mfxRes;
136 }
137
138 guint
139 gst_msdk_get_va_fourcc_from_mfx_fourcc (mfxU32 fourcc)
140 {
141   const struct fourcc_map *m = gst_msdk_fourcc_mfx_to_va;
142
143   for (; m->mfx_fourcc != 0; m++) {
144     if (m->mfx_fourcc == fourcc)
145       return m->va_fourcc;
146   }
147
148   return 0;
149 }
150
151 guint
152 gst_msdk_get_mfx_fourcc_from_va_fourcc (guint32 fourcc)
153 {
154   const struct fourcc_map *m = gst_msdk_fourcc_mfx_to_va;
155
156   for (; m->va_fourcc != 0; m++) {
157     if (m->va_fourcc == fourcc)
158       return m->mfx_fourcc;
159   }
160
161   return 0;
162 }
163
164 guint
165 gst_msdk_get_va_rt_format_from_mfx_rt_format (mfxU32 format)
166 {
167   const struct rt_map *m = gst_msdk_rt_mfx_to_va;
168
169   for (; m->mfx_rt_format != 0; m++) {
170     if (m->mfx_rt_format == format)
171       return m->va_rt_format;
172   }
173
174   return 0;
175 }
176
177 guint
178 gst_msdk_get_mfx_rt_format_from_va_rt_format (guint32 format)
179 {
180   const struct rt_map *m = gst_msdk_rt_mfx_to_va;
181
182   for (; m->va_rt_format != 0; m++) {
183     if (m->va_rt_format == format)
184       return m->mfx_rt_format;
185   }
186
187   return 0;
188 }