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