• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
33  * SECTION:element-msdkmpeg2enc
34  * @title: msdkmpeg2enc
35  * @short_description: Intel MSDK MPEG2 encoder
36  *
37  * MPEG2 video encoder based on Intel MFX
38  *
39  * ## Example launch line
40  * ```
41  * gst-launch-1.0 videotestsrc num-buffers=90 ! msdkmpeg2enc ! mpegvideoparse ! filesink location=output.mpg
42  * ```
43  *
44  * Since: 1.12
45  *
46  */
47 
48 #ifdef HAVE_CONFIG_H
49 #  include <config.h>
50 #endif
51 
52 #include "gstmsdkmpeg2enc.h"
53 
54 GST_DEBUG_CATEGORY_EXTERN (gst_msdkmpeg2enc_debug);
55 #define GST_CAT_DEFAULT gst_msdkmpeg2enc_debug
56 
57 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("video/mpeg, "
61         "framerate = (fraction) [0/1, MAX], "
62         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ], "
63         "mpegversion = (int) 2 , systemstream = (bool) false, "
64         "profile = (string) { high, main, simple }")
65     );
66 
67 #define gst_msdkmpeg2enc_parent_class parent_class
68 G_DEFINE_TYPE (GstMsdkMPEG2Enc, gst_msdkmpeg2enc, GST_TYPE_MSDKENC);
69 
70 static gboolean
gst_msdkmpeg2enc_set_format(GstMsdkEnc * encoder)71 gst_msdkmpeg2enc_set_format (GstMsdkEnc * encoder)
72 {
73   GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (encoder);
74   GstCaps *template_caps;
75   GstCaps *allowed_caps = NULL;
76 
77   thiz->profile = 0;
78 
79   template_caps = gst_static_pad_template_get_caps (&src_factory);
80   allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
81 
82   /* If downstream has ANY caps let encoder decide profile and level */
83   if (allowed_caps == template_caps) {
84     GST_INFO_OBJECT (thiz,
85         "downstream has ANY caps, profile/level set to auto");
86   } else if (allowed_caps) {
87     GstStructure *s;
88     const gchar *profile;
89 
90     if (gst_caps_is_empty (allowed_caps)) {
91       gst_caps_unref (allowed_caps);
92       gst_caps_unref (template_caps);
93       return FALSE;
94     }
95 
96     allowed_caps = gst_caps_make_writable (allowed_caps);
97     allowed_caps = gst_caps_fixate (allowed_caps);
98     s = gst_caps_get_structure (allowed_caps, 0);
99 
100     profile = gst_structure_get_string (s, "profile");
101     if (profile) {
102       if (!strcmp (profile, "high")) {
103         thiz->profile = MFX_PROFILE_MPEG2_HIGH;
104       } else if (!strcmp (profile, "main")) {
105         thiz->profile = MFX_PROFILE_MPEG2_MAIN;
106       } else if (!strcmp (profile, "simple")) {
107         thiz->profile = MFX_PROFILE_MPEG2_SIMPLE;
108       } else {
109         g_assert_not_reached ();
110       }
111     }
112 
113     gst_caps_unref (allowed_caps);
114   }
115 
116   gst_caps_unref (template_caps);
117 
118   return TRUE;
119 }
120 
121 static gboolean
gst_msdkmpeg2enc_configure(GstMsdkEnc * encoder)122 gst_msdkmpeg2enc_configure (GstMsdkEnc * encoder)
123 {
124   GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (encoder);
125 
126   encoder->param.mfx.CodecId = MFX_CODEC_MPEG2;
127   encoder->param.mfx.CodecProfile = thiz->profile;
128   encoder->param.mfx.CodecLevel = 0;
129 
130   /* Enable Extended Coding options */
131   gst_msdkenc_ensure_extended_coding_options (encoder);
132 
133   return TRUE;
134 }
135 
136 static inline const gchar *
profile_to_string(gint profile)137 profile_to_string (gint profile)
138 {
139   switch (profile) {
140     case MFX_PROFILE_MPEG2_HIGH:
141       return "high";
142     case MFX_PROFILE_MPEG2_MAIN:
143       return "main";
144     case MFX_PROFILE_MPEG2_SIMPLE:
145       return "simple";
146     default:
147       break;
148   }
149 
150   return NULL;
151 }
152 
153 static GstCaps *
gst_msdkmpeg2enc_set_src_caps(GstMsdkEnc * encoder)154 gst_msdkmpeg2enc_set_src_caps (GstMsdkEnc * encoder)
155 {
156   GstCaps *caps;
157   GstStructure *structure;
158   const gchar *profile;
159 
160   caps = gst_caps_from_string ("video/mpeg, mpegversion=2, systemstream=false");
161   structure = gst_caps_get_structure (caps, 0);
162 
163   profile = profile_to_string (encoder->param.mfx.CodecProfile);
164   if (profile)
165     gst_structure_set (structure, "profile", G_TYPE_STRING, profile, NULL);
166 
167   return caps;
168 }
169 
170 static void
gst_msdkmpeg2enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)171 gst_msdkmpeg2enc_set_property (GObject * object, guint prop_id,
172     const GValue * value, GParamSpec * pspec)
173 {
174   GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (object);
175 
176   if (!gst_msdkenc_set_common_property (object, prop_id, value, pspec))
177     GST_WARNING_OBJECT (thiz, "Failed to set common encode property");
178 }
179 
180 static void
gst_msdkmpeg2enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)181 gst_msdkmpeg2enc_get_property (GObject * object, guint prop_id, GValue * value,
182     GParamSpec * pspec)
183 {
184   GstMsdkMPEG2Enc *thiz = GST_MSDKMPEG2ENC (object);
185 
186   if (!gst_msdkenc_get_common_property (object, prop_id, value, pspec))
187     GST_WARNING_OBJECT (thiz, "Failed to get common encode property");
188 }
189 
190 static void
gst_msdkmpeg2enc_class_init(GstMsdkMPEG2EncClass * klass)191 gst_msdkmpeg2enc_class_init (GstMsdkMPEG2EncClass * klass)
192 {
193   GObjectClass *gobject_class;
194   GstElementClass *element_class;
195   GstMsdkEncClass *encoder_class;
196 
197   gobject_class = G_OBJECT_CLASS (klass);
198   element_class = GST_ELEMENT_CLASS (klass);
199   encoder_class = GST_MSDKENC_CLASS (klass);
200 
201   gobject_class->set_property = gst_msdkmpeg2enc_set_property;
202   gobject_class->get_property = gst_msdkmpeg2enc_get_property;
203 
204   encoder_class->set_format = gst_msdkmpeg2enc_set_format;
205   encoder_class->configure = gst_msdkmpeg2enc_configure;
206   encoder_class->set_src_caps = gst_msdkmpeg2enc_set_src_caps;
207 
208   gst_msdkenc_install_common_properties (encoder_class);
209 
210   gst_element_class_set_static_metadata (element_class,
211       "Intel MSDK MPEG2 encoder",
212       "Codec/Encoder/Video/Hardware",
213       "MPEG2 video encoder based on " MFX_API_SDK,
214       "Josep Torra <jtorra@oblong.com>");
215 
216   gst_element_class_add_static_pad_template (element_class, &src_factory);
217 }
218 
219 static void
gst_msdkmpeg2enc_init(GstMsdkMPEG2Enc * thiz)220 gst_msdkmpeg2enc_init (GstMsdkMPEG2Enc * thiz)
221 {
222 }
223