gstpngenc.c 7.62 KB
Newer Older
1
2
3
4
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * Filter:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
5
 * Copyright (C) 2000 Donald A. Graft
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

19
20
21
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
22
23
#include <string.h>
#include <gst/gst.h>
24
#include "gstpngenc.h"
Iain Holmes's avatar
Iain Holmes committed
25
#include <gst/video/video.h>
26

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
27
#define MAX_HEIGHT		4096
28
29
30


GstElementDetails gst_pngenc_details = {
31
  "PNG encoder",
32
  "Codec/Encoder/Image",
Benjamin Otte's avatar
Benjamin Otte committed
33
  "Encode a video frame to a .png image",
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  "Jeremy SIMON <jsimon13@yahoo.fr>",
};


/* Filter signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
  ARG_0
};

Iain Holmes's avatar
Iain Holmes committed
50
static void     gst_pngenc_base_init    (gpointer g_class);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
51
52
static void	gst_pngenc_class_init	(GstPngEncClass *klass);
static void	gst_pngenc_init		(GstPngEnc *pngenc);
53

54
static void	gst_pngenc_chain	(GstPad *pad, GstData *_data);
55
56
57
58

static GstElementClass *parent_class = NULL;


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
59
static void user_error_fn (png_structp png_ptr, png_const_charp error_msg)
60
61
62
63
{
  g_warning("%s", error_msg);
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
64
static void user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
65
66
67
68
69
70
71
72
73
74
75
{
  g_warning("%s", warning_msg);
}


GType gst_pngenc_get_type (void)
{
  static GType pngenc_type = 0;

  if (!pngenc_type) {
    static const GTypeInfo pngenc_info = {
Iain Holmes's avatar
Iain Holmes committed
76
77
      sizeof (GstPngEncClass),
      gst_pngenc_base_init,
78
79
80
81
82
83
84
85
86
      NULL,
      (GClassInitFunc) gst_pngenc_class_init,
      NULL,
      NULL,
      sizeof (GstPngEnc),
      0,
      (GInstanceInitFunc) gst_pngenc_init,
    };

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
87
88
    pngenc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngEnc",
		                          &pngenc_info, 0);
89
90
91
92
  }
  return pngenc_type;
}

Iain Holmes's avatar
Iain Holmes committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
static GstCaps*
png_caps_factory (void)
{
  return gst_caps_new ( "png_png",
			"video/x-png",
			gst_props_new (
			  "width",     GST_PROPS_INT_RANGE (16, 4096),
			  "height",    GST_PROPS_INT_RANGE (16, 4096),
			  "framerate", GST_PROPS_FLOAT_RANGE (0, G_MAXFLOAT),
			  NULL));
}


static GstCaps*
raw_caps_factory (void)
{ 
  return gst_caps_new ( "png_raw", 
  			"video/x-raw-rgb",
			 GST_VIDEO_RGB_PAD_TEMPLATE_PROPS_24
	              );
}

static void
gst_pngenc_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  GstCaps *raw_caps, *png_caps;
  
  raw_caps = raw_caps_factory ();
  png_caps = png_caps_factory ();

  pngenc_sink_template = gst_pad_template_new ("sink", GST_PAD_SINK,
					       GST_PAD_ALWAYS,
					       raw_caps, NULL);
  
  pngenc_src_template = gst_pad_template_new ("src", GST_PAD_SRC,
					      GST_PAD_ALWAYS,
					      png_caps, NULL);
  
  gst_element_class_add_pad_template (element_class, pngenc_sink_template);
  gst_element_class_add_pad_template (element_class, pngenc_src_template);
  gst_element_class_set_details (element_class, &gst_pngenc_details);
}

137
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
138
gst_pngenc_class_init (GstPngEncClass *klass)
139
140
141
142
143
144
145
146
147
148
149
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
}


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
150
static GstPadLinkReturn
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
151
gst_pngenc_sinklink (GstPad *pad, GstCaps *caps)
152
153
{
  GstPngEnc *pngenc;
154
  gfloat fps;
155
156
157
158

  pngenc = GST_PNGENC (gst_pad_get_parent (pad));

  if (!GST_CAPS_IS_FIXED (caps))
159
    return GST_PAD_LINK_DELAYED;
160
161
162

  gst_caps_get_int (caps, "width", &pngenc->width);
  gst_caps_get_int (caps, "height", &pngenc->height);
163
  gst_caps_get_float (caps, "framerate", &fps);
164
165
  gst_caps_get_int (caps, "bpp", &pngenc->bpp);

166
167
168
169
170
171
  caps = GST_CAPS_NEW ("png_src",
		       "video/x-png",
			 "framerate", GST_PROPS_FLOAT (fps),
			 "width",     GST_PROPS_INT (pngenc->width),
			 "height",    GST_PROPS_INT (pngenc->height));

172
173
174
175
176
177
  return gst_pad_try_set_caps (pngenc->srcpad, caps);
}

static void
gst_pngenc_init (GstPngEnc * pngenc)
{
178
  pngenc->sinkpad = gst_pad_new_from_template (pngenc_sink_template, "sink");
179
  gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->sinkpad);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
180
181

  pngenc->srcpad = gst_pad_new ("src", GST_PAD_SRC);
182
183
184
  gst_element_add_pad (GST_ELEMENT (pngenc), pngenc->srcpad);

  gst_pad_set_chain_function (pngenc->sinkpad, gst_pngenc_chain);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
185
  gst_pad_set_link_function (pngenc->sinkpad, gst_pngenc_sinklink);
186

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
187
188
  pngenc->png_struct_ptr = NULL;
  pngenc->png_info_ptr = NULL;
189
190
191

}

192
void user_flush_data (png_structp png_ptr)
193
194
195
{
GstPngEnc *pngenc;

196
  pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
197

198
  gst_pad_push (pngenc->srcpad, GST_DATA (gst_event_new (GST_EVENT_FLUSH)));
199
200
}

201
202

void user_write_data (png_structp png_ptr, png_bytep data, png_uint_32 length)
203
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
204
205
  GstBuffer *buffer;
  GstPngEnc *pngenc;
206

207
  pngenc = (GstPngEnc *) png_get_io_ptr (png_ptr);
208
209

  buffer = gst_buffer_new();
210
  GST_BUFFER_DATA (buffer) = g_memdup (data, length);
211
212
  GST_BUFFER_SIZE (buffer) = length;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
213
214
  if (pngenc->buffer_out)
  {
215
    pngenc->buffer_out = gst_buffer_merge (pngenc->buffer_out, buffer);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
216
    gst_buffer_unref (buffer);
217
218
219
220
221
222
  }
  else
    pngenc->buffer_out = buffer;
}

static void
223
gst_pngenc_chain (GstPad *pad, GstData *_data)
224
{
225
  GstBuffer *buf = GST_BUFFER (_data);
226
  GstPngEnc *pngenc;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
227
228
229
230
231
  gint row_index;
  png_byte *row_pointers[MAX_HEIGHT];
  GstEvent *event;

  pngenc = GST_PNGENC (gst_pad_get_parent (pad));
232

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
233
234
  pngenc->buffer_out = NULL;
  if (!GST_PAD_IS_USABLE (pngenc->srcpad))
235
236
  {
    gst_buffer_unref (buf);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
237
    return;
238
239
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
240
241
242
243
244
245
  /* initialize png struct stuff */
  pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
		           (png_voidp) NULL, user_error_fn, user_warning_fn);
  /* FIXME: better error handling */
  if (pngenc->png_struct_ptr == NULL)
     g_warning ("Failed to initialize png structure");
246

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
247
248
249
250
251
252
253
254
255
256
  pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
  if (!pngenc->png_info_ptr)
  {
    png_destroy_read_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL,
		             (png_infopp) NULL);
  }

  /* non-0 return is from a longjmp inside of libpng */
  if (setjmp (pngenc->png_struct_ptr->jmpbuf) != 0)
  {
257
    GST_DEBUG ("returning from longjmp");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
258
259
260
    png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
    return;
  }
261

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
262
263
  png_set_filter (pngenc->png_struct_ptr, 0,
		  PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
264
  png_set_compression_level (pngenc->png_struct_ptr, 9);
265
266
267
268
269
270

  png_set_IHDR(
    pngenc->png_struct_ptr,
    pngenc->png_info_ptr,
    pngenc->width,
    pngenc->height,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
271
    pngenc->bpp / 3,
272
273
274
275
276
277
    PNG_COLOR_TYPE_RGB,
    PNG_INTERLACE_NONE,
    PNG_COMPRESSION_TYPE_DEFAULT,
    PNG_FILTER_TYPE_DEFAULT
  );

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
278
279
280
281
282
283
  png_set_write_fn (pngenc->png_struct_ptr, pngenc,
		    (png_rw_ptr) user_write_data, user_flush_data);

  for (row_index = 0; row_index < pngenc->height; row_index++)
    row_pointers[row_index] = GST_BUFFER_DATA (buf) +
	                      (pngenc->width * row_index * pngenc->bpp / 8);
284

285
286
287
  png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
  png_write_image (pngenc->png_struct_ptr, row_pointers);
  png_write_end (pngenc->png_struct_ptr, NULL);
288

289
  user_flush_data (pngenc->png_struct_ptr);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
290

291
  png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
292
  png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);
293

294
  gst_pad_push (pngenc->srcpad, GST_DATA (pngenc->buffer_out));
295
296
297

  /* send EOS event, since a frame has been pushed out */
  event = gst_event_new (GST_EVENT_EOS);
298
  gst_pad_push (pngenc->srcpad, GST_DATA (event));
299
300
301

  gst_buffer_unref (buf);
}