gstpngdec.c 17.1 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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
19

Wim Taymans's avatar
Wim Taymans committed
20
#include "gstpngdec.h"
21 22

#include <string.h>
Wim Taymans's avatar
Wim Taymans committed
23
#include <gst/video/video.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
24
#include <gst/gst-i18n-plugin.h>
Wim Taymans's avatar
Wim Taymans committed
25 26 27 28 29 30 31 32

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

33 34
GST_DEBUG_CATEGORY (pngdec_debug);
#define GST_CAT_DEFAULT pngdec_debug
Wim Taymans's avatar
Wim Taymans committed
35 36 37 38 39

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

40 41
static gboolean gst_pngdec_libpng_init (GstPngDec * pngdec);
static gboolean gst_pngdec_libpng_clear (GstPngDec * pngdec);
Wim Taymans's avatar
Wim Taymans committed
42

43 44
static GstStateChangeReturn gst_pngdec_change_state (GstElement * element,
    GstStateChange transition);
Wim Taymans's avatar
Wim Taymans committed
45

46 47 48 49 50
static gboolean gst_pngdec_sink_activate_push (GstPad * sinkpad,
    gboolean active);
static gboolean gst_pngdec_sink_activate_pull (GstPad * sinkpad,
    gboolean active);
static gboolean gst_pngdec_sink_activate (GstPad * sinkpad);
Wim Taymans's avatar
Wim Taymans committed
51

52 53 54 55 56 57 58
static GstFlowReturn gst_pngdec_caps_create_and_set (GstPngDec * pngdec);

static void gst_pngdec_task (GstPad * pad);
static GstFlowReturn gst_pngdec_chain (GstPad * pad, GstBuffer * buffer);
static gboolean gst_pngdec_sink_event (GstPad * pad, GstEvent * event);

static GstElementClass *parent_class = NULL;
Wim Taymans's avatar
Wim Taymans committed
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

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,
88
    GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBA ";" GST_VIDEO_CAPS_RGB)
Wim Taymans's avatar
Wim Taymans committed
89 90 91 92 93 94
    );

static GstStaticPadTemplate gst_pngdec_sink_pad_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
95
    GST_STATIC_CAPS ("image/png")
Wim Taymans's avatar
Wim Taymans committed
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
    );

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);

121 122
  gstelement_class->change_state = gst_pngdec_change_state;

123
  GST_DEBUG_CATEGORY_INIT (pngdec_debug, "pngdec", 0, "PNG image decoder");
Wim Taymans's avatar
Wim Taymans committed
124 125
}

126

Wim Taymans's avatar
Wim Taymans committed
127 128 129 130 131
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");
132 133 134 135 136 137 138
  gst_pad_set_activate_function (pngdec->sinkpad, gst_pngdec_sink_activate);
  gst_pad_set_activatepush_function (pngdec->sinkpad,
      gst_pngdec_sink_activate_push);
  gst_pad_set_activatepull_function (pngdec->sinkpad,
      gst_pngdec_sink_activate_pull);
  gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain);
  gst_pad_set_event_function (pngdec->sinkpad, gst_pngdec_sink_event);
139
  gst_pad_use_fixed_caps (pngdec->sinkpad);
Wim Taymans's avatar
Wim Taymans committed
140 141 142 143
  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");
144
  gst_pad_use_fixed_caps (pngdec->srcpad);
Wim Taymans's avatar
Wim Taymans committed
145 146
  gst_element_add_pad (GST_ELEMENT (pngdec), pngdec->srcpad);

147
  /* gst_pad_set_chain_function (pngdec->sinkpad, gst_pngdec_chain); */
Wim Taymans's avatar
Wim Taymans committed
148

149
  pngdec->buffer_out = NULL;
Wim Taymans's avatar
Wim Taymans committed
150 151
  pngdec->png = NULL;
  pngdec->info = NULL;
152 153
  pngdec->endinfo = NULL;
  pngdec->setup = FALSE;
Wim Taymans's avatar
Wim Taymans committed
154 155 156 157

  pngdec->color_type = -1;
  pngdec->width = -1;
  pngdec->height = -1;
158
  pngdec->bpp = -1;
159
  pngdec->fps = 0.0;
Wim Taymans's avatar
Wim Taymans committed
160 161 162
}

static void
163
user_error_fn (png_structp png_ptr, png_const_charp error_msg)
Wim Taymans's avatar
Wim Taymans committed
164
{
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  GST_ERROR ("%s", error_msg);
}

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

static void
user_info_callback (png_structp png_ptr, png_infop info)
{
  GstPngDec *pngdec = NULL;
  GstFlowReturn ret = GST_FLOW_OK;
  gint bpc = 0;
  png_uint_32 width, height;
  size_t buffer_size;
  GstBuffer *buffer = NULL;

  pngdec = GST_PNGDEC (png_ptr->io_ptr);
Wim Taymans's avatar
Wim Taymans committed
185

186
  GST_LOG ("info ready");
Wim Taymans's avatar
Wim Taymans committed
187

188 189
  /* Get bits per channel */
  bpc = png_get_bit_depth (pngdec->png, pngdec->info);
190

191 192 193 194
  /* We don't handle 16 bits per color, strip down to 8 */
  if (bpc == 16) {
    GST_LOG ("this is a 16 bits per channel PNG image, strip down to 8 bits");
    png_set_strip_16 (pngdec->png);
Wim Taymans's avatar
Wim Taymans committed
195 196
  }

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  /* Update the info structure */
  png_read_update_info (pngdec->png, pngdec->info);

  /* Get IHDR header again after transformation settings */

  png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
      &bpc, &pngdec->color_type, NULL, NULL, NULL);

  pngdec->width = width;
  pngdec->height = height;

  /* Generate the caps and configure */
  ret = gst_pngdec_caps_create_and_set (pngdec);
  if (ret != GST_FLOW_OK) {
    goto beach;
  }
Wim Taymans's avatar
Wim Taymans committed
213

214 215 216 217 218 219 220 221 222 223 224 225 226
  /* Allocate output buffer */
  pngdec->rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
  buffer_size = pngdec->height * GST_ROUND_UP_4 (pngdec->rowbytes);
  ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
      buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
  if (ret != GST_FLOW_OK) {
    goto beach;
  }

  pngdec->buffer_out = buffer;

beach:
  pngdec->ret = ret;
Wim Taymans's avatar
Wim Taymans committed
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
static void
user_endrow_callback (png_structp png_ptr, png_bytep new_row,
    png_uint_32 row_num, int pass)
{
  GstPngDec *pngdec = NULL;

  pngdec = GST_PNGDEC (png_ptr->io_ptr);

  /* FIXME: implement interlaced pictures */

  if (GST_IS_BUFFER (pngdec->buffer_out)) {
    size_t offset = row_num * GST_ROUND_UP_4 (pngdec->rowbytes);

    GST_LOG ("got row %d, copying in buffer %p at offset %d", row_num,
        pngdec->buffer_out, offset);
    memcpy (GST_BUFFER_DATA (pngdec->buffer_out) + offset, new_row,
        pngdec->rowbytes);
    pngdec->ret = GST_FLOW_OK;
  } else {
    GST_LOG ("we don't have any output buffer to write this row !");
    pngdec->ret = GST_FLOW_ERROR;
  }
}

static void
user_end_callback (png_structp png_ptr, png_infop info)
{
  GstPngDec *pngdec = NULL;

  pngdec = GST_PNGDEC (png_ptr->io_ptr);

  GST_LOG ("and we are done reading this image, pushing it and setting eos");

  /* Push our buffer and then EOS */
  pngdec->ret = gst_pad_push (pngdec->srcpad, pngdec->buffer_out);
  pngdec->ret = gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
}

static void
user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
{
  GstPngDec *pngdec;
  GstBuffer *buffer;
  GstFlowReturn ret = GST_FLOW_OK;

  pngdec = GST_PNGDEC (png_ptr->io_ptr);

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

  ret = gst_pad_pull_range (pngdec->sinkpad, pngdec->offset, length, &buffer);
  if ((ret != GST_FLOW_OK) || (GST_BUFFER_SIZE (buffer) != length))
    goto pause;

  memcpy (data, GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));

284 285
  gst_buffer_unref (buffer);

286 287 288 289 290 291 292 293 294
  pngdec->offset += length;

  return;

pause:
  GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
  gst_pad_pause_task (pngdec->sinkpad);
  if (GST_FLOW_IS_FATAL (ret)) {
    gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
295 296 297
    GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
        (_("Internal data stream error.")),
        ("stream stopped, reason %s", gst_flow_get_name (ret)));
298 299
  }
}
300 301

static GstFlowReturn
302
gst_pngdec_caps_create_and_set (GstPngDec * pngdec)
Wim Taymans's avatar
Wim Taymans committed
303
{
304
  GstFlowReturn ret = GST_FLOW_OK;
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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  GstCaps *caps = NULL, *res = NULL;
  GstPadTemplate *templ = NULL;

  g_return_val_if_fail (GST_IS_PNGDEC (pngdec), GST_FLOW_ERROR);

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

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

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

  templ = gst_static_pad_template_get (&gst_pngdec_src_pad_template);

  res = gst_caps_intersect (caps, gst_pad_template_get_caps (templ));

  gst_caps_unref (caps);

  if (!gst_pad_set_caps (pngdec->srcpad, res)) {
    ret = GST_FLOW_ERROR;
  }

  GST_LOG ("our caps %s", gst_caps_to_string (res));

  gst_caps_unref (res);

beach:
  return ret;
}

static void
gst_pngdec_task (GstPad * pad)
{
Wim Taymans's avatar
Wim Taymans committed
355
  GstPngDec *pngdec;
356 357 358
  GstBuffer *buffer = NULL;
  size_t buffer_size = 0;
  gint i = 0, bpc = 0;
Wim Taymans's avatar
Wim Taymans committed
359
  png_bytep *rows, inp;
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  png_uint_32 width, height, rowbytes;
  GstFlowReturn ret = GST_FLOW_OK;

  pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));

  /* Let libpng come back here on error */
  if (setjmp (png_jmpbuf (pngdec->png))) {
    ret = GST_FLOW_ERROR;
    goto pause;
  }

  /* Set reading callback */
  png_set_read_fn (pngdec->png, pngdec, user_read_data);

  /* Read info */
  png_read_info (pngdec->png, pngdec->info);

  /* Get bits per channel */
  bpc = png_get_bit_depth (pngdec->png, pngdec->info);

  /* We don't handle 16 bits per color, strip down to 8 */
  if (bpc == 16) {
    GST_LOG ("this is a 16 bits per channel PNG image, strip down to 8 bits");
    png_set_strip_16 (pngdec->png);
  }

  /* Update the info structure */
  png_read_update_info (pngdec->png, pngdec->info);

  /* Get IHDR header again after transformation settings */

  png_get_IHDR (pngdec->png, pngdec->info, &width, &height,
      &bpc, &pngdec->color_type, NULL, NULL, NULL);
Wim Taymans's avatar
Wim Taymans committed
393

394 395
  pngdec->width = width;
  pngdec->height = height;
396

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  /* Generate the caps and configure */
  ret = gst_pngdec_caps_create_and_set (pngdec);
  if (ret != GST_FLOW_OK) {
    goto pause;
  }

  /* Allocate output buffer */
  rowbytes = png_get_rowbytes (pngdec->png, pngdec->info);
  buffer_size = pngdec->height * GST_ROUND_UP_4 (rowbytes);
  ret = gst_pad_alloc_buffer (pngdec->srcpad, GST_BUFFER_OFFSET_NONE,
      buffer_size, GST_PAD_CAPS (pngdec->srcpad), &buffer);
  if (ret != GST_FLOW_OK) {
    goto pause;
  }

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

  inp = GST_BUFFER_DATA (buffer);

  for (i = 0; i < height; i++) {
    rows[i] = inp;
    inp += GST_ROUND_UP_4 (rowbytes);
  }

  /* Read the actual picture */
  png_read_image (pngdec->png, rows);
  free (rows);

  /* Push the raw RGB frame */
  ret = gst_pad_push (pngdec->srcpad, buffer);
  if (ret != GST_FLOW_OK) {
    goto pause;
  }

  /* And we are done */
  gst_pad_pause_task (pngdec->sinkpad);
  gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
  return;

pause:
  GST_LOG_OBJECT (pngdec, "pausing task, reason %s", gst_flow_get_name (ret));
  gst_pad_pause_task (pngdec->sinkpad);
  if (GST_FLOW_IS_FATAL (ret)) {
    gst_pad_push_event (pngdec->srcpad, gst_event_new_eos ());
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
441 442 443
    GST_ELEMENT_ERROR (pngdec, STREAM, FAILED,
        (_("Internal data stream error.")),
        ("stream stopped, reason %s", gst_flow_get_name (ret)));
444 445 446 447 448 449 450 451
  }
}

static GstFlowReturn
gst_pngdec_chain (GstPad * pad, GstBuffer * buffer)
{
  GstPngDec *pngdec;
  GstFlowReturn ret = GST_FLOW_OK;
Wim Taymans's avatar
Wim Taymans committed
452

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
  pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));

  if (!pngdec->setup) {
    GST_LOG ("we are not configured yet");
    ret = GST_FLOW_WRONG_STATE;
    goto beach;
  }

  /* Something is going wrong in our callbacks */
  if (pngdec->ret != GST_FLOW_OK) {
    ret = pngdec->ret;
    goto beach;
  }

  /* Let libpng come back here on error */
  if (setjmp (png_jmpbuf (pngdec->png))) {
    ret = GST_FLOW_ERROR;
470
    goto beach;
Wim Taymans's avatar
Wim Taymans committed
471 472
  }

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
  /* Progressive loading of the PNG image */
  png_process_data (pngdec->png, pngdec->info, GST_BUFFER_DATA (buffer),
      GST_BUFFER_SIZE (buffer));

  /* And release the buffer */
  gst_buffer_unref (buffer);

beach:
  return ret;
}

static gboolean
gst_pngdec_sink_event (GstPad * pad, GstEvent * event)
{
  GstPngDec *pngdec;

  pngdec = GST_PNGDEC (GST_OBJECT_PARENT (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_EOS:
      gst_pngdec_libpng_clear (pngdec);
    default:
      return gst_pad_event_default (pad, event);
  }
}


/* Clean up the libpng structures */
static gboolean
gst_pngdec_libpng_clear (GstPngDec * pngdec)
{
  png_infopp info = NULL, endinfo = NULL;

  g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);

  GST_LOG ("cleaning up libpng structures");

  if (pngdec->info) {
    info = &pngdec->info;
  }

  if (pngdec->endinfo) {
    endinfo = &pngdec->endinfo;
  }

  if (pngdec->png) {
    png_destroy_read_struct (&(pngdec->png), info, endinfo);
    pngdec->png = NULL;
    pngdec->info = NULL;
    pngdec->endinfo = NULL;
  }

  pngdec->bpp = pngdec->color_type = pngdec->height = pngdec->width = -1;
Wim Taymans's avatar
Wim Taymans committed
526
  pngdec->offset = 0;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
  pngdec->rowbytes = 0;
  pngdec->buffer_out = NULL;

  pngdec->setup = FALSE;

  return TRUE;
}

static gboolean
gst_pngdec_libpng_init (GstPngDec * pngdec)
{
  g_return_val_if_fail (GST_IS_PNGDEC (pngdec), FALSE);

  if (pngdec->setup) {
    goto beach;
  }
Wim Taymans's avatar
Wim Taymans committed
543 544 545 546 547 548

  /* 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) {
549 550
    GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
        ("Failed to initialize png structure"));
551
    goto beach;
Wim Taymans's avatar
Wim Taymans committed
552 553 554 555
  }

  pngdec->info = png_create_info_struct (pngdec->png);
  if (pngdec->info == NULL) {
556
    gst_pngdec_libpng_clear (pngdec);
557 558
    GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
        ("Failed to initialize info structure"));
559
    goto beach;
Wim Taymans's avatar
Wim Taymans committed
560 561 562 563
  }

  pngdec->endinfo = png_create_info_struct (pngdec->png);
  if (pngdec->endinfo == NULL) {
564
    gst_pngdec_libpng_clear (pngdec);
565 566
    GST_ELEMENT_ERROR (pngdec, LIBRARY, INIT, (NULL),
        ("Failed to initialize endinfo structure"));
567
    goto beach;
Wim Taymans's avatar
Wim Taymans committed
568 569
  }

570
  pngdec->setup = TRUE;
Wim Taymans's avatar
Wim Taymans committed
571

572 573 574
beach:
  return pngdec->setup;
}
Wim Taymans's avatar
Wim Taymans committed
575

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
static GstStateChangeReturn
gst_pngdec_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;
  GstPngDec *pngdec = NULL;

  pngdec = GST_PNGDEC (element);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      gst_pngdec_libpng_init (pngdec);
      break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_pngdec_libpng_clear (pngdec);
      break;
    default:
      break;
593 594
  }

595
  ret = parent_class->change_state (element, transition);
596

597 598
  return ret;
}
599

600 601 602 603 604
/* this function gets called when we activate ourselves in push mode. */
static gboolean
gst_pngdec_sink_activate_push (GstPad * sinkpad, gboolean active)
{
  GstPngDec *pngdec;
Wim Taymans's avatar
Wim Taymans committed
605

606
  pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
Wim Taymans's avatar
Wim Taymans committed
607

608
  pngdec->ret = GST_FLOW_OK;
609

610 611 612 613 614 615 616
  if (active) {
    /* Let libpng come back here on error */
    if (setjmp (png_jmpbuf (pngdec->png))) {
      GST_LOG ("failed setting up libpng jumb");
      gst_pngdec_libpng_clear (pngdec);
      return FALSE;
    }
Wim Taymans's avatar
Wim Taymans committed
617

618 619 620
    GST_LOG ("setting up progressive loading callbacks");
    png_set_progressive_read_fn (pngdec->png, pngdec,
        user_info_callback, user_endrow_callback, user_end_callback);
Wim Taymans's avatar
Wim Taymans committed
621 622
  }

623 624
  return TRUE;
}
Wim Taymans's avatar
Wim Taymans committed
625

626 627 628 629 630 631 632
/* this function gets called when we activate ourselves in pull mode.
 * We can perform  random access to the resource and we start a task
 * to start reading */
static gboolean
gst_pngdec_sink_activate_pull (GstPad * sinkpad, gboolean active)
{
  GstPngDec *pngdec;
Wim Taymans's avatar
Wim Taymans committed
633

634
  pngdec = GST_PNGDEC (GST_OBJECT_PARENT (sinkpad));
Wim Taymans's avatar
Wim Taymans committed
635

636 637 638 639 640 641 642
  if (active) {
    return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_pngdec_task,
        sinkpad);
  } else {
    return gst_pad_stop_task (sinkpad);
  }
}
643

644 645 646 647 648 649 650 651 652 653 654 655 656 657
/* this function is called when the pad is activated and should start
 * processing data.
 *
 * We check if we can do random access to decide if we work push or
 * pull based.
 */
static gboolean
gst_pngdec_sink_activate (GstPad * sinkpad)
{
  if (gst_pad_check_pull_range (sinkpad)) {
    return gst_pad_activate_pull (sinkpad, TRUE);
  } else {
    return gst_pad_activate_push (sinkpad, TRUE);
  }
Wim Taymans's avatar
Wim Taymans committed
658
}