2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
#include <gst/gst.h>
|
|
|
|
#include "gsttextoverlay.h"
|
2003-11-02 01:21:38 +00:00
|
|
|
/*#include "gsttexttestsrc.h"*/
|
|
|
|
/*#include "gstsubparse.h"*/
|
|
|
|
/*#include "SDL_blit.h"*/
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
static GstElementDetails textoverlay_details = {
|
2004-03-14 22:34:33 +00:00
|
|
|
"Text Overlay",
|
|
|
|
"Filter/Editor/Video",
|
|
|
|
"Adds text strings on top of a video buffer",
|
|
|
|
"Gustavo J. A. M. Carneiro <gjc@inescporto.pt>"
|
2003-07-17 06:29:48 +00:00
|
|
|
};
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARG_0,
|
|
|
|
ARG_TEXT,
|
|
|
|
ARG_VALIGN,
|
|
|
|
ARG_HALIGN,
|
|
|
|
ARG_X0,
|
|
|
|
ARG_Y0,
|
|
|
|
ARG_FONT_DESC,
|
2003-07-17 06:29:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-12-22 01:47:09 +00:00
|
|
|
static GstStaticPadTemplate textoverlay_src_template_factory =
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/x-raw-yuv, "
|
2004-03-15 19:32:27 +00:00
|
|
|
"format = (fourcc) I420, "
|
|
|
|
"width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
2003-12-22 01:47:09 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate video_sink_template_factory =
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("video_sink",
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/x-raw-yuv, "
|
2004-03-15 19:32:27 +00:00
|
|
|
"format = (fourcc) I420, "
|
|
|
|
"width = (int) [ 1, MAX ], " "height = (int) [ 1, MAX ]")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
2003-12-22 01:47:09 +00:00
|
|
|
|
|
|
|
static GstStaticPadTemplate text_sink_template_factory =
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("text_sink",
|
2003-12-22 01:47:09 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("text/x-pango-markup; text/plain")
|
2004-03-14 22:34:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static void gst_textoverlay_base_init (gpointer g_class);
|
|
|
|
static void gst_textoverlay_class_init (GstTextOverlayClass * klass);
|
|
|
|
static void gst_textoverlay_init (GstTextOverlay * overlay);
|
|
|
|
static void gst_textoverlay_set_property (GObject * object,
|
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_textoverlay_get_property (GObject * object,
|
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
static GstElementStateReturn gst_textoverlay_change_state (GstElement *
|
|
|
|
element);
|
|
|
|
static void gst_textoverlay_finalize (GObject * object);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2003-07-17 06:29:48 +00:00
|
|
|
/*static guint gst_textoverlay_signals[LAST_SIGNAL] = { 0 }; */
|
|
|
|
|
|
|
|
|
|
|
|
GType
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_get_type (void)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
static GType textoverlay_type = 0;
|
|
|
|
|
|
|
|
if (!textoverlay_type) {
|
|
|
|
static const GTypeInfo textoverlay_info = {
|
|
|
|
sizeof (GstTextOverlayClass),
|
|
|
|
gst_textoverlay_base_init,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_textoverlay_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstTextOverlay),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_textoverlay_init,
|
|
|
|
};
|
2004-03-15 19:32:27 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
textoverlay_type =
|
2004-03-15 19:32:27 +00:00
|
|
|
g_type_register_static (GST_TYPE_ELEMENT, "GstTextOverlay",
|
|
|
|
&textoverlay_info, 0);
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
return textoverlay_type;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
2003-11-02 01:21:38 +00:00
|
|
|
static void
|
|
|
|
gst_textoverlay_base_init (gpointer g_class)
|
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
2003-11-02 01:21:38 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&textoverlay_src_template_factory));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&video_sink_template_factory));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&text_sink_template_factory));
|
2003-11-02 01:21:38 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_element_class_set_details (element_class, &textoverlay_details);
|
2003-11-02 01:21:38 +00:00
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_class_init (GstTextOverlayClass * klass)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_textoverlay_finalize;
|
|
|
|
gobject_class->set_property = gst_textoverlay_set_property;
|
|
|
|
gobject_class->get_property = gst_textoverlay_get_property;
|
|
|
|
|
|
|
|
gstelement_class->change_state = gst_textoverlay_change_state;
|
|
|
|
klass->pango_context = pango_ft2_get_context (72, 72);
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TEXT,
|
|
|
|
g_param_spec_string ("text", "text",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Text to be display,"
|
|
|
|
" in pango markup format.", "", G_PARAM_WRITABLE));
|
2004-03-14 22:34:33 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_VALIGN,
|
|
|
|
g_param_spec_string ("valign", "vertical alignment",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Vertical alignment of the text. "
|
|
|
|
"Can be either 'baseline', 'bottom', or 'top'",
|
|
|
|
"baseline", G_PARAM_WRITABLE));
|
2004-03-14 22:34:33 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_HALIGN,
|
|
|
|
g_param_spec_string ("halign", "horizontal alignment",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Horizontal alignment of the text. "
|
|
|
|
"Can be either 'left', 'right', or 'center'",
|
|
|
|
"center", G_PARAM_WRITABLE));
|
2004-03-14 22:34:33 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_X0,
|
|
|
|
g_param_spec_int ("x0", "X position",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Initial X position."
|
|
|
|
" Horizontal aligment takes this point"
|
|
|
|
" as reference.", G_MININT, G_MAXINT, 0, G_PARAM_WRITABLE));
|
2004-03-14 22:34:33 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_Y0,
|
|
|
|
g_param_spec_int ("y0", "Y position",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Initial Y position."
|
|
|
|
" Vertical aligment takes this point"
|
|
|
|
" as reference.", G_MININT, G_MAXINT, 0, G_PARAM_WRITABLE));
|
2004-03-14 22:34:33 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FONT_DESC,
|
|
|
|
g_param_spec_string ("font-desc", "font description",
|
2004-03-15 19:32:27 +00:00
|
|
|
"Pango font description of font "
|
|
|
|
"to be used for rendering. "
|
|
|
|
"See documentation of "
|
|
|
|
"pango_font_description_from_string"
|
|
|
|
" for syntax.", "", G_PARAM_WRITABLE));
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
resize_bitmap (GstTextOverlay * overlay, int width, int height)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
FT_Bitmap *bitmap = &overlay->bitmap;
|
|
|
|
int pitch = (width | 3) + 1;
|
|
|
|
int size = pitch * height;
|
|
|
|
|
|
|
|
/* no need to keep reallocating; just keep the maximum size so far */
|
|
|
|
if (size <= overlay->bitmap_buffer_size) {
|
2003-07-17 06:29:48 +00:00
|
|
|
bitmap->rows = height;
|
|
|
|
bitmap->width = width;
|
|
|
|
bitmap->pitch = pitch;
|
2004-03-14 22:34:33 +00:00
|
|
|
memset (bitmap->buffer, 0, overlay->bitmap_buffer_size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!bitmap->buffer) {
|
|
|
|
/* initialize */
|
|
|
|
bitmap->pixel_mode = ft_pixel_mode_grays;
|
|
|
|
bitmap->num_grays = 256;
|
|
|
|
}
|
|
|
|
if (bitmap->buffer)
|
|
|
|
bitmap->buffer = g_realloc (bitmap->buffer, size);
|
|
|
|
else
|
|
|
|
bitmap->buffer = g_malloc (size);
|
|
|
|
bitmap->rows = height;
|
|
|
|
bitmap->width = width;
|
|
|
|
bitmap->pitch = pitch;
|
|
|
|
memset (bitmap->buffer, 0, size);
|
|
|
|
overlay->bitmap_buffer_size = size;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
render_text (GstTextOverlay * overlay)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
PangoRectangle ink_rect, logical_rect;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
pango_layout_get_pixel_extents (overlay->layout, &ink_rect, &logical_rect);
|
|
|
|
resize_bitmap (overlay, ink_rect.width, ink_rect.height + ink_rect.y);
|
|
|
|
pango_ft2_render_layout (&overlay->bitmap, overlay->layout, 0, 0);
|
|
|
|
overlay->baseline_y = ink_rect.y;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static GstPadLinkReturn */
|
|
|
|
/* gst_textoverlay_text_sinkconnect (GstPad *pad, GstCaps *caps) */
|
|
|
|
/* { */
|
|
|
|
/* return GST_PAD_LINK_DONE; */
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
|
|
|
|
static GstPadLinkReturn
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_video_sinkconnect (GstPad * pad, const GstCaps * caps)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstTextOverlay *overlay;
|
|
|
|
GstStructure *structure;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay = GST_TEXTOVERLAY (gst_pad_get_parent (pad));
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
overlay->width = overlay->height = 0;
|
|
|
|
gst_structure_get_int (structure, "width", &overlay->width);
|
|
|
|
gst_structure_get_int (structure, "height", &overlay->height);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
return gst_pad_try_set_caps (overlay->srcpad, caps);
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_text_overlay_blit_yuv420 (GstTextOverlay * overlay, FT_Bitmap * bitmap,
|
|
|
|
guchar * pixbuf, int x0, int y0)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-15 19:32:27 +00:00
|
|
|
int y; /* text bitmap coordinates */
|
|
|
|
int x1, y1; /* video buffer coordinates */
|
2004-03-14 22:34:33 +00:00
|
|
|
int rowinc, bit_rowinc, uv_rowinc;
|
|
|
|
guchar *p, *bitp, *u_p;
|
|
|
|
int video_width = overlay->width, video_height = overlay->height;
|
2004-03-15 19:32:27 +00:00
|
|
|
int bitmap_x0 = x0 < 1 ? -(x0 - 1) : 1; /* 1 pixel border */
|
|
|
|
int bitmap_y0 = y0 < 1 ? -(y0 - 1) : 1; /* 1 pixel border */
|
2004-03-14 22:34:33 +00:00
|
|
|
int bitmap_width = bitmap->width - bitmap_x0;
|
|
|
|
int bitmap_height = bitmap->rows - bitmap_y0;
|
|
|
|
int u_plane_size;
|
|
|
|
int skip_y, skip_x;
|
|
|
|
guchar v;
|
|
|
|
|
2004-03-15 19:32:27 +00:00
|
|
|
if (x0 + bitmap_x0 + bitmap_width > video_width - 1) /* 1 pixel border */
|
2004-03-14 22:34:33 +00:00
|
|
|
bitmap_width -= x0 + bitmap_x0 + bitmap_width - video_width + 1;
|
2004-03-15 19:32:27 +00:00
|
|
|
if (y0 + bitmap_y0 + bitmap_height > video_height - 1) /* 1 pixel border */
|
2004-03-14 22:34:33 +00:00
|
|
|
bitmap_height -= y0 + bitmap_y0 + bitmap_height - video_height + 1;
|
|
|
|
|
|
|
|
rowinc = video_width - bitmap_width;
|
|
|
|
uv_rowinc = video_width / 2 - bitmap_width / 2;
|
|
|
|
bit_rowinc = bitmap->pitch - bitmap_width;
|
|
|
|
u_plane_size = (video_width / 2) * (video_height / 2);
|
|
|
|
|
|
|
|
y1 = y0 + bitmap_y0;
|
|
|
|
x1 = x0 + bitmap_x0;
|
|
|
|
p = pixbuf + video_width * y1 + x1;
|
|
|
|
bitp = bitmap->buffer + bitmap->pitch * bitmap_y0 + bitmap_x0;
|
|
|
|
for (y = bitmap_y0; y < bitmap_height; y++) {
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = bitmap_width; n > 0; --n) {
|
|
|
|
v = *bitp;
|
|
|
|
if (v) {
|
2004-03-15 19:32:27 +00:00
|
|
|
p[-1] = CLAMP (p[-1] - v, 0, 255);
|
|
|
|
p[1] = CLAMP (p[1] - v, 0, 255);
|
|
|
|
p[-video_width] = CLAMP (p[-video_width] - v, 0, 255);
|
|
|
|
p[video_width] = CLAMP (p[video_width] - v, 0, 255);
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
p++;
|
|
|
|
bitp++;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
p += rowinc;
|
|
|
|
bitp += bit_rowinc;
|
|
|
|
}
|
|
|
|
|
|
|
|
y = bitmap_y0;
|
|
|
|
y1 = y0 + bitmap_y0;
|
|
|
|
x1 = x0 + bitmap_x0;
|
|
|
|
bitp = bitmap->buffer + bitmap->pitch * bitmap_y0 + bitmap_x0;
|
|
|
|
p = pixbuf + video_width * y1 + x1;
|
|
|
|
u_p =
|
|
|
|
pixbuf + video_width * video_height + (video_width >> 1) * (y1 >> 1) +
|
|
|
|
(x1 >> 1);
|
|
|
|
skip_y = 0;
|
|
|
|
skip_x = 0;
|
|
|
|
|
|
|
|
for (; y < bitmap_height; y++) {
|
|
|
|
int n;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
x1 = x0 + bitmap_x0;
|
|
|
|
skip_x = 0;
|
2004-03-14 22:34:33 +00:00
|
|
|
for (n = bitmap_width; n > 0; --n) {
|
|
|
|
v = *bitp;
|
|
|
|
if (v) {
|
2004-03-15 19:32:27 +00:00
|
|
|
*p = v;
|
|
|
|
if (!skip_y) {
|
|
|
|
u_p[0] = u_p[u_plane_size] = 0x80;
|
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
if (!skip_y) {
|
2004-03-15 19:32:27 +00:00
|
|
|
skip_x = !skip_x;
|
|
|
|
if (!skip_x)
|
|
|
|
u_p++;
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
bitp++;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
/*if (!skip_x && !skip_y) u_p--; */
|
|
|
|
p += rowinc;
|
|
|
|
bitp += bit_rowinc;
|
|
|
|
skip_y = !skip_y;
|
|
|
|
u_p += skip_y ? uv_rowinc : 0;
|
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_video_chain (GstPad * pad, GstData * _data)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstBuffer *buf = GST_BUFFER (_data);
|
|
|
|
GstTextOverlay *overlay;
|
|
|
|
guchar *pixbuf;
|
|
|
|
gint x0, y0;
|
|
|
|
|
|
|
|
g_return_if_fail (pad != NULL);
|
|
|
|
g_return_if_fail (GST_IS_PAD (pad));
|
|
|
|
g_return_if_fail (buf != NULL);
|
|
|
|
overlay = GST_TEXTOVERLAY (gst_pad_get_parent (pad));
|
|
|
|
g_return_if_fail (overlay != NULL);
|
|
|
|
g_return_if_fail (GST_IS_TEXTOVERLAY (overlay));
|
|
|
|
|
|
|
|
pixbuf = GST_BUFFER_DATA (buf);
|
|
|
|
|
|
|
|
x0 = overlay->x0;
|
|
|
|
y0 = overlay->y0;
|
|
|
|
switch (overlay->valign) {
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_TEXT_OVERLAY_VALIGN_BOTTOM:
|
2004-03-14 22:34:33 +00:00
|
|
|
y0 += overlay->bitmap.rows;
|
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_TEXT_OVERLAY_VALIGN_BASELINE:
|
2004-03-14 22:34:33 +00:00
|
|
|
y0 -= (overlay->bitmap.rows - overlay->baseline_y);
|
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_TEXT_OVERLAY_VALIGN_TOP:
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (overlay->halign) {
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_TEXT_OVERLAY_HALIGN_LEFT:
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_TEXT_OVERLAY_HALIGN_RIGHT:
|
2004-03-14 22:34:33 +00:00
|
|
|
x0 -= overlay->bitmap.width;
|
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_TEXT_OVERLAY_HALIGN_CENTER:
|
2004-03-14 22:34:33 +00:00
|
|
|
x0 -= overlay->bitmap.width / 2;
|
|
|
|
break;
|
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (overlay->bitmap.buffer)
|
|
|
|
gst_text_overlay_blit_yuv420 (overlay, &overlay->bitmap, pixbuf, x0, y0);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_pad_push (overlay->srcpad, GST_DATA (buf));
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
2003-08-26 06:56:58 +00:00
|
|
|
#define PAST_END(buffer, time) \
|
|
|
|
(GST_BUFFER_TIMESTAMP (buffer) != GST_CLOCK_TIME_NONE && \
|
|
|
|
GST_BUFFER_DURATION (buffer) != GST_CLOCK_TIME_NONE && \
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) \
|
|
|
|
< (time))
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_loop (GstElement * element)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstTextOverlay *overlay;
|
|
|
|
GstBuffer *video_frame;
|
|
|
|
guint64 now;
|
|
|
|
|
|
|
|
g_return_if_fail (element != NULL);
|
|
|
|
g_return_if_fail (GST_IS_TEXTOVERLAY (element));
|
|
|
|
overlay = GST_TEXTOVERLAY (element);
|
|
|
|
|
|
|
|
video_frame = GST_BUFFER (gst_pad_pull (overlay->video_sinkpad));
|
|
|
|
now = GST_BUFFER_TIMESTAMP (video_frame);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This state machine has a bug that can't be resolved easily.
|
|
|
|
* (Needs a more complicated state machine.) Basically, if the
|
|
|
|
* text that came from a buffer from the sink pad is being
|
|
|
|
* displayed, and the default text is changed by set_parameter,
|
|
|
|
* we'll incorrectly display the default text.
|
|
|
|
*
|
|
|
|
* Otherwise, this is a pretty decent state machine that handles
|
|
|
|
* buffer timestamps and durations correctly. (I think)
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (overlay->next_buffer == NULL) {
|
|
|
|
GST_DEBUG ("attempting to pull a buffer");
|
|
|
|
|
|
|
|
/* read all text buffers until we get one "in the future" */
|
|
|
|
if (!GST_PAD_IS_USABLE (overlay->text_sinkpad)) {
|
|
|
|
break;
|
2003-08-26 06:56:58 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->next_buffer = GST_BUFFER (gst_pad_pull (overlay->text_sinkpad));
|
|
|
|
if (!overlay->next_buffer)
|
|
|
|
break;
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (PAST_END (overlay->next_buffer, now)) {
|
|
|
|
gst_buffer_unref (overlay->next_buffer);
|
2003-08-26 06:56:58 +00:00
|
|
|
overlay->next_buffer = NULL;
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (overlay->next_buffer &&
|
|
|
|
(GST_BUFFER_TIMESTAMP (overlay->next_buffer) <= now ||
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (overlay->next_buffer) == GST_CLOCK_TIME_NONE)) {
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_DEBUG ("using new buffer");
|
|
|
|
|
|
|
|
if (overlay->current_buffer) {
|
|
|
|
gst_buffer_unref (overlay->current_buffer);
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->current_buffer = overlay->next_buffer;
|
|
|
|
overlay->next_buffer = NULL;
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_DEBUG ("rendering '%*s'",
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_BUFFER_SIZE (overlay->current_buffer),
|
|
|
|
GST_BUFFER_DATA (overlay->current_buffer));
|
2004-03-14 22:34:33 +00:00
|
|
|
pango_layout_set_markup (overlay->layout,
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_BUFFER_DATA (overlay->current_buffer),
|
|
|
|
GST_BUFFER_SIZE (overlay->current_buffer));
|
2004-03-14 22:34:33 +00:00
|
|
|
render_text (overlay);
|
|
|
|
overlay->need_render = FALSE;
|
|
|
|
}
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (overlay->current_buffer && PAST_END (overlay->current_buffer, now)) {
|
|
|
|
GST_DEBUG ("dropping old buffer");
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_buffer_unref (overlay->current_buffer);
|
|
|
|
overlay->current_buffer = NULL;
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->need_render = TRUE;
|
|
|
|
}
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
if (overlay->need_render) {
|
|
|
|
GST_DEBUG ("rendering '%s'", overlay->default_text);
|
|
|
|
pango_layout_set_markup (overlay->layout,
|
2004-03-15 19:32:27 +00:00
|
|
|
overlay->default_text, strlen (overlay->default_text));
|
2004-03-14 22:34:33 +00:00
|
|
|
render_text (overlay);
|
|
|
|
|
|
|
|
overlay->need_render = FALSE;
|
|
|
|
}
|
2003-08-26 06:56:58 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_video_chain (overlay->srcpad, GST_DATA (video_frame));
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstElementStateReturn
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_change_state (GstElement * element)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstTextOverlay *overlay;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay = GST_TEXTOVERLAY (element);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (GST_STATE_TRANSITION (element)) {
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_STATE_PAUSED_TO_PLAYING:
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_STATE_PLAYING_TO_PAUSED:
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
parent_class->change_state (element);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
return GST_STATE_SUCCESS;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_finalize (GObject * object)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstTextOverlay *overlay = GST_TEXTOVERLAY (object);
|
|
|
|
|
|
|
|
if (overlay->layout) {
|
|
|
|
g_object_unref (overlay->layout);
|
|
|
|
overlay->layout = NULL;
|
|
|
|
}
|
|
|
|
if (overlay->bitmap.buffer) {
|
|
|
|
g_free (overlay->bitmap.buffer);
|
|
|
|
overlay->bitmap.buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_init (GstTextOverlay * overlay)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
/* video sink */
|
|
|
|
overlay->video_sinkpad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&video_sink_template_factory), "video_sink");
|
2003-07-17 06:29:48 +00:00
|
|
|
/* gst_pad_set_chain_function(overlay->video_sinkpad, gst_textoverlay_video_chain); */
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_pad_set_link_function (overlay->video_sinkpad,
|
|
|
|
gst_textoverlay_video_sinkconnect);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (overlay), overlay->video_sinkpad);
|
|
|
|
|
|
|
|
/* text sink */
|
|
|
|
overlay->text_sinkpad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&text_sink_template_factory), "text_sink");
|
2003-07-17 06:29:48 +00:00
|
|
|
/* gst_pad_set_link_function(overlay->text_sinkpad, gst_textoverlay_text_sinkconnect); */
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (overlay), overlay->text_sinkpad);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
/* (video) source */
|
|
|
|
overlay->srcpad =
|
|
|
|
gst_pad_new_from_template (gst_static_pad_template_get
|
|
|
|
(&textoverlay_src_template_factory), "src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (overlay), overlay->srcpad);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->layout =
|
|
|
|
pango_layout_new (GST_TEXTOVERLAY_GET_CLASS (overlay)->pango_context);
|
|
|
|
memset (&overlay->bitmap, 0, sizeof (overlay->bitmap));
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->halign = GST_TEXT_OVERLAY_HALIGN_CENTER;
|
|
|
|
overlay->valign = GST_TEXT_OVERLAY_VALIGN_BASELINE;
|
|
|
|
overlay->x0 = overlay->y0 = 0;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->default_text = g_strdup ("");
|
|
|
|
overlay->need_render = TRUE;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_element_set_loop_function (GST_ELEMENT (overlay), gst_textoverlay_loop);
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstTextOverlay *overlay;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_TEXTOVERLAY (object));
|
|
|
|
overlay = GST_TEXTOVERLAY (object);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (prop_id) {
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
case ARG_TEXT:
|
2004-03-14 22:34:33 +00:00
|
|
|
if (overlay->default_text) {
|
2004-03-15 19:32:27 +00:00
|
|
|
g_free (overlay->default_text);
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
overlay->default_text = g_strdup (g_value_get_string (value));
|
|
|
|
overlay->need_render = TRUE;
|
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
case ARG_VALIGN:
|
2004-03-14 22:34:33 +00:00
|
|
|
if (strcasecmp (g_value_get_string (value), "baseline") == 0)
|
2004-03-15 19:32:27 +00:00
|
|
|
overlay->valign = GST_TEXT_OVERLAY_VALIGN_BASELINE;
|
2004-03-14 22:34:33 +00:00
|
|
|
else if (strcasecmp (g_value_get_string (value), "bottom") == 0)
|
2004-03-15 19:32:27 +00:00
|
|
|
overlay->valign = GST_TEXT_OVERLAY_VALIGN_BOTTOM;
|
2004-03-14 22:34:33 +00:00
|
|
|
else if (strcasecmp (g_value_get_string (value), "top") == 0)
|
2004-03-15 19:32:27 +00:00
|
|
|
overlay->valign = GST_TEXT_OVERLAY_VALIGN_TOP;
|
2004-03-14 22:34:33 +00:00
|
|
|
else
|
2004-03-15 19:32:27 +00:00
|
|
|
g_warning ("Invalid 'valign' property value: %s",
|
|
|
|
g_value_get_string (value));
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
case ARG_HALIGN:
|
2004-03-14 22:34:33 +00:00
|
|
|
if (strcasecmp (g_value_get_string (value), "left") == 0)
|
2004-03-15 19:32:27 +00:00
|
|
|
overlay->halign = GST_TEXT_OVERLAY_HALIGN_LEFT;
|
2004-03-14 22:34:33 +00:00
|
|
|
else if (strcasecmp (g_value_get_string (value), "right") == 0)
|
2004-03-15 19:32:27 +00:00
|
|
|
overlay->halign = GST_TEXT_OVERLAY_HALIGN_RIGHT;
|
2004-03-14 22:34:33 +00:00
|
|
|
else if (strcasecmp (g_value_get_string (value), "center") == 0)
|
2004-03-15 19:32:27 +00:00
|
|
|
overlay->halign = GST_TEXT_OVERLAY_HALIGN_CENTER;
|
2004-03-14 22:34:33 +00:00
|
|
|
else
|
2004-03-15 19:32:27 +00:00
|
|
|
g_warning ("Invalid 'halign' property value: %s",
|
|
|
|
g_value_get_string (value));
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
case ARG_X0:
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->x0 = g_value_get_int (value);
|
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
case ARG_Y0:
|
2004-03-14 22:34:33 +00:00
|
|
|
overlay->y0 = g_value_get_int (value);
|
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
|
|
|
case ARG_FONT_DESC:
|
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
PangoFontDescription *desc;
|
|
|
|
|
|
|
|
desc = pango_font_description_from_string (g_value_get_string (value));
|
|
|
|
if (desc) {
|
2004-03-15 19:32:27 +00:00
|
|
|
g_message ("font description set: %s", g_value_get_string (value));
|
|
|
|
pango_layout_set_font_description (overlay->layout, desc);
|
|
|
|
pango_font_description_free (desc);
|
|
|
|
render_text (overlay);
|
2004-03-14 22:34:33 +00:00
|
|
|
} else
|
2004-03-15 19:32:27 +00:00
|
|
|
g_warning ("font description parse failed: %s",
|
|
|
|
g_value_get_string (value));
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2004-03-14 22:34:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-14 22:34:33 +00:00
|
|
|
gst_textoverlay_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
GstTextOverlay *overlay;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
/* it's not null if we got it, but it might not be ours */
|
|
|
|
g_return_if_fail (GST_IS_TEXTOVERLAY (object));
|
|
|
|
overlay = GST_TEXTOVERLAY (object);
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
switch (prop_id) {
|
2003-07-17 06:29:48 +00:00
|
|
|
default:
|
2004-03-14 22:34:33 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2004-03-14 22:34:33 +00:00
|
|
|
plugin_init (GstPlugin * plugin)
|
2003-07-17 06:29:48 +00:00
|
|
|
{
|
2004-03-30 07:07:45 +00:00
|
|
|
if (!gst_element_register (plugin, "textoverlay", GST_RANK_NONE,
|
2004-03-15 19:32:27 +00:00
|
|
|
GST_TYPE_TEXTOVERLAY))
|
2004-03-14 22:34:33 +00:00
|
|
|
return FALSE;
|
2003-07-17 06:29:48 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
/*texttestsrc_plugin_init(module, plugin); */
|
|
|
|
/*subparse_plugin_init(module, plugin); */
|
|
|
|
return TRUE;
|
2003-07-17 06:29:48 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
2003-07-17 06:29:48 +00:00
|
|
|
GST_VERSION_MINOR,
|
|
|
|
"textoverlay",
|
2004-03-14 22:34:33 +00:00
|
|
|
"Text overlay", plugin_init, VERSION, "GPL", GST_PACKAGE, GST_ORIGIN)
|