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
|
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-pnmdec
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: pnmdec
|
2009-09-10 06:23:22 +00:00
|
|
|
*
|
|
|
|
* Decodes pnm images.
|
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Example launch line
|
2009-09-10 06:23:22 +00:00
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 filesrc location=test.pnm ! pnmdec ! videoconvert ! autovideosink
|
2009-09-10 06:23:22 +00:00
|
|
|
* ]| The above pipeline reads a pnm file and renders it to the screen.
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2009-09-10 06:23:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2014-06-12 11:08:35 +00:00
|
|
|
#include <gst/base/gstbytereader.h>
|
2009-09-10 06:23:22 +00:00
|
|
|
#include <string.h>
|
2014-06-12 11:08:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
static gboolean gst_pnmdec_start (GstVideoDecoder * decoder);
|
2016-05-23 21:17:22 +00:00
|
|
|
static GstFlowReturn gst_pnmdec_finish (GstVideoDecoder * decoder);
|
2015-10-17 08:51:24 +00:00
|
|
|
static gboolean gst_pnmdec_set_format (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecState * state);
|
|
|
|
static gboolean gst_pnmdec_stop (GstVideoDecoder * decoder);
|
2014-06-12 11:08:35 +00:00
|
|
|
static GstFlowReturn gst_pnmdec_parse (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos);
|
|
|
|
static GstFlowReturn gst_pnmdec_handle_frame (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame);
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_parse_ascii (GstPnmdec * s, const guint8 * b, guint bs);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstPnmdec, gst_pnmdec, GST_TYPE_VIDEO_DECODER);
|
2009-09-10 06:23:22 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_pnmdec_src_pad_template =
|
2015-10-22 15:06:01 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
2014-06-12 11:08:35 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2015-10-22 15:06:01 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
|
|
|
|
("{ RGB, GRAY8, GRAY16_BE, GRAY16_LE }")));
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2016-05-16 11:22:57 +00:00
|
|
|
static GstStaticCaps gst_pnmdec_gray16_caps =
|
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ GRAY16_BE, GRAY16_LE }"));
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
static GstStaticPadTemplate gst_pnmdec_sink_pad_template =
|
2014-06-12 11:08:35 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2009-09-10 06:23:22 +00:00
|
|
|
GST_STATIC_CAPS (MIME_ALL));
|
|
|
|
|
2015-10-22 15:06:01 +00:00
|
|
|
GST_DEBUG_CATEGORY (pnmdecoder_debug);
|
|
|
|
#define GST_CAT_DEFAULT pnmdecoder_debug
|
2010-03-22 11:02:16 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
static void
|
|
|
|
gst_pnmdec_class_init (GstPnmdecClass * klass)
|
2009-09-13 17:33:57 +00:00
|
|
|
{
|
2014-06-12 11:08:35 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
GstVideoDecoderClass *vdec_class = (GstVideoDecoderClass *) klass;
|
2009-09-13 17:39:59 +00:00
|
|
|
|
2015-10-22 15:06:01 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (pnmdecoder_debug, "pnmdec", 0, "PNM Video Decoder");
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_pnmdec_src_pad_template);
|
|
|
|
gst_element_class_add_static_pad_template (element_class,
|
|
|
|
&gst_pnmdec_sink_pad_template);
|
2015-10-22 15:06:01 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
gst_element_class_set_static_metadata (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-13 17:33:57 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
vdec_class->start = gst_pnmdec_start;
|
2016-05-23 21:17:22 +00:00
|
|
|
vdec_class->finish = gst_pnmdec_finish;
|
2015-10-17 08:51:24 +00:00
|
|
|
vdec_class->stop = gst_pnmdec_stop;
|
2014-06-12 11:08:35 +00:00
|
|
|
vdec_class->parse = gst_pnmdec_parse;
|
|
|
|
vdec_class->handle_frame = gst_pnmdec_handle_frame;
|
2015-10-17 08:51:24 +00:00
|
|
|
vdec_class->set_format = gst_pnmdec_set_format;
|
2009-09-13 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
static void
|
|
|
|
gst_pnmdec_flush (GstPnmdec * s)
|
2009-09-17 07:38:02 +00:00
|
|
|
{
|
2015-10-17 03:45:42 +00:00
|
|
|
memset (&s->mngr, 0, sizeof (s->mngr));
|
2014-06-12 11:08:35 +00:00
|
|
|
s->size = 0;
|
|
|
|
s->current_size = 0;
|
|
|
|
if (s->buf) {
|
|
|
|
gst_buffer_unref (s->buf);
|
2009-09-17 07:38:02 +00:00
|
|
|
s->buf = NULL;
|
|
|
|
}
|
2014-06-12 11:08:35 +00:00
|
|
|
}
|
2009-09-17 07:38:02 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
static void
|
|
|
|
gst_pnmdec_init (GstPnmdec * s)
|
|
|
|
{
|
|
|
|
/* Initialize decoder */
|
|
|
|
s->buf = NULL;
|
|
|
|
gst_pnmdec_flush (s);
|
2015-08-15 15:55:49 +00:00
|
|
|
|
|
|
|
gst_video_decoder_set_use_default_pad_acceptcaps (GST_VIDEO_DECODER_CAST
|
|
|
|
(s), TRUE);
|
|
|
|
GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_DECODER_SINK_PAD (s));
|
2009-09-17 07:38:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 11:22:57 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_negotiate (GstVideoDecoder * decoder)
|
2015-10-22 15:06:01 +00:00
|
|
|
{
|
|
|
|
GstPnmdec *pnmdec = (GstPnmdec *) decoder;
|
|
|
|
GstVideoFormat fmt = GST_VIDEO_FORMAT_UNKNOWN;
|
2016-05-16 11:22:57 +00:00
|
|
|
GstVideoCodecState *output_state;
|
|
|
|
|
|
|
|
switch (pnmdec->mngr.info.type) {
|
|
|
|
case GST_PNM_TYPE_BITMAP:
|
|
|
|
if (pnmdec->mngr.info.encoding == GST_PNM_ENCODING_ASCII) {
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
pnmdec->size = pnmdec->mngr.info.width * pnmdec->mngr.info.height * 1;
|
|
|
|
fmt = GST_VIDEO_FORMAT_GRAY8;
|
|
|
|
break;
|
|
|
|
case GST_PNM_TYPE_GRAYMAP:
|
|
|
|
if (pnmdec->mngr.info.max > 255) {
|
|
|
|
GstCaps *gray16_caps = gst_static_caps_get (&gst_pnmdec_gray16_caps);
|
|
|
|
GstCaps *peercaps;
|
|
|
|
GstStructure *peerstruct;
|
|
|
|
const gchar *fmtstr;
|
|
|
|
|
|
|
|
pnmdec->size = pnmdec->mngr.info.width * pnmdec->mngr.info.height * 2;
|
2019-09-02 19:08:44 +00:00
|
|
|
/* perform some basic negotiation to resolve which endianness,
|
2016-05-16 11:22:57 +00:00
|
|
|
* if any, is supported by the component downstream. Query
|
|
|
|
* the peer caps, intersecting with our preferred caps
|
|
|
|
*/
|
|
|
|
peercaps =
|
|
|
|
gst_pad_peer_query_caps (GST_VIDEO_DECODER_SRC_PAD (decoder),
|
|
|
|
gray16_caps);
|
|
|
|
gst_caps_unref (gray16_caps);
|
|
|
|
|
|
|
|
GST_DEBUG ("Received caps from peer: %" GST_PTR_FORMAT, peercaps);
|
|
|
|
if (gst_caps_is_empty (peercaps)) {
|
|
|
|
gst_caps_unref (peercaps);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_caps_is_fixed (peercaps))
|
|
|
|
peercaps = gst_caps_fixate (peercaps);
|
|
|
|
|
|
|
|
peerstruct = gst_caps_get_structure (peercaps, 0);
|
|
|
|
fmtstr = gst_structure_get_string (peerstruct, "format");
|
|
|
|
if (fmtstr) {
|
|
|
|
if (g_str_equal (fmtstr, "GRAY16_BE")) {
|
2015-10-22 15:06:01 +00:00
|
|
|
fmt = GST_VIDEO_FORMAT_GRAY16_BE;
|
2016-05-16 11:22:57 +00:00
|
|
|
} else if (g_str_equal (fmtstr, "GRAY16_LE")) {
|
2015-10-22 15:06:01 +00:00
|
|
|
fmt = GST_VIDEO_FORMAT_GRAY16_LE;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 11:22:57 +00:00
|
|
|
gst_caps_unref (peercaps);
|
|
|
|
} else {
|
|
|
|
pnmdec->size = pnmdec->mngr.info.width * pnmdec->mngr.info.height * 1;
|
|
|
|
fmt = GST_VIDEO_FORMAT_GRAY8;
|
2015-10-22 15:06:01 +00:00
|
|
|
}
|
2016-05-16 11:22:57 +00:00
|
|
|
break;
|
|
|
|
case GST_PNM_TYPE_PIXMAP:
|
|
|
|
pnmdec->size = pnmdec->mngr.info.width * pnmdec->mngr.info.height * 3;
|
|
|
|
fmt = GST_VIDEO_FORMAT_RGB;
|
|
|
|
break;
|
2015-10-22 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 11:22:57 +00:00
|
|
|
if (fmt == GST_VIDEO_FORMAT_UNKNOWN)
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
|
|
|
|
pnmdec->out_format = fmt;
|
|
|
|
|
|
|
|
output_state =
|
|
|
|
gst_video_decoder_set_output_state (decoder, fmt,
|
|
|
|
pnmdec->mngr.info.width, pnmdec->mngr.info.height, pnmdec->input_state);
|
|
|
|
gst_video_codec_state_unref (output_state);
|
|
|
|
|
|
|
|
if (gst_video_decoder_negotiate (decoder) == FALSE)
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2015-10-22 15:06:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-17 08:51:24 +00:00
|
|
|
static gboolean
|
|
|
|
gst_pnmdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
|
|
|
|
{
|
|
|
|
GstPnmdec *pnmdec = (GstPnmdec *) decoder;
|
|
|
|
|
2016-05-16 11:22:57 +00:00
|
|
|
gst_pnmdec_negotiate (decoder);
|
2015-10-22 15:06:01 +00:00
|
|
|
|
2015-10-17 08:51:24 +00:00
|
|
|
if (pnmdec->input_state)
|
|
|
|
gst_video_codec_state_unref (pnmdec->input_state);
|
|
|
|
pnmdec->input_state = gst_video_codec_state_ref (state);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_pnmdec_stop (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstPnmdec *pnmdec = (GstPnmdec *) decoder;
|
|
|
|
|
|
|
|
if (pnmdec->input_state) {
|
|
|
|
gst_video_codec_state_unref (pnmdec->input_state);
|
|
|
|
pnmdec->input_state = NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-16 04:06:45 +00:00
|
|
|
if (pnmdec->buf) {
|
|
|
|
gst_buffer_unref (pnmdec->buf);
|
|
|
|
pnmdec->buf = NULL;
|
|
|
|
}
|
2015-10-17 08:51:24 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-09-17 07:38:02 +00:00
|
|
|
static GstFlowReturn
|
2014-06-12 11:08:35 +00:00
|
|
|
gst_pnmdec_parse_ascii (GstPnmdec * s, const guint8 * b, guint bs)
|
2009-09-17 07:38:02 +00:00
|
|
|
{
|
|
|
|
GScanner *scanner;
|
|
|
|
guint i = 0;
|
2016-05-23 21:17:22 +00:00
|
|
|
guint target, last_val = 0;
|
2012-09-12 22:34:03 +00:00
|
|
|
GstMapInfo map;
|
2014-06-12 11:08:35 +00:00
|
|
|
guint8 *outdata;
|
2009-09-22 06:11:36 +00:00
|
|
|
|
2019-10-22 06:30:34 +00:00
|
|
|
if (!s->buf)
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
target = s->size - s->current_size;
|
2012-09-12 22:34:03 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
gst_buffer_map (s->buf, &map, GST_MAP_WRITE);
|
|
|
|
|
2016-05-23 21:17:22 +00:00
|
|
|
if (bs) {
|
|
|
|
GST_MEMDUMP_OBJECT (s, "Starting parse:", b, MIN (16, bs));
|
|
|
|
}
|
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
/* leave the number of bytes already parsed */
|
|
|
|
outdata = map.data + s->current_size;
|
2009-09-22 06:11:36 +00:00
|
|
|
|
2016-05-23 21:17:22 +00:00
|
|
|
if (s->have_last_val) {
|
|
|
|
while (bs && *b >= '0' && *b <= '9') {
|
|
|
|
s->last_val = 10 * s->last_val + *b - '0';
|
2009-09-22 06:11:36 +00:00
|
|
|
b++;
|
|
|
|
if (!--bs) {
|
2012-09-12 22:34:03 +00:00
|
|
|
goto drop_error;
|
2009-09-22 06:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-23 21:17:22 +00:00
|
|
|
if (s->last_val > s->mngr.info.max) {
|
2009-09-22 06:11:36 +00:00
|
|
|
GST_DEBUG_OBJECT (s, "Corrupt ASCII encoded PNM file.");
|
2012-09-12 22:34:03 +00:00
|
|
|
goto drop_error;
|
2009-09-22 06:11:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 21:17:22 +00:00
|
|
|
GST_LOG_OBJECT (s, "Collected partial value from previous parse - %u",
|
|
|
|
s->last_val);
|
|
|
|
if (s->mngr.info.max > 255) {
|
|
|
|
if (i + 1 >= target) {
|
|
|
|
GST_DEBUG_OBJECT (s, "PNM file contains too much data.");
|
|
|
|
goto drop_error;
|
|
|
|
}
|
|
|
|
if (s->out_format == GST_VIDEO_FORMAT_GRAY16_BE)
|
|
|
|
GST_WRITE_UINT16_BE (outdata + i, s->last_val);
|
|
|
|
else
|
|
|
|
GST_WRITE_UINT16_LE (outdata + i, s->last_val);
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
outdata[i++] = s->last_val;
|
|
|
|
}
|
|
|
|
last_val = s->last_val;
|
|
|
|
s->have_last_val = FALSE;
|
2009-09-22 06:11:36 +00:00
|
|
|
}
|
2009-09-17 07:38:02 +00:00
|
|
|
|
2016-05-23 21:17:22 +00:00
|
|
|
/* Might be no data if we're at EOS */
|
|
|
|
if (!bs)
|
|
|
|
goto done;
|
|
|
|
|
2009-09-17 07:38:02 +00:00
|
|
|
scanner = g_scanner_new (NULL);
|
2014-06-12 11:08:35 +00:00
|
|
|
g_scanner_input_text (scanner, (gchar *) 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:
|
2016-05-23 21:17:22 +00:00
|
|
|
if (s->mngr.info.max > 255) {
|
|
|
|
if (i + 1 >= target) {
|
|
|
|
GST_DEBUG_OBJECT (s,
|
|
|
|
"PNM file contains too much data after line %u col %u.",
|
|
|
|
scanner->line, scanner->position);
|
|
|
|
g_scanner_destroy (scanner);
|
|
|
|
goto done; // drop_error;
|
|
|
|
}
|
|
|
|
if (s->out_format == GST_VIDEO_FORMAT_GRAY16_BE)
|
|
|
|
GST_WRITE_UINT16_BE (outdata + i, scanner->value.v_int);
|
|
|
|
else
|
|
|
|
GST_WRITE_UINT16_LE (outdata + i, scanner->value.v_int);
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
if (i == target) {
|
|
|
|
GST_DEBUG_OBJECT (s,
|
|
|
|
"PNM file contains too much data after line %u col %u.",
|
|
|
|
scanner->line, scanner->position);
|
|
|
|
g_scanner_destroy (scanner);
|
|
|
|
goto drop_error;
|
|
|
|
}
|
|
|
|
outdata[i++] = scanner->value.v_int;
|
2009-09-17 07:38:02 +00:00
|
|
|
}
|
2016-05-23 21:17:22 +00:00
|
|
|
last_val = scanner->value.v_int;
|
2009-09-17 07:38:02 +00:00
|
|
|
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. */
|
2016-05-23 21:17:22 +00:00
|
|
|
if (i && i < target && b[bs - 1] >= '0' && b[bs - 1] <= '9') {
|
|
|
|
s->last_val = last_val;
|
|
|
|
s->have_last_val = TRUE;
|
|
|
|
if (s->mngr.info.max > 255)
|
|
|
|
i -= 2;
|
|
|
|
else
|
|
|
|
i--;
|
|
|
|
GST_LOG_OBJECT (s, "Stored last value %u for next parse cycle",
|
|
|
|
s->last_val);
|
2009-09-17 07:38:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 21:17:22 +00:00
|
|
|
done:
|
|
|
|
/* Update the number of output bytes parsed in this scan */
|
2014-06-12 11:08:35 +00:00
|
|
|
s->current_size += i;
|
2016-05-23 21:17:22 +00:00
|
|
|
GST_LOG_OBJECT (s, "Parsed %u bytes, now have %u bytes of %u output",
|
|
|
|
i, s->current_size, s->size);
|
2014-06-12 11:08:35 +00:00
|
|
|
gst_buffer_unmap (s->buf, &map);
|
2012-09-12 22:34:03 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
return GST_FLOW_OK;
|
2012-09-12 22:34:03 +00:00
|
|
|
|
|
|
|
drop_error:
|
2014-06-12 11:08:35 +00:00
|
|
|
gst_buffer_unmap (s->buf, &map);
|
|
|
|
|
2012-09-12 22:34:03 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2009-09-17 07:38:02 +00:00
|
|
|
}
|
|
|
|
|
2009-09-10 06:23:22 +00:00
|
|
|
static GstFlowReturn
|
2014-06-12 11:08:35 +00:00
|
|
|
gst_pnmdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
|
2009-09-10 06:23:22 +00:00
|
|
|
{
|
2014-06-12 11:08:35 +00:00
|
|
|
GstPnmdec *s = (GstPnmdec *) decoder;
|
|
|
|
GstMapInfo imap, omap;
|
|
|
|
guint i_rowstride;
|
|
|
|
guint o_rowstride;
|
|
|
|
GstFlowReturn r = GST_FLOW_OK;
|
2014-06-18 06:14:54 +00:00
|
|
|
gint bytes, i, total_bytes = 0;
|
2014-06-12 11:08:35 +00:00
|
|
|
|
|
|
|
r = gst_video_decoder_allocate_output_frame (decoder, frame);
|
|
|
|
if (r != GST_FLOW_OK) {
|
2015-10-17 19:48:11 +00:00
|
|
|
gst_video_decoder_drop_frame (GST_VIDEO_DECODER (s), frame);
|
2014-06-12 11:08:35 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2014-06-17 07:20:17 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
if (s->mngr.info.encoding == GST_PNM_ENCODING_ASCII) {
|
|
|
|
/* In case of ASCII parsed data is stored in buf, so input needs to be
|
|
|
|
taken from here for frame processing */
|
|
|
|
gst_buffer_map (s->buf, &imap, GST_MAP_READ);
|
|
|
|
} else {
|
|
|
|
gst_buffer_map (frame->input_buffer, &imap, GST_MAP_READ);
|
|
|
|
}
|
|
|
|
gst_buffer_map (frame->output_buffer, &omap, GST_MAP_WRITE);
|
|
|
|
|
|
|
|
gst_buffer_copy_into (frame->output_buffer, frame->input_buffer,
|
|
|
|
GST_BUFFER_COPY_METADATA, 0, 0);
|
|
|
|
|
2014-06-17 07:20:17 +00:00
|
|
|
if (s->mngr.info.type == GST_PNM_TYPE_BITMAP) {
|
|
|
|
bytes = (s->mngr.info.width * s->mngr.info.height + 7) / 8;
|
|
|
|
for (i = 0; i < bytes; i++) {
|
|
|
|
omap.data[i * 8] = (imap.data[i] & 0x80) ? 0 : 255;
|
|
|
|
omap.data[i * 8 + 1] = (imap.data[i] & 0x40) ? 0 : 255;
|
|
|
|
omap.data[i * 8 + 2] = (imap.data[i] & 0x20) ? 0 : 255;
|
|
|
|
omap.data[i * 8 + 3] = (imap.data[i] & 0x10) ? 0 : 255;
|
|
|
|
omap.data[i * 8 + 4] = (imap.data[i] & 0x08) ? 0 : 255;
|
|
|
|
omap.data[i * 8 + 5] = (imap.data[i] & 0x04) ? 0 : 255;
|
|
|
|
omap.data[i * 8 + 6] = (imap.data[i] & 0x02) ? 0 : 255;
|
|
|
|
omap.data[i * 8 + 7] = (imap.data[i] & 0x01) ? 0 : 255;
|
|
|
|
}
|
2014-06-18 06:14:54 +00:00
|
|
|
total_bytes = bytes * 8;
|
2014-06-17 07:20:17 +00:00
|
|
|
} else
|
|
|
|
/* Need to convert from PNM rowstride to GStreamer rowstride */
|
2014-06-12 11:08:35 +00:00
|
|
|
if (s->mngr.info.width % 4 != 0) {
|
|
|
|
if (s->mngr.info.type == GST_PNM_TYPE_PIXMAP) {
|
|
|
|
i_rowstride = 3 * s->mngr.info.width;
|
|
|
|
o_rowstride = GST_ROUND_UP_4 (i_rowstride);
|
|
|
|
} else {
|
2015-10-22 15:06:01 +00:00
|
|
|
if (s->mngr.info.max > 255)
|
|
|
|
i_rowstride = s->mngr.info.width * 2;
|
|
|
|
else
|
|
|
|
i_rowstride = s->mngr.info.width;
|
2014-06-12 11:08:35 +00:00
|
|
|
o_rowstride = GST_ROUND_UP_4 (i_rowstride);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < s->mngr.info.height; i++)
|
|
|
|
memcpy (omap.data + i * o_rowstride, imap.data + i * i_rowstride,
|
|
|
|
i_rowstride);
|
2014-06-18 06:14:54 +00:00
|
|
|
total_bytes = o_rowstride * s->mngr.info.height;
|
2014-06-12 11:08:35 +00:00
|
|
|
} else {
|
|
|
|
memcpy (omap.data, imap.data, s->size);
|
2014-06-18 06:14:54 +00:00
|
|
|
total_bytes = s->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->mngr.info.type != GST_PNM_TYPE_BITMAP) {
|
2015-10-22 15:06:01 +00:00
|
|
|
if (s->mngr.info.max > 255 && s->mngr.info.max < 65535) {
|
2016-05-16 11:22:57 +00:00
|
|
|
/* Convert the pixels from 0 - max range to 0 - 65535 range
|
|
|
|
* and appropriate endianness (input is always BE) */
|
|
|
|
guint8 *data = omap.data;
|
2015-10-22 15:06:01 +00:00
|
|
|
gint max = s->mngr.info.max;
|
2016-05-16 11:22:57 +00:00
|
|
|
if (s->out_format == GST_VIDEO_FORMAT_GRAY16_BE) {
|
|
|
|
for (i = 0; i < total_bytes; i += 2) {
|
|
|
|
/* Clamp to 65535 */
|
|
|
|
guint16 val = GST_READ_UINT16_BE (data + i);
|
|
|
|
val = (val > max) ? 65535 : 65535 * val / max;
|
|
|
|
GST_WRITE_UINT16_BE (data + i, val);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < total_bytes; i += 2) {
|
|
|
|
/* Clamp to 65535 */
|
|
|
|
guint16 val = GST_READ_UINT16_BE (data + i);
|
|
|
|
val = (val > max) ? 65535 : 65535 * val / max;
|
|
|
|
GST_WRITE_UINT16_LE (data + i, val);
|
2015-10-22 15:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (s->mngr.info.max < 255) {
|
2016-05-16 11:22:57 +00:00
|
|
|
/* Convert the pixels from 0 - max range to 0 - 255 range */
|
2014-06-18 06:14:54 +00:00
|
|
|
gint max = s->mngr.info.max;
|
|
|
|
for (i = 0; i < total_bytes; i++) {
|
|
|
|
if (omap.data[i] <= max) {
|
|
|
|
omap.data[i] = 255 * omap.data[i] / max;
|
|
|
|
} else {
|
|
|
|
/* This is an error case, wherein value in the data stream is
|
|
|
|
more than max. Clamp such values to 255 */
|
|
|
|
omap.data[i] = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-12 11:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->mngr.info.encoding == GST_PNM_ENCODING_ASCII) {
|
|
|
|
gst_buffer_unmap (s->buf, &imap);
|
|
|
|
} else {
|
|
|
|
gst_buffer_unmap (frame->input_buffer, &imap);
|
|
|
|
}
|
|
|
|
gst_buffer_unmap (frame->output_buffer, &omap);
|
|
|
|
|
2016-05-23 21:17:22 +00:00
|
|
|
s->current_size = 0;
|
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
r = gst_video_decoder_finish_frame (GST_VIDEO_DECODER (s), frame);
|
|
|
|
|
|
|
|
out:
|
|
|
|
gst_pnmdec_flush (s);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_parse (GstVideoDecoder * decoder, GstVideoCodecFrame * frame,
|
|
|
|
GstAdapter * adapter, gboolean at_eos)
|
|
|
|
{
|
|
|
|
gsize size;
|
|
|
|
GstPnmdec *s = GST_PNMDEC (decoder);
|
2009-09-10 06:23:22 +00:00
|
|
|
GstFlowReturn r = GST_FLOW_OK;
|
2009-09-17 07:38:02 +00:00
|
|
|
guint offset = 0;
|
2016-05-23 21:17:22 +00:00
|
|
|
const guint8 *raw_data = NULL;
|
2014-06-12 11:08:35 +00:00
|
|
|
|
|
|
|
GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
|
|
|
|
|
|
|
|
size = gst_adapter_available (adapter);
|
2016-05-23 21:17:22 +00:00
|
|
|
if (size > 0)
|
|
|
|
raw_data = gst_adapter_map (adapter, size);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (s, "Entering parse with %" G_GSIZE_FORMAT " bytes. at_eos %d",
|
|
|
|
size, at_eos);
|
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) {
|
2012-09-12 22:34:03 +00:00
|
|
|
GstPnmInfoMngrResult res;
|
|
|
|
|
2016-05-23 21:17:22 +00:00
|
|
|
if (size < 8)
|
|
|
|
goto need_more_data;
|
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
res = gst_pnm_info_mngr_scan (&s->mngr, raw_data, size);
|
2012-09-12 22:34:03 +00:00
|
|
|
|
|
|
|
switch (res) {
|
2009-09-10 06:23:22 +00:00
|
|
|
case GST_PNM_INFO_MNGR_RESULT_FAILED:
|
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:
|
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:
|
2016-05-16 11:22:57 +00:00
|
|
|
|
|
|
|
r = gst_pnmdec_negotiate (decoder);
|
|
|
|
if (r != GST_FLOW_OK)
|
2009-09-13 17:13:24 +00:00
|
|
|
goto out;
|
2014-06-12 11:08:35 +00:00
|
|
|
|
|
|
|
if (s->mngr.info.encoding == GST_PNM_ENCODING_ASCII) {
|
|
|
|
/* It is not possible to know the size of input ascii data to parse.
|
|
|
|
So we have to parse and know the number of pixels parsed and
|
|
|
|
then finally decide when we have full frame */
|
2016-05-23 21:17:22 +00:00
|
|
|
GST_DEBUG_OBJECT (s, "Allocating output frame of size %u", s->size);
|
2014-06-12 11:08:35 +00:00
|
|
|
s->buf = gst_buffer_new_and_alloc (s->size);
|
|
|
|
}
|
|
|
|
offset = s->mngr.data_offset;
|
|
|
|
gst_adapter_flush (adapter, offset);
|
|
|
|
size = size - offset;
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
if (s->mngr.info.encoding == GST_PNM_ENCODING_ASCII) {
|
2016-05-23 21:17:22 +00:00
|
|
|
/* Parse ASCII data and populate s->current_size with the number of
|
2014-06-12 11:08:35 +00:00
|
|
|
bytes actually parsed from the input data */
|
2016-05-23 21:17:22 +00:00
|
|
|
GST_DEBUG_OBJECT (s, "Parsing %u bytes at offset %u", (guint) size, offset);
|
2014-06-12 11:08:35 +00:00
|
|
|
r = gst_pnmdec_parse_ascii (s, raw_data + offset, size);
|
|
|
|
} else {
|
2014-06-17 07:20:17 +00:00
|
|
|
/* Bitmap Contains 8 pixels in a byte */
|
|
|
|
if (s->mngr.info.type == GST_PNM_TYPE_BITMAP)
|
|
|
|
s->current_size += (size * 8);
|
|
|
|
else
|
|
|
|
s->current_size += size;
|
2009-09-10 06:53:46 +00:00
|
|
|
}
|
2014-06-17 07:20:17 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
gst_video_decoder_add_to_frame (decoder, size);
|
|
|
|
if (s->size <= s->current_size) {
|
|
|
|
goto have_full_frame;
|
2009-09-10 06:53:46 +00:00
|
|
|
}
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
need_more_data:
|
|
|
|
return GST_VIDEO_DECODER_FLOW_NEED_DATA;
|
2009-09-10 06:23:22 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
have_full_frame:
|
|
|
|
return gst_video_decoder_have_frame (decoder);
|
2009-09-10 06:53:46 +00:00
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
out:
|
2009-09-10 06:23:22 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-06-12 11:08:35 +00:00
|
|
|
static gboolean
|
|
|
|
gst_pnmdec_start (GstVideoDecoder * decoder)
|
2009-09-10 06:23:22 +00:00
|
|
|
{
|
2014-06-12 11:08:35 +00:00
|
|
|
GstPnmdec *pnmdec = (GstPnmdec *) decoder;
|
|
|
|
gst_video_decoder_set_packetized (GST_VIDEO_DECODER (pnmdec), FALSE);
|
2016-05-23 21:17:22 +00:00
|
|
|
gst_pnmdec_flush (pnmdec);
|
2014-06-12 11:08:35 +00:00
|
|
|
return TRUE;
|
2009-09-10 06:23:22 +00:00
|
|
|
}
|
2016-05-23 21:17:22 +00:00
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_pnmdec_finish (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstPnmdec *s = (GstPnmdec *) decoder;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (s, "finishing");
|
|
|
|
|
|
|
|
if (s->mngr.info.encoding == GST_PNM_ENCODING_ASCII) {
|
|
|
|
/* One last go at outputting any final value */
|
|
|
|
gst_pnmdec_parse_ascii (s, 0, 0);
|
2019-10-22 06:30:34 +00:00
|
|
|
if (s->size && s->size <= s->current_size) {
|
2016-05-23 21:17:22 +00:00
|
|
|
return gst_video_decoder_have_frame (decoder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|