3 * Copyright (C) 2016 Igalia
6 * Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7 * Javier Martin <javiermartin@by.com.es>
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.
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.
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.
30 #include <drm_fourcc.h>
32 #include "gstkmsutils.h"
38 GstVideoFormat format;
40 #define DEF_FMT(fourcc, fmt) \
41 { DRM_FORMAT_##fourcc,GST_VIDEO_FORMAT_##fmt }
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),
51 DEF_FMT (ARGB8888, ARGB),
52 DEF_FMT (XRGB8888, xRGB),
53 DEF_FMT (ABGR8888, ABGR),
54 DEF_FMT (XBGR8888, xBGR),
56 /* DEF_FMT (YUYV, ???), */
57 /* DEF_FMT (YVYU, ???), */
58 DEF_FMT (YUV420, I420),
59 DEF_FMT (YVU420, YV12),
60 DEF_FMT (YUV422, Y42B),
70 gst_video_format_from_drm (guint32 drmfmt)
74 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
75 if (format_map[i].fourcc == drmfmt)
76 return format_map[i].format;
79 return GST_VIDEO_FORMAT_UNKNOWN;
83 gst_drm_format_from_video (GstVideoFormat fmt)
87 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
88 if (format_map[i].format == fmt)
89 return format_map[i].fourcc;
96 gst_video_format_to_structure (GstVideoFormat format)
98 GstStructure *structure;
101 if (format != GST_VIDEO_FORMAT_UNKNOWN)
102 structure = gst_structure_new ("video/x-raw", "format", G_TYPE_STRING,
103 gst_video_format_to_string (format), NULL);
109 gst_kms_sink_caps_template_fill (void)
113 GstStructure *template;
115 caps = gst_caps_new_empty ();
116 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
117 template = gst_video_format_to_structure (format_map[i].format);
118 gst_structure_set (template,
119 "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
120 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
121 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
122 gst_caps_append_structure (caps, template);
124 return gst_caps_simplify (caps);
127 static const gint device_par_map[][2] = {
128 {1, 1}, /* regular screen */
129 {16, 15}, /* PAL TV */
130 {11, 10}, /* 525 line Rec.601 video */
131 {54, 59}, /* 625 line Rec.601 video */
132 {64, 45}, /* 1280x1024 on 16:9 display */
133 {5, 3}, /* 1280x1024 on 4:3 display */
134 {4, 3} /* 800x600 on 16:9 display */
137 #define DELTA(ratio, idx, w) \
138 (ABS(ratio - ((gdouble)device_par_map[idx][w] / device_par_map[idx][!(w)])))
141 gst_video_calculate_device_ratio (guint dev_width, guint dev_height,
142 guint dev_width_mm, guint dev_height_mm,
143 guint * dpy_par_n, guint * dpy_par_d)
145 gdouble ratio, delta, cur_delta;
146 gint i, j, index, windex;
148 /* First, calculate the "real" ratio based on the X values; which is
149 * the "physical" w/h divided by the w/h in pixels of the display */
150 if (dev_width == 0 || dev_height == 0
151 || dev_width_mm == 0 || dev_height_mm == 0)
154 ratio = (gdouble) (dev_width_mm * dev_height) / (dev_height_mm * dev_width);
156 /* Now, find the one from device_par_map[][2] with the lowest delta
158 delta = DELTA (ratio, 0, 0);
162 for (i = 1; i < G_N_ELEMENTS (device_par_map); i++) {
163 for (j = 0; j < 2; j++) {
164 cur_delta = DELTA (ratio, i, j);
165 if (cur_delta < delta) {
174 *dpy_par_n = device_par_map[index][windex];
177 *dpy_par_d = device_par_map[index][windex ^ 1];