1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/* GStreamer
* Copyright (C) 2010 David Schleef <ds@entropywave.com>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <gst/base/gstbasesrc.h>
#include "gstlinsyssdisrc.h"
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/ioctl.h>
#include "sdivideo.h"
/* prototypes */
static void gst_linsys_sdi_src_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_linsys_sdi_src_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
static void gst_linsys_sdi_src_dispose (GObject * object);
static void gst_linsys_sdi_src_finalize (GObject * object);
static GstCaps *gst_linsys_sdi_src_get_caps (GstBaseSrc * src);
static gboolean gst_linsys_sdi_src_set_caps (GstBaseSrc * src, GstCaps * caps);
static gboolean gst_linsys_sdi_src_negotiate (GstBaseSrc * src);
static gboolean gst_linsys_sdi_src_newsegment (GstBaseSrc * src);
static gboolean gst_linsys_sdi_src_start (GstBaseSrc * src);
static gboolean gst_linsys_sdi_src_stop (GstBaseSrc * src);
static void
gst_linsys_sdi_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
GstClockTime * start, GstClockTime * end);
static gboolean gst_linsys_sdi_src_get_size (GstBaseSrc * src, guint64 * size);
static gboolean gst_linsys_sdi_src_is_seekable (GstBaseSrc * src);
static gboolean gst_linsys_sdi_src_unlock (GstBaseSrc * src);
static gboolean gst_linsys_sdi_src_event (GstBaseSrc * src, GstEvent * event);
static GstFlowReturn
gst_linsys_sdi_src_create (GstBaseSrc * src, guint64 offset, guint size,
GstBuffer ** buf);
static gboolean gst_linsys_sdi_src_do_seek (GstBaseSrc * src,
GstSegment * segment);
static gboolean gst_linsys_sdi_src_query (GstBaseSrc * src, GstQuery * query);
static gboolean gst_linsys_sdi_src_check_get_range (GstBaseSrc * src);
static void gst_linsys_sdi_src_fixate (GstBaseSrc * src, GstCaps * caps);
static gboolean gst_linsys_sdi_src_unlock_stop (GstBaseSrc * src);
static gboolean
gst_linsys_sdi_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * seek,
GstSegment * segment);
enum
{
PROP_0,
PROP_DEVICE
};
#define DEFAULT_DEVICE "/dev/sdirx0"
GST_DEBUG_CATEGORY (gst_linsys_sdi_src_debug);
#define GST_CAT_DEFAULT gst_linsys_sdi_src_debug
/* pad templates */
static GstStaticPadTemplate gst_linsys_sdi_src_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-raw-yuv,format=(fourcc)UYVY,"
"width=720,height=480,pixel-aspect-ratio=10/11,"
"framerate=30000/1001,interlaced=true,"
"colorspec=sdtv,chroma-site=mpeg2")
);
/* class initialization */
GST_BOILERPLATE (GstLinsysSdiSrc, gst_linsys_sdi_src, GstBaseSrc,
GST_TYPE_BASE_SRC);
static void
gst_linsys_sdi_src_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_linsys_sdi_src_src_template));
gst_element_class_set_details_simple (element_class, "SDI video source",
"Source/Video", "Reads video from SDI capture device",
"David Schleef <ds@entropywave.com>");
}
static void
gst_linsys_sdi_src_class_init (GstLinsysSdiSrcClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstBaseSrcClass *base_src_class = GST_BASE_SRC_CLASS (klass);
GST_DEBUG_CATEGORY_INIT (gst_linsys_sdi_src_debug, "linsyssdisrc", 0,
"FIXME");
gobject_class->set_property = gst_linsys_sdi_src_set_property;
gobject_class->get_property = gst_linsys_sdi_src_get_property;
gobject_class->dispose = gst_linsys_sdi_src_dispose;
gobject_class->finalize = gst_linsys_sdi_src_finalize;
base_src_class->get_caps = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_get_caps);
base_src_class->set_caps = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_set_caps);
if (0)
base_src_class->negotiate =
GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_negotiate);
base_src_class->newsegment =
GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_newsegment);
base_src_class->start = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_start);
base_src_class->stop = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_stop);
base_src_class->get_times = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_get_times);
base_src_class->get_size = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_get_size);
base_src_class->is_seekable =
GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_is_seekable);
base_src_class->unlock = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_unlock);
base_src_class->event = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_event);
base_src_class->create = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_create);
if (0)
base_src_class->do_seek = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_do_seek);
base_src_class->query = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_query);
base_src_class->check_get_range =
GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_check_get_range);
base_src_class->fixate = GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_fixate);
base_src_class->unlock_stop =
GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_unlock_stop);
base_src_class->prepare_seek_segment =
GST_DEBUG_FUNCPTR (gst_linsys_sdi_src_prepare_seek_segment);
g_object_class_install_property (gobject_class, PROP_DEVICE,
g_param_spec_string ("device", "Device", "device to transmit data on",
DEFAULT_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}
static void
gst_linsys_sdi_src_init (GstLinsysSdiSrc * linsyssdisrc,
GstLinsysSdiSrcClass * linsyssdisrc_class)
{
gst_base_src_set_live (GST_BASE_SRC (linsyssdisrc), TRUE);
gst_base_src_set_blocksize (GST_BASE_SRC (linsyssdisrc), 720 * 480 * 2);
linsyssdisrc->device = g_strdup (DEFAULT_DEVICE);
linsyssdisrc->is_625 = FALSE;
linsyssdisrc->fd = -1;
}
void
gst_linsys_sdi_src_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstLinsysSdiSrc *linsyssdisrc;
g_return_if_fail (GST_IS_LINSYS_SDI_SRC (object));
linsyssdisrc = GST_LINSYS_SDI_SRC (object);
switch (property_id) {
case PROP_DEVICE:
g_free (linsyssdisrc->device);
linsyssdisrc->device = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_linsys_sdi_src_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstLinsysSdiSrc *linsyssdisrc;
g_return_if_fail (GST_IS_LINSYS_SDI_SRC (object));
linsyssdisrc = GST_LINSYS_SDI_SRC (object);
switch (property_id) {
case PROP_DEVICE:
g_value_set_string (value, linsyssdisrc->device);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
void
gst_linsys_sdi_src_dispose (GObject * object)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (object);
g_return_if_fail (linsyssdisrc != NULL);
/* clean up as possible. may be called multiple times */
g_free (linsyssdisrc->device);
linsyssdisrc->device = NULL;
G_OBJECT_CLASS (parent_class)->dispose (object);
}
void
gst_linsys_sdi_src_finalize (GObject * object)
{
g_return_if_fail (GST_IS_LINSYS_SDI_SRC (object));
/* clean up object here */
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static GstCaps *
gst_linsys_sdi_src_get_caps (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "get_caps");
return NULL;
}
static gboolean
gst_linsys_sdi_src_set_caps (GstBaseSrc * src, GstCaps * caps)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "set_caps");
return TRUE;
}
static gboolean
gst_linsys_sdi_src_negotiate (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "negotiate");
return TRUE;
}
static gboolean
gst_linsys_sdi_src_newsegment (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "newsegment");
return TRUE;
}
static gboolean
gst_linsys_sdi_src_start (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
int fd;
GST_DEBUG_OBJECT (linsyssdisrc, "start");
fd = open (linsyssdisrc->device, O_RDONLY);
if (fd < 0) {
GST_ERROR_OBJECT (src, "failed to open device");
return FALSE;
}
linsyssdisrc->fd = fd;
if (linsyssdisrc->is_625) {
linsyssdisrc->tmpdata = g_malloc (864 * 625 * 2);
} else {
linsyssdisrc->tmpdata = g_malloc (858 * 525 * 2);
}
linsyssdisrc->have_sync = FALSE;
return TRUE;
}
static gboolean
gst_linsys_sdi_src_stop (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "stop");
#if 0
if (linsyssdisrc->fd > 0) {
close (linsyssdisrc->fd);
linsyssdisrc->fd = -1;
}
g_free (linsyssdisrc->tmpdata);
linsyssdisrc->tmpdata = NULL;
#endif
return TRUE;
}
static void
gst_linsys_sdi_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
GstClockTime * start, GstClockTime * end)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "get_times");
}
static gboolean
gst_linsys_sdi_src_get_size (GstBaseSrc * src, guint64 * size)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "get_size");
return FALSE;
}
static gboolean
gst_linsys_sdi_src_is_seekable (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "is_seekable");
return FALSE;
}
static gboolean
gst_linsys_sdi_src_unlock (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "unlock");
return TRUE;
}
static gboolean
gst_linsys_sdi_src_event (GstBaseSrc * src, GstEvent * event)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "event");
return TRUE;
}
static void
sdi_demux (guint8 * data, GstBuffer * buf, gboolean is_625)
{
int j;
int line;
int offset;
if (is_625) {
offset = (864 - 720) / 2;
for (j = 0; j < 480; j++) {
if (j & 1) {
line = 23 + (j - 1) / 2;
} else {
line = 335 + j / 2;
}
memcpy (GST_BUFFER_DATA (buf) + j * 720 * 2,
data + (line - 1) * 864 * 2 + offset * 4, 720 * 2);
}
} else {
offset = (858 - 720) / 2;
for (j = 0; j < 480; j++) {
if (j & 1) {
line = 23 + (j - 1) / 2;
} else {
line = 285 + j / 2;
}
memcpy (GST_BUFFER_DATA (buf) + j * 720 * 2,
data + (line - 1) * 858 * 2 + offset * 4, 720 * 2);
}
}
}
static GstFlowReturn
gst_linsys_sdi_src_create (GstBaseSrc * src, guint64 _offset, guint size,
GstBuffer ** buf)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
int offset;
int ret;
struct pollfd pfd;
int sdi_size;
int sdi_width;
guint8 *data = linsyssdisrc->tmpdata;
if (linsyssdisrc->fd < 0)
return GST_FLOW_WRONG_STATE;
if (linsyssdisrc->is_625) {
sdi_width = 864;
sdi_size = 864 * 625 * 2;
} else {
sdi_width = 858;
sdi_size = 858 * 525 * 2;
}
GST_DEBUG_OBJECT (linsyssdisrc, "create size=%d fd=%d", size,
linsyssdisrc->fd);
offset = 0;
while (offset < sdi_size) {
pfd.fd = linsyssdisrc->fd;
pfd.events = POLLIN | POLLPRI;
ret = poll (&pfd, 1, 1000);
if (ret < 0) {
GST_ERROR_OBJECT (src, "poll failed %d", ret);
return GST_FLOW_ERROR;
}
if (pfd.revents & POLLIN) {
if (linsyssdisrc->have_sync) {
ret = read (linsyssdisrc->fd, data + offset, sdi_size - offset);
} else {
ret = read (linsyssdisrc->fd, data + offset, sdi_width * 2);
}
if (ret < 0) {
GST_ERROR_OBJECT (src, "read failed %d", ret);
return GST_FLOW_ERROR;
}
if (!linsyssdisrc->have_sync) {
int v = (data[3] >> 5) & 1;
int f = (data[3] >> 6) & 1;
if (!linsyssdisrc->have_vblank && (f == 0) && (v == 1)) {
linsyssdisrc->have_vblank = TRUE;
} else if (linsyssdisrc->have_vblank && (f == 0) && (v == 0)) {
offset += sdi_width * 2 * 9;
linsyssdisrc->have_sync = TRUE;
offset += ret;
}
} else {
offset += ret;
}
}
if (pfd.revents & POLLPRI) {
long val;
ret = ioctl (linsyssdisrc->fd, SDIVIDEO_IOC_RXGETEVENTS, &val);
if (ret < 0) {
GST_ERROR_OBJECT (src, "ioctl failed %d", ret);
return GST_FLOW_ERROR;
}
if (val & SDIVIDEO_EVENT_RX_BUFFER) {
GST_ERROR_OBJECT (src, "receive buffer overrun");
return GST_FLOW_ERROR;
}
if (val & SDIVIDEO_EVENT_RX_FIFO) {
GST_ERROR_OBJECT (src, "receive FIFO overrun");
return GST_FLOW_ERROR;
}
if (val & SDIVIDEO_EVENT_RX_CARRIER) {
GST_ERROR_OBJECT (src, "carrier status change");
}
}
}
*buf = gst_buffer_new_and_alloc (size);
sdi_demux (data, *buf, linsyssdisrc->is_625);
return GST_FLOW_OK;
}
static gboolean
gst_linsys_sdi_src_do_seek (GstBaseSrc * src, GstSegment * segment)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "do_seek");
return FALSE;
}
static gboolean
gst_linsys_sdi_src_query (GstBaseSrc * src, GstQuery * query)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "query");
return TRUE;
}
static gboolean
gst_linsys_sdi_src_check_get_range (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "get_range");
return FALSE;
}
static void
gst_linsys_sdi_src_fixate (GstBaseSrc * src, GstCaps * caps)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "fixate");
}
static gboolean
gst_linsys_sdi_src_unlock_stop (GstBaseSrc * src)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "stop");
return TRUE;
}
static gboolean
gst_linsys_sdi_src_prepare_seek_segment (GstBaseSrc * src, GstEvent * seek,
GstSegment * segment)
{
GstLinsysSdiSrc *linsyssdisrc = GST_LINSYS_SDI_SRC (src);
GST_DEBUG_OBJECT (linsyssdisrc, "seek_segment");
return FALSE;
}
|