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