v4l2videodec: Coding style fixes
[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_OUTPUT_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->v4l2capture, prop_id, value,
99           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   GstV4l2Error error = GST_V4L2_ERROR_INIT;
233   gboolean ret = TRUE;
234   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
235
236   GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
237
238   if (self->input_state) {
239     if (gst_v4l2_object_caps_equal (self->v4l2output, state->caps)) {
240       GST_DEBUG_OBJECT (self, "Compatible caps");
241       goto done;
242     }
243     gst_video_codec_state_unref (self->input_state);
244     self->input_state = NULL;
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, &error);
250
251   if (ret)
252     self->input_state = gst_video_codec_state_ref (state);
253   else
254     gst_v4l2_error (self, &error);
255
256 done:
257   return ret;
258 }
259
260 static gboolean
261 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
262 {
263   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
264
265   GST_DEBUG_OBJECT (self, "Flushed");
266
267   /* Ensure the processing thread has stopped for the reverse playback
268    * discount case */
269   if (g_atomic_int_get (&self->processing)) {
270     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
271
272     gst_v4l2_object_unlock (self->v4l2output);
273     gst_v4l2_object_unlock (self->v4l2capture);
274     gst_pad_stop_task (decoder->srcpad);
275     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
276   }
277
278   self->output_flow = GST_FLOW_OK;
279
280   gst_v4l2_object_unlock_stop (self->v4l2output);
281   gst_v4l2_object_unlock_stop (self->v4l2capture);
282
283   return TRUE;
284 }
285
286 static gboolean
287 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
288 {
289   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
290
291   /* We don't allow renegotiation without carefull disabling the pool */
292   if (self->v4l2capture->pool &&
293       gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
294     return TRUE;
295
296   return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
297 }
298
299 static gboolean
300 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
301 {
302   struct v4l2_decoder_cmd dcmd = { 0, };
303
304   GST_DEBUG_OBJECT (v4l2object->element,
305       "sending v4l2 decoder command %u with flags %u", cmd, flags);
306
307   if (!GST_V4L2_IS_OPEN (v4l2object))
308     return FALSE;
309
310   dcmd.cmd = cmd;
311   dcmd.flags = flags;
312   if (v4l2_ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
313     goto dcmd_failed;
314
315   return TRUE;
316
317 dcmd_failed:
318   if (errno == ENOTTY) {
319     GST_INFO_OBJECT (v4l2object->element,
320         "Failed to send decoder command %u with flags %u for '%s'. (%s)",
321         cmd, flags, v4l2object->videodev, g_strerror (errno));
322   } else {
323     GST_ERROR_OBJECT (v4l2object->element,
324         "Failed to send decoder command %u with flags %u for '%s'. (%s)",
325         cmd, flags, v4l2object->videodev, g_strerror (errno));
326   }
327   return FALSE;
328 }
329
330 static GstFlowReturn
331 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
332 {
333   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
334   GstFlowReturn ret = GST_FLOW_OK;
335   GstBuffer *buffer;
336
337   if (!g_atomic_int_get (&self->processing))
338     goto done;
339
340   GST_DEBUG_OBJECT (self, "Finishing decoding");
341
342   GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
343
344   if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
345     GstTask *task = decoder->srcpad->task;
346
347     /* If the decoder stop command succeeded, just wait until processing is
348      * finished */
349     GST_OBJECT_LOCK (task);
350     while (GST_TASK_STATE (task) == GST_TASK_STARTED)
351       GST_TASK_WAIT (task);
352     GST_OBJECT_UNLOCK (task);
353     ret = GST_FLOW_FLUSHING;
354   } else {
355     /* otherwise keep queuing empty buffers until the processing thread has
356      * stopped, _pool_process() will return FLUSHING when that happened */
357     while (ret == GST_FLOW_OK) {
358       buffer = gst_buffer_new ();
359       ret =
360           gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
361               v4l2output->pool), &buffer);
362       gst_buffer_unref (buffer);
363     }
364   }
365
366   /* and ensure the processing thread has stopped in case another error
367    * occured. */
368   gst_v4l2_object_unlock (self->v4l2capture);
369   gst_pad_stop_task (decoder->srcpad);
370   GST_VIDEO_DECODER_STREAM_LOCK (decoder);
371
372   if (ret == GST_FLOW_FLUSHING)
373     ret = self->output_flow;
374
375   GST_DEBUG_OBJECT (decoder, "Done draining buffers");
376
377 done:
378   return ret;
379 }
380
381 static GstVideoCodecFrame *
382 gst_v4l2_video_dec_get_oldest_frame (GstVideoDecoder * decoder)
383 {
384   GstVideoCodecFrame *frame = NULL;
385   GList *frames, *l;
386   gint count = 0;
387
388   frames = gst_video_decoder_get_frames (decoder);
389
390   for (l = frames; l != NULL; l = l->next) {
391     GstVideoCodecFrame *f = l->data;
392
393     if (!frame || frame->pts > f->pts)
394       frame = f;
395
396     count++;
397   }
398
399   if (frame) {
400     GST_LOG_OBJECT (decoder,
401         "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
402         frame->system_frame_number, GST_TIME_ARGS (frame->pts), count - 1);
403     gst_video_codec_frame_ref (frame);
404   }
405
406   g_list_free_full (frames, (GDestroyNotify) gst_video_codec_frame_unref);
407
408   return frame;
409 }
410
411 static void
412 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
413 {
414   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
415   GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
416   GstBufferPool *pool;
417   GstVideoCodecFrame *frame;
418   GstBuffer *buffer = NULL;
419   GstFlowReturn ret;
420
421   GST_LOG_OBJECT (decoder, "Allocate output buffer");
422
423   do {
424     /* We cannot use the base class allotate helper since it taking the internal
425      * stream lock. we know that the acquire may need to poll until more frames
426      * comes in and holding this lock would prevent that.
427      */
428     pool = gst_video_decoder_get_buffer_pool (decoder);
429
430     /* Pool may be NULL if we started going to READY state */
431     if (pool == NULL) {
432       ret = GST_FLOW_FLUSHING;
433       goto beach;
434     }
435
436     ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
437     g_object_unref (pool);
438
439     if (ret != GST_FLOW_OK)
440       goto beach;
441
442     GST_LOG_OBJECT (decoder, "Process output buffer");
443     ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer);
444
445   } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
446
447   if (ret != GST_FLOW_OK)
448     goto beach;
449
450   frame = gst_v4l2_video_dec_get_oldest_frame (decoder);
451
452   if (frame) {
453     frame->output_buffer = buffer;
454     buffer = NULL;
455     ret = gst_video_decoder_finish_frame (decoder, frame);
456
457     if (ret != GST_FLOW_OK)
458       goto beach;
459   } else {
460     GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
461     gst_buffer_unref (buffer);
462   }
463
464   return;
465
466 beach:
467   GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
468       gst_flow_get_name (ret));
469
470   gst_buffer_replace (&buffer, NULL);
471   self->output_flow = ret;
472   g_atomic_int_set (&self->processing, FALSE);
473   gst_v4l2_object_unlock (self->v4l2output);
474   gst_pad_pause_task (decoder->srcpad);
475 }
476
477 static void
478 gst_v4l2_video_dec_loop_stopped (GstV4l2VideoDec * self)
479 {
480   /* When flushing, decoding thread may never run */
481   if (g_atomic_int_get (&self->processing)) {
482     GST_DEBUG_OBJECT (self, "Early stop of decoding thread");
483     self->output_flow = GST_FLOW_FLUSHING;
484     g_atomic_int_set (&self->processing, FALSE);
485   }
486
487   GST_DEBUG_OBJECT (self, "Decoding task destroyed: %s",
488       gst_flow_get_name (self->output_flow));
489 }
490
491 static gboolean
492 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
493     GstStructure * structure, gpointer user_data)
494 {
495   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
496   GstVideoAlignment *align = &self->v4l2capture->align;
497   GstVideoInfo *info = &self->v4l2capture->info;
498   int width, height;
499
500   if (!gst_structure_get_int (structure, "width", &width))
501     return TRUE;
502
503   if (!gst_structure_get_int (structure, "height", &height))
504     return TRUE;
505
506   if (align->padding_left != 0 || align->padding_top != 0 ||
507       width != info->width + align->padding_right ||
508       height != info->height + align->padding_bottom)
509     return TRUE;
510
511   gst_structure_set (structure,
512       "width", G_TYPE_INT, width - align->padding_right,
513       "height", G_TYPE_INT, height - align->padding_bottom, NULL);
514
515   return TRUE;
516 }
517
518 static GstFlowReturn
519 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
520     GstVideoCodecFrame * frame)
521 {
522   GstV4l2Error error = GST_V4L2_ERROR_INIT;
523   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
524   GstFlowReturn ret = GST_FLOW_OK;
525
526   GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
527
528   if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
529     goto flushing;
530
531   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
532     if (!self->input_state)
533       goto not_negotiated;
534     if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
535             &error))
536       goto not_negotiated;
537   }
538
539   if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
540     GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
541     GstVideoInfo info;
542     GstVideoCodecState *output_state;
543     GstBuffer *codec_data;
544     GstCaps *acquired_caps, *available_caps, *caps, *filter;
545     GstStructure *st;
546
547     GST_DEBUG_OBJECT (self, "Sending header");
548
549     codec_data = self->input_state->codec_data;
550
551     /* We are running in byte-stream mode, so we don't know the headers, but
552      * we need to send something, otherwise the decoder will refuse to
553      * intialize.
554      */
555     if (codec_data) {
556       gst_buffer_ref (codec_data);
557     } else {
558       codec_data = frame->input_buffer;
559       frame->input_buffer = NULL;
560     }
561
562     /* Ensure input internal pool is active */
563     if (!gst_buffer_pool_is_active (pool)) {
564       GstStructure *config = gst_buffer_pool_get_config (pool);
565       gst_buffer_pool_config_set_params (config, self->input_state->caps,
566           self->v4l2output->info.size, 2, 2);
567
568       /* There is no reason to refuse this config */
569       if (!gst_buffer_pool_set_config (pool, config))
570         goto activate_failed;
571
572       if (!gst_buffer_pool_set_active (pool, TRUE))
573         goto activate_failed;
574     }
575
576     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
577     ret =
578         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
579             v4l2output->pool), &codec_data);
580     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
581
582     gst_buffer_unref (codec_data);
583
584     /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
585      * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
586      * and returns the visible size as with/height and the coded size as
587      * padding. */
588     if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
589       goto not_negotiated;
590
591     /* Create caps from the acquired format, remove the format field */
592     acquired_caps = gst_video_info_to_caps (&info);
593     st = gst_caps_get_structure (acquired_caps, 0);
594     gst_structure_remove_field (st, "format");
595
596     /* Probe currently available pixel formats */
597     available_caps = gst_v4l2_object_probe_caps (self->v4l2capture, NULL);
598     available_caps = gst_caps_make_writable (available_caps);
599
600     /* Replace coded size with visible size, we want to negotiate visible size
601      * with downstream, not coded size. */
602     gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
603
604     filter = gst_caps_intersect_full (available_caps, acquired_caps,
605         GST_CAPS_INTERSECT_FIRST);
606     gst_caps_unref (acquired_caps);
607     gst_caps_unref (available_caps);
608     caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
609     gst_caps_unref (filter);
610
611     GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
612     if (gst_caps_is_empty (caps)) {
613       gst_caps_unref (caps);
614       goto not_negotiated;
615     }
616
617     /* Fixate pixel format */
618     caps = gst_caps_fixate (caps);
619
620     GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
621
622     /* Try to set negotiated format, on success replace acquired format */
623     if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
624       gst_video_info_from_caps (&info, caps);
625     else
626       gst_v4l2_clear_error (&error);
627     gst_caps_unref (caps);
628
629     output_state = gst_video_decoder_set_output_state (decoder,
630         info.finfo->format, info.width, info.height, self->input_state);
631
632     /* Copy the rest of the information, there might be more in the future */
633     output_state->info.interlace_mode = info.interlace_mode;
634     gst_video_codec_state_unref (output_state);
635
636     if (!gst_video_decoder_negotiate (decoder)) {
637       if (GST_PAD_IS_FLUSHING (decoder->srcpad))
638         goto flushing;
639       else
640         goto not_negotiated;
641     }
642
643     /* Ensure our internal pool is activated */
644     if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
645             TRUE))
646       goto activate_failed;
647   }
648
649   if (g_atomic_int_get (&self->processing) == FALSE) {
650     /* It's possible that the processing thread stopped due to an error */
651     if (self->output_flow != GST_FLOW_OK &&
652         self->output_flow != GST_FLOW_FLUSHING) {
653       GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
654       ret = self->output_flow;
655       goto drop;
656     }
657
658     GST_DEBUG_OBJECT (self, "Starting decoding thread");
659
660     /* Start the processing task, when it quits, the task will disable input
661      * processing to unlock input if draining, or prevent potential block */
662     g_atomic_int_set (&self->processing, TRUE);
663     if (!gst_pad_start_task (decoder->srcpad,
664             (GstTaskFunction) gst_v4l2_video_dec_loop, self,
665             (GDestroyNotify) gst_v4l2_video_dec_loop_stopped))
666       goto start_task_failed;
667   }
668
669   if (frame->input_buffer) {
670     GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
671     ret =
672         gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
673             pool), &frame->input_buffer);
674     GST_VIDEO_DECODER_STREAM_LOCK (decoder);
675
676     if (ret == GST_FLOW_FLUSHING) {
677       if (g_atomic_int_get (&self->processing) == FALSE)
678         ret = self->output_flow;
679       goto drop;
680     } else if (ret != GST_FLOW_OK) {
681       goto process_failed;
682     }
683
684     /* No need to keep input arround */
685     gst_buffer_replace (&frame->input_buffer, NULL);
686   }
687
688   gst_video_codec_frame_unref (frame);
689   return ret;
690
691   /* ERRORS */
692 not_negotiated:
693   {
694     GST_ERROR_OBJECT (self, "not negotiated");
695     ret = GST_FLOW_NOT_NEGOTIATED;
696     gst_v4l2_error (self, &error);
697     goto drop;
698   }
699 activate_failed:
700   {
701     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
702         (_("Failed to allocate required memory.")),
703         ("Buffer pool activation failed"));
704     ret = GST_FLOW_ERROR;
705     goto drop;
706   }
707 flushing:
708   {
709     ret = GST_FLOW_FLUSHING;
710     goto drop;
711   }
712
713 start_task_failed:
714   {
715     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
716         (_("Failed to start decoding thread.")), (NULL));
717     g_atomic_int_set (&self->processing, FALSE);
718     ret = GST_FLOW_ERROR;
719     goto drop;
720   }
721 process_failed:
722   {
723     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
724         (_("Failed to process frame.")),
725         ("Maybe be due to not enough memory or failing driver"));
726     ret = GST_FLOW_ERROR;
727     goto drop;
728   }
729 drop:
730   {
731     gst_video_decoder_drop_frame (decoder, frame);
732     return ret;
733   }
734 }
735
736 static gboolean
737 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
738     GstQuery * query)
739 {
740   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
741   GstClockTime latency;
742   gboolean ret = FALSE;
743
744   if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
745     ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
746         query);
747
748   latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
749   gst_video_decoder_set_latency (decoder, latency, latency);
750
751   return ret;
752 }
753
754 static gboolean
755 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
756 {
757   gboolean ret = TRUE;
758   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
759
760   switch (GST_QUERY_TYPE (query)) {
761     case GST_QUERY_CAPS:{
762       GstCaps *filter, *result = NULL;
763       GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
764
765       gst_query_parse_caps (query, &filter);
766
767       if (self->probed_srccaps)
768         result = gst_caps_ref (self->probed_srccaps);
769       else
770         result = gst_pad_get_pad_template_caps (pad);
771
772       if (filter) {
773         GstCaps *tmp = result;
774         result =
775             gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
776         gst_caps_unref (tmp);
777       }
778
779       GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
780
781       gst_query_set_caps_result (query, result);
782       gst_caps_unref (result);
783       break;
784     }
785
786     default:
787       ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
788       break;
789   }
790
791   return ret;
792 }
793
794 static GstCaps *
795 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
796 {
797   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
798   GstCaps *result;
799
800   result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
801       filter);
802
803   GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
804
805   return result;
806 }
807
808 static gboolean
809 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
810 {
811   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
812   gboolean ret;
813
814   switch (GST_EVENT_TYPE (event)) {
815     case GST_EVENT_FLUSH_START:
816       GST_DEBUG_OBJECT (self, "flush start");
817       gst_v4l2_object_unlock (self->v4l2output);
818       gst_v4l2_object_unlock (self->v4l2capture);
819       break;
820     default:
821       break;
822   }
823
824   ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
825
826   switch (GST_EVENT_TYPE (event)) {
827     case GST_EVENT_FLUSH_START:
828       /* The processing thread should stop now, wait for it */
829       gst_pad_stop_task (decoder->srcpad);
830       GST_DEBUG_OBJECT (self, "flush start done");
831       break;
832     default:
833       break;
834   }
835
836   return ret;
837 }
838
839 static GstStateChangeReturn
840 gst_v4l2_video_dec_change_state (GstElement * element,
841     GstStateChange transition)
842 {
843   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
844   GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
845
846   if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
847     g_atomic_int_set (&self->active, FALSE);
848     gst_v4l2_object_unlock (self->v4l2output);
849     gst_v4l2_object_unlock (self->v4l2capture);
850     gst_pad_stop_task (decoder->srcpad);
851   }
852
853   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
854 }
855
856 static void
857 gst_v4l2_video_dec_dispose (GObject * object)
858 {
859   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
860
861   gst_caps_replace (&self->probed_sinkcaps, NULL);
862   gst_caps_replace (&self->probed_srccaps, NULL);
863
864   G_OBJECT_CLASS (parent_class)->dispose (object);
865 }
866
867 static void
868 gst_v4l2_video_dec_finalize (GObject * object)
869 {
870   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
871
872   gst_v4l2_object_destroy (self->v4l2capture);
873   gst_v4l2_object_destroy (self->v4l2output);
874
875   G_OBJECT_CLASS (parent_class)->finalize (object);
876 }
877
878 static void
879 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
880 {
881   /* V4L2 object are created in subinstance_init */
882 }
883
884 static void
885 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
886 {
887   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
888   GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
889   GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
890
891   gst_video_decoder_set_packetized (decoder, TRUE);
892
893   self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
894       V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
895       gst_v4l2_get_output, gst_v4l2_set_output, NULL);
896   self->v4l2output->no_initial_format = TRUE;
897   self->v4l2output->keep_aspect = FALSE;
898
899   self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
900       V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
901       gst_v4l2_get_input, gst_v4l2_set_input, NULL);
902   self->v4l2capture->no_initial_format = TRUE;
903   self->v4l2output->keep_aspect = FALSE;
904 }
905
906 static void
907 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
908 {
909   GstElementClass *element_class;
910   GObjectClass *gobject_class;
911   GstVideoDecoderClass *video_decoder_class;
912
913   parent_class = g_type_class_peek_parent (klass);
914
915   element_class = (GstElementClass *) klass;
916   gobject_class = (GObjectClass *) klass;
917   video_decoder_class = (GstVideoDecoderClass *) klass;
918
919   GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
920       "V4L2 Video Decoder");
921
922   gst_element_class_set_static_metadata (element_class,
923       "V4L2 Video Decoder",
924       "Codec/Decoder/Video",
925       "Decode video streams via V4L2 API",
926       "Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>");
927
928   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
929   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
930   gobject_class->set_property =
931       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
932   gobject_class->get_property =
933       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
934
935   video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
936   video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
937   video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
938   video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
939   video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
940   video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
941   video_decoder_class->set_format =
942       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
943   video_decoder_class->negotiate =
944       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
945   video_decoder_class->decide_allocation =
946       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
947   /* FIXME propose_allocation or not ? */
948   video_decoder_class->handle_frame =
949       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
950   video_decoder_class->getcaps =
951       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
952   video_decoder_class->src_query =
953       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
954   video_decoder_class->sink_event =
955       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
956
957   element_class->change_state =
958       GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
959
960   gst_v4l2_object_install_m2m_properties_helper (gobject_class);
961 }
962
963 static void
964 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
965 {
966   GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
967   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
968   GstV4l2VideoDecCData *cdata = data;
969
970   klass->default_device = cdata->device;
971
972   /* Note: gst_pad_template_new() take the floating ref from the caps */
973   gst_element_class_add_pad_template (element_class,
974       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
975           cdata->sink_caps));
976   gst_element_class_add_pad_template (element_class,
977       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
978           cdata->src_caps));
979
980   g_free (cdata);
981 }
982
983 /* Probing functions */
984 gboolean
985 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
986 {
987   gboolean ret = FALSE;
988
989   if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
990       && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
991     ret = TRUE;
992
993   return ret;
994 }
995
996 gboolean
997 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
998     const gchar * device_path, GstCaps * sink_caps, GstCaps * src_caps)
999 {
1000   GTypeQuery type_query;
1001   GTypeInfo type_info = { 0, };
1002   GType type, subtype;
1003   gchar *type_name;
1004   GstV4l2VideoDecCData *cdata;
1005
1006   cdata = g_new0 (GstV4l2VideoDecCData, 1);
1007   cdata->device = g_strdup (device_path);
1008   cdata->sink_caps = gst_caps_ref (sink_caps);
1009   cdata->src_caps = gst_caps_ref (src_caps);
1010
1011   type = gst_v4l2_video_dec_get_type ();
1012   g_type_query (type, &type_query);
1013   memset (&type_info, 0, sizeof (type_info));
1014   type_info.class_size = type_query.class_size;
1015   type_info.instance_size = type_query.instance_size;
1016   type_info.class_init = gst_v4l2_video_dec_subclass_init;
1017   type_info.class_data = cdata;
1018   type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1019
1020   type_name = g_strdup_printf ("v4l2%sdec", basename);
1021   subtype = g_type_register_static (type, type_name, &type_info, 0);
1022
1023   gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1, subtype);
1024
1025   g_free (type_name);
1026
1027   return TRUE;
1028 }