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