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