2010-01-22 02:19:24 +00:00
|
|
|
/* GStreamer PNM encoder
|
|
|
|
* Copyright (C) 2009 Lutz Mueller <lutz@users.sourceforge.net>
|
2009-09-10 06:23:22 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-09-10 06:23:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-pnmenc
|
|
|
|
*
|
2009-09-16 06:22:19 +00:00
|
|
|
* Encodes pnm images. This plugin supports both raw and ASCII encoding.
|
|
|
|
* To enable ASCII encoding, set the parameter ascii to TRUE. If you omit
|
|
|
|
* the parameter or set it to FALSE, the output will be raw encoded.
|
2009-09-10 06:23:22 +00:00
|
|
|
*
|
2010-10-19 12:06:36 +00:00
|
|
|
* <refsect>
|
2009-09-10 06:23:22 +00:00
|
|
|
* <title>Example launch line</title>
|
|
|
|
* |[
|
2012-09-14 14:45:34 +00:00
|
|
|
* gst-launch videotestsrc num_buffers=1 ! videoconvert ! "video/x-raw,format=GRAY8" ! pnmenc ascii=true ! filesink location=test.pnm
|
2009-09-16 06:22:19 +00:00
|
|
|
* ]| The above pipeline writes a test pnm file (ASCII encoding).
|
2009-09-10 06:23:22 +00:00
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
2012-09-12 22:34:03 +00:00
|
|
|
/*
|
|
|
|
* FIXME: Port to GstVideoEncoder
|
|
|
|
*/
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstpnmenc.h"
|
|
|
|
#include "gstpnmutils.h"
|
|
|
|
|
|
|
|
#include <gst/gstutils.h>
|
2009-09-10 06:48:12 +00:00
|
|
|
#include <gst/video/video.h>
|
2009-09-10 06:23:22 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-09-16 06:22:19 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
GST_PNMENC_PROP_0,
|
|
|
|
GST_PNMENC_PROP_ASCII
|
|
|
|
/* Add here. */
|
|
|
|
};
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
static GstStaticPadTemplate sink_pad_template =
|
2009-09-13 17:13:24 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
2012-09-12 22:34:03 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB") "; "
|
|
|
|
GST_VIDEO_CAPS_MAKE ("GRAY8")));
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate src_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS (MIME_ALL));
|
|
|
|
|
2012-09-12 22:34:03 +00:00
|
|
|
G_DEFINE_TYPE (GstPnmenc, gst_pnmenc, GST_TYPE_ELEMENT);
|
2010-03-22 11:02:16 +00:00
|
|
|
|
2009-09-16 06:22:19 +00:00
|
|
|
static void
|
|
|
|
gst_pnmenc_set_property (GObject * object, guint prop_id, const GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPnmenc *s = GST_PNMENC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case GST_PNMENC_PROP_ASCII:
|
|
|
|
if (g_value_get_boolean (value))
|
|
|
|
s->info.encoding = GST_PNM_ENCODING_ASCII;
|
|
|
|
else
|
|
|
|
s->info.encoding = GST_PNM_ENCODING_RAW;
|
|
|
|
s->info.fields |= GST_PNM_INFO_FIELDS_ENCODING;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pnmenc_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstPnmenc *s = GST_PNMENC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case GST_PNMENC_PROP_ASCII:
|
|
|
|
g_value_set_boolean (value, s->info.encoding == GST_PNM_ENCODING_ASCII);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
static GstFlowReturn
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_pnmenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2009-09-10 06:23:22 +00:00
|
|
|
{
|
2012-09-12 22:34:03 +00:00
|
|
|
GstPnmenc *s = GST_PNMENC (parent);
|
2009-09-10 06:23:22 +00:00
|
|
|
GstFlowReturn r;
|
|
|
|
gchar *header;
|
|
|
|
GstBuffer *out;
|
|
|
|
|
2014-05-06 08:48:58 +00:00
|
|
|
if (s->info.width == 0 || s->info.height == 0 || s->info.fields == 0)
|
|
|
|
goto not_negotiated;
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
/* Assumption: One buffer, one image. That is, always first write header. */
|
|
|
|
header = g_strdup_printf ("P%i\n%i %i\n%i\n",
|
2009-09-16 06:22:19 +00:00
|
|
|
s->info.type + 3 * (1 - s->info.encoding), s->info.width, s->info.height,
|
|
|
|
s->info.max);
|
2012-09-12 22:34:03 +00:00
|
|
|
out = gst_buffer_new_wrapped (header, strlen (header));
|
2009-09-13 17:13:24 +00:00
|
|
|
if ((r = gst_pad_push (s->src, out)) != GST_FLOW_OK)
|
2009-09-10 06:48:12 +00:00
|
|
|
goto out;
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2009-09-13 17:33:57 +00:00
|
|
|
/* Need to convert from GStreamer rowstride to PNM rowstride */
|
2009-09-13 17:39:59 +00:00
|
|
|
if (s->info.width % 4 != 0) {
|
|
|
|
guint i_rowstride;
|
|
|
|
guint o_rowstride;
|
|
|
|
GstBuffer *obuf;
|
2009-09-13 17:33:57 +00:00
|
|
|
guint i;
|
2012-09-12 22:34:03 +00:00
|
|
|
GstMapInfo imap, omap;
|
2009-09-13 17:33:57 +00:00
|
|
|
|
2009-09-16 06:22:19 +00:00
|
|
|
if (s->info.type == GST_PNM_TYPE_PIXMAP) {
|
2009-09-13 17:39:59 +00:00
|
|
|
o_rowstride = 3 * s->info.width;
|
|
|
|
i_rowstride = GST_ROUND_UP_4 (o_rowstride);
|
|
|
|
} else {
|
|
|
|
o_rowstride = s->info.width;
|
|
|
|
i_rowstride = GST_ROUND_UP_4 (o_rowstride);
|
|
|
|
}
|
|
|
|
|
|
|
|
obuf = gst_buffer_new_and_alloc (o_rowstride * s->info.height);
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_buffer_map (obuf, &omap, GST_MAP_WRITE);
|
|
|
|
gst_buffer_map (buf, &imap, GST_MAP_READ);
|
2009-09-13 17:33:57 +00:00
|
|
|
for (i = 0; i < s->info.height; i++)
|
2012-09-12 22:34:03 +00:00
|
|
|
memcpy (omap.data + o_rowstride * i, imap.data + i_rowstride * i,
|
|
|
|
o_rowstride);
|
|
|
|
gst_buffer_unmap (buf, &imap);
|
|
|
|
gst_buffer_unmap (obuf, &omap);
|
2009-09-13 17:33:57 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
buf = obuf;
|
|
|
|
} else {
|
|
|
|
/* Pass through the data. */
|
2012-09-12 22:34:03 +00:00
|
|
|
buf = gst_buffer_make_writable (buf);
|
2009-09-13 17:33:57 +00:00
|
|
|
}
|
2009-09-16 06:22:19 +00:00
|
|
|
|
|
|
|
/* We might need to convert to ASCII... */
|
|
|
|
if (s->info.encoding == GST_PNM_ENCODING_ASCII) {
|
|
|
|
GstBuffer *obuf;
|
|
|
|
guint i, o;
|
2012-09-12 22:34:03 +00:00
|
|
|
GstMapInfo imap, omap;
|
2009-09-16 06:22:19 +00:00
|
|
|
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_buffer_map (buf, &imap, GST_MAP_READ);
|
|
|
|
obuf = gst_buffer_new_and_alloc (imap.size * (4 + 1 / 20.));
|
|
|
|
gst_buffer_map (obuf, &omap, GST_MAP_WRITE);
|
|
|
|
for (i = o = 0; i < imap.size; i++) {
|
|
|
|
g_snprintf ((char *) omap.data + o, 4, "%3i", imap.data[i]);
|
2009-09-16 06:22:19 +00:00
|
|
|
o += 3;
|
2012-09-12 22:34:03 +00:00
|
|
|
omap.data[o++] = ' ';
|
2009-09-17 07:38:02 +00:00
|
|
|
if (!((i + 1) % 20))
|
2012-09-12 22:34:03 +00:00
|
|
|
omap.data[o++] = '\n';
|
2009-09-16 06:22:19 +00:00
|
|
|
}
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_buffer_unmap (buf, &imap);
|
|
|
|
gst_buffer_unmap (obuf, &omap);
|
2009-09-16 06:22:19 +00:00
|
|
|
gst_buffer_unref (buf);
|
|
|
|
buf = obuf;
|
|
|
|
}
|
|
|
|
|
2009-09-13 17:13:24 +00:00
|
|
|
r = gst_pad_push (s->src, buf);
|
2009-09-10 06:48:12 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
return r;
|
2014-05-06 08:48:58 +00:00
|
|
|
|
|
|
|
not_negotiated:
|
|
|
|
{
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_pnmenc_setcaps (GstPnmenc * s, GstCaps * caps)
|
2009-09-10 06:23:22 +00:00
|
|
|
{
|
2012-09-12 22:34:03 +00:00
|
|
|
gboolean r;
|
2009-09-13 17:13:24 +00:00
|
|
|
GstCaps *srccaps;
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2009-09-13 17:13:24 +00:00
|
|
|
s->info.max = 255;
|
|
|
|
s->info.fields = GST_PNM_INFO_FIELDS_MAX;
|
|
|
|
|
2012-09-12 22:34:03 +00:00
|
|
|
if (!gst_video_info_from_caps (&s->vinfo, caps))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (GST_VIDEO_INFO_IS_RGB (&s->vinfo)) {
|
2009-09-16 06:22:19 +00:00
|
|
|
s->info.type = GST_PNM_TYPE_PIXMAP;
|
2009-09-13 17:13:24 +00:00
|
|
|
srccaps = gst_caps_from_string (MIME_PM);
|
2012-09-12 22:34:03 +00:00
|
|
|
} else if (GST_VIDEO_INFO_IS_GRAY (&s->vinfo)) {
|
2009-09-16 06:22:19 +00:00
|
|
|
s->info.type = GST_PNM_TYPE_GRAYMAP;
|
2009-09-13 17:13:24 +00:00
|
|
|
srccaps = gst_caps_from_string (MIME_GM);
|
|
|
|
} else {
|
2012-09-12 22:34:03 +00:00
|
|
|
return FALSE;
|
2009-09-13 17:13:24 +00:00
|
|
|
}
|
2012-09-12 22:34:03 +00:00
|
|
|
r = gst_pad_set_caps (s->src, srccaps);
|
2009-09-13 17:13:24 +00:00
|
|
|
gst_caps_unref (srccaps);
|
|
|
|
s->info.fields |= GST_PNM_INFO_FIELDS_TYPE;
|
|
|
|
|
|
|
|
/* Remember width and height of the input data. */
|
2012-09-12 22:34:03 +00:00
|
|
|
s->info.width = GST_VIDEO_INFO_WIDTH (&s->vinfo);
|
|
|
|
s->info.height = GST_VIDEO_INFO_HEIGHT (&s->vinfo);
|
2009-09-13 17:13:24 +00:00
|
|
|
s->info.fields |= GST_PNM_INFO_FIELDS_WIDTH | GST_PNM_INFO_FIELDS_HEIGHT;
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2012-09-12 22:34:03 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_pnmenc_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
|
|
|
{
|
|
|
|
GstPnmenc *s = GST_PNMENC (parent);
|
|
|
|
gboolean r = FALSE;
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
r = gst_pnmenc_setcaps (s, caps);
|
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
r = gst_pad_event_default (pad, parent, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-09-13 17:13:24 +00:00
|
|
|
return r;
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_pnmenc_init (GstPnmenc * s)
|
2009-09-10 06:23:22 +00:00
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
|
2010-11-28 04:13:38 +00:00
|
|
|
pad = gst_pad_new_from_static_template (&sink_pad_template, "sink");
|
2009-09-10 06:23:22 +00:00
|
|
|
gst_pad_set_chain_function (pad, gst_pnmenc_chain);
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_pad_set_event_function (pad, gst_pnmenc_sink_event);
|
2009-09-10 06:48:12 +00:00
|
|
|
gst_pad_use_fixed_caps (pad);
|
2009-09-10 06:23:22 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (s), pad);
|
|
|
|
|
2010-11-28 04:13:38 +00:00
|
|
|
s->src = gst_pad_new_from_static_template (&src_pad_template, "src");
|
2009-09-13 17:13:24 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (s), s->src);
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-09-12 22:34:03 +00:00
|
|
|
gst_pnmenc_class_init (GstPnmencClass * klass)
|
2009-09-10 06:23:22 +00:00
|
|
|
{
|
2012-09-12 22:34:03 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
2009-09-10 06:23:22 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sink_pad_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&src_pad_template));
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class, "PNM image encoder",
|
2010-01-22 02:22:56 +00:00
|
|
|
"Codec/Encoder/Image",
|
|
|
|
"Encodes images into portable pixmap or graymap (PNM) format",
|
|
|
|
"Lutz Mueller <lutz@users.sourceforge.net>");
|
2009-09-16 06:22:19 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_pnmenc_set_property;
|
|
|
|
gobject_class->get_property = gst_pnmenc_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, GST_PNMENC_PROP_ASCII,
|
|
|
|
g_param_spec_boolean ("ascii", "ASCII Encoding", "The output will be "
|
2010-10-19 10:43:14 +00:00
|
|
|
"ASCII encoded", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|