1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
4 * Copyright (C) <2009> Young-Ho Cha <ganadist@gmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22
23 /**
24 * SECTION:element-textrender
25 * @title: textrender
26 * @see_also: #GstTextOverlay
27 *
28 * This plugin renders text received on the text sink pad to a video
29 * buffer (retaining the alpha channel), so it can later be overlayed
30 * on top of video streams using other elements.
31 *
32 * The text can contain newline characters. (FIXME: What about text
33 * wrapping? It does not make sense in this context)
34 *
35 * ## Example launch lines
36 * |[
37 * gst-launch-1.0 -v filesrc location=subtitles.srt ! subparse ! textrender ! videoconvert ! autovideosink
38 * ]|
39 *
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45 #include <string.h>
46 #include <gst/gst.h>
47 #include <gst/video/video.h>
48
49 #include "gsttextrender.h"
50 #include "gstpangoelements.h"
51
52
53 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
54 # define CAIRO_ARGB_A 3
55 # define CAIRO_ARGB_R 2
56 # define CAIRO_ARGB_G 1
57 # define CAIRO_ARGB_B 0
58 #else
59 # define CAIRO_ARGB_A 0
60 # define CAIRO_ARGB_R 1
61 # define CAIRO_ARGB_G 2
62 # define CAIRO_ARGB_B 3
63 #endif
64
65 GST_DEBUG_CATEGORY_EXTERN (pango_debug);
66 #define GST_CAT_DEFAULT pango_debug
67
68 #define MINIMUM_OUTLINE_OFFSET 1.0
69
70 #define DEFAULT_PROP_VALIGNMENT GST_TEXT_RENDER_VALIGN_BASELINE
71 #define DEFAULT_PROP_HALIGNMENT GST_TEXT_RENDER_HALIGN_CENTER
72 #define DEFAULT_PROP_LINE_ALIGNMENT GST_TEXT_RENDER_LINE_ALIGN_CENTER
73 #define DEFAULT_PROP_XPAD 25
74 #define DEFAULT_PROP_YPAD 25
75
76 #define DEFAULT_RENDER_WIDTH 720
77 #define DEFAULT_RENDER_HEIGHT 576
78
79 enum
80 {
81 PROP_0,
82 PROP_HALIGNMENT,
83 PROP_VALIGNMENT,
84 PROP_LINE_ALIGNMENT,
85 PROP_XPAD,
86 PROP_YPAD,
87 PROP_FONT_DESC
88 };
89
90 #define VIDEO_FORMATS "{ AYUV, ARGB } "
91
92 static GstStaticPadTemplate src_template_factory =
93 GST_STATIC_PAD_TEMPLATE ("src",
94 GST_PAD_SRC,
95 GST_PAD_ALWAYS,
96 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (VIDEO_FORMATS))
97 );
98
99 static GstStaticPadTemplate sink_template_factory =
100 GST_STATIC_PAD_TEMPLATE ("sink",
101 GST_PAD_SINK,
102 GST_PAD_ALWAYS,
103 GST_STATIC_CAPS ("text/x-raw, format = { pango-markup, utf8 }")
104 );
105
106 #define GST_TYPE_TEXT_RENDER_VALIGN (gst_text_render_valign_get_type())
107 static GType
gst_text_render_valign_get_type(void)108 gst_text_render_valign_get_type (void)
109 {
110 static GType text_render_valign_type = 0;
111 static const GEnumValue text_render_valign[] = {
112 {GST_TEXT_RENDER_VALIGN_BASELINE, "baseline", "baseline"},
113 {GST_TEXT_RENDER_VALIGN_BOTTOM, "bottom", "bottom"},
114 {GST_TEXT_RENDER_VALIGN_TOP, "top", "top"},
115 {0, NULL, NULL},
116 };
117
118 if (!text_render_valign_type) {
119 text_render_valign_type =
120 g_enum_register_static ("GstTextRenderVAlign", text_render_valign);
121 }
122 return text_render_valign_type;
123 }
124
125 #define GST_TYPE_TEXT_RENDER_HALIGN (gst_text_render_halign_get_type())
126 static GType
gst_text_render_halign_get_type(void)127 gst_text_render_halign_get_type (void)
128 {
129 static GType text_render_halign_type = 0;
130 static const GEnumValue text_render_halign[] = {
131 {GST_TEXT_RENDER_HALIGN_LEFT, "left", "left"},
132 {GST_TEXT_RENDER_HALIGN_CENTER, "center", "center"},
133 {GST_TEXT_RENDER_HALIGN_RIGHT, "right", "right"},
134 {0, NULL, NULL},
135 };
136
137 if (!text_render_halign_type) {
138 text_render_halign_type =
139 g_enum_register_static ("GstTextRenderHAlign", text_render_halign);
140 }
141 return text_render_halign_type;
142 }
143
144 #define GST_TYPE_TEXT_RENDER_LINE_ALIGN (gst_text_render_line_align_get_type())
145 static GType
gst_text_render_line_align_get_type(void)146 gst_text_render_line_align_get_type (void)
147 {
148 static GType text_render_line_align_type = 0;
149 static const GEnumValue text_render_line_align[] = {
150 {GST_TEXT_RENDER_LINE_ALIGN_LEFT, "left", "left"},
151 {GST_TEXT_RENDER_LINE_ALIGN_CENTER, "center", "center"},
152 {GST_TEXT_RENDER_LINE_ALIGN_RIGHT, "right", "right"},
153 {0, NULL, NULL}
154 };
155
156 if (!text_render_line_align_type) {
157 text_render_line_align_type =
158 g_enum_register_static ("GstTextRenderLineAlign",
159 text_render_line_align);
160 }
161 return text_render_line_align_type;
162 }
163
164 static void gst_text_render_adjust_values_with_fontdesc (GstTextRender *
165 render, PangoFontDescription * desc);
166
167 #define gst_text_render_parent_class parent_class
168 G_DEFINE_TYPE (GstTextRender, gst_text_render, GST_TYPE_ELEMENT);
169 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (textrender, "textrender",
170 GST_RANK_NONE, GST_TYPE_TEXT_RENDER, pango_element_init (plugin));
171
172 static void gst_text_render_finalize (GObject * object);
173 static void gst_text_render_set_property (GObject * object,
174 guint prop_id, const GValue * value, GParamSpec * pspec);
175 static void gst_text_render_get_property (GObject * object,
176 guint prop_id, GValue * value, GParamSpec * pspec);
177
178 static void
gst_text_render_class_init(GstTextRenderClass * klass)179 gst_text_render_class_init (GstTextRenderClass * klass)
180 {
181 GObjectClass *gobject_class;
182 GstElementClass *gstelement_class;
183
184 gobject_class = (GObjectClass *) klass;
185 gstelement_class = (GstElementClass *) klass;
186
187 parent_class = g_type_class_peek_parent (klass);
188
189 gobject_class->finalize = gst_text_render_finalize;
190 gobject_class->set_property = gst_text_render_set_property;
191 gobject_class->get_property = gst_text_render_get_property;
192
193 gst_element_class_add_static_pad_template (gstelement_class,
194 &src_template_factory);
195 gst_element_class_add_static_pad_template (gstelement_class,
196 &sink_template_factory);
197
198 gst_element_class_set_static_metadata (gstelement_class, "Text renderer",
199 "Filter/Editor/Video",
200 "Renders a text string to an image bitmap",
201 "David Schleef <ds@schleef.org>, "
202 "GStreamer maintainers <gstreamer-devel@lists.freedesktop.org>");
203
204 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FONT_DESC,
205 g_param_spec_string ("font-desc", "font description",
206 "Pango font description of font "
207 "to be used for rendering. "
208 "See documentation of "
209 "pango_font_description_from_string"
210 " for syntax.", "", G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_VALIGNMENT,
212 g_param_spec_enum ("valignment", "vertical alignment",
213 "Vertical alignment of the text", GST_TYPE_TEXT_RENDER_VALIGN,
214 DEFAULT_PROP_VALIGNMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HALIGNMENT,
216 g_param_spec_enum ("halignment", "horizontal alignment",
217 "Horizontal alignment of the text", GST_TYPE_TEXT_RENDER_HALIGN,
218 DEFAULT_PROP_HALIGNMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_XPAD,
220 g_param_spec_int ("xpad", "horizontal paddding",
221 "Horizontal paddding when using left/right alignment", 0, G_MAXINT,
222 DEFAULT_PROP_XPAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_YPAD,
224 g_param_spec_int ("ypad", "vertical padding",
225 "Vertical padding when using top/bottom alignment", 0, G_MAXINT,
226 DEFAULT_PROP_YPAD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LINE_ALIGNMENT,
228 g_param_spec_enum ("line-alignment", "line alignment",
229 "Alignment of text lines relative to each other.",
230 GST_TYPE_TEXT_RENDER_LINE_ALIGN, DEFAULT_PROP_LINE_ALIGNMENT,
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232
233 gst_type_mark_as_plugin_api (GST_TYPE_TEXT_RENDER_HALIGN, 0);
234 gst_type_mark_as_plugin_api (GST_TYPE_TEXT_RENDER_VALIGN, 0);
235 gst_type_mark_as_plugin_api (GST_TYPE_TEXT_RENDER_LINE_ALIGN, 0);
236 }
237
238 static void
gst_text_render_adjust_values_with_fontdesc(GstTextRender * render,PangoFontDescription * desc)239 gst_text_render_adjust_values_with_fontdesc (GstTextRender * render,
240 PangoFontDescription * desc)
241 {
242 gint font_size = pango_font_description_get_size (desc) / PANGO_SCALE;
243
244 render->shadow_offset = (double) (font_size) / 13.0;
245 render->outline_offset = (double) (font_size) / 15.0;
246 if (render->outline_offset < MINIMUM_OUTLINE_OFFSET)
247 render->outline_offset = MINIMUM_OUTLINE_OFFSET;
248 }
249
250 static void
gst_text_render_render_pangocairo(GstTextRender * render)251 gst_text_render_render_pangocairo (GstTextRender * render)
252 {
253 cairo_t *cr;
254 cairo_surface_t *surface;
255 cairo_t *cr_shadow;
256 cairo_surface_t *surface_shadow;
257 PangoRectangle ink_rect, logical_rect;
258 gint width, height;
259
260 pango_layout_get_pixel_extents (render->layout, &ink_rect, &logical_rect);
261
262 width = logical_rect.width + render->shadow_offset;
263 height = logical_rect.height + logical_rect.y + render->shadow_offset;
264
265 surface_shadow = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
266 cr_shadow = cairo_create (surface_shadow);
267
268 /* clear shadow surface */
269 cairo_set_operator (cr_shadow, CAIRO_OPERATOR_CLEAR);
270 cairo_paint (cr_shadow);
271 cairo_set_operator (cr_shadow, CAIRO_OPERATOR_OVER);
272
273 /* draw shadow text */
274 cairo_save (cr_shadow);
275 cairo_set_source_rgba (cr_shadow, 0.0, 0.0, 0.0, 0.5);
276 cairo_translate (cr_shadow, render->shadow_offset, render->shadow_offset);
277 pango_cairo_show_layout (cr_shadow, render->layout);
278 cairo_restore (cr_shadow);
279
280 /* draw outline text */
281 cairo_save (cr_shadow);
282 cairo_set_source_rgb (cr_shadow, 0.0, 0.0, 0.0);
283 cairo_set_line_width (cr_shadow, render->outline_offset);
284 pango_cairo_layout_path (cr_shadow, render->layout);
285 cairo_stroke (cr_shadow);
286 cairo_restore (cr_shadow);
287
288 cairo_destroy (cr_shadow);
289
290 render->text_image = g_realloc (render->text_image, 4 * width * height);
291
292 surface = cairo_image_surface_create_for_data (render->text_image,
293 CAIRO_FORMAT_ARGB32, width, height, width * 4);
294 cr = cairo_create (surface);
295 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
296 cairo_paint (cr);
297 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
298
299 /* set default color */
300 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
301
302 cairo_save (cr);
303 /* draw text */
304 pango_cairo_show_layout (cr, render->layout);
305 cairo_restore (cr);
306
307 /* composite shadow with offset */
308 cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
309 cairo_set_source_surface (cr, surface_shadow, 0.0, 0.0);
310 cairo_paint (cr);
311
312 cairo_destroy (cr);
313 cairo_surface_destroy (surface_shadow);
314 cairo_surface_destroy (surface);
315 render->image_width = width;
316 render->image_height = height;
317 }
318
319 static void
gst_text_render_check_argb(GstTextRender * render)320 gst_text_render_check_argb (GstTextRender * render)
321 {
322 GstCaps *peer_caps;
323 peer_caps = gst_pad_get_allowed_caps (render->srcpad);
324 if (G_LIKELY (peer_caps)) {
325 guint i = 0, n = 0;
326
327 n = gst_caps_get_size (peer_caps);
328 GST_DEBUG_OBJECT (render, "peer allowed caps (%u structure(s)) are %"
329 GST_PTR_FORMAT, n, peer_caps);
330
331 /* Check if AYUV or ARGB is first */
332 for (i = 0; i < n; i++) {
333 GstStructure *s;
334 GstVideoFormat vformat;
335 const GstVideoFormatInfo *info;
336 const gchar *fmt;
337
338 s = gst_caps_get_structure (peer_caps, i);
339 if (!gst_structure_has_name (s, "video/x-raw"))
340 continue;
341
342 fmt = gst_structure_get_string (s, "format");
343 if (fmt == NULL)
344 continue;
345
346 vformat = gst_video_format_from_string (fmt);
347 info = gst_video_format_get_info (vformat);
348 if (info == NULL)
349 continue;
350
351 render->use_ARGB = GST_VIDEO_FORMAT_INFO_IS_RGB (info);
352 }
353 gst_caps_unref (peer_caps);
354 }
355 }
356
357 static gboolean
gst_text_render_src_setcaps(GstTextRender * render,GstCaps * caps)358 gst_text_render_src_setcaps (GstTextRender * render, GstCaps * caps)
359 {
360 GstStructure *structure;
361 gboolean ret;
362 gint width = 0, height = 0;
363
364 structure = gst_caps_get_structure (caps, 0);
365 gst_structure_get_int (structure, "width", &width);
366 gst_structure_get_int (structure, "height", &height);
367
368 GST_DEBUG_OBJECT (render, "Got caps %" GST_PTR_FORMAT, caps);
369
370 if (width >= render->image_width && height >= render->image_height) {
371 render->width = width;
372 render->height = height;
373 }
374
375 gst_text_render_check_argb (render);
376
377 ret = gst_pad_set_caps (render->srcpad, caps);
378
379 return ret;
380 }
381
382 static GstCaps *
gst_text_render_fixate_caps(GstTextRender * render,GstCaps * caps)383 gst_text_render_fixate_caps (GstTextRender * render, GstCaps * caps)
384 {
385 GstStructure *s;
386
387 caps = gst_caps_truncate (caps);
388
389 caps = gst_caps_make_writable (caps);
390 s = gst_caps_get_structure (caps, 0);
391
392 GST_DEBUG ("Fixating caps %" GST_PTR_FORMAT, caps);
393 gst_structure_fixate_field_nearest_int (s, "width", MAX (render->image_width,
394 DEFAULT_RENDER_WIDTH));
395 gst_structure_fixate_field_nearest_int (s, "height",
396 MAX (render->image_height + render->ypad, DEFAULT_RENDER_HEIGHT));
397 caps = gst_caps_fixate (caps);
398 GST_DEBUG ("Fixated to %" GST_PTR_FORMAT, caps);
399
400 return caps;
401 }
402
403 #define CAIRO_UNPREMULTIPLY(a,r,g,b) G_STMT_START { \
404 b = (a > 0) ? MIN ((b * 255 + a / 2) / a, 255) : 0; \
405 g = (a > 0) ? MIN ((g * 255 + a / 2) / a, 255) : 0; \
406 r = (a > 0) ? MIN ((r * 255 + a / 2) / a, 255) : 0; \
407 } G_STMT_END
408
409 static void
gst_text_renderer_image_to_ayuv(GstTextRender * render,guchar * pixbuf,int xpos,int ypos,int stride)410 gst_text_renderer_image_to_ayuv (GstTextRender * render, guchar * pixbuf,
411 int xpos, int ypos, int stride)
412 {
413 int y; /* text bitmap coordinates */
414 guchar *p, *bitp;
415 guchar a, r, g, b;
416 int width, height;
417
418 width = render->image_width;
419 height = render->image_height;
420
421 for (y = 0; y < height && ypos + y < render->height; y++) {
422 int n;
423 p = pixbuf + (ypos + y) * stride + xpos * 4;
424 bitp = render->text_image + y * width * 4;
425 for (n = 0; n < width && n < render->width; n++) {
426 b = bitp[CAIRO_ARGB_B];
427 g = bitp[CAIRO_ARGB_G];
428 r = bitp[CAIRO_ARGB_R];
429 a = bitp[CAIRO_ARGB_A];
430 bitp += 4;
431
432 /* Cairo uses pre-multiplied ARGB, unpremultiply it */
433 CAIRO_UNPREMULTIPLY (a, r, g, b);
434
435 *p++ = a;
436 *p++ = CLAMP ((int) (((19595 * r) >> 16) + ((38470 * g) >> 16) +
437 ((7471 * b) >> 16)), 0, 255);
438 *p++ = CLAMP ((int) (-((11059 * r) >> 16) - ((21709 * g) >> 16) +
439 ((32768 * b) >> 16) + 128), 0, 255);
440 *p++ = CLAMP ((int) (((32768 * r) >> 16) - ((27439 * g) >> 16) -
441 ((5329 * b) >> 16) + 128), 0, 255);
442 }
443 }
444 }
445
446 static void
gst_text_renderer_image_to_argb(GstTextRender * render,guchar * pixbuf,int xpos,int ypos,int stride)447 gst_text_renderer_image_to_argb (GstTextRender * render, guchar * pixbuf,
448 int xpos, int ypos, int stride)
449 {
450 int i, j;
451 guchar *p, *bitp;
452 int width, height;
453
454 width = render->image_width;
455 height = render->image_height;
456
457 for (i = 0; i < height && ypos + i < render->height; i++) {
458 p = pixbuf + (ypos + i) * stride + xpos * 4;
459 bitp = render->text_image + i * width * 4;
460 for (j = 0; j < width && j < render->width; j++) {
461 p[0] = bitp[CAIRO_ARGB_A];
462 p[1] = bitp[CAIRO_ARGB_R];
463 p[2] = bitp[CAIRO_ARGB_G];
464 p[3] = bitp[CAIRO_ARGB_B];
465
466 /* Cairo uses pre-multiplied ARGB, unpremultiply it */
467 CAIRO_UNPREMULTIPLY (p[0], p[1], p[2], p[3]);
468
469 bitp += 4;
470 p += 4;
471 }
472 }
473 }
474
475 static GstFlowReturn
gst_text_render_renegotiate(GstTextRender * render)476 gst_text_render_renegotiate (GstTextRender * render)
477 {
478 GstCaps *caps = NULL, *padcaps;
479 GstFlowReturn ret = GST_FLOW_OK;
480
481 gst_text_render_check_argb (render);
482
483 padcaps = gst_pad_query_caps (render->srcpad, NULL);
484 caps = gst_pad_peer_query_caps (render->srcpad, padcaps);
485 gst_caps_unref (padcaps);
486
487 if (!caps || gst_caps_is_empty (caps)) {
488 GST_ELEMENT_ERROR (render, CORE, NEGOTIATION, (NULL), (NULL));
489 ret = GST_FLOW_ERROR;
490 goto done;
491 }
492
493 caps = gst_text_render_fixate_caps (render, caps);
494
495 if (!gst_text_render_src_setcaps (render, caps)) {
496 GST_ELEMENT_ERROR (render, CORE, NEGOTIATION, (NULL), (NULL));
497 ret = GST_FLOW_ERROR;
498 goto done;
499 }
500
501 done:
502 if (caps)
503 gst_caps_unref (caps);
504 return ret;
505 }
506
507 static GstFlowReturn
gst_text_render_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)508 gst_text_render_chain (GstPad * pad, GstObject * parent, GstBuffer * inbuf)
509 {
510 GstTextRender *render;
511 GstFlowReturn ret;
512 GstBuffer *outbuf;
513 GstMapInfo map;
514 guint8 *data;
515 gsize size;
516 gint n;
517 gint xpos, ypos;
518
519 render = GST_TEXT_RENDER (parent);
520
521 gst_buffer_map (inbuf, &map, GST_MAP_READ);
522 data = map.data;
523 size = map.size;
524
525 /* somehow pango barfs over "\0" buffers... */
526 while (size > 0 &&
527 (data[size - 1] == '\r' ||
528 data[size - 1] == '\n' || data[size - 1] == '\0')) {
529 size--;
530 }
531
532 /* render text */
533 GST_DEBUG ("rendering '%*s'", (gint) size, data);
534 pango_layout_set_markup (render->layout, (gchar *) data, size);
535 gst_text_render_render_pangocairo (render);
536 gst_buffer_unmap (inbuf, &map);
537
538 if (gst_pad_check_reconfigure (render->srcpad)
539 || !gst_pad_has_current_caps (render->srcpad)) {
540 ret = gst_text_render_renegotiate (render);
541 if (ret != GST_FLOW_OK)
542 goto done;
543 }
544
545 if (render->segment_event) {
546 gst_pad_push_event (render->srcpad, render->segment_event);
547 render->segment_event = NULL;
548 }
549
550 GST_DEBUG ("Allocating buffer WxH = %dx%d", render->width, render->height);
551 outbuf = gst_buffer_new_and_alloc (render->width * render->height * 4);
552
553 gst_buffer_copy_into (outbuf, inbuf, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
554
555 gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
556 data = map.data;
557 size = map.size;
558
559 if (render->use_ARGB) {
560 memset (data, 0, render->width * render->height * 4);
561 } else {
562 for (n = 0; n < render->width * render->height; n++) {
563 data[n * 4] = data[n * 4 + 1] = 0;
564 data[n * 4 + 2] = data[n * 4 + 3] = 128;
565 }
566 }
567
568 switch (render->halign) {
569 case GST_TEXT_RENDER_HALIGN_LEFT:
570 xpos = render->xpad;
571 break;
572 case GST_TEXT_RENDER_HALIGN_CENTER:
573 xpos = (render->width - render->image_width) / 2;
574 break;
575 case GST_TEXT_RENDER_HALIGN_RIGHT:
576 xpos = render->width - render->image_width - render->xpad;
577 break;
578 default:
579 xpos = 0;
580 }
581
582 switch (render->valign) {
583 case GST_TEXT_RENDER_VALIGN_BOTTOM:
584 ypos = render->height - render->image_height - render->ypad;
585 break;
586 case GST_TEXT_RENDER_VALIGN_BASELINE:
587 ypos = render->height - (render->image_height + render->ypad);
588 break;
589 case GST_TEXT_RENDER_VALIGN_TOP:
590 ypos = render->ypad;
591 break;
592 default:
593 ypos = render->ypad;
594 break;
595 }
596
597 if (render->text_image) {
598 if (render->use_ARGB) {
599 gst_text_renderer_image_to_argb (render, data, xpos, ypos,
600 render->width * 4);
601 } else {
602 gst_text_renderer_image_to_ayuv (render, data, xpos, ypos,
603 render->width * 4);
604 }
605 }
606 gst_buffer_unmap (outbuf, &map);
607
608 ret = gst_pad_push (render->srcpad, outbuf);
609
610 done:
611 gst_buffer_unref (inbuf);
612
613 return ret;
614 }
615
616 static gboolean
gst_text_render_event(GstPad * pad,GstObject * parent,GstEvent * event)617 gst_text_render_event (GstPad * pad, GstObject * parent, GstEvent * event)
618 {
619 GstTextRender *render = GST_TEXT_RENDER (parent);
620 gboolean ret = TRUE;
621
622 switch (GST_EVENT_TYPE (event)) {
623 case GST_EVENT_SEGMENT:
624 {
625 if (gst_pad_has_current_caps (render->srcpad)) {
626 ret = gst_pad_push_event (render->srcpad, event);
627 } else {
628 gst_event_replace (&render->segment_event, event);
629 gst_event_unref (event);
630 }
631 break;
632 }
633 default:
634 ret = gst_pad_push_event (render->srcpad, event);
635 break;
636 }
637
638 return ret;
639 }
640
641 static void
gst_text_render_finalize(GObject * object)642 gst_text_render_finalize (GObject * object)
643 {
644 GstTextRender *render = GST_TEXT_RENDER (object);
645
646 gst_event_replace (&render->segment_event, NULL);
647
648 g_free (render->text_image);
649
650 if (render->layout)
651 g_object_unref (render->layout);
652
653 if (render->pango_context)
654 g_object_unref (render->pango_context);
655
656 G_OBJECT_CLASS (parent_class)->finalize (object);
657 }
658
659 static void
gst_text_render_init(GstTextRender * render)660 gst_text_render_init (GstTextRender * render)
661 {
662 GstPadTemplate *template;
663 PangoFontMap *fontmap;
664
665 /* sink */
666 template = gst_static_pad_template_get (&sink_template_factory);
667 render->sinkpad = gst_pad_new_from_template (template, "sink");
668 gst_object_unref (template);
669 gst_pad_set_chain_function (render->sinkpad,
670 GST_DEBUG_FUNCPTR (gst_text_render_chain));
671 gst_pad_set_event_function (render->sinkpad,
672 GST_DEBUG_FUNCPTR (gst_text_render_event));
673
674 gst_element_add_pad (GST_ELEMENT (render), render->sinkpad);
675
676 /* source */
677 template = gst_static_pad_template_get (&src_template_factory);
678 render->srcpad = gst_pad_new_from_template (template, "src");
679 gst_object_unref (template);
680
681 gst_element_add_pad (GST_ELEMENT (render), render->srcpad);
682
683 fontmap = pango_cairo_font_map_new ();
684 render->pango_context =
685 pango_font_map_create_context (PANGO_FONT_MAP (fontmap));
686 g_object_unref (fontmap);
687
688 render->line_align = DEFAULT_PROP_LINE_ALIGNMENT;
689 render->layout = pango_layout_new (render->pango_context);
690 pango_layout_set_alignment (render->layout,
691 (PangoAlignment) render->line_align);
692
693 render->halign = DEFAULT_PROP_HALIGNMENT;
694 render->valign = DEFAULT_PROP_VALIGNMENT;
695 render->xpad = DEFAULT_PROP_XPAD;
696 render->ypad = DEFAULT_PROP_YPAD;
697
698 render->width = DEFAULT_RENDER_WIDTH;
699 render->height = DEFAULT_RENDER_HEIGHT;
700
701 render->use_ARGB = FALSE;
702 }
703
704 static void
gst_text_render_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)705 gst_text_render_set_property (GObject * object, guint prop_id,
706 const GValue * value, GParamSpec * pspec)
707 {
708 GstTextRender *render = GST_TEXT_RENDER (object);
709
710 switch (prop_id) {
711 case PROP_VALIGNMENT:
712 render->valign = g_value_get_enum (value);
713 break;
714 case PROP_HALIGNMENT:
715 render->halign = g_value_get_enum (value);
716 break;
717 case PROP_LINE_ALIGNMENT:
718 render->line_align = g_value_get_enum (value);
719 pango_layout_set_alignment (render->layout,
720 (PangoAlignment) render->line_align);
721 break;
722 case PROP_XPAD:
723 render->xpad = g_value_get_int (value);
724 break;
725 case PROP_YPAD:
726 render->ypad = g_value_get_int (value);
727 break;
728 case PROP_FONT_DESC:
729 {
730 PangoFontDescription *desc;
731
732 desc = pango_font_description_from_string (g_value_get_string (value));
733 if (desc) {
734 GST_LOG ("font description set: %s", g_value_get_string (value));
735 GST_OBJECT_LOCK (render);
736 pango_layout_set_font_description (render->layout, desc);
737 gst_text_render_adjust_values_with_fontdesc (render, desc);
738 pango_font_description_free (desc);
739 gst_text_render_render_pangocairo (render);
740 GST_OBJECT_UNLOCK (render);
741 } else {
742 GST_WARNING ("font description parse failed: %s",
743 g_value_get_string (value));
744 }
745 break;
746 }
747
748 default:
749 break;
750 }
751 }
752
753 static void
gst_text_render_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)754 gst_text_render_get_property (GObject * object, guint prop_id,
755 GValue * value, GParamSpec * pspec)
756 {
757 GstTextRender *render = GST_TEXT_RENDER (object);
758
759 switch (prop_id) {
760 case PROP_VALIGNMENT:
761 g_value_set_enum (value, render->valign);
762 break;
763 case PROP_HALIGNMENT:
764 g_value_set_enum (value, render->halign);
765 break;
766 case PROP_LINE_ALIGNMENT:
767 g_value_set_enum (value, render->line_align);
768 break;
769 case PROP_XPAD:
770 g_value_set_int (value, render->xpad);
771 break;
772 case PROP_YPAD:
773 g_value_set_int (value, render->ypad);
774 break;
775 default:
776 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
777 break;
778 }
779 }
780