packaging: add packaging folder and update submodule pkgs
[platform/upstream/gstreamer-vaapi.git] / tests / test-subpicture.c
1 /*
2  *  test-subpicture.c - Test GstVaapiSubpicture
3  *
4  *  Copyright (C) <2011-2013> Intel Corporation
5  *  Copyright (C) <2011> Collabora Ltd.
6  *  Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
21  */
22
23 #include "gst/vaapi/sysdeps.h"
24 #include <string.h>
25 #include <gst/vaapi/gstvaapisurface.h>
26 #include "decoder.h"
27 #include "output.h"
28 #include "test-subpicture-data.h"
29
30 static inline void pause(void)
31 {
32     g_print("Press any key to continue...\n");
33     getchar();
34 }
35
36 static gchar *g_codec_str;
37 static gdouble g_global_alpha = 1.0;
38
39 static GOptionEntry g_options[] = {
40     { "codec", 'c',
41       0,
42       G_OPTION_ARG_STRING, &g_codec_str,
43       "codec to test", NULL },
44 #ifdef HAVE_GST_VIDEO_OVERLAY_HWCAPS
45     { "global-alpha", 'g',
46       0,
47       G_OPTION_ARG_DOUBLE, &g_global_alpha,
48       "global-alpha value", NULL },
49 #endif
50     { NULL, }
51 };
52
53 static void
54 upload_subpicture(GstBuffer *buffer, const VideoSubpictureInfo *subinfo)
55 {
56     const guint32 * const src = subinfo->data;
57     guint i, len = subinfo->data_size / 4;
58     GstMapInfo map_info;
59     guint32 *dst;
60
61     if (!gst_buffer_map(buffer, &map_info, GST_MAP_WRITE))
62         return;
63     dst = (guint32 *)map_info.data;
64
65     /* Convert from RGBA source to ARGB */
66     for (i = 0; i < len; i++) {
67         const guint32 rgba = src[i];
68         dst[i] = (rgba >> 8) | (rgba << 24);
69     }
70
71     gst_buffer_unmap(buffer, &map_info);
72 }
73
74 int
75 main(int argc, char *argv[])
76 {
77     GstVaapiDisplay      *display;
78     GstVaapiWindow       *window;
79     GstVaapiDecoder      *decoder;
80     GstVaapiSurfaceProxy *proxy;
81     GstVaapiSurface      *surface;
82     GstBuffer            *buffer;
83     VideoSubpictureInfo   subinfo;
84     GstVaapiRectangle     subrect;
85     GstVideoOverlayRectangle *overlay;
86     GstVideoOverlayComposition *compo;
87     guint flags = 0;
88
89     static const guint win_width  = 640;
90     static const guint win_height = 480;
91
92     if (!video_output_init(&argc, argv, g_options))
93         g_error("failed to initialize video output subsystem");
94
95 #ifdef HAVE_GST_VIDEO_OVERLAY_HWCAPS
96     if (g_global_alpha != 1.0)
97         flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
98 #endif
99
100     g_print("Test subpicture\n");
101
102     display = video_output_create_display(NULL);
103     if (!display)
104         g_error("could not create VA display");
105
106     window = video_output_create_window(display, win_width, win_height);
107     if (!window)
108         g_error("could not create window");
109
110     decoder = decoder_new(display, g_codec_str);
111     if (!decoder)
112         g_error("could not create decoder");
113
114     if (!decoder_put_buffers(decoder))
115         g_error("could not fill decoder with sample data");
116
117     proxy = decoder_get_surface(decoder);
118     if (!proxy)
119         g_error("could not get decoded surface");
120
121     surface = gst_vaapi_surface_proxy_get_surface(proxy);
122
123     subpicture_get_info(&subinfo);
124     buffer = gst_buffer_new_and_alloc(subinfo.data_size);
125     upload_subpicture(buffer, &subinfo);
126
127     /* We position the subpicture at the bottom center */
128     subrect.x = (gst_vaapi_surface_get_width(surface) - subinfo.width) / 2;
129     subrect.y = gst_vaapi_surface_get_height(surface) - subinfo.height - 10;
130     subrect.height = subinfo.height;
131     subrect.width = subinfo.width;
132
133 #if GST_CHECK_VERSION(1,0,0)
134     {
135         GstVideoMeta * const vmeta =
136             gst_buffer_add_video_meta(buffer, GST_VIDEO_FRAME_FLAG_NONE,
137                 GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB,
138                 subinfo.width, subinfo.height);
139         if (!vmeta)
140             g_error("could not create video meta");
141
142         overlay = gst_video_overlay_rectangle_new_raw(buffer,
143             subrect.x, subrect.y, subrect.width, subrect.height, flags);
144     }
145 #else
146     overlay = gst_video_overlay_rectangle_new_argb(buffer,
147         subinfo.width, subinfo.height, subinfo.width * 4,
148         subrect.x, subrect.y, subrect.width, subrect.height, flags);
149 #endif
150     if (!overlay)
151         g_error("could not create video overlay");
152     gst_buffer_unref(buffer);
153
154 #ifdef HAVE_GST_VIDEO_OVERLAY_HWCAPS
155     if (flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
156         gst_video_overlay_rectangle_set_global_alpha(overlay, g_global_alpha);
157 #endif
158
159     compo = gst_video_overlay_composition_new(overlay);
160     if (!compo)
161         g_error("could not create video overlay composition");
162     gst_video_overlay_rectangle_unref(overlay);
163
164     if (!gst_vaapi_surface_set_subpictures_from_composition(surface, compo,
165             FALSE))
166         g_error("could not create subpictures from video overlay compoition");
167
168     gst_vaapi_window_show(window);
169
170     if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL,
171             GST_VAAPI_PICTURE_STRUCTURE_FRAME))
172         g_error("could not render surface");
173
174     pause();
175
176     gst_video_overlay_composition_unref(compo);
177     gst_vaapi_surface_proxy_unref(proxy);
178     gst_vaapi_decoder_unref(decoder);
179     gst_vaapi_window_unref(window);
180     gst_vaapi_display_unref(display);
181     g_free(g_codec_str);
182     video_output_exit();
183     return 0;
184 }