blob: 33052a8e1cffcac7a2a5ce92c95da8bc6d2b1ab2 [file] [log] [blame]
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001/*
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
Nicolas Dufresnebec03852017-07-12 20:49:47 -040032#include "gstv4l2object.h"
Nicolas Dufresneb7811562014-03-04 18:31:27 -050033#include "gstv4l2transform.h"
Nicolas Dufresneb7811562014-03-04 18:31:27 -050034
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
43GST_DEBUG_CATEGORY_STATIC (gst_v4l2_transform_debug);
44#define GST_CAT_DEFAULT gst_v4l2_transform_debug
45
46
47enum
48{
49 PROP_0,
Nicolas Dufresne077fcbc2014-05-15 18:08:53 -040050 V4L2_STD_OBJECT_PROPS
Nicolas Dufresneb7811562014-03-04 18:31:27 -050051};
52
53typedef struct
54{
55 gchar *device;
56 GstCaps *sink_caps;
57 GstCaps *src_caps;
58} GstV4l2TransformCData;
59
60#define gst_v4l2_transform_parent_class parent_class
61G_DEFINE_ABSTRACT_TYPE (GstV4l2Transform, gst_v4l2_transform,
62 GST_TYPE_BASE_TRANSFORM);
63
64static void
65gst_v4l2_transform_set_property (GObject * object,
66 guint prop_id, const GValue * value, GParamSpec * pspec)
67{
68 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
69
70 switch (prop_id) {
Nicolas Dufresne077fcbc2014-05-15 18:08:53 -040071 case PROP_OUTPUT_IO_MODE:
Nicolas Dufresneb7811562014-03-04 18:31:27 -050072 gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
73 pspec);
74 break;
75 case PROP_CAPTURE_IO_MODE:
Nicolas Dufresne4ecd25d2015-11-19 15:33:45 -050076 gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
77 pspec);
Nicolas Dufresneb7811562014-03-04 18:31:27 -050078 break;
79
Nicolas Dufresneb7811562014-03-04 18:31:27 -050080 /* 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
90static void
91gst_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) {
Nicolas Dufresne077fcbc2014-05-15 18:08:53 -040097 case PROP_OUTPUT_IO_MODE:
Nicolas Dufresneb7811562014-03-04 18:31:27 -050098 gst_v4l2_object_get_property_helper (self->v4l2output, prop_id, value,
99 pspec);
100 break;
101 case PROP_CAPTURE_IO_MODE:
Nicolas Dufresne077fcbc2014-05-15 18:08:53 -0400102 gst_v4l2_object_get_property_helper (self->v4l2capture, prop_id, value,
103 pspec);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500104 break;
105
106 /* By default read from output */
107 default:
108 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
109 prop_id, value, pspec)) {
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 }
112 break;
113 }
114}
115
116static gboolean
Nicolas Dufresne783417e2014-04-15 17:31:42 -0400117gst_v4l2_transform_open (GstV4l2Transform * self)
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500118{
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500119 GST_DEBUG_OBJECT (self, "Opening");
120
121 if (!gst_v4l2_object_open (self->v4l2output))
122 goto failure;
123
124 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
125 goto failure;
126
127 self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
128 gst_v4l2_object_get_raw_caps ());
129
130 if (gst_caps_is_empty (self->probed_sinkcaps))
131 goto no_input_format;
132
133 self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
134 gst_v4l2_object_get_raw_caps ());
135
136 if (gst_caps_is_empty (self->probed_srccaps))
137 goto no_output_format;
138
139 return TRUE;
140
141no_input_format:
142 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
143 (_("Converter on device %s has no supported input format"),
144 self->v4l2output->videodev), (NULL));
145 goto failure;
146
147
148no_output_format:
149 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
150 (_("Converter on device %s has no supported output format"),
151 self->v4l2output->videodev), (NULL));
152 goto failure;
153
154failure:
155 if (GST_V4L2_IS_OPEN (self->v4l2output))
156 gst_v4l2_object_close (self->v4l2output);
157
158 if (GST_V4L2_IS_OPEN (self->v4l2capture))
159 gst_v4l2_object_close (self->v4l2capture);
160
161 gst_caps_replace (&self->probed_srccaps, NULL);
162 gst_caps_replace (&self->probed_sinkcaps, NULL);
163
164 return FALSE;
165}
166
Nicolas Dufresne783417e2014-04-15 17:31:42 -0400167static void
168gst_v4l2_transform_close (GstV4l2Transform * self)
169{
170 GST_DEBUG_OBJECT (self, "Closing");
171
172 gst_v4l2_object_close (self->v4l2output);
173 gst_v4l2_object_close (self->v4l2capture);
174
175 gst_caps_replace (&self->probed_srccaps, NULL);
Jean-Michel Hautboisdd45e452015-08-20 14:45:33 +0200176 gst_caps_replace (&self->probed_sinkcaps, NULL);
Nicolas Dufresne783417e2014-04-15 17:31:42 -0400177}
178
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500179static gboolean
180gst_v4l2_transform_stop (GstBaseTransform * trans)
181{
182 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
183
Nicolas Dufresne783417e2014-04-15 17:31:42 -0400184 GST_DEBUG_OBJECT (self, "Stop");
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500185
Nicolas Dufresne6d5ffe82014-03-15 18:56:51 +0100186 gst_v4l2_object_stop (self->v4l2output);
187 gst_v4l2_object_stop (self->v4l2capture);
Nicolas Dufresne026f79b2014-04-15 16:43:15 -0400188 gst_caps_replace (&self->incaps, NULL);
189 gst_caps_replace (&self->outcaps, NULL);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500190
191 return TRUE;
192}
193
194static gboolean
195gst_v4l2_transform_set_caps (GstBaseTransform * trans, GstCaps * incaps,
196 GstCaps * outcaps)
197{
Nicolas Dufresne3b5cac32016-06-07 16:04:52 -0400198 GstV4l2Error error = GST_V4L2_ERROR_INIT;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500199 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
200
Nicolas Dufresne026f79b2014-04-15 16:43:15 -0400201 if (self->incaps && self->outcaps) {
202 if (gst_caps_is_equal (incaps, self->incaps) &&
203 gst_caps_is_equal (outcaps, self->outcaps)) {
204 GST_DEBUG_OBJECT (trans, "Caps did not changed");
205 return TRUE;
206 }
207 }
208
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500209 /* TODO Add renegotiation support */
210 g_return_val_if_fail (!GST_V4L2_IS_ACTIVE (self->v4l2output), FALSE);
211 g_return_val_if_fail (!GST_V4L2_IS_ACTIVE (self->v4l2capture), FALSE);
212
Nicolas Dufresne026f79b2014-04-15 16:43:15 -0400213 gst_caps_replace (&self->incaps, incaps);
214 gst_caps_replace (&self->outcaps, outcaps);
215
Nicolas Dufresne3b5cac32016-06-07 16:04:52 -0400216 if (!gst_v4l2_object_set_format (self->v4l2output, incaps, &error))
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500217 goto incaps_failed;
218
Nicolas Dufresne3b5cac32016-06-07 16:04:52 -0400219 if (!gst_v4l2_object_set_format (self->v4l2capture, outcaps, &error))
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500220 goto outcaps_failed;
221
Nicolas Dufresned4c24cc2014-03-20 15:31:22 -0400222 /* FIXME implement fallback if crop not supported */
223 if (!gst_v4l2_object_set_crop (self->v4l2output))
224 goto failed;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500225
Nicolas Dufresned4c24cc2014-03-20 15:31:22 -0400226 if (!gst_v4l2_object_set_crop (self->v4l2capture))
227 goto failed;
228
229 return TRUE;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500230
231incaps_failed:
232 {
233 GST_ERROR_OBJECT (self, "failed to set input caps: %" GST_PTR_FORMAT,
234 incaps);
Nicolas Dufresne3b5cac32016-06-07 16:04:52 -0400235 gst_v4l2_error (self, &error);
Nicolas Dufresned4c24cc2014-03-20 15:31:22 -0400236 goto failed;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500237 }
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500238outcaps_failed:
239 {
240 gst_v4l2_object_stop (self->v4l2output);
241 GST_ERROR_OBJECT (self, "failed to set output caps: %" GST_PTR_FORMAT,
242 outcaps);
Nicolas Dufresne3b5cac32016-06-07 16:04:52 -0400243 gst_v4l2_error (self, &error);
Nicolas Dufresned4c24cc2014-03-20 15:31:22 -0400244 goto failed;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500245 }
Nicolas Dufresned4c24cc2014-03-20 15:31:22 -0400246failed:
247 return FALSE;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500248}
249
250static gboolean
251gst_v4l2_transform_query (GstBaseTransform * trans, GstPadDirection direction,
252 GstQuery * query)
253{
254 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
255 gboolean ret = TRUE;
256
257 switch (GST_QUERY_TYPE (query)) {
258 case GST_QUERY_CAPS:{
259 GstCaps *filter, *caps = NULL, *result = NULL;
260 GstPad *pad, *otherpad;
261
262 gst_query_parse_caps (query, &filter);
263
264 if (direction == GST_PAD_SRC) {
265 pad = GST_BASE_TRANSFORM_SRC_PAD (trans);
266 otherpad = GST_BASE_TRANSFORM_SINK_PAD (trans);
267 if (self->probed_srccaps)
268 caps = gst_caps_ref (self->probed_srccaps);
269 } else {
270 pad = GST_BASE_TRANSFORM_SINK_PAD (trans);
271 otherpad = GST_BASE_TRANSFORM_SRC_PAD (trans);
Nicolas Dufresnea270dc72014-05-09 12:14:23 -0400272 if (self->probed_sinkcaps)
Nicolas Dufresnef8101962014-03-20 17:26:05 -0400273 caps = gst_caps_ref (self->probed_sinkcaps);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500274 }
275
276 if (!caps)
277 caps = gst_pad_get_pad_template_caps (pad);
278
279 if (filter) {
280 GstCaps *tmp = caps;
281 caps = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
282 gst_caps_unref (tmp);
283 }
284
285 result = gst_pad_peer_query_caps (otherpad, caps);
286 result = gst_caps_make_writable (result);
287 gst_caps_append (result, caps);
288
289 GST_DEBUG_OBJECT (self, "Returning %s caps %" GST_PTR_FORMAT,
290 GST_PAD_NAME (pad), result);
291
292 gst_query_set_caps_result (query, result);
293 gst_caps_unref (result);
294 break;
295 }
296
297 default:
298 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction,
299 query);
300 break;
301 }
302
303 return ret;
304}
305
306static gboolean
307gst_v4l2_transform_decide_allocation (GstBaseTransform * trans,
308 GstQuery * query)
309{
310 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
311 gboolean ret = FALSE;
312
Nicolas Dufresnedeb40de2014-04-29 12:48:04 -0400313 GST_DEBUG_OBJECT (self, "called");
314
Nicolas Dufresne91efa792014-03-16 19:11:16 +0100315 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query)) {
316 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2capture->pool);
317
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500318 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans,
319 query);
320
Nicolas Dufresne91efa792014-03-16 19:11:16 +0100321 if (!gst_buffer_pool_set_active (pool, TRUE))
322 goto activate_failed;
323 }
324
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500325 return ret;
Nicolas Dufresne91efa792014-03-16 19:11:16 +0100326
327activate_failed:
328 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
329 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
Nicolas Dufresne16356972014-04-08 18:54:50 -0400330 return TRUE;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500331}
332
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500333static gboolean
334gst_v4l2_transform_propose_allocation (GstBaseTransform * trans,
335 GstQuery * decide_query, GstQuery * query)
336{
337 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
338 gboolean ret = FALSE;
339
Nicolas Dufresnedeb40de2014-04-29 12:48:04 -0400340 GST_DEBUG_OBJECT (self, "called");
341
Nicolas Dufresne527df8b2014-03-27 13:21:25 -0400342 if (decide_query == NULL)
343 ret = TRUE;
344 else
345 ret = gst_v4l2_object_propose_allocation (self->v4l2output, query);
346
347 if (ret)
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500348 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans,
Nicolas Dufresne527df8b2014-03-27 13:21:25 -0400349 decide_query, query);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500350
351 return ret;
352}
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500353
354/* copies the given caps */
355static GstCaps *
356gst_v4l2_transform_caps_remove_format_info (GstCaps * caps)
357{
358 GstStructure *st;
359 GstCapsFeatures *f;
360 gint i, n;
361 GstCaps *res;
362
363 res = gst_caps_new_empty ();
364
365 n = gst_caps_get_size (caps);
366 for (i = 0; i < n; i++) {
367 st = gst_caps_get_structure (caps, i);
368 f = gst_caps_get_features (caps, i);
369
370 /* If this is already expressed by the existing caps
371 * skip this structure */
372 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
373 continue;
374
375 st = gst_structure_copy (st);
376 /* Only remove format info for the cases when we can actually convert */
377 if (!gst_caps_features_is_any (f)
378 && gst_caps_features_is_equal (f,
379 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
380 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
Philipp Zabel3eeef5f2016-05-20 12:49:53 +0200381 "width", "height", "pixel-aspect-ratio", NULL);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500382
383 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
384 }
385
386 return res;
387}
388
389/* The caps can be transformed into any other caps with format info removed.
390 * However, we should prefer passthrough, so if passthrough is possible,
391 * put it first in the list. */
392static GstCaps *
393gst_v4l2_transform_transform_caps (GstBaseTransform * btrans,
394 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
395{
396 GstCaps *tmp, *tmp2;
397 GstCaps *result;
398
399 /* Get all possible caps that we can transform to */
400 tmp = gst_v4l2_transform_caps_remove_format_info (caps);
401
402 if (filter) {
403 tmp2 = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
404 gst_caps_unref (tmp);
405 tmp = tmp2;
406 }
407
408 result = tmp;
409
410 GST_DEBUG_OBJECT (btrans, "transformed %" GST_PTR_FORMAT " into %"
411 GST_PTR_FORMAT, caps, result);
412
413 return result;
414}
415
416static GstCaps *
417gst_v4l2_transform_fixate_caps (GstBaseTransform * trans,
418 GstPadDirection direction, GstCaps * caps, GstCaps * othercaps)
419{
Enrico Jorns3067a602015-01-09 11:36:11 +0100420 GstStructure *ins, *outs;
421 const GValue *from_par, *to_par;
422 GValue fpar = { 0, }, tpar = {
423 0,};
424
425 othercaps = gst_caps_truncate (othercaps);
426 othercaps = gst_caps_make_writable (othercaps);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500427
428 GST_DEBUG_OBJECT (trans, "trying to fixate othercaps %" GST_PTR_FORMAT
429 " based on caps %" GST_PTR_FORMAT, othercaps, caps);
430
Enrico Jorns3067a602015-01-09 11:36:11 +0100431 ins = gst_caps_get_structure (caps, 0);
432 outs = gst_caps_get_structure (othercaps, 0);
433
Enrico Jorns5829f322015-01-28 12:07:58 +0100434 {
435 const gchar *in_format;
436
437 in_format = gst_structure_get_string (ins, "format");
438 if (in_format) {
439 /* Try to set output format for pass through */
440 gst_structure_fixate_field_string (outs, "format", in_format);
441 }
442
443 }
444
Enrico Jorns3067a602015-01-09 11:36:11 +0100445 from_par = gst_structure_get_value (ins, "pixel-aspect-ratio");
446 to_par = gst_structure_get_value (outs, "pixel-aspect-ratio");
447
448 /* If we're fixating from the sinkpad we always set the PAR and
449 * assume that missing PAR on the sinkpad means 1/1 and
450 * missing PAR on the srcpad means undefined
451 */
452 if (direction == GST_PAD_SINK) {
453 if (!from_par) {
454 g_value_init (&fpar, GST_TYPE_FRACTION);
455 gst_value_set_fraction (&fpar, 1, 1);
456 from_par = &fpar;
457 }
458 if (!to_par) {
459 g_value_init (&tpar, GST_TYPE_FRACTION_RANGE);
460 gst_value_set_fraction_range_full (&tpar, 1, G_MAXINT, G_MAXINT, 1);
461 to_par = &tpar;
462 }
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500463 } else {
Enrico Jorns3067a602015-01-09 11:36:11 +0100464 if (!to_par) {
465 g_value_init (&tpar, GST_TYPE_FRACTION);
466 gst_value_set_fraction (&tpar, 1, 1);
467 to_par = &tpar;
468
469 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
470 NULL);
471 }
472 if (!from_par) {
473 g_value_init (&fpar, GST_TYPE_FRACTION);
474 gst_value_set_fraction (&fpar, 1, 1);
475 from_par = &fpar;
476 }
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500477 }
478
Enrico Jorns3067a602015-01-09 11:36:11 +0100479 /* we have both PAR but they might not be fixated */
480 {
481 gint from_w, from_h, from_par_n, from_par_d, to_par_n, to_par_d;
482 gint w = 0, h = 0;
483 gint from_dar_n, from_dar_d;
484 gint num, den;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500485
Enrico Jorns3067a602015-01-09 11:36:11 +0100486 /* from_par should be fixed */
487 g_return_val_if_fail (gst_value_is_fixed (from_par), othercaps);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500488
Enrico Jorns3067a602015-01-09 11:36:11 +0100489 from_par_n = gst_value_get_fraction_numerator (from_par);
490 from_par_d = gst_value_get_fraction_denominator (from_par);
491
492 gst_structure_get_int (ins, "width", &from_w);
493 gst_structure_get_int (ins, "height", &from_h);
494
495 gst_structure_get_int (outs, "width", &w);
496 gst_structure_get_int (outs, "height", &h);
497
498 /* if both width and height are already fixed, we can't do anything
499 * about it anymore */
500 if (w && h) {
501 guint n, d;
502
503 GST_DEBUG_OBJECT (trans, "dimensions already set to %dx%d, not fixating",
504 w, h);
505 if (!gst_value_is_fixed (to_par)) {
506 if (gst_video_calculate_display_ratio (&n, &d, from_w, from_h,
507 from_par_n, from_par_d, w, h)) {
508 GST_DEBUG_OBJECT (trans, "fixating to_par to %dx%d", n, d);
509 if (gst_structure_has_field (outs, "pixel-aspect-ratio"))
510 gst_structure_fixate_field_nearest_fraction (outs,
511 "pixel-aspect-ratio", n, d);
512 else if (n != d)
513 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
514 n, d, NULL);
515 }
516 }
517 goto done;
518 }
519
520 /* Calculate input DAR */
521 if (!gst_util_fraction_multiply (from_w, from_h, from_par_n, from_par_d,
522 &from_dar_n, &from_dar_d)) {
523 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
524 ("Error calculating the output scaled size - integer overflow"));
525 goto done;
526 }
527
528 GST_DEBUG_OBJECT (trans, "Input DAR is %d/%d", from_dar_n, from_dar_d);
529
530 /* If either width or height are fixed there's not much we
531 * can do either except choosing a height or width and PAR
532 * that matches the DAR as good as possible
533 */
534 if (h) {
535 GstStructure *tmp;
536 gint set_w, set_par_n, set_par_d;
537
538 GST_DEBUG_OBJECT (trans, "height is fixed (%d)", h);
539
540 /* If the PAR is fixed too, there's not much to do
541 * except choosing the width that is nearest to the
542 * width with the same DAR */
543 if (gst_value_is_fixed (to_par)) {
544 to_par_n = gst_value_get_fraction_numerator (to_par);
545 to_par_d = gst_value_get_fraction_denominator (to_par);
546
547 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
548
549 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
550 to_par_n, &num, &den)) {
551 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
552 ("Error calculating the output scaled size - integer overflow"));
553 goto done;
554 }
555
556 w = (guint) gst_util_uint64_scale_int (h, num, den);
557 gst_structure_fixate_field_nearest_int (outs, "width", w);
558
559 goto done;
560 }
561
562 /* The PAR is not fixed and it's quite likely that we can set
563 * an arbitrary PAR. */
564
565 /* Check if we can keep the input width */
566 tmp = gst_structure_copy (outs);
567 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
568 gst_structure_get_int (tmp, "width", &set_w);
569
570 /* Might have failed but try to keep the DAR nonetheless by
571 * adjusting the PAR */
572 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, h, set_w,
573 &to_par_n, &to_par_d)) {
574 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
575 ("Error calculating the output scaled size - integer overflow"));
576 gst_structure_free (tmp);
577 goto done;
578 }
579
580 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
581 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
582 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
583 to_par_n, to_par_d);
584 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
585 &set_par_d);
586 gst_structure_free (tmp);
587
588 /* Check if the adjusted PAR is accepted */
589 if (set_par_n == to_par_n && set_par_d == to_par_d) {
590 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
591 set_par_n != set_par_d)
592 gst_structure_set (outs, "width", G_TYPE_INT, set_w,
593 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
594 NULL);
595 goto done;
596 }
597
598 /* Otherwise scale the width to the new PAR and check if the
599 * adjusted with is accepted. If all that fails we can't keep
600 * the DAR */
601 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
602 set_par_n, &num, &den)) {
603 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
604 ("Error calculating the output scaled size - integer overflow"));
605 goto done;
606 }
607
608 w = (guint) gst_util_uint64_scale_int (h, num, den);
609 gst_structure_fixate_field_nearest_int (outs, "width", w);
610 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
611 set_par_n != set_par_d)
612 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
613 set_par_n, set_par_d, NULL);
614
615 goto done;
616 } else if (w) {
617 GstStructure *tmp;
618 gint set_h, set_par_n, set_par_d;
619
620 GST_DEBUG_OBJECT (trans, "width is fixed (%d)", w);
621
622 /* If the PAR is fixed too, there's not much to do
623 * except choosing the height that is nearest to the
624 * height with the same DAR */
625 if (gst_value_is_fixed (to_par)) {
626 to_par_n = gst_value_get_fraction_numerator (to_par);
627 to_par_d = gst_value_get_fraction_denominator (to_par);
628
629 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
630
631 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
632 to_par_n, &num, &den)) {
633 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
634 ("Error calculating the output scaled size - integer overflow"));
635 goto done;
636 }
637
638 h = (guint) gst_util_uint64_scale_int (w, den, num);
639 gst_structure_fixate_field_nearest_int (outs, "height", h);
640
641 goto done;
642 }
643
644 /* The PAR is not fixed and it's quite likely that we can set
645 * an arbitrary PAR. */
646
647 /* Check if we can keep the input height */
648 tmp = gst_structure_copy (outs);
649 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
650 gst_structure_get_int (tmp, "height", &set_h);
651
652 /* Might have failed but try to keep the DAR nonetheless by
653 * adjusting the PAR */
654 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, w,
655 &to_par_n, &to_par_d)) {
656 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
657 ("Error calculating the output scaled size - integer overflow"));
658 gst_structure_free (tmp);
659 goto done;
660 }
661 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
662 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
663 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
664 to_par_n, to_par_d);
665 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
666 &set_par_d);
667 gst_structure_free (tmp);
668
669 /* Check if the adjusted PAR is accepted */
670 if (set_par_n == to_par_n && set_par_d == to_par_d) {
671 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
672 set_par_n != set_par_d)
673 gst_structure_set (outs, "height", G_TYPE_INT, set_h,
674 "pixel-aspect-ratio", GST_TYPE_FRACTION, set_par_n, set_par_d,
675 NULL);
676 goto done;
677 }
678
679 /* Otherwise scale the height to the new PAR and check if the
680 * adjusted with is accepted. If all that fails we can't keep
681 * the DAR */
682 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
683 set_par_n, &num, &den)) {
684 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
685 ("Error calculating the output scaled size - integer overflow"));
686 goto done;
687 }
688
689 h = (guint) gst_util_uint64_scale_int (w, den, num);
690 gst_structure_fixate_field_nearest_int (outs, "height", h);
691 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
692 set_par_n != set_par_d)
693 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
694 set_par_n, set_par_d, NULL);
695
696 goto done;
697 } else if (gst_value_is_fixed (to_par)) {
698 GstStructure *tmp;
699 gint set_h, set_w, f_h, f_w;
700
701 to_par_n = gst_value_get_fraction_numerator (to_par);
702 to_par_d = gst_value_get_fraction_denominator (to_par);
703
Philipp Zabel89a2f0a2016-05-20 12:32:25 +0200704 GST_DEBUG_OBJECT (trans, "PAR is fixed %d/%d", to_par_n, to_par_d);
705
Enrico Jorns3067a602015-01-09 11:36:11 +0100706 /* Calculate scale factor for the PAR change */
Philipp Zabel89a2f0a2016-05-20 12:32:25 +0200707 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, to_par_d,
708 to_par_n, &num, &den)) {
Enrico Jorns3067a602015-01-09 11:36:11 +0100709 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
710 ("Error calculating the output scaled size - integer overflow"));
711 goto done;
712 }
713
714 /* Try to keep the input height (because of interlacing) */
715 tmp = gst_structure_copy (outs);
716 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
717 gst_structure_get_int (tmp, "height", &set_h);
718
719 /* This might have failed but try to scale the width
720 * to keep the DAR nonetheless */
721 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
722 gst_structure_fixate_field_nearest_int (tmp, "width", w);
723 gst_structure_get_int (tmp, "width", &set_w);
724 gst_structure_free (tmp);
725
726 /* We kept the DAR and the height is nearest to the original height */
727 if (set_w == w) {
728 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
729 G_TYPE_INT, set_h, NULL);
730 goto done;
731 }
732
733 f_h = set_h;
734 f_w = set_w;
735
736 /* If the former failed, try to keep the input width at least */
737 tmp = gst_structure_copy (outs);
738 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
739 gst_structure_get_int (tmp, "width", &set_w);
740
741 /* This might have failed but try to scale the width
742 * to keep the DAR nonetheless */
743 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
744 gst_structure_fixate_field_nearest_int (tmp, "height", h);
745 gst_structure_get_int (tmp, "height", &set_h);
746 gst_structure_free (tmp);
747
748 /* We kept the DAR and the width is nearest to the original width */
749 if (set_h == h) {
750 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
751 G_TYPE_INT, set_h, NULL);
752 goto done;
753 }
754
755 /* If all this failed, keep the height that was nearest to the orignal
756 * height and the nearest possible width. This changes the DAR but
757 * there's not much else to do here.
758 */
759 gst_structure_set (outs, "width", G_TYPE_INT, f_w, "height", G_TYPE_INT,
760 f_h, NULL);
761 goto done;
762 } else {
763 GstStructure *tmp;
764 gint set_h, set_w, set_par_n, set_par_d, tmp2;
765
766 /* width, height and PAR are not fixed but passthrough is not possible */
767
768 /* First try to keep the height and width as good as possible
769 * and scale PAR */
770 tmp = gst_structure_copy (outs);
771 gst_structure_fixate_field_nearest_int (tmp, "height", from_h);
772 gst_structure_get_int (tmp, "height", &set_h);
773 gst_structure_fixate_field_nearest_int (tmp, "width", from_w);
774 gst_structure_get_int (tmp, "width", &set_w);
775
776 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_h, set_w,
777 &to_par_n, &to_par_d)) {
778 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
779 ("Error calculating the output scaled size - integer overflow"));
Luis de Bethencourt911f7b32015-07-02 14:34:57 +0100780 gst_structure_free (tmp);
Enrico Jorns3067a602015-01-09 11:36:11 +0100781 goto done;
782 }
783
784 if (!gst_structure_has_field (tmp, "pixel-aspect-ratio"))
785 gst_structure_set_value (tmp, "pixel-aspect-ratio", to_par);
786 gst_structure_fixate_field_nearest_fraction (tmp, "pixel-aspect-ratio",
787 to_par_n, to_par_d);
788 gst_structure_get_fraction (tmp, "pixel-aspect-ratio", &set_par_n,
789 &set_par_d);
790 gst_structure_free (tmp);
791
792 if (set_par_n == to_par_n && set_par_d == to_par_d) {
793 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
794 G_TYPE_INT, set_h, NULL);
795
796 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
797 set_par_n != set_par_d)
798 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
799 set_par_n, set_par_d, NULL);
800 goto done;
801 }
802
803 /* Otherwise try to scale width to keep the DAR with the set
804 * PAR and height */
805 if (!gst_util_fraction_multiply (from_dar_n, from_dar_d, set_par_d,
806 set_par_n, &num, &den)) {
807 GST_ELEMENT_ERROR (trans, CORE, NEGOTIATION, (NULL),
808 ("Error calculating the output scaled size - integer overflow"));
809 goto done;
810 }
811
812 w = (guint) gst_util_uint64_scale_int (set_h, num, den);
813 tmp = gst_structure_copy (outs);
814 gst_structure_fixate_field_nearest_int (tmp, "width", w);
815 gst_structure_get_int (tmp, "width", &tmp2);
816 gst_structure_free (tmp);
817
818 if (tmp2 == w) {
819 gst_structure_set (outs, "width", G_TYPE_INT, tmp2, "height",
820 G_TYPE_INT, set_h, NULL);
821 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
822 set_par_n != set_par_d)
823 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
824 set_par_n, set_par_d, NULL);
825 goto done;
826 }
827
828 /* ... or try the same with the height */
829 h = (guint) gst_util_uint64_scale_int (set_w, den, num);
830 tmp = gst_structure_copy (outs);
831 gst_structure_fixate_field_nearest_int (tmp, "height", h);
832 gst_structure_get_int (tmp, "height", &tmp2);
833 gst_structure_free (tmp);
834
835 if (tmp2 == h) {
836 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
837 G_TYPE_INT, tmp2, NULL);
838 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
839 set_par_n != set_par_d)
840 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
841 set_par_n, set_par_d, NULL);
842 goto done;
843 }
844
845 /* If all fails we can't keep the DAR and take the nearest values
846 * for everything from the first try */
847 gst_structure_set (outs, "width", G_TYPE_INT, set_w, "height",
848 G_TYPE_INT, set_h, NULL);
849 if (gst_structure_has_field (outs, "pixel-aspect-ratio") ||
850 set_par_n != set_par_d)
851 gst_structure_set (outs, "pixel-aspect-ratio", GST_TYPE_FRACTION,
852 set_par_n, set_par_d, NULL);
853 }
854 }
855
856done:
857 GST_DEBUG_OBJECT (trans, "fixated othercaps to %" GST_PTR_FORMAT, othercaps);
858
859 if (from_par == &fpar)
860 g_value_unset (&fpar);
861 if (to_par == &tpar)
862 g_value_unset (&tpar);
863
Enrico Jorns5829f322015-01-28 12:07:58 +0100864 /* fixate remaining fields */
865 othercaps = gst_caps_fixate (othercaps);
866
Nicolas Dufresnef9e01a52016-06-06 17:54:10 -0400867 if (direction == GST_PAD_SINK) {
868 if (gst_caps_is_subset (caps, othercaps)) {
869 gst_caps_replace (&othercaps, caps);
870 }
871 }
872
Enrico Jorns3067a602015-01-09 11:36:11 +0100873 return othercaps;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500874}
875
876static GstFlowReturn
877gst_v4l2_transform_prepare_output_buffer (GstBaseTransform * trans,
878 GstBuffer * inbuf, GstBuffer ** outbuf)
879{
880 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
881 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
882 GstFlowReturn ret = GST_FLOW_OK;
Víctor Manuel Jáquez Leal4f7ee5c2014-03-15 16:53:54 +0000883 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_CLASS (parent_class);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500884
885 if (gst_base_transform_is_passthrough (trans)) {
886 GST_DEBUG_OBJECT (self, "Passthrough, no need to do anything");
887 *outbuf = inbuf;
888 goto beach;
889 }
890
Nicolas Dufresne91efa792014-03-16 19:11:16 +0100891 /* Ensure input internal pool is active */
Nicolas Dufresne23e273a2014-04-15 16:43:41 -0400892 if (!gst_buffer_pool_is_active (pool)) {
893 GstStructure *config = gst_buffer_pool_get_config (pool);
Pablo Anton927ba932015-05-21 17:41:12 +0200894 gint min = self->v4l2output->min_buffers == 0 ? GST_V4L2_MIN_BUFFERS :
895 self->v4l2output->min_buffers;
Nicolas Dufresne23e273a2014-04-15 16:43:41 -0400896 gst_buffer_pool_config_set_params (config, self->incaps,
Pablo Anton927ba932015-05-21 17:41:12 +0200897 self->v4l2output->info.size, min, min);
Nicolas Dufresne23e273a2014-04-15 16:43:41 -0400898
899 /* There is no reason to refuse this config */
900 if (!gst_buffer_pool_set_config (pool, config))
901 goto activate_failed;
902
903 if (!gst_buffer_pool_set_active (pool, TRUE))
904 goto activate_failed;
905 }
Nicolas Dufresne91efa792014-03-16 19:11:16 +0100906
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500907 GST_DEBUG_OBJECT (self, "Queue input buffer");
Nicolas Dufresnedeb40de2014-04-29 12:48:04 -0400908 ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), &inbuf);
Nicolas Dufresne91efa792014-03-16 19:11:16 +0100909 if (G_UNLIKELY (ret != GST_FLOW_OK))
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500910 goto beach;
911
Nicolas Dufresneb9992e42014-11-16 12:34:17 -0500912 do {
913 pool = gst_base_transform_get_buffer_pool (trans);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500914
Nicolas Dufresneb9992e42014-11-16 12:34:17 -0500915 if (!gst_buffer_pool_set_active (pool, TRUE))
916 goto activate_failed;
Nicolas Dufresne10b67e92014-03-18 17:33:38 -0400917
Nicolas Dufresneb9992e42014-11-16 12:34:17 -0500918 GST_DEBUG_OBJECT (self, "Dequeue output buffer");
919 ret = gst_buffer_pool_acquire_buffer (pool, outbuf, NULL);
920 g_object_unref (pool);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500921
Nicolas Dufresneb9992e42014-11-16 12:34:17 -0500922 if (ret != GST_FLOW_OK)
923 goto alloc_failed;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500924
Nicolas Dufresneb9992e42014-11-16 12:34:17 -0500925 pool = self->v4l2capture->pool;
926 ret = gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (pool), outbuf);
927
928 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500929
930 if (ret != GST_FLOW_OK) {
931 gst_buffer_unref (*outbuf);
932 *outbuf = NULL;
933 }
934
Víctor Manuel Jáquez Leal4f7ee5c2014-03-15 16:53:54 +0000935 if (bclass->copy_metadata)
936 if (!bclass->copy_metadata (trans, inbuf, *outbuf)) {
937 /* something failed, post a warning */
938 GST_ELEMENT_WARNING (self, STREAM, NOT_IMPLEMENTED,
939 ("could not copy metadata"), (NULL));
940 }
941
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500942beach:
943 return ret;
944
945activate_failed:
946 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
947 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
Nicolas Dufresne5f10d2b2014-05-14 17:18:52 -0400948 g_object_unref (pool);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500949 return GST_FLOW_ERROR;
950
951alloc_failed:
952 GST_DEBUG_OBJECT (self, "could not allocate buffer from pool");
953 return ret;
954}
955
956static GstFlowReturn
957gst_v4l2_transform_transform (GstBaseTransform * trans, GstBuffer * inbuf,
958 GstBuffer * outbuf)
959{
960 /* Nothing to do */
961 return GST_FLOW_OK;
962}
963
964static gboolean
965gst_v4l2_transform_sink_event (GstBaseTransform * trans, GstEvent * event)
966{
967 GstV4l2Transform *self = GST_V4L2_TRANSFORM (trans);
Nicolas Dufresnea4b58112014-05-14 17:18:35 -0400968 gboolean ret;
969
970 /* Nothing to flush in passthrough */
971 if (gst_base_transform_is_passthrough (trans))
972 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500973
974 switch (GST_EVENT_TYPE (event)) {
975 case GST_EVENT_FLUSH_START:
Nicolas Dufresne6f5edb32014-04-29 14:56:31 -0400976 GST_DEBUG_OBJECT (self, "flush start");
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500977 gst_v4l2_object_unlock (self->v4l2output);
978 gst_v4l2_object_unlock (self->v4l2capture);
Nicolas Dufresnebf191842014-05-09 12:11:54 -0400979 break;
Nicolas Dufresneb7811562014-03-04 18:31:27 -0500980 default:
981 break;
982 }
983
Nicolas Dufresnea4b58112014-05-14 17:18:35 -0400984 ret = GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
985
986 switch (GST_EVENT_TYPE (event)) {
987 case GST_EVENT_FLUSH_STOP:
988 /* Buffer should be back now */
Nicolas Dufresnee2fd7e22014-05-24 20:20:07 -0400989 GST_DEBUG_OBJECT (self, "flush stop");
990 gst_v4l2_object_unlock_stop (self->v4l2capture);
991 gst_v4l2_object_unlock_stop (self->v4l2output);
Nicolas Dufresne1f902e22017-08-05 12:23:30 -0400992 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
993 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
Nicolas Dufresnea4b58112014-05-14 17:18:35 -0400994 break;
995 default:
996 break;
997 }
998
999 return ret;
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001000}
1001
1002static GstStateChangeReturn
1003gst_v4l2_transform_change_state (GstElement * element,
1004 GstStateChange transition)
1005{
1006 GstV4l2Transform *self = GST_V4L2_TRANSFORM (element);
Nicolas Dufresne783417e2014-04-15 17:31:42 -04001007 GstStateChangeReturn ret;
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001008
Nicolas Dufresne783417e2014-04-15 17:31:42 -04001009 switch (transition) {
1010 case GST_STATE_CHANGE_NULL_TO_READY:
1011 if (!gst_v4l2_transform_open (self))
1012 return GST_STATE_CHANGE_FAILURE;
1013 break;
1014 case GST_STATE_CHANGE_PAUSED_TO_READY:
1015 gst_v4l2_object_unlock (self->v4l2output);
1016 gst_v4l2_object_unlock (self->v4l2capture);
1017 break;
1018 default:
1019 break;
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001020 }
1021
Nicolas Dufresne783417e2014-04-15 17:31:42 -04001022 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1023
1024 switch (transition) {
1025 case GST_STATE_CHANGE_READY_TO_NULL:
1026 gst_v4l2_transform_close (self);
1027 break;
1028 default:
1029 break;
1030 }
1031
1032 return ret;
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001033}
1034
1035static void
1036gst_v4l2_transform_dispose (GObject * object)
1037{
1038 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1039
1040 gst_caps_replace (&self->probed_sinkcaps, NULL);
1041 gst_caps_replace (&self->probed_srccaps, NULL);
1042
1043 G_OBJECT_CLASS (parent_class)->dispose (object);
1044}
1045
1046static void
1047gst_v4l2_transform_finalize (GObject * object)
1048{
1049 GstV4l2Transform *self = GST_V4L2_TRANSFORM (object);
1050
1051 gst_v4l2_object_destroy (self->v4l2capture);
1052 gst_v4l2_object_destroy (self->v4l2output);
1053
1054 G_OBJECT_CLASS (parent_class)->finalize (object);
1055}
1056
1057static void
1058gst_v4l2_transform_init (GstV4l2Transform * self)
1059{
1060 /* V4L2 object are created in subinstance_init */
Nicolas Dufresne3500e442014-04-18 17:51:07 -04001061 /* enable QoS */
1062 gst_base_transform_set_qos_enabled (GST_BASE_TRANSFORM (self), TRUE);
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001063}
1064
1065static void
Nicolas Dufresne7a4b0762014-03-16 11:36:19 +01001066gst_v4l2_transform_subinstance_init (GTypeInstance * instance, gpointer g_class)
1067{
1068 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1069 GstV4l2Transform *self = GST_V4L2_TRANSFORM (instance);
1070
1071 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
Nicolas Dufresnee575e4e2017-12-13 14:39:47 -05001072 GST_OBJECT (GST_BASE_TRANSFORM_SINK_PAD (self)),
Nicolas Dufresne7a4b0762014-03-16 11:36:19 +01001073 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1074 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1075 self->v4l2output->no_initial_format = TRUE;
1076 self->v4l2output->keep_aspect = FALSE;
1077
1078 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
Nicolas Dufresnee575e4e2017-12-13 14:39:47 -05001079 GST_OBJECT (GST_BASE_TRANSFORM_SRC_PAD (self)),
Nicolas Dufresne7a4b0762014-03-16 11:36:19 +01001080 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1081 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1082 self->v4l2capture->no_initial_format = TRUE;
1083 self->v4l2output->keep_aspect = FALSE;
Nicolas Dufresne7a4b0762014-03-16 11:36:19 +01001084}
1085
1086static void
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001087gst_v4l2_transform_class_init (GstV4l2TransformClass * klass)
1088{
1089 GstElementClass *element_class;
1090 GObjectClass *gobject_class;
1091 GstBaseTransformClass *base_transform_class;
1092
1093 element_class = (GstElementClass *) klass;
1094 gobject_class = (GObjectClass *) klass;
1095 base_transform_class = (GstBaseTransformClass *) klass;
1096
Nicolas Dufresne01feadc2014-05-04 19:13:37 -04001097 GST_DEBUG_CATEGORY_INIT (gst_v4l2_transform_debug, "v4l2transform", 0,
1098 "V4L2 Converter");
1099
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001100 gst_element_class_set_static_metadata (element_class,
1101 "V4L2 Video Converter",
Enrico Jorns3067a602015-01-09 11:36:11 +01001102 "Filter/Converter/Video/Scaler",
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001103 "Transform streams via V4L2 API",
1104 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1105
1106 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_transform_dispose);
1107 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_transform_finalize);
1108 gobject_class->set_property =
1109 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_property);
1110 gobject_class->get_property =
1111 GST_DEBUG_FUNCPTR (gst_v4l2_transform_get_property);
1112
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001113 base_transform_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_transform_stop);
1114 base_transform_class->set_caps =
1115 GST_DEBUG_FUNCPTR (gst_v4l2_transform_set_caps);
1116 base_transform_class->query = GST_DEBUG_FUNCPTR (gst_v4l2_transform_query);
1117 base_transform_class->sink_event =
1118 GST_DEBUG_FUNCPTR (gst_v4l2_transform_sink_event);
1119 base_transform_class->decide_allocation =
1120 GST_DEBUG_FUNCPTR (gst_v4l2_transform_decide_allocation);
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001121 base_transform_class->propose_allocation =
1122 GST_DEBUG_FUNCPTR (gst_v4l2_transform_propose_allocation);
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001123 base_transform_class->transform_caps =
1124 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform_caps);
1125 base_transform_class->fixate_caps =
1126 GST_DEBUG_FUNCPTR (gst_v4l2_transform_fixate_caps);
1127 base_transform_class->prepare_output_buffer =
1128 GST_DEBUG_FUNCPTR (gst_v4l2_transform_prepare_output_buffer);
1129 base_transform_class->transform =
1130 GST_DEBUG_FUNCPTR (gst_v4l2_transform_transform);
1131
1132 base_transform_class->passthrough_on_same_caps = TRUE;
1133
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001134 element_class->change_state =
1135 GST_DEBUG_FUNCPTR (gst_v4l2_transform_change_state);
1136
Nicolas Dufresne077fcbc2014-05-15 18:08:53 -04001137 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001138}
1139
1140static void
1141gst_v4l2_transform_subclass_init (gpointer g_class, gpointer data)
1142{
1143 GstV4l2TransformClass *klass = GST_V4L2_TRANSFORM_CLASS (g_class);
1144 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1145 GstV4l2TransformCData *cdata = data;
1146
1147 klass->default_device = cdata->device;
1148
1149 /* Note: gst_pad_template_new() take the floating ref from the caps */
1150 gst_element_class_add_pad_template (element_class,
1151 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1152 cdata->sink_caps));
1153 gst_element_class_add_pad_template (element_class,
1154 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1155 cdata->src_caps));
1156
1157 g_free (cdata);
1158}
1159
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001160/* Probing functions */
1161gboolean
1162gst_v4l2_is_transform (GstCaps * sink_caps, GstCaps * src_caps)
1163{
1164 gboolean ret = FALSE;
1165
1166 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_raw_caps ())
1167 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1168 ret = TRUE;
1169
1170 return ret;
1171}
1172
Nicolas Dufresne1762c2e2017-07-27 10:51:07 -04001173void
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001174gst_v4l2_transform_register (GstPlugin * plugin, const gchar * basename,
1175 const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1176{
1177 GTypeQuery type_query;
1178 GTypeInfo type_info = { 0, };
1179 GType type, subtype;
1180 gchar *type_name;
1181 GstV4l2TransformCData *cdata;
1182
1183 cdata = g_new0 (GstV4l2TransformCData, 1);
1184 cdata->device = g_strdup (device_path);
1185 cdata->sink_caps = gst_caps_ref (sink_caps);
1186 cdata->src_caps = gst_caps_ref (src_caps);
1187
1188 type = gst_v4l2_transform_get_type ();
1189 g_type_query (type, &type_query);
1190 memset (&type_info, 0, sizeof (type_info));
1191 type_info.class_size = type_query.class_size;
1192 type_info.instance_size = type_query.instance_size;
1193 type_info.class_init = gst_v4l2_transform_subclass_init;
1194 type_info.class_data = cdata;
1195 type_info.instance_init = gst_v4l2_transform_subinstance_init;
1196
1197 type_name = g_strdup_printf ("v4l2%sconvert", basename);
1198 subtype = g_type_register_static (type, type_name, &type_info, 0);
1199
Nicolas Dufresne1762c2e2017-07-27 10:51:07 -04001200 if (!gst_element_register (plugin, type_name, GST_RANK_NONE, subtype))
1201 GST_WARNING ("Failed to register plugin '%s'", type_name);
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001202
1203 g_free (type_name);
Nicolas Dufresneb7811562014-03-04 18:31:27 -05001204}