gstv4l2videodec.c 38.7 KB
Newer Older
1
2
/*
 * Copyright (C) 2014 Collabora Ltd.
3
 *     Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

26
27
28
29
30
31
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

32
#include "gstv4l2object.h"
33
34
#include "gstv4l2videodec.h"

35
36
#include "gstv4l2h264codec.h"
#include "gstv4l2h265codec.h"
37
#include "gstv4l2mpeg2codec.h"
38
39
40
41
#include "gstv4l2mpeg4codec.h"
#include "gstv4l2vp8codec.h"
#include "gstv4l2vp9codec.h"

42
43
44
45
46
47
#include <string.h>
#include <gst/gst-i18n-plugin.h>

GST_DEBUG_CATEGORY_STATIC (gst_v4l2_video_dec_debug);
#define GST_CAT_DEFAULT gst_v4l2_video_dec_debug

48
49
50
51
52
typedef struct
{
  gchar *device;
  GstCaps *sink_caps;
  GstCaps *src_caps;
53
54
  const gchar *longname;
  const gchar *description;
55
  const GstV4l2Codec *codec;
56
} GstV4l2VideoDecCData;
57

58
59
60
enum
{
  PROP_0,
61
  V4L2_STD_OBJECT_PROPS
62
63
};

64
65
66
#define gst_v4l2_video_dec_parent_class parent_class
G_DEFINE_ABSTRACT_TYPE (GstV4l2VideoDec, gst_v4l2_video_dec,
    GST_TYPE_VIDEO_DECODER);
67

68
69
static GstFlowReturn gst_v4l2_video_dec_finish (GstVideoDecoder * decoder);

70
71
72
73
74
75
76
77
static void
gst_v4l2_video_dec_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);

  switch (prop_id) {
    case PROP_CAPTURE_IO_MODE:
78
79
80
81
      if (!gst_v4l2_object_set_property_helper (self->v4l2capture,
              prop_id, value, pspec)) {
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
      break;

      /* By default, only set on output */
    default:
      if (!gst_v4l2_object_set_property_helper (self->v4l2output,
              prop_id, value, pspec)) {
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      }
      break;
  }
}

static void
gst_v4l2_video_dec_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);

  switch (prop_id) {
    case PROP_CAPTURE_IO_MODE:
102
103
104
105
      if (!gst_v4l2_object_get_property_helper (self->v4l2capture,
              prop_id, value, pspec)) {
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      break;

      /* By default read from output */
    default:
      if (!gst_v4l2_object_get_property_helper (self->v4l2output,
              prop_id, value, pspec)) {
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      }
      break;
  }
}

static gboolean
gst_v4l2_video_dec_open (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
122
  GstV4l2Error error = GST_V4L2_ERROR_INIT;
123
  GstCaps *codec_caps;
124
125
126

  GST_DEBUG_OBJECT (self, "Opening");

127
  if (!gst_v4l2_object_open (self->v4l2output, &error))
128
129
130
131
132
    goto failure;

  if (!gst_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
    goto failure;

133
134
135
136
  codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
  self->probed_sinkcaps = gst_v4l2_object_probe_caps (self->v4l2output,
      codec_caps);
  gst_caps_unref (codec_caps);
137
138
139
140
141
142
143
144

  if (gst_caps_is_empty (self->probed_sinkcaps))
    goto no_encoded_format;

  return TRUE;

no_encoded_format:
  GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
145
      (_("Decoder on device %s has no supported input format"),
146
147
148
149
150
151
152
153
154
155
156
157
158
          self->v4l2output->videodev), (NULL));
  goto failure;

failure:
  if (GST_V4L2_IS_OPEN (self->v4l2output))
    gst_v4l2_object_close (self->v4l2output);

  if (GST_V4L2_IS_OPEN (self->v4l2capture))
    gst_v4l2_object_close (self->v4l2capture);

  gst_caps_replace (&self->probed_srccaps, NULL);
  gst_caps_replace (&self->probed_sinkcaps, NULL);

159
160
  gst_v4l2_error (self, &error);

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  return FALSE;
}

static gboolean
gst_v4l2_video_dec_close (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

  GST_DEBUG_OBJECT (self, "Closing");

  gst_v4l2_object_close (self->v4l2output);
  gst_v4l2_object_close (self->v4l2capture);
  gst_caps_replace (&self->probed_srccaps, NULL);
  gst_caps_replace (&self->probed_sinkcaps, NULL);

  return TRUE;
}

static gboolean
gst_v4l2_video_dec_start (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

  GST_DEBUG_OBJECT (self, "Starting");

  gst_v4l2_object_unlock (self->v4l2output);
  g_atomic_int_set (&self->active, TRUE);
  self->output_flow = GST_FLOW_OK;

  return TRUE;
}

static gboolean
gst_v4l2_video_dec_stop (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

  GST_DEBUG_OBJECT (self, "Stopping");

200
201
202
203
204
  gst_v4l2_object_unlock (self->v4l2output);
  gst_v4l2_object_unlock (self->v4l2capture);

  /* Wait for capture thread to stop */
  gst_pad_stop_task (decoder->srcpad);
205
206

  GST_VIDEO_DECODER_STREAM_LOCK (decoder);
207
208
209
  self->output_flow = GST_FLOW_OK;
  GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  /* Should have been flushed already */
  g_assert (g_atomic_int_get (&self->active) == FALSE);

  gst_v4l2_object_stop (self->v4l2output);
  gst_v4l2_object_stop (self->v4l2capture);

  if (self->input_state) {
    gst_video_codec_state_unref (self->input_state);
    self->input_state = NULL;
  }

  GST_DEBUG_OBJECT (self, "Stopped");

  return TRUE;
}

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
static gboolean
compatible_caps (GstV4l2VideoDec * self, GstCaps * new_caps)
{
  GstCaps *current_caps, *caps1, *caps2;
  GstStructure *s;
  gboolean ret;

  current_caps = gst_v4l2_object_get_current_caps (self->v4l2output);
  if (!current_caps)
    return FALSE;

  caps1 = gst_caps_copy (current_caps);
  s = gst_caps_get_structure (caps1, 0);
  gst_structure_remove_field (s, "framerate");

  caps2 = gst_caps_copy (new_caps);
  s = gst_caps_get_structure (caps2, 0);
  gst_structure_remove_field (s, "framerate");

  ret = gst_caps_is_equal (caps1, caps2);

  gst_caps_unref (caps1);
  gst_caps_unref (caps2);
  gst_caps_unref (current_caps);

  return ret;
}

254
255
256
257
static gboolean
gst_v4l2_video_dec_set_format (GstVideoDecoder * decoder,
    GstVideoCodecState * state)
{
258
  GstV4l2Error error = GST_V4L2_ERROR_INIT;
259
260
261
262
263
264
  gboolean ret = TRUE;
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

  GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);

  if (self->input_state) {
265
    if (compatible_caps (self, state->caps)) {
266
267
268
269
      GST_DEBUG_OBJECT (self, "Compatible caps");
      goto done;
    }
    gst_video_codec_state_unref (self->input_state);
270
    self->input_state = NULL;
271

272
273
274
    gst_v4l2_video_dec_finish (decoder);
    gst_v4l2_object_stop (self->v4l2output);

275
276
277
278
279
280
281
    /* The renegotiation flow don't blend with the base class flow. To properly
     * stop the capture pool, if the buffers can't be orphaned, we need to
     * reclaim our buffers, which will happend through the allocation query.
     * The allocation query is triggered by gst_video_decoder_negotiate() which
     * requires the output caps to be set, but we can't know this information
     * as we rely on the decoder, which requires the capture queue to be
     * stopped.
282
283
284
285
286
287
     *
     * To workaround this issue, we simply run an allocation query with the
     * old negotiated caps in order to drain/reclaim our buffers. That breaks
     * the complexity and should not have much impact in performance since the
     * following allocation query will happen on a drained pipeline and won't
     * block. */
288
289
    if (self->v4l2capture->pool &&
        !gst_v4l2_buffer_pool_orphan (&self->v4l2capture->pool)) {
290
      GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
291
292
293
294
295
296
      if (caps) {
        GstQuery *query = gst_query_new_allocation (caps, FALSE);
        gst_pad_peer_query (decoder->srcpad, query);
        gst_query_unref (query);
        gst_caps_unref (caps);
      }
297
298
299
300
    }

    gst_v4l2_object_stop (self->v4l2capture);
    self->output_flow = GST_FLOW_OK;
301
302
  }

303
  ret = gst_v4l2_object_set_format (self->v4l2output, state->caps, &error);
304

305
306
307
308
309
310
311
  gst_caps_replace (&self->probed_srccaps, NULL);
  self->probed_srccaps = gst_v4l2_object_probe_caps (self->v4l2capture,
      gst_v4l2_object_get_raw_caps ());

  if (gst_caps_is_empty (self->probed_srccaps))
    goto no_raw_format;

312
313
  if (ret)
    self->input_state = gst_video_codec_state_ref (state);
314
315
  else
    gst_v4l2_error (self, &error);
316
317
318

done:
  return ret;
319
320
321
322
323
324

no_raw_format:
  GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
      (_("Decoder on device %s has no supported output format"),
          self->v4l2output->videodev), (NULL));
  return GST_FLOW_ERROR;
325
326
327
328
329
330
331
}

static gboolean
gst_v4l2_video_dec_flush (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

332
  GST_DEBUG_OBJECT (self, "Flushed");
333

334
335
  /* Ensure the processing thread has stopped for the reverse playback
   * discount case */
336
  if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED) {
337
    GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
338

339
340
341
    gst_v4l2_object_unlock (self->v4l2output);
    gst_v4l2_object_unlock (self->v4l2capture);
    gst_pad_stop_task (decoder->srcpad);
342
    GST_VIDEO_DECODER_STREAM_LOCK (decoder);
343
  }
344

345
346
347
  if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
    return TRUE;

348
  self->output_flow = GST_FLOW_OK;
349

350
351
352
  gst_v4l2_object_unlock_stop (self->v4l2output);
  gst_v4l2_object_unlock_stop (self->v4l2capture);

353
354
355
  if (self->v4l2output->pool)
    gst_v4l2_buffer_pool_flush (self->v4l2output->pool);

356
357
358
  /* gst_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
   * called after gst_v4l2_object_unlock_stop() stopped flushing the buffer
   * pool. */
359
360
361
  if (self->v4l2capture->pool)
    gst_v4l2_buffer_pool_flush (self->v4l2capture->pool);

362
363
364
365
366
367
  return TRUE;
}

static gboolean
gst_v4l2_video_dec_negotiate (GstVideoDecoder * decoder)
{
368
369
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

370
  /* We don't allow renegotiation without careful disabling the pool */
371
372
373
374
  if (self->v4l2capture->pool &&
      gst_buffer_pool_is_active (GST_BUFFER_POOL (self->v4l2capture->pool)))
    return TRUE;

375
376
377
  return GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
}

378
379
380
381
382
383
384
385
386
387
388
389
390
static gboolean
gst_v4l2_decoder_cmd (GstV4l2Object * v4l2object, guint cmd, guint flags)
{
  struct v4l2_decoder_cmd dcmd = { 0, };

  GST_DEBUG_OBJECT (v4l2object->element,
      "sending v4l2 decoder command %u with flags %u", cmd, flags);

  if (!GST_V4L2_IS_OPEN (v4l2object))
    return FALSE;

  dcmd.cmd = cmd;
  dcmd.flags = flags;
391
  if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
    goto dcmd_failed;

  return TRUE;

dcmd_failed:
  if (errno == ENOTTY) {
    GST_INFO_OBJECT (v4l2object->element,
        "Failed to send decoder command %u with flags %u for '%s'. (%s)",
        cmd, flags, v4l2object->videodev, g_strerror (errno));
  } else {
    GST_ERROR_OBJECT (v4l2object->element,
        "Failed to send decoder command %u with flags %u for '%s'. (%s)",
        cmd, flags, v4l2object->videodev, g_strerror (errno));
  }
  return FALSE;
}

409
410
411
412
413
414
415
static GstFlowReturn
gst_v4l2_video_dec_finish (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
  GstFlowReturn ret = GST_FLOW_OK;
  GstBuffer *buffer;

416
  if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
417
418
419
420
421
    goto done;

  GST_DEBUG_OBJECT (self, "Finishing decoding");

  GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
422
423
424
425
426
427

  if (gst_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0)) {
    GstTask *task = decoder->srcpad->task;

    /* If the decoder stop command succeeded, just wait until processing is
     * finished */
428
    GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
429
430
431
432
433
434
435
436
437
438
439
440
    GST_OBJECT_LOCK (task);
    while (GST_TASK_STATE (task) == GST_TASK_STARTED)
      GST_TASK_WAIT (task);
    GST_OBJECT_UNLOCK (task);
    ret = GST_FLOW_FLUSHING;
  } else {
    /* otherwise keep queuing empty buffers until the processing thread has
     * stopped, _pool_process() will return FLUSHING when that happened */
    while (ret == GST_FLOW_OK) {
      buffer = gst_buffer_new ();
      ret =
          gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
441
              v4l2output->pool), &buffer, NULL);
442
443
      gst_buffer_unref (buffer);
    }
444
445
  }

446
  /* and ensure the processing thread has stopped in case another error
447
   * occurred. */
448
449
450
  gst_v4l2_object_unlock (self->v4l2capture);
  gst_pad_stop_task (decoder->srcpad);
  GST_VIDEO_DECODER_STREAM_LOCK (decoder);
451

452
453
454
455
456
  if (ret == GST_FLOW_FLUSHING)
    ret = self->output_flow;

  GST_DEBUG_OBJECT (decoder, "Done draining buffers");

457
458
  /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */

459
460
461
462
done:
  return ret;
}

463
static GstFlowReturn
464
465
466
467
468
469
470
471
gst_v4l2_video_dec_drain (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

  GST_DEBUG_OBJECT (self, "Draining...");
  gst_v4l2_video_dec_finish (decoder);
  gst_v4l2_video_dec_flush (decoder);

472
  return GST_FLOW_OK;
473
474
}

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
static gboolean
check_system_frame_number_too_old (guint32 current, guint32 old)
{
  guint32 absdiff = current > old ? current - old : old - current;

  /* More than 100 frames in the past, or current wrapped around */
  if (absdiff > 100) {
    /* Wraparound and difference is actually smaller than 100 */
    if (absdiff > G_MAXUINT32 - 100)
      return FALSE;
    return TRUE;
  }

  return FALSE;
}

491
492
493
494
static void
gst_v4l2_video_dec_loop (GstVideoDecoder * decoder)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
495
  GstV4l2BufferPool *v4l2_pool = GST_V4L2_BUFFER_POOL (self->v4l2capture->pool);
496
497
498
499
500
501
502
  GstBufferPool *pool;
  GstVideoCodecFrame *frame;
  GstBuffer *buffer = NULL;
  GstFlowReturn ret;

  GST_LOG_OBJECT (decoder, "Allocate output buffer");

503
  self->output_flow = GST_FLOW_OK;
504
505
506
507
508
509
  do {
    /* We cannot use the base class allotate helper since it taking the internal
     * stream lock. we know that the acquire may need to poll until more frames
     * comes in and holding this lock would prevent that.
     */
    pool = gst_video_decoder_get_buffer_pool (decoder);
510

511
512
513
514
515
    /* Pool may be NULL if we started going to READY state */
    if (pool == NULL) {
      ret = GST_FLOW_FLUSHING;
      goto beach;
    }
516

517
518
    ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
    g_object_unref (pool);
519

520
521
522
523
    if (ret != GST_FLOW_OK)
      goto beach;

    GST_LOG_OBJECT (decoder, "Process output buffer");
524
    ret = gst_v4l2_buffer_pool_process (v4l2_pool, &buffer, NULL);
525
  } while (ret == GST_V4L2_FLOW_CORRUPTED_BUFFER);
526
527
528
529

  if (ret != GST_FLOW_OK)
    goto beach;

530
531
532
533
534
  if (GST_BUFFER_TIMESTAMP (buffer) % GST_SECOND != 0)
    GST_ERROR_OBJECT (decoder,
        "Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git");
  GST_LOG_OBJECT (decoder, "Got buffer for frame number %u",
      (guint32) (GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND));
535

536
537
538
  frame =
      gst_video_decoder_get_frame (decoder,
      GST_BUFFER_TIMESTAMP (buffer) / GST_SECOND);
539
  if (frame) {
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
    GstVideoCodecFrame *oldest_frame;
    gboolean warned = FALSE;

    /* Garbage collect old frames in case of codec bugs */
    while ((oldest_frame = gst_video_decoder_get_oldest_frame (decoder)) &&
        check_system_frame_number_too_old (frame->system_frame_number,
            oldest_frame->system_frame_number)) {
      gst_video_decoder_drop_frame (decoder, oldest_frame);
      oldest_frame = NULL;

      if (!warned) {
        g_warning ("%s: Too old frames, bug in decoder -- please file a bug",
            GST_ELEMENT_NAME (decoder));
        warned = TRUE;
      }
    }
    if (oldest_frame)
      gst_video_codec_frame_unref (oldest_frame);

559
560
561
562
563
564
565
566
567
568
569
570
571
572
    frame->output_buffer = buffer;
    buffer = NULL;
    ret = gst_video_decoder_finish_frame (decoder, frame);

    if (ret != GST_FLOW_OK)
      goto beach;
  } else {
    GST_WARNING_OBJECT (decoder, "Decoder is producing too many buffers");
    gst_buffer_unref (buffer);
  }

  return;

beach:
573
574
  GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
      gst_flow_get_name (ret));
575
576
577
578
579
580
581

  gst_buffer_replace (&buffer, NULL);
  self->output_flow = ret;
  gst_v4l2_object_unlock (self->v4l2output);
  gst_pad_pause_task (decoder->srcpad);
}

582
static gboolean
583
gst_v4l2_video_remove_padding (GstCapsFeatures * features,
584
585
586
587
588
589
590
    GstStructure * structure, gpointer user_data)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (user_data);
  GstVideoAlignment *align = &self->v4l2capture->align;
  GstVideoInfo *info = &self->v4l2capture->info;
  int width, height;

591
  if (!gst_structure_get_int (structure, "width", &width))
592
593
    return TRUE;

594
  if (!gst_structure_get_int (structure, "height", &height))
595
596
597
598
599
600
    return TRUE;

  if (align->padding_left != 0 || align->padding_top != 0 ||
      height != info->height + align->padding_bottom)
    return TRUE;

601
602
603
604
605
606
607
608
609
610
611
  if (height == info->height + align->padding_bottom) {
    /* Some drivers may round up width to the padded with */
    if (width == info->width + align->padding_right)
      gst_structure_set (structure,
          "width", G_TYPE_INT, width - align->padding_right,
          "height", G_TYPE_INT, height - align->padding_bottom, NULL);
    /* Some drivers may keep visible width and only round up bytesperline */
    else if (width == info->width)
      gst_structure_set (structure,
          "height", G_TYPE_INT, height - align->padding_bottom, NULL);
  }
612
613
614
615

  return TRUE;
}

616
617
618
619
static GstFlowReturn
gst_v4l2_video_dec_handle_frame (GstVideoDecoder * decoder,
    GstVideoCodecFrame * frame)
{
620
  GstV4l2Error error = GST_V4L2_ERROR_INIT;
621
622
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
  GstFlowReturn ret = GST_FLOW_OK;
623
624
  gboolean processed = FALSE;
  GstBuffer *tmp;
625
  GstTaskState task_state;
626
627
628
629
630
631
632

  GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);

  if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
    goto flushing;

  if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2output))) {
633
634
    if (!self->input_state)
      goto not_negotiated;
635
    if (!gst_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
636
            &error))
637
638
639
640
      goto not_negotiated;
  }

  if (G_UNLIKELY (!GST_V4L2_IS_ACTIVE (self->v4l2capture))) {
641
    GstBufferPool *pool = GST_BUFFER_POOL (self->v4l2output->pool);
642
643
644
    GstVideoInfo info;
    GstVideoCodecState *output_state;
    GstBuffer *codec_data;
645
    GstCaps *acquired_caps, *available_caps, *caps, *filter;
646
    GstStructure *st;
647
    guint32 dummy_frame_number = 0;
648
649
650
651
652
653
654

    GST_DEBUG_OBJECT (self, "Sending header");

    codec_data = self->input_state->codec_data;

    /* We are running in byte-stream mode, so we don't know the headers, but
     * we need to send something, otherwise the decoder will refuse to
655
     * initialize.
656
657
658
659
     */
    if (codec_data) {
      gst_buffer_ref (codec_data);
    } else {
660
661
      codec_data = gst_buffer_ref (frame->input_buffer);
      processed = TRUE;
662
663
    }

664
665
666
    /* Ensure input internal pool is active */
    if (!gst_buffer_pool_is_active (pool)) {
      GstStructure *config = gst_buffer_pool_get_config (pool);
667
668
669
670
      guint min = MAX (self->v4l2output->min_buffers,
          GST_V4L2_MIN_BUFFERS (self->v4l2output));
      guint max = VIDEO_MAX_FRAME;

671
      gst_buffer_pool_config_set_params (config, self->input_state->caps,
672
          self->v4l2output->info.size, min, max);
673
674
675
676
677
678
679
680

      /* There is no reason to refuse this config */
      if (!gst_buffer_pool_set_config (pool, config))
        goto activate_failed;

      if (!gst_buffer_pool_set_active (pool, TRUE))
        goto activate_failed;
    }
681

682
    GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
683
684
    GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
        processed ? frame->system_frame_number : 0);
685
    ret =
686
        gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->
687
688
            v4l2output->pool), &codec_data,
        processed ? &frame->system_frame_number : &dummy_frame_number);
689
690
691
692
    GST_VIDEO_DECODER_STREAM_LOCK (decoder);

    gst_buffer_unref (codec_data);

693
694
695
696
    /* init capture fps according to output */
    self->v4l2capture->info.fps_d = self->v4l2output->info.fps_d;
    self->v4l2capture->info.fps_n = self->v4l2output->info.fps_n;

697
698
699
700
    /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
     * in the compose rectangle. gst_v4l2_object_acquire_format() checks both
     * and returns the visible size as with/height and the coded size as
     * padding. */
701
    if (!gst_v4l2_object_acquire_format (self->v4l2capture, &info))
702
703
      goto not_negotiated;

704
705
706
707
    /* gst_v4l2_object_acquire_format() does not set fps, copy from sink */
    info.fps_n = self->v4l2output->info.fps_n;
    info.fps_d = self->v4l2output->info.fps_d;

708
709
    /* Create caps from the acquired format, remove the format field */
    acquired_caps = gst_video_info_to_caps (&info);
710
    GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
711
    st = gst_caps_get_structure (acquired_caps, 0);
712
713
    gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
        NULL);
714
715

    /* Probe currently available pixel formats */
716
    available_caps = gst_caps_copy (self->probed_srccaps);
717
    GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
718
719
720
721
722
723
724

    /* Replace coded size with visible size, we want to negotiate visible size
     * with downstream, not coded size. */
    gst_caps_map_in_place (available_caps, gst_v4l2_video_remove_padding, self);

    filter = gst_caps_intersect_full (available_caps, acquired_caps,
        GST_CAPS_INTERSECT_FIRST);
725
    GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
726
    gst_caps_unref (acquired_caps);
727
    gst_caps_unref (available_caps);
728
729
730
    caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
    gst_caps_unref (filter);

Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
731
    GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
732
733
734
735
736
737
    if (gst_caps_is_empty (caps)) {
      gst_caps_unref (caps);
      goto not_negotiated;
    }

    /* Fixate pixel format */
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
738
    caps = gst_caps_fixate (caps);
739
740
741
742

    GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);

    /* Try to set negotiated format, on success replace acquired format */
743
    if (gst_v4l2_object_set_format (self->v4l2capture, caps, &error))
744
      gst_video_info_from_caps (&info, caps);
745
746
    else
      gst_v4l2_clear_error (&error);
747
748
    gst_caps_unref (caps);

749
750
751
752
753
754
755
756
757
758
759
760
761
    output_state = gst_video_decoder_set_output_state (decoder,
        info.finfo->format, info.width, info.height, self->input_state);

    /* Copy the rest of the information, there might be more in the future */
    output_state->info.interlace_mode = info.interlace_mode;
    gst_video_codec_state_unref (output_state);

    if (!gst_video_decoder_negotiate (decoder)) {
      if (GST_PAD_IS_FLUSHING (decoder->srcpad))
        goto flushing;
      else
        goto not_negotiated;
    }
762
763
764
765
766

    /* Ensure our internal pool is activated */
    if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
            TRUE))
      goto activate_failed;
767
768
  }

769
770
  task_state = gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self));
  if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) {
771
772
773
    /* It's possible that the processing thread stopped due to an error */
    if (self->output_flow != GST_FLOW_OK &&
        self->output_flow != GST_FLOW_FLUSHING) {
774
775
776
777
778
779
780
781
782
      GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
      ret = self->output_flow;
      goto drop;
    }

    GST_DEBUG_OBJECT (self, "Starting decoding thread");

    /* Start the processing task, when it quits, the task will disable input
     * processing to unlock input if draining, or prevent potential block */
783
    self->output_flow = GST_FLOW_FLUSHING;
784
    if (!gst_pad_start_task (decoder->srcpad,
785
            (GstTaskFunction) gst_v4l2_video_dec_loop, self, NULL))
786
      goto start_task_failed;
787
788
  }

789
  if (!processed) {
790
    GST_VIDEO_DECODER_STREAM_UNLOCK (decoder);
791
792
    GST_LOG_OBJECT (decoder, "Passing buffer with system frame number %u",
        frame->system_frame_number);
793
794
    ret =
        gst_v4l2_buffer_pool_process (GST_V4L2_BUFFER_POOL (self->v4l2output->
795
            pool), &frame->input_buffer, &frame->system_frame_number);
796
797
798
    GST_VIDEO_DECODER_STREAM_LOCK (decoder);

    if (ret == GST_FLOW_FLUSHING) {
799
800
      if (gst_pad_get_task_state (GST_VIDEO_DECODER_SRC_PAD (self)) !=
          GST_TASK_STARTED)
801
        ret = self->output_flow;
802
803
804
      goto drop;
    } else if (ret != GST_FLOW_OK) {
      goto process_failed;
805
806
807
    }
  }

808
  /* No need to keep input around */
809
810
811
812
813
814
815
  tmp = frame->input_buffer;
  frame->input_buffer = gst_buffer_new ();
  gst_buffer_copy_into (frame->input_buffer, tmp,
      GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
      GST_BUFFER_COPY_META, 0, 0);
  gst_buffer_unref (tmp);

816
817
818
819
820
821
822
823
  gst_video_codec_frame_unref (frame);
  return ret;

  /* ERRORS */
not_negotiated:
  {
    GST_ERROR_OBJECT (self, "not negotiated");
    ret = GST_FLOW_NOT_NEGOTIATED;
824
    gst_v4l2_error (self, &error);
825
826
    goto drop;
  }
827
828
829
activate_failed:
  {
    GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
830
        (_("Failed to allocate required memory.")),
831
        ("Buffer pool activation failed"));
832
833
    ret = GST_FLOW_ERROR;
    goto drop;
834
  }
835
836
837
838
839
flushing:
  {
    ret = GST_FLOW_FLUSHING;
    goto drop;
  }
840
841
842
843
844
845
846
847

start_task_failed:
  {
    GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
        (_("Failed to start decoding thread.")), (NULL));
    ret = GST_FLOW_ERROR;
    goto drop;
  }
848
849
850
851
852
853
854
855
process_failed:
  {
    GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
        (_("Failed to process frame.")),
        ("Maybe be due to not enough memory or failing driver"));
    ret = GST_FLOW_ERROR;
    goto drop;
  }
856
857
858
859
860
861
862
863
864
865
866
867
drop:
  {
    gst_video_decoder_drop_frame (decoder, frame);
    return ret;
  }
}

static gboolean
gst_v4l2_video_dec_decide_allocation (GstVideoDecoder * decoder,
    GstQuery * query)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
868
  GstClockTime latency;
869
870
871
872
873
874
  gboolean ret = FALSE;

  if (gst_v4l2_object_decide_allocation (self->v4l2capture, query))
    ret = GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder,
        query);

875
876
  if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration)) {
    latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
877
878
879
    GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
        G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
        self->v4l2capture->min_buffers, self->v4l2capture->duration);
880
881
882
883
    gst_video_decoder_set_latency (decoder, latency, latency);
  } else {
    GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
  }
884

885
886
887
888
889
890
891
892
893
894
895
896
  return ret;
}

static gboolean
gst_v4l2_video_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
{
  gboolean ret = TRUE;
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_CAPS:{
      GstCaps *filter, *result = NULL;
897
898
      GstPad *pad = GST_VIDEO_DECODER_SRC_PAD (decoder);

899
900
901
902
903
      gst_query_parse_caps (query, &filter);

      if (self->probed_srccaps)
        result = gst_caps_ref (self->probed_srccaps);
      else
904
        result = gst_pad_get_pad_template_caps (pad);
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927

      if (filter) {
        GstCaps *tmp = result;
        result =
            gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
        gst_caps_unref (tmp);
      }

      GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);

      gst_query_set_caps_result (query, result);
      gst_caps_unref (result);
      break;
    }

    default:
      ret = GST_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
      break;
  }

  return ret;
}

Nicolas Dufresne's avatar
Nicolas Dufresne committed
928
929
static GstCaps *
gst_v4l2_video_dec_sink_getcaps (GstVideoDecoder * decoder, GstCaps * filter)
930
931
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
Nicolas Dufresne's avatar
Nicolas Dufresne committed
932
  GstCaps *result;
933

Nicolas Dufresne's avatar
Nicolas Dufresne committed
934
935
  result = gst_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
      filter);
936

Nicolas Dufresne's avatar
Nicolas Dufresne committed
937
  GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
938

Nicolas Dufresne's avatar
Nicolas Dufresne committed
939
  return result;
940
941
942
943
944
945
}

static gboolean
gst_v4l2_video_dec_sink_event (GstVideoDecoder * decoder, GstEvent * event)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (decoder);
946
  gboolean ret;
947
  GstEventType type = GST_EVENT_TYPE (event);
948

949
  switch (type) {
950
    case GST_EVENT_FLUSH_START:
951
      GST_DEBUG_OBJECT (self, "flush start");
952
953
      gst_v4l2_object_unlock (self->v4l2output);
      gst_v4l2_object_unlock (self->v4l2capture);
954
      break;
955
956
957
958
    default:
      break;
  }

959
960
  ret = GST_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);

961
  switch (type) {
962
963
964
965
966
967
968
969
970
971
    case GST_EVENT_FLUSH_START:
      /* The processing thread should stop now, wait for it */
      gst_pad_stop_task (decoder->srcpad);
      GST_DEBUG_OBJECT (self, "flush start done");
      break;
    default:
      break;
  }

  return ret;
972
973
974
975
976
977
978
}

static GstStateChangeReturn
gst_v4l2_video_dec_change_state (GstElement * element,
    GstStateChange transition)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (element);
979
  GstVideoDecoder *decoder = GST_VIDEO_DECODER (element);
980
981
982
983
984

  if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
    g_atomic_int_set (&self->active, FALSE);
    gst_v4l2_object_unlock (self->v4l2output);
    gst_v4l2_object_unlock (self->v4l2capture);
985
    gst_pad_stop_task (decoder->srcpad);
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  }

  return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
}

static void
gst_v4l2_video_dec_dispose (GObject * object)
{
  GstV4l2VideoDec *self = GST_V4L2_VIDEO_DEC (object);

  gst_caps_replace (&self->probed_sinkcaps, NULL);
  gst_caps_replace (&self->probed_srccaps, NULL);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}
For faster browsing, not all history is shown. View entire blame