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