1 /*
2 * Copyright (C) 2014 Collabora Ltd.
3 * Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
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 "gstv4l2object.h"
33 #include "gstv4l2videodec.h"
34
35 #include "gstv4l2h264codec.h"
36 #include "gstv4l2h265codec.h"
37 #include "gstv4l2mpeg2codec.h"
38 #include "gstv4l2mpeg4codec.h"
39 #include "gstv4l2vp8codec.h"
40 #include "gstv4l2vp9codec.h"
41
42 #include <string.h>
43 #include <gst/gst-i18n-plugin.h>
44
45 GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
46 #define GST_CAT_DEFAULT gst_v4l2_video_dec_debug
47
48 typedef struct
49 {
50 gchar *device;
51 GstCaps *sink_caps;
52 GstCaps *src_caps;
53 const gchar *longname;
54 const gchar *description;
55 const GstV4l2Codec *codec;
56 } GstV4l2VideoDecCData;
57
58 enum
59 {
60 PROP_0,
61 V4L2_STD_OBJECT_PROPS
62 };
63
64 #define gst_v4l2_video_dec_parent_class parent_class
65 G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
66 GST_TYPE_VIDEO_DECODER);
67
68 static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder);
69
70 static void
gst_v4l2_video_dec_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)71 gst_v4l2_video_dec_set_property (GObject * object,
72 guint prop_id, const GValue * value, GParamSpec * pspec)
73 {
74 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
75
76 switch (prop_id) {
77 case PROP_CAPTURE_IO_MODE:
78 if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
79 prop_id, value, pspec)) {
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
81 }
82 break;
83
84 /* By default, only set on output */
85 default:
86 if (!gst_v4l2_object_set_property_helper (self->v4l2output,
87 prop_id, value, pspec)) {
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89 }
90 break;
91 }
92 }
93
94 static void
gst_v4l2_video_dec_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)95 gst_v4l2_video_dec_get_property (GObject * object,
96 guint prop_id, GValue * value, GParamSpec * pspec)
97 {
98 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
99
100 switch (prop_id) {
101 case PROP_CAPTURE_IO_MODE:
102 if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
103 prop_id, value, pspec)) {
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
105 }
106 break;
107
108 /* By default read from output */
109 default:
110 if (!gst_v4l2_object_get_property_helper (self->v4l2output,
111 prop_id, value, pspec)) {
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 }
114 break;
115 }
116 }
117
118 static gboolean
gst_v4l2_video_dec_open(GstVideoDecoder * decoder)119 gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
120 {
121 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
122 GstV4l2Error error = GST_V4L2_ERROR_INIT;
123 GstCaps *codec_caps;
124
125 GST_DEBUG_OBJECT (self, "Opening");
126
127 if (!gst_v4l2_object_open (self->v4l2output, &error))
128 goto failure;
129
130 if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
131 goto failure;
132
133 codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
134 self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
135 codec_caps);
136 gst_caps_unref (codec_caps);
137
138 if (gst_caps_is_empty (self->probed_sinkcaps))
139 goto no_encoded_format;
140
141 return TRUE;
142
143 no_encoded_format:
144 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
145 (_("Decoder on device %s has no supported input format"),
146 self->v4l2output->videodev), (NULL));
147 goto failure;
148
149 failure:
150 if (GST_V4L2_IS_OPEN (self->v4l2output))
151 gst_v4l2_object_close (self->v4l2output);
152
153 if (GST_V4L2_IS_OPEN (self->v4l2capture))
154 gst_v4l2_object_close (self->v4l2capture);
155
156 gst_caps_replace (&self->probed_srccaps, NULL);
157 gst_caps_replace (&self->probed_sinkcaps, NULL);
158
159 gst_v4l2_error (self, &error);
160
161 return FALSE;
162 }
163
164 static gboolean
gst_v4l2_video_dec_close(GstVideoDecoder * decoder)165 gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
166 {
167 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
168
169 GST_DEBUG_OBJECT (self, "Closing");
170
171 gst_v4l2_object_close (self->v4l2output);
172 gst_v4l2_object_close (self->v4l2capture);
173 gst_caps_replace (&self->probed_srccaps, NULL);
174 gst_caps_replace (&self->probed_sinkcaps, NULL);
175
176 return TRUE;
177 }
178
179 static gboolean
gst_v4l2_video_dec_start(GstVideoDecoder * decoder)180 gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
181 {
182 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
183
184 GST_DEBUG_OBJECT (self, "Starting");
185
186 gst_v4l2_object_unlock (self->v4l2output);
187 g_atomic_int_set (&self->active, TRUE);
188 self->output_flow = GST_FLOW_OK;
189
190 return TRUE;
191 }
192
193 static gboolean
gst_v4l2_video_dec_stop(GstVideoDecoder * decoder)194 gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
195 {
196 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
197
198 GST_DEBUG_OBJECT (self, "Stopping");
199
200 gst_v4l2_object_unlock (self->v4l2output);
201 gst_v4l2_object_unlock (self->v4l2capture);
202
203 /* Wait for capture thread to stop */
204 gst_pad_stop_task (decoder->srcpad);
205
206 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
207 self->output_flow = GST_FLOW_OK;
208 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
209
210 /* Should have been flushed already */
211 g_assert (g_atomic_int_get (&self->active) == FALSE);
212
213 gst_v4l2_object_stop (self->v4l2output);
214 gst_v4l2_object_stop (self->v4l2capture);
215
216 if (self->input_state) {
217 gst_video_codec_state_unref (self->input_state);
218 self->input_state = NULL;
219 }
220
221 GST_DEBUG_OBJECT (self, "Stopped");
222
223 return TRUE;
224 }
225
226 static gboolean
compatible_caps(GstV4l2VideoDec * self,GstCaps * new_caps)227 compatible_caps (GstV4l2VideoDec * self, GstCaps * new_caps)
228 {
229 GstCaps *current_caps, *caps1, *caps2;
230 GstStructure *s;
231 gboolean ret;
232
233 current_caps = gst_v4l2_object_get_current_caps (self->v4l2output);
234 if (!current_caps)
235 return FALSE;
236
237 caps1 = gst_caps_copy (current_caps);
238 s = gst_caps_get_structure (caps1, 0);
239 gst_structure_remove_field (s, "framerate");
240
241 caps2 = gst_caps_copy (new_caps);
242 s = gst_caps_get_structure (caps2, 0);
243 gst_structure_remove_field (s, "framerate");
244
245 ret = gst_caps_is_equal (caps1, caps2);
246
247 gst_caps_unref (caps1);
248 gst_caps_unref (caps2);
249 gst_caps_unref (current_caps);
250
251 return ret;
252 }
253
254 static gboolean
gst_v4l2_video_dec_set_format(GstVideoDecoder * decoder,GstVideoCodecState * state)255 gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
256 GstVideoCodecState * state)
257 {
258 GstV4l2Error error = GST_V4L2_ERROR_INIT;
259 gboolean ret = TRUE;
260 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
261
262 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
263
264 if (self->input_state) {
265 if (compatible_caps (self, state->caps)) {
266 GST_DEBUG_OBJECT (self, "Compatible caps");
267 goto done;
268 }
269 gst_video_codec_state_unref (self->input_state);
270 self->input_state = NULL;
271
272 gst_v4l2_video_dec_finish (decoder);
273 gst_v4l2_object_stop (self->v4l2output);
274
275 /* The renegotiation flow don't blend with the base class flow. To properly
276 * stop the capture pool, if the buffers can't be orphaned, we need to
277 * reclaim our buffers, which will happend through the allocation query.
278 * The allocation query is triggered by gst_video_decoder_negotiate() which
279 * requires the output caps to be set, but we can't know this information
280 * as we rely on the decoder, which requires the capture queue to be
281 * stopped.
282 *
283 * To workaround this issue, we simply run an allocation query with the
284 * old negotiated caps in order to drain/reclaim our buffers. That breaks
285 * the complexity and should not have much impact in performance since the
286 * following allocation query will happen on a drained pipeline and won't
287 * block. */
288 if (self->v4l2capture->pool &&
289 !gst_v4l2_buffer_pool_orphan (&self->v4l2capture->pool)) {
290 GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
291 if (caps) {
292 GstQuery *query = gst_query_new_allocation (caps, FALSE);
293 gst_pad_peer_query (decoder->srcpad, query);
294 gst_query_unref (query);
295 gst_caps_unref (caps);
296 }
297 }
298
299 gst_v4l2_object_stop (self->v4l2capture);
300 self->output_flow = GST_FLOW_OK;
301 }
302
303 ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
304
305 gst_caps_replace (&self->probed_srccaps, NULL);
306 self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
307 gst_v4l2_object_get_raw_caps ());
308
309 if (gst_caps_is_empty (self->probed_srccaps))
310 goto no_raw_format;
311
312 if (ret)
313 self->input_state = gst_video_codec_state_ref (state);
314 else
315 gst_v4l2_error (self, &error);
316
317 done:
318 return ret;
319
320 no_raw_format:
321 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
322 (_("Decoder on device %s has no supported output format"),
323 self->v4l2output->videodev), (NULL));
324 return GST_FLOW_ERROR;
325 }
326
327 static gboolean
gst_v4l2_video_dec_flush(GstVideoDecoder * decoder)328 gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
329 {
330 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
331
332 GST_DEBUG_OBJECT (self, "Flushed");
333
334 /* Ensure the processing thread has stopped for the reverse playback
335 * discount case */
336 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
337 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
338
339 gst_v4l2_object_unlock (self->v4l2output);
340 gst_v4l2_object_unlock (self->v4l2capture);
341 gst_pad_stop_task (decoder->srcpad);
342 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
343 }
344
345 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
346 return TRUE;
347
348 self->output_flow = GST_FLOW_OK;
349
350 gst_v4l2_object_unlock_stop (self->v4l2output);
351 gst_v4l2_object_unlock_stop (self->v4l2capture);
352
353 if (self->v4l2output->pool)
354 gst_v4l2_buffer_pool_flush (self->v4l2output->pool);
355
356 /* gst_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
357 * called after gst_v4l2_object_unlock_stop() stopped flushing the buffer
358 * pool. */
359 if (self->v4l2capture->pool)
360 gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);
361
362 return TRUE;
363 }
364
365 static gboolean
gst_v4l2_video_dec_negotiate(GstVideoDecoder * decoder)366 gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
367 {
368 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
369
370 /* We don't allow renegotiation without careful disabling the pool */
371 if (self->v4l2capture->pool &&
372 gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
373 return TRUE;
374
375 return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
376 }
377
378 static gboolean
gst_v4l2_decoder_cmd(GstV4l2Object * v4l2object,guint cmd,guint flags)379 gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
380 {
381 struct v4l2_decoder_cmd dcmd = { 0, };
382
383 GST_DEBUG_OBJECT (v4l2object->element,
384 "sending v4l2 decoder command %u with flags %u", cmd, flags);
385
386 if (!GST_V4L2_IS_OPEN (v4l2object))
387 return FALSE;
388
389 dcmd.cmd = cmd;
390 dcmd.flags = flags;
391 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
392 goto dcmd_failed;
393
394 return TRUE;
395
396 dcmd_failed:
397 if (errno == ENOTTY) {
398 GST_INFO_OBJECT (v4l2object->element,
399 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
400 cmd, flags, v4l2object->videodev, g_strerror (errno));
401 } else {
402 GST_ERROR_OBJECT (v4l2object->element,
403 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
404 cmd, flags, v4l2object->videodev, g_strerror (errno));
405 }
406 return FALSE;
407 }
408
409 static GstFlowReturn
gst_v4l2_video_dec_finish(GstVideoDecoder * decoder)410 gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
411 {
412 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
413 GstFlowReturn ret = GST_FLOW_OK;
414 GstBuffer *buffer;
415
416 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
417 goto done;
418
419 GST_DEBUG_OBJECT (self, "Finishing decoding");
420
421 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
422
423 if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
424 GstTask *task;
425
426 GST_OBJECT_LOCK (decoder->srcpad);
427 task = GST_PAD_TASK (decoder->srcpad);
428 if (task)
429 gst_object_ref (task);
430 GST_OBJECT_UNLOCK (decoder->srcpad);
431
432 if (task) {
433 /* If the decoder stop command succeeded, just wait until processing is
434 * finished */
435 GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
436 GST_OBJECT_LOCK (task);
437 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
438 GST_TASK_WAIT (task);
439 GST_OBJECT_UNLOCK (task);
440
441 ret = GST_FLOW_FLUSHING;
442 gst_object_unref (task);
443 }
444 } else {
445 /* otherwise keep queuing empty buffers until the processing thread has
446 * stopped, _pool_process() will return FLUSHING when that happened */
447 while (ret == GST_FLOW_OK) {
448 buffer = gst_buffer_new ();
449 ret =
450 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
451 v4l2output->pool), &buffer, NULL);
452 gst_buffer_unref (buffer);
453 }
454 }
455
456 /* and ensure the processing thread has stopped in case another error
457 * occurred. */
458 gst_v4l2_object_unlock (self->v4l2capture);
459 gst_pad_stop_task (decoder->srcpad);
460 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
461
462 if (ret == GST_FLOW_FLUSHING)
463 ret = self->output_flow;
464
465 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
466
467 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
468
469 done:
470 return ret;
471 }
472
473 static GstFlowReturn
gst_v4l2_video_dec_drain(GstVideoDecoder * decoder)474 gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
475 {
476 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
477
478 GST_DEBUG_OBJECT (self, "Draining...");
479 gst_v4l2_video_dec_finish (decoder);
480 gst_v4l2_video_dec_flush (decoder);
481
482 return GST_FLOW_OK;
483 }
484
485 static gboolean
check_system_frame_number_too_old(guint32 current,guint32 old)486 check_system_frame_number_too_old (guint32 current, guint32 old)
487 {
488 guint32 absdiff = current > old ? current - old : old - current;
489
490 /* More than 100 frames in the past, or current wrapped around */
491 if (absdiff > 100) {
492 /* Wraparound and difference is actually smaller than 100 */
493 if (absdiff > G_MAXUINT32 - 100)
494 return FALSE;
495 return TRUE;
496 }
497
498 return FALSE;
499 }
500
501 static void
gst_v4l2_video_dec_loop(GstVideoDecoder * decoder)502 gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
503 {
504 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
505 GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
506 GstBufferPool *pool;
507 GstVideoCodecFrame *frame;
508 GstBuffer *buffer = NULL;
509 GstFlowReturn ret;
510
511 GST_LOG_OBJECT (decoder, "Allocate output buffer");
512
513 self->output_flow = GST_FLOW_OK;
514 do {
515 /* We cannot use the base class allotate helper since it taking the internal
516 * stream lock. we know that the acquire may need to poll until more frames
517 * comes in and holding this lock would prevent that.
518 */
519 pool = gst_video_decoder_get_buffer_pool (decoder);
520
521 /* Pool may be NULL if we started going to READY state */
522 if (pool == NULL) {
523 ret = GST_FLOW_FLUSHING;
524 goto beach;
525 }
526
527 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
528 g_object_unref (pool);
529
530 if (ret != GST_FLOW_OK)
531 goto beach;
532
533 GST_LOG_OBJECT (decoder, "Process output buffer");
534 ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer, NULL);
535 } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
536
537 if (ret != GST_FLOW_OK)
538 goto beach;
539
540 if (GST_BUFFER_TIMESTAMP (buffer) % GST_SECOND != 0)
541 GST_ERROR_OBJECT (decoder,
542 "Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git");
543 GST_LOG_OBJECT (decoder, "Got buffer for frame number %u",
544 (guint32) (GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND));
545
546 frame =
547 gst_video_decoder_get_frame (decoder,
548 GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND);
549 if (frame) {
550 GstVideoCodecFrame *oldest_frame;
551 gboolean warned = FALSE;
552
553 /* Garbage collect old frames in case of codec bugs */
554 while ((oldest_frame = gst_video_decoder_get_oldest_frame (decoder)) &&
555 check_system_frame_number_too_old (frame->system_frame_number,
556 oldest_frame->system_frame_number)) {
557 gst_video_decoder_drop_frame (decoder, oldest_frame);
558 oldest_frame = NULL;
559
560 if (!warned) {
561 g_warning ("%s: Too old frames, bug in decoder -- please file a bug",
562 GST_ELEMENT_NAME (decoder));
563 warned = TRUE;
564 }
565 }
566 if (oldest_frame)
567 gst_video_codec_frame_unref (oldest_frame);
568
569 frame->duration = self->v4l2capture->duration;
570 frame->output_buffer = buffer;
571 buffer = NULL;
572 ret = gst_video_decoder_finish_frame (decoder, frame);
573
574 if (ret != GST_FLOW_OK)
575 goto beach;
576 } else {
577 GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
578 gst_buffer_unref (buffer);
579 }
580
581 return;
582
583 beach:
584 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
585 gst_flow_get_name (ret));
586
587 gst_buffer_replace (&buffer, NULL);
588 self->output_flow = ret;
589 gst_v4l2_object_unlock (self->v4l2output);
590 gst_pad_pause_task (decoder->srcpad);
591 }
592
593 static gboolean
gst_v4l2_video_remove_padding(GstCapsFeatures * features,GstStructure * structure,gpointer user_data)594 gst_v4l2_video_remove_padding (GstCapsFeatures * features,
595 GstStructure * structure, gpointer user_data)
596 {
597 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
598 GstVideoAlignment *align = &self->v4l2capture->align;
599 GstVideoInfo *info = &self->v4l2capture->info;
600 int width, height;
601
602 if (!gst_structure_get_int (structure, "width", &width))
603 return TRUE;
604
605 if (!gst_structure_get_int (structure, "height", &height))
606 return TRUE;
607
608 if (align->padding_left != 0 || align->padding_top != 0 ||
609 height != info->height + align->padding_bottom)
610 return TRUE;
611
612 if (height == info->height + align->padding_bottom) {
613 /* Some drivers may round up width to the padded with */
614 if (width == info->width + align->padding_right)
615 gst_structure_set (structure,
616 "width", G_TYPE_INT, width - align->padding_right,
617 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
618 /* Some drivers may keep visible width and only round up bytesperline */
619 else if (width == info->width)
620 gst_structure_set (structure,
621 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
622 }
623
624 return TRUE;
625 }
626
627 static GstFlowReturn
gst_v4l2_video_dec_handle_frame(GstVideoDecoder * decoder,GstVideoCodecFrame * frame)628 gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
629 GstVideoCodecFrame * frame)
630 {
631 GstV4l2Error error = GST_V4L2_ERROR_INIT;
632 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
633 GstFlowReturn ret = GST_FLOW_OK;
634 gboolean processed = FALSE;
635 GstBuffer *tmp;
636 GstTaskState task_state;
637
638 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
639
640 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
641 goto flushing;
642
643 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
644 if (!self->input_state)
645 goto not_negotiated;
646 if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
647 &error))
648 goto not_negotiated;
649 }
650
651 if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
652 GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
653 GstVideoInfo info;
654 GstVideoCodecState *output_state;
655 GstBuffer *codec_data;
656 GstCaps *acquired_caps, *available_caps, *caps, *filter;
657 GstStructure *st;
658 guint32 dummy_frame_number = 0;
659
660 GST_DEBUG_OBJECT (self, "Sending header");
661
662 codec_data = self->input_state->codec_data;
663
664 /* We are running in byte-stream mode, so we don't know the headers, but
665 * we need to send something, otherwise the decoder will refuse to
666 * initialize.
667 */
668 if (codec_data) {
669 gst_buffer_ref (codec_data);
670 } else {
671 codec_data = gst_buffer_ref (frame->input_buffer);
672 processed = TRUE;
673 }
674
675 /* Ensure input internal pool is active */
676 if (!gst_buffer_pool_is_active (pool)) {
677 GstStructure *config = gst_buffer_pool_get_config (pool);
678 guint min = MAX (self->v4l2output->min_buffers,
679 GST_V4L2_MIN_BUFFERS (self->v4l2output));
680 guint max = VIDEO_MAX_FRAME;
681
682 gst_buffer_pool_config_set_params (config, self->input_state->caps,
683 self->v4l2output->info.size, min, max);
684
685 /* There is no reason to refuse this config */
686 if (!gst_buffer_pool_set_config (pool, config))
687 goto activate_failed;
688
689 if (!gst_buffer_pool_set_active (pool, TRUE))
690 goto activate_failed;
691 }
692
693 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
694 GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
695 processed ? frame->system_frame_number : 0);
696 ret =
697 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
698 v4l2output->pool), &codec_data,
699 processed ? &frame->system_frame_number : &dummy_frame_number);
700 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
701
702 gst_buffer_unref (codec_data);
703
704 /* init capture fps according to output */
705 self->v4l2capture->info.fps_d = self->v4l2output->info.fps_d;
706 self->v4l2capture->info.fps_n = self->v4l2output->info.fps_n;
707
708 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
709 * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
710 * and returns the visible size as with/height and the coded size as
711 * padding. */
712 if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
713 goto not_negotiated;
714
715 /* gst_v4l2_object_acquire_format() does not set fps, copy from sink */
716 info.fps_n = self->v4l2output->info.fps_n;
717 info.fps_d = self->v4l2output->info.fps_d;
718
719 /* Create caps from the acquired format, remove the format field */
720 acquired_caps = gst_video_info_to_caps (&info);
721 GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
722 st = gst_caps_get_structure (acquired_caps, 0);
723 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
724 NULL);
725
726 /* Probe currently available pixel formats */
727 available_caps = gst_caps_copy (self->probed_srccaps);
728 GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
729
730 /* Replace coded size with visible size, we want to negotiate visible size
731 * with downstream, not coded size. */
732 gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);
733
734 filter = gst_caps_intersect_full (available_caps, acquired_caps,
735 GST_CAPS_INTERSECT_FIRST);
736 GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
737 gst_caps_unref (acquired_caps);
738 gst_caps_unref (available_caps);
739 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
740 gst_caps_unref (filter);
741
742 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
743 if (gst_caps_is_empty (caps)) {
744 gst_caps_unref (caps);
745 goto not_negotiated;
746 }
747
748 /* Fixate pixel format */
749 caps = gst_caps_fixate (caps);
750
751 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
752
753 /* Try to set negotiated format, on success replace acquired format */
754 if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
755 gst_video_info_from_caps (&info, caps);
756 else
757 gst_v4l2_clear_error (&error);
758 gst_caps_unref (caps);
759
760 output_state = gst_video_decoder_set_output_state (decoder,
761 info.finfo->format, info.width, info.height, self->input_state);
762
763 /* Copy the rest of the information, there might be more in the future */
764 output_state->info.interlace_mode = info.interlace_mode;
765 output_state->info.colorimetry = info.colorimetry;
766 gst_video_codec_state_unref (output_state);
767
768 if (!gst_video_decoder_negotiate (decoder)) {
769 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
770 goto flushing;
771 else
772 goto not_negotiated;
773 }
774
775 /* Ensure our internal pool is activated */
776 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
777 TRUE))
778 goto activate_failed;
779 }
780
781 task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
782 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
783 /* It's possible that the processing thread stopped due to an error */
784 if (self->output_flow != GST_FLOW_OK &&
785 self->output_flow != GST_FLOW_FLUSHING) {
786 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
787 ret = self->output_flow;
788 goto drop;
789 }
790
791 GST_DEBUG_OBJECT (self, "Starting decoding thread");
792
793 /* Start the processing task, when it quits, the task will disable input
794 * processing to unlock input if draining, or prevent potential block */
795 self->output_flow = GST_FLOW_FLUSHING;
796 if (!gst_pad_start_task (decoder->srcpad,
797 (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
798 goto start_task_failed;
799 }
800
801 if (!processed) {
802 GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
803 GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
804 frame->system_frame_number);
805 ret =
806 gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
807 pool), &frame->input_buffer, &frame->system_frame_number);
808 GST_VIDEO_DECODER_STREAM_LOCK (decoder);
809
810 if (ret == GST_FLOW_FLUSHING) {
811 if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
812 GST_TASK_STARTED)
813 ret = self->output_flow;
814 goto drop;
815 } else if (ret != GST_FLOW_OK) {
816 goto process_failed;
817 }
818 }
819
820 /* No need to keep input around */
821 tmp = frame->input_buffer;
822 frame->input_buffer = gst_buffer_new ();
823 gst_buffer_copy_into (frame->input_buffer, tmp,
824 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
825 GST_BUFFER_COPY_META, 0, 0);
826 gst_buffer_unref (tmp);
827
828 gst_video_codec_frame_unref (frame);
829 return ret;
830
831 /* ERRORS */
832 not_negotiated:
833 {
834 GST_ERROR_OBJECT (self, "not negotiated");
835 ret = GST_FLOW_NOT_NEGOTIATED;
836 gst_v4l2_error (self, &error);
837 goto drop;
838 }
839 activate_failed:
840 {
841 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
842 (_("Failed to allocate required memory.")),
843 ("Buffer pool activation failed"));
844 ret = GST_FLOW_ERROR;
845 goto drop;
846 }
847 flushing:
848 {
849 ret = GST_FLOW_FLUSHING;
850 goto drop;
851 }
852
853 start_task_failed:
854 {
855 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
856 (_("Failed to start decoding thread.")), (NULL));
857 ret = GST_FLOW_ERROR;
858 goto drop;
859 }
860 process_failed:
861 {
862 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
863 (_("Failed to process frame.")),
864 ("Maybe be due to not enough memory or failing driver"));
865 ret = GST_FLOW_ERROR;
866 goto drop;
867 }
868 drop:
869 {
870 gst_video_decoder_drop_frame (decoder, frame);
871 return ret;
872 }
873 }
874
875 static gboolean
gst_v4l2_video_dec_decide_allocation(GstVideoDecoder * decoder,GstQuery * query)876 gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
877 GstQuery * query)
878 {
879 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
880 GstClockTime latency;
881 gboolean ret = FALSE;
882
883 if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
884 ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
885 query);
886
887 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
888 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
889 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
890 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
891 self->v4l2capture->min_buffers, self->v4l2capture->duration);
892 gst_video_decoder_set_latency (decoder, latency, latency);
893 } else {
894 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
895 }
896
897 return ret;
898 }
899
900 static gboolean
gst_v4l2_video_dec_src_query(GstVideoDecoder * decoder,GstQuery * query)901 gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
902 {
903 gboolean ret = TRUE;
904 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
905
906 switch (GST_QUERY_TYPE (query)) {
907 case GST_QUERY_CAPS:{
908 GstCaps *filter, *result = NULL;
909 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);
910
911 gst_query_parse_caps (query, &filter);
912
913 if (self->probed_srccaps)
914 result = gst_caps_ref (self->probed_srccaps);
915 else
916 result = gst_pad_get_pad_template_caps (pad);
917
918 if (filter) {
919 GstCaps *tmp = result;
920 result =
921 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
922 gst_caps_unref (tmp);
923 }
924
925 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
926
927 gst_query_set_caps_result (query, result);
928 gst_caps_unref (result);
929 break;
930 }
931
932 default:
933 ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
934 break;
935 }
936
937 return ret;
938 }
939
940 static GstCaps *
gst_v4l2_video_dec_sink_getcaps(GstVideoDecoder * decoder,GstCaps * filter)941 gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
942 {
943 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
944 GstCaps *result;
945
946 result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
947 filter);
948
949 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
950
951 return result;
952 }
953
954 static gboolean
gst_v4l2_video_dec_sink_event(GstVideoDecoder * decoder,GstEvent * event)955 gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
956 {
957 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
958 gboolean ret;
959 GstEventType type = GST_EVENT_TYPE (event);
960
961 switch (type) {
962 case GST_EVENT_FLUSH_START:
963 GST_DEBUG_OBJECT (self, "flush start");
964 gst_v4l2_object_unlock (self->v4l2output);
965 gst_v4l2_object_unlock (self->v4l2capture);
966 break;
967 default:
968 break;
969 }
970
971 ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
972
973 switch (type) {
974 case GST_EVENT_FLUSH_START:
975 /* The processing thread should stop now, wait for it */
976 gst_pad_stop_task (decoder->srcpad);
977 GST_DEBUG_OBJECT (self, "flush start done");
978 break;
979 default:
980 break;
981 }
982
983 return ret;
984 }
985
986 static GstStateChangeReturn
gst_v4l2_video_dec_change_state(GstElement * element,GstStateChange transition)987 gst_v4l2_video_dec_change_state (GstElement * element,
988 GstStateChange transition)
989 {
990 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
991 GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
992
993 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
994 g_atomic_int_set (&self->active, FALSE);
995 gst_v4l2_object_unlock (self->v4l2output);
996 gst_v4l2_object_unlock (self->v4l2capture);
997 gst_pad_stop_task (decoder->srcpad);
998 }
999
1000 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1001 }
1002
1003 static void
gst_v4l2_video_dec_dispose(GObject * object)1004 gst_v4l2_video_dec_dispose (GObject * object)
1005 {
1006 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
1007
1008 gst_caps_replace (&self->probed_sinkcaps, NULL);
1009 gst_caps_replace (&self->probed_srccaps, NULL);
1010
1011 G_OBJECT_CLASS (parent_class)->dispose (object);
1012 }
1013
1014 static void
gst_v4l2_video_dec_finalize(GObject * object)1015 gst_v4l2_video_dec_finalize (GObject * object)
1016 {
1017 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);
1018
1019 gst_v4l2_object_destroy (self->v4l2capture);
1020 gst_v4l2_object_destroy (self->v4l2output);
1021
1022 G_OBJECT_CLASS (parent_class)->finalize (object);
1023 }
1024
1025 static void
gst_v4l2_video_dec_init(GstV4l2VideoDec * self)1026 gst_v4l2_video_dec_init (GstV4l2VideoDec * self)
1027 {
1028 /* V4L2 object are created in subinstance_init */
1029 }
1030
1031 static void
gst_v4l2_video_dec_subinstance_init(GTypeInstance * instance,gpointer g_class)1032 gst_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
1033 {
1034 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1035 GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (instance);
1036 GstVideoDecoder *decoder = GST_VIDEO_DECODER (instance);
1037
1038 gst_video_decoder_set_packetized (decoder, TRUE);
1039
1040 self->v4l2output = gst_v4l2_object_new (GST_ELEMENT (self),
1041 GST_OBJECT (GST_VIDEO_DECODER_SINK_PAD (self)),
1042 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1043 gst_v4l2_get_output, gst_v4l2_set_output, NULL);
1044 self->v4l2output->no_initial_format = TRUE;
1045 self->v4l2output->keep_aspect = FALSE;
1046
1047 self->v4l2capture = gst_v4l2_object_new (GST_ELEMENT (self),
1048 GST_OBJECT (GST_VIDEO_DECODER_SRC_PAD (self)),
1049 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1050 gst_v4l2_get_input, gst_v4l2_set_input, NULL);
1051 }
1052
1053 static void
gst_v4l2_video_dec_class_init(GstV4l2VideoDecClass * klass)1054 gst_v4l2_video_dec_class_init (GstV4l2VideoDecClass * klass)
1055 {
1056 GstElementClass *element_class;
1057 GObjectClass *gobject_class;
1058 GstVideoDecoderClass *video_decoder_class;
1059
1060 parent_class = g_type_class_peek_parent (klass);
1061
1062 element_class = (GstElementClass *) klass;
1063 gobject_class = (GObjectClass *) klass;
1064 video_decoder_class = (GstVideoDecoderClass *) klass;
1065
1066 GST_DEBUG_CATEGORY_INIT (gst_v4l2_video_dec_debug, "v4l2videodec", 0,
1067 "V4L2 Video Decoder");
1068
1069 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_dispose);
1070 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finalize);
1071 gobject_class->set_property =
1072 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_property);
1073 gobject_class->get_property =
1074 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_get_property);
1075
1076 video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_open);
1077 video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_close);
1078 video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_start);
1079 video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_stop);
1080 video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_finish);
1081 video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_flush);
1082 video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_drain);
1083 video_decoder_class->set_format =
1084 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_set_format);
1085 video_decoder_class->negotiate =
1086 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_negotiate);
1087 video_decoder_class->decide_allocation =
1088 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_decide_allocation);
1089 /* FIXME propose_allocation or not ? */
1090 video_decoder_class->handle_frame =
1091 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_handle_frame);
1092 video_decoder_class->getcaps =
1093 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_getcaps);
1094 video_decoder_class->src_query =
1095 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_src_query);
1096 video_decoder_class->sink_event =
1097 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_sink_event);
1098
1099 element_class->change_state =
1100 GST_DEBUG_FUNCPTR (gst_v4l2_video_dec_change_state);
1101
1102 gst_v4l2_object_install_m2m_properties_helper (gobject_class);
1103 }
1104
1105 static void
gst_v4l2_video_dec_subclass_init(gpointer g_class,gpointer data)1106 gst_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
1107 {
1108 GstV4l2VideoDecClass *klass = GST_V4L2_VIDEO_DEC_CLASS (g_class);
1109 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1110 GstV4l2VideoDecCData *cdata = data;
1111
1112 klass->default_device = cdata->device;
1113
1114 gst_element_class_add_pad_template (element_class,
1115 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1116 cdata->sink_caps));
1117 gst_element_class_add_pad_template (element_class,
1118 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1119 cdata->src_caps));
1120
1121 gst_element_class_set_metadata (element_class, cdata->longname,
1122 "Codec/Decoder/Video/Hardware", cdata->description,
1123 "Nicolas Dufresne <nicolas.dufresne@collabora.com>");
1124
1125 gst_caps_unref (cdata->sink_caps);
1126 gst_caps_unref (cdata->src_caps);
1127 g_free (cdata);
1128 }
1129
1130 /* Probing functions */
1131 gboolean
gst_v4l2_is_video_dec(GstCaps * sink_caps,GstCaps * src_caps)1132 gst_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
1133 {
1134 gboolean ret = FALSE;
1135
1136 if (gst_caps_is_subset (sink_caps, gst_v4l2_object_get_codec_caps ())
1137 && gst_caps_is_subset (src_caps, gst_v4l2_object_get_raw_caps ()))
1138 ret = TRUE;
1139
1140 return ret;
1141 }
1142
1143 static gchar *
gst_v4l2_video_dec_set_metadata(GstStructure * s,GstV4l2VideoDecCData * cdata,const gchar * basename)1144 gst_v4l2_video_dec_set_metadata (GstStructure * s, GstV4l2VideoDecCData * cdata,
1145 const gchar * basename)
1146 {
1147 gchar *codec_name = NULL;
1148 gchar *type_name = NULL;
1149
1150 #define SET_META(codec) \
1151 G_STMT_START { \
1152 cdata->longname = "V4L2 " codec " Decoder"; \
1153 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1154 codec_name = g_ascii_strdown (codec, -1); \
1155 } G_STMT_END
1156
1157 if (gst_structure_has_name (s, "image/jpeg")) {
1158 SET_META ("JPEG");
1159 } else if (gst_structure_has_name (s, "video/mpeg")) {
1160 gint mpegversion = 0;
1161 gst_structure_get_int (s, "mpegversion", &mpegversion);
1162
1163 if (mpegversion == 2) {
1164 SET_META ("MPEG2");
1165 cdata->codec = gst_v4l2_mpeg2_get_codec ();
1166 } else {
1167 SET_META ("MPEG4");
1168 cdata->codec = gst_v4l2_mpeg4_get_codec ();
1169 }
1170 } else if (gst_structure_has_name (s, "video/x-h263")) {
1171 SET_META ("H263");
1172 } else if (gst_structure_has_name (s, "video/x-fwht")) {
1173 SET_META ("FWHT");
1174 } else if (gst_structure_has_name (s, "video/x-h264")) {
1175 SET_META ("H264");
1176 cdata->codec = gst_v4l2_h264_get_codec ();
1177 } else if (gst_structure_has_name (s, "video/x-h265")) {
1178 SET_META ("H265");
1179 cdata->codec = gst_v4l2_h265_get_codec ();
1180 } else if (gst_structure_has_name (s, "video/x-wmv")) {
1181 SET_META ("VC1");
1182 } else if (gst_structure_has_name (s, "video/x-vp8")) {
1183 SET_META ("VP8");
1184 cdata->codec = gst_v4l2_vp8_get_codec ();
1185 } else if (gst_structure_has_name (s, "video/x-vp9")) {
1186 SET_META ("VP9");
1187 cdata->codec = gst_v4l2_vp9_get_codec ();
1188 } else if (gst_structure_has_name (s, "video/x-bayer")) {
1189 SET_META ("BAYER");
1190 } else if (gst_structure_has_name (s, "video/x-sonix")) {
1191 SET_META ("SONIX");
1192 } else if (gst_structure_has_name (s, "video/x-pwc1")) {
1193 SET_META ("PWC1");
1194 } else if (gst_structure_has_name (s, "video/x-pwc2")) {
1195 SET_META ("PWC2");
1196 } else {
1197 /* This code should be kept on sync with the exposed CODEC type of format
1198 * from gstv4l2object.c. This warning will only occur in case we forget
1199 * to also add a format here. */
1200 gchar *s_str = gst_structure_to_string (s);
1201 g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
1202 "bug, please report at https://bugs.gnome.org", s_str);
1203 g_free (s_str);
1204 }
1205
1206 if (codec_name) {
1207 type_name = g_strdup_printf ("v4l2%sdec", codec_name);
1208 if (g_type_from_name (type_name) != 0) {
1209 g_free (type_name);
1210 type_name = g_strdup_printf ("v4l2%s%sdec", basename, codec_name);
1211 }
1212
1213 g_free (codec_name);
1214 }
1215
1216 return type_name;
1217 #undef SET_META
1218 }
1219
1220 void
gst_v4l2_video_dec_register(GstPlugin * plugin,const gchar * basename,const gchar * device_path,gint video_fd,GstCaps * sink_caps,GstCaps * src_caps)1221 gst_v4l2_video_dec_register (GstPlugin * plugin, const gchar * basename,
1222 const gchar * device_path, gint video_fd, GstCaps * sink_caps,
1223 GstCaps * src_caps)
1224 {
1225 gint i;
1226
1227 for (i = 0; i < gst_caps_get_size (sink_caps); i++) {
1228 GstV4l2VideoDecCData *cdata;
1229 GstStructure *s;
1230 GTypeQuery type_query;
1231 GTypeInfo type_info = { 0, };
1232 GType type, subtype;
1233 gchar *type_name;
1234
1235 s = gst_caps_get_structure (sink_caps, i);
1236
1237 cdata = g_new0 (GstV4l2VideoDecCData, 1);
1238 cdata->device = g_strdup (device_path);
1239 cdata->sink_caps = gst_caps_new_empty ();
1240 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
1241 cdata->src_caps = gst_caps_ref (src_caps);
1242 type_name = gst_v4l2_video_dec_set_metadata (s, cdata, basename);
1243
1244 /* Skip over if we hit an unmapped type */
1245 if (!type_name) {
1246 g_free (cdata);
1247 continue;
1248 }
1249
1250 if (cdata->codec != NULL && cdata->codec != gst_v4l2_vp8_get_codec ()
1251 && cdata->codec != gst_v4l2_vp9_get_codec ()) {
1252 GValue value = G_VALUE_INIT;
1253
1254 if (gst_v4l2_codec_probe_levels (cdata->codec, video_fd, &value)) {
1255 gst_caps_set_value (cdata->sink_caps, "level", &value);
1256 g_value_unset (&value);
1257 }
1258
1259 if (gst_v4l2_codec_probe_profiles (cdata->codec, video_fd, &value)) {
1260 gst_caps_set_value (cdata->sink_caps, "profile", &value);
1261 g_value_unset (&value);
1262 }
1263 }
1264
1265 type = gst_v4l2_video_dec_get_type ();
1266 g_type_query (type, &type_query);
1267 memset (&type_info, 0, sizeof (type_info));
1268 type_info.class_size = type_query.class_size;
1269 type_info.instance_size = type_query.instance_size;
1270 type_info.class_init = gst_v4l2_video_dec_subclass_init;
1271 type_info.class_data = cdata;
1272 type_info.instance_init = gst_v4l2_video_dec_subinstance_init;
1273
1274 subtype = g_type_register_static (type, type_name, &type_info, 0);
1275 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
1276 subtype))
1277 GST_WARNING ("Failed to register plugin '%s'", type_name);
1278
1279 g_free (type_name);
1280 }
1281 }
1282