1 /*
2 * GStreamer Intel MSDK plugin
3 * Copyright (c) 2019 Intel Corporation. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /**
33 * SECTION: element-msdkvp9enc
34 * @title: msdkvp9enc
35 * @short_description: Intel MSDK VP9 encoder
36 *
37 * VP9 video encoder based on Intel MFX
38 *
39 * ## Example launch line
40 * ```
41 * gst-launch-1.0 videotestsrc num-buffers=90 ! msdkvp9enc ! matroskamux ! filesink location=output.webm
42 * ```
43 *
44 * Since: 1.18
45 *
46 */
47
48 #ifdef HAVE_CONFIG_H
49 # include <config.h>
50 #endif
51
52 #include <gst/allocators/gstdmabuf.h>
53
54 #include "gstmsdkvp9enc.h"
55
56 GST_DEBUG_CATEGORY_EXTERN (gst_msdkvp9enc_debug);
57 #define GST_CAT_DEFAULT gst_msdkvp9enc_debug
58
59 #define RAW_FORMATS "NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE, VUYA"
60 #define PROFILES "0, 1, 2"
61
62 #if (MFX_VERSION >= 1027)
63 #define COMMON_FORMAT "{ " RAW_FORMATS ", Y410 }"
64 #define SRC_PROFILES "{ " PROFILES ", 3 }"
65 #else
66 #define COMMON_FORMAT "{ " RAW_FORMATS " }"
67 #define SRC_PROFILES "{ " PROFILES " }"
68 #endif
69
70 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_PAD_SINK,
72 GST_PAD_ALWAYS,
73 GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
74 "{ NV12, P010_10LE }")));
75
76 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_PAD_SRC,
78 GST_PAD_ALWAYS,
79 GST_STATIC_CAPS ("video/x-vp9, "
80 "framerate = (fraction) [0/1, MAX], "
81 "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
82 "profile = (string) " SRC_PROFILES)
83 );
84
85 #define gst_msdkvp9enc_parent_class parent_class
86 G_DEFINE_TYPE (GstMsdkVP9Enc, gst_msdkvp9enc, GST_TYPE_MSDKENC);
87
88 static gboolean
gst_msdkvp9enc_set_format(GstMsdkEnc * encoder)89 gst_msdkvp9enc_set_format (GstMsdkEnc * encoder)
90 {
91 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (encoder);
92 GstCaps *template_caps;
93 GstCaps *allowed_caps = NULL;
94
95 thiz->profile = MFX_PROFILE_VP9_0;
96 template_caps = gst_static_pad_template_get_caps (&src_factory);
97 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
98
99 /* If downstream has ANY caps let encoder decide profile and level */
100 if (allowed_caps == template_caps) {
101 GST_INFO_OBJECT (thiz,
102 "downstream has ANY caps, profile/level set to auto");
103 } else if (allowed_caps) {
104 GstStructure *s;
105 const gchar *profile;
106
107 if (gst_caps_is_empty (allowed_caps)) {
108 gst_caps_unref (allowed_caps);
109 gst_caps_unref (template_caps);
110 return FALSE;
111 }
112
113 allowed_caps = gst_caps_make_writable (allowed_caps);
114 allowed_caps = gst_caps_fixate (allowed_caps);
115 s = gst_caps_get_structure (allowed_caps, 0);
116 profile = gst_structure_get_string (s, "profile");
117
118 if (profile) {
119 if (!strcmp (profile, "3")) {
120 thiz->profile = MFX_PROFILE_VP9_3;
121 } else if (!strcmp (profile, "2")) {
122 thiz->profile = MFX_PROFILE_VP9_2;
123 } else if (!strcmp (profile, "1")) {
124 thiz->profile = MFX_PROFILE_VP9_1;
125 } else if (!strcmp (profile, "0")) {
126 thiz->profile = MFX_PROFILE_VP9_0;
127 } else {
128 g_assert_not_reached ();
129 }
130 }
131
132 gst_caps_unref (allowed_caps);
133 }
134
135 gst_caps_unref (template_caps);
136
137 return TRUE;
138 }
139
140 static gboolean
gst_msdkvp9enc_configure(GstMsdkEnc * encoder)141 gst_msdkvp9enc_configure (GstMsdkEnc * encoder)
142 {
143 GstMsdkVP9Enc *vp9enc = GST_MSDKVP9ENC (encoder);
144 mfxSession session;
145
146 if (encoder->hardware) {
147 session = gst_msdk_context_get_session (encoder->context);
148
149 if (!gst_msdk_load_plugin (session, &MFX_PLUGINID_VP9E_HW, 1, "msdkvp9enc"))
150 return FALSE;
151 }
152
153 encoder->num_extra_frames = encoder->async_depth - 1;
154 encoder->param.mfx.CodecId = MFX_CODEC_VP9;
155 encoder->param.mfx.CodecLevel = 0;
156
157 switch (encoder->param.mfx.FrameInfo.FourCC) {
158 #if (MFX_VERSION >= 1027)
159 case MFX_FOURCC_Y410:
160 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_3;
161 break;
162 #endif
163
164 case MFX_FOURCC_P010:
165 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_2;
166 break;
167
168 case MFX_FOURCC_AYUV:
169 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_1;
170 break;
171
172 default:
173 encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_0;
174 break;
175 }
176
177 /* As the frame width and height is rounded up to 128 and 32 since commit 8daac1c,
178 * so the width, height for initialization should be rounded up to 128 and 32
179 * too because VP9 encoder in MSDK will do some check on width and height.
180 */
181 encoder->param.mfx.FrameInfo.Width =
182 GST_ROUND_UP_128 (encoder->param.mfx.FrameInfo.CropW);
183 encoder->param.mfx.FrameInfo.Height =
184 GST_ROUND_UP_32 (encoder->param.mfx.FrameInfo.CropH);
185
186 /* Always turn on this flag for VP9 */
187 encoder->param.mfx.LowPower = MFX_CODINGOPTION_ON;
188
189 /* Enable Extended coding options */
190 gst_msdkenc_ensure_extended_coding_options (encoder);
191
192 memset (&vp9enc->ext_vp9, 0, sizeof (vp9enc->ext_vp9));
193 vp9enc->ext_vp9.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
194 vp9enc->ext_vp9.Header.BufferSz = sizeof (vp9enc->ext_vp9);
195 vp9enc->ext_vp9.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
196
197 gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & vp9enc->ext_vp9);
198
199 return TRUE;
200 }
201
202 static inline const gchar *
profile_to_string(gint profile)203 profile_to_string (gint profile)
204 {
205 switch (profile) {
206 case MFX_PROFILE_VP9_3:
207 return "3";
208 case MFX_PROFILE_VP9_2:
209 return "2";
210 case MFX_PROFILE_VP9_1:
211 return "1";
212 case MFX_PROFILE_VP9_0:
213 return "0";
214 default:
215 break;
216 }
217
218 return NULL;
219 }
220
221 static GstCaps *
gst_msdkvp9enc_set_src_caps(GstMsdkEnc * encoder)222 gst_msdkvp9enc_set_src_caps (GstMsdkEnc * encoder)
223 {
224 GstCaps *caps;
225 GstStructure *structure;
226 const gchar *profile;
227
228 caps = gst_caps_new_empty_simple ("video/x-vp9");
229 structure = gst_caps_get_structure (caps, 0);
230
231 profile = profile_to_string (encoder->param.mfx.CodecProfile);
232 if (profile)
233 gst_structure_set (structure, "profile", G_TYPE_STRING, profile, NULL);
234
235 return caps;
236 }
237
238 static void
gst_msdkvp9enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)239 gst_msdkvp9enc_set_property (GObject * object, guint prop_id,
240 const GValue * value, GParamSpec * pspec)
241 {
242 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
243
244 if (!gst_msdkenc_set_common_property (object, prop_id, value, pspec))
245 GST_WARNING_OBJECT (thiz, "Failed to set common encode property");
246 }
247
248 static void
gst_msdkvp9enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)249 gst_msdkvp9enc_get_property (GObject * object, guint prop_id, GValue * value,
250 GParamSpec * pspec)
251 {
252 GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
253
254 if (!gst_msdkenc_get_common_property (object, prop_id, value, pspec))
255 GST_WARNING_OBJECT (thiz, "Failed to get common encode property");
256 }
257
258 static void
gst_msdkvp9enc_class_init(GstMsdkVP9EncClass * klass)259 gst_msdkvp9enc_class_init (GstMsdkVP9EncClass * klass)
260 {
261 GObjectClass *gobject_class;
262 GstElementClass *element_class;
263 GstMsdkEncClass *encoder_class;
264
265 gobject_class = G_OBJECT_CLASS (klass);
266 element_class = GST_ELEMENT_CLASS (klass);
267 encoder_class = GST_MSDKENC_CLASS (klass);
268
269 gobject_class->set_property = gst_msdkvp9enc_set_property;
270 gobject_class->get_property = gst_msdkvp9enc_get_property;
271
272 encoder_class->set_format = gst_msdkvp9enc_set_format;
273 encoder_class->configure = gst_msdkvp9enc_configure;
274 encoder_class->set_src_caps = gst_msdkvp9enc_set_src_caps;
275 encoder_class->qp_max = 255;
276 encoder_class->qp_min = 0;
277
278 gst_msdkenc_install_common_properties (encoder_class);
279
280 gst_element_class_set_static_metadata (element_class,
281 "Intel MSDK VP9 encoder",
282 "Codec/Encoder/Video/Hardware",
283 "VP9 video encoder based on " MFX_API_SDK,
284 "Haihao Xiang <haihao.xiang@intel.com>");
285
286 gst_element_class_add_static_pad_template (element_class, &sink_factory);
287 gst_element_class_add_static_pad_template (element_class, &src_factory);
288 }
289
290 static void
gst_msdkvp9enc_init(GstMsdkVP9Enc * thiz)291 gst_msdkvp9enc_init (GstMsdkVP9Enc * thiz)
292 {
293 }
294