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