v4l2: Set Hardware classifier on encoders
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2h265enc.c
1 /*
2  * Copyright (C) 2018 NVIDIA CORPORATION.
3  *    Author: Amit Pandya <apandya@nvidia.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 <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "gstv4l2object.h"
33 #include "gstv4l2h265enc.h"
34
35 #include <string.h>
36 #include <gst/gst-i18n-plugin.h>
37
38 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_h265_enc_debug);
39 #define GST_CAT_DEFAULT gst_v4l2_h265_enc_debug
40
41 static GstStaticCaps src_template_caps =
42 GST_STATIC_CAPS ("video/x-h265, stream-format=(string) byte-stream, "
43     "alignment=(string) au");
44
45 enum
46 {
47   PROP_0,
48   V4L2_STD_OBJECT_PROPS,
49 /* TODO add H265 controls */
50 };
51
52 #define gst_v4l2_h265_enc_parent_class parent_class
53 G_DEFINE_TYPE (GstV4l2H265Enc, gst_v4l2_h265_enc, GST_TYPE_V4L2_VIDEO_ENC);
54
55 static void
56 gst_v4l2_h265_enc_set_property (GObject * object,
57     guint prop_id, const GValue * value, GParamSpec * pspec)
58 {
59   /* TODO */
60 }
61
62 static void
63 gst_v4l2_h265_enc_get_property (GObject * object,
64     guint prop_id, GValue * value, GParamSpec * pspec)
65 {
66   /* TODO */
67 }
68
69 static gint
70 v4l2_profile_from_string (const gchar * profile)
71 {
72   gint v4l2_profile = -1;
73
74   if (g_str_equal (profile, "main")) {
75     v4l2_profile = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN;
76   } else if (g_str_equal (profile, "mainstillpicture")) {
77     v4l2_profile = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE;
78   } else if (g_str_equal (profile, "main10")) {
79     v4l2_profile = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10;
80   } else {
81     GST_WARNING ("Unsupported profile string '%s'", profile);
82   }
83
84   return v4l2_profile;
85 }
86
87 static const gchar *
88 v4l2_profile_to_string (gint v4l2_profile)
89 {
90   switch (v4l2_profile) {
91     case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
92       return "main";
93     case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
94       return "mainstillpicture";
95     case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
96       return "main10";
97     default:
98       GST_WARNING ("Unsupported V4L2 profile %i", v4l2_profile);
99       break;
100   }
101   return NULL;
102 }
103
104 static gint
105 v4l2_level_from_string (const gchar * level)
106 {
107   gint v4l2_level = -1;
108
109   if (g_str_equal (level, "1"))
110     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_1;
111   else if (g_str_equal (level, "2"))
112     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_2;
113   else if (g_str_equal (level, "2.1"))
114     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1;
115   else if (g_str_equal (level, "3"))
116     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_3;
117   else if (g_str_equal (level, "3.1"))
118     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1;
119   else if (g_str_equal (level, "4"))
120     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_4;
121   else if (g_str_equal (level, "4.1"))
122     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1;
123   else if (g_str_equal (level, "5"))
124     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_5;
125   else if (g_str_equal (level, "5.1"))
126     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1;
127   else if (g_str_equal (level, "5.2"))
128     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2;
129   else if (g_str_equal (level, "6"))
130     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_6;
131   else if (g_str_equal (level, "6.1"))
132     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1;
133   else if (g_str_equal (level, "6.2"))
134     v4l2_level = V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2;
135   else
136     GST_WARNING ("Unsupported level '%s'", level);
137
138   return v4l2_level;
139 }
140
141 static const gchar *
142 v4l2_level_to_string (gint v4l2_level)
143 {
144   switch (v4l2_level) {
145     case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
146       return "1";
147     case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
148       return "2";
149     case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
150       return "2.1";
151     case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
152       return "3";
153     case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
154       return "3.1";
155     case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
156       return "4";
157     case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
158       return "4.1";
159     case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
160       return "5";
161     case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
162       return "5.1";
163     case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
164       return "5.2";
165     case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
166       return "6";
167     case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
168       return "6.1";
169     case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
170       return "6.2";
171     default:
172       GST_WARNING ("Unsupported V4L2 level %i", v4l2_level);
173       break;
174   }
175
176   return NULL;
177 }
178
179 static void
180 gst_v4l2_h265_enc_init (GstV4l2H265Enc * self)
181 {
182 }
183
184 static void
185 gst_v4l2_h265_enc_class_init (GstV4l2H265EncClass * klass)
186 {
187   GstElementClass *element_class;
188   GObjectClass *gobject_class;
189   GstV4l2VideoEncClass *baseclass;
190
191   parent_class = g_type_class_peek_parent (klass);
192
193   element_class = (GstElementClass *) klass;
194   gobject_class = (GObjectClass *) klass;
195   baseclass = (GstV4l2VideoEncClass *) (klass);
196
197   GST_DEBUG_CATEGORY_INIT (gst_v4l2_h265_enc_debug, "v4l2h265enc", 0,
198       "V4L2 H.265 Encoder");
199
200   gst_element_class_set_static_metadata (element_class,
201       "V4L2 H.265 Encoder",
202       "Codec/Encoder/Video/Hardware",
203       "Encode H.265 video streams via V4L2 API",
204       "Amit Pandya <apandya@nvidia.com>");
205
206   gobject_class->set_property =
207       GST_DEBUG_FUNCPTR (gst_v4l2_h265_enc_set_property);
208   gobject_class->get_property =
209       GST_DEBUG_FUNCPTR (gst_v4l2_h265_enc_get_property);
210
211   baseclass->codec_name = "H265";
212   baseclass->profile_cid = V4L2_CID_MPEG_VIDEO_HEVC_PROFILE;
213   baseclass->profile_to_string = v4l2_profile_to_string;
214   baseclass->profile_from_string = v4l2_profile_from_string;
215   baseclass->level_cid = V4L2_CID_MPEG_VIDEO_HEVC_LEVEL;
216   baseclass->level_to_string = v4l2_level_to_string;
217   baseclass->level_from_string = v4l2_level_from_string;
218 }
219
220 /* Probing functions */
221 gboolean
222 gst_v4l2_is_h265_enc (GstCaps * sink_caps, GstCaps * src_caps)
223 {
224   return gst_v4l2_is_video_enc (sink_caps, src_caps,
225       gst_static_caps_get (&src_template_caps));
226 }
227
228 void
229 gst_v4l2_h265_enc_register (GstPlugin * plugin, const gchar * basename,
230     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
231 {
232   gst_v4l2_video_enc_register (plugin, GST_TYPE_V4L2_H265_ENC,
233       "h265", basename, device_path, sink_caps,
234       gst_static_caps_get (&src_template_caps), src_caps);
235 }