1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2018, Intel Corporation
3  * All rights reserved.
4  *
5  * Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
30  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #  include <config.h>
36 #endif
37 
38 #include <mfxplugin.h>
39 #include <mfxvp9.h>
40 
41 #include "gstmsdkvp9dec.h"
42 #include "gstmsdkvideomemory.h"
43 
44 GST_DEBUG_CATEGORY_EXTERN (gst_msdkvp9dec_debug);
45 #define GST_CAT_DEFAULT gst_msdkvp9dec_debug
46 
47 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
48     GST_PAD_SINK,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("video/x-vp9")
51     );
52 
53 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
54     GST_PAD_SRC,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("video/x-raw, "
57         "format = (string) { NV12, P010_10LE }, "
58         "framerate = (fraction) [0, MAX], "
59         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ],"
60         "interlace-mode = (string) progressive;"
61         GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_DMABUF,
62             "{ NV12, P010_10LE }") ";")
63     );
64 
65 #define gst_msdkvp9dec_parent_class parent_class
66 G_DEFINE_TYPE (GstMsdkVP9Dec, gst_msdkvp9dec, GST_TYPE_MSDKDEC);
67 
68 static gboolean
gst_msdkvp9dec_configure(GstMsdkDec * decoder)69 gst_msdkvp9dec_configure (GstMsdkDec * decoder)
70 {
71   GstMsdkVP9Dec *vp9dec = GST_MSDKVP9DEC (decoder);
72   mfxSession session;
73   mfxStatus status;
74   const mfxPluginUID *uid;
75 
76   session = gst_msdk_context_get_session (decoder->context);
77 
78   uid = &MFX_PLUGINID_VP9D_HW;
79 
80   status = MFXVideoUSER_Load (session, uid, 1);
81   if (status < MFX_ERR_NONE) {
82     GST_ERROR_OBJECT (vp9dec, "Media SDK Plugin load failed (%s)",
83         msdk_status_to_string (status));
84     return FALSE;
85   } else if (status > MFX_ERR_NONE) {
86     GST_WARNING_OBJECT (vp9dec, "Media SDK Plugin load warning: %s",
87         msdk_status_to_string (status));
88   }
89 
90   decoder->param.mfx.CodecId = MFX_CODEC_VP9;
91   /* Replaced with width and height rounded up to 16 */
92   decoder->param.mfx.FrameInfo.Width =
93       GST_ROUND_UP_16 (decoder->param.mfx.FrameInfo.CropW);
94   decoder->param.mfx.FrameInfo.Height =
95       GST_ROUND_UP_16 (decoder->param.mfx.FrameInfo.CropH);
96 
97   decoder->force_reset_on_res_change = FALSE;
98 
99   /* This is a deprecated attribute in msdk-2017 version, but some
100    * customers still using this for low-latency streaming of non-b-frame
101    * encoded streams */
102   decoder->param.mfx.DecodedOrder = vp9dec->output_order;
103   return TRUE;
104 }
105 
106 static void
gst_msdkdec_vp9_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)107 gst_msdkdec_vp9_set_property (GObject * object, guint prop_id,
108     const GValue * value, GParamSpec * pspec)
109 {
110   GstMsdkVP9Dec *thiz = GST_MSDKVP9DEC (object);
111   GstState state;
112 
113   GST_OBJECT_LOCK (thiz);
114   state = GST_STATE (thiz);
115 
116   if (!gst_msdkdec_prop_check_state (state, pspec)) {
117     GST_WARNING_OBJECT (thiz, "setting property in wrong state");
118     GST_OBJECT_UNLOCK (thiz);
119     return;
120   }
121   switch (prop_id) {
122     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
123       thiz->output_order = g_value_get_enum (value);
124       break;
125     default:
126       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127       break;
128   }
129   GST_OBJECT_UNLOCK (thiz);
130   return;
131 }
132 
133 static void
gst_msdkdec_vp9_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)134 gst_msdkdec_vp9_get_property (GObject * object, guint prop_id, GValue * value,
135     GParamSpec * pspec)
136 {
137   GstMsdkVP9Dec *thiz = GST_MSDKVP9DEC (object);
138 
139   GST_OBJECT_LOCK (thiz);
140   switch (prop_id) {
141     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
142       g_value_set_enum (value, thiz->output_order);
143       break;
144     default:
145       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146       break;
147   }
148   GST_OBJECT_UNLOCK (thiz);
149 }
150 
151 static gboolean
gst_msdkvp9dec_preinit_decoder(GstMsdkDec * decoder)152 gst_msdkvp9dec_preinit_decoder (GstMsdkDec * decoder)
153 {
154   decoder->param.mfx.FrameInfo.Width =
155       GST_ROUND_UP_16 (decoder->param.mfx.FrameInfo.Width);
156   decoder->param.mfx.FrameInfo.Height =
157       GST_ROUND_UP_16 (decoder->param.mfx.FrameInfo.Height);
158 
159   decoder->param.mfx.FrameInfo.PicStruct =
160       decoder->param.mfx.FrameInfo.PicStruct ? decoder->param.mfx.
161       FrameInfo.PicStruct : MFX_PICSTRUCT_PROGRESSIVE;
162 
163   return TRUE;
164 }
165 
166 static void
gst_msdkvp9dec_class_init(GstMsdkVP9DecClass * klass)167 gst_msdkvp9dec_class_init (GstMsdkVP9DecClass * klass)
168 {
169   GObjectClass *gobject_class;
170   GstElementClass *element_class;
171   GstMsdkDecClass *decoder_class;
172 
173   gobject_class = G_OBJECT_CLASS (klass);
174   element_class = GST_ELEMENT_CLASS (klass);
175   decoder_class = GST_MSDKDEC_CLASS (klass);
176 
177   gobject_class->set_property = gst_msdkdec_vp9_set_property;
178   gobject_class->get_property = gst_msdkdec_vp9_get_property;
179 
180   decoder_class->configure = GST_DEBUG_FUNCPTR (gst_msdkvp9dec_configure);
181   decoder_class->preinit_decoder =
182       GST_DEBUG_FUNCPTR (gst_msdkvp9dec_preinit_decoder);
183 
184   gst_element_class_set_static_metadata (element_class,
185       "Intel MSDK VP9 decoder",
186       "Codec/Decoder/Video/Hardware",
187       "VP9 video decoder based on Intel Media SDK",
188       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
189 
190   gst_msdkdec_prop_install_output_oder_property (gobject_class);
191 
192   gst_element_class_add_static_pad_template (element_class, &sink_factory);
193   gst_element_class_add_static_pad_template (element_class, &src_factory);
194 }
195 
196 static void
gst_msdkvp9dec_init(GstMsdkVP9Dec * thiz)197 gst_msdkvp9dec_init (GstMsdkVP9Dec * thiz)
198 {
199   thiz->output_order = PROP_OUTPUT_ORDER_DEFAULT;
200 }
201