gstv4l2object.c 98.6 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * common format / caps utilities:
 */
typedef enum
{
  GST_V4L2_RAW = 1 << 0,
  GST_V4L2_CODEC = 1 << 1,
  GST_V4L2_TRANSPORT = 1 << 2,
  GST_V4L2_NO_PARSE = 1 << 3,
  GST_V4L2_ALL = 0xffff
} GstV4L2FormatFlags;

typedef struct
{
  guint32 format;
  gboolean dimensions;
  GstV4L2FormatFlags flags;
} GstV4L2FormatDesc;

static const GstV4L2FormatDesc gst_v4l2_formats[] = {
  /* from Linux 2.6.15 videodev2.h */
  {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},

  /* two planes -- one Y, one Cr + Cb interleaved  */
  {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},

  /*  The following formats are not defined in the V4L2 specification */
  {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},

  /* see http://www.siliconimaging.com/RGB%20Bayer.htm */
  {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},

  /* compressed formats */
  {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},
  {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
  {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
  /* VP8 not parseable */
  {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},

  /*  Vendor-specific formats   */
  {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},
};

#define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))

146
147
static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);

148
149

#define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
150
static GType
151
152
153
154
155
156
gst_v4l2_device_get_type (void)
{
  static GType v4l2_device_type = 0;

  if (v4l2_device_type == 0) {
    static const GFlagsValue values[] = {
157
158
159
160
161
162
163
164
165
      {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"},
166
167
168
169
170
171
172
173
174
175
176

      {0, NULL, NULL}
    };

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

  return v4l2_device_type;
}

Rob Clark's avatar
Rob Clark committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#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;
}

229
GType
Wim Taymans's avatar
Wim Taymans committed
230
231
232
233
234
235
236
237
238
239
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"},
240
      {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
241
242
      {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
          "dmabuf-import"},
Wim Taymans's avatar
Wim Taymans committed
243
244
245
246
247
248
249
250

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

251
void
Rob Clark's avatar
Rob Clark committed
252
253
gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
    const char *default_device)
254
{
255
256
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device", "Device location",
257
          default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
258
259
  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
260
261
          "Name of the device", DEFAULT_PROP_DEVICE_NAME,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
262
263
264
  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,
265
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
266
267
  g_object_class_install_property (gobject_class, PROP_FLAGS,
      g_param_spec_flags ("flags", "Flags", "Device type flags",
268
269
          GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
270

271
  /**
272
   * GstV4l2Src:brightness:
273
274
275
   *
   * Picture brightness, or more precisely, the black level
   */
276
277
278
279
280
  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));
281
  /**
282
   * GstV4l2Src:contrast:
283
284
285
   *
   * Picture contrast or luma gain
   */
286
287
288
289
290
  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));
291
  /**
292
   * GstV4l2Src:saturation:
293
294
295
   *
   * Picture color saturation or chroma gain
   */
296
297
298
299
300
  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));
301
  /**
302
   * GstV4l2Src:hue:
303
304
305
   *
   * Hue or color balance
   */
306
307
308
309
310
  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
311
312

  /**
313
   * GstV4l2Src:norm:
Rob Clark's avatar
Rob Clark committed
314
315
316
317
318
319
320
321
   *
   * 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
322
323

  /**
324
   * GstV4l2Src:io-mode:
Wim Taymans's avatar
Wim Taymans committed
325
326
327
328
329
330
331
332
   *
   * 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));
333
334

  /**
335
   * GstV4l2Src:extra-controls:
336
337
338
339
340
341
342
343
344
345
346
   *
   * 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));
347
348

  /**
349
   * GstV4l2Src:pixel-aspect-ratio:
350
351
352
353
354
355
356
357
358
359
360
361
   *
   * 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));

  /**
362
   * GstV4l2Src:force-aspect-ratio:
363
364
365
366
367
368
369
370
371
372
373
   *
   * 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));

374
375
376
}

GstV4l2Object *
377
gst_v4l2_object_new (GstElement * element,
Rob Clark's avatar
Rob Clark committed
378
    enum v4l2_buf_type type,
379
    const char *default_device,
380
    GstV4l2GetInOutFunction get_in_out_func,
381
382
    GstV4l2SetInOutFunction set_in_out_func,
    GstV4l2UpdateFpsFunction update_fps_func)
383
384
385
386
{
  GstV4l2Object *v4l2object;

  /*
Rob Clark's avatar
Rob Clark committed
387
   * some default values
388
389
390
   */
  v4l2object = g_new0 (GstV4l2Object, 1);

Rob Clark's avatar
Rob Clark committed
391
392
393
  v4l2object->type = type;
  v4l2object->formats = NULL;

394
395
396
  v4l2object->element = element;
  v4l2object->get_in_out_func = get_in_out_func;
  v4l2object->set_in_out_func = set_in_out_func;
397
  v4l2object->update_fps_func = update_fps_func;
398
399

  v4l2object->video_fd = -1;
400
  v4l2object->poll = gst_poll_new (TRUE);
401
  v4l2object->active = FALSE;
Rob Clark's avatar
Rob Clark committed
402
  v4l2object->videodev = g_strdup (default_device);
403

404
405
  v4l2object->norms = NULL;
  v4l2object->channels = NULL;
406
407
408
409
  v4l2object->colors = NULL;

  v4l2object->xwindow_id = 0;

410
411
  v4l2object->keep_aspect = TRUE;

412
413
  v4l2object->n_v4l2_planes = 0;

414
415
  v4l2object->no_initial_format = FALSE;

416
417
418
  return v4l2object;
}

Rob Clark's avatar
Rob Clark committed
419
420
421
static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);


422
void
423
gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
424
{
425
  g_return_if_fail (v4l2object != NULL);
426

427
428
429
  if (v4l2object->videodev)
    g_free (v4l2object->videodev);

430
431
432
  if (v4l2object->poll)
    gst_poll_free (v4l2object->poll);

433
434
435
  if (v4l2object->channel)
    g_free (v4l2object->channel);

Rob Clark's avatar
Rob Clark committed
436
437
438
439
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

440
441
442
443
  if (v4l2object->probed_caps) {
    gst_caps_unref (v4l2object->probed_caps);
  }

444
  g_free (v4l2object);
445
446
}

Rob Clark's avatar
Rob Clark committed
447
448
449
450
451
452
453
454
455
456
457

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

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
482

483
gboolean
484
gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
485
486
487
488
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
Wim Taymans's avatar
Wim Taymans committed
489
      g_free (v4l2object->videodev);
490
      v4l2object->videodev = g_value_dup_string (value);
491
      break;
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
    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
507
508
    case PROP_TV_NORM:
      v4l2object->tv_norm = g_value_get_enum (value);
509
      break;
Rob Clark's avatar
Rob Clark committed
510
#if 0
511
    case PROP_CHANNEL:
512
513
514
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
515
            (gchar *) g_value_get_string (value));
516
517
518
519
520
521
522

        if (channel) {
          /* like gst_tuner_set_channel (tuner, channel)
             without g_object_notify */
          gst_v4l2_tuner_set_channel (v4l2object, channel);
        }
      } else {
523
524
        g_free (v4l2object->channel);
        v4l2object->channel = g_value_dup_string (value);
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
      }
      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;
544
#endif
Wim Taymans's avatar
Wim Taymans committed
545
546
547
    case PROP_IO_MODE:
      v4l2object->req_mode = g_value_get_enum (value);
      break;
548
549
550
551
552
553
554
555
556
557
558
    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;
    }
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
    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;
574
575
576
577
578
579
580
581
582
    default:
      return FALSE;
      break;
  }
  return TRUE;
}


gboolean
583
gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
584
585
586
587
588
589
590
591
    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:
    {
592
      const guchar *new = NULL;
593

594
      if (GST_V4L2_IS_OPEN (v4l2object)) {
595
        new = v4l2object->vcap.card;
596
      } else if (gst_v4l2_open (v4l2object)) {
597
        new = v4l2object->vcap.card;
598
599
        gst_v4l2_close (v4l2object);
      }
600
      g_value_set_string (value, (gchar *) new);
601
602
      break;
    }
603
604
605
606
607
608
609
610
    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;
    }
611
612
613
614
615
616
617
618
    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
619
620
621
            V4L2_CAP_VIDEO_OVERLAY |
            V4L2_CAP_VBI_CAPTURE |
            V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
622
623
624
625
626
627

        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;
628
629
630
631
      }
      g_value_set_flags (value, flags);
      break;
    }
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
    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
650
651
652
    case PROP_TV_NORM:
      g_value_set_enum (value, v4l2object->tv_norm);
      break;
Wim Taymans's avatar
Wim Taymans committed
653
654
655
    case PROP_IO_MODE:
      g_value_set_enum (value, v4l2object->req_mode);
      break;
656
657
658
    case PROP_EXTRA_CONTROLS:
      gst_value_set_structure (value, v4l2object->extra_controls);
      break;
659
660
661
662
663
664
665
    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;
666
667
668
669
670
671
672
673
674
675
676
677
    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
678
679
680
681
682
683
  GstTuner *tuner;

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

  tuner = GST_TUNER (v4l2object->element);
684

Rob Clark's avatar
Rob Clark committed
685
686
  if (v4l2object->tv_norm)
    norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
687
688
  GST_DEBUG_OBJECT (v4l2object->element, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
      "norm=%p", (guint64) v4l2object->tv_norm, norm);
689
690
691
692
693
694
  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
695
696
      v4l2object->tv_norm =
          gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
697
698
699
700
      gst_tuner_norm_changed (tuner, norm);
    }
  }

701
702
  if (v4l2object->channel)
    channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
703
704
705
706
  if (channel) {
    gst_tuner_set_channel (tuner, channel);
  } else {
    channel =
707
708
        GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
            (v4l2object->element)));
709
710
711
712
713
    if (channel) {
      g_free (v4l2object->channel);
      v4l2object->channel = g_strdup (channel->label);
      gst_tuner_channel_changed (tuner, channel);
    }
714
715
  }

716
717
  if (channel
      && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
718
719
720
721
722
723
724
725
726
727
728
729
730
731
    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
732
gst_v4l2_object_open (GstV4l2Object * v4l2object)
733
734
735
736
737
738
739
740
741
{
  if (gst_v4l2_open (v4l2object))
    gst_v4l2_set_defaults (v4l2object);
  else
    return FALSE;

  return TRUE;
}

742
743
744
745
746
747
748
749
750
751
gboolean
gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
{
  gboolean ret;

  ret = gst_v4l2_dup (v4l2object, other);

  return ret;
}

752
gboolean
753
gst_v4l2_object_close (GstV4l2Object * v4l2object)
754
755
756
757
{
  if (!gst_v4l2_close (v4l2object))
    return FALSE;

758
759
  gst_caps_replace (&v4l2object->probed_caps, NULL);

760
761
762
763
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

764
765
  return TRUE;
}
Rob Clark's avatar
Rob Clark committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782

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
783
    if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
784
785
786
787
        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
788
789
        return fmt;
      }
Rob Clark's avatar
Rob Clark committed
790
791
792
793
794
795
796
797
798
799
    }
    walk = g_slist_next (walk);
  }

  return NULL;
}



/* complete made up ranking, the values themselves are meaningless */
800
801
/* 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
802
803
804
805
806
807
808
809
810
811
812
813
#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
814
gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
Rob Clark's avatar
Rob Clark committed
815
{
816
  guint32 fourcc = fmt->pixelformat;
817
  gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
818
819
  gint rank = 0;

Rob Clark's avatar
Rob Clark committed
820
821
  switch (fourcc) {
    case V4L2_PIX_FMT_MJPEG:
Thiago Santos's avatar
Thiago Santos committed
822
    case V4L2_PIX_FMT_PJPG:
823
824
      rank = JPEG_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
825
    case V4L2_PIX_FMT_JPEG:
826
827
      rank = JPEG_BASE_RANK + 1;
      break;
828
829
830
    case V4L2_PIX_FMT_MPEG:    /* MPEG          */
      rank = JPEG_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
831
832
833
834
835
836

    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:
837
838
      rank = RGB_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
839
840
841

    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
842
843
      rank = RGB_BASE_RANK - 1;
      break;
Rob Clark's avatar
Rob Clark committed
844
845
846

    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_BGR32:
847
848
      rank = RGB_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
849
850

    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
851
852
      rank = GREY_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
853
854

    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
855
    case V4L2_PIX_FMT_NV12M:   /* Same as NV12      */
856
    case V4L2_PIX_FMT_NV12MT:  /* NV12 64x32 tile   */
Rob Clark's avatar
Rob Clark committed
857
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
858
    case V4L2_PIX_FMT_NV21M:   /* Same as NV21      */
Rob Clark's avatar
Rob Clark committed
859
860
    case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
    case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
861
862
      rank = YUV_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
863
864

    case V4L2_PIX_FMT_YVU410:  /* YVU9,  9 bits per pixel */
865
866
      rank = YUV_BASE_RANK + 3;
      break;
Rob Clark's avatar
Rob Clark committed
867
    case V4L2_PIX_FMT_YUV410:  /* YUV9,  9 bits per pixel */
868
869
      rank = YUV_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
870
    case V4L2_PIX_FMT_YUV420:  /* I420, 12 bits per pixel */
871
872
      rank = YUV_BASE_RANK + 7;
      break;
Rob Clark's avatar
Rob Clark committed
873
    case V4L2_PIX_FMT_YUYV:    /* YUY2, 16 bits per pixel */
874
875
      rank = YUV_BASE_RANK + 10;
      break;
Rob Clark's avatar
Rob Clark committed
876
    case V4L2_PIX_FMT_YVU420:  /* YV12, 12 bits per pixel */
877
878
      rank = YUV_BASE_RANK + 6;
      break;
Rob Clark's avatar
Rob Clark committed
879
    case V4L2_PIX_FMT_UYVY:    /* UYVY, 16 bits per pixel */
880
881
      rank = YUV_BASE_RANK + 9;
      break;
Rob Clark's avatar
Rob Clark committed
882
    case V4L2_PIX_FMT_Y41P:    /* Y41P, 12 bits per pixel */
883
884
      rank = YUV_BASE_RANK + 5;
      break;
Rob Clark's avatar
Rob Clark committed
885
    case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
886
887
      rank = YUV_BASE_RANK + 4;
      break;
Rob Clark's avatar
Rob Clark committed
888
    case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
889
890
      rank = YUV_BASE_RANK + 8;
      break;
Rob Clark's avatar
Rob Clark committed
891
892

    case V4L2_PIX_FMT_DV:
893
894
      rank = DV_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
895
896

    case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
897
898
      rank = 0;
      break;
Rob Clark's avatar
Rob Clark committed
899
900

    case V4L2_PIX_FMT_SBGGR8:
901
902
      rank = BAYER_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
903
904

    case V4L2_PIX_FMT_SN9C10X:
905
906
      rank = S910_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
907
908

    case V4L2_PIX_FMT_PWC1:
909
910
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
911
    case V4L2_PIX_FMT_PWC2:
912
913
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
914
915

    default:
916
      rank = 0;
Rob Clark's avatar
Rob Clark committed
917
918
919
      break;
  }

920
921
922
923
924
925
926
927
  /* 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
928
929
930
931
932
933
934
}



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

938
  if (fa->pixelformat == fb->pixelformat)
Rob Clark's avatar
Rob Clark committed
939
940
    return 0;

941
942
  return gst_v4l2_object_format_get_rank (fb) -
      gst_v4l2_object_format_get_rank (fa);
Rob Clark's avatar
Rob Clark committed
943
944
945
946
947
948
949
950
}

/******************************************************
 * gst_v4l2_object_fill_format_list():
 *   create list of supported capture formats
 * return value: TRUE on success, FALSE on error
 ******************************************************/
static gboolean
951
952
gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
    enum v4l2_buf_type type)
Rob Clark's avatar
Rob Clark committed
953
954
955
956
957
958
959
960
961
962
963
{
  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;
964
    format->type = type;
Rob Clark's avatar
Rob Clark committed
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989

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

990
991
992
993
994
995
996
997
998
999
1000
#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)" : "");