1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2016, Intel Corporation
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 NEGLIGDECE
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 #include <mfxplugin.h>
37 
38 #include "gstmsdkh265dec.h"
39 #include "gstmsdkvideomemory.h"
40 
41 GST_DEBUG_CATEGORY_EXTERN (gst_msdkh265dec_debug);
42 #define GST_CAT_DEFAULT gst_msdkh265dec_debug
43 
44 /* TODO: update both sink and src dynamically */
45 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("video/x-h265, "
49         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
50         "stream-format = (string) byte-stream , alignment = (string) au , "
51         "profile = (string) { main, main-10 } ")
52     );
53 
54 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("video/x-raw, "
58         "format = (string) { NV12, P010_10LE }, "
59         "framerate = (fraction) [0, MAX], "
60         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ],"
61         "interlace-mode = (string) progressive;"
62         GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF,
63             "{ NV12, P010_10LE }") ";")
64     );
65 
66 #define gst_msdkh265dec_parent_class parent_class
67 G_DEFINE_TYPE (GstMsdkH265Dec, gst_msdkh265dec, GST_TYPE_MSDKDEC);
68 
69 static gboolean
gst_msdkh265dec_configure(GstMsdkDec * decoder)70 gst_msdkh265dec_configure (GstMsdkDec * decoder)
71 {
72   GstMsdkH265Dec *h265dec = GST_MSDKH265DEC (decoder);
73   mfxSession session;
74   mfxStatus status;
75   const mfxPluginUID *uid;
76 
77   session = gst_msdk_context_get_session (decoder->context);
78 
79   if (decoder->hardware)
80     uid = &MFX_PLUGINID_HEVCD_HW;
81   else
82     uid = &MFX_PLUGINID_HEVCD_SW;
83 
84   status = MFXVideoUSER_Load (session, uid, 1);
85   if (status < MFX_ERR_NONE) {
86     GST_ERROR_OBJECT (h265dec, "Media SDK Plugin load failed (%s)",
87         msdk_status_to_string (status));
88     return FALSE;
89   } else if (status > MFX_ERR_NONE) {
90     GST_WARNING_OBJECT (h265dec, "Media SDK Plugin load warning: %s",
91         msdk_status_to_string (status));
92   }
93 
94   decoder->param.mfx.CodecId = MFX_CODEC_HEVC;
95 
96   /* This is a deprecated attribute in msdk-2017 version, but some
97    * customers still using this for low-latency streaming of non-b-frame
98    * encoded streams */
99   decoder->param.mfx.DecodedOrder = h265dec->output_order;
100   return TRUE;
101 }
102 
103 static void
gst_msdkdec_h265_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)104 gst_msdkdec_h265_set_property (GObject * object, guint prop_id,
105     const GValue * value, GParamSpec * pspec)
106 {
107   GstMsdkH265Dec *thiz = GST_MSDKH265DEC (object);
108   GstState state;
109 
110   GST_OBJECT_LOCK (thiz);
111   state = GST_STATE (thiz);
112 
113   if (!gst_msdkdec_prop_check_state (state, pspec)) {
114     GST_WARNING_OBJECT (thiz, "setting property in wrong state");
115     GST_OBJECT_UNLOCK (thiz);
116     return;
117   }
118   switch (prop_id) {
119     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
120       thiz->output_order = g_value_get_enum (value);
121       break;
122     default:
123       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124       break;
125   }
126   GST_OBJECT_UNLOCK (thiz);
127   return;
128 }
129 
130 static void
gst_msdkdec_h265_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)131 gst_msdkdec_h265_get_property (GObject * object, guint prop_id, GValue * value,
132     GParamSpec * pspec)
133 {
134   GstMsdkH265Dec *thiz = GST_MSDKH265DEC (object);
135 
136   GST_OBJECT_LOCK (thiz);
137   switch (prop_id) {
138     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
139       g_value_set_enum (value, thiz->output_order);
140       break;
141     default:
142       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143       break;
144   }
145   GST_OBJECT_UNLOCK (thiz);
146 }
147 
148 static void
gst_msdkh265dec_class_init(GstMsdkH265DecClass * klass)149 gst_msdkh265dec_class_init (GstMsdkH265DecClass * klass)
150 {
151   GObjectClass *gobject_class;
152   GstElementClass *element_class;
153   GstMsdkDecClass *decoder_class;
154 
155   gobject_class = G_OBJECT_CLASS (klass);
156   element_class = GST_ELEMENT_CLASS (klass);
157   decoder_class = GST_MSDKDEC_CLASS (klass);
158 
159   gobject_class->set_property = gst_msdkdec_h265_set_property;
160   gobject_class->get_property = gst_msdkdec_h265_get_property;
161 
162   decoder_class->configure = GST_DEBUG_FUNCPTR (gst_msdkh265dec_configure);
163 
164   gst_element_class_set_static_metadata (element_class,
165       "Intel MSDK H265 decoder",
166       "Codec/Decoder/Video/Hardware",
167       "H265 video decoder based on Intel Media SDK",
168       "Scott D Phillips <scott.d.phillips@intel.com>");
169 
170   gst_msdkdec_prop_install_output_oder_property (gobject_class);
171 
172   gst_element_class_add_static_pad_template (element_class, &sink_factory);
173   gst_element_class_add_static_pad_template (element_class, &src_factory);
174 }
175 
176 static void
gst_msdkh265dec_init(GstMsdkH265Dec * thiz)177 gst_msdkh265dec_init (GstMsdkH265Dec * thiz)
178 {
179   thiz->output_order = PROP_OUTPUT_ORDER_DEFAULT;
180 }
181