vaapipostproc: don't crash with dynamic framerate (0/1).
[platform/upstream/gstreamer-vaapi.git] / tests / test-surfaces.c
1 /*
2  *  test-surfaces.c - Test GstVaapiSurface and GstVaapiSurfacePool
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6  *  Copyright (C) 2012-2013 Intel Corporation
7  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public License
11  *  as published by the Free Software Foundation; either version 2.1
12  *  of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free
21  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  *  Boston, MA 02110-1301 USA
23  */
24
25 #include <gst/vaapi/gstvaapisurface.h>
26 #include <gst/vaapi/gstvaapisurfacepool.h>
27 #include "output.h"
28
29 #define MAX_SURFACES 4
30
31 int
32 main(int argc, char *argv[])
33 {
34     GstVaapiDisplay    *display;
35     GstVaapiSurface    *surface;
36     GstVaapiID          surface_id;
37     GstVaapiSurface    *surfaces[MAX_SURFACES];
38     GstVaapiVideoPool  *pool;
39     GstVideoInfo        vi;
40     gint                i;
41
42     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
43     static const guint              width       = 320;
44     static const guint              height      = 240;
45
46     if (!video_output_init(&argc, argv, NULL))
47         g_error("failed to initialize video output subsystem");
48
49     display = video_output_create_display(NULL);
50     if (!display)
51         g_error("could not create Gst/VA display");
52
53     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
54     if (!surface)
55         g_error("could not create Gst/VA surface");
56
57     surface_id = gst_vaapi_surface_get_id(surface);
58     g_print("created surface %" GST_VAAPI_ID_FORMAT "\n",
59             GST_VAAPI_ID_ARGS(surface_id));
60
61     gst_vaapi_object_unref(surface);
62
63     gst_video_info_set_format(&vi, GST_VIDEO_FORMAT_ENCODED, width, height);
64
65     pool = gst_vaapi_surface_pool_new(display, &vi);
66     if (!pool)
67         g_error("could not create Gst/VA surface pool");
68
69     for (i = 0; i < MAX_SURFACES; i++) {
70         surface = gst_vaapi_video_pool_get_object(pool);
71         if (!surface)
72             g_error("could not allocate Gst/VA surface from pool");
73         g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool\n",
74                 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surface)));
75         surfaces[i] = surface;
76     }
77
78     /* Check the pool doesn't return the last free'd surface */
79     surface = gst_vaapi_object_ref(surfaces[1]);
80
81     for (i = 0; i < 2; i++)
82         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
83
84     for (i = 0; i < 2; i++) {
85         surfaces[i] = gst_vaapi_video_pool_get_object(pool);
86         if (!surfaces[i])
87             g_error("could not re-allocate Gst/VA surface%d from pool", i);
88         g_print("created surface %" GST_VAAPI_ID_FORMAT " from pool (realloc)\n",
89                 GST_VAAPI_ID_ARGS(gst_vaapi_surface_get_id(surfaces[i])));
90     }
91
92     if (surface == surfaces[0])
93         g_error("Gst/VA pool doesn't queue free surfaces");
94
95     for (i = MAX_SURFACES - 1; i >= 0; i--) {
96         if (!surfaces[i])
97             continue;
98         gst_vaapi_video_pool_put_object(pool, surfaces[i]);
99         surfaces[i] = NULL;
100     }
101
102     /* Unref in random order to check objects are correctly refcounted */
103     gst_vaapi_display_unref(display);
104     gst_vaapi_video_pool_unref(pool);
105     gst_vaapi_object_unref(surface);
106     video_output_exit();
107     return 0;
108 }