• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string.h>
39 
40 #ifdef _WIN32
41 #define posix_memalign(d, a, s) ((*((void**)d) = _aligned_malloc(s, a)) ? 0 : -1)
42 #endif
43 
44 #ifndef _WIN32
45 #define _aligned_free free
46 #endif
47 
48 static gboolean
ensure_data(GstMsdkSystemMemory * mem)49 ensure_data (GstMsdkSystemMemory * mem)
50 {
51   gsize size;
52   void *data;
53   GstVideoInfo *info;
54   GstAllocator *allocator;
55   GstMsdkSystemAllocator *msdk_allocator;
56 
57   allocator = GST_MEMORY_CAST (mem)->allocator;
58   msdk_allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
59 
60   info = &msdk_allocator->image_info;
61   size = GST_VIDEO_INFO_SIZE (info);
62 
63   if (mem->cache)
64     return TRUE;
65 
66   if (posix_memalign (&data, 32, size) != 0) {
67     GST_ERROR ("Memory allocation failed");
68     return FALSE;
69   }
70 
71   mem->cache = data;
72   mem->cached_data[0] = mem->cache;
73   mem->cached_data[1] = mem->cache + GST_VIDEO_INFO_PLANE_OFFSET (info, 1);
74   mem->cached_data[2] = mem->cache + GST_VIDEO_INFO_PLANE_OFFSET (info, 2);
75 
76   mem->destination_pitches[0] = GST_VIDEO_INFO_PLANE_STRIDE (info, 0);
77   mem->destination_pitches[1] = GST_VIDEO_INFO_PLANE_STRIDE (info, 1);
78   mem->destination_pitches[2] = GST_VIDEO_INFO_PLANE_STRIDE (info, 2);
79 
80   switch (GST_VIDEO_INFO_FORMAT (info)) {
81     case GST_VIDEO_FORMAT_NV12:
82     case GST_VIDEO_FORMAT_P010_10LE:
83     case GST_VIDEO_FORMAT_P012_LE:
84       mem->surface->Data.Y = mem->cached_data[0];
85       mem->surface->Data.UV = mem->cached_data[1];
86       mem->surface->Data.Pitch = mem->destination_pitches[0];
87       break;
88     case GST_VIDEO_FORMAT_YV12:
89       mem->surface->Data.Y = mem->cached_data[0];
90       mem->surface->Data.U = mem->cached_data[2];
91       mem->surface->Data.V = mem->cached_data[1];
92       mem->surface->Data.Pitch = mem->destination_pitches[0];
93       break;
94     case GST_VIDEO_FORMAT_I420:
95       mem->surface->Data.Y = mem->cached_data[0];
96       mem->surface->Data.U = mem->cached_data[1];
97       mem->surface->Data.V = mem->cached_data[2];
98       mem->surface->Data.Pitch = mem->destination_pitches[0];
99       break;
100     case GST_VIDEO_FORMAT_YUY2:
101       mem->surface->Data.Y = mem->cached_data[0];
102       mem->surface->Data.U = mem->surface->Data.Y + 1;
103       mem->surface->Data.V = mem->surface->Data.Y + 3;
104       mem->surface->Data.Pitch = mem->destination_pitches[0];
105       break;
106     case GST_VIDEO_FORMAT_UYVY:
107       mem->surface->Data.Y = mem->cached_data[0];
108       mem->surface->Data.U = mem->surface->Data.Y;
109       mem->surface->Data.V = mem->surface->Data.U + 2;
110       mem->surface->Data.Pitch = mem->destination_pitches[0];
111       break;
112     case GST_VIDEO_FORMAT_BGRA:
113     case GST_VIDEO_FORMAT_BGRx:
114       mem->surface->Data.B = mem->cached_data[0];
115       mem->surface->Data.G = mem->surface->Data.B + 1;
116       mem->surface->Data.R = mem->surface->Data.B + 2;
117       mem->surface->Data.A = mem->surface->Data.B + 3;
118       mem->surface->Data.Pitch = mem->destination_pitches[0];
119       break;
120 #if (MFX_VERSION >= 1028)
121     case GST_VIDEO_FORMAT_RGB16:
122       mem->surface->Data.R = mem->cached_data[0];
123       mem->surface->Data.G = mem->surface->Data.R;
124       mem->surface->Data.B = mem->surface->Data.R;
125       mem->surface->Data.Pitch = mem->destination_pitches[0];
126       break;
127 #endif
128     case GST_VIDEO_FORMAT_VUYA:
129       mem->surface->Data.V = mem->cached_data[0];
130       mem->surface->Data.U = mem->surface->Data.V + 1;
131       mem->surface->Data.Y = mem->surface->Data.V + 2;
132       mem->surface->Data.A = mem->surface->Data.V + 3;
133       mem->surface->Data.PitchHigh =
134           (mfxU16) (mem->destination_pitches[0] / (1 << 16));
135       mem->surface->Data.PitchLow =
136           (mfxU16) (mem->destination_pitches[0] % (1 << 16));
137       break;
138     case GST_VIDEO_FORMAT_BGR10A2_LE:
139       mem->surface->Data.R = mem->cached_data[0];
140       mem->surface->Data.G = mem->surface->Data.R;
141       mem->surface->Data.B = mem->surface->Data.R;
142       mem->surface->Data.A = mem->surface->Data.R;
143       mem->surface->Data.Pitch = mem->destination_pitches[0];
144       break;
145     case GST_VIDEO_FORMAT_Y210:
146     case GST_VIDEO_FORMAT_Y212_LE:
147       mem->surface->Data.Y = mem->cached_data[0];
148       mem->surface->Data.U = mem->surface->Data.Y + 2;
149       mem->surface->Data.V = mem->surface->Data.Y + 6;
150       mem->surface->Data.Pitch = mem->destination_pitches[0];
151       break;
152     case GST_VIDEO_FORMAT_Y410:
153       mem->surface->Data.U = mem->cached_data[0];       /* Data.Y410 */
154       mem->surface->Data.Pitch = mem->destination_pitches[0];
155       break;
156     case GST_VIDEO_FORMAT_Y412_LE:
157       mem->surface->Data.U = mem->cached_data[0];
158       mem->surface->Data.Y = mem->surface->Data.U + 2;
159       mem->surface->Data.V = mem->surface->Data.U + 4;
160       mem->surface->Data.A = mem->surface->Data.U + 6;
161       mem->surface->Data.Pitch = mem->destination_pitches[0];
162       break;
163 #if (MFX_VERSION >= 2004)
164     case GST_VIDEO_FORMAT_RGBP:
165       mem->surface->Data.Pitch = mem->destination_pitches[0];
166       mem->surface->Data.R = mem->cached_data[0];
167       mem->surface->Data.G = mem->cached_data[1];
168       mem->surface->Data.B = mem->cached_data[2];
169       break;
170     case GST_VIDEO_FORMAT_BGRP:
171       mem->surface->Data.Pitch = mem->destination_pitches[0];
172       mem->surface->Data.B = mem->cached_data[0];
173       mem->surface->Data.G = mem->cached_data[1];
174       mem->surface->Data.R = mem->cached_data[2];
175       break;
176 #endif
177 
178     default:
179       g_assert_not_reached ();
180       break;
181   }
182 
183   return TRUE;
184 }
185 
186 static mfxFrameSurface1 *
gst_msdk_system_allocator_create_surface(GstAllocator * allocator)187 gst_msdk_system_allocator_create_surface (GstAllocator * allocator)
188 {
189   mfxFrameInfo frame_info = { {0,}, 0, };
190   mfxFrameSurface1 *surface;
191   GstMsdkSystemAllocator *msdk_system_allocator =
192       GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
193 
194   surface = (mfxFrameSurface1 *) g_slice_new0 (mfxFrameSurface1);
195 
196   if (!surface) {
197     GST_ERROR ("failed to allocate surface");
198     return NULL;
199   }
200 
201   gst_msdk_set_mfx_frame_info_from_video_info (&frame_info,
202       &msdk_system_allocator->image_info);
203 
204   surface->Info = frame_info;
205 
206   return surface;
207 }
208 
209 GstMemory *
gst_msdk_system_memory_new(GstAllocator * base_allocator)210 gst_msdk_system_memory_new (GstAllocator * base_allocator)
211 {
212   GstMsdkSystemAllocator *allocator;
213   GstVideoInfo *vip;
214   GstMsdkSystemMemory *mem;
215 
216   g_return_val_if_fail (base_allocator, NULL);
217   g_return_val_if_fail (GST_IS_MSDK_SYSTEM_ALLOCATOR (base_allocator), NULL);
218 
219   allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_allocator);
220 
221   mem = g_slice_new0 (GstMsdkSystemMemory);
222   if (!mem)
223     return NULL;
224 
225   mem->surface = gst_msdk_system_allocator_create_surface (base_allocator);
226 
227   if (!mem->surface) {
228     g_slice_free (GstMsdkSystemMemory, mem);
229     return NULL;
230   }
231 
232   vip = &allocator->image_info;
233   gst_memory_init (&mem->parent_instance, 0,
234       base_allocator, NULL, GST_VIDEO_INFO_SIZE (vip), 0, 0,
235       GST_VIDEO_INFO_SIZE (vip));
236 
237   if (!ensure_data (mem)) {
238     g_slice_free (mfxFrameSurface1, mem->surface);
239     g_slice_free (GstMsdkSystemMemory, mem);
240     return NULL;
241   }
242 
243   return GST_MEMORY_CAST (mem);
244 }
245 
246 static gpointer
gst_msdk_system_memory_map_full(GstMemory * base_mem,GstMapInfo * info,gsize maxsize)247 gst_msdk_system_memory_map_full (GstMemory * base_mem, GstMapInfo * info,
248     gsize maxsize)
249 {
250   GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
251 
252   g_return_val_if_fail (mem, NULL);
253 
254   if (!mem->surface) {
255     GST_WARNING ("The surface is not allocated");
256     return NULL;
257   }
258 
259   if ((info->flags & GST_MAP_WRITE) && mem->surface
260       && mem->surface->Data.Locked) {
261     GST_WARNING ("The surface in memory %p is not still available", mem);
262     return NULL;
263   }
264 
265   switch (mem->surface->Info.FourCC) {
266     case MFX_FOURCC_RGB4:
267       return mem->surface->Data.B;      /* The first channel is B */
268 
269       /* The first channel in memory is V for MFX_FOURCC_AYUV (GST_VIDEO_FORMAT_VUYA) format */
270     case MFX_FOURCC_AYUV:
271       return mem->surface->Data.V;
272 
273 #if (MFX_VERSION >= 1027)
274     case MFX_FOURCC_Y410:
275       return mem->surface->Data.U;      /* Data.Y410 */
276 #endif
277 
278 #if (MFX_VERSION >= 1031)
279     case MFX_FOURCC_Y416:
280       return mem->surface->Data.U;      /* The first channel is U */
281 #endif
282 
283 #if (MFX_VERSION >= 2004)
284     case MFX_FOURCC_RGBP:
285       return mem->surface->Data.R;
286 
287     case MFX_FOURCC_BGRP:
288       return mem->surface->Data.B;
289 #endif
290 
291     default:
292       return mem->surface->Data.Y;
293   }
294 }
295 
296 static void
gst_msdk_system_memory_unmap(GstMemory * base_mem)297 gst_msdk_system_memory_unmap (GstMemory * base_mem)
298 {
299 }
300 
301 static GstMemory *
gst_msdk_system_memory_copy(GstMemory * base_mem,gssize offset,gssize size)302 gst_msdk_system_memory_copy (GstMemory * base_mem, gssize offset, gssize size)
303 {
304   GstMsdkSystemMemory *copy;
305   GstVideoInfo *info;
306   GstMsdkSystemAllocator *msdk_allocator;
307   gsize mem_size;
308 
309   /* FIXME: can we consider offset and size here ? */
310   copy =
311       (GstMsdkSystemMemory *) gst_msdk_system_memory_new (base_mem->allocator);
312 
313   msdk_allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_mem->allocator);
314 
315   info = &msdk_allocator->image_info;
316   mem_size = GST_VIDEO_INFO_SIZE (info);
317 
318   memcpy (copy->cache, GST_MSDK_SYSTEM_MEMORY_CAST (base_mem)->cache, mem_size);
319 
320   return GST_MEMORY_CAST (copy);
321 }
322 
323 /* GstMsdkSystemAllocator */
324 G_DEFINE_TYPE (GstMsdkSystemAllocator, gst_msdk_system_allocator,
325     GST_TYPE_ALLOCATOR);
326 
327 static void
gst_msdk_system_allocator_free(GstAllocator * allocator,GstMemory * base_mem)328 gst_msdk_system_allocator_free (GstAllocator * allocator, GstMemory * base_mem)
329 {
330   GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
331 
332   _aligned_free (mem->cache);
333   g_slice_free (mfxFrameSurface1, mem->surface);
334   g_slice_free (GstMsdkSystemMemory, mem);
335 }
336 
337 static GstMemory *
gst_msdk_system_allocator_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)338 gst_msdk_system_allocator_alloc (GstAllocator * allocator, gsize size,
339     GstAllocationParams * params)
340 {
341   return gst_msdk_system_memory_new (allocator);
342 }
343 
344 static void
gst_msdk_system_allocator_class_init(GstMsdkSystemAllocatorClass * klass)345 gst_msdk_system_allocator_class_init (GstMsdkSystemAllocatorClass * klass)
346 {
347   GstAllocatorClass *const allocator_class = GST_ALLOCATOR_CLASS (klass);
348 
349   allocator_class->alloc = gst_msdk_system_allocator_alloc;
350   allocator_class->free = gst_msdk_system_allocator_free;
351 }
352 
353 static void
gst_msdk_system_allocator_init(GstMsdkSystemAllocator * allocator)354 gst_msdk_system_allocator_init (GstMsdkSystemAllocator * allocator)
355 {
356   GstAllocator *const base_allocator = GST_ALLOCATOR_CAST (allocator);
357 
358   base_allocator->mem_type = GST_MSDK_SYSTEM_MEMORY_NAME;
359   base_allocator->mem_map_full = gst_msdk_system_memory_map_full;
360   base_allocator->mem_unmap = gst_msdk_system_memory_unmap;
361   base_allocator->mem_copy = gst_msdk_system_memory_copy;
362 
363   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
364 }
365 
366 GstAllocator *
gst_msdk_system_allocator_new(GstVideoInfo * image_info)367 gst_msdk_system_allocator_new (GstVideoInfo * image_info)
368 {
369   GstMsdkSystemAllocator *allocator;
370 
371   g_return_val_if_fail (image_info != NULL, NULL);
372 
373   allocator = g_object_new (GST_TYPE_MSDK_SYSTEM_ALLOCATOR, NULL);
374   if (!allocator)
375     return NULL;
376 
377   allocator->image_info = *image_info;
378 
379   return GST_ALLOCATOR_CAST (allocator);
380 }
381