2010-03-05 17:11:52 +00:00
|
|
|
/*
|
|
|
|
* gstvaapisink.c - VA-API video sink
|
|
|
|
*
|
|
|
|
* gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2010-03-12 23:53:48 +00:00
|
|
|
#include <gst/gst.h>
|
2010-03-15 17:09:12 +00:00
|
|
|
#include <gst/vaapi/gstvaapivideobuffer.h>
|
2010-03-16 09:03:10 +00:00
|
|
|
#include <gst/vaapi/gstvaapivideosink.h>
|
2010-03-12 23:53:48 +00:00
|
|
|
#include <gst/vaapi/gstvaapidisplay_x11.h>
|
2010-03-15 17:09:12 +00:00
|
|
|
#include <gst/vaapi/gstvaapiwindow_x11.h>
|
2010-03-05 17:11:52 +00:00
|
|
|
#include "gstvaapisink.h"
|
|
|
|
|
2010-03-12 23:53:48 +00:00
|
|
|
#define GST_PLUGIN_NAME "vaapisink"
|
|
|
|
#define GST_PLUGIN_DESC "A VA-API based videosink"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapisink);
|
|
|
|
#define GST_CAT_DEFAULT gst_debug_vaapisink
|
|
|
|
|
2010-03-05 17:11:52 +00:00
|
|
|
/* ElementFactory information */
|
|
|
|
static const GstElementDetails gst_vaapisink_details =
|
|
|
|
GST_ELEMENT_DETAILS(
|
|
|
|
"Video sink",
|
|
|
|
"Sink/Video",
|
|
|
|
"A VA-API based videosink",
|
|
|
|
"Gwenole Beauchesne <gbeauchesne@splitted-desktop.com>");
|
|
|
|
|
|
|
|
/* Default template */
|
|
|
|
static GstStaticPadTemplate gst_vaapisink_sink_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE(
|
|
|
|
"sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS(
|
|
|
|
"video/x-vaapi-surface, "
|
|
|
|
"width = (int) [ 1, MAX ], "
|
|
|
|
"height = (int) [ 1, MAX ]; "));
|
|
|
|
|
2010-03-12 23:53:48 +00:00
|
|
|
static void gst_vaapisink_iface_init(GType type);
|
|
|
|
|
|
|
|
GST_BOILERPLATE_FULL(
|
|
|
|
GstVaapiSink,
|
|
|
|
gst_vaapisink,
|
|
|
|
GstVideoSink,
|
|
|
|
GST_TYPE_VIDEO_SINK,
|
|
|
|
gst_vaapisink_iface_init);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
|
|
|
|
PROP_DISPLAY,
|
|
|
|
PROP_DISPLAY_NAME,
|
2010-03-22 12:03:26 +00:00
|
|
|
PROP_FULLSCREEN
|
2010-03-12 23:53:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static GstVaapiDisplay *
|
2010-03-16 09:03:10 +00:00
|
|
|
gst_vaapi_video_sink_do_get_display(GstVaapiVideoSink *sink)
|
2010-03-12 23:53:48 +00:00
|
|
|
{
|
|
|
|
return gst_vaapisink_get_display(GST_VAAPISINK(sink));
|
|
|
|
}
|
|
|
|
|
2010-03-16 09:03:10 +00:00
|
|
|
static void gst_vaapi_video_sink_iface_init(GstVaapiVideoSinkInterface *iface)
|
2010-03-12 23:53:48 +00:00
|
|
|
{
|
2010-03-16 09:03:10 +00:00
|
|
|
iface->get_display = gst_vaapi_video_sink_do_get_display;
|
2010-03-12 23:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gst_vaapisink_iface_init(GType type)
|
|
|
|
{
|
|
|
|
const GType g_define_type_id = type;
|
|
|
|
|
2010-03-16 09:03:10 +00:00
|
|
|
G_IMPLEMENT_INTERFACE(GST_VAAPI_TYPE_VIDEO_SINK,
|
|
|
|
gst_vaapi_video_sink_iface_init);
|
2010-03-12 23:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapisink_destroy(GstVaapiSink *sink)
|
|
|
|
{
|
|
|
|
if (sink->display) {
|
|
|
|
g_object_unref(sink->display);
|
|
|
|
sink->display = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sink->display_name) {
|
|
|
|
g_free(sink->display_name);
|
|
|
|
sink->display_name = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-15 17:09:12 +00:00
|
|
|
static inline gboolean
|
|
|
|
gst_vaapisink_ensure_display(GstVaapiSink *sink)
|
|
|
|
{
|
|
|
|
if (!sink->display)
|
|
|
|
sink->display = gst_vaapi_display_x11_new(sink->display_name);
|
|
|
|
return sink->display != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vaapisink_start(GstBaseSink *base_sink)
|
|
|
|
{
|
|
|
|
GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
|
|
|
|
|
|
|
|
if (!gst_vaapisink_ensure_display(sink))
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vaapisink_stop(GstBaseSink *base_sink)
|
|
|
|
{
|
|
|
|
GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
|
|
|
|
|
|
|
|
if (sink->window) {
|
|
|
|
g_object_unref(sink->window);
|
|
|
|
sink->window = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sink->display) {
|
|
|
|
g_object_unref(sink->display);
|
|
|
|
sink->display = NULL;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-17 07:20:19 +00:00
|
|
|
static gboolean
|
|
|
|
gst_vaapisink_set_caps(GstBaseSink *base_sink, GstCaps *caps)
|
|
|
|
{
|
|
|
|
GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
|
|
|
|
GstStructure * const structure = gst_caps_get_structure(caps, 0);
|
2010-03-22 12:03:26 +00:00
|
|
|
GstVideoRectangle * const win_rect = &sink->window_rect;
|
2010-03-22 10:03:24 +00:00
|
|
|
guint num, den;
|
|
|
|
guint win_width, win_height;
|
|
|
|
guint display_width, display_height, display_par_n, display_par_d;
|
|
|
|
gint video_width, video_height, video_par_n = 1, video_par_d = 1;
|
|
|
|
gdouble win_ratio;
|
2010-03-17 07:20:19 +00:00
|
|
|
|
|
|
|
if (!structure)
|
|
|
|
return FALSE;
|
2010-03-22 10:03:24 +00:00
|
|
|
if (!gst_structure_get_int(structure, "width", &video_width))
|
2010-03-17 07:20:19 +00:00
|
|
|
return FALSE;
|
2010-03-22 10:03:24 +00:00
|
|
|
if (!gst_structure_get_int(structure, "height", &video_height))
|
2010-03-17 07:20:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2010-03-22 10:03:24 +00:00
|
|
|
gst_video_parse_caps_pixel_aspect_ratio(caps, &video_par_n, &video_par_d);
|
|
|
|
gst_vaapi_display_get_size(sink->display, &display_width, &display_height);
|
|
|
|
gst_vaapi_display_get_pixel_aspect_ratio(
|
|
|
|
sink->display,
|
|
|
|
&display_par_n, &display_par_d
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!gst_video_calculate_display_ratio(&num, &den,
|
|
|
|
video_width, video_height,
|
|
|
|
video_par_n, video_par_d,
|
|
|
|
display_par_n, display_par_d))
|
|
|
|
return FALSE;
|
|
|
|
GST_DEBUG("video size %dx%d, calculated display ratio %d/%d",
|
|
|
|
video_width, video_height, num, den);
|
|
|
|
|
|
|
|
if ((video_height % den) == 0) {
|
|
|
|
GST_DEBUG("keeping video height");
|
|
|
|
win_width = gst_util_uint64_scale_int(video_height, num, den);
|
|
|
|
win_height = video_height;
|
|
|
|
}
|
|
|
|
else if ((video_width % num) == 0) {
|
|
|
|
GST_DEBUG("keeping video width");
|
|
|
|
win_width = video_width;
|
|
|
|
win_height = gst_util_uint64_scale_int(video_width, den, num);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GST_DEBUG("approximating while keeping video height");
|
|
|
|
win_width = gst_util_uint64_scale_int (video_height, num, den);
|
|
|
|
win_height = video_height;
|
|
|
|
}
|
|
|
|
win_ratio = (gdouble)win_width / win_height;
|
|
|
|
GST_DEBUG("scaling to %ux%u", win_width, win_height);
|
|
|
|
|
2010-03-22 12:03:26 +00:00
|
|
|
if (sink->fullscreen ||
|
|
|
|
win_width > display_width || win_height > display_height) {
|
2010-03-22 10:03:24 +00:00
|
|
|
if (video_width > video_height) {
|
|
|
|
win_width = display_width;
|
|
|
|
win_height = display_width / win_ratio;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
win_width = display_height * win_ratio;
|
|
|
|
win_height = display_height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GST_DEBUG("window size %ux%u", win_width, win_height);
|
|
|
|
|
2010-03-22 12:03:26 +00:00
|
|
|
if (sink->fullscreen) {
|
|
|
|
win_rect->x = (display_width - win_width) / 2;
|
|
|
|
win_rect->y = (display_height - win_height) / 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
win_rect->x = 0;
|
|
|
|
win_rect->y = 0;
|
|
|
|
}
|
|
|
|
win_rect->w = win_width;
|
|
|
|
win_rect->h = win_height;
|
|
|
|
|
2010-03-17 07:20:19 +00:00
|
|
|
if (sink->window)
|
2010-03-22 10:03:24 +00:00
|
|
|
gst_vaapi_window_set_size(sink->window, win_width, win_height);
|
2010-03-17 07:20:19 +00:00
|
|
|
else {
|
2010-03-22 10:03:24 +00:00
|
|
|
sink->window = gst_vaapi_window_x11_new(
|
|
|
|
sink->display,
|
|
|
|
win_width, win_height
|
|
|
|
);
|
2010-03-17 07:20:19 +00:00
|
|
|
if (!sink->window)
|
|
|
|
return FALSE;
|
2010-03-22 12:03:26 +00:00
|
|
|
gst_vaapi_window_set_fullscreen(sink->window, sink->fullscreen);
|
2010-03-17 07:20:19 +00:00
|
|
|
gst_vaapi_window_show(sink->window);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-15 17:09:12 +00:00
|
|
|
static GstFlowReturn
|
2010-03-17 07:20:19 +00:00
|
|
|
gst_vaapisink_show_frame(GstBaseSink *base_sink, GstBuffer *buffer)
|
2010-03-15 17:09:12 +00:00
|
|
|
{
|
2010-03-17 07:20:19 +00:00
|
|
|
GstVaapiSink * const sink = GST_VAAPISINK(base_sink);
|
2010-03-15 17:09:12 +00:00
|
|
|
GstVaapiVideoBuffer * const vbuffer = GST_VAAPI_VIDEO_BUFFER(buffer);
|
|
|
|
GstVaapiSurface *surface;
|
2010-03-17 07:20:19 +00:00
|
|
|
guint flags;
|
|
|
|
|
|
|
|
if (!sink->window)
|
|
|
|
return GST_FLOW_UNEXPECTED;
|
2010-03-15 17:09:12 +00:00
|
|
|
|
|
|
|
surface = gst_vaapi_video_buffer_get_surface(vbuffer);
|
|
|
|
if (!surface)
|
|
|
|
return GST_FLOW_UNEXPECTED;
|
|
|
|
|
|
|
|
flags = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
|
2010-03-22 12:03:26 +00:00
|
|
|
if (!gst_vaapi_window_put_surface(sink->window, surface, NULL,
|
|
|
|
&sink->window_rect, flags))
|
2010-03-15 17:09:12 +00:00
|
|
|
return GST_FLOW_UNEXPECTED;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-12 23:53:48 +00:00
|
|
|
static void
|
|
|
|
gst_vaapisink_finalize(GObject *object)
|
|
|
|
{
|
|
|
|
gst_vaapisink_destroy(GST_VAAPISINK(object));
|
|
|
|
|
|
|
|
G_OBJECT_CLASS(parent_class)->finalize(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapisink_set_property(
|
|
|
|
GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec
|
|
|
|
)
|
|
|
|
{
|
|
|
|
GstVaapiSink * const sink = GST_VAAPISINK(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_DISPLAY_NAME:
|
|
|
|
g_free(sink->display_name);
|
|
|
|
sink->display_name = g_strdup(g_value_get_string(value));
|
|
|
|
break;
|
2010-03-22 12:03:26 +00:00
|
|
|
case PROP_FULLSCREEN:
|
|
|
|
sink->fullscreen = g_value_get_boolean(value);
|
|
|
|
break;
|
2010-03-12 23:53:48 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapisink_get_property(
|
|
|
|
GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec
|
|
|
|
)
|
|
|
|
{
|
|
|
|
GstVaapiSink * const sink = GST_VAAPISINK(object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_DISPLAY:
|
|
|
|
g_value_set_object(value, sink->display);
|
|
|
|
break;
|
|
|
|
case PROP_DISPLAY_NAME:
|
|
|
|
g_value_set_string(value, sink->display_name);
|
|
|
|
break;
|
2010-03-22 12:03:26 +00:00
|
|
|
case PROP_FULLSCREEN:
|
|
|
|
g_value_set_boolean(value, sink->fullscreen);
|
|
|
|
break;
|
2010-03-12 23:53:48 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 17:11:52 +00:00
|
|
|
|
|
|
|
static void gst_vaapisink_base_init(gpointer klass)
|
|
|
|
{
|
|
|
|
GstElementClass * const element_class = GST_ELEMENT_CLASS(klass);
|
|
|
|
|
|
|
|
gst_element_class_set_details(element_class, &gst_vaapisink_details);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template(
|
|
|
|
element_class,
|
|
|
|
gst_static_pad_template_get(&gst_vaapisink_sink_factory)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gst_vaapisink_class_init(GstVaapiSinkClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass * const object_class = G_OBJECT_CLASS(klass);
|
|
|
|
GstBaseSinkClass * const basesink_class = GST_BASE_SINK_CLASS(klass);
|
2010-03-12 23:53:48 +00:00
|
|
|
|
|
|
|
object_class->finalize = gst_vaapisink_finalize;
|
|
|
|
object_class->set_property = gst_vaapisink_set_property;
|
|
|
|
object_class->get_property = gst_vaapisink_get_property;
|
|
|
|
|
2010-03-15 17:09:12 +00:00
|
|
|
basesink_class->start = gst_vaapisink_start;
|
|
|
|
basesink_class->stop = gst_vaapisink_stop;
|
2010-03-17 07:20:19 +00:00
|
|
|
basesink_class->set_caps = gst_vaapisink_set_caps;
|
|
|
|
basesink_class->preroll = gst_vaapisink_show_frame;
|
|
|
|
basesink_class->render = gst_vaapisink_show_frame;
|
2010-03-15 17:09:12 +00:00
|
|
|
|
2010-03-12 23:53:48 +00:00
|
|
|
g_object_class_install_property
|
|
|
|
(object_class,
|
|
|
|
PROP_DISPLAY,
|
|
|
|
g_param_spec_object("display",
|
|
|
|
"display",
|
|
|
|
"display",
|
|
|
|
GST_VAAPI_TYPE_DISPLAY,
|
|
|
|
G_PARAM_READABLE));
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
(object_class,
|
|
|
|
PROP_DISPLAY_NAME,
|
|
|
|
g_param_spec_string("display-name",
|
|
|
|
"X11 display name",
|
|
|
|
"X11 display name",
|
|
|
|
"",
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2010-03-22 12:03:26 +00:00
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
(object_class,
|
|
|
|
PROP_FULLSCREEN,
|
|
|
|
g_param_spec_boolean("fullscreen",
|
|
|
|
"Fullscreen",
|
|
|
|
"Requests window in fullscreen state",
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE));
|
2010-03-05 17:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gst_vaapisink_init(GstVaapiSink *sink, GstVaapiSinkClass *klass)
|
|
|
|
{
|
2010-03-12 23:53:48 +00:00
|
|
|
sink->display_name = NULL;
|
|
|
|
sink->display = NULL;
|
2010-03-22 12:03:26 +00:00
|
|
|
sink->fullscreen = FALSE;
|
2010-03-12 23:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstVaapiDisplay *
|
|
|
|
gst_vaapisink_get_display(GstVaapiSink *sink)
|
|
|
|
{
|
2010-03-15 17:09:12 +00:00
|
|
|
if (!gst_vaapisink_ensure_display(sink))
|
|
|
|
return NULL;
|
2010-03-12 23:53:48 +00:00
|
|
|
return sink->display;
|
2010-03-05 17:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean plugin_init(GstPlugin *plugin)
|
|
|
|
{
|
2010-03-12 23:53:48 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT(gst_debug_vaapisink,
|
|
|
|
GST_PLUGIN_NAME, 0, GST_PLUGIN_DESC);
|
|
|
|
|
2010-03-05 17:11:52 +00:00
|
|
|
return gst_element_register(plugin,
|
2010-03-12 23:53:48 +00:00
|
|
|
GST_PLUGIN_NAME,
|
2010-03-05 17:11:52 +00:00
|
|
|
GST_RANK_PRIMARY,
|
|
|
|
GST_TYPE_VAAPISINK);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_PLUGIN_DEFINE(
|
|
|
|
GST_VERSION_MAJOR, GST_VERSION_MINOR,
|
2010-03-12 23:53:48 +00:00
|
|
|
GST_PLUGIN_NAME,
|
|
|
|
GST_PLUGIN_DESC,
|
2010-03-05 17:11:52 +00:00
|
|
|
plugin_init,
|
|
|
|
PACKAGE_VERSION,
|
|
|
|
"GPL",
|
|
|
|
PACKAGE,
|
|
|
|
PACKAGE_BUGREPORT);
|