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