gstv4l2object.c 92 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
41
42
#include "v4l2_calls.h"
#include "gstv4l2tuner.h"
#include "gstv4l2colorbalance.h"

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

45
46
#include <gst/video/video.h>

Rob Clark's avatar
Rob Clark committed
47
GST_DEBUG_CATEGORY_EXTERN (v4l2_debug);
48
GST_DEBUG_CATEGORY_EXTERN (GST_CAT_PERFORMANCE);
Rob Clark's avatar
Rob Clark committed
49
50
#define GST_CAT_DEFAULT v4l2_debug

51
#define DEFAULT_PROP_DEVICE_NAME        NULL
52
#define DEFAULT_PROP_DEVICE_FD          -1
53
#define DEFAULT_PROP_FLAGS              0
Rob Clark's avatar
Rob Clark committed
54
#define DEFAULT_PROP_TV_NORM            0
55
#define DEFAULT_PROP_CHANNEL            NULL
56
#define DEFAULT_PROP_FREQUENCY          0
Wim Taymans's avatar
Wim Taymans committed
57
#define DEFAULT_PROP_IO_MODE            GST_V4L2_IO_AUTO
58

59
60
#define ENCODED_BUFFER_SIZE             (1 * 1024 * 1024)

Stefan Kost's avatar
Stefan Kost committed
61
62
63
64
65
66
enum
{
  PROP_0,
  V4L2_STD_OBJECT_PROPS,
};

67
68
static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);

69
70

#define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
71
static GType
72
73
74
75
76
77
gst_v4l2_device_get_type (void)
{
  static GType v4l2_device_type = 0;

  if (v4l2_device_type == 0) {
    static const GFlagsValue values[] = {
78
79
80
81
82
83
84
85
86
      {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"},
87
88
89
90
91
92
93
94
95
96
97

      {0, NULL, NULL}
    };

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

  return v4l2_device_type;
}

Rob Clark's avatar
Rob Clark committed
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
144
145
146
147
148
149
#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;
}

150
GType
Wim Taymans's avatar
Wim Taymans committed
151
152
153
154
155
156
157
158
159
160
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"},
161
      {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
Wim Taymans's avatar
Wim Taymans committed
162
163
164
165
166
167
168
169

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

170
void
Rob Clark's avatar
Rob Clark committed
171
172
gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
    const char *default_device)
173
{
174
175
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device", "Device location",
176
          default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177
178
  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
179
180
          "Name of the device", DEFAULT_PROP_DEVICE_NAME,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
181
182
183
  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,
184
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
185
186
  g_object_class_install_property (gobject_class, PROP_FLAGS,
      g_param_spec_flags ("flags", "Flags", "Device type flags",
187
188
          GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
189

190
  /**
191
   * GstV4l2Src:brightness:
192
193
194
   *
   * Picture brightness, or more precisely, the black level
   */
195
196
197
198
199
  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));
200
  /**
201
   * GstV4l2Src:contrast:
202
203
204
   *
   * Picture contrast or luma gain
   */
205
206
207
208
209
  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));
210
  /**
211
   * GstV4l2Src:saturation:
212
213
214
   *
   * Picture color saturation or chroma gain
   */
215
216
217
218
219
  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));
220
  /**
221
   * GstV4l2Src:hue:
222
223
224
   *
   * Hue or color balance
   */
225
226
227
228
229
  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
230
231

  /**
232
   * GstV4l2Src:norm:
Rob Clark's avatar
Rob Clark committed
233
234
235
236
237
238
239
240
   *
   * 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
241
242

  /**
243
   * GstV4l2Src:io-mode:
Wim Taymans's avatar
Wim Taymans committed
244
245
246
247
248
249
250
251
   *
   * 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));
252
253

  /**
254
   * GstV4l2Src:extra-controls:
255
256
257
258
259
260
261
262
263
264
265
   *
   * 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));
266
267

  /**
268
   * GstV4l2Src:pixel-aspect-ratio:
269
270
271
272
273
274
275
276
277
278
279
280
   *
   * 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));

  /**
281
   * GstV4l2Src:force-aspect-ratio:
282
283
284
285
286
287
288
289
290
291
292
   *
   * 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));

293
294
295
}

GstV4l2Object *
296
gst_v4l2_object_new (GstElement * element,
Rob Clark's avatar
Rob Clark committed
297
    enum v4l2_buf_type type,
298
    const char *default_device,
299
    GstV4l2GetInOutFunction get_in_out_func,
300
301
    GstV4l2SetInOutFunction set_in_out_func,
    GstV4l2UpdateFpsFunction update_fps_func)
302
303
304
305
{
  GstV4l2Object *v4l2object;

  /*
Rob Clark's avatar
Rob Clark committed
306
   * some default values
307
308
309
   */
  v4l2object = g_new0 (GstV4l2Object, 1);

Rob Clark's avatar
Rob Clark committed
310
311
312
  v4l2object->type = type;
  v4l2object->formats = NULL;

313
314
315
  v4l2object->element = element;
  v4l2object->get_in_out_func = get_in_out_func;
  v4l2object->set_in_out_func = set_in_out_func;
316
  v4l2object->update_fps_func = update_fps_func;
317
318

  v4l2object->video_fd = -1;
319
  v4l2object->poll = gst_poll_new (TRUE);
320
  v4l2object->active = FALSE;
Rob Clark's avatar
Rob Clark committed
321
  v4l2object->videodev = g_strdup (default_device);
322

323
324
  v4l2object->norms = NULL;
  v4l2object->channels = NULL;
325
326
327
328
  v4l2object->colors = NULL;

  v4l2object->xwindow_id = 0;

329
330
  v4l2object->keep_aspect = TRUE;

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  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;

353
354
  v4l2object->no_initial_format = FALSE;

355
356
357
  return v4l2object;
}

Rob Clark's avatar
Rob Clark committed
358
359
360
static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);


361
void
362
gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
363
{
364
  g_return_if_fail (v4l2object != NULL);
365

366
367
368
  if (v4l2object->videodev)
    g_free (v4l2object->videodev);

369
370
371
  if (v4l2object->poll)
    gst_poll_free (v4l2object->poll);

372
373
374
  if (v4l2object->channel)
    g_free (v4l2object->channel);

Rob Clark's avatar
Rob Clark committed
375
376
377
378
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

379
380
381
382
  if (v4l2object->probed_caps) {
    gst_caps_unref (v4l2object->probed_caps);
  }

383
  g_free (v4l2object);
384
385
}

Rob Clark's avatar
Rob Clark committed
386
387
388
389
390
391
392
393
394
395
396

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

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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
421

422
gboolean
423
gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
424
425
426
427
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
Wim Taymans's avatar
Wim Taymans committed
428
      g_free (v4l2object->videodev);
429
      v4l2object->videodev = g_value_dup_string (value);
430
      break;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
    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
446
447
    case PROP_TV_NORM:
      v4l2object->tv_norm = g_value_get_enum (value);
448
      break;
Rob Clark's avatar
Rob Clark committed
449
#if 0
450
    case PROP_CHANNEL:
451
452
453
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
454
            (gchar *) g_value_get_string (value));
455
456
457
458
459
460
461

        if (channel) {
          /* like gst_tuner_set_channel (tuner, channel)
             without g_object_notify */
          gst_v4l2_tuner_set_channel (v4l2object, channel);
        }
      } else {
462
463
        g_free (v4l2object->channel);
        v4l2object->channel = g_value_dup_string (value);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
      }
      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;
483
#endif
Wim Taymans's avatar
Wim Taymans committed
484
485
486
    case PROP_IO_MODE:
      v4l2object->req_mode = g_value_get_enum (value);
      break;
487
488
489
490
491
492
493
494
495
496
497
    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;
    }
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
    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;
513
514
515
516
517
518
519
520
521
    default:
      return FALSE;
      break;
  }
  return TRUE;
}


gboolean
522
gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
523
524
525
526
527
528
529
530
    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:
    {
531
      const guchar *new = NULL;
532

533
      if (GST_V4L2_IS_OPEN (v4l2object)) {
534
        new = v4l2object->vcap.card;
535
      } else if (gst_v4l2_open (v4l2object)) {
536
        new = v4l2object->vcap.card;
537
538
        gst_v4l2_close (v4l2object);
      }
539
      g_value_set_string (value, (gchar *) new);
540
541
      break;
    }
542
543
544
545
546
547
548
549
    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;
    }
550
551
552
553
554
555
556
557
    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
558
559
560
            V4L2_CAP_VIDEO_OVERLAY |
            V4L2_CAP_VBI_CAPTURE |
            V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
561
562
563
564
565
566

        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;
567
568
569
570
      }
      g_value_set_flags (value, flags);
      break;
    }
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
    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
589
590
591
    case PROP_TV_NORM:
      g_value_set_enum (value, v4l2object->tv_norm);
      break;
Wim Taymans's avatar
Wim Taymans committed
592
593
594
    case PROP_IO_MODE:
      g_value_set_enum (value, v4l2object->req_mode);
      break;
595
596
597
    case PROP_EXTRA_CONTROLS:
      gst_value_set_structure (value, v4l2object->extra_controls);
      break;
598
599
600
601
602
603
604
    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;
605
606
607
608
609
610
611
612
613
614
615
616
    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
617
618
619
620
621
622
  GstTuner *tuner;

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

  tuner = GST_TUNER (v4l2object->element);
623

Rob Clark's avatar
Rob Clark committed
624
625
  if (v4l2object->tv_norm)
    norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
626
627
  GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
      "norm=%p", (guint64) v4l2object->tv_norm, norm);
628
629
630
631
632
633
  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
634
635
      v4l2object->tv_norm =
          gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
636
637
638
639
      gst_tuner_norm_changed (tuner, norm);
    }
  }

640
641
  if (v4l2object->channel)
    channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
642
643
644
645
  if (channel) {
    gst_tuner_set_channel (tuner, channel);
  } else {
    channel =
646
647
        GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
            (v4l2object->element)));
648
649
650
651
652
    if (channel) {
      g_free (v4l2object->channel);
      v4l2object->channel = g_strdup (channel->label);
      gst_tuner_channel_changed (tuner, channel);
    }
653
654
  }

655
656
  if (channel
      && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
657
658
659
660
661
662
663
664
665
666
667
668
669
670
    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
671
gst_v4l2_object_open (GstV4l2Object * v4l2object)
672
673
674
675
676
677
678
679
680
{
  if (gst_v4l2_open (v4l2object))
    gst_v4l2_set_defaults (v4l2object);
  else
    return FALSE;

  return TRUE;
}

681
682
683
684
685
686
687
688
689
690
gboolean
gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
{
  gboolean ret;

  ret = gst_v4l2_dup (v4l2object, other);

  return ret;
}

691
gboolean
692
gst_v4l2_object_close (GstV4l2Object * v4l2object)
693
694
695
696
{
  if (!gst_v4l2_close (v4l2object))
    return FALSE;

697
698
  gst_caps_replace (&v4l2object->probed_caps, NULL);

699
700
701
702
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

703
704
  return TRUE;
}
Rob Clark's avatar
Rob Clark committed
705
706
707
708
709


/*
 * common format / caps utilities:
 */
710
711
712
713
714
typedef enum
{
  GST_V4L2_RAW = 1 << 0,
  GST_V4L2_CODEC = 1 << 1,
  GST_V4L2_TRANSPORT = 1 << 2,
715
  GST_V4L2_NO_PARSE = 1 << 3,
716
717
718
  GST_V4L2_ALL = 0xffff
} GstV4L2FormatFlags;

719
720
721
722
typedef struct
{
  guint32 format;
  gboolean dimensions;
723
  GstV4L2FormatFlags flags;
724
} GstV4L2FormatDesc;
Rob Clark's avatar
Rob Clark committed
725

726
static const GstV4L2FormatDesc gst_v4l2_formats[] = {
Rob Clark's avatar
Rob Clark committed
727
  /* from Linux 2.6.15 videodev2.h */
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
  {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
745
746

  /* two planes -- one Y, one Cr + Cb interleaved  */
747
748
  {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
749
  {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
750
751
  {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
Rob Clark's avatar
Rob Clark committed
752
753

  /*  The following formats are not defined in the V4L2 specification */
754
755
756
757
  {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
758
759

  /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
760
  {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
761
762

  /* compressed formats */
763
764
765
766
  {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
767
  {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
768
769
770
  {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
Rob Clark's avatar
Rob Clark committed
771

772
773
  {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
774
775
  /* VP8 not parseable */
  {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
776

Rob Clark's avatar
Rob Clark committed
777
  /*  Vendor-specific formats   */
778
779
780
781
782
  {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
Rob Clark's avatar
Rob Clark committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
};

#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
804
    if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
805
806
807
808
        fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
        fmt->pixelformat == V4L2_PIX_FMT_PJPG) {
      if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
          fourcc == V4L2_PIX_FMT_PJPG) {
Thiago Santos's avatar
Thiago Santos committed
809
810
        return fmt;
      }
Rob Clark's avatar
Rob Clark committed
811
812
813
814
815
816
817
818
819
820
    }
    walk = g_slist_next (walk);
  }

  return NULL;
}



/* complete made up ranking, the values themselves are meaningless */
821
822
/* 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
823
824
825
826
827
828
829
830
831
832
833
834
#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

static gint
835
gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
Rob Clark's avatar
Rob Clark committed
836
{
837
  guint32 fourcc = fmt->pixelformat;
838
  gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
839
840
  gint rank = 0;

Rob Clark's avatar
Rob Clark committed
841
842
  switch (fourcc) {
    case V4L2_PIX_FMT_MJPEG:
Thiago Santos's avatar
Thiago Santos committed
843
    case V4L2_PIX_FMT_PJPG:
844
845
      rank = JPEG_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
846
    case V4L2_PIX_FMT_JPEG:
847
848
      rank = JPEG_BASE_RANK + 1;
      break;
849
850
851
    case V4L2_PIX_FMT_MPEG:    /* MPEG          */
      rank = JPEG_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
852
853
854
855
856
857

    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:
858
859
      rank = RGB_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
860
861
862

    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
863
864
      rank = RGB_BASE_RANK - 1;
      break;
Rob Clark's avatar
Rob Clark committed
865
866
867

    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_BGR32:
868
869
      rank = RGB_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
870
871

    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
872
873
      rank = GREY_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
874
875

    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
876
    case V4L2_PIX_FMT_NV12M:   /* Same as NV12      */
877
    case V4L2_PIX_FMT_NV12MT:  /* NV12 64x32 tile   */
Rob Clark's avatar
Rob Clark committed
878
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
879
    case V4L2_PIX_FMT_NV21M:   /* Same as NV21      */
Rob Clark's avatar
Rob Clark committed
880
881
    case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
    case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
882
883
      rank = YUV_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
884
885

    case V4L2_PIX_FMT_YVU410:  /* YVU9,  9 bits per pixel */
886
887
      rank = YUV_BASE_RANK + 3;
      break;
Rob Clark's avatar
Rob Clark committed
888
    case V4L2_PIX_FMT_YUV410:  /* YUV9,  9 bits per pixel */
889
890
      rank = YUV_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
891
    case V4L2_PIX_FMT_YUV420:  /* I420, 12 bits per pixel */
892
893
      rank = YUV_BASE_RANK + 7;
      break;
Rob Clark's avatar
Rob Clark committed
894
    case V4L2_PIX_FMT_YUYV:    /* YUY2, 16 bits per pixel */
895
896
      rank = YUV_BASE_RANK + 10;
      break;
Rob Clark's avatar
Rob Clark committed
897
    case V4L2_PIX_FMT_YVU420:  /* YV12, 12 bits per pixel */
898
899
      rank = YUV_BASE_RANK + 6;
      break;
Rob Clark's avatar
Rob Clark committed
900
    case V4L2_PIX_FMT_UYVY:    /* UYVY, 16 bits per pixel */
901
902
      rank = YUV_BASE_RANK + 9;
      break;
Rob Clark's avatar
Rob Clark committed
903
    case V4L2_PIX_FMT_Y41P:    /* Y41P, 12 bits per pixel */
904
905
      rank = YUV_BASE_RANK + 5;
      break;
Rob Clark's avatar
Rob Clark committed
906
    case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
907
908
      rank = YUV_BASE_RANK + 4;
      break;
Rob Clark's avatar
Rob Clark committed
909
    case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
910
911
      rank = YUV_BASE_RANK + 8;
      break;
Rob Clark's avatar
Rob Clark committed
912
913

    case V4L2_PIX_FMT_DV:
914
915
      rank = DV_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
916
917

    case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
918
919
      rank = 0;
      break;
Rob Clark's avatar
Rob Clark committed
920
921

    case V4L2_PIX_FMT_SBGGR8:
922
923
      rank = BAYER_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
924
925

    case V4L2_PIX_FMT_SN9C10X:
926
927
      rank = S910_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
928
929

    case V4L2_PIX_FMT_PWC1:
930
931
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
932
    case V4L2_PIX_FMT_PWC2:
933
934
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
935
936

    default:
937
      rank = 0;
Rob Clark's avatar
Rob Clark committed
938
939
940
      break;
  }

941
942
943
944
945
946
947
948
  /* 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
949
950
951
952
953
954
955
}



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

959
  if (fa->pixelformat == fb->pixelformat)
Rob Clark's avatar
Rob Clark committed
960
961
    return 0;

962
963
  return gst_v4l2_object_format_get_rank (fb) -
      gst_v4l2_object_format_get_rank (fa);
Rob Clark's avatar
Rob Clark committed
964
965
966
967
968
969
970
971
}

/******************************************************
 * gst_v4l2_object_fill_format_list():
 *   create list of supported capture formats
 * return value: TRUE on success, FALSE on error
 ******************************************************/
static gboolean
972
973
gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
    enum v4l2_buf_type type)
Rob Clark's avatar
Rob Clark committed
974
975
976
977
978
979
980
981
982
983
984
{
  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;
985
    format->type = type;
Rob Clark's avatar
Rob Clark committed
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

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

1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
#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
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037

  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
1038
/*
1039
1040
1041
  * Get the list of supported capture formats, a list of
  * <code>struct v4l2_fmtdesc</code>.
  */
1042
static GSList *
Rob Clark's avatar
Rob Clark committed
1043
1044
gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
{
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
  if (!v4l2object->formats) {

    /* check usual way */
    gst_v4l2_object_fill_format_list (v4l2object, v4l2object->type);

    /* if our driver supports multi-planar
     * and if formats are still empty then we can workaround driver bug
     * by also looking up formats as if our device was not supporting
     * multiplanar */
    if (!v4l2object->formats) {
      switch (v4l2object->type) {
        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
          gst_v4l2_object_fill_format_list (v4l2object,
              V4L2_BUF_TYPE_VIDEO_CAPTURE);
          break;

        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
          gst_v4l2_object_fill_format_list (v4l2object,
              V4L2_BUF_TYPE_VIDEO_OUTPUT);
          break;

        default:
          break;
      }
    }
  }
Rob Clark's avatar
Rob Clark committed
1071
1072
1073
  return v4l2object->formats;
}

1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
static GstVideoFormat
gst_v4l2_object_v4l2fourcc_to_video_format (guint32 fourcc)
{
  GstVideoFormat format;

  switch (fourcc) {
    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
      format = GST_VIDEO_FORMAT_GRAY8;
      break;
    case V4L2_PIX_FMT_RGB555:
      format = GST_VIDEO_FORMAT_RGB15;
      break;
    case V4L2_PIX_FMT_RGB565:
      format = GST_VIDEO_FORMAT_RGB16;
      break;
    case V4L2_PIX_FMT_RGB24:
      format = GST_VIDEO_FORMAT_RGB;
      break;
    case V4L2_PIX_FMT_BGR24:
      format = GST_VIDEO_FORMAT_BGR;
      break;
    case V4L2_PIX_FMT_RGB32:
1096
      format = GST_VIDEO_FORMAT_xRGB;
1097
1098
1099
1100
1101
1102
1103
1104
      break;
    case V4L2_PIX_FMT_BGR32:
      format = GST_VIDEO_FORMAT_BGRx;
      break;
    case V4L2_PIX_FMT_NV12:
    case V4L2_PIX_FMT_NV12M:
      format = GST_VIDEO_FORMAT_NV12;
      break;
1105
1106
1107
    case V4L2_PIX_FMT_NV12MT:
      format = GST_VIDEO_FORMAT_NV12_64Z32;
      break;
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
    case V4L2_PIX_FMT_NV21:
    case V4L2_PIX_FMT_NV21M:
      format = GST_VIDEO_FORMAT_NV21;
      break;
    case V4L2_PIX_FMT_YVU410:
      format = GST_VIDEO_FORMAT_YVU9;
      break;
    case V4L2_PIX_FMT_YUV410:
      format = GST_VIDEO_FORMAT_YUV9;
      break;
    case V4L2_PIX_FMT_YUV420:
      format = GST_VIDEO_FORMAT_I420;
      break;
    case V4L2_PIX_FMT_YUYV:
      format = GST_VIDEO_FORMAT_YUY2;
      break;
    case V4L2_PIX_FMT_YVU420:
      format = GST_VIDEO_FORMAT_YV12;
      break;
    case V4L2_PIX_FMT_UYVY:
      format = GST_VIDEO_FORMAT_UYVY;
      break;
#if 0
    case V4L2_PIX_FMT_Y41P:
      format = GST_VIDEO_FORMAT_Y41P;
      break;
#endif
    case V4L2_PIX_FMT_YUV411P:
      format = GST_VIDEO_FORMAT_Y41B;
      break;
    case V4L2_PIX_FMT_YUV422P:
      format = GST_VIDEO_FORMAT_Y42B;
      break;
    case V4L2_PIX_FMT_YVYU:
      format = GST_VIDEO_FORMAT_YVYU;
      break;
    default:
      format = GST_VIDEO_FORMAT_UNKNOWN;
      g_assert_not_reached ();
      break;
  }

  return format;
}
Rob Clark's avatar
Rob Clark committed
1152

1153
GstStructure *
Rob Clark's avatar
Rob Clark committed
1154
1155
1156
1157
1158
1159
gst_v4l2_object_v4l2fourcc_to_structure (guint32 fourcc)
{
  GstStructure *structure = NULL;

  switch (fourcc) {
    case V4L2_PIX_FMT_MJPEG:   /* Motion-JPEG */
Thiago Santos's avatar
Thiago Santos committed
1160
    case V4L2_PIX_FMT_PJPG:    /* Progressive-JPEG */
Rob Clark's avatar
Rob Clark committed
1161
    case V4L2_PIX_FMT_JPEG:    /* JFIF JPEG */
Wim Taymans's avatar
Wim Taymans committed
1162
      structure = gst_structure_new_empty ("image/jpeg");
Rob Clark's avatar
Rob Clark committed
1163
1164
1165
1166
1167
      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;
1168
1169
1170
1171
1172
1173
1174
1175
    case V4L2_PIX_FMT_MPEG1:
      structure = gst_structure_new ("video/mpeg",
          "mpegversion", G_TYPE_INT, 2, NULL);
      break;
    case V4L2_PIX_FMT_MPEG2:
      structure = gst_structure_new ("video/mpeg",
          "mpegversion", G_TYPE_INT, 2, NULL);
      break;
1176
1177
1178
1179
1180
1181
1182
1183
1184
    case V4L2_PIX_FMT_MPEG4:
      structure = gst_structure_new ("video/mpeg",
          "mpegversion", G_TYPE_INT, 4, "systemstream",
          G_TYPE_BOOLEAN, FALSE, NULL);
      break;
    case V4L2_PIX_FMT_H263:
      structure = gst_structure_new ("video/x-h263",
          "variant", G_TYPE_STRING, "itu", NULL);
      break;
1185
    case V4L2_PIX_FMT_H264:    /* H.264 */
Nicolas Dufresne's avatar
Nicolas Dufresne committed
1186
1187
1188
      structure = gst_structure_new ("video/x-h264",
          "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
          G_TYPE_STRING, "au", NULL);
1189
      break;
1190
1191
1192
    case V4L2_PIX_FMT_VP8:
      structure = gst_structure_new_empty ("video/x-vp8");
      break;
1193
1194
1195
1196
1197
    case V4L2_PIX_FMT_RGB332:
    case V4L2_PIX_FMT_RGB555X:
    case V4L2_PIX_FMT_RGB565X:
      /* FIXME: get correct fourccs here */
      break;
1198
    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
1199
1200
1201
1202
1203
1204
    case V4L2_PIX_FMT_RGB555:
    case V4L2_PIX_FMT_RGB565:
    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_BGR32:
Rob Clark's avatar
Rob Clark committed
1205
    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
1206
    case V4L2_PIX_FMT_NV12M:
1207
    case V4L2_PIX_FMT_NV12MT:
Rob Clark's avatar
Rob Clark committed
1208
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
1209
    case V4L2_PIX_FMT_NV21M:
Rob Clark's avatar
Rob Clark committed
1210
1211
1212
1213
1214
1215
    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:
1216
#if 0
Rob Clark's avatar
Rob Clark committed
1217
    case V4L2_PIX_FMT_Y41P:
1218
#endif
Rob Clark's avatar
Rob Clark committed
1219
1220
1221
    case V4L2_PIX_FMT_YUV422P:
    case V4L2_PIX_FMT_YVYU:
    case V4L2_PIX_FMT_YUV411P:{
1222
      GstVideoFormat format;
1223
      format = gst_v4l2_object_v4l2fourcc_to_video_format (fourcc);
1224
1225
1226
      if (format != GST_VIDEO_FORMAT_UNKNOWN)
        structure = gst_structure_new ("video/x-raw",
            "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
Rob Clark's avatar
Rob Clark committed
1227
1228
1229
1230
1231
1232
1233
1234
      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          */
1235
1236
      structure = gst_structure_new ("video/mpegts",
          "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
Rob Clark's avatar
Rob Clark committed
1237
1238
1239
1240
      break;
    case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
      break;
    case V4L2_PIX_FMT_SBGGR8:
Wim Taymans's avatar
Wim Taymans committed
1241
      structure = gst_structure_new_empty ("video/x-bayer");
Rob Clark's avatar
Rob Clark committed
1242
1243
      break;
    case V4L2_PIX_FMT_SN9C10X:
Wim Taymans's avatar
Wim Taymans committed
1244
      structure = gst_structure_new_empty ("video/x-sonix");
Rob Clark's avatar
Rob Clark committed
1245
1246
      break;
    case V4L2_PIX_FMT_PWC1:
Wim Taymans's avatar
Wim Taymans committed
1247
      structure = gst_structure_new_empty ("video/x-pwc1");
Rob Clark's avatar
Rob Clark committed
1248
1249
      break;
    case V4L2_PIX_FMT_PWC2:
Wim Taymans's avatar
Wim Taymans committed
1250
      structure = gst_structure_new_empty ("video/x-pwc2");
Rob Clark's avatar
Rob Clark committed
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
      break;
    default:
      GST_DEBUG ("Unknown fourcc 0x%08x %" GST_FOURCC_FORMAT,
          fourcc, GST_FOURCC_ARGS (fourcc));
      break;
  }

  return structure;
}


1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
static GstCaps *
gst_v4l2_object_get_caps_helper (GstV4L2FormatFlags flags)
{
  GstStructure *structure;
  GstCaps *caps;
  guint i;

  caps = gst_caps_new_empty ();
  for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {

    if ((gst_v4l2_formats[i].flags & flags) == 0)
      continue;

    structure =
        gst_v4l2_object_v4l2fourcc_to_structure (gst_v4l2_formats[i].format);
    if (structure) {
      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);
      }
      gst_caps_append_structure (caps, structure);
    }
  }

  return gst_caps_simplify (caps);
}
Rob Clark's avatar
Rob Clark committed
1290
1291
1292
1293
1294
1295

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

1296
1297
  if (caps == NULL)
    caps = gst_v4l2_object_get_caps_helper (GST_V4L2_ALL);
Rob Clark's avatar
Rob Clark committed
1298

1299
1300
  return gst_caps_ref (caps);
}
Rob Clark's avatar
Rob Clark committed
1301

1302
1303
1304
1305
1306
1307
1308
GstCaps *
gst_v4l2_object_get_raw_caps (void)
{
  static GstCaps *caps = NULL;

  if (caps == NULL)
    caps = gst_v4l2_object_get_caps_helper (GST_V4L2_RAW);
Rob Clark's avatar
Rob Clark committed
1309
1310
1311
1312

  return gst_caps_ref (caps);
}

1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
GstCaps *
gst_v4l2_object_get_codec_caps (void)
{
  static GstCaps *caps = NULL;

  if (caps == NULL)
    caps = gst_v4l2_object_get_caps_helper (GST_V4L2_CODEC);

  return gst_caps_ref (caps);
}
Rob Clark's avatar
Rob Clark committed
1323

1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
/* gst_v4l2_object_choose_fourcc:
 * @obj a #GstV4l2Object
 * @fourcc_splane The format type in single plane representation
 * @fourcc_mplane The format type in multi-plane representation
 * @fourcc Set to the first format to try
 * @fourcc_alt The alternative format to use, or zero if mplane is not
 * supported. Note that if alternate is used, the prefered_non_contiguous
 * setting need to be inversed.
 *
 * Certain format can be stored into multi-planar buffer type with two
 * representation. As an example, NV12, which has two planes, can be stored
 * into 1 plane of multi-planar buffer sturcture, or two. This function will
 * choose the right format to use base on the object settings.
 */
static void
gst_v4l2_object_choose_fourcc (GstV4l2Object * obj, guint32 fourcc_splane,
    guint32 fourcc_mplane, guint32 * fourcc, guint32 * fourcc_alt)
1341
{
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
  if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) {
    if (obj->prefered_non_contiguous) {
      *fourcc = fourcc_mplane;
      *fourcc_alt = fourcc_splane;
    } else {
      *fourcc = fourcc_splane;
      *fourcc_alt = fourcc_mplane;
    }
  } else {
    *fourcc = fourcc_splane;
    *fourcc_alt = 0;
1353
1354
1355
  }
}

Rob Clark's avatar
Rob Clark committed
1356
1357
1358
1359
1360
1361
1362
/* 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
 */
1363
static gboolean
Rob Clark's avatar
Rob Clark committed
1364
gst_v4l2_object_get_caps_info (GstV4l2Object * v4l2object, GstCaps * caps,
Wim Taymans's avatar
Wim Taymans committed
1365
    struct v4l2_fmtdesc **format, GstVideoInfo * info)
Rob Clark's avatar
Rob Clark committed
1366
1367
{
  GstStructure *structure;
1368
  guint32 fourcc, fourcc_alt = 0;
Rob Clark's avatar
Rob Clark committed
1369
  const gchar *mimetype;
Wim Taymans's avatar
Wim Taymans committed
1370
  struct v4l2_fmtdesc *fmt;
Rob Clark's avatar
Rob Clark committed
1371
1372
1373
1374
1375
1376

  /* default unknown values */
  fourcc = 0;

  structure = gst_caps_get_structure (caps, 0);

1377
1378
  mimetype = gst_structure_get_name (structure);

1379
1380
  if (!gst_video_info_from_caps (info, caps))
    goto invalid_format;
Rob Clark's avatar
Rob Clark committed
1381

1382
  if (g_str_equal (mimetype, "video/x-raw")) {
Wim Taymans's avatar
Wim Taymans committed
1383
    switch (GST_VIDEO_INFO_FORMAT (info)) {
1384
      case GST_VIDEO_FORMAT_I420:
Rob Clark's avatar
Rob Clark committed
1385
1386
        fourcc = V4L2_PIX_FMT_YUV420;
        break;
1387
      case GST_VIDEO_FORMAT_YUY2:
Rob Clark's avatar
Rob Clark committed
1388
1389
        fourcc = V4L2_PIX_FMT_YUYV;
        break;
1390
1391
#if 0
      case GST_VIDEO_FORMAT_Y41P:
Rob Clark's avatar
Rob Clark committed
1392
1393
        fourcc = V4L2_PIX_FMT_Y41P;
        break;
1394
1395
#endif
      case GST_VIDEO_FORMAT_UYVY:
Rob Clark's avatar
Rob Clark committed
1396
1397
        fourcc = V4L2_PIX_FMT_UYVY;
        break;
1398
      case GST_VIDEO_FORMAT_YV12:
Rob Clark's avatar
Rob Clark committed
1399
1400
        fourcc = V4L2_PIX_FMT_YVU420;
        break;
1401