taglist, plugins: fix compiler warnings with GLib >= 2.76
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / msdk / gstmsdkvp9enc.c
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 #ifdef _WIN32
71 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
72     GST_PAD_SINK,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
75             "{ NV12, P010_10LE }") "; "
76         GST_MSDK_CAPS_MAKE_WITH_D3D11_FEATURE ("{ NV12, P010_10LE }")));
77 #else
78 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
79     GST_PAD_SINK,
80     GST_PAD_ALWAYS,
81     GST_STATIC_CAPS (GST_MSDK_CAPS_STR (COMMON_FORMAT,
82             "{ NV12, P010_10LE }") "; "
83         GST_MSDK_CAPS_MAKE_WITH_VA_FEATURE ("NV12")));
84 #endif
85
86 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
87     GST_PAD_SRC,
88     GST_PAD_ALWAYS,
89     GST_STATIC_CAPS ("video/x-vp9, "
90         "framerate = (fraction) [0/1, MAX], "
91         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
92         "profile = (string) " SRC_PROFILES)
93     );
94
95 #define gst_msdkvp9enc_parent_class parent_class
96 G_DEFINE_TYPE (GstMsdkVP9Enc, gst_msdkvp9enc, GST_TYPE_MSDKENC);
97
98 static gboolean
99 gst_msdkvp9enc_set_format (GstMsdkEnc * encoder)
100 {
101   GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (encoder);
102   GstCaps *template_caps;
103   GstCaps *allowed_caps = NULL;
104
105   thiz->profile = MFX_PROFILE_VP9_0;
106   template_caps = gst_static_pad_template_get_caps (&src_factory);
107   allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
108
109   /* If downstream has ANY caps let encoder decide profile and level */
110   if (allowed_caps == template_caps) {
111     GST_INFO_OBJECT (thiz,
112         "downstream has ANY caps, profile/level set to auto");
113   } else if (allowed_caps) {
114     GstStructure *s;
115     const gchar *profile;
116
117     if (gst_caps_is_empty (allowed_caps)) {
118       gst_caps_unref (allowed_caps);
119       gst_caps_unref (template_caps);
120       return FALSE;
121     }
122
123     allowed_caps = gst_caps_make_writable (allowed_caps);
124     allowed_caps = gst_caps_fixate (allowed_caps);
125     s = gst_caps_get_structure (allowed_caps, 0);
126     profile = gst_structure_get_string (s, "profile");
127
128     if (profile) {
129       if (!strcmp (profile, "3")) {
130         thiz->profile = MFX_PROFILE_VP9_3;
131       } else if (!strcmp (profile, "2")) {
132         thiz->profile = MFX_PROFILE_VP9_2;
133       } else if (!strcmp (profile, "1")) {
134         thiz->profile = MFX_PROFILE_VP9_1;
135       } else if (!strcmp (profile, "0")) {
136         thiz->profile = MFX_PROFILE_VP9_0;
137       } else {
138         g_assert_not_reached ();
139       }
140     }
141
142     gst_caps_unref (allowed_caps);
143   }
144
145   gst_caps_unref (template_caps);
146
147   return TRUE;
148 }
149
150 static gboolean
151 gst_msdkvp9enc_configure (GstMsdkEnc * encoder)
152 {
153   GstMsdkVP9Enc *vp9enc = GST_MSDKVP9ENC (encoder);
154   mfxSession session;
155
156   if (encoder->hardware) {
157     session = gst_msdk_context_get_session (encoder->context);
158
159     if (!gst_msdk_load_plugin (session, &MFX_PLUGINID_VP9E_HW, 1, "msdkvp9enc"))
160       return FALSE;
161   }
162
163   encoder->num_extra_frames = encoder->async_depth - 1;
164   encoder->param.mfx.CodecId = MFX_CODEC_VP9;
165   encoder->param.mfx.CodecLevel = 0;
166
167   switch (encoder->param.mfx.FrameInfo.FourCC) {
168 #if (MFX_VERSION >= 1027)
169     case MFX_FOURCC_Y410:
170       encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_3;
171       break;
172 #endif
173
174     case MFX_FOURCC_P010:
175       encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_2;
176       break;
177
178     case MFX_FOURCC_AYUV:
179       encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_1;
180       break;
181
182     default:
183       encoder->param.mfx.CodecProfile = MFX_PROFILE_VP9_0;
184       break;
185   }
186
187   /* As the frame width and height is rounded up to 128 and 32 since commit 8daac1c,
188    * so the width, height for initialization should be rounded up to 128 and 32
189    * too because VP9 encoder in MSDK will do some check on width and height.
190    */
191   encoder->param.mfx.FrameInfo.Width =
192       GST_ROUND_UP_128 (encoder->param.mfx.FrameInfo.CropW);
193   encoder->param.mfx.FrameInfo.Height =
194       GST_ROUND_UP_32 (encoder->param.mfx.FrameInfo.CropH);
195
196   /* Always turn on this flag for VP9 */
197   encoder->param.mfx.LowPower = MFX_CODINGOPTION_ON;
198
199   /* Enable Extended coding options */
200   gst_msdkenc_ensure_extended_coding_options (encoder);
201
202   memset (&vp9enc->ext_vp9, 0, sizeof (vp9enc->ext_vp9));
203   vp9enc->ext_vp9.Header.BufferId = MFX_EXTBUFF_VP9_PARAM;
204   vp9enc->ext_vp9.Header.BufferSz = sizeof (vp9enc->ext_vp9);
205   vp9enc->ext_vp9.WriteIVFHeaders = MFX_CODINGOPTION_OFF;
206
207   gst_msdkenc_add_extra_param (encoder, (mfxExtBuffer *) & vp9enc->ext_vp9);
208
209   return TRUE;
210 }
211
212 static inline const gchar *
213 profile_to_string (gint profile)
214 {
215   switch (profile) {
216     case MFX_PROFILE_VP9_3:
217       return "3";
218     case MFX_PROFILE_VP9_2:
219       return "2";
220     case MFX_PROFILE_VP9_1:
221       return "1";
222     case MFX_PROFILE_VP9_0:
223       return "0";
224     default:
225       break;
226   }
227
228   return NULL;
229 }
230
231 static GstCaps *
232 gst_msdkvp9enc_set_src_caps (GstMsdkEnc * encoder)
233 {
234   GstCaps *caps;
235   GstStructure *structure;
236   const gchar *profile;
237
238   caps = gst_caps_new_empty_simple ("video/x-vp9");
239   structure = gst_caps_get_structure (caps, 0);
240
241   profile = profile_to_string (encoder->param.mfx.CodecProfile);
242   if (profile)
243     gst_structure_set (structure, "profile", G_TYPE_STRING, profile, NULL);
244
245   return caps;
246 }
247
248 static void
249 gst_msdkvp9enc_set_property (GObject * object, guint prop_id,
250     const GValue * value, GParamSpec * pspec)
251 {
252   GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
253
254   if (!gst_msdkenc_set_common_property (object, prop_id, value, pspec))
255     GST_WARNING_OBJECT (thiz, "Failed to set common encode property");
256 }
257
258 static void
259 gst_msdkvp9enc_get_property (GObject * object, guint prop_id, GValue * value,
260     GParamSpec * pspec)
261 {
262   GstMsdkVP9Enc *thiz = GST_MSDKVP9ENC (object);
263
264   if (!gst_msdkenc_get_common_property (object, prop_id, value, pspec))
265     GST_WARNING_OBJECT (thiz, "Failed to get common encode property");
266 }
267
268 static void
269 gst_msdkvp9enc_class_init (GstMsdkVP9EncClass * klass)
270 {
271   GObjectClass *gobject_class;
272   GstElementClass *element_class;
273   GstMsdkEncClass *encoder_class;
274
275   gobject_class = G_OBJECT_CLASS (klass);
276   element_class = GST_ELEMENT_CLASS (klass);
277   encoder_class = GST_MSDKENC_CLASS (klass);
278
279   gobject_class->set_property = gst_msdkvp9enc_set_property;
280   gobject_class->get_property = gst_msdkvp9enc_get_property;
281
282   encoder_class->set_format = gst_msdkvp9enc_set_format;
283   encoder_class->configure = gst_msdkvp9enc_configure;
284   encoder_class->set_src_caps = gst_msdkvp9enc_set_src_caps;
285   encoder_class->qp_max = 255;
286   encoder_class->qp_min = 0;
287
288   gst_msdkenc_install_common_properties (encoder_class);
289
290   gst_element_class_set_static_metadata (element_class,
291       "Intel MSDK VP9 encoder",
292       "Codec/Encoder/Video/Hardware",
293       "VP9 video encoder based on " MFX_API_SDK,
294       "Haihao Xiang <haihao.xiang@intel.com>");
295
296   gst_element_class_add_static_pad_template (element_class, &sink_factory);
297   gst_element_class_add_static_pad_template (element_class, &src_factory);
298 }
299
300 static void
301 gst_msdkvp9enc_init (GstMsdkVP9Enc * thiz)
302 {
303 }