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