pngenc: make setcaps more robust, use gstvideo functions

A setcaps function needs to actually verify the caps carefully. In this case,
it was possible to e.g. link a video decoder with YUV+RGB template caps to
pngenc.  That would cause a crash when the decoder pushes a YUV buffer. Same
thing when pushing a valid buffer that exceeds the resolution limits.

Also, missing framerate caps field would cause a glib critical warning due to
invalid GValue. This fails hard now.
This commit is contained in:
René Stadler 2011-10-16 19:41:28 +02:00 committed by René Stadler
parent 5baca05ec3
commit 9eb55c3d8f
3 changed files with 46 additions and 28 deletions

View file

@ -2,7 +2,8 @@ plugin_LTLIBRARIES = libgstpng.la
libgstpng_la_SOURCES = gstpng.c gstpngenc.c gstpngdec.c libgstpng_la_SOURCES = gstpng.c gstpngenc.c gstpngdec.c
libgstpng_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS) $(LIBPNG_CFLAGS) libgstpng_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS) $(LIBPNG_CFLAGS)
libgstpng_la_LIBADD = $(GST_LIBS) $(LIBPNG_LIBS) libgstpng_la_LIBADD = $(GST_PLUGINS_BASE_LIBS) -lgstvideo-@GST_MAJORMINOR@ \
$(GST_LIBS) $(LIBPNG_LIBS)
libgstpng_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) libgstpng_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstpng_la_LIBTOOLFLAGS = --tag=disable-static libgstpng_la_LIBTOOLFLAGS = --tag=disable-static

View file

@ -146,35 +146,60 @@ static gboolean
gst_pngenc_setcaps (GstPad * pad, GstCaps * caps) gst_pngenc_setcaps (GstPad * pad, GstCaps * caps)
{ {
GstPngEnc *pngenc; GstPngEnc *pngenc;
const GValue *fps; GstVideoFormat format;
GstStructure *structure; int fps_n, fps_d;
GstCaps *pcaps; GstCaps *pcaps;
gboolean ret = TRUE; gboolean ret;
pngenc = GST_PNGENC (gst_pad_get_parent (pad)); pngenc = GST_PNGENC (gst_pad_get_parent (pad));
structure = gst_caps_get_structure (caps, 0); ret = gst_video_format_parse_caps (caps, &format,
gst_structure_get_int (structure, "width", &pngenc->width); &pngenc->width, &pngenc->height);
gst_structure_get_int (structure, "height", &pngenc->height); if (G_LIKELY (ret))
fps = gst_structure_get_value (structure, "framerate"); ret = gst_video_parse_caps_framerate (caps, &fps_n, &fps_d);
gst_structure_get_int (structure, "bpp", &pngenc->bpp);
if (pngenc->bpp == 32) if (G_UNLIKELY (!ret))
pngenc->stride = pngenc->width * 4; goto done;
else if (pngenc->bpp == 8)
pngenc->stride = GST_ROUND_UP_4 (pngenc->width); switch (format) {
else case GST_VIDEO_FORMAT_RGBA:
pngenc->stride = GST_ROUND_UP_4 (pngenc->width * 3); 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 > 4096 ||
pngenc->height < 16 || pngenc->height > 4096)) {
ret = FALSE;
goto done;
}
pngenc->stride = gst_video_format_get_row_stride (format, 0, pngenc->width);
pcaps = gst_caps_new_simple ("image/png", pcaps = gst_caps_new_simple ("image/png",
"width", G_TYPE_INT, pngenc->width, "width", G_TYPE_INT, pngenc->width,
"height", G_TYPE_INT, pngenc->height, NULL); "height", G_TYPE_INT, pngenc->height,
structure = gst_caps_get_structure (pcaps, 0); "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
gst_structure_set_value (structure, "framerate", fps);
ret = gst_pad_set_caps (pngenc->srcpad, pcaps); ret = gst_pad_set_caps (pngenc->srcpad, pcaps);
gst_caps_unref (pcaps); gst_caps_unref (pcaps);
/* Fall-through. */
done:
if (G_UNLIKELY (!ret)) {
pngenc->width = 0;
pngenc->height = 0;
}
gst_object_unref (pngenc); gst_object_unref (pngenc);
return ret; return ret;
@ -238,7 +263,6 @@ gst_pngenc_chain (GstPad * pad, GstBuffer * buf)
{ {
GstPngEnc *pngenc; GstPngEnc *pngenc;
gint row_index; gint row_index;
gint color_type;
png_byte *row_pointers[MAX_HEIGHT]; png_byte *row_pointers[MAX_HEIGHT];
GstFlowReturn ret = GST_FLOW_OK; GstFlowReturn ret = GST_FLOW_OK;
GstBuffer *encoded_buf = NULL; GstBuffer *encoded_buf = NULL;
@ -282,19 +306,12 @@ gst_pngenc_chain (GstPad * pad, GstBuffer * buf)
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE); PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE);
png_set_compression_level (pngenc->png_struct_ptr, pngenc->compression_level); 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, png_set_IHDR (pngenc->png_struct_ptr,
pngenc->png_info_ptr, pngenc->png_info_ptr,
pngenc->width, pngenc->width,
pngenc->height, pngenc->height,
8, 8,
color_type, pngenc->png_color_type,
PNG_INTERLACE_NONE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

View file

@ -49,9 +49,9 @@ struct _GstPngEnc
png_structp png_struct_ptr; png_structp png_struct_ptr;
png_infop png_info_ptr; png_infop png_info_ptr;
gint png_color_type;
gint width; gint width;
gint height; gint height;
gint bpp;
gint stride; gint stride;
guint compression_level; guint compression_level;