1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* GStreamer PNG Parser
* Copyright (C) <2013> Collabora Ltd
* @author Olivier Crete <olivier.crete@collabora.com>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "gstpngparse.h"
#include <gst/base/base.h>
#include <gst/pbutils/pbutils.h>
#define PNG_SIGNATURE G_GUINT64_CONSTANT (0x89504E470D0A1A0A)
GST_DEBUG_CATEGORY (png_parse_debug);
#define GST_CAT_DEFAULT png_parse_debug
static GstStaticPadTemplate srctemplate =
GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("image/png, width = (int)[1, MAX], height = (int)[1, MAX],"
"parsed = (boolean) true")
);
static GstStaticPadTemplate sinktemplate =
GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("image/png")
);
#define parent_class gst_png_parse_parent_class
G_DEFINE_TYPE (GstPngParse, gst_png_parse, GST_TYPE_BASE_PARSE);
static gboolean gst_png_parse_start (GstBaseParse * parse);
static gboolean gst_png_parse_event (GstBaseParse * parse, GstEvent * event);
static GstFlowReturn gst_png_parse_handle_frame (GstBaseParse * parse,
GstBaseParseFrame * frame, gint * skipsize);
static GstFlowReturn gst_png_parse_pre_push_frame (GstBaseParse * parse,
GstBaseParseFrame * frame);
static void
gst_png_parse_class_init (GstPngParseClass * klass)
{
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
GST_DEBUG_CATEGORY_INIT (png_parse_debug, "pngparse", 0, "png parser");
gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
gst_element_class_set_static_metadata (gstelement_class, "PNG parser",
"Codec/Parser/Video/Image",
"Parses PNG files", "Olivier Crete <olivier.crete@collabora.com>");
/* Override BaseParse vfuncs */
parse_class->start = GST_DEBUG_FUNCPTR (gst_png_parse_start);
parse_class->sink_event = GST_DEBUG_FUNCPTR (gst_png_parse_event);
parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_png_parse_handle_frame);
parse_class->pre_push_frame =
GST_DEBUG_FUNCPTR (gst_png_parse_pre_push_frame);
}
static void
gst_png_parse_init (GstPngParse * pngparse)
{
GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (pngparse));
GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (pngparse));
}
static gboolean
gst_png_parse_start (GstBaseParse * parse)
{
GstPngParse *pngparse = GST_PNG_PARSE (parse);
GST_DEBUG_OBJECT (pngparse, "start");
/* the start code and at least 2 empty frames (IHDR and IEND) */
gst_base_parse_set_min_frame_size (parse, 8 + 12 + 12);
pngparse->width = 0;
pngparse->height = 0;
pngparse->sent_codec_tag = FALSE;
return TRUE;
}
static gboolean
gst_png_parse_event (GstBaseParse * parse, GstEvent * event)
{
gboolean res;
res = GST_BASE_PARSE_CLASS (parent_class)->sink_event (parse, event);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_FLUSH_STOP:
/* the start code and at least 2 empty frames (IHDR and IEND) */
gst_base_parse_set_min_frame_size (parse, 8 + 12 + 12);
break;
default:
break;
}
return res;
}
static GstFlowReturn
gst_png_parse_handle_frame (GstBaseParse * parse,
GstBaseParseFrame * frame, gint * skipsize)
{
GstPngParse *pngparse = GST_PNG_PARSE (parse);
GstMapInfo map;
GstByteReader reader;
GstFlowReturn ret = GST_FLOW_OK;
guint64 signature;
guint width = 0, height = 0;
gst_buffer_map (frame->buffer, &map, GST_MAP_READ);
gst_byte_reader_init (&reader, map.data, map.size);
if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
goto beach;
if (signature != PNG_SIGNATURE) {
for (;;) {
guint offset;
offset = gst_byte_reader_masked_scan_uint32 (&reader, 0xffffffff,
0x89504E47, 0, gst_byte_reader_get_remaining (&reader));
if (offset == -1) {
*skipsize = gst_byte_reader_get_remaining (&reader) - 4;
goto beach;
}
gst_byte_reader_skip (&reader, offset);
if (!gst_byte_reader_peek_uint64_be (&reader, &signature))
goto beach;
if (signature == PNG_SIGNATURE) {
/* We're skipping, go out, we'll be back */
*skipsize = gst_byte_reader_get_pos (&reader);
goto beach;
}
gst_byte_reader_skip (&reader, 4);
}
}
gst_byte_reader_skip (&reader, 8);
for (;;) {
guint32 length;
guint32 code;
if (!gst_byte_reader_get_uint32_be (&reader, &length))
goto beach;
if (!gst_byte_reader_get_uint32_le (&reader, &code))
goto beach;
GST_TRACE_OBJECT (parse, "%" GST_FOURCC_FORMAT " chunk, %u bytes",
GST_FOURCC_ARGS (code), length);
if (code == GST_MAKE_FOURCC ('I', 'H', 'D', 'R')) {
if (!gst_byte_reader_get_uint32_be (&reader, &width))
goto beach;
if (!gst_byte_reader_get_uint32_be (&reader, &height))
goto beach;
length -= 8;
} else if (code == GST_MAKE_FOURCC ('I', 'D', 'A', 'T')) {
gst_base_parse_set_min_frame_size (parse,
gst_byte_reader_get_pos (&reader) + 4 + length + 12);
}
if (!gst_byte_reader_skip (&reader, length + 4))
goto beach;
if (code == GST_MAKE_FOURCC ('I', 'E', 'N', 'D')) {
/* the start code and at least 2 empty frames (IHDR and IEND) */
gst_base_parse_set_min_frame_size (parse, 8 + 12 + 12);
if (pngparse->width != width || pngparse->height != height) {
GstCaps *caps, *sink_caps;
pngparse->height = height;
pngparse->width = width;
caps = gst_caps_new_simple ("image/png",
"width", G_TYPE_INT, width, "height", G_TYPE_INT, height, NULL);
sink_caps =
gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (pngparse));
if (sink_caps) {
GstStructure *st;
gint fr_num, fr_denom;
st = gst_caps_get_structure (sink_caps, 0);
if (st
&& gst_structure_get_fraction (st, "framerate", &fr_num,
&fr_denom)) {
gst_caps_set_simple (caps,
"framerate", GST_TYPE_FRACTION, fr_num, fr_denom, NULL);
} else {
GST_WARNING_OBJECT (pngparse, "No framerate set");
}
gst_caps_unref (sink_caps);
}
if (!gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps))
ret = GST_FLOW_NOT_NEGOTIATED;
gst_caps_unref (caps);
if (ret != GST_FLOW_OK)
goto beach;
}
gst_buffer_unmap (frame->buffer, &map);
return gst_base_parse_finish_frame (parse, frame,
gst_byte_reader_get_pos (&reader));
}
}
beach:
gst_buffer_unmap (frame->buffer, &map);
return ret;
}
static GstFlowReturn
gst_png_parse_pre_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
{
GstPngParse *pngparse = GST_PNG_PARSE (parse);
if (!pngparse->sent_codec_tag) {
GstTagList *taglist;
GstCaps *caps;
/* codec tag */
caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
if (G_UNLIKELY (caps == NULL)) {
if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
GST_INFO_OBJECT (parse, "Src pad is flushing");
return GST_FLOW_FLUSHING;
} else {
GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
return GST_FLOW_NOT_NEGOTIATED;
}
}
taglist = gst_tag_list_new_empty ();
gst_pb_utils_add_codec_description_to_tag_list (taglist,
GST_TAG_VIDEO_CODEC, caps);
gst_caps_unref (caps);
gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
gst_tag_list_unref (taglist);
/* also signals the end of first-frame processing */
pngparse->sent_codec_tag = TRUE;
}
return GST_FLOW_OK;
}
|