b43a6c9b271a045a3b47a4f3ec99b29b699a1ea2
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2videodec.c
1 /*
2  * Copyright (C) 2014 Collabora Ltd.
3  *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
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 "gstv4l2videodec.h"
34
35 #include <string.h>
36 #include <gst/gst-i18n-plugin.h>
37
38 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
39 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
40
41 typedef struct
42 {
43   gchar *device;
44   GstCaps *sink_caps;
45   GstCaps *src_caps;
46   const gchar *longname;
47   const gchar *description;
48 } GstV4l2VideoDecCData;
49
50 enum
51 {
52   PROP_0,
53   V4L2_STD_OBJECT_PROPS
54 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
55   , PROP_TBM_OUTPUT
56 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
57 };
58
59 #define gst_v4l2_video_dec_parent_class parent_class
60 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
61     GST_TYPE_VIDEO_DECODER);
62
63 static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder);
64
65 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
66 static void gst_v4l2_video_dec_flush_buffer_event (GstVideoDecoder * decoder)
67 {
68   gboolean ret = FALSE;
69
70   if (!decoder) {
71     GST_ERROR("no decoder");
72     return;
73   }
74
75   ret = gst_pad_push_event (decoder->srcpad,
76       gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM | GST_EVENT_TYPE_SERIALIZED,
77           gst_structure_new_empty("tizen/flush-buffer")));
78
79   GST_WARNING_OBJECT(decoder, "event push ret[%d] for flush-buffer", ret);
80 }
81 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
82 static void
83 gst_v4l2_video_dec_set_property (GObject * object,
84     guint prop_id, const GValue * value, GParamSpec * pspec)
85 {
86   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
87
88   switch (prop_id) {
89     case PROP_CAPTURE_IO_MODE:
90       if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
91               prop_id, value, pspec)) {
92         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93       }
94       break;
95 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
96     case PROP_TBM_OUTPUT:
97       self->v4l2capture->tbm_output = g_value_get_boolean (value);
98       GST_INFO_OBJECT (self, "tbm output [%d]", self->v4l2capture->tbm_output);
99       break;
100 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
101
102
103       /* By default, only set on output */
104     default:
105       if (!gst_v4l2_object_set_property_helper (self->v4l2output,
106               prop_id, value, pspec)) {
107         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108       }
109       break;
110   }
111 }
112
113 static void
114 gst_v4l2_video_dec_get_property (GObject * object,
115     guint prop_id, GValue * value, GParamSpec * pspec)
116 {
117   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
118
119   switch (prop_id) {
120     case PROP_CAPTURE_IO_MODE:
121       if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
122               prop_id, value, pspec)) {
123         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124       }
125       break;
126 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
127     case PROP_TBM_OUTPUT:
128       GST_INFO_OBJECT (self, "tbm output [%d]", self->v4l2capture->tbm_output);
129       g_value_set_boolean (value, self->v4l2capture->tbm_output);
130       break;
131 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
132
133       /* By default read from output */
134     default:
135       if (!gst_v4l2_object_get_property_helper (self->v4l2output,
136               prop_id, value, pspec)) {
137         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138       }
139       break;
140   }
141 }
142
143 static gboolean
144 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
145 {
146   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
147   GstCaps *codec_caps;
148
149   GST_DEBUG_OBJECT (self, "Opening");
150
151   if (!gst_v4l2_object_open (self->v4l2output))
152     goto failure;
153
154   if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
155     goto failure;
156
157   codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
158   self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
159       codec_caps);
160   gst_caps_unref (codec_caps);
161
162   if (gst_caps_is_empty (self->probed_sinkcaps))
163     goto no_encoded_format;
164
165   return TRUE;
166
167 no_encoded_format:
168   GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
169       (_("Decoder on device %s has no supported input format"),
170           self->v4l2output->videodev), (NULL));
171   goto failure;
172
173 failure:
174   if (GST_V4L2_IS_OPEN (self->v4l2output))
175     gst_v4l2_object_close (self->v4l2output);
176
177   if (GST_V4L2_IS_OPEN (self->v4l2capture))
178     gst_v4l2_object_close (self->v4l2capture);
179
180   gst_caps_replace (&self->probed_srccaps, NULL);
181   gst_caps_replace (&self->probed_sinkcaps, NULL);
182
183   return FALSE;
184 }
185
186 static gboolean
187 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
188 {
189   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
190
191   GST_DEBUG_OBJECT (self, "Closing");
192
193   gst_v4l2_object_close (self->v4l2output);
194   gst_v4l2_object_close (self->v4l2capture);
195   gst_caps_replace (&self->probed_srccaps, NULL);
196   gst_caps_replace (&self->probed_sinkcaps, NULL);
197
198   return TRUE;
199 }
200
201 static gboolean
202 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
203 {
204   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
205
206   GST_DEBUG_OBJECT (self, "Starting");
207
208   gst_v4l2_object_unlock (self->v4l2output);
209   g_atomic_int_set (&self->active, TRUE);
210   self->output_flow = GST_FLOW_OK;
211
212   return TRUE;
213 }
214
215 static gboolean
216 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
217 {
218   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
219
220   GST_DEBUG_OBJECT (self, "Stopping");
221
222   gst_v4l2_object_unlock (self->v4l2output);
223   gst_v4l2_object_unlock (self->v4l2capture);
224
225   /* Wait for capture thread to stop */
226   gst_pad_stop_task (decoder->srcpad);
227
228   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
229   self->output_flow = GST_FLOW_OK;
230   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
231
232   /* Should have been flushed already */
233   g_assert (g_atomic_int_get (&self->active) == FALSE);
234 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
235   gst_v4l2_video_dec_flush_buffer_event (decoder);
236 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
237
238   gst_v4l2_object_stop (self->v4l2output);
239   gst_v4l2_object_stop (self->v4l2capture);
240
241   if (self->input_state) {
242     gst_video_codec_state_unref (self->input_state);
243     self->input_state = NULL;
244   }
245
246   GST_DEBUG_OBJECT (self, "Stopped");
247
248   return TRUE;
249 }
250
251 static gboolean
252 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
253     GstVideoCodecState * state)
254 {
255   GstV4l2Error error = GST_V4L2_ERROR_INIT;
256   gboolean ret = TRUE;
257   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
258
259   GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
260
261   if (self->input_state) {
262     if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
263       GST_DEBUG_OBJECT (self, "Compatible caps");
264       goto done;
265     }
266     gst_video_codec_state_unref (self->input_state);
267     self->input_state = NULL;
268
269     gst_v4l2_video_dec_finish (decoder);
270     gst_v4l2_object_stop (self->v4l2output);
271
272     /* The renegotiation flow don't blend with the base class flow. To properly
273      * stop the capture pool, if the buffers can't be orphaned, we need to
274      * reclaim our buffers, which will happend through the allocation query.
275      * The allocation query is triggered by gst_video_decoder_negotiate() which
276      * requires the output caps to be set, but we can't know this information
277      * as we rely on the decoder, which requires the capture queue to be
278      * stopped.
279      *
280      * To workaround this issue, we simply run an allocation query with the
281      * old negotiated caps in order to drain/reclaim our buffers. That breaks
282      * the complexity and should not have much impact in performance since the
283      * following allocation query will happen on a drained pipeline and won't
284      * block. */
285     if (self->v4l2capture->pool &&
286         !gst_v4l2_buffer_pool_orphan (&self->v4l2capture->pool)) {
287       GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
288       if (caps) {
289         GstQuery *query = gst_query_new_allocation (caps, FALSE);
290         gst_pad_peer_query (decoder->srcpad, query);
291         gst_query_unref (query);
292         gst_caps_unref (caps);
293       }
294     }
295
296     gst_v4l2_object_stop (self->v4l2capture);
297     self->output_flow = GST_FLOW_OK;
298   }
299
300   ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
301
302   gst_caps_replace (&self->probed_srccaps, NULL);
303   self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
304       gst_v4l2_object_get_raw_caps ());
305
306   if (gst_caps_is_empty (self->probed_srccaps))
307     goto no_raw_format;
308
309   if (ret)
310     self->input_state = gst_video_codec_state_ref (state);
311   else
312     gst_v4l2_error (self, &error);
313
314 done:
315   return ret;
316
317 no_raw_format:
318   GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
319       (_("Decoder on device %s has no supported output format"),
320           self->v4l2output->videodev), (NULL));
321   return GST_FLOW_ERROR;
322 }
323
324 static gboolean
325 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
326 {
327   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
328
329   GST_DEBUG_OBJECT (self, "Flushed");
330
331   /* Ensure the processing thread has stopped for the reverse playback
332    * discount case */
333   if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
334     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
335
336     gst_v4l2_object_unlock (self->v4l2output);
337     gst_v4l2_object_unlock (self->v4l2capture);
338     gst_pad_stop_task (decoder->srcpad);
339     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
340   }
341
342   self->output_flow = GST_FLOW_OK;
343
344   gst_v4l2_object_unlock_stop (self->v4l2output);
345   gst_v4l2_object_unlock_stop (self->v4l2capture);
346
347   if (self->v4l2output->pool)
348     gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
349
350 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
351   gst_v4l2_video_dec_flush_buffer_event (decoder);
352 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
353   /* gst_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
354    * called after gst_v4l2_object_unlock_stop() stopped flushing the buffer
355    * pool. */
356   if (self->v4l2capture->pool)
357     gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
358
359   return TRUE;
360 }
361
362 static gboolean
363 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
364 {
365   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
366
367   /* We don't allow renegotiation without carefull disabling the pool */
368   if (self->v4l2capture->pool &&
369       gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
370     return TRUE;
371
372   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
373 }
374
375 static gboolean
376 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
377 {
378   struct v4l2_decoder_cmd dcmd = { 0, };
379
380   GST_DEBUG_OBJECT (v4l2object->element,
381       "sending v4l2 decoder command %u with flags %u", cmd, flags);
382
383   if (!GST_V4L2_IS_OPEN (v4l2object))
384     return FALSE;
385
386   dcmd.cmd = cmd;
387   dcmd.flags = flags;
388   if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
389     goto dcmd_failed;
390
391   return TRUE;
392
393 dcmd_failed:
394   if (errno == ENOTTY) {
395     GST_INFO_OBJECT (v4l2object->element,
396         "Failed to send decoder command %u with flags %u for '%s'. (%s)",
397         cmd, flags, v4l2object->videodev, g_strerror (errno));
398   } else {
399     GST_ERROR_OBJECT (v4l2object->element,
400         "Failed to send decoder command %u with flags %u for '%s'. (%s)",
401         cmd, flags, v4l2object->videodev, g_strerror (errno));
402   }
403   return FALSE;
404 }
405
406 static GstFlowReturn
407 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
408 {
409   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
410   GstFlowReturn ret = GST_FLOW_OK;
411   GstBuffer *buffer;
412
413   if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
414     goto done;
415
416   GST_DEBUG_OBJECT (self, "Finishing decoding");
417
418   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
419
420   if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
421     GstTask *task = decoder->srcpad->task;
422
423     /* If the decoder stop command succeeded, just wait until processing is
424      * finished */
425     GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
426     GST_OBJECT_LOCK (task);
427     while (GST_TASK_STATE (task) == GST_TASK_STARTED)
428       GST_TASK_WAIT (task);
429     GST_OBJECT_UNLOCK (task);
430     ret = GST_FLOW_FLUSHING;
431   } else {
432     /* otherwise keep queuing empty buffers until the processing thread has
433      * stopped, _pool_process() will return FLUSHING when that happened */
434     while (ret == GST_FLOW_OK) {
435       buffer = gst_buffer_new ();
436       ret =
437           gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
438               v4l2output->pool), &buffer);
439       gst_buffer_unref (buffer);
440     }
441   }
442
443   /* and ensure the processing thread has stopped in case another error
444    * occured. */
445   gst_v4l2_object_unlock (self->v4l2capture);
446   gst_pad_stop_task (decoder->srcpad);
447   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
448
449   if (ret == GST_FLOW_FLUSHING)
450     ret = self->output_flow;
451
452   GST_DEBUG_OBJECT (decoder, "Done draining buffers");
453
454   /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
455
456 done:
457   return ret;
458 }
459
460 static GstFlowReturn
461 gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
462 {
463   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
464
465   GST_DEBUG_OBJECT (self, "Draining...");
466   gst_v4l2_video_dec_finish (decoder);
467   gst_v4l2_video_dec_flush (decoder);
468
469   return GST_FLOW_OK;
470 }
471
472 static GstVideoCodecFrame *
473 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
474 {
475   GstVideoCodecFrame *frame = NULL;
476   GList *frames, *l;
477   gint count = 0;
478
479   frames = gst_video_decoder_get_frames (decoder);
480
481   for (l = frames; l != NULL; l = l->next) {
482     GstVideoCodecFrame *f = l->data;
483
484     if (!frame || frame->pts > f->pts)
485       frame = f;
486
487     count++;
488   }
489
490   if (frame) {
491     GST_LOG_OBJECT (decoder,
492         "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
493         frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
494     gst_video_codec_frame_ref (frame);
495   }
496
497   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
498
499   return frame;
500 }
501
502 static void
503 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
504 {
505   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
506   GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
507   GstBufferPool *pool;
508   GstVideoCodecFrame *frame;
509   GstBuffer *buffer = NULL;
510   GstFlowReturn ret;
511
512   GST_LOG_OBJECT (decoder, "Allocate output buffer");
513
514   self->output_flow = GST_FLOW_OK;
515   do {
516     /* We cannot use the base class allotate helper since it taking the internal
517      * stream lock. we know that the acquire may need to poll until more frames
518      * comes in and holding this lock would prevent that.
519      */
520     pool = gst_video_decoder_get_buffer_pool (decoder);
521
522     /* Pool may be NULL if we started going to READY state */
523     if (pool == NULL) {
524       ret = GST_FLOW_FLUSHING;
525       goto beach;
526     }
527
528     ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
529     g_object_unref (pool);
530
531     if (ret != GST_FLOW_OK)
532       goto beach;
533
534     GST_LOG_OBJECT (decoder, "Process output buffer");
535     ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
536
537   } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
538
539   if (ret != GST_FLOW_OK)
540     goto beach;
541
542   frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
543
544   if (frame) {
545     frame->output_buffer = buffer;
546     buffer = NULL;
547     ret = gst_video_decoder_finish_frame (decoder, frame);
548
549     if (ret != GST_FLOW_OK)
550       goto beach;
551   } else {
552     GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
553     gst_buffer_unref (buffer);
554   }
555
556   return;
557
558 beach:
559   GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
560       gst_flow_get_name (ret));
561
562   gst_buffer_replace (&buffer, NULL);
563   self->output_flow = ret;
564   gst_v4l2_object_unlock (self->v4l2output);
565   gst_pad_pause_task (decoder->srcpad);
566 }
567
568 static gboolean
569 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
570     GstStructure * structure, gpointer user_data)
571 {
572   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
573   GstVideoAlignment *align = &self->v4l2capture->align;
574   GstVideoInfo *info = &self->v4l2capture->info;
575   int width, height;
576
577   if (!gst_structure_get_int (structure, "width", &width))
578     return TRUE;
579
580   if (!gst_structure_get_int (structure, "height", &height))
581     return TRUE;
582
583   if (align->padding_left != 0 || align->padding_top != 0 ||
584       height != info->height + align->padding_bottom)
585     return TRUE;
586
587   if (height == info->height + align->padding_bottom) {
588     /* Some drivers may round up width to the padded with */
589     if (width == info->width + align->padding_right)
590       gst_structure_set (structure,
591           "width", G_TYPE_INT, width - align->padding_right,
592           "height", G_TYPE_INT, height - align->padding_bottom, NULL);
593     /* Some drivers may keep visible width and only round up bytesperline */
594     else if (width == info->width)
595       gst_structure_set (structure,
596           "height", G_TYPE_INT, height - align->padding_bottom, NULL);
597   }
598
599   return TRUE;
600 }
601
602 static GstFlowReturn
603 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
604     GstVideoCodecFrame * frame)
605 {
606   GstV4l2Error error = GST_V4L2_ERROR_INIT;
607   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
608   GstFlowReturn ret = GST_FLOW_OK;
609   gboolean processed = FALSE;
610   GstBuffer *tmp;
611   GstTaskState task_state;
612 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
613   GstStructure *structure = NULL;
614   const gchar *caps_format = NULL;
615 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
616
617   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
618
619   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
620     goto flushing;
621
622   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
623     if (!self->input_state)
624       goto not_negotiated;
625     if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
626             &error))
627       goto not_negotiated;
628   }
629
630   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
631     GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
632     GstVideoInfo info;
633     GstVideoCodecState *output_state;
634     GstBuffer *codec_data;
635     GstCaps *acquired_caps, *available_caps, *caps, *filter;
636     GstStructure *st;
637
638     GST_DEBUG_OBJECT (self, "Sending header");
639
640     codec_data = self->input_state->codec_data;
641
642     /* We are running in byte-stream mode, so we don't know the headers, but
643      * we need to send something, otherwise the decoder will refuse to
644      * intialize.
645      */
646     if (codec_data) {
647       gst_buffer_ref (codec_data);
648     } else {
649       codec_data = gst_buffer_ref (frame->input_buffer);
650       processed = TRUE;
651     }
652
653     /* Ensure input internal pool is active */
654     if (!gst_buffer_pool_is_active (pool)) {
655       GstStructure *config = gst_buffer_pool_get_config (pool);
656       gst_buffer_pool_config_set_params (config, self->input_state->caps,
657           self->v4l2output->info.size, 2, 2);
658
659       /* There is no reason to refuse this config */
660       if (!gst_buffer_pool_set_config (pool, config))
661         goto activate_failed;
662
663       if (!gst_buffer_pool_set_active (pool, TRUE))
664         goto activate_failed;
665     }
666
667     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
668     ret =
669         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
670             v4l2output->pool), &codec_data);
671     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
672
673     gst_buffer_unref (codec_data);
674
675     /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
676      * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
677      * and returns the visible size as with/height and the coded size as
678      * padding. */
679     if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
680       goto not_negotiated;
681
682     /* Create caps from the acquired format, remove the format field */
683     acquired_caps = gst_video_info_to_caps (&info);
684     GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
685     st = gst_caps_get_structure (acquired_caps, 0);
686     gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
687         NULL);
688
689     /* Probe currently available pixel formats */
690     available_caps = gst_caps_copy (self->probed_srccaps);
691     GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
692
693     /* Replace coded size with visible size, we want to negotiate visible size
694      * with downstream, not coded size. */
695     gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
696
697     filter = gst_caps_intersect_full (available_caps, acquired_caps,
698         GST_CAPS_INTERSECT_FIRST);
699     GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
700     gst_caps_unref (acquired_caps);
701     gst_caps_unref (available_caps);
702     caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
703     gst_caps_unref (filter);
704
705     GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
706     if (gst_caps_is_empty (caps)) {
707       gst_caps_unref (caps);
708       goto not_negotiated;
709     }
710
711     /* Fixate pixel format */
712     caps = gst_caps_fixate (caps);
713
714     GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
715 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
716     structure = gst_caps_get_structure (caps, 0);
717     caps_format = gst_structure_get_string (structure, "format");
718
719     if (!strcmp (caps_format, "I420")) {
720       GST_INFO_OBJECT (self, "I420 -> S420");
721       gst_caps_set_simple (caps, "format", G_TYPE_STRING, "S420", NULL);
722     } else if (!strcmp (caps_format, "NV12")) {
723       GST_INFO_OBJECT (self, "NV12 -> SN12");
724       gst_caps_set_simple (caps, "format", G_TYPE_STRING, "SN12", NULL);
725     }
726     GST_INFO_OBJECT (self, "Updated decoded caps: %" GST_PTR_FORMAT, caps);
727 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
728
729     /* Try to set negotiated format, on success replace acquired format */
730     if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
731       gst_video_info_from_caps (&info, caps);
732     else
733       gst_v4l2_clear_error (&error);
734     gst_caps_unref (caps);
735
736     output_state = gst_video_decoder_set_output_state (decoder,
737         info.finfo->format, info.width, info.height, self->input_state);
738
739     /* Copy the rest of the information, there might be more in the future */
740     output_state->info.interlace_mode = info.interlace_mode;
741     gst_video_codec_state_unref (output_state);
742
743     if (!gst_video_decoder_negotiate (decoder)) {
744       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
745         goto flushing;
746       else
747         goto not_negotiated;
748     }
749
750     /* Ensure our internal pool is activated */
751     if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
752             TRUE))
753       goto activate_failed;
754   }
755
756   task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
757   if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
758     /* It's possible that the processing thread stopped due to an error */
759     if (self->output_flow != GST_FLOW_OK &&
760         self->output_flow != GST_FLOW_FLUSHING) {
761       GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
762       ret = self->output_flow;
763       goto drop;
764     }
765
766     GST_DEBUG_OBJECT (self, "Starting decoding thread");
767
768     /* Start the processing task, when it quits, the task will disable input
769      * processing to unlock input if draining, or prevent potential block */
770     self->output_flow = GST_FLOW_FLUSHING;
771     if (!gst_pad_start_task (decoder->srcpad,
772             (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
773       goto start_task_failed;
774   }
775
776   if (!processed) {
777     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
778     ret =
779         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
780             pool), &frame->input_buffer);
781     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
782
783     if (ret == GST_FLOW_FLUSHING) {
784       if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
785           GST_TASK_STARTED)
786         ret = self->output_flow;
787       goto drop;
788     } else if (ret != GST_FLOW_OK) {
789       goto process_failed;
790     }
791   }
792
793   /* No need to keep input arround */
794   tmp = frame->input_buffer;
795   frame->input_buffer = gst_buffer_new ();
796   gst_buffer_copy_into (frame->input_buffer, tmp,
797       GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
798       GST_BUFFER_COPY_META, 0, 0);
799   gst_buffer_unref (tmp);
800
801   gst_video_codec_frame_unref (frame);
802   return ret;
803
804   /* ERRORS */
805 not_negotiated:
806   {
807     GST_ERROR_OBJECT (self, "not negotiated");
808     ret = GST_FLOW_NOT_NEGOTIATED;
809     gst_v4l2_error (self, &error);
810     goto drop;
811   }
812 activate_failed:
813   {
814     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
815         (_("Failed to allocate required memory.")),
816         ("Buffer pool activation failed"));
817     ret = GST_FLOW_ERROR;
818     goto drop;
819   }
820 flushing:
821   {
822     ret = GST_FLOW_FLUSHING;
823     goto drop;
824   }
825
826 start_task_failed:
827   {
828     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
829         (_("Failed to start decoding thread.")), (NULL));
830     ret = GST_FLOW_ERROR;
831     goto drop;
832   }
833 process_failed:
834   {
835     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
836         (_("Failed to process frame.")),
837         ("Maybe be due to not enough memory or failing driver"));
838     ret = GST_FLOW_ERROR;
839     goto drop;
840   }
841 drop:
842   {
843     gst_video_decoder_drop_frame (decoder, frame);
844     return ret;
845   }
846 }
847
848 static gboolean
849 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
850     GstQuery * query)
851 {
852   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
853   GstClockTime latency;
854   gboolean ret = FALSE;
855
856   if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
857     ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
858         query);
859
860   if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
861     latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
862     GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
863         G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
864         self->v4l2capture->min_buffers, self->v4l2capture->duration);
865     gst_video_decoder_set_latency (decoder, latency, latency);
866   } else {
867     GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
868   }
869
870   return ret;
871 }
872
873 static gboolean
874 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
875 {
876   gboolean ret = TRUE;
877   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
878
879   switch (GST_QUERY_TYPE (query)) {
880     case GST_QUERY_CAPS:{
881       GstCaps *filter, *result = NULL;
882       GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
883
884       gst_query_parse_caps (query, &filter);
885
886       if (self->probed_srccaps)
887         result = gst_caps_ref (self->probed_srccaps);
888       else
889         result = gst_pad_get_pad_template_caps (pad);
890
891       if (filter) {
892         GstCaps *tmp = result;
893         result =
894             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
895         gst_caps_unref (tmp);
896       }
897
898       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
899
900       gst_query_set_caps_result (query, result);
901       gst_caps_unref (result);
902       break;
903     }
904
905     default:
906       ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
907       break;
908   }
909
910   return ret;
911 }
912
913 static GstCaps *
914 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
915 {
916   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
917   GstCaps *result;
918
919   result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
920       filter);
921
922   GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
923
924   return result;
925 }
926
927 static gboolean
928 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
929 {
930   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
931   gboolean ret;
932   GstEventType type = GST_EVENT_TYPE (event);
933
934   switch (type) {
935     case GST_EVENT_FLUSH_START:
936       GST_DEBUG_OBJECT (self, "flush start");
937       gst_v4l2_object_unlock (self->v4l2output);
938       gst_v4l2_object_unlock (self->v4l2capture);
939       break;
940     default:
941       break;
942   }
943
944   ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
945
946   switch (type) {
947     case GST_EVENT_FLUSH_START:
948       /* The processing thread should stop now, wait for it */
949       gst_pad_stop_task (decoder->srcpad);
950       GST_DEBUG_OBJECT (self, "flush start done");
951       break;
952     default:
953       break;
954   }
955
956   return ret;
957 }
958
959 static GstStateChangeReturn
960 gst_v4l2_video_dec_change_state (GstElement * element,
961     GstStateChange transition)
962 {
963   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
964   GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
965
966   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
967     g_atomic_int_set (&self->active, FALSE);
968     gst_v4l2_object_unlock (self->v4l2output);
969     gst_v4l2_object_unlock (self->v4l2capture);
970     gst_pad_stop_task (decoder->srcpad);
971   }
972
973   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
974 }
975
976 static void
977 gst_v4l2_video_dec_dispose (GObject * object)
978 {
979   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
980
981   gst_caps_replace (&self->probed_sinkcaps, NULL);
982   gst_caps_replace (&self->probed_srccaps, NULL);
983
984   G_OBJECT_CLASS (parent_class)->dispose (object);
985 }
986
987 static void
988 gst_v4l2_video_dec_finalize (GObject * object)
989 {
990   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
991
992   gst_v4l2_object_destroy (self->v4l2capture);
993   gst_v4l2_object_destroy (self->v4l2output);
994
995   G_OBJECT_CLASS (parent_class)->finalize (object);
996 }
997
998 static void
999 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
1000 {
1001   /* V4L2 object are created in subinstance_init */
1002 }
1003
1004 static void
1005 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
1006 {
1007   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1008   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
1009   GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
1010
1011   gst_video_decoder_set_packetized (decoder, TRUE);
1012
1013   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1014       GST_OBJECT (GST_VIDEO_DECODER_SINK_PAD (self)),
1015       V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1016       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1017   self->v4l2output->no_initial_format = TRUE;
1018   self->v4l2output->keep_aspect = FALSE;
1019
1020   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1021       GST_OBJECT (GST_VIDEO_DECODER_SRC_PAD (self)),
1022       V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1023       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1024 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
1025   self->v4l2capture->tbm_output = TRUE;
1026 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
1027 }
1028
1029 static void
1030 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
1031 {
1032   GstElementClass *element_class;
1033   GObjectClass *gobject_class;
1034   GstVideoDecoderClass *video_decoder_class;
1035
1036   parent_class = g_type_class_peek_parent (klass);
1037
1038   element_class = (GstElementClass *) klass;
1039   gobject_class = (GObjectClass *) klass;
1040   video_decoder_class = (GstVideoDecoderClass *) klass;
1041
1042   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
1043       "V4L2 Video Decoder");
1044
1045   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
1046   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
1047   gobject_class->set_property =
1048       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
1049   gobject_class->get_property =
1050       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
1051
1052   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
1053   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
1054   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
1055   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
1056   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
1057   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
1058   video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_drain);
1059   video_decoder_class->set_format =
1060       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
1061   video_decoder_class->negotiate =
1062       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
1063   video_decoder_class->decide_allocation =
1064       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
1065   /* FIXME propose_allocation or not ? */
1066   video_decoder_class->handle_frame =
1067       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
1068   video_decoder_class->getcaps =
1069       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
1070   video_decoder_class->src_query =
1071       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
1072   video_decoder_class->sink_event =
1073       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
1074
1075   element_class->change_state =
1076       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
1077
1078   gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1079 #ifdef TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER
1080   g_object_class_install_property (gobject_class, PROP_TBM_OUTPUT,
1081       g_param_spec_boolean ("tbm-output", "Enable TBM for output buffer",
1082           "It works for only DMABUF mode.",
1083           TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1084 #endif /* TIZEN_FEATURE_TBM_SUPPORT_FOR_V4L2_DECODER */
1085 }
1086
1087 static void
1088 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
1089 {
1090   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1091   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1092   GstV4l2VideoDecCData *cdata = data;
1093
1094   klass->default_device = cdata->device;
1095
1096   /* Note: gst_pad_template_new() take the floating ref from the caps */
1097   gst_element_class_add_pad_template (element_class,
1098       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1099           cdata->sink_caps));
1100   gst_element_class_add_pad_template (element_class,
1101       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1102           cdata->src_caps));
1103
1104   gst_element_class_set_static_metadata (element_class, cdata->longname,
1105       "Codec/Decoder/Video/Hardware", cdata->description,
1106       "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1107
1108   gst_caps_unref (cdata->sink_caps);
1109   gst_caps_unref (cdata->src_caps);
1110   g_free (cdata);
1111 }
1112
1113 /* Probing functions */
1114 gboolean
1115 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
1116 {
1117   gboolean ret = FALSE;
1118
1119   if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
1120       && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1121     ret = TRUE;
1122
1123   return ret;
1124 }
1125
1126 static gchar *
1127 gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
1128     const gchar * basename)
1129 {
1130   gchar *codec_name = NULL;
1131   gchar *type_name = NULL;
1132
1133 #define SET_META(codec) \
1134 G_STMT_START { \
1135   cdata->longname = "V4L2 " codec " Decoder"; \
1136   cdata->description = "Decodes " codec " streams via V4L2 API"; \
1137   codec_name = g_ascii_strdown (codec, -1); \
1138 } G_STMT_END
1139
1140   if (gst_structure_has_name (s, "image/jpeg")) {
1141     SET_META ("JPEG");
1142   } else if (gst_structure_has_name (s, "video/mpeg")) {
1143     gint mpegversion = 0;
1144     gst_structure_get_int (s, "mpegversion", &mpegversion);
1145
1146     if (mpegversion == 2) {
1147       SET_META ("MPEG2");
1148     } else {
1149       SET_META ("MPEG4");
1150     }
1151   } else if (gst_structure_has_name (s, "video/x-h263")) {
1152     SET_META ("H263");
1153   } else if (gst_structure_has_name (s, "video/x-fwht")) {
1154     SET_META ("FWHT");
1155   } else if (gst_structure_has_name (s, "video/x-h264")) {
1156     SET_META ("H264");
1157   } else if (gst_structure_has_name (s, "video/x-h265")) {
1158     SET_META ("H265");
1159   } else if (gst_structure_has_name (s, "video/x-wmv")) {
1160     SET_META ("VC1");
1161   } else if (gst_structure_has_name (s, "video/x-vp8")) {
1162     SET_META ("VP8");
1163   } else if (gst_structure_has_name (s, "video/x-vp9")) {
1164     SET_META ("VP9");
1165   } else if (gst_structure_has_name (s, "video/x-bayer")) {
1166     SET_META ("BAYER");
1167   } else if (gst_structure_has_name (s, "video/x-sonix")) {
1168     SET_META ("SONIX");
1169   } else if (gst_structure_has_name (s, "video/x-pwc1")) {
1170     SET_META ("PWC1");
1171   } else if (gst_structure_has_name (s, "video/x-pwc2")) {
1172     SET_META ("PWC2");
1173   } else {
1174     /* This code should be kept on sync with the exposed CODEC type of format
1175      * from gstv4l2object.c. This warning will only occure in case we forget
1176      * to also add a format here. */
1177     gchar *s_str = gst_structure_to_string (s);
1178     g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
1179         "bug, please report at https://bugs.gnome.org", s_str);
1180     g_free (s_str);
1181   }
1182
1183   if (codec_name) {
1184     type_name = g_strdup_printf ("v4l2%sdec", codec_name);
1185     if (g_type_from_name (type_name) != 0) {
1186       g_free (type_name);
1187       type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
1188     }
1189
1190     g_free (codec_name);
1191   }
1192
1193   return type_name;
1194 #undef SET_META
1195 }
1196
1197 void
1198 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1199     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
1200 {
1201   gint i;
1202
1203   for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
1204     GstV4l2VideoDecCData *cdata;
1205     GstStructure *s;
1206     GTypeQuery type_query;
1207     GTypeInfo type_info = { 0, };
1208     GType type, subtype;
1209     gchar *type_name;
1210
1211     s = gst_caps_get_structure (sink_caps, i);
1212
1213     cdata = g_new0 (GstV4l2VideoDecCData, 1);
1214     cdata->device = g_strdup (device_path);
1215     cdata->sink_caps = gst_caps_new_empty ();
1216     gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
1217     cdata->src_caps = gst_caps_ref (src_caps);
1218     type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
1219
1220     /* Skip over if we hit an unmapped type */
1221     if (!type_name) {
1222       g_free (cdata);
1223       continue;
1224     }
1225
1226     type = gst_v4l2_video_dec_get_type ();
1227     g_type_query (type, &type_query);
1228     memset (&type_info, 0, sizeof (type_info));
1229     type_info.class_size = type_query.class_size;
1230     type_info.instance_size = type_query.instance_size;
1231     type_info.class_init = gst_v4l2_video_dec_subclass_init;
1232     type_info.class_data = cdata;
1233     type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1234
1235     subtype = g_type_register_static (type, type_name, &type_info, 0);
1236 #ifdef TIZEN_FEATURE_V4L2VIDEO_ADJ_RANK
1237     if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY,
1238             subtype))
1239       GST_WARNING ("Failed to register plugin '%s'", type_name);
1240 #else
1241     if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
1242             subtype))
1243       GST_WARNING ("Failed to register plugin '%s'", type_name);
1244 #endif
1245
1246     g_free (type_name);
1247   }
1248 }