1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2016, Oblong Industries, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 # include <config.h>
34 #endif
35
36 #include "gstmsdkmpeg2enc.h"
37
38 GST_DEBUG_CATEGORY_EXTERN (gst_msdkmpeg2enc_debug);
39 #define GST_CAT_DEFAULT gst_msdkmpeg2enc_debug
40
41 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
42 GST_PAD_SRC,
43 GST_PAD_ALWAYS,
44 GST_STATIC_CAPS ("video/mpeg, "
45 "framerate = (fraction) [0/1, MAX], "
46 "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
47 "mpegversion = (int) 2 , systemstream = (bool) false, "
48 "profile = (string) { high, main, simple }")
49 );
50
51 #define gst_msdkmpeg2enc_parent_class parent_class
52 G_DEFINE_TYPE (GstMsdkMPEG2Enc, gst_msdkmpeg2enc, GST_TYPE_MSDKENC);
53
54 static gboolean
gst_msdkmpeg2enc_set_format(GstMsdkEnc * encoder)55 gst_msdkmpeg2enc_set_format (GstMsdkEnc * encoder)
56 {
57 GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (encoder);
58 GstCaps *template_caps;
59 GstCaps *allowed_caps = NULL;
60
61 thiz->profile = 0;
62
63 template_caps = gst_static_pad_template_get_caps (&src_factory);
64 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
65
66 /* If downstream has ANY caps let encoder decide profile and level */
67 if (allowed_caps == template_caps) {
68 GST_INFO_OBJECT (thiz,
69 "downstream has ANY caps, profile/level set to auto");
70 } else if (allowed_caps) {
71 GstStructure *s;
72 const gchar *profile;
73
74 if (gst_caps_is_empty (allowed_caps)) {
75 gst_caps_unref (allowed_caps);
76 gst_caps_unref (template_caps);
77 return FALSE;
78 }
79
80 allowed_caps = gst_caps_make_writable (allowed_caps);
81 allowed_caps = gst_caps_fixate (allowed_caps);
82 s = gst_caps_get_structure (allowed_caps, 0);
83
84 profile = gst_structure_get_string (s, "profile");
85 if (profile) {
86 if (!strcmp (profile, "high")) {
87 thiz->profile = MFX_PROFILE_MPEG2_HIGH;
88 } else if (!strcmp (profile, "main")) {
89 thiz->profile = MFX_PROFILE_MPEG2_MAIN;
90 } else if (!strcmp (profile, "simple")) {
91 thiz->profile = MFX_PROFILE_MPEG2_SIMPLE;
92 } else {
93 g_assert_not_reached ();
94 }
95 }
96
97 gst_caps_unref (allowed_caps);
98 }
99
100 gst_caps_unref (template_caps);
101
102 return TRUE;
103 }
104
105 static gboolean
gst_msdkmpeg2enc_configure(GstMsdkEnc * encoder)106 gst_msdkmpeg2enc_configure (GstMsdkEnc * encoder)
107 {
108 GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (encoder);
109
110 encoder->param.mfx.CodecId = MFX_CODEC_MPEG2;
111 encoder->param.mfx.CodecProfile = thiz->profile;
112 encoder->param.mfx.CodecLevel = 0;
113
114 /* Enable Extended Coding options */
115 gst_msdkenc_ensure_extended_coding_options (encoder);
116
117 return TRUE;
118 }
119
120 static inline const gchar *
profile_to_string(gint profile)121 profile_to_string (gint profile)
122 {
123 switch (profile) {
124 case MFX_PROFILE_MPEG2_HIGH:
125 return "high";
126 case MFX_PROFILE_MPEG2_MAIN:
127 return "main";
128 case MFX_PROFILE_MPEG2_SIMPLE:
129 return "simple";
130 default:
131 break;
132 }
133
134 return NULL;
135 }
136
137 static GstCaps *
gst_msdkmpeg2enc_set_src_caps(GstMsdkEnc * encoder)138 gst_msdkmpeg2enc_set_src_caps (GstMsdkEnc * encoder)
139 {
140 GstCaps *caps;
141 GstStructure *structure;
142 const gchar *profile;
143
144 caps = gst_caps_from_string ("video/mpeg, mpegversion=2, systemstream=false");
145 structure = gst_caps_get_structure (caps, 0);
146
147 profile = profile_to_string (encoder->param.mfx.CodecProfile);
148 if (profile)
149 gst_structure_set (structure, "profile", G_TYPE_STRING, profile, NULL);
150
151 return caps;
152 }
153
154 static void
gst_msdkmpeg2enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)155 gst_msdkmpeg2enc_set_property (GObject * object, guint prop_id,
156 const GValue * value, GParamSpec * pspec)
157 {
158 GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (object);
159
160 if (!gst_msdkenc_set_common_property (object, prop_id, value, pspec))
161 GST_WARNING_OBJECT (thiz, "Failed to set common encode property");
162 }
163
164 static void
gst_msdkmpeg2enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)165 gst_msdkmpeg2enc_get_property (GObject * object, guint prop_id, GValue * value,
166 GParamSpec * pspec)
167 {
168 GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (object);
169
170 if (!gst_msdkenc_get_common_property (object, prop_id, value, pspec))
171 GST_WARNING_OBJECT (thiz, "Failed to get common encode property");
172 }
173
174 static void
gst_msdkmpeg2enc_class_init(GstMsdkMPEG2EncClass * klass)175 gst_msdkmpeg2enc_class_init (GstMsdkMPEG2EncClass * klass)
176 {
177 GObjectClass *gobject_class;
178 GstElementClass *element_class;
179 GstMsdkEncClass *encoder_class;
180
181 gobject_class = G_OBJECT_CLASS (klass);
182 element_class = GST_ELEMENT_CLASS (klass);
183 encoder_class = GST_MSDKENC_CLASS (klass);
184
185 gobject_class->set_property = gst_msdkmpeg2enc_set_property;
186 gobject_class->get_property = gst_msdkmpeg2enc_get_property;
187
188 encoder_class->set_format = gst_msdkmpeg2enc_set_format;
189 encoder_class->configure = gst_msdkmpeg2enc_configure;
190 encoder_class->set_src_caps = gst_msdkmpeg2enc_set_src_caps;
191
192 gst_msdkenc_install_common_properties (encoder_class);
193
194 gst_element_class_set_static_metadata (element_class,
195 "Intel MSDK MPEG2 encoder",
196 "Codec/Encoder/Video/Hardware",
197 "MPEG2 video encoder based on Intel Media SDK",
198 "Josep Torra <jtorra@oblong.com>");
199
200 gst_element_class_add_static_pad_template (element_class, &src_factory);
201 }
202
203 static void
gst_msdkmpeg2enc_init(GstMsdkMPEG2Enc * thiz)204 gst_msdkmpeg2enc_init (GstMsdkMPEG2Enc * thiz)
205 {
206 }
207