gstv4l2object.c 98.3 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
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
Rob Clark's avatar
Rob Clark committed
18
 * USA.
19 20
 */

21 22 23 24
/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
 * with newer GLib versions (>= 2.31.0) */
#define GLIB_DISABLE_DEPRECATION_WARNINGS

25 26 27 28 29 30 31 32 33 34
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

35 36 37 38
#ifdef HAVE_GUDEV
#include <gudev/gudev.h>
#endif

39 40
#include "v4l2_calls.h"
#include "gstv4l2tuner.h"
Rob Clark's avatar
Rob Clark committed
41
#ifdef HAVE_XVIDEO
42
#include "gstv4l2videooverlay.h"
43 44 45
#endif
#include "gstv4l2colorbalance.h"

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

48 49
#include <gst/video/video.h>

50 51 52 53 54 55 56
/* 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
57

58 59 60 61 62 63
#ifndef V4L2_PIX_FMT_NV12M
#define V4L2_PIX_FMT_NV12M GST_MAKE_FOURCC ('N', 'M', '1', '2')
#endif
#ifndef V4L2_PIX_FMT_NV21M
#define V4L2_PIX_FMT_NV21M GST_MAKE_FOURCC ('N', 'M', '2', '1')
#endif
64 65 66 67 68 69 70 71 72
#ifndef V4L2_PIX_FMT_MPEG1
#define V4L2_PIX_FMT_MPEG1 GST_MAKE_FOURCC ('M', 'P', 'G', '1')
#endif
#ifndef V4L2_PIX_FMT_MPEG2
#define V4L2_PIX_FMT_MPEG2 GST_MAKE_FOURCC ('M', 'P', 'G', '2')
#endif
#ifndef V4L2_PIX_FMT_MPEG4
#define V4L2_PIX_FMT_MPEG4 GST_MAKE_FOURCC ('M', 'P', 'G', '4')
#endif
73

Rob Clark's avatar
Rob Clark committed
74
GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
75
GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
Rob Clark's avatar
Rob Clark committed
76 77
#define GST_CAT_DEFAULT v4l2_debug

78
#define DEFAULT_PROP_DEVICE_NAME        NULL
79
#define DEFAULT_PROP_DEVICE_FD          -1
80
#define DEFAULT_PROP_FLAGS              0
Rob Clark's avatar
Rob Clark committed
81
#define DEFAULT_PROP_TV_NORM            0
82
#define DEFAULT_PROP_CHANNEL            NULL
83
#define DEFAULT_PROP_FREQUENCY          0
Wim Taymans's avatar
Wim Taymans committed
84
#define DEFAULT_PROP_IO_MODE            GST_V4L2_IO_AUTO
85

86 87
#define ENCODED_BUFFER_SIZE             (1 * 1024 * 1024)

Stefan Kost's avatar
Stefan Kost committed
88 89 90 91 92 93
enum
{
  PROP_0,
  V4L2_STD_OBJECT_PROPS,
};

94 95
static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);

96
#if 0
Wim Taymans's avatar
Wim Taymans committed
97 98
G_LOCK_DEFINE_STATIC (probe_lock);

Stefan Kost's avatar
Stefan Kost committed
99
const GList *
100 101 102 103 104
gst_v4l2_probe_get_properties (GstPropertyProbe * probe)
{
  GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
  static GList *list = NULL;

Wim Taymans's avatar
Wim Taymans committed
105
  G_LOCK (probe_lock);
106 107 108 109 110

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

Wim Taymans's avatar
Wim Taymans committed
111
  G_UNLOCK (probe_lock);
112 113 114 115

  return list;
}

116 117 118 119
static gboolean init = FALSE;
static GList *devices = NULL;

#ifdef HAVE_GUDEV
120
static gboolean
121
gst_v4l2_class_probe_devices_with_udev (GstElementClass * klass, gboolean check,
122 123
    GList ** klass_devices)
{
124
  GUdevClient *client = NULL;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  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;
172

173 174 175 176 177 178 179 180
  return init;
}
#endif /* HAVE_GUDEV */

static gboolean
gst_v4l2_class_probe_devices (GstElementClass * klass, gboolean check,
    GList ** klass_devices)
{
181
  if (!check) {
Stefan Kost's avatar
Stefan Kost committed
182
    const gchar *dev_base[] = { "/dev/video", "/dev/v4l2/video", NULL };
183 184 185
    gint base, n, fd;

    while (devices) {
186
      gchar *device = devices->data;
187
      devices = g_list_remove (devices, device);
188 189 190 191
      g_free (device);
    }

    /*
Rob Clark's avatar
Rob Clark committed
192
     * detect /dev entries
193 194 195 196 197 198 199 200 201
     */
    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
202
         * does the /dev/ entry exist at all?
203 204 205
         */
        if (stat (device, &s) == 0) {
          /*
Rob Clark's avatar
Rob Clark committed
206
           * yes: is a device attached?
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
           */
          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:
238 239 240 241
#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 */
242
      gst_v4l2_class_probe_devices (klass, FALSE, klass_devices);
243
#endif /* HAVE_GUDEV */
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      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:
260 261 262 263
#ifdef HAVE_GUDEV
      ret =
          !gst_v4l2_class_probe_devices_with_udev (klass, FALSE, klass_devices);
#else /* !HAVE_GUDEV */
264
      ret = !gst_v4l2_class_probe_devices (klass, TRUE, klass_devices);
265
#endif /* HAVE_GUDEV */
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      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;
}
318
#endif
319 320

#define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
321
static GType
322 323 324 325 326 327
gst_v4l2_device_get_type (void)
{
  static GType v4l2_device_type = 0;

  if (v4l2_device_type == 0) {
    static const GFlagsValue values[] = {
328 329 330 331 332 333 334 335 336
      {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"},
337 338 339 340 341 342 343 344 345 346 347

      {0, NULL, NULL}
    };

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

  return v4l2_device_type;
}

Rob Clark's avatar
Rob Clark committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
#define GST_TYPE_V4L2_TV_NORM (gst_v4l2_tv_norm_get_type ())
static GType
gst_v4l2_tv_norm_get_type (void)
{
  static GType v4l2_tv_norm = 0;

  if (!v4l2_tv_norm) {
    static const GEnumValue tv_norms[] = {
      {0, "none", "none"},

      {V4L2_STD_NTSC, "NTSC", "NTSC"},
      {V4L2_STD_NTSC_M, "NTSC-M", "NTSC-M"},
      {V4L2_STD_NTSC_M_JP, "NTSC-M-JP", "NTSC-M-JP"},
      {V4L2_STD_NTSC_M_KR, "NTSC-M-KR", "NTSC-M-KR"},
      {V4L2_STD_NTSC_443, "NTSC-443", "NTSC-443"},

      {V4L2_STD_PAL, "PAL", "PAL"},
      {V4L2_STD_PAL_BG, "PAL-BG", "PAL-BG"},
      {V4L2_STD_PAL_B, "PAL-B", "PAL-B"},
      {V4L2_STD_PAL_B1, "PAL-B1", "PAL-B1"},
      {V4L2_STD_PAL_G, "PAL-G", "PAL-G"},
      {V4L2_STD_PAL_H, "PAL-H", "PAL-H"},
      {V4L2_STD_PAL_I, "PAL-I", "PAL-I"},
      {V4L2_STD_PAL_DK, "PAL-DK", "PAL-DK"},
      {V4L2_STD_PAL_D, "PAL-D", "PAL-D"},
      {V4L2_STD_PAL_D1, "PAL-D1", "PAL-D1"},
      {V4L2_STD_PAL_K, "PAL-K", "PAL-K"},
      {V4L2_STD_PAL_M, "PAL-M", "PAL-M"},
      {V4L2_STD_PAL_N, "PAL-N", "PAL-N"},
      {V4L2_STD_PAL_Nc, "PAL-Nc", "PAL-Nc"},
      {V4L2_STD_PAL_60, "PAL-60", "PAL-60"},

      {V4L2_STD_SECAM, "SECAM", "SECAM"},
      {V4L2_STD_SECAM_B, "SECAM-B", "SECAM-B"},
      {V4L2_STD_SECAM_G, "SECAM-G", "SECAM-G"},
      {V4L2_STD_SECAM_H, "SECAM-H", "SECAM-H"},
      {V4L2_STD_SECAM_DK, "SECAM-DK", "SECAM-DK"},
      {V4L2_STD_SECAM_D, "SECAM-D", "SECAM-D"},
      {V4L2_STD_SECAM_K, "SECAM-K", "SECAM-K"},
      {V4L2_STD_SECAM_K1, "SECAM-K1", "SECAM-K1"},
      {V4L2_STD_SECAM_L, "SECAM-L", "SECAM-L"},
      {V4L2_STD_SECAM_LC, "SECAM-Lc", "SECAM-Lc"},

      {0, NULL, NULL}
    };

    v4l2_tv_norm = g_enum_register_static ("V4L2_TV_norms", tv_norms);
  }

  return v4l2_tv_norm;
}

400
GType
Wim Taymans's avatar
Wim Taymans committed
401 402 403 404 405 406 407 408 409 410
gst_v4l2_io_mode_get_type (void)
{
  static GType v4l2_io_mode = 0;

  if (!v4l2_io_mode) {
    static const GEnumValue io_modes[] = {
      {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
      {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
      {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
      {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
411
      {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
Wim Taymans's avatar
Wim Taymans committed
412 413 414 415 416 417 418 419

      {0, NULL, NULL}
    };
    v4l2_io_mode = g_enum_register_static ("GstV4l2IOMode", io_modes);
  }
  return v4l2_io_mode;
}

420
void
Rob Clark's avatar
Rob Clark committed
421 422
gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
    const char *default_device)
423
{
424 425
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device", "Device location",
426
          default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 428
  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
429 430
          "Name of the device", DEFAULT_PROP_DEVICE_NAME,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
431 432 433
  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,
434
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
435 436
  g_object_class_install_property (gobject_class, PROP_FLAGS,
      g_param_spec_flags ("flags", "Flags", "Device type flags",
437 438
          GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
439

440
  /**
441
   * GstV4l2Src:brightness:
442 443 444
   *
   * Picture brightness, or more precisely, the black level
   */
445 446 447 448 449
  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));
450
  /**
451
   * GstV4l2Src:contrast:
452 453 454
   *
   * Picture contrast or luma gain
   */
455 456 457 458 459
  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));
460
  /**
461
   * GstV4l2Src:saturation:
462 463 464
   *
   * Picture color saturation or chroma gain
   */
465 466 467 468 469
  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));
470
  /**
471
   * GstV4l2Src:hue:
472 473 474
   *
   * Hue or color balance
   */
475 476 477 478 479
  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));
Rob Clark's avatar
Rob Clark committed
480 481

  /**
482
   * GstV4l2Src:norm:
Rob Clark's avatar
Rob Clark committed
483 484 485 486 487 488 489 490
   *
   * TV norm
   */
  g_object_class_install_property (gobject_class, PROP_TV_NORM,
      g_param_spec_enum ("norm", "TV norm",
          "video standard",
          GST_TYPE_V4L2_TV_NORM, DEFAULT_PROP_TV_NORM,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
491 492

  /**
493
   * GstV4l2Src:io-mode:
Wim Taymans's avatar
Wim Taymans committed
494 495 496 497 498 499 500 501
   *
   * IO Mode
   */
  g_object_class_install_property (gobject_class, PROP_IO_MODE,
      g_param_spec_enum ("io-mode", "IO mode",
          "I/O mode",
          GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
502 503

  /**
504
   * GstV4l2Src:extra-controls:
505 506 507 508 509 510 511 512 513 514 515
   *
   * Additional v4l2 controls for the device. The controls are identified
   * by the control name (lowercase with '_' for any non-alphanumeric
   * characters).
   *
   * Since: 1.2
   */
  g_object_class_install_property (gobject_class, PROP_EXTRA_CONTROLS,
      g_param_spec_boxed ("extra-controls", "Extra Controls",
          "Extra v4l2 controls (CIDs) for the device",
          GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 517

  /**
518
   * GstV4l2Src:pixel-aspect-ratio:
519 520 521 522 523 524 525 526 527 528 529 530
   *
   * The pixel aspect ratio of the device. This overwrites the pixel aspect
   * ratio queried from the device.
   *
   * Since: 1.2
   */
  g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
      g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
          "Overwrite the pixel aspect ratio of the device", "1/1",
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
531
   * GstV4l2Src:force-aspect-ratio:
532 533 534 535 536 537 538 539 540 541 542
   *
   * When enabled, the pixel aspect ratio queried from the device or set
   * with the pixel-aspect-ratio property will be enforced.
   *
   * Since: 1.2
   */
  g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
      g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
          "When enabled, the pixel aspect ratio will be enforced", TRUE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

543 544 545
}

GstV4l2Object *
546
gst_v4l2_object_new (GstElement * element,
Rob Clark's avatar
Rob Clark committed
547
    enum v4l2_buf_type type,
548
    const char *default_device,
549
    GstV4l2GetInOutFunction get_in_out_func,
550 551
    GstV4l2SetInOutFunction set_in_out_func,
    GstV4l2UpdateFpsFunction update_fps_func)
552 553 554 555
{
  GstV4l2Object *v4l2object;

  /*
Rob Clark's avatar
Rob Clark committed
556
   * some default values
557 558 559
   */
  v4l2object = g_new0 (GstV4l2Object, 1);

Rob Clark's avatar
Rob Clark committed
560 561 562
  v4l2object->type = type;
  v4l2object->formats = NULL;

563 564 565
  v4l2object->element = element;
  v4l2object->get_in_out_func = get_in_out_func;
  v4l2object->set_in_out_func = set_in_out_func;
566
  v4l2object->update_fps_func = update_fps_func;
567 568

  v4l2object->video_fd = -1;
569
  v4l2object->poll = gst_poll_new (TRUE);
570
  v4l2object->active = FALSE;
Rob Clark's avatar
Rob Clark committed
571
  v4l2object->videodev = g_strdup (default_device);
572

573 574
  v4l2object->norms = NULL;
  v4l2object->channels = NULL;
575 576 577 578
  v4l2object->colors = NULL;

  v4l2object->xwindow_id = 0;

579 580
  v4l2object->keep_aspect = TRUE;

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
  v4l2object->n_v4l2_planes = 0;

  /*
   * this boolean only applies in v4l2-MPLANE mode.
   * TRUE: means it prefers to use several v4l2 (non contiguous)
   * planes. For example if the device supports NV12 and NV12M
   * both in MPLANE mode, then it will prefer NV12M
   * FALSE: means it prefers to use one v4l2 plane (which contains
   * all gst planes as if it was working in non-v4l2-MPLANE mode.
   * For example if the device supports NV12 and NV12M
   * both in MPLANE mode, then it will prefer NV12
   *
   * this boolean is also used to manage the case where the
   * device only supports the mode MPLANE and at the same time it
   * does not support both NV12 and NV12M. So in this case we first
   * try to use the prefered config, and at least try the other case
   * if it fails. For example in MPLANE mode if it has NV12 and not
   * NV21M then even if you set prefered_non_contiguous to TRUE it will
   * try NV21 as well.
   */
  v4l2object->prefered_non_contiguous = TRUE;

603 604
  v4l2object->no_initial_format = FALSE;

605 606 607
  return v4l2object;
}

Rob Clark's avatar
Rob Clark committed
608 609 610
static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);


611
void
612
gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
613
{
614
  g_return_if_fail (v4l2object != NULL);
615

616 617 618
  if (v4l2object->videodev)
    g_free (v4l2object->videodev);

619 620 621
  if (v4l2object->poll)
    gst_poll_free (v4l2object->poll);

622 623 624
  if (v4l2object->channel)
    g_free (v4l2object->channel);

Rob Clark's avatar
Rob Clark committed
625 626 627 628
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

629 630 631 632
  if (v4l2object->probed_caps) {
    gst_caps_unref (v4l2object->probed_caps);
  }

633
  g_free (v4l2object);
634 635
}

Rob Clark's avatar
Rob Clark committed
636 637 638 639 640 641 642 643 644 645 646

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;
}

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
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
671

672
gboolean
673
gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
674 675 676 677
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
Wim Taymans's avatar
Wim Taymans committed
678
      g_free (v4l2object->videodev);
679
      v4l2object->videodev = g_value_dup_string (value);
680
      break;
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
    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;
Rob Clark's avatar
Rob Clark committed
696 697
    case PROP_TV_NORM:
      v4l2object->tv_norm = g_value_get_enum (value);
698
      break;
Rob Clark's avatar
Rob Clark committed
699
#if 0
700
    case PROP_CHANNEL:
701 702 703
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
704
            (gchar *) g_value_get_string (value));
705 706 707 708 709 710 711

        if (channel) {
          /* like gst_tuner_set_channel (tuner, channel)
             without g_object_notify */
          gst_v4l2_tuner_set_channel (v4l2object, channel);
        }
      } else {
712 713
        g_free (v4l2object->channel);
        v4l2object->channel = g_value_dup_string (value);
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
      }
      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;
733
#endif
Wim Taymans's avatar
Wim Taymans committed
734 735 736
    case PROP_IO_MODE:
      v4l2object->req_mode = g_value_get_enum (value);
      break;
737 738 739 740 741 742 743 744 745 746 747
    case PROP_EXTRA_CONTROLS:{
      const GstStructure *s = gst_value_get_structure (value);

      if (v4l2object->extra_controls)
        gst_structure_free (v4l2object->extra_controls);

      v4l2object->extra_controls = s ? gst_structure_copy (s) : NULL;
      if (GST_V4L2_IS_OPEN (v4l2object))
        gst_v4l2_set_controls (v4l2object, v4l2object->extra_controls);
      break;
    }
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
    case PROP_PIXEL_ASPECT_RATIO:
      g_free (v4l2object->par);
      v4l2object->par = g_new0 (GValue, 1);
      g_value_init (v4l2object->par, GST_TYPE_FRACTION);
      if (!g_value_transform (value, v4l2object->par)) {
        g_warning ("Could not transform string to aspect ratio");
        gst_value_set_fraction (v4l2object->par, 1, 1);
      }
      GST_DEBUG_OBJECT (v4l2object->element, "set PAR to %d/%d",
          gst_value_get_fraction_numerator (v4l2object->par),
          gst_value_get_fraction_denominator (v4l2object->par));
      break;
    case PROP_FORCE_ASPECT_RATIO:
      v4l2object->keep_aspect = g_value_get_boolean (value);
      break;
763 764 765 766 767 768 769 770 771
    default:
      return FALSE;
      break;
  }
  return TRUE;
}


gboolean
772
gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
773 774 775 776 777 778 779 780
    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:
    {
781
      const guchar *new = NULL;
782

783
      if (GST_V4L2_IS_OPEN (v4l2object)) {
784
        new = v4l2object->vcap.card;
785
      } else if (gst_v4l2_open (v4l2object)) {
786
        new = v4l2object->vcap.card;
787 788
        gst_v4l2_close (v4l2object);
      }
789
      g_value_set_string (value, (gchar *) new);
790 791
      break;
    }
792 793 794 795 796 797 798 799
    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;
    }
800 801 802 803 804 805 806 807
    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
808 809 810
            V4L2_CAP_VIDEO_OVERLAY |
            V4L2_CAP_VBI_CAPTURE |
            V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
811 812 813 814 815 816

        if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
          flags |= V4L2_CAP_VIDEO_CAPTURE;

        if (v4l2object->vcap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
          flags |= V4L2_CAP_VIDEO_OUTPUT;
817 818 819 820
      }
      g_value_set_flags (value, flags);
      break;
    }
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    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;
Rob Clark's avatar
Rob Clark committed
839 840 841
    case PROP_TV_NORM:
      g_value_set_enum (value, v4l2object->tv_norm);
      break;
Wim Taymans's avatar
Wim Taymans committed
842 843 844
    case PROP_IO_MODE:
      g_value_set_enum (value, v4l2object->req_mode);
      break;
845 846 847
    case PROP_EXTRA_CONTROLS:
      gst_value_set_structure (value, v4l2object->extra_controls);
      break;
848 849 850 851 852 853 854
    case PROP_PIXEL_ASPECT_RATIO:
      if (v4l2object->par)
        g_value_transform (v4l2object->par, value);
      break;
    case PROP_FORCE_ASPECT_RATIO:
      g_value_set_boolean (value, v4l2object->keep_aspect);
      break;
855 856 857 858 859 860 861 862 863 864 865 866
    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
867 868 869 870 871 872
  GstTuner *tuner;

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

  tuner = GST_TUNER (v4l2object->element);
873

Rob Clark's avatar
Rob Clark committed
874 875
  if (v4l2object->tv_norm)
    norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
876 877
  GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
      "norm=%p", (guint64) v4l2object->tv_norm, norm);
878 879 880 881 882 883
  if (norm) {
    gst_tuner_set_norm (tuner, norm);
  } else {
    norm =
        GST_TUNER_NORM (gst_tuner_get_norm (GST_TUNER (v4l2object->element)));
    if (norm) {
Rob Clark's avatar
Rob Clark committed
884 885
      v4l2object->tv_norm =
          gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
886 887 888 889
      gst_tuner_norm_changed (tuner, norm);
    }
  }

890 891
  if (v4l2object->channel)
    channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
892 893 894 895
  if (channel) {
    gst_tuner_set_channel (tuner, channel);
  } else {
    channel =
896 897
        GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
            (v4l2object->element)));
898 899 900 901 902
    if (channel) {
      g_free (v4l2object->channel);
      v4l2object->channel = g_strdup (channel->label);
      gst_tuner_channel_changed (tuner, channel);
    }
903 904
  }

905 906
  if (channel
      && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
907 908 909 910 911 912 913 914 915 916 917 918 919 920
    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
921
gst_v4l2_object_open (GstV4l2Object * v4l2object)
922 923 924 925 926 927
{
  if (gst_v4l2_open (v4l2object))
    gst_v4l2_set_defaults (v4l2object);
  else
    return FALSE;

Rob Clark's avatar
Rob Clark committed
928
#ifdef HAVE_XVIDEO
929
  gst_v4l2_video_overlay_start (v4l2object);
930 931 932 933 934
#endif

  return TRUE;
}

935 936 937 938 939 940 941 942 943 944 945 946 947 948
gboolean
gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
{
  gboolean ret;

  ret = gst_v4l2_dup (v4l2object, other);

#ifdef HAVE_XVIDEO
  gst_v4l2_video_overlay_start (v4l2object);
#endif

  return ret;
}

949
gboolean
950
gst_v4l2_object_close (GstV4l2Object * v4l2object)
951
{
Rob Clark's avatar
Rob Clark committed
952
#ifdef HAVE_XVIDEO
953
  gst_v4l2_video_overlay_stop (v4l2object);
954 955 956 957 958
#endif

  if (!gst_v4l2_close (v4l2object))
    return FALSE;

959 960
  gst_caps_replace (&v4l2object->probed_caps, NULL);

961 962 963 964
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

965 966
  return TRUE;
}
Rob Clark's avatar
Rob Clark committed
967 968 969 970 971


/*
 * common format / caps utilities:
 */
972 973 974 975 976 977 978 979
typedef enum
{
  GST_V4L2_RAW = 1 << 0,
  GST_V4L2_CODEC = 1 << 1,
  GST_V4L2_TRANSPORT = 1 << 2,
  GST_V4L2_ALL = 0xffff
} GstV4L2FormatFlags;

980 981 982 983
typedef struct
{
  guint32 format;
  gboolean dimensions;
984
  GstV4L2FormatFlags flags;
985
} GstV4L2FormatDesc;
Rob Clark's avatar
Rob Clark committed
986

987
static const GstV4L2FormatDesc gst_v4l2_formats[] = {
Rob Clark's avatar
Rob Clark committed
988
  /* from Linux 2.6.15 videodev2.h */
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
Rob Clark's avatar
Rob Clark committed
1006 1007

  /* two planes -- one Y, one Cr + Cb interleaved  */
1008 1009 1010 1011
  {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
Rob Clark's avatar
Rob Clark committed
1012 1013

  /*  The following formats are not defined in the V4L2 specification */
1014 1015 1016 1017
  {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
Rob Clark's avatar
Rob Clark committed
1018 1019 1020

  /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
#ifdef V4L2_PIX_FMT_SBGGR8
1021
  {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
1022 1023 1024
#endif

  /* compressed formats */
1025 1026
  {V4L2_PIX_FMT_MJPEG, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_JPEG, TRUE, GST_V4L2_CODEC},
1027
#ifdef V4L2_PIX_FMT_PJPG
1028
  {V4L2_PIX_FMT_PJPG, TRUE, GST_V4L2_CODEC},
1029
#endif
1030 1031
  {V4L2_PIX_FMT_DV, TRUE, GST_V4L2_TRANSPORT},
  {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
1032 1033
  {V4L2_PIX_FMT_MPEG1, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_MPEG2, TRUE, GST_V4L2_CODEC},
1034
  {V4L2_PIX_FMT_MPEG4, TRUE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
1035

1036
#ifdef V4L2_PIX_FMT_H263
1037
  {V4L2_PIX_FMT_H263, TRUE, GST_V4L2_CODEC},
1038
#endif
1039
#ifdef V4L2_PIX_FMT_H264
1040
  {V4L2_PIX_FMT_H264, TRUE, GST_V4L2_CODEC},
1041
#endif
1042

Rob Clark's avatar
Rob Clark committed
1043
  /*  Vendor-specific formats   */
1044
  {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
1045 1046

#ifdef V4L2_PIX_FMT_SN9C10X
1047
  {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
1048 1049
#endif
#ifdef V4L2_PIX_FMT_PWC1
1050
  {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
1051 1052
#endif
#ifdef V4L2_PIX_FMT_PWC2
1053
  {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
1054 1055
#endif
#ifdef V4L2_PIX_FMT_YVYU
1056
  {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
Rob Clark's avatar
Rob Clark committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
#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 */
Thiago Santos's avatar
Thiago Santos committed
1079
    if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
        fmt->pixelformat == V4L2_PIX_FMT_JPEG
#ifdef V4L2_PIX_FMT_PJPG
        || fmt->pixelformat == V4L2_PIX_FMT_PJPG
#endif
        ) {
      if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG
#ifdef V4L2_PIX_FMT_PJPG
          || fourcc == V4L2_PIX_FMT_PJPG
#endif
          ) {
Thiago Santos's avatar
Thiago Santos committed
1090 1091
        return fmt;
      }
Rob Clark's avatar
Rob Clark committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    }
    walk = g_slist_next (walk);
  }

  return NULL;
}



/* complete made up ranking, the values themselves are meaningless */
1102 1103
/* These ranks MUST be X such that X<<15 fits on a signed int - see
   the comment at the end of gst_v4l2_object_format_get_rank. */
Rob Clark's avatar
Rob Clark committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
#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

1115 1116 1117 1118 1119 1120 1121
/* 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
1122
static gint
1123
gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
Rob Clark's avatar
Rob Clark committed
1124
{
1125
  guint32 fourcc = fmt->pixelformat;
1126
  gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
1127 1128
  gint rank = 0;

Rob Clark's avatar
Rob Clark committed
1129 1130
  switch (fourcc) {
    case V4L2_PIX_FMT_MJPEG:
1131
#ifdef V4L2_PIX_FMT_PJPG
Thiago Santos's avatar
Thiago Santos committed
1132
    case V4L2_PIX_FMT_PJPG:
1133 1134
      rank = JPEG_BASE_RANK;
      break;
1135
#endif
Rob Clark's avatar
Rob Clark committed
1136
    case V4L2_PIX_FMT_JPEG:
1137 1138
      rank = JPEG_BASE_RANK + 1;
      break;
1139 1140 1141
    case V4L2_PIX_FMT_MPEG:    /* MPEG          */
      rank = JPEG_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
1142 1143 1144 1145 1146 1147

    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:
1148 1149
      rank = RGB_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1150 1151 1152

    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
1153 1154
      rank = RGB_BASE_RANK - 1;
      break;
Rob Clark's avatar
Rob Clark committed
1155 1156 1157

    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_BGR32:
1158 1159
      rank = RGB_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1160 1161

    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
1162 1163
      rank = GREY_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1164 1165

    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
1166
    case V4L2_PIX_FMT_NV12M:   /* Same as NV12      */
Rob Clark's avatar
Rob Clark committed
1167
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
1168
    case V4L2_PIX_FMT_NV21M:   /* Same as NV21      */
Rob Clark's avatar
Rob Clark committed
1169 1170
    case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
    case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
1171 1172
      rank = YUV_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed