1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2016, Intel Corporation
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 #ifdef HAVE_MFX_MFXDEFS_H
37 #  include <mfx/mfxstructures.h>
38 #  include <mfx/mfxjpeg.h>
39 #else
40 #  include "mfxstructures.h"
41 #  include "mfxjpeg.h"
42 #endif
43 
44 #include "gstmsdkmjpegenc.h"
45 
46 GST_DEBUG_CATEGORY_EXTERN (gst_msdkmjpegenc_debug);
47 #define GST_CAT_DEFAULT gst_msdkmjpegenc_debug
48 
49 enum
50 {
51   PROP_0,
52   PROP_QUALITY
53 };
54 
55 #define DEFAULT_QUALITY 85
56 
57 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("image/jpeg, "
61         "framerate = (fraction) [0/1, MAX], "
62         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ]")
63     );
64 
65 #define gst_msdkmjpegenc_parent_class parent_class
66 G_DEFINE_TYPE (GstMsdkMJPEGEnc, gst_msdkmjpegenc, GST_TYPE_MSDKENC);
67 
68 static gboolean
gst_msdkmjpegenc_set_format(GstMsdkEnc * encoder)69 gst_msdkmjpegenc_set_format (GstMsdkEnc * encoder)
70 {
71   return TRUE;
72 }
73 
74 static gboolean
gst_msdkmjpegenc_configure(GstMsdkEnc * encoder)75 gst_msdkmjpegenc_configure (GstMsdkEnc * encoder)
76 {
77   GstMsdkMJPEGEnc *mjpegenc = GST_MSDKMJPEGENC (encoder);
78 
79   encoder->param.mfx.CodecId = MFX_CODEC_JPEG;
80   encoder->param.mfx.Quality = mjpegenc->quality;
81   encoder->param.mfx.Interleaved = 1;
82   encoder->param.mfx.RestartInterval = 0;
83   encoder->param.mfx.BufferSizeInKB = 3072;
84 
85   return TRUE;
86 }
87 
88 static GstCaps *
gst_msdkmjpegenc_set_src_caps(GstMsdkEnc * encoder)89 gst_msdkmjpegenc_set_src_caps (GstMsdkEnc * encoder)
90 {
91   GstCaps *caps;
92 
93   caps = gst_caps_from_string ("image/jpeg");
94 
95   return caps;
96 }
97 
98 static void
gst_msdkmjpegenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)99 gst_msdkmjpegenc_get_property (GObject * object, guint prop_id, GValue * value,
100     GParamSpec * pspec)
101 {
102   GstMsdkMJPEGEnc *thiz = GST_MSDKMJPEGENC (object);
103 
104   GST_OBJECT_LOCK (thiz);
105   switch (prop_id) {
106     case PROP_QUALITY:
107       g_value_set_uint (value, thiz->quality);
108       break;
109     default:
110       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111       break;
112   }
113   GST_OBJECT_UNLOCK (thiz);
114 }
115 
116 static void
gst_msdkmjpegenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)117 gst_msdkmjpegenc_set_property (GObject * object, guint prop_id,
118     const GValue * value, GParamSpec * pspec)
119 {
120   GstMsdkMJPEGEnc *thiz = GST_MSDKMJPEGENC (object);
121 
122   GST_OBJECT_LOCK (thiz);
123   switch (prop_id) {
124     case PROP_QUALITY:
125       thiz->quality = g_value_get_uint (value);
126       break;
127     default:
128       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129       break;
130   }
131   GST_OBJECT_UNLOCK (thiz);
132 }
133 
134 static void
gst_msdkmjpegenc_class_init(GstMsdkMJPEGEncClass * klass)135 gst_msdkmjpegenc_class_init (GstMsdkMJPEGEncClass * klass)
136 {
137   GObjectClass *gobject_class;
138   GstElementClass *element_class;
139   GstMsdkEncClass *encoder_class;
140 
141   gobject_class = G_OBJECT_CLASS (klass);
142   element_class = GST_ELEMENT_CLASS (klass);
143   encoder_class = GST_MSDKENC_CLASS (klass);
144 
145   encoder_class->set_format = gst_msdkmjpegenc_set_format;
146   encoder_class->configure = gst_msdkmjpegenc_configure;
147   encoder_class->set_src_caps = gst_msdkmjpegenc_set_src_caps;
148 
149   gobject_class->get_property = gst_msdkmjpegenc_get_property;
150   gobject_class->set_property = gst_msdkmjpegenc_set_property;
151 
152   g_object_class_install_property (gobject_class, PROP_QUALITY,
153       g_param_spec_uint ("quality", "Quality", "Quality of encoding",
154           0, 100, DEFAULT_QUALITY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 
156   gst_element_class_set_static_metadata (element_class,
157       "Intel MSDK MJPEG encoder",
158       "Codec/Encoder/Video/Hardware",
159       "MJPEG video encoder based on Intel Media SDK",
160       "Scott D Phillips <scott.d.phillips@intel.com>");
161 
162   gst_element_class_add_static_pad_template (element_class, &src_factory);
163 }
164 
165 static void
gst_msdkmjpegenc_init(GstMsdkMJPEGEnc * thiz)166 gst_msdkmjpegenc_init (GstMsdkMJPEGEnc * thiz)
167 {
168   thiz->quality = DEFAULT_QUALITY;
169 }
170