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