1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
3 * Copyright (c) 2018, Igalia S.L.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #ifndef _WIN32
34 #include <unistd.h>
35 #endif
36 #include <stdlib.h>
37 #include "gstmsdksystemmemory.h"
38
39 #ifdef _WIN32
40 #define posix_memalign(d, a, s) ((*((void**)d) = _aligned_malloc(s, a)) ? 0 : -1)
41 #endif
42
43 #ifndef _WIN32
44 #define _aligned_free free
45 #endif
46
47 static gboolean
ensure_data(GstMsdkSystemMemory * mem)48 ensure_data (GstMsdkSystemMemory * mem)
49 {
50 gsize size;
51 void *data;
52 GstVideoInfo *info;
53 GstAllocator *allocator;
54 GstMsdkSystemAllocator *msdk_allocator;
55
56 allocator = GST_MEMORY_CAST (mem)->allocator;
57 msdk_allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
58
59 info = &msdk_allocator->image_info;
60 size = GST_VIDEO_INFO_SIZE (info);
61
62 if (mem->cache)
63 return TRUE;
64
65 if (posix_memalign (&data, 32, size) != 0) {
66 GST_ERROR ("Memory allocation failed");
67 return FALSE;
68 }
69
70 mem->cache = data;
71 mem->cached_data[0] = mem->cache;
72 mem->cached_data[1] = mem->cache + GST_VIDEO_INFO_PLANE_OFFSET (info, 1);
73 mem->cached_data[2] = mem->cache + GST_VIDEO_INFO_PLANE_OFFSET (info, 2);
74
75 mem->destination_pitches[0] = GST_VIDEO_INFO_PLANE_STRIDE (info, 0);
76 mem->destination_pitches[1] = GST_VIDEO_INFO_PLANE_STRIDE (info, 1);
77 mem->destination_pitches[2] = GST_VIDEO_INFO_PLANE_STRIDE (info, 2);
78
79 switch (GST_VIDEO_INFO_FORMAT (info)) {
80 case GST_VIDEO_FORMAT_NV12:
81 case GST_VIDEO_FORMAT_P010_10LE:
82 mem->surface->Data.Y = mem->cached_data[0];
83 mem->surface->Data.UV = mem->cached_data[1];
84 mem->surface->Data.Pitch = mem->destination_pitches[0];
85 break;
86 case GST_VIDEO_FORMAT_YV12:
87 mem->surface->Data.Y = mem->cached_data[0];
88 mem->surface->Data.U = mem->cached_data[2];
89 mem->surface->Data.V = mem->cached_data[1];
90 mem->surface->Data.Pitch = mem->destination_pitches[0];
91 break;
92 case GST_VIDEO_FORMAT_I420:
93 mem->surface->Data.Y = mem->cached_data[0];
94 mem->surface->Data.U = mem->cached_data[1];
95 mem->surface->Data.V = mem->cached_data[2];
96 mem->surface->Data.Pitch = mem->destination_pitches[0];
97 break;
98 case GST_VIDEO_FORMAT_YUY2:
99 mem->surface->Data.Y = mem->cached_data[0];
100 mem->surface->Data.U = mem->surface->Data.Y + 1;
101 mem->surface->Data.V = mem->surface->Data.Y + 3;
102 mem->surface->Data.Pitch = mem->destination_pitches[0];
103 break;
104 case GST_VIDEO_FORMAT_UYVY:
105 mem->surface->Data.Y = mem->cached_data[0];
106 mem->surface->Data.U = mem->surface->Data.Y;
107 mem->surface->Data.V = mem->surface->Data.U + 2;
108 mem->surface->Data.Pitch = mem->destination_pitches[0];
109 break;
110 case GST_VIDEO_FORMAT_BGRA:
111 mem->surface->Data.R = mem->cached_data[0];
112 mem->surface->Data.G = mem->surface->Data.R + 1;
113 mem->surface->Data.B = mem->surface->Data.R + 2;
114 mem->surface->Data.Pitch = mem->destination_pitches[0];
115 break;
116 #if (MFX_VERSION >= 1028)
117 case GST_VIDEO_FORMAT_RGB16:
118 mem->surface->Data.R = mem->cached_data[0];
119 mem->surface->Data.G = mem->surface->Data.R;
120 mem->surface->Data.B = mem->surface->Data.R;
121 mem->surface->Data.Pitch = mem->destination_pitches[0];
122 break;
123 #endif
124 case GST_VIDEO_FORMAT_VUYA:
125 mem->surface->Data.V = mem->cached_data[0];
126 mem->surface->Data.U = mem->surface->Data.V + 1;
127 mem->surface->Data.Y = mem->surface->Data.V + 2;
128 mem->surface->Data.A = mem->surface->Data.V + 3;
129 mem->surface->Data.PitchHigh =
130 (mfxU16) (mem->destination_pitches[0] / (1 << 16));
131 mem->surface->Data.PitchLow =
132 (mfxU16) (mem->destination_pitches[0] % (1 << 16));
133 break;
134 case GST_VIDEO_FORMAT_BGR10A2_LE:
135 mem->surface->Data.R = mem->cached_data[0];
136 mem->surface->Data.G = mem->surface->Data.R;
137 mem->surface->Data.B = mem->surface->Data.R;
138 mem->surface->Data.A = mem->surface->Data.R;
139 mem->surface->Data.Pitch = mem->destination_pitches[0];
140 break;
141 default:
142 g_assert_not_reached ();
143 break;
144 }
145
146 return TRUE;
147 }
148
149 static mfxFrameSurface1 *
gst_msdk_system_allocator_create_surface(GstAllocator * allocator)150 gst_msdk_system_allocator_create_surface (GstAllocator * allocator)
151 {
152 mfxFrameInfo frame_info = { {0,}, 0, };
153 mfxFrameSurface1 *surface;
154 GstMsdkSystemAllocator *msdk_system_allocator =
155 GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
156
157 surface = (mfxFrameSurface1 *) g_slice_new0 (mfxFrameSurface1);
158
159 if (!surface) {
160 GST_ERROR ("failed to allocate surface");
161 return NULL;
162 }
163
164 gst_msdk_set_mfx_frame_info_from_video_info (&frame_info,
165 &msdk_system_allocator->image_info);
166
167 surface->Info = frame_info;
168
169 return surface;
170 }
171
172 GstMemory *
gst_msdk_system_memory_new(GstAllocator * base_allocator)173 gst_msdk_system_memory_new (GstAllocator * base_allocator)
174 {
175 GstMsdkSystemAllocator *allocator;
176 GstVideoInfo *vip;
177 GstMsdkSystemMemory *mem;
178
179 g_return_val_if_fail (base_allocator, NULL);
180 g_return_val_if_fail (GST_IS_MSDK_SYSTEM_ALLOCATOR (base_allocator), NULL);
181
182 allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_allocator);
183
184 mem = g_slice_new0 (GstMsdkSystemMemory);
185 if (!mem)
186 return NULL;
187
188 mem->surface = gst_msdk_system_allocator_create_surface (base_allocator);
189
190 vip = &allocator->image_info;
191 gst_memory_init (&mem->parent_instance, GST_MEMORY_FLAG_NO_SHARE,
192 base_allocator, NULL, GST_VIDEO_INFO_SIZE (vip), 0, 0,
193 GST_VIDEO_INFO_SIZE (vip));
194
195 if (!ensure_data (mem))
196 return NULL;
197
198 return GST_MEMORY_CAST (mem);
199 }
200
201 static gpointer
gst_msdk_system_memory_map_full(GstMemory * base_mem,GstMapInfo * info,gsize maxsize)202 gst_msdk_system_memory_map_full (GstMemory * base_mem, GstMapInfo * info,
203 gsize maxsize)
204 {
205 GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
206
207 g_return_val_if_fail (mem, NULL);
208
209 if (!mem->surface) {
210 GST_WARNING ("The surface is not allocated");
211 return NULL;
212 }
213
214 if ((info->flags & GST_MAP_WRITE) && mem->surface
215 && mem->surface->Data.Locked) {
216 GST_WARNING ("The surface in memory %p is not still avaliable", mem);
217 return NULL;
218 }
219
220 return mem->surface->Data.Y;
221 }
222
223 static void
gst_msdk_system_memory_unmap(GstMemory * base_mem)224 gst_msdk_system_memory_unmap (GstMemory * base_mem)
225 {
226 }
227
228 /* GstMsdkSystemAllocator */
229 G_DEFINE_TYPE (GstMsdkSystemAllocator, gst_msdk_system_allocator,
230 GST_TYPE_ALLOCATOR);
231
232 static void
gst_msdk_system_allocator_free(GstAllocator * allocator,GstMemory * base_mem)233 gst_msdk_system_allocator_free (GstAllocator * allocator, GstMemory * base_mem)
234 {
235 GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
236
237 _aligned_free (mem->cache);
238 g_slice_free (mfxFrameSurface1, mem->surface);
239 }
240
241 static GstMemory *
gst_msdk_system_allocator_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)242 gst_msdk_system_allocator_alloc (GstAllocator * allocator, gsize size,
243 GstAllocationParams * params)
244 {
245 return gst_msdk_system_memory_new (allocator);
246 }
247
248 static void
gst_msdk_system_allocator_class_init(GstMsdkSystemAllocatorClass * klass)249 gst_msdk_system_allocator_class_init (GstMsdkSystemAllocatorClass * klass)
250 {
251 GstAllocatorClass *const allocator_class = GST_ALLOCATOR_CLASS (klass);
252
253 allocator_class->alloc = gst_msdk_system_allocator_alloc;
254 allocator_class->free = gst_msdk_system_allocator_free;
255 }
256
257 static void
gst_msdk_system_allocator_init(GstMsdkSystemAllocator * allocator)258 gst_msdk_system_allocator_init (GstMsdkSystemAllocator * allocator)
259 {
260 GstAllocator *const base_allocator = GST_ALLOCATOR_CAST (allocator);
261
262 base_allocator->mem_type = GST_MSDK_SYSTEM_MEMORY_NAME;
263 base_allocator->mem_map_full = gst_msdk_system_memory_map_full;
264 base_allocator->mem_unmap = gst_msdk_system_memory_unmap;
265
266 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
267 }
268
269 GstAllocator *
gst_msdk_system_allocator_new(GstVideoInfo * image_info)270 gst_msdk_system_allocator_new (GstVideoInfo * image_info)
271 {
272 GstMsdkSystemAllocator *allocator;
273
274 g_return_val_if_fail (image_info != NULL, NULL);
275
276 allocator = g_object_new (GST_TYPE_MSDK_SYSTEM_ALLOCATOR, NULL);
277 if (!allocator)
278 return NULL;
279
280 allocator->image_info = *image_info;
281
282 return GST_ALLOCATOR_CAST (allocator);
283 }
284