1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2018, Intel Corporation
3  *
4  * Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  *    this list of conditions and the following disclaimer in the documentation
16  *    and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifdef HAVE_CONFIG_H
36 #  include <config.h>
37 #endif
38 
39 #include "gstmsdkmpeg2dec.h"
40 
41 GST_DEBUG_CATEGORY_EXTERN (gst_msdkmpeg2dec_debug);
42 #define GST_CAT_DEFAULT gst_msdkmpeg2dec_debug
43 
44 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("video/mpeg, "
48         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
49         "mpegversion = (int) 2, " "systemstream = (boolean) false")
50     );
51 
52 #define gst_msdkmpeg2dec_parent_class parent_class
53 G_DEFINE_TYPE (GstMsdkMPEG2Dec, gst_msdkmpeg2dec, GST_TYPE_MSDKDEC);
54 
55 static gboolean
gst_msdkmpeg2dec_configure(GstMsdkDec * decoder)56 gst_msdkmpeg2dec_configure (GstMsdkDec * decoder)
57 {
58   GstMsdkMPEG2Dec *mpeg2dec = GST_MSDKMPEG2DEC (decoder);
59   decoder->param.mfx.CodecId = MFX_CODEC_MPEG2;
60 
61   /* This is a deprecated attribute in msdk-2017 version, but some
62    * customers still using this for low-latency streaming of non-b-frame
63    * encoded streams */
64   decoder->param.mfx.DecodedOrder = mpeg2dec->output_order;
65   return TRUE;
66 }
67 
68 static void
gst_msdkdec_mpeg2_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)69 gst_msdkdec_mpeg2_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec)
71 {
72   GstMsdkMPEG2Dec *thiz = GST_MSDKMPEG2DEC (object);
73   GstState state;
74 
75   GST_OBJECT_LOCK (thiz);
76   state = GST_STATE (thiz);
77 
78   if (!gst_msdkdec_prop_check_state (state, pspec)) {
79     GST_WARNING_OBJECT (thiz, "setting property in wrong state");
80     GST_OBJECT_UNLOCK (thiz);
81     return;
82   }
83   switch (prop_id) {
84     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
85       thiz->output_order = g_value_get_enum (value);
86       break;
87     default:
88       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89       break;
90   }
91   GST_OBJECT_UNLOCK (thiz);
92   return;
93 }
94 
95 static void
gst_msdkdec_mpeg2_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)96 gst_msdkdec_mpeg2_get_property (GObject * object, guint prop_id, GValue * value,
97     GParamSpec * pspec)
98 {
99   GstMsdkMPEG2Dec *thiz = GST_MSDKMPEG2DEC (object);
100 
101   GST_OBJECT_LOCK (thiz);
102   switch (prop_id) {
103     case GST_MSDKDEC_PROP_OUTPUT_ORDER:
104       g_value_set_enum (value, thiz->output_order);
105       break;
106     default:
107       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108       break;
109   }
110   GST_OBJECT_UNLOCK (thiz);
111 }
112 
113 static void
gst_msdkmpeg2dec_class_init(GstMsdkMPEG2DecClass * klass)114 gst_msdkmpeg2dec_class_init (GstMsdkMPEG2DecClass * klass)
115 {
116   GObjectClass *gobject_class;
117   GstElementClass *element_class;
118   GstMsdkDecClass *decoder_class;
119 
120   gobject_class = G_OBJECT_CLASS (klass);
121   element_class = GST_ELEMENT_CLASS (klass);
122   decoder_class = GST_MSDKDEC_CLASS (klass);
123 
124   gobject_class->set_property = gst_msdkdec_mpeg2_set_property;
125   gobject_class->get_property = gst_msdkdec_mpeg2_get_property;
126 
127   decoder_class->configure = GST_DEBUG_FUNCPTR (gst_msdkmpeg2dec_configure);
128 
129   gst_element_class_set_static_metadata (element_class,
130       "Intel MSDK MPEG2 decoder",
131       "Codec/Decoder/Video/Hardware",
132       "MPEG2 video decoder based on Intel Media SDK",
133       "Sreerenj Balachandran <sreerenj.balachandran@intel.com>");
134 
135   gst_msdkdec_prop_install_output_oder_property (gobject_class);
136 
137   gst_element_class_add_static_pad_template (element_class, &sink_factory);
138 }
139 
140 static void
gst_msdkmpeg2dec_init(GstMsdkMPEG2Dec * thiz)141 gst_msdkmpeg2dec_init (GstMsdkMPEG2Dec * thiz)
142 {
143   thiz->output_order = PROP_OUTPUT_ORDER_DEFAULT;
144 }
145