/* this function handles the link with other elements */
static gboolean
gst_slvideo_set_caps (GstBaseSink * bsink, GstCaps * caps)
{
	GstSLVideo *filter;
	GstStructure *structure;
	GstCaps *intersection;
	
	GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);
	
	filter = GST_SLVIDEO(bsink);
	
	intersection = llgst_caps_intersect (filter->caps, caps);
	if (llgst_caps_is_empty (intersection))
	{
		// no overlap between our caps and requested caps
		return FALSE;
	}
	llgst_caps_unref(intersection);
	
	int width, height;
	gboolean ret;
	const GValue *fps;
	const GValue *par;
	structure = llgst_caps_get_structure (caps, 0);
	ret = llgst_structure_get_int (structure, "width", &width);
	ret = ret && llgst_structure_get_int (structure, "height", &height);
	fps = llgst_structure_get_value (structure, "framerate");
	ret = ret && (fps != NULL);
	par = llgst_structure_get_value (structure, "pixel-aspect-ratio");
	if (!ret)
		return FALSE;

	filter->width = width;
	filter->height = height;
	filter->fps_n = llgst_value_get_fraction_numerator(fps);
	filter->fps_d = llgst_value_get_fraction_denominator(fps);
	if (par)
	{
		filter->par_n = llgst_value_get_fraction_numerator(par);
		filter->par_d = llgst_value_get_fraction_denominator(par);
	}
	else
	{
		filter->par_n = 1;
		filter->par_d = 1;
	}
	GST_VIDEO_SINK_WIDTH(filter) = width;
	GST_VIDEO_SINK_HEIGHT(filter) = height;
	
	filter->format = SLV_PF_UNKNOWN;
	if (0 == strcmp(llgst_structure_get_name(structure),
			"video/x-raw-rgb"))
	{
		int red_mask;
		int green_mask;
		int blue_mask;
		llgst_structure_get_int(structure, "red_mask", &red_mask);
		llgst_structure_get_int(structure, "green_mask", &green_mask);
		llgst_structure_get_int(structure, "blue_mask", &blue_mask);
		if ((unsigned int)red_mask   == 0xFF000000 &&
		    (unsigned int)green_mask == 0x00FF0000 &&
		    (unsigned int)blue_mask  == 0x0000FF00)
		{
			filter->format = SLV_PF_RGBX;
			//fprintf(stderr, "\n\nPIXEL FORMAT RGB\n\n");
		} else if ((unsigned int)red_mask   == 0x0000FF00 &&
			   (unsigned int)green_mask == 0x00FF0000 &&
			   (unsigned int)blue_mask  == 0xFF000000)
		{
			filter->format = SLV_PF_BGRX;
			//fprintf(stderr, "\n\nPIXEL FORMAT BGR\n\n");
		}
	}
	
	return TRUE;
}
/* this function handles the link with other elements */
static gboolean
gst_slvideo_set_caps (GstBaseSink * bsink, GstCaps * caps)
{
    GstSLVideo *filter;
    GstStructure *structure;

    GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);

    filter = GST_SLVIDEO(bsink);

    int width, height;
    gboolean ret;
    const GValue *fps;
    const GValue *par;
    structure = llgst_caps_get_structure (caps, 0);
    ret = llgst_structure_get_int (structure, "width", &width);
    ret = ret && llgst_structure_get_int (structure, "height", &height);
    fps = llgst_structure_get_value (structure, "framerate");
    ret = ret && (fps != NULL);
    par = llgst_structure_get_value (structure, "pixel-aspect-ratio");
    if (!ret)
        return FALSE;

    INFOMSG("** filter caps set with width=%d, height=%d", width, height);

    GST_OBJECT_LOCK(filter);

    filter->width = width;
    filter->height = height;

    filter->fps_n = llgst_value_get_fraction_numerator(fps);
    filter->fps_d = llgst_value_get_fraction_denominator(fps);
    if (par)
    {
        filter->par_n = llgst_value_get_fraction_numerator(par);
        filter->par_d = llgst_value_get_fraction_denominator(par);
    }
    else
    {
        filter->par_n = 1;
        filter->par_d = 1;
    }
    GST_VIDEO_SINK_WIDTH(filter) = width;
    GST_VIDEO_SINK_HEIGHT(filter) = height;

    // crufty lump - we *always* accept *only* RGBX now.
    /*
    filter->format = SLV_PF_UNKNOWN;
    if (0 == strcmp(llgst_structure_get_name(structure),
    		"video/x-raw-rgb"))
    {
    	int red_mask;
    	int green_mask;
    	int blue_mask;
    	llgst_structure_get_int(structure, "red_mask", &red_mask);
    	llgst_structure_get_int(structure, "green_mask", &green_mask);
    	llgst_structure_get_int(structure, "blue_mask", &blue_mask);
    	if ((unsigned int)red_mask   == 0xFF000000 &&
    	    (unsigned int)green_mask == 0x00FF0000 &&
    	    (unsigned int)blue_mask  == 0x0000FF00)
    	{
    		filter->format = SLV_PF_RGBX;
    		//fprintf(stderr, "\n\nPIXEL FORMAT RGB\n\n");
    	} else if ((unsigned int)red_mask   == 0x0000FF00 &&
    		   (unsigned int)green_mask == 0x00FF0000 &&
    		   (unsigned int)blue_mask  == 0xFF000000)
    	{
    		filter->format = SLV_PF_BGRX;
    		//fprintf(stderr, "\n\nPIXEL FORMAT BGR\n\n");
    	}
    	}*/

    filter->format = SLV_PF_RGBX;

    GST_OBJECT_UNLOCK(filter);

    return TRUE;
}