v4l2videodec: Ensure internal pool are activated
[platform/upstream/gst-plugins-good.git] / sys / v4l2 / gstv4l2videodec.c
1 /*
2  * Copyright (C) 2014 Collabora Ltd.
3  *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 #include "gstv4l2videodec.h"
33 #include "v4l2_calls.h"
34
35 #include <string.h>
36 #include <gst/gst-i18n-plugin.h>
37
38 #define DEFAULT_PROP_DEVICE "/dev/video0"
39
40 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
41 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
42
43 static gboolean gst_v4l2_video_dec_flush (GstVideoDecoder * decoder);
44
45 typedef struct
46 {
47   gchar *device;
48   GstCaps *sink_caps;
49   GstCaps *src_caps;
50 } GstV4l2VideoDecCData;
51
52 enum
53 {
54   PROP_0,
55   V4L2_STD_OBJECT_PROPS,
56   PROP_CAPTURE_IO_MODE,
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 void
64 gst_v4l2_video_dec_set_property (GObject * object,
65     guint prop_id, const GValue * value, GParamSpec * pspec)
66 {
67   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
68
69   switch (prop_id) {
70       /* Split IO mode so output is configure through 'io-mode' and capture
71        * through 'capture-io-mode' */
72     case PROP_IO_MODE:
73       gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
74           pspec);
75       break;
76     case PROP_CAPTURE_IO_MODE:
77       gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
78           pspec);
79       break;
80
81     case PROP_DEVICE:
82       gst_v4l2_object_set_property_helper (self->v4l2output, prop_id, value,
83           pspec);
84       gst_v4l2_object_set_property_helper (self->v4l2capture, prop_id, value,
85           pspec);
86       break;
87
88       /* By default, only set on output */
89     default:
90       if (!gst_v4l2_object_set_property_helper (self->v4l2output,
91               prop_id, value, pspec)) {
92         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93       }
94       break;
95   }
96 }
97
98 static void
99 gst_v4l2_video_dec_get_property (GObject * object,
100     guint prop_id, GValue * value, GParamSpec * pspec)
101 {
102   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
103
104   switch (prop_id) {
105     case PROP_IO_MODE:
106       gst_v4l2_object_get_property_helper (self->v4l2output, prop_id, value,
107           pspec);
108       break;
109     case PROP_CAPTURE_IO_MODE:
110       gst_v4l2_object_get_property_helper (self->v4l2output, PROP_IO_MODE,
111           value, pspec);
112       break;
113
114       /* By default read from output */
115     default:
116       if (!gst_v4l2_object_get_property_helper (self->v4l2output,
117               prop_id, value, pspec)) {
118         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119       }
120       break;
121   }
122 }
123
124 static gboolean
125 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
126 {
127   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
128
129   GST_DEBUG_OBJECT (self, "Opening");
130
131   if (!gst_v4l2_object_open (self->v4l2output))
132     goto failure;
133
134   if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
135     goto failure;
136
137   self->probed_sinkcaps = gst_v4l2_object_get_caps (self->v4l2output,
138       gst_v4l2_object_get_codec_caps ());
139
140   if (gst_caps_is_empty (self->probed_sinkcaps))
141     goto no_encoded_format;
142
143   self->probed_srccaps = gst_v4l2_object_get_caps (self->v4l2capture,
144       gst_v4l2_object_get_raw_caps ());
145
146   if (gst_caps_is_empty (self->probed_srccaps))
147     goto no_raw_format;
148
149   return TRUE;
150
151 no_encoded_format:
152   GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
153       (_("Encoder on device %s has no supported input format"),
154           self->v4l2output->videodev), (NULL));
155   goto failure;
156
157
158 no_raw_format:
159   GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
160       (_("Encoder on device %s has no supported output format"),
161           self->v4l2output->videodev), (NULL));
162   goto failure;
163
164 failure:
165   if (GST_V4L2_IS_OPEN (self->v4l2output))
166     gst_v4l2_object_close (self->v4l2output);
167
168   if (GST_V4L2_IS_OPEN (self->v4l2capture))
169     gst_v4l2_object_close (self->v4l2capture);
170
171   gst_caps_replace (&self->probed_srccaps, NULL);
172   gst_caps_replace (&self->probed_sinkcaps, NULL);
173
174   return FALSE;
175 }
176
177 static gboolean
178 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
179 {
180   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
181
182   GST_DEBUG_OBJECT (self, "Closing");
183
184   gst_v4l2_object_close (self->v4l2output);
185   gst_v4l2_object_close (self->v4l2capture);
186   gst_caps_replace (&self->probed_srccaps, NULL);
187   gst_caps_replace (&self->probed_sinkcaps, NULL);
188
189   return TRUE;
190 }
191
192 static gboolean
193 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
194 {
195   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
196
197   GST_DEBUG_OBJECT (self, "Starting");
198
199   gst_v4l2_object_unlock (self->v4l2output);
200   g_atomic_int_set (&self->active, TRUE);
201   self->output_flow = GST_FLOW_OK;
202
203   return TRUE;
204 }
205
206 static gboolean
207 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
208 {
209   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
210
211   GST_DEBUG_OBJECT (self, "Stopping");
212
213   gst_v4l2_object_unlock (self->v4l2output);
214   gst_v4l2_object_unlock (self->v4l2capture);
215
216   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
217   /* Wait for capture thread to stop */
218   gst_pad_stop_task (decoder->srcpad);
219   self->output_flow = GST_FLOW_OK;
220   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
221
222   /* Should have been flushed already */
223   g_assert (g_atomic_int_get (&self->active) == FALSE);
224   g_assert (g_atomic_int_get (&self->processing) == FALSE);
225
226   gst_v4l2_object_stop (self->v4l2output);
227   gst_v4l2_object_stop (self->v4l2capture);
228
229   if (self->input_state) {
230     gst_video_codec_state_unref (self->input_state);
231     self->input_state = NULL;
232   }
233
234   GST_DEBUG_OBJECT (self, "Stopped");
235
236   return TRUE;
237 }
238
239 static gboolean
240 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
241     GstVideoCodecState * state)
242 {
243   gboolean ret = TRUE;
244   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
245
246   GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
247
248   if (self->input_state) {
249     if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
250       GST_DEBUG_OBJECT (self, "Compatible caps");
251       goto done;
252     }
253     gst_video_codec_state_unref (self->input_state);
254     self->input_state = NULL;
255
256     /* FIXME we probably need to do more work if pools are active */
257   }
258
259   ret = gst_v4l2_object_set_format (self->v4l2output, state->caps);
260
261   if (ret)
262     self->input_state = gst_video_codec_state_ref (state);
263
264 done:
265   return ret;
266 }
267
268 static gboolean
269 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
270 {
271   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
272
273   GST_DEBUG_OBJECT (self, "Flushing");
274
275   /* Wait for capture thread to stop */
276   gst_pad_stop_task (decoder->srcpad);
277   self->output_flow = GST_FLOW_OK;
278
279   if (self->v4l2output->pool)
280     gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2output->pool));
281
282   if (self->v4l2capture->pool)
283     gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2capture->pool));
284
285   /* Output will remain flushing until new frame comes in */
286   gst_v4l2_object_unlock_stop (self->v4l2capture);
287
288   return TRUE;
289 }
290
291 static gboolean
292 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
293 {
294   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
295 }
296
297 static GstFlowReturn
298 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
299 {
300   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
301   GstFlowReturn ret = GST_FLOW_OK;
302   GstBuffer *buffer;
303
304   if (!self->processing)
305     goto done;
306
307   GST_DEBUG_OBJECT (self, "Finishing decoding");
308
309   /* Keep queuing empty buffers until the processing thread has stopped,
310    * _pool_process() will return FLUSHING when that happened */
311   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
312   while (ret == GST_FLOW_OK) {
313     buffer = gst_buffer_new ();
314     ret =
315         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
316             v4l2output->pool), buffer);
317     gst_buffer_unref (buffer);
318   }
319   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
320
321   /* Ensure the processing thread has stopped */
322   if (self->processing) {
323     gst_v4l2_object_unlock (self->v4l2capture);
324     gst_pad_stop_task (decoder->srcpad);
325     g_assert (g_atomic_int_get (&self->processing) == FALSE);
326   }
327
328   if (ret == GST_FLOW_FLUSHING)
329     ret = self->output_flow;
330
331   GST_DEBUG_OBJECT (decoder, "Done draining buffers");
332
333 done:
334   return ret;
335 }
336
337 static GstVideoCodecFrame *
338 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
339 {
340   GstVideoCodecFrame *frame = NULL;
341   GList *frames, *l;
342   gint count = 0;
343
344   frames = gst_video_decoder_get_frames (decoder);
345
346   for (l = frames; l != NULL; l = l->next) {
347     GstVideoCodecFrame *f = l->data;
348
349     if (!frame || frame->pts > f->pts)
350       frame = f;
351
352     count++;
353   }
354
355   if (frame) {
356     GST_LOG_OBJECT (decoder,
357         "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
358         frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
359     gst_video_codec_frame_ref (frame);
360   }
361
362   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
363
364   return frame;
365 }
366
367 static void
368 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
369 {
370   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
371   GstBufferPool *pool;
372   GstVideoCodecFrame *frame;
373   GstBuffer *buffer = NULL;
374   GstFlowReturn ret;
375
376   GST_LOG_OBJECT (decoder, "Allocate output buffer");
377
378   /* We cannot use the base class allotate helper since it taking the internal
379    * stream lock. we know that the acquire may need to poll until more frames
380    * comes in and holding this lock would prevent that.
381    */
382   pool = gst_video_decoder_get_buffer_pool (decoder);
383
384   /* Pool may be NULL if we started going to READY state */
385   if (pool == NULL) {
386     ret = GST_FLOW_FLUSHING;
387     goto beach;
388   }
389
390   ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
391   g_object_unref (pool);
392
393   if (ret != GST_FLOW_OK)
394     goto beach;
395
396   /* Check if buffer isn't the last one */
397   if (gst_buffer_get_size (buffer) == 0)
398     goto beach;
399
400   GST_LOG_OBJECT (decoder, "Process output buffer");
401   ret =
402       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
403           v4l2capture->pool), buffer);
404
405   if (ret != GST_FLOW_OK)
406     goto beach;
407
408   frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
409
410   if (frame) {
411     frame->output_buffer = buffer;
412     buffer = NULL;
413     ret = gst_video_decoder_finish_frame (decoder, frame);
414
415     if (ret != GST_FLOW_OK)
416       goto beach;
417   } else {
418     GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
419     gst_buffer_unref (buffer);
420   }
421
422   return;
423
424 beach:
425   GST_DEBUG_OBJECT (decoder, "Leaving output thread");
426
427   gst_buffer_replace (&buffer, NULL);
428   self->output_flow = ret;
429   g_atomic_int_set (&self->processing, FALSE);
430   gst_v4l2_object_unlock (self->v4l2output);
431   gst_pad_pause_task (decoder->srcpad);
432 }
433
434 static GstFlowReturn
435 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
436     GstVideoCodecFrame * frame)
437 {
438   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
439   GstFlowReturn ret = GST_FLOW_OK;
440
441   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
442
443   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
444     goto flushing;
445
446   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
447     if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps))
448       goto not_negotiated;
449   }
450
451   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
452     GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
453     GstVideoInfo info;
454     GstVideoCodecState *output_state;
455     GstBuffer *codec_data;
456
457     GST_DEBUG_OBJECT (self, "Sending header");
458
459     codec_data = self->input_state->codec_data;
460
461     /* We are running in byte-stream mode, so we don't know the headers, but
462      * we need to send something, otherwise the decoder will refuse to
463      * intialize.
464      */
465     if (codec_data) {
466       gst_buffer_ref (codec_data);
467     } else {
468       codec_data = frame->input_buffer;
469       frame->input_buffer = NULL;
470     }
471
472     if (!gst_buffer_pool_set_active (pool, TRUE))
473       goto activate_failed;
474
475     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
476     gst_v4l2_object_unlock_stop (self->v4l2output);
477     ret =
478         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
479             v4l2output->pool), codec_data);
480     gst_v4l2_object_unlock (self->v4l2output);
481     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
482
483     gst_buffer_unref (codec_data);
484
485     if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
486       goto not_negotiated;
487
488     output_state = gst_video_decoder_set_output_state (decoder,
489         info.finfo->format, info.width, info.height, self->input_state);
490
491     /* Copy the rest of the information, there might be more in the future */
492     output_state->info.interlace_mode = info.interlace_mode;
493     gst_video_codec_state_unref (output_state);
494
495     if (!gst_video_decoder_negotiate (decoder)) {
496       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
497         goto flushing;
498       else
499         goto not_negotiated;
500     }
501
502     /* Ensure our internal pool is activated */
503     if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
504             TRUE))
505       goto activate_failed;
506   }
507
508   if (g_atomic_int_get (&self->processing) == FALSE) {
509     /* It possible that the processing thread stopped due to an error */
510     if (self->output_flow != GST_FLOW_OK) {
511       GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
512       ret = self->output_flow;
513       goto drop;
514     }
515
516     GST_DEBUG_OBJECT (self, "Starting decoding thread");
517
518     /* Enable processing input */
519     gst_v4l2_object_unlock_stop (self->v4l2output);
520
521     /* Start the processing task, when it quits, the task will disable input
522      * processing to unlock input if draining, or prevent potential block */
523     g_atomic_int_set (&self->processing, TRUE);
524     gst_pad_start_task (decoder->srcpad,
525         (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL);
526   }
527
528   if (frame->input_buffer) {
529     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
530     ret =
531         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
532             pool), frame->input_buffer);
533     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
534
535     if (ret == GST_FLOW_FLUSHING) {
536       if (g_atomic_int_get (&self->processing) == FALSE)
537         ret = self->output_flow;
538     }
539
540     /* No need to keep input arround */
541     gst_buffer_replace (&frame->input_buffer, NULL);
542   }
543
544   gst_video_codec_frame_unref (frame);
545   return ret;
546
547   /* ERRORS */
548 not_negotiated:
549   {
550     GST_ERROR_OBJECT (self, "not negotiated");
551     ret = GST_FLOW_NOT_NEGOTIATED;
552     goto drop;
553   }
554 activate_failed:
555   {
556     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
557         (_("Failed to allocated required memory.")),
558         ("Buffer pool activation failed"));
559     return GST_FLOW_ERROR;
560   }
561 flushing:
562   {
563     ret = GST_FLOW_FLUSHING;
564     goto drop;
565   }
566 drop:
567   {
568     gst_video_decoder_drop_frame (decoder, frame);
569     return ret;
570   }
571 }
572
573 static gboolean
574 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
575     GstQuery * query)
576 {
577   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
578   GstClockTime latency;
579   gboolean ret = FALSE;
580
581   if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
582     ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
583         query);
584
585   latency = self->v4l2capture->min_buffers_for_capture *
586       self->v4l2capture->duration;
587   gst_video_decoder_set_latency (decoder, latency, latency);
588
589   return ret;
590 }
591
592 static gboolean
593 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
594 {
595   gboolean ret = TRUE;
596   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
597
598   switch (GST_QUERY_TYPE (query)) {
599     case GST_QUERY_CAPS:{
600       GstCaps *filter, *result = NULL;
601       GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
602
603       gst_query_parse_caps (query, &filter);
604
605       if (self->probed_srccaps)
606         result = gst_caps_ref (self->probed_srccaps);
607       else
608         result = gst_pad_get_pad_template_caps (pad);
609
610       if (filter) {
611         GstCaps *tmp = result;
612         result =
613             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
614         gst_caps_unref (tmp);
615       }
616
617       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
618
619       gst_query_set_caps_result (query, result);
620       gst_caps_unref (result);
621       break;
622     }
623
624     default:
625       ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
626       break;
627   }
628
629   return ret;
630 }
631
632 static gboolean
633 gst_v4l2_video_dec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
634 {
635   gboolean ret = TRUE;
636   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
637
638   switch (GST_QUERY_TYPE (query)) {
639     case GST_QUERY_CAPS:{
640       GstCaps *filter, *result = NULL;
641       GstPad *pad = GST_VIDEO_DECODER_SINK_PAD (decoder);
642       gst_query_parse_caps (query, &filter);
643
644       if (self->probed_sinkcaps)
645         result = gst_caps_ref (self->probed_sinkcaps);
646       else
647         result = gst_pad_get_pad_template_caps (pad);
648
649       if (filter) {
650         GstCaps *tmp = result;
651         result =
652             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
653         gst_caps_unref (tmp);
654       }
655
656       GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
657
658       gst_query_set_caps_result (query, result);
659       gst_caps_unref (result);
660       break;
661     }
662
663     default:
664       ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_query (decoder, query);
665       break;
666   }
667
668   return ret;
669 }
670
671 static gboolean
672 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
673 {
674   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
675
676   switch (GST_EVENT_TYPE (event)) {
677     case GST_EVENT_FLUSH_START:
678       gst_v4l2_object_unlock (self->v4l2output);
679       gst_v4l2_object_unlock (self->v4l2capture);
680     default:
681       break;
682   }
683
684   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
685 }
686
687 static GstStateChangeReturn
688 gst_v4l2_video_dec_change_state (GstElement * element,
689     GstStateChange transition)
690 {
691   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
692
693   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
694     g_atomic_int_set (&self->active, FALSE);
695     gst_v4l2_object_unlock (self->v4l2output);
696     gst_v4l2_object_unlock (self->v4l2capture);
697   }
698
699   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
700 }
701
702 static void
703 gst_v4l2_video_dec_dispose (GObject * object)
704 {
705   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
706
707   gst_caps_replace (&self->probed_sinkcaps, NULL);
708   gst_caps_replace (&self->probed_srccaps, NULL);
709
710   G_OBJECT_CLASS (parent_class)->dispose (object);
711 }
712
713 static void
714 gst_v4l2_video_dec_finalize (GObject * object)
715 {
716   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
717
718   gst_v4l2_object_destroy (self->v4l2capture);
719   gst_v4l2_object_destroy (self->v4l2output);
720
721   G_OBJECT_CLASS (parent_class)->finalize (object);
722 }
723
724 static void
725 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
726 {
727   /* V4L2 object are created in subinstance_init */
728 }
729
730 static void
731 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
732 {
733   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
734   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
735   GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
736
737   gst_video_decoder_set_packetized (decoder, TRUE);
738
739   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
740       V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
741       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
742   self->v4l2output->no_initial_format = TRUE;
743   self->v4l2output->keep_aspect = FALSE;
744
745   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
746       V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
747       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
748   self->v4l2capture->no_initial_format = TRUE;
749   self->v4l2output->keep_aspect = FALSE;
750
751   g_object_set (self, "device", klass->default_device, NULL);
752 }
753
754 static void
755 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
756 {
757   GstElementClass *element_class;
758   GObjectClass *gobject_class;
759   GstVideoDecoderClass *video_decoder_class;
760
761   parent_class = g_type_class_peek_parent (klass);
762
763   element_class = (GstElementClass *) klass;
764   gobject_class = (GObjectClass *) klass;
765   video_decoder_class = (GstVideoDecoderClass *) klass;
766
767   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
768       "V4L2 Video Decoder");
769
770   gst_element_class_set_static_metadata (element_class,
771       "V4L2 Video Decoder",
772       "Codec/Decoder/Video",
773       "Decode video streams via V4L2 API",
774       "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
775
776   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
777   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
778   gobject_class->set_property =
779       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
780   gobject_class->get_property =
781       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
782
783   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
784   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
785   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
786   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
787   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
788   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
789   video_decoder_class->set_format =
790       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
791   video_decoder_class->negotiate =
792       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
793   video_decoder_class->decide_allocation =
794       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
795   /* FIXME propose_allocation or not ? */
796   video_decoder_class->handle_frame =
797       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
798   video_decoder_class->sink_query =
799       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_query);
800   video_decoder_class->src_query =
801       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
802   video_decoder_class->sink_event =
803       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
804
805   element_class->change_state =
806       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
807
808   gst_v4l2_object_install_properties_helper (gobject_class,
809       DEFAULT_PROP_DEVICE);
810
811   /**
812    * GstV4l2VideoDec:capture-io-mode
813    *
814    * Capture IO Mode
815    */
816   g_object_class_install_property (gobject_class, PROP_IO_MODE,
817       g_param_spec_enum ("capture-io-mode", "Capture IO mode",
818           "Capture I/O mode",
819           GST_TYPE_V4L2_IO_MODE, GST_V4L2_IO_AUTO,
820           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
821 }
822
823 static void
824 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
825 {
826   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
827   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
828   GstV4l2VideoDecCData *cdata = data;
829
830   klass->default_device = cdata->device;
831
832   /* Note: gst_pad_template_new() take the floating ref from the caps */
833   gst_element_class_add_pad_template (element_class,
834       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
835           cdata->sink_caps));
836   gst_element_class_add_pad_template (element_class,
837       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
838           cdata->src_caps));
839
840   g_free (cdata);
841 }
842
843 /* Probing functions */
844 gboolean
845 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
846 {
847   gboolean ret = FALSE;
848
849   if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
850       && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
851     ret = TRUE;
852
853   return ret;
854 }
855
856 gboolean
857 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
858     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
859 {
860   GTypeQuery type_query;
861   GTypeInfo type_info = { 0, };
862   GType type, subtype;
863   gchar *type_name;
864   GstV4l2VideoDecCData *cdata;
865
866   cdata = g_new0 (GstV4l2VideoDecCData, 1);
867   cdata->device = g_strdup (device_path);
868   cdata->sink_caps = gst_caps_ref (sink_caps);
869   cdata->src_caps = gst_caps_ref (src_caps);
870
871   type = gst_v4l2_video_dec_get_type ();
872   g_type_query (type, &type_query);
873   memset (&type_info, 0, sizeof (type_info));
874   type_info.class_size = type_query.class_size;
875   type_info.instance_size = type_query.instance_size;
876   type_info.class_init = gst_v4l2_video_dec_subclass_init;
877   type_info.class_data = cdata;
878   type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
879
880   type_name = g_strdup_printf ("v4l2%sdec", basename);
881   subtype = g_type_register_static (type, type_name, &type_info, 0);
882
883   gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
884
885   g_free (type_name);
886
887   return TRUE;
888 }