documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / sys / msdk / gstmsdksystemmemory.c
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
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       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];
86       break;
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];
92       break;
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];
98       break;
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];
104       break;
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];
110       break;
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];
117       break;
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];
124       break;
125 #endif
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));
135       break;
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];
142       break;
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];
148       break;
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];
152       break;
153
154     default:
155       g_assert_not_reached ();
156       break;
157   }
158
159   return TRUE;
160 }
161
162 static mfxFrameSurface1 *
163 gst_msdk_system_allocator_create_surface (GstAllocator * allocator)
164 {
165   mfxFrameInfo frame_info = { {0,}, 0, };
166   mfxFrameSurface1 *surface;
167   GstMsdkSystemAllocator *msdk_system_allocator =
168       GST_MSDK_SYSTEM_ALLOCATOR_CAST (allocator);
169
170   surface = (mfxFrameSurface1 *) g_slice_new0 (mfxFrameSurface1);
171
172   if (!surface) {
173     GST_ERROR ("failed to allocate surface");
174     return NULL;
175   }
176
177   gst_msdk_set_mfx_frame_info_from_video_info (&frame_info,
178       &msdk_system_allocator->image_info);
179
180   surface->Info = frame_info;
181
182   return surface;
183 }
184
185 GstMemory *
186 gst_msdk_system_memory_new (GstAllocator * base_allocator)
187 {
188   GstMsdkSystemAllocator *allocator;
189   GstVideoInfo *vip;
190   GstMsdkSystemMemory *mem;
191
192   g_return_val_if_fail (base_allocator, NULL);
193   g_return_val_if_fail (GST_IS_MSDK_SYSTEM_ALLOCATOR (base_allocator), NULL);
194
195   allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_allocator);
196
197   mem = g_slice_new0 (GstMsdkSystemMemory);
198   if (!mem)
199     return NULL;
200
201   mem->surface = gst_msdk_system_allocator_create_surface (base_allocator);
202
203   if (!mem->surface) {
204     g_slice_free (GstMsdkSystemMemory, mem);
205     return NULL;
206   }
207
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));
212
213   if (!ensure_data (mem)) {
214     g_slice_free (mfxFrameSurface1, mem->surface);
215     g_slice_free (GstMsdkSystemMemory, mem);
216     return NULL;
217   }
218
219   return GST_MEMORY_CAST (mem);
220 }
221
222 static gpointer
223 gst_msdk_system_memory_map_full (GstMemory * base_mem, GstMapInfo * info,
224     gsize maxsize)
225 {
226   GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
227
228   g_return_val_if_fail (mem, NULL);
229
230   if (!mem->surface) {
231     GST_WARNING ("The surface is not allocated");
232     return NULL;
233   }
234
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);
238     return NULL;
239   }
240
241   switch (mem->surface->Info.FourCC) {
242     case MFX_FOURCC_RGB4:
243       return mem->surface->Data.B;      /* The first channel is B */
244
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;
248
249 #if (MFX_VERSION >= 1027)
250     case MFX_FOURCC_Y410:
251       return mem->surface->Data.U;      /* Data.Y410 */
252 #endif
253
254     default:
255       return mem->surface->Data.Y;
256   }
257 }
258
259 static void
260 gst_msdk_system_memory_unmap (GstMemory * base_mem)
261 {
262 }
263
264 static GstMemory *
265 gst_msdk_system_memory_copy (GstMemory * base_mem, gssize offset, gssize size)
266 {
267   GstMsdkSystemMemory *copy;
268   GstVideoInfo *info;
269   GstMsdkSystemAllocator *msdk_allocator;
270   gsize mem_size;
271
272   /* FIXME: can we consider offset and size here ? */
273   copy =
274       (GstMsdkSystemMemory *) gst_msdk_system_memory_new (base_mem->allocator);
275
276   msdk_allocator = GST_MSDK_SYSTEM_ALLOCATOR_CAST (base_mem->allocator);
277
278   info = &msdk_allocator->image_info;
279   mem_size = GST_VIDEO_INFO_SIZE (info);
280
281   memcpy (copy->cache, GST_MSDK_SYSTEM_MEMORY_CAST (base_mem)->cache, mem_size);
282
283   return GST_MEMORY_CAST (copy);
284 }
285
286 /* GstMsdkSystemAllocator */
287 G_DEFINE_TYPE (GstMsdkSystemAllocator, gst_msdk_system_allocator,
288     GST_TYPE_ALLOCATOR);
289
290 static void
291 gst_msdk_system_allocator_free (GstAllocator * allocator, GstMemory * base_mem)
292 {
293   GstMsdkSystemMemory *const mem = GST_MSDK_SYSTEM_MEMORY_CAST (base_mem);
294
295   _aligned_free (mem->cache);
296   g_slice_free (mfxFrameSurface1, mem->surface);
297   g_slice_free (GstMsdkSystemMemory, mem);
298 }
299
300 static GstMemory *
301 gst_msdk_system_allocator_alloc (GstAllocator * allocator, gsize size,
302     GstAllocationParams * params)
303 {
304   return gst_msdk_system_memory_new (allocator);
305 }
306
307 static void
308 gst_msdk_system_allocator_class_init (GstMsdkSystemAllocatorClass * klass)
309 {
310   GstAllocatorClass *const allocator_class = GST_ALLOCATOR_CLASS (klass);
311
312   allocator_class->alloc = gst_msdk_system_allocator_alloc;
313   allocator_class->free = gst_msdk_system_allocator_free;
314 }
315
316 static void
317 gst_msdk_system_allocator_init (GstMsdkSystemAllocator * allocator)
318 {
319   GstAllocator *const base_allocator = GST_ALLOCATOR_CAST (allocator);
320
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;
325
326   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
327 }
328
329 GstAllocator *
330 gst_msdk_system_allocator_new (GstVideoInfo * image_info)
331 {
332   GstMsdkSystemAllocator *allocator;
333
334   g_return_val_if_fail (image_info != NULL, NULL);
335
336   allocator = g_object_new (GST_TYPE_MSDK_SYSTEM_ALLOCATOR, NULL);
337   if (!allocator)
338     return NULL;
339
340   allocator->image_info = *image_info;
341
342   return GST_ALLOCATOR_CAST (allocator);
343 }