gstv4l2object.c 146 KB
Newer Older
1
2
/* GStreamer
 *
3
 * Copyright (C) 2001-2002 Ronald Bultje <rbultje@ronald.bitfreak.net>
Edgard Lima's avatar
Edgard Lima committed
4
 *               2006 Edgard Lima <edgard.lima@gmail.com>
5
6
 *
 * 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
25
26
27
28
 */

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

#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
29
30
31
#include <sys/mman.h>
#include <sys/ioctl.h>

32

33
34
35
36
#ifdef HAVE_GUDEV
#include <gudev/gudev.h>
#endif

37
#include "ext/videodev2.h"
38
#include "gstv4l2object.h"
39
40
41
#include "gstv4l2tuner.h"
#include "gstv4l2colorbalance.h"

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

44
#include <gst/video/video.h>
45
#include <gst/allocators/gstdmabuf.h>
46

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

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

56
#define ENCODED_BUFFER_SIZE             (2 * 1024 * 1024)
57

Stefan Kost's avatar
Stefan Kost committed
58
59
60
61
62
63
enum
{
  PROP_0,
  V4L2_STD_OBJECT_PROPS,
};

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * 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[] = {
84
  /* RGB formats */
85
  {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
86
87
88
89
  {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
90
91
  {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
92
  {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
93
94
  {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
95
96
97
98
99
100
101
102
103
  {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},

  /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
  {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
104
105
  {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
106
107

  /* Grey formats */
108
  {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},

  /* Palette formats */
  {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},

  /* Chrominance formats */
  {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},

  /* Luminance+Chrominance formats */
124
125
  {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
126
  {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
127
  {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
128
129
  {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
130
  {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
131
  {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
132
133
134
  {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
135
136
137
138
139
140
141
142
143
144
  {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
145
146
147
148
149

  /* 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},
150
  {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
151
152
  {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
153
154
155
156
157
  {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
  {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
158
  {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
159

160
  /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
161
  {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_CODEC},
162
163
164
  {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_CODEC},
165
166
167
168
169
170
171

  /* 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},
Ezequiel Garcia's avatar
Ezequiel Garcia committed
172
  {V4L2_PIX_FMT_FWHT, FALSE, GST_V4L2_CODEC},
173
174
175
  {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
176
  {V4L2_PIX_FMT_HEVC, FALSE, GST_V4L2_CODEC},
177
  {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
178
179
180
  {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
181
182
183
  {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
  {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
184
  {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
Nicolas Dufresne's avatar
Nicolas Dufresne committed
185
  {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
186
187
188
189
190
191
192
193
194
195

  /*  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},
};

#define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))

196
197
static GSList *gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object);

198
199

#define GST_TYPE_V4L2_DEVICE_FLAGS (gst_v4l2_device_get_type ())
200
static GType
201
202
203
204
205
206
gst_v4l2_device_get_type (void)
{
  static GType v4l2_device_type = 0;

  if (v4l2_device_type == 0) {
    static const GFlagsValue values[] = {
207
208
209
210
211
212
213
214
215
      {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"},
216
217
218
219
220
221
222
223
224
225
226

      {0, NULL, NULL}
    };

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

  return v4l2_device_type;
}

Rob Clark's avatar
Rob Clark committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#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;
}

279
GType
Wim Taymans's avatar
Wim Taymans committed
280
281
282
283
284
285
286
287
288
289
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"},
290
      {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
291
292
      {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
          "dmabuf-import"},
Wim Taymans's avatar
Wim Taymans committed
293
294
295
296
297
298
299
300

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

301
void
Rob Clark's avatar
Rob Clark committed
302
303
gst_v4l2_object_install_properties_helper (GObjectClass * gobject_class,
    const char *default_device)
304
{
305
306
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device", "Device location",
307
          default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308
309
  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
310
311
          "Name of the device", DEFAULT_PROP_DEVICE_NAME,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
312
313
314
  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,
315
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
316
317
  g_object_class_install_property (gobject_class, PROP_FLAGS,
      g_param_spec_flags ("flags", "Flags", "Device type flags",
318
319
          GST_TYPE_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320

321
  /**
322
   * GstV4l2Src:brightness:
323
324
325
   *
   * Picture brightness, or more precisely, the black level
   */
326
327
328
329
330
  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));
331
  /**
332
   * GstV4l2Src:contrast:
333
334
335
   *
   * Picture contrast or luma gain
   */
336
337
338
339
340
  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));
341
  /**
342
   * GstV4l2Src:saturation:
343
344
345
   *
   * Picture color saturation or chroma gain
   */
346
347
348
349
350
  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));
351
  /**
352
   * GstV4l2Src:hue:
353
354
355
   *
   * Hue or color balance
   */
356
357
358
359
360
  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
361
362

  /**
363
   * GstV4l2Src:norm:
Rob Clark's avatar
Rob Clark committed
364
365
366
367
368
369
370
371
   *
   * 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
372
373

  /**
374
   * GstV4l2Src:io-mode:
Wim Taymans's avatar
Wim Taymans committed
375
376
377
378
379
380
381
382
   *
   * 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));
383
384

  /**
385
   * GstV4l2Src:extra-controls:
386
387
388
389
390
391
392
393
394
395
396
   *
   * 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));
397
398

  /**
399
   * GstV4l2Src:pixel-aspect-ratio:
400
401
402
403
404
405
406
407
408
409
410
411
   *
   * 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));

  /**
412
   * GstV4l2Src:force-aspect-ratio:
413
414
415
416
417
418
419
420
421
422
423
   *
   * 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));

424
425
}

426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
void
gst_v4l2_object_install_m2m_properties_helper (GObjectClass * gobject_class)
{
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device", "Device location",
          NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
          "Name of the device", DEFAULT_PROP_DEVICE_NAME,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  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,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_OUTPUT_IO_MODE,
      g_param_spec_enum ("output-io-mode", "Output IO mode",
          "Output side I/O mode (matches sink pad)",
          GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (gobject_class, PROP_CAPTURE_IO_MODE,
      g_param_spec_enum ("capture-io-mode", "Capture IO mode",
          "Capture I/O mode (matches src pad)",
          GST_TYPE_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

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

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
#ifdef HAVE_LIBV4L2
#if SIZEOF_OFF_T < 8

static gpointer
v4l2_mmap_wrapper (gpointer start, gsize length, gint prot, gint flags, gint fd,
    off_t offset)
{
  return v4l2_mmap (start, length, prot, flags, fd, (gint64) offset);
}

#define v4l2_mmap v4l2_mmap_wrapper

#endif /* SIZEOF_OFF_T < 8 */
#endif /* HAVE_LIBV4L2 */

477
GstV4l2Object *
478
gst_v4l2_object_new (GstElement * element,
479
    GstObject * debug_object,
Rob Clark's avatar
Rob Clark committed
480
    enum v4l2_buf_type type,
481
    const char *default_device,
482
    GstV4l2GetInOutFunction get_in_out_func,
483
484
    GstV4l2SetInOutFunction set_in_out_func,
    GstV4l2UpdateFpsFunction update_fps_func)
485
486
487
488
{
  GstV4l2Object *v4l2object;

  /*
Rob Clark's avatar
Rob Clark committed
489
   * some default values
490
491
492
   */
  v4l2object = g_new0 (GstV4l2Object, 1);

Rob Clark's avatar
Rob Clark committed
493
494
495
  v4l2object->type = type;
  v4l2object->formats = NULL;

496
  v4l2object->element = element;
497
  v4l2object->dbg_obj = debug_object;
498
499
  v4l2object->get_in_out_func = get_in_out_func;
  v4l2object->set_in_out_func = set_in_out_func;
500
  v4l2object->update_fps_func = update_fps_func;
501
502

  v4l2object->video_fd = -1;
503
  v4l2object->active = FALSE;
Rob Clark's avatar
Rob Clark committed
504
  v4l2object->videodev = g_strdup (default_device);
505

506
507
  v4l2object->norms = NULL;
  v4l2object->channels = NULL;
508
509
  v4l2object->colors = NULL;

510
511
  v4l2object->keep_aspect = TRUE;

512
513
  v4l2object->n_v4l2_planes = 0;

514
515
  v4l2object->no_initial_format = FALSE;

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  /* We now disable libv4l2 by default, but have an env to enable it. */
#ifdef HAVE_LIBV4L2
  if (g_getenv ("GST_V4L2_USE_LIBV4L2")) {
    v4l2object->fd_open = v4l2_fd_open;
    v4l2object->close = v4l2_close;
    v4l2object->dup = v4l2_dup;
    v4l2object->ioctl = v4l2_ioctl;
    v4l2object->read = v4l2_read;
    v4l2object->mmap = v4l2_mmap;
    v4l2object->munmap = v4l2_munmap;
  } else
#endif
  {
    v4l2object->fd_open = NULL;
    v4l2object->close = close;
    v4l2object->dup = dup;
    v4l2object->ioctl = ioctl;
    v4l2object->read = read;
534
    v4l2object->mmap = mmap;
535
536
537
    v4l2object->munmap = munmap;
  }

538
539
540
  return v4l2object;
}

Rob Clark's avatar
Rob Clark committed
541
542
543
static gboolean gst_v4l2_object_clear_format_list (GstV4l2Object * v4l2object);


544
void
545
gst_v4l2_object_destroy (GstV4l2Object * v4l2object)
546
{
547
  g_return_if_fail (v4l2object != NULL);
548

549
  g_free (v4l2object->videodev);
550

551
  g_free (v4l2object->channel);
552

Rob Clark's avatar
Rob Clark committed
553
554
555
556
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

557
558
559
560
  if (v4l2object->probed_caps) {
    gst_caps_unref (v4l2object->probed_caps);
  }

561
562
563
564
  if (v4l2object->extra_controls) {
    gst_structure_free (v4l2object->extra_controls);
  }

565
  g_free (v4l2object);
566
567
}

Rob Clark's avatar
Rob Clark committed
568
569
570
571
572
573
574
575
576
577
578

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

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
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
603

604
gboolean
605
gst_v4l2_object_set_property_helper (GstV4l2Object * v4l2object,
606
607
608
609
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  switch (prop_id) {
    case PROP_DEVICE:
Wim Taymans's avatar
Wim Taymans committed
610
      g_free (v4l2object->videodev);
611
      v4l2object->videodev = g_value_dup_string (value);
612
      break;
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
    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
628
629
    case PROP_TV_NORM:
      v4l2object->tv_norm = g_value_get_enum (value);
630
      break;
Rob Clark's avatar
Rob Clark committed
631
#if 0
632
    case PROP_CHANNEL:
633
634
635
      if (GST_V4L2_IS_OPEN (v4l2object)) {
        GstTuner *tuner = GST_TUNER (v4l2object->element);
        GstTunerChannel *channel = gst_tuner_find_channel_by_name (tuner,
636
            (gchar *) g_value_get_string (value));
637
638
639
640
641
642
643

        if (channel) {
          /* like gst_tuner_set_channel (tuner, channel)
             without g_object_notify */
          gst_v4l2_tuner_set_channel (v4l2object, channel);
        }
      } else {
644
645
        g_free (v4l2object->channel);
        v4l2object->channel = g_value_dup_string (value);
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
      }
      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;
665
#endif
666

Wim Taymans's avatar
Wim Taymans committed
667
668
669
    case PROP_IO_MODE:
      v4l2object->req_mode = g_value_get_enum (value);
      break;
670
671
672
673
674
675
676
677
    case PROP_CAPTURE_IO_MODE:
      g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
      v4l2object->req_mode = g_value_get_enum (value);
      break;
    case PROP_OUTPUT_IO_MODE:
      g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
      v4l2object->req_mode = g_value_get_enum (value);
      break;
678
679
680
681
682
683
684
685
686
687
688
    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;
    }
689
    case PROP_PIXEL_ASPECT_RATIO:
690
691
692
693
      if (v4l2object->par) {
        g_value_unset (v4l2object->par);
        g_free (v4l2object->par);
      }
694
695
696
697
698
699
      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);
      }
700

701
      GST_DEBUG_OBJECT (v4l2object->dbg_obj, "set PAR to %d/%d",
702
703
704
705
706
707
          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;
708
709
710
711
712
713
714
715
716
    default:
      return FALSE;
      break;
  }
  return TRUE;
}


gboolean
717
gst_v4l2_object_get_property_helper (GstV4l2Object * v4l2object,
718
719
720
721
722
723
724
725
    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:
    {
726
      const guchar *name = NULL;
727

728
729
730
731
      if (GST_V4L2_IS_OPEN (v4l2object))
        name = v4l2object->vcap.card;

      g_value_set_string (value, (gchar *) name);
732
733
      break;
    }
734
735
736
737
738
739
740
741
    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;
    }
742
743
744
745
746
    case PROP_FLAGS:
    {
      guint flags = 0;

      if (GST_V4L2_IS_OPEN (v4l2object)) {
747
        flags |= v4l2object->device_caps &
748
749
            (V4L2_CAP_VIDEO_CAPTURE |
            V4L2_CAP_VIDEO_OUTPUT |
750
751
752
            V4L2_CAP_VIDEO_OVERLAY |
            V4L2_CAP_VBI_CAPTURE |
            V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
753

754
        if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
755
756
          flags |= V4L2_CAP_VIDEO_CAPTURE;

757
        if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
758
          flags |= V4L2_CAP_VIDEO_OUTPUT;
759
760
761
762
      }
      g_value_set_flags (value, flags);
      break;
    }
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
    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
781
782
783
    case PROP_TV_NORM:
      g_value_set_enum (value, v4l2object->tv_norm);
      break;
Wim Taymans's avatar
Wim Taymans committed
784
785
786
    case PROP_IO_MODE:
      g_value_set_enum (value, v4l2object->req_mode);
      break;
787
788
789
790
791
792
793
794
    case PROP_CAPTURE_IO_MODE:
      g_return_val_if_fail (!V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
      g_value_set_enum (value, v4l2object->req_mode);
      break;
    case PROP_OUTPUT_IO_MODE:
      g_return_val_if_fail (V4L2_TYPE_IS_OUTPUT (v4l2object->type), FALSE);
      g_value_set_enum (value, v4l2object->req_mode);
      break;
795
796
797
    case PROP_EXTRA_CONTROLS:
      gst_value_set_structure (value, v4l2object->extra_controls);
      break;
798
799
800
801
802
803
804
    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;
805
806
807
808
809
810
811
    default:
      return FALSE;
      break;
  }
  return TRUE;
}

812
813
814
static void
gst_v4l2_get_driver_min_buffers (GstV4l2Object * v4l2object)
{
815
  struct v4l2_control control = { 0, };
816

817
  g_return_if_fail (GST_V4L2_IS_OPEN (v4l2object));
818

819
820
821
822
823
  if (V4L2_TYPE_IS_OUTPUT (v4l2object->type))
    control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
  else
    control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;

824
  if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0) {
825
    GST_DEBUG_OBJECT (v4l2object->dbg_obj,
826
827
        "driver requires a minimum of %d buffers", control.value);
    v4l2object->min_buffers = control.value;
828
829
830
831
832
  } else {
    v4l2object->min_buffers = 0;
  }
}

833
834
835
836
837
static void
gst_v4l2_set_defaults (GstV4l2Object * v4l2object)
{
  GstTunerNorm *norm = NULL;
  GstTunerChannel *channel = NULL;
Rob Clark's avatar
Rob Clark committed
838
839
840
841
842
843
  GstTuner *tuner;

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

  tuner = GST_TUNER (v4l2object->element);
844

Rob Clark's avatar
Rob Clark committed
845
846
  if (v4l2object->tv_norm)
    norm = gst_v4l2_tuner_get_norm_by_std_id (v4l2object, v4l2object->tv_norm);
847
  GST_DEBUG_OBJECT (v4l2object->dbg_obj, "tv_norm=0x%" G_GINT64_MODIFIER "x, "
848
      "norm=%p", (guint64) v4l2object->tv_norm, norm);
849
850
851
852
853
854
  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
855
856
      v4l2object->tv_norm =
          gst_v4l2_tuner_get_std_id_by_norm (v4l2object, norm);
857
858
859
860
      gst_tuner_norm_changed (tuner, norm);
    }
  }

861
862
  if (v4l2object->channel)
    channel = gst_tuner_find_channel_by_name (tuner, v4l2object->channel);
863
864
865
866
  if (channel) {
    gst_tuner_set_channel (tuner, channel);
  } else {
    channel =
867
868
        GST_TUNER_CHANNEL (gst_tuner_get_channel (GST_TUNER
            (v4l2object->element)));
869
870
871
872
873
    if (channel) {
      g_free (v4l2object->channel);
      v4l2object->channel = g_strdup (channel->label);
      gst_tuner_channel_changed (tuner, channel);
    }
874
875
  }

876
877
  if (channel
      && GST_TUNER_CHANNEL_HAS_FLAG (channel, GST_TUNER_CHANNEL_FREQUENCY)) {
878
879
880
881
882
883
884
885
886
887
888
889
890
891
    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
892
gst_v4l2_object_open (GstV4l2Object * v4l2object)
893
894
895
896
897
898
899
900
901
{
  if (gst_v4l2_open (v4l2object))
    gst_v4l2_set_defaults (v4l2object);
  else
    return FALSE;

  return TRUE;
}

902
903
904
905
906
907
908
909
910
911
gboolean
gst_v4l2_object_open_shared (GstV4l2Object * v4l2object, GstV4l2Object * other)
{
  gboolean ret;

  ret = gst_v4l2_dup (v4l2object, other);

  return ret;
}

912
gboolean
913
gst_v4l2_object_close (GstV4l2Object * v4l2object)
914
915
916
917
{
  if (!gst_v4l2_close (v4l2object))
    return FALSE;

918
919
  gst_caps_replace (&v4l2object->probed_caps, NULL);

920
921
922
  /* reset our copy of the device caps */
  v4l2object->device_caps = 0;

923
924
925
926
  if (v4l2object->formats) {
    gst_v4l2_object_clear_format_list (v4l2object);
  }

927
928
929
930
931
  if (v4l2object->par) {
    g_value_unset (v4l2object->par);
    g_free (v4l2object->par);
    v4l2object->par = NULL;
  }
932

933
934
935
936
  if (v4l2object->channel) {
    g_free (v4l2object->channel);
    v4l2object->channel = NULL;
  }
937

938
939
  return TRUE;
}
Rob Clark's avatar
Rob Clark committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956

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
957
    if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
958
959
960
961
        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
962
963
        return fmt;
      }
Rob Clark's avatar
Rob Clark committed
964
965
966
967
968
969
970
971
972
973
    }
    walk = g_slist_next (walk);
  }

  return NULL;
}



/* complete made up ranking, the values themselves are meaningless */
974
975
/* 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
976
977
978
979
980
981
982
983
984
985
986
987
#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
988
gst_v4l2_object_format_get_rank (const struct v4l2_fmtdesc *fmt)
Rob Clark's avatar
Rob Clark committed
989
{
990
  guint32 fourcc = fmt->pixelformat;
991
  gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
992
993
  gint rank = 0;

Rob Clark's avatar
Rob Clark committed
994
995
  switch (fourcc) {
    case V4L2_PIX_FMT_MJPEG:
Thiago Santos's avatar
Thiago Santos committed
996
    case V4L2_PIX_FMT_PJPG:
997
998
      rank = JPEG_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
999
    case V4L2_PIX_FMT_JPEG:
1000
1001
      rank = JPEG_BASE_RANK + 1;
      break;
1002
1003
1004
    case V4L2_PIX_FMT_MPEG:    /* MPEG          */
      rank = JPEG_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
1005
1006

    case V4L2_PIX_FMT_RGB332:
1007
1008
    case V4L2_PIX_FMT_ARGB555:
    case V4L2_PIX_FMT_XRGB555:
Rob Clark's avatar
Rob Clark committed
1009
    case V4L2_PIX_FMT_RGB555:
1010
1011
    case V4L2_PIX_FMT_ARGB555X:
    case V4L2_PIX_FMT_XRGB555X:
Rob Clark's avatar
Rob Clark committed
1012
    case V4L2_PIX_FMT_RGB555X:
1013
    case V4L2_PIX_FMT_BGR666:
Rob Clark's avatar
Rob Clark committed
1014
1015
    case V4L2_PIX_FMT_RGB565:
    case V4L2_PIX_FMT_RGB565X:
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
    case V4L2_PIX_FMT_RGB444:
    case V4L2_PIX_FMT_Y4:
    case V4L2_PIX_FMT_Y6:
    case V4L2_PIX_FMT_Y10:
    case V4L2_PIX_FMT_Y12:
    case V4L2_PIX_FMT_Y10BPACK:
    case V4L2_PIX_FMT_YUV555:
    case V4L2_PIX_FMT_YUV565:
    case V4L2_PIX_FMT_YUV32:
    case V4L2_PIX_FMT_NV12MT_16X16:
    case V4L2_PIX_FMT_NV42:
    case V4L2_PIX_FMT_H264_MVC:
1028
1029
      rank = RGB_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1030
1031
1032

    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
1033
1034
      rank = RGB_BASE_RANK - 1;
      break;
Rob Clark's avatar
Rob Clark committed
1035
1036
1037

    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_BGR32:
1038
1039
1040
1041
    case V4L2_PIX_FMT_ABGR32:
    case V4L2_PIX_FMT_XBGR32:
    case V4L2_PIX_FMT_ARGB32:
    case V4L2_PIX_FMT_XRGB32:
1042
1043
      rank = RGB_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1044
1045

    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
1046
1047
      rank = GREY_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1048
1049

    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
1050
    case V4L2_PIX_FMT_NV12M:   /* Same as NV12      */
1051
    case V4L2_PIX_FMT_NV12MT:  /* NV12 64x32 tile   */
Rob Clark's avatar
Rob Clark committed
1052
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
1053
    case V4L2_PIX_FMT_NV21M:   /* Same as NV21      */
Rob Clark's avatar
Rob Clark committed
1054
1055
    case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
    case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
1056
1057
1058
1059
1060
    case V4L2_PIX_FMT_NV16:    /* 16  Y/CbCr 4:2:2  */
    case V4L2_PIX_FMT_NV16M:   /* Same as NV16      */
    case V4L2_PIX_FMT_NV61:    /* 16  Y/CrCb 4:2:2  */
    case V4L2_PIX_FMT_NV61M:   /* Same as NV61      */
    case V4L2_PIX_FMT_NV24:    /* 24  Y/CrCb 4:4:4  */
1061
1062
      rank = YUV_ODD_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1063
1064

    case V4L2_PIX_FMT_YVU410:  /* YVU9,  9 bits per pixel */
1065
1066
      rank = YUV_BASE_RANK + 3;
      break;
Rob Clark's avatar
Rob Clark committed
1067
    case V4L2_PIX_FMT_YUV410:  /* YUV9,  9 bits per pixel */
1068
1069
      rank = YUV_BASE_RANK + 2;
      break;
Rob Clark's avatar
Rob Clark committed
1070
    case V4L2_PIX_FMT_YUV420:  /* I420, 12 bits per pixel */
1071
    case V4L2_PIX_FMT_YUV420M:
1072
1073
      rank = YUV_BASE_RANK + 7;
      break;
Rob Clark's avatar
Rob Clark committed
1074
    case V4L2_PIX_FMT_YUYV:    /* YUY2, 16 bits per pixel */
1075
1076
      rank = YUV_BASE_RANK + 10;
      break;
Rob Clark's avatar
Rob Clark committed
1077
    case V4L2_PIX_FMT_YVU420:  /* YV12, 12 bits per pixel */
1078
1079
      rank = YUV_BASE_RANK + 6;
      break;
Rob Clark's avatar
Rob Clark committed
1080
    case V4L2_PIX_FMT_UYVY:    /* UYVY, 16 bits per pixel */
1081
1082
      rank = YUV_BASE_RANK + 9;
      break;
1083
1084
1085
    case V4L2_PIX_FMT_YUV444:
      rank = YUV_BASE_RANK + 6;
      break;
Rob Clark's avatar
Rob Clark committed
1086
    case V4L2_PIX_FMT_Y41P:    /* Y41P, 12 bits per pixel */
1087
1088
      rank = YUV_BASE_RANK + 5;
      break;
Rob Clark's avatar
Rob Clark committed
1089
    case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1090
1091
      rank = YUV_BASE_RANK + 4;
      break;
Rob Clark's avatar
Rob Clark committed
1092
    case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1093
1094
      rank = YUV_BASE_RANK + 8;
      break;
Rob Clark's avatar
Rob Clark committed
1095
1096

    case V4L2_PIX_FMT_DV:
1097
1098
      rank = DV_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1099
1100

    case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
1101
1102
      rank = 0;
      break;
Rob Clark's avatar
Rob Clark committed
1103
1104

    case V4L2_PIX_FMT_SBGGR8:
1105
1106
1107
    case V4L2_PIX_FMT_SGBRG8:
    case V4L2_PIX_FMT_SGRBG8:
    case V4L2_PIX_FMT_SRGGB8:
1108
1109
      rank = BAYER_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1110
1111

    case V4L2_PIX_FMT_SN9C10X:
1112
1113
      rank = S910_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1114
1115

    case V4L2_PIX_FMT_PWC1:
1116
1117
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1118
    case V4L2_PIX_FMT_PWC2:
1119
1120
      rank = PWC_BASE_RANK;
      break;
Rob Clark's avatar
Rob Clark committed
1121
1122

    default:
1123
      rank = 0;
Rob Clark's avatar
Rob Clark committed
1124
1125
1126
      break;
  }

1127
1128
1129
1130
1131
1132
1133
1134
  /* 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
1135
1136
1137
1138
1139
1140
1141
}



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

1145
  if (fa->pixelformat == fb->pixelformat)
Rob Clark's avatar
Rob Clark committed
1146
1147
    return 0;

1148
1149
  return gst_v4l2_object_format_get_rank (fb) -
      gst_v4l2_object_format_get_rank (fa);
Rob Clark's avatar
Rob Clark committed
1150
1151
1152
1153
1154
1155
1156
1157
}

/******************************************************
 * gst_v4l2_object_fill_format_list():
 *   create list of supported capture formats
 * return value: TRUE on success, FALSE on error
 ******************************************************/
static gboolean
1158
1159
gst_v4l2_object_fill_format_list (GstV4l2Object * v4l2object,
    enum v4l2_buf_type type)
Rob Clark's avatar
Rob Clark committed
1160
1161
1162
1163
{
  gint n;
  struct v4l2_fmtdesc *format;

1164
  GST_DEBUG_OBJECT (v4l2object->dbg_obj, "getting src format enumerations");
Rob Clark's avatar
Rob Clark committed
1165
1166
1167
1168
1169
1170

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

    format->index = n;
1171
    format->type = type;
Rob Clark's avatar
Rob Clark committed
1172

1173
    if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0) {
Rob Clark's avatar
Rob Clark committed
1174
1175
1176
1177
1178
1179
1180
1181
      if (errno == EINVAL) {
        g_free (format);
        break;                  /* end of enumeration */
      } else {
        goto failed;
      }
    }

1182
1183
1184
1185
    GST_LOG_OBJECT (v4l2object->dbg_obj, "index:       %u", format->index);
    GST_LOG_OBJECT (v4l2object->dbg_obj, "type:        %d", format->type);
    GST_LOG_OBJECT (v4l2object->dbg_obj, "flags:       %08x", format->flags);
    GST_LOG_OBJECT (v4l2object->dbg_obj, "description: '%s'",
Rob Clark's avatar
Rob Clark committed
1186
        format->description);
1187
    GST_LOG_OBJECT (v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
Rob Clark's avatar
Rob Clark committed
1188
1189
1190
1191
1192
1193
1194
1195
1196
        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);
  }

1197
1198
1199
1200
#ifndef GST_DISABLE_GST_DEBUG
  {
    GSList *l;

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

1205
      GST_INFO_OBJECT (v4l2object->dbg_obj,
1206
1207
1208
1209
1210
          "  %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS (format->pixelformat),
          ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
    }
  }
#endif
Rob Clark's avatar
Rob Clark committed
1211
1212
1213
1214
1215
1216

  return TRUE;

  /* ERRORS */
failed:
  {
1217
1218
    g_free (format);

1219
    if (v4l2object->element)
1220
1221
      return FALSE;

Rob Clark's avatar
Rob Clark committed
1222
    GST_ELEMENT_ERROR (v4l2object->element, RESOURCE, SETTINGS,
1223
1224
1225
1226
        (_("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)));
1227

Rob Clark's avatar
Rob Clark committed
1228
1229
1230
1231
    return FALSE;
  }
}

Stefan Kost's avatar
Stefan Kost committed
1232
/*
1233
1234
1235
  * Get the list of supported capture formats, a list of
  * <code>struct v4l2_fmtdesc</code>.
  */
1236
static GSList *
Rob Clark's avatar
Rob Clark committed
1237
1238
gst_v4l2_object_get_format_list (GstV4l2Object * v4l2object)
{
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
  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
1265
1266
1267
  return v4l2object->formats;
}

1268
1269
1270
1271
1272
1273
1274
1275
1276
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;
1277
1278
1279
1280
1281
1282
1283
    case V4L2_PIX_FMT_Y16:
      format = GST_VIDEO_FORMAT_GRAY16_LE;
      break;
    case V4L2_PIX_FMT_Y16_BE:
      format = GST_VIDEO_FORMAT_GRAY16_BE;
      break;
    case V4L2_PIX_FMT_XRGB555:
1284
1285
1286
    case V4L2_PIX_FMT_RGB555:
      format = GST_VIDEO_FORMAT_RGB15;
      break;
1287
1288
1289
1290
    case V4L2_PIX_FMT_XRGB555X:
    case V4L2_PIX_FMT_RGB555X:
      format = GST_VIDEO_FORMAT_BGR15;
      break;
1291
1292
1293
1294
1295
1296
1297
1298
1299
    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;
1300
    case V4L2_PIX_FMT_XRGB32:
1301
    case V4L2_PIX_FMT_RGB32: