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 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];
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];
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];
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];
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];
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];
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];
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));
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];
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];
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];
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];
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];
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];
179 g_assert_not_reached ();
186 static mfxFrameSurface1 *
187 gst_msdk_system_allocator_create_surface (GstAllocator * allocator)
189 mfxFrameInfo frame_info = { {0,}, 0, };
190 mfxFrameSurface1 *surface;
191 GstMsdkSystemAllocator *msdk_system_allocator =
192 GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
194 surface = (mfxFrameSurface1 *) g_slice_new0 (mfxFrameSurface1);
197 GST_ERROR ("failed to allocate surface");
201 gst_msdk_set_mfx_frame_info_from_video_info (&frame_info,
202 &msdk_system_allocator->image_info);
204 surface->Info = frame_info;
210 gst_msdk_system_memory_new (GstAllocator * base_allocator)
212 GstMsdkSystemAllocator *allocator;
214 GstMsdkSystemMemory *mem;
216 g_return_val_if_fail (base_allocator, NULL);
217 g_return_val_if_fail (GST_IS_MSDK_SYSTEM_ALLOCATOR (base_allocator), NULL);
219 allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_allocator);
221 mem = g_slice_new0 (GstMsdkSystemMemory);
225 mem->surface = gst_msdk_system_allocator_create_surface (base_allocator);
228 g_slice_free (GstMsdkSystemMemory, mem);
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));
237 if (!ensure_data (mem)) {
238 g_slice_free (mfxFrameSurface1, mem->surface);
239 g_slice_free (GstMsdkSystemMemory, mem);
243 return GST_MEMORY_CAST (mem);
247 gst_msdk_system_memory_map_full (GstMemory * base_mem, GstMapInfo * info,
250 GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
252 g_return_val_if_fail (mem, NULL);
255 GST_WARNING ("The surface is not allocated");
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);
265 switch (mem->surface->Info.FourCC) {
266 case MFX_FOURCC_RGB4:
267 return mem->surface->Data.B; /* The first channel is B */
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;
273 #if (MFX_VERSION >= 1027)
274 case MFX_FOURCC_Y410:
275 return mem->surface->Data.U; /* Data.Y410 */
278 #if (MFX_VERSION >= 1031)
279 case MFX_FOURCC_Y416:
280 return mem->surface->Data.U; /* The first channel is U */
283 #if (MFX_VERSION >= 2004)
284 case MFX_FOURCC_RGBP:
285 return mem->surface->Data.R;
287 case MFX_FOURCC_BGRP:
288 return mem->surface->Data.B;
292 return mem->surface->Data.Y;
297 gst_msdk_system_memory_unmap (GstMemory * base_mem)
302 gst_msdk_system_memory_copy (GstMemory * base_mem, gssize offset, gssize size)
304 GstMsdkSystemMemory *copy;
306 GstMsdkSystemAllocator *msdk_allocator;
309 /* FIXME: can we consider offset and size here ? */
311 (GstMsdkSystemMemory *) gst_msdk_system_memory_new (base_mem->allocator);
313 msdk_allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_mem->allocator);
315 info = &msdk_allocator->image_info;
316 mem_size = GST_VIDEO_INFO_SIZE (info);
318 memcpy (copy->cache, GST_MSDK_SYSTEM_MEMORY_CAST (base_mem)->cache, mem_size);
320 return GST_MEMORY_CAST (copy);
323 /* GstMsdkSystemAllocator */
324 G_DEFINE_TYPE (GstMsdkSystemAllocator, gst_msdk_system_allocator,
328 gst_msdk_system_allocator_free (GstAllocator * allocator, GstMemory * base_mem)
330 GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
332 _aligned_free (mem->cache);
333 g_slice_free (mfxFrameSurface1, mem->surface);
334 g_slice_free (GstMsdkSystemMemory, mem);
338 gst_msdk_system_allocator_alloc (GstAllocator * allocator, gsize size,
339 GstAllocationParams * params)
341 return gst_msdk_system_memory_new (allocator);
345 gst_msdk_system_allocator_class_init (GstMsdkSystemAllocatorClass * klass)
347 GstAllocatorClass *const allocator_class = GST_ALLOCATOR_CLASS (klass);
349 allocator_class->alloc = gst_msdk_system_allocator_alloc;
350 allocator_class->free = gst_msdk_system_allocator_free;
354 gst_msdk_system_allocator_init (GstMsdkSystemAllocator * allocator)
356 GstAllocator *const base_allocator = GST_ALLOCATOR_CAST (allocator);
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;
363 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
367 gst_msdk_system_allocator_new (GstVideoInfo * image_info)
369 GstMsdkSystemAllocator *allocator;
371 g_return_val_if_fail (image_info != NULL, NULL);
373 allocator = g_object_new (GST_TYPE_MSDK_SYSTEM_ALLOCATOR, NULL);
377 allocator->image_info = *image_info;
379 return GST_ALLOCATOR_CAST (allocator);