1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
3 * Copyright (c) 2018, Igalia S.L.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
37 #include "gstmsdksystemmemory.h"
41 #define posix_memalign(d, a, s) ((*((void**)d) = _aligned_malloc(s, a)) ? 0 : -1)
45 #define _aligned_free free
49 ensure_data (GstMsdkSystemMemory * mem)
54 GstAllocator *allocator;
55 GstMsdkSystemAllocator *msdk_allocator;
57 allocator = GST_MEMORY_CAST (mem)->allocator;
58 msdk_allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
60 info = &msdk_allocator->image_info;
61 size = GST_VIDEO_INFO_SIZE (info);
66 if (posix_memalign (&data, 32, size) != 0) {
67 GST_ERROR ("Memory allocation failed");
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);
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);
80 switch (GST_VIDEO_INFO_FORMAT (info)) {
81 case GST_VIDEO_FORMAT_NV12:
82 case GST_VIDEO_FORMAT_P010_10LE:
83 mem->surface->Data.Y = mem->cached_data[0];
84 mem->surface->Data.UV = mem->cached_data[1];
85 mem->surface->Data.Pitch = mem->destination_pitches[0];
87 case GST_VIDEO_FORMAT_YV12:
88 mem->surface->Data.Y = mem->cached_data[0];
89 mem->surface->Data.U = mem->cached_data[2];
90 mem->surface->Data.V = mem->cached_data[1];
91 mem->surface->Data.Pitch = mem->destination_pitches[0];
93 case GST_VIDEO_FORMAT_I420:
94 mem->surface->Data.Y = mem->cached_data[0];
95 mem->surface->Data.U = mem->cached_data[1];
96 mem->surface->Data.V = mem->cached_data[2];
97 mem->surface->Data.Pitch = mem->destination_pitches[0];
99 case GST_VIDEO_FORMAT_YUY2:
100 mem->surface->Data.Y = mem->cached_data[0];
101 mem->surface->Data.U = mem->surface->Data.Y + 1;
102 mem->surface->Data.V = mem->surface->Data.Y + 3;
103 mem->surface->Data.Pitch = mem->destination_pitches[0];
105 case GST_VIDEO_FORMAT_UYVY:
106 mem->surface->Data.Y = mem->cached_data[0];
107 mem->surface->Data.U = mem->surface->Data.Y;
108 mem->surface->Data.V = mem->surface->Data.U + 2;
109 mem->surface->Data.Pitch = mem->destination_pitches[0];
111 case GST_VIDEO_FORMAT_BGRA:
112 mem->surface->Data.B = mem->cached_data[0];
113 mem->surface->Data.G = mem->surface->Data.B + 1;
114 mem->surface->Data.R = mem->surface->Data.B + 2;
115 mem->surface->Data.A = mem->surface->Data.B + 3;
116 mem->surface->Data.Pitch = mem->destination_pitches[0];
118 #if (MFX_VERSION >= 1028)
119 case GST_VIDEO_FORMAT_RGB16:
120 mem->surface->Data.R = mem->cached_data[0];
121 mem->surface->Data.G = mem->surface->Data.R;
122 mem->surface->Data.B = mem->surface->Data.R;
123 mem->surface->Data.Pitch = mem->destination_pitches[0];
126 case GST_VIDEO_FORMAT_VUYA:
127 mem->surface->Data.V = mem->cached_data[0];
128 mem->surface->Data.U = mem->surface->Data.V + 1;
129 mem->surface->Data.Y = mem->surface->Data.V + 2;
130 mem->surface->Data.A = mem->surface->Data.V + 3;
131 mem->surface->Data.PitchHigh =
132 (mfxU16) (mem->destination_pitches[0] / (1 << 16));
133 mem->surface->Data.PitchLow =
134 (mfxU16) (mem->destination_pitches[0] % (1 << 16));
136 case GST_VIDEO_FORMAT_BGR10A2_LE:
137 mem->surface->Data.R = mem->cached_data[0];
138 mem->surface->Data.G = mem->surface->Data.R;
139 mem->surface->Data.B = mem->surface->Data.R;
140 mem->surface->Data.A = mem->surface->Data.R;
141 mem->surface->Data.Pitch = mem->destination_pitches[0];
143 case GST_VIDEO_FORMAT_Y210:
144 mem->surface->Data.Y = mem->cached_data[0];
145 mem->surface->Data.U = mem->surface->Data.Y + 2;
146 mem->surface->Data.V = mem->surface->Data.Y + 6;
147 mem->surface->Data.Pitch = mem->destination_pitches[0];
149 case GST_VIDEO_FORMAT_Y410:
150 mem->surface->Data.U = mem->cached_data[0]; /* Data.Y410 */
151 mem->surface->Data.Pitch = mem->destination_pitches[0];
155 g_assert_not_reached ();
162 static mfxFrameSurface1 *
163 gst_msdk_system_allocator_create_surface (GstAllocator * allocator)
165 mfxFrameInfo frame_info = { {0,}, 0, };
166 mfxFrameSurface1 *surface;
167 GstMsdkSystemAllocator *msdk_system_allocator =
168 GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
170 surface = (mfxFrameSurface1 *) g_slice_new0 (mfxFrameSurface1);
173 GST_ERROR ("failed to allocate surface");
177 gst_msdk_set_mfx_frame_info_from_video_info (&frame_info,
178 &msdk_system_allocator->image_info);
180 surface->Info = frame_info;
186 gst_msdk_system_memory_new (GstAllocator * base_allocator)
188 GstMsdkSystemAllocator *allocator;
190 GstMsdkSystemMemory *mem;
192 g_return_val_if_fail (base_allocator, NULL);
193 g_return_val_if_fail (GST_IS_MSDK_SYSTEM_ALLOCATOR (base_allocator), NULL);
195 allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_allocator);
197 mem = g_slice_new0 (GstMsdkSystemMemory);
201 mem->surface = gst_msdk_system_allocator_create_surface (base_allocator);
204 g_slice_free (GstMsdkSystemMemory, mem);
208 vip = &allocator->image_info;
209 gst_memory_init (&mem->parent_instance, 0,
210 base_allocator, NULL, GST_VIDEO_INFO_SIZE (vip), 0, 0,
211 GST_VIDEO_INFO_SIZE (vip));
213 if (!ensure_data (mem)) {
214 g_slice_free (mfxFrameSurface1, mem->surface);
215 g_slice_free (GstMsdkSystemMemory, mem);
219 return GST_MEMORY_CAST (mem);
223 gst_msdk_system_memory_map_full (GstMemory * base_mem, GstMapInfo * info,
226 GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
228 g_return_val_if_fail (mem, NULL);
231 GST_WARNING ("The surface is not allocated");
235 if ((info->flags & GST_MAP_WRITE) && mem->surface
236 && mem->surface->Data.Locked) {
237 GST_WARNING ("The surface in memory %p is not still available", mem);
241 switch (mem->surface->Info.FourCC) {
242 case MFX_FOURCC_RGB4:
243 return mem->surface->Data.B; /* The first channel is B */
245 /* The first channel in memory is V for MFX_FOURCC_AYUV (GST_VIDEO_FORMAT_VUYA) format */
246 case MFX_FOURCC_AYUV:
247 return mem->surface->Data.V;
249 #if (MFX_VERSION >= 1027)
250 case MFX_FOURCC_Y410:
251 return mem->surface->Data.U; /* Data.Y410 */
255 return mem->surface->Data.Y;
260 gst_msdk_system_memory_unmap (GstMemory * base_mem)
265 gst_msdk_system_memory_copy (GstMemory * base_mem, gssize offset, gssize size)
267 GstMsdkSystemMemory *copy;
269 GstMsdkSystemAllocator *msdk_allocator;
272 /* FIXME: can we consider offset and size here ? */
274 (GstMsdkSystemMemory *) gst_msdk_system_memory_new (base_mem->allocator);
276 msdk_allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_mem->allocator);
278 info = &msdk_allocator->image_info;
279 mem_size = GST_VIDEO_INFO_SIZE (info);
281 memcpy (copy->cache, GST_MSDK_SYSTEM_MEMORY_CAST (base_mem)->cache, mem_size);
283 return GST_MEMORY_CAST (copy);
286 /* GstMsdkSystemAllocator */
287 G_DEFINE_TYPE (GstMsdkSystemAllocator, gst_msdk_system_allocator,
291 gst_msdk_system_allocator_free (GstAllocator * allocator, GstMemory * base_mem)
293 GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
295 _aligned_free (mem->cache);
296 g_slice_free (mfxFrameSurface1, mem->surface);
297 g_slice_free (GstMsdkSystemMemory, mem);
301 gst_msdk_system_allocator_alloc (GstAllocator * allocator, gsize size,
302 GstAllocationParams * params)
304 return gst_msdk_system_memory_new (allocator);
308 gst_msdk_system_allocator_class_init (GstMsdkSystemAllocatorClass * klass)
310 GstAllocatorClass *const allocator_class = GST_ALLOCATOR_CLASS (klass);
312 allocator_class->alloc = gst_msdk_system_allocator_alloc;
313 allocator_class->free = gst_msdk_system_allocator_free;
317 gst_msdk_system_allocator_init (GstMsdkSystemAllocator * allocator)
319 GstAllocator *const base_allocator = GST_ALLOCATOR_CAST (allocator);
321 base_allocator->mem_type = GST_MSDK_SYSTEM_MEMORY_NAME;
322 base_allocator->mem_map_full = gst_msdk_system_memory_map_full;
323 base_allocator->mem_unmap = gst_msdk_system_memory_unmap;
324 base_allocator->mem_copy = gst_msdk_system_memory_copy;
326 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
330 gst_msdk_system_allocator_new (GstVideoInfo * image_info)
332 GstMsdkSystemAllocator *allocator;
334 g_return_val_if_fail (image_info != NULL, NULL);
336 allocator = g_object_new (GST_TYPE_MSDK_SYSTEM_ALLOCATOR, NULL);
340 allocator->image_info = *image_info;
342 return GST_ALLOCATOR_CAST (allocator);