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