v4l2videodec: handle stop being called without flush
[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
255     /* FIXME we probably need to do more work if pools are active */
256   }
257
258   ret = gst_v4l2_object_set_format (self->v4l2output, state->caps);
259
260   if (ret)
261     self->input_state = gst_video_codec_state_ref (state);
262
263 done:
264   return ret;
265 }
266
267 static gboolean
268 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
269 {
270   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
271
272   GST_DEBUG_OBJECT (self, "Flushing");
273
274   /* Wait for capture thread to stop */
275   gst_pad_stop_task (decoder->srcpad);
276   self->output_flow = GST_FLOW_OK;
277
278   gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2output->pool));
279   gst_v4l2_buffer_pool_flush (GST_V4L2_BUFFER_POOL (self->v4l2capture->pool));
280
281   /* Output will remain flushing until new frame comes in */
282   gst_v4l2_object_unlock_stop (self->v4l2capture);
283
284   return TRUE;
285 }
286
287 static gboolean
288 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
289 {
290   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
291 }
292
293 static GstFlowReturn
294 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
295 {
296   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
297   GstFlowReturn ret = GST_FLOW_OK;
298   GstBuffer *buffer;
299
300   if (!self->processing)
301     goto done;
302
303   GST_DEBUG_OBJECT (self, "Finishing decoding");
304
305   /* Keep queuing empty buffers until the processing thread has stopped,
306    * _pool_process() will return FLUSHING when that happened */
307   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
308   while (ret == GST_FLOW_OK) {
309     buffer = gst_buffer_new ();
310     ret =
311         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
312             v4l2output->pool), buffer);
313     gst_buffer_unref (buffer);
314   }
315   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
316
317   /* Ensure the processing thread has stopped */
318   if (self->processing) {
319     gst_v4l2_object_unlock (self->v4l2capture);
320     gst_pad_stop_task (decoder->srcpad);
321     g_assert (g_atomic_int_get (&self->processing) == FALSE);
322   }
323
324   if (ret == GST_FLOW_FLUSHING)
325     ret = self->output_flow;
326
327   GST_DEBUG_OBJECT (decoder, "Done draining buffers");
328
329 done:
330   return ret;
331 }
332
333 static GstVideoCodecFrame *
334 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
335 {
336   GstVideoCodecFrame *frame = NULL;
337   GList *frames, *l;
338   gint count = 0;
339
340   frames = gst_video_decoder_get_frames (decoder);
341
342   for (l = frames; l != NULL; l = l->next) {
343     GstVideoCodecFrame *f = l->data;
344
345     if (!frame || frame->pts > f->pts)
346       frame = f;
347
348     count++;
349   }
350
351   if (frame) {
352     GST_LOG_OBJECT (decoder,
353         "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
354         frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
355     gst_video_codec_frame_ref (frame);
356   }
357
358   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
359
360   return frame;
361 }
362
363 static void
364 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
365 {
366   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
367   GstBufferPool *pool;
368   GstVideoCodecFrame *frame;
369   GstBuffer *buffer = NULL;
370   GstFlowReturn ret;
371
372   GST_LOG_OBJECT (decoder, "Allocate output buffer");
373
374   /* We cannot use the base class allotate helper since it taking the internal
375    * stream lock. we know that the acquire may need to poll until more frames
376    * comes in and holding this lock would prevent that.
377    */
378   pool = gst_video_decoder_get_buffer_pool (decoder);
379   ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
380   g_object_unref (pool);
381
382   if (ret != GST_FLOW_OK)
383     goto beach;
384
385   /* Check if buffer isn't the last one */
386   if (gst_buffer_get_size (buffer) == 0)
387     goto beach;
388
389   GST_LOG_OBJECT (decoder, "Process output buffer");
390   ret =
391       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
392           v4l2capture->pool), buffer);
393
394   if (ret != GST_FLOW_OK)
395     goto beach;
396
397   frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
398
399   if (frame) {
400     frame->output_buffer = buffer;
401     buffer = NULL;
402     ret = gst_video_decoder_finish_frame (decoder, frame);
403
404     if (ret != GST_FLOW_OK)
405       goto beach;
406   } else {
407     GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
408     gst_buffer_unref (buffer);
409   }
410
411   return;
412
413 beach:
414   GST_DEBUG_OBJECT (decoder, "Leaving output thread");
415
416   gst_buffer_replace (&buffer, NULL);
417   self->output_flow = ret;
418   g_atomic_int_set (&self->processing, FALSE);
419   gst_v4l2_object_unlock (self->v4l2output);
420   gst_pad_pause_task (decoder->srcpad);
421 }
422
423 static GstFlowReturn
424 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
425     GstVideoCodecFrame * frame)
426 {
427   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
428   GstFlowReturn ret = GST_FLOW_OK;
429
430   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
431
432   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
433     goto flushing;
434
435   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
436     if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps))
437       goto not_negotiated;
438   }
439
440   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
441     GstVideoInfo info;
442     GstVideoCodecState *output_state;
443     GstBuffer *codec_data;
444
445     GST_DEBUG_OBJECT (self, "Sending header");
446
447     codec_data = self->input_state->codec_data;
448
449     /* We are running in byte-stream mode, so we don't know the headers, but
450      * we need to send something, otherwise the decoder will refuse to
451      * intialize.
452      */
453     if (codec_data) {
454       gst_buffer_ref (codec_data);
455     } else {
456       codec_data = frame->input_buffer;
457       frame->input_buffer = NULL;
458     }
459
460     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
461     gst_v4l2_object_unlock_stop (self->v4l2output);
462     ret =
463         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
464             v4l2output->pool), codec_data);
465     gst_v4l2_object_unlock (self->v4l2output);
466     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
467
468     gst_buffer_unref (codec_data);
469
470     if (!gst_v4l2_object_setup_format (self->v4l2capture, &info, &self->align))
471       goto not_negotiated;
472
473     output_state = gst_video_decoder_set_output_state (decoder,
474         info.finfo->format, info.width, info.height, self->input_state);
475
476     /* Copy the rest of the information, there might be more in the future */
477     output_state->info.interlace_mode = info.interlace_mode;
478     gst_video_codec_state_unref (output_state);
479
480     if (!gst_video_decoder_negotiate (decoder)) {
481       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
482         goto flushing;
483       else
484         goto not_negotiated;
485     }
486   }
487
488   if (g_atomic_int_get (&self->processing) == FALSE) {
489     /* It possible that the processing thread stopped due to an error */
490     if (self->output_flow != GST_FLOW_OK) {
491       GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
492       ret = self->output_flow;
493       goto drop;
494     }
495
496     GST_DEBUG_OBJECT (self, "Starting decoding thread");
497
498     /* Enable processing input */
499     gst_v4l2_object_unlock_stop (self->v4l2output);
500
501     /* Start the processing task, when it quits, the task will disable input
502      * processing to unlock input if draining, or prevent potential block */
503     g_atomic_int_set (&self->processing, TRUE);
504     gst_pad_start_task (decoder->srcpad,
505         (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL);
506   }
507
508   if (frame->input_buffer) {
509     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
510     ret =
511         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
512             pool), frame->input_buffer);
513     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
514
515     if (ret == GST_FLOW_FLUSHING) {
516       if (g_atomic_int_get (&self->processing) == FALSE)
517         ret = self->output_flow;
518     }
519
520     /* No need to keep input arround */
521     gst_buffer_replace (&frame->input_buffer, NULL);
522   }
523
524   gst_video_codec_frame_unref (frame);
525   return ret;
526
527   /* ERRORS */
528 not_negotiated:
529   {
530     GST_ERROR_OBJECT (self, "not negotiated");
531     ret = GST_FLOW_NOT_NEGOTIATED;
532     goto drop;
533   }
534 flushing:
535   {
536     ret = GST_FLOW_FLUSHING;
537     goto drop;
538   }
539 drop:
540   {
541     gst_video_decoder_drop_frame (decoder, frame);
542     return ret;
543   }
544 }
545
546 static gboolean
547 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
548     GstQuery * query)
549 {
550   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
551   GstClockTime latency;
552   gboolean ret = FALSE;
553
554   if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
555     ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
556         query);
557
558   latency = self->v4l2capture->min_buffers_for_capture *
559       self->v4l2capture->duration;
560   gst_video_decoder_set_latency (decoder, latency, latency);
561
562   return ret;
563 }
564
565 static gboolean
566 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
567 {
568   gboolean ret = TRUE;
569   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
570
571   switch (GST_QUERY_TYPE (query)) {
572     case GST_QUERY_CAPS:{
573       GstCaps *filter, *result = NULL;
574       GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
575
576       gst_query_parse_caps (query, &filter);
577
578       if (self->probed_srccaps)
579         result = gst_caps_ref (self->probed_srccaps);
580       else
581         result = gst_pad_get_pad_template_caps (pad);
582
583       if (filter) {
584         GstCaps *tmp = result;
585         result =
586             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
587         gst_caps_unref (tmp);
588       }
589
590       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
591
592       gst_query_set_caps_result (query, result);
593       gst_caps_unref (result);
594       break;
595     }
596
597     default:
598       ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
599       break;
600   }
601
602   return ret;
603 }
604
605 static gboolean
606 gst_v4l2_video_dec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
607 {
608   gboolean ret = TRUE;
609   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
610
611   switch (GST_QUERY_TYPE (query)) {
612     case GST_QUERY_CAPS:{
613       GstCaps *filter, *result = NULL;
614       GstPad *pad = GST_VIDEO_DECODER_SINK_PAD (decoder);
615       gst_query_parse_caps (query, &filter);
616
617       if (self->probed_sinkcaps)
618         result = gst_caps_ref (self->probed_sinkcaps);
619       else
620         result = gst_pad_get_pad_template_caps (pad);
621
622       if (filter) {
623         GstCaps *tmp = result;
624         result =
625             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
626         gst_caps_unref (tmp);
627       }
628
629       GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
630
631       gst_query_set_caps_result (query, result);
632       gst_caps_unref (result);
633       break;
634     }
635
636     default:
637       ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_query (decoder, query);
638       break;
639   }
640
641   return ret;
642 }
643
644 static gboolean
645 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
646 {
647   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
648
649   switch (GST_EVENT_TYPE (event)) {
650     case GST_EVENT_FLUSH_START:
651       gst_v4l2_object_unlock (self->v4l2output);
652       gst_v4l2_object_unlock (self->v4l2capture);
653     default:
654       break;
655   }
656
657   return GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
658 }
659
660 static GstStateChangeReturn
661 gst_v4l2_video_dec_change_state (GstElement * element,
662     GstStateChange transition)
663 {
664   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
665
666   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
667     g_atomic_int_set (&self->active, FALSE);
668     gst_v4l2_object_unlock (self->v4l2output);
669     gst_v4l2_object_unlock (self->v4l2capture);
670   }
671
672   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
673 }
674
675 static void
676 gst_v4l2_video_dec_dispose (GObject * object)
677 {
678   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
679
680   gst_caps_replace (&self->probed_sinkcaps, NULL);
681   gst_caps_replace (&self->probed_srccaps, NULL);
682
683   G_OBJECT_CLASS (parent_class)->dispose (object);
684 }
685
686 static void
687 gst_v4l2_video_dec_finalize (GObject * object)
688 {
689   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
690
691   gst_v4l2_object_destroy (self->v4l2capture);
692   gst_v4l2_object_destroy (self->v4l2output);
693
694   G_OBJECT_CLASS (parent_class)->finalize (object);
695 }
696
697 static void
698 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
699 {
700   /* V4L2 object are created in subinstance_init */
701 }
702
703 static void
704 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
705 {
706   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
707   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
708   GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
709
710   gst_video_decoder_set_packetized (decoder, TRUE);
711
712   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
713       V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
714       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
715   self->v4l2output->no_initial_format = TRUE;
716   self->v4l2output->keep_aspect = FALSE;
717
718   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
719       V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
720       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
721   self->v4l2capture->no_initial_format = TRUE;
722   self->v4l2output->keep_aspect = FALSE;
723
724   g_object_set (self, "device", klass->default_device, NULL);
725 }
726
727 static void
728 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
729 {
730   GstElementClass *element_class;
731   GObjectClass *gobject_class;
732   GstVideoDecoderClass *video_decoder_class;
733
734   parent_class = g_type_class_peek_parent (klass);
735
736   element_class = (GstElementClass *) klass;
737   gobject_class = (GObjectClass *) klass;
738   video_decoder_class = (GstVideoDecoderClass *) klass;
739
740   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
741       "V4L2 Video Decoder");
742
743   gst_element_class_set_static_metadata (element_class,
744       "V4L2 Video Decoder",
745       "Codec/Decoder/Video",
746       "Decode video streams via V4L2 API",
747       "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
748
749   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
750   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
751   gobject_class->set_property =
752       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
753   gobject_class->get_property =
754       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
755
756   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
757   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
758   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
759   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
760   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
761   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
762   video_decoder_class->set_format =
763       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
764   video_decoder_class->negotiate =
765       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
766   video_decoder_class->decide_allocation =
767       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
768   /* FIXME propose_allocation or not ? */
769   video_decoder_class->handle_frame =
770       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
771   video_decoder_class->sink_query =
772       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_query);
773   video_decoder_class->src_query =
774       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
775   video_decoder_class->sink_event =
776       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
777
778   element_class->change_state =
779       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
780
781   gst_v4l2_object_install_properties_helper (gobject_class,
782       DEFAULT_PROP_DEVICE);
783
784   /**
785    * GstV4l2VideoDec:capture-io-mode
786    *
787    * Capture IO Mode
788    */
789   g_object_class_install_property (gobject_class, PROP_IO_MODE,
790       g_param_spec_enum ("capture-io-mode", "Capture IO mode",
791           "Capture I/O mode",
792           GST_TYPE_V4L2_IO_MODE, GST_V4L2_IO_AUTO,
793           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
794 }
795
796 static void
797 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
798 {
799   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
800   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
801   GstV4l2VideoDecCData *cdata = data;
802
803   klass->default_device = cdata->device;
804
805   /* Note: gst_pad_template_new() take the floating ref from the caps */
806   gst_element_class_add_pad_template (element_class,
807       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
808           cdata->sink_caps));
809   gst_element_class_add_pad_template (element_class,
810       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
811           cdata->src_caps));
812
813   g_free (cdata);
814 }
815
816 /* Probing functions */
817 gboolean
818 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
819 {
820   gboolean ret = FALSE;
821
822   if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
823       && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
824     ret = TRUE;
825
826   return ret;
827 }
828
829 gboolean
830 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
831     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
832 {
833   GTypeQuery type_query;
834   GTypeInfo type_info = { 0, };
835   GType type, subtype;
836   gchar *type_name;
837   GstV4l2VideoDecCData *cdata;
838
839   cdata = g_new0 (GstV4l2VideoDecCData, 1);
840   cdata->device = g_strdup (device_path);
841   cdata->sink_caps = gst_caps_ref (sink_caps);
842   cdata->src_caps = gst_caps_ref (src_caps);
843
844   type = gst_v4l2_video_dec_get_type ();
845   g_type_query (type, &type_query);
846   memset (&type_info, 0, sizeof (type_info));
847   type_info.class_size = type_query.class_size;
848   type_info.instance_size = type_query.instance_size;
849   type_info.class_init = gst_v4l2_video_dec_subclass_init;
850   type_info.class_data = cdata;
851   type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
852
853   type_name = g_strdup_printf ("v4l2%sdec", basename);
854   subtype = g_type_register_static (type, type_name, &type_info, 0);
855
856   gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
857
858   g_free (type_name);
859
860   return TRUE;
861 }