summaryrefslogtreecommitdiff
path: root/gst/vaapi/gstvaapipostproc.c
blob: 8565a5e4d186fcec04401575f194d35aab43de29 (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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
 *  gstvaapipostproc.c - VA-API video postprocessing
 *
 *  Copyright (C) 2012 Intel Corporation
 *
 *  This library 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 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

/**
 * SECTION:gstvaapipostproc
 * @short_description: A video postprocessing filter
 *
 * vaapipostproc consists in various postprocessing algorithms to be
 * applied to VA surfaces. So far, only basic bob deinterlacing is
 * implemented.
 */

#include "config.h"
#include <gst/video/video.h>
#include <gst/video/videocontext.h>
#include <gst/vaapi/gstvaapivideosink.h>
#include <gst/vaapi/gstvaapivideobuffer.h>

#include "gstvaapipostproc.h"
#include "gstvaapipluginutil.h"
#include "gstvaapipluginbuffer.h"

#define GST_PLUGIN_NAME "vaapipostproc"
#define GST_PLUGIN_DESC "A video postprocessing filter"

GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapipostproc);
#define GST_CAT_DEFAULT gst_debug_vaapipostproc

/* ElementFactory information */
static const GstElementDetails gst_vaapipostproc_details =
    GST_ELEMENT_DETAILS(
        "VA-API video postprocessing",
        "Filter/Converter/Video",
        GST_PLUGIN_DESC,
        "Gwenole Beauchesne <gwenole.beauchesne@intel.com>");

/* Default templates */
static const char gst_vaapipostproc_sink_caps_str[] =
    GST_VAAPI_SURFACE_CAPS ", "
    "interlaced = (boolean) { true, false }";

static const char gst_vaapipostproc_src_caps_str[] =
    GST_VAAPI_SURFACE_CAPS ", "
    "interlaced = (boolean) false";

static GstStaticPadTemplate gst_vaapipostproc_sink_factory =
    GST_STATIC_PAD_TEMPLATE(
        "sink",
        GST_PAD_SINK,
        GST_PAD_ALWAYS,
        GST_STATIC_CAPS(gst_vaapipostproc_sink_caps_str));

static GstStaticPadTemplate gst_vaapipostproc_src_factory =
    GST_STATIC_PAD_TEMPLATE(
        "src",
        GST_PAD_SRC,
        GST_PAD_ALWAYS,
        GST_STATIC_CAPS(gst_vaapipostproc_src_caps_str));

static void
gst_vaapipostproc_implements_iface_init(GstImplementsInterfaceClass *iface);

static void
gst_video_context_interface_init(GstVideoContextInterface *iface);

#define GstVideoContextClass GstVideoContextInterface
G_DEFINE_TYPE_WITH_CODE(
    GstVaapiPostproc,
    gst_vaapipostproc,
    GST_TYPE_ELEMENT,
    G_IMPLEMENT_INTERFACE(GST_TYPE_IMPLEMENTS_INTERFACE,
                          gst_vaapipostproc_implements_iface_init);
    G_IMPLEMENT_INTERFACE(GST_TYPE_VIDEO_CONTEXT,
                          gst_video_context_interface_init));

enum {
    PROP_0,

    PROP_DEINTERLACE_MODE,
    PROP_DEINTERLACE_METHOD,
};

#define DEFAULT_DEINTERLACE_MODE        GST_VAAPI_DEINTERLACE_MODE_AUTO
#define DEFAULT_DEINTERLACE_METHOD      GST_VAAPI_DEINTERLACE_METHOD_BOB

#define GST_TYPE_VAAPI_DEINTERLACE_MODES \
    gst_vaapi_deinterlace_modes_get_type()

static GType
gst_vaapi_deinterlace_modes_get_type(void)
{
    static GType deinterlace_modes_type = 0;

    static const GEnumValue modes_types[] = {
        { GST_VAAPI_DEINTERLACE_MODE_AUTO,
          "Auto detection", "auto" },
        { GST_VAAPI_DEINTERLACE_MODE_INTERLACED,
          "Force deinterlacing", "interlaced" },
        { GST_VAAPI_DEINTERLACE_MODE_DISABLED,
          "Never deinterlace", "disabled" },
        { 0, NULL, NULL },
    };

    if (!deinterlace_modes_type) {
        deinterlace_modes_type =
            g_enum_register_static("GstVaapiDeinterlaceModes", modes_types);
    }
    return deinterlace_modes_type;
}

#define GST_TYPE_VAAPI_DEINTERLACE_METHODS \
    gst_vaapi_deinterlace_methods_get_type()

static GType
gst_vaapi_deinterlace_methods_get_type(void)
{
    static GType deinterlace_methods_type = 0;

    static const GEnumValue methods_types[] = {
        { GST_VAAPI_DEINTERLACE_METHOD_BOB,
          "Bob deinterlacing", "bob" },
#if 0
        /* VA/VPP */
        { GST_VAAPI_DEINTERLACE_METHOD_WEAVE,
          "Weave deinterlacing", "weave" },
        { GST_VAAPI_DEINTERLACE_METHOD_MOTION_ADAPTIVE,
          "Motion adaptive deinterlacing", "motion-adaptive" },
        { GST_VAAPI_DEINTERLACE_METHOD_MOTION_COMPENSATED,
          "Motion compensated deinterlacing", "motion-compensated" },
#endif
        { 0, NULL, NULL },
    };

    if (!deinterlace_methods_type) {
        deinterlace_methods_type =
            g_enum_register_static("GstVaapiDeinterlaceMethods", methods_types);
    }
    return deinterlace_methods_type;
}

static inline GstVaapiPostproc *
get_vaapipostproc_from_pad(GstPad *pad)
{
    return GST_VAAPIPOSTPROC(gst_pad_get_parent_element(pad));
}

/* GstImplementsInterface interface */

static gboolean
gst_vaapipostproc_implements_interface_supported(
    GstImplementsInterface *iface,
    GType                   type
)
{
    return (type == GST_TYPE_VIDEO_CONTEXT);
}

static void
gst_vaapipostproc_implements_iface_init(GstImplementsInterfaceClass *iface)
{
    iface->supported = gst_vaapipostproc_implements_interface_supported;
}

/* GstVideoContext interface */

static void
gst_vaapipostproc_set_video_context(
    GstVideoContext *context,
    const gchar     *type,
    const GValue    *value
)
{
    GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(context);

    gst_vaapi_set_display(type, value, &postproc->display);
}

static void
gst_video_context_interface_init(GstVideoContextInterface *iface)
{
    iface->set_context = gst_vaapipostproc_set_video_context;
}

static inline gboolean
gst_vaapipostproc_ensure_display(GstVaapiPostproc *postproc)
{
    return gst_vaapi_ensure_display(postproc, GST_VAAPI_DISPLAY_TYPE_ANY,
        &postproc->display);
}

static gboolean
gst_vaapipostproc_create(GstVaapiPostproc *postproc, GstCaps *caps)
{
    if (!gst_vaapipostproc_ensure_display(postproc))
        return FALSE;

    gst_caps_replace(&postproc->postproc_caps, caps);
    return TRUE;
}

static void
gst_vaapipostproc_destroy(GstVaapiPostproc *postproc)
{
    gst_caps_replace(&postproc->postproc_caps, NULL);

    g_clear_object(&postproc->display);
}

static gboolean
gst_vaapipostproc_reset(GstVaapiPostproc *postproc, GstCaps *caps)
{
    if (postproc->postproc_caps &&
        gst_caps_is_always_compatible(caps, postproc->postproc_caps))
        return TRUE;

    gst_vaapipostproc_destroy(postproc);
    return gst_vaapipostproc_create(postproc, caps);
}

static gboolean
gst_vaapipostproc_start(GstVaapiPostproc *postproc)
{
    if (!gst_vaapipostproc_ensure_display(postproc))
        return FALSE;
    return TRUE;
}

static gboolean
gst_vaapipostproc_stop(GstVaapiPostproc *postproc)
{
    if (postproc->display) {
        g_object_unref(postproc->display);
        postproc->display = NULL;
    }
    return TRUE;
}

static GstFlowReturn
gst_vaapipostproc_process(GstVaapiPostproc *postproc, GstBuffer *buf)
{
    GstVaapiVideoBuffer *vbuf = GST_VAAPI_VIDEO_BUFFER(buf);
    GstVaapiSurfaceProxy *proxy;
    GstClockTime timestamp;
    GstFlowReturn ret;
    GstBuffer *outbuf = NULL;
    guint outbuf_flags, flags;
    gboolean interlaced, tff;

    flags = gst_vaapi_video_buffer_get_render_flags(vbuf);

    /* Deinterlacing disabled, push frame */
    if (!postproc->deinterlace) {
        gst_vaapi_video_buffer_set_render_flags(vbuf, flags);
        ret = gst_pad_push(postproc->srcpad, buf);
        if (ret != GST_FLOW_OK)
            goto error_push_buffer;
        return GST_FLOW_OK;
    }

    timestamp  = GST_BUFFER_TIMESTAMP(buf);
    proxy      = gst_vaapi_video_buffer_get_surface_proxy(vbuf);
    interlaced = gst_vaapi_surface_proxy_get_interlaced(proxy);
    tff        = gst_vaapi_surface_proxy_get_tff(proxy);

    flags &= ~(GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD|
               GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD);

    /* First field */
    outbuf = gst_vaapi_video_buffer_new_with_surface_proxy(proxy);
    if (!outbuf)
        goto error_create_buffer;

    vbuf = GST_VAAPI_VIDEO_BUFFER(outbuf);
    outbuf_flags = flags;
    outbuf_flags |= interlaced ? (
        tff ?
        GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD :
        GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD) :
        GST_VAAPI_PICTURE_STRUCTURE_FRAME;
    gst_vaapi_video_buffer_set_render_flags(vbuf, outbuf_flags);

    GST_BUFFER_TIMESTAMP(outbuf) = timestamp;
    GST_BUFFER_DURATION(outbuf)  = postproc->field_duration;
    gst_buffer_set_caps(outbuf, postproc->srcpad_caps);
    ret = gst_pad_push(postproc->srcpad, outbuf);
    if (ret != GST_FLOW_OK)
        goto error_push_buffer;

    /* Second field */
    outbuf = gst_vaapi_video_buffer_new_with_surface_proxy(proxy);
    if (!outbuf)
        goto error_create_buffer;

    vbuf = GST_VAAPI_VIDEO_BUFFER(outbuf);
    outbuf_flags = flags;
    outbuf_flags |= interlaced ? (
        tff ?
        GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD :
        GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD) :
        GST_VAAPI_PICTURE_STRUCTURE_FRAME;
    gst_vaapi_video_buffer_set_render_flags(vbuf, outbuf_flags);

    GST_BUFFER_TIMESTAMP(outbuf) = timestamp + postproc->field_duration;
    GST_BUFFER_DURATION(outbuf)  = postproc->field_duration;
    gst_buffer_set_caps(outbuf, postproc->srcpad_caps);
    ret = gst_pad_push(postproc->srcpad, outbuf);
    if (ret != GST_FLOW_OK)
        goto error_push_buffer;

    gst_buffer_unref(buf);
    return GST_FLOW_OK;

    /* ERRORS */
error_create_buffer:
    {
        GST_ERROR("failed to create output buffer");
        gst_buffer_unref(buf);
        return GST_FLOW_UNEXPECTED;
    }
error_push_buffer:
    {
        if (ret != GST_FLOW_WRONG_STATE)
            GST_ERROR("failed to push output buffer to video sink");
        gst_buffer_unref(buf);
        return GST_FLOW_UNEXPECTED;
    }
}

static gboolean
gst_vaapipostproc_update_sink_caps(GstVaapiPostproc *postproc, GstCaps *caps)
{
    gint fps_n, fps_d;
    gboolean interlaced;

    if (!gst_video_parse_caps_framerate(caps, &fps_n, &fps_d))
        return FALSE;
    postproc->fps_n = fps_n;
    postproc->fps_d = fps_d;

    switch (postproc->deinterlace_mode) {
    case GST_VAAPI_DEINTERLACE_MODE_AUTO:
        if (!gst_video_format_parse_caps_interlaced(caps, &interlaced))
            return FALSE;
        postproc->deinterlace = interlaced;
        break;
    case GST_VAAPI_DEINTERLACE_MODE_INTERLACED:
        postproc->deinterlace = TRUE;
        break;
    case GST_VAAPI_DEINTERLACE_MODE_DISABLED:
        postproc->deinterlace = FALSE;
        break;
    }

    postproc->field_duration = gst_util_uint64_scale(
        GST_SECOND,
        postproc->fps_d,
        (1 + postproc->deinterlace) * postproc->fps_n
    );

    gst_caps_replace(&postproc->sinkpad_caps, caps);
    return TRUE;
}

static gboolean
gst_vaapipostproc_update_src_caps(GstVaapiPostproc *postproc, GstCaps *caps)
{
    GstCaps *src_caps;
    GstStructure *structure;
    const GValue *v_width, *v_height, *v_par;
    gint fps_n, fps_d;

    if (postproc->srcpad_caps)
        src_caps = gst_caps_make_writable(postproc->srcpad_caps);
    else
        src_caps = gst_caps_from_string(GST_VAAPI_SURFACE_CAPS_NAME);
    if (!src_caps)
        return FALSE;
    postproc->srcpad_caps = src_caps;

    structure    = gst_caps_get_structure(caps, 0);
    v_width      = gst_structure_get_value(structure, "width");
    v_height     = gst_structure_get_value(structure, "height");
    v_par        = gst_structure_get_value(structure, "pixel-aspect-ratio");

    structure = gst_caps_get_structure(src_caps, 0);
    if (v_width && v_height) {
        gst_structure_set_value(structure, "width", v_width);
        gst_structure_set_value(structure, "height", v_height);
    }
    if (v_par)
        gst_structure_set_value(structure, "pixel-aspect-ratio", v_par);

    gst_structure_set(structure, "type", G_TYPE_STRING, "vaapi", NULL);
    gst_structure_set(structure, "opengl", G_TYPE_BOOLEAN, USE_GLX, NULL);

    if (!postproc->deinterlace)
        gst_structure_remove_field(structure, "interlaced");
    else {
        /* Set double framerate in interlaced mode */
        if (!gst_util_fraction_multiply(postproc->fps_n, postproc->fps_d,
                                        2, 1,
                                        &fps_n, &fps_d))
            return FALSE;

        gst_structure_set(
            structure,
            "interlaced", G_TYPE_BOOLEAN, FALSE,
            "framerate", GST_TYPE_FRACTION, fps_n, fps_d,
            NULL
        );
    }
    return gst_pad_set_caps(postproc->srcpad, src_caps);
}

static gboolean
gst_vaapipostproc_ensure_allowed_caps(GstVaapiPostproc *postproc)
{
    if (postproc->allowed_caps)
        return TRUE;

    postproc->allowed_caps =
        gst_caps_from_string(gst_vaapipostproc_sink_caps_str);
    if (!postproc->allowed_caps)
        return FALSE;

    /* XXX: append VA/VPP filters */
    return TRUE;
}

static GstCaps *
gst_vaapipostproc_get_caps(GstPad *pad)
{
    GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
    GstCaps *out_caps;

    if (gst_vaapipostproc_ensure_allowed_caps(postproc))
        out_caps = gst_caps_ref(postproc->allowed_caps);
    else
        out_caps = gst_caps_new_empty();

    gst_object_unref(postproc);
    return out_caps;
}

static gboolean
gst_vaapipostproc_set_caps(GstPad *pad, GstCaps *caps)
{
    GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
    gboolean success = FALSE;

    g_return_val_if_fail(pad == postproc->sinkpad, FALSE);

    do {
        if (!gst_vaapipostproc_update_sink_caps(postproc, caps))
            break;
        if (!gst_vaapipostproc_update_src_caps(postproc, caps))
            break;
        if (!gst_vaapipostproc_reset(postproc, postproc->sinkpad_caps))
            break;
        success = TRUE;
    } while (0);
    gst_object_unref(postproc);
    return success;
}

static GstFlowReturn
gst_vaapipostproc_chain(GstPad *pad, GstBuffer *buf)
{
    GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
    GstFlowReturn ret;

    ret = gst_vaapipostproc_process(postproc, buf);
    gst_object_unref(postproc);
    return ret;
}

static gboolean
gst_vaapipostproc_sink_event(GstPad *pad, GstEvent *event)
{
    GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
    gboolean success;

    GST_DEBUG("handle sink event '%s'", GST_EVENT_TYPE_NAME(event));

    /* Propagate event downstream */
    success = gst_pad_push_event(postproc->srcpad, event);
    gst_object_unref(postproc);
    return success;
}

static gboolean
gst_vaapipostproc_src_event(GstPad *pad, GstEvent *event)
{
    GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
    gboolean success;

    GST_DEBUG("handle src event '%s'", GST_EVENT_TYPE_NAME(event));

    /* Propagate event upstream */
    success = gst_pad_push_event(postproc->sinkpad, event);
    gst_object_unref(postproc);
    return success;
}

static gboolean
gst_vaapipostproc_query(GstPad *pad, GstQuery *query)
{
    GstVaapiPostproc * const postproc = get_vaapipostproc_from_pad(pad);
    gboolean success;

    GST_DEBUG("sharing display %p", postproc->display);

    if (gst_vaapi_reply_to_query(query, postproc->display))
        success = TRUE;
    else
        success = gst_pad_query_default(pad, query);

    gst_object_unref(postproc);
    return success;
}

static void
gst_vaapipostproc_finalize(GObject *object)
{
    GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(object);

    gst_vaapipostproc_destroy(postproc);

    gst_caps_replace(&postproc->sinkpad_caps, NULL);
    gst_caps_replace(&postproc->srcpad_caps,  NULL);
    gst_caps_replace(&postproc->allowed_caps, NULL);

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

static void
gst_vaapipostproc_set_property(
    GObject      *object,
    guint         prop_id,
    const GValue *value,
    GParamSpec   *pspec
)
{
    GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(object);

    switch (prop_id) {
    case PROP_DEINTERLACE_MODE:
        postproc->deinterlace_mode = g_value_get_enum(value);
        break;
    case PROP_DEINTERLACE_METHOD:
        postproc->deinterlace_method = g_value_get_enum(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
gst_vaapipostproc_get_property(
    GObject    *object,
    guint       prop_id,
    GValue     *value,
    GParamSpec *pspec
)
{
    GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(object);

    switch (prop_id) {
    case PROP_DEINTERLACE_MODE:
        g_value_set_enum(value, postproc->deinterlace_mode);
        break;
    case PROP_DEINTERLACE_METHOD:
        g_value_set_enum(value, postproc->deinterlace_method);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static GstStateChangeReturn
gst_vaapipostproc_change_state(GstElement *element, GstStateChange transition)
{
    GstVaapiPostproc * const postproc = GST_VAAPIPOSTPROC(element);
    GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;

    switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
        if (!gst_vaapipostproc_start(postproc))
            return GST_STATE_CHANGE_FAILURE;
        break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
        break;
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
        break;
    default:
        break;
    }

    ret = GST_ELEMENT_CLASS(gst_vaapipostproc_parent_class)->change_state(element, transition);
    if (ret != GST_STATE_CHANGE_SUCCESS)
        return ret;

    switch (transition) {
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
        break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
        break;
    case GST_STATE_CHANGE_READY_TO_NULL:
        if (!gst_vaapipostproc_stop(postproc))
            return GST_STATE_CHANGE_FAILURE;
        break;
    default:
        break;
    }
    return GST_STATE_CHANGE_SUCCESS;
}

static void
gst_vaapipostproc_class_init(GstVaapiPostprocClass *klass)
{
    GObjectClass * const object_class = G_OBJECT_CLASS(klass);
    GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
    GstPadTemplate *pad_template;

    GST_DEBUG_CATEGORY_INIT(gst_debug_vaapipostproc,
                            GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);

    object_class->finalize      = gst_vaapipostproc_finalize;
    object_class->set_property  = gst_vaapipostproc_set_property;
    object_class->get_property  = gst_vaapipostproc_get_property;

    element_class->change_state = gst_vaapipostproc_change_state;

    gst_element_class_set_details_simple(
        element_class,
        gst_vaapipostproc_details.longname,
        gst_vaapipostproc_details.klass,
        gst_vaapipostproc_details.description,
        gst_vaapipostproc_details.author
    );

    /* sink pad */
    pad_template = gst_static_pad_template_get(&gst_vaapipostproc_sink_factory);
    gst_element_class_add_pad_template(element_class, pad_template);
    gst_object_unref(pad_template);

    /* src pad */
    pad_template = gst_static_pad_template_get(&gst_vaapipostproc_src_factory);
    gst_element_class_add_pad_template(element_class, pad_template);
    gst_object_unref(pad_template);

    /**
     * GstVaapiPostproc:deinterlace-mode:
     *
     * This selects whether the deinterlacing should always be applied or if
     * they should only be applied on content that has the "interlaced" flag
     * on the caps.
     */
    g_object_class_install_property
        (object_class,
         PROP_DEINTERLACE_MODE,
         g_param_spec_enum("deinterlace",
                           "Deinterlace",
                           "Deinterlace mode to use",
                           GST_TYPE_VAAPI_DEINTERLACE_MODES,
                           DEFAULT_DEINTERLACE_MODE,
                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

    /**
     * GstVaapiPostproc:deinterlace-method:
     *
     * This selects the deinterlacing method to apply.
     */
    g_object_class_install_property
        (object_class,
         PROP_DEINTERLACE_METHOD,
         g_param_spec_enum("deinterlace-method",
                           "Deinterlace method",
                           "Deinterlace method to use",
                           GST_TYPE_VAAPI_DEINTERLACE_METHODS,
                           DEFAULT_DEINTERLACE_METHOD,
                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

static void
gst_vaapipostproc_init(GstVaapiPostproc *postproc)
{
    GstVaapiPostprocClass *klass = GST_VAAPIPOSTPROC_GET_CLASS(postproc);
    GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);

    postproc->allowed_caps              = NULL;
    postproc->postproc_caps             = NULL;
    postproc->display                   = NULL;
    postproc->surface_width             = 0;
    postproc->surface_height            = 0;
    postproc->deinterlace               = FALSE;
    postproc->deinterlace_mode          = DEFAULT_DEINTERLACE_MODE;
    postproc->deinterlace_method        = DEFAULT_DEINTERLACE_METHOD;
    postproc->field_duration            = GST_CLOCK_TIME_NONE;
    postproc->fps_n                     = 0;
    postproc->fps_d                     = 0;

    /* Pad through which data comes in to the element */
    postproc->sinkpad = gst_pad_new_from_template(
        gst_element_class_get_pad_template(element_class, "sink"),
        "sink"
    );
    postproc->sinkpad_caps = NULL;

    gst_pad_set_getcaps_function(postproc->sinkpad, gst_vaapipostproc_get_caps);
    gst_pad_set_setcaps_function(postproc->sinkpad, gst_vaapipostproc_set_caps);
    gst_pad_set_chain_function(postproc->sinkpad, gst_vaapipostproc_chain);
    gst_pad_set_event_function(postproc->sinkpad, gst_vaapipostproc_sink_event);
    gst_pad_set_query_function(postproc->sinkpad, gst_vaapipostproc_query);
    gst_element_add_pad(GST_ELEMENT(postproc), postproc->sinkpad);

    /* Pad through which data goes out of the element */
    postproc->srcpad = gst_pad_new_from_template(
        gst_element_class_get_pad_template(element_class, "src"),
        "src"
    );
    postproc->srcpad_caps = NULL;

    gst_pad_set_event_function(postproc->srcpad, gst_vaapipostproc_src_event);
    gst_pad_set_query_function(postproc->srcpad, gst_vaapipostproc_query);
    gst_element_add_pad(GST_ELEMENT(postproc), postproc->srcpad);
}