-
-/* This function calculates the pixel aspect ratio based on the properties
- * in the xcontext structure and stores it there. */
-static void
-gst_xvimagesink_calculate_pixel_aspect_ratio (GstXContext * xcontext)
-{
- static const gint par[][2] = {
- {1, 1}, /* regular screen */
- {16, 15}, /* PAL TV */
- {11, 10}, /* 525 line Rec.601 video */
- {54, 59}, /* 625 line Rec.601 video */
- {64, 45}, /* 1280x1024 on 16:9 display */
- {5, 3}, /* 1280x1024 on 4:3 display */
- {4, 3} /* 800x600 on 16:9 display */
- };
- gint i;
- gint index;
- gdouble ratio;
- gdouble delta;
-
-#define DELTA(idx) (ABS (ratio - ((gdouble) par[idx][0] / par[idx][1])))
-
- /* first calculate the "real" ratio based on the X values;
- * which is the "physical" w/h divided by the w/h in pixels of the display */
- ratio = (gdouble) (xcontext->widthmm * xcontext->height)
- / (xcontext->heightmm * xcontext->width);
-
- /* DirectFB's X in 720x576 reports the physical dimensions wrong, so
- * override here */
- if (xcontext->width == 720 && xcontext->height == 576) {
- ratio = 4.0 * 576 / (3.0 * 720);
- }
- GST_DEBUG ("calculated pixel aspect ratio: %f", ratio);
-
- /* now find the one from par[][2] with the lowest delta to the real one */
- delta = DELTA (0);
- index = 0;
-
- for (i = 1; i < sizeof (par) / (sizeof (gint) * 2); ++i) {
- gdouble this_delta = DELTA (i);
-
- if (this_delta < delta) {
- index = i;
- delta = this_delta;
- }
- }
-
- GST_DEBUG ("Decided on index %d (%d/%d)", index,
- par[index][0], par[index][1]);
-
- g_free (xcontext->par);
- xcontext->par = g_new0 (GValue, 1);
- g_value_init (xcontext->par, GST_TYPE_FRACTION);
- gst_value_set_fraction (xcontext->par, par[index][0], par[index][1]);
- GST_DEBUG ("set xcontext PAR to %d/%d",
- gst_value_get_fraction_numerator (xcontext->par),
- gst_value_get_fraction_denominator (xcontext->par));
-}
-
-/* This function gets the X Display and global info about it. Everything is
- stored in our object and will be cleaned when the object is disposed. Note
- here that caps for supported format are generated without any window or
- image creation */
-static GstXContext *
-gst_xvimagesink_xcontext_get (GstXvImageSink * xvimagesink)
-{
- GstXContext *xcontext = NULL;
- XPixmapFormatValues *px_formats = NULL;
- gint nb_formats = 0, i, j, N_attr;
- XvAttribute *xv_attr;
- Atom prop_atom;
- const char *channels[4] = { "XV_HUE", "XV_SATURATION",
- "XV_BRIGHTNESS", "XV_CONTRAST"
- };
-
- g_return_val_if_fail (GST_IS_XVIMAGESINK (xvimagesink), NULL);
-
- xcontext = g_new0 (GstXContext, 1);
- xcontext->im_format = 0;
-
- g_mutex_lock (xvimagesink->x_lock);
-
- xcontext->disp = XOpenDisplay (xvimagesink->display_name);
-
- if (!xcontext->disp) {
- g_mutex_unlock (xvimagesink->x_lock);
- g_free (xcontext);
- GST_ELEMENT_ERROR (xvimagesink, RESOURCE, WRITE,
- ("Could not initialise Xv output"), ("Could not open display"));
- return NULL;
- }
-
- xcontext->screen = DefaultScreenOfDisplay (xcontext->disp);
- xcontext->screen_num = DefaultScreen (xcontext->disp);
- xcontext->visual = DefaultVisual (xcontext->disp, xcontext->screen_num);
- xcontext->root = DefaultRootWindow (xcontext->disp);
- xcontext->white = XWhitePixel (xcontext->disp, xcontext->screen_num);
- xcontext->black = XBlackPixel (xcontext->disp, xcontext->screen_num);
- xcontext->depth = DefaultDepthOfScreen (xcontext->screen);
-
- xcontext->width = DisplayWidth (xcontext->disp, xcontext->screen_num);
- xcontext->height = DisplayHeight (xcontext->disp, xcontext->screen_num);
- xcontext->widthmm = DisplayWidthMM (xcontext->disp, xcontext->screen_num);
- xcontext->heightmm = DisplayHeightMM (xcontext->disp, xcontext->screen_num);
-
- GST_DEBUG_OBJECT (xvimagesink, "X reports %dx%d pixels and %d mm x %d mm",
- xcontext->width, xcontext->height, xcontext->widthmm, xcontext->heightmm);
-
- gst_xvimagesink_calculate_pixel_aspect_ratio (xcontext);
- /* We get supported pixmap formats at supported depth */
- px_formats = XListPixmapFormats (xcontext->disp, &nb_formats);
-
- if (!px_formats) {
- XCloseDisplay (xcontext->disp);
- g_mutex_unlock (xvimagesink->x_lock);
- g_free (xcontext->par);
- g_free (xcontext);
- GST_ELEMENT_ERROR (xvimagesink, RESOURCE, SETTINGS,
- ("Could not initialise Xv output"), ("Could not get pixel formats"));
- return NULL;
- }
-
- /* We get bpp value corresponding to our running depth */
- for (i = 0; i < nb_formats; i++) {
- if (px_formats[i].depth == xcontext->depth)
- xcontext->bpp = px_formats[i].bits_per_pixel;
- }
-
- XFree (px_formats);
-
- xcontext->endianness =
- (ImageByteOrder (xcontext->disp) ==
- LSBFirst) ? G_LITTLE_ENDIAN : G_BIG_ENDIAN;
-
- /* our caps system handles 24/32bpp RGB as big-endian. */
- if ((xcontext->bpp == 24 || xcontext->bpp == 32) &&
- xcontext->endianness == G_LITTLE_ENDIAN) {
- xcontext->endianness = G_BIG_ENDIAN;
- xcontext->visual->red_mask = GUINT32_TO_BE (xcontext->visual->red_mask);
- xcontext->visual->green_mask = GUINT32_TO_BE (xcontext->visual->green_mask);
- xcontext->visual->blue_mask = GUINT32_TO_BE (xcontext->visual->blue_mask);
- if (xcontext->bpp == 24) {
- xcontext->visual->red_mask >>= 8;
- xcontext->visual->green_mask >>= 8;
- xcontext->visual->blue_mask >>= 8;
- }
- }
-
- xcontext->caps = gst_xvimagesink_get_xv_support (xvimagesink, xcontext);
-
- /* Search for XShm extension support */
-#ifdef HAVE_XSHM
- if (XShmQueryExtension (xcontext->disp) &&
- gst_xvimagesink_check_xshm_calls (xvimagesink, xcontext)) {
- xcontext->use_xshm = TRUE;
- GST_DEBUG ("xvimagesink is using XShm extension");
- } else
-#endif /* HAVE_XSHM */
- {
- xcontext->use_xshm = FALSE;
- GST_DEBUG ("xvimagesink is not using XShm extension");
- }
-
- if (!xcontext->caps) {
- XCloseDisplay (xcontext->disp);
- g_mutex_unlock (xvimagesink->x_lock);
- g_free (xcontext->par);
- g_free (xcontext);
- /* GST_ELEMENT_ERROR is thrown by gst_xvimagesink_get_xv_support */
- return NULL;
- }
-
- xv_attr = XvQueryPortAttributes (xcontext->disp,
- xcontext->xv_port_id, &N_attr);
-
-
- /* Generate the channels list */
- for (i = 0; i < (sizeof (channels) / sizeof (char *)); i++) {
- XvAttribute *matching_attr = NULL;
-
- /* Retrieve the property atom if it exists. If it doesn't exist,
- * the attribute itself must not either, so we can skip */
- prop_atom = XInternAtom (xcontext->disp, channels[i], True);
- if (prop_atom == None)
- continue;
-
- if (xv_attr != NULL) {
- for (j = 0; j < N_attr && matching_attr == NULL; ++j)
- if (!g_ascii_strcasecmp (channels[i], xv_attr[j].name))
- matching_attr = xv_attr + j;
- }
-
- if (matching_attr) {
- GstColorBalanceChannel *channel;
-
- channel = g_object_new (GST_TYPE_COLOR_BALANCE_CHANNEL, NULL);
- channel->label = g_strdup (channels[i]);
- channel->min_value = matching_attr ? matching_attr->min_value : -1000;
- channel->max_value = matching_attr ? matching_attr->max_value : 1000;
-
- xcontext->channels_list = g_list_append (xcontext->channels_list,
- channel);
-
- /* If the colorbalance settings have not been touched we get Xv values
- as defaults and update our internal variables */
- if (!xvimagesink->cb_changed) {
- gint val;
-
- XvGetPortAttribute (xcontext->disp, xcontext->xv_port_id,
- prop_atom, &val);
- /* Normalize val to [-1000, 1000] */
- val = floor (0.5 + -1000 + 2000 * (val - channel->min_value) /
- (double) (channel->max_value - channel->min_value));
-
- if (!g_ascii_strcasecmp (channels[i], "XV_HUE"))
- xvimagesink->hue = val;
- else if (!g_ascii_strcasecmp (channels[i], "XV_SATURATION"))
- xvimagesink->saturation = val;
- else if (!g_ascii_strcasecmp (channels[i], "XV_BRIGHTNESS"))
- xvimagesink->brightness = val;
- else if (!g_ascii_strcasecmp (channels[i], "XV_CONTRAST"))
- xvimagesink->contrast = val;
- }
- }
- }
-
- if (xv_attr)
- XFree (xv_attr);
-
- g_mutex_unlock (xvimagesink->x_lock);
-
- return xcontext;
-}
-
-/* This function cleans the X context. Closing the Display, releasing the XV
- port and unrefing the caps for supported formats. */
-static void
-gst_xvimagesink_xcontext_clear (GstXvImageSink * xvimagesink)
-{
- GList *formats_list, *channels_list;
- GstXContext *xcontext;
- gint i = 0;
-
- g_return_if_fail (GST_IS_XVIMAGESINK (xvimagesink));
-
- GST_OBJECT_LOCK (xvimagesink);
- if (xvimagesink->xcontext == NULL) {
- GST_OBJECT_UNLOCK (xvimagesink);
- return;
- }
-
- /* Take the XContext from the sink and clean it up */
- xcontext = xvimagesink->xcontext;
- xvimagesink->xcontext = NULL;
-
- GST_OBJECT_UNLOCK (xvimagesink);
-
-
- formats_list = xcontext->formats_list;
-
- while (formats_list) {
- GstXvImageFormat *format = formats_list->data;
-
- gst_caps_unref (format->caps);
- g_free (format);
- formats_list = g_list_next (formats_list);
- }
-
- if (xcontext->formats_list)
- g_list_free (xcontext->formats_list);
-
- channels_list = xcontext->channels_list;
-
- while (channels_list) {
- GstColorBalanceChannel *channel = channels_list->data;
-
- g_object_unref (channel);
- channels_list = g_list_next (channels_list);
- }
-
- if (xcontext->channels_list)
- g_list_free (xcontext->channels_list);
-
- gst_caps_unref (xcontext->caps);
- if (xcontext->last_caps)
- gst_caps_replace (&xcontext->last_caps, NULL);
-
- for (i = 0; i < xcontext->nb_adaptors; i++) {
- g_free (xcontext->adaptors[i]);
- }
-
- g_free (xcontext->adaptors);
-
- g_free (xcontext->par);
-
- g_mutex_lock (xvimagesink->x_lock);
-
- GST_DEBUG_OBJECT (xvimagesink, "Closing display and freeing X Context");
-
- XvUngrabPort (xcontext->disp, xcontext->xv_port_id, 0);
-
- XCloseDisplay (xcontext->disp);
-
- g_mutex_unlock (xvimagesink->x_lock);
-
- g_free (xcontext);
-}
-