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_stride;
578 gint u_offset, u_stride;
579 gint v_offset, 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,
591 y_stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 0, width);
592 u_stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 1, width);
593 v_stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 2, width);
596 if (ass_image->dst_y > height || ass_image->dst_x > width)
599 /* blend subtitles onto the video frame */
600 alpha = 255 - ((ass_image->color) & 0xff);
601 r = ((ass_image->color) >> 24) & 0xff;
602 g = ((ass_image->color) >> 16) & 0xff;
603 b = ((ass_image->color) >> 8) & 0xff;
605 Y = rgb_to_y (r, g, b);
606 U = rgb_to_u (r, g, b);
607 V = rgb_to_v (r, g, b);
609 w = MIN (ass_image->w, width - ass_image->dst_x);
610 h = MIN (ass_image->h, height - ass_image->dst_y);
614 src_stride = ass_image->stride;
616 src = ass_image->bitmap;
618 buffer->data + y_offset + ass_image->dst_y * y_stride +
621 buffer->data + u_offset + ((ass_image->dst_y + 1) / 2) * u_stride +
622 (ass_image->dst_x + 1) / 2;
624 buffer->data + v_offset + ((ass_image->dst_y + 1) / 2) * v_stride +
625 (ass_image->dst_x + 1) / 2;
627 for (y = 0; y < h - 1; y += 2) {
628 for (x = 0; x < w - 1; x += 2) {
629 k = src[0] * alpha / 255;
631 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
633 k = src[1] * alpha / 255;
635 dst_y[1] = (k * Y + (255 - k) * dst_y[1]) / 255;
640 k = src[0] * alpha / 255;
642 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
644 k = src[1] * alpha / 255;
646 dst_y[1] = (k * Y + (255 - k) * dst_y[1]) / 255;
649 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
650 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
654 src += -src_stride + 2;
655 dst_y += -y_stride + 2;
659 k = src[0] * alpha / 255;
661 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
666 k = src[0] * alpha / 255;
668 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
671 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
672 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
676 src += -src_stride + 1;
677 dst_y += -y_stride + 1;
680 src += src_stride + (src_stride - w);
681 dst_y += y_stride + (y_stride - w);
682 dst_u += u_stride - w2;
683 dst_v += v_stride - w2;
687 for (x = 0; x < w - 1; x += 2) {
688 k = src[0] * alpha / 255;
690 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 255;
692 k = src[1] * alpha / 255;
694 dst_y[1] = (k * Y + (255 - k) * dst_y[1]) / 255;
697 dst_u[0] = (k2 * U + (255 - k2) * dst_u[0]) / 255;
698 dst_v[0] = (k2 * V + (255 - k2) * dst_v[0]) / 255;
707 k = src[0] * alpha / 255;
709 dst_y[0] = (k * Y + (255 - k) * dst_y[0]) / 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;
718 ass_image = ass_image->next;
721 GST_LOG_OBJECT (render, "amount of rendered ass_image: %u", counter);
725 gst_ass_render_setcaps_video (GstPad * pad, GstCaps * caps)
727 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
728 gboolean ret = FALSE;
729 gint par_n = 1, par_d = 1;
735 if (!gst_video_format_parse_caps (caps, &render->format, &render->width,
737 !gst_video_parse_caps_framerate (caps, &render->fps_n, &render->fps_d)) {
738 GST_ERROR_OBJECT (render, "Can't parse caps: %" GST_PTR_FORMAT, caps);
743 gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d);
745 ret = gst_pad_set_caps (render->srcpad, caps);
749 switch (render->format) {
750 case GST_VIDEO_FORMAT_RGB:
751 render->blit = blit_rgb;
753 case GST_VIDEO_FORMAT_BGR:
754 render->blit = blit_bgr;
756 case GST_VIDEO_FORMAT_xRGB:
757 render->blit = blit_xrgb;
759 case GST_VIDEO_FORMAT_xBGR:
760 render->blit = blit_xbgr;
762 case GST_VIDEO_FORMAT_RGBx:
763 render->blit = blit_rgbx;
765 case GST_VIDEO_FORMAT_BGRx:
766 render->blit = blit_bgrx;
768 case GST_VIDEO_FORMAT_I420:
769 render->blit = blit_i420;
776 g_mutex_lock (render->ass_mutex);
777 ass_set_frame_size (render->ass_renderer, render->width, render->height);
779 dar = (((gdouble) par_n) * ((gdouble) render->width))
780 / (((gdouble) par_d) * ((gdouble) render->height));
781 #if !defined(LIBASS_VERSION) || LIBASS_VERSION < 0x00907000
782 ass_set_aspect_ratio (render->ass_renderer, dar);
784 ass_set_aspect_ratio (render->ass_renderer,
785 dar, ((gdouble) render->width) / ((gdouble) render->height));
787 ass_set_font_scale (render->ass_renderer, 1.0);
788 ass_set_hinting (render->ass_renderer, ASS_HINTING_LIGHT);
790 #if !defined(LIBASS_VERSION) || LIBASS_VERSION < 0x00907000
791 ass_set_fonts (render->ass_renderer, "Arial", "sans-serif");
792 ass_set_fonts (render->ass_renderer, NULL, "Sans");
794 ass_set_fonts (render->ass_renderer, "Arial", "sans-serif", 1, NULL, 1);
795 ass_set_fonts (render->ass_renderer, NULL, "Sans", 1, NULL, 1);
797 ass_set_margins (render->ass_renderer, 0, 0, 0, 0);
798 ass_set_use_margins (render->ass_renderer, 0);
799 g_mutex_unlock (render->ass_mutex);
801 render->renderer_init_ok = TRUE;
803 GST_DEBUG_OBJECT (render, "ass renderer setup complete");
806 gst_object_unref (render);
812 gst_ass_render_setcaps_text (GstPad * pad, GstCaps * caps)
814 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
815 GstStructure *structure;
818 gchar *codec_private;
819 guint codec_private_size;
820 gboolean ret = FALSE;
822 structure = gst_caps_get_structure (caps, 0);
824 GST_DEBUG_OBJECT (render, "text pad linked with caps: %" GST_PTR_FORMAT,
827 value = gst_structure_get_value (structure, "codec_data");
829 g_mutex_lock (render->ass_mutex);
831 priv = gst_value_get_buffer (value);
832 g_return_val_if_fail (priv != NULL, FALSE);
834 codec_private = (gchar *) GST_BUFFER_DATA (priv);
835 codec_private_size = GST_BUFFER_SIZE (priv);
837 if (!render->ass_track)
838 render->ass_track = ass_new_track (render->ass_library);
840 ass_process_codec_private (render->ass_track,
841 codec_private, codec_private_size);
843 GST_DEBUG_OBJECT (render, "ass track created");
845 render->track_init_ok = TRUE;
848 } else if (!render->ass_track) {
849 render->ass_track = ass_new_track (render->ass_library);
851 render->track_init_ok = TRUE;
855 g_mutex_unlock (render->ass_mutex);
857 gst_object_unref (render);
864 gst_ass_render_process_text (GstAssRender * render, GstBuffer * buffer,
865 GstClockTime running_time, GstClockTime duration)
867 gchar *data = (gchar *) GST_BUFFER_DATA (buffer);
868 guint size = GST_BUFFER_SIZE (buffer);
869 gdouble pts_start, pts_end;
871 pts_start = running_time;
872 pts_start /= GST_MSECOND;
874 pts_end /= GST_MSECOND;
876 GST_DEBUG_OBJECT (render,
877 "Processing subtitles with running time %" GST_TIME_FORMAT
878 " and duration %" GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
879 GST_TIME_ARGS (duration));
880 g_mutex_lock (render->ass_mutex);
881 ass_process_chunk (render->ass_track, data, size, pts_start, pts_end);
882 g_mutex_unlock (render->ass_mutex);
883 gst_buffer_unref (buffer);
887 gst_ass_render_bufferalloc_video (GstPad * pad, guint64 offset, guint size,
888 GstCaps * caps, GstBuffer ** buffer)
890 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
891 GstFlowReturn ret = GST_FLOW_WRONG_STATE;
894 GST_OBJECT_LOCK (render);
895 allocpad = render->srcpad ? gst_object_ref (render->srcpad) : NULL;
896 GST_OBJECT_UNLOCK (render);
899 ret = gst_pad_alloc_buffer (allocpad, offset, size, caps, buffer);
900 gst_object_unref (allocpad);
903 gst_object_unref (render);
909 gst_ass_render_chain_video (GstPad * pad, GstBuffer * buffer)
911 GstAssRender *render = GST_ASS_RENDER (GST_PAD_PARENT (pad));
912 GstFlowReturn ret = GST_FLOW_OK;
913 gboolean in_seg = FALSE;
914 gint64 start, stop, clip_start = 0, clip_stop = 0;
915 ASS_Image *ass_image;
917 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
918 GST_WARNING_OBJECT (render, "buffer without timestamp, discarding");
919 gst_buffer_unref (buffer);
923 /* ignore buffers that are outside of the current segment */
924 start = GST_BUFFER_TIMESTAMP (buffer);
926 if (!GST_BUFFER_DURATION_IS_VALID (buffer)) {
927 stop = GST_CLOCK_TIME_NONE;
929 stop = start + GST_BUFFER_DURATION (buffer);
932 /* segment_clip() will adjust start unconditionally to segment_start if
933 * no stop time is provided, so handle this ourselves */
934 if (stop == GST_CLOCK_TIME_NONE && start < render->video_segment.start)
938 gst_segment_clip (&render->video_segment, GST_FORMAT_TIME, start, stop,
939 &clip_start, &clip_stop);
944 /* if the buffer is only partially in the segment, fix up stamps */
945 if (clip_start != start || (stop != -1 && clip_stop != stop)) {
946 GST_DEBUG_OBJECT (render, "clipping buffer timestamp/duration to segment");
947 buffer = gst_buffer_make_metadata_writable (buffer);
948 GST_BUFFER_TIMESTAMP (buffer) = clip_start;
950 GST_BUFFER_DURATION (buffer) = clip_stop - clip_start;
953 gst_segment_set_last_stop (&render->video_segment, GST_FORMAT_TIME,
956 g_mutex_lock (render->subtitle_mutex);
957 if (render->subtitle_pending) {
958 GstClockTime sub_running_time, vid_running_time;
959 GstClockTime sub_running_time_end, vid_running_time_end;
962 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
963 GST_BUFFER_TIMESTAMP (render->subtitle_pending));
964 sub_running_time_end =
965 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
966 GST_BUFFER_TIMESTAMP (render->subtitle_pending) +
967 GST_BUFFER_DURATION (render->subtitle_pending));
969 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
970 GST_BUFFER_TIMESTAMP (buffer));
971 vid_running_time_end =
972 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
973 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer));
975 if (sub_running_time_end < vid_running_time) {
976 gst_buffer_unref (render->subtitle_pending);
977 GST_DEBUG_OBJECT (render,
978 "Too late text buffer, dropping (%" GST_TIME_FORMAT " < %"
979 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time_end),
980 GST_TIME_ARGS (vid_running_time));
981 render->subtitle_pending = NULL;
982 g_cond_signal (render->subtitle_cond);
983 } else if (sub_running_time <= vid_running_time_end + GST_SECOND / 2) {
984 gst_ass_render_process_text (render, render->subtitle_pending,
985 sub_running_time, sub_running_time_end - sub_running_time);
986 render->subtitle_pending = NULL;
987 g_cond_signal (render->subtitle_cond);
990 g_mutex_unlock (render->subtitle_mutex);
992 /* now start rendering subtitles, if all conditions are met */
993 if (render->renderer_init_ok && render->track_init_ok && render->enable) {
994 GstClockTime running_time;
996 #ifndef GST_DISABLE_GST_DEBUG
1001 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
1002 GST_BUFFER_TIMESTAMP (buffer));
1003 GST_DEBUG_OBJECT (render,
1004 "rendering frame for running time %" GST_TIME_FORMAT,
1005 GST_TIME_ARGS (running_time));
1006 /* libass needs timestamps in ms */
1007 timestamp = running_time / GST_MSECOND;
1009 g_mutex_lock (render->ass_mutex);
1010 #ifndef GST_DISABLE_GST_DEBUG
1011 /* only for testing right now. could possibly be used for optimizations? */
1012 step = ass_step_sub (render->ass_track, timestamp, 1);
1013 GST_DEBUG_OBJECT (render, "Current running time: %" GST_TIME_FORMAT
1014 " // Next event: %" GST_TIME_FORMAT,
1015 GST_TIME_ARGS (running_time), GST_TIME_ARGS (step * GST_MSECOND));
1018 /* not sure what the last parameter to this call is for (detect_change) */
1019 ass_image = ass_render_frame (render->ass_renderer, render->ass_track,
1021 g_mutex_unlock (render->ass_mutex);
1023 if (ass_image != NULL) {
1024 buffer = gst_buffer_make_writable (buffer);
1025 render->blit (render, ass_image, buffer);
1027 GST_LOG_OBJECT (render, "nothing to render right now");
1030 GST_LOG_OBJECT (render, "rendering disabled, doing buffer passthrough");
1033 ret = gst_pad_push (render->srcpad, buffer);
1039 GST_DEBUG_OBJECT (render, "buffer out of segment, discarding");
1040 gst_buffer_unref (buffer);
1045 static GstFlowReturn
1046 gst_ass_render_chain_text (GstPad * pad, GstBuffer * buffer)
1048 GstFlowReturn ret = GST_FLOW_OK;
1049 GstAssRender *render = GST_ASS_RENDER (GST_PAD_PARENT (pad));
1050 GstClockTime timestamp, duration;
1051 GstClockTime sub_running_time, vid_running_time;
1052 GstClockTime sub_running_time_end;
1053 gint64 cstart, cstop;
1056 if (render->subtitle_flushing) {
1057 gst_buffer_unref (buffer);
1058 return GST_FLOW_WRONG_STATE;
1061 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1062 duration = GST_BUFFER_DURATION (buffer);
1064 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)
1065 || !GST_CLOCK_TIME_IS_VALID (duration))) {
1066 GST_WARNING_OBJECT (render,
1067 "Text buffer without valid timestamp" " or duration, dropping");
1068 gst_buffer_unref (buffer);
1073 gst_segment_clip (&render->subtitle_segment, GST_FORMAT_TIME, timestamp,
1074 timestamp + duration, &cstart, &cstop);
1076 GST_DEBUG_OBJECT (render,
1077 "Text buffer before segment start (%" GST_TIME_FORMAT " < %"
1078 GST_TIME_FORMAT ")", GST_TIME_ARGS (timestamp),
1079 GST_TIME_ARGS (render->subtitle_segment.start));
1080 gst_buffer_unref (buffer);
1084 GST_BUFFER_TIMESTAMP (buffer) = timestamp = cstart;
1085 GST_BUFFER_DURATION (buffer) = duration = cstop - cstart;
1087 gst_segment_set_last_stop (&render->subtitle_segment, GST_FORMAT_TIME,
1088 GST_BUFFER_TIMESTAMP (buffer));
1091 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
1093 sub_running_time_end =
1094 gst_segment_to_running_time (&render->subtitle_segment, GST_FORMAT_TIME,
1095 timestamp + duration);
1097 gst_segment_to_running_time (&render->video_segment, GST_FORMAT_TIME,
1098 render->video_segment.last_stop);
1100 if (render->fps_n && render->fps_d)
1102 gst_util_uint64_scale (GST_SECOND, render->fps_d, render->fps_n);
1104 if (sub_running_time > vid_running_time + GST_SECOND / 2) {
1105 g_assert (render->subtitle_pending == NULL);
1106 g_mutex_lock (render->subtitle_mutex);
1107 if (G_UNLIKELY (render->subtitle_flushing)) {
1108 GST_DEBUG_OBJECT (render, "Text pad flushing");
1109 gst_object_unref (buffer);
1110 g_mutex_unlock (render->subtitle_mutex);
1111 return GST_FLOW_WRONG_STATE;
1113 GST_DEBUG_OBJECT (render,
1114 "Too early text buffer, waiting (%" GST_TIME_FORMAT " > %"
1115 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time),
1116 GST_TIME_ARGS (vid_running_time));
1117 render->subtitle_pending = buffer;
1118 g_cond_wait (render->subtitle_cond, render->subtitle_mutex);
1119 g_mutex_unlock (render->subtitle_mutex);
1120 } else if (sub_running_time_end < vid_running_time) {
1121 GST_DEBUG_OBJECT (render,
1122 "Too late text buffer, dropping (%" GST_TIME_FORMAT " < %"
1123 GST_TIME_FORMAT, GST_TIME_ARGS (sub_running_time_end),
1124 GST_TIME_ARGS (vid_running_time));
1125 gst_buffer_unref (buffer);
1128 gst_ass_render_process_text (render, buffer, sub_running_time,
1129 sub_running_time_end - sub_running_time);
1133 GST_DEBUG_OBJECT (render,
1134 "processed text packet with timestamp %" GST_TIME_FORMAT
1135 " and duration %" GST_TIME_FORMAT,
1136 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1142 gst_ass_render_handle_tags (GstAssRender * render, GstTagList * taglist)
1144 static const gchar *mimetypes[] = {
1145 "application/x-font-ttf",
1146 "application/x-font-otf",
1147 "application/x-truetype-font"
1149 static const gchar *extensions[] = {
1159 tag_size = gst_tag_list_get_tag_size (taglist, GST_TAG_ATTACHMENT);
1160 if (tag_size > 0 && render->embeddedfonts) {
1161 const GValue *value;
1164 GstStructure *structure;
1165 gboolean valid_mimetype, valid_extension;
1167 const gchar *filename;
1169 GST_DEBUG_OBJECT (render, "TAG event has attachments");
1171 for (index = 0; index < tag_size; index++) {
1172 value = gst_tag_list_get_value_index (taglist, GST_TAG_ATTACHMENT, index);
1173 buf = gst_value_get_buffer (value);
1174 if (!buf || !GST_BUFFER_CAPS (buf))
1177 caps = GST_BUFFER_CAPS (buf);
1178 structure = gst_caps_get_structure (caps, 0);
1180 valid_mimetype = FALSE;
1181 valid_extension = FALSE;
1183 for (j = 0; j < G_N_ELEMENTS (mimetypes); j++) {
1184 if (gst_structure_has_name (structure, mimetypes[j])) {
1185 valid_mimetype = TRUE;
1189 filename = gst_structure_get_string (structure, "filename");
1193 if (!valid_mimetype) {
1194 guint len = strlen (filename);
1195 const gchar *extension = filename + len - 4;
1196 for (j = 0; j < G_N_ELEMENTS (extensions); j++) {
1197 if (g_ascii_strcasecmp (extension, extensions[j]) == 0) {
1198 valid_extension = TRUE;
1204 if (valid_mimetype || valid_extension) {
1205 g_mutex_lock (render->ass_mutex);
1206 ass_add_font (render->ass_library, (gchar *) filename,
1207 (gchar *) GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
1208 GST_DEBUG_OBJECT (render, "registered new font %s", filename);
1209 g_mutex_unlock (render->ass_mutex);
1216 gst_ass_render_event_video (GstPad * pad, GstEvent * event)
1218 gboolean ret = FALSE;
1219 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
1221 GST_DEBUG_OBJECT (pad, "received video event %s",
1222 GST_EVENT_TYPE_NAME (event));
1224 switch (GST_EVENT_TYPE (event)) {
1225 case GST_EVENT_NEWSEGMENT:
1229 gint64 start, stop, time;
1232 GST_DEBUG_OBJECT (render, "received new segment");
1234 gst_event_parse_new_segment (event, &update, &rate, &format, &start,
1237 if (format == GST_FORMAT_TIME) {
1238 GST_DEBUG_OBJECT (render, "VIDEO SEGMENT now: %" GST_SEGMENT_FORMAT,
1239 &render->video_segment);
1241 gst_segment_set_newsegment (&render->video_segment, update, rate,
1242 format, start, stop, time);
1244 GST_DEBUG_OBJECT (render, "VIDEO SEGMENT after: %" GST_SEGMENT_FORMAT,
1245 &render->video_segment);
1246 ret = gst_pad_push_event (render->srcpad, event);
1248 GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1249 ("received non-TIME newsegment event on video input"));
1251 gst_event_unref (event);
1257 GstTagList *taglist = NULL;
1259 /* tag events may contain attachments which might be fonts */
1260 GST_DEBUG_OBJECT (render, "got TAG event");
1262 gst_event_parse_tag (event, &taglist);
1263 gst_ass_render_handle_tags (render, taglist);
1264 ret = gst_pad_push_event (render->srcpad, event);
1267 case GST_EVENT_FLUSH_STOP:
1268 gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
1270 ret = gst_pad_push_event (render->srcpad, event);
1274 gst_object_unref (render);
1280 gst_ass_render_event_text (GstPad * pad, GstEvent * event)
1283 gboolean ret = FALSE;
1284 GstAssRender *render = GST_ASS_RENDER (gst_pad_get_parent (pad));
1286 GST_DEBUG_OBJECT (pad, "received text event %s", GST_EVENT_TYPE_NAME (event));
1288 switch (GST_EVENT_TYPE (event)) {
1289 case GST_EVENT_NEWSEGMENT:
1293 gint64 start, stop, time;
1296 GST_DEBUG_OBJECT (render, "received new segment");
1298 gst_event_parse_new_segment (event, &update, &rate, &format, &start,
1301 if (format == GST_FORMAT_TIME) {
1302 GST_DEBUG_OBJECT (render, "SUBTITLE SEGMENT now: %" GST_SEGMENT_FORMAT,
1303 &render->subtitle_segment);
1305 gst_segment_set_newsegment (&render->subtitle_segment, update, rate,
1306 format, start, stop, time);
1308 GST_DEBUG_OBJECT (render,
1309 "SUBTITLE SEGMENT after: %" GST_SEGMENT_FORMAT,
1310 &render->subtitle_segment);
1312 gst_event_unref (event);
1314 GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1315 ("received non-TIME newsegment event on subtitle input"));
1317 gst_event_unref (event);
1321 case GST_EVENT_FLUSH_STOP:
1322 gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
1323 render->subtitle_flushing = FALSE;
1324 gst_event_unref (event);
1327 case GST_EVENT_FLUSH_START:
1328 GST_DEBUG_OBJECT (render, "begin flushing");
1329 g_mutex_lock (render->ass_mutex);
1330 if (render->ass_track) {
1331 /* delete any events on the ass_track */
1332 for (i = 0; i < render->ass_track->n_events; i++) {
1333 GST_DEBUG_OBJECT (render, "deleted event with eid %i", i);
1334 ass_free_event (render->ass_track, i);
1336 render->ass_track->n_events = 0;
1337 GST_DEBUG_OBJECT (render, "done flushing");
1339 g_mutex_unlock (render->ass_mutex);
1340 g_mutex_lock (render->subtitle_mutex);
1341 if (render->subtitle_pending)
1342 gst_buffer_unref (render->subtitle_pending);
1343 render->subtitle_pending = NULL;
1344 render->subtitle_flushing = TRUE;
1345 g_cond_signal (render->subtitle_cond);
1346 g_mutex_unlock (render->subtitle_mutex);
1347 gst_event_unref (event);
1351 GST_OBJECT_LOCK (render);
1352 GST_INFO_OBJECT (render, "text EOS");
1353 GST_OBJECT_UNLOCK (render);
1354 gst_event_unref (event);
1359 GstTagList *taglist = NULL;
1361 /* tag events may contain attachments which might be fonts */
1362 GST_DEBUG_OBJECT (render, "got TAG event");
1364 gst_event_parse_tag (event, &taglist);
1365 gst_ass_render_handle_tags (render, taglist);
1366 ret = gst_pad_push_event (render->srcpad, event);
1370 ret = gst_pad_push_event (render->srcpad, event);
1374 gst_object_unref (render);
1380 plugin_init (GstPlugin * plugin)
1382 GST_DEBUG_CATEGORY_INIT (gst_ass_render_debug, "assrender",
1383 0, "ASS/SSA subtitle renderer");
1384 GST_DEBUG_CATEGORY_INIT (gst_ass_render_lib_debug, "assrender_library",
1385 0, "ASS/SSA subtitle renderer library");
1387 return gst_element_register (plugin, "assrender",
1388 GST_RANK_PRIMARY, GST_TYPE_ASS_RENDER);
1391 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1394 "ASS/SSA subtitle renderer",
1395 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)