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