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