1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2011 Intel Corporation
4  * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2014 Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include "wlvideoformat.h"
29 
30 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
31 #define GST_CAT_DEFAULT gstwayland_debug
32 
33 typedef struct
34 {
35   enum wl_shm_format wl_format;
36   GstVideoFormat gst_format;
37 } wl_ShmVideoFormat;
38 
39 typedef struct
40 {
41   guint wl_format;
42   GstVideoFormat gst_format;
43 } wl_DmabufVideoFormat;
44 
45 static const wl_ShmVideoFormat shm_formats[] = {
46 #if G_BYTE_ORDER == G_BIG_ENDIAN
47   {WL_SHM_FORMAT_XRGB8888, GST_VIDEO_FORMAT_xRGB},
48   {WL_SHM_FORMAT_ARGB8888, GST_VIDEO_FORMAT_ARGB},
49   {WL_SHM_FORMAT_XBGR8888, GST_VIDEO_FORMAT_xBGR},
50   {WL_SHM_FORMAT_RGBX8888, GST_VIDEO_FORMAT_RGBx},
51   {WL_SHM_FORMAT_BGRX8888, GST_VIDEO_FORMAT_BGRx},
52   {WL_SHM_FORMAT_ABGR8888, GST_VIDEO_FORMAT_ABGR},
53   {WL_SHM_FORMAT_RGBA8888, GST_VIDEO_FORMAT_RGBA},
54   {WL_SHM_FORMAT_BGRA8888, GST_VIDEO_FORMAT_BGRA},
55 #else
56   {WL_SHM_FORMAT_XRGB8888, GST_VIDEO_FORMAT_BGRx},
57   {WL_SHM_FORMAT_ARGB8888, GST_VIDEO_FORMAT_BGRA},
58   {WL_SHM_FORMAT_XBGR8888, GST_VIDEO_FORMAT_RGBx},
59   {WL_SHM_FORMAT_RGBX8888, GST_VIDEO_FORMAT_xBGR},
60   {WL_SHM_FORMAT_BGRX8888, GST_VIDEO_FORMAT_xRGB},
61   {WL_SHM_FORMAT_ABGR8888, GST_VIDEO_FORMAT_RGBA},
62   {WL_SHM_FORMAT_RGBA8888, GST_VIDEO_FORMAT_ABGR},
63   {WL_SHM_FORMAT_BGRA8888, GST_VIDEO_FORMAT_ARGB},
64 #endif
65   {WL_SHM_FORMAT_RGB888, GST_VIDEO_FORMAT_RGB},
66   {WL_SHM_FORMAT_BGR888, GST_VIDEO_FORMAT_BGR},
67   {WL_SHM_FORMAT_RGB565, GST_VIDEO_FORMAT_RGB16},
68   {WL_SHM_FORMAT_BGR565, GST_VIDEO_FORMAT_BGR16},
69 
70   {WL_SHM_FORMAT_YUYV, GST_VIDEO_FORMAT_YUY2},
71   {WL_SHM_FORMAT_YVYU, GST_VIDEO_FORMAT_YVYU},
72   {WL_SHM_FORMAT_UYVY, GST_VIDEO_FORMAT_UYVY},
73   {WL_SHM_FORMAT_AYUV, GST_VIDEO_FORMAT_AYUV},
74   {WL_SHM_FORMAT_NV12, GST_VIDEO_FORMAT_NV12},
75   {WL_SHM_FORMAT_NV21, GST_VIDEO_FORMAT_NV21},
76   {WL_SHM_FORMAT_NV16, GST_VIDEO_FORMAT_NV16},
77   {WL_SHM_FORMAT_YUV410, GST_VIDEO_FORMAT_YUV9},
78   {WL_SHM_FORMAT_YVU410, GST_VIDEO_FORMAT_YVU9},
79   {WL_SHM_FORMAT_YUV411, GST_VIDEO_FORMAT_Y41B},
80   {WL_SHM_FORMAT_YUV420, GST_VIDEO_FORMAT_I420},
81   {WL_SHM_FORMAT_YVU420, GST_VIDEO_FORMAT_YV12},
82   {WL_SHM_FORMAT_YUV422, GST_VIDEO_FORMAT_Y42B},
83   {WL_SHM_FORMAT_YUV444, GST_VIDEO_FORMAT_v308},
84 };
85 
86 static const wl_DmabufVideoFormat dmabuf_formats[] = {
87 #if G_BYTE_ORDER == G_BIG_ENDIAN
88   {DRM_FORMAT_XRGB8888, GST_VIDEO_FORMAT_xRGB},
89   {DRM_FORMAT_ARGB8888, GST_VIDEO_FORMAT_ARGB},
90   {DRM_FORMAT_XBGR8888, GST_VIDEO_FORMAT_xBGR},
91   {DRM_FORMAT_RGBX8888, GST_VIDEO_FORMAT_RGBx},
92   {DRM_FORMAT_BGRX8888, GST_VIDEO_FORMAT_BGRx},
93   {DRM_FORMAT_ABGR8888, GST_VIDEO_FORMAT_ABGR},
94   {DRM_FORMAT_RGBA8888, GST_VIDEO_FORMAT_RGBA},
95   {DRM_FORMAT_BGRA8888, GST_VIDEO_FORMAT_BGRA},
96   {DRM_FORMAT_RGB888, GST_VIDEO_FORMAT_RGB},
97   {DRM_FORMAT_BGR888, GST_VIDEO_FORMAT_BGR},
98 #else
99   {DRM_FORMAT_XRGB8888, GST_VIDEO_FORMAT_BGRx},
100   {DRM_FORMAT_ARGB8888, GST_VIDEO_FORMAT_BGRA},
101   {DRM_FORMAT_XBGR8888, GST_VIDEO_FORMAT_RGBx},
102   {DRM_FORMAT_RGBX8888, GST_VIDEO_FORMAT_xBGR},
103   {DRM_FORMAT_BGRX8888, GST_VIDEO_FORMAT_xRGB},
104   {DRM_FORMAT_ABGR8888, GST_VIDEO_FORMAT_RGBA},
105   {DRM_FORMAT_RGBA8888, GST_VIDEO_FORMAT_ABGR},
106   {DRM_FORMAT_BGRA8888, GST_VIDEO_FORMAT_ARGB},
107   {DRM_FORMAT_RGB888, GST_VIDEO_FORMAT_BGR},
108   {DRM_FORMAT_BGR888, GST_VIDEO_FORMAT_RGB},
109 #endif
110   {DRM_FORMAT_RGB565, GST_VIDEO_FORMAT_RGB16},
111   {DRM_FORMAT_YUYV, GST_VIDEO_FORMAT_YUY2},
112   {DRM_FORMAT_NV12, GST_VIDEO_FORMAT_NV12},
113 };
114 
115 enum wl_shm_format
gst_video_format_to_wl_shm_format(GstVideoFormat format)116 gst_video_format_to_wl_shm_format (GstVideoFormat format)
117 {
118   guint i;
119 
120   for (i = 0; i < G_N_ELEMENTS (shm_formats); i++)
121     if (shm_formats[i].gst_format == format)
122       return shm_formats[i].wl_format;
123 
124   GST_WARNING ("wayland shm video format not found");
125   return -1;
126 }
127 
128 gint
gst_video_format_to_wl_dmabuf_format(GstVideoFormat format)129 gst_video_format_to_wl_dmabuf_format (GstVideoFormat format)
130 {
131   guint i;
132 
133   for (i = 0; i < G_N_ELEMENTS (dmabuf_formats); i++)
134     if (dmabuf_formats[i].gst_format == format)
135       return dmabuf_formats[i].wl_format;
136 
137   GST_WARNING ("wayland dmabuf video format not found");
138   return -1;
139 }
140 
141 GstVideoFormat
gst_wl_shm_format_to_video_format(enum wl_shm_format wl_format)142 gst_wl_shm_format_to_video_format (enum wl_shm_format wl_format)
143 {
144   guint i;
145 
146   for (i = 0; i < G_N_ELEMENTS (shm_formats); i++)
147     if (shm_formats[i].wl_format == wl_format)
148       return shm_formats[i].gst_format;
149 
150   return GST_VIDEO_FORMAT_UNKNOWN;
151 }
152 
153 GstVideoFormat
gst_wl_dmabuf_format_to_video_format(guint wl_format)154 gst_wl_dmabuf_format_to_video_format (guint wl_format)
155 {
156   guint i;
157 
158   for (i = 0; i < G_N_ELEMENTS (dmabuf_formats); i++)
159     if (dmabuf_formats[i].wl_format == wl_format)
160       return dmabuf_formats[i].gst_format;
161 
162   return GST_VIDEO_FORMAT_UNKNOWN;
163 }
164 
165 const gchar *
gst_wl_shm_format_to_string(enum wl_shm_format wl_format)166 gst_wl_shm_format_to_string (enum wl_shm_format wl_format)
167 {
168   return gst_video_format_to_string
169       (gst_wl_shm_format_to_video_format (wl_format));
170 }
171 
172 const gchar *
gst_wl_dmabuf_format_to_string(guint wl_format)173 gst_wl_dmabuf_format_to_string (guint wl_format)
174 {
175   return gst_video_format_to_string
176       (gst_wl_dmabuf_format_to_video_format (wl_format));
177 }
178