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