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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
46 DEF_FMT (ARGB8888, BGRA),
47 DEF_FMT (XRGB8888, BGRx),
48 DEF_FMT (ABGR8888, RGBA),
49 DEF_FMT (XBGR8888, RGBx),
50 DEF_FMT (BGR888, RGB),
51 DEF_FMT (RGB888, BGR),
52 #else
53 DEF_FMT (ARGB8888, ARGB),
54 DEF_FMT (XRGB8888, xRGB),
55 DEF_FMT (ABGR8888, ABGR),
56 DEF_FMT (XBGR8888, xBGR),
57 DEF_FMT (RGB888, RGB),
58 DEF_FMT (BGR888, BGR),
59 #endif
60 DEF_FMT (UYVY, UYVY),
61 DEF_FMT (YUYV, YUY2),
62 DEF_FMT (YVYU, YVYU),
63 DEF_FMT (YUV420, I420),
64 DEF_FMT (YVU420, YV12),
65 DEF_FMT (YUV422, Y42B),
66 DEF_FMT (NV12, NV12),
67 DEF_FMT (NV21, NV21),
68 DEF_FMT (NV16, NV16),
69
70 #undef DEF_FMT
71 };
72 /* *INDENT-ON* */
73
74 GstVideoFormat
gst_video_format_from_drm(guint32 drmfmt)75 gst_video_format_from_drm (guint32 drmfmt)
76 {
77 gint i;
78
79 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
80 if (format_map[i].fourcc == drmfmt)
81 return format_map[i].format;
82 }
83
84 return GST_VIDEO_FORMAT_UNKNOWN;
85 }
86
87 guint32
gst_drm_format_from_video(GstVideoFormat fmt)88 gst_drm_format_from_video (GstVideoFormat fmt)
89 {
90 gint i;
91
92 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
93 if (format_map[i].format == fmt)
94 return format_map[i].fourcc;
95 }
96
97 return 0;
98 }
99
100 guint32
gst_drm_bpp_from_drm(guint32 drmfmt)101 gst_drm_bpp_from_drm (guint32 drmfmt)
102 {
103 guint32 bpp;
104
105 switch (drmfmt) {
106 case DRM_FORMAT_YUV420:
107 case DRM_FORMAT_YVU420:
108 case DRM_FORMAT_YUV422:
109 case DRM_FORMAT_NV12:
110 case DRM_FORMAT_NV21:
111 case DRM_FORMAT_NV16:
112 bpp = 8;
113 break;
114 case DRM_FORMAT_UYVY:
115 case DRM_FORMAT_YUYV:
116 case DRM_FORMAT_YVYU:
117 bpp = 16;
118 break;
119 case DRM_FORMAT_BGR888:
120 case DRM_FORMAT_RGB888:
121 bpp = 24;
122 break;
123 default:
124 bpp = 32;
125 break;
126 }
127
128 return bpp;
129 }
130
131 guint32
gst_drm_height_from_drm(guint32 drmfmt,guint32 height)132 gst_drm_height_from_drm (guint32 drmfmt, guint32 height)
133 {
134 guint32 ret;
135
136 switch (drmfmt) {
137 case DRM_FORMAT_YUV420:
138 case DRM_FORMAT_YVU420:
139 case DRM_FORMAT_YUV422:
140 case DRM_FORMAT_NV12:
141 case DRM_FORMAT_NV21:
142 ret = height * 3 / 2;
143 break;
144 case DRM_FORMAT_NV16:
145 ret = height * 2;
146 break;
147 default:
148 ret = height;
149 break;
150 }
151
152 return ret;
153 }
154
155 static GstStructure *
gst_video_format_to_structure(GstVideoFormat format)156 gst_video_format_to_structure (GstVideoFormat format)
157 {
158 GstStructure *structure;
159
160 structure = NULL;
161 if (format != GST_VIDEO_FORMAT_UNKNOWN)
162 structure = gst_structure_new ("video/x-raw", "format", G_TYPE_STRING,
163 gst_video_format_to_string (format), NULL);
164
165 return structure;
166 }
167
168 GstCaps *
gst_kms_sink_caps_template_fill(void)169 gst_kms_sink_caps_template_fill (void)
170 {
171 gint i;
172 GstCaps *caps;
173 GstStructure *template;
174
175 caps = gst_caps_new_empty ();
176 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
177 template = gst_video_format_to_structure (format_map[i].format);
178 gst_structure_set (template,
179 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
180 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
181 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
182 gst_caps_append_structure (caps, template);
183 }
184 return gst_caps_simplify (caps);
185 }
186
187 static const gint device_par_map[][2] = {
188 {1, 1}, /* regular screen */
189 {16, 15}, /* PAL TV */
190 {11, 10}, /* 525 line Rec.601 video */
191 {54, 59}, /* 625 line Rec.601 video */
192 {64, 45}, /* 1280x1024 on 16:9 display */
193 {5, 3}, /* 1280x1024 on 4:3 display */
194 {4, 3} /* 800x600 on 16:9 display */
195 };
196
197 #define DELTA(ratio, idx, w) \
198 (ABS(ratio - ((gdouble)device_par_map[idx][w] / device_par_map[idx][!(w)])))
199
200 void
gst_video_calculate_device_ratio(guint dev_width,guint dev_height,guint dev_width_mm,guint dev_height_mm,guint * dpy_par_n,guint * dpy_par_d)201 gst_video_calculate_device_ratio (guint dev_width, guint dev_height,
202 guint dev_width_mm, guint dev_height_mm,
203 guint * dpy_par_n, guint * dpy_par_d)
204 {
205 gdouble ratio, delta, cur_delta;
206 gint i, j, index, windex;
207
208 /* First, calculate the "real" ratio based on the X values; which is
209 * the "physical" w/h divided by the w/h in pixels of the display */
210 if (dev_width == 0 || dev_height == 0
211 || dev_width_mm == 0 || dev_height_mm == 0)
212 ratio = 1.0;
213 else
214 ratio = (gdouble) (dev_width_mm * dev_height) / (dev_height_mm * dev_width);
215
216 /* Now, find the one from device_par_map[][2] with the lowest delta
217 * to the real one */
218 delta = DELTA (ratio, 0, 0);
219 index = 0;
220 windex = 0;
221
222 for (i = 1; i < G_N_ELEMENTS (device_par_map); i++) {
223 for (j = 0; j < 2; j++) {
224 cur_delta = DELTA (ratio, i, j);
225 if (cur_delta < delta) {
226 index = i;
227 windex = j;
228 delta = cur_delta;
229 }
230 }
231 }
232
233 if (dpy_par_n)
234 *dpy_par_n = device_par_map[index][windex];
235
236 if (dpy_par_d)
237 *dpy_par_d = device_par_map[index][windex ^ 1];
238 }
239