vaapisink: Expose the overlay capability for compatibility with dvbsuboverlay.
[platform/upstream/gstreamer.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     { "global-alpha", 'g',
45       0,
46       G_OPTION_ARG_DOUBLE, &g_global_alpha,
47       "global-alpha value", NULL },
48     { NULL, }
49 };
50
51 static void
52 upload_subpicture(GstBuffer *buffer, const VideoSubpictureInfo *subinfo)
53 {
54     const guint32 * const src = subinfo->data;
55     guint i, len = subinfo->data_size / 4;
56     GstMapInfo map_info;
57     guint32 *dst;
58
59     if (!gst_buffer_map(buffer, &map_info, GST_MAP_WRITE))
60         return;
61     dst = (guint32 *)map_info.data;
62
63     /* Convert from RGBA source to ARGB */
64     for (i = 0; i < len; i++) {
65         const guint32 rgba = src[i];
66         dst[i] = (rgba >> 8) | (rgba << 24);
67     }
68
69     gst_buffer_unmap(buffer, &map_info);
70 }
71
72 int
73 main(int argc, char *argv[])
74 {
75     GstVaapiDisplay      *display;
76     GstVaapiWindow       *window;
77     GstVaapiDecoder      *decoder;
78     GstVaapiSurfaceProxy *proxy;
79     GstVaapiSurface      *surface;
80     GstBuffer            *buffer;
81     VideoSubpictureInfo   subinfo;
82     GstVaapiRectangle     subrect;
83     GstVideoOverlayRectangle *overlay;
84     GstVideoOverlayComposition *compo;
85     guint flags = 0;
86
87     static const guint win_width  = 640;
88     static const guint win_height = 480;
89
90     if (!video_output_init(&argc, argv, g_options))
91         g_error("failed to initialize video output subsystem");
92
93     if (g_global_alpha != 1.0)
94         flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
95
96     g_print("Test subpicture\n");
97
98     display = video_output_create_display(NULL);
99     if (!display)
100         g_error("could not create VA display");
101
102     window = video_output_create_window(display, win_width, win_height);
103     if (!window)
104         g_error("could not create window");
105
106     decoder = decoder_new(display, g_codec_str);
107     if (!decoder)
108         g_error("could not create decoder");
109
110     if (!decoder_put_buffers(decoder))
111         g_error("could not fill decoder with sample data");
112
113     proxy = decoder_get_surface(decoder);
114     if (!proxy)
115         g_error("could not get decoded surface");
116
117     surface = gst_vaapi_surface_proxy_get_surface(proxy);
118
119     subpicture_get_info(&subinfo);
120     buffer = gst_buffer_new_and_alloc(subinfo.data_size);
121     upload_subpicture(buffer, &subinfo);
122
123     /* We position the subpicture at the bottom center */
124     subrect.x = (gst_vaapi_surface_get_width(surface) - subinfo.width) / 2;
125     subrect.y = gst_vaapi_surface_get_height(surface) - subinfo.height - 10;
126     subrect.height = subinfo.height;
127     subrect.width = subinfo.width;
128
129     {
130         GstVideoMeta * const vmeta =
131             gst_buffer_add_video_meta(buffer, GST_VIDEO_FRAME_FLAG_NONE,
132                 GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB,
133                 subinfo.width, subinfo.height);
134         if (!vmeta)
135             g_error("could not create video meta");
136
137         overlay = gst_video_overlay_rectangle_new_raw(buffer,
138             subrect.x, subrect.y, subrect.width, subrect.height, flags);
139     }
140     if (!overlay)
141         g_error("could not create video overlay");
142     gst_buffer_unref(buffer);
143
144     if (flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
145         gst_video_overlay_rectangle_set_global_alpha(overlay, g_global_alpha);
146
147     compo = gst_video_overlay_composition_new(overlay);
148     if (!compo)
149         g_error("could not create video overlay composition");
150     gst_video_overlay_rectangle_unref(overlay);
151
152     if (!gst_vaapi_surface_set_subpictures_from_composition(surface, compo,
153             FALSE))
154         g_error("could not create subpictures from video overlay compoition");
155
156     gst_vaapi_window_show(window);
157
158     if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL,
159             GST_VAAPI_PICTURE_STRUCTURE_FRAME))
160         g_error("could not render surface");
161
162     pause();
163
164     gst_video_overlay_composition_unref(compo);
165     gst_vaapi_surface_proxy_unref(proxy);
166     gst_vaapi_decoder_unref(decoder);
167     gst_vaapi_window_unref(window);
168     gst_vaapi_display_unref(display);
169     g_free(g_codec_str);
170     video_output_exit();
171     return 0;
172 }