2009-08-19 10:22:30 +00:00
|
|
|
/* GStreamer
|
|
|
|
*
|
|
|
|
* jpegparse: a parser for JPEG streams
|
|
|
|
*
|
|
|
|
* Copyright (C) <2009> Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
|
|
|
|
* Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser 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-08-19 10:22:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-jpegparse
|
|
|
|
* @short_description: JPEG parser
|
|
|
|
*
|
|
|
|
* Parses a JPEG stream into JPEG images. It looks for EOI boundaries to
|
|
|
|
* split a continuous stream into single-frame buffers. Also reads the
|
|
|
|
* image header searching for image properties such as width and height
|
2010-05-07 19:53:17 +00:00
|
|
|
* among others. Jpegparse can also extract metadata (e.g. xmp).
|
2010-03-24 14:05:20 +00:00
|
|
|
*
|
2009-08-19 10:22:30 +00:00
|
|
|
* <refsect2>
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 -v souphttpsrc location=... ! jpegparse ! matroskamux ! filesink location=...
|
2009-08-19 10:22:30 +00:00
|
|
|
* ]|
|
|
|
|
* The above pipeline fetches a motion JPEG stream from an IP camera over
|
|
|
|
* HTTP and stores it in a matroska file.
|
|
|
|
* </refsect2>
|
|
|
|
*/
|
2010-05-07 19:53:17 +00:00
|
|
|
/* FIXME: output plain JFIF APP marker only. This provides best code reuse.
|
|
|
|
* JPEG decoders would not need to handle this part anymore. Also when remuxing
|
|
|
|
* (... ! jpegparse ! ... ! jifmux ! ...) metadata consolidation would be
|
|
|
|
* easier.
|
|
|
|
*/
|
2009-08-19 10:22:30 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2010-02-02 09:22:06 +00:00
|
|
|
#include <string.h>
|
2009-08-19 10:22:30 +00:00
|
|
|
#include <gst/base/gstbytereader.h>
|
2010-03-11 19:32:28 +00:00
|
|
|
#include <gst/tag/tag.h>
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
#include "gstjpegparse.h"
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_jpeg_parse_src_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("image/jpeg, "
|
2011-10-05 18:06:20 +00:00
|
|
|
"format = (string) { I420, Y41B, UYVY, YV12 }, "
|
2009-08-19 10:22:30 +00:00
|
|
|
"width = (int) [ 0, MAX ],"
|
|
|
|
"height = (int) [ 0, MAX ], "
|
|
|
|
"framerate = (fraction) [ 0/1, MAX ], " "parsed = (boolean) true")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_jpeg_parse_sink_pad_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2013-04-12 15:01:38 +00:00
|
|
|
GST_STATIC_CAPS ("image/jpeg")
|
2009-08-19 10:22:30 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (jpeg_parse_debug);
|
|
|
|
#define GST_CAT_DEFAULT jpeg_parse_debug
|
|
|
|
|
|
|
|
struct _GstJpegParsePrivate
|
|
|
|
{
|
2010-02-02 09:22:06 +00:00
|
|
|
guint last_offset;
|
2010-04-29 14:33:44 +00:00
|
|
|
guint last_entropy_len;
|
2010-05-31 16:30:19 +00:00
|
|
|
gboolean last_resync;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
/* negotiated state */
|
|
|
|
gint caps_width, caps_height;
|
|
|
|
gint caps_framerate_numerator;
|
|
|
|
gint caps_framerate_denominator;
|
|
|
|
|
|
|
|
/* the parsed frame size */
|
|
|
|
guint16 width, height;
|
|
|
|
|
2011-10-05 18:06:20 +00:00
|
|
|
/* format color space */
|
|
|
|
const gchar *format;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
/* TRUE if the src caps sets a specific framerate */
|
|
|
|
gboolean has_fps;
|
|
|
|
|
|
|
|
/* the (expected) timestamp of the next frame */
|
|
|
|
guint64 next_ts;
|
|
|
|
|
|
|
|
/* duration of the current frame */
|
|
|
|
guint64 duration;
|
|
|
|
|
|
|
|
/* video state */
|
|
|
|
gint framerate_numerator;
|
|
|
|
gint framerate_denominator;
|
2010-08-17 20:30:10 +00:00
|
|
|
|
|
|
|
/* tags */
|
|
|
|
GstTagList *tags;
|
2009-08-19 10:22:30 +00:00
|
|
|
};
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_jpeg_parse_handle_frame (GstBaseParse * bparse, GstBaseParseFrame * frame,
|
|
|
|
gint * skipsize);
|
|
|
|
static gboolean gst_jpeg_parse_set_sink_caps (GstBaseParse * parse,
|
2012-09-11 23:36:24 +00:00
|
|
|
GstCaps * caps);
|
2014-04-22 02:05:48 +00:00
|
|
|
static gboolean gst_jpeg_parse_sink_event (GstBaseParse * parse,
|
2012-09-11 23:36:24 +00:00
|
|
|
GstEvent * event);
|
2014-04-22 02:05:48 +00:00
|
|
|
static gboolean gst_jpeg_parse_start (GstBaseParse * parse);
|
|
|
|
static gboolean gst_jpeg_parse_stop (GstBaseParse * parse);
|
|
|
|
static GstFlowReturn gst_jpeg_parse_pre_push_frame (GstBaseParse * bparse,
|
|
|
|
GstBaseParseFrame * frame);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2011-10-05 18:06:20 +00:00
|
|
|
#define gst_jpeg_parse_parent_class parent_class
|
2014-04-22 02:05:48 +00:00
|
|
|
G_DEFINE_TYPE (GstJpegParse, gst_jpeg_parse, GST_TYPE_BASE_PARSE);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_jpeg_parse_class_init (GstJpegParseClass * klass)
|
|
|
|
{
|
2014-04-22 02:05:48 +00:00
|
|
|
GstBaseParseClass *gstbaseparse_class;
|
2009-08-19 10:22:30 +00:00
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
gstbaseparse_class = (GstBaseParseClass *) klass;
|
2009-08-19 10:22:30 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
|
|
|
|
g_type_class_add_private (gobject_class, sizeof (GstJpegParsePrivate));
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
gstbaseparse_class->start = gst_jpeg_parse_start;
|
|
|
|
gstbaseparse_class->stop = gst_jpeg_parse_stop;
|
|
|
|
gstbaseparse_class->set_sink_caps = gst_jpeg_parse_set_sink_caps;
|
|
|
|
gstbaseparse_class->sink_event = gst_jpeg_parse_sink_event;
|
|
|
|
gstbaseparse_class->handle_frame = gst_jpeg_parse_handle_frame;
|
|
|
|
gstbaseparse_class->pre_push_frame = gst_jpeg_parse_pre_push_frame;
|
2011-10-05 18:06:20 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_jpeg_parse_src_pad_template));
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_jpeg_parse_sink_pad_template));
|
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2011-10-05 18:06:20 +00:00
|
|
|
"JPEG stream parser",
|
|
|
|
"Video/Parser",
|
|
|
|
"Parse JPEG images into single-frame buffers",
|
|
|
|
"Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>");
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (jpeg_parse_debug, "jpegparse", 0, "JPEG parser");
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-05 18:06:20 +00:00
|
|
|
gst_jpeg_parse_init (GstJpegParse * parse)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
2010-02-01 13:04:28 +00:00
|
|
|
parse->priv = G_TYPE_INSTANCE_GET_PRIVATE (parse, GST_TYPE_JPEG_PARSE,
|
|
|
|
GstJpegParsePrivate);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
parse->priv->next_ts = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_jpeg_parse_set_sink_caps (GstBaseParse * bparse, GstCaps * caps)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
2014-04-22 02:05:48 +00:00
|
|
|
GstJpegParse *parse = GST_JPEG_PARSE_CAST (bparse);
|
2009-08-19 10:22:30 +00:00
|
|
|
GstStructure *s = gst_caps_get_structure (caps, 0);
|
|
|
|
const GValue *framerate;
|
|
|
|
|
|
|
|
if ((framerate = gst_structure_get_value (s, "framerate")) != NULL) {
|
|
|
|
if (GST_VALUE_HOLDS_FRACTION (framerate)) {
|
|
|
|
parse->priv->framerate_numerator =
|
|
|
|
gst_value_get_fraction_numerator (framerate);
|
|
|
|
parse->priv->framerate_denominator =
|
|
|
|
gst_value_get_fraction_denominator (framerate);
|
|
|
|
parse->priv->has_fps = TRUE;
|
|
|
|
GST_DEBUG_OBJECT (parse, "got framerate of %d/%d",
|
|
|
|
parse->priv->framerate_numerator, parse->priv->framerate_denominator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-02-01 15:46:29 +00:00
|
|
|
|
2010-02-01 13:04:28 +00:00
|
|
|
/*
|
|
|
|
* gst_jpeg_parse_skip_to_jpeg_header:
|
|
|
|
* @parse: the parser
|
|
|
|
*
|
|
|
|
* Flush everything until the next JPEG header. The header is considered
|
|
|
|
* to be the a start marker SOI (0xff 0xd8) followed by any other marker
|
|
|
|
* (0xff ...).
|
|
|
|
*
|
|
|
|
* Returns: TRUE if the header was found, FALSE if more data is needed.
|
|
|
|
*/
|
2009-08-19 10:22:30 +00:00
|
|
|
static gboolean
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_jpeg_parse_skip_to_jpeg_header (GstJpegParse * parse, GstMapInfo * mapinfo,
|
|
|
|
gint * skipsize)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
|
|
|
gboolean ret = TRUE;
|
2014-04-22 02:05:48 +00:00
|
|
|
GstByteReader reader;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
if (mapinfo->size < 4)
|
2009-08-19 10:22:30 +00:00
|
|
|
return FALSE;
|
2010-02-02 09:22:06 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_byte_reader_init (&reader, mapinfo->data, mapinfo->size);
|
|
|
|
|
|
|
|
*skipsize = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffff00,
|
|
|
|
0xffd8ff00, 0, mapinfo->size);
|
|
|
|
if (*skipsize == -1) {
|
|
|
|
*skipsize = mapinfo->size - 3; /* Last 3 bytes + 1 more may match header. */
|
2009-08-19 10:22:30 +00:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline gboolean
|
|
|
|
gst_jpeg_parse_parse_tag_has_entropy_segment (guint8 tag)
|
|
|
|
{
|
|
|
|
if (tag == SOS || (tag >= RST0 && tag <= RST7))
|
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-12-06 17:19:20 +00:00
|
|
|
/* returns image length in bytes if parsed successfully,
|
|
|
|
* otherwise 0 if more data needed,
|
|
|
|
* if < 0 the absolute value needs to be flushed */
|
|
|
|
static gint
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_jpeg_parse_get_image_length (GstJpegParse * parse, GstMapInfo * mapinfo)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
2010-05-31 16:30:19 +00:00
|
|
|
guint size;
|
|
|
|
gboolean resync;
|
|
|
|
gint offset, noffset;
|
2014-04-22 02:05:48 +00:00
|
|
|
GstByteReader reader;
|
2010-02-02 09:22:06 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
size = mapinfo->size;
|
|
|
|
gst_byte_reader_init (&reader, mapinfo->data, mapinfo->size);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
/* TODO could be removed as previous functions already guarantee this to be
|
|
|
|
* true */
|
2010-05-31 16:30:19 +00:00
|
|
|
/* we expect at least 4 bytes, first of which start marker */
|
2014-04-22 02:05:48 +00:00
|
|
|
if (gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000, 0xffd80000, 0,
|
|
|
|
4))
|
2010-05-31 16:30:19 +00:00
|
|
|
return 0;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
GST_DEBUG ("Parsing jpeg image data (%u bytes)", size);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
GST_DEBUG ("Parse state: offset=%d, resync=%d, entropy len=%d",
|
|
|
|
parse->priv->last_offset, parse->priv->last_resync,
|
|
|
|
parse->priv->last_entropy_len);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
/* offset is 2 less than actual offset;
|
|
|
|
* - adapter needs at least 4 bytes for scanning,
|
|
|
|
* - start and end marker ensure at least that much
|
|
|
|
*/
|
|
|
|
/* resume from state offset */
|
|
|
|
offset = parse->priv->last_offset;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
while (1) {
|
|
|
|
guint frame_len;
|
|
|
|
guint32 value;
|
|
|
|
|
|
|
|
noffset =
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_byte_reader_masked_scan_uint32_peek (&reader, 0x0000ff00,
|
|
|
|
0x0000ff00, offset, size - offset, &value);
|
2010-05-31 16:30:19 +00:00
|
|
|
/* lost sync if 0xff marker not where expected */
|
|
|
|
if ((resync = (noffset != offset))) {
|
|
|
|
GST_DEBUG ("Lost sync at 0x%08x, resyncing", offset + 2);
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
2010-05-31 16:30:19 +00:00
|
|
|
/* may have marker, but could have been resyncng */
|
|
|
|
resync = resync || parse->priv->last_resync;
|
|
|
|
/* Skip over extra 0xff */
|
2010-07-05 09:48:08 +00:00
|
|
|
while ((noffset >= 0) && ((value & 0xff) == 0xff)) {
|
2010-05-31 16:30:19 +00:00
|
|
|
noffset++;
|
|
|
|
noffset =
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_byte_reader_masked_scan_uint32_peek (&reader, 0x0000ff00,
|
|
|
|
0x0000ff00, noffset, size - noffset, &value);
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
2010-05-31 16:30:19 +00:00
|
|
|
/* enough bytes left for marker? (we need 0xNN after the 0xff) */
|
|
|
|
if (noffset < 0) {
|
|
|
|
GST_DEBUG ("at end of input and no EOI marker found, need more data");
|
|
|
|
goto need_more_data;
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
/* now lock on the marker we found */
|
|
|
|
offset = noffset;
|
|
|
|
value = value & 0xff;
|
|
|
|
if (value == 0xd9) {
|
|
|
|
GST_DEBUG ("0x%08x: EOI marker", offset + 2);
|
|
|
|
/* clear parse state */
|
|
|
|
parse->priv->last_resync = FALSE;
|
|
|
|
parse->priv->last_offset = 0;
|
|
|
|
return (offset + 4);
|
2010-12-06 17:19:20 +00:00
|
|
|
} else if (value == 0xd8) {
|
|
|
|
/* Skip this frame if we found another SOI marker */
|
|
|
|
GST_DEBUG ("0x%08x: SOI marker before EOI, skipping", offset + 2);
|
|
|
|
/* clear parse state */
|
|
|
|
parse->priv->last_resync = FALSE;
|
|
|
|
parse->priv->last_offset = 0;
|
|
|
|
return -(offset + 2);
|
2010-05-31 16:30:19 +00:00
|
|
|
}
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
if (value >= 0xd0 && value <= 0xd7)
|
|
|
|
frame_len = 0;
|
|
|
|
else {
|
|
|
|
/* peek tag and subsequent length */
|
|
|
|
if (offset + 2 + 4 > size)
|
|
|
|
goto need_more_data;
|
|
|
|
else
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_byte_reader_masked_scan_uint32_peek (&reader, 0x0, 0x0, offset + 2,
|
|
|
|
4, &frame_len);
|
2010-05-31 16:30:19 +00:00
|
|
|
frame_len = frame_len & 0xffff;
|
|
|
|
}
|
|
|
|
GST_DEBUG ("0x%08x: tag %02x, frame_len=%u", offset + 2, value, frame_len);
|
|
|
|
/* the frame length includes the 2 bytes for the length; here we want at
|
|
|
|
* least 2 more bytes at the end for an end marker */
|
|
|
|
if (offset + 2 + 2 + frame_len + 2 > size) {
|
|
|
|
goto need_more_data;
|
|
|
|
}
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
if (gst_jpeg_parse_parse_tag_has_entropy_segment (value)) {
|
|
|
|
guint eseglen = parse->priv->last_entropy_len;
|
|
|
|
|
|
|
|
GST_DEBUG ("0x%08x: finding entropy segment length", offset + 2);
|
|
|
|
noffset = offset + 2 + frame_len + eseglen;
|
|
|
|
while (1) {
|
2014-04-22 02:05:48 +00:00
|
|
|
noffset = gst_byte_reader_masked_scan_uint32_peek (&reader, 0x0000ff00,
|
2010-05-31 16:30:19 +00:00
|
|
|
0x0000ff00, noffset, size - noffset, &value);
|
|
|
|
if (noffset < 0) {
|
|
|
|
/* need more data */
|
|
|
|
parse->priv->last_entropy_len = size - offset - 4 - frame_len - 2;
|
|
|
|
goto need_more_data;
|
|
|
|
}
|
|
|
|
if ((value & 0xff) != 0x00) {
|
|
|
|
eseglen = noffset - offset - frame_len - 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
noffset++;
|
|
|
|
}
|
|
|
|
parse->priv->last_entropy_len = 0;
|
|
|
|
frame_len += eseglen;
|
|
|
|
GST_DEBUG ("entropy segment length=%u => frame_len=%u", eseglen,
|
|
|
|
frame_len);
|
|
|
|
}
|
|
|
|
if (resync) {
|
|
|
|
/* check if we will still be in sync if we interpret
|
|
|
|
* this as a sync point and skip this frame */
|
|
|
|
noffset = offset + frame_len + 2;
|
2014-04-22 02:05:48 +00:00
|
|
|
noffset =
|
|
|
|
gst_byte_reader_masked_scan_uint32 (&reader, 0x0000ff00, 0x0000ff00,
|
2010-05-31 16:30:19 +00:00
|
|
|
noffset, 4);
|
|
|
|
if (noffset < 0) {
|
|
|
|
/* ignore and continue resyncing until we hit the end
|
|
|
|
* of our data or find a sync point that looks okay */
|
2010-12-06 17:18:11 +00:00
|
|
|
offset++;
|
2010-05-31 16:30:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
GST_DEBUG ("found sync at 0x%x", offset + 2);
|
|
|
|
}
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
offset += frame_len + 2;
|
|
|
|
}
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2010-05-31 16:30:19 +00:00
|
|
|
/* EXITS */
|
|
|
|
need_more_data:
|
|
|
|
{
|
|
|
|
parse->priv->last_offset = offset;
|
|
|
|
parse->priv->last_resync = resync;
|
2009-08-19 10:22:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-17 09:49:30 +00:00
|
|
|
static inline gboolean
|
2009-08-19 10:22:30 +00:00
|
|
|
gst_jpeg_parse_sof (GstJpegParse * parse, GstByteReader * reader)
|
|
|
|
{
|
2010-03-11 16:58:25 +00:00
|
|
|
guint8 numcomps = 0; /* Number of components in image
|
2009-08-19 10:22:30 +00:00
|
|
|
(1 for gray, 3 for YUV, etc.) */
|
|
|
|
guint8 precision; /* precision (in bits) for the samples */
|
2011-06-04 19:35:03 +00:00
|
|
|
guint8 compId[3] G_GNUC_UNUSED; /* unique value identifying each component */
|
|
|
|
guint8 qtId[3] G_GNUC_UNUSED; /* quantization table ID to use for this comp */
|
2009-08-19 10:22:30 +00:00
|
|
|
guint8 blockWidth[3]; /* Array[numComponents] giving the number of
|
|
|
|
blocks (horiz) in this component */
|
|
|
|
guint8 blockHeight[3]; /* Same for the vertical part of this component */
|
2010-03-11 16:58:25 +00:00
|
|
|
guint8 i, value = 0;
|
2009-08-19 10:22:30 +00:00
|
|
|
gint temp;
|
|
|
|
|
2010-02-01 13:04:28 +00:00
|
|
|
/* flush length field */
|
2009-08-19 10:22:30 +00:00
|
|
|
if (!gst_byte_reader_skip (reader, 2))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Get sample precision */
|
|
|
|
if (!gst_byte_reader_get_uint8 (reader, &precision))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Get w and h */
|
|
|
|
if (!gst_byte_reader_get_uint16_be (reader, &parse->priv->height))
|
|
|
|
return FALSE;
|
|
|
|
if (!gst_byte_reader_get_uint16_be (reader, &parse->priv->width))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Get number of components */
|
|
|
|
if (!gst_byte_reader_get_uint8 (reader, &numcomps))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-06-04 19:35:03 +00:00
|
|
|
if (numcomps > 3) /* FIXME */
|
2009-08-19 10:22:30 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Get decimation and quantization table id for each component */
|
|
|
|
for (i = 0; i < numcomps; i++) {
|
|
|
|
/* Get component ID number */
|
|
|
|
if (!gst_byte_reader_get_uint8 (reader, &value))
|
|
|
|
return FALSE;
|
|
|
|
compId[i] = value;
|
|
|
|
|
|
|
|
/* Get decimation */
|
|
|
|
if (!gst_byte_reader_get_uint8 (reader, &value))
|
|
|
|
return FALSE;
|
|
|
|
blockWidth[i] = (value & 0xf0) >> 4;
|
|
|
|
blockHeight[i] = (value & 0x0f);
|
|
|
|
|
|
|
|
/* Get quantization table id */
|
|
|
|
if (!gst_byte_reader_get_uint8 (reader, &value))
|
|
|
|
return FALSE;
|
|
|
|
qtId[i] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numcomps == 1) {
|
2011-10-05 18:06:20 +00:00
|
|
|
/* gray image - no format */
|
|
|
|
parse->priv->format = "";
|
2009-08-19 10:22:30 +00:00
|
|
|
} else if (numcomps == 3) {
|
|
|
|
temp = (blockWidth[0] * blockHeight[0]) / (blockWidth[1] * blockHeight[1]);
|
|
|
|
|
|
|
|
if (temp == 4 && blockHeight[0] == 2)
|
2011-10-05 18:06:20 +00:00
|
|
|
parse->priv->format = "I420";
|
2009-08-19 10:22:30 +00:00
|
|
|
else if (temp == 4 && blockHeight[0] == 4)
|
2011-10-05 18:06:20 +00:00
|
|
|
parse->priv->format = "Y41B";
|
2009-08-19 10:22:30 +00:00
|
|
|
else if (temp == 2)
|
2011-10-05 18:06:20 +00:00
|
|
|
parse->priv->format = "UYVY";
|
2009-08-19 10:22:30 +00:00
|
|
|
else if (temp == 1)
|
2011-10-05 18:06:20 +00:00
|
|
|
parse->priv->format = "YV12";
|
2009-08-19 10:22:30 +00:00
|
|
|
else
|
2011-10-05 18:06:20 +00:00
|
|
|
parse->priv->format = "";
|
2009-08-19 10:22:30 +00:00
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-02-01 13:04:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse, "Header parsed");
|
|
|
|
|
2009-08-19 10:22:30 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-16 16:35:36 +00:00
|
|
|
static inline gboolean
|
|
|
|
gst_jpeg_parse_skip_marker (GstJpegParse * parse,
|
|
|
|
GstByteReader * reader, guint8 marker)
|
|
|
|
{
|
2011-01-06 11:51:27 +00:00
|
|
|
guint16 size = 0;
|
2010-11-16 16:35:36 +00:00
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint16_be (reader, &size))
|
|
|
|
return FALSE;
|
|
|
|
|
2012-09-12 07:10:35 +00:00
|
|
|
#ifndef GST_DISABLE_GST_DEBUG
|
2010-11-21 14:05:43 +00:00
|
|
|
/* We'd pry the id of the skipped application segment */
|
|
|
|
if (marker >= APP0 && marker <= APP15) {
|
|
|
|
const gchar *id_str = NULL;
|
|
|
|
|
2011-04-11 15:29:28 +00:00
|
|
|
if (gst_byte_reader_peek_string_utf8 (reader, &id_str)) {
|
|
|
|
GST_DEBUG_OBJECT (parse, "unhandled marker %x: '%s' skiping %u bytes",
|
|
|
|
marker, id_str ? id_str : "(NULL)", size);
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (parse, "unhandled marker %x skiping %u bytes", marker,
|
|
|
|
size);
|
|
|
|
}
|
2010-11-21 14:05:43 +00:00
|
|
|
}
|
|
|
|
#else
|
2011-04-11 15:29:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse, "unhandled marker %x skiping %u bytes", marker,
|
|
|
|
size);
|
2012-09-12 07:10:35 +00:00
|
|
|
#endif // GST_DISABLE_GST_DEBUG
|
2010-11-21 14:05:43 +00:00
|
|
|
|
|
|
|
if (!gst_byte_reader_skip (reader, size - 2))
|
|
|
|
return FALSE;
|
2010-11-16 16:35:36 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-21 14:09:17 +00:00
|
|
|
static inline GstTagList *
|
|
|
|
get_tag_list (GstJpegParse * parse)
|
|
|
|
{
|
|
|
|
if (!parse->priv->tags)
|
2012-09-11 23:36:24 +00:00
|
|
|
parse->priv->tags = gst_tag_list_new_empty ();
|
2010-11-21 14:09:17 +00:00
|
|
|
return parse->priv->tags;
|
|
|
|
}
|
|
|
|
|
2010-11-16 17:22:07 +00:00
|
|
|
static inline void
|
|
|
|
extract_and_queue_tags (GstJpegParse * parse, guint size, guint8 * data,
|
2011-10-05 18:06:20 +00:00
|
|
|
GstTagList * (*tag_func) (GstBuffer * buff))
|
2010-11-16 17:22:07 +00:00
|
|
|
{
|
|
|
|
GstTagList *tags;
|
|
|
|
GstBuffer *buf;
|
|
|
|
|
2012-09-11 23:36:24 +00:00
|
|
|
buf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, data, size, 0,
|
|
|
|
size, NULL, NULL);
|
2010-11-16 17:22:07 +00:00
|
|
|
|
|
|
|
tags = tag_func (buf);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
|
|
|
|
if (tags) {
|
|
|
|
GstTagList *taglist = parse->priv->tags;
|
|
|
|
if (taglist) {
|
|
|
|
gst_tag_list_insert (taglist, tags, GST_TAG_MERGE_REPLACE);
|
2012-08-04 15:31:30 +00:00
|
|
|
gst_tag_list_unref (tags);
|
2010-11-16 17:22:07 +00:00
|
|
|
} else {
|
|
|
|
parse->priv->tags = tags;
|
|
|
|
}
|
2011-04-11 15:29:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse, "collected tags: %" GST_PTR_FORMAT,
|
|
|
|
parse->priv->tags);
|
2010-11-16 17:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline gboolean
|
|
|
|
gst_jpeg_parse_app1 (GstJpegParse * parse, GstByteReader * reader)
|
|
|
|
{
|
2011-04-16 09:18:44 +00:00
|
|
|
guint16 size = 0;
|
2010-11-16 17:22:07 +00:00
|
|
|
const gchar *id_str;
|
|
|
|
const guint8 *data = NULL;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint16_be (reader, &size))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
size -= 2; /* 2 bytes for the mark */
|
|
|
|
if (!gst_byte_reader_peek_string_utf8 (reader, &id_str))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!strncmp (id_str, "Exif", 4)) {
|
|
|
|
|
|
|
|
/* skip id + NUL + padding */
|
|
|
|
if (!gst_byte_reader_skip (reader, 6))
|
|
|
|
return FALSE;
|
2011-04-11 15:31:45 +00:00
|
|
|
size -= 6;
|
2010-11-16 17:22:07 +00:00
|
|
|
|
|
|
|
/* handle exif metadata */
|
|
|
|
if (!gst_byte_reader_get_data (reader, size, &data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
extract_and_queue_tags (parse, size, (guint8 *) data,
|
|
|
|
gst_tag_list_from_exif_buffer_with_tiff_header);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (parse, "parsed marker %x: '%s' %u bytes",
|
|
|
|
APP1, id_str, size);
|
|
|
|
|
|
|
|
} else if (!strncmp (id_str, "http://ns.adobe.com/xap/1.0/", 28)) {
|
|
|
|
|
|
|
|
/* skip the id + NUL */
|
|
|
|
if (!gst_byte_reader_skip (reader, 29))
|
|
|
|
return FALSE;
|
2011-04-11 15:31:45 +00:00
|
|
|
size -= 29;
|
2010-11-16 17:22:07 +00:00
|
|
|
|
|
|
|
/* handle xmp metadata */
|
|
|
|
if (!gst_byte_reader_get_data (reader, size, &data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
extract_and_queue_tags (parse, size, (guint8 *) data,
|
|
|
|
gst_tag_list_from_xmp_buffer);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (parse, "parsed marker %x: '%s' %u bytes",
|
|
|
|
APP1, id_str, size);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!gst_jpeg_parse_skip_marker (parse, reader, APP1))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-13 10:38:02 +00:00
|
|
|
static inline gchar *
|
|
|
|
get_utf8_from_data (const guint8 * data, guint16 size)
|
|
|
|
{
|
|
|
|
const gchar *env_vars[] = { "GST_JPEG_TAG_ENCODING",
|
|
|
|
"GST_TAG_ENCODING", NULL
|
|
|
|
};
|
|
|
|
const char *str = (gchar *) data;
|
|
|
|
|
|
|
|
return gst_tag_freeform_string_to_utf8 (str, size, env_vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read comment and post as tag */
|
|
|
|
static inline gboolean
|
|
|
|
gst_jpeg_parse_com (GstJpegParse * parse, GstByteReader * reader)
|
|
|
|
{
|
|
|
|
const guint8 *data = NULL;
|
|
|
|
guint16 size = 0;
|
|
|
|
gchar *comment;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint16_be (reader, &size))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
size -= 2;
|
|
|
|
if (!gst_byte_reader_get_data (reader, size, &data))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
comment = get_utf8_from_data (data, size);
|
|
|
|
|
|
|
|
if (comment) {
|
|
|
|
GstTagList *taglist = get_tag_list (parse);
|
|
|
|
gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
|
|
|
|
GST_TAG_COMMENT, comment, NULL);
|
2011-04-11 15:29:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse, "collected tags: %" GST_PTR_FORMAT, taglist);
|
2010-08-13 10:38:02 +00:00
|
|
|
g_free (comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-08-19 10:22:30 +00:00
|
|
|
static gboolean
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_jpeg_parse_read_header (GstJpegParse * parse, GstMapInfo * map, gint len)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
2011-10-05 18:06:20 +00:00
|
|
|
GstByteReader reader;
|
2010-03-11 16:58:25 +00:00
|
|
|
guint8 marker = 0;
|
2009-08-19 10:22:30 +00:00
|
|
|
gboolean foundSOF = FALSE;
|
2011-10-05 18:06:20 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_byte_reader_init (&reader, map->data, len);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
if (!gst_byte_reader_peek_uint8 (&reader, &marker))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
while (marker == 0xff) {
|
|
|
|
if (!gst_byte_reader_skip (&reader, 1))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!gst_byte_reader_get_uint8 (&reader, &marker))
|
|
|
|
goto error;
|
|
|
|
|
2010-02-01 13:04:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse, "marker = %x", marker);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
switch (marker) {
|
|
|
|
case SOS: /* start of scan (begins compressed data) */
|
2011-10-05 18:06:20 +00:00
|
|
|
goto done;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
case SOI:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DRI:
|
|
|
|
if (!gst_byte_reader_skip (&reader, 4)) /* fixed size */
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
2010-08-13 10:38:02 +00:00
|
|
|
case COM:
|
|
|
|
if (!gst_jpeg_parse_com (parse, &reader))
|
2010-02-01 15:16:16 +00:00
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
2010-11-16 17:22:07 +00:00
|
|
|
case APP1:
|
|
|
|
if (!gst_jpeg_parse_app1 (parse, &reader))
|
2010-03-11 19:32:28 +00:00
|
|
|
goto error;
|
|
|
|
break;
|
2010-11-16 16:47:17 +00:00
|
|
|
|
2009-08-19 10:22:30 +00:00
|
|
|
case DHT:
|
|
|
|
case DQT:
|
|
|
|
/* Ignore these codes */
|
2010-11-16 16:35:36 +00:00
|
|
|
if (!gst_jpeg_parse_skip_marker (parse, &reader, marker))
|
2009-08-19 10:22:30 +00:00
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SOF0:
|
2010-02-01 13:04:28 +00:00
|
|
|
/* parse Start Of Frame */
|
2009-08-19 10:22:30 +00:00
|
|
|
if (!gst_jpeg_parse_sof (parse, &reader))
|
|
|
|
goto error;
|
|
|
|
|
2011-10-05 18:06:20 +00:00
|
|
|
foundSOF = TRUE;
|
|
|
|
goto done;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
default:
|
2014-04-22 02:05:48 +00:00
|
|
|
if (marker == JPG || (marker >= JPG0 && marker <= JPG13) ||
|
|
|
|
(marker >= APP0 && marker <= APP15)) {
|
2010-11-16 16:47:17 +00:00
|
|
|
if (!gst_jpeg_parse_skip_marker (parse, &reader, marker))
|
|
|
|
goto error;
|
2011-10-05 18:06:20 +00:00
|
|
|
} else
|
|
|
|
goto unhandled;
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_byte_reader_peek_uint8 (&reader, &marker))
|
|
|
|
goto error;
|
|
|
|
}
|
2011-10-05 18:06:20 +00:00
|
|
|
done:
|
2009-08-19 10:22:30 +00:00
|
|
|
|
|
|
|
return foundSOF;
|
|
|
|
|
2011-10-05 18:06:20 +00:00
|
|
|
/* ERRORS */
|
2009-08-19 10:22:30 +00:00
|
|
|
error:
|
2011-10-05 18:06:20 +00:00
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (parse,
|
|
|
|
"Error parsing image header (need more than %u bytes available)",
|
|
|
|
gst_byte_reader_get_remaining (&reader));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
unhandled:
|
|
|
|
{
|
|
|
|
GST_WARNING_OBJECT (parse, "unhandled marker %x, leaving", marker);
|
|
|
|
/* Not SOF or SOI. Must not be a JPEG file (or file pointer
|
|
|
|
* is placed wrong). In either case, it's an error. */
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_jpeg_parse_set_new_caps (GstJpegParse * parse, gboolean header_ok)
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
gboolean res;
|
|
|
|
|
2010-02-01 13:04:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse, "setting caps on srcpad (hdr_ok=%d, have_fps=%d)",
|
|
|
|
header_ok, parse->priv->has_fps);
|
|
|
|
|
2009-08-19 10:22:30 +00:00
|
|
|
caps = gst_caps_new_simple ("image/jpeg",
|
|
|
|
"parsed", G_TYPE_BOOLEAN, TRUE, NULL);
|
|
|
|
|
|
|
|
if (header_ok == TRUE) {
|
|
|
|
gst_caps_set_simple (caps,
|
2011-10-05 18:06:20 +00:00
|
|
|
"format", G_TYPE_STRING, parse->priv->format,
|
2009-08-19 10:22:30 +00:00
|
|
|
"width", G_TYPE_INT, parse->priv->width,
|
|
|
|
"height", G_TYPE_INT, parse->priv->height, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse->priv->has_fps == TRUE) {
|
|
|
|
/* we have a framerate */
|
|
|
|
gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
|
|
|
|
parse->priv->framerate_numerator,
|
|
|
|
parse->priv->framerate_denominator, NULL);
|
|
|
|
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (parse->priv->duration)
|
|
|
|
&& parse->priv->framerate_numerator != 0) {
|
|
|
|
parse->priv->duration = gst_util_uint64_scale_int (GST_SECOND,
|
2012-04-17 22:56:50 +00:00
|
|
|
parse->priv->framerate_denominator, parse->priv->framerate_numerator);
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* unknown duration */
|
|
|
|
parse->priv->duration = GST_CLOCK_TIME_NONE;
|
2010-02-01 15:46:29 +00:00
|
|
|
gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
2010-02-01 13:04:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse,
|
|
|
|
"setting downstream caps on %s:%s to %" GST_PTR_FORMAT,
|
2014-04-22 02:05:48 +00:00
|
|
|
GST_DEBUG_PAD_NAME (GST_BASE_PARSE_SRC_PAD (parse)), caps);
|
|
|
|
res = gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
|
2009-08-19 10:22:30 +00:00
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_jpeg_parse_pre_push_frame (GstBaseParse * bparse, GstBaseParseFrame * frame)
|
|
|
|
{
|
|
|
|
GstJpegParse *parse = GST_JPEG_PARSE_CAST (bparse);
|
|
|
|
GstBuffer *outbuf = frame->buffer;
|
|
|
|
|
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = parse->priv->next_ts;
|
|
|
|
|
|
|
|
if (parse->priv->has_fps && GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)
|
|
|
|
&& GST_CLOCK_TIME_IS_VALID (parse->priv->duration)) {
|
|
|
|
parse->priv->next_ts += parse->priv->duration;
|
|
|
|
} else {
|
|
|
|
parse->priv->duration = GST_CLOCK_TIME_NONE;
|
|
|
|
parse->priv->next_ts = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_BUFFER_DURATION (outbuf) = parse->priv->duration;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_jpeg_parse_handle_frame (GstBaseParse * bparse, GstBaseParseFrame * frame,
|
|
|
|
gint * skipsize)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
2014-04-22 02:05:48 +00:00
|
|
|
GstJpegParse *parse = GST_JPEG_PARSE_CAST (bparse);
|
|
|
|
gint len;
|
|
|
|
GstClockTime timestamp, duration;
|
|
|
|
GstMapInfo mapinfo;
|
2009-08-19 10:22:30 +00:00
|
|
|
gboolean header_ok;
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
timestamp = GST_BUFFER_PTS (frame->buffer);
|
|
|
|
duration = GST_BUFFER_DURATION (frame->buffer);
|
|
|
|
|
|
|
|
if (!gst_buffer_map (frame->buffer, &mapinfo, GST_MAP_READ)) {
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_jpeg_parse_skip_to_jpeg_header (parse, &mapinfo, skipsize)) {
|
|
|
|
gst_buffer_unmap (frame->buffer, &mapinfo);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)))
|
|
|
|
parse->priv->next_ts = timestamp;
|
|
|
|
|
|
|
|
if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (duration)))
|
|
|
|
parse->priv->duration = duration;
|
|
|
|
|
|
|
|
len = gst_jpeg_parse_get_image_length (parse, &mapinfo);
|
|
|
|
if (len == 0) {
|
|
|
|
gst_buffer_unmap (frame->buffer, &mapinfo);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
} else if (len < 0) {
|
|
|
|
*skipsize = -len;
|
|
|
|
gst_buffer_unmap (frame->buffer, &mapinfo);
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we already have a EOI */
|
|
|
|
GST_LOG_OBJECT (parse, "parsed image of size %d", len);
|
|
|
|
|
2010-02-02 09:22:06 +00:00
|
|
|
/* reset the offset (only when we flushed) */
|
2010-05-31 16:30:19 +00:00
|
|
|
parse->priv->last_offset = 0;
|
2010-04-29 14:33:44 +00:00
|
|
|
parse->priv->last_entropy_len = 0;
|
2010-02-02 09:22:06 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
header_ok = gst_jpeg_parse_read_header (parse, &mapinfo, len);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_buffer_unmap (frame->buffer, &mapinfo);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
if (parse->priv->width != parse->priv->caps_width
|
2009-08-19 10:22:30 +00:00
|
|
|
|| parse->priv->height != parse->priv->caps_height
|
|
|
|
|| parse->priv->framerate_numerator !=
|
|
|
|
parse->priv->caps_framerate_numerator
|
|
|
|
|| parse->priv->framerate_denominator !=
|
|
|
|
parse->priv->caps_framerate_denominator) {
|
|
|
|
if (!gst_jpeg_parse_set_new_caps (parse, header_ok)) {
|
|
|
|
GST_ELEMENT_ERROR (parse, CORE, NEGOTIATION,
|
|
|
|
("Can't set caps to the src pad"), ("Can't set caps to the src pad"));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-08-17 20:30:10 +00:00
|
|
|
if (parse->priv->tags) {
|
2011-04-11 15:29:28 +00:00
|
|
|
GST_DEBUG_OBJECT (parse, "Pushing tags: %" GST_PTR_FORMAT,
|
|
|
|
parse->priv->tags);
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (parse),
|
2012-09-11 23:36:24 +00:00
|
|
|
gst_event_new_tag (parse->priv->tags));
|
2010-08-17 20:30:10 +00:00
|
|
|
parse->priv->tags = NULL;
|
|
|
|
}
|
|
|
|
|
2009-08-19 10:22:30 +00:00
|
|
|
parse->priv->caps_width = parse->priv->width;
|
|
|
|
parse->priv->caps_height = parse->priv->height;
|
|
|
|
parse->priv->caps_framerate_numerator = parse->priv->framerate_numerator;
|
|
|
|
parse->priv->caps_framerate_denominator =
|
|
|
|
parse->priv->framerate_denominator;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
return gst_base_parse_finish_frame (bparse, frame, len);
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2014-04-22 02:05:48 +00:00
|
|
|
gst_jpeg_parse_sink_event (GstBaseParse * bparse, GstEvent * event)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
2014-04-22 02:05:48 +00:00
|
|
|
GstJpegParse *parse = GST_JPEG_PARSE_CAST (bparse);
|
2009-08-19 10:22:30 +00:00
|
|
|
gboolean res = TRUE;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (parse, "event : %s", GST_EVENT_TYPE_NAME (event));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2010-05-31 16:30:36 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
parse->priv->next_ts = GST_CLOCK_TIME_NONE;
|
2012-04-18 02:46:12 +00:00
|
|
|
parse->priv->duration = GST_CLOCK_TIME_NONE;
|
2010-05-31 16:30:36 +00:00
|
|
|
parse->priv->last_offset = 0;
|
|
|
|
parse->priv->last_entropy_len = 0;
|
|
|
|
parse->priv->last_resync = FALSE;
|
2009-08-19 10:22:30 +00:00
|
|
|
break;
|
2010-08-17 20:30:10 +00:00
|
|
|
case GST_EVENT_TAG:{
|
2014-04-22 02:05:48 +00:00
|
|
|
if (gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (parse)))
|
|
|
|
res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (bparse, event);
|
2010-08-17 20:30:10 +00:00
|
|
|
else {
|
2010-12-13 15:23:37 +00:00
|
|
|
GstTagList *taglist = NULL;
|
|
|
|
|
|
|
|
gst_event_parse_tag (event, &taglist);
|
2010-08-17 20:30:10 +00:00
|
|
|
/* Hold on to the tags till the srcpad caps are definitely set */
|
2010-11-21 14:09:17 +00:00
|
|
|
gst_tag_list_insert (get_tag_list (parse), taglist,
|
|
|
|
GST_TAG_MERGE_REPLACE);
|
2011-04-11 15:29:28 +00:00
|
|
|
GST_DEBUG ("collected tags: %" GST_PTR_FORMAT, parse->priv->tags);
|
2010-12-13 15:23:37 +00:00
|
|
|
gst_event_unref (event);
|
2010-08-17 20:30:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-08-19 10:22:30 +00:00
|
|
|
default:
|
2014-04-22 02:05:48 +00:00
|
|
|
res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (bparse, event);
|
2009-08-19 10:22:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
static gboolean
|
|
|
|
gst_jpeg_parse_start (GstBaseParse * bparse)
|
2009-08-19 10:22:30 +00:00
|
|
|
{
|
|
|
|
GstJpegParse *parse;
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse = GST_JPEG_PARSE_CAST (bparse);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse->priv->has_fps = FALSE;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse->priv->width = parse->priv->height = 0;
|
|
|
|
parse->priv->framerate_numerator = 0;
|
|
|
|
parse->priv->framerate_denominator = 1;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse->priv->caps_framerate_numerator =
|
|
|
|
parse->priv->caps_framerate_denominator = 0;
|
|
|
|
parse->priv->caps_width = parse->priv->caps_height = -1;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse->priv->next_ts = GST_CLOCK_TIME_NONE;
|
|
|
|
parse->priv->duration = GST_CLOCK_TIME_NONE;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse->priv->last_offset = 0;
|
|
|
|
parse->priv->last_entropy_len = 0;
|
|
|
|
parse->priv->last_resync = FALSE;
|
2010-02-02 09:22:06 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse->priv->tags = NULL;
|
2010-08-17 20:30:10 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_jpeg_parse_stop (GstBaseParse * bparse)
|
|
|
|
{
|
|
|
|
GstJpegParse *parse;
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
parse = GST_JPEG_PARSE_CAST (bparse);
|
2009-08-19 10:22:30 +00:00
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
if (parse->priv->tags) {
|
|
|
|
gst_tag_list_unref (parse->priv->tags);
|
|
|
|
parse->priv->tags = NULL;
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 02:05:48 +00:00
|
|
|
return TRUE;
|
2009-08-19 10:22:30 +00:00
|
|
|
}
|