1 /*
2 * Copyright (C) 2014 SUMOMO Computer Association
3 * Author: ayaka <ayaka@soulik.info>
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 "gstv4l2h264enc.h"
34
35 #include <string.h>
36 #include <gst/gst-i18n-plugin.h>
37
38 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_h264_enc_debug);
39 #define GST_CAT_DEFAULT gst_v4l2_h264_enc_debug
40
41
42 static GstStaticCaps src_template_caps =
43 GST_STATIC_CAPS ("video/x-h264, stream-format=(string) byte-stream, "
44 "alignment=(string) au");
45
46 enum
47 {
48 PROP_0,
49 V4L2_STD_OBJECT_PROPS,
50 /* TODO add H264 controls
51 * PROP_I_FRAME_QP,
52 * PROP_P_FRAME_QP,
53 * PROP_B_FRAME_QP,
54 * PROP_MIN_QP,
55 * PROP_MAX_QP,
56 * PROP_8x8_TRANSFORM,
57 * PROP_CPB_SIZE,
58 * PROP_ENTROPY_MODE,
59 * PROP_I_PERIOD,
60 * PROP_LOOP_FILTER_ALPHA,
61 * PROP_LOOP_FILTER_BETA,
62 * PROP_LOOP_FILTER_MODE,
63 * PROP_VUI_EXT_SAR_HEIGHT,
64 * PROP_VUI_EXT_SAR_WIDTH,
65 * PROP_VUI_SAR_ENABLED,
66 * PROP_VUI_SAR_IDC,
67 * PROP_SEI_FRAME_PACKING,
68 * PROP_SEI_FP_CURRENT_FRAME_0,
69 * PROP_SEI_FP_ARRANGEMENT_TYP,
70 * ...
71 * */
72 };
73
74 #define gst_v4l2_h264_enc_parent_class parent_class
75 G_DEFINE_TYPE (GstV4l2H264Enc, gst_v4l2_h264_enc, GST_TYPE_V4L2_VIDEO_ENC);
76
77 static void
gst_v4l2_h264_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)78 gst_v4l2_h264_enc_set_property (GObject * object,
79 guint prop_id, const GValue * value, GParamSpec * pspec)
80 {
81 /* TODO */
82 }
83
84 static void
gst_v4l2_h264_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)85 gst_v4l2_h264_enc_get_property (GObject * object,
86 guint prop_id, GValue * value, GParamSpec * pspec)
87 {
88 /* TODO */
89 }
90
91 static gint
v4l2_profile_from_string(const gchar * profile)92 v4l2_profile_from_string (const gchar * profile)
93 {
94 gint v4l2_profile = -1;
95
96 if (g_str_equal (profile, "baseline")) {
97 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
98 } else if (g_str_equal (profile, "constrained-baseline")) {
99 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE;
100 } else if (g_str_equal (profile, "main")) {
101 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
102 } else if (g_str_equal (profile, "extended")) {
103 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED;
104 } else if (g_str_equal (profile, "high")) {
105 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
106 } else if (g_str_equal (profile, "high-10")) {
107 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_10;
108 } else if (g_str_equal (profile, "high-4:2:2")) {
109 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_422;
110 } else if (g_str_equal (profile, "high-4:4:4")) {
111 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_PREDICTIVE;
112 } else if (g_str_equal (profile, "high-10-intra")) {
113 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_10_INTRA;
114 } else if (g_str_equal (profile, "high-4:2:2-intra")) {
115 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_422_INTRA;
116 } else if (g_str_equal (profile, "high-4:4:4-intra")) {
117 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_INTRA;
118 } else if (g_str_equal (profile, "cavlc-4:4:4-intra")) {
119 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_CAVLC_444_INTRA;
120 } else if (g_str_equal (profile, "scalable-baseline")) {
121 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_SCALABLE_BASELINE;
122 } else if (g_str_equal (profile, "scalable-high")) {
123 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_SCALABLE_HIGH;
124 } else if (g_str_equal (profile, "scalable-high-intra")) {
125 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_SCALABLE_HIGH_INTRA;
126 } else if (g_str_equal (profile, "stereo-high")) {
127 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH;
128 } else if (g_str_equal (profile, "multiview-high")) {
129 v4l2_profile = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH;
130 } else {
131 GST_WARNING ("Unsupported profile string '%s'", profile);
132 }
133
134 return v4l2_profile;
135 }
136
137 static const gchar *
v4l2_profile_to_string(gint v4l2_profile)138 v4l2_profile_to_string (gint v4l2_profile)
139 {
140 switch (v4l2_profile) {
141 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
142 return "baseline";
143 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
144 return "constrained-baseline";
145 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
146 return "main";
147 case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
148 return "extended";
149 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
150 return "high";
151 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_10:
152 return "high-10";
153 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_422:
154 return "high-4:2:2";
155 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_PREDICTIVE:
156 return "high-4:4:4";
157 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_10_INTRA:
158 return "high-10-intra";
159 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_422_INTRA:
160 return "high-4:2:2-intra";
161 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_INTRA:
162 return "high-4:4:4-intra";
163 case V4L2_MPEG_VIDEO_H264_PROFILE_CAVLC_444_INTRA:
164 return "cavlc-4:4:4-intra";
165 case V4L2_MPEG_VIDEO_H264_PROFILE_SCALABLE_BASELINE:
166 return "scalable-baseline";
167 case V4L2_MPEG_VIDEO_H264_PROFILE_SCALABLE_HIGH:
168 return "scalable-high";
169 case V4L2_MPEG_VIDEO_H264_PROFILE_SCALABLE_HIGH_INTRA:
170 return "scalable-high-intra";
171 case V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH:
172 return "stereo-high";
173 case V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH:
174 return "multiview-high";
175 default:
176 GST_WARNING ("Unsupported V4L2 profile %i", v4l2_profile);
177 break;
178 }
179
180 return NULL;
181 }
182
183 static gint
v4l2_level_from_string(const gchar * level)184 v4l2_level_from_string (const gchar * level)
185 {
186 gint v4l2_level = -1;
187
188 if (g_str_equal (level, "1"))
189 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
190 else if (g_str_equal (level, "1b"))
191 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_1B;
192 else if (g_str_equal (level, "1.1"))
193 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
194 else if (g_str_equal (level, "1.2"))
195 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_1_2;
196 else if (g_str_equal (level, "1.3"))
197 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_1_3;
198 else if (g_str_equal (level, "2"))
199 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_2_0;
200 else if (g_str_equal (level, "2.1"))
201 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
202 else if (g_str_equal (level, "2.2"))
203 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
204 else if (g_str_equal (level, "3"))
205 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_3_0;
206 else if (g_str_equal (level, "3.1"))
207 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
208 else if (g_str_equal (level, "3.2"))
209 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
210 else if (g_str_equal (level, "4"))
211 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
212 else if (g_str_equal (level, "4.1"))
213 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
214 else if (g_str_equal (level, "4.2"))
215 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
216 else if (g_str_equal (level, "5"))
217 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
218 else if (g_str_equal (level, "5.1"))
219 v4l2_level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
220 else
221 GST_WARNING ("Unsupported level '%s'", level);
222
223 return v4l2_level;
224 }
225
226 static const gchar *
v4l2_level_to_string(gint v4l2_level)227 v4l2_level_to_string (gint v4l2_level)
228 {
229 switch (v4l2_level) {
230 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
231 return "1";
232 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
233 return "1b";
234 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
235 return "1.1";
236 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
237 return "1.2";
238 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
239 return "1.3";
240 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
241 return "2";
242 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
243 return "2.1";
244 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
245 return "2.2";
246 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
247 return "3.0";
248 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
249 return "3.1";
250 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
251 return "3.2";
252 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
253 return "4";
254 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
255 return "4.1";
256 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
257 return "4.2";
258 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
259 return "5";
260 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
261 return "5.1";
262 default:
263 GST_WARNING ("Unsupported V4L2 level %i", v4l2_level);
264 break;
265 }
266
267 return NULL;
268 }
269
270 static void
gst_v4l2_h264_enc_init(GstV4l2H264Enc * self)271 gst_v4l2_h264_enc_init (GstV4l2H264Enc * self)
272 {
273 }
274
275 static void
gst_v4l2_h264_enc_class_init(GstV4l2H264EncClass * klass)276 gst_v4l2_h264_enc_class_init (GstV4l2H264EncClass * klass)
277 {
278 GstElementClass *element_class;
279 GObjectClass *gobject_class;
280 GstV4l2VideoEncClass *baseclass;
281
282 parent_class = g_type_class_peek_parent (klass);
283
284 element_class = (GstElementClass *) klass;
285 gobject_class = (GObjectClass *) klass;
286 baseclass = (GstV4l2VideoEncClass *) (klass);
287
288 GST_DEBUG_CATEGORY_INIT (gst_v4l2_h264_enc_debug, "v4l2h264enc", 0,
289 "V4L2 H.264 Encoder");
290
291 gst_element_class_set_static_metadata (element_class,
292 "V4L2 H.264 Encoder",
293 "Codec/Encoder/Video/Hardware",
294 "Encode H.264 video streams via V4L2 API", "ayaka <ayaka@soulik.info>");
295
296 gobject_class->set_property =
297 GST_DEBUG_FUNCPTR (gst_v4l2_h264_enc_set_property);
298 gobject_class->get_property =
299 GST_DEBUG_FUNCPTR (gst_v4l2_h264_enc_get_property);
300
301 baseclass->codec_name = "H264";
302 baseclass->profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
303 baseclass->profile_to_string = v4l2_profile_to_string;
304 baseclass->profile_from_string = v4l2_profile_from_string;
305 baseclass->level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
306 baseclass->level_to_string = v4l2_level_to_string;
307 baseclass->level_from_string = v4l2_level_from_string;
308 }
309
310 /* Probing functions */
311 gboolean
gst_v4l2_is_h264_enc(GstCaps * sink_caps,GstCaps * src_caps)312 gst_v4l2_is_h264_enc (GstCaps * sink_caps, GstCaps * src_caps)
313 {
314 return gst_v4l2_is_video_enc (sink_caps, src_caps,
315 gst_static_caps_get (&src_template_caps));
316 }
317
318 void
gst_v4l2_h264_enc_register(GstPlugin * plugin,const gchar * basename,const gchar * device_path,GstCaps * sink_caps,GstCaps * src_caps)319 gst_v4l2_h264_enc_register (GstPlugin * plugin, const gchar * basename,
320 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
321 {
322 gst_v4l2_video_enc_register (plugin, GST_TYPE_V4L2_H264_ENC,
323 "h264", basename, device_path, sink_caps,
324 gst_static_caps_get (&src_template_caps), src_caps);
325 }
326