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