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 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
63 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR ";"
64 GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
65 GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx ";"
66 GST_VIDEO_CAPS_YUV ("I420"))
69 static GstStaticPadTemplate video_sink_factory =
70 GST_STATIC_PAD_TEMPLATE ("video_sink",
73 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR ";"
74 GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
75 GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx ";"
76 GST_VIDEO_CAPS_YUV ("I420"))
79 static GstStaticPadTemplate text_sink_factory =
80 GST_STATIC_PAD_TEMPLATE ("text_sink",
83 GST_STATIC_CAPS ("application/x-ass; application/x-ssa")
86 static void gst_ass_render_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_ass_render_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
91 static void gst_ass_render_finalize (GObject * object);
93 static GstStateChangeReturn gst_ass_render_change_state (GstElement * element,
94 GstStateChange transition);
96 GST_BOILERPLATE (GstAssRender, gst_ass_render, GstElement, GST_TYPE_ELEMENT);
98 static GstCaps *gst_ass_render_getcaps (GstPad * pad);
100 static gboolean gst_ass_render_setcaps_video (GstPad * pad, GstCaps * caps);
101 static gboolean gst_ass_render_setcaps_text (GstPad * pad, GstCaps * caps);
103 static GstFlowReturn gst_ass_render_chain_video (GstPad * pad, GstBuffer * buf);
104 static GstFlowReturn gst_ass_render_chain_text (GstPad * pad, GstBuffer * buf);
106 static gboolean gst_ass_render_event_video (GstPad * pad, GstEvent * event);
107 static gboolean gst_ass_render_event_text (GstPad * pad, GstEvent * event);
108 static gboolean gst_ass_render_event_src (GstPad * pad, GstEvent * event);
110 static GstFlowReturn gst_ass_render_bufferalloc_video (GstPad * pad,
111 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buffer);
113 static gboolean gst_ass_render_query_src (GstPad * pad, GstQuery * query);
116 gst_ass_render_base_init (gpointer gclass)
118 GstElementClass *element_class = (GstElementClass *) gclass;
120 gst_element_class_add_pad_template (element_class,
121 gst_static_pad_template_get (&src_factory));
122 gst_element_class_add_pad_template (element_class,
123 gst_static_pad_template_get (&video_sink_factory));
124 gst_element_class_add_pad_template (element_class,
125 gst_static_pad_template_get (&text_sink_factory));
127 gst_element_class_set_details_simple (element_class, "ASS/SSA Render",
128 "Mixer/Video/Overlay/Subtitle",
129 "Renders ASS/SSA subtitles with libass",
130 "Benjamin Schmitz <vortex@wolpzone.de>, "
131 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
134 /* initialize the plugin's class */
136 gst_ass_render_class_init (GstAssRenderClass * klass)
138 GObjectClass *gobject_class = (GObjectClass *) klass;
139 GstElementClass *gstelement_class = (GstElementClass *) klass;
141 gobject_class->set_property = gst_ass_render_set_property;
142 gobject_class->get_property = gst_ass_render_get_property;
143 gobject_class->finalize = gst_ass_render_finalize;
145 g_object_class_install_property (gobject_class, PROP_ENABLE,
146 g_param_spec_boolean ("enable", "Enable",
147 "Enable rendering of subtitles", TRUE,
148 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149 g_object_class_install_property (gobject_class, PROP_EMBEDDEDFONTS,
150 g_param_spec_boolean ("embeddedfonts", "Embedded Fonts",
151 "Extract and use fonts embedded in the stream", TRUE,
152 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
154 gstelement_class->change_state =
155 GST_DEBUG_FUNCPTR (gst_ass_render_change_state);
158 #if defined(LIBASS_VERSION) && LIBASS_VERSION >= 0x00907000
160 _libass_message_cb (gint level, const gchar * fmt, va_list args,
163 gchar *message = g_strdup_vprintf (fmt, args);
166 GST_CAT_ERROR_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
168 GST_CAT_WARNING_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
170 GST_CAT_INFO_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
172 GST_CAT_DEBUG_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
174 GST_CAT_LOG_OBJECT (gst_ass_render_lib_debug, render, "%s", message);
181 gst_ass_render_init (GstAssRender * render, GstAssRenderClass * gclass)
183 GST_DEBUG_OBJECT (render, "init");
185 render->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
186 render->video_sinkpad =
187 gst_pad_new_from_static_template (&video_sink_factory, "video_sink");
188 render->text_sinkpad =
189 gst_pad_new_from_static_template (&text_sink_factory, "text_sink");
191 gst_pad_set_setcaps_function (render->video_sinkpad,
192 GST_DEBUG_FUNCPTR (gst_ass_render_setcaps_video));
193 gst_pad_set_setcaps_function (render->text_sinkpad,
194 GST_DEBUG_FUNCPTR (gst_ass_render_setcaps_text));
196 gst_pad_set_getcaps_function (render->srcpad,
197 GST_DEBUG_FUNCPTR (gst_ass_render_getcaps));
198 gst_pad_set_getcaps_function (render->video_sinkpad,
199 GST_DEBUG_FUNCPTR (gst_ass_render_getcaps));
201 gst_pad_set_chain_function (render->video_sinkpad,
202 GST_DEBUG_FUNCPTR (gst_ass_render_chain_video));
203 gst_pad_set_chain_function (render->text_sinkpad,
204 GST_DEBUG_FUNCPTR (gst_ass_render_chain_text));
206 gst_pad_set_event_function (render->video_sinkpad,
207 GST_DEBUG_FUNCPTR (gst_ass_render_event_video));
208 gst_pad_set_event_function (render->text_sinkpad,
209 GST_DEBUG_FUNCPTR (gst_ass_render_event_text));
210 gst_pad_set_event_function (render->srcpad,
211 GST_DEBUG_FUNCPTR (gst_ass_render_event_src));
213 gst_pad_set_bufferalloc_function (render->video_sinkpad,
214 GST_DEBUG_FUNCPTR (gst_ass_render_bufferalloc_video));
216 gst_pad_set_query_function (render->srcpad,
217 GST_DEBUG_FUNCPTR (gst_ass_render_query_src));
219 gst_element_add_pad (GST_ELEMENT (render), render->srcpad);
220 gst_element_add_pad (GST_ELEMENT (render), render->video_sinkpad);
221 gst_element_add_pad (GST_ELEMENT (render), render->text_sinkpad);
226 render->subtitle_mutex = g_mutex_new ();
227 render->subtitle_cond = g_cond_new ();
229 render->renderer_init_ok = FALSE;
230 render->track_init_ok = FALSE;
231 render->enable = TRUE;
232 render->embeddedfonts = TRUE;
234 gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
235 gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
237 render->ass_mutex = g_mutex_new ();
238 render->ass_library = ass_library_init ();
239 #if defined(LIBASS_VERSION) && LIBASS_VERSION >= 0x00907000
240 ass_set_message_cb (render->ass_library, _libass_message_cb, render);
242 ass_set_extract_fonts (render->ass_library, 1);
244 render->ass_renderer = ass_renderer_init (render->ass_library);
245 if (!render->ass_renderer) {
246 GST_WARNING_OBJECT (render, "cannot create renderer instance");
247 g_assert_not_reached ();
250 render->ass_track = NULL;
252 GST_DEBUG_OBJECT (render, "init complete");
256 gst_ass_render_finalize (GObject * object)
258 GstAssRender *render = GST_ASS_RENDER (object);
260 if (render->subtitle_mutex)
261 g_mutex_free (render->subtitle_mutex);
263 if (render->subtitle_cond)
264 g_cond_free (render->subtitle_cond);
266 if (render->ass_track) {
267 ass_free_track (render->ass_track);
270 if (render->ass_renderer) {
271 ass_renderer_done (render->ass_renderer);
274 if (render->ass_library) {
275 ass_library_done (render->ass_library);
278 if (render->ass_mutex)
279 g_mutex_free (render->ass_mutex);
281 G_OBJECT_CLASS (parent_class)->finalize (object);
285 gst_ass_render_set_property (GObject * object, guint prop_id,
286 const GValue * value, GParamSpec * pspec)
288 GstAssRender *render = GST_ASS_RENDER (object);
292 render->enable = g_value_get_boolean (value);
294 case PROP_EMBEDDEDFONTS:
295 render->embeddedfonts = g_value_get_boolean (value);
296 g_mutex_lock (render->ass_mutex);
297 ass_set_extract_fonts (render->ass_library, render->embeddedfonts);
298 g_mutex_unlock (render->ass_mutex);
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
307 gst_ass_render_get_property (GObject * object, guint prop_id,
308 GValue * value, GParamSpec * pspec)
310 GstAssRender *render = GST_ASS_RENDER (object);
314 g_value_set_boolean (value, render->enable);
316 case PROP_EMBEDDEDFONTS:
317 g_value_set_boolean (value, render->embeddedfonts);
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325 static GstStateChangeReturn
326 gst_ass_render_change_state (GstElement * element, GstStateChange transition)
328 GstAssRender *render = GST_ASS_RENDER (element);
329 GstStateChangeReturn ret;
331 switch (transition) {
332 case GST_STATE_CHANGE_READY_TO_PAUSED:
333 render->subtitle_flushing = FALSE;
334 gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
335 gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
337 case GST_STATE_CHANGE_NULL_TO_READY:
338 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
342 case GST_STATE_CHANGE_PAUSED_TO_READY:
343 g_mutex_lock (render->subtitle_mutex);
344 render->subtitle_flushing = TRUE;
345 if (render->subtitle_pending)
346 gst_buffer_unref (render->subtitle_pending);
347 render->subtitle_pending = NULL;
348 g_cond_signal (render->subtitle_cond);
349 g_mutex_unlock (render->subtitle_mutex);
353 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
355 switch (transition) {
356 case GST_STATE_CHANGE_PAUSED_TO_READY:
357 g_mutex_lock (render->ass_mutex);
358 if (render->ass_track)
359 ass_free_track (render->ass_track);
360 render->ass_track = NULL;
361 g_mutex_unlock (render->ass_mutex);
362 render->track_init_ok = FALSE;
363 render->renderer_init_ok = FALSE;
365 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
366 case GST_STATE_CHANGE_READY_TO_NULL:
376 gst_ass_render_query_src (GstPad * pad, GstQuery * query)
378 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
381 ret = gst_pad_peer_query (render->video_sinkpad, query);
383 gst_object_unref (render);
388 gst_ass_render_event_src (GstPad * pad, GstEvent * event)
390 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
391 gboolean ret = FALSE;
393 switch (GST_EVENT_TYPE (event)) {
394 case GST_EVENT_SEEK:{
397 GST_DEBUG_OBJECT (render, "seek received, driving from here");
399 gst_event_parse_seek (event, NULL, NULL, &flags, NULL, NULL, NULL, NULL);
401 /* Flush downstream, only for flushing seek */
402 if (flags & GST_SEEK_FLAG_FLUSH)
403 gst_pad_push_event (render->srcpad, gst_event_new_flush_start ());
405 /* Mark subtitle as flushing, unblocks chains */
406 g_mutex_lock (render->subtitle_mutex);
407 if (render->subtitle_pending)
408 gst_buffer_unref (render->subtitle_pending);
409 render->subtitle_pending = NULL;
410 render->subtitle_flushing = TRUE;
411 g_cond_signal (render->subtitle_cond);
412 g_mutex_unlock (render->subtitle_mutex);
414 /* Seek on each sink pad */
415 gst_event_ref (event);
416 ret = gst_pad_push_event (render->video_sinkpad, event);
418 ret = gst_pad_push_event (render->text_sinkpad, event);
420 gst_event_unref (event);
425 gst_event_ref (event);
426 ret = gst_pad_push_event (render->video_sinkpad, event);
427 gst_pad_push_event (render->text_sinkpad, event);
431 gst_object_unref (render);
437 gst_ass_render_getcaps (GstPad * pad)
439 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
443 if (pad == render->srcpad)
444 otherpad = render->video_sinkpad;
446 otherpad = render->srcpad;
448 /* we can do what the peer can */
449 caps = gst_pad_peer_get_caps (otherpad);
452 const GstCaps *templ;
454 /* filtered against our padtemplate */
455 templ = gst_pad_get_pad_template_caps (otherpad);
456 temp = gst_caps_intersect (caps, templ);
457 gst_caps_unref (caps);
458 /* this is what we can do */
461 /* no peer, our padtemplate is enough then */
462 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
465 gst_object_unref (render);
470 #define CREATE_RGB_BLIT_FUNCTION(name,bpp,R,G,B) \
472 blit_##name (GstAssRender * render, ASS_Image * ass_image, GstBuffer * buffer) \
475 gint alpha, r, g, b, k; \
479 gint width = render->width; \
480 gint height = render->height; \
481 gint dst_stride = GST_ROUND_UP_4 (width * bpp); \
485 while (ass_image) { \
486 if (ass_image->dst_y > height || ass_image->dst_x > width) \
489 /* blend subtitles onto the video frame */ \
490 alpha = 255 - ((ass_image->color) & 0xff); \
491 r = ((ass_image->color) >> 24) & 0xff; \
492 g = ((ass_image->color) >> 16) & 0xff; \
493 b = ((ass_image->color) >> 8) & 0xff; \
494 src = ass_image->bitmap; \
495 dst = buffer->data + ass_image->dst_y * dst_stride + ass_image->dst_x * bpp; \
497 w = MIN (ass_image->w, width - ass_image->dst_x); \
498 h = MIN (ass_image->h, height - ass_image->dst_y); \
499 src_skip = ass_image->stride - w; \
500 dst_skip = dst_stride - w * bpp; \
502 for (y = 0; y < h; y++) { \
503 for (x = 0; x < w; x++) { \
504 k = src[0] * alpha / 255; \
505 dst[R] = (k * r + (255 - k) * dst[R]) / 255; \
506 dst[G] = (k * g + (255 - k) * dst[G]) / 255; \
507 dst[B] = (k * b + (255 - k) * dst[B]) / 255; \
516 ass_image = ass_image->next; \
518 GST_LOG_OBJECT (render, "amount of rendered ass_image: %u", counter); \
521 CREATE_RGB_BLIT_FUNCTION (rgb, 3, 0, 1, 2);
522 CREATE_RGB_BLIT_FUNCTION (bgr, 3, 2, 1, 0);
523 CREATE_RGB_BLIT_FUNCTION (xrgb, 4, 1, 2, 3);
524 CREATE_RGB_BLIT_FUNCTION (xbgr, 4, 3, 2, 1);
525 CREATE_RGB_BLIT_FUNCTION (rgbx, 4, 0, 1, 2);
526 CREATE_RGB_BLIT_FUNCTION (bgrx, 4, 2, 1, 0);
528 #undef CREATE_RGB_BLIT_FUNCTION
531 rgb_to_y (gint r, gint g, gint b)
535 ret = (gint) (((19595 * r) >> 16) + ((38470 * g) >> 16) + ((7471 * b) >> 16));
536 ret = CLAMP (ret, 0, 255);
541 rgb_to_u (gint r, gint g, gint b)
546 (gint) (-((11059 * r) >> 16) - ((21709 * g) >> 16) + ((32768 * b) >> 16) +
548 ret = CLAMP (ret, 0, 255);
553 rgb_to_v (gint r, gint g, gint b)
558 (gint) (((32768 * r) >> 16) - ((27439 * g) >> 16) - ((5329 * b) >> 16) +
560 ret = CLAMP (ret, 0, 255);
565 blit_i420 (GstAssRender * render, ASS_Image * ass_image, GstBuffer * buffer)
568 gint alpha, r, g, b, k, k2;
571 guint8 *dst_y, *dst_u, *dst_v;
574 gint width = render->width;
575 gint height = render->height;
577 gint y_offset, y_height, y_width, y_stride;
578 gint u_offset, u_height, u_width, u_stride;
579 gint v_offset, v_height, v_width, v_stride;
582 gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 0, width,
585 gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 1, width,
588 gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 2, width,
592 gst_video_format_get_component_height (GST_VIDEO_FORMAT_I420, 0, height);
594 gst_video_format_get_component_height (GST_VIDEO_FORMAT_I420, 1, height);
596 gst_video_format_get_component_height (GST_VIDEO_FORMAT_I420, 2, height);
599 gst_video_format_get_component_width (GST_VIDEO_FORMAT_I420, 0, width);
601 gst_video_format_get_component_width (GST_VIDEO_FORMAT_I420, 1, width);
603 gst_video_format_get_component_width (GST_VIDEO_FORMAT_I420, 2, width);
605 y_stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 0, width);
606 u_stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 1, width);
607 v_stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 2, width);
610 if (ass_image->dst_y > height || ass_image->dst_x > width)
613 /* blend subtitles onto the video frame */
614 alpha = 255 - ((ass_image->color) & 0xff);
615 r = ((ass_image->color) >> 24) & 0xff;
616 g = ((ass_image->color) >> 16) & 0xff;
617 b = ((ass_image->color) >> 8) & 0xff;
619 Y = rgb_to_y (r, g, b);
620 U = rgb_to_u (r, g, b);
621 V = rgb_to_v (r, g, b);
623 w = MIN (ass_image->w, width - ass_image->dst_x);
624 h = MIN (ass_image->h, height - ass_image->dst_y);
629 src_stride = ass_image->stride;
631 src = ass_image->bitmap;
633 buffer->data + y_offset + ass_image->dst_y * y_stride +
636 buffer->data + u_offset + ((ass_image->dst_y + 1) / 2) * u_stride +
637 (ass_image->dst_x + 1) / 2;
639 buffer->data + v_offset + ((ass_image->dst_y + 1) / 2) * v_stride +
640 (ass_image->dst_x + 1) / 2;
642 for (y = 0; y < h - 1; y += 2) {
643 for (x = 0; x < w - 1; x += 2) {
644 k = src[0] * alpha / 255;
646 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
648 k = src[1] * alpha / 255;
650 dst_y[1] = (k * Y + (255 - k) * dst_y[1]) / 255;
655 k = src[0] * alpha / 255;
657 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
659 k = src[1] * alpha / 255;
661 dst_y[1] = (k * Y + (255 - k) * dst_y[1]) / 255;
664 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
665 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
669 src += -src_stride + 2;
670 dst_y += -y_stride + 2;
674 k = src[0] * alpha / 255;
676 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
681 k = src[0] * alpha / 255;
683 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
686 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
687 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
691 src += -src_stride + 1;
692 dst_y += -y_stride + 1;
695 src += src_stride + (src_stride - w);
696 dst_y += y_stride + (y_stride - w);
697 dst_u += u_stride - w2;
698 dst_v += v_stride - w2;
702 for (x = 0; x < w - 1; x += 2) {
703 k = src[0] * alpha / 255;
705 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
707 k = src[1] * alpha / 255;
709 dst_y[1] = (k * Y + (255 - k) * dst_y[1]) / 255;
712 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
713 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
722 k = src[0] * alpha / 255;
724 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
726 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
727 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
733 ass_image = ass_image->next;
736 GST_LOG_OBJECT (render, "amount of rendered ass_image: %u", counter);
740 gst_ass_render_setcaps_video (GstPad * pad, GstCaps * caps)
742 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
743 gboolean ret = FALSE;
744 gint par_n = 1, par_d = 1;
750 if (!gst_video_format_parse_caps (caps, &render->format, &render->width,
752 !gst_video_parse_caps_framerate (caps, &render->fps_n, &render->fps_d)) {
753 GST_ERROR_OBJECT (render, "Can't parse caps: %" GST_PTR_FORMAT, caps);
758 gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d);
760 ret = gst_pad_set_caps (render->srcpad, caps);
764 switch (render->format) {
765 case GST_VIDEO_FORMAT_RGB:
766 render->blit = blit_rgb;
768 case GST_VIDEO_FORMAT_BGR:
769 render->blit = blit_bgr;
771 case GST_VIDEO_FORMAT_xRGB:
772 render->blit = blit_xrgb;
774 case GST_VIDEO_FORMAT_xBGR:
775 render->blit = blit_xbgr;
777 case GST_VIDEO_FORMAT_RGBx:
778 render->blit = blit_rgbx;
780 case GST_VIDEO_FORMAT_BGRx:
781 render->blit = blit_bgrx;
783 case GST_VIDEO_FORMAT_I420:
784 render->blit = blit_i420;
791 g_mutex_lock (render->ass_mutex);
792 ass_set_frame_size (render->ass_renderer, render->width, render->height);
794 dar = (((gdouble) par_n) * ((gdouble) render->width))
795 / (((gdouble) par_d) * ((gdouble) render->height));
796 #if !defined(LIBASS_VERSION) || LIBASS_VERSION < 0x00907000
797 ass_set_aspect_ratio (render->ass_renderer, dar);
799 ass_set_aspect_ratio (render->ass_renderer,
800 dar, ((gdouble) render->width) / ((gdouble) render->height));
802 ass_set_font_scale (render->ass_renderer, 1.0);
803 ass_set_hinting (render->ass_renderer, ASS_HINTING_LIGHT);
805 #if !defined(LIBASS_VERSION) || LIBASS_VERSION < 0x00907000
806 ass_set_fonts (render->ass_renderer, "Arial", "sans-serif");
807 ass_set_fonts (render->ass_renderer, NULL, "Sans");
809 ass_set_fonts (render->ass_renderer, "Arial", "sans-serif", 1, NULL, 1);
810 ass_set_fonts (render->ass_renderer, NULL, "Sans", 1, NULL, 1);
812 ass_set_margins (render->ass_renderer, 0, 0, 0, 0);
813 ass_set_use_margins (render->ass_renderer, 0);
814 g_mutex_unlock (render->ass_mutex);
816 render->renderer_init_ok = TRUE;
818 GST_DEBUG_OBJECT (render, "ass renderer setup complete");
821 gst_object_unref (render);
827 gst_ass_render_setcaps_text (GstPad * pad, GstCaps * caps)
829 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
830 GstStructure *structure;
833 gchar *codec_private;
834 guint codec_private_size;
835 gboolean ret = FALSE;
837 structure = gst_caps_get_structure (caps, 0);
839 GST_DEBUG_OBJECT (render, "text pad linked with caps: %" GST_PTR_FORMAT,
842 value = gst_structure_get_value (structure, "codec_data");
844 g_mutex_lock (render->ass_mutex);
846 priv = gst_value_get_buffer (value);
847 g_return_val_if_fail (priv != NULL, FALSE);
849 codec_private = (gchar *) GST_BUFFER_DATA (priv);
850 codec_private_size = GST_BUFFER_SIZE (priv);
852 if (!render->ass_track)
853 render->ass_track = ass_new_track (render->ass_library);
855 ass_process_codec_private (render->ass_track,
856 codec_private, codec_private_size);
858 GST_DEBUG_OBJECT (render, "ass track created");
860 render->track_init_ok = TRUE;
863 } else if (!render->ass_track) {
864 render->ass_track = ass_new_track (render->ass_library);
866 render->track_init_ok = TRUE;
870 g_mutex_unlock (render->ass_mutex);
872 gst_object_unref (render);
879 gst_ass_render_process_text (GstAssRender * render, GstBuffer * buffer,
880 GstClockTime running_time, GstClockTime duration)
882 gchar *data = (gchar *) GST_BUFFER_DATA (buffer);
883 guint size = GST_BUFFER_SIZE (buffer);
884 gdouble pts_start, pts_end;
886 pts_start = running_time;
887 pts_start /= GST_MSECOND;
889 pts_end /= GST_MSECOND;
891 GST_DEBUG_OBJECT (render,
892 "Processing subtitles with running time %" GST_TIME_FORMAT
893 " and duration %" GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
894 GST_TIME_ARGS (duration));
895 g_mutex_lock (render->ass_mutex);
896 ass_process_chunk (render->ass_track, data, size, pts_start, pts_end);
897 g_mutex_unlock (render->ass_mutex);
898 gst_buffer_unref (buffer);
902 gst_ass_render_bufferalloc_video (GstPad * pad, guint64 offset, guint size,
903 GstCaps * caps, GstBuffer ** buffer)
905 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
906 GstFlowReturn ret = GST_FLOW_WRONG_STATE;
909 GST_OBJECT_LOCK (render);
910 allocpad = render->srcpad ? gst_object_ref (render->srcpad) : NULL;
911 GST_OBJECT_UNLOCK (render);
914 ret = gst_pad_alloc_buffer (allocpad, offset, size, caps, buffer);
915 gst_object_unref (allocpad);
918 gst_object_unref (render);
924 gst_ass_render_chain_video (GstPad * pad, GstBuffer * buffer)
926 GstAssRender *render = GST_ASS_RENDER (GST_PAD_PARENT (pad));
927 GstFlowReturn ret = GST_FLOW_OK;
928 gboolean in_seg = FALSE;
929 gint64 start, stop, clip_start = 0, clip_stop = 0;
930 ASS_Image *ass_image;
932 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
933 GST_WARNING_OBJECT (render, "buffer without timestamp, discarding");
934 gst_buffer_unref (buffer);
938 /* ignore buffers that are outside of the current segment */
939 start = GST_BUFFER_TIMESTAMP (buffer);
941 if (!GST_BUFFER_DURATION_IS_VALID (buffer)) {
942 stop = GST_CLOCK_TIME_NONE;
944 stop = start + GST_BUFFER_DURATION (buffer);
947 /* segment_clip() will adjust start unconditionally to segment_start if
948 * no stop time is provided, so handle this ourselves */
949 if (stop == GST_CLOCK_TIME_NONE && start < render->video_segment.start)
953 gst_segment_clip (&render->video_segment, GST_FORMAT_TIME, start, stop,
954 &clip_start, &clip_stop);
959 /* if the buffer is only partially in the segment, fix up stamps */
960 if (clip_start != start || (stop != -1 && clip_stop != stop)) {
961 GST_DEBUG_OBJECT (render, "clipping buffer timestamp/duration to segment");
962 buffer = gst_buffer_make_metadata_writable (buffer);
963 GST_BUFFER_TIMESTAMP (buffer) = clip_start;
965 GST_BUFFER_DURATION (buffer) = clip_stop - clip_start;
968 gst_segment_set_last_stop (&render->video_segment, GST_FORMAT_TIME,
971 g_mutex_lock (render->subtitle_mutex);
972 if (render->subtitle_pending) {
973 GstClockTime sub_running_time, vid_running_time;
974 GstClockTime sub_running_time_end, vid_running_time_end;
977 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
978 GST_BUFFER_TIMESTAMP (render->subtitle_pending));
979 sub_running_time_end =
980 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
981 GST_BUFFER_TIMESTAMP (render->subtitle_pending) +
982 GST_BUFFER_DURATION (render->subtitle_pending));
984 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
985 GST_BUFFER_TIMESTAMP (buffer));
986 vid_running_time_end =
987 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
988 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer));
990 if (sub_running_time_end < vid_running_time) {
991 gst_buffer_unref (render->subtitle_pending);
992 GST_DEBUG_OBJECT (render,
993 "Too late text buffer, dropping (%" GST_TIME_FORMAT " < %"
994 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time_end),
995 GST_TIME_ARGS (vid_running_time));
996 render->subtitle_pending = NULL;
997 g_cond_signal (render->subtitle_cond);
998 } else if (sub_running_time <= vid_running_time_end + GST_SECOND / 2) {
999 gst_ass_render_process_text (render, render->subtitle_pending,
1000 sub_running_time, sub_running_time_end - sub_running_time);
1001 render->subtitle_pending = NULL;
1002 g_cond_signal (render->subtitle_cond);
1005 g_mutex_unlock (render->subtitle_mutex);
1007 /* now start rendering subtitles, if all conditions are met */
1008 if (render->renderer_init_ok && render->track_init_ok && render->enable) {
1009 GstClockTime running_time;
1011 #ifndef GST_DISABLE_GST_DEBUG
1016 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
1017 GST_BUFFER_TIMESTAMP (buffer));
1018 GST_DEBUG_OBJECT (render,
1019 "rendering frame for running time %" GST_TIME_FORMAT,
1020 GST_TIME_ARGS (running_time));
1021 /* libass needs timestamps in ms */
1022 timestamp = running_time / GST_MSECOND;
1024 g_mutex_lock (render->ass_mutex);
1025 #ifndef GST_DISABLE_GST_DEBUG
1026 /* only for testing right now. could possibly be used for optimizations? */
1027 step = ass_step_sub (render->ass_track, timestamp, 1);
1028 GST_DEBUG_OBJECT (render, "Current running time: %" GST_TIME_FORMAT
1029 " // Next event: %" GST_TIME_FORMAT,
1030 GST_TIME_ARGS (running_time), GST_TIME_ARGS (step * GST_MSECOND));
1033 /* not sure what the last parameter to this call is for (detect_change) */
1034 ass_image = ass_render_frame (render->ass_renderer, render->ass_track,
1036 g_mutex_unlock (render->ass_mutex);
1038 if (ass_image != NULL) {
1039 buffer = gst_buffer_make_writable (buffer);
1040 render->blit (render, ass_image, buffer);
1042 GST_LOG_OBJECT (render, "nothing to render right now");
1045 GST_LOG_OBJECT (render, "rendering disabled, doing buffer passthrough");
1048 ret = gst_pad_push (render->srcpad, buffer);
1054 GST_DEBUG_OBJECT (render, "buffer out of segment, discarding");
1055 gst_buffer_unref (buffer);
1060 static GstFlowReturn
1061 gst_ass_render_chain_text (GstPad * pad, GstBuffer * buffer)
1063 GstFlowReturn ret = GST_FLOW_OK;
1064 GstAssRender *render = GST_ASS_RENDER (GST_PAD_PARENT (pad));
1065 GstClockTime timestamp, duration;
1066 GstClockTime sub_running_time, vid_running_time;
1067 GstClockTime sub_running_time_end;
1068 gint64 cstart, cstop;
1071 if (render->subtitle_flushing) {
1072 gst_buffer_unref (buffer);
1073 return GST_FLOW_WRONG_STATE;
1076 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1077 duration = GST_BUFFER_DURATION (buffer);
1079 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)
1080 || !GST_CLOCK_TIME_IS_VALID (duration))) {
1081 GST_WARNING_OBJECT (render,
1082 "Text buffer without valid timestamp" " or duration, dropping");
1083 gst_buffer_unref (buffer);
1088 gst_segment_clip (&render->subtitle_segment, GST_FORMAT_TIME, timestamp,
1089 timestamp + duration, &cstart, &cstop);
1091 GST_DEBUG_OBJECT (render,
1092 "Text buffer before segment start (%" GST_TIME_FORMAT " < %"
1093 GST_TIME_FORMAT ")", GST_TIME_ARGS (timestamp),
1094 GST_TIME_ARGS (render->subtitle_segment.start));
1095 gst_buffer_unref (buffer);
1099 GST_BUFFER_TIMESTAMP (buffer) = timestamp = cstart;
1100 GST_BUFFER_DURATION (buffer) = duration = cstop - cstart;
1102 gst_segment_set_last_stop (&render->subtitle_segment, GST_FORMAT_TIME,
1103 GST_BUFFER_TIMESTAMP (buffer));
1106 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
1108 sub_running_time_end =
1109 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
1110 timestamp + duration);
1112 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
1113 render->video_segment.last_stop);
1115 if (render->fps_n && render->fps_d)
1117 gst_util_uint64_scale (GST_SECOND, render->fps_d, render->fps_n);
1119 if (sub_running_time > vid_running_time + GST_SECOND / 2) {
1120 g_assert (render->subtitle_pending == NULL);
1121 g_mutex_lock (render->subtitle_mutex);
1122 if (G_UNLIKELY (render->subtitle_flushing)) {
1123 GST_DEBUG_OBJECT (render, "Text pad flushing");
1124 gst_object_unref (buffer);
1125 g_mutex_unlock (render->subtitle_mutex);
1126 return GST_FLOW_WRONG_STATE;
1128 GST_DEBUG_OBJECT (render,
1129 "Too early text buffer, waiting (%" GST_TIME_FORMAT " > %"
1130 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time),
1131 GST_TIME_ARGS (vid_running_time));
1132 render->subtitle_pending = buffer;
1133 g_cond_wait (render->subtitle_cond, render->subtitle_mutex);
1134 g_mutex_unlock (render->subtitle_mutex);
1135 } else if (sub_running_time_end < vid_running_time) {
1136 GST_DEBUG_OBJECT (render,
1137 "Too late text buffer, dropping (%" GST_TIME_FORMAT " < %"
1138 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time_end),
1139 GST_TIME_ARGS (vid_running_time));
1140 gst_buffer_unref (buffer);
1143 gst_ass_render_process_text (render, buffer, sub_running_time,
1144 sub_running_time_end - sub_running_time);
1148 GST_DEBUG_OBJECT (render,
1149 "processed text packet with timestamp %" GST_TIME_FORMAT
1150 " and duration %" GST_TIME_FORMAT,
1151 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1157 gst_ass_render_handle_tags (GstAssRender * render, GstTagList * taglist)
1159 static const gchar *mimetypes[] = {
1160 "application/x-font-ttf",
1161 "application/x-font-otf",
1162 "application/x-truetype-font"
1164 static const gchar *extensions[] = {
1174 tag_size = gst_tag_list_get_tag_size (taglist, GST_TAG_ATTACHMENT);
1175 if (tag_size > 0 && render->embeddedfonts) {
1176 const GValue *value;
1179 GstStructure *structure;
1180 gboolean valid_mimetype, valid_extension;
1182 const gchar *filename;
1184 GST_DEBUG_OBJECT (render, "TAG event has attachments");
1186 for (index = 0; index < tag_size; index++) {
1187 value = gst_tag_list_get_value_index (taglist, GST_TAG_ATTACHMENT, index);
1188 buf = gst_value_get_buffer (value);
1189 if (!buf || !GST_BUFFER_CAPS (buf))
1192 caps = GST_BUFFER_CAPS (buf);
1193 structure = gst_caps_get_structure (caps, 0);
1195 valid_mimetype = FALSE;
1196 valid_extension = FALSE;
1198 for (j = 0; j < G_N_ELEMENTS (mimetypes); j++) {
1199 if (gst_structure_has_name (structure, mimetypes[j])) {
1200 valid_mimetype = TRUE;
1204 filename = gst_structure_get_string (structure, "filename");
1208 if (!valid_mimetype) {
1209 guint len = strlen (filename);
1210 const gchar *extension = filename + len - 4;
1211 for (j = 0; j < G_N_ELEMENTS (extensions); j++) {
1212 if (g_ascii_strcasecmp (extension, extensions[j]) == 0) {
1213 valid_extension = TRUE;
1219 if (valid_mimetype || valid_extension) {
1220 g_mutex_lock (render->ass_mutex);
1221 ass_add_font (render->ass_library, (gchar *) filename,
1222 (gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1223 GST_DEBUG_OBJECT (render, "registered new font %s", filename);
1224 g_mutex_unlock (render->ass_mutex);
1231 gst_ass_render_event_video (GstPad * pad, GstEvent * event)
1233 gboolean ret = FALSE;
1234 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
1236 GST_DEBUG_OBJECT (pad, "received video event %s",
1237 GST_EVENT_TYPE_NAME (event));
1239 switch (GST_EVENT_TYPE (event)) {
1240 case GST_EVENT_NEWSEGMENT:
1244 gint64 start, stop, time;
1247 GST_DEBUG_OBJECT (render, "received new segment");
1249 gst_event_parse_new_segment (event, &update, &rate, &format, &start,
1252 if (format == GST_FORMAT_TIME) {
1253 GST_DEBUG_OBJECT (render, "VIDEO SEGMENT now: %" GST_SEGMENT_FORMAT,
1254 &render->video_segment);
1256 gst_segment_set_newsegment (&render->video_segment, update, rate,
1257 format, start, stop, time);
1259 GST_DEBUG_OBJECT (render, "VIDEO SEGMENT after: %" GST_SEGMENT_FORMAT,
1260 &render->video_segment);
1261 ret = gst_pad_push_event (render->srcpad, event);
1263 GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1264 ("received non-TIME newsegment event on video input"));
1266 gst_event_unref (event);
1272 GstTagList *taglist = NULL;
1274 /* tag events may contain attachments which might be fonts */
1275 GST_DEBUG_OBJECT (render, "got TAG event");
1277 gst_event_parse_tag (event, &taglist);
1278 gst_ass_render_handle_tags (render, taglist);
1279 ret = gst_pad_push_event (render->srcpad, event);
1282 case GST_EVENT_FLUSH_STOP:
1283 gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
1285 ret = gst_pad_push_event (render->srcpad, event);
1289 gst_object_unref (render);
1295 gst_ass_render_event_text (GstPad * pad, GstEvent * event)
1298 gboolean ret = FALSE;
1299 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
1301 GST_DEBUG_OBJECT (pad, "received text event %s", GST_EVENT_TYPE_NAME (event));
1303 switch (GST_EVENT_TYPE (event)) {
1304 case GST_EVENT_NEWSEGMENT:
1308 gint64 start, stop, time;
1311 GST_DEBUG_OBJECT (render, "received new segment");
1313 gst_event_parse_new_segment (event, &update, &rate, &format, &start,
1316 if (format == GST_FORMAT_TIME) {
1317 GST_DEBUG_OBJECT (render, "SUBTITLE SEGMENT now: %" GST_SEGMENT_FORMAT,
1318 &render->subtitle_segment);
1320 gst_segment_set_newsegment (&render->subtitle_segment, update, rate,
1321 format, start, stop, time);
1323 GST_DEBUG_OBJECT (render,
1324 "SUBTITLE SEGMENT after: %" GST_SEGMENT_FORMAT,
1325 &render->subtitle_segment);
1327 gst_event_unref (event);
1329 GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1330 ("received non-TIME newsegment event on subtitle input"));
1332 gst_event_unref (event);
1336 case GST_EVENT_FLUSH_STOP:
1337 gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
1338 render->subtitle_flushing = FALSE;
1339 gst_event_unref (event);
1342 case GST_EVENT_FLUSH_START:
1343 GST_DEBUG_OBJECT (render, "begin flushing");
1344 g_mutex_lock (render->ass_mutex);
1345 if (render->ass_track) {
1346 /* delete any events on the ass_track */
1347 for (i = 0; i < render->ass_track->n_events; i++) {
1348 GST_DEBUG_OBJECT (render, "deleted event with eid %i", i);
1349 ass_free_event (render->ass_track, i);
1351 render->ass_track->n_events = 0;
1352 GST_DEBUG_OBJECT (render, "done flushing");
1354 g_mutex_unlock (render->ass_mutex);
1355 g_mutex_lock (render->subtitle_mutex);
1356 if (render->subtitle_pending)
1357 gst_buffer_unref (render->subtitle_pending);
1358 render->subtitle_pending = NULL;
1359 render->subtitle_flushing = TRUE;
1360 g_cond_signal (render->subtitle_cond);
1361 g_mutex_unlock (render->subtitle_mutex);
1362 gst_event_unref (event);
1366 GST_OBJECT_LOCK (render);
1367 GST_INFO_OBJECT (render, "text EOS");
1368 GST_OBJECT_UNLOCK (render);
1369 gst_event_unref (event);
1374 GstTagList *taglist = NULL;
1376 /* tag events may contain attachments which might be fonts */
1377 GST_DEBUG_OBJECT (render, "got TAG event");
1379 gst_event_parse_tag (event, &taglist);
1380 gst_ass_render_handle_tags (render, taglist);
1381 ret = gst_pad_push_event (render->srcpad, event);
1385 ret = gst_pad_push_event (render->srcpad, event);
1389 gst_object_unref (render);
1395 plugin_init (GstPlugin * plugin)
1397 GST_DEBUG_CATEGORY_INIT (gst_ass_render_debug, "assrender",
1398 0, "ASS/SSA subtitle renderer");
1399 GST_DEBUG_CATEGORY_INIT (gst_ass_render_lib_debug, "assrender_library",
1400 0, "ASS/SSA subtitle renderer library");
1402 return gst_element_register (plugin, "assrender",
1403 GST_RANK_PRIMARY, GST_TYPE_ASS_RENDER);
1406 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1409 "ASS/SSA subtitle renderer",
1410 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)