1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2018, Intel Corporation
3  * All rights reserved.
4  *
5  * 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 /* sample pipeline: gst-launch-1.0 filesrc location=video.wmv ! asfdemux ! vc1parse !  msdkvc1dec ! videoconvert ! xvimagesink */
35 
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39 
40 #include "gstmsdkvc1dec.h"
41 
42 GST_DEBUG_CATEGORY_EXTERN (gst_msdkvc1dec_debug);
43 #define GST_CAT_DEFAULT gst_msdkvc1dec_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-wmv, "
49         "framerate = (fraction) [0/1, MAX], "
50         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
51         "wmvversion= (int) 3, "
52         "format= (string) WMV3, "
53         "header-format= (string) none, "
54         "stream-format= (string) sequence-layer-frame-layer, "
55         "profile = (string) {simple, main}" ";"
56         "video/x-wmv, "
57         "framerate = (fraction) [0/1, MAX], "
58         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
59         "wmvversion= (int) 3, "
60         "format= (string) WVC1, "
61         "header-format= (string) asf, "
62         "stream-format= (string) bdu, " "profile = (string) advanced" ";")
63     );
64 
65 #define gst_msdkvc1dec_parent_class parent_class
66 G_DEFINE_TYPE (GstMsdkVC1Dec, gst_msdkvc1dec, GST_TYPE_MSDKDEC);
67 
68 static gboolean
gst_msdkvc1dec_configure(GstMsdkDec * decoder)69 gst_msdkvc1dec_configure (GstMsdkDec * decoder)
70 {
71   GstMsdkVC1Dec *vc1dec = GST_MSDKVC1DEC (decoder);
72   GstBuffer *buffer;
73   GstCaps *caps;
74   GstStructure *structure;
75   const gchar *profile_str;
76 
77   caps = decoder->input_state->caps;
78   if (!caps)
79     return FALSE;
80 
81   structure = gst_caps_get_structure (caps, 0);
82   if (!structure)
83     return FALSE;
84 
85   decoder->param.mfx.CodecId = MFX_CODEC_VC1;
86 
87   profile_str = gst_structure_get_string (structure, "profile");
88 
89   if (!strcmp (profile_str, "simple"))
90     decoder->param.mfx.CodecProfile = MFX_PROFILE_VC1_SIMPLE;
91   else if (!strcmp (profile_str, "main"))
92     decoder->param.mfx.CodecProfile = MFX_PROFILE_VC1_MAIN;
93   else {
94     decoder->param.mfx.CodecProfile = MFX_PROFILE_VC1_ADVANCED;
95     /* asf advanced profile codec-data has 1 byte in the begining
96      * which is the ASF binding byte. MediaSDK can't recognize this
97      * byte, so discard it */
98     if (decoder->input_state->codec_data) {
99       buffer = gst_buffer_copy_region (decoder->input_state->codec_data,
100           GST_BUFFER_COPY_DEEP | GST_BUFFER_COPY_MEMORY, 1,
101           gst_buffer_get_size (decoder->input_state->codec_data) - 1);
102       gst_adapter_push (decoder->adapter, buffer);
103     }
104 
105     decoder->is_packetized = FALSE;
106   }
107 
108   /* This is a deprecated attribute in msdk-2017 version, but some
109    * customers still using this for low-latency streaming of non-b-frame
110    * encoded streams */
111   decoder->param.mfx.DecodedOrder = vc1dec->output_order;
112   return TRUE;
113 
114   return TRUE;
115 }
116 
117 static void
gst_msdkdec_vc1_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)118 gst_msdkdec_vc1_set_property (GObject * object, guint prop_id,
119     const GValue * value, GParamSpec * pspec)
120 {
121   GstMsdkVC1Dec *thiz = GST_MSDKVC1DEC (object);
122   GstState state;
123 
124   GST_OBJECT_LOCK (thiz);
125   state = GST_STATE (thiz);
126 
127   if (!gst_msdkdec_prop_check_state (state, pspec)) {
128     GST_WARNING_OBJECT (thiz, "setting property in wrong state");
129     GST_OBJECT_UNLOCK (thiz);
130     return;
131   }
132   switch (prop_id) {
133     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
134       thiz->output_order = g_value_get_enum (value);
135       break;
136     default:
137       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138       break;
139   }
140   GST_OBJECT_UNLOCK (thiz);
141   return;
142 }
143 
144 static void
gst_msdkdec_vc1_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)145 gst_msdkdec_vc1_get_property (GObject * object, guint prop_id, GValue * value,
146     GParamSpec * pspec)
147 {
148   GstMsdkVC1Dec *thiz = GST_MSDKVC1DEC (object);
149 
150   GST_OBJECT_LOCK (thiz);
151   switch (prop_id) {
152     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
153       g_value_set_enum (value, thiz->output_order);
154       break;
155     default:
156       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157       break;
158   }
159   GST_OBJECT_UNLOCK (thiz);
160 }
161 
162 static gboolean
gst_msdkvc1dec_preinit_decoder(GstMsdkDec * decoder)163 gst_msdkvc1dec_preinit_decoder (GstMsdkDec * decoder)
164 {
165   decoder->param.mfx.FrameInfo.Width =
166       GST_ROUND_UP_16 (decoder->param.mfx.FrameInfo.Width);
167   decoder->param.mfx.FrameInfo.Height =
168       GST_ROUND_UP_32 (decoder->param.mfx.FrameInfo.Height);
169 
170   return TRUE;
171 }
172 
173 static void
gst_msdkvc1dec_class_init(GstMsdkVC1DecClass * klass)174 gst_msdkvc1dec_class_init (GstMsdkVC1DecClass * klass)
175 {
176   GObjectClass *gobject_class;
177   GstElementClass *element_class;
178   GstMsdkDecClass *decoder_class;
179 
180   gobject_class = G_OBJECT_CLASS (klass);
181   element_class = GST_ELEMENT_CLASS (klass);
182   decoder_class = GST_MSDKDEC_CLASS (klass);
183 
184   gobject_class->set_property = gst_msdkdec_vc1_set_property;
185   gobject_class->get_property = gst_msdkdec_vc1_get_property;
186 
187   decoder_class->configure = GST_DEBUG_FUNCPTR (gst_msdkvc1dec_configure);
188   decoder_class->preinit_decoder =
189       GST_DEBUG_FUNCPTR (gst_msdkvc1dec_preinit_decoder);
190 
191   gst_element_class_set_static_metadata (element_class,
192       "Intel MSDK VC1 decoder",
193       "Codec/Decoder/Video/Hardware",
194       "VC1/WMV video decoder based on Intel Media SDK",
195       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
196 
197   gst_msdkdec_prop_install_output_oder_property (gobject_class);
198 
199   gst_element_class_add_static_pad_template (element_class, &sink_factory);
200 }
201 
202 static void
gst_msdkvc1dec_init(GstMsdkVC1Dec * thiz)203 gst_msdkvc1dec_init (GstMsdkVC1Dec * thiz)
204 {
205   thiz->output_order = PROP_OUTPUT_ORDER_DEFAULT;
206 }
207