• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-jp2kdecimator
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 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (jp2kdecimator, "jp2kdecimator",
83     GST_RANK_NONE, GST_TYPE_JP2K_DECIMATOR,
84     GST_DEBUG_CATEGORY_INIT (gst_jp2k_decimator_debug, "jp2kdecimator", 0,
85         "JPEG2000 decimator"));
86 static void
gst_jp2k_decimator_class_init(GstJP2kDecimatorClass * klass)87 gst_jp2k_decimator_class_init (GstJP2kDecimatorClass * klass)
88 {
89   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
90   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
91 
92   gst_element_class_set_static_metadata (gstelement_class,
93       "JPEG2000 decimator",
94       "Filter/Image",
95       "Removes information from JPEG2000 streams without recompression",
96       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
97 
98   gst_element_class_add_static_pad_template (gstelement_class,
99       &sink_pad_template);
100   gst_element_class_add_static_pad_template (gstelement_class,
101       &src_pad_template);
102 
103   gobject_class->set_property = gst_jp2k_decimator_set_property;
104   gobject_class->get_property = gst_jp2k_decimator_get_property;
105 
106   g_object_class_install_property (gobject_class, PROP_MAX_LAYERS,
107       g_param_spec_int ("max-layers", "Maximum Number of Layers",
108           "Maximum number of layers to keep (0 == all)", 0, 65535,
109           DEFAULT_MAX_LAYERS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
110 
111   g_object_class_install_property (gobject_class, PROP_MAX_DECOMPOSITION_LEVELS,
112       g_param_spec_int ("max-decomposition-levels",
113           "Maximum Number of Decomposition Levels",
114           "Maximum number of decomposition levels to keep (-1 == all)", -1, 32,
115           DEFAULT_MAX_DECOMPOSITION_LEVELS,
116           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117 }
118 
119 static void
gst_jp2k_decimator_init(GstJP2kDecimator * self)120 gst_jp2k_decimator_init (GstJP2kDecimator * self)
121 {
122   self->max_layers = DEFAULT_MAX_LAYERS;
123   self->max_decomposition_levels = DEFAULT_MAX_DECOMPOSITION_LEVELS;
124 
125   self->sinkpad = gst_pad_new_from_static_template (&sink_pad_template, "sink");
126   GST_PAD_SET_PROXY_CAPS (self->sinkpad);
127   GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
128 
129   gst_pad_set_chain_function (self->sinkpad,
130       GST_DEBUG_FUNCPTR (gst_jp2k_decimator_sink_chain));
131   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
132 
133   self->srcpad = gst_pad_new_from_static_template (&src_pad_template, "src");
134   GST_PAD_SET_PROXY_CAPS (self->srcpad);
135   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
136 }
137 
138 static void
gst_jp2k_decimator_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)139 gst_jp2k_decimator_set_property (GObject * object,
140     guint prop_id, const GValue * value, GParamSpec * pspec)
141 {
142   GstJP2kDecimator *self = GST_JP2K_DECIMATOR (object);
143 
144   switch (prop_id) {
145     case PROP_MAX_LAYERS:
146       self->max_layers = g_value_get_int (value);
147       break;
148     case PROP_MAX_DECOMPOSITION_LEVELS:
149       self->max_decomposition_levels = g_value_get_int (value);
150       break;
151     default:
152       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153       break;
154   }
155 }
156 
157 static void
gst_jp2k_decimator_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)158 gst_jp2k_decimator_get_property (GObject * object,
159     guint prop_id, GValue * value, GParamSpec * pspec)
160 {
161   GstJP2kDecimator *self = GST_JP2K_DECIMATOR (object);
162 
163   switch (prop_id) {
164     case PROP_MAX_LAYERS:
165       g_value_set_int (value, self->max_layers);
166       break;
167     case PROP_MAX_DECOMPOSITION_LEVELS:
168       g_value_set_int (value, self->max_decomposition_levels);
169       break;
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172       break;
173   }
174 }
175 
176 static GstFlowReturn
gst_jp2k_decimator_decimate_jpc(GstJP2kDecimator * self,GstBuffer * inbuf,GstBuffer ** outbuf_)177 gst_jp2k_decimator_decimate_jpc (GstJP2kDecimator * self, GstBuffer * inbuf,
178     GstBuffer ** outbuf_)
179 {
180   GstBuffer *outbuf = NULL;
181   GstFlowReturn ret = GST_FLOW_OK;
182   GstMapInfo info;
183   GstByteReader reader;
184   GstByteWriter writer;
185   MainHeader main_header;
186 
187 
188   if (!gst_buffer_map (inbuf, &info, GST_MAP_READ)) {
189     GST_ELEMENT_ERROR (self, STREAM, WRONG_TYPE, ("Unable to map memory"),
190         (NULL));
191     gst_buffer_unref (inbuf);
192     return GST_FLOW_ERROR;
193   }
194 
195   gst_byte_reader_init (&reader, info.data, info.size);
196   gst_byte_writer_init_with_size (&writer, gst_buffer_get_size (inbuf), FALSE);
197 
198   /* main header */
199   memset (&main_header, 0, sizeof (MainHeader));
200   ret = parse_main_header (self, &reader, &main_header);
201   if (ret != GST_FLOW_OK)
202     goto done;
203 
204   ret = decimate_main_header (self, &main_header);
205   if (ret != GST_FLOW_OK)
206     goto done;
207 
208   ret = write_main_header (self, &writer, &main_header);
209   if (ret != GST_FLOW_OK)
210     goto done;
211 
212   outbuf = gst_byte_writer_reset_and_get_buffer (&writer);
213   gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_METADATA, 0, -1);
214 
215   GST_DEBUG_OBJECT (self,
216       "Decimated buffer from %" G_GSIZE_FORMAT " bytes to %" G_GSIZE_FORMAT
217       " bytes (%.2lf%%)",
218       gst_buffer_get_size (inbuf), gst_buffer_get_size (outbuf),
219       (100 * gst_buffer_get_size (outbuf)) /
220       ((gdouble) gst_buffer_get_size (inbuf)));
221 
222 done:
223   gst_buffer_unmap (inbuf, &info);
224 
225   *outbuf_ = outbuf;
226   reset_main_header (self, &main_header);
227   gst_buffer_unref (inbuf);
228 
229   return ret;
230 }
231 
232 static GstFlowReturn
gst_jp2k_decimator_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)233 gst_jp2k_decimator_sink_chain (GstPad * pad, GstObject * parent,
234     GstBuffer * inbuf)
235 {
236   GstJP2kDecimator *self = GST_JP2K_DECIMATOR (parent);
237   GstFlowReturn ret;
238   GstBuffer *outbuf = NULL;
239 
240   GST_LOG_OBJECT (pad,
241       "Handling inbuf with timestamp %" GST_TIME_FORMAT " and duration %"
242       GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (inbuf)),
243       GST_TIME_ARGS (GST_BUFFER_DURATION (inbuf)));
244 
245   if (self->max_layers == 0 && self->max_decomposition_levels == -1) {
246     outbuf = inbuf;
247     inbuf = NULL;
248     ret = GST_FLOW_OK;
249   } else {
250     ret = gst_jp2k_decimator_decimate_jpc (self, inbuf, &outbuf);
251   }
252 
253   if (G_UNLIKELY (ret != GST_FLOW_OK))
254     return ret;
255 
256   ret = gst_pad_push (self->srcpad, outbuf);
257 
258   return ret;
259 }
260 
261 static gboolean
plugin_init(GstPlugin * plugin)262 plugin_init (GstPlugin * plugin)
263 {
264   return GST_ELEMENT_REGISTER (jp2kdecimator, plugin);
265 }
266 
267 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
268     GST_VERSION_MINOR,
269     jp2kdecimator,
270     "JPEG2000 decimator", plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME,
271     GST_PACKAGE_ORIGIN)
272