32c9c1918f1c1a4f6b5057d05af8384ce2d9ca4c
[platform/upstream/gstreamer-vaapi.git] / tests / test-subpicture.c
1 /*
2  *  test-subpicture.c - Test GstVaapiSubpicture
3  *
4  *  Copyright (C) <2011> 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 "config.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     guint32 * const dst = (guint32 *)GST_BUFFER_DATA(buffer);
55     const guint32 * const src = subinfo->data;
56     guint i, len = subinfo->data_size / 4;
57
58     /* Convert from RGBA source to ARGB */
59     for (i = 0; i < len; i++) {
60         const guint32 rgba = src[i];
61         dst[i] = (rgba >> 8) | (rgba << 24);
62     }
63 }
64
65 int
66 main(int argc, char *argv[])
67 {
68     GstVaapiDisplay      *display;
69     GstVaapiWindow       *window;
70     GstVaapiDecoder      *decoder;
71     GstVaapiSurface      *surface;
72     GstBuffer            *buffer;
73     VideoSubpictureInfo   subinfo;
74     GstVaapiRectangle     subrect;
75     GstVideoOverlayRectangle *overlay;
76     GstVideoOverlayComposition *compo;
77     guint flags = 0;
78
79     static const guint win_width  = 640;
80     static const guint win_height = 480;
81
82     if (!video_output_init(&argc, argv, g_options))
83         g_error("failed to initialize video output subsystem");
84
85     if (g_global_alpha != 1.0)
86         flags |= GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA;
87
88     g_print("Test subpicture\n");
89
90     display = video_output_create_display(NULL);
91     if (!display)
92         g_error("could not create VA display");
93
94     window = video_output_create_window(display, win_width, win_height);
95     if (!window)
96         g_error("could not create window");
97
98     decoder = decoder_new(display, g_codec_str);
99     if (!decoder)
100         g_error("could not create decoder");
101
102     if (!decoder_put_buffers(decoder))
103         g_error("could not fill decoder with sample data");
104
105     surface = decoder_get_surface(decoder);
106     if (!surface)
107         g_error("could not get decoded surface");
108
109     subpicture_get_info(&subinfo);
110     buffer = gst_buffer_new_and_alloc(subinfo.data_size);
111     upload_subpicture(buffer, &subinfo);
112
113     /* We position the subpicture at the bottom center */
114     subrect.x = (gst_vaapi_surface_get_width(surface) - subinfo.width) / 2;
115     subrect.y = gst_vaapi_surface_get_height(surface) - subinfo.height - 10;
116     subrect.height = subinfo.height;
117     subrect.width = subinfo.width;
118
119     overlay = gst_video_overlay_rectangle_new_argb(buffer,
120         subinfo.width, subinfo.height, subinfo.width * 4,
121         subrect.x, subrect.y, subrect.width, subrect.height, flags);
122     if (!overlay)
123         g_error("could not create video overlay");
124     gst_buffer_unref(buffer);
125
126     if (flags & GST_VIDEO_OVERLAY_FORMAT_FLAG_GLOBAL_ALPHA)
127         gst_video_overlay_rectangle_set_global_alpha(overlay, g_global_alpha);
128
129     compo = gst_video_overlay_composition_new(overlay);
130     if (!compo)
131         g_error("could not create video overlay composition");
132     gst_video_overlay_rectangle_unref(overlay);
133
134     if (!gst_vaapi_surface_set_subpictures_from_composition(surface, compo,
135             FALSE))
136         g_error("could not create subpictures from video overlay compoition");
137
138     gst_vaapi_window_show(window);
139
140     if (!gst_vaapi_window_put_surface(window, surface, NULL, NULL,
141             GST_VAAPI_PICTURE_STRUCTURE_FRAME))
142         g_error("could not render surface");
143
144     pause();
145
146     gst_video_overlay_composition_unref(compo);
147     g_object_unref(decoder);
148     g_object_unref(window);
149     g_object_unref(display);
150     g_free(g_codec_str);
151     video_output_exit();
152     return 0;
153 }