- g_mutex_unlock (xvimagesink->x_lock);
- g_mutex_unlock (xvimagesink->flow_lock);
-}
-
-static void
-gst_lookup_xv_port_from_adaptor (GstXContext * xcontext,
- XvAdaptorInfo * adaptors, int adaptor_no)
-{
- gint j;
- gint res;
-
- /* Do we support XvImageMask ? */
- if (!(adaptors[adaptor_no].type & XvImageMask)) {
- GST_DEBUG ("XV Adaptor %s has no support for XvImageMask",
- adaptors[adaptor_no].name);
- return;
- }
-
- /* We found such an adaptor, looking for an available port */
- for (j = 0; j < adaptors[adaptor_no].num_ports && !xcontext->xv_port_id; j++) {
- /* We try to grab the port */
- res = XvGrabPort (xcontext->disp, adaptors[adaptor_no].base_id + j, 0);
- if (Success == res) {
- xcontext->xv_port_id = adaptors[adaptor_no].base_id + j;
- GST_DEBUG ("XV Adaptor %s with %ld ports", adaptors[adaptor_no].name,
- adaptors[adaptor_no].num_ports);
- } else {
- GST_DEBUG ("GrabPort %d for XV Adaptor %s failed: %d", j,
- adaptors[adaptor_no].name, res);
- }
- }
-}
-
-/* This function generates a caps with all supported format by the first
- Xv grabable port we find. We store each one of the supported formats in a
- format list and append the format to a newly created caps that we return
- If this function does not return NULL because of an error, it also grabs
- the port via XvGrabPort */
-static GstCaps *
-gst_xvimagesink_get_xv_support (GstXvImageSink * xvimagesink,
- GstXContext * xcontext)
-{
- gint i;
- XvAdaptorInfo *adaptors;
- gint nb_formats;
- XvImageFormatValues *formats = NULL;
- guint nb_encodings;
- XvEncodingInfo *encodings = NULL;
- gulong max_w = G_MAXINT, max_h = G_MAXINT;
- GstCaps *caps = NULL;
- GstCaps *rgb_caps = NULL;
-
- g_return_val_if_fail (xcontext != NULL, NULL);
-
- /* First let's check that XVideo extension is available */
- if (!XQueryExtension (xcontext->disp, "XVideo", &i, &i, &i)) {
- GST_ELEMENT_ERROR (xvimagesink, RESOURCE, SETTINGS,
- ("Could not initialise Xv output"),
- ("XVideo extension is not available"));
- return NULL;
- }
-
- /* Then we get adaptors list */
- if (Success != XvQueryAdaptors (xcontext->disp, xcontext->root,
- &xcontext->nb_adaptors, &adaptors)) {
- GST_ELEMENT_ERROR (xvimagesink, RESOURCE, SETTINGS,
- ("Could not initialise Xv output"),
- ("Failed getting XV adaptors list"));
- return NULL;
- }
-
- xcontext->xv_port_id = 0;
-
- GST_DEBUG ("Found %u XV adaptor(s)", xcontext->nb_adaptors);
-
- xcontext->adaptors =
- (gchar **) g_malloc0 (xcontext->nb_adaptors * sizeof (gchar *));
-
- /* Now fill up our adaptor name array */
- for (i = 0; i < xcontext->nb_adaptors; i++) {
- xcontext->adaptors[i] = g_strdup (adaptors[i].name);
- }
-
- if (xvimagesink->adaptor_no >= 0 &&
- xvimagesink->adaptor_no < xcontext->nb_adaptors) {
- /* Find xv port from user defined adaptor */
- gst_lookup_xv_port_from_adaptor (xcontext, adaptors,
- xvimagesink->adaptor_no);
- }
-
- if (!xcontext->xv_port_id) {
- /* Now search for an adaptor that supports XvImageMask */
- for (i = 0; i < xcontext->nb_adaptors && !xcontext->xv_port_id; i++) {
- gst_lookup_xv_port_from_adaptor (xcontext, adaptors, i);
- xvimagesink->adaptor_no = i;
- }
- }
-
- XvFreeAdaptorInfo (adaptors);
-
- if (!xcontext->xv_port_id) {
- xvimagesink->adaptor_no = -1;
- GST_ELEMENT_ERROR (xvimagesink, RESOURCE, BUSY,
- ("Could not initialise Xv output"), ("No port available"));
- return NULL;
- }
-
- /* Set XV_AUTOPAINT_COLORKEY and XV_DOUBLE_BUFFER and XV_COLORKEY */
- {
- int count, todo = 3;
- XvAttribute *const attr = XvQueryPortAttributes (xcontext->disp,
- xcontext->xv_port_id, &count);
- static const char autopaint[] = "XV_AUTOPAINT_COLORKEY";
- static const char dbl_buffer[] = "XV_DOUBLE_BUFFER";
- static const char colorkey[] = "XV_COLORKEY";
-
- GST_DEBUG_OBJECT (xvimagesink, "Checking %d Xv port attributes", count);
-
- xvimagesink->have_autopaint_colorkey = FALSE;
- xvimagesink->have_double_buffer = FALSE;
- xvimagesink->have_colorkey = FALSE;
-
- for (i = 0; ((i < count) && todo); i++)
- if (!strcmp (attr[i].name, autopaint)) {
- const Atom atom = XInternAtom (xcontext->disp, autopaint, False);
-
- /* turn on autopaint colorkey */
- XvSetPortAttribute (xcontext->disp, xcontext->xv_port_id, atom,
- (xvimagesink->autopaint_colorkey ? 1 : 0));
- todo--;
- xvimagesink->have_autopaint_colorkey = TRUE;
- } else if (!strcmp (attr[i].name, dbl_buffer)) {
- const Atom atom = XInternAtom (xcontext->disp, dbl_buffer, False);
-
- XvSetPortAttribute (xcontext->disp, xcontext->xv_port_id, atom,
- (xvimagesink->double_buffer ? 1 : 0));
- todo--;
- xvimagesink->have_double_buffer = TRUE;
- } else if (!strcmp (attr[i].name, colorkey)) {
- /* Set the colorkey, default is something that is dark but hopefully
- * won't randomly appear on the screen elsewhere (ie not black or greys)
- * can be overridden by setting "colorkey" property
- */
- const Atom atom = XInternAtom (xcontext->disp, colorkey, False);
- guint32 ckey = 0;
- gboolean set_attr = TRUE;
- guint cr, cg, cb;
-
- /* set a colorkey in the right format RGB565/RGB888
- * We only handle these 2 cases, because they're the only types of
- * devices we've encountered. If we don't recognise it, leave it alone
- */
- cr = (xvimagesink->colorkey >> 16);
- cg = (xvimagesink->colorkey >> 8) & 0xFF;
- cb = (xvimagesink->colorkey) & 0xFF;
- switch (xcontext->depth) {
- case 16: /* RGB 565 */
- cr >>= 3;
- cg >>= 2;
- cb >>= 3;
- ckey = (cr << 11) | (cg << 5) | cb;
- break;
- case 24:
- case 32: /* RGB 888 / ARGB 8888 */
- ckey = (cr << 16) | (cg << 8) | cb;
- break;
- default:
- GST_DEBUG_OBJECT (xvimagesink,
- "Unknown bit depth %d for Xv Colorkey - not adjusting",
- xcontext->depth);
- set_attr = FALSE;
- break;
- }
-
- if (set_attr) {
- ckey = CLAMP (ckey, (guint32) attr[i].min_value,
- (guint32) attr[i].max_value);
- GST_LOG_OBJECT (xvimagesink,
- "Setting color key for display depth %d to 0x%x",
- xcontext->depth, ckey);
-
- XvSetPortAttribute (xcontext->disp, xcontext->xv_port_id, atom,
- (gint) ckey);
- }
- todo--;
- xvimagesink->have_colorkey = TRUE;
- }
-
- XFree (attr);
- }
-
- /* Get the list of encodings supported by the adapter and look for the
- * XV_IMAGE encoding so we can determine the maximum width and height
- * supported */
- XvQueryEncodings (xcontext->disp, xcontext->xv_port_id, &nb_encodings,
- &encodings);
-
- for (i = 0; i < nb_encodings; i++) {
- GST_LOG_OBJECT (xvimagesink,
- "Encoding %d, name %s, max wxh %lux%lu rate %d/%d",
- i, encodings[i].name, encodings[i].width, encodings[i].height,
- encodings[i].rate.numerator, encodings[i].rate.denominator);
- if (strcmp (encodings[i].name, "XV_IMAGE") == 0) {
- max_w = encodings[i].width;
- max_h = encodings[i].height;
- }
- }
-
- XvFreeEncodingInfo (encodings);
-
- /* We get all image formats supported by our port */
- formats = XvListImageFormats (xcontext->disp,
- xcontext->xv_port_id, &nb_formats);
- caps = gst_caps_new_empty ();
- for (i = 0; i < nb_formats; i++) {
- GstCaps *format_caps = NULL;
- gboolean is_rgb_format = FALSE;
- GstVideoFormat vformat;
-
- /* We set the image format of the xcontext to an existing one. This
- is just some valid image format for making our xshm calls check before
- caps negotiation really happens. */
- xcontext->im_format = formats[i].id;
-
- switch (formats[i].type) {
- case XvRGB:
- {
- XvImageFormatValues *fmt = &(formats[i]);
- gint endianness;
-
- endianness =
- (fmt->byte_order == LSBFirst ? G_LITTLE_ENDIAN : G_BIG_ENDIAN);
-
- vformat = gst_video_format_from_masks (fmt->depth, fmt->bits_per_pixel,
- endianness, fmt->red_mask, fmt->green_mask, fmt->blue_mask, 0);
- if (vformat == GST_VIDEO_FORMAT_UNKNOWN)
- break;
-
- format_caps = gst_caps_new_simple ("video/x-raw",
- "format", G_TYPE_STRING, gst_video_format_to_string (vformat),
- "width", GST_TYPE_INT_RANGE, 1, max_w,
- "height", GST_TYPE_INT_RANGE, 1, max_h,
- "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
-
- is_rgb_format = TRUE;
- break;
- }
- case XvYUV:
- {
- vformat = gst_video_format_from_fourcc (formats[i].id);
- if (vformat == GST_VIDEO_FORMAT_UNKNOWN)
- break;
-
- format_caps = gst_caps_new_simple ("video/x-raw",
- "format", G_TYPE_STRING, gst_video_format_to_string (vformat),
- "width", GST_TYPE_INT_RANGE, 1, max_w,
- "height", GST_TYPE_INT_RANGE, 1, max_h,
- "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
- break;
- }
- default:
- vformat = GST_VIDEO_FORMAT_UNKNOWN;
- g_assert_not_reached ();
- break;
- }
-
- if (format_caps) {
- GstXvImageFormat *format = NULL;
-
- format = g_new0 (GstXvImageFormat, 1);
- if (format) {
- format->format = formats[i].id;
- format->vformat = vformat;
- format->caps = gst_caps_copy (format_caps);
- xcontext->formats_list = g_list_append (xcontext->formats_list, format);
- }
-
- if (is_rgb_format) {
- if (rgb_caps == NULL)
- rgb_caps = format_caps;
- else
- gst_caps_append (rgb_caps, format_caps);
- } else
- gst_caps_append (caps, format_caps);
- }
- }
-
- /* Collected all caps into either the caps or rgb_caps structures.
- * Append rgb_caps on the end of YUV, so that YUV is always preferred */
- if (rgb_caps)
- gst_caps_append (caps, rgb_caps);
-
- if (formats)
- XFree (formats);
-
- GST_DEBUG ("Generated the following caps: %" GST_PTR_FORMAT, caps);
-
- if (gst_caps_is_empty (caps)) {
- gst_caps_unref (caps);
- XvUngrabPort (xcontext->disp, xcontext->xv_port_id, 0);
- GST_ELEMENT_ERROR (xvimagesink, STREAM, WRONG_TYPE, (NULL),
- ("No supported format found"));
- return NULL;
- }
-
- return caps;