1 /* GStreamer PNG Parser
2 * Copyright (C) <2013> Collabora Ltd
3 * @author Olivier Crete <olivier.crete@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "gstvideoparserselements.h"
26 #include "gstpngparse.h"
28 #include <gst/base/base.h>
29 #include <gst/pbutils/pbutils.h>
31 #define PNG_SIGNATURE G_GUINT64_CONSTANT (0x89504E470D0A1A0A)
33 GST_DEBUG_CATEGORY (png_parse_debug);
34 #define GST_CAT_DEFAULT png_parse_debug
36 static GstStaticPadTemplate srctemplate =
37 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
39 GST_STATIC_CAPS ("image/png, width = (int)[1, MAX], height = (int)[1, MAX],"
40 "parsed = (boolean) true")
43 static GstStaticPadTemplate sinktemplate =
44 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
46 GST_STATIC_CAPS ("image/png")
49 #define parent_class gst_png_parse_parent_class
50 G_DEFINE_TYPE (GstPngParse, gst_png_parse, GST_TYPE_BASE_PARSE);
51 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (pngparse, "pngparse", GST_RANK_PRIMARY,
52 GST_TYPE_PNG_PARSE, videoparsers_element_init (plugin));
54 static gboolean gst_png_parse_start (GstBaseParse * parse);
55 static gboolean gst_png_parse_event (GstBaseParse * parse, GstEvent * event);
56 static GstFlowReturn gst_png_parse_handle_frame (GstBaseParse * parse,
57 GstBaseParseFrame * frame, gint * skipsize);
58 static GstFlowReturn gst_png_parse_pre_push_frame (GstBaseParse * parse,
59 GstBaseParseFrame * frame);
62 gst_png_parse_class_init (GstPngParseClass * klass)
64 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
65 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
67 GST_DEBUG_CATEGORY_INIT (png_parse_debug, "pngparse", 0, "png parser");
69 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
70 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
71 gst_element_class_set_static_metadata (gstelement_class, "PNG parser",
72 "Codec/Parser/Video/Image",
73 "Parses PNG files", "Olivier Crete <olivier.crete@collabora.com>");
75 /* Override BaseParse vfuncs */
76 parse_class->start = GST_DEBUG_FUNCPTR (gst_png_parse_start);
77 parse_class->sink_event = GST_DEBUG_FUNCPTR (gst_png_parse_event);
78 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_png_parse_handle_frame);
79 parse_class->pre_push_frame =
80 GST_DEBUG_FUNCPTR (gst_png_parse_pre_push_frame);
84 gst_png_parse_init (GstPngParse * pngparse)
86 GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (pngparse));
87 GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (pngparse));
91 gst_png_parse_start (GstBaseParse * parse)
93 GstPngParse *pngparse = GST_PNG_PARSE (parse);
95 GST_DEBUG_OBJECT (pngparse, "start");
97 /* the start code and at least 2 empty frames (IHDR and IEND) */
98 gst_base_parse_set_min_frame_size (parse, 8 + 12 + 12);
101 pngparse->height = 0;
103 pngparse->sent_codec_tag = FALSE;
109 gst_png_parse_event (GstBaseParse * parse, GstEvent * event)
113 res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
115 switch (GST_EVENT_TYPE (event)) {
116 case GST_EVENT_FLUSH_STOP:
117 /* the start code and at least 2 empty frames (IHDR and IEND) */
118 gst_base_parse_set_min_frame_size (parse, 8 + 12 + 12);
128 gst_png_parse_handle_frame (GstBaseParse * parse,
129 GstBaseParseFrame * frame, gint * skipsize)
131 GstPngParse *pngparse = GST_PNG_PARSE (parse);
133 GstByteReader reader;
134 GstFlowReturn ret = GST_FLOW_OK;
136 guint width = 0, height = 0;
138 gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
139 gst_byte_reader_init (&reader, map.data, map.size);
141 if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
144 if (signature != PNG_SIGNATURE) {
148 offset = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff,
149 0x89504E47, 0, gst_byte_reader_get_remaining (&reader));
152 *skipsize = gst_byte_reader_get_remaining (&reader) - 4;
156 gst_byte_reader_skip (&reader, offset);
158 if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
161 if (signature == PNG_SIGNATURE) {
162 /* We're skipping, go out, we'll be back */
163 *skipsize = gst_byte_reader_get_pos (&reader);
166 gst_byte_reader_skip (&reader, 4);
170 gst_byte_reader_skip (&reader, 8);
176 if (!gst_byte_reader_get_uint32_be (&reader, &length))
178 if (!gst_byte_reader_get_uint32_le (&reader, &code))
181 GST_TRACE_OBJECT (parse, "%" GST_FOURCC_FORMAT " chunk, %u bytes",
182 GST_FOURCC_ARGS (code), length);
184 if (code == GST_MAKE_FOURCC ('I', 'H', 'D', 'R')) {
185 if (!gst_byte_reader_get_uint32_be (&reader, &width))
187 if (!gst_byte_reader_get_uint32_be (&reader, &height))
190 } else if (code == GST_MAKE_FOURCC ('I', 'D', 'A', 'T')) {
191 gst_base_parse_set_min_frame_size (parse,
192 gst_byte_reader_get_pos (&reader) + 4 + length + 12);
195 if (!gst_byte_reader_skip (&reader, length + 4))
198 if (code == GST_MAKE_FOURCC ('I', 'E', 'N', 'D')) {
199 /* the start code and at least 2 empty frames (IHDR and IEND) */
200 gst_base_parse_set_min_frame_size (parse, 8 + 12 + 12);
202 if (pngparse->width != width || pngparse->height != height) {
203 GstCaps *caps, *sink_caps;
205 pngparse->height = height;
206 pngparse->width = width;
208 caps = gst_caps_new_simple ("image/png",
209 "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL);
212 gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (pngparse));
216 gint fr_num, fr_denom;
218 st = gst_caps_get_structure (sink_caps, 0);
220 && gst_structure_get_fraction (st, "framerate", &fr_num,
222 gst_caps_set_simple (caps,
223 "framerate", GST_TYPE_FRACTION, fr_num, fr_denom, NULL);
225 GST_WARNING_OBJECT (pngparse, "No framerate set");
228 gst_caps_unref (sink_caps);
231 if (!gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps))
232 ret = GST_FLOW_NOT_NEGOTIATED;
234 gst_caps_unref (caps);
236 if (ret != GST_FLOW_OK)
241 gst_buffer_unmap (frame->buffer, &map);
242 return gst_base_parse_finish_frame (parse, frame,
243 gst_byte_reader_get_pos (&reader));
249 gst_buffer_unmap (frame->buffer, &map);
255 gst_png_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
257 GstPngParse *pngparse = GST_PNG_PARSE (parse);
259 if (!pngparse->sent_codec_tag) {
264 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
265 if (G_UNLIKELY (caps == NULL)) {
266 if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
267 GST_INFO_OBJECT (parse, "Src pad is flushing");
268 return GST_FLOW_FLUSHING;
270 GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
271 return GST_FLOW_NOT_NEGOTIATED;
275 taglist = gst_tag_list_new_empty ();
276 gst_pb_utils_add_codec_description_to_tag_list (taglist,
277 GST_TAG_VIDEO_CODEC, caps);
278 gst_caps_unref (caps);
280 gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
281 gst_tag_list_unref (taglist);
283 /* also signals the end of first-frame processing */
284 pngparse->sent_codec_tag = TRUE;