• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 Igalia S.L.
3  *    Author: Philippe Normand <philn@igalia.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <fcntl.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 
32 #include "gstv4l2codec.h"
33 #include "ext/videodev2.h"
34 
35 #include <gst/gst.h>
36 
37 gboolean
gst_v4l2_codec_probe_profiles(const GstV4l2Codec * codec,gint video_fd,GValue * profiles)38 gst_v4l2_codec_probe_profiles (const GstV4l2Codec * codec, gint video_fd,
39     GValue * profiles)
40 {
41   struct v4l2_queryctrl query_ctrl;
42   gboolean ret = FALSE;
43 
44   memset (&query_ctrl, 0, sizeof (query_ctrl));
45   query_ctrl.id = codec->profile_cid;
46 
47   if (ioctl (video_fd, VIDIOC_QUERYCTRL, &query_ctrl) == 0) {
48     if (query_ctrl.flags & V4L2_CTRL_FLAG_DISABLED)
49       return FALSE;
50 
51     if (query_ctrl.type == V4L2_CTRL_TYPE_MENU) {
52       struct v4l2_querymenu query_menu;
53 
54       memset (&query_menu, 0, sizeof (query_menu));
55       query_menu.id = query_ctrl.id;
56 
57       g_value_init (profiles, GST_TYPE_LIST);
58       for (query_menu.index = query_ctrl.minimum;
59           query_menu.index <= query_ctrl.maximum; query_menu.index++) {
60         if (ioctl (video_fd, VIDIOC_QUERYMENU, &query_menu) >= 0) {
61           GValue value = G_VALUE_INIT;
62 
63           g_value_init (&value, G_TYPE_STRING);
64           g_value_set_string (&value,
65               codec->profile_to_string (query_menu.index));
66           gst_value_list_append_and_take_value (profiles, &value);
67           ret = TRUE;
68         }
69       }
70 
71       if (gst_value_list_get_size (profiles) == 0) {
72         g_value_unset (profiles);
73         ret = FALSE;
74       }
75     }
76   }
77 
78   return ret;
79 }
80 
81 gboolean
gst_v4l2_codec_probe_levels(const GstV4l2Codec * codec,gint video_fd,GValue * levels)82 gst_v4l2_codec_probe_levels (const GstV4l2Codec * codec, gint video_fd,
83     GValue * levels)
84 {
85   struct v4l2_queryctrl query_ctrl;
86   gboolean ret = FALSE;
87 
88   memset (&query_ctrl, 0, sizeof (query_ctrl));
89   query_ctrl.id = codec->level_cid;
90 
91   if (ioctl (video_fd, VIDIOC_QUERYCTRL, &query_ctrl) == 0) {
92     if (query_ctrl.flags & V4L2_CTRL_FLAG_DISABLED)
93       return FALSE;
94 
95     if (query_ctrl.type == V4L2_CTRL_TYPE_MENU) {
96       struct v4l2_querymenu query_menu;
97 
98       memset (&query_menu, 0, sizeof (query_menu));
99       query_menu.id = query_ctrl.id;
100       query_menu.index = query_ctrl.maximum;
101 
102       if (ioctl (video_fd, VIDIOC_QUERYMENU, &query_menu) >= 0) {
103         gint32 i;
104 
105         g_value_init (levels, GST_TYPE_LIST);
106 
107         /* Assume that all levels below the highest one reported by the driver are supported. */
108         for (i = query_ctrl.minimum; i <= query_ctrl.maximum; i++) {
109           GValue value = G_VALUE_INIT;
110 
111           g_value_init (&value, G_TYPE_STRING);
112           g_value_set_string (&value, codec->level_to_string (i));
113           gst_value_list_append_and_take_value (levels, &value);
114           ret = TRUE;
115         }
116 
117         if (gst_value_list_get_size (levels) == 0) {
118           g_value_unset (levels);
119           ret = FALSE;
120         }
121       }
122     }
123   }
124 
125   return ret;
126 }
127