2 * test-subpicture.c - Test GstVaapiSubpicture
4 * Copyright (C) <2011> Intel Corporation
5 * Copyright (C) <2011> Collabora Ltd.
6 * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
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.
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.
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
24 #include <gst/vaapi/gstvaapidisplay_x11.h>
25 #include <gst/vaapi/gstvaapiwindow_x11.h>
26 #include <gst/vaapi/gstvaapidecoder.h>
27 #include <gst/vaapi/gstvaapidecoder_ffmpeg.h>
28 #include <gst/vaapi/gstvaapisurface.h>
29 #include "test-h264.h"
30 #include "test-subpicture-data.h"
32 typedef void (*GetVideoInfoFunc)(VideoDecodeInfo *info);
34 typedef struct _CodecDefs CodecDefs;
36 const gchar *codec_str;
37 GetVideoInfoFunc get_video_info;
40 static const CodecDefs g_codec_defs[] = {
41 #define INIT_FUNCS(CODEC) { #CODEC, CODEC##_get_video_info }
47 static const CodecDefs *
48 get_codec_defs(const gchar *codec_str)
51 for (c = g_codec_defs; c->codec_str; c++)
52 if (strcmp(codec_str, c->codec_str) == 0)
57 static inline void pause(void)
59 g_print("Press any key to continue...\n");
63 static gchar *g_codec_str;
65 static GOptionEntry g_options[] = {
68 G_OPTION_ARG_STRING, &g_codec_str,
69 "codec to test", NULL },
74 main(int argc, char *argv[])
76 GOptionContext *options;
77 GstVaapiDisplay *display;
78 GstVaapiWindow *window;
79 GstVaapiDecoder *decoder;
80 GstCaps *decoder_caps;
81 GstStructure *structure;
82 GstVaapiDecoderStatus status;
83 const CodecDefs *codec;
85 GstVaapiSurfaceProxy *proxy;
87 VideoSubpictureInfo subinfo;
88 GstVaapiImage *subtitle_image;
89 GstVaapiSubpicture *subpicture;
91 GstVaapiRectangle sub_rect;
93 static const guint win_width = 640;
94 static const guint win_height = 480;
96 gst_init(&argc, &argv);
98 options = g_option_context_new(" - test-decode options");
99 g_option_context_add_main_entries(options, g_options, NULL);
100 g_option_context_parse(options, &argc, &argv, NULL);
101 g_option_context_free(options);
104 g_codec_str = g_strdup("h264");
106 g_print("Test %s decode\n", g_codec_str);
107 codec = get_codec_defs(g_codec_str);
109 g_error("no %s codec data found", g_codec_str);
111 display = gst_vaapi_display_x11_new(NULL);
113 g_error("could not create VA display");
115 window = gst_vaapi_window_x11_new(display, win_width, win_height);
117 g_error("could not create window");
119 codec->get_video_info(&info);
120 decoder_caps = gst_vaapi_profile_get_caps(info.profile);
122 g_error("could not create decoder caps");
124 structure = gst_caps_get_structure(decoder_caps, 0);
125 if (info.width > 0 && info.height > 0)
128 "width", G_TYPE_INT, info.width,
129 "height", G_TYPE_INT, info.height,
133 decoder = gst_vaapi_decoder_ffmpeg_new(display, decoder_caps);
135 g_error("could not create FFmpeg decoder");
136 gst_caps_unref(decoder_caps);
138 buffer = gst_buffer_new();
140 g_error("could not create encoded data buffer");
141 gst_buffer_set_data(buffer, (guchar *)info.data, info.data_size);
143 if (!gst_vaapi_decoder_put_buffer(decoder, buffer))
144 g_error("could not send video data to the decoder");
145 gst_buffer_unref(buffer);
147 if (!gst_vaapi_decoder_put_buffer(decoder, NULL))
148 g_error("could not send EOS to the decoder");
150 proxy = gst_vaapi_decoder_get_surface(decoder, &status);
152 g_error("could not get decoded surface (decoder status %d)", status);
154 subpicture_get_info (&subinfo);
156 /* Adding subpicture */
157 argbcaps = gst_caps_new_simple ("video/x-raw-rgb",
158 "endianness", G_TYPE_INT, 1,
159 "bpp", G_TYPE_INT, 32,
160 "width", G_TYPE_INT, subinfo.width,
161 "height", G_TYPE_INT, subinfo.height,
164 buffer = gst_buffer_new ();
165 gst_buffer_set_data(buffer, (guchar *)subinfo.data, subinfo.data_size);
166 gst_buffer_set_caps (buffer, argbcaps);
168 subtitle_image = gst_vaapi_image_new (display,
169 GST_VAAPI_IMAGE_RGBA, subinfo.width, subinfo.height);
171 gst_vaapi_image_update_from_buffer (subtitle_image, buffer);
173 subpicture = gst_vaapi_subpicture_new (subtitle_image);
175 /* We position it as a subtitle, centered at the bottom. */
176 sub_rect.x = (win_width - subinfo.width) / 2;
177 sub_rect.y = win_height - subinfo.height - 10;
178 sub_rect.height = subinfo.height;
179 sub_rect.width = subinfo.height;
181 if (!gst_vaapi_surface_associate_subpicture (
182 GST_VAAPI_SURFACE_PROXY_SURFACE(proxy),
186 g_error("could not associate subpicture");
188 gst_vaapi_window_show(window);
190 if (!gst_vaapi_window_put_surface(window,
191 GST_VAAPI_SURFACE_PROXY_SURFACE(proxy),
194 GST_VAAPI_PICTURE_STRUCTURE_FRAME))
195 g_error("could not render surface");
199 gst_buffer_unref(buffer);
200 g_object_unref(proxy);
201 g_object_unref(decoder);
202 g_object_unref(window);
203 g_object_unref(display);