2 * Copyright (c) 2008 Benjamin Schmitz <vortex@wolpzone.de>
3 * Copyright (c) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-assrender
24 * Renders timestamped SSA/ASS subtitles on top of a video stream.
27 * <title>Example launch line</title>
29 * gst-launch -v filesrc location=/path/to/mkv ! matroskademux name=d ! queue ! mp3parse ! mad ! audioconvert ! autoaudiosink d. ! queue ! ffdec_h264 ! ffmpegcolorspace ! r. d. ! queue ! "application/x-ass" ! assrender name=r ! ffmpegcolorspace ! autovideosink
30 * ]| This pipeline demuxes a Matroska file with h.264 video, MP3 audio and embedded ASS subtitles and renders the subtitles on top of the video.
39 #include "gstassrender.h"
43 GST_DEBUG_CATEGORY_STATIC (gst_ass_render_debug);
44 GST_DEBUG_CATEGORY_STATIC (gst_ass_render_lib_debug);
45 #define GST_CAT_DEFAULT gst_ass_render_debug
47 /* Filter signals and props */
60 #define FORMATS "{ RGB, BGR, xRGB, xBGR, RGBx, BGRx, I420 }"
62 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
65 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
68 static GstStaticPadTemplate video_sink_factory =
69 GST_STATIC_PAD_TEMPLATE ("video_sink",
72 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (FORMATS))
75 static GstStaticPadTemplate text_sink_factory =
76 GST_STATIC_PAD_TEMPLATE ("text_sink",
79 GST_STATIC_CAPS ("application/x-ass; application/x-ssa")
82 static void gst_ass_render_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_ass_render_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
87 static void gst_ass_render_finalize (GObject * object);
89 static GstStateChangeReturn gst_ass_render_change_state (GstElement * element,
90 GstStateChange transition);
92 #define gst_ass_render_parent_class parent_class
93 G_DEFINE_TYPE (GstAssRender, gst_ass_render, GST_TYPE_ELEMENT);
95 static GstCaps *gst_ass_render_getcaps (GstPad * pad, GstCaps * filter);
97 static gboolean gst_ass_render_setcaps_video (GstPad * pad, GstCaps * caps);
98 static gboolean gst_ass_render_setcaps_text (GstPad * pad, GstCaps * caps);
100 static GstFlowReturn gst_ass_render_chain_video (GstPad * pad,
101 GstObject * parent, GstBuffer * buf);
102 static GstFlowReturn gst_ass_render_chain_text (GstPad * pad,
103 GstObject * parent, GstBuffer * buf);
105 static gboolean gst_ass_render_event_video (GstPad * pad, GstObject * parent,
107 static gboolean gst_ass_render_event_text (GstPad * pad, GstObject * parent,
109 static gboolean gst_ass_render_event_src (GstPad * pad, GstObject * parent,
112 static gboolean gst_ass_render_query_video (GstPad * pad, GstObject * parent,
114 static gboolean gst_ass_render_query_src (GstPad * pad, GstObject * parent,
117 /* initialize the plugin's class */
119 gst_ass_render_class_init (GstAssRenderClass * klass)
121 GObjectClass *gobject_class = (GObjectClass *) klass;
122 GstElementClass *gstelement_class = (GstElementClass *) klass;
124 gobject_class->set_property = gst_ass_render_set_property;
125 gobject_class->get_property = gst_ass_render_get_property;
126 gobject_class->finalize = gst_ass_render_finalize;
128 g_object_class_install_property (gobject_class, PROP_ENABLE,
129 g_param_spec_boolean ("enable", "Enable",
130 "Enable rendering of subtitles", TRUE,
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132 g_object_class_install_property (gobject_class, PROP_EMBEDDEDFONTS,
133 g_param_spec_boolean ("embeddedfonts", "Embedded Fonts",
134 "Extract and use fonts embedded in the stream", TRUE,
135 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
137 gstelement_class->change_state =
138 GST_DEBUG_FUNCPTR (gst_ass_render_change_state);
140 gst_element_class_add_pad_template (gstelement_class,
141 gst_static_pad_template_get (&src_factory));
142 gst_element_class_add_pad_template (gstelement_class,
143 gst_static_pad_template_get (&video_sink_factory));
144 gst_element_class_add_pad_template (gstelement_class,
145 gst_static_pad_template_get (&text_sink_factory));
147 gst_element_class_set_details_simple (gstelement_class, "ASS/SSA Render",
148 "Mixer/Video/Overlay/Subtitle",
149 "Renders ASS/SSA subtitles with libass",
150 "Benjamin Schmitz <vortex@wolpzone.de>, "
151 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
154 #if defined(LIBASS_VERSION) && LIBASS_VERSION >= 0x00907000
156 _libass_message_cb (gint level, const gchar * fmt, va_list args,
159 gchar *message = g_strdup_vprintf (fmt, args);
162 GST_CAT_ERROR_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
164 GST_CAT_WARNING_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
166 GST_CAT_INFO_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
168 GST_CAT_DEBUG_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
170 GST_CAT_LOG_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
177 gst_ass_render_init (GstAssRender * render)
179 GST_DEBUG_OBJECT (render, "init");
181 render->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
182 render->video_sinkpad =
183 gst_pad_new_from_static_template (&video_sink_factory, "video_sink");
184 render->text_sinkpad =
185 gst_pad_new_from_static_template (&text_sink_factory, "text_sink");
187 gst_pad_set_chain_function (render->video_sinkpad,
188 GST_DEBUG_FUNCPTR (gst_ass_render_chain_video));
189 gst_pad_set_chain_function (render->text_sinkpad,
190 GST_DEBUG_FUNCPTR (gst_ass_render_chain_text));
192 gst_pad_set_event_function (render->video_sinkpad,
193 GST_DEBUG_FUNCPTR (gst_ass_render_event_video));
194 gst_pad_set_event_function (render->text_sinkpad,
195 GST_DEBUG_FUNCPTR (gst_ass_render_event_text));
196 gst_pad_set_event_function (render->srcpad,
197 GST_DEBUG_FUNCPTR (gst_ass_render_event_src));
199 gst_pad_set_query_function (render->srcpad,
200 GST_DEBUG_FUNCPTR (gst_ass_render_query_src));
201 gst_pad_set_query_function (render->video_sinkpad,
202 GST_DEBUG_FUNCPTR (gst_ass_render_query_video));
204 gst_element_add_pad (GST_ELEMENT (render), render->srcpad);
205 gst_element_add_pad (GST_ELEMENT (render), render->video_sinkpad);
206 gst_element_add_pad (GST_ELEMENT (render), render->text_sinkpad);
208 gst_video_info_init (&render->info);
210 g_mutex_init (&render->subtitle_mutex);
211 g_cond_init (&render->subtitle_cond);
213 render->renderer_init_ok = FALSE;
214 render->track_init_ok = FALSE;
215 render->enable = TRUE;
216 render->embeddedfonts = TRUE;
218 gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
219 gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
221 g_mutex_init (&render->ass_mutex);
222 render->ass_library = ass_library_init ();
223 #if defined(LIBASS_VERSION) && LIBASS_VERSION >= 0x00907000
224 ass_set_message_cb (render->ass_library, _libass_message_cb, render);
226 ass_set_extract_fonts (render->ass_library, 1);
228 render->ass_renderer = ass_renderer_init (render->ass_library);
229 if (!render->ass_renderer) {
230 GST_WARNING_OBJECT (render, "cannot create renderer instance");
231 g_assert_not_reached ();
234 render->ass_track = NULL;
236 GST_DEBUG_OBJECT (render, "init complete");
240 gst_ass_render_finalize (GObject * object)
242 GstAssRender *render = GST_ASS_RENDER (object);
244 g_mutex_clear (&render->subtitle_mutex);
245 g_cond_clear (&render->subtitle_cond);
247 if (render->ass_track) {
248 ass_free_track (render->ass_track);
251 if (render->ass_renderer) {
252 ass_renderer_done (render->ass_renderer);
255 if (render->ass_library) {
256 ass_library_done (render->ass_library);
259 g_mutex_clear (&render->ass_mutex);
261 G_OBJECT_CLASS (parent_class)->finalize (object);
265 gst_ass_render_set_property (GObject * object, guint prop_id,
266 const GValue * value, GParamSpec * pspec)
268 GstAssRender *render = GST_ASS_RENDER (object);
272 render->enable = g_value_get_boolean (value);
274 case PROP_EMBEDDEDFONTS:
275 render->embeddedfonts = g_value_get_boolean (value);
276 g_mutex_lock (&render->ass_mutex);
277 ass_set_extract_fonts (render->ass_library, render->embeddedfonts);
278 g_mutex_unlock (&render->ass_mutex);
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 gst_ass_render_get_property (GObject * object, guint prop_id,
288 GValue * value, GParamSpec * pspec)
290 GstAssRender *render = GST_ASS_RENDER (object);
294 g_value_set_boolean (value, render->enable);
296 case PROP_EMBEDDEDFONTS:
297 g_value_set_boolean (value, render->embeddedfonts);
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 static GstStateChangeReturn
306 gst_ass_render_change_state (GstElement * element, GstStateChange transition)
308 GstAssRender *render = GST_ASS_RENDER (element);
309 GstStateChangeReturn ret;
311 switch (transition) {
312 case GST_STATE_CHANGE_READY_TO_PAUSED:
313 render->subtitle_flushing = FALSE;
314 gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
315 gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
317 case GST_STATE_CHANGE_NULL_TO_READY:
318 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
322 case GST_STATE_CHANGE_PAUSED_TO_READY:
323 g_mutex_lock (&render->subtitle_mutex);
324 render->subtitle_flushing = TRUE;
325 if (render->subtitle_pending)
326 gst_buffer_unref (render->subtitle_pending);
327 render->subtitle_pending = NULL;
328 g_cond_signal (&render->subtitle_cond);
329 g_mutex_unlock (&render->subtitle_mutex);
333 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
335 switch (transition) {
336 case GST_STATE_CHANGE_PAUSED_TO_READY:
337 g_mutex_lock (&render->ass_mutex);
338 if (render->ass_track)
339 ass_free_track (render->ass_track);
340 render->ass_track = NULL;
341 g_mutex_unlock (&render->ass_mutex);
342 render->track_init_ok = FALSE;
343 render->renderer_init_ok = FALSE;
345 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
346 case GST_STATE_CHANGE_READY_TO_NULL:
356 gst_ass_render_query_src (GstPad * pad, GstObject * parent, GstQuery * query)
358 gboolean res = FALSE;
360 switch (GST_QUERY_TYPE (query)) {
363 GstCaps *filter, *caps;
365 gst_query_parse_caps (query, &filter);
366 caps = gst_ass_render_getcaps (pad, filter);
367 gst_query_set_caps_result (query, caps);
368 gst_caps_unref (caps);
373 res = gst_pad_query_default (pad, parent, query);
381 gst_ass_render_event_src (GstPad * pad, GstObject * parent, GstEvent * event)
383 GstAssRender *render = GST_ASS_RENDER (parent);
384 gboolean ret = FALSE;
386 switch (GST_EVENT_TYPE (event)) {
387 case GST_EVENT_SEEK:{
390 GST_DEBUG_OBJECT (render, "seek received, driving from here");
392 gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL, NULL);
394 /* Flush downstream, only for flushing seek */
395 if (flags & GST_SEEK_FLAG_FLUSH)
396 gst_pad_push_event (render->srcpad, gst_event_new_flush_start ());
398 /* Mark subtitle as flushing, unblocks chains */
399 g_mutex_lock (&render->subtitle_mutex);
400 if (render->subtitle_pending)
401 gst_buffer_unref (render->subtitle_pending);
402 render->subtitle_pending = NULL;
403 render->subtitle_flushing = TRUE;
404 g_cond_signal (&render->subtitle_cond);
405 g_mutex_unlock (&render->subtitle_mutex);
407 /* Seek on each sink pad */
408 gst_event_ref (event);
409 ret = gst_pad_push_event (render->video_sinkpad, event);
411 ret = gst_pad_push_event (render->text_sinkpad, event);
413 gst_event_unref (event);
418 gst_event_ref (event);
419 ret = gst_pad_push_event (render->video_sinkpad, event);
420 gst_pad_push_event (render->text_sinkpad, event);
428 gst_ass_render_getcaps (GstPad * pad, GstCaps * filter)
430 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
434 if (pad == render->srcpad)
435 otherpad = render->video_sinkpad;
437 otherpad = render->srcpad;
439 /* we can do what the peer can */
440 caps = gst_pad_peer_query_caps (otherpad, filter);
443 const GstCaps *templ;
445 /* filtered against our padtemplate */
446 templ = gst_pad_get_pad_template_caps (otherpad);
447 temp = gst_caps_intersect (caps, templ);
448 gst_caps_unref (caps);
449 /* this is what we can do */
452 /* no peer, our padtemplate is enough then */
453 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
456 gst_object_unref (render);
461 #define CREATE_RGB_BLIT_FUNCTION(name,bpp,R,G,B) \
463 blit_##name (GstAssRender * render, ASS_Image * ass_image, GstVideoFrame * frame) \
466 gint alpha, r, g, b, k; \
468 guint8 *dst, *data; \
476 width = GST_VIDEO_FRAME_WIDTH (frame); \
477 height = GST_VIDEO_FRAME_HEIGHT (frame); \
478 dst_stride = GST_VIDEO_FRAME_PLANE_STRIDE (frame, 0); \
479 data = GST_VIDEO_FRAME_PLANE_DATA (frame, 0); \
481 while (ass_image) { \
482 if (ass_image->dst_y > height || ass_image->dst_x > width) \
485 /* blend subtitles onto the video frame */ \
486 alpha = 255 - ((ass_image->color) & 0xff); \
487 r = ((ass_image->color) >> 24) & 0xff; \
488 g = ((ass_image->color) >> 16) & 0xff; \
489 b = ((ass_image->color) >> 8) & 0xff; \
490 src = ass_image->bitmap; \
491 dst = data + ass_image->dst_y * dst_stride + ass_image->dst_x * bpp; \
493 w = MIN (ass_image->w, width - ass_image->dst_x); \
494 h = MIN (ass_image->h, height - ass_image->dst_y); \
495 src_skip = ass_image->stride - w; \
496 dst_skip = dst_stride - w * bpp; \
498 for (y = 0; y < h; y++) { \
499 for (x = 0; x < w; x++) { \
500 k = src[0] * alpha / 255; \
501 dst[R] = (k * r + (255 - k) * dst[R]) / 255; \
502 dst[G] = (k * g + (255 - k) * dst[G]) / 255; \
503 dst[B] = (k * b + (255 - k) * dst[B]) / 255; \
512 ass_image = ass_image->next; \
514 GST_LOG_OBJECT (render, "amount of rendered ass_image: %u", counter); \
517 CREATE_RGB_BLIT_FUNCTION (rgb, 3, 0, 1, 2);
518 CREATE_RGB_BLIT_FUNCTION (bgr, 3, 2, 1, 0);
519 CREATE_RGB_BLIT_FUNCTION (xrgb, 4, 1, 2, 3);
520 CREATE_RGB_BLIT_FUNCTION (xbgr, 4, 3, 2, 1);
521 CREATE_RGB_BLIT_FUNCTION (rgbx, 4, 0, 1, 2);
522 CREATE_RGB_BLIT_FUNCTION (bgrx, 4, 2, 1, 0);
524 #undef CREATE_RGB_BLIT_FUNCTION
527 rgb_to_y (gint r, gint g, gint b)
531 ret = (gint) (((19595 * r) >> 16) + ((38470 * g) >> 16) + ((7471 * b) >> 16));
532 ret = CLAMP (ret, 0, 255);
537 rgb_to_u (gint r, gint g, gint b)
542 (gint) (-((11059 * r) >> 16) - ((21709 * g) >> 16) + ((32768 * b) >> 16) +
544 ret = CLAMP (ret, 0, 255);
549 rgb_to_v (gint r, gint g, gint b)
554 (gint) (((32768 * r) >> 16) - ((27439 * g) >> 16) - ((5329 * b) >> 16) +
556 ret = CLAMP (ret, 0, 255);
561 blit_i420 (GstAssRender * render, ASS_Image * ass_image, GstVideoFrame * frame)
564 gint alpha, r, g, b, k, k2;
567 guint8 *dst_y, *dst_u, *dst_v;
569 /* FIXME ignoring source image stride might be wrong here */
575 guint8 *y_data, *u_data, *v_data;
576 gint y_stride, u_stride, v_stride;
578 width = GST_VIDEO_FRAME_WIDTH (frame);
579 height = GST_VIDEO_FRAME_HEIGHT (frame);
581 y_data = GST_VIDEO_FRAME_COMP_DATA (frame, 0);
582 u_data = GST_VIDEO_FRAME_COMP_DATA (frame, 1);
583 v_data = GST_VIDEO_FRAME_COMP_DATA (frame, 2);
585 y_stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 0);
586 u_stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 1);
587 v_stride = GST_VIDEO_FRAME_COMP_STRIDE (frame, 2);
590 if (ass_image->dst_y > height || ass_image->dst_x > width)
593 /* blend subtitles onto the video frame */
594 alpha = 255 - ((ass_image->color) & 0xff);
595 r = ((ass_image->color) >> 24) & 0xff;
596 g = ((ass_image->color) >> 16) & 0xff;
597 b = ((ass_image->color) >> 8) & 0xff;
599 Y = rgb_to_y (r, g, b);
600 U = rgb_to_u (r, g, b);
601 V = rgb_to_v (r, g, b);
603 w = MIN (ass_image->w, width - ass_image->dst_x);
604 h = MIN (ass_image->h, height - ass_image->dst_y);
609 src_stride = ass_image->stride;
612 src = ass_image->bitmap;
614 dst_y = y_data + ass_image->dst_y * y_stride + ass_image->dst_x;
615 dst_u = u_data + (ass_image->dst_y / 2) * u_stride + ass_image->dst_x / 2;
616 dst_v = v_data + (ass_image->dst_y / 2) * v_stride + ass_image->dst_x / 2;
619 for (y = 0; y < h; y++) {
620 dst_y = y_data + (ass_image->dst_y + y) * y_stride + ass_image->dst_x;
621 for (x = 0; x < w; x++) {
622 k = src[y * ass_image->w + x] * alpha / 255;
623 dst_y[x] = (k * Y + (255 - k) * dst_y[x]) / 255;
628 if (ass_image->dst_y & 1) {
629 dst_u = u_data + (ass_image->dst_y / 2) * u_stride + ass_image->dst_x / 2;
630 dst_v = v_data + (ass_image->dst_y / 2) * v_stride + ass_image->dst_x / 2;
632 if (ass_image->dst_x & 1) {
633 k2 = src[y * ass_image->w + x] * alpha / 255;
635 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
636 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
641 for (; x < w - 1; x += 2) {
642 k2 = src[y * ass_image->w + x] * alpha / 255;
643 k2 += src[y * ass_image->w + x + 1] * alpha / 255;
645 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
646 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
651 k2 = src[y * ass_image->w + x] * alpha / 255;
653 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
654 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
658 for (; y < h - 1; y += 2) {
659 dst_u = u_data + ((ass_image->dst_y + y) / 2) * u_stride +
660 ass_image->dst_x / 2;
661 dst_v = v_data + ((ass_image->dst_y + y) / 2) * v_stride +
662 ass_image->dst_x / 2;
664 if (ass_image->dst_x & 1) {
665 k2 = src[y * ass_image->w + x] * alpha / 255;
666 k2 += src[(y + 1) * ass_image->w + x] * alpha / 255;
668 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
669 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
674 for (; x < w - 1; x += 2) {
675 k2 = src[y * ass_image->w + x] * alpha / 255;
676 k2 += src[y * ass_image->w + x + 1] * alpha / 255;
677 k2 += src[(y + 1) * ass_image->w + x] * alpha / 255;
678 k2 += src[(y + 1) * ass_image->w + x + 1] * alpha / 255;
680 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
681 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
686 k2 = src[y * ass_image->w + x] * alpha / 255;
687 k2 += src[(y + 1) * ass_image->w + x] * alpha / 255;
689 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
690 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
695 dst_u = u_data + (ass_image->dst_y / 2) * u_stride + ass_image->dst_x / 2;
696 dst_v = v_data + (ass_image->dst_y / 2) * v_stride + ass_image->dst_x / 2;
698 if (ass_image->dst_x & 1) {
699 k2 = src[y * ass_image->w + x] * alpha / 255;
701 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
702 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
707 for (; x < w - 1; x += 2) {
708 k2 = src[y * ass_image->w + x] * alpha / 255;
709 k2 += src[y * ass_image->w + x + 1] * alpha / 255;
711 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
712 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
717 k2 = src[y * ass_image->w + x] * alpha / 255;
719 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
720 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
728 ass_image = ass_image->next;
731 GST_LOG_OBJECT (render, "amount of rendered ass_image: %u", counter);
735 gst_ass_render_setcaps_video (GstPad * pad, GstCaps * caps)
737 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
738 gboolean ret = FALSE;
739 gint par_n = 1, par_d = 1;
743 if (!gst_video_info_from_caps (&info, caps))
748 ret = gst_pad_set_caps (render->srcpad, caps);
752 switch (GST_VIDEO_INFO_FORMAT (&info)) {
753 case GST_VIDEO_FORMAT_RGB:
754 render->blit = blit_rgb;
756 case GST_VIDEO_FORMAT_BGR:
757 render->blit = blit_bgr;
759 case GST_VIDEO_FORMAT_xRGB:
760 render->blit = blit_xrgb;
762 case GST_VIDEO_FORMAT_xBGR:
763 render->blit = blit_xbgr;
765 case GST_VIDEO_FORMAT_RGBx:
766 render->blit = blit_rgbx;
768 case GST_VIDEO_FORMAT_BGRx:
769 render->blit = blit_bgrx;
771 case GST_VIDEO_FORMAT_I420:
772 render->blit = blit_i420;
779 g_mutex_lock (&render->ass_mutex);
780 ass_set_frame_size (render->ass_renderer, info.width, info.height);
782 dar = (((gdouble) par_n) * ((gdouble) info.width))
783 / (((gdouble) par_d) * ((gdouble) info.height));
784 #if !defined(LIBASS_VERSION) || LIBASS_VERSION < 0x00907000
785 ass_set_aspect_ratio (render->ass_renderer, dar);
787 ass_set_aspect_ratio (render->ass_renderer,
788 dar, ((gdouble) info.width) / ((gdouble) info.height));
790 ass_set_font_scale (render->ass_renderer, 1.0);
791 ass_set_hinting (render->ass_renderer, ASS_HINTING_LIGHT);
793 #if !defined(LIBASS_VERSION) || LIBASS_VERSION < 0x00907000
794 ass_set_fonts (render->ass_renderer, "Arial", "sans-serif");
795 ass_set_fonts (render->ass_renderer, NULL, "Sans");
797 ass_set_fonts (render->ass_renderer, "Arial", "sans-serif", 1, NULL, 1);
798 ass_set_fonts (render->ass_renderer, NULL, "Sans", 1, NULL, 1);
800 ass_set_margins (render->ass_renderer, 0, 0, 0, 0);
801 ass_set_use_margins (render->ass_renderer, 0);
802 g_mutex_unlock (&render->ass_mutex);
804 render->renderer_init_ok = TRUE;
806 GST_DEBUG_OBJECT (render, "ass renderer setup complete");
809 gst_object_unref (render);
816 GST_ERROR_OBJECT (render, "Can't parse caps: %" GST_PTR_FORMAT, caps);
823 gst_ass_render_setcaps_text (GstPad * pad, GstCaps * caps)
825 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
826 GstStructure *structure;
830 gboolean ret = FALSE;
832 structure = gst_caps_get_structure (caps, 0);
834 GST_DEBUG_OBJECT (render, "text pad linked with caps: %" GST_PTR_FORMAT,
837 value = gst_structure_get_value (structure, "codec_data");
839 g_mutex_lock (&render->ass_mutex);
841 priv = gst_value_get_buffer (value);
842 g_return_val_if_fail (priv != NULL, FALSE);
844 gst_buffer_map (priv, &map, GST_MAP_READ);
846 if (!render->ass_track)
847 render->ass_track = ass_new_track (render->ass_library);
849 ass_process_codec_private (render->ass_track, (char *) map.data, map.size);
851 gst_buffer_unmap (priv, &map);
853 GST_DEBUG_OBJECT (render, "ass track created");
855 render->track_init_ok = TRUE;
858 } else if (!render->ass_track) {
859 render->ass_track = ass_new_track (render->ass_library);
861 render->track_init_ok = TRUE;
865 g_mutex_unlock (&render->ass_mutex);
867 gst_object_unref (render);
874 gst_ass_render_process_text (GstAssRender * render, GstBuffer * buffer,
875 GstClockTime running_time, GstClockTime duration)
878 gdouble pts_start, pts_end;
880 pts_start = running_time;
881 pts_start /= GST_MSECOND;
883 pts_end /= GST_MSECOND;
885 GST_DEBUG_OBJECT (render,
886 "Processing subtitles with running time %" GST_TIME_FORMAT
887 " and duration %" GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
888 GST_TIME_ARGS (duration));
890 gst_buffer_map (buffer, &map, GST_MAP_READ);
892 g_mutex_lock (&render->ass_mutex);
893 ass_process_chunk (render->ass_track, (gchar *) map.data, map.size,
895 g_mutex_unlock (&render->ass_mutex);
897 gst_buffer_unmap (buffer, &map);
898 gst_buffer_unref (buffer);
902 gst_ass_render_chain_video (GstPad * pad, GstObject * parent,
905 GstAssRender *render = GST_ASS_RENDER (parent);
906 GstFlowReturn ret = GST_FLOW_OK;
907 gboolean in_seg = FALSE;
908 guint64 start, stop, clip_start = 0, clip_stop = 0;
909 ASS_Image *ass_image;
911 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
912 GST_WARNING_OBJECT (render, "buffer without timestamp, discarding");
913 gst_buffer_unref (buffer);
917 /* ignore buffers that are outside of the current segment */
918 start = GST_BUFFER_TIMESTAMP (buffer);
920 if (!GST_BUFFER_DURATION_IS_VALID (buffer)) {
921 stop = GST_CLOCK_TIME_NONE;
923 stop = start + GST_BUFFER_DURATION (buffer);
926 /* segment_clip() will adjust start unconditionally to segment_start if
927 * no stop time is provided, so handle this ourselves */
928 if (stop == GST_CLOCK_TIME_NONE && start < render->video_segment.start)
932 gst_segment_clip (&render->video_segment, GST_FORMAT_TIME, start, stop,
933 &clip_start, &clip_stop);
938 /* if the buffer is only partially in the segment, fix up stamps */
939 if (clip_start != start || (stop != -1 && clip_stop != stop)) {
940 GST_DEBUG_OBJECT (render, "clipping buffer timestamp/duration to segment");
941 buffer = gst_buffer_make_writable (buffer);
942 GST_BUFFER_TIMESTAMP (buffer) = clip_start;
944 GST_BUFFER_DURATION (buffer) = clip_stop - clip_start;
947 render->video_segment.position = clip_start;
949 g_mutex_lock (&render->subtitle_mutex);
950 if (render->subtitle_pending) {
951 GstClockTime sub_running_time, vid_running_time;
952 GstClockTime sub_running_time_end, vid_running_time_end;
955 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
956 GST_BUFFER_TIMESTAMP (render->subtitle_pending));
957 sub_running_time_end =
958 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
959 GST_BUFFER_TIMESTAMP (render->subtitle_pending) +
960 GST_BUFFER_DURATION (render->subtitle_pending));
962 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
963 GST_BUFFER_TIMESTAMP (buffer));
964 vid_running_time_end =
965 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
966 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer));
968 if (sub_running_time_end < vid_running_time) {
969 gst_buffer_unref (render->subtitle_pending);
970 GST_DEBUG_OBJECT (render,
971 "Too late text buffer, dropping (%" GST_TIME_FORMAT " < %"
972 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time_end),
973 GST_TIME_ARGS (vid_running_time));
974 render->subtitle_pending = NULL;
975 g_cond_signal (&render->subtitle_cond);
976 } else if (sub_running_time <= vid_running_time_end + GST_SECOND / 2) {
977 gst_ass_render_process_text (render, render->subtitle_pending,
978 sub_running_time, sub_running_time_end - sub_running_time);
979 render->subtitle_pending = NULL;
980 g_cond_signal (&render->subtitle_cond);
983 g_mutex_unlock (&render->subtitle_mutex);
985 /* now start rendering subtitles, if all conditions are met */
986 if (render->renderer_init_ok && render->track_init_ok && render->enable) {
987 GstClockTime running_time;
989 #ifndef GST_DISABLE_GST_DEBUG
994 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
995 GST_BUFFER_TIMESTAMP (buffer));
996 GST_DEBUG_OBJECT (render,
997 "rendering frame for running time %" GST_TIME_FORMAT,
998 GST_TIME_ARGS (running_time));
999 /* libass needs timestamps in ms */
1000 timestamp = running_time / GST_MSECOND;
1002 g_mutex_lock (&render->ass_mutex);
1003 #ifndef GST_DISABLE_GST_DEBUG
1004 /* only for testing right now. could possibly be used for optimizations? */
1005 step = ass_step_sub (render->ass_track, timestamp, 1);
1006 GST_DEBUG_OBJECT (render, "Current running time: %" GST_TIME_FORMAT
1007 " // Next event: %" GST_TIME_FORMAT,
1008 GST_TIME_ARGS (running_time), GST_TIME_ARGS (step * GST_MSECOND));
1011 /* not sure what the last parameter to this call is for (detect_change) */
1012 ass_image = ass_render_frame (render->ass_renderer, render->ass_track,
1014 g_mutex_unlock (&render->ass_mutex);
1016 if (ass_image != NULL) {
1017 GstVideoFrame frame;
1019 buffer = gst_buffer_make_writable (buffer);
1021 gst_video_frame_map (&frame, &render->info, buffer, GST_MAP_WRITE);
1022 render->blit (render, ass_image, &frame);
1023 gst_video_frame_unmap (&frame);
1025 GST_LOG_OBJECT (render, "nothing to render right now");
1028 GST_LOG_OBJECT (render, "rendering disabled, doing buffer passthrough");
1031 ret = gst_pad_push (render->srcpad, buffer);
1037 GST_DEBUG_OBJECT (render, "buffer out of segment, discarding");
1038 gst_buffer_unref (buffer);
1043 static GstFlowReturn
1044 gst_ass_render_chain_text (GstPad * pad, GstObject * parent, GstBuffer * buffer)
1046 GstFlowReturn ret = GST_FLOW_OK;
1047 GstAssRender *render = GST_ASS_RENDER (parent);
1048 GstClockTime timestamp, duration;
1049 GstClockTime sub_running_time, vid_running_time;
1050 GstClockTime sub_running_time_end;
1051 guint64 cstart, cstop;
1054 if (render->subtitle_flushing) {
1055 gst_buffer_unref (buffer);
1056 return GST_FLOW_FLUSHING;
1059 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1060 duration = GST_BUFFER_DURATION (buffer);
1062 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)
1063 || !GST_CLOCK_TIME_IS_VALID (duration))) {
1064 GST_WARNING_OBJECT (render,
1065 "Text buffer without valid timestamp" " or duration, dropping");
1066 gst_buffer_unref (buffer);
1071 gst_segment_clip (&render->subtitle_segment, GST_FORMAT_TIME, timestamp,
1072 timestamp + duration, &cstart, &cstop);
1074 GST_DEBUG_OBJECT (render,
1075 "Text buffer before segment start (%" GST_TIME_FORMAT " < %"
1076 GST_TIME_FORMAT ")", GST_TIME_ARGS (timestamp),
1077 GST_TIME_ARGS (render->subtitle_segment.start));
1078 gst_buffer_unref (buffer);
1082 GST_BUFFER_TIMESTAMP (buffer) = timestamp = cstart;
1083 GST_BUFFER_DURATION (buffer) = duration = cstop - cstart;
1085 render->subtitle_segment.position = GST_BUFFER_TIMESTAMP (buffer);
1088 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
1090 sub_running_time_end =
1091 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
1092 timestamp + duration);
1094 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
1095 render->video_segment.position);
1097 if (render->info.fps_n && render->info.fps_d)
1099 gst_util_uint64_scale (GST_SECOND, render->info.fps_d,
1100 render->info.fps_n);
1102 if (sub_running_time > vid_running_time + GST_SECOND / 2) {
1103 g_assert (render->subtitle_pending == NULL);
1104 g_mutex_lock (&render->subtitle_mutex);
1105 if (G_UNLIKELY (render->subtitle_flushing)) {
1106 GST_DEBUG_OBJECT (render, "Text pad flushing");
1107 gst_buffer_unref (buffer);
1108 g_mutex_unlock (&render->subtitle_mutex);
1109 return GST_FLOW_FLUSHING;
1111 GST_DEBUG_OBJECT (render,
1112 "Too early text buffer, waiting (%" GST_TIME_FORMAT " > %"
1113 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time),
1114 GST_TIME_ARGS (vid_running_time));
1115 render->subtitle_pending = buffer;
1116 g_cond_wait (&render->subtitle_cond, &render->subtitle_mutex);
1117 g_mutex_unlock (&render->subtitle_mutex);
1118 } else if (sub_running_time_end < vid_running_time) {
1119 GST_DEBUG_OBJECT (render,
1120 "Too late text buffer, dropping (%" GST_TIME_FORMAT " < %"
1121 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time_end),
1122 GST_TIME_ARGS (vid_running_time));
1123 gst_buffer_unref (buffer);
1126 gst_ass_render_process_text (render, buffer, sub_running_time,
1127 sub_running_time_end - sub_running_time);
1131 GST_DEBUG_OBJECT (render,
1132 "processed text packet with timestamp %" GST_TIME_FORMAT
1133 " and duration %" GST_TIME_FORMAT,
1134 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1140 gst_ass_render_handle_tags (GstAssRender * render, GstTagList * taglist)
1143 static const gchar *mimetypes[] = {
1144 "application/x-font-ttf",
1145 "application/x-font-otf",
1146 "application/x-truetype-font"
1148 static const gchar *extensions[] = {
1158 tag_size = gst_tag_list_get_tag_size (taglist, GST_TAG_ATTACHMENT);
1159 if (tag_size > 0 && render->embeddedfonts) {
1161 const GValue *value;
1164 GstStructure *structure;
1165 gboolean valid_mimetype, valid_extension;
1167 const gchar *filename;
1171 GST_DEBUG_OBJECT (render, "TAG event has attachments");
1173 for (index = 0; index < tag_size; index++) {
1175 value = gst_tag_list_get_value_index (taglist, GST_TAG_ATTACHMENT, index);
1176 buf = gst_value_get_buffer (value);
1177 if (!buf || !GST_BUFFER_CAPS (buf))
1180 caps = GST_BUFFER_CAPS (buf);
1181 structure = gst_caps_get_structure (caps, 0);
1183 valid_mimetype = FALSE;
1184 valid_extension = FALSE;
1186 for (j = 0; j < G_N_ELEMENTS (mimetypes); j++) {
1187 if (gst_structure_has_name (structure, mimetypes[j])) {
1188 valid_mimetype = TRUE;
1192 filename = gst_structure_get_string (structure, "filename");
1196 if (!valid_mimetype) {
1197 guint len = strlen (filename);
1198 const gchar *extension = filename + len - 4;
1199 for (j = 0; j < G_N_ELEMENTS (extensions); j++) {
1200 if (g_ascii_strcasecmp (extension, extensions[j]) == 0) {
1201 valid_extension = TRUE;
1207 if (valid_mimetype || valid_extension) {
1208 g_mutex_lock (&render->ass_mutex);
1209 ass_add_font (render->ass_library, (gchar *) filename,
1210 (gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1211 GST_DEBUG_OBJECT (render, "registered new font %s", filename);
1212 g_mutex_unlock (&render->ass_mutex);
1220 gst_ass_render_event_video (GstPad * pad, GstObject * parent, GstEvent * event)
1222 gboolean ret = FALSE;
1223 GstAssRender *render = GST_ASS_RENDER (parent);
1225 GST_DEBUG_OBJECT (pad, "received video event %s",
1226 GST_EVENT_TYPE_NAME (event));
1228 switch (GST_EVENT_TYPE (event)) {
1229 case GST_EVENT_CAPS:
1233 gst_event_parse_caps (event, &caps);
1234 ret = gst_ass_render_setcaps_video (pad, caps);
1235 gst_event_unref (event);
1238 case GST_EVENT_SEGMENT:
1242 GST_DEBUG_OBJECT (render, "received new segment");
1244 gst_event_copy_segment (event, &segment);
1246 if (segment.format == GST_FORMAT_TIME) {
1247 GST_DEBUG_OBJECT (render, "VIDEO SEGMENT now: %" GST_SEGMENT_FORMAT,
1248 &render->video_segment);
1250 render->video_segment = segment;
1252 GST_DEBUG_OBJECT (render, "VIDEO SEGMENT after: %" GST_SEGMENT_FORMAT,
1253 &render->video_segment);
1254 ret = gst_pad_push_event (render->srcpad, event);
1256 GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1257 ("received non-TIME newsegment event on video input"));
1259 gst_event_unref (event);
1265 GstTagList *taglist = NULL;
1267 /* tag events may contain attachments which might be fonts */
1268 GST_DEBUG_OBJECT (render, "got TAG event");
1270 gst_event_parse_tag (event, &taglist);
1271 gst_ass_render_handle_tags (render, taglist);
1272 ret = gst_pad_push_event (render->srcpad, event);
1275 case GST_EVENT_FLUSH_STOP:
1276 gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
1278 ret = gst_pad_push_event (render->srcpad, event);
1286 gst_ass_render_query_video (GstPad * pad, GstObject * parent, GstQuery * query)
1288 gboolean res = FALSE;
1290 switch (GST_QUERY_TYPE (query)) {
1291 case GST_QUERY_CAPS:
1293 GstCaps *filter, *caps;
1295 gst_query_parse_caps (query, &filter);
1296 caps = gst_ass_render_getcaps (pad, filter);
1297 gst_query_set_caps_result (query, caps);
1298 gst_caps_unref (caps);
1303 res = gst_pad_query_default (pad, parent, query);
1311 gst_ass_render_event_text (GstPad * pad, GstObject * parent, GstEvent * event)
1314 gboolean ret = FALSE;
1315 GstAssRender *render = GST_ASS_RENDER (parent);
1317 GST_DEBUG_OBJECT (pad, "received text event %s", GST_EVENT_TYPE_NAME (event));
1319 switch (GST_EVENT_TYPE (event)) {
1320 case GST_EVENT_CAPS:
1324 gst_event_parse_caps (event, &caps);
1325 ret = gst_ass_render_setcaps_text (pad, caps);
1326 gst_event_unref (event);
1329 case GST_EVENT_SEGMENT:
1333 GST_DEBUG_OBJECT (render, "received new segment");
1335 gst_event_copy_segment (event, &segment);
1337 if (segment.format == GST_FORMAT_TIME) {
1338 GST_DEBUG_OBJECT (render, "SUBTITLE SEGMENT now: %" GST_SEGMENT_FORMAT,
1339 &render->subtitle_segment);
1341 render->subtitle_segment = segment;
1343 GST_DEBUG_OBJECT (render,
1344 "SUBTITLE SEGMENT after: %" GST_SEGMENT_FORMAT,
1345 &render->subtitle_segment);
1347 gst_event_unref (event);
1349 GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1350 ("received non-TIME newsegment event on subtitle input"));
1352 gst_event_unref (event);
1356 case GST_EVENT_FLUSH_STOP:
1357 gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
1358 render->subtitle_flushing = FALSE;
1359 gst_event_unref (event);
1362 case GST_EVENT_FLUSH_START:
1363 GST_DEBUG_OBJECT (render, "begin flushing");
1364 g_mutex_lock (&render->ass_mutex);
1365 if (render->ass_track) {
1366 /* delete any events on the ass_track */
1367 for (i = 0; i < render->ass_track->n_events; i++) {
1368 GST_DEBUG_OBJECT (render, "deleted event with eid %i", i);
1369 ass_free_event (render->ass_track, i);
1371 render->ass_track->n_events = 0;
1372 GST_DEBUG_OBJECT (render, "done flushing");
1374 g_mutex_unlock (&render->ass_mutex);
1375 g_mutex_lock (&render->subtitle_mutex);
1376 if (render->subtitle_pending)
1377 gst_buffer_unref (render->subtitle_pending);
1378 render->subtitle_pending = NULL;
1379 render->subtitle_flushing = TRUE;
1380 g_cond_signal (&render->subtitle_cond);
1381 g_mutex_unlock (&render->subtitle_mutex);
1382 gst_event_unref (event);
1386 GST_OBJECT_LOCK (render);
1387 GST_INFO_OBJECT (render, "text EOS");
1388 GST_OBJECT_UNLOCK (render);
1389 gst_event_unref (event);
1394 GstTagList *taglist = NULL;
1396 /* tag events may contain attachments which might be fonts */
1397 GST_DEBUG_OBJECT (render, "got TAG event");
1399 gst_event_parse_tag (event, &taglist);
1400 gst_ass_render_handle_tags (render, taglist);
1401 ret = gst_pad_push_event (render->srcpad, event);
1405 ret = gst_pad_push_event (render->srcpad, event);
1413 plugin_init (GstPlugin * plugin)
1415 GST_DEBUG_CATEGORY_INIT (gst_ass_render_debug, "assrender",
1416 0, "ASS/SSA subtitle renderer");
1417 GST_DEBUG_CATEGORY_INIT (gst_ass_render_lib_debug, "assrender_library",
1418 0, "ASS/SSA subtitle renderer library");
1420 return gst_element_register (plugin, "assrender",
1421 GST_RANK_PRIMARY, GST_TYPE_ASS_RENDER);
1424 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1427 "ASS/SSA subtitle renderer",
1428 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)