v4l2videodec: Add trace for FLUSH_START/STOP handling
[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, "Flushed");
263
264   /* Ensure the processing thread has stopped for the reverse playback
265    * discount case */
266   if (g_atomic_int_get (&self->processing)) {
267     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
268
269     gst_v4l2_object_unlock (self->v4l2output);
270     gst_v4l2_object_unlock (self->v4l2capture);
271     gst_pad_stop_task (decoder->srcpad);
272     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
273   }
274
275   self->output_flow = GST_FLOW_OK;
276
277   gst_v4l2_object_unlock_stop (self->v4l2output);
278   gst_v4l2_object_unlock_stop (self->v4l2capture);
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
312   /* and ensure the processing thread has stopped in case another error
313    * occured. */
314   gst_v4l2_object_unlock (self->v4l2capture);
315   gst_pad_stop_task (decoder->srcpad);
316   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
317
318   if (ret == GST_FLOW_FLUSHING)
319     ret = self->output_flow;
320
321   GST_DEBUG_OBJECT (decoder, "Done draining buffers");
322
323 done:
324   return ret;
325 }
326
327 static GstVideoCodecFrame *
328 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
329 {
330   GstVideoCodecFrame *frame = NULL;
331   GList *frames, *l;
332   gint count = 0;
333
334   frames = gst_video_decoder_get_frames (decoder);
335
336   for (l = frames; l != NULL; l = l->next) {
337     GstVideoCodecFrame *f = l->data;
338
339     if (!frame || frame->pts > f->pts)
340       frame = f;
341
342     count++;
343   }
344
345   if (frame) {
346     GST_LOG_OBJECT (decoder,
347         "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
348         frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
349     gst_video_codec_frame_ref (frame);
350   }
351
352   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
353
354   return frame;
355 }
356
357 static void
358 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
359 {
360   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
361   GstBufferPool *pool;
362   GstVideoCodecFrame *frame;
363   GstBuffer *buffer = NULL;
364   GstFlowReturn ret;
365
366   GST_LOG_OBJECT (decoder, "Allocate output buffer");
367
368   /* We cannot use the base class allotate helper since it taking the internal
369    * stream lock. we know that the acquire may need to poll until more frames
370    * comes in and holding this lock would prevent that.
371    */
372   pool = gst_video_decoder_get_buffer_pool (decoder);
373
374   /* Pool may be NULL if we started going to READY state */
375   if (pool == NULL) {
376     ret = GST_FLOW_FLUSHING;
377     goto beach;
378   }
379
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   GST_LOG_OBJECT (decoder, "Process output buffer");
387   ret =
388       gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
389           v4l2capture->pool), &buffer);
390
391   if (ret != GST_FLOW_OK)
392     goto beach;
393
394   frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
395
396   if (frame) {
397     frame->output_buffer = buffer;
398     buffer = NULL;
399     ret = gst_video_decoder_finish_frame (decoder, frame);
400
401     if (ret != GST_FLOW_OK)
402       goto beach;
403   } else {
404     GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
405     gst_buffer_unref (buffer);
406   }
407
408   return;
409
410 beach:
411   GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
412       gst_flow_get_name (ret));
413
414   gst_buffer_replace (&buffer, NULL);
415   self->output_flow = ret;
416   g_atomic_int_set (&self->processing, FALSE);
417   gst_v4l2_object_unlock (self->v4l2output);
418   gst_pad_pause_task (decoder->srcpad);
419 }
420
421 static GstFlowReturn
422 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
423     GstVideoCodecFrame * frame)
424 {
425   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
426   GstFlowReturn ret = GST_FLOW_OK;
427
428   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
429
430   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
431     goto flushing;
432
433   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
434     if (!self->input_state)
435       goto not_negotiated;
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     GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
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     /* Ensure input internal pool is active */
462     if (!gst_buffer_pool_is_active (pool)) {
463       GstStructure *config = gst_buffer_pool_get_config (pool);
464       gst_buffer_pool_config_set_params (config, self->input_state->caps,
465           self->v4l2output->info.size, 2, 2);
466
467       /* There is no reason to refuse this config */
468       if (!gst_buffer_pool_set_config (pool, config))
469         goto activate_failed;
470
471       if (!gst_buffer_pool_set_active (pool, TRUE))
472         goto activate_failed;
473     }
474
475     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
476     ret =
477         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
478             v4l2output->pool), &codec_data);
479     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
480
481     gst_buffer_unref (codec_data);
482
483     if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
484       goto not_negotiated;
485
486     output_state = gst_video_decoder_set_output_state (decoder,
487         info.finfo->format, info.width, info.height, self->input_state);
488
489     /* Copy the rest of the information, there might be more in the future */
490     output_state->info.interlace_mode = info.interlace_mode;
491     gst_video_codec_state_unref (output_state);
492
493     if (!gst_video_decoder_negotiate (decoder)) {
494       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
495         goto flushing;
496       else
497         goto not_negotiated;
498     }
499
500     /* Ensure our internal pool is activated */
501     if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
502             TRUE))
503       goto activate_failed;
504   }
505
506   if (g_atomic_int_get (&self->processing) == FALSE) {
507     /* It possible that the processing thread stopped due to an error */
508     if (self->output_flow != GST_FLOW_OK) {
509       GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
510       ret = self->output_flow;
511       goto drop;
512     }
513
514     GST_DEBUG_OBJECT (self, "Starting decoding thread");
515
516     /* Start the processing task, when it quits, the task will disable input
517      * processing to unlock input if draining, or prevent potential block */
518     g_atomic_int_set (&self->processing, TRUE);
519     gst_pad_start_task (decoder->srcpad,
520         (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL);
521   }
522
523   if (frame->input_buffer) {
524     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
525     ret =
526         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
527             pool), &frame->input_buffer);
528     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
529
530     if (ret == GST_FLOW_FLUSHING) {
531       if (g_atomic_int_get (&self->processing) == FALSE)
532         ret = self->output_flow;
533       goto drop;
534     } else if (ret != GST_FLOW_OK) {
535       goto process_failed;
536     }
537
538     /* No need to keep input arround */
539     gst_buffer_replace (&frame->input_buffer, NULL);
540   }
541
542   gst_video_codec_frame_unref (frame);
543   return ret;
544
545   /* ERRORS */
546 not_negotiated:
547   {
548     GST_ERROR_OBJECT (self, "not negotiated");
549     ret = GST_FLOW_NOT_NEGOTIATED;
550     goto drop;
551   }
552 activate_failed:
553   {
554     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
555         (_("Failed to allocate required memory.")),
556         ("Buffer pool activation failed"));
557     ret = GST_FLOW_ERROR;
558     goto drop;
559   }
560 flushing:
561   {
562     ret = GST_FLOW_FLUSHING;
563     goto drop;
564   }
565 process_failed:
566   {
567     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
568         (_("Failed to process frame.")),
569         ("Maybe be due to not enough memory or failing driver"));
570     ret = GST_FLOW_ERROR;
571     goto drop;
572   }
573 drop:
574   {
575     gst_video_decoder_drop_frame (decoder, frame);
576     return ret;
577   }
578 }
579
580 static gboolean
581 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
582     GstQuery * query)
583 {
584   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
585   GstClockTime latency;
586   gboolean ret = FALSE;
587
588   if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
589     ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
590         query);
591
592   latency = self->v4l2capture->min_buffers_for_capture *
593       self->v4l2capture->duration;
594   gst_video_decoder_set_latency (decoder, latency, latency);
595
596   return ret;
597 }
598
599 static gboolean
600 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
601 {
602   gboolean ret = TRUE;
603   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
604
605   switch (GST_QUERY_TYPE (query)) {
606     case GST_QUERY_CAPS:{
607       GstCaps *filter, *result = NULL;
608       GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
609
610       gst_query_parse_caps (query, &filter);
611
612       if (self->probed_srccaps)
613         result = gst_caps_ref (self->probed_srccaps);
614       else
615         result = gst_pad_get_pad_template_caps (pad);
616
617       if (filter) {
618         GstCaps *tmp = result;
619         result =
620             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
621         gst_caps_unref (tmp);
622       }
623
624       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
625
626       gst_query_set_caps_result (query, result);
627       gst_caps_unref (result);
628       break;
629     }
630
631     default:
632       ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
633       break;
634   }
635
636   return ret;
637 }
638
639 static gboolean
640 gst_v4l2_video_dec_sink_query (GstVideoDecoder * decoder, GstQuery * query)
641 {
642   gboolean ret = TRUE;
643   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
644
645   switch (GST_QUERY_TYPE (query)) {
646     case GST_QUERY_CAPS:{
647       GstCaps *filter, *result = NULL;
648       GstPad *pad = GST_VIDEO_DECODER_SINK_PAD (decoder);
649       gst_query_parse_caps (query, &filter);
650
651       if (self->probed_sinkcaps)
652         result = gst_caps_ref (self->probed_sinkcaps);
653       else
654         result = gst_pad_get_pad_template_caps (pad);
655
656       if (filter) {
657         GstCaps *tmp = result;
658         result =
659             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
660         gst_caps_unref (tmp);
661       }
662
663       GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
664
665       gst_query_set_caps_result (query, result);
666       gst_caps_unref (result);
667       break;
668     }
669
670     default:
671       ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_query (decoder, query);
672       break;
673   }
674
675   return ret;
676 }
677
678 static gboolean
679 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
680 {
681   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
682   gboolean ret;
683
684   switch (GST_EVENT_TYPE (event)) {
685     case GST_EVENT_FLUSH_START:
686       GST_DEBUG_OBJECT (self, "flush start");
687       gst_v4l2_object_unlock (self->v4l2output);
688       gst_v4l2_object_unlock (self->v4l2capture);
689       break;
690     default:
691       break;
692   }
693
694   ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
695
696   switch (GST_EVENT_TYPE (event)) {
697     case GST_EVENT_FLUSH_START:
698       /* The processing thread should stop now, wait for it */
699       gst_pad_stop_task (decoder->srcpad);
700       GST_DEBUG_OBJECT (self, "flush start done");
701       break;
702     default:
703       break;
704   }
705
706   return ret;
707 }
708
709 static GstStateChangeReturn
710 gst_v4l2_video_dec_change_state (GstElement * element,
711     GstStateChange transition)
712 {
713   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
714   GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
715
716   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
717     g_atomic_int_set (&self->active, FALSE);
718     gst_v4l2_object_unlock (self->v4l2output);
719     gst_v4l2_object_unlock (self->v4l2capture);
720     gst_pad_stop_task (decoder->srcpad);
721   }
722
723   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
724 }
725
726 static void
727 gst_v4l2_video_dec_dispose (GObject * object)
728 {
729   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
730
731   gst_caps_replace (&self->probed_sinkcaps, NULL);
732   gst_caps_replace (&self->probed_srccaps, NULL);
733
734   G_OBJECT_CLASS (parent_class)->dispose (object);
735 }
736
737 static void
738 gst_v4l2_video_dec_finalize (GObject * object)
739 {
740   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
741
742   gst_v4l2_object_destroy (self->v4l2capture);
743   gst_v4l2_object_destroy (self->v4l2output);
744
745   G_OBJECT_CLASS (parent_class)->finalize (object);
746 }
747
748 static void
749 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
750 {
751   /* V4L2 object are created in subinstance_init */
752 }
753
754 static void
755 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
756 {
757   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
758   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
759   GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
760
761   gst_video_decoder_set_packetized (decoder, TRUE);
762
763   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
764       V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
765       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
766   self->v4l2output->no_initial_format = TRUE;
767   self->v4l2output->keep_aspect = FALSE;
768
769   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
770       V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
771       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
772   self->v4l2capture->no_initial_format = TRUE;
773   self->v4l2output->keep_aspect = FALSE;
774 }
775
776 static void
777 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
778 {
779   GstElementClass *element_class;
780   GObjectClass *gobject_class;
781   GstVideoDecoderClass *video_decoder_class;
782
783   parent_class = g_type_class_peek_parent (klass);
784
785   element_class = (GstElementClass *) klass;
786   gobject_class = (GObjectClass *) klass;
787   video_decoder_class = (GstVideoDecoderClass *) klass;
788
789   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
790       "V4L2 Video Decoder");
791
792   gst_element_class_set_static_metadata (element_class,
793       "V4L2 Video Decoder",
794       "Codec/Decoder/Video",
795       "Decode video streams via V4L2 API",
796       "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
797
798   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
799   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
800   gobject_class->set_property =
801       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
802   gobject_class->get_property =
803       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
804
805   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
806   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
807   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
808   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
809   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
810   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
811   video_decoder_class->set_format =
812       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
813   video_decoder_class->negotiate =
814       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
815   video_decoder_class->decide_allocation =
816       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
817   /* FIXME propose_allocation or not ? */
818   video_decoder_class->handle_frame =
819       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
820   video_decoder_class->sink_query =
821       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_query);
822   video_decoder_class->src_query =
823       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
824   video_decoder_class->sink_event =
825       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
826
827   element_class->change_state =
828       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
829
830   gst_v4l2_object_install_m2m_properties_helper (gobject_class);
831 }
832
833 static void
834 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
835 {
836   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
837   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
838   GstV4l2VideoDecCData *cdata = data;
839
840   klass->default_device = cdata->device;
841
842   /* Note: gst_pad_template_new() take the floating ref from the caps */
843   gst_element_class_add_pad_template (element_class,
844       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
845           cdata->sink_caps));
846   gst_element_class_add_pad_template (element_class,
847       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
848           cdata->src_caps));
849
850   g_free (cdata);
851 }
852
853 /* Probing functions */
854 gboolean
855 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
856 {
857   gboolean ret = FALSE;
858
859   if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
860       && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
861     ret = TRUE;
862
863   return ret;
864 }
865
866 gboolean
867 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
868     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
869 {
870   GTypeQuery type_query;
871   GTypeInfo type_info = { 0, };
872   GType type, subtype;
873   gchar *type_name;
874   GstV4l2VideoDecCData *cdata;
875
876   cdata = g_new0 (GstV4l2VideoDecCData, 1);
877   cdata->device = g_strdup (device_path);
878   cdata->sink_caps = gst_caps_ref (sink_caps);
879   cdata->src_caps = gst_caps_ref (src_caps);
880
881   type = gst_v4l2_video_dec_get_type ();
882   g_type_query (type, &type_query);
883   memset (&type_info, 0, sizeof (type_info));
884   type_info.class_size = type_query.class_size;
885   type_info.instance_size = type_query.instance_size;
886   type_info.class_init = gst_v4l2_video_dec_subclass_init;
887   type_info.class_data = cdata;
888   type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
889
890   type_name = g_strdup_printf ("v4l2%sdec", basename);
891   subtype = g_type_register_static (type, type_name, &type_info, 0);
892
893   gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
894
895   g_free (type_name);
896
897   return TRUE;
898 }