1 /*
2  * Copyright (C) 2017 Collabora Inc.
3  *    Author: Nicolas Dufresne <nicolas.dufresne@collabora.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 "gstv4l2mpeg4enc.h"
34 
35 #include <string.h>
36 #include <gst/gst-i18n-plugin.h>
37 
38 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_mpeg4_enc_debug);
39 #define GST_CAT_DEFAULT gst_v4l2_mpeg4_enc_debug
40 
41 static GstStaticCaps src_template_caps =
42 GST_STATIC_CAPS ("video/mpeg, mpegversion=(int)4, systemstream=(boolean)FALSE");
43 
44 enum
45 {
46   PROP_0,
47   V4L2_STD_OBJECT_PROPS,
48   /* TODO */
49 };
50 
51 #define gst_v4l2_mpeg4_enc_parent_class parent_class
52 G_DEFINE_TYPE (GstV4l2Mpeg4Enc, gst_v4l2_mpeg4_enc, GST_TYPE_V4L2_VIDEO_ENC);
53 
54 static void
gst_v4l2_mpeg4_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)55 gst_v4l2_mpeg4_enc_set_property (GObject * object,
56     guint prop_id, const GValue * value, GParamSpec * pspec)
57 {
58   /* TODO */
59 }
60 
61 static void
gst_v4l2_mpeg4_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)62 gst_v4l2_mpeg4_enc_get_property (GObject * object,
63     guint prop_id, GValue * value, GParamSpec * pspec)
64 {
65   /* TODO */
66 }
67 
68 static gint
v4l2_profile_from_string(const gchar * profile)69 v4l2_profile_from_string (const gchar * profile)
70 {
71   gint v4l2_profile = -1;
72 
73   if (g_str_equal (profile, "simple"))
74     v4l2_profile = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE;
75   else if (g_str_equal (profile, "advanced-simple"))
76     v4l2_profile = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE;
77   else if (g_str_equal (profile, "core"))
78     v4l2_profile = V4L2_MPEG_VIDEO_MPEG4_PROFILE_CORE;
79   else if (g_str_equal (profile, "simple-scalable"))
80     v4l2_profile = V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE_SCALABLE;
81   else if (g_str_equal (profile, "advanced-coding-efficiency"))
82     v4l2_profile = V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY;
83   else
84     GST_WARNING ("Unsupported profile string '%s'", profile);
85 
86   return v4l2_profile;
87 }
88 
89 static const gchar *
v4l2_profile_to_string(gint v4l2_profile)90 v4l2_profile_to_string (gint v4l2_profile)
91 {
92   switch (v4l2_profile) {
93     case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
94       return "simple";
95     case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
96       return "advanced-simple";
97     case V4L2_MPEG_VIDEO_MPEG4_PROFILE_CORE:
98       return "core";
99     case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE_SCALABLE:
100       return "simple-scalable";
101     case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY:
102       return "advanced-coding-efficiency";
103     default:
104       GST_WARNING ("Unsupported V4L2 profile %i", v4l2_profile);
105       break;
106   }
107 
108   return NULL;
109 }
110 
111 static gint
v4l2_level_from_string(const gchar * level)112 v4l2_level_from_string (const gchar * level)
113 {
114   gint v4l2_level = -1;
115 
116   if (g_str_equal (level, "0"))
117     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0;
118   else if (g_str_equal (level, "0b"))
119     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B;
120   else if (g_str_equal (level, "1"))
121     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_1;
122   else if (g_str_equal (level, "2"))
123     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_2;
124   else if (g_str_equal (level, "3"))
125     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_3;
126   else if (g_str_equal (level, "3b"))
127     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B;
128   else if (g_str_equal (level, "4"))
129     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_4;
130   else if (g_str_equal (level, "5"))
131     v4l2_level = V4L2_MPEG_VIDEO_MPEG4_LEVEL_5;
132   else
133     GST_WARNING ("Unsupported level '%s'", level);
134 
135   return v4l2_level;
136 }
137 
138 static const gchar *
v4l2_level_to_string(gint v4l2_level)139 v4l2_level_to_string (gint v4l2_level)
140 {
141   switch (v4l2_level) {
142     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0:
143       return "0";
144     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B:
145       return "0b";
146     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_1:
147       return "1";
148     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_2:
149       return "2";
150     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_3:
151       return "3";
152     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_3B:
153       return "3b";
154     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_4:
155       return "4";
156     case V4L2_MPEG_VIDEO_MPEG4_LEVEL_5:
157       return "5";
158     default:
159       GST_WARNING ("Unsupported V4L2 level %i", v4l2_level);
160       break;
161   }
162 
163   return NULL;
164 }
165 
166 static void
gst_v4l2_mpeg4_enc_init(GstV4l2Mpeg4Enc * self)167 gst_v4l2_mpeg4_enc_init (GstV4l2Mpeg4Enc * self)
168 {
169 }
170 
171 static void
gst_v4l2_mpeg4_enc_class_init(GstV4l2Mpeg4EncClass * klass)172 gst_v4l2_mpeg4_enc_class_init (GstV4l2Mpeg4EncClass * klass)
173 {
174   GstElementClass *element_class;
175   GObjectClass *gobject_class;
176   GstV4l2VideoEncClass *baseclass;
177 
178   parent_class = g_type_class_peek_parent (klass);
179 
180   element_class = (GstElementClass *) klass;
181   gobject_class = (GObjectClass *) klass;
182   baseclass = (GstV4l2VideoEncClass *) (klass);
183 
184   GST_DEBUG_CATEGORY_INIT (gst_v4l2_mpeg4_enc_debug, "v4l2mpeg4enc", 0,
185       "V4L2 MPEG4 Encoder");
186 
187   gst_element_class_set_static_metadata (element_class,
188       "V4L2 MPEG4 Encoder",
189       "Codec/Encoder/Video/Hardware",
190       "Encode MPEG4 video streams via V4L2 API",
191       "Nicolas Dufresne <nicolas.dufresne@collabora.com");
192 
193   gobject_class->set_property =
194       GST_DEBUG_FUNCPTR (gst_v4l2_mpeg4_enc_set_property);
195   gobject_class->get_property =
196       GST_DEBUG_FUNCPTR (gst_v4l2_mpeg4_enc_get_property);
197 
198   baseclass->codec_name = "MPEG4";
199   baseclass->profile_cid = V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE;
200   baseclass->profile_to_string = v4l2_profile_to_string;
201   baseclass->profile_from_string = v4l2_profile_from_string;
202   baseclass->level_cid = V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL;
203   baseclass->level_to_string = v4l2_level_to_string;
204   baseclass->level_from_string = v4l2_level_from_string;
205 }
206 
207 /* Probing functions */
208 gboolean
gst_v4l2_is_mpeg4_enc(GstCaps * sink_caps,GstCaps * src_caps)209 gst_v4l2_is_mpeg4_enc (GstCaps * sink_caps, GstCaps * src_caps)
210 {
211   return gst_v4l2_is_video_enc (sink_caps, src_caps,
212       gst_static_caps_get (&src_template_caps));
213 }
214 
215 void
gst_v4l2_mpeg4_enc_register(GstPlugin * plugin,const gchar * basename,const gchar * device_path,GstCaps * sink_caps,GstCaps * src_caps)216 gst_v4l2_mpeg4_enc_register (GstPlugin * plugin, const gchar * basename,
217     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
218 {
219   gst_v4l2_video_enc_register (plugin, GST_TYPE_V4L2_MPEG4_ENC,
220       "mpeg4", basename, device_path, sink_caps,
221       gst_static_caps_get (&src_template_caps), src_caps);
222 }
223