Beispiel #1
0
static void
gst_pngenc_finalize (GObject * object)
{
  GstPngEnc *pngenc = GST_PNGENC (object);

  if (pngenc->input_state)
    gst_video_codec_state_unref (pngenc->input_state);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}
Beispiel #2
0
static gboolean
gst_pngenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state)
{
  GstPngEnc *pngenc;
  gboolean ret = TRUE;
  GstVideoInfo *info;
  GstVideoCodecState *output_state;

  pngenc = GST_PNGENC (encoder);
  info = &state->info;

  switch (GST_VIDEO_INFO_FORMAT (info)) {
    case GST_VIDEO_FORMAT_RGBA:
      pngenc->png_color_type = PNG_COLOR_TYPE_RGBA;
      break;
    case GST_VIDEO_FORMAT_RGB:
      pngenc->png_color_type = PNG_COLOR_TYPE_RGB;
      break;
    case GST_VIDEO_FORMAT_GRAY8:
    case GST_VIDEO_FORMAT_GRAY16_BE:
      pngenc->png_color_type = PNG_COLOR_TYPE_GRAY;
      break;
    default:
      ret = FALSE;
      goto done;
  }

  switch (GST_VIDEO_INFO_FORMAT (info)) {
    case GST_VIDEO_FORMAT_GRAY16_BE:
      pngenc->depth = 16;
      break;
    default:                   /* GST_VIDEO_FORMAT_RGB and GST_VIDEO_FORMAT_GRAY8 */
      pngenc->depth = 8;
      break;
  }

  if (pngenc->input_state)
    gst_video_codec_state_unref (pngenc->input_state);
  pngenc->input_state = gst_video_codec_state_ref (state);

  output_state =
      gst_video_encoder_set_output_state (encoder,
      gst_caps_new_empty_simple ("image/png"), state);
  gst_video_codec_state_unref (output_state);

done:

  return ret;
}
Beispiel #3
0
static void
gst_pngenc_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  GstPngEnc *pngenc;

  pngenc = GST_PNGENC (object);

  switch (prop_id) {
    case ARG_SNAPSHOT:
      pngenc->snapshot = g_value_get_boolean (value);
      break;
    case ARG_COMPRESSION_LEVEL:
      pngenc->compression_level = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}
Beispiel #4
0
static void
gst_pngenc_get_property (GObject * object,
                         guint prop_id, GValue * value, GParamSpec * pspec)
{
    GstPngEnc *pngenc;

    pngenc = GST_PNGENC (object);

    switch (prop_id) {
    case ARG_SNAPSHOT:
        g_value_set_boolean (value, pngenc->snapshot);
        break;
    /*     case ARG_NEWMEDIA: */
    /*       g_value_set_boolean (value, pngenc->newmedia); */
    /*       break; */
    case ARG_COMPRESSION_LEVEL:
        g_value_set_uint (value, pngenc->compression_level);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}
Beispiel #5
0
static gboolean
gst_pngenc_setcaps (GstPad * pad, GstCaps * caps)
{
  GstPngEnc *pngenc;
  const GValue *fps;
  GstStructure *structure;
  GstCaps *pcaps;
  gboolean ret = TRUE;

  pngenc = GST_PNGENC (gst_pad_get_parent (pad));

  structure = gst_caps_get_structure (caps, 0);
  gst_structure_get_int (structure, "width", &pngenc->width);
  gst_structure_get_int (structure, "height", &pngenc->height);
  fps = gst_structure_get_value (structure, "framerate");
  gst_structure_get_int (structure, "bpp", &pngenc->bpp);

  if (pngenc->bpp == 32)
    pngenc->stride = pngenc->width * 4;
  else if (pngenc->bpp == 8)
    pngenc->stride = GST_ROUND_UP_4 (pngenc->width);
  else
    pngenc->stride = GST_ROUND_UP_4 (pngenc->width * 3);

  pcaps = gst_caps_new_simple ("image/png",
      "width", G_TYPE_INT, pngenc->width,
      "height", G_TYPE_INT, pngenc->height, NULL);
  structure = gst_caps_get_structure (pcaps, 0);
  gst_structure_set_value (structure, "framerate", fps);

  ret = gst_pad_set_caps (pngenc->srcpad, pcaps);

  gst_caps_unref (pcaps);
  gst_object_unref (pngenc);

  return ret;
}
Beispiel #6
0
static gboolean
gst_pngenc_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
    GstPngEnc *enc;
    gboolean res;

    enc = GST_PNGENC (parent);

    switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_CAPS:
    {
        GstCaps *caps;

        gst_event_parse_caps (event, &caps);
        res = gst_pngenc_setcaps (enc, caps);
        gst_event_unref (event);
        break;
    }
    default:
        res = gst_pad_push_event (enc->srcpad, event);
        break;
    }
    return res;
}
Beispiel #7
0
static GstFlowReturn
gst_pngenc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
{
    GstPngEnc *pngenc;
    gint row_index;
    png_byte **row_pointers;
    GstFlowReturn ret = GST_FLOW_OK;
    GstBuffer *encoded_buf = NULL;
    GstVideoFrame frame;

    pngenc = GST_PNGENC (parent);

    GST_DEBUG_OBJECT (pngenc, "BEGINNING");

    if (G_UNLIKELY (pngenc->width <= 0 || pngenc->height <= 0)) {
        ret = GST_FLOW_NOT_NEGOTIATED;
        goto exit;
    }

    if (!gst_video_frame_map (&frame, &pngenc->info, buf, GST_MAP_READ)) {
        GST_ELEMENT_ERROR (pngenc, STREAM, FORMAT, (NULL),
                           ("Failed to map video frame, caps problem?"));
        ret = GST_FLOW_ERROR;
        goto exit;
    }

    /* initialize png struct stuff */
    pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
                             (png_voidp) NULL, user_error_fn, user_warning_fn);
    if (pngenc->png_struct_ptr == NULL) {
        GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
                           ("Failed to initialize png structure"));
        ret = GST_FLOW_ERROR;
        goto done;
    }

    pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
    if (!pngenc->png_info_ptr) {
        png_destroy_write_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL);
        GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
                           ("Failed to initialize the png info structure"));
        ret = GST_FLOW_ERROR;
        goto done;
    }

    /* non-0 return is from a longjmp inside of libpng */
    if (setjmp (png_jmpbuf (pngenc->png_struct_ptr)) != 0) {
        png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
        GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
                           ("returning from longjmp"));
        ret = GST_FLOW_ERROR;
        goto done;
    }

    png_set_filter (pngenc->png_struct_ptr, 0,
                    PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
    png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level);

    png_set_IHDR (pngenc->png_struct_ptr,
                  pngenc->png_info_ptr,
                  pngenc->width,
                  pngenc->height,
                  pngenc->depth,
                  pngenc->png_color_type,
                  PNG_INTERLACE_NONE,
                  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

    png_set_write_fn (pngenc->png_struct_ptr, pngenc,
                      (png_rw_ptr) user_write_data, user_flush_data);

    row_pointers = g_new (png_byte *, pngenc->height);

    for (row_index = 0; row_index < pngenc->height; row_index++) {
        row_pointers[row_index] = GST_VIDEO_FRAME_COMP_DATA (&frame, 0) +
                                  (row_index * GST_VIDEO_FRAME_COMP_STRIDE (&frame, 0));
    }

    /* allocate the output buffer */
    pngenc->buffer_out =
        gst_buffer_new_and_alloc (pngenc->height * pngenc->width);
    pngenc->written = 0;

    png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
    png_write_image (pngenc->png_struct_ptr, row_pointers);
    png_write_end (pngenc->png_struct_ptr, NULL);

    g_free (row_pointers);

    GST_DEBUG_OBJECT (pngenc, "written %d", pngenc->written);

    encoded_buf =
        gst_buffer_copy_region (pngenc->buffer_out, GST_BUFFER_COPY_MEMORY,
                                0, pngenc->written);

    png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
    png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);

    GST_BUFFER_TIMESTAMP (encoded_buf) = GST_BUFFER_TIMESTAMP (buf);
    GST_BUFFER_DURATION (encoded_buf) = GST_BUFFER_DURATION (buf);

    if ((ret = gst_pad_push (pngenc->srcpad, encoded_buf)) != GST_FLOW_OK)
        goto done;

    if (pngenc->snapshot) {
        GstEvent *event;

        GST_DEBUG_OBJECT (pngenc, "snapshot mode, sending EOS");
        /* send EOS event, since a frame has been pushed out */
        event = gst_event_new_eos ();

        gst_pad_push_event (pngenc->srcpad, event);
        ret = GST_FLOW_EOS;
    }

done:
    gst_video_frame_unmap (&frame);
exit:
    gst_buffer_unref (buf);
    GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);

    if (pngenc->buffer_out != NULL) {
        gst_buffer_unref (pngenc->buffer_out);
        pngenc->buffer_out = NULL;
    }

    return ret;
}
Beispiel #8
0
static GstFlowReturn
gst_pngenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame)
{
  GstPngEnc *pngenc;
  gint row_index;
  png_byte **row_pointers;
  GstFlowReturn ret = GST_FLOW_OK;
  GstVideoInfo *info;
  GstVideoFrame vframe;

  pngenc = GST_PNGENC (encoder);
  info = &pngenc->input_state->info;

  GST_DEBUG_OBJECT (pngenc, "BEGINNING");

  /* initialize png struct stuff */
  pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
      (png_voidp) NULL, user_error_fn, user_warning_fn);
  if (pngenc->png_struct_ptr == NULL)
    goto struct_init_fail;

  pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
  if (!pngenc->png_info_ptr)
    goto png_info_fail;

  /* non-0 return is from a longjmp inside of libpng */
  if (setjmp (png_jmpbuf (pngenc->png_struct_ptr)) != 0)
    goto longjmp_fail;

  png_set_filter (pngenc->png_struct_ptr, 0,
      PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
  png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level);

  png_set_IHDR (pngenc->png_struct_ptr,
      pngenc->png_info_ptr,
      GST_VIDEO_INFO_WIDTH (info),
      GST_VIDEO_INFO_HEIGHT (info),
      pngenc->depth,
      pngenc->png_color_type,
      PNG_INTERLACE_NONE,
      PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

  png_set_write_fn (pngenc->png_struct_ptr, pngenc,
      (png_rw_ptr) user_write_data, user_flush_data);

  row_pointers = g_new (png_byte *, GST_VIDEO_INFO_HEIGHT (info));
  if (!gst_video_frame_map (&vframe, &pngenc->input_state->info,
          frame->input_buffer, GST_MAP_READ)) {
    GST_ELEMENT_ERROR (pngenc, STREAM, FORMAT, (NULL),
        ("Failed to map video frame, caps problem?"));
    ret = GST_FLOW_ERROR;
    goto done;
  }

  for (row_index = 0; row_index < GST_VIDEO_INFO_HEIGHT (info); row_index++) {
    row_pointers[row_index] = GST_VIDEO_FRAME_COMP_DATA (&vframe, 0) +
        (row_index * GST_VIDEO_FRAME_COMP_STRIDE (&vframe, 0));
  }

  /* allocate the output buffer */
  pngenc->buffer_out = gst_buffer_new ();

  png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
  png_write_image (pngenc->png_struct_ptr, row_pointers);
  png_write_end (pngenc->png_struct_ptr, NULL);

  g_free (row_pointers);
  gst_video_frame_unmap (&vframe);

  png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
  png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);

  /* Set final size and store */
  frame->output_buffer = pngenc->buffer_out;

  pngenc->buffer_out = NULL;

  if ((ret = gst_video_encoder_finish_frame (encoder, frame)) != GST_FLOW_OK)
    goto done;

  if (pngenc->snapshot)
    ret = GST_FLOW_EOS;

done:
  GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);

  return ret;

  /* ERRORS */
struct_init_fail:
  {
    GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
        ("Failed to initialize png structure"));
    return GST_FLOW_ERROR;
  }

png_info_fail:
  {
    png_destroy_write_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL);
    GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
        ("Failed to initialize the png info structure"));
    return GST_FLOW_ERROR;
  }

longjmp_fail:
  {
    png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
    GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
        ("returning from longjmp"));
    return GST_FLOW_ERROR;
  }
}
Beispiel #9
0
static GstFlowReturn
gst_pngenc_chain (GstPad * pad, GstBuffer * buf)
{
  GstPngEnc *pngenc;
  gint row_index;
  gint color_type;
  png_byte *row_pointers[MAX_HEIGHT];
  GstFlowReturn ret = GST_FLOW_OK;
  GstBuffer *encoded_buf = NULL;

  pngenc = GST_PNGENC (gst_pad_get_parent (pad));

  GST_DEBUG_OBJECT (pngenc, "BEGINNING");

  /* initialize png struct stuff */
  pngenc->png_struct_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
      (png_voidp) NULL, user_error_fn, user_warning_fn);
  if (pngenc->png_struct_ptr == NULL) {
    gst_buffer_unref (buf);
    GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
        ("Failed to initialize png structure"));
    ret = GST_FLOW_ERROR;
    goto done;
  }

  pngenc->png_info_ptr = png_create_info_struct (pngenc->png_struct_ptr);
  if (!pngenc->png_info_ptr) {
    gst_buffer_unref (buf);
    png_destroy_write_struct (&(pngenc->png_struct_ptr), (png_infopp) NULL);
    GST_ELEMENT_ERROR (pngenc, LIBRARY, INIT, (NULL),
        ("Failed to initialize the png info structure"));
    ret = GST_FLOW_ERROR;
    goto done;
  }

  /* non-0 return is from a longjmp inside of libpng */
  if (setjmp (png_jmpbuf (pngenc->png_struct_ptr)) != 0) {
    gst_buffer_unref (buf);
    png_destroy_write_struct (&pngenc->png_struct_ptr, &pngenc->png_info_ptr);
    GST_ELEMENT_ERROR (pngenc, LIBRARY, FAILED, (NULL),
        ("returning from longjmp"));
    ret = GST_FLOW_ERROR;
    goto done;
  }

  png_set_filter (pngenc->png_struct_ptr, 0,
      PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
  png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level);

  if (pngenc->bpp == 32)
    color_type = PNG_COLOR_TYPE_RGBA;
  else if (pngenc->bpp == 8)
    color_type = PNG_COLOR_TYPE_GRAY;
  else
    color_type = PNG_COLOR_TYPE_RGB;

  png_set_IHDR (pngenc->png_struct_ptr,
      pngenc->png_info_ptr,
      pngenc->width,
      pngenc->height,
      8,
      color_type,
      PNG_INTERLACE_NONE,
      PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

  png_set_write_fn (pngenc->png_struct_ptr, pngenc,
      (png_rw_ptr) user_write_data, user_flush_data);

  for (row_index = 0; row_index < pngenc->height; row_index++) {
    row_pointers[row_index] = GST_BUFFER_DATA (buf) +
        (row_index * pngenc->stride);
  }

  /* allocate the output buffer */
  pngenc->buffer_out =
      gst_buffer_new_and_alloc (pngenc->height * pngenc->stride);
  pngenc->written = 0;

  png_write_info (pngenc->png_struct_ptr, pngenc->png_info_ptr);
  png_write_image (pngenc->png_struct_ptr, row_pointers);
  png_write_end (pngenc->png_struct_ptr, NULL);

  encoded_buf = gst_buffer_create_sub (pngenc->buffer_out, 0, pngenc->written);

  png_destroy_info_struct (pngenc->png_struct_ptr, &pngenc->png_info_ptr);
  png_destroy_write_struct (&pngenc->png_struct_ptr, (png_infopp) NULL);
  gst_buffer_copy_metadata (encoded_buf, buf, GST_BUFFER_COPY_TIMESTAMPS);
  gst_buffer_unref (buf);
  gst_buffer_set_caps (encoded_buf, GST_PAD_CAPS (pngenc->srcpad));

  if ((ret = gst_pad_push (pngenc->srcpad, encoded_buf)) != GST_FLOW_OK)
    goto done;

  if (pngenc->snapshot) {
    GstEvent *event;

    GST_DEBUG_OBJECT (pngenc, "snapshot mode, sending EOS");
    /* send EOS event, since a frame has been pushed out */
    event = gst_event_new_eos ();

    gst_pad_push_event (pngenc->srcpad, event);
    ret = GST_FLOW_UNEXPECTED;
  }

done:
  GST_DEBUG_OBJECT (pngenc, "END, ret:%d", ret);

  if (pngenc->buffer_out != NULL) {
    gst_buffer_unref (pngenc->buffer_out);
    pngenc->buffer_out = NULL;
  }

  gst_object_unref (pngenc);
  return ret;
}
Beispiel #10
0
static gboolean
gst_pngenc_setcaps (GstPad * pad, GstCaps * caps)
{
  GstPngEnc *pngenc;
  GstVideoFormat format;
  int fps_n, fps_d;
  GstCaps *pcaps;
  gboolean ret;

  pngenc = GST_PNGENC (gst_pad_get_parent (pad));

  ret = gst_video_format_parse_caps (caps, &format,
      &pngenc->width, &pngenc->height);
  if (G_LIKELY (ret))
    ret = gst_video_parse_caps_framerate (caps, &fps_n, &fps_d);

  if (G_UNLIKELY (!ret))
    goto done;

  switch (format) {
    case GST_VIDEO_FORMAT_RGBA:
      pngenc->png_color_type = PNG_COLOR_TYPE_RGBA;
      break;
    case GST_VIDEO_FORMAT_RGB:
      pngenc->png_color_type = PNG_COLOR_TYPE_RGB;
      break;
    case GST_VIDEO_FORMAT_GRAY8:
      pngenc->png_color_type = PNG_COLOR_TYPE_GRAY;
      break;
    default:
      ret = FALSE;
      goto done;
  }

  if (G_UNLIKELY (pngenc->width < 16 || pngenc->width > 1000000 ||
          pngenc->height < 16 || pngenc->height > 1000000)) {
    ret = FALSE;
    goto done;
  }

  pngenc->stride = gst_video_format_get_row_stride (format, 0, pngenc->width);

  pcaps = gst_caps_new_simple ("image/png",
      "width", G_TYPE_INT, pngenc->width,
      "height", G_TYPE_INT, pngenc->height,
      "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);

  ret = gst_pad_set_caps (pngenc->srcpad, pcaps);

  gst_caps_unref (pcaps);

  /* Fall-through. */
done:
  if (G_UNLIKELY (!ret)) {
    pngenc->width = 0;
    pngenc->height = 0;
  }

  gst_object_unref (pngenc);

  return ret;
}