summaryrefslogtreecommitdiff
path: root/gst/vaapi/gstvaapioverlay.c
blob: f3baf75e241a28f18db019484935c499b85cd272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 *  gstvaapioverlay.c - VA-API vpp overlay
 *
 *  Copyright (C) 2019 Intel Corporation
 *    Author: U. Artie Eoff <ullysses.a.eoff@intel.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This program 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
*/

#include "gstvaapioverlay.h"
#include "gstvaapipluginutil.h"
#include "gstvaapivideobufferpool.h"

#define GST_PLUGIN_NAME "vaapioverlay"
#define GST_PLUGIN_DESC "A VA-API overlay filter"

GST_DEBUG_CATEGORY_STATIC (gst_debug_vaapi_overlay);
#ifndef GST_DISABLE_GST_DEBUG
#define GST_CAT_DEFAULT gst_debug_vaapi_overlay
#else
#define GST_CAT_DEFAULT NULL
#endif

/* Default templates */
/* *INDENT-OFF* */
static const char gst_vaapi_overlay_sink_caps_str[] =
  GST_VAAPI_MAKE_SURFACE_CAPS ";"
  GST_VIDEO_CAPS_MAKE (GST_VAAPI_FORMATS_ALL);
/* *INDENT-ON* */

/* *INDENT-OFF* */
static const char gst_vaapi_overlay_src_caps_str[] =
  GST_VAAPI_MAKE_SURFACE_CAPS ";"
  GST_VIDEO_CAPS_MAKE (GST_VAAPI_FORMATS_ALL);
/* *INDENT-ON* */

/* *INDENT-OFF* */
static GstStaticPadTemplate gst_vaapi_overlay_sink_factory =
  GST_STATIC_PAD_TEMPLATE ("sink_%u",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS (gst_vaapi_overlay_sink_caps_str));
/* *INDENT-ON* */

/* *INDENT-OFF* */
static GstStaticPadTemplate gst_vaapi_overlay_src_factory =
  GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (gst_vaapi_overlay_src_caps_str));
/* *INDENT-ON* */

G_DEFINE_TYPE (GstVaapiOverlaySinkPad, gst_vaapi_overlay_sink_pad,
    GST_TYPE_VIDEO_AGGREGATOR_PAD);

#define DEFAULT_PAD_XPOS   0
#define DEFAULT_PAD_YPOS   0
#define DEFAULT_PAD_ALPHA  1.0

enum
{
  PROP_PAD_0,
  PROP_PAD_XPOS,
  PROP_PAD_YPOS,
  PROP_PAD_ALPHA,
};

static void
gst_vaapi_overlay_sink_pad_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstVaapiOverlaySinkPad *pad = GST_VAAPI_OVERLAY_SINK_PAD (object);

  switch (prop_id) {
    case PROP_PAD_XPOS:
      g_value_set_int (value, pad->xpos);
      break;
    case PROP_PAD_YPOS:
      g_value_set_int (value, pad->ypos);
      break;
    case PROP_PAD_ALPHA:
      g_value_set_double (value, pad->alpha);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_vaapi_overlay_sink_pad_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstVaapiOverlaySinkPad *pad = GST_VAAPI_OVERLAY_SINK_PAD (object);

  switch (prop_id) {
    case PROP_PAD_XPOS:
      pad->xpos = g_value_get_int (value);
      break;
    case PROP_PAD_YPOS:
      pad->ypos = g_value_get_int (value);
      break;
    case PROP_PAD_ALPHA:
      pad->alpha = g_value_get_double (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_vaapi_overlay_sink_pad_finalize (GObject * object)
{
  gst_vaapi_pad_private_finalize (GST_VAAPI_OVERLAY_SINK_PAD (object)->priv);

  G_OBJECT_CLASS (gst_vaapi_overlay_sink_pad_parent_class)->finalize (object);
}

static void
gst_vaapi_overlay_sink_pad_class_init (GstVaapiOverlaySinkPadClass * klass)
{
  GObjectClass *const gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = gst_vaapi_overlay_sink_pad_finalize;
  gobject_class->set_property = gst_vaapi_overlay_sink_pad_set_property;
  gobject_class->get_property = gst_vaapi_overlay_sink_pad_get_property;

  g_object_class_install_property (gobject_class, PROP_PAD_XPOS,
      g_param_spec_int ("xpos", "X Position", "X Position of the picture",
          G_MININT, G_MAXINT, DEFAULT_PAD_XPOS,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_PAD_YPOS,
      g_param_spec_int ("ypos", "Y Position", "Y Position of the picture",
          G_MININT, G_MAXINT, DEFAULT_PAD_YPOS,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class, PROP_PAD_ALPHA,
      g_param_spec_double ("alpha", "Alpha", "Alpha of the picture", 0.0, 1.0,
          DEFAULT_PAD_ALPHA,
          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
}

static void
gst_vaapi_overlay_sink_pad_init (GstVaapiOverlaySinkPad * pad)
{
  pad->xpos = DEFAULT_PAD_XPOS;
  pad->ypos = DEFAULT_PAD_YPOS;
  pad->alpha = DEFAULT_PAD_ALPHA;
  pad->priv = gst_vaapi_pad_private_new ();
}

static void
gst_vaapi_overlay_child_proxy_init (gpointer g_iface, gpointer iface_data);

G_DEFINE_TYPE_WITH_CODE (GstVaapiOverlay, gst_vaapi_overlay,
    GST_TYPE_VIDEO_AGGREGATOR, GST_VAAPI_PLUGIN_BASE_INIT_INTERFACES
    G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY,
        gst_vaapi_overlay_child_proxy_init));

GST_VAAPI_PLUGIN_BASE_DEFINE_SET_CONTEXT (gst_vaapi_overlay_parent_class);

static GstPad *
gst_vaapi_overlay_request_new_pad (GstElement * element, GstPadTemplate * templ,
    const gchar * req_name, const GstCaps * caps)
{
  GstPad *newpad = GST_PAD (GST_ELEMENT_CLASS
      (gst_vaapi_overlay_parent_class)->request_new_pad (element, templ,
          req_name, caps));

  if (!newpad)
    GST_DEBUG_OBJECT (element, "could not create/add pad");
  else
    gst_child_proxy_child_added (GST_CHILD_PROXY (element), G_OBJECT (newpad),
        GST_OBJECT_NAME (newpad));

  return newpad;
}

static void
gst_vaapi_overlay_release_pad (GstElement * element, GstPad * pad)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (element);

  gst_child_proxy_child_removed (GST_CHILD_PROXY (overlay), G_OBJECT (pad),
      GST_OBJECT_NAME (pad));

  GST_ELEMENT_CLASS (gst_vaapi_overlay_parent_class)->release_pad (element,
      pad);
}

static inline gboolean
gst_vaapi_overlay_ensure_display (GstVaapiOverlay * overlay)
{
  return gst_vaapi_plugin_base_ensure_display (GST_VAAPI_PLUGIN_BASE (overlay));
}

static gboolean
gst_vaapi_overlay_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
    GstQuery * query)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (agg);

  if (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT) {
    if (gst_vaapi_handle_context_query (GST_ELEMENT (overlay), query)) {
      GST_DEBUG_OBJECT (overlay, "sharing display %" GST_PTR_FORMAT,
          GST_VAAPI_PLUGIN_BASE_DISPLAY (overlay));
      return TRUE;
    }
  } else if (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION) {
    GstCaps *caps;

    gst_query_parse_allocation (query, &caps, NULL);

    if (caps == NULL)
      return FALSE;

    if (!gst_vaapi_plugin_base_pad_set_caps
        (GST_VAAPI_PLUGIN_BASE (overlay), GST_PAD (bpad), caps, NULL, NULL))
      return FALSE;
  }

  return GST_AGGREGATOR_CLASS (gst_vaapi_overlay_parent_class)->sink_query
      (agg, bpad, query);
}

static gboolean
gst_vaapi_overlay_src_query (GstAggregator * agg, GstQuery * query)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (agg);

  if (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT) {
    if (gst_vaapi_handle_context_query (GST_ELEMENT (overlay), query)) {
      GST_DEBUG_OBJECT (overlay, "sharing display %" GST_PTR_FORMAT,
          GST_VAAPI_PLUGIN_BASE_DISPLAY (overlay));
      return TRUE;
    }
  }

  return GST_AGGREGATOR_CLASS (gst_vaapi_overlay_parent_class)->src_query
      (agg, query);
}

static gboolean
gst_vaapi_overlay_start (GstAggregator * agg)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (agg);

  if (!gst_vaapi_plugin_base_open (GST_VAAPI_PLUGIN_BASE (overlay)))
    return FALSE;

  if (!gst_vaapi_overlay_ensure_display (overlay))
    return FALSE;

  overlay->blend =
      gst_vaapi_blend_new (GST_VAAPI_PLUGIN_BASE_DISPLAY (overlay));
  if (!overlay->blend)
    return FALSE;

  return TRUE;
}

static gboolean
_reset_sinkpad_private (GstElement * element, GstPad * pad, gpointer user_data)
{
  gst_vaapi_pad_private_reset (GST_VAAPI_OVERLAY_SINK_PAD (pad)->priv);

  return TRUE;
}

static gboolean
gst_vaapi_overlay_stop (GstAggregator * agg)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (agg);

  gst_vaapi_video_pool_replace (&overlay->blend_pool, NULL);
  gst_vaapi_blend_replace (&overlay->blend, NULL);

  gst_vaapi_plugin_base_close (GST_VAAPI_PLUGIN_BASE (overlay));

  gst_element_foreach_sink_pad (GST_ELEMENT (overlay), _reset_sinkpad_private,
      NULL);

  return TRUE;
}

static void
gst_vaapi_overlay_destroy (GstVaapiOverlay * const overlay)
{
  gst_vaapi_plugin_base_close (GST_VAAPI_PLUGIN_BASE (overlay));
  gst_element_foreach_sink_pad (GST_ELEMENT (overlay), _reset_sinkpad_private,
      NULL);
}

static void
gst_vaapi_overlay_finalize (GObject * object)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (object);

  gst_vaapi_overlay_destroy (overlay);
  gst_vaapi_plugin_base_finalize (GST_VAAPI_PLUGIN_BASE (overlay));

  G_OBJECT_CLASS (gst_vaapi_overlay_parent_class)->finalize (object);
}

static gboolean
gst_vaapi_overlay_propose_allocation (GstAggregator * agg,
    GstAggregatorPad * pad, GstQuery * decide_query, GstQuery * query)
{
  return gst_vaapi_plugin_base_pad_propose_allocation
      (GST_VAAPI_PLUGIN_BASE (agg), GST_PAD (pad), query);
}

static gboolean
gst_vaapi_overlay_decide_allocation (GstAggregator * agg, GstQuery * query)
{
  return gst_vaapi_plugin_base_decide_allocation
      (GST_VAAPI_PLUGIN_BASE (agg), query);
}

static gboolean
gst_vaapi_overlay_process_frames (GstVaapiOverlay * overlay)
{
  GList *l;

  for (l = GST_ELEMENT (overlay)->sinkpads; l; l = l->next) {
    GstVideoAggregatorPad *vagg_pad = GST_VIDEO_AGGREGATOR_PAD (l->data);
    GstVaapiOverlaySinkPad *pad = GST_VAAPI_OVERLAY_SINK_PAD (vagg_pad);
    GstVideoFrame *inframe =
        gst_video_aggregator_pad_get_prepared_frame (vagg_pad);
    GstBuffer *inbuf = NULL;
    GstBuffer *buf = gst_video_aggregator_pad_get_current_buffer (vagg_pad);
    GstVaapiVideoMeta *inbuf_meta;
    GstVaapiRectangle target_rect;

    if (gst_vaapi_plugin_base_pad_get_input_buffer (GST_VAAPI_PLUGIN_BASE
            (overlay), GST_PAD (pad), buf, &inbuf) != GST_FLOW_OK)
      return FALSE;

    /* Current sinkpad may have reached EOS */
    if (!inframe || !inbuf)
      continue;

    target_rect.x = pad->xpos;
    target_rect.y = pad->ypos;
    target_rect.width = GST_VIDEO_FRAME_WIDTH (inframe);
    target_rect.height = GST_VIDEO_FRAME_HEIGHT (inframe);

    inbuf_meta = gst_buffer_get_vaapi_video_meta (inbuf);

    if (!inbuf_meta) {
      gst_buffer_unref (inbuf);
      return FALSE;
    }

    if (!gst_vaapi_blend_process_render (overlay->blend,
            gst_vaapi_video_meta_get_surface (inbuf_meta),
            gst_vaapi_video_meta_get_render_rect (inbuf_meta),
            &target_rect, pad->alpha)) {
      gst_buffer_unref (inbuf);
      return FALSE;
    }

    gst_buffer_unref (inbuf);
  }

  return TRUE;
}

static GstFlowReturn
gst_vaapi_overlay_aggregate_frames (GstVideoAggregator * vagg,
    GstBuffer * outbuf)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (vagg);
  GstVaapiVideoMeta *outbuf_meta;
  GstVaapiSurface *outbuf_surface;
  GstVaapiSurfaceProxy *proxy;

  if (!overlay->blend_pool) {
    GstVaapiVideoPool *pool =
        gst_vaapi_surface_pool_new_full (GST_VAAPI_PLUGIN_BASE_DISPLAY
        (overlay),
        GST_VAAPI_PLUGIN_BASE_SRC_PAD_INFO (overlay), 0);
    if (!pool)
      return GST_FLOW_ERROR;
    gst_vaapi_video_pool_replace (&overlay->blend_pool, pool);
    gst_vaapi_video_pool_unref (pool);
  }

  outbuf_meta = gst_buffer_get_vaapi_video_meta (outbuf);
  if (!outbuf_meta)
    return GST_FLOW_ERROR;

  if (!gst_vaapi_video_meta_get_surface_proxy (outbuf_meta)) {
    proxy = gst_vaapi_surface_proxy_new_from_pool
        (GST_VAAPI_SURFACE_POOL (overlay->blend_pool));
    if (!proxy)
      return GST_FLOW_ERROR;
    gst_vaapi_video_meta_set_surface_proxy (outbuf_meta, proxy);
    gst_vaapi_surface_proxy_unref (proxy);
  }

  outbuf_surface = gst_vaapi_video_meta_get_surface (outbuf_meta);

  if (!gst_vaapi_blend_process_begin (overlay->blend, outbuf_surface))
    return GST_FLOW_ERROR;

  if (!gst_vaapi_overlay_process_frames (overlay)) {
    gst_vaapi_blend_process_end (overlay->blend);
    return GST_FLOW_ERROR;
  }

  if (!gst_vaapi_blend_process_end (overlay->blend))
    return GST_FLOW_ERROR;

  return GST_FLOW_OK;
}

static GstFlowReturn
gst_vaapi_overlay_create_output_buffer (GstVideoAggregator * vagg,
    GstBuffer ** outbuf)
{
  GstVaapiOverlay *const overlay = GST_VAAPI_OVERLAY (vagg);
  GstBufferPool *const pool =
      GST_VAAPI_PLUGIN_BASE_SRC_PAD_BUFFER_POOL (overlay);

  g_return_val_if_fail (pool != NULL, GST_FLOW_ERROR);

  if (!gst_buffer_pool_is_active (pool) &&
      !gst_buffer_pool_set_active (pool, TRUE)) {
    GST_ERROR_OBJECT (overlay, "failed to activate output video buffer pool");
    return GST_FLOW_ERROR;
  }

  *outbuf = NULL;
  if ((gst_buffer_pool_acquire_buffer (pool, outbuf, NULL) != GST_FLOW_OK)
      || !outbuf) {
    GST_ERROR_OBJECT (overlay, "failed to create output video buffer");
    return GST_FLOW_ERROR;
  }

  return GST_FLOW_OK;
}

static gboolean
gst_vaapi_overlay_negotiated_src_caps (GstAggregator * agg, GstCaps * caps)
{
  if (!gst_vaapi_plugin_base_set_caps (GST_VAAPI_PLUGIN_BASE (agg), NULL, caps))
    return FALSE;

  return
      GST_AGGREGATOR_CLASS (gst_vaapi_overlay_parent_class)->negotiated_src_caps
      (agg, caps);
}

static GstCaps *
gst_vaapi_overlay_fixate_src_caps (GstAggregator * agg, GstCaps * caps)
{
  GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
  GList *l;
  gint best_width = -1, best_height = -1;
  gint best_fps_n = -1, best_fps_d = -1;
  gdouble best_fps = 0.;
  GstCaps *ret = NULL;
  GstStructure *s;

  ret = gst_caps_make_writable (caps);

  GST_OBJECT_LOCK (vagg);
  for (l = GST_ELEMENT (vagg)->sinkpads; l; l = l->next) {
    GstVideoAggregatorPad *vaggpad = l->data;
    GstVaapiOverlaySinkPad *pad = GST_VAAPI_OVERLAY_SINK_PAD (vaggpad);
    gint this_width, this_height;
    gint fps_n, fps_d;
    gdouble cur_fps;

    fps_n = GST_VIDEO_INFO_FPS_N (&vaggpad->info);
    fps_d = GST_VIDEO_INFO_FPS_D (&vaggpad->info);

    this_width = GST_VIDEO_INFO_WIDTH (&vaggpad->info) + MAX (pad->xpos, 0);
    this_height = GST_VIDEO_INFO_HEIGHT (&vaggpad->info) + MAX (pad->ypos, 0);

    if (best_width < this_width)
      best_width = this_width;
    if (best_height < this_height)
      best_height = this_height;

    if (fps_d == 0)
      cur_fps = 0.0;
    else
      gst_util_fraction_to_double (fps_n, fps_d, &cur_fps);

    if (best_fps < cur_fps) {
      best_fps = cur_fps;
      best_fps_n = fps_n;
      best_fps_d = fps_d;
    }
  }
  GST_OBJECT_UNLOCK (vagg);

  if (best_fps_n <= 0 || best_fps_d <= 0 || best_fps == 0.0) {
    best_fps_n = 25;
    best_fps_d = 1;
    best_fps = 25.0;
  }

  s = gst_caps_get_structure (ret, 0);
  gst_structure_fixate_field_nearest_int (s, "width", best_width);
  gst_structure_fixate_field_nearest_int (s, "height", best_height);
  gst_structure_fixate_field_nearest_fraction (s, "framerate", best_fps_n,
      best_fps_d);

  return gst_caps_fixate (ret);
}

static GstVaapiPadPrivate *
gst_vaapi_overlay_get_vaapi_pad_private (GstVaapiPluginBase * plugin,
    GstPad * pad)
{
  if (GST_IS_VAAPI_OVERLAY_SINK_PAD (pad))
    return GST_VAAPI_OVERLAY_SINK_PAD (pad)->priv;

  g_assert (GST_VAAPI_PLUGIN_BASE_SRC_PAD (plugin) == pad);
  return GST_VAAPI_PLUGIN_BASE_SRC_PAD_PRIVATE (plugin);
}

static void
gst_vaapi_overlay_class_init (GstVaapiOverlayClass * klass)
{
  GObjectClass *const object_class = G_OBJECT_CLASS (klass);
  GstElementClass *const element_class = GST_ELEMENT_CLASS (klass);
  GstAggregatorClass *const agg_class = GST_AGGREGATOR_CLASS (klass);
  GstVideoAggregatorClass *const vagg_class =
      GST_VIDEO_AGGREGATOR_CLASS (klass);
  GstVaapiPluginBaseClass *plugin_class = GST_VAAPI_PLUGIN_BASE_CLASS (klass);

  GST_DEBUG_CATEGORY_INIT (gst_debug_vaapi_overlay,
      GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);

  gst_vaapi_plugin_base_class_init (plugin_class);
  plugin_class->get_vaapi_pad_private =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_get_vaapi_pad_private);

  object_class->finalize = GST_DEBUG_FUNCPTR (gst_vaapi_overlay_finalize);

  agg_class->sink_query = GST_DEBUG_FUNCPTR (gst_vaapi_overlay_sink_query);
  agg_class->src_query = GST_DEBUG_FUNCPTR (gst_vaapi_overlay_src_query);
  agg_class->start = GST_DEBUG_FUNCPTR (gst_vaapi_overlay_start);
  agg_class->propose_allocation =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_propose_allocation);
  agg_class->fixate_src_caps =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_fixate_src_caps);
  agg_class->negotiated_src_caps =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_negotiated_src_caps);
  agg_class->decide_allocation =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_decide_allocation);
  agg_class->stop = GST_DEBUG_FUNCPTR (gst_vaapi_overlay_stop);

  vagg_class->aggregate_frames =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_aggregate_frames);
  vagg_class->create_output_buffer =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_create_output_buffer);

  element_class->request_new_pad =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_request_new_pad);
  element_class->release_pad =
      GST_DEBUG_FUNCPTR (gst_vaapi_overlay_release_pad);
  element_class->set_context = GST_DEBUG_FUNCPTR (gst_vaapi_base_set_context);

  gst_element_class_add_static_pad_template_with_gtype (element_class,
      &gst_vaapi_overlay_sink_factory, GST_TYPE_VAAPI_OVERLAY_SINK_PAD);

  gst_element_class_add_static_pad_template_with_gtype (element_class,
      &gst_vaapi_overlay_src_factory, GST_TYPE_AGGREGATOR_PAD);

  gst_element_class_set_static_metadata (element_class,
      "VA-API overlay",
      "Filter/Editor/Video/Compositor/Hardware",
      GST_PLUGIN_DESC, "U. Artie Eoff <ullysses.a.eoff@intel.com>");
}

static void
gst_vaapi_overlay_init (GstVaapiOverlay * overlay)
{
  gst_vaapi_plugin_base_init (GST_VAAPI_PLUGIN_BASE (overlay), GST_CAT_DEFAULT);
}

/* GstChildProxy implementation */
static GObject *
gst_vaapi_overlay_child_proxy_get_child_by_index (GstChildProxy * child_proxy,
    guint index)
{
  GstVaapiOverlay *overlay = GST_VAAPI_OVERLAY (child_proxy);
  GObject *obj = NULL;

  GST_OBJECT_LOCK (overlay);
  obj = g_list_nth_data (GST_ELEMENT_CAST (overlay)->sinkpads, index);
  if (obj)
    gst_object_ref (obj);
  GST_OBJECT_UNLOCK (overlay);

  return obj;
}

static guint
gst_vaapi_overlay_child_proxy_get_children_count (GstChildProxy * child_proxy)
{
  guint count = 0;
  GstVaapiOverlay *overlay = GST_VAAPI_OVERLAY (child_proxy);

  GST_OBJECT_LOCK (overlay);
  count = GST_ELEMENT_CAST (overlay)->numsinkpads;
  GST_OBJECT_UNLOCK (overlay);

  return count;
}

static void
gst_vaapi_overlay_child_proxy_init (gpointer g_iface, gpointer iface_data)
{
  GstChildProxyInterface *iface = g_iface;

  iface->get_child_by_index = gst_vaapi_overlay_child_proxy_get_child_by_index;
  iface->get_children_count = gst_vaapi_overlay_child_proxy_get_children_count;
}

gboolean
gst_vaapioverlay_register (GstPlugin * plugin, GstVaapiDisplay * display)
{
  GstVaapiBlend *blend = NULL;

  blend = gst_vaapi_blend_new (display);
  if (!blend)
    return FALSE;
  gst_vaapi_blend_replace (&blend, NULL);

  return gst_element_register (plugin, "vaapioverlay",
      GST_RANK_PRIMARY, GST_TYPE_VAAPI_OVERLAY);
}