1 /*
2 * Copyright 2018 Rockchip Electronics Co., Ltd
3 * Author: Randy Li <randy.li@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 "gstmppjpegenc.h"
32
33 #define GST_MPP_JPEG_ENC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
34 GST_TYPE_MPP_JPEG_ENC, GstMppJpegEnc))
35
36 #define GST_CAT_DEFAULT mpp_jpeg_enc_debug
37 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
38
39 struct _GstMppJpegEnc
40 {
41 GstMppEnc parent;
42
43 guint quant;
44 };
45
46 #define parent_class gst_mpp_jpeg_enc_parent_class
47 G_DEFINE_TYPE (GstMppJpegEnc, gst_mpp_jpeg_enc, GST_TYPE_MPP_ENC);
48
49 #define DEFAULT_PROP_QUANT 10
50
51 enum
52 {
53 PROP_0,
54 PROP_QUANT,
55 PROP_LAST,
56 };
57
58 #define GST_MPP_JPEG_ENC_SIZE_CAPS \
59 "width = (int) [ 16, MAX ], height = (int) [ 16, MAX ]"
60
61 static GstStaticPadTemplate gst_mpp_jpeg_enc_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
63 GST_PAD_SRC,
64 GST_PAD_ALWAYS,
65 GST_STATIC_CAPS ("image/jpeg, "
66 GST_MPP_JPEG_ENC_SIZE_CAPS "," "sof-marker = { 0 }"));
67
68 static GstStaticPadTemplate gst_mpp_jpeg_enc_sink_template =
69 GST_STATIC_PAD_TEMPLATE ("sink",
70 GST_PAD_SINK,
71 GST_PAD_ALWAYS,
72 GST_STATIC_CAPS ("video/x-raw,"
73 "format = (string) { " MPP_ENC_FORMATS " }, "
74 GST_MPP_JPEG_ENC_SIZE_CAPS));
75
76 static void
gst_mpp_h264_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)77 gst_mpp_h264_enc_set_property (GObject * object,
78 guint prop_id, const GValue * value, GParamSpec * pspec)
79 {
80 GstVideoEncoder *encoder = GST_VIDEO_ENCODER (object);
81 GstMppJpegEnc *self = GST_MPP_JPEG_ENC (encoder);
82 GstMppEnc *mppenc = GST_MPP_ENC (encoder);
83
84 switch (prop_id) {
85 case PROP_QUANT:{
86 guint quant = g_value_get_uint (value);
87 if (self->quant == quant)
88 return;
89
90 self->quant = quant;
91 break;
92 }
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 return;
96 }
97
98 mppenc->prop_dirty = TRUE;
99 }
100
101 static void
gst_mpp_h264_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)102 gst_mpp_h264_enc_get_property (GObject * object,
103 guint prop_id, GValue * value, GParamSpec * pspec)
104 {
105 GstVideoEncoder *encoder = GST_VIDEO_ENCODER (object);
106 GstMppJpegEnc *self = GST_MPP_JPEG_ENC (encoder);
107
108 switch (prop_id) {
109 case PROP_QUANT:
110 g_value_set_uint (value, self->quant);
111 break;
112 default:
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
114 break;
115 }
116 }
117
118 static gboolean
gst_mpp_jpeg_enc_apply_properties(GstVideoEncoder * encoder)119 gst_mpp_jpeg_enc_apply_properties (GstVideoEncoder * encoder)
120 {
121 GstMppJpegEnc *self = GST_MPP_JPEG_ENC (encoder);
122 GstMppEnc *mppenc = GST_MPP_ENC (encoder);
123
124 if (!mppenc->prop_dirty)
125 return TRUE;
126
127 mpp_enc_cfg_set_s32 (mppenc->mpp_cfg, "jpeg:quant", self->quant);
128
129 return gst_mpp_enc_apply_properties (encoder);
130 }
131
132 static gboolean
gst_mpp_jpeg_enc_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)133 gst_mpp_jpeg_enc_set_format (GstVideoEncoder * encoder,
134 GstVideoCodecState * state)
135 {
136 GstVideoEncoderClass *pclass = GST_VIDEO_ENCODER_CLASS (parent_class);
137 GstCaps *caps;
138
139 if (!pclass->set_format (encoder, state))
140 return FALSE;
141
142 if (!gst_mpp_jpeg_enc_apply_properties (encoder))
143 return FALSE;
144
145 caps = gst_caps_new_empty_simple ("image/jpeg");
146 return gst_mpp_enc_set_src_caps (encoder, caps);
147 }
148
149 static GstFlowReturn
gst_mpp_jpeg_enc_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)150 gst_mpp_jpeg_enc_handle_frame (GstVideoEncoder * encoder,
151 GstVideoCodecFrame * frame)
152 {
153 GstVideoEncoderClass *pclass = GST_VIDEO_ENCODER_CLASS (parent_class);
154
155 if (G_UNLIKELY (!gst_mpp_jpeg_enc_apply_properties (encoder))) {
156 gst_video_codec_frame_unref (frame);
157 return GST_FLOW_NOT_NEGOTIATED;
158 }
159
160 return pclass->handle_frame (encoder, frame);
161 }
162
163 static void
gst_mpp_jpeg_enc_init(GstMppJpegEnc * self)164 gst_mpp_jpeg_enc_init (GstMppJpegEnc * self)
165 {
166 self->parent.mpp_type = MPP_VIDEO_CodingMJPEG;
167
168 self->quant = DEFAULT_PROP_QUANT;
169 }
170
171 static void
gst_mpp_jpeg_enc_class_init(GstMppJpegEncClass * klass)172 gst_mpp_jpeg_enc_class_init (GstMppJpegEncClass * klass)
173 {
174 GstVideoEncoderClass *encoder_class = GST_VIDEO_ENCODER_CLASS (klass);
175 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
176 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
177
178 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "mppjpegenc", 0,
179 "MPP JPEG encoder");
180
181 encoder_class->set_format = GST_DEBUG_FUNCPTR (gst_mpp_jpeg_enc_set_format);
182 encoder_class->handle_frame =
183 GST_DEBUG_FUNCPTR (gst_mpp_jpeg_enc_handle_frame);
184
185 gobject_class->set_property =
186 GST_DEBUG_FUNCPTR (gst_mpp_h264_enc_set_property);
187 gobject_class->get_property =
188 GST_DEBUG_FUNCPTR (gst_mpp_h264_enc_get_property);
189
190 g_object_class_install_property (gobject_class, PROP_QUANT,
191 g_param_spec_uint ("quant", "Quant",
192 "JPEG Quantization", 0, 10, DEFAULT_PROP_QUANT,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194
195 gst_element_class_add_pad_template (element_class,
196 gst_static_pad_template_get (&gst_mpp_jpeg_enc_src_template));
197
198 gst_element_class_add_pad_template (element_class,
199 gst_static_pad_template_get (&gst_mpp_jpeg_enc_sink_template));
200
201 gst_element_class_set_static_metadata (element_class,
202 "Rockchip Mpp JPEG Encoder", "Codec/Encoder/Video",
203 "Encode video streams via Rockchip Mpp",
204 "Randy Li <randy.li@rock-chips.com>, "
205 "Jeffy Chen <jeffy.chen@rock-chips.com>");
206 }
207
208 gboolean
gst_mpp_jpeg_enc_register(GstPlugin * plugin,guint rank)209 gst_mpp_jpeg_enc_register (GstPlugin * plugin, guint rank)
210 {
211 if (!gst_mpp_enc_supported (MPP_VIDEO_CodingMJPEG))
212 return FALSE;
213
214 return gst_element_register (plugin, "mppjpegenc", rank,
215 gst_mpp_jpeg_enc_get_type ());
216 }
217