2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
5 * Copyright (C) 2000 Donald A. Graft
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Library General Public License for more details.
12 * You should have received a copy of the GNU Library General Public
13 * License along with this library; if not, write to the
14 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15 * Boston, MA 02111-1307, USA.
19 * SECTION:element-pngenc
29 #include "gstpngenc.h"
30 #include <gst/video/video.h>
33 #define MAX_HEIGHT 4096
36 static const GstElementDetails gst_pngenc_details =
37 GST_ELEMENT_DETAILS ("PNG image encoder",
38 "Codec/Encoder/Image",
39 "Encode a video frame to a .png image",
40 "Jeremy SIMON <jsimon13@yahoo.fr>");
42 GST_DEBUG_CATEGORY_STATIC (pngenc_debug);
43 #define GST_CAT_DEFAULT pngenc_debug
45 /* Filter signals and args */
52 #define DEFAULT_SNAPSHOT TRUE
53 /* #define DEFAULT_NEWMEDIA FALSE */
54 #define DEFAULT_COMPRESSION_LEVEL 6
64 static GstStaticPadTemplate pngenc_src_template =
65 GST_STATIC_PAD_TEMPLATE ("src",
68 GST_STATIC_CAPS ("image/png, "
69 "width = (int) [ 16, 4096 ], "
70 "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0.0, MAX ]")
73 static GstStaticPadTemplate pngenc_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
80 /* static GstElementClass *parent_class = NULL; */
82 GST_BOILERPLATE (GstPngEnc, gst_pngenc, GstElement, GST_TYPE_ELEMENT);
84 static void gst_pngenc_set_property (GObject * object,
85 guint prop_id, const GValue * value, GParamSpec * pspec);
86 static void gst_pngenc_get_property (GObject * object,
87 guint prop_id, GValue * value, GParamSpec * pspec);
89 static GstFlowReturn gst_pngenc_chain (GstPad * pad, GstBuffer * data);
92 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
94 g_warning ("%s", error_msg);
98 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
100 g_warning ("%s", warning_msg);
104 gst_pngenc_base_init (gpointer g_class)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
108 gst_element_class_add_pad_template
109 (element_class, gst_static_pad_template_get (&pngenc_sink_template));
110 gst_element_class_add_pad_template
111 (element_class, gst_static_pad_template_get (&pngenc_src_template));
112 gst_element_class_set_details (element_class, &gst_pngenc_details);
116 gst_pngenc_class_init (GstPngEncClass * klass)
118 GObjectClass *gobject_class;
120 gobject_class = (GObjectClass *) klass;
122 parent_class = g_type_class_peek_parent (klass);
124 gobject_class->get_property = gst_pngenc_get_property;
125 gobject_class->set_property = gst_pngenc_set_property;
127 g_object_class_install_property (gobject_class, ARG_SNAPSHOT,
128 g_param_spec_boolean ("snapshot", "Snapshot",
129 "Send EOS after encoding a frame, useful for snapshots",
130 DEFAULT_SNAPSHOT, (GParamFlags) G_PARAM_READWRITE));
132 /* g_object_class_install_property (gobject_class, ARG_NEWMEDIA, */
133 /* g_param_spec_boolean ("newmedia", "newmedia", */
134 /* "Send new media discontinuity after encoding each frame", */
135 /* DEFAULT_NEWMEDIA, (GParamFlags) G_PARAM_READWRITE)); */
137 g_object_class_install_property
138 (gobject_class, ARG_COMPRESSION_LEVEL,
139 g_param_spec_uint ("compression-level", "compression-level",
140 "PNG compression level",
141 Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
142 DEFAULT_COMPRESSION_LEVEL, (GParamFlags) G_PARAM_READWRITE));
144 GST_DEBUG_CATEGORY_INIT (pngenc_debug, "pngenc", 0, "PNG image encoder");
149 gst_pngenc_setcaps (GstPad * pad, GstCaps * caps)
153 GstStructure *structure;
157 pngenc = GST_PNGENC (gst_pad_get_parent (pad));
159 structure = gst_caps_get_structure (caps, 0);
160 gst_structure_get_int (structure, "width", &pngenc->width);
161 gst_structure_get_int (structure, "height", &pngenc->height);
162 fps = gst_structure_get_value (structure, "framerate");
163 gst_structure_get_int (structure, "bpp", &pngenc->bpp);
165 if (pngenc->bpp == 32)
166 pngenc->stride = pngenc->width * 4;
168 pngenc->stride = GST_ROUND_UP_4 (pngenc->width * 3);
170 pcaps = gst_caps_new_simple ("image/png",
171 "width", G_TYPE_INT, pngenc->width,
172 "height", G_TYPE_INT, pngenc->height, NULL);
173 structure = gst_caps_get_structure (pcaps, 0);
174 gst_structure_set_value (structure, "framerate", fps);
176 ret = gst_pad_set_caps (pngenc->srcpad, pcaps);
178 gst_caps_unref (pcaps);
179 gst_object_unref (pngenc);
185 gst_pngenc_init (GstPngEnc * pngenc, GstPngEncClass * g_class)
188 pngenc->sinkpad = gst_pad_new_from_static_template
189 (&pngenc_sink_template, "sink");
190 gst_pad_set_chain_function (pngenc->sinkpad, gst_pngenc_chain);
191 /* gst_pad_set_link_function (pngenc->sinkpad, gst_pngenc_sinklink); */
192 /* gst_pad_set_getcaps_function (pngenc->sinkpad, gst_pngenc_sink_getcaps); */
193 gst_pad_set_setcaps_function (pngenc->sinkpad, gst_pngenc_setcaps);
194 gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->sinkpad);
197 pngenc->srcpad = gst_pad_new_from_static_template
198 (&pngenc_src_template, "src");
199 /* pngenc->srcpad = gst_pad_new ("src", GST_PAD_SRC); */
200 /* gst_pad_set_getcaps_function (pngenc->srcpad, gst_pngenc_src_getcaps); */
201 /* gst_pad_set_setcaps_function (pngenc->srcpad, gst_pngenc_setcaps); */
202 gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->srcpad);
205 pngenc->png_struct_ptr = NULL;
206 pngenc->png_info_ptr = NULL;
208 pngenc->snapshot = DEFAULT_SNAPSHOT;
209 /* pngenc->newmedia = FALSE; */
210 pngenc->compression_level = DEFAULT_COMPRESSION_LEVEL;
214 user_flush_data (png_structp png_ptr G_GNUC_UNUSED)
219 user_write_data (png_structp png_ptr, png_bytep data, png_uint_32 length)
223 pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
225 if (pngenc->written + length >= GST_BUFFER_SIZE (pngenc->buffer_out)) {
226 GST_ERROR_OBJECT (pngenc, "output buffer bigger than the input buffer!?");
228 longjmp (pngenc->png_struct_ptr->jmpbuf, 1);
234 memcpy (GST_BUFFER_DATA (pngenc->buffer_out) + pngenc->written, data, length);
235 pngenc->written += length;
239 gst_pngenc_chain (GstPad * pad, GstBuffer * buf)
243 png_byte *row_pointers[MAX_HEIGHT];
244 GstFlowReturn ret = GST_FLOW_OK;
245 GstBuffer *encoded_buf = NULL;
247 pngenc = GST_PNGENC (gst_pad_get_parent (pad));
249 GST_DEBUG_OBJECT (pngenc, "BEGINNING");
251 /* initialize png struct stuff */
252 pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
253 (png_voidp) NULL, user_error_fn, user_warning_fn);
254 if (pngenc->png_struct_ptr == NULL) {
255 gst_buffer_unref (buf);
256 GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
257 ("Failed to initialize png structure"));
258 ret = GST_FLOW_ERROR;
262 pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
263 if (!pngenc->png_info_ptr) {
264 gst_buffer_unref (buf);
265 png_destroy_write_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL);
266 GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
267 ("Failed to initialize the png info structure"));
268 ret = GST_FLOW_ERROR;
272 /* non-0 return is from a longjmp inside of libpng */
273 if (setjmp (pngenc->png_struct_ptr->jmpbuf) != 0) {
274 gst_buffer_unref (buf);
275 png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
276 GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
277 ("returning from longjmp"));
278 ret = GST_FLOW_ERROR;
282 png_set_filter (pngenc->png_struct_ptr, 0,
283 PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
284 png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level);
286 png_set_IHDR (pngenc->png_struct_ptr,
287 pngenc->png_info_ptr,
291 (pngenc->bpp == 32) ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
293 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
295 png_set_write_fn (pngenc->png_struct_ptr, pngenc,
296 (png_rw_ptr) user_write_data, user_flush_data);
298 for (row_index = 0; row_index < pngenc->height; row_index++) {
299 row_pointers[row_index] = GST_BUFFER_DATA (buf) +
300 (row_index * pngenc->stride);
303 /* allocate the output buffer */
305 gst_buffer_new_and_alloc (pngenc->height * pngenc->stride);
308 png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
309 png_write_image (pngenc->png_struct_ptr, row_pointers);
310 png_write_end (pngenc->png_struct_ptr, NULL);
312 encoded_buf = gst_buffer_create_sub (pngenc->buffer_out, 0, pngenc->written);
314 png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
315 png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);
316 gst_buffer_copy_metadata (encoded_buf, buf, GST_BUFFER_COPY_TIMESTAMPS);
317 gst_buffer_unref (buf);
318 gst_buffer_set_caps (encoded_buf, GST_PAD_CAPS (pngenc->srcpad));
320 if ((ret = gst_pad_push (pngenc->srcpad, encoded_buf)) != GST_FLOW_OK)
323 if (pngenc->snapshot) {
326 GST_DEBUG_OBJECT (pngenc, "snapshot mode, sending EOS");
327 /* send EOS event, since a frame has been pushed out */
328 event = gst_event_new_eos ();
330 gst_pad_push_event (pngenc->srcpad, event);
331 ret = GST_FLOW_UNEXPECTED;
335 GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);
337 if (pngenc->buffer_out != NULL) {
338 gst_buffer_unref (pngenc->buffer_out);
339 pngenc->buffer_out = NULL;
342 gst_object_unref (pngenc);
348 gst_pngenc_get_property (GObject * object,
349 guint prop_id, GValue * value, GParamSpec * pspec)
353 pngenc = GST_PNGENC (object);
357 g_value_set_boolean (value, pngenc->snapshot);
359 /* case ARG_NEWMEDIA: */
360 /* g_value_set_boolean (value, pngenc->newmedia); */
362 case ARG_COMPRESSION_LEVEL:
363 g_value_set_uint (value, pngenc->compression_level);
366 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 gst_pngenc_set_property (GObject * object,
374 guint prop_id, const GValue * value, GParamSpec * pspec)
378 pngenc = GST_PNGENC (object);
382 pngenc->snapshot = g_value_get_boolean (value);
384 /* case ARG_NEWMEDIA: */
385 /* pngenc->newmedia = g_value_get_boolean (value); */
387 case ARG_COMPRESSION_LEVEL:
388 pngenc->compression_level = g_value_get_uint (value);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);