2 * Copyright (C) 2014 SUMOMO Computer Association
3 * Author: ayaka <ayaka@soulik.info>
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.
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.
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.
32 #include "gstv4l2object.h"
33 #include "gstv4l2h264enc.h"
36 #include <gst/gst-i18n-plugin.h>
38 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_h264_enc_debug);
39 #define GST_CAT_DEFAULT gst_v4l2_h264_enc_debug
42 static GstStaticCaps src_template_caps =
43 GST_STATIC_CAPS ("video/x-h264, stream-format=(string) byte-stream, "
44 "alignment=(string) au");
49 V4L2_STD_OBJECT_PROPS,
50 /* TODO add H264 controls
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,
67 * PROP_SEI_FRAME_PACKING,
68 * PROP_SEI_FP_CURRENT_FRAME_0,
69 * PROP_SEI_FP_ARRANGEMENT_TYP,
74 #define gst_v4l2_h264_enc_parent_class parent_class
75 G_DEFINE_TYPE (GstV4l2H264Enc, gst_v4l2_h264_enc, GST_TYPE_V4L2_VIDEO_ENC);
78 gst_v4l2_h264_enc_set_property (GObject * object,
79 guint prop_id, const GValue * value, GParamSpec * pspec)
85 gst_v4l2_h264_enc_get_property (GObject * object,
86 guint prop_id, GValue * value, GParamSpec * pspec)
92 v4l2_profile_from_string (const gchar * profile)
94 gint v4l2_profile = -1;
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;
131 GST_WARNING ("Unsupported profile string '%s'", profile);
138 v4l2_profile_to_string (gint v4l2_profile)
140 switch (v4l2_profile) {
141 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
143 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
144 return "constrained-baseline";
145 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
147 case V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED:
149 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
151 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_10:
153 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_422:
155 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_PREDICTIVE:
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";
176 GST_WARNING ("Unsupported V4L2 profile %i", v4l2_profile);
184 v4l2_level_from_string (const gchar * level)
186 gint v4l2_level = -1;
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;
221 GST_WARNING ("Unsupported level '%s'", level);
227 v4l2_level_to_string (gint v4l2_level)
229 switch (v4l2_level) {
230 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
232 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
234 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
236 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
238 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
240 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
242 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
244 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
246 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
248 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
250 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
252 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
254 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
256 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
258 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
260 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
263 GST_WARNING ("Unsupported V4L2 level %i", v4l2_level);
271 gst_v4l2_h264_enc_init (GstV4l2H264Enc * self)
276 gst_v4l2_h264_enc_class_init (GstV4l2H264EncClass * klass)
278 GstElementClass *element_class;
279 GObjectClass *gobject_class;
280 GstV4l2VideoEncClass *baseclass;
282 parent_class = g_type_class_peek_parent (klass);
284 element_class = (GstElementClass *) klass;
285 gobject_class = (GObjectClass *) klass;
286 baseclass = (GstV4l2VideoEncClass *) (klass);
288 GST_DEBUG_CATEGORY_INIT (gst_v4l2_h264_enc_debug, "v4l2h264enc", 0,
289 "V4L2 H.264 Encoder");
291 gst_element_class_set_static_metadata (element_class,
292 "V4L2 H.264 Encoder",
293 "Codec/Encoder/Video",
294 "Encode H.264 video streams via V4L2 API", "ayaka <ayaka@soulik.info>");
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);
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;
310 /* Probing functions */
312 gst_v4l2_is_h264_enc (GstCaps * sink_caps, GstCaps * src_caps)
314 return gst_v4l2_is_video_enc (sink_caps, src_caps,
315 gst_static_caps_get (&src_template_caps));
319 gst_v4l2_h264_enc_register (GstPlugin * plugin, const gchar * basename,
320 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
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);