Add decoder demos. Use -c (mpeg2|h264|vc1) to select the codec.
[profile/ivi/gstreamer-vaapi.git] / tests / test-textures.c
1 /*
2  *  test-textures.c - Test GstVaapiTexture
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19  */
20
21 #include <gst/vaapi/gstvaapidisplay_glx.h>
22 #include <gst/vaapi/gstvaapiwindow_glx.h>
23 #include <gst/vaapi/gstvaapisurface.h>
24 #include <gst/vaapi/gstvaapiimage.h>
25 #include <gst/vaapi/gstvaapitexture.h>
26 #include "image.h"
27
28 static inline void pause(void)
29 {
30     g_print("Press any key to continue...\n");
31     getchar();
32 }
33
34 static inline guint gl_get_current_texture_2d(void)
35 {
36     GLint texture;
37     glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture);
38     return (guint)texture;
39 }
40
41 int
42 main(int argc, char *argv[])
43 {
44     GstVaapiDisplay    *display;
45     GstVaapiWindow     *window;
46     GstVaapiWindowGLX  *glx_window;
47     GstVaapiSurface    *surface;
48     GstVaapiImage      *image;
49     GstVaapiTexture    *textures[2];
50     GstVaapiTexture    *texture;
51     GLuint              texture_id;
52     GstVaapiRectangle   src_rect;
53     GstVaapiRectangle   dst_rect;
54     guint               flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
55
56     static const GstVaapiChromaType chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
57     static const guint              width       = 320;
58     static const guint              height      = 240;
59     static const guint              win_width   = 640;
60     static const guint              win_height  = 480;
61
62     gst_init(&argc, &argv);
63
64     display = gst_vaapi_display_glx_new(NULL);
65     if (!display)
66         g_error("could not create VA display");
67
68     surface = gst_vaapi_surface_new(display, chroma_type, width, height);
69     if (!surface)
70         g_error("could not create VA surface");
71
72     image = image_generate(display, GST_VAAPI_IMAGE_NV12, width, height);
73     if (!image)
74         g_error("could not create VA image");
75     if (!image_upload(image, surface))
76         g_error("could not upload VA image to surface");
77
78     window = gst_vaapi_window_glx_new(display, win_width, win_height);
79     if (!window)
80         g_error("could not create window");
81     glx_window = GST_VAAPI_WINDOW_GLX(window);
82
83     gst_vaapi_window_show(window);
84
85     if (!gst_vaapi_window_glx_make_current(glx_window))
86         g_error("coult not bind GL context");
87
88     g_print("#\n");
89     g_print("# Create texture with gst_vaapi_texture_new()\n");
90     g_print("#\n");
91     {
92         texture = gst_vaapi_texture_new(
93             display,
94             GL_TEXTURE_2D,
95             GL_RGBA,
96             width,
97             height
98         );
99         if (!texture)
100             g_error("could not create VA texture");
101
102         textures[0] = texture;
103         texture_id  = gst_vaapi_texture_get_id(texture);
104
105         if (!gst_vaapi_texture_put_surface(texture, surface, flags))
106             g_error("could not transfer VA surface to texture");
107
108         if (!gst_vaapi_window_glx_put_texture(glx_window, texture, NULL, NULL))
109             g_error("could not render texture into the window");
110     }
111
112     g_print("#\n");
113     g_print("# Create texture with gst_vaapi_texture_new_with_texture()\n");
114     g_print("#\n");
115     {
116         const GLenum target = GL_TEXTURE_2D;
117         const GLenum format = GL_BGRA;
118
119         glEnable(target);
120         glGenTextures(1, &texture_id);
121         glBindTexture(target, texture_id);
122         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
123         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
124         glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
125         glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
126         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
127         glTexImage2D(
128             target,
129             0,
130             GL_RGBA8,
131             width, height,
132             0,
133             format,
134             GL_UNSIGNED_BYTE,
135             NULL
136         );
137         glDisable(target);
138
139         texture = gst_vaapi_texture_new_with_texture(
140             display,
141             texture_id,
142             target,
143             format
144         );
145         if (!texture)
146             g_error("could not create VA texture");
147
148         if (texture_id != gst_vaapi_texture_get_id(texture))
149             g_error("invalid texture id");
150
151         if (gl_get_current_texture_2d() != texture_id)
152             g_error("gst_vaapi_texture_new_with_texture() altered texture bindings");
153
154         textures[1] = texture;
155
156         if (!gst_vaapi_texture_put_surface(texture, surface, flags))
157             g_error("could not transfer VA surface to texture");
158
159         if (gl_get_current_texture_2d() != texture_id)
160             g_error("gst_vaapi_texture_put_surface() altered texture bindings");
161
162         src_rect.x      = 0;
163         src_rect.y      = 0;
164         src_rect.width  = width;
165         src_rect.height = height;
166
167         dst_rect.x      = win_width/2;
168         dst_rect.y      = win_height/2;
169         dst_rect.width  = win_width/2;
170         dst_rect.height = win_height/2;
171
172         if (!gst_vaapi_window_glx_put_texture(glx_window, texture, 
173                                               &src_rect, &dst_rect))
174             g_error("could not render texture into the window");
175
176         if (gl_get_current_texture_2d() != texture_id)
177             g_error("gst_vaapi_window_glx_put_texture() altered texture bindings");
178     }
179
180     gst_vaapi_window_glx_swap_buffers(glx_window);
181     pause();
182
183     g_object_unref(textures[0]);
184     g_object_unref(textures[1]);
185     glDeleteTextures(1, &texture_id);
186
187     g_object_unref(window);
188     g_object_unref(display);
189     gst_deinit();
190     return 0;
191 }