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