summaryrefslogtreecommitdiffstats
path: root/ext/libpng/gstpngdec.c
blob: e0d2270f63497a4b34bf8b2bb3d7d64e872e4372 (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * 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.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstpngdec.h"

#include <string.h>
#include <gst/video/video.h>

static GstElementDetails gst_pngdec_details = {
  "PNG decoder",
  "Codec/Decoder/Image",
  "Decode a png video frame to a raw image",
  "Wim Taymans <wim@fluendo.com>",
};

GST_DEBUG_CATEGORY (pngdec_debug);
#define GST_CAT_DEFAULT pngdec_debug

static void gst_pngdec_base_init (gpointer g_class);
static void gst_pngdec_class_init (GstPngDecClass * klass);
static void gst_pngdec_init (GstPngDec * pngdec);

static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buf);

static GstElementClass *parent_class = NULL;

static void
user_error_fn (png_structp png_ptr, png_const_charp error_msg)
{
  g_warning ("%s", error_msg);
}

static void
user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
{
  g_warning ("%s", warning_msg);
}

GType
gst_pngdec_get_type (void)
{
  static GType pngdec_type = 0;

  if (!pngdec_type) {
    static const GTypeInfo pngdec_info = {
      sizeof (GstPngDecClass),
      gst_pngdec_base_init,
      NULL,
      (GClassInitFunc) gst_pngdec_class_init,
      NULL,
      NULL,
      sizeof (GstPngDec),
      0,
      (GInstanceInitFunc) gst_pngdec_init,
    };

    pngdec_type = g_type_register_static (GST_TYPE_ELEMENT, "GstPngDec",
        &pngdec_info, 0);
  }
  return pngdec_type;
}

static GstStaticPadTemplate gst_pngdec_src_pad_template =
    GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
    );

static GstStaticPadTemplate gst_pngdec_sink_pad_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("image/png, "
        "width = (int) [ 16, 4096 ], "
        "height = (int) [ 16, 4096 ], " "framerate = (double) [ 0.0, MAX ]")
    );

static void
gst_pngdec_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_pngdec_src_pad_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_pngdec_sink_pad_template));
  gst_element_class_set_details (element_class, &gst_pngdec_details);
}

static void
gst_pngdec_class_init (GstPngDecClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

  GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
}


static void
gst_pngdec_init (GstPngDec * pngdec)
{
  pngdec->sinkpad = gst_pad_new_from_template (gst_static_pad_template_get
      (&gst_pngdec_sink_pad_template), "sink");
  gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->sinkpad);

  pngdec->srcpad = gst_pad_new_from_template (gst_static_pad_template_get
      (&gst_pngdec_src_pad_template), "src");
  gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);

  gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);

  pngdec->png = NULL;
  pngdec->info = NULL;

  pngdec->color_type = -1;
  pngdec->width = -1;
  pngdec->height = -1;
  pngdec->fps = 0.0;
}

static void
user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
{
  GstPngDec *dec;

  dec = GST_PNGDEC (png_ptr->io_ptr);

  GST_LOG ("reading %d bytes of data at offset %d", length, dec->offset);

  if (GST_BUFFER_SIZE (dec->buffer_in) < dec->offset + length) {
    g_warning ("reading past end of buffer");
  }

  memcpy (data, GST_BUFFER_DATA (dec->buffer_in) + dec->offset, length);

  dec->offset += length;
}

#define ROUND_UP_4(x) (((x) + 3) & ~3)

static GstFlowReturn
gst_pngdec_chain (GstPad * pad, GstBuffer * buf)
{
  GstFlowReturn ret = GST_FLOW_OK;
  GstPngDec *pngdec;
  png_uint_32 width, height;
  gint depth, color;
  png_bytep *rows, inp;
  GstBuffer *out;
  gint i;

  GST_LOG ("chain");

  pngdec = GST_PNGDEC (gst_pad_get_parent (pad));

  if (!GST_PAD_IS_USABLE (pngdec->srcpad)) {
    gst_buffer_unref (buf);
    ret = GST_FLOW_UNEXPECTED;
    goto beach;
  }

  pngdec->buffer_in = buf;
  pngdec->offset = 0;

  /* initialize png struct stuff */
  pngdec->png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
      (png_voidp) NULL, user_error_fn, user_warning_fn);

  if (pngdec->png == NULL) {
    gst_buffer_unref (buf);
    GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
        ("Failed to initialize png structure"));
    ret = GST_FLOW_UNEXPECTED;
    goto beach;
  }

  pngdec->info = png_create_info_struct (pngdec->png);
  if (pngdec->info == NULL) {
    gst_buffer_unref (buf);
    png_destroy_read_struct (&(pngdec->png), (png_infopp) NULL,
        (png_infopp) NULL);
    GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
        ("Failed to initialize info structure"));
    ret = GST_FLOW_UNEXPECTED;
    goto beach;
  }

  pngdec->endinfo = png_create_info_struct (pngdec->png);
  if (pngdec->endinfo == NULL) {
    gst_buffer_unref (buf);
    png_destroy_read_struct (&pngdec->png, &pngdec->info, (png_infopp) NULL);
    GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
        ("Failed to initialize endinfo structure"));
    ret = GST_FLOW_UNEXPECTED;
    goto beach;
  }

  /* non-0 return is from a longjmp inside of libpng */
  if (setjmp (pngdec->png->jmpbuf) != 0) {
    gst_buffer_unref (buf);
    png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
    GST_ELEMENT_ERROR (pngdec, LIBRARY, FAILED, (NULL),
        ("returning from longjmp"));
    ret = GST_FLOW_UNEXPECTED;
    goto beach;
  }

  png_set_read_fn (pngdec->png, pngdec, user_read_data);

  png_read_info (pngdec->png, pngdec->info);
  png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
      &depth, &color, NULL, NULL, NULL);

  if (pngdec->info->bit_depth != 8) {
    gst_buffer_unref (buf);
    png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
    GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
        ("pngdec only supports 8 bit images for now"));
    ret = GST_FLOW_UNEXPECTED;
    goto beach;
  }

  if (pngdec->width != width ||
      pngdec->height != height ||
      pngdec->color_type != pngdec->info->color_type) {
    GstCaps *caps, *templ, *res;
    gboolean ret;
    gint bpp;

    GST_LOG ("this is a %dx%d PNG image", width, height);
    pngdec->width = width;
    pngdec->height = height;
    pngdec->color_type = pngdec->info->color_type;

    switch (pngdec->color_type) {
      case PNG_COLOR_TYPE_RGB:
        GST_LOG ("we have no alpha channel, depth is 24 bits");
        bpp = 24;
        break;
      case PNG_COLOR_TYPE_RGB_ALPHA:
        GST_LOG ("we have an alpha channel, depth is 32 bits");
        bpp = 32;
        break;
      default:
        GST_ELEMENT_ERROR (pngdec, STREAM, NOT_IMPLEMENTED, (NULL),
            ("pngdec does not support grayscale or paletted data yet"));
        ret = GST_FLOW_UNEXPECTED;
        goto beach;
    }

    caps = gst_caps_new_simple ("video/x-raw-rgb",
        "width", G_TYPE_INT, width,
        "height", G_TYPE_INT, height,
        "bpp", G_TYPE_INT, bpp, "framerate", G_TYPE_DOUBLE, pngdec->fps, NULL);

    templ = gst_caps_copy (gst_pad_template_get_caps
        (gst_static_pad_template_get (&gst_pngdec_src_pad_template)));

    res = gst_caps_intersect (templ, caps);
    gst_caps_unref (caps);
    gst_caps_unref (templ);
    {
      gchar *caps_str = gst_caps_to_string (res);

      GST_LOG ("intersecting caps with template gave %s", caps_str);
      g_free (caps_str);
    }
    ret = gst_pad_set_caps (pngdec->srcpad, res);
    gst_caps_unref (res);

    if (!ret) {
      gst_buffer_unref (buf);
      png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);
      GST_ELEMENT_ERROR (pngdec, CORE, NEGOTIATION, (NULL), (NULL));
      ret = GST_FLOW_UNEXPECTED;
      goto beach;
    }
  }

  rows = (png_bytep *) g_malloc (sizeof (png_bytep) * height);

  ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
      height * ROUND_UP_4 (pngdec->info->rowbytes),
      GST_PAD_CAPS (pngdec->srcpad), &out);

  if (ret != GST_FLOW_OK) {
    goto beach;
  }

  inp = GST_BUFFER_DATA (out);
  for (i = 0; i < height; i++) {
    rows[i] = inp;
    inp += ROUND_UP_4 (pngdec->info->rowbytes);
  }

  png_read_image (pngdec->png, rows);
  free (rows);

  png_destroy_info_struct (pngdec->png, &pngdec->info);
  png_destroy_read_struct (&pngdec->png, &pngdec->info, &pngdec->endinfo);

  pngdec->buffer_in = NULL;
  gst_buffer_unref (buf);

  /* Pushing */
  GST_LOG ("pushing our raw RGB frame of %d bytes", GST_BUFFER_SIZE (out));
  ret = gst_pad_push (pngdec->srcpad, out);

beach:
  return ret;
}