db2748cf74b10bbbe8a3960a2eb1b66e7d791be1
[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  * Copyright (C) 2012 Collabora Ltd.
8  *      Author : Edward Hervey <edward@collabora.com>
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  */
21 /**
22  * SECTION:element-pngenc
23  *
24  * Encodes png images.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 #include <string.h>
31 #include <gst/gst.h>
32 #include "gstpngenc.h"
33 #include <gst/video/video.h>
34 #include <gst/video/gstvideometa.h>
35 #include <zlib.h>
36
37 GST_DEBUG_CATEGORY_STATIC (pngenc_debug);
38 #define GST_CAT_DEFAULT pngenc_debug
39
40 /* Filter signals and args */
41 enum
42 {
43   /* FILL ME */
44   LAST_SIGNAL
45 };
46
47 #define DEFAULT_SNAPSHOT                FALSE
48 #define DEFAULT_COMPRESSION_LEVEL       6
49
50 enum
51 {
52   ARG_0,
53   ARG_SNAPSHOT,
54   ARG_COMPRESSION_LEVEL
55 };
56
57 static GstStaticPadTemplate pngenc_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("image/png, "
62         "width = (int) [ 16, 1000000 ], "
63         "height = (int) [ 16, 1000000 ], " "framerate = " GST_VIDEO_FPS_RANGE)
64     );
65
66 static GstStaticPadTemplate pngenc_sink_template =
67 GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBA, RGB, GRAY8, GRAY16_BE }"))
71     );
72
73 #define parent_class gst_pngenc_parent_class
74 G_DEFINE_TYPE (GstPngEnc, gst_pngenc, GST_TYPE_VIDEO_ENCODER);
75
76 static void gst_pngenc_set_property (GObject * object,
77     guint prop_id, const GValue * value, GParamSpec * pspec);
78 static void gst_pngenc_get_property (GObject * object,
79     guint prop_id, GValue * value, GParamSpec * pspec);
80
81 static GstFlowReturn gst_pngenc_handle_frame (GstVideoEncoder * encoder,
82     GstVideoCodecFrame * frame);
83 static gboolean gst_pngenc_set_format (GstVideoEncoder * encoder,
84     GstVideoCodecState * state);
85 static gboolean gst_pngenc_propose_allocation (GstVideoEncoder * encoder,
86     GstQuery * query);
87
88 static void gst_pngenc_finalize (GObject * object);
89
90 static void
91 user_error_fn (png_structp png_ptr, png_const_charp error_msg)
92 {
93   g_warning ("%s", error_msg);
94 }
95
96 static void
97 user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
98 {
99   g_warning ("%s", warning_msg);
100 }
101
102 static void
103 gst_pngenc_class_init (GstPngEncClass * klass)
104 {
105   GObjectClass *gobject_class;
106   GstElementClass *element_class;
107   GstVideoEncoderClass *venc_class;
108
109   gobject_class = (GObjectClass *) klass;
110   element_class = (GstElementClass *) klass;
111   venc_class = (GstVideoEncoderClass *) klass;
112
113   parent_class = g_type_class_peek_parent (klass);
114
115   gobject_class->get_property = gst_pngenc_get_property;
116   gobject_class->set_property = gst_pngenc_set_property;
117
118   g_object_class_install_property (gobject_class, ARG_SNAPSHOT,
119       g_param_spec_boolean ("snapshot", "Snapshot",
120           "Send EOS after encoding a frame, useful for snapshots",
121           DEFAULT_SNAPSHOT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122
123   g_object_class_install_property (gobject_class, ARG_COMPRESSION_LEVEL,
124       g_param_spec_uint ("compression-level", "compression-level",
125           "PNG compression level",
126           Z_NO_COMPRESSION, Z_BEST_COMPRESSION,
127           DEFAULT_COMPRESSION_LEVEL,
128           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129
130   gst_element_class_add_pad_template
131       (element_class, gst_static_pad_template_get (&pngenc_sink_template));
132   gst_element_class_add_pad_template
133       (element_class, gst_static_pad_template_get (&pngenc_src_template));
134   gst_element_class_set_metadata (element_class, "PNG image encoder",
135       "Codec/Encoder/Image",
136       "Encode a video frame to a .png image",
137       "Jeremy SIMON <jsimon13@yahoo.fr>");
138
139   venc_class->set_format = gst_pngenc_set_format;
140   venc_class->handle_frame = gst_pngenc_handle_frame;
141   venc_class->propose_allocation = gst_pngenc_propose_allocation;
142   gobject_class->finalize = gst_pngenc_finalize;
143
144   GST_DEBUG_CATEGORY_INIT (pngenc_debug, "pngenc", 0, "PNG image encoder");
145 }
146
147
148 static gboolean
149 gst_pngenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state)
150 {
151   GstPngEnc *pngenc;
152   gboolean ret = TRUE;
153   GstVideoInfo *info;
154   GstVideoCodecState *output_state;
155
156   pngenc = GST_PNGENC (encoder);
157   info = &state->info;
158
159   switch (GST_VIDEO_INFO_FORMAT (info)) {
160     case GST_VIDEO_FORMAT_RGBA:
161       pngenc->png_color_type = PNG_COLOR_TYPE_RGBA;
162       break;
163     case GST_VIDEO_FORMAT_RGB:
164       pngenc->png_color_type = PNG_COLOR_TYPE_RGB;
165       break;
166     case GST_VIDEO_FORMAT_GRAY8:
167     case GST_VIDEO_FORMAT_GRAY16_BE:
168       pngenc->png_color_type = PNG_COLOR_TYPE_GRAY;
169       break;
170     default:
171       ret = FALSE;
172       goto done;
173   }
174
175   switch (GST_VIDEO_INFO_FORMAT (info)) {
176     case GST_VIDEO_FORMAT_GRAY16_BE:
177       pngenc->depth = 16;
178       break;
179     default:                   /* GST_VIDEO_FORMAT_RGB and GST_VIDEO_FORMAT_GRAY8 */
180       pngenc->depth = 8;
181       break;
182   }
183
184   if (pngenc->input_state)
185     gst_video_codec_state_unref (pngenc->input_state);
186   pngenc->input_state = gst_video_codec_state_ref (state);
187
188   output_state =
189       gst_video_encoder_set_output_state (encoder,
190       gst_caps_new_empty_simple ("image/png"), state);
191   gst_video_codec_state_unref (output_state);
192
193 done:
194
195   return ret;
196 }
197
198 static void
199 gst_pngenc_init (GstPngEnc * pngenc)
200 {
201   /* init settings */
202   pngenc->png_struct_ptr = NULL;
203   pngenc->png_info_ptr = NULL;
204
205   pngenc->snapshot = DEFAULT_SNAPSHOT;
206   pngenc->compression_level = DEFAULT_COMPRESSION_LEVEL;
207 }
208
209 static void
210 gst_pngenc_finalize (GObject * object)
211 {
212   GstPngEnc *pngenc = GST_PNGENC (object);
213
214   if (pngenc->input_state)
215     gst_video_codec_state_unref (pngenc->input_state);
216
217   G_OBJECT_CLASS (parent_class)->finalize (object);
218 }
219
220 static void
221 user_flush_data (png_structp png_ptr G_GNUC_UNUSED)
222 {
223 }
224
225 static void
226 user_write_data (png_structp png_ptr, png_bytep data, png_uint_32 length)
227 {
228   GstPngEnc *pngenc;
229   GstMemory *mem;
230   GstMapInfo minfo;
231
232   pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
233
234   mem = gst_allocator_alloc (NULL, length, NULL);
235   if (!mem) {
236     GST_ERROR_OBJECT (pngenc, "Failed to allocate memory");
237     png_error (png_ptr, "Failed to allocate memory");
238
239     /* never reached */
240     return;
241   }
242
243   if (!gst_memory_map (mem, &minfo, GST_MAP_WRITE)) {
244     GST_ERROR_OBJECT (pngenc, "Failed to map memory");
245     gst_memory_unref (mem);
246
247     png_error (png_ptr, "Failed to map memory");
248
249     /* never reached */
250     return;
251   }
252
253   memcpy (minfo.data, data, length);
254   gst_memory_unmap (mem, &minfo);
255
256   gst_buffer_append_memory (pngenc->buffer_out, mem);
257 }
258
259 static GstFlowReturn
260 gst_pngenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
261 {
262   GstPngEnc *pngenc;
263   gint row_index;
264   png_byte **row_pointers;
265   GstFlowReturn ret = GST_FLOW_OK;
266   GstVideoInfo *info;
267   GstVideoFrame vframe;
268
269   pngenc = GST_PNGENC (encoder);
270   info = &pngenc->input_state->info;
271
272   GST_DEBUG_OBJECT (pngenc, "BEGINNING");
273
274   /* initialize png struct stuff */
275   pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
276       (png_voidp) NULL, user_error_fn, user_warning_fn);
277   if (pngenc->png_struct_ptr == NULL)
278     goto struct_init_fail;
279
280   pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
281   if (!pngenc->png_info_ptr)
282     goto png_info_fail;
283
284   /* non-0 return is from a longjmp inside of libpng */
285   if (setjmp (png_jmpbuf (pngenc->png_struct_ptr)) != 0)
286     goto longjmp_fail;
287
288   png_set_filter (pngenc->png_struct_ptr, 0,
289       PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
290   png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level);
291
292   png_set_IHDR (pngenc->png_struct_ptr,
293       pngenc->png_info_ptr,
294       GST_VIDEO_INFO_WIDTH (info),
295       GST_VIDEO_INFO_HEIGHT (info),
296       pngenc->depth,
297       pngenc->png_color_type,
298       PNG_INTERLACE_NONE,
299       PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
300
301   png_set_write_fn (pngenc->png_struct_ptr, pngenc,
302       (png_rw_ptr) user_write_data, user_flush_data);
303
304   row_pointers = g_new (png_byte *, GST_VIDEO_INFO_HEIGHT (info));
305   if (!gst_video_frame_map (&vframe, &pngenc->input_state->info,
306           frame->input_buffer, GST_MAP_READ)) {
307     GST_ELEMENT_ERROR (pngenc, STREAM, FORMAT, (NULL),
308         ("Failed to map video frame, caps problem?"));
309     ret = GST_FLOW_ERROR;
310     goto done;
311   }
312
313   for (row_index = 0; row_index < GST_VIDEO_INFO_HEIGHT (info); row_index++) {
314     row_pointers[row_index] = GST_VIDEO_FRAME_COMP_DATA (&vframe, 0) +
315         (row_index * GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 0));
316   }
317
318   /* allocate the output buffer */
319   pngenc->buffer_out = gst_buffer_new ();
320
321   png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
322   png_write_image (pngenc->png_struct_ptr, row_pointers);
323   png_write_end (pngenc->png_struct_ptr, NULL);
324
325   g_free (row_pointers);
326
327   png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
328   png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);
329
330   /* Set final size and store */
331   frame->output_buffer = pngenc->buffer_out;
332
333   pngenc->buffer_out = NULL;
334
335   if ((ret = gst_video_encoder_finish_frame (encoder, frame)) != GST_FLOW_OK)
336     goto done;
337
338   if (pngenc->snapshot)
339     ret = GST_FLOW_EOS;
340
341 done:
342   GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);
343
344   return ret;
345
346   /* ERRORS */
347 struct_init_fail:
348   {
349     GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
350         ("Failed to initialize png structure"));
351     return GST_FLOW_ERROR;
352   }
353
354 png_info_fail:
355   {
356     png_destroy_write_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL);
357     GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
358         ("Failed to initialize the png info structure"));
359     return GST_FLOW_ERROR;
360   }
361
362 longjmp_fail:
363   {
364     png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
365     GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
366         ("returning from longjmp"));
367     return GST_FLOW_ERROR;
368   }
369 }
370
371 static gboolean
372 gst_pngenc_propose_allocation (GstVideoEncoder * encoder, GstQuery * query)
373 {
374   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
375
376   return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
377       query);
378 }
379
380 static void
381 gst_pngenc_get_property (GObject * object,
382     guint prop_id, GValue * value, GParamSpec * pspec)
383 {
384   GstPngEnc *pngenc;
385
386   pngenc = GST_PNGENC (object);
387
388   switch (prop_id) {
389     case ARG_SNAPSHOT:
390       g_value_set_boolean (value, pngenc->snapshot);
391       break;
392     case ARG_COMPRESSION_LEVEL:
393       g_value_set_uint (value, pngenc->compression_level);
394       break;
395     default:
396       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
397       break;
398   }
399 }
400
401
402 static void
403 gst_pngenc_set_property (GObject * object,
404     guint prop_id, const GValue * value, GParamSpec * pspec)
405 {
406   GstPngEnc *pngenc;
407
408   pngenc = GST_PNGENC (object);
409
410   switch (prop_id) {
411     case ARG_SNAPSHOT:
412       pngenc->snapshot = g_value_get_boolean (value);
413       break;
414     case ARG_COMPRESSION_LEVEL:
415       pngenc->compression_level = g_value_get_uint (value);
416       break;
417     default:
418       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
419       break;
420   }
421 }