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