2010-01-22 02:19:24 +00:00
|
|
|
/* GStreamer PNM decoder
|
|
|
|
* 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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-pnmdec
|
|
|
|
*
|
|
|
|
* Decodes pnm images.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
* |[
|
2009-09-13 17:13:24 +00:00
|
|
|
* gst-launch filesrc location=test.pnm ! pnmdec ! ffmpegcolorspace ! autovideosink
|
2009-09-10 06:23:22 +00:00
|
|
|
* ]| The above pipeline reads a pnm file and renders it to the screen.
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gstpnmdec.h"
|
|
|
|
#include "gstpnmutils.h"
|
|
|
|
|
|
|
|
#include <gst/gstutils.h>
|
2009-09-10 06:53:46 +00:00
|
|
|
#include <gst/video/video.h>
|
2009-09-10 06:23:22 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_pnmdec_src_pad_template =
|
2009-09-13 17:13:24 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB "; "
|
|
|
|
"video/x-raw-gray, width =" GST_VIDEO_SIZE_RANGE ", "
|
|
|
|
"height =" GST_VIDEO_SIZE_RANGE ", framerate =" GST_VIDEO_FPS_RANGE ", "
|
2009-09-13 17:17:15 +00:00
|
|
|
"bpp= (int) 8, depth= (int) 8"));
|
2009-09-10 06:23:22 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_pnmdec_sink_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS (MIME_ALL));
|
|
|
|
|
2010-03-22 11:02:16 +00:00
|
|
|
GST_BOILERPLATE (GstPnmdec, gst_pnmdec, GstElement, GST_TYPE_ELEMENT);
|
|
|
|
|
2009-09-13 17:33:57 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_push (GstPnmdec * s, GstPad * src, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
/* Need to convert from PNM rowstride to GStreamer rowstride */
|
2009-09-13 17:39:59 +00:00
|
|
|
if (s->mngr.info.width % 4 != 0) {
|
|
|
|
guint i_rowstride;
|
|
|
|
guint o_rowstride;
|
|
|
|
GstBuffer *obuf;
|
2009-09-13 17:33:57 +00:00
|
|
|
guint i;
|
|
|
|
|
2009-09-16 06:22:19 +00:00
|
|
|
if (s->mngr.info.type == GST_PNM_TYPE_PIXMAP) {
|
2009-09-13 17:39:59 +00:00
|
|
|
i_rowstride = 3 * s->mngr.info.width;
|
|
|
|
o_rowstride = GST_ROUND_UP_4 (i_rowstride);
|
|
|
|
} else {
|
|
|
|
i_rowstride = s->mngr.info.width;
|
|
|
|
o_rowstride = GST_ROUND_UP_4 (i_rowstride);
|
|
|
|
}
|
|
|
|
|
|
|
|
obuf = gst_buffer_new_and_alloc (o_rowstride * s->mngr.info.height);
|
|
|
|
|
2009-09-13 17:33:57 +00:00
|
|
|
gst_buffer_copy_metadata (obuf, buf, GST_BUFFER_COPY_ALL);
|
|
|
|
|
|
|
|
for (i = 0; i < s->mngr.info.height; i++)
|
|
|
|
memcpy (GST_BUFFER_DATA (obuf) + i * o_rowstride,
|
|
|
|
GST_BUFFER_DATA (buf) + i * i_rowstride, i_rowstride);
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return gst_pad_push (src, obuf);
|
|
|
|
} else {
|
|
|
|
return gst_pad_push (src, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-17 07:38:02 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_chain_raw (GstPnmdec * s, GstPad * src, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstFlowReturn r = GST_FLOW_OK;
|
2009-09-22 06:11:36 +00:00
|
|
|
GstBuffer *out;
|
2009-09-17 07:38:02 +00:00
|
|
|
|
|
|
|
/* If we got the whole image, just push the buffer. */
|
|
|
|
if (GST_BUFFER_SIZE (buf) == s->size) {
|
|
|
|
memset (&s->mngr, 0, sizeof (GstPnmInfoMngr));
|
|
|
|
s->size = 0;
|
|
|
|
gst_buffer_set_caps (buf, GST_PAD_CAPS (src));
|
|
|
|
return gst_pnmdec_push (s, src, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We didn't get the whole image. */
|
|
|
|
if (!s->buf) {
|
|
|
|
s->buf = buf;
|
|
|
|
} else {
|
2009-09-22 06:11:36 +00:00
|
|
|
out = gst_buffer_span (s->buf, 0, buf,
|
2009-09-17 07:38:02 +00:00
|
|
|
GST_BUFFER_SIZE (s->buf) + GST_BUFFER_SIZE (buf));
|
2009-09-22 06:11:36 +00:00
|
|
|
gst_buffer_unref (buf);
|
2009-09-17 07:38:02 +00:00
|
|
|
gst_buffer_unref (s->buf);
|
2009-09-22 06:11:36 +00:00
|
|
|
s->buf = out;
|
2009-09-17 07:38:02 +00:00
|
|
|
}
|
|
|
|
if (!s->buf)
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
/* Do we now have the full image? If yes, push. */
|
|
|
|
if (GST_BUFFER_SIZE (s->buf) == s->size) {
|
|
|
|
gst_buffer_set_caps (s->buf, GST_PAD_CAPS (src));
|
|
|
|
r = gst_pnmdec_push (s, src, s->buf);
|
|
|
|
s->buf = NULL;
|
|
|
|
memset (&s->mngr, 0, sizeof (GstPnmInfoMngr));
|
|
|
|
s->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_chain_ascii (GstPnmdec * s, GstPad * src, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GScanner *scanner;
|
2009-09-22 06:11:36 +00:00
|
|
|
GstBuffer *out;
|
2009-09-17 07:38:02 +00:00
|
|
|
guint i = 0;
|
2009-09-22 06:11:36 +00:00
|
|
|
gchar *b = (gchar *) GST_BUFFER_DATA (buf);
|
|
|
|
guint bs = GST_BUFFER_SIZE (buf);
|
|
|
|
guint target = s->size - (s->buf ? GST_BUFFER_SIZE (s->buf) : 0);
|
|
|
|
|
|
|
|
if (!bs) {
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->last_byte) {
|
|
|
|
while (*b >= '0' && *b <= '9') {
|
|
|
|
s->last_byte = 10 * s->last_byte + *b - '0';
|
|
|
|
b++;
|
|
|
|
if (!--bs) {
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s->last_byte > 255) {
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
GST_DEBUG_OBJECT (s, "Corrupt ASCII encoded PNM file.");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = gst_buffer_new_and_alloc (target);
|
|
|
|
|
|
|
|
if (s->last_byte) {
|
|
|
|
GST_BUFFER_DATA (out)[i++] = s->last_byte;
|
|
|
|
s->last_byte = 0;
|
|
|
|
}
|
2009-09-17 07:38:02 +00:00
|
|
|
|
|
|
|
scanner = g_scanner_new (NULL);
|
2009-09-22 06:11:36 +00:00
|
|
|
g_scanner_input_text (scanner, b, bs);
|
2009-09-17 07:38:02 +00:00
|
|
|
while (!g_scanner_eof (scanner)) {
|
|
|
|
switch (g_scanner_get_next_token (scanner)) {
|
|
|
|
case G_TOKEN_INT:
|
2009-09-22 06:11:36 +00:00
|
|
|
if (i == target) {
|
2009-09-17 07:38:02 +00:00
|
|
|
GST_DEBUG_OBJECT (s, "PNM file contains too much data.");
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
gst_buffer_unref (out);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
GST_BUFFER_DATA (out)[i++] = scanner->value.v_int;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Should we care? */ ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_scanner_destroy (scanner);
|
|
|
|
|
2009-09-22 06:11:36 +00:00
|
|
|
/* If we didn't get the whole image, handle the last byte with care. */
|
|
|
|
if (i < target && b[bs - 1] > '0' && b[bs - 1] <= '9')
|
|
|
|
s->last_byte = GST_BUFFER_DATA (out)[--i];
|
|
|
|
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
if (!i) {
|
2009-09-17 07:38:02 +00:00
|
|
|
gst_buffer_unref (out);
|
2009-09-22 06:11:36 +00:00
|
|
|
return GST_FLOW_OK;
|
2009-09-17 07:38:02 +00:00
|
|
|
}
|
|
|
|
|
2009-09-22 06:11:36 +00:00
|
|
|
GST_BUFFER_SIZE (out) = i;
|
2009-09-17 07:38:02 +00:00
|
|
|
return gst_pnmdec_chain_raw (s, src, out);
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_chain (GstPad * pad, GstBuffer * data)
|
|
|
|
{
|
|
|
|
GstPnmdec *s = GST_PNMDEC (gst_pad_get_parent (pad));
|
|
|
|
GstPad *src = gst_element_get_static_pad (GST_ELEMENT (s), "src");
|
|
|
|
GstCaps *caps = NULL;
|
|
|
|
GstFlowReturn r = GST_FLOW_OK;
|
2009-09-17 07:38:02 +00:00
|
|
|
guint offset = 0;
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2009-09-22 06:11:36 +00:00
|
|
|
if (s->mngr.info.fields != GST_PNM_INFO_FIELDS_ALL) {
|
2009-09-10 06:23:22 +00:00
|
|
|
switch (gst_pnm_info_mngr_scan (&s->mngr, GST_BUFFER_DATA (data),
|
|
|
|
GST_BUFFER_SIZE (data))) {
|
|
|
|
case GST_PNM_INFO_MNGR_RESULT_FAILED:
|
|
|
|
gst_buffer_unref (data);
|
2009-09-10 06:53:46 +00:00
|
|
|
r = GST_FLOW_ERROR;
|
|
|
|
goto out;
|
2009-09-10 06:23:22 +00:00
|
|
|
case GST_PNM_INFO_MNGR_RESULT_READING:
|
|
|
|
gst_buffer_unref (data);
|
2009-09-10 06:53:46 +00:00
|
|
|
r = GST_FLOW_OK;
|
|
|
|
goto out;
|
2009-09-10 06:23:22 +00:00
|
|
|
case GST_PNM_INFO_MNGR_RESULT_FINISHED:
|
|
|
|
offset = s->mngr.data_offset;
|
2009-09-13 17:13:24 +00:00
|
|
|
caps = gst_caps_copy (gst_pad_get_pad_template_caps (src));
|
2009-09-10 06:23:22 +00:00
|
|
|
switch (s->mngr.info.type) {
|
2009-09-16 06:22:19 +00:00
|
|
|
case GST_PNM_TYPE_BITMAP:
|
|
|
|
GST_DEBUG_OBJECT (s, "FIXME: BITMAP format not implemented!");
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
gst_buffer_unref (data);
|
|
|
|
r = GST_FLOW_ERROR;
|
|
|
|
goto out;
|
|
|
|
case GST_PNM_TYPE_GRAYMAP:
|
2009-09-13 17:13:24 +00:00
|
|
|
gst_caps_remove_structure (caps, 0);
|
2009-09-10 06:23:22 +00:00
|
|
|
s->size = s->mngr.info.width * s->mngr.info.height * 1;
|
|
|
|
break;
|
2009-09-16 06:22:19 +00:00
|
|
|
case GST_PNM_TYPE_PIXMAP:
|
2009-09-13 17:13:24 +00:00
|
|
|
gst_caps_remove_structure (caps, 1);
|
2009-09-10 06:23:22 +00:00
|
|
|
s->size = s->mngr.info.width * s->mngr.info.height * 3;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-13 17:13:24 +00:00
|
|
|
gst_caps_set_simple (caps,
|
|
|
|
"width", G_TYPE_INT, s->mngr.info.width,
|
|
|
|
"height", G_TYPE_INT, s->mngr.info.height, "framerate",
|
|
|
|
GST_TYPE_FRACTION, 0, 1, NULL);
|
|
|
|
if (!gst_pad_set_caps (src, caps)) {
|
|
|
|
gst_caps_unref (caps);
|
2009-09-16 06:22:19 +00:00
|
|
|
gst_buffer_unref (data);
|
2009-09-13 17:13:24 +00:00
|
|
|
r = GST_FLOW_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 06:53:46 +00:00
|
|
|
if (offset == GST_BUFFER_SIZE (data)) {
|
2009-09-16 06:22:19 +00:00
|
|
|
gst_buffer_unref (data);
|
2009-09-10 06:53:46 +00:00
|
|
|
r = GST_FLOW_OK;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2009-09-17 07:38:02 +00:00
|
|
|
if (offset) {
|
|
|
|
GstBuffer *buf = gst_buffer_create_sub (data, offset,
|
2009-09-10 06:23:22 +00:00
|
|
|
GST_BUFFER_SIZE (data) - offset);
|
2009-09-17 07:38:02 +00:00
|
|
|
gst_buffer_unref (data);
|
|
|
|
data = buf;
|
2009-09-10 06:53:46 +00:00
|
|
|
}
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2009-09-17 07:38:02 +00:00
|
|
|
if (s->mngr.info.encoding == GST_PNM_ENCODING_ASCII)
|
|
|
|
r = gst_pnmdec_chain_ascii (s, src, data);
|
|
|
|
else
|
|
|
|
r = gst_pnmdec_chain_raw (s, src, data);
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2009-09-10 06:53:46 +00:00
|
|
|
out:
|
|
|
|
gst_object_unref (src);
|
|
|
|
gst_object_unref (s);
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pnmdec_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstPnmdec *dec = GST_PNMDEC (object);
|
|
|
|
|
|
|
|
if (dec->buf) {
|
|
|
|
gst_buffer_unref (dec->buf);
|
|
|
|
dec->buf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pnmdec_init (GstPnmdec * s, GstPnmdecClass * klass)
|
|
|
|
{
|
|
|
|
GstPad *pad;
|
|
|
|
|
|
|
|
pad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_pnmdec_sink_pad_template), "sink");
|
|
|
|
gst_pad_set_chain_function (pad, gst_pnmdec_chain);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (s), pad);
|
|
|
|
|
|
|
|
pad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&gst_pnmdec_src_pad_template), "src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (s), pad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pnmdec_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_pnmdec_sink_pad_template));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_pnmdec_src_pad_template));
|
2010-01-22 02:22:56 +00:00
|
|
|
gst_element_class_set_details_simple (element_class, "PNM image decoder",
|
|
|
|
"Codec/Decoder/Image",
|
|
|
|
"Decodes images in portable pixmap/graymap/bitmap/anymamp (PNM) format",
|
|
|
|
"Lutz Mueller <lutz@users.sourceforge.net>");
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_pnmdec_class_init (GstPnmdecClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_pnmdec_finalize;
|
|
|
|
}
|