gstv4l2object.c 61.1 KB
Newer Older
1 2
/* GStreamer
 *
3
 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
4 5 6
 *               2006 Edgard Lima <edgard.lima@indt.org.br>
 *
 * gstv4l2object.c: base class for V4L2 elements
7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. This library is distributed in the hope
 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
Rob Clark's avatar
Rob Clark committed
18
 * USA.
19 20 21 22 23 24 25 26 27 28 29 30
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

31 32 33 34
#ifdef HAVE_GUDEV
#include <gudev/gudev.h>
#endif

35 36
#include "v4l2_calls.h"
#include "gstv4l2tuner.h"
Rob Clark's avatar
Rob Clark committed
37
#ifdef HAVE_XVIDEO
38 39 40 41
#include "gstv4l2xoverlay.h"
#endif
#include "gstv4l2colorbalance.h"

Rob Clark's avatar
Rob Clark committed
42 43
#include "gst/gst-i18n-plugin.h"

44 45 46 47 48 49 50
/* videodev2.h is not versioned and we can't easily check for the presence
 * of enum values at compile time, but the V4L2_CAP_VIDEO_OUTPUT_OVERLAY define
 * was added in the same commit as V4L2_FIELD_INTERLACED_{TB,BT} (b2787845) */
#ifndef V4L2_CAP_VIDEO_OUTPUT_OVERLAY
#define V4L2_FIELD_INTERLACED_TB 8
#define V4L2_FIELD_INTERLACED_BT 9
#endif
Rob Clark's avatar
Rob Clark committed
51 52 53 54 55

GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
#define GST_CAT_DEFAULT v4l2_debug


56
#define DEFAULT_PROP_DEVICE_NAME 	NULL
57
#define DEFAULT_PROP_DEVICE_FD          -1
58
#define DEFAULT_PROP_FLAGS              0
59
#define DEFAULT_PROP_NORM               NULL
60
#define DEFAULT_PROP_CHANNEL            NULL
61 62
#define DEFAULT_PROP_FREQUENCY          0

Stefan Kost's avatar
Stefan Kost committed
63 64 65 66 67 68 69
enum
{
  PROP_0,
  V4L2_STD_OBJECT_PROPS,
};

const GList *
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
{
  GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
  static GList *list = NULL;

  /* well, not perfect, but better than no locking at all.
   * In the worst case we leak a list node, so who cares? */
  GST_CLASS_LOCK (GST_OBJECT_CLASS (klass));

  if (!list) {
    list = g_list_append (NULL, g_object_class_find_property (klass, "device"));
  }

  GST_CLASS_UNLOCK (GST_OBJECT_CLASS (klass));

  return list;
}

88 89 90 91
static gboolean init = FALSE;
static GList *devices = NULL;

#ifdef HAVE_GUDEV
92
static gboolean
93
gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
94 95
    GList ** klass_devices)
{
96
  GUdevClient *client = NULL;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  GList *item;

  if (!check) {
    while (devices) {
      gchar *device = devices->data;
      devices = g_list_remove (devices, device);
      g_free (device);
    }

    GST_INFO ("Enumerating video4linux devices from udev");
    client = g_udev_client_new (NULL);
    if (!client) {
      GST_WARNING ("Failed to initialize gudev client");
      goto finish;
    }

    item = g_udev_client_query_by_subsystem (client, "video4linux");
    while (item) {
      GUdevDevice *device = item->data;
      gchar *devnode = g_strdup (g_udev_device_get_device_file (device));
      gint api = g_udev_device_get_property_as_int (device, "ID_V4L_VERSION");
      GST_INFO ("Found new device: %s, API: %d", devnode, api);
      /* Append v4l2 devices only. If api is 0 probably v4l_id has
         been stripped out of the current udev installation, append
         anyway */
      if (api == 0) {
        GST_WARNING
            ("Couldn't retrieve ID_V4L_VERSION, silly udev installation?");
      }
      if ((api == 2 || api == 0)) {
        devices = g_list_append (devices, devnode);
      } else {
        g_free (devnode);
      }
      g_object_unref (device);
      item = item->next;
    }
    g_list_free (item);
    init = TRUE;
  }

finish:
  if (client) {
    g_object_unref (client);
  }

  *klass_devices = devices;
144

145 146 147 148 149 150 151 152
  return init;
}
#endif /* HAVE_GUDEV */

static gboolean
gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
    GList ** klass_devices)
{
153
  if (!check) {
Stefan Kost's avatar
Stefan Kost committed
154
    const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
155 156 157
    gint base, n, fd;

    while (devices) {
158
      gchar *device = devices->data;
159
      devices = g_list_remove (devices, device);
160 161 162 163
      g_free (device);
    }

    /*
Rob Clark's avatar
Rob Clark committed
164
     * detect /dev entries
165 166 167 168 169 170 171 172 173
     */
    for (n = 0; n < 64; n++) {
      for (base = 0; dev_base[base] != NULL; base++) {
        struct stat s;
        gchar *device = g_strdup_printf ("%s%d",
            dev_base[base],
            n);

        /*
Rob Clark's avatar
Rob Clark committed
174
         * does the /dev/ entry exist at all?
175 176 177
         */
        if (stat (device, &s) == 0) {
          /*
Rob Clark's avatar
Rob Clark committed
178
           * yes: is a device attached?
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
           */
          if (S_ISCHR (s.st_mode)) {

            if ((fd = open (device, O_RDWR | O_NONBLOCK)) > 0 || errno == EBUSY) {
              if (fd > 0)
                close (fd);

              devices = g_list_append (devices, device);
              break;
            }
          }
        }
        g_free (device);
      }
    }
    init = TRUE;
  }

  *klass_devices = devices;

  return init;
}

void
gst_v4l2_probe_probe_property (GstPropertyProbe * probe,
    guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);

  switch (prop_id) {
    case PROP_DEVICE:
210 211 212 213
#ifdef HAVE_GUDEV
      if (!gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices))
        gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
#else /* !HAVE_GUDEV */
214
      gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
215
#endif /* HAVE_GUDEV */
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
      break;
  }
}

gboolean
gst_v4l2_probe_needs_probe (GstPropertyProbe * probe,
    guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
  gboolean ret = FALSE;

  switch (prop_id) {
    case PROP_DEVICE:
232 233 234 235
#ifdef HAVE_GUDEV
      ret =
          !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
#else /* !HAVE_GUDEV */
236
      ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
237
#endif /* HAVE_GUDEV */
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
      break;
  }
  return ret;
}

static GValueArray *
gst_v4l2_class_list_devices (GstElementClass * klass, GList ** klass_devices)
{
  GValueArray *array;
  GValue value = { 0 };
  GList *item;

  if (!*klass_devices)
    return NULL;

  array = g_value_array_new (g_list_length (*klass_devices));
  item = *klass_devices;
  g_value_init (&value, G_TYPE_STRING);
  while (item) {
    gchar *device = item->data;

    g_value_set_string (&value, device);
    g_value_array_append (array, &value);

    item = item->next;
  }
  g_value_unset (&value);

  return array;
}

GValueArray *
gst_v4l2_probe_get_values (GstPropertyProbe * probe,
    guint prop_id, const GParamSpec * pspec, GList ** klass_devices)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (probe);
  GValueArray *array = NULL;

  switch (prop_id) {
    case PROP_DEVICE:
      array = gst_v4l2_class_list_devices (klass, klass_devices);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
      break;
  }

  return array;
}

#define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
292
static GType
293 294 295 296 297 298
gst_v4l2_device_get_type (void)
{
  static GType v4l2_device_type = 0;

  if (v4l2_device_type == 0) {
    static const GFlagsValue values[] = {
299 300 301 302 303 304 305 306 307
      {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
      {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
      {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},

      {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
      {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},

      {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
      {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
308 309 310 311 312 313 314 315 316 317 318 319

      {0, NULL, NULL}
    };

    v4l2_device_type =
        g_flags_register_static ("GstV4l2DeviceTypeFlags", values);
  }

  return v4l2_device_type;
}

void
Rob Clark's avatar
Rob Clark committed
320 321
gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
    const char *default_device)
322
{
323 324
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device", "Device location",
325
          default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 327
  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
328 329
          "Name of the device", DEFAULT_PROP_DEVICE_NAME,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
330 331 332
  g_object_class_install_property (gobject_class, PROP_DEVICE_FD,
      g_param_spec_int ("device-fd", "File descriptor",
          "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
333
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
334 335
  g_object_class_install_property (gobject_class, PROP_FLAGS,
      g_param_spec_flags ("flags", "Flags", "Device type flags",
336 337
          GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
338

339 340 341 342 343 344 345
  /**
   * GstV4l2Src:brightness
   *
   * Picture brightness, or more precisely, the black level
   *
   * Since: 0.10.26
   */
346 347 348 349 350
  g_object_class_install_property (gobject_class, PROP_BRIGHTNESS,
      g_param_spec_int ("brightness", "Brightness",
          "Picture brightness, or more precisely, the black level", G_MININT,
          G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
351 352 353 354 355 356 357
  /**
   * GstV4l2Src:contrast
   *
   * Picture contrast or luma gain
   *
   * Since: 0.10.26
   */
358 359 360 361 362
  g_object_class_install_property (gobject_class, PROP_CONTRAST,
      g_param_spec_int ("contrast", "Contrast",
          "Picture contrast or luma gain", G_MININT,
          G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
363 364 365 366 367 368 369
  /**
   * GstV4l2Src:saturation
   *
   * Picture color saturation or chroma gain
   *
   * Since: 0.10.26
   */
370 371 372 373 374
  g_object_class_install_property (gobject_class, PROP_SATURATION,
      g_param_spec_int ("saturation", "Saturation",
          "Picture color saturation or chroma gain", G_MININT,
          G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
375 376 377 378 379 380 381
  /**
   * GstV4l2Src:hue
   *
   * Hue or color balance
   *
   * Since: 0.10.26
   */
382 383 384 385 386
  g_object_class_install_property (gobject_class, PROP_HUE,
      g_param_spec_int ("hue", "Hue",
          "Hue or color balance", G_MININT,
          G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
387 388 389
}

GstV4l2Object *
390
gst_v4l2_object_new (GstElement * element,
Rob Clark's avatar
Rob Clark committed
391
    enum v4l2_buf_type type,
392
    const char *default_device,
393
    GstV4l2GetInOutFunction get_in_out_func,
394 395
    GstV4l2SetInOutFunction set_in_out_func,
    GstV4l2UpdateFpsFunction update_fps_func)
396 397 398 399
{
  GstV4l2Object *v4l2object;

  /*
Rob Clark's avatar
Rob Clark committed
400
   * some default values
401 402 403
   */
  v4l2object = g_new0 (GstV4l2Object, 1);

Rob Clark's avatar
Rob Clark committed
404 405 406
  v4l2object->type = type;
  v4l2object->formats = NULL;

407 408 409
  v4l2object->element = element;
  v4l2object->get_in_out_func = get_in_out_func;
  v4l2object->set_in_out_func = set_in_out_func;
410
  v4l2object->update_fps_func = update_fps_func;
411 412

  v4l2object->video_fd = -1;
413
  v4l2object->poll = gst_poll_new (TRUE);
414
  v4l2object->buffer = NULL;
Rob Clark's avatar
Rob Clark committed
415
  v4l2object->videodev = g_strdup (default_device);
416

417 418
  v4l2object->norms = NULL;
  v4l2object->channels = NULL;
419 420 421 422 423 424 425
  v4l2object->colors = NULL;

  v4l2object->xwindow_id = 0;

  return v4l2object;
}

Rob Clark's avatar
Rob Clark committed
426 427 428
static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);


429
void
430
gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
431
{
432
  g_return_if_fail (v4l2object != NULL);
433

434 435 436
  if (v4l2object->videodev)
    g_free (v4l2object->videodev);

437 438 439
  if (v4l2object->poll)
    gst_poll_free (v4l2object->poll);

440 441 442
  if (v4l2object->channel)
    g_free (v4l2object->channel);

443 444 445
  if (v4l2object->norm)
    g_free (v4l2object->norm);

Rob Clark's avatar
Rob Clark committed
446 447 448 449
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

450
  g_free (v4l2object);
451 452
}

Rob Clark's avatar
Rob Clark committed
453 454 455 456 457 458 459 460 461 462 463

static gboolean
gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object)
{
  g_slist_foreach (v4l2object->formats, (GFunc) g_free, NULL);
  g_slist_free (v4l2object->formats);
  v4l2object->formats = NULL;

  return TRUE;
}

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
static gint
gst_v4l2_object_prop_to_cid (guint prop_id)
{
  gint cid = -1;

  switch (prop_id) {
    case PROP_BRIGHTNESS:
      cid = V4L2_CID_BRIGHTNESS;
      break;
    case PROP_CONTRAST:
      cid = V4L2_CID_CONTRAST;
      break;
    case PROP_SATURATION:
      cid = V4L2_CID_SATURATION;
      break;
    case PROP_HUE:
      cid = V4L2_CID_HUE;
      break;
    default:
      GST_WARNING ("unmapped property id: %d", prop_id);
  }
  return cid;
}

Rob Clark's avatar
Rob Clark committed
488

489
gboolean
490
gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
491 492 493 494
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
Wim Taymans's avatar
Wim Taymans committed
495
      g_free (v4l2object->videodev);
496
      v4l2object->videodev = g_value_dup_string (value);
497
      break;
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    case PROP_BRIGHTNESS:
    case PROP_CONTRAST:
    case PROP_SATURATION:
    case PROP_HUE:
    {
      gint cid = gst_v4l2_object_prop_to_cid (prop_id);

      if (cid != -1) {
        if (GST_V4L2_IS_OPEN (v4l2object)) {
          gst_v4l2_set_attribute (v4l2object, cid, g_value_get_int (value));
        }
      }
      return TRUE;
    }
      break;
513
#if 0
514
    case PROP_NORM:
515 516 517
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerNorm *norm = gst_tuner_find_norm_by_name (tuner,
518
            (gchar *) g_value_get_string (value));
519 520 521 522 523 524 525

        if (norm) {
          /* like gst_tuner_set_norm (tuner, norm)
             without g_object_notify */
          gst_v4l2_tuner_set_norm (v4l2object, norm);
        }
      } else {
526 527
        g_free (v4l2object->norm);
        v4l2object->norm = g_value_dup_string (value);
528 529
      }
      break;
530
    case PROP_CHANNEL:
531 532 533
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
534
            (gchar *) g_value_get_string (value));
535 536 537 538 539 540 541

        if (channel) {
          /* like gst_tuner_set_channel (tuner, channel)
             without g_object_notify */
          gst_v4l2_tuner_set_channel (v4l2object, channel);
        }
      } else {
542 543
        g_free (v4l2object->channel);
        v4l2object->channel = g_value_dup_string (value);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
      }
      break;
    case PROP_FREQUENCY:
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_get_channel (tuner);

        if (channel &&
            GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
          /* like
             gst_tuner_set_frequency (tuner, channel, g_value_get_ulong (value))
             without g_object_notify */
          gst_v4l2_tuner_set_frequency (v4l2object, channel,
              g_value_get_ulong (value));
        }
      } else {
        v4l2object->frequency = g_value_get_ulong (value);
      }
      break;
563
#endif
564 565 566 567 568 569 570 571 572
    default:
      return FALSE;
      break;
  }
  return TRUE;
}


gboolean
573
gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
574 575 576 577 578 579 580 581
    guint prop_id, GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
      g_value_set_string (value, v4l2object->videodev);
      break;
    case PROP_DEVICE_NAME:
    {
582
      const guchar *new = NULL;
583

584
      if (GST_V4L2_IS_OPEN (v4l2object)) {
585
        new = v4l2object->vcap.card;
586
      } else if (gst_v4l2_open (v4l2object)) {
587
        new = v4l2object->vcap.card;
588 589
        gst_v4l2_close (v4l2object);
      }
590
      g_value_set_string (value, (gchar *) new);
591 592
      break;
    }
593 594 595 596 597 598 599 600
    case PROP_DEVICE_FD:
    {
      if (GST_V4L2_IS_OPEN (v4l2object))
        g_value_set_int (value, v4l2object->video_fd);
      else
        g_value_set_int (value, DEFAULT_PROP_DEVICE_FD);
      break;
    }
601 602 603 604 605 606 607 608
    case PROP_FLAGS:
    {
      guint flags = 0;

      if (GST_V4L2_IS_OPEN (v4l2object)) {
        flags |= v4l2object->vcap.capabilities &
            (V4L2_CAP_VIDEO_CAPTURE |
            V4L2_CAP_VIDEO_OUTPUT |
Edgard Lima's avatar
Edgard Lima committed
609 610 611
            V4L2_CAP_VIDEO_OVERLAY |
            V4L2_CAP_VBI_CAPTURE |
            V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
612 613 614 615
      }
      g_value_set_flags (value, flags);
      break;
    }
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    case PROP_BRIGHTNESS:
    case PROP_CONTRAST:
    case PROP_SATURATION:
    case PROP_HUE:
    {
      gint cid = gst_v4l2_object_prop_to_cid (prop_id);

      if (cid != -1) {
        if (GST_V4L2_IS_OPEN (v4l2object)) {
          gint v;
          if (gst_v4l2_get_attribute (v4l2object, cid, &v)) {
            g_value_set_int (value, v);
          }
        }
      }
      return TRUE;
    }
      break;
634 635 636 637 638 639 640 641 642 643 644 645
    default:
      return FALSE;
      break;
  }
  return TRUE;
}

static void
gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
{
  GstTunerNorm *norm = NULL;
  GstTunerChannel *channel = NULL;
Rob Clark's avatar
Rob Clark committed
646 647 648 649 650 651
  GstTuner *tuner;

  if (!GST_IS_TUNER (v4l2object->element))
    return;

  tuner = GST_TUNER (v4l2object->element);
652

653 654
  if (v4l2object->norm)
    norm = gst_tuner_find_norm_by_name (tuner, v4l2object->norm);
655 656 657 658 659 660
  if (norm) {
    gst_tuner_set_norm (tuner, norm);
  } else {
    norm =
        GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
    if (norm) {
661 662
      g_free (v4l2object->norm);
      v4l2object->norm = g_strdup (norm->label);
663 664 665 666
      gst_tuner_norm_changed (tuner, norm);
    }
  }

667 668
  if (v4l2object->channel)
    channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
669 670 671 672
  if (channel) {
    gst_tuner_set_channel (tuner, channel);
  } else {
    channel =
673 674
        GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
            (v4l2object->element)));
675 676 677 678 679
    if (channel) {
      g_free (v4l2object->channel);
      v4l2object->channel = g_strdup (channel->label);
      gst_tuner_channel_changed (tuner, channel);
    }
680 681
  }

682 683
  if (channel
      && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
684 685 686 687 688 689 690 691 692 693 694 695 696 697
    if (v4l2object->frequency != 0) {
      gst_tuner_set_frequency (tuner, channel, v4l2object->frequency);
    } else {
      v4l2object->frequency = gst_tuner_get_frequency (tuner, channel);
      if (v4l2object->frequency == 0) {
        /* guess */
        gst_tuner_set_frequency (tuner, channel, 1000);
      } else {
      }
    }
  }
}

gboolean
698
gst_v4l2_object_start (GstV4l2Object * v4l2object)
699 700 701 702 703 704
{
  if (gst_v4l2_open (v4l2object))
    gst_v4l2_set_defaults (v4l2object);
  else
    return FALSE;

Rob Clark's avatar
Rob Clark committed
705
#ifdef HAVE_XVIDEO
706 707 708 709 710 711 712
  gst_v4l2_xoverlay_start (v4l2object);
#endif

  return TRUE;
}

gboolean
713
gst_v4l2_object_stop (GstV4l2Object * v4l2object)
714
{
Rob Clark's avatar
Rob Clark committed
715
#ifdef HAVE_XVIDEO
716 717 718 719 720 721
  gst_v4l2_xoverlay_stop (v4l2object);
#endif

  if (!gst_v4l2_close (v4l2object))
    return FALSE;

722 723 724 725
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

726 727
  return TRUE;
}
Rob Clark's avatar
Rob Clark committed
728 729 730 731 732


/*
 * common format / caps utilities:
 */
733 734 735 736 737
typedef struct
{
  guint32 format;
  gboolean dimensions;
} GstV4L2FormatDesc;
Rob Clark's avatar
Rob Clark committed
738

739
static const GstV4L2FormatDesc gst_v4l2_formats[] = {
Rob Clark's avatar
Rob Clark committed
740
  /* from Linux 2.6.15 videodev2.h */
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  {V4L2_PIX_FMT_RGB332, TRUE},
  {V4L2_PIX_FMT_RGB555, TRUE},
  {V4L2_PIX_FMT_RGB565, TRUE},
  {V4L2_PIX_FMT_RGB555X, TRUE},
  {V4L2_PIX_FMT_RGB565X, TRUE},
  {V4L2_PIX_FMT_BGR24, TRUE},
  {V4L2_PIX_FMT_RGB24, TRUE},
  {V4L2_PIX_FMT_BGR32, TRUE},
  {V4L2_PIX_FMT_RGB32, TRUE},
  {V4L2_PIX_FMT_GREY, TRUE},
  {V4L2_PIX_FMT_YVU410, TRUE},
  {V4L2_PIX_FMT_YVU420, TRUE},
  {V4L2_PIX_FMT_YUYV, TRUE},
  {V4L2_PIX_FMT_UYVY, TRUE},
  {V4L2_PIX_FMT_YUV422P, TRUE},
  {V4L2_PIX_FMT_YUV411P, TRUE},
  {V4L2_PIX_FMT_Y41P, TRUE},
Rob Clark's avatar
Rob Clark committed
758 759

  /* two planes -- one Y, one Cr + Cb interleaved  */
760 761
  {V4L2_PIX_FMT_NV12, TRUE},
  {V4L2_PIX_FMT_NV21, TRUE},
Rob Clark's avatar
Rob Clark committed
762 763

  /*  The following formats are not defined in the V4L2 specification */
764 765 766 767
  {V4L2_PIX_FMT_YUV410, TRUE},
  {V4L2_PIX_FMT_YUV420, TRUE},
  {V4L2_PIX_FMT_YYUV, TRUE},
  {V4L2_PIX_FMT_HI240, TRUE},
Rob Clark's avatar
Rob Clark committed
768 769 770

  /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
#ifdef V4L2_PIX_FMT_SBGGR8
771
  {V4L2_PIX_FMT_SBGGR8, TRUE},
Rob Clark's avatar
Rob Clark committed
772 773 774
#endif

  /* compressed formats */
775 776 777 778
  {V4L2_PIX_FMT_MJPEG, TRUE},
  {V4L2_PIX_FMT_JPEG, TRUE},
  {V4L2_PIX_FMT_DV, TRUE},
  {V4L2_PIX_FMT_MPEG, FALSE},
Rob Clark's avatar
Rob Clark committed
779 780

  /*  Vendor-specific formats   */
781
  {V4L2_PIX_FMT_WNVA, TRUE},
Rob Clark's avatar
Rob Clark committed
782 783

#ifdef V4L2_PIX_FMT_SN9C10X
784
  {V4L2_PIX_FMT_SN9C10X, TRUE},
Rob Clark's avatar
Rob Clark committed
785 786
#endif
#ifdef V4L2_PIX_FMT_PWC1
787
  {V4L2_PIX_FMT_PWC1, TRUE},
Rob Clark's avatar
Rob Clark committed
788 789
#endif
#ifdef V4L2_PIX_FMT_PWC2
790
  {V4L2_PIX_FMT_PWC2, TRUE},
Rob Clark's avatar
Rob Clark committed
791 792
#endif
#ifdef V4L2_PIX_FMT_YVYU
793
  {V4L2_PIX_FMT_YVYU, TRUE},
Rob Clark's avatar
Rob Clark committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
#endif
};

#define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))


static struct v4l2_fmtdesc *
gst_v4l2_object_get_format_from_fourcc (GstV4l2Object * v4l2object,
    guint32 fourcc)
{
  struct v4l2_fmtdesc *fmt;
  GSList *walk;

  if (fourcc == 0)
    return NULL;

  walk = gst_v4l2_object_get_format_list (v4l2object);
  while (walk) {
    fmt = (struct v4l2_fmtdesc *) walk->data;
    if (fmt->pixelformat == fourcc)
      return fmt;
    /* special case for jpeg */
    if ((fmt->pixelformat == V4L2_PIX_FMT_MJPEG && fourcc == V4L2_PIX_FMT_JPEG)
        || (fmt->pixelformat == V4L2_PIX_FMT_JPEG
            && fourcc == V4L2_PIX_FMT_MJPEG)) {
      return fmt;
    }
    walk = g_slist_next (walk);
  }

  return NULL;
}



/* complete made up ranking, the values themselves are meaningless */
#define YUV_BASE_RANK     1000
#define JPEG_BASE_RANK     500
#define DV_BASE_RANK       200
#define RGB_BASE_RANK      100
#define YUV_ODD_BASE_RANK   50
#define RGB_ODD_BASE_RANK   25
#define BAYER_BASE_RANK     15
#define S910_BASE_RANK      10
#define GREY_BASE_RANK       5
#define PWC_BASE_RANK        1

841 842 843 844 845 846 847
/* This flag is already used by libv4l2 although
 * it was added to the Linux kernel in 2.6.32
 */
#ifndef V4L2_FMT_FLAG_EMULATED
#define V4L2_FMT_FLAG_EMULATED 0x0002
#endif

Rob Clark's avatar
Rob Clark committed
848
static gint
849
gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
Rob Clark's avatar
Rob Clark committed
850
{
851
  guint32 fourcc = fmt->pixelformat;
852
  gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
853 854
  gint rank = 0;

Rob Clark's avatar
Rob Clark committed
855 856
  switch (fourcc) {
    case V4L2_PIX_FMT_MJPEG:
857 858
      rank = JPEG_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
859
    case V4L2_PIX_FMT_JPEG:
860 861
      rank = JPEG_BASE_RANK + 1;
      break;
862 863 864
    case V4L2_PIX_FMT_MPEG:    /* MPEG          */
      rank = JPEG_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
865 866 867 868 869 870

    case V4L2_PIX_FMT_RGB332:
    case V4L2_PIX_FMT_RGB555:
    case V4L2_PIX_FMT_RGB555X:
    case V4L2_PIX_FMT_RGB565:
    case V4L2_PIX_FMT_RGB565X:
871 872
      rank = RGB_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
873 874 875

    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
876 877
      rank = RGB_BASE_RANK - 1;
      break;
Rob Clark's avatar
Rob Clark committed
878 879 880

    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_BGR32:
881 882
      rank = RGB_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
883 884

    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
885 886
      rank = GREY_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
887 888 889 890 891

    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
    case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
    case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
892 893
      rank = YUV_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
894 895

    case V4L2_PIX_FMT_YVU410:  /* YVU9,  9 bits per pixel */
896 897
      rank = YUV_BASE_RANK + 3;
      break;
Rob Clark's avatar
Rob Clark committed
898
    case V4L2_PIX_FMT_YUV410:  /* YUV9,  9 bits per pixel */
899 900
      rank = YUV_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
901
    case V4L2_PIX_FMT_YUV420:  /* I420, 12 bits per pixel */
902 903
      rank = YUV_BASE_RANK + 7;
      break;
Rob Clark's avatar
Rob Clark committed
904
    case V4L2_PIX_FMT_YUYV:    /* YUY2, 16 bits per pixel */
905 906
      rank = YUV_BASE_RANK + 10;
      break;
Rob Clark's avatar
Rob Clark committed
907
    case V4L2_PIX_FMT_YVU420:  /* YV12, 12 bits per pixel */
908 909
      rank = YUV_BASE_RANK + 6;
      break;
Rob Clark's avatar
Rob Clark committed
910
    case V4L2_PIX_FMT_UYVY:    /* UYVY, 16 bits per pixel */
911 912
      rank = YUV_BASE_RANK + 9;
      break;
Rob Clark's avatar
Rob Clark committed
913
    case V4L2_PIX_FMT_Y41P:    /* Y41P, 12 bits per pixel */
914 915
      rank = YUV_BASE_RANK + 5;
      break;
Rob Clark's avatar
Rob Clark committed
916
    case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
917 918
      rank = YUV_BASE_RANK + 4;
      break;
Rob Clark's avatar
Rob Clark committed
919
    case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
920 921
      rank = YUV_BASE_RANK + 8;
      break;
Rob Clark's avatar
Rob Clark committed
922 923

    case V4L2_PIX_FMT_DV:
924 925
      rank = DV_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
926 927

    case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
928 929
      rank = 0;
      break;
Rob Clark's avatar
Rob Clark committed
930 931 932

#ifdef V4L2_PIX_FMT_SBGGR8
    case V4L2_PIX_FMT_SBGGR8:
933 934
      rank = BAYER_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
935 936 937 938
#endif

#ifdef V4L2_PIX_FMT_SN9C10X
    case V4L2_PIX_FMT_SN9C10X:
939 940
      rank = S910_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
941 942 943 944
#endif

#ifdef V4L2_PIX_FMT_PWC1
    case V4L2_PIX_FMT_PWC1:
945 946
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
947 948 949
#endif
#ifdef V4L2_PIX_FMT_PWC2
    case V4L2_PIX_FMT_PWC2:
950 951
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
952 953 954
#endif

    default:
955
      rank = 0;
Rob Clark's avatar
Rob Clark committed
956 957 958
      break;
  }

959 960 961 962 963 964 965 966
  /* All ranks are below 1<<15 so a shift by 15
   * will a) make all non-emulated formats larger
   * than emulated and b) will not overflow
   */
  if (!emulated)
    rank <<= 15;

  return rank;
Rob Clark's avatar
Rob Clark committed
967 968 969 970 971 972 973
}



static gint
format_cmp_func (gconstpointer a, gconstpointer b)
{
974 975
  const struct v4l2_fmtdesc *fa = a;
  const struct v4l2_fmtdesc *fb = b;
Rob Clark's avatar
Rob Clark committed
976

977
  if (fa->pixelformat == fb->pixelformat)
Rob Clark's avatar
Rob Clark committed
978 979
    return 0;

980 981
  return gst_v4l2_object_format_get_rank (fb) -
      gst_v4l2_object_format_get_rank (fa);
Rob Clark's avatar
Rob Clark committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
}

/******************************************************
 * gst_v4l2_object_fill_format_list():
 *   create list of supported capture formats
 * return value: TRUE on success, FALSE on error
 ******************************************************/
static gboolean
gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object)
{
  gint n;
  struct v4l2_fmtdesc *format;

  GST_DEBUG_OBJECT (v4l2object->element, "getting src format enumerations");

  /* format enumeration */
  for (n = 0;; n++) {
    format = g_new0 (struct v4l2_fmtdesc, 1);

    format->index = n;
    format->type = v4l2object->type;

    if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
      if (errno == EINVAL) {
        g_free (format);
        break;                  /* end of enumeration */
      } else {
        goto failed;
      }
    }

    GST_LOG_OBJECT (v4l2object->element, "index:       %u", format->index);
    GST_LOG_OBJECT (v4l2object->element, "type:        %d", format->type);
    GST_LOG_OBJECT (v4l2object->element, "flags:       %08x", format->flags);
    GST_LOG_OBJECT (v4l2object->element, "description: '%s'",
        format->description);
    GST_LOG_OBJECT (v4l2object->element, "pixelformat: %" GST_FOURCC_FORMAT,
        GST_FOURCC_ARGS (format->pixelformat));

    /* sort formats according to our preference;  we do this, because caps
     * are probed in the order the formats are in the list, and the order of
     * formats in the final probed caps matters for things like fixation */
    v4l2object->formats = g_slist_insert_sorted (v4l2object->formats, format,
        (GCompareFunc) format_cmp_func);
  }

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
#ifndef GST_DISABLE_GST_DEBUG
  {
    GSList *l;

    GST_INFO_OBJECT (v4l2object->element, "got %d format(s):", n);
    for (l = v4l2object->formats; l != NULL; l = l->next) {
      format = l->data;

      GST_INFO_OBJECT (v4l2object->element,
          "  %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
          ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
    }
  }
#endif
Rob Clark's avatar
Rob Clark committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

  return TRUE;

  /* ERRORS */
failed:
  {
    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
        (_("Failed to enumerate possible video formats device '%s' can work with"), v4l2object->videodev), ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)", n, v4l2object->videodev, errno, g_strerror (errno)));
    g_free (format);
    return FALSE;
  }
}

Stefan Kost's avatar
Stefan Kost committed
1055
/*
Rob Clark's avatar
Rob Clark committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
 * Get the list of supported capture formats, a list of
 * <code>struct v4l2_fmtdesc</code>.
 */
GSList *
gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
{
  if (!v4l2object->formats)
    gst_v4l2_object_fill_format_list (v4l2object);
  return v4l2object->formats;
}


GstStructure *
gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
{
  GstStructure *structure = NULL;

  switch (fourcc) {
    case V4L2_PIX_FMT_MJPEG:   /* Motion-JPEG */
    case V4L2_PIX_FMT_JPEG:    /* JFIF JPEG */
      structure = gst_structure_new ("image/jpeg", NULL);
      break;
    case V4L2_PIX_FMT_RGB332:
    case V4L2_PIX_FMT_RGB555:
    case V4L2_PIX_FMT_RGB555X:
    case V4L2_PIX_FMT_RGB565:
    case V4L2_PIX_FMT_RGB565X:
    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_BGR32:{
      guint depth = 0, bpp = 0;

      gint endianness = 0;

      guint32 r_mask = 0, b_mask = 0, g_mask = 0;

      switch (fourcc) {
        case V4L2_PIX_FMT_RGB332:
          bpp = depth = 8;
          endianness = G_BYTE_ORDER;    /* 'like, whatever' */
          r_mask = 0xe0;
          g_mask = 0x1c;
          b_mask = 0x03;
          break;
        case V4L2_PIX_FMT_RGB555:
        case V4L2_PIX_FMT_RGB555X:
          bpp = 16;
          depth = 15;
          endianness =
              fourcc == V4L2_PIX_FMT_RGB555X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
          r_mask = 0x7c00;
          g_mask = 0x03e0;
          b_mask = 0x001f;
          break;
        case V4L2_PIX_FMT_RGB565:
        case V4L2_PIX_FMT_RGB565X:
          bpp = depth = 16;
          endianness =
              fourcc == V4L2_PIX_FMT_RGB565X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
          r_mask = 0xf800;
          g_mask = 0x07e0;
          b_mask = 0x001f;
          break;
        case V4L2_PIX_FMT_RGB24:
          bpp = depth = 24;
          endianness = G_BIG_ENDIAN;
          r_mask = 0xff0000;
          g_mask = 0x00ff00;
          b_mask = 0x0000ff;
          break;
        case V4L2_PIX_FMT_BGR24:
          bpp = depth = 24;
          endianness = G_BIG_ENDIAN;
          r_mask = 0x0000ff;
          g_mask = 0x00ff00;
          b_mask = 0xff0000;
          break;
        case V4L2_PIX_FMT_RGB32:
          bpp = depth = 32;
          endianness = G_BIG_ENDIAN;
          r_mask = 0xff000000;
          g_mask = 0x00ff0000;
          b_mask = 0x0000ff00;
          break;
        case V4L2_PIX_FMT_BGR32:
          bpp = depth = 32;
          endianness = G_BIG_ENDIAN;
          r_mask = 0x000000ff;
          g_mask = 0x0000ff00;
          b_mask = 0x00ff0000;
          break;
        default:
          g_assert_not_reached ();
          break;
      }
      structure = gst_structure_new ("video/x-raw-rgb",
          "bpp", G_TYPE_INT, bpp,
          "depth", G_TYPE_INT, depth,
          "red_mask", G_TYPE_INT, r_mask,
          "green_mask", G_TYPE_INT, g_mask,
          "blue_mask", G_TYPE_INT, b_mask,
          "endianness", G_TYPE_INT, endianness, NULL);
      break;
    }
    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
      structure = gst_structure_new ("video/x-raw-gray",
          "bpp", G_TYPE_INT, 8, NULL);
      break;
    case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
    case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
      /* FIXME: get correct fourccs here */
      break;
    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
    case V4L2_PIX_FMT_YVU410:
    case V4L2_PIX_FMT_YUV410:
    case V4L2_PIX_FMT_YUV420:  /* I420/IYUV */
    case V4L2_PIX_FMT_YUYV:
    case V4L2_PIX_FMT_YVU420:
    case V4L2_PIX_FMT_UYVY:
    case V4L2_PIX_FMT_Y41P:
    case V4L2_PIX_FMT_YUV422P:
#ifdef V4L2_PIX_FMT_YVYU
    case V4L2_PIX_FMT_YVYU:
#endif
    case V4L2_PIX_FMT_YUV411P:{
      guint32 fcc = 0;

      switch (fourcc) {
        case V4L2_PIX_FMT_NV12:
          fcc = GST_MAKE_FOURCC ('N', 'V', '1', '2');
          break;
        case V4L2_PIX_FMT_NV21:
          fcc = GST_MAKE_FOURCC ('N', 'V', '2', '1');
          break;
        case V4L2_PIX_FMT_YVU410:
          fcc = GST_MAKE_FOURCC ('Y', 'V', 'U', '9');
          break;
        case V4L2_PIX_FMT_YUV410:
          fcc = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
          break;
        case V4L2_PIX_FMT_YUV420:
          fcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
          break;
        case V4L2_PIX_FMT_YUYV:
          fcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
          break;
        case V4L2_PIX_FMT_YVU420:
          fcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
          break;
        case V4L2_PIX_FMT_UYVY:
          fcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
          break;
        case V4L2_PIX_FMT_Y41P:
          fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'P');
          break;
        case V4L2_PIX_FMT_YUV411P:
          fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
          break;
        case V4L2_PIX_FMT_YUV422P:
          fcc = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
          break;
#ifdef V4L2_PIX_FMT_YVYU
        case V4L2_PIX_FMT_YVYU:
          fcc = GST_MAKE_FOURCC ('Y', 'V', 'Y', 'U');
          break;
#endif
        default:
          g_assert_not_reached ();
          break;
      }
      structure = gst_structure_new ("video/x-raw-yuv",
          "format", GST_TYPE_FOURCC, fcc, NULL);
      break;
    }
    case V4L2_PIX_FMT_DV:
      structure =
          gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
          NULL);
      break;
    case V4L2_PIX_FMT_MPEG:    /* MPEG          */
1238
      structure = gst_structure_new ("video/mpegts", NULL);
Rob Clark's avatar
Rob Clark committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
      break;
    case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
      break;
#ifdef V4L2_PIX_FMT_SBGGR8
    case V4L2_PIX_FMT_SBGGR8:
      structure = gst_structure_new ("video/x-raw-bayer", NULL);
      break;
#endif
#ifdef V4L2_PIX_FMT_SN9C10X
    case V4L2_PIX_FMT_SN9C10X:
      structure = gst_structure_new ("video/x-sonix", NULL);
      break;
#endif
#ifdef V4L2_PIX_FMT_PWC1
    case V4L2_PIX_FMT_PWC1:
      structure = gst_structure_new ("video/x-pwc1", NULL);
      break;
#endif
#ifdef V4L2_PIX_FMT_PWC2
    case V4L2_PIX_FMT_PWC2:
      structure = gst_structure_new ("video/x-pwc2", NULL);
      break;
#endif
    default:
      GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
          fourcc, GST_FOURCC_ARGS (fourcc));
      break;
  }

  return structure;
}



GstCaps *
gst_v4l2_object_get_all_caps (void)
{
  static GstCaps *caps = NULL;

  if (caps == NULL) {
    GstStructure *structure;

    guint i;

    caps = gst_caps_new_empty ();
    for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
1285 1286
      structure =
          gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
Rob Clark's avatar
Rob Clark committed
1287
      if (structure) {
1288 1289 1290 1291 1292 1293
        if (gst_v4l2_formats[i].dimensions) {
          gst_structure_set (structure,
              "width", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
              "height", GST_TYPE_INT_RANGE, 1, GST_V4L2_MAX_SIZE,
              "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, 100, 1, NULL);
        }
Rob Clark's avatar
Rob Clark committed
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
        gst_caps_append_structure (caps, structure);
      }
    }
  }

  return gst_caps_ref (caps);
}


/* collect data for the given caps
 * @caps: given input caps
 * @format: location for the v4l format
 * @w/@h: location for width and height
 * @fps_n/@fps_d: location for framerate
 * @size: location for expected size of the frame or 0 if unknown
 */
gboolean