• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer DVB subtitles overlay
2  * Copyright (c) 2010 Mart Raudsepp <mart.raudsepp@collabora.co.uk>
3  * Copyright (c) 2010 ONELAN Ltd.
4  *
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.
9  *
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.
14  *
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * SECTION:element-dvbsuboverlay
23  * @title: dvbsuboverlay
24  *
25  * Renders DVB subtitles on top of a video stream.
26  *
27  * ## Example launch line
28  * |[ FIXME
29  * gst-launch-1.0 -v filesrc location=/path/to/ts ! mpegtsdemux name=d ! queue ! mpegaudioparse ! mpg123audiodec ! audioconvert ! autoaudiosink \
30  *     d. ! queue ! mpegvideoparse ! mpeg2dec ! videoconvert ! r. \
31  *     d. ! queue ! "subpicture/x-dvb" ! dvbsuboverlay name=r ! videoconvert ! autovideosink
32  * ]| This pipeline demuxes a MPEG-TS file with MPEG2 video, MP3 audio and embedded DVB subtitles and renders the subtitles on top of the video.
33  *
34  */
35 
36 
37 #ifdef HAVE_CONFIG_H
38 #  include <config.h>
39 #endif
40 
41 #include <gst/glib-compat-private.h>
42 #include "gstdvbsuboverlay.h"
43 
44 #include <gst/video/gstvideometa.h>
45 
46 #include <string.h>
47 
48 GST_DEBUG_CATEGORY_STATIC (gst_dvbsub_overlay_debug);
49 #define GST_CAT_DEFAULT gst_dvbsub_overlay_debug
50 
51 /* Filter signals and props */
52 enum
53 {
54   LAST_SIGNAL
55 };
56 
57 enum
58 {
59   PROP_0,
60   PROP_ENABLE,
61   PROP_MAX_PAGE_TIMEOUT,
62   PROP_FORCE_END
63 };
64 
65 #define DEFAULT_ENABLE (TRUE)
66 #define DEFAULT_MAX_PAGE_TIMEOUT (0)
67 #define DEFAULT_FORCE_END (FALSE)
68 
69 #define VIDEO_FORMATS GST_VIDEO_OVERLAY_COMPOSITION_BLEND_FORMATS
70 
71 #define DVBSUB_OVERLAY_CAPS GST_VIDEO_CAPS_MAKE(VIDEO_FORMATS)
72 
73 #define DVBSUB_OVERLAY_ALL_CAPS DVBSUB_OVERLAY_CAPS ";" \
74     GST_VIDEO_CAPS_MAKE_WITH_FEATURES ("ANY", GST_VIDEO_FORMATS_ALL)
75 
76 static GstStaticCaps sw_template_caps = GST_STATIC_CAPS (DVBSUB_OVERLAY_CAPS);
77 
78 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
79     GST_PAD_SRC,
80     GST_PAD_ALWAYS,
81     GST_STATIC_CAPS (DVBSUB_OVERLAY_ALL_CAPS)
82     );
83 
84 static GstStaticPadTemplate video_sink_factory =
85 GST_STATIC_PAD_TEMPLATE ("video_sink",
86     GST_PAD_SINK,
87     GST_PAD_ALWAYS,
88     GST_STATIC_CAPS (DVBSUB_OVERLAY_ALL_CAPS)
89     );
90 
91 static GstStaticPadTemplate text_sink_factory =
92 GST_STATIC_PAD_TEMPLATE ("text_sink",
93     GST_PAD_SINK,
94     GST_PAD_ALWAYS,
95     GST_STATIC_CAPS ("subpicture/x-dvb")
96     );
97 
98 static void gst_dvbsub_overlay_set_property (GObject * object, guint prop_id,
99     const GValue * value, GParamSpec * pspec);
100 static void gst_dvbsub_overlay_get_property (GObject * object, guint prop_id,
101     GValue * value, GParamSpec * pspec);
102 
103 static void gst_dvbsub_overlay_finalize (GObject * object);
104 
105 static GstStateChangeReturn gst_dvbsub_overlay_change_state (GstElement *
106     element, GstStateChange transition);
107 
108 #define gst_dvbsub_overlay_parent_class parent_class
109 G_DEFINE_TYPE (GstDVBSubOverlay, gst_dvbsub_overlay, GST_TYPE_ELEMENT);
110 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (dvbsuboverlay, "dvbsuboverlay",
111     GST_RANK_PRIMARY, GST_TYPE_DVBSUB_OVERLAY,
112     GST_DEBUG_CATEGORY_INIT (gst_dvbsub_overlay_debug, "dvbsuboverlay", 0,
113         "DVB subtitle overlay");
114     );
115 
116 static GstCaps *gst_dvbsub_overlay_get_videosink_caps (GstDVBSubOverlay *
117     render, GstPad * pad, GstCaps * filter);
118 static GstCaps *gst_dvbsub_overlay_get_src_caps (GstDVBSubOverlay * render,
119     GstPad * pad, GstCaps * filter);
120 
121 static GstFlowReturn gst_dvbsub_overlay_chain_video (GstPad * pad,
122     GstObject * parent, GstBuffer * buf);
123 static GstFlowReturn gst_dvbsub_overlay_chain_text (GstPad * pad,
124     GstObject * parent, GstBuffer * buf);
125 
126 static gboolean gst_dvbsub_overlay_event_video (GstPad * pad,
127     GstObject * parent, GstEvent * event);
128 static gboolean gst_dvbsub_overlay_event_text (GstPad * pad, GstObject * parent,
129     GstEvent * event);
130 static gboolean gst_dvbsub_overlay_event_src (GstPad * pad, GstObject * parent,
131     GstEvent * event);
132 
133 static void new_dvb_subtitles_cb (DvbSub * dvb_sub, DVBSubtitles * subs,
134     gpointer user_data);
135 
136 static gboolean gst_dvbsub_overlay_query_video (GstPad * pad,
137     GstObject * parent, GstQuery * query);
138 static gboolean gst_dvbsub_overlay_query_src (GstPad * pad, GstObject * parent,
139     GstQuery * query);
140 
141 /* initialize the plugin's class */
142 static void
gst_dvbsub_overlay_class_init(GstDVBSubOverlayClass * klass)143 gst_dvbsub_overlay_class_init (GstDVBSubOverlayClass * klass)
144 {
145   GObjectClass *gobject_class = (GObjectClass *) klass;
146   GstElementClass *gstelement_class = (GstElementClass *) klass;
147 
148   gobject_class->set_property = gst_dvbsub_overlay_set_property;
149   gobject_class->get_property = gst_dvbsub_overlay_get_property;
150   gobject_class->finalize = gst_dvbsub_overlay_finalize;
151 
152   g_object_class_install_property (gobject_class, PROP_ENABLE, g_param_spec_boolean ("enable", "Enable",        /* FIXME: "enable" vs "silent"? */
153           "Enable rendering of subtitles", DEFAULT_ENABLE,
154           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 
156   g_object_class_install_property (gobject_class, PROP_MAX_PAGE_TIMEOUT,
157       g_param_spec_int ("max-page-timeout", "max-page-timeout",
158           "Limit maximum display time of a subtitle page (0 - disabled, value in seconds)",
159           0, G_MAXINT, DEFAULT_MAX_PAGE_TIMEOUT,
160           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 
162   g_object_class_install_property (gobject_class, PROP_FORCE_END,
163       g_param_spec_boolean ("force-end", "Force End",
164           "Assume PES-aligned subtitles and force end-of-display",
165           DEFAULT_FORCE_END, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 
167   gstelement_class->change_state =
168       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_change_state);
169 
170   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
171   gst_element_class_add_static_pad_template (gstelement_class,
172       &video_sink_factory);
173   gst_element_class_add_static_pad_template (gstelement_class,
174       &text_sink_factory);
175 
176   gst_element_class_set_static_metadata (gstelement_class,
177       "DVB Subtitles Overlay",
178       "Mixer/Video/Overlay/Subtitle",
179       "Renders DVB subtitles", "Mart Raudsepp <mart.raudsepp@collabora.co.uk>");
180 }
181 
182 static void
gst_dvbsub_overlay_flush_subtitles(GstDVBSubOverlay * render)183 gst_dvbsub_overlay_flush_subtitles (GstDVBSubOverlay * render)
184 {
185   DVBSubtitles *subs;
186 
187   g_mutex_lock (&render->dvbsub_mutex);
188   while ((subs = g_queue_pop_head (render->pending_subtitles))) {
189     dvb_subtitles_free (subs);
190   }
191 
192   if (render->current_subtitle)
193     dvb_subtitles_free (render->current_subtitle);
194   render->current_subtitle = NULL;
195 
196   if (render->current_comp)
197     gst_video_overlay_composition_unref (render->current_comp);
198   render->current_comp = NULL;
199 
200   if (render->dvb_sub)
201     dvb_sub_free (render->dvb_sub);
202 
203   render->dvb_sub = dvb_sub_new ();
204 
205   {
206     DvbSubCallbacks dvbsub_callbacks = { &new_dvb_subtitles_cb, };
207     dvb_sub_set_callbacks (render->dvb_sub, &dvbsub_callbacks, render);
208   }
209 
210   render->last_text_pts = GST_CLOCK_TIME_NONE;
211   render->pending_sub = FALSE;
212 
213   g_mutex_unlock (&render->dvbsub_mutex);
214 }
215 
216 static void
gst_dvbsub_overlay_init(GstDVBSubOverlay * render)217 gst_dvbsub_overlay_init (GstDVBSubOverlay * render)
218 {
219   GST_DEBUG_OBJECT (render, "init");
220 
221   render->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
222   render->video_sinkpad =
223       gst_pad_new_from_static_template (&video_sink_factory, "video_sink");
224   render->text_sinkpad =
225       gst_pad_new_from_static_template (&text_sink_factory, "text_sink");
226 
227   gst_pad_set_chain_function (render->video_sinkpad,
228       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_chain_video));
229   gst_pad_set_chain_function (render->text_sinkpad,
230       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_chain_text));
231 
232   gst_pad_set_event_function (render->video_sinkpad,
233       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_event_video));
234   gst_pad_set_event_function (render->text_sinkpad,
235       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_event_text));
236   gst_pad_set_event_function (render->srcpad,
237       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_event_src));
238 
239   gst_pad_set_query_function (render->video_sinkpad,
240       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_query_video));
241   gst_pad_set_query_function (render->srcpad,
242       GST_DEBUG_FUNCPTR (gst_dvbsub_overlay_query_src));
243 
244   GST_PAD_SET_PROXY_ALLOCATION (render->video_sinkpad);
245 
246   gst_element_add_pad (GST_ELEMENT (render), render->srcpad);
247   gst_element_add_pad (GST_ELEMENT (render), render->video_sinkpad);
248   gst_element_add_pad (GST_ELEMENT (render), render->text_sinkpad);
249 
250   gst_video_info_init (&render->info);
251 
252   render->current_subtitle = NULL;
253   render->pending_subtitles = g_queue_new ();
254 
255   render->enable = DEFAULT_ENABLE;
256   render->max_page_timeout = DEFAULT_MAX_PAGE_TIMEOUT;
257   render->force_end = DEFAULT_FORCE_END;
258 
259   g_mutex_init (&render->dvbsub_mutex);
260   gst_dvbsub_overlay_flush_subtitles (render);
261 
262   gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
263   gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
264 
265   GST_DEBUG_OBJECT (render, "init complete");
266 }
267 
268 static void
gst_dvbsub_overlay_finalize(GObject * object)269 gst_dvbsub_overlay_finalize (GObject * object)
270 {
271   GstDVBSubOverlay *overlay = GST_DVBSUB_OVERLAY (object);
272   DVBSubtitles *subs;
273 
274   while ((subs = g_queue_pop_head (overlay->pending_subtitles))) {
275     dvb_subtitles_free (subs);
276   }
277   g_queue_free (overlay->pending_subtitles);
278 
279   if (overlay->current_subtitle)
280     dvb_subtitles_free (overlay->current_subtitle);
281   overlay->current_subtitle = NULL;
282 
283   if (overlay->current_comp)
284     gst_video_overlay_composition_unref (overlay->current_comp);
285   overlay->current_comp = NULL;
286 
287   if (overlay->dvb_sub)
288     dvb_sub_free (overlay->dvb_sub);
289 
290   g_mutex_clear (&overlay->dvbsub_mutex);
291 
292   G_OBJECT_CLASS (parent_class)->finalize (object);
293 }
294 
295 static void
gst_dvbsub_overlay_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)296 gst_dvbsub_overlay_set_property (GObject * object, guint prop_id,
297     const GValue * value, GParamSpec * pspec)
298 {
299   GstDVBSubOverlay *overlay = GST_DVBSUB_OVERLAY (object);
300 
301   switch (prop_id) {
302     case PROP_ENABLE:
303       g_atomic_int_set (&overlay->enable, g_value_get_boolean (value));
304       break;
305     case PROP_MAX_PAGE_TIMEOUT:
306       g_atomic_int_set (&overlay->max_page_timeout, g_value_get_int (value));
307       break;
308     case PROP_FORCE_END:
309       g_atomic_int_set (&overlay->force_end, g_value_get_boolean (value));
310       break;
311     default:
312       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313       break;
314   }
315 }
316 
317 static void
gst_dvbsub_overlay_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)318 gst_dvbsub_overlay_get_property (GObject * object, guint prop_id,
319     GValue * value, GParamSpec * pspec)
320 {
321   GstDVBSubOverlay *overlay = GST_DVBSUB_OVERLAY (object);
322 
323   switch (prop_id) {
324     case PROP_ENABLE:
325       g_value_set_boolean (value, g_atomic_int_get (&overlay->enable));
326       break;
327     case PROP_MAX_PAGE_TIMEOUT:
328       g_value_set_int (value, g_atomic_int_get (&overlay->max_page_timeout));
329       break;
330     case PROP_FORCE_END:
331       g_value_set_boolean (value, g_atomic_int_get (&overlay->force_end));
332       break;
333     default:
334       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
335       break;
336   }
337 }
338 
339 static GstStateChangeReturn
gst_dvbsub_overlay_change_state(GstElement * element,GstStateChange transition)340 gst_dvbsub_overlay_change_state (GstElement * element,
341     GstStateChange transition)
342 {
343   GstDVBSubOverlay *render = GST_DVBSUB_OVERLAY (element);
344   GstStateChangeReturn ret;
345 
346   switch (transition) {
347     case GST_STATE_CHANGE_READY_TO_PAUSED:
348       gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
349       gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
350       break;
351     case GST_STATE_CHANGE_NULL_TO_READY:
352     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
353     default:
354       break;
355   }
356 
357   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
358 
359   switch (transition) {
360     case GST_STATE_CHANGE_PAUSED_TO_READY:
361       gst_dvbsub_overlay_flush_subtitles (render);
362       gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
363       gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
364       gst_video_info_init (&render->info);
365       break;
366     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
367     case GST_STATE_CHANGE_READY_TO_NULL:
368     default:
369       break;
370   }
371 
372 
373   return ret;
374 }
375 
376 static gboolean
gst_dvbsub_overlay_query_src(GstPad * pad,GstObject * parent,GstQuery * query)377 gst_dvbsub_overlay_query_src (GstPad * pad, GstObject * parent,
378     GstQuery * query)
379 {
380   GstDVBSubOverlay *render = GST_DVBSUB_OVERLAY (parent);
381   gboolean ret;
382 
383   switch (GST_QUERY_TYPE (query)) {
384     case GST_QUERY_CAPS:
385     {
386       GstCaps *filter, *caps;
387 
388       gst_query_parse_caps (query, &filter);
389       caps = gst_dvbsub_overlay_get_src_caps (render, pad, filter);
390       gst_query_set_caps_result (query, caps);
391       gst_caps_unref (caps);
392       ret = TRUE;
393       break;
394     }
395     default:
396       ret = gst_pad_query_default (pad, parent, query);
397       break;
398   }
399 
400   return ret;
401 }
402 
403 static gboolean
gst_dvbsub_overlay_event_src(GstPad * pad,GstObject * parent,GstEvent * event)404 gst_dvbsub_overlay_event_src (GstPad * pad, GstObject * parent,
405     GstEvent * event)
406 {
407   GstDVBSubOverlay *render = GST_DVBSUB_OVERLAY (parent);
408   gboolean ret = FALSE;
409 
410   gst_event_ref (event);
411   ret = gst_pad_push_event (render->video_sinkpad, event);
412   gst_pad_push_event (render->text_sinkpad, event);
413 
414   return ret;
415 }
416 
417 /**
418  * gst_dvbsub_overlay_add_feature_and_intersect:
419  *
420  * Creates a new #GstCaps containing the (given caps +
421  * given caps feature) + (given caps intersected by the
422  * given filter).
423  *
424  * Returns: the new #GstCaps
425  */
426 static GstCaps *
gst_dvbsub_overlay_add_feature_and_intersect(GstCaps * caps,const gchar * feature,GstCaps * filter)427 gst_dvbsub_overlay_add_feature_and_intersect (GstCaps * caps,
428     const gchar * feature, GstCaps * filter)
429 {
430   int i, caps_size;
431   GstCaps *new_caps;
432 
433   new_caps = gst_caps_copy (caps);
434 
435   caps_size = gst_caps_get_size (new_caps);
436   for (i = 0; i < caps_size; i++) {
437     GstCapsFeatures *features = gst_caps_get_features (new_caps, i);
438     if (!gst_caps_features_is_any (features)) {
439       gst_caps_features_add (features, feature);
440     }
441   }
442 
443   gst_caps_append (new_caps, gst_caps_intersect_full (caps,
444           filter, GST_CAPS_INTERSECT_FIRST));
445 
446   return new_caps;
447 }
448 
449 /**
450  * gst_dvbsub_overlay_intersect_by_feature:
451  *
452  * Creates a new #GstCaps based on the following filtering rule.
453  *
454  * For each individual caps contained in given caps, if the
455  * caps uses the given caps feature, keep a version of the caps
456  * with the feature and an another one without. Otherwise, intersect
457  * the caps with the given filter.
458  *
459  * Returns: the new #GstCaps
460  */
461 static GstCaps *
gst_dvbsub_overlay_intersect_by_feature(GstCaps * caps,const gchar * feature,GstCaps * filter)462 gst_dvbsub_overlay_intersect_by_feature (GstCaps * caps,
463     const gchar * feature, GstCaps * filter)
464 {
465   int i, caps_size;
466   GstCaps *new_caps;
467 
468   new_caps = gst_caps_new_empty ();
469 
470   caps_size = gst_caps_get_size (caps);
471   for (i = 0; i < caps_size; i++) {
472     GstStructure *caps_structure = gst_caps_get_structure (caps, i);
473     GstCapsFeatures *caps_features =
474         gst_caps_features_copy (gst_caps_get_features (caps, i));
475     GstCaps *filtered_caps;
476     GstCaps *simple_caps =
477         gst_caps_new_full (gst_structure_copy (caps_structure), NULL);
478     gst_caps_set_features (simple_caps, 0, caps_features);
479 
480     if (gst_caps_features_contains (caps_features, feature)) {
481       gst_caps_append (new_caps, gst_caps_copy (simple_caps));
482 
483       gst_caps_features_remove (caps_features, feature);
484       filtered_caps = gst_caps_ref (simple_caps);
485     } else {
486       filtered_caps = gst_caps_intersect_full (simple_caps, filter,
487           GST_CAPS_INTERSECT_FIRST);
488     }
489 
490     gst_caps_unref (simple_caps);
491     gst_caps_append (new_caps, filtered_caps);
492   }
493 
494   return new_caps;
495 }
496 
497 static GstCaps *
gst_dvbsub_overlay_get_videosink_caps(GstDVBSubOverlay * render,GstPad * pad,GstCaps * filter)498 gst_dvbsub_overlay_get_videosink_caps (GstDVBSubOverlay * render, GstPad * pad,
499     GstCaps * filter)
500 {
501   GstPad *srcpad = render->srcpad;
502   GstCaps *peer_caps = NULL, *caps = NULL, *dvdsub_overlay_filter = NULL;
503 
504   if (filter) {
505     /* filter caps + composition feature + filter caps
506      * filtered by the software caps. */
507     GstCaps *sw_caps = gst_static_caps_get (&sw_template_caps);
508     dvdsub_overlay_filter =
509         gst_dvbsub_overlay_add_feature_and_intersect (filter,
510         GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, sw_caps);
511     gst_caps_unref (sw_caps);
512 
513     GST_DEBUG_OBJECT (render, "dvdsub_overlay filter %" GST_PTR_FORMAT,
514         dvdsub_overlay_filter);
515   }
516 
517   peer_caps = gst_pad_peer_query_caps (srcpad, dvdsub_overlay_filter);
518 
519   if (dvdsub_overlay_filter)
520     gst_caps_unref (dvdsub_overlay_filter);
521 
522   if (peer_caps) {
523 
524     GST_DEBUG_OBJECT (pad, "peer caps  %" GST_PTR_FORMAT, peer_caps);
525 
526     if (gst_caps_is_any (peer_caps)) {
527 
528       /* if peer returns ANY caps, return filtered src pad template caps */
529       caps = gst_pad_get_pad_template_caps (srcpad);
530 
531     } else {
532 
533       /* duplicate caps which contains the composition into one version with
534        * the meta and one without. Filter the other caps by the software caps */
535       GstCaps *sw_caps = gst_static_caps_get (&sw_template_caps);
536       caps = gst_dvbsub_overlay_intersect_by_feature (peer_caps,
537           GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, sw_caps);
538       gst_caps_unref (sw_caps);
539     }
540 
541     gst_caps_unref (peer_caps);
542 
543   } else {
544     /* no peer, our padtemplate is enough then */
545     caps = gst_pad_get_pad_template_caps (pad);
546   }
547 
548   if (filter) {
549     GstCaps *intersection;
550 
551     intersection =
552         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
553     gst_caps_unref (caps);
554     caps = intersection;
555   }
556 
557   GST_DEBUG_OBJECT (render, "returning  %" GST_PTR_FORMAT, caps);
558 
559   return caps;
560 }
561 
562 static GstCaps *
gst_dvbsub_overlay_get_src_caps(GstDVBSubOverlay * render,GstPad * pad,GstCaps * filter)563 gst_dvbsub_overlay_get_src_caps (GstDVBSubOverlay * render, GstPad * pad,
564     GstCaps * filter)
565 {
566   GstPad *sinkpad = render->video_sinkpad;
567   GstCaps *peer_caps = NULL, *caps = NULL, *dvdsub_overlay_filter = NULL;
568 
569   if (filter) {
570     /* duplicate filter caps which contains the composition into one version
571      * with the meta and one without. Filter the other caps by the software
572      * caps */
573     GstCaps *sw_caps = gst_static_caps_get (&sw_template_caps);
574     dvdsub_overlay_filter =
575         gst_dvbsub_overlay_intersect_by_feature (filter,
576         GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, sw_caps);
577     gst_caps_unref (sw_caps);
578   }
579 
580   peer_caps = gst_pad_peer_query_caps (sinkpad, dvdsub_overlay_filter);
581 
582   if (dvdsub_overlay_filter)
583     gst_caps_unref (dvdsub_overlay_filter);
584 
585   if (peer_caps) {
586 
587     GST_DEBUG_OBJECT (pad, "peer caps  %" GST_PTR_FORMAT, peer_caps);
588 
589     if (gst_caps_is_any (peer_caps)) {
590 
591       /* if peer returns ANY caps, return filtered sink pad template caps */
592       caps = gst_pad_get_pad_template_caps (sinkpad);
593     } else {
594 
595       /* return upstream caps + composition feature + upstream caps
596        * filtered by the software caps. */
597       GstCaps *sw_caps = gst_static_caps_get (&sw_template_caps);
598       caps = gst_dvbsub_overlay_add_feature_and_intersect (peer_caps,
599           GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, sw_caps);
600       gst_caps_unref (sw_caps);
601     }
602 
603     gst_caps_unref (peer_caps);
604 
605   } else {
606     /* no peer, our padtemplate is enough then */
607     caps = gst_pad_get_pad_template_caps (pad);
608   }
609 
610   if (filter) {
611     GstCaps *intersection = gst_caps_intersect_full (filter, caps,
612         GST_CAPS_INTERSECT_FIRST);
613     gst_caps_unref (caps);
614     caps = intersection;
615   }
616 
617   GST_DEBUG_OBJECT (render, "returning  %" GST_PTR_FORMAT, caps);
618 
619   return caps;
620 }
621 
622 static gboolean
gst_dvbsub_overlay_can_handle_caps(GstCaps * incaps)623 gst_dvbsub_overlay_can_handle_caps (GstCaps * incaps)
624 {
625   gboolean ret;
626   GstCaps *caps;
627   static GstStaticCaps static_caps = GST_STATIC_CAPS (DVBSUB_OVERLAY_CAPS);
628 
629   caps = gst_static_caps_get (&static_caps);
630   ret = gst_caps_is_subset (incaps, caps);
631   gst_caps_unref (caps);
632 
633   return ret;
634 }
635 
636 /* only negotiate/query video overlay composition support for now */
637 static gboolean
gst_dvbsub_overlay_negotiate(GstDVBSubOverlay * overlay,GstCaps * caps)638 gst_dvbsub_overlay_negotiate (GstDVBSubOverlay * overlay, GstCaps * caps)
639 {
640   gboolean ret;
641   gboolean attach = FALSE;
642   gboolean caps_has_meta = TRUE;
643   GstCapsFeatures *f;
644 
645   GST_DEBUG_OBJECT (overlay, "performing negotiation");
646 
647   if (!caps) {
648     caps = gst_pad_get_current_caps (overlay->srcpad);
649   } else {
650     gst_caps_ref (caps);
651   }
652 
653   if (!caps || gst_caps_is_empty (caps))
654     goto no_format;
655 
656   /* Try to use the overlay meta if possible */
657   f = gst_caps_get_features (caps, 0);
658 
659   /* if the caps doesn't have the overlay meta, we query if downstream
660    * accepts it before trying the version without the meta
661    * If upstream already is using the meta then we can only use it */
662   if (!f
663       || !gst_caps_features_contains (f,
664           GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION)) {
665     GstCaps *overlay_caps;
666     GstCaps *peercaps;
667 
668     /* In this case we added the meta, but we can work without it
669      * so preserve the original caps so we can use it as a fallback */
670     overlay_caps = gst_caps_copy (caps);
671 
672     f = gst_caps_get_features (overlay_caps, 0);
673     gst_caps_features_add (f,
674         GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION);
675 
676     /* FIXME: We should probably check if downstream *prefers* the
677      * overlay meta, and only enforce usage of it if we can't handle
678      * the format ourselves and thus would have to drop the overlays.
679      * Otherwise we should prefer what downstream wants here.
680      */
681     peercaps = gst_pad_peer_query_caps (overlay->srcpad, NULL);
682     caps_has_meta = gst_caps_can_intersect (peercaps, overlay_caps);
683     gst_caps_unref (peercaps);
684 
685     GST_DEBUG_OBJECT (overlay, "Downstream accepts the overlay meta: %d",
686         caps_has_meta);
687     if (caps_has_meta) {
688       gst_caps_unref (caps);
689       caps = overlay_caps;
690 
691     } else {
692       /* fallback to the original */
693       gst_caps_unref (overlay_caps);
694       caps_has_meta = FALSE;
695     }
696 
697   }
698   GST_DEBUG_OBJECT (overlay, "Using caps %" GST_PTR_FORMAT, caps);
699   ret = gst_pad_set_caps (overlay->srcpad, caps);
700 
701   if (ret) {
702     GstQuery *query;
703 
704     /* find supported meta */
705     query = gst_query_new_allocation (caps, FALSE);
706 
707     if (!gst_pad_peer_query (overlay->srcpad, query)) {
708       /* no problem, we use the query defaults */
709       GST_DEBUG_OBJECT (overlay, "ALLOCATION query failed");
710     }
711 
712     if (caps_has_meta && gst_query_find_allocation_meta (query,
713             GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL))
714       attach = TRUE;
715 
716     overlay->attach_compo_to_buffer = attach;
717 
718     gst_query_unref (query);
719   }
720   gst_caps_unref (caps);
721 
722   return ret;
723 
724 no_format:
725   {
726     if (caps)
727       gst_caps_unref (caps);
728     return FALSE;
729   }
730 }
731 
732 static gboolean
gst_dvbsub_overlay_setcaps_video(GstPad * pad,GstCaps * caps)733 gst_dvbsub_overlay_setcaps_video (GstPad * pad, GstCaps * caps)
734 {
735   GstDVBSubOverlay *render = GST_DVBSUB_OVERLAY (gst_pad_get_parent (pad));
736   gboolean ret = FALSE;
737   GstVideoInfo info;
738 
739   if (!gst_video_info_from_caps (&info, caps))
740     goto invalid_caps;
741 
742   render->info = info;
743 
744   ret = gst_dvbsub_overlay_negotiate (render, caps);
745 
746   if (!render->attach_compo_to_buffer &&
747       !gst_dvbsub_overlay_can_handle_caps (caps))
748     goto unsupported_caps;
749 
750   GST_DEBUG_OBJECT (render, "dvbsub overlay renderer setup complete");
751 
752 out:
753   gst_object_unref (render);
754 
755   return ret;
756 
757   /* ERRORS */
758 invalid_caps:
759   {
760     GST_ERROR_OBJECT (render, "Can't parse caps: %" GST_PTR_FORMAT, caps);
761     ret = FALSE;
762     goto out;
763   }
764 unsupported_caps:
765   {
766     GST_ERROR_OBJECT (render, "Unsupported caps: %" GST_PTR_FORMAT, caps);
767     ret = FALSE;
768     goto out;
769   }
770 }
771 
772 static void
gst_dvbsub_overlay_process_text(GstDVBSubOverlay * overlay,GstBuffer * buffer,guint64 pts)773 gst_dvbsub_overlay_process_text (GstDVBSubOverlay * overlay, GstBuffer * buffer,
774     guint64 pts)
775 {
776   GstMapInfo map;
777 
778   GST_DEBUG_OBJECT (overlay,
779       "Processing subtitles with PTS=%" G_GUINT64_FORMAT
780       " which is a time of %" GST_TIME_FORMAT, pts, GST_TIME_ARGS (pts));
781 
782   gst_buffer_map (buffer, &map, GST_MAP_READ);
783 
784   GST_DEBUG_OBJECT (overlay, "Feeding %" G_GSIZE_FORMAT " bytes to libdvbsub",
785       map.size);
786 
787   g_mutex_lock (&overlay->dvbsub_mutex);
788   overlay->pending_sub = TRUE;
789   dvb_sub_feed_with_pts (overlay->dvb_sub, pts, map.data, map.size);
790   g_mutex_unlock (&overlay->dvbsub_mutex);
791 
792   gst_buffer_unmap (buffer, &map);
793   gst_buffer_unref (buffer);
794 
795   if (overlay->pending_sub && overlay->force_end) {
796     GST_DEBUG_OBJECT (overlay, "forcing subtitle end");
797     dvb_sub_feed_with_pts (overlay->dvb_sub, overlay->last_text_pts, NULL, 0);
798     g_assert (overlay->pending_sub == FALSE);
799   }
800 }
801 
802 static void
new_dvb_subtitles_cb(DvbSub * dvb_sub,DVBSubtitles * subs,gpointer user_data)803 new_dvb_subtitles_cb (DvbSub * dvb_sub, DVBSubtitles * subs, gpointer user_data)
804 {
805   GstDVBSubOverlay *overlay = GST_DVBSUB_OVERLAY (user_data);
806   int max_page_timeout;
807   guint64 start, stop;
808 
809   max_page_timeout = g_atomic_int_get (&overlay->max_page_timeout);
810   if (max_page_timeout > 0)
811     subs->page_time_out = MIN (subs->page_time_out, max_page_timeout);
812 
813   GST_INFO_OBJECT (overlay,
814       "New DVB subtitles arrived with a page_time_out of %d and %d regions for "
815       "PTS=%" G_GUINT64_FORMAT ", which should be at time %" GST_TIME_FORMAT,
816       subs->page_time_out, subs->num_rects, subs->pts,
817       GST_TIME_ARGS (subs->pts));
818 
819   /* spec says page_time_out is not to be taken very accurately anyway,
820    * and 0 does not make useful sense anyway */
821   if (!subs->page_time_out) {
822     GST_WARNING_OBJECT (overlay, "overriding page_time_out 0");
823     subs->page_time_out = 1;
824   }
825 
826   /* clip and convert to running time */
827   start = subs->pts;
828   stop = subs->pts + subs->page_time_out;
829 
830   if (!(gst_segment_clip (&overlay->subtitle_segment, GST_FORMAT_TIME,
831               start, stop, &start, &stop)))
832     goto out_of_segment;
833 
834   subs->page_time_out = stop - start;
835 
836   gst_segment_to_running_time (&overlay->subtitle_segment, GST_FORMAT_TIME,
837       start);
838   g_assert (GST_CLOCK_TIME_IS_VALID (start));
839   subs->pts = start;
840 
841   GST_DEBUG_OBJECT (overlay, "SUBTITLE real running time: %" GST_TIME_FORMAT,
842       GST_TIME_ARGS (start));
843 
844   g_queue_push_tail (overlay->pending_subtitles, subs);
845   overlay->pending_sub = FALSE;
846 
847   return;
848 
849 out_of_segment:
850   {
851     GST_DEBUG_OBJECT (overlay, "subtitle out of segment, discarding");
852     dvb_subtitles_free (subs);
853   }
854 }
855 
856 static GstFlowReturn
gst_dvbsub_overlay_chain_text(GstPad * pad,GstObject * parent,GstBuffer * buffer)857 gst_dvbsub_overlay_chain_text (GstPad * pad, GstObject * parent,
858     GstBuffer * buffer)
859 {
860   GstDVBSubOverlay *overlay = GST_DVBSUB_OVERLAY (parent);
861 
862   GST_INFO_OBJECT (overlay,
863       "subpicture/x-dvb buffer with size %" G_GSIZE_FORMAT,
864       gst_buffer_get_size (buffer));
865 
866   GST_LOG_OBJECT (overlay,
867       "Video segment: %" GST_SEGMENT_FORMAT " --- Subtitle segment: %"
868       GST_SEGMENT_FORMAT " --- BUFFER: ts=%" GST_TIME_FORMAT,
869       &overlay->video_segment, &overlay->subtitle_segment,
870       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
871 
872   /* DVB subtitle packets are required to carry the PTS */
873   if (G_UNLIKELY (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer))) {
874     GST_WARNING_OBJECT (overlay,
875         "Text buffer without valid timestamp, dropping");
876     gst_buffer_unref (buffer);
877     return GST_FLOW_OK;
878   }
879 
880   /* spec states multiple PES packets may have same PTS,
881    * and same PTS packets make up a display set */
882   if (overlay->pending_sub &&
883       overlay->last_text_pts != GST_BUFFER_TIMESTAMP (buffer)) {
884     GST_DEBUG_OBJECT (overlay, "finishing previous subtitle");
885     dvb_sub_feed_with_pts (overlay->dvb_sub, overlay->last_text_pts, NULL, 0);
886     overlay->pending_sub = FALSE;
887   }
888 
889   overlay->last_text_pts = GST_BUFFER_TIMESTAMP (buffer);
890 
891   /* As the passed start and stop is equal, we shouldn't need to care about out of segment at all,
892    * the subtitle data for the PTS is completely out of interest to us. A given display set must
893    * carry the same PTS value. */
894   /* FIXME: Consider with larger than 64kB display sets, which would be cut into multiple packets,
895    * FIXME: does our waiting + render code work when there are more than one packets before
896    * FIXME: rendering callback will get called? */
897 
898   overlay->subtitle_segment.position = GST_BUFFER_TIMESTAMP (buffer);
899 
900   gst_dvbsub_overlay_process_text (overlay, buffer,
901       GST_BUFFER_TIMESTAMP (buffer));
902 
903   return GST_FLOW_OK;
904 }
905 
906 static GstVideoOverlayComposition *
gst_dvbsub_overlay_subs_to_comp(GstDVBSubOverlay * overlay,DVBSubtitles * subs)907 gst_dvbsub_overlay_subs_to_comp (GstDVBSubOverlay * overlay,
908     DVBSubtitles * subs)
909 {
910   GstVideoOverlayComposition *comp = NULL;
911   GstVideoOverlayRectangle *rect;
912   gint width, height, dw, dh, wx, wy;
913   gint i;
914 
915   g_return_val_if_fail (subs != NULL && subs->num_rects > 0, NULL);
916 
917   width = GST_VIDEO_INFO_WIDTH (&overlay->info);
918   height = GST_VIDEO_INFO_HEIGHT (&overlay->info);
919 
920   dw = subs->display_def.display_width;
921   dh = subs->display_def.display_height;
922 
923   GST_LOG_OBJECT (overlay,
924       "converting %d rectangles for display %dx%d -> video %dx%d",
925       subs->num_rects, dw, dh, width, height);
926 
927   if (subs->display_def.window_flag) {
928     wx = subs->display_def.window_x;
929     wy = subs->display_def.window_y;
930     GST_LOG_OBJECT (overlay, "display window %dx%d @ (%d, %d)",
931         subs->display_def.window_width, subs->display_def.window_height,
932         wx, wy);
933   } else {
934     wx = 0;
935     wy = 0;
936   }
937 
938   for (i = 0; i < subs->num_rects; i++) {
939     DVBSubtitleRect *srect = &subs->rects[i];
940     GstBuffer *buf;
941     gint w, h;
942     guint8 *in_data;
943     guint32 *palette, *data;
944     gint rx, ry, rw, rh, stride;
945     gint k, l;
946     GstMapInfo map;
947 
948     GST_LOG_OBJECT (overlay, "rectangle %d: %dx%d @ (%d, %d)", i,
949         srect->w, srect->h, srect->x, srect->y);
950 
951     w = srect->w;
952     h = srect->h;
953 
954     buf = gst_buffer_new_and_alloc (w * h * 4);
955     gst_buffer_map (buf, &map, GST_MAP_WRITE);
956     data = (guint32 *) map.data;
957     in_data = srect->pict.data;
958     palette = srect->pict.palette;
959     stride = srect->pict.rowstride;
960     for (k = 0; k < h; k++) {
961       for (l = 0; l < w; l++) {
962         guint32 ayuv;
963 
964         ayuv = palette[*in_data];
965         GST_WRITE_UINT32_BE (data, ayuv);
966         in_data++;
967         data++;
968       }
969       in_data += stride - w;
970     }
971     gst_buffer_unmap (buf, &map);
972 
973     /* this is assuming the subtitle rectangle coordinates are relative
974      * to the window (if there is one) within a display of specified dimension.
975      * Coordinate wrt the latter is then scaled to the actual dimension of
976      * the video we are dealing with here. */
977     rx = gst_util_uint64_scale (wx + srect->x, width, dw);
978     ry = gst_util_uint64_scale (wy + srect->y, height, dh);
979     rw = gst_util_uint64_scale (srect->w, width, dw);
980     rh = gst_util_uint64_scale (srect->h, height, dh);
981 
982     GST_LOG_OBJECT (overlay, "rectangle %d rendered: %dx%d @ (%d, %d)", i,
983         rw, rh, rx, ry);
984 
985     gst_buffer_add_video_meta (buf, GST_VIDEO_FRAME_FLAG_NONE,
986         GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_YUV, w, h);
987     rect = gst_video_overlay_rectangle_new_raw (buf, rx, ry, rw, rh, 0);
988     g_assert (rect);
989     if (comp) {
990       gst_video_overlay_composition_add_rectangle (comp, rect);
991     } else {
992       comp = gst_video_overlay_composition_new (rect);
993     }
994     gst_video_overlay_rectangle_unref (rect);
995     gst_buffer_unref (buf);
996   }
997 
998   return comp;
999 }
1000 
1001 static GstFlowReturn
gst_dvbsub_overlay_chain_video(GstPad * pad,GstObject * parent,GstBuffer * buffer)1002 gst_dvbsub_overlay_chain_video (GstPad * pad, GstObject * parent,
1003     GstBuffer * buffer)
1004 {
1005   GstDVBSubOverlay *overlay = GST_DVBSUB_OVERLAY (parent);
1006   GstFlowReturn ret = GST_FLOW_OK;
1007   gint64 start, stop;
1008   guint64 cstart, cstop;
1009   gboolean in_seg;
1010   GstClockTime vid_running_time, vid_running_time_end;
1011 
1012   if (GST_VIDEO_INFO_FORMAT (&overlay->info) == GST_VIDEO_FORMAT_UNKNOWN)
1013     return GST_FLOW_NOT_NEGOTIATED;
1014 
1015   if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1016     goto missing_timestamp;
1017 
1018   start = GST_BUFFER_TIMESTAMP (buffer);
1019 
1020   GST_LOG_OBJECT (overlay,
1021       "Video segment: %" GST_SEGMENT_FORMAT " --- Subtitle position: %"
1022       GST_TIME_FORMAT " --- BUFFER: ts=%" GST_TIME_FORMAT,
1023       &overlay->video_segment,
1024       GST_TIME_ARGS (overlay->subtitle_segment.position),
1025       GST_TIME_ARGS (start));
1026 
1027   /* ignore buffers that are outside of the current segment */
1028   if (!GST_BUFFER_DURATION_IS_VALID (buffer)) {
1029     stop = GST_CLOCK_TIME_NONE;
1030   } else {
1031     stop = start + GST_BUFFER_DURATION (buffer);
1032   }
1033 
1034   in_seg = gst_segment_clip (&overlay->video_segment, GST_FORMAT_TIME,
1035       start, stop, &cstart, &cstop);
1036   if (!in_seg) {
1037     GST_DEBUG_OBJECT (overlay, "Buffer outside configured segment -- dropping");
1038     gst_buffer_unref (buffer);
1039     return GST_FLOW_OK;
1040   }
1041 
1042   buffer = gst_buffer_make_writable (buffer);
1043   GST_BUFFER_TIMESTAMP (buffer) = cstart;
1044   if (GST_BUFFER_DURATION_IS_VALID (buffer))
1045     GST_BUFFER_DURATION (buffer) = cstop - cstart;
1046 
1047   vid_running_time =
1048       gst_segment_to_running_time (&overlay->video_segment, GST_FORMAT_TIME,
1049       cstart);
1050   if (GST_BUFFER_DURATION_IS_VALID (buffer))
1051     vid_running_time_end =
1052         gst_segment_to_running_time (&overlay->video_segment, GST_FORMAT_TIME,
1053         cstop);
1054   else
1055     vid_running_time_end = vid_running_time;
1056 
1057   GST_DEBUG_OBJECT (overlay, "Video running time: %" GST_TIME_FORMAT,
1058       GST_TIME_ARGS (vid_running_time));
1059 
1060   overlay->video_segment.position = GST_BUFFER_TIMESTAMP (buffer);
1061 
1062   g_mutex_lock (&overlay->dvbsub_mutex);
1063   if (!g_queue_is_empty (overlay->pending_subtitles)) {
1064     DVBSubtitles *tmp, *candidate = NULL;
1065 
1066     while (!g_queue_is_empty (overlay->pending_subtitles)) {
1067       tmp = g_queue_peek_head (overlay->pending_subtitles);
1068 
1069       if (tmp->pts > vid_running_time_end) {
1070         /* For a future video frame */
1071         break;
1072       } else if (tmp->num_rects == 0) {
1073         /* Clear screen */
1074         if (overlay->current_subtitle)
1075           dvb_subtitles_free (overlay->current_subtitle);
1076         overlay->current_subtitle = NULL;
1077         if (candidate)
1078           dvb_subtitles_free (candidate);
1079         candidate = NULL;
1080         g_queue_pop_head (overlay->pending_subtitles);
1081         dvb_subtitles_free (tmp);
1082         tmp = NULL;
1083       } else if (tmp->pts + tmp->page_time_out * GST_SECOND *
1084           ABS (overlay->subtitle_segment.rate) >= vid_running_time) {
1085         if (candidate)
1086           dvb_subtitles_free (candidate);
1087         candidate = tmp;
1088         g_queue_pop_head (overlay->pending_subtitles);
1089       } else {
1090         /* Too late */
1091         dvb_subtitles_free (tmp);
1092         tmp = NULL;
1093         g_queue_pop_head (overlay->pending_subtitles);
1094       }
1095     }
1096 
1097     if (candidate) {
1098       GST_DEBUG_OBJECT (overlay,
1099           "Time to show the next subtitle page (%" GST_TIME_FORMAT " >= %"
1100           GST_TIME_FORMAT ") - it has %u regions",
1101           GST_TIME_ARGS (vid_running_time), GST_TIME_ARGS (candidate->pts),
1102           candidate->num_rects);
1103       dvb_subtitles_free (overlay->current_subtitle);
1104       overlay->current_subtitle = candidate;
1105       if (overlay->current_comp)
1106         gst_video_overlay_composition_unref (overlay->current_comp);
1107       overlay->current_comp =
1108           gst_dvbsub_overlay_subs_to_comp (overlay, overlay->current_subtitle);
1109     }
1110   }
1111 
1112   /* Check that we haven't hit the fallback timeout for current subtitle page */
1113   if (overlay->current_subtitle
1114       && vid_running_time >
1115       (overlay->current_subtitle->pts +
1116           overlay->current_subtitle->page_time_out * GST_SECOND *
1117           ABS (overlay->subtitle_segment.rate))) {
1118     GST_INFO_OBJECT (overlay,
1119         "Subtitle page not redefined before fallback page_time_out of %u seconds (missed data?) - deleting current page",
1120         overlay->current_subtitle->page_time_out);
1121     dvb_subtitles_free (overlay->current_subtitle);
1122     overlay->current_subtitle = NULL;
1123   }
1124 
1125   /* Now render it */
1126   if (g_atomic_int_get (&overlay->enable) && overlay->current_subtitle) {
1127     GstVideoFrame frame;
1128 
1129     g_assert (overlay->current_comp);
1130     if (overlay->attach_compo_to_buffer) {
1131       GST_DEBUG_OBJECT (overlay, "Attaching overlay image to video buffer");
1132       gst_buffer_add_video_overlay_composition_meta (buffer,
1133           overlay->current_comp);
1134     } else {
1135       GST_DEBUG_OBJECT (overlay, "Blending overlay image to video buffer");
1136       gst_video_frame_map (&frame, &overlay->info, buffer, GST_MAP_READWRITE);
1137       gst_video_overlay_composition_blend (overlay->current_comp, &frame);
1138       gst_video_frame_unmap (&frame);
1139     }
1140   }
1141   g_mutex_unlock (&overlay->dvbsub_mutex);
1142 
1143   ret = gst_pad_push (overlay->srcpad, buffer);
1144 
1145   return ret;
1146 
1147 missing_timestamp:
1148   {
1149     GST_WARNING_OBJECT (overlay, "video buffer without timestamp, discarding");
1150     gst_buffer_unref (buffer);
1151     return GST_FLOW_OK;
1152   }
1153 }
1154 
1155 static gboolean
gst_dvbsub_overlay_query_video(GstPad * pad,GstObject * parent,GstQuery * query)1156 gst_dvbsub_overlay_query_video (GstPad * pad, GstObject * parent,
1157     GstQuery * query)
1158 {
1159   GstDVBSubOverlay *render = (GstDVBSubOverlay *) parent;
1160   gboolean ret;
1161 
1162   switch (GST_QUERY_TYPE (query)) {
1163     case GST_QUERY_CAPS:
1164     {
1165       GstCaps *filter, *caps;
1166 
1167       gst_query_parse_caps (query, &filter);
1168       caps = gst_dvbsub_overlay_get_videosink_caps (render, pad, filter);
1169       gst_query_set_caps_result (query, caps);
1170       gst_caps_unref (caps);
1171       ret = TRUE;
1172       break;
1173     }
1174     default:
1175       ret = gst_pad_query_default (pad, parent, query);
1176       break;
1177   }
1178 
1179   return ret;
1180 }
1181 
1182 static gboolean
gst_dvbsub_overlay_event_video(GstPad * pad,GstObject * parent,GstEvent * event)1183 gst_dvbsub_overlay_event_video (GstPad * pad, GstObject * parent,
1184     GstEvent * event)
1185 {
1186   gboolean ret = FALSE;
1187   GstDVBSubOverlay *render = GST_DVBSUB_OVERLAY (parent);
1188 
1189   GST_DEBUG_OBJECT (pad, "received video event %s",
1190       GST_EVENT_TYPE_NAME (event));
1191 
1192   switch (GST_EVENT_TYPE (event)) {
1193     case GST_EVENT_CAPS:
1194     {
1195       GstCaps *caps;
1196 
1197       gst_event_parse_caps (event, &caps);
1198       ret = gst_dvbsub_overlay_setcaps_video (pad, caps);
1199       gst_event_unref (event);
1200       break;
1201     }
1202     case GST_EVENT_SEGMENT:
1203     {
1204       GstSegment seg;
1205 
1206       GST_DEBUG_OBJECT (render, "received new segment");
1207 
1208       gst_event_copy_segment (event, &seg);
1209 
1210       if (seg.format == GST_FORMAT_TIME) {
1211         GST_DEBUG_OBJECT (render, "VIDEO SEGMENT now: %" GST_SEGMENT_FORMAT,
1212             &render->video_segment);
1213 
1214         render->video_segment = seg;
1215 
1216         GST_DEBUG_OBJECT (render, "VIDEO SEGMENT after: %" GST_SEGMENT_FORMAT,
1217             &render->video_segment);
1218         ret = gst_pad_push_event (render->srcpad, event);
1219       } else {
1220         GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1221             ("received non-TIME newsegment event on video input"));
1222         ret = FALSE;
1223         gst_event_unref (event);
1224       }
1225       break;
1226     }
1227     case GST_EVENT_FLUSH_STOP:
1228       gst_segment_init (&render->video_segment, GST_FORMAT_TIME);
1229     default:
1230       ret = gst_pad_push_event (render->srcpad, event);
1231       break;
1232   }
1233 
1234   return ret;
1235 }
1236 
1237 static gboolean
gst_dvbsub_overlay_event_text(GstPad * pad,GstObject * parent,GstEvent * event)1238 gst_dvbsub_overlay_event_text (GstPad * pad, GstObject * parent,
1239     GstEvent * event)
1240 {
1241   gboolean ret = FALSE;
1242   GstDVBSubOverlay *render = GST_DVBSUB_OVERLAY (parent);
1243 
1244   GST_DEBUG_OBJECT (pad, "received text event %s", GST_EVENT_TYPE_NAME (event));
1245 
1246   switch (GST_EVENT_TYPE (event)) {
1247     case GST_EVENT_SEGMENT:
1248     {
1249       GstSegment seg;
1250 
1251       GST_DEBUG_OBJECT (render, "received new segment");
1252 
1253       gst_event_copy_segment (event, &seg);
1254 
1255       if (seg.format == GST_FORMAT_TIME) {
1256         GST_DEBUG_OBJECT (render, "SUBTITLE SEGMENT now: %" GST_SEGMENT_FORMAT,
1257             &render->subtitle_segment);
1258 
1259         render->subtitle_segment = seg;
1260 
1261         GST_DEBUG_OBJECT (render,
1262             "SUBTITLE SEGMENT after: %" GST_SEGMENT_FORMAT,
1263             &render->subtitle_segment);
1264         ret = TRUE;
1265         gst_event_unref (event);
1266       } else {
1267         GST_ELEMENT_WARNING (render, STREAM, MUX, (NULL),
1268             ("received non-TIME newsegment event on subtitle sinkpad"));
1269         ret = FALSE;
1270         gst_event_unref (event);
1271       }
1272       break;
1273     }
1274     case GST_EVENT_FLUSH_STOP:
1275       GST_DEBUG_OBJECT (render, "stop flushing");
1276       gst_dvbsub_overlay_flush_subtitles (render);
1277       gst_segment_init (&render->subtitle_segment, GST_FORMAT_TIME);
1278       gst_event_unref (event);
1279       ret = TRUE;
1280       break;
1281     case GST_EVENT_FLUSH_START:
1282       GST_DEBUG_OBJECT (render, "begin flushing");
1283       gst_event_unref (event);
1284       ret = TRUE;
1285       break;
1286     case GST_EVENT_EOS:
1287       GST_INFO_OBJECT (render, "text EOS");
1288       gst_event_unref (event);
1289       ret = TRUE;
1290       break;
1291     case GST_EVENT_GAP:
1292       gst_event_unref (event);
1293       ret = TRUE;
1294       break;
1295     case GST_EVENT_CAPS:
1296       /* don't want to forward the subtitle caps */
1297       gst_event_unref (event);
1298       ret = TRUE;
1299       break;
1300     default:
1301       ret = gst_pad_push_event (render->srcpad, event);
1302       break;
1303   }
1304 
1305   return ret;
1306 }
1307 
1308 static gboolean
plugin_init(GstPlugin * plugin)1309 plugin_init (GstPlugin * plugin)
1310 {
1311   return GST_ELEMENT_REGISTER (dvbsuboverlay, plugin);
1312 }
1313 
1314 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1315     GST_VERSION_MINOR,
1316     dvbsuboverlay,
1317     "DVB subtitle renderer",
1318     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
1319