Move files from gst-plugins-bad into the "subprojects/gst-plugins-bad/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / msdk / gstmsdkmjpegenc.c
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 /**
33  * SECTION: element-msdkmjpegenc
34  * @title: msdkmjpegenc
35  * @short_description: Intel MSDK MJPEG encoder
36  *
37  * MJPEG video encoder based on Intel MFX
38  *
39  * ## Example launch line
40  * ```
41  * gst-launch-1.0 videotestsrc num-buffers=1 ! msdkmjpegenc ! jpegparse ! filesink location=output.jpg
42  * ```
43  *
44  * Since: 1.12
45  *
46  */
47
48 #ifdef HAVE_CONFIG_H
49 #  include <config.h>
50 #endif
51
52 #ifdef HAVE_MFX_MFXDEFS_H
53 #  include <mfx/mfxstructures.h>
54 #  include <mfx/mfxjpeg.h>
55 #else
56 #  include "mfxstructures.h"
57 #  include "mfxjpeg.h"
58 #endif
59
60 #include "gstmsdkmjpegenc.h"
61
62 GST_DEBUG_CATEGORY_EXTERN (gst_msdkmjpegenc_debug);
63 #define GST_CAT_DEFAULT gst_msdkmjpegenc_debug
64
65 enum
66 {
67   PROP_0,
68   PROP_QUALITY
69 };
70
71 #define DEFAULT_QUALITY 85
72
73 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
74     GST_PAD_SRC,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS ("image/jpeg, "
77         "framerate = (fraction) [0/1, MAX], "
78         "width = (int) [ 1, MAX ], height = (int) [ 1, MAX ]")
79     );
80
81 #define gst_msdkmjpegenc_parent_class parent_class
82 G_DEFINE_TYPE (GstMsdkMJPEGEnc, gst_msdkmjpegenc, GST_TYPE_MSDKENC);
83
84 static gboolean
85 gst_msdkmjpegenc_set_format (GstMsdkEnc * encoder)
86 {
87   return TRUE;
88 }
89
90 static gboolean
91 gst_msdkmjpegenc_configure (GstMsdkEnc * encoder)
92 {
93   GstMsdkMJPEGEnc *mjpegenc = GST_MSDKMJPEGENC (encoder);
94
95   encoder->param.mfx.CodecId = MFX_CODEC_JPEG;
96   encoder->param.mfx.Quality = mjpegenc->quality;
97   encoder->param.mfx.Interleaved = 1;
98   encoder->param.mfx.RestartInterval = 0;
99   encoder->param.mfx.BufferSizeInKB = 3072;
100
101   return TRUE;
102 }
103
104 static GstCaps *
105 gst_msdkmjpegenc_set_src_caps (GstMsdkEnc * encoder)
106 {
107   GstCaps *caps;
108
109   caps = gst_caps_from_string ("image/jpeg");
110
111   return caps;
112 }
113
114 static void
115 gst_msdkmjpegenc_get_property (GObject * object, guint prop_id, GValue * value,
116     GParamSpec * pspec)
117 {
118   GstMsdkMJPEGEnc *thiz = GST_MSDKMJPEGENC (object);
119
120   GST_OBJECT_LOCK (thiz);
121   switch (prop_id) {
122     case PROP_QUALITY:
123       g_value_set_uint (value, thiz->quality);
124       break;
125     default:
126       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127       break;
128   }
129   GST_OBJECT_UNLOCK (thiz);
130 }
131
132 static void
133 gst_msdkmjpegenc_set_property (GObject * object, guint prop_id,
134     const GValue * value, GParamSpec * pspec)
135 {
136   GstMsdkMJPEGEnc *thiz = GST_MSDKMJPEGENC (object);
137
138   GST_OBJECT_LOCK (thiz);
139   switch (prop_id) {
140     case PROP_QUALITY:
141       thiz->quality = g_value_get_uint (value);
142       break;
143     default:
144       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145       break;
146   }
147   GST_OBJECT_UNLOCK (thiz);
148 }
149
150 static gboolean
151 gst_msdkmjpegenc_need_conversion (GstMsdkEnc * encoder, GstVideoInfo * info,
152     GstVideoFormat * out_format)
153 {
154   switch (GST_VIDEO_INFO_FORMAT (info)) {
155     case GST_VIDEO_FORMAT_NV12:
156     case GST_VIDEO_FORMAT_YUY2:
157     case GST_VIDEO_FORMAT_BGRA:
158       return FALSE;
159
160     case GST_VIDEO_FORMAT_UYVY:
161       *out_format = GST_VIDEO_FORMAT_YUY2;
162       return TRUE;
163
164     default:
165       *out_format = GST_VIDEO_FORMAT_NV12;
166       return TRUE;
167   }
168 }
169
170 static void
171 gst_msdkmjpegenc_class_init (GstMsdkMJPEGEncClass * klass)
172 {
173   GObjectClass *gobject_class;
174   GstElementClass *element_class;
175   GstMsdkEncClass *encoder_class;
176
177   gobject_class = G_OBJECT_CLASS (klass);
178   element_class = GST_ELEMENT_CLASS (klass);
179   encoder_class = GST_MSDKENC_CLASS (klass);
180
181   encoder_class->set_format = gst_msdkmjpegenc_set_format;
182   encoder_class->configure = gst_msdkmjpegenc_configure;
183   encoder_class->set_src_caps = gst_msdkmjpegenc_set_src_caps;
184   encoder_class->need_conversion = gst_msdkmjpegenc_need_conversion;
185
186   gobject_class->get_property = gst_msdkmjpegenc_get_property;
187   gobject_class->set_property = gst_msdkmjpegenc_set_property;
188
189   g_object_class_install_property (gobject_class, PROP_QUALITY,
190       g_param_spec_uint ("quality", "Quality", "Quality of encoding",
191           0, 100, DEFAULT_QUALITY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192
193   gst_element_class_set_static_metadata (element_class,
194       "Intel MSDK MJPEG encoder",
195       "Codec/Encoder/Video/Hardware",
196       "MJPEG video encoder based on " MFX_API_SDK,
197       "Scott D Phillips <scott.d.phillips@intel.com>");
198
199   gst_element_class_add_static_pad_template (element_class, &src_factory);
200 }
201
202 static void
203 gst_msdkmjpegenc_init (GstMsdkMJPEGEnc * thiz)
204 {
205   thiz->quality = DEFAULT_QUALITY;
206 }