2
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
26
#include "gstsnapshot.h"
27
#include <gst/video/video.h>
29
#define MAX_HEIGHT 2048
31
/* elementfactory information */
32
static const GstElementDetails snapshot_details =
33
GST_ELEMENT_DETAILS ("Snapshot",
34
"Filter/Editor/Video",
35
"Dump a frame to a png file",
36
"Jeremy SIMON <jsimon13@yahoo.fr>");
38
static GstStaticPadTemplate snapshot_src_factory =
39
GST_STATIC_PAD_TEMPLATE ("src",
42
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
45
static GstStaticPadTemplate snapshot_sink_factory =
46
GST_STATIC_PAD_TEMPLATE ("sink",
49
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB)
52
/* Snapshot signals and args */
67
static GType gst_snapshot_get_type (void);
68
static void gst_snapshot_base_init (gpointer g_class);
69
static void gst_snapshot_class_init (GstSnapshotClass * klass);
70
static void gst_snapshot_init (GstSnapshot * snapshot);
72
static void gst_snapshot_chain (GstPad * pad, GstData * _data);
74
static void gst_snapshot_set_property (GObject * object, guint prop_id,
75
const GValue * value, GParamSpec * pspec);
76
static void gst_snapshot_get_property (GObject * object, guint prop_id,
77
GValue * value, GParamSpec * pspec);
78
static void snapshot_handler (GstElement * element);
80
static GstElementClass *parent_class = NULL;
81
static guint gst_snapshot_signals[LAST_SIGNAL] = { 0 };
85
user_error_fn (png_structp png_ptr, png_const_charp error_msg)
87
g_warning ("%s", error_msg);
91
user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
93
g_warning ("%s", warning_msg);
97
gst_snapshot_get_type (void)
99
static GType snapshot_type = 0;
101
if (!snapshot_type) {
102
static const GTypeInfo snapshot_info = {
103
sizeof (GstSnapshotClass),
104
gst_snapshot_base_init,
106
(GClassInitFunc) gst_snapshot_class_init,
109
sizeof (GstSnapshot),
111
(GInstanceInitFunc) gst_snapshot_init,
115
g_type_register_static (GST_TYPE_ELEMENT, "GstSnapshot", &snapshot_info,
118
return snapshot_type;
122
gst_snapshot_base_init (gpointer g_class)
124
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
126
gst_element_class_add_pad_template (element_class,
127
gst_static_pad_template_get (&snapshot_sink_factory));
128
gst_element_class_add_pad_template (element_class,
129
gst_static_pad_template_get (&snapshot_src_factory));
131
gst_element_class_set_details (element_class, &snapshot_details);
135
gst_snapshot_class_init (GstSnapshotClass * klass)
137
GObjectClass *gobject_class;
138
GstElementClass *gstelement_class;
140
gobject_class = (GObjectClass *) klass;
141
gstelement_class = (GstElementClass *) klass;
143
parent_class = g_type_class_peek_parent (klass);
145
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FRAME,
146
g_param_spec_long ("frame", "frame", "frame",
147
0, G_MAXLONG, 0, G_PARAM_READWRITE));
148
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCATION,
149
g_param_spec_string ("location", "location", "location",
150
0, G_PARAM_READWRITE));
152
gst_snapshot_signals[SNAPSHOT_SIGNAL] =
153
g_signal_new ("snapshot", G_TYPE_FROM_CLASS (klass),
154
G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSnapshotClass, snapshot),
155
NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
157
klass->snapshot = snapshot_handler;
159
gobject_class->set_property = gst_snapshot_set_property;
160
gobject_class->get_property = gst_snapshot_get_property;
164
snapshot_handler (GstElement * element)
166
GstSnapshot *snapshot;
168
snapshot = GST_SNAPSHOT (element);
169
snapshot->snapshot_asked = TRUE;
173
gst_snapshot_sinkconnect (GstPad * pad, const GstCaps * caps)
177
GstStructure *structure;
179
filter = GST_SNAPSHOT (gst_pad_get_parent (pad));
181
structure = gst_caps_get_structure (caps, 0);
182
gst_structure_get_int (structure, "width", &filter->width);
183
gst_structure_get_int (structure, "height", &filter->height);
184
gst_structure_get_double (structure, "framerate", &fps);
185
gst_structure_get_fourcc (structure, "format", &filter->format);
188
gst_pad_try_set_caps (filter->srcpad, caps);
190
return GST_PAD_LINK_OK;
194
gst_snapshot_init (GstSnapshot * snapshot)
197
gst_pad_new_from_static_template (&snapshot_sink_factory, "sink");
198
gst_pad_set_link_function (snapshot->sinkpad, gst_snapshot_sinkconnect);
199
gst_pad_set_chain_function (snapshot->sinkpad, gst_snapshot_chain);
200
gst_element_add_pad (GST_ELEMENT (snapshot), snapshot->sinkpad);
203
gst_pad_new_from_static_template (&snapshot_src_factory, "src");
204
gst_element_add_pad (GST_ELEMENT (snapshot), snapshot->srcpad);
206
snapshot->cur_frame = 0;
207
snapshot->frame = -1;
208
snapshot->snapshot_asked = FALSE;
213
gst_snapshot_chain (GstPad * pad, GstData * _data)
215
GstBuffer *buf = GST_BUFFER (_data);
216
GstSnapshot *snapshot;
220
png_byte *row_pointers[MAX_HEIGHT];
223
g_return_if_fail (pad != NULL);
224
g_return_if_fail (GST_IS_PAD (pad));
225
g_return_if_fail (buf != NULL);
227
snapshot = GST_SNAPSHOT (GST_OBJECT_PARENT (pad));
229
data = GST_BUFFER_DATA (buf);
230
size = GST_BUFFER_SIZE (buf);
232
GST_DEBUG ("snapshot: have buffer of %d\n", GST_BUFFER_SIZE (buf));
234
snapshot->cur_frame++;
235
if (snapshot->cur_frame == snapshot->frame ||
236
snapshot->snapshot_asked == TRUE) {
237
snapshot->snapshot_asked = FALSE;
239
GST_INFO ("dumpfile : %s\n", snapshot->location);
240
fp = fopen (snapshot->location, "wb");
242
g_warning (" Can not open %s\n", snapshot->location);
244
snapshot->png_struct_ptr =
245
png_create_write_struct (PNG_LIBPNG_VER_STRING, (png_voidp) NULL,
246
user_error_fn, user_warning_fn);
247
if (snapshot->png_struct_ptr == NULL)
248
g_warning ("Failed to initialize png structure");
250
snapshot->png_info_ptr =
251
png_create_info_struct (snapshot->png_struct_ptr);
253
if (setjmp (snapshot->png_struct_ptr->jmpbuf))
254
png_destroy_write_struct (&snapshot->png_struct_ptr,
255
&snapshot->png_info_ptr);
257
png_set_filter (snapshot->png_struct_ptr, 0,
258
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
259
png_init_io (snapshot->png_struct_ptr, fp);
260
png_set_compression_level (snapshot->png_struct_ptr, 9);
261
png_set_IHDR (snapshot->png_struct_ptr,
262
snapshot->png_info_ptr,
265
snapshot->to_bpp / 3,
268
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
270
for (i = 0; i < snapshot->height; i++)
271
row_pointers[i] = data + (snapshot->width * i * snapshot->to_bpp / 8);
273
png_write_info (snapshot->png_struct_ptr, snapshot->png_info_ptr);
274
png_write_image (snapshot->png_struct_ptr, row_pointers);
275
png_write_end (snapshot->png_struct_ptr, NULL);
276
png_destroy_info_struct (snapshot->png_struct_ptr,
277
&snapshot->png_info_ptr);
278
png_destroy_write_struct (&snapshot->png_struct_ptr, (png_infopp) NULL);
283
gst_pad_push (snapshot->srcpad, GST_DATA (buf));
287
gst_snapshot_set_property (GObject * object, guint prop_id,
288
const GValue * value, GParamSpec * pspec)
290
GstSnapshot *snapshot;
292
g_return_if_fail (GST_IS_SNAPSHOT (object));
293
snapshot = GST_SNAPSHOT (object);
297
snapshot->location = g_strdup (g_value_get_string (value));
300
snapshot->frame = g_value_get_long (value);
308
gst_snapshot_get_property (GObject * object, guint prop_id, GValue * value,
311
GstSnapshot *snapshot;
313
g_return_if_fail (GST_IS_SNAPSHOT (object));
314
snapshot = GST_SNAPSHOT (object);
318
g_value_set_string (value, snapshot->location);
321
g_value_set_long (value, snapshot->frame);
324
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331
plugin_init (GstPlugin * plugin)
333
if (!gst_element_register (plugin, "snapshot", GST_RANK_NONE,
340
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
343
"Dump a frame to a png file",
344
plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)