xref: /OK3568_Linux_fs/external/gstreamer-rockchip/gst/rockchipmpp/gstmppvp8enc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2020 Rockchip Electronics Co., Ltd
3  *     Author: Jeffy Chen <jeffy.chen@rock-chips.com>
4  *
5  * Copyright 2021 Rockchip Electronics Co., Ltd
6  *     Author: Jeffy Chen <jeffy.chen@rock-chips.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <string.h>
30 
31 #include "gstmppvp8enc.h"
32 
33 #define GST_MPP_VP8_ENC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
34     GST_TYPE_MPP_VP8_ENC, GstMppVp8Enc))
35 
36 #define GST_CAT_DEFAULT mpp_vp8_enc_debug
37 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
38 
39 struct _GstMppVp8Enc
40 {
41   GstMppEnc parent;
42 
43   guint qp_init;
44   guint qp_min;
45   guint qp_max;
46 };
47 
48 #define parent_class gst_mpp_vp8_enc_parent_class
49 G_DEFINE_TYPE (GstMppVp8Enc, gst_mpp_vp8_enc, GST_TYPE_MPP_ENC);
50 
51 #define DEFAULT_PROP_QP_INIT 40
52 #define DEFAULT_PROP_QP_MIN 0
53 #define DEFAULT_PROP_QP_MAX 127
54 
55 enum
56 {
57   PROP_0,
58   PROP_QP_INIT,
59   PROP_QP_MIN,
60   PROP_QP_MAX,
61   PROP_LAST,
62 };
63 
64 #define GST_MPP_VP8_ENC_SIZE_CAPS \
65     "width  = (int) [ 128, MAX ], height = (int) [ 64, MAX ]"
66 
67 static GstStaticPadTemplate gst_mpp_vp8_enc_src_template =
68 GST_STATIC_PAD_TEMPLATE ("src",
69     GST_PAD_SRC,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS ("video/x-vp8, " GST_MPP_VP8_ENC_SIZE_CAPS));
72 
73 static GstStaticPadTemplate gst_mpp_vp8_enc_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
75     GST_PAD_SINK,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS ("video/x-raw,"
78         "format = (string) { " MPP_ENC_FORMATS " }, "
79         GST_MPP_VP8_ENC_SIZE_CAPS));
80 
81 static void
gst_mpp_vp8_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)82 gst_mpp_vp8_enc_set_property (GObject * object,
83     guint prop_id, const GValue * value, GParamSpec * pspec)
84 {
85   GstVideoEncoder *encoder = GST_VIDEO_ENCODER (object);
86   GstMppVp8Enc *self = GST_MPP_VP8_ENC (encoder);
87   GstMppEnc *mppenc = GST_MPP_ENC (encoder);
88 
89   switch (prop_id) {
90     case PROP_QP_INIT:{
91       guint qp_init = g_value_get_uint (value);
92       if (self->qp_init == qp_init)
93         return;
94 
95       self->qp_init = qp_init;
96       break;
97     }
98     case PROP_QP_MIN:{
99       guint qp_min = g_value_get_uint (value);
100       if (self->qp_min == qp_min)
101         return;
102 
103       self->qp_min = qp_min;
104       break;
105     }
106     case PROP_QP_MAX:{
107       guint qp_max = g_value_get_uint (value);
108       if (self->qp_max == qp_max)
109         return;
110 
111       self->qp_max = qp_max;
112       break;
113     }
114     default:
115       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116       return;
117   }
118 
119   mppenc->prop_dirty = TRUE;
120 }
121 
122 static void
gst_mpp_vp8_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)123 gst_mpp_vp8_enc_get_property (GObject * object,
124     guint prop_id, GValue * value, GParamSpec * pspec)
125 {
126   GstVideoEncoder *encoder = GST_VIDEO_ENCODER (object);
127   GstMppVp8Enc *self = GST_MPP_VP8_ENC (encoder);
128 
129   switch (prop_id) {
130     case PROP_QP_INIT:
131       g_value_set_uint (value, self->qp_init);
132       break;
133     case PROP_QP_MIN:
134       g_value_set_uint (value, self->qp_min);
135       break;
136     case PROP_QP_MAX:
137       g_value_set_uint (value, self->qp_max);
138       break;
139     default:
140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141       break;
142   }
143 }
144 
145 static gboolean
gst_mpp_vp8_enc_apply_properties(GstVideoEncoder * encoder)146 gst_mpp_vp8_enc_apply_properties (GstVideoEncoder * encoder)
147 {
148   GstMppVp8Enc *self = GST_MPP_VP8_ENC (encoder);
149   GstMppEnc *mppenc = GST_MPP_ENC (encoder);
150 
151   if (!mppenc->prop_dirty)
152     return TRUE;
153 
154   mpp_enc_cfg_set_s32 (mppenc->mpp_cfg, "vp8:qp_init", self->qp_init);
155   mpp_enc_cfg_set_s32 (mppenc->mpp_cfg, "vp8:qp_max", self->qp_max);
156   mpp_enc_cfg_set_s32 (mppenc->mpp_cfg, "vp8:qp_min", self->qp_min);
157   mpp_enc_cfg_set_s32 (mppenc->mpp_cfg, "vp8:disable_ivf", 1);
158 
159   return gst_mpp_enc_apply_properties (encoder);
160 }
161 
162 static gboolean
gst_mpp_vp8_enc_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)163 gst_mpp_vp8_enc_set_format (GstVideoEncoder * encoder,
164     GstVideoCodecState * state)
165 {
166   GstVideoEncoderClass *pclass = GST_VIDEO_ENCODER_CLASS (parent_class);
167   GstCaps *caps;
168 
169   if (!pclass->set_format (encoder, state))
170     return FALSE;
171 
172   if (!gst_mpp_vp8_enc_apply_properties (encoder))
173     return FALSE;
174 
175   caps = gst_caps_new_empty_simple ("video/x-vp8");
176   return gst_mpp_enc_set_src_caps (encoder, caps);
177 }
178 
179 static GstFlowReturn
gst_mpp_vp8_enc_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)180 gst_mpp_vp8_enc_handle_frame (GstVideoEncoder * encoder,
181     GstVideoCodecFrame * frame)
182 {
183   GstVideoEncoderClass *pclass = GST_VIDEO_ENCODER_CLASS (parent_class);
184 
185   if (G_UNLIKELY (!gst_mpp_vp8_enc_apply_properties (encoder))) {
186     gst_video_codec_frame_unref (frame);
187     return GST_FLOW_NOT_NEGOTIATED;
188   }
189 
190   return pclass->handle_frame (encoder, frame);
191 }
192 
193 static void
gst_mpp_vp8_enc_init(GstMppVp8Enc * self)194 gst_mpp_vp8_enc_init (GstMppVp8Enc * self)
195 {
196   self->parent.mpp_type = MPP_VIDEO_CodingVP8;
197 
198   self->qp_init = DEFAULT_PROP_QP_INIT;
199   self->qp_min = DEFAULT_PROP_QP_MIN;
200   self->qp_max = DEFAULT_PROP_QP_MAX;
201 }
202 
203 static void
gst_mpp_vp8_enc_class_init(GstMppVp8EncClass * klass)204 gst_mpp_vp8_enc_class_init (GstMppVp8EncClass * klass)
205 {
206   GstVideoEncoderClass *encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
207   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
209 
210   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "mppvp8enc", 0, "MPP VP8 encoder");
211 
212   encoder_class->set_format = GST_DEBUG_FUNCPTR (gst_mpp_vp8_enc_set_format);
213   encoder_class->handle_frame =
214       GST_DEBUG_FUNCPTR (gst_mpp_vp8_enc_handle_frame);
215 
216   gobject_class->set_property =
217       GST_DEBUG_FUNCPTR (gst_mpp_vp8_enc_set_property);
218   gobject_class->get_property =
219       GST_DEBUG_FUNCPTR (gst_mpp_vp8_enc_get_property);
220 
221   g_object_class_install_property (gobject_class, PROP_QP_INIT,
222       g_param_spec_uint ("qp-init", "Initial QP",
223           "Initial QP (lower value means higher quality)",
224           0, 127, DEFAULT_PROP_QP_INIT,
225           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 
227   g_object_class_install_property (gobject_class, PROP_QP_MIN,
228       g_param_spec_uint ("qp-min", "Min QP",
229           "Min QP", 0, 127, DEFAULT_PROP_QP_MIN,
230           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 
232   g_object_class_install_property (gobject_class, PROP_QP_MAX,
233       g_param_spec_uint ("qp-max", "Max QP",
234           "Max QP", 0, 127, DEFAULT_PROP_QP_MAX,
235           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 
237   gst_element_class_add_pad_template (element_class,
238       gst_static_pad_template_get (&gst_mpp_vp8_enc_src_template));
239 
240   gst_element_class_add_pad_template (element_class,
241       gst_static_pad_template_get (&gst_mpp_vp8_enc_sink_template));
242 
243   gst_element_class_set_static_metadata (element_class,
244       "Rockchip Mpp VP8 Encoder", "Codec/Encoder/Video",
245       "Encode video streams via Rockchip Mpp",
246       "Jeffy Chen <jeffy.chen@rock-chips.com>");
247 }
248 
249 gboolean
gst_mpp_vp8_enc_register(GstPlugin * plugin,guint rank)250 gst_mpp_vp8_enc_register (GstPlugin * plugin, guint rank)
251 {
252   if (!gst_mpp_enc_supported (MPP_VIDEO_CodingVP8))
253     return FALSE;
254 
255   if (g_getenv ("GST_MPP_VP8ENC_FAKE_VP8ENC"))
256     gst_element_register (plugin, "vp8enc", GST_RANK_PRIMARY,
257         gst_mpp_vp8_enc_get_type ());
258 
259   return gst_element_register (plugin, "mppvp8enc", rank,
260       gst_mpp_vp8_enc_get_type ());
261 }
262