close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
[platform/upstream/gstreamer.git] / ext / libpng / gstpngenc.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * Filter:
5  * Copyright (C) 2000 Donald A. Graft
6  *
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.
11  *
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.
16  *
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 #include <string.h>
23 #include <gst/gst.h>
24 #include "gstpngenc.h"
25 #include <gst/video/video.h>
26 #include <zlib.h>
27
28 #define MAX_HEIGHT              4096
29
30
31 static GstElementDetails gst_pngenc_details = {
32   "PNG encoder",
33   "Codec/Encoder/Image",
34   "Encode a video frame to a .png image",
35   "Jeremy SIMON <jsimon13@yahoo.fr>",
36 };
37
38 GST_DEBUG_CATEGORY (pngenc_debug);
39 #define GST_CAT_DEFAULT pngenc_debug
40
41 /* Filter signals and args */
42 enum
43 {
44   /* FILL ME */
45   LAST_SIGNAL
46 };
47
48 #define DEFAULT_SNAPSHOT                TRUE
49 /* #define DEFAULT_NEWMEDIA             FALSE */
50 #define DEFAULT_COMPRESSION_LEVEL       6
51
52 enum
53 {
54   ARG_0,
55   ARG_SNAPSHOT,
56 /*   ARG_NEWMEDIA, */
57   ARG_COMPRESSION_LEVEL
58 };
59
60 static GstStaticPadTemplate pngenc_src_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
62     GST_PAD_SRC,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("image/png, "
65         "width = (int) [ 16, 4096 ], "
66         "height = (int) [ 16, 4096 ], " "framerate = (fraction) [ 0.0, MAX ]")
67     );
68
69 static GstStaticPadTemplate pngenc_sink_template =
70     GST_STATIC_PAD_TEMPLATE ("sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
74     );
75
76 /* static GstElementClass *parent_class = NULL; */
77
78 GST_BOILERPLATE (GstPngEnc, gst_pngenc, GstElement, GST_TYPE_ELEMENT);
79
80 static void gst_pngenc_set_property (GObject * object,
81     guint prop_id, const GValue * value, GParamSpec * pspec);
82 static void gst_pngenc_get_property (GObject * object,
83     guint prop_id, GValue * value, GParamSpec * pspec);
84
85 static GstFlowReturn gst_pngenc_chain (GstPad * pad, GstBuffer * data);
86
87 static void
88 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
89 {
90   g_warning ("%s", error_msg);
91 }
92
93 static void
94 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
95 {
96   g_warning ("%s", warning_msg);
97 }
98
99 static void
100 gst_pngenc_base_init (gpointer g_class)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
103
104   gst_element_class_add_pad_template
105       (element_class, gst_static_pad_template_get (&pngenc_sink_template));
106   gst_element_class_add_pad_template
107       (element_class, gst_static_pad_template_get (&pngenc_src_template));
108   gst_element_class_set_details (element_class, &gst_pngenc_details);
109 }
110
111 static void
112 gst_pngenc_class_init (GstPngEncClass * klass)
113 {
114   GObjectClass *gobject_class;
115   GstElementClass *gstelement_class;
116
117   gobject_class = (GObjectClass *) klass;
118   gstelement_class = (GstElementClass *) klass;
119
120   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
121
122   gobject_class->get_property = gst_pngenc_get_property;
123   gobject_class->set_property = gst_pngenc_set_property;
124
125   g_object_class_install_property (gobject_class, ARG_SNAPSHOT,
126       g_param_spec_boolean ("snapshot", "Snapshot",
127           "Send EOS after encoding a frame, useful for snapshots",
128           DEFAULT_SNAPSHOT, (GParamFlags) G_PARAM_READWRITE));
129
130 /*   g_object_class_install_property (gobject_class, ARG_NEWMEDIA, */
131 /*       g_param_spec_boolean ("newmedia", "newmedia", */
132 /*           "Send new media discontinuity after encoding each frame", */
133 /*           DEFAULT_NEWMEDIA, (GParamFlags) G_PARAM_READWRITE)); */
134
135   g_object_class_install_property
136       (gobject_class, ARG_COMPRESSION_LEVEL,
137       g_param_spec_uint ("compression-level", "compression-level",
138           "PNG compression level",
139           Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
140           DEFAULT_COMPRESSION_LEVEL, (GParamFlags) G_PARAM_READWRITE));
141
142   GST_DEBUG_CATEGORY_INIT (pngenc_debug, "pngenc", 0, "PNG image encoder");
143 }
144
145
146 static gboolean
147 gst_pngenc_setcaps (GstPad * pad, GstCaps * caps)
148 {
149   GstPngEnc *pngenc;
150   const GValue *fps;
151   GstStructure *structure;
152   GstCaps *pcaps;
153   gboolean ret = TRUE;
154   GstPad *opeer;
155
156   pngenc = GST_PNGENC (gst_pad_get_parent (pad));
157
158   structure = gst_caps_get_structure (caps, 0);
159   gst_structure_get_int (structure, "width", &pngenc->width);
160   gst_structure_get_int (structure, "height", &pngenc->height);
161   fps = gst_structure_get_value (structure, "framerate");
162   gst_structure_get_int (structure, "bpp", &pngenc->bpp);
163
164   opeer = gst_pad_get_peer (pngenc->srcpad);
165   if (opeer) {
166     pcaps = gst_caps_new_simple ("image/png",
167         "width", G_TYPE_INT, pngenc->width,
168         "height", G_TYPE_INT, pngenc->height, NULL);
169     structure = gst_caps_get_structure (pcaps, 0);
170     gst_structure_set_value (structure, "framerate", fps);
171
172     if (gst_pad_accept_caps (opeer, pcaps)) {
173       gst_pad_set_caps (pngenc->srcpad, pcaps);
174     } else
175       ret = FALSE;
176     gst_caps_unref (pcaps);
177     gst_object_unref (opeer);
178   }
179
180   gst_object_unref (pngenc);
181
182   return ret;
183 }
184
185 static void
186 gst_pngenc_init (GstPngEnc * pngenc, GstPngEncClass * g_class)
187 {
188   /* sinkpad */
189   pngenc->sinkpad = gst_pad_new_from_template
190       (gst_static_pad_template_get (&pngenc_sink_template), "sink");
191   gst_pad_set_chain_function (pngenc->sinkpad, gst_pngenc_chain);
192   /*   gst_pad_set_link_function (pngenc->sinkpad, gst_pngenc_sinklink); */
193   /*   gst_pad_set_getcaps_function (pngenc->sinkpad, gst_pngenc_sink_getcaps); */
194   gst_pad_set_setcaps_function (pngenc->sinkpad, gst_pngenc_setcaps);
195   gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->sinkpad);
196
197   /* srcpad */
198   pngenc->srcpad = gst_pad_new_from_template
199       (gst_static_pad_template_get (&pngenc_src_template), "src");
200   /*   pngenc->srcpad = gst_pad_new ("src", GST_PAD_SRC); */
201   /*   gst_pad_set_getcaps_function (pngenc->srcpad, gst_pngenc_src_getcaps); */
202   /*   gst_pad_set_setcaps_function (pngenc->srcpad, gst_pngenc_setcaps); */
203   gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->srcpad);
204
205   /* init settings */
206   pngenc->png_struct_ptr = NULL;
207   pngenc->png_info_ptr = NULL;
208
209   pngenc->snapshot = DEFAULT_SNAPSHOT;
210 /*   pngenc->newmedia = FALSE; */
211   pngenc->compression_level = DEFAULT_COMPRESSION_LEVEL;
212 }
213
214 static void
215 user_flush_data (png_structp png_ptr)
216 {
217   GstPngEnc *pngenc;
218
219   pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
220
221   gst_pad_push_event (pngenc->srcpad, gst_event_new_flush_start ());
222   gst_pad_push_event (pngenc->srcpad, gst_event_new_flush_stop ());
223 }
224
225
226 static void
227 user_write_data (png_structp png_ptr, png_bytep data, png_uint_32 length)
228 {
229   GstBuffer *buffer;
230   GstPngEnc *pngenc;
231
232   pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
233
234   buffer = gst_buffer_new ();
235   GST_BUFFER_DATA (buffer) = g_memdup (data, length);
236   GST_BUFFER_SIZE (buffer) = length;
237
238   if (pngenc->buffer_out) {
239     GstBuffer *merge;
240
241     merge = gst_buffer_merge (pngenc->buffer_out, buffer);
242     gst_buffer_unref (buffer);
243     gst_buffer_unref (pngenc->buffer_out);
244     pngenc->buffer_out = merge;
245   } else
246     pngenc->buffer_out = buffer;
247 }
248
249 static GstFlowReturn
250 gst_pngenc_chain (GstPad * pad, GstBuffer * buf)
251 {
252   GstPngEnc *pngenc;
253   gint row_index;
254   png_byte *row_pointers[MAX_HEIGHT];
255   GstFlowReturn ret = GST_FLOW_OK;
256
257   pngenc = GST_PNGENC (gst_pad_get_parent (pad));
258
259   GST_DEBUG_OBJECT (pngenc, "BEGINNING");
260
261   pngenc->buffer_out = NULL;
262
263   /* initialize png struct stuff */
264   pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
265       (png_voidp) NULL, user_error_fn, user_warning_fn);
266   if (pngenc->png_struct_ptr == NULL) {
267     gst_buffer_unref (buf);
268     GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
269         ("Failed to initialize png structure"));
270     ret = GST_FLOW_ERROR;
271     goto done;
272   }
273
274   pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
275   if (!pngenc->png_info_ptr) {
276     gst_buffer_unref (buf);
277     png_destroy_write_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL);
278     GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
279         ("Failed to initialize the png info structure"));
280     ret = GST_FLOW_ERROR;
281     goto done;
282   }
283
284   /* non-0 return is from a longjmp inside of libpng */
285   if (setjmp (pngenc->png_struct_ptr->jmpbuf) != 0) {
286     gst_buffer_unref (buf);
287     png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
288     GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
289         ("returning from longjmp"));
290     ret = GST_FLOW_ERROR;
291     goto done;
292   }
293
294   png_set_filter (pngenc->png_struct_ptr, 0,
295       PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
296   png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level);
297
298   png_set_IHDR (pngenc->png_struct_ptr,
299       pngenc->png_info_ptr,
300       pngenc->width,
301       pngenc->height,
302       8,
303       (pngenc->bpp == 32) ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
304       PNG_INTERLACE_NONE,
305       PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
306
307   png_set_write_fn (pngenc->png_struct_ptr, pngenc,
308       (png_rw_ptr) user_write_data, user_flush_data);
309
310   for (row_index = 0; row_index < pngenc->height; row_index++)
311     row_pointers[row_index] = GST_BUFFER_DATA (buf) +
312         (row_index * pngenc->png_info_ptr->rowbytes);
313
314   png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
315   png_write_image (pngenc->png_struct_ptr, row_pointers);
316   png_write_end (pngenc->png_struct_ptr, NULL);
317
318   user_flush_data (pngenc->png_struct_ptr);
319
320   png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
321   png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);
322   gst_buffer_stamp (pngenc->buffer_out, buf);
323   gst_buffer_unref (buf);
324
325   if ((ret = gst_pad_push (pngenc->srcpad, pngenc->buffer_out)) != GST_FLOW_OK)
326     goto done;
327
328   if (pngenc->snapshot) {
329     GstEvent *event;
330
331     GST_DEBUG_OBJECT (pngenc, "snapshot mode, sending EOS");
332     /* send EOS event, since a frame has been pushed out */
333     event = gst_event_new_eos ();
334
335     ret = gst_pad_push_event (pngenc->srcpad, event);
336
337     if (!(GST_FLOW_IS_FATAL (ret)))
338       ret = GST_FLOW_UNEXPECTED;
339   }
340 /*  else if (pngenc->newmedia) { */
341 /*     /\* send new media discont *\/ */
342 /*     GstEvent *newmedia_event; */
343
344 /*     newmedia_event = */
345 /*         gst_event_new_discontinuous (TRUE, GST_FORMAT_TIME, (gint64) 0, */
346 /*         GST_FORMAT_UNDEFINED); */
347 /*     ret = gst_pad_push (pngenc->srcpad, GST_DATA (newmedia_event)); */
348 /*   } */
349 done:
350   GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);
351
352   gst_object_unref (pngenc);
353   return ret;
354 }
355
356
357 static void
358 gst_pngenc_get_property (GObject * object,
359     guint prop_id, GValue * value, GParamSpec * pspec)
360 {
361   GstPngEnc *pngenc;
362
363   pngenc = GST_PNGENC (object);
364
365   switch (prop_id) {
366     case ARG_SNAPSHOT:
367       g_value_set_boolean (value, pngenc->snapshot);
368       break;
369 /*     case ARG_NEWMEDIA: */
370 /*       g_value_set_boolean (value, pngenc->newmedia); */
371 /*       break; */
372     case ARG_COMPRESSION_LEVEL:
373       g_value_set_uint (value, pngenc->compression_level);
374       break;
375     default:
376       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377       break;
378   }
379 }
380
381
382 static void
383 gst_pngenc_set_property (GObject * object,
384     guint prop_id, const GValue * value, GParamSpec * pspec)
385 {
386   GstPngEnc *pngenc;
387
388   pngenc = GST_PNGENC (object);
389
390   switch (prop_id) {
391     case ARG_SNAPSHOT:
392       pngenc->snapshot = g_value_get_boolean (value);
393       break;
394 /*     case ARG_NEWMEDIA: */
395 /*       pngenc->newmedia = g_value_get_boolean (value); */
396 /*       break; */
397     case ARG_COMPRESSION_LEVEL:
398       pngenc->compression_level = g_value_get_uint (value);
399       break;
400     default:
401       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
402       break;
403   }
404 }