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

23
#ifdef HAVE_CONFIG_H
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
24
#include <config.h>
25
26
#endif

27
28
29
#include <string.h>
#include <sys/time.h>
#include "v4l2src_calls.h"
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
30
31
32
33
34
35
36
37
38
39
40
#include <sys/ioctl.h>
#include <unistd.h>


static GstElementDetails gst_v4l2src_details =
GST_ELEMENT_DETAILS ("Video (video4linux2/raw) Source",
    "Source/Video",
    "Reads raw frames from a video4linux2 (BT8x8) device",
    "Ronald Bultje <rbultje@ronald.bitfreak.net>,"
    " Edgard Lima <edgard.lima@indt.org.br>");

41

42
43
44
GST_DEBUG_CATEGORY (v4l2src_debug);
#define GST_CAT_DEFAULT v4l2src_debug

45

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
46
47
enum
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
48
49
  PROP_0,
  PROP_USE_FIXED_FPS
50
51
52
};


Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
static guint32 gst_v4l2_formats[] = {
  /* from Linux 2.6.15 videodev2.h */
  V4L2_PIX_FMT_RGB332,
  V4L2_PIX_FMT_RGB555,
  V4L2_PIX_FMT_RGB565,
  V4L2_PIX_FMT_RGB555X,
  V4L2_PIX_FMT_RGB565X,
  V4L2_PIX_FMT_BGR24,
  V4L2_PIX_FMT_RGB24,
  V4L2_PIX_FMT_BGR32,
  V4L2_PIX_FMT_RGB32,
  V4L2_PIX_FMT_GREY,
  V4L2_PIX_FMT_YVU410,
  V4L2_PIX_FMT_YVU420,
  V4L2_PIX_FMT_YUYV,
  V4L2_PIX_FMT_UYVY,
  V4L2_PIX_FMT_YUV422P,
  V4L2_PIX_FMT_YUV411P,
  V4L2_PIX_FMT_Y41P,

  /* two planes -- one Y, one Cr + Cb interleaved  */
  V4L2_PIX_FMT_NV12,
  V4L2_PIX_FMT_NV21,

  /*  The following formats are not defined in the V4L2 specification */
  V4L2_PIX_FMT_YUV410,
  V4L2_PIX_FMT_YUV420,
  V4L2_PIX_FMT_YYUV,
  V4L2_PIX_FMT_HI240,

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

  /* compressed formats */
  V4L2_PIX_FMT_MJPEG,
  V4L2_PIX_FMT_JPEG,
  V4L2_PIX_FMT_DV,
  V4L2_PIX_FMT_MPEG,

  /*  Vendor-specific formats   */
93
94
  V4L2_PIX_FMT_WNVA
      /* V4L2_PIX_FMT_SN9C10X FIX */
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
95
96
      /* V4L2_PIX_FMT_PWC1 FIX */
      /* V4L2_PIX_FMT_PWC2 FIX */
97
};
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
98

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
99

100
101
#define GST_V4L2_FORMAT_COUNT (G_N_ELEMENTS (gst_v4l2_formats))

102

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
103
GST_BOILERPLATE (GstV4l2Src, gst_v4l2src, GstV4l2Element, GST_TYPE_V4L2ELEMENT);
104

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
105
static void gst_v4l2src_dispose (GObject * object);
106

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
107
108
109
110
111
112
/* basesrc methods */
static gboolean gst_v4l2src_start (GstBaseSrc * src);
static gboolean gst_v4l2src_stop (GstBaseSrc * src);
static gboolean gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps);
static GstCaps *gst_v4l2src_get_caps (GstBaseSrc * src);
static GstFlowReturn gst_v4l2src_create (GstPushSrc * src, GstBuffer ** out);
113

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
114
static void gst_v4l2src_fixate (GstPad * pad, GstCaps * caps);
115

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
116
117
118
static void
gst_v4l2src_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
119
120
static void gst_v4l2src_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
121
122

static GstCaps *gst_v4l2src_get_all_caps (void);
123

Ronald S. Bultje's avatar
hmm...    
Ronald S. Bultje committed
124
static void
125
gst_v4l2src_base_init (gpointer g_class)
Ronald S. Bultje's avatar
hmm...    
Ronald S. Bultje committed
126
{
127
  GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
128

129
130
  GST_DEBUG_CATEGORY_INIT (v4l2src_debug, "v4l2src", 0, "V4L2 source element");

131
  gst_element_class_set_details (gstelement_class, &gst_v4l2src_details);
Ronald S. Bultje's avatar
hmm...    
Ronald S. Bultje committed
132

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
133
134
135
136
  gst_element_class_add_pad_template
      (gstelement_class,
      gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
          gst_v4l2src_get_all_caps ()));
Ronald S. Bultje's avatar
hmm...    
Ronald S. Bultje committed
137
}
138
139

static void
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
140
gst_v4l2src_class_init (GstV4l2SrcClass * klass)
141
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
142
143
144
  GObjectClass *gobject_class;
  GstBaseSrcClass *basesrc_class;
  GstPushSrcClass *pushsrc_class;
145

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
146
147
148
  gobject_class = G_OBJECT_CLASS (klass);
  basesrc_class = GST_BASE_SRC_CLASS (klass);
  pushsrc_class = GST_PUSH_SRC_CLASS (klass);
149
150
151
152

  gobject_class->set_property = gst_v4l2src_set_property;
  gobject_class->get_property = gst_v4l2src_get_property;

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
153
154
  g_object_class_install_property
      (gobject_class, PROP_USE_FIXED_FPS,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
155
      g_param_spec_boolean ("use_fixed_fps", "Use Fixed FPS",
156
157
158
          "Drop/Insert frames to reach a certain FPS (TRUE) "
          "or adapt FPS to suit the number of frabbed frames",
          TRUE, G_PARAM_READWRITE));
159

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
160
161
162
163
  basesrc_class->get_caps = gst_v4l2src_get_caps;
  basesrc_class->set_caps = gst_v4l2src_set_caps;
  basesrc_class->start = gst_v4l2src_start;
  basesrc_class->stop = gst_v4l2src_stop;
164

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
165
  pushsrc_class->create = gst_v4l2src_create;
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
166
167
168

  gobject_class->dispose = gst_v4l2src_dispose;

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
169
}
170
171

static void
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
172
gst_v4l2src_init (GstV4l2Src * v4l2src, GstV4l2SrcClass * klass)
173
174
{

175
  v4l2src->breq.count = 0;
176

177
  v4l2src->formats = NULL;
178

179
180
  /* fps */
  v4l2src->use_fixed_fps = TRUE;
181
182

  v4l2src->is_capturing = FALSE;
183

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
184
185
  gst_pad_set_fixatecaps_function (GST_BASE_SRC_PAD (v4l2src),
      gst_v4l2src_fixate);
186

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
187
  gst_base_src_set_live (GST_BASE_SRC (v4l2src), TRUE);
188
189
}

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

static void
gst_v4l2src_dispose (GObject * object)
{
  GstV4l2Src *v4l2src = GST_V4L2SRC (object);

  if (v4l2src->formats) {
    gst_v4l2src_clear_format_list (v4l2src);
  }

  if (((GObjectClass *) parent_class)->dispose)
    ((GObjectClass *) parent_class)->dispose (object);
}


205
static void
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
206
207
gst_v4l2src_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
208
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
209
  GstV4l2Src *v4l2src;
210

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
211
212
  g_return_if_fail (GST_IS_V4L2SRC (object));
  v4l2src = GST_V4L2SRC (object);
213

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
214
215
216
217
218
219
220
221
222
223
  switch (prop_id) {
    case PROP_USE_FIXED_FPS:
      if (!GST_V4L2_IS_ACTIVE (GST_V4L2ELEMENT (v4l2src))) {
        v4l2src->use_fixed_fps = g_value_get_boolean (value);
      }
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
224
  }
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
225
}
226
227


Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
228
229
230
231
232
233
234
235
static void
gst_v4l2src_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec)
{
  GstV4l2Src *v4l2src;

  g_return_if_fail (GST_IS_V4L2SRC (object));
  v4l2src = GST_V4L2SRC (object);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
236

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
237
238
239
240
  switch (prop_id) {
    case PROP_USE_FIXED_FPS:
      g_value_set_boolean (value, v4l2src->use_fixed_fps);
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
241

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
242
243
244
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
245
  }
246
247
}

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
248
249
250

gboolean
get_fmt_width_height (GstV4l2Src * v4l2src, int *width, int *height)
251
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
252
  int ret;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
253

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
254
  struct v4l2_format format;
255

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
256
  memset (&format, 0x00, sizeof (format));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
257

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
258
  format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
259

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
260
261
262
263
264
265
  ret = ioctl (GST_V4L2ELEMENT (v4l2src)->video_fd, VIDIOC_G_FMT, &format);

  if (ret == 0) {

    *width = format.fmt.pix.width;
    *height = format.fmt.pix.height;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
266
267
268

  }

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
269
270
  return (ret == 0);

271
272
}

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
273
274
275
/* this function is a bit of a last resort */
static void
gst_v4l2src_fixate (GstPad * pad, GstCaps * caps)
276
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
277
278
279
  GstStructure *structure;
  gint i;
  G_GNUC_UNUSED gchar *caps_str;
280

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
281
282
283
  caps_str = gst_caps_to_string (caps);
  GST_DEBUG_OBJECT (GST_PAD_PARENT (pad), "fixating caps %s", caps_str);
  g_free (caps_str);
284

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
285
286
287
288
289
290
  for (i = 0; i < gst_caps_get_size (caps); ++i) {
    structure = gst_caps_get_structure (caps, i);
    const GValue *v;

    gst_structure_fixate_field_nearest_int (structure, "width", G_MAXINT);
    gst_structure_fixate_field_nearest_int (structure, "height", G_MAXINT);
291
    gst_structure_fixate_field_nearest_fraction (structure, "framerate", 15, 2);
292

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
293
294
295
296
297
298
299
300
301
302
    v = gst_structure_get_value (structure, "format");
    if (v && G_VALUE_TYPE (v) != GST_TYPE_FOURCC) {
      guint32 fourcc;

      g_return_if_fail (G_VALUE_TYPE (v) == GST_TYPE_LIST);

      fourcc = gst_value_get_fourcc (gst_value_list_get_value (v, 0));
      gst_structure_set (structure, "format", GST_TYPE_FOURCC, fourcc, NULL);
    }
  }
303
304
}

David Schleef's avatar
David Schleef committed
305
static GstStructure *
306
gst_v4l2src_v4l2fourcc_to_caps (guint32 fourcc)
307
{
308
  GstStructure *structure = NULL;
309

310
  switch (fourcc) {
311
312
    case V4L2_PIX_FMT_MJPEG:   /* Motion-JPEG */
    case V4L2_PIX_FMT_JPEG:    /* JFIF JPEG */
313
      structure = gst_structure_new ("image/jpeg", NULL);
314
315
316
317
318
319
320
321
322
      break;
    case V4L2_PIX_FMT_RGB332:
    case V4L2_PIX_FMT_RGB555:
    case V4L2_PIX_FMT_RGB555X:
    case V4L2_PIX_FMT_RGB565:
    case V4L2_PIX_FMT_RGB565X:
    case V4L2_PIX_FMT_RGB24:
    case V4L2_PIX_FMT_BGR24:
    case V4L2_PIX_FMT_RGB32:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
323
324
    case V4L2_PIX_FMT_BGR32:{
      guint depth = 0, bpp = 0;
325
326
327
328
      gint endianness = 0;
      guint32 r_mask = 0, b_mask = 0, g_mask = 0;

      switch (fourcc) {
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
        case V4L2_PIX_FMT_RGB332:
          bpp = depth = 8;
          endianness = G_BYTE_ORDER;    /* 'like, whatever' */
          r_mask = 0xe0;
          g_mask = 0x1c;
          b_mask = 0x03;
          break;
        case V4L2_PIX_FMT_RGB555:
        case V4L2_PIX_FMT_RGB555X:
          bpp = 16;
          depth = 15;
          endianness =
              fourcc == V4L2_PIX_FMT_RGB555X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
          r_mask = 0x7c00;
          g_mask = 0x03e0;
          b_mask = 0x001f;
          break;
        case V4L2_PIX_FMT_RGB565:
        case V4L2_PIX_FMT_RGB565X:
          bpp = depth = 16;
          endianness =
              fourcc == V4L2_PIX_FMT_RGB565X ? G_BIG_ENDIAN : G_LITTLE_ENDIAN;
          r_mask = 0xf800;
          g_mask = 0x07e0;
          b_mask = 0x001f;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
354
          break;
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        case V4L2_PIX_FMT_RGB24:
          bpp = depth = 24;
          endianness = G_BIG_ENDIAN;
          r_mask = 0xff0000;
          g_mask = 0x00ff00;
          b_mask = 0x0000ff;
          break;
        case V4L2_PIX_FMT_BGR24:
          bpp = depth = 24;
          endianness = G_BIG_ENDIAN;
          r_mask = 0x0000ff;
          g_mask = 0x00ff00;
          b_mask = 0xff0000;
          break;
        case V4L2_PIX_FMT_RGB32:
          bpp = depth = 32;
          endianness = G_BIG_ENDIAN;
          r_mask = 0xff000000;
          g_mask = 0x00ff0000;
          b_mask = 0x0000ff00;
          break;
        case V4L2_PIX_FMT_BGR32:
          bpp = depth = 32;
          endianness = G_BIG_ENDIAN;
          r_mask = 0x000000ff;
          g_mask = 0x0000ff00;
          b_mask = 0x00ff0000;
          break;
        default:
          g_assert_not_reached ();
          break;
386
387
      }
      structure = gst_structure_new ("video/x-raw-rgb",
388
389
390
391
392
393
          "bpp", G_TYPE_INT, bpp,
          "depth", G_TYPE_INT, depth,
          "red_mask", G_TYPE_INT, r_mask,
          "green_mask", G_TYPE_INT, g_mask,
          "blue_mask", G_TYPE_INT, b_mask,
          "endianness", G_TYPE_INT, endianness, NULL);
394
395
      break;
    }
396
397
398
399
400
    case V4L2_PIX_FMT_GREY:    /*  8  Greyscale     */
    case V4L2_PIX_FMT_NV12:    /* 12  Y/CbCr 4:2:0  */
    case V4L2_PIX_FMT_NV21:    /* 12  Y/CrCb 4:2:0  */
    case V4L2_PIX_FMT_YYUV:    /* 16  YUV 4:2:2     */
    case V4L2_PIX_FMT_HI240:   /*  8  8-bit color   */
401
402
403
404
      /* FIXME: get correct fourccs here */
      break;
    case V4L2_PIX_FMT_YVU410:
    case V4L2_PIX_FMT_YUV410:
405
    case V4L2_PIX_FMT_YUV420:  /* I420/IYUV */
406
407
408
    case V4L2_PIX_FMT_YUYV:
    case V4L2_PIX_FMT_YVU420:
    case V4L2_PIX_FMT_UYVY:
409
410
411
    case V4L2_PIX_FMT_Y41P:
    case V4L2_PIX_FMT_YUV422P:
    case V4L2_PIX_FMT_YUV411P:{
412
413
414
      guint32 fcc = 0;

      switch (fourcc) {
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
        case V4L2_PIX_FMT_YVU410:
          fcc = GST_MAKE_FOURCC ('Y', 'V', 'U', '9');
          break;
        case V4L2_PIX_FMT_YUV410:
          fcc = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
          break;
        case V4L2_PIX_FMT_YUV420:
          fcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
          break;
        case V4L2_PIX_FMT_YUYV:
          fcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
          break;
        case V4L2_PIX_FMT_YVU420:
          fcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
          break;
        case V4L2_PIX_FMT_UYVY:
          fcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
          break;
        case V4L2_PIX_FMT_Y41P:
434
435
436
          fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'P');
          break;
        case V4L2_PIX_FMT_YUV411P:
437
438
          fcc = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
          break;
439
440
441
        case V4L2_PIX_FMT_YUV422P:
          fcc = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
          break;
442
443
444
        default:
          g_assert_not_reached ();
          break;
445
446
      }
      structure = gst_structure_new ("video/x-raw-yuv",
447
          "format", GST_TYPE_FOURCC, fcc, NULL);
448
449
450
      break;
    }
    case V4L2_PIX_FMT_DV:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
451
      structure =
452
453
          gst_structure_new ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
          NULL);
454
      break;
455
    case V4L2_PIX_FMT_MPEG:    /* MPEG          */
456
457
      /* someone figure out the MPEG format used... */
      break;
458
    case V4L2_PIX_FMT_WNVA:    /* Winnov hw compres */
459
460
      break;
    default:
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
461
      GST_DEBUG ("Unknown fourcc 0x%08x " GST_FOURCC_FORMAT,
462
          fourcc, GST_FOURCC_ARGS (fourcc));
463
464
      break;
  }
465

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
466
  return structure;
467
}
468

469
static guint32
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
470
gst_v4l2_fourcc_from_structure (GstStructure * structure)
471
472
473
474
{
  guint32 fourcc = 0;
  const gchar *mimetype = gst_structure_get_name (structure);

475
476
  if (!strcmp (mimetype, "video/x-raw-yuv")) {
    gst_structure_get_fourcc (structure, "format", &fourcc);
477
478

    switch (fourcc) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
479
480
      case GST_MAKE_FOURCC ('I', '4', '2', '0'):
      case GST_MAKE_FOURCC ('I', 'Y', 'U', 'V'):
481
482
        fourcc = V4L2_PIX_FMT_YUV420;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
483
      case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
484
485
        fourcc = V4L2_PIX_FMT_YUYV;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
486
      case GST_MAKE_FOURCC ('Y', '4', '1', 'P'):
487
488
        fourcc = V4L2_PIX_FMT_Y41P;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
489
      case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
490
491
        fourcc = V4L2_PIX_FMT_UYVY;
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
492
      case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
493
494
        fourcc = V4L2_PIX_FMT_YVU420;
        break;
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
      case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
        fourcc = V4L2_PIX_FMT_YUV411P;
        break;
      case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
        fourcc = V4L2_PIX_FMT_YUV422P;
        break;
    }
  } else if (!strcmp (mimetype, "video/x-raw-rgb")) {
    gint depth, endianness, r_mask;

    gst_structure_get_int (structure, "depth", &depth);
    gst_structure_get_int (structure, "endianness", &endianness);
    gst_structure_get_int (structure, "red_mask", &r_mask);

    switch (depth) {
      case 8:
        fourcc = V4L2_PIX_FMT_RGB332;
        break;
      case 15:
        fourcc = (endianness == G_LITTLE_ENDIAN) ?
            V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB555X;
        break;
      case 16:
        fourcc = (endianness == G_LITTLE_ENDIAN) ?
            V4L2_PIX_FMT_RGB565 : V4L2_PIX_FMT_RGB565X;
        break;
      case 24:
        fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR24 : V4L2_PIX_FMT_RGB24;
        break;
      case 32:
        fourcc = (r_mask == 0xFF) ? V4L2_PIX_FMT_BGR32 : V4L2_PIX_FMT_RGB32;
526
        break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
527
    }
528
529
  } else if (strcmp (mimetype, "video/x-dv") == 0) {
    fourcc = V4L2_PIX_FMT_DV;
530
  } else if (strcmp (mimetype, "image/jpeg") == 0) {
531
532
533
534
535
    fourcc = V4L2_PIX_FMT_JPEG;
  }

  return fourcc;
}
536

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
static struct v4l2_fmtdesc *
gst_v4l2src_get_format_from_fourcc (GstV4l2Src * v4l2src, guint32 fourcc)
{
  struct v4l2_fmtdesc *fmt;
  GSList *walk;

  if (fourcc == 0)
    return NULL;

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

  return NULL;
}

563
static struct v4l2_fmtdesc *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
564
gst_v4l2_caps_to_v4l2fourcc (GstV4l2Src * v4l2src, GstStructure * structure)
565
{
566
567
568
569
  return gst_v4l2src_get_format_from_fourcc (v4l2src,
      gst_v4l2_fourcc_from_structure (structure));
}

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
570
static GstCaps *
571
572
573
574
575
576
577
578
579
580
581
582
gst_v4l2src_get_all_caps (void)
{
  static GstCaps *caps = NULL;

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

    caps = gst_caps_new_empty ();
    for (i = 0; i < GST_V4L2_FORMAT_COUNT; i++) {
      structure = gst_v4l2src_v4l2fourcc_to_caps (gst_v4l2_formats[i]);
      if (structure) {
583
584
585
        gst_structure_set (structure,
            "width", GST_TYPE_INT_RANGE, 1, 4096,
            "height", GST_TYPE_INT_RANGE, 1, 4096,
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
586
            "framerate", GST_TYPE_FRACTION_RANGE, 1, 1, 100, 1, NULL);
587
        gst_caps_append_structure (caps, structure);
588
589
590
      }
    }
  }
591

592
  return caps;
593
594
}

595
static GstCaps *
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
596
gst_v4l2src_get_caps (GstBaseSrc * src)
597
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
598
  GstV4l2Src *v4l2src = GST_V4L2SRC (src);
599
600
601
602
603
  GstCaps *caps;
  struct v4l2_fmtdesc *format;
  int min_w, max_w, min_h, max_h;
  GSList *walk;
  GstStructure *structure;
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
604
605
  gint fps_n, fps_d;

606

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
607
  if (!GST_V4L2_IS_OPEN (GST_V4L2ELEMENT (v4l2src))) {
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
608
609
610
    return
        gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
            (v4l2src)));
611
  }
David Schleef's avatar
David Schleef committed
612

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
613
614
615
  if (!v4l2src->formats)
    gst_v4l2src_fill_format_list (v4l2src);

616
  /* build our own capslist */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
617
  caps = gst_caps_new_empty ();
618
  walk = v4l2src->formats;
619
620
621
622
  if (!gst_v4l2src_get_fps (v4l2src, &fps_n, &fps_d)) {
    fps_n = 0;
    fps_d = 1;
  }
623
624
625
626
627
  while (walk) {
    format = (struct v4l2_fmtdesc *) walk->data;
    walk = g_slist_next (walk);

    /* get size delimiters */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
628
    if (!gst_v4l2src_get_size_limits (v4l2src, format,
629
            &min_w, &max_w, &min_h, &max_h)) {
630
631
      continue;
    }
632
    /* template */
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
633
634
635
636
    min_w = CLAMP (min_w, 1, 4096);
    min_h = CLAMP (min_h, 1, 4096);
    max_w = CLAMP (max_w, min_w, 4096);
    max_h = CLAMP (max_h, min_h, 4096);
637
638
639
640
641
642

    /* add to list */
    structure = gst_v4l2src_v4l2fourcc_to_caps (format->pixelformat);

    if (structure) {
      gst_structure_set (structure,
643
          "width", GST_TYPE_INT_RANGE, min_w, max_w,
644
645
646
647
648
          "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
      if (fps_n > 0) {
        gst_structure_set (structure, "framerate", GST_TYPE_FRACTION,
            fps_n, fps_d, NULL);
      }
649
650

      gst_caps_append_structure (caps, structure);
651
652
653
654
655
656

      if (fps_n <= 0) {
        gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION_RANGE,
            1, 1, 100, 1, NULL);
      }

657
658
    }
  }
659

660
  return caps;
661
662
}

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
663
664
static gboolean
gst_v4l2src_set_caps (GstBaseSrc * src, GstCaps * caps)
665
{
666
  GstV4l2Src *v4l2src;
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
667
668
669
  gint w, h;
  GstStructure *structure;
  struct v4l2_fmtdesc *format;
670

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
671
  v4l2src = GST_V4L2SRC (src);
672

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
673
674
675
676
677
678
679
680
681
682
  /* if we're not open, punt -- we'll get setcaps'd later via negotiate */
  if (!GST_V4L2_IS_OPEN (v4l2src))
    return FALSE;

  /* make sure we stop capturing and dealloc buffers */
  if (GST_V4L2_IS_ACTIVE (v4l2src)) {
    if (!gst_v4l2src_capture_stop (v4l2src))
      return FALSE;
    if (!gst_v4l2src_capture_deinit (v4l2src))
      return FALSE;
683
  }
684

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
685
686
  /* it's fixed, one struct */
  structure = gst_caps_get_structure (caps, 0);
687

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
  /* we want our own v4l2 type of fourcc codes */
  if (!(format = gst_v4l2_caps_to_v4l2fourcc (v4l2src, structure))) {
    return FALSE;
  }

  gst_structure_get_int (structure, "width", &w);
  gst_structure_get_int (structure, "height", &h);

  GST_DEBUG_OBJECT (v4l2src, "trying to set_capture %dx%d, format %s",
      w, h, format->description);
  /* this only fills in v4l2src->mmap values */
  if (!gst_v4l2src_set_capture (v4l2src, format, w, h)) {
    GST_WARNING_OBJECT (v4l2src, "could not set_capture %dx%d, format %s",
        w, h, format->description);
    return FALSE;
703
  }
704

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
705
706
707
708
709
  if (!gst_v4l2src_capture_init (v4l2src))
    return FALSE;

  if (!gst_v4l2src_capture_start (v4l2src))
    return FALSE;
710

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
711
  return TRUE;
712
}
713

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
714
715
716
717
718
719
/* start and stop are not symmetric -- start will open the device, but not start
   capture. it's setcaps that will start capture, which is called via basesrc's
   negotiate method. stop will both stop capture and close the device.
*/
static gboolean
gst_v4l2src_start (GstBaseSrc * src)
720
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
721
  GstV4l2Src *v4l2src = GST_V4L2SRC (src);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
722

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
723
724
  if (!GST_BASE_SRC_CLASS (parent_class)->start (src))
    return FALSE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
725

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
726
  v4l2src->offset = 0;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
727

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
728
729
  return TRUE;
}
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
730

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
731
732
733
734
735
736
737
738
739
740
741
static gboolean
gst_v4l2src_stop (GstBaseSrc * src)
{
  GstV4l2Src *v4l2src = GST_V4L2SRC (src);

  if (GST_V4L2_IS_ACTIVE (v4l2src) && !gst_v4l2src_capture_stop (v4l2src))
    return FALSE;

  if (GST_V4L2ELEMENT (v4l2src)->buffer != NULL) {
    if (!gst_v4l2src_capture_deinit (v4l2src))
      return FALSE;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
742
  }
743

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
744
745
  if (!GST_BASE_SRC_CLASS (parent_class)->stop (src))
    return FALSE;
746

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
747
748
749
750
751
  return TRUE;
}

static GstFlowReturn
gst_v4l2src_get_read (GstV4l2Src * v4l2src, GstBuffer ** buf)
752
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
753
754
  gint amount;
  gint buffersize;
755

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
756
  buffersize = v4l2src->format.fmt.pix.sizeimage;
757

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
758
759
760
  do {
    *buf = gst_v4l2src_buffer_new (v4l2src, buffersize, NULL, NULL);
    GST_BUFFER_OFFSET (*buf) = GST_BUFFER_OFFSET_NONE;
761

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
762
763
764
765
    amount =
        read (GST_V4L2ELEMENT (v4l2src)->video_fd, GST_BUFFER_DATA (*buf),
        buffersize);
    if (amount == buffersize) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
766
      break;
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
    } else if (amount == -1) {
      if (errno == EAGAIN || errno == EINTR) {
        continue;
      } else {
        GST_ELEMENT_ERROR (v4l2src, RESOURCE, SYNC, (NULL),
            ("error read()ing a buffer on device %s: %s",
                GST_V4L2ELEMENT (v4l2src)->videodev, g_strerror (errno)));
        gst_buffer_unref (*buf);
        return GST_FLOW_ERROR;
      }
    } else {
      GST_ELEMENT_ERROR (v4l2src, RESOURCE, SYNC, (NULL),
          ("error read()ing a buffer on device %s: got only %d bytes instead of expected %d",
              GST_V4L2ELEMENT (v4l2src)->videodev, amount, buffersize));
      gst_buffer_unref (*buf);
      return GST_FLOW_ERROR;
    }
  } while (TRUE);
785

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
786
  return GST_FLOW_OK;
787

788
789
790
}


Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
791
792
static GstFlowReturn
gst_v4l2src_get_mmap (GstV4l2Src * v4l2src, GstBuffer ** buf)
793
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
794
  gint i, num = -1;
795

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
796

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
797
798
799
800
  /* grab a frame from the device */
  num = gst_v4l2src_grab_frame (v4l2src);
  if (num == -1)
    return GST_FLOW_ERROR;
801

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
  i = v4l2src->format.fmt.pix.sizeimage;

  /* check if this is the last buffer in the queue. If so do a memcpy to put it back asap
     to avoid framedrops and deadlocks because of stupid elements */
  if (g_atomic_int_get (&v4l2src->pool->refcount) == v4l2src->breq.count) {
    GST_LOG_OBJECT (v4l2src, "using memcpy'd buffer");
    *buf = gst_v4l2src_buffer_new (v4l2src, i, NULL, NULL);
    memcpy (GST_BUFFER_DATA (*buf), v4l2src->pool->buffers[num].start, i);
    if (!gst_v4l2src_queue_frame (v4l2src, num)) {
      gst_buffer_unref (*buf);
      return GST_FLOW_ERROR;
    }
  } else {
    GST_LOG_OBJECT (v4l2src, "using mmap'd buffer");
    *buf =
        gst_v4l2src_buffer_new (v4l2src, i, v4l2src->pool->buffers[num].start,
        &v4l2src->pool->buffers[num]);
    /* no need to be careful here, both are > 0, because the element uses them */
    g_atomic_int_inc (&v4l2src->pool->buffers[num].refcount);
    g_atomic_int_inc (&v4l2src->pool->refcount);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
822
823
  }

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
824
  return GST_FLOW_OK;
825
826
}

Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
827
828
static GstFlowReturn
gst_v4l2src_create (GstPushSrc * src, GstBuffer ** buf)
829
{
Edgard Gusmão Lima's avatar
Edgard Gusmão Lima committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
  GstV4l2Src *v4l2src = GST_V4L2SRC (src);
  GstFlowReturn ret;
  gint fps_n, fps_d;

  if (v4l2src->use_fixed_fps
      && gst_v4l2src_get_fps (v4l2src, &fps_n, &fps_d) == 0) {
    GST_ELEMENT_ERROR (v4l2src, RESOURCE, SETTINGS, (NULL),
        ("could not get frame rate for element"));
    return GST_FLOW_ERROR;
  }

  if (v4l2src->breq.memory == V4L2_MEMORY_MMAP) {
    ret = gst_v4l2src_get_mmap (v4l2src, buf);
  } else {
    ret = gst_v4l2src_get_read (v4l2src, buf);
  }

  return ret;
848
}