msdkh265enc: add P010_10LE to the sink pad template
[platform/upstream/gstreamer.git] / sys / msdk / gstmsdkh265enc.c
1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2016, Oblong Industries, Inc.
3  * 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 #ifdef HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #ifdef HAVE_MFX_MFXDEFS_H
37 #  include <mfx/mfxplugin.h>
38 #else
39 #  include "mfxplugin.h"
40 #endif
41
42 #include <gst/allocators/gstdmabuf.h>
43
44 #include "gstmsdkh265enc.h"
45
46 GST_DEBUG_CATEGORY_EXTERN (gst_msdkh265enc_debug);
47 #define GST_CAT_DEFAULT gst_msdkh265enc_debug
48
49 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("video/x-raw, "
53         "format = (string) { NV12, I420, YV12, YUY2, UYVY, BGRA, P010_10LE }, "
54         "framerate = (fraction) [0, MAX], "
55         "width = (int) [ 16, MAX ], height = (int) [ 16, MAX ],"
56         "interlace-mode = (string) progressive" ";"
57         GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF,
58             "{ NV12 }")));
59
60 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("video/x-h265, "
64         "framerate = (fraction) [0/1, MAX], "
65         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
66         "stream-format = (string) byte-stream , alignment = (string) au , "
67         "profile = (string) main")
68     );
69
70 #define gst_msdkh265enc_parent_class parent_class
71 G_DEFINE_TYPE (GstMsdkH265Enc, gst_msdkh265enc, GST_TYPE_MSDKENC);
72
73 static gboolean
74 gst_msdkh265enc_set_format (GstMsdkEnc * encoder)
75 {
76   return TRUE;
77 }
78
79 static gboolean
80 gst_msdkh265enc_configure (GstMsdkEnc * encoder)
81 {
82   GstMsdkH265Enc *h265enc = GST_MSDKH265ENC (encoder);
83   mfxSession session;
84   mfxStatus status;
85   const mfxPluginUID *uid;
86
87   session = gst_msdk_context_get_session (encoder->context);
88
89   if (encoder->hardware)
90     uid = &MFX_PLUGINID_HEVCE_HW;
91   else
92     uid = &MFX_PLUGINID_HEVCE_SW;
93
94   status = MFXVideoUSER_Load (session, uid, 1);
95   if (status < MFX_ERR_NONE) {
96     GST_ERROR_OBJECT (h265enc, "Media SDK Plugin load failed (%s)",
97         msdk_status_to_string (status));
98     return FALSE;
99   } else if (status > MFX_ERR_NONE) {
100     GST_WARNING_OBJECT (h265enc, "Media SDK Plugin load warning: %s",
101         msdk_status_to_string (status));
102   }
103
104   encoder->param.mfx.CodecId = MFX_CODEC_HEVC;
105   encoder->param.mfx.CodecProfile = MFX_PROFILE_HEVC_MAIN;
106
107   /* IdrInterval field of MediaSDK HEVC encoder behaves differently
108    * than other encoders. IdrInteval == 1 indicate every
109    * I-frame should be an IDR, IdrInteval == 2 means every other
110    * I-frame is an IDR etc. So we generalize the behaviour of property
111    * "i-frames" by incrementing the value by one in each case*/
112   encoder->param.mfx.IdrInterval += 1;
113
114   /* Enable Extended coding options */
115   gst_msdkenc_ensure_extended_coding_options (encoder);
116
117   return TRUE;
118 }
119
120 static inline const gchar *
121 level_to_string (gint level)
122 {
123   switch (level) {
124     case MFX_LEVEL_HEVC_1:
125       return "1";
126     case MFX_LEVEL_HEVC_2:
127       return "2";
128     case MFX_LEVEL_HEVC_21:
129       return "2.1";
130     case MFX_LEVEL_HEVC_3:
131       return "3";
132     case MFX_LEVEL_HEVC_31:
133       return "3.1";
134     case MFX_LEVEL_HEVC_4:
135       return "4";
136     case MFX_LEVEL_HEVC_41:
137       return "4.1";
138     case MFX_LEVEL_HEVC_5:
139       return "5";
140     case MFX_LEVEL_HEVC_51:
141       return "5.1";
142     case MFX_LEVEL_HEVC_52:
143       return "5.2";
144     case MFX_LEVEL_HEVC_6:
145       return "6";
146     case MFX_LEVEL_HEVC_61:
147       return "6.1";
148     case MFX_LEVEL_HEVC_62:
149       return "6.2";
150     default:
151       break;
152   }
153
154   return NULL;
155 }
156
157 static GstCaps *
158 gst_msdkh265enc_set_src_caps (GstMsdkEnc * encoder)
159 {
160   GstCaps *caps;
161   GstStructure *structure;
162   const gchar *level;
163
164   caps = gst_caps_new_empty_simple ("video/x-h265");
165   structure = gst_caps_get_structure (caps, 0);
166
167   gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
168       NULL);
169
170   gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);
171
172   gst_structure_set (structure, "profile", G_TYPE_STRING, "main", NULL);
173
174   level = level_to_string (encoder->param.mfx.CodecLevel);
175   if (level)
176     gst_structure_set (structure, "level", G_TYPE_STRING, level, NULL);
177
178   return caps;
179 }
180
181 static void
182 gst_msdkh265enc_set_property (GObject * object, guint prop_id,
183     const GValue * value, GParamSpec * pspec)
184 {
185   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
186
187   if (!gst_msdkenc_set_common_property (object, prop_id, value, pspec))
188     GST_WARNING_OBJECT (thiz, "Failed to set common encode property");
189 }
190
191 static void
192 gst_msdkh265enc_get_property (GObject * object, guint prop_id, GValue * value,
193     GParamSpec * pspec)
194 {
195   GstMsdkH265Enc *thiz = GST_MSDKH265ENC (object);
196
197   if (!gst_msdkenc_get_common_property (object, prop_id, value, pspec))
198     GST_WARNING_OBJECT (thiz, "Failed to get common encode property");
199 }
200
201 static void
202 gst_msdkh265enc_class_init (GstMsdkH265EncClass * klass)
203 {
204   GObjectClass *gobject_class;
205   GstElementClass *element_class;
206   GstMsdkEncClass *encoder_class;
207
208   gobject_class = G_OBJECT_CLASS (klass);
209   element_class = GST_ELEMENT_CLASS (klass);
210   encoder_class = GST_MSDKENC_CLASS (klass);
211
212   gobject_class->set_property = gst_msdkh265enc_set_property;
213   gobject_class->get_property = gst_msdkh265enc_get_property;
214
215   encoder_class->set_format = gst_msdkh265enc_set_format;
216   encoder_class->configure = gst_msdkh265enc_configure;
217   encoder_class->set_src_caps = gst_msdkh265enc_set_src_caps;
218
219   gst_msdkenc_install_common_properties (encoder_class);
220
221   gst_element_class_set_static_metadata (element_class,
222       "Intel MSDK H265 encoder",
223       "Codec/Encoder/Video",
224       "H265 video encoder based on Intel Media SDK",
225       "Josep Torra <jtorra@oblong.com>");
226
227   gst_element_class_add_static_pad_template (element_class, &sink_factory);
228   gst_element_class_add_static_pad_template (element_class, &src_factory);
229 }
230
231 static void
232 gst_msdkh265enc_init (GstMsdkH265Enc * thiz)
233 {
234 }