kmssink: Remove big endian format inversion
[platform/upstream/gstreamer.git] / sys / kms / gstkmsutils.c
1 /* GStreamer
2  *
3  * Copyright (C) 2016 Igalia
4  *
5  * Authors:
6  *  Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7  *  Javier Martin <javiermartin@by.com.es>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <drm_fourcc.h>
31
32 #include "gstkmsutils.h"
33
34 /* *INDENT-OFF* */
35 static const struct
36 {
37   guint32 fourcc;
38   GstVideoFormat format;
39 } format_map[] = {
40 #define DEF_FMT(fourcc, fmt) \
41   { DRM_FORMAT_##fourcc,GST_VIDEO_FORMAT_##fmt }
42
43   /* DEF_FMT (XRGB1555, ???), */
44   /* DEF_FMT (XBGR1555, ???), */
45   DEF_FMT (ARGB8888, BGRA),
46   DEF_FMT (XRGB8888, BGRx),
47   DEF_FMT (ABGR8888, RGBA),
48   DEF_FMT (XBGR8888, RGBx),
49   DEF_FMT (BGR888, RGB),
50   DEF_FMT (RGB888, BGR),
51   DEF_FMT (P010, P010_10LE),
52   DEF_FMT (P016, P016_LE),
53   DEF_FMT (UYVY, UYVY),
54   DEF_FMT (YUYV, YUY2),
55   DEF_FMT (YVYU, YVYU),
56   DEF_FMT (YUV420, I420),
57   DEF_FMT (YVU420, YV12),
58   DEF_FMT (YUV422, Y42B),
59   DEF_FMT (NV12, NV12),
60   DEF_FMT (NV21, NV21),
61   DEF_FMT (NV16, NV16),
62   DEF_FMT (NV61, NV61),
63   DEF_FMT (NV24, NV24),
64
65 #undef DEF_FMT
66 };
67 /* *INDENT-ON* */
68
69 GstVideoFormat
70 gst_video_format_from_drm (guint32 drmfmt)
71 {
72   gint i;
73
74   for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
75     if (format_map[i].fourcc == drmfmt)
76       return format_map[i].format;
77   }
78
79   return GST_VIDEO_FORMAT_UNKNOWN;
80 }
81
82 guint32
83 gst_drm_format_from_video (GstVideoFormat fmt)
84 {
85   gint i;
86
87   for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
88     if (format_map[i].format == fmt)
89       return format_map[i].fourcc;
90   }
91
92   return 0;
93 }
94
95 guint32
96 gst_drm_bpp_from_drm (guint32 drmfmt)
97 {
98   guint32 bpp;
99
100   switch (drmfmt) {
101     case DRM_FORMAT_YUV420:
102     case DRM_FORMAT_YVU420:
103     case DRM_FORMAT_YUV422:
104     case DRM_FORMAT_NV12:
105     case DRM_FORMAT_NV21:
106     case DRM_FORMAT_NV16:
107     case DRM_FORMAT_NV61:
108     case DRM_FORMAT_NV24:
109       bpp = 8;
110       break;
111     case DRM_FORMAT_P010:
112       bpp = 10;
113       break;
114     case DRM_FORMAT_UYVY:
115     case DRM_FORMAT_YUYV:
116     case DRM_FORMAT_YVYU:
117     case DRM_FORMAT_P016:
118       bpp = 16;
119       break;
120     case DRM_FORMAT_BGR888:
121     case DRM_FORMAT_RGB888:
122       bpp = 24;
123       break;
124     default:
125       bpp = 32;
126       break;
127   }
128
129   return bpp;
130 }
131
132 guint32
133 gst_drm_height_from_drm (guint32 drmfmt, guint32 height)
134 {
135   guint32 ret;
136
137   switch (drmfmt) {
138     case DRM_FORMAT_YUV420:
139     case DRM_FORMAT_YVU420:
140     case DRM_FORMAT_YUV422:
141     case DRM_FORMAT_NV12:
142     case DRM_FORMAT_NV21:
143     case DRM_FORMAT_P010:
144     case DRM_FORMAT_P016:
145       ret = height * 3 / 2;
146       break;
147     case DRM_FORMAT_NV16:
148     case DRM_FORMAT_NV61:
149       ret = height * 2;
150       break;
151     case DRM_FORMAT_NV24:
152       ret = height * 3;
153       break;
154     default:
155       ret = height;
156       break;
157   }
158
159   return ret;
160 }
161
162 static GstStructure *
163 gst_video_format_to_structure (GstVideoFormat format)
164 {
165   GstStructure *structure;
166
167   structure = NULL;
168   if (format != GST_VIDEO_FORMAT_UNKNOWN)
169     structure = gst_structure_new ("video/x-raw", "format", G_TYPE_STRING,
170         gst_video_format_to_string (format), NULL);
171
172   return structure;
173 }
174
175 GstCaps *
176 gst_kms_sink_caps_template_fill (void)
177 {
178   gint i;
179   GstCaps *caps;
180   GstStructure *template;
181
182   caps = gst_caps_new_empty ();
183   for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
184     template = gst_video_format_to_structure (format_map[i].format);
185     gst_structure_set (template,
186         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
187         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
188         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
189     gst_caps_append_structure (caps, template);
190   }
191   return gst_caps_simplify (caps);
192 }
193
194 static const gint device_par_map[][2] = {
195   {1, 1},                       /* regular screen */
196   {16, 15},                     /* PAL TV */
197   {11, 10},                     /* 525 line Rec.601 video */
198   {54, 59},                     /* 625 line Rec.601 video */
199   {64, 45},                     /* 1280x1024 on 16:9 display */
200   {5, 3},                       /* 1280x1024 on 4:3 display */
201   {4, 3}                        /* 800x600 on 16:9 display */
202 };
203
204 #define DELTA(ratio, idx, w) \
205   (ABS(ratio - ((gdouble)device_par_map[idx][w] / device_par_map[idx][!(w)])))
206
207 void
208 gst_video_calculate_device_ratio (guint dev_width, guint dev_height,
209     guint dev_width_mm, guint dev_height_mm,
210     guint * dpy_par_n, guint * dpy_par_d)
211 {
212   gdouble ratio, delta, cur_delta;
213   gint i, j, index, windex;
214
215   /* First, calculate the "real" ratio based on the X values; which is
216    * the "physical" w/h divided by the w/h in pixels of the display */
217   if (dev_width == 0 || dev_height == 0
218       || dev_width_mm == 0 || dev_height_mm == 0)
219     ratio = 1.0;
220   else
221     ratio = (gdouble) (dev_width_mm * dev_height) / (dev_height_mm * dev_width);
222
223   /* Now, find the one from device_par_map[][2] with the lowest delta
224    * to the real one */
225   delta = DELTA (ratio, 0, 0);
226   index = 0;
227   windex = 0;
228
229   for (i = 1; i < G_N_ELEMENTS (device_par_map); i++) {
230     for (j = 0; j < 2; j++) {
231       cur_delta = DELTA (ratio, i, j);
232       if (cur_delta < delta) {
233         index = i;
234         windex = j;
235         delta = cur_delta;
236       }
237     }
238   }
239
240   if (dpy_par_n)
241     *dpy_par_n = device_par_map[index][windex];
242
243   if (dpy_par_d)
244     *dpy_par_d = device_par_map[index][windex ^ 1];
245 }