[vlc-devel] [PATCH 07/16] Add configuration for secondary subtitles

Roland Bewick roland.bewick at gmail.com
Tue May 21 20:08:31 CEST 2019


Secondary subtitle margin and alignment
---
 src/libvlc-module.c                 | 23 +++++++++++++++++++++
 src/video_output/vout_subpictures.c | 41 +++++++++++++++++++++++++++++--------
 2 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/src/libvlc-module.c b/src/libvlc-module.c
index aeddd1844b..aef485b45a 100644
--- a/src/libvlc-module.c
+++ b/src/libvlc-module.c
@@ -370,6 +370,11 @@ static const char *const ppsz_pos_descriptions[] =
 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
   N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
 
+static const int pi_sub_align_values[] = { -1, 0, 1, 2, 4, 8, 5, 6, 9, 10 };
+static const char *const ppsz_sub_align_descriptions[] =
+{ N_("Unset"), N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
+  N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
+
 #define SS_TEXT N_("Disable screensaver")
 #define SS_LONGTEXT N_("Disable the screensaver during video playback." )
 
@@ -742,6 +747,15 @@ static const char *const ppsz_prefres[] = {
 #define SPU_LONGTEXT N_( \
     "You can completely disable the sub-picture processing.")
 
+#define SECONDARY_SPU_POSITION_TEXT N_("Position of secondary subtitles")
+#define SECONDARY_SPU_POSITION_LONGTEXT N_( \
+    "Place on video where to display secondary subtitles (default bottom center).")
+
+#define SECONDARY_SUB_MARGIN_TEXT N_("Force secondary subtitle position")
+#define SECONDARY_SUB_MARGIN_LONGTEXT N_( \
+    "You can use this option to vertically adjust the position secondary " \
+    "subtitles are displayed.")
+
 #define OSD_TEXT N_("On Screen Display")
 #define OSD_LONGTEXT N_( \
     "VLC can display messages on the video. This is called OSD (On Screen " \
@@ -1752,6 +1766,15 @@ vlc_module_begin ()
     add_module_list("sub-filter", "sub filter", NULL,
                     SUB_FILTER_TEXT, SUB_FILTER_LONGTEXT)
 
+    set_section( N_( "Dual Subtitles" ) , NULL )
+    add_integer( "secondary-spu-alignment", -1, SECONDARY_SPU_POSITION_TEXT,
+                 SECONDARY_SPU_POSITION_LONGTEXT, false )
+        change_integer_list( pi_sub_align_values, ppsz_sub_align_descriptions )
+    /* Push the secondary subtitles up a bit so they won't overlap with
+       the primary subtitles using the default settings.*/
+    add_integer( "secondary-sub-margin", 100, SECONDARY_SUB_MARGIN_TEXT,
+                 SECONDARY_SUB_MARGIN_LONGTEXT, true )
+
 /* Input options */
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_GENERAL )
diff --git a/src/video_output/vout_subpictures.c b/src/video_output/vout_subpictures.c
index 214c84c8d6..2e6087ca1f 100644
--- a/src/video_output/vout_subpictures.c
+++ b/src/video_output/vout_subpictures.c
@@ -52,6 +52,9 @@
 /* Number of simultaneous subpictures */
 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
 
+/* ID of the primary SPU. Secondary SPU IDs will be higher. */
+#define SPU_ID_PRIMARY 1
+
 /* Hold of subpicture with converted ts */
 typedef struct {
     subpicture_t *subpicture;
@@ -91,6 +94,9 @@ struct spu_private_t {
     atomic_int margin;                 /**< force position of a subpicture */
     video_palette_t palette;              /**< force palette of subpicture */
 
+    int secondary_sub_alignment;   /**< force alignment for secondary subs */
+    int secondary_sub_margin;      /**< move the secondary sub vertically  */
+
     /* Subpiture filters */
     char           *source_chain_current;
     char           *source_chain_update;
@@ -467,23 +473,28 @@ static void SpuAreaFitInside(spu_area_t *area, const spu_area_t *boundary)
  */
 static void SpuRegionPlace(int *x, int *y,
                            const subpicture_t *subpic,
-                           const subpicture_region_t *region)
+                           const subpicture_region_t *region,
+                           int i_secondary_spu_align_override)
 {
+    int i_align = region->i_align;
+    if (subpic->i_spu_id > SPU_ID_PRIMARY && i_secondary_spu_align_override >= 0)
+        i_align = i_secondary_spu_align_override;
+
     assert(region->i_x != INT_MAX && region->i_y != INT_MAX);
     if (subpic->b_absolute) {
         *x = region->i_x;
         *y = region->i_y;
     } else {
-        if (region->i_align & SUBPICTURE_ALIGN_TOP)
-            *y = region->i_y;
-        else if (region->i_align & SUBPICTURE_ALIGN_BOTTOM)
+        if (i_align & SUBPICTURE_ALIGN_TOP)
+            *y = region->i_y + ( subpic->b_subtitle ? region->fmt.i_visible_height : 0 );
+        else if (i_align & SUBPICTURE_ALIGN_BOTTOM)
             *y = subpic->i_original_picture_height - region->fmt.i_visible_height - region->i_y;
         else
             *y = subpic->i_original_picture_height / 2 - region->fmt.i_visible_height / 2;
 
-        if (region->i_align & SUBPICTURE_ALIGN_LEFT)
+        if (i_align & SUBPICTURE_ALIGN_LEFT)
             *x = region->i_x;
-        else if (region->i_align & SUBPICTURE_ALIGN_RIGHT)
+        else if (i_align & SUBPICTURE_ALIGN_RIGHT)
             *x = subpic->i_original_picture_width - region->fmt.i_visible_width - region->i_x;
         else
             *x = subpic->i_original_picture_width / 2 - region->fmt.i_visible_width / 2;
@@ -776,7 +787,9 @@ static void SpuRenderRegion(spu_t *spu,
 
     /* Compute the margin which is expressed in destination pixel unit
      * The margin is applied only to subtitle and when no forced crop is
-     * requested (dvd menu) */
+     * requested (dvd menu).
+     * Note: Margin will also be applied to secondary subtitles if they exist
+     * to ensure that overlap does not occur. */
     int y_margin = 0;
     if (!crop_requested && subpic->b_subtitle)
         y_margin = spu_invscale_h(atomic_load(&sys->margin), scale_size);
@@ -784,7 +797,14 @@ static void SpuRenderRegion(spu_t *spu,
     /* Place the picture
      * We compute the position in the rendered size */
     SpuRegionPlace(&x_offset, &y_offset,
-                   subpic, region);
+                   subpic, region, sys->secondary_sub_alignment);
+
+    if (subpic->i_spu_id > SPU_ID_PRIMARY && !subpic->b_absolute)
+    {
+        /* Move the secondary subtitles by the secondary margin before overlap detection.
+           This way, overlaps will be resolved if they still exist. */
+        y_offset -= spu_invscale_h(atomic_load(&sys->secondary_sub_margin), scale_size);
+    }
 
     /* Save this position for subtitle overlap support
      * it is really important that there are given without scale_size applied */
@@ -1385,6 +1405,11 @@ spu_t *spu_Create(vlc_object_t *object, vout_thread_t *vout)
     sys->scale_yuvp = NULL;
 
     atomic_init(&sys->margin, var_InheritInteger(spu, "sub-margin"));
+    atomic_init(&sys->secondary_sub_margin,
+                var_InheritInteger(spu, "secondary-sub-margin"));
+
+    sys->secondary_sub_alignment = var_InheritInteger(spu,
+                                                      "secondary-spu-alignment");
 
     /* Register the default subpicture channel */
     sys->channel = VOUT_SPU_CHANNEL_AVAIL_FIRST;
-- 
2.11.0



More information about the vlc-devel mailing list