taglist, plugins: fix compiler warnings with GLib >= 2.76
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / msdk / gstmsdkallocator.c
1 /*
2  * GStreamer Intel MSDK plugin
3  * Copyright (c) 2022 Intel Corporation. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "gstmsdkallocator.h"
33
34 static gboolean
35 map_data (GstBuffer * buffer, mfxFrameSurface1 * mfx_surface, GstVideoInfo info)
36 {
37   guint stride;
38   GstVideoFrame frame;
39
40   if (!gst_video_frame_map (&frame, &info, buffer, GST_MAP_READWRITE))
41     return FALSE;
42
43   stride = GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0);
44
45   switch (GST_VIDEO_INFO_FORMAT (&info)) {
46     case GST_VIDEO_FORMAT_NV12:
47     case GST_VIDEO_FORMAT_P010_10LE:
48     case GST_VIDEO_FORMAT_P012_LE:
49       mfx_surface->Data.Y = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
50       mfx_surface->Data.UV = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 1);
51       mfx_surface->Data.Pitch = (mfxU16) stride;
52       break;
53     case GST_VIDEO_FORMAT_YV12:
54       mfx_surface->Data.Y = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
55       mfx_surface->Data.U = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 2);
56       mfx_surface->Data.V = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 1);
57       mfx_surface->Data.Pitch = (mfxU16) stride;
58       break;
59     case GST_VIDEO_FORMAT_I420:
60       mfx_surface->Data.Y = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
61       mfx_surface->Data.U = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 1);
62       mfx_surface->Data.V = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 2);
63       mfx_surface->Data.Pitch = (mfxU16) stride;
64       break;
65     case GST_VIDEO_FORMAT_YUY2:
66       mfx_surface->Data.Y = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
67       mfx_surface->Data.U = mfx_surface->Data.Y + 1;
68       mfx_surface->Data.V = mfx_surface->Data.Y + 3;
69       mfx_surface->Data.Pitch = (mfxU16) stride;
70       break;
71     case GST_VIDEO_FORMAT_UYVY:
72       mfx_surface->Data.Y = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
73       mfx_surface->Data.U = mfx_surface->Data.Y;
74       mfx_surface->Data.V = mfx_surface->Data.U + 2;
75       mfx_surface->Data.Pitch = (mfxU16) stride;
76       break;
77     case GST_VIDEO_FORMAT_VUYA:
78       mfx_surface->Data.V = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
79       mfx_surface->Data.U = mfx_surface->Data.V + 1;
80       mfx_surface->Data.Y = mfx_surface->Data.V + 2;
81       mfx_surface->Data.A = mfx_surface->Data.V + 3;
82       mfx_surface->Data.PitchHigh = (mfxU16) (stride / (1 << 16));
83       mfx_surface->Data.PitchLow = (mfxU16) (stride % (1 << 16));
84       break;
85     case GST_VIDEO_FORMAT_BGRA:
86     case GST_VIDEO_FORMAT_BGRx:
87       mfx_surface->Data.B = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
88       mfx_surface->Data.G = mfx_surface->Data.B + 1;
89       mfx_surface->Data.R = mfx_surface->Data.B + 2;
90       mfx_surface->Data.A = mfx_surface->Data.B + 3;
91       mfx_surface->Data.Pitch = (mfxU16) stride;
92       break;
93     case GST_VIDEO_FORMAT_Y210:
94     case GST_VIDEO_FORMAT_Y212_LE:
95       mfx_surface->Data.Y = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
96       mfx_surface->Data.U = mfx_surface->Data.Y + 2;
97       mfx_surface->Data.V = mfx_surface->Data.Y + 6;
98       mfx_surface->Data.Pitch = (mfxU16) stride;
99       break;
100     case GST_VIDEO_FORMAT_Y410:
101       mfx_surface->Data.Y410 =
102           (mfxY410 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
103       mfx_surface->Data.Pitch = stride;
104       break;
105     case GST_VIDEO_FORMAT_Y412_LE:
106       mfx_surface->Data.U = (mfxU8 *) GST_VIDEO_FRAME_PLANE_DATA (&frame, 0);
107       mfx_surface->Data.Y = mfx_surface->Data.Y + 2;
108       mfx_surface->Data.V = mfx_surface->Data.Y + 4;
109       mfx_surface->Data.A = mfx_surface->Data.Y + 6;
110       mfx_surface->Data.Pitch = (mfxU16) stride;
111       break;
112     default:
113       g_assert_not_reached ();
114       break;
115   }
116
117   gst_video_frame_unmap (&frame);
118   return TRUE;
119 }
120
121 GstMsdkSurface *
122 gst_msdk_import_sys_mem_to_msdk_surface (GstBuffer * buf, GstVideoInfo info)
123 {
124   GstMsdkSurface *msdk_surface = NULL;
125   GstMapInfo map_info;
126   mfxFrameInfo frame_info = { 0, };
127   mfxFrameSurface1 *mfx_surface = NULL;
128
129   if (!gst_buffer_map (buf, &map_info, GST_MAP_READ)) {
130     GST_ERROR ("Failed to map buffer");
131     return msdk_surface;
132   }
133
134   mfx_surface = g_slice_new0 (mfxFrameSurface1);
135   mfx_surface->Data.MemId = (mfxMemId) map_info.data;
136
137   if (!map_data (buf, mfx_surface, info)) {
138     g_slice_free (mfxFrameSurface1, mfx_surface);
139     return msdk_surface;
140   }
141
142   gst_buffer_unmap (buf, &map_info);
143
144   gst_msdk_set_mfx_frame_info_from_video_info (&frame_info, &info);
145   mfx_surface->Info = frame_info;
146
147   msdk_surface = g_slice_new0 (GstMsdkSurface);
148   msdk_surface->surface = mfx_surface;
149
150   return msdk_surface;
151 }