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