xref: /OK3568_Linux_fs/external/gstreamer-rockchip/gst/rkximage/gstkmsutils.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 #ifdef HAVE_NV12_10LE40
51   DEF_FMT (NV12_10, NV12_10LE40),
52   DEF_FMT (NV15, NV12_10LE40),
53 #endif
54 #else
55   DEF_FMT (ARGB8888, ARGB),
56   DEF_FMT (XRGB8888, xRGB),
57   DEF_FMT (ABGR8888, ABGR),
58   DEF_FMT (XBGR8888, xBGR),
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_NV12_10:
115     case DRM_FORMAT_NV15:
116       bpp = 10;
117       break;
118     case DRM_FORMAT_UYVY:
119     case DRM_FORMAT_YUYV:
120     case DRM_FORMAT_YVYU:
121       bpp = 16;
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_NV12_10:
142     case DRM_FORMAT_NV15:
143     case DRM_FORMAT_NV21:
144       ret = height * 3 / 2;
145       break;
146     case DRM_FORMAT_NV16:
147       ret = height * 2;
148       break;
149     default:
150       ret = height;
151       break;
152   }
153 
154   return ret;
155 }
156 
157 static GstStructure *
gst_video_format_to_structure(GstVideoFormat format)158 gst_video_format_to_structure (GstVideoFormat format)
159 {
160   GstStructure *structure;
161 
162   structure = NULL;
163   if (format != GST_VIDEO_FORMAT_UNKNOWN)
164     structure = gst_structure_new ("video/x-raw", "format", G_TYPE_STRING,
165         gst_video_format_to_string (format), NULL);
166 
167   return structure;
168 }
169 
170 GstCaps *
gst_kms_sink_caps_template_fill(void)171 gst_kms_sink_caps_template_fill (void)
172 {
173   gint i;
174   GstCaps *caps;
175   GstStructure *template;
176 
177   caps = gst_caps_new_empty ();
178   for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
179     template = gst_video_format_to_structure (format_map[i].format);
180     gst_structure_set (template,
181         "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
182         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT,
183         "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
184     gst_caps_append_structure (caps, template);
185   }
186   return gst_caps_simplify (caps);
187 }
188 
189 static const gint device_par_map[][2] = {
190   {1, 1},                       /* regular screen */
191   {16, 15},                     /* PAL TV */
192   {11, 10},                     /* 525 line Rec.601 video */
193   {54, 59},                     /* 625 line Rec.601 video */
194   {64, 45},                     /* 1280x1024 on 16:9 display */
195   {5, 3},                       /* 1280x1024 on 4:3 display */
196   {4, 3}                        /* 800x600 on 16:9 display */
197 };
198 
199 #define DELTA(ratio, idx, w) \
200   (ABS(ratio - ((gdouble)device_par_map[idx][w] / device_par_map[idx][!(w)])))
201 
202 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)203 gst_video_calculate_device_ratio (guint dev_width, guint dev_height,
204     guint dev_width_mm, guint dev_height_mm,
205     guint * dpy_par_n, guint * dpy_par_d)
206 {
207   gdouble ratio, delta, cur_delta;
208   gint i, j, index, windex;
209 
210   /* First, calculate the "real" ratio based on the X values; which is
211    * the "physical" w/h divided by the w/h in pixels of the display */
212   if (dev_width == 0 || dev_height == 0
213       || dev_width_mm == 0 || dev_height_mm == 0)
214     ratio = 1.0;
215   else
216     ratio = (gdouble) (dev_width_mm * dev_height) / (dev_height_mm * dev_width);
217 
218   /* Now, find the one from device_par_map[][2] with the lowest delta
219    * to the real one */
220   delta = DELTA (ratio, 0, 0);
221   index = 0;
222   windex = 0;
223 
224   for (i = 1; i < G_N_ELEMENTS (device_par_map); i++) {
225     for (j = 0; j < 2; j++) {
226       cur_delta = DELTA (ratio, i, j);
227       if (cur_delta < delta) {
228         index = i;
229         windex = j;
230         delta = cur_delta;
231       }
232     }
233   }
234 
235   if (dpy_par_n)
236     *dpy_par_n = device_par_map[index][windex];
237 
238   if (dpy_par_d)
239     *dpy_par_d = device_par_map[index][windex ^ 1];
240 }
241