Skip to contentSkip to conversation

v4l2 H264 encoder plugin changes

PATCH In review [email protected] 3 participants 4 messages May 19, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 v4l2 H264 encoder plugin changes

From: Roopang Patel <[email protected]>

Changes:
1) Given support of v4l2 extra controls such as bitrate,gopsize and iperiod for H264 encoding and also added as v4l2 H264 plugin properties to set and get for dynamic value change.

2) We need to set these extra controls before the gstreamer pipeline set format API invoke.so, we are setting extra controls when the pipeline change the state GST_STATE_CHANGE_READY_TO_PAUSED.

---
 sys/v4l2/gstv4l2h264enc.c | 135 +++++++++++++++++++++++++++++++++++++-
 sys/v4l2/gstv4l2h264enc.h |   9 +++
 2 files changed, 142 insertions(+), 2 deletions(-)
sys/v4l2/gstv4l2h264enc.c +133 -2
index ee160b3a0..169e6ae69 100644
@@ -47,6 +47,9 @@ enum
 {
   PROP_0,
   V4L2_STD_OBJECT_PROPS,
+  PROP_BITRATE,
+  PROP_GOPSIZE,
+  PROP_I_PERIOD,
 /* TODO add H264 controls
  * PROP_I_FRAME_QP,
  * PROP_P_FRAME_QP,
@@ -78,14 +81,46 @@ static void
 gst_v4l2_h264_enc_set_property (GObject * object,
     guint prop_id, const GValue * value, GParamSpec * pspec)
 {
-  /* TODO */
+  GstV4l2H264Enc *self = GST_V4L2_H264_ENC (object);
+
+  switch(prop_id)
+  {
+    case PROP_BITRATE:{
+     self->bitrate = g_value_get_uint (value);
+    }break;
+    case PROP_GOPSIZE:{
+     self->gopsize = g_value_get_uint (value);
+    }break;
+    case PROP_I_PERIOD:{
+     self->iperiod = g_value_get_uint (value);
+    }break;
+    default:{
+     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }break;
+  }
 }
 
 static void
 gst_v4l2_h264_enc_get_property (GObject * object,
     guint prop_id, GValue * value, GParamSpec * pspec)
 {
-  /* TODO */
+  GstV4l2H264Enc *self = GST_V4L2_H264_ENC (object);
+
+  switch(prop_id)
+  {
+    case PROP_BITRATE:{
+     g_value_set_uint (value, self->bitrate);
+    }break;
+    case PROP_GOPSIZE:{
+     g_value_set_uint (value, self->gopsize);
+    }break;
+    case PROP_I_PERIOD:{
+     g_value_set_uint (value, self->iperiod);
+    }break;
+    default:{
+     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }break;
+  }
 }
 
 static gint
@@ -270,6 +305,82 @@ v4l2_level_to_string (gint v4l2_level)
 static void
 gst_v4l2_h264_enc_init (GstV4l2H264Enc * self)
 {
+  /* Initializing H264 encoder control parameter with
+  * default values */
+  self->bitrate = DEFAULT_PROP_BITRATE;
+  self->gopsize = DEFAULT_PROP_GOPSIZE;
+  self->iperiod = DEFAULT_PROP_I_PERIOD;
+}
+
+static void
+gst_v4l2_h264_enc_get_ctrls(GstV4l2Object * v4l2object)
+{
+  struct v4l2_ext_controls ctrls;
+  struct v4l2_ext_control controls[3];
+  guint i;
+
+  memset(&ctrls, 0, sizeof(ctrls));
+  memset(controls, 0, sizeof(controls));
+
+  ctrls.which = V4L2_CTRL_WHICH_CUR_VAL;
+  ctrls.count = 3;
+  ctrls.controls = controls;
+
+  controls[0].id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
+  controls[1].id = V4L2_CID_MPEG_VIDEO_BITRATE;
+  controls[2].id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD;
+
+  if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_EXT_CTRLS, &ctrls)) {
+    GST_DEBUG_OBJECT(v4l2object,"Failed to get extra "
+    "controls for H264 encoder");
+    return;
+  }
+}
+
+static void
+gst_v4l2_h264_enc_set_ctrls(GstV4l2Object * v4l2object, guint bitrate,
+							guint gop_size, guint i_period)
+{
+  struct v4l2_ext_controls ctrls;
+  struct v4l2_ext_control controls[3];
+  guint i;
+
+  memset(&ctrls, 0, sizeof(ctrls));
+  memset(controls, 0, sizeof(controls));
+
+  ctrls.which = V4L2_CTRL_WHICH_CUR_VAL;
+  ctrls.count = 3;
+  ctrls.controls = controls;
+
+  controls[0].id = V4L2_CID_MPEG_VIDEO_GOP_SIZE;
+  controls[0].value = gop_size;
+  controls[1].id = V4L2_CID_MPEG_VIDEO_BITRATE;
+  controls[1].value = bitrate;
+  controls[2].id = V4L2_CID_MPEG_VIDEO_H264_I_PERIOD;
+  controls[2].value = i_period;
+
+  if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_EXT_CTRLS, &ctrls)) {
+    GST_DEBUG_OBJECT(v4l2object,"Failed to set extra "
+    "controls for encoder");
+    return;
+  }
+}
+
+static GstStateChangeReturn
+gst_v4l2_h264_enc_change_state (GstElement * element,
+    GstStateChange transition)
+{
+  GstV4l2VideoEnc *parent = GST_V4L2_VIDEO_ENC (element);
+  GstV4l2H264Enc *self = GST_V4L2_H264_ENC (element);
+
+  if(transition == GST_STATE_CHANGE_READY_TO_PAUSED)
+  {
+    gst_v4l2_h264_enc_set_ctrls(parent->v4l2output, self->bitrate,
+                                self->gopsize, self->iperiod);
+    gst_v4l2_h264_enc_get_ctrls(parent->v4l2output);
+  }
+
+  return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
 }
 
 static void
@@ -278,12 +389,14 @@ gst_v4l2_h264_enc_class_init (GstV4l2H264EncClass * klass)
   GstElementClass *element_class;
   GObjectClass *gobject_class;
   GstV4l2VideoEncClass *baseclass;
+  GstVideoEncoderClass *video_encoder_class;
 
   parent_class = g_type_class_peek_parent (klass);
 
   element_class = (GstElementClass *) klass;
   gobject_class = (GObjectClass *) klass;
   baseclass = (GstV4l2VideoEncClass *) (klass);
+  video_encoder_class = (GstVideoEncoderClass *) klass;
 
   GST_DEBUG_CATEGORY_INIT (gst_v4l2_h264_enc_debug, "v4l2h264enc", 0,
       "V4L2 H.264 Encoder");
@@ -298,6 +411,9 @@ gst_v4l2_h264_enc_class_init (GstV4l2H264EncClass * klass)
   gobject_class->get_property =
       GST_DEBUG_FUNCPTR (gst_v4l2_h264_enc_get_property);
 
+  element_class->change_state =
+        GST_DEBUG_FUNCPTR (gst_v4l2_h264_enc_change_state);
+
   baseclass->codec_name = "H264";
   baseclass->profile_cid = V4L2_CID_MPEG_VIDEO_H264_PROFILE;
   baseclass->profile_to_string = v4l2_profile_to_string;
@@ -305,6 +421,21 @@ gst_v4l2_h264_enc_class_init (GstV4l2H264EncClass * klass)
   baseclass->level_cid = V4L2_CID_MPEG_VIDEO_H264_LEVEL;
   baseclass->level_to_string = v4l2_level_to_string;
   baseclass->level_from_string = v4l2_level_from_string;
+
+  g_object_class_install_property (gobject_class, PROP_BITRATE,
+    g_param_spec_uint ("bitrate", "Bitrate", "Bitrate in kbit/sec",
+    24000, 100 * 1000 * 1000, DEFAULT_PROP_BITRATE,
+    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS ));
+
+  g_object_class_install_property (gobject_class, PROP_GOPSIZE,
+    g_param_spec_uint ("gopsize", "Gopsize", "IDR frame interval",
+    1,7200, DEFAULT_PROP_GOPSIZE,
+    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS ));
+
+  g_object_class_install_property (gobject_class, PROP_I_PERIOD,
+    g_param_spec_uint ("iperiod", "Iperiod", "I Frame Period",
+    1,600, DEFAULT_PROP_I_PERIOD,
+    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS ));
 }
 
 /* Probing functions */
sys/v4l2/gstv4l2h264enc.h +9 -0
index 3bfa34346..370bcf535 100644
@@ -39,9 +39,18 @@ G_BEGIN_DECLS
 typedef struct _GstV4l2H264Enc GstV4l2H264Enc;
 typedef struct _GstV4l2H264EncClass GstV4l2H264EncClass;
 
+#define DEFAULT_PROP_BITRATE 	(500 * 1000)
+#define DEFAULT_PROP_GOPSIZE 	(1800)
+#define DEFAULT_PROP_I_PERIOD 	(30)
+
 struct _GstV4l2H264Enc
 {
   GstV4l2VideoEnc parent;
+
+  /*H264 Extra Controls*/
+  guint bitrate;
+  guint gopsize;
+  guint iperiod;
 };
 
 struct _GstV4l2H264EncClass
Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · On 18:16-20210519, [email protected] wrote:

From: Roopang Patel <[email protected]>

Changes:
1) Given support of v4l2 extra controls such as bitrate,gopsize and iperiod for H264 encoding and also added as v4l2 H264 plugin properties to set and get for dynamic value change.

Quoted 4 lines

2) We need to set these extra controls before the gstreamer pipeline set format API invoke.so, we are setting extra controls when the pipeline change the state GST_STATE_CHANGE_READY_TO_PAUSED.

I'd suggest you might want to study how to write a proper commit message
https://www.kernel.org/doc/html/v4.10/process/submitting-patches.html#describe-your-changes

is a good reference, so is a bunch of internet articles..

Quoted 4 lines
---
 sys/v4l2/gstv4l2h264enc.c | 135 +++++++++++++++++++++++++++++++++++++-
 sys/v4l2/gstv4l2h264enc.h |   9 +++
 2 files changed, 142 insertions(+), 2 deletions(-)
	Ummm.... -ECONFUSED.

How does this patch apply to [1]

[1] http://arago-project.org/git/?p=meta-arago.git;a=tree

Quoted 27 lines
sys/v4l2/gstv4l2h264enc.c +10 -1
index ee160b3a0..169e6ae69 100644
@@ -47,6 +47,9 @@ enum
 {
   PROP_0,
   V4L2_STD_OBJECT_PROPS,
+  PROP_BITRATE,
+  PROP_GOPSIZE,
+  PROP_I_PERIOD,
 /* TODO add H264 controls
  * PROP_I_FRAME_QP,
  * PROP_P_FRAME_QP,
@@ -78,14 +81,46 @@ static void
 gst_v4l2_h264_enc_set_property (GObject * object,
     guint prop_id, const GValue * value, GParamSpec * pspec)
 {
-  /* TODO */
+  GstV4l2H264Enc *self = GST_V4L2_H264_ENC (object);
+
+  switch(prop_id)
+  {
+    case PROP_BITRATE:{
+     self->bitrate = g_value_get_uint (value);
+    }break;

sys/v4l2/gstv4l2h264enc.c static void · line 100

Quoted 10 lines
sys/v4l2/gstv4l2h264enc.c +10 -0
+    case PROP_GOPSIZE:{
+     self->gopsize = g_value_get_uint (value);
+    }break;
+    case PROP_I_PERIOD:{
+     self->iperiod = g_value_get_uint (value);
+    }break;
+    default:{
+     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }break;
+  }

If this is a kernel patch, please don't post it in public kernel list with ti.com ID - this is wrong in so many kernel coding style ways.

[...]

Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D
#2 lore raw
Quoted 61 lines · Nishanth Menon
Quoted 8 lines · On 18:16-20210519, [email protected] wrote:

From: Roopang Patel <[email protected]>

Changes:
1) Given support of v4l2 extra controls such as bitrate,gopsize and iperiod for H264 encoding and also added as v4l2 H264 plugin properties to set and get for dynamic value change.

Quoted 4 lines

2) We need to set these extra controls before the gstreamer pipeline set format API invoke.so, we are setting extra controls when the pipeline change the state GST_STATE_CHANGE_READY_TO_PAUSED.

I'd suggest you might want to study how to write a proper commit message
https://www.kernel.org/doc/html/v4.10/process/submitting-patches.html#describe-your-changes

is a good reference, so is a bunch of internet articles..

Quoted 4 lines
---
 sys/v4l2/gstv4l2h264enc.c | 135 +++++++++++++++++++++++++++++++++++++-
 sys/v4l2/gstv4l2h264enc.h |   9 +++
 2 files changed, 142 insertions(+), 2 deletions(-)
	Ummm.... -ECONFUSED.

How does this patch apply to [1]

[1] http://arago-project.org/git/?p=meta-arago.git;a=tree

Quoted 32 lines
sys/v4l2/gstv4l2h264enc.c +15 -1
index ee160b3a0..169e6ae69 100644
@@ -47,6 +47,9 @@ enum
 {
   PROP_0,
   V4L2_STD_OBJECT_PROPS,
+  PROP_BITRATE,
+  PROP_GOPSIZE,
+  PROP_I_PERIOD,
 /* TODO add H264 controls
  * PROP_I_FRAME_QP,
  * PROP_P_FRAME_QP,
@@ -78,14 +81,46 @@ static void
 gst_v4l2_h264_enc_set_property (GObject * object,
     guint prop_id, const GValue * value, GParamSpec * pspec)
 {
-  /* TODO */
+  GstV4l2H264Enc *self = GST_V4L2_H264_ENC (object);
+
+  switch(prop_id)
+  {
+    case PROP_BITRATE:{
+     self->bitrate = g_value_get_uint (value);
+    }break;
+    case PROP_GOPSIZE:{
+     self->gopsize = g_value_get_uint (value);
+    }break;
+    case PROP_I_PERIOD:{
+     self->iperiod = g_value_get_uint (value);
Quoted 8 lines

sys/v4l2/gstv4l2h264enc.c static void · line 100

Quoted 5 lines
sys/v4l2/gstv4l2h264enc.c +5 -0
+    }break;
+    default:{
+     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }break;
+  }

If this is a kernel patch, please don't post it in public kernel list with ti.com ID - this is wrong in so many kernel coding style ways.

I believe this patch is for gstreamer-plugins-good:
https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/blob/master/sys/v4l2/gstv4l2h264enc.c

Still, not directly applicable to meta-araqgo...

Signature · 4 lines
Regards,
Denys Dmytriyenko <[email protected]>
PGP: 0x420902729A92C964 - https://denix.org/0x420902729A92C964
Fingerprint: 25FC E4A5 8A72 2F69 1186  6D76 4209 0272 9A92 C964
#3 lore raw

On 19:06-20210519, Denys Dmytriyenko wrote:
[...]

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

+ }

If this is a kernel patch, please don't post it in public kernel list with ti.com ID - this is wrong in so many kernel coding style ways.

I believe this patch is for gstreamer-plugins-good:
https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/blob/master/sys/v4l2/gstv4l2h264enc.c

Still, not directly applicable to meta-araqgo...

Thanks Denys.
https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/merge_requests
seems to be the path.

https://gitlab.freedesktop.org/gstreamer/gst-plugins-good/-/commits/master/sys/v4l2/gstv4l2h264enc.c
should give an idea of what the subject line, commit message style and code style should be.

Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D