1 /* GStreamer
2 * Copyright (C) 2010 Oblong Industries, Inc.
3 * Copyright (C) 2010 Collabora Multimedia
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20 /**
21 * SECTION:element-gstjp2kdecimator
22 * @title: gstjp2kdecimator
23 *
24 * The jp2kdecimator element removes information from JPEG2000 images without reencoding.
25 *
26 * ## Example launch line
27 * |[
28 * gst-launch-1.0 -v videotestsrc num-buffers=1 ! jp2kenc ! \
29 * gstjp2kdecimator max-decomposition-levels=2 ! jp2kdec ! \
30 * videoconvert ! autovideosink
31 * ]|
32 * This pipelines encodes a test image to JPEG2000, only keeps 3 decomposition levels
33 * decodes the decimated image again and shows it on the screen.
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <gst/gst.h>
42
43 #include "gstjp2kdecimator.h"
44
45 #include "jp2kcodestream.h"
46
47 #include <gst/base/gstbytereader.h>
48 #include <gst/base/gstbytewriter.h>
49
50 #include <string.h>
51
52 static GstStaticPadTemplate sink_pad_template =
53 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
54 GST_STATIC_CAPS ("image/x-jpc"));
55
56 static GstStaticPadTemplate src_pad_template =
57 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
58 GST_STATIC_CAPS ("image/x-jpc"));
59
60 enum
61 {
62 PROP_0,
63 PROP_MAX_LAYERS,
64 PROP_MAX_DECOMPOSITION_LEVELS
65 };
66
67 #define DEFAULT_MAX_LAYERS (0)
68 #define DEFAULT_MAX_DECOMPOSITION_LEVELS (-1)
69
70 static void gst_jp2k_decimator_set_property (GObject * object,
71 guint prop_id, const GValue * value, GParamSpec * pspec);
72 static void gst_jp2k_decimator_get_property (GObject * object,
73 guint prop_id, GValue * value, GParamSpec * pspec);
74
75 static GstFlowReturn gst_jp2k_decimator_sink_chain (GstPad * pad,
76 GstObject * parent, GstBuffer * inbuf);
77
78 GST_DEBUG_CATEGORY (gst_jp2k_decimator_debug);
79 #define GST_CAT_DEFAULT gst_jp2k_decimator_debug
80
81 G_DEFINE_TYPE (GstJP2kDecimator, gst_jp2k_decimator, GST_TYPE_ELEMENT);
82
83 static void
gst_jp2k_decimator_class_init(GstJP2kDecimatorClass * klass)84 gst_jp2k_decimator_class_init (GstJP2kDecimatorClass * klass)
85 {
86 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
88
89 gst_element_class_set_static_metadata (gstelement_class,
90 "JPEG2000 decimator",
91 "Filter/Image",
92 "Removes information from JPEG2000 streams without recompression",
93 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
94
95 gst_element_class_add_static_pad_template (gstelement_class,
96 &sink_pad_template);
97 gst_element_class_add_static_pad_template (gstelement_class,
98 &src_pad_template);
99
100 gobject_class->set_property = gst_jp2k_decimator_set_property;
101 gobject_class->get_property = gst_jp2k_decimator_get_property;
102
103 g_object_class_install_property (gobject_class, PROP_MAX_LAYERS,
104 g_param_spec_int ("max-layers", "Maximum Number of Layers",
105 "Maximum number of layers to keep (0 == all)", 0, 65535,
106 DEFAULT_MAX_LAYERS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107
108 g_object_class_install_property (gobject_class, PROP_MAX_DECOMPOSITION_LEVELS,
109 g_param_spec_int ("max-decomposition-levels",
110 "Maximum Number of Decomposition Levels",
111 "Maximum number of decomposition levels to keep (-1 == all)", -1, 32,
112 DEFAULT_MAX_DECOMPOSITION_LEVELS,
113 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114 }
115
116 static void
gst_jp2k_decimator_init(GstJP2kDecimator * self)117 gst_jp2k_decimator_init (GstJP2kDecimator * self)
118 {
119 self->max_layers = DEFAULT_MAX_LAYERS;
120 self->max_decomposition_levels = DEFAULT_MAX_DECOMPOSITION_LEVELS;
121
122 self->sinkpad = gst_pad_new_from_static_template (&sink_pad_template, "sink");
123 GST_PAD_SET_PROXY_CAPS (self->sinkpad);
124 GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
125
126 gst_pad_set_chain_function (self->sinkpad,
127 GST_DEBUG_FUNCPTR (gst_jp2k_decimator_sink_chain));
128 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
129
130 self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
131 GST_PAD_SET_PROXY_CAPS (self->srcpad);
132 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
133 }
134
135 static void
gst_jp2k_decimator_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)136 gst_jp2k_decimator_set_property (GObject * object,
137 guint prop_id, const GValue * value, GParamSpec * pspec)
138 {
139 GstJP2kDecimator *self = GST_JP2K_DECIMATOR (object);
140
141 switch (prop_id) {
142 case PROP_MAX_LAYERS:
143 self->max_layers = g_value_get_int (value);
144 break;
145 case PROP_MAX_DECOMPOSITION_LEVELS:
146 self->max_decomposition_levels = g_value_get_int (value);
147 break;
148 default:
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 break;
151 }
152 }
153
154 static void
gst_jp2k_decimator_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)155 gst_jp2k_decimator_get_property (GObject * object,
156 guint prop_id, GValue * value, GParamSpec * pspec)
157 {
158 GstJP2kDecimator *self = GST_JP2K_DECIMATOR (object);
159
160 switch (prop_id) {
161 case PROP_MAX_LAYERS:
162 g_value_set_int (value, self->max_layers);
163 break;
164 case PROP_MAX_DECOMPOSITION_LEVELS:
165 g_value_set_int (value, self->max_decomposition_levels);
166 break;
167 default:
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 break;
170 }
171 }
172
173 static GstFlowReturn
gst_jp2k_decimator_decimate_jpc(GstJP2kDecimator * self,GstBuffer * inbuf,GstBuffer ** outbuf_)174 gst_jp2k_decimator_decimate_jpc (GstJP2kDecimator * self, GstBuffer * inbuf,
175 GstBuffer ** outbuf_)
176 {
177 GstBuffer *outbuf = NULL;
178 GstFlowReturn ret = GST_FLOW_OK;
179 GstMapInfo info;
180 GstByteReader reader;
181 GstByteWriter writer;
182 MainHeader main_header;
183
184
185 if (!gst_buffer_map (inbuf, &info, GST_MAP_READ)) {
186 GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE, ("Unable to map memory"),
187 (NULL));
188 gst_buffer_unref (inbuf);
189 return GST_FLOW_ERROR;
190 }
191
192 gst_byte_reader_init (&reader, info.data, info.size);
193 gst_byte_writer_init_with_size (&writer, gst_buffer_get_size (inbuf), FALSE);
194
195 /* main header */
196 memset (&main_header, 0, sizeof (MainHeader));
197 ret = parse_main_header (self, &reader, &main_header);
198 if (ret != GST_FLOW_OK)
199 goto done;
200
201 ret = decimate_main_header (self, &main_header);
202 if (ret != GST_FLOW_OK)
203 goto done;
204
205 ret = write_main_header (self, &writer, &main_header);
206 if (ret != GST_FLOW_OK)
207 goto done;
208
209 outbuf = gst_byte_writer_reset_and_get_buffer (&writer);
210 gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_METADATA, 0, -1);
211
212 GST_DEBUG_OBJECT (self,
213 "Decimated buffer from %" G_GSIZE_FORMAT " bytes to %" G_GSIZE_FORMAT
214 " bytes (%.2lf%%)",
215 gst_buffer_get_size (inbuf), gst_buffer_get_size (outbuf),
216 (100 * gst_buffer_get_size (outbuf)) /
217 ((gdouble) gst_buffer_get_size (inbuf)));
218
219 done:
220 gst_buffer_unmap (inbuf, &info);
221
222 *outbuf_ = outbuf;
223 reset_main_header (self, &main_header);
224 gst_buffer_unref (inbuf);
225
226 return ret;
227 }
228
229 static GstFlowReturn
gst_jp2k_decimator_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)230 gst_jp2k_decimator_sink_chain (GstPad * pad, GstObject * parent,
231 GstBuffer * inbuf)
232 {
233 GstJP2kDecimator *self = GST_JP2K_DECIMATOR (parent);
234 GstFlowReturn ret;
235 GstBuffer *outbuf = NULL;
236
237 GST_LOG_OBJECT (pad,
238 "Handling inbuf with timestamp %" GST_TIME_FORMAT " and duration %"
239 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)),
240 GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)));
241
242 if (self->max_layers == 0 && self->max_decomposition_levels == -1) {
243 outbuf = inbuf;
244 inbuf = NULL;
245 ret = GST_FLOW_OK;
246 } else {
247 ret = gst_jp2k_decimator_decimate_jpc (self, inbuf, &outbuf);
248 }
249
250 if (G_UNLIKELY (ret != GST_FLOW_OK))
251 return ret;
252
253 ret = gst_pad_push (self->srcpad, outbuf);
254
255 return ret;
256 }
257
258 static gboolean
plugin_init(GstPlugin * plugin)259 plugin_init (GstPlugin * plugin)
260 {
261 GST_DEBUG_CATEGORY_INIT (gst_jp2k_decimator_debug, "jp2kdecimator", 0,
262 "JPEG2000 decimator");
263
264 gst_element_register (plugin, "jp2kdecimator", GST_RANK_NONE,
265 GST_TYPE_JP2K_DECIMATOR);
266
267 return TRUE;
268 }
269
270 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
271 GST_VERSION_MINOR,
272 jp2kdecimator,
273 "JPEG2000 decimator", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
274 GST_PACKAGE_ORIGIN)
275