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