1 /*
2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "gstv4l2object.h"
33 #include "gstv4l2transform.h"
34
35 #include <string.h>
36 #include <gst/gst-i18n-plugin.h>
37
38 #define DEFAULT_PROP_DEVICE "/dev/video10"
39
40 #define V4L2_TRANSFORM_QUARK \
41 g_quark_from_static_string("gst-v4l2-transform-info")
42
43 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_transform_debug);
44 #define GST_CAT_DEFAULT gst_v4l2_transform_debug
45
46
47 enum
48 {
49 PROP_0,
50 V4L2_STD_OBJECT_PROPS,
51 PROP_DISABLE_PASSTHROUGH
52 };
53
54 typedef struct
55 {
56 gchar *device;
57 GstCaps *sink_caps;
58 GstCaps *src_caps;
59 } GstV4l2TransformCData;
60
61 #define gst_v4l2_transform_parent_class parent_class
62 G_DEFINE_ABSTRACT_TYPE (GstV4l2Transform, gst_v4l2_transform,
63 GST_TYPE_BASE_TRANSFORM);
64
65 static void
gst_v4l2_transform_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)66 gst_v4l2_transform_set_property (GObject * object,
67 guint prop_id, const GValue * value, GParamSpec * pspec)
68 {
69 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
70
71 switch (prop_id) {
72 case PROP_CAPTURE_IO_MODE:
73 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
74 pspec);
75 break;
76 case PROP_DISABLE_PASSTHROUGH:
77 self->disable_passthrough = g_value_get_boolean (value);
78 break;
79
80 /* By default, only set on output */
81 default:
82 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
83 prop_id, value, pspec)) {
84 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 }
86 break;
87 }
88 }
89
90 static void
gst_v4l2_transform_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)91 gst_v4l2_transform_get_property (GObject * object,
92 guint prop_id, GValue * value, GParamSpec * pspec)
93 {
94 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
95
96 switch (prop_id) {
97 case PROP_CAPTURE_IO_MODE:
98 gst_v4l2_object_get_property_helper (self->v4l2capture, prop_id, value,
99 pspec);
100 break;
101 case PROP_DISABLE_PASSTHROUGH:
102 g_value_set_boolean (value, self->disable_passthrough);
103 break;
104
105 /* By default read from output */
106 default:
107 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
108 prop_id, value, pspec)) {
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
110 }
111 break;
112 }
113 }
114
115 static gboolean
gst_v4l2_transform_open(GstV4l2Transform * self)116 gst_v4l2_transform_open (GstV4l2Transform * self)
117 {
118 GstV4l2Error error = GST_V4L2_ERROR_INIT;
119
120 GST_DEBUG_OBJECT (self, "Opening");
121
122 if (!gst_v4l2_object_open (self->v4l2output, &error))
123 goto failure;
124
125 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
126 goto failure;
127
128 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
129 gst_v4l2_object_get_raw_caps ());
130
131 if (gst_caps_is_empty (self->probed_sinkcaps))
132 goto no_input_format;
133
134 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
135 gst_v4l2_object_get_raw_caps ());
136
137 if (gst_caps_is_empty (self->probed_srccaps))
138 goto no_output_format;
139
140 return TRUE;
141
142 no_input_format:
143 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
144 (_("Converter on device %s has no supported input format"),
145 self->v4l2output->videodev), (NULL));
146 goto failure;
147
148
149 no_output_format:
150 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
151 (_("Converter on device %s has no supported output format"),
152 self->v4l2output->videodev), (NULL));
153 goto failure;
154
155 failure:
156 if (GST_V4L2_IS_OPEN (self->v4l2output))
157 gst_v4l2_object_close (self->v4l2output);
158
159 if (GST_V4L2_IS_OPEN (self->v4l2capture))
160 gst_v4l2_object_close (self->v4l2capture);
161
162 gst_caps_replace (&self->probed_srccaps, NULL);
163 gst_caps_replace (&self->probed_sinkcaps, NULL);
164
165 gst_v4l2_error (self, &error);
166
167 return FALSE;
168 }
169
170 static void
gst_v4l2_transform_close(GstV4l2Transform * self)171 gst_v4l2_transform_close (GstV4l2Transform * self)
172 {
173 GST_DEBUG_OBJECT (self, "Closing");
174
175 gst_v4l2_object_close (self->v4l2output);
176 gst_v4l2_object_close (self->v4l2capture);
177
178 gst_caps_replace (&self->probed_srccaps, NULL);
179 gst_caps_replace (&self->probed_sinkcaps, NULL);
180 }
181
182 static gboolean
gst_v4l2_transform_stop(GstBaseTransform * trans)183 gst_v4l2_transform_stop (GstBaseTransform * trans)
184 {
185 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
186
187 GST_DEBUG_OBJECT (self, "Stop");
188
189 gst_v4l2_object_stop (self->v4l2output);
190 gst_v4l2_object_stop (self->v4l2capture);
191 gst_caps_replace (&self->incaps, NULL);
192 gst_caps_replace (&self->outcaps, NULL);
193
194 return TRUE;
195 }
196
197 static gboolean
gst_v4l2_transform_set_caps(GstBaseTransform * trans,GstCaps * incaps,GstCaps * outcaps)198 gst_v4l2_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps,
199 GstCaps * outcaps)
200 {
201 GstV4l2Error error = GST_V4L2_ERROR_INIT;
202 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
203
204 if (self->disable_passthrough)
205 gst_base_transform_set_passthrough (trans, FALSE);
206
207 if (self->incaps && self->outcaps) {
208 if (gst_caps_is_equal (incaps, self->incaps) &&
209 gst_caps_is_equal (outcaps, self->outcaps)) {
210 GST_DEBUG_OBJECT (trans, "Caps did not changed");
211 return TRUE;
212 }
213 }
214
215 gst_v4l2_object_stop (self->v4l2output);
216 gst_v4l2_object_stop (self->v4l2capture);
217
218 if (!gst_v4l2_object_set_format (self->v4l2output, incaps, &error))
219 goto incaps_failed;
220
221 if (!gst_v4l2_object_set_format (self->v4l2capture, outcaps, &error))
222 goto outcaps_failed;
223
224 gst_caps_replace (&self->incaps, incaps);
225 gst_caps_replace (&self->outcaps, outcaps);
226
227 /* FIXME implement fallback if crop not supported */
228 if (!gst_v4l2_object_setup_padding (self->v4l2output))
229 goto failed;
230
231 if (!gst_v4l2_object_setup_padding (self->v4l2capture))
232 goto failed;
233
234 return TRUE;
235
236 incaps_failed:
237 {
238 GST_ERROR_OBJECT (self, "failed to set input caps: %" GST_PTR_FORMAT,
239 incaps);
240 gst_v4l2_error (self, &error);
241 goto failed;
242 }
243 outcaps_failed:
244 {
245 gst_v4l2_object_stop (self->v4l2output);
246 GST_ERROR_OBJECT (self, "failed to set output caps: %" GST_PTR_FORMAT,
247 outcaps);
248 gst_v4l2_error (self, &error);
249 goto failed;
250 }
251 failed:
252 return FALSE;
253 }
254
255 static gboolean
gst_v4l2_transform_query(GstBaseTransform * trans,GstPadDirection direction,GstQuery * query)256 gst_v4l2_transform_query (GstBaseTransform * trans, GstPadDirection direction,
257 GstQuery * query)
258 {
259 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
260 gboolean ret = TRUE;
261
262 switch (GST_QUERY_TYPE (query)) {
263 case GST_QUERY_CAPS:{
264 GstCaps *filter, *caps = NULL, *result = NULL;
265 GstPad *pad, *otherpad;
266
267 gst_query_parse_caps (query, &filter);
268
269 if (direction == GST_PAD_SRC) {
270 pad = GST_BASE_TRANSFORM_SRC_PAD (trans);
271 otherpad = GST_BASE_TRANSFORM_SINK_PAD (trans);
272 if (self->probed_srccaps)
273 caps = gst_caps_ref (self->probed_srccaps);
274 } else {
275 pad = GST_BASE_TRANSFORM_SINK_PAD (trans);
276 otherpad = GST_BASE_TRANSFORM_SRC_PAD (trans);
277 if (self->probed_sinkcaps)
278 caps = gst_caps_ref (self->probed_sinkcaps);
279 }
280
281 if (!caps)
282 caps = gst_pad_get_pad_template_caps (pad);
283
284 if (filter) {
285 GstCaps *tmp = caps;
286 caps = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
287 gst_caps_unref (tmp);
288 }
289
290 result = gst_pad_peer_query_caps (otherpad, caps);
291 result = gst_caps_make_writable (result);
292 gst_caps_append (result, caps);
293
294 GST_DEBUG_OBJECT (self, "Returning %s caps %" GST_PTR_FORMAT,
295 GST_PAD_NAME (pad), result);
296
297 gst_query_set_caps_result (query, result);
298 gst_caps_unref (result);
299 break;
300 }
301
302 default:
303 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
304 query);
305 break;
306 }
307
308 return ret;
309 }
310
311 static gboolean
gst_v4l2_transform_decide_allocation(GstBaseTransform * trans,GstQuery * query)312 gst_v4l2_transform_decide_allocation (GstBaseTransform * trans,
313 GstQuery * query)
314 {
315 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
316 gboolean ret = FALSE;
317
318 GST_DEBUG_OBJECT (self, "called");
319
320 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query)) {
321 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2capture->pool);
322
323 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
324 query);
325
326 if (!gst_buffer_pool_set_active (pool, TRUE))
327 goto activate_failed;
328 }
329
330 return ret;
331
332 activate_failed:
333 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
334 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
335 return TRUE;
336 }
337
338 static gboolean
gst_v4l2_transform_propose_allocation(GstBaseTransform * trans,GstQuery * decide_query,GstQuery * query)339 gst_v4l2_transform_propose_allocation (GstBaseTransform * trans,
340 GstQuery * decide_query, GstQuery * query)
341 {
342 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
343 gboolean ret = FALSE;
344
345 GST_DEBUG_OBJECT (self, "called");
346
347 if (decide_query == NULL)
348 ret = TRUE;
349 else
350 ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
351
352 if (ret)
353 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
354 decide_query, query);
355
356 return ret;
357 }
358
359 /* copies the given caps */
360 static GstCaps *
gst_v4l2_transform_caps_remove_format_info(GstCaps * caps)361 gst_v4l2_transform_caps_remove_format_info (GstCaps * caps)
362 {
363 GstStructure *st;
364 GstCapsFeatures *f;
365 gint i, n;
366 GstCaps *res;
367
368 res = gst_caps_new_empty ();
369
370 n = gst_caps_get_size (caps);
371 for (i = 0; i < n; i++) {
372 st = gst_caps_get_structure (caps, i);
373 f = gst_caps_get_features (caps, i);
374
375 /* If this is already expressed by the existing caps
376 * skip this structure */
377 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
378 continue;
379
380 st = gst_structure_copy (st);
381 /* Only remove format info for the cases when we can actually convert */
382 if (!gst_caps_features_is_any (f)
383 && gst_caps_features_is_equal (f,
384 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
385 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
386 "width", "height", "pixel-aspect-ratio", NULL);
387
388 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
389 }
390
391 return res;
392 }
393
394 /* The caps can be transformed into any other caps with format info removed.
395 * However, we should prefer passthrough, so if passthrough is possible,
396 * put it first in the list. */
397 static GstCaps *
gst_v4l2_transform_transform_caps(GstBaseTransform * btrans,GstPadDirection direction,GstCaps * caps,GstCaps * filter)398 gst_v4l2_transform_transform_caps (GstBaseTransform * btrans,
399 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
400 {
401 GstCaps *tmp, *tmp2;
402 GstCaps *result;
403
404 /* Get all possible caps that we can transform to */
405 tmp = gst_v4l2_transform_caps_remove_format_info (caps);
406
407 if (filter) {
408 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
409 gst_caps_unref (tmp);
410 tmp = tmp2;
411 }
412
413 result = tmp;
414
415 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
416 GST_PTR_FORMAT, caps, result);
417
418 return result;
419 }
420
421 static GstCaps *
gst_v4l2_transform_fixate_caps(GstBaseTransform * trans,GstPadDirection direction,GstCaps * caps,GstCaps * othercaps)422 gst_v4l2_transform_fixate_caps (GstBaseTransform * trans,
423 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
424 {
425 GstStructure *ins, *outs;
426 const GValue *from_par, *to_par;
427 GValue fpar = { 0, }, tpar = {
428 0,};
429
430 othercaps = gst_caps_truncate (othercaps);
431 othercaps = gst_caps_make_writable (othercaps);
432
433 GST_DEBUG_OBJECT (trans, "trying to fixate othercaps %" GST_PTR_FORMAT
434 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
435
436 ins = gst_caps_get_structure (caps, 0);
437 outs = gst_caps_get_structure (othercaps, 0);
438
439 {
440 const gchar *in_format;
441
442 in_format = gst_structure_get_string (ins, "format");
443 if (in_format) {
444 /* Try to set output format for pass through */
445 gst_structure_fixate_field_string (outs, "format", in_format);
446 }
447
448 }
449
450 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
451 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
452
453 /* If we're fixating from the sinkpad we always set the PAR and
454 * assume that missing PAR on the sinkpad means 1/1 and
455 * missing PAR on the srcpad means undefined
456 */
457 if (direction == GST_PAD_SINK) {
458 if (!from_par) {
459 g_value_init (&fpar, GST_TYPE_FRACTION);
460 gst_value_set_fraction (&fpar, 1, 1);
461 from_par = &fpar;
462 }
463 if (!to_par) {
464 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
465 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
466 to_par = &tpar;
467 }
468 } else {
469 if (!to_par) {
470 g_value_init (&tpar, GST_TYPE_FRACTION);
471 gst_value_set_fraction (&tpar, 1, 1);
472 to_par = &tpar;
473
474 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
475 NULL);
476 }
477 if (!from_par) {
478 g_value_init (&fpar, GST_TYPE_FRACTION);
479 gst_value_set_fraction (&fpar, 1, 1);
480 from_par = &fpar;
481 }
482 }
483
484 /* we have both PAR but they might not be fixated */
485 {
486 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
487 gint w = 0, h = 0;
488 gint from_dar_n, from_dar_d;
489 gint num, den;
490
491 /* from_par should be fixed */
492 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
493
494 from_par_n = gst_value_get_fraction_numerator (from_par);
495 from_par_d = gst_value_get_fraction_denominator (from_par);
496
497 gst_structure_get_int (ins, "width", &from_w);
498 gst_structure_get_int (ins, "height", &from_h);
499
500 gst_structure_get_int (outs, "width", &w);
501 gst_structure_get_int (outs, "height", &h);
502
503 /* if both width and height are already fixed, we can't do anything
504 * about it anymore */
505 if (w && h) {
506 guint n, d;
507
508 GST_DEBUG_OBJECT (trans, "dimensions already set to %dx%d, not fixating",
509 w, h);
510 if (!gst_value_is_fixed (to_par)) {
511 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
512 from_par_n, from_par_d, w, h)) {
513 GST_DEBUG_OBJECT (trans, "fixating to_par to %dx%d", n, d);
514 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
515 gst_structure_fixate_field_nearest_fraction (outs,
516 "pixel-aspect-ratio", n, d);
517 else if (n != d)
518 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
519 n, d, NULL);
520 }
521 }
522 goto done;
523 }
524
525 /* Calculate input DAR */
526 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
527 &from_dar_n, &from_dar_d)) {
528 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
529 ("Error calculating the output scaled size - integer overflow"));
530 goto done;
531 }
532
533 GST_DEBUG_OBJECT (trans, "Input DAR is %d/%d", from_dar_n, from_dar_d);
534
535 /* If either width or height are fixed there's not much we
536 * can do either except choosing a height or width and PAR
537 * that matches the DAR as good as possible
538 */
539 if (h) {
540 GstStructure *tmp;
541 gint set_w, set_par_n, set_par_d;
542
543 GST_DEBUG_OBJECT (trans, "height is fixed (%d)", h);
544
545 /* If the PAR is fixed too, there's not much to do
546 * except choosing the width that is nearest to the
547 * width with the same DAR */
548 if (gst_value_is_fixed (to_par)) {
549 to_par_n = gst_value_get_fraction_numerator (to_par);
550 to_par_d = gst_value_get_fraction_denominator (to_par);
551
552 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
553
554 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
555 to_par_n, &num, &den)) {
556 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
557 ("Error calculating the output scaled size - integer overflow"));
558 goto done;
559 }
560
561 w = (guint) gst_util_uint64_scale_int (h, num, den);
562 gst_structure_fixate_field_nearest_int (outs, "width", w);
563
564 goto done;
565 }
566
567 /* The PAR is not fixed and it's quite likely that we can set
568 * an arbitrary PAR. */
569
570 /* Check if we can keep the input width */
571 tmp = gst_structure_copy (outs);
572 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
573 gst_structure_get_int (tmp, "width", &set_w);
574
575 /* Might have failed but try to keep the DAR nonetheless by
576 * adjusting the PAR */
577 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
578 &to_par_n, &to_par_d)) {
579 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
580 ("Error calculating the output scaled size - integer overflow"));
581 gst_structure_free (tmp);
582 goto done;
583 }
584
585 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
586 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
587 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
588 to_par_n, to_par_d);
589 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
590 &set_par_d);
591 gst_structure_free (tmp);
592
593 /* Check if the adjusted PAR is accepted */
594 if (set_par_n == to_par_n && set_par_d == to_par_d) {
595 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
596 set_par_n != set_par_d)
597 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
598 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
599 NULL);
600 goto done;
601 }
602
603 /* Otherwise scale the width to the new PAR and check if the
604 * adjusted with is accepted. If all that fails we can't keep
605 * the DAR */
606 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
607 set_par_n, &num, &den)) {
608 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
609 ("Error calculating the output scaled size - integer overflow"));
610 goto done;
611 }
612
613 w = (guint) gst_util_uint64_scale_int (h, num, den);
614 gst_structure_fixate_field_nearest_int (outs, "width", w);
615 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
616 set_par_n != set_par_d)
617 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
618 set_par_n, set_par_d, NULL);
619
620 goto done;
621 } else if (w) {
622 GstStructure *tmp;
623 gint set_h, set_par_n, set_par_d;
624
625 GST_DEBUG_OBJECT (trans, "width is fixed (%d)", w);
626
627 /* If the PAR is fixed too, there's not much to do
628 * except choosing the height that is nearest to the
629 * height with the same DAR */
630 if (gst_value_is_fixed (to_par)) {
631 to_par_n = gst_value_get_fraction_numerator (to_par);
632 to_par_d = gst_value_get_fraction_denominator (to_par);
633
634 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
635
636 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
637 to_par_n, &num, &den)) {
638 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
639 ("Error calculating the output scaled size - integer overflow"));
640 goto done;
641 }
642
643 h = (guint) gst_util_uint64_scale_int (w, den, num);
644 gst_structure_fixate_field_nearest_int (outs, "height", h);
645
646 goto done;
647 }
648
649 /* The PAR is not fixed and it's quite likely that we can set
650 * an arbitrary PAR. */
651
652 /* Check if we can keep the input height */
653 tmp = gst_structure_copy (outs);
654 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
655 gst_structure_get_int (tmp, "height", &set_h);
656
657 /* Might have failed but try to keep the DAR nonetheless by
658 * adjusting the PAR */
659 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
660 &to_par_n, &to_par_d)) {
661 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
662 ("Error calculating the output scaled size - integer overflow"));
663 gst_structure_free (tmp);
664 goto done;
665 }
666 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
667 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
668 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
669 to_par_n, to_par_d);
670 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
671 &set_par_d);
672 gst_structure_free (tmp);
673
674 /* Check if the adjusted PAR is accepted */
675 if (set_par_n == to_par_n && set_par_d == to_par_d) {
676 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
677 set_par_n != set_par_d)
678 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
679 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
680 NULL);
681 goto done;
682 }
683
684 /* Otherwise scale the height to the new PAR and check if the
685 * adjusted with is accepted. If all that fails we can't keep
686 * the DAR */
687 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
688 set_par_n, &num, &den)) {
689 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
690 ("Error calculating the output scaled size - integer overflow"));
691 goto done;
692 }
693
694 h = (guint) gst_util_uint64_scale_int (w, den, num);
695 gst_structure_fixate_field_nearest_int (outs, "height", h);
696 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
697 set_par_n != set_par_d)
698 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
699 set_par_n, set_par_d, NULL);
700
701 goto done;
702 } else if (gst_value_is_fixed (to_par)) {
703 GstStructure *tmp;
704 gint set_h, set_w, f_h, f_w;
705
706 to_par_n = gst_value_get_fraction_numerator (to_par);
707 to_par_d = gst_value_get_fraction_denominator (to_par);
708
709 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
710
711 /* Calculate scale factor for the PAR change */
712 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
713 to_par_n, &num, &den)) {
714 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
715 ("Error calculating the output scaled size - integer overflow"));
716 goto done;
717 }
718
719 /* Try to keep the input height (because of interlacing) */
720 tmp = gst_structure_copy (outs);
721 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
722 gst_structure_get_int (tmp, "height", &set_h);
723
724 /* This might have failed but try to scale the width
725 * to keep the DAR nonetheless */
726 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
727 gst_structure_fixate_field_nearest_int (tmp, "width", w);
728 gst_structure_get_int (tmp, "width", &set_w);
729 gst_structure_free (tmp);
730
731 /* We kept the DAR and the height is nearest to the original height */
732 if (set_w == w) {
733 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
734 G_TYPE_INT, set_h, NULL);
735 goto done;
736 }
737
738 f_h = set_h;
739 f_w = set_w;
740
741 /* If the former failed, try to keep the input width at least */
742 tmp = gst_structure_copy (outs);
743 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
744 gst_structure_get_int (tmp, "width", &set_w);
745
746 /* This might have failed but try to scale the width
747 * to keep the DAR nonetheless */
748 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
749 gst_structure_fixate_field_nearest_int (tmp, "height", h);
750 gst_structure_get_int (tmp, "height", &set_h);
751 gst_structure_free (tmp);
752
753 /* We kept the DAR and the width is nearest to the original width */
754 if (set_h == h) {
755 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
756 G_TYPE_INT, set_h, NULL);
757 goto done;
758 }
759
760 /* If all this failed, keep the height that was nearest to the original
761 * height and the nearest possible width. This changes the DAR but
762 * there's not much else to do here.
763 */
764 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
765 f_h, NULL);
766 goto done;
767 } else {
768 GstStructure *tmp;
769 gint set_h, set_w, set_par_n, set_par_d, tmp2;
770
771 /* width, height and PAR are not fixed but passthrough is not possible */
772
773 /* First try to keep the height and width as good as possible
774 * and scale PAR */
775 tmp = gst_structure_copy (outs);
776 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
777 gst_structure_get_int (tmp, "height", &set_h);
778 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
779 gst_structure_get_int (tmp, "width", &set_w);
780
781 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
782 &to_par_n, &to_par_d)) {
783 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
784 ("Error calculating the output scaled size - integer overflow"));
785 gst_structure_free (tmp);
786 goto done;
787 }
788
789 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
790 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
791 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
792 to_par_n, to_par_d);
793 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
794 &set_par_d);
795 gst_structure_free (tmp);
796
797 if (set_par_n == to_par_n && set_par_d == to_par_d) {
798 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
799 G_TYPE_INT, set_h, NULL);
800
801 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
802 set_par_n != set_par_d)
803 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
804 set_par_n, set_par_d, NULL);
805 goto done;
806 }
807
808 /* Otherwise try to scale width to keep the DAR with the set
809 * PAR and height */
810 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
811 set_par_n, &num, &den)) {
812 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
813 ("Error calculating the output scaled size - integer overflow"));
814 goto done;
815 }
816
817 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
818 tmp = gst_structure_copy (outs);
819 gst_structure_fixate_field_nearest_int (tmp, "width", w);
820 gst_structure_get_int (tmp, "width", &tmp2);
821 gst_structure_free (tmp);
822
823 if (tmp2 == w) {
824 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
825 G_TYPE_INT, set_h, NULL);
826 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
827 set_par_n != set_par_d)
828 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
829 set_par_n, set_par_d, NULL);
830 goto done;
831 }
832
833 /* ... or try the same with the height */
834 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
835 tmp = gst_structure_copy (outs);
836 gst_structure_fixate_field_nearest_int (tmp, "height", h);
837 gst_structure_get_int (tmp, "height", &tmp2);
838 gst_structure_free (tmp);
839
840 if (tmp2 == h) {
841 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
842 G_TYPE_INT, tmp2, NULL);
843 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
844 set_par_n != set_par_d)
845 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
846 set_par_n, set_par_d, NULL);
847 goto done;
848 }
849
850 /* If all fails we can't keep the DAR and take the nearest values
851 * for everything from the first try */
852 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
853 G_TYPE_INT, set_h, NULL);
854 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
855 set_par_n != set_par_d)
856 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
857 set_par_n, set_par_d, NULL);
858 }
859 }
860
861 done:
862 GST_DEBUG_OBJECT (trans, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
863
864 if (from_par == &fpar)
865 g_value_unset (&fpar);
866 if (to_par == &tpar)
867 g_value_unset (&tpar);
868
869 /* fixate remaining fields */
870 othercaps = gst_caps_fixate (othercaps);
871
872 if (direction == GST_PAD_SINK) {
873 if (gst_caps_is_subset (caps, othercaps)) {
874 gst_caps_replace (&othercaps, caps);
875 }
876 }
877
878 return othercaps;
879 }
880
881 static GstFlowReturn
gst_v4l2_transform_prepare_output_buffer(GstBaseTransform * trans,GstBuffer * inbuf,GstBuffer ** outbuf)882 gst_v4l2_transform_prepare_output_buffer (GstBaseTransform * trans,
883 GstBuffer * inbuf, GstBuffer ** outbuf)
884 {
885 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
886 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
887 GstFlowReturn ret = GST_FLOW_OK;
888 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_CLASS (parent_class);
889
890 if (gst_base_transform_is_passthrough (trans)) {
891 GST_DEBUG_OBJECT (self, "Passthrough, no need to do anything");
892 *outbuf = inbuf;
893 goto beach;
894 }
895
896 /* Ensure input internal pool is active */
897 if (!gst_buffer_pool_is_active (pool)) {
898 GstStructure *config = gst_buffer_pool_get_config (pool);
899 gint min = MAX (GST_V4L2_MIN_BUFFERS (self->v4l2output),
900 self->v4l2output->min_buffers);
901
902 if (self->v4l2output->mode == GST_V4L2_IO_USERPTR ||
903 self->v4l2output->mode == GST_V4L2_IO_DMABUF_IMPORT) {
904 if (!gst_v4l2_object_try_import (self->v4l2output, inbuf)) {
905 GST_ERROR_OBJECT (self, "cannot import buffers from upstream");
906 return GST_FLOW_ERROR;
907 }
908
909 if (self->v4l2output->need_video_meta) {
910 /* We may need video meta if imported buffer is using non-standard
911 * stride/padding */
912 gst_buffer_pool_config_add_option (config,
913 GST_BUFFER_POOL_OPTION_VIDEO_META);
914 }
915 }
916
917 gst_buffer_pool_config_set_params (config, self->incaps,
918 self->v4l2output->info.size, min, min);
919
920 /* There is no reason to refuse this config */
921 if (!gst_buffer_pool_set_config (pool, config))
922 goto activate_failed;
923
924 if (!gst_buffer_pool_set_active (pool, TRUE))
925 goto activate_failed;
926 }
927
928 GST_DEBUG_OBJECT (self, "Queue input buffer");
929 ret =
930 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), &inbuf, NULL);
931 if (G_UNLIKELY (ret != GST_FLOW_OK))
932 goto beach;
933
934 do {
935 pool = gst_base_transform_get_buffer_pool (trans);
936
937 if (!gst_buffer_pool_set_active (pool, TRUE))
938 goto activate_failed;
939
940 GST_DEBUG_OBJECT (self, "Dequeue output buffer");
941 ret = gst_buffer_pool_acquire_buffer (pool, outbuf, NULL);
942 g_object_unref (pool);
943
944 if (ret != GST_FLOW_OK)
945 goto alloc_failed;
946
947 pool = self->v4l2capture->pool;
948 ret =
949 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), outbuf,
950 NULL);
951
952 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
953
954 if (ret != GST_FLOW_OK) {
955 gst_buffer_unref (*outbuf);
956 *outbuf = NULL;
957 }
958
959 if (bclass->copy_metadata)
960 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
961 /* something failed, post a warning */
962 GST_ELEMENT_WARNING (self, STREAM, NOT_IMPLEMENTED,
963 ("could not copy metadata"), (NULL));
964 }
965
966 beach:
967 return ret;
968
969 activate_failed:
970 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
971 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
972 g_object_unref (pool);
973 return GST_FLOW_ERROR;
974
975 alloc_failed:
976 GST_DEBUG_OBJECT (self, "could not allocate buffer from pool");
977 return ret;
978 }
979
980 static GstFlowReturn
gst_v4l2_transform_transform(GstBaseTransform * trans,GstBuffer * inbuf,GstBuffer * outbuf)981 gst_v4l2_transform_transform (GstBaseTransform * trans, GstBuffer * inbuf,
982 GstBuffer * outbuf)
983 {
984 /* Nothing to do */
985 return GST_FLOW_OK;
986 }
987
988 static gboolean
gst_v4l2_transform_sink_event(GstBaseTransform * trans,GstEvent * event)989 gst_v4l2_transform_sink_event (GstBaseTransform * trans, GstEvent * event)
990 {
991 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
992 gboolean ret;
993 GstEventType type = GST_EVENT_TYPE (event);
994
995 /* Nothing to flush in passthrough */
996 if (gst_base_transform_is_passthrough (trans))
997 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
998
999 switch (type) {
1000 case GST_EVENT_FLUSH_START:
1001 GST_DEBUG_OBJECT (self, "flush start");
1002 gst_v4l2_object_unlock (self->v4l2output);
1003 gst_v4l2_object_unlock (self->v4l2capture);
1004 break;
1005 default:
1006 break;
1007 }
1008
1009 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
1010
1011 switch (type) {
1012 case GST_EVENT_FLUSH_STOP:
1013 /* Buffer should be back now */
1014 GST_DEBUG_OBJECT (self, "flush stop");
1015 gst_v4l2_object_unlock_stop (self->v4l2capture);
1016 gst_v4l2_object_unlock_stop (self->v4l2output);
1017 if (self->v4l2output->pool)
1018 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
1019 if (self->v4l2capture->pool)
1020 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
1021 break;
1022 default:
1023 break;
1024 }
1025
1026 return ret;
1027 }
1028
1029 static GstStateChangeReturn
gst_v4l2_transform_change_state(GstElement * element,GstStateChange transition)1030 gst_v4l2_transform_change_state (GstElement * element,
1031 GstStateChange transition)
1032 {
1033 GstV4l2Transform *self = GST_V4L2_TRANSFORM (element);
1034 GstStateChangeReturn ret;
1035
1036 switch (transition) {
1037 case GST_STATE_CHANGE_NULL_TO_READY:
1038 if (!gst_v4l2_transform_open (self))
1039 return GST_STATE_CHANGE_FAILURE;
1040 break;
1041 case GST_STATE_CHANGE_PAUSED_TO_READY:
1042 gst_v4l2_object_unlock (self->v4l2output);
1043 gst_v4l2_object_unlock (self->v4l2capture);
1044 break;
1045 default:
1046 break;
1047 }
1048
1049 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1050
1051 switch (transition) {
1052 case GST_STATE_CHANGE_READY_TO_NULL:
1053 gst_v4l2_transform_close (self);
1054 break;
1055 default:
1056 break;
1057 }
1058
1059 return ret;
1060 }
1061
1062 static void
gst_v4l2_transform_dispose(GObject * object)1063 gst_v4l2_transform_dispose (GObject * object)
1064 {
1065 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1066
1067 gst_caps_replace (&self->probed_sinkcaps, NULL);
1068 gst_caps_replace (&self->probed_srccaps, NULL);
1069
1070 G_OBJECT_CLASS (parent_class)->dispose (object);
1071 }
1072
1073 static void
gst_v4l2_transform_finalize(GObject * object)1074 gst_v4l2_transform_finalize (GObject * object)
1075 {
1076 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1077
1078 gst_v4l2_object_destroy (self->v4l2capture);
1079 gst_v4l2_object_destroy (self->v4l2output);
1080
1081 G_OBJECT_CLASS (parent_class)->finalize (object);
1082 }
1083
1084 static void
gst_v4l2_transform_init(GstV4l2Transform * self)1085 gst_v4l2_transform_init (GstV4l2Transform * self)
1086 {
1087 /* V4L2 object are created in subinstance_init */
1088 /* enable QoS */
1089 gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (self), TRUE);
1090 }
1091
1092 static void
gst_v4l2_transform_subinstance_init(GTypeInstance * instance,gpointer g_class)1093 gst_v4l2_transform_subinstance_init (GTypeInstance * instance, gpointer g_class)
1094 {
1095 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1096 GstV4l2Transform *self = GST_V4L2_TRANSFORM (instance);
1097
1098 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1099 GST_OBJECT (GST_BASE_TRANSFORM_SINK_PAD (self)),
1100 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1101 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1102 self->v4l2output->no_initial_format = TRUE;
1103 self->v4l2output->keep_aspect = FALSE;
1104
1105 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1106 GST_OBJECT (GST_BASE_TRANSFORM_SRC_PAD (self)),
1107 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1108 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1109 }
1110
1111 static void
gst_v4l2_transform_class_init(GstV4l2TransformClass * klass)1112 gst_v4l2_transform_class_init (GstV4l2TransformClass * klass)
1113 {
1114 GstElementClass *element_class;
1115 GObjectClass *gobject_class;
1116 GstBaseTransformClass *base_transform_class;
1117
1118 element_class = (GstElementClass *) klass;
1119 gobject_class = (GObjectClass *) klass;
1120 base_transform_class = (GstBaseTransformClass *) klass;
1121
1122 GST_DEBUG_CATEGORY_INIT (gst_v4l2_transform_debug, "v4l2transform", 0,
1123 "V4L2 Converter");
1124
1125 gst_element_class_set_static_metadata (element_class,
1126 "V4L2 Video Converter",
1127 "Filter/Converter/Video/Scaler",
1128 "Transform streams via V4L2 API",
1129 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1130
1131 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_transform_dispose);
1132 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_transform_finalize);
1133 gobject_class->set_property =
1134 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_property);
1135 gobject_class->get_property =
1136 GST_DEBUG_FUNCPTR (gst_v4l2_transform_get_property);
1137
1138 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_transform_stop);
1139 base_transform_class->set_caps =
1140 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_caps);
1141 base_transform_class->query = GST_DEBUG_FUNCPTR (gst_v4l2_transform_query);
1142 base_transform_class->sink_event =
1143 GST_DEBUG_FUNCPTR (gst_v4l2_transform_sink_event);
1144 base_transform_class->decide_allocation =
1145 GST_DEBUG_FUNCPTR (gst_v4l2_transform_decide_allocation);
1146 base_transform_class->propose_allocation =
1147 GST_DEBUG_FUNCPTR (gst_v4l2_transform_propose_allocation);
1148 base_transform_class->transform_caps =
1149 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform_caps);
1150 base_transform_class->fixate_caps =
1151 GST_DEBUG_FUNCPTR (gst_v4l2_transform_fixate_caps);
1152 base_transform_class->prepare_output_buffer =
1153 GST_DEBUG_FUNCPTR (gst_v4l2_transform_prepare_output_buffer);
1154 base_transform_class->transform =
1155 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform);
1156
1157 base_transform_class->passthrough_on_same_caps = TRUE;
1158
1159 element_class->change_state =
1160 GST_DEBUG_FUNCPTR (gst_v4l2_transform_change_state);
1161
1162 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1163
1164 g_object_class_install_property (gobject_class, PROP_DISABLE_PASSTHROUGH,
1165 g_param_spec_boolean ("disable-passthrough", "Disable Passthrough",
1166 "Forces passing buffers through the converter", FALSE,
1167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1168 }
1169
1170 static void
gst_v4l2_transform_subclass_init(gpointer g_class,gpointer data)1171 gst_v4l2_transform_subclass_init (gpointer g_class, gpointer data)
1172 {
1173 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1174 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1175 GstV4l2TransformCData *cdata = data;
1176
1177 klass->default_device = cdata->device;
1178
1179 gst_element_class_add_pad_template (element_class,
1180 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1181 cdata->sink_caps));
1182 gst_element_class_add_pad_template (element_class,
1183 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1184 cdata->src_caps));
1185
1186 gst_caps_unref (cdata->sink_caps);
1187 gst_caps_unref (cdata->src_caps);
1188 g_free (cdata);
1189 }
1190
1191 /* Probing functions */
1192 gboolean
gst_v4l2_is_transform(GstCaps * sink_caps,GstCaps * src_caps)1193 gst_v4l2_is_transform (GstCaps * sink_caps, GstCaps * src_caps)
1194 {
1195 gboolean ret = FALSE;
1196
1197 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_raw_caps ())
1198 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1199 ret = TRUE;
1200
1201 return ret;
1202 }
1203
1204 void
gst_v4l2_transform_register(GstPlugin * plugin,const gchar * basename,const gchar * device_path,GstCaps * sink_caps,GstCaps * src_caps)1205 gst_v4l2_transform_register (GstPlugin * plugin, const gchar * basename,
1206 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1207 {
1208 GTypeQuery type_query;
1209 GTypeInfo type_info = { 0, };
1210 GType type, subtype;
1211 gchar *type_name;
1212 GstV4l2TransformCData *cdata;
1213
1214 cdata = g_new0 (GstV4l2TransformCData, 1);
1215 cdata->device = g_strdup (device_path);
1216 cdata->sink_caps = gst_caps_ref (sink_caps);
1217 cdata->src_caps = gst_caps_ref (src_caps);
1218
1219 type = gst_v4l2_transform_get_type ();
1220 g_type_query (type, &type_query);
1221 memset (&type_info, 0, sizeof (type_info));
1222 type_info.class_size = type_query.class_size;
1223 type_info.instance_size = type_query.instance_size;
1224 type_info.class_init = gst_v4l2_transform_subclass_init;
1225 type_info.class_data = cdata;
1226 type_info.instance_init = gst_v4l2_transform_subinstance_init;
1227
1228 if (g_type_from_name ("v4l2convert") != 0)
1229 type_name = g_strdup_printf ("v4l2%sconvert", basename);
1230 else
1231 type_name = g_strdup ("v4l2convert");
1232 subtype = g_type_register_static (type, type_name, &type_info, 0);
1233
1234 if (!gst_element_register (plugin, type_name, GST_RANK_NONE, subtype))
1235 GST_WARNING ("Failed to register plugin '%s'", type_name);
1236
1237 g_free (type_name);
1238 }
1239