]> git.sesse.net Git - vlc/blobdiff - src/video_output/video_output.c
vout: use atomic variables for stats instead of spin lock
[vlc] / src / video_output / video_output.c
index 81c6c4011dd753e1ee623242ea32de2fb390b1ab..b83d04055d05013bee94960ef6585251b2b862a7 100644 (file)
@@ -5,26 +5,26 @@
  * It includes functions allowing to open a new thread, send pictures to a
  * thread, and destroy a previously oppened video output thread.
  *****************************************************************************
- * Copyright (C) 2000-2007 the VideoLAN team
+ * Copyright (C) 2000-2007 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *          Gildas Bazin <gbazin@videolan.org>
  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -75,7 +75,8 @@ static void VoutDestructor(vlc_object_t *);
 static int VoutValidateFormat(video_format_t *dst,
                               const video_format_t *src)
 {
-    if (src->i_width <= 0 || src->i_height <= 0)
+    if (src->i_width <= 0  || src->i_width  > 8192 ||
+        src->i_height <= 0 || src->i_height > 8192)
         return VLC_EGENERIC;
     if (src->i_sar_num <= 0 || src->i_sar_den <= 0)
         return VLC_EGENERIC;
@@ -92,6 +93,22 @@ static int VoutValidateFormat(video_format_t *dst,
     video_format_FixRgb(dst);
     return VLC_SUCCESS;
 }
+static void VideoFormatCopyCropAr(video_format_t *dst,
+                                  const video_format_t *src)
+{
+    video_format_CopyCrop(dst, src);
+    dst->i_sar_num = src->i_sar_num;
+    dst->i_sar_den = src->i_sar_den;
+}
+static bool VideoFormatIsCropArEqual(video_format_t *dst,
+                                     const video_format_t *src)
+{
+    return dst->i_sar_num * src->i_sar_den == dst->i_sar_den * src->i_sar_num &&
+           dst->i_x_offset       == src->i_x_offset &&
+           dst->i_y_offset       == src->i_y_offset &&
+           dst->i_visible_width  == src->i_visible_width &&
+           dst->i_visible_height == src->i_visible_height;
+}
 
 static vout_thread_t *VoutCreate(vlc_object_t *object,
                                  const vout_configuration_t *cfg)
@@ -103,7 +120,7 @@ static vout_thread_t *VoutCreate(vlc_object_t *object,
     /* Allocate descriptor */
     vout_thread_t *vout = vlc_custom_create(object,
                                             sizeof(*vout) + sizeof(*vout->p),
-                                            VLC_OBJECT_VOUT, "video output");
+                                            "video output");
     if (!vout) {
         video_format_Clean(&original);
         return NULL;
@@ -127,9 +144,6 @@ static vout_thread_t *VoutCreate(vlc_object_t *object,
     vlc_mutex_init(&vout->p->filter.lock);
     vlc_mutex_init(&vout->p->spu_lock);
 
-    /* Attach the new object now so we can use var inheritance below */
-    vlc_object_attach(vout, object);
-
     /* Initialize subpicture unit */
     vout->p->spu = spu_Create(vout);
 
@@ -385,7 +399,7 @@ picture_t *vout_GetPicture(vout_thread_t *vout)
     picture_t *picture = picture_pool_Get(vout->p->decoder_pool);
     if (picture) {
         picture_Reset(picture);
-        picture->p_next = NULL;
+        VideoFormatCopyCropAr(&picture->format, &vout->p->original);
     }
     vlc_mutex_unlock(&vout->p->picture_lock);
 
@@ -456,8 +470,8 @@ int vout_GetSnapshot(vout_thread_t *vout,
         if (type && image_Type2Fourcc(type))
             codec = image_Type2Fourcc(type);
 
-        const int override_width  = var_GetInteger(vout, "snapshot-width");
-        const int override_height = var_GetInteger(vout, "snapshot-height");
+        const int override_width  = var_InheritInteger(vout, "snapshot-width");
+        const int override_height = var_InheritInteger(vout, "snapshot-height");
 
         if (picture_Export(VLC_OBJECT(vout), image_dst, fmt,
                            picture, codec, override_width, override_height)) {
@@ -535,6 +549,11 @@ void vout_ControlChangeFilters(vout_thread_t *vout, const char *filters)
     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_FILTERS,
                             filters);
 }
+void vout_ControlChangeSubSources(vout_thread_t *vout, const char *filters)
+{
+    vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_SOURCES,
+                            filters);
+}
 void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
 {
     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
@@ -641,12 +660,16 @@ void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
 }
 
 /* */
-
 static picture_t *VoutVideoFilterInteractiveNewPicture(filter_t *filter)
 {
     vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
 
-    return picture_pool_Get(vout->p->private_pool);
+    picture_t *picture = picture_pool_Get(vout->p->private_pool);
+    if (picture) {
+        picture_Reset(picture);
+        VideoFormatCopyCropAr(&picture->format, &filter->fmt_out.video);
+    }
+    return picture;
 }
 static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
 {
@@ -655,6 +678,7 @@ static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
     vlc_assert_locked(&vout->p->filter.lock);
     if (filter_chain_GetLength(vout->p->filter.chain_interactive) == 0)
         return VoutVideoFilterInteractiveNewPicture(filter);
+
     return picture_NewFromFormat(&filter->fmt_out.video);
 }
 static void VoutVideoFilterDelPicture(filter_t *filter, picture_t *picture)
@@ -676,6 +700,118 @@ static int VoutVideoFilterInteractiveAllocationSetup(filter_t *filter, void *dat
     filter->p_owner             = data; /* vout */
     return VLC_SUCCESS;
 }
+static void ThreadFilterFlush(vout_thread_t *vout, bool is_locked)
+{
+    if (vout->p->displayed.current)
+        picture_Release( vout->p->displayed.current );
+    vout->p->displayed.current = NULL;
+
+    if (vout->p->displayed.next)
+        picture_Release( vout->p->displayed.next );
+    vout->p->displayed.next = NULL;
+
+    if (!is_locked)
+        vlc_mutex_lock(&vout->p->filter.lock);
+    filter_chain_VideoFlush(vout->p->filter.chain_static);
+    filter_chain_VideoFlush(vout->p->filter.chain_interactive);
+    if (!is_locked)
+        vlc_mutex_unlock(&vout->p->filter.lock);
+}
+
+typedef struct {
+    char           *name;
+    config_chain_t *cfg;
+} vout_filter_t;
+
+static void ThreadChangeFilters(vout_thread_t *vout,
+                                const video_format_t *source,
+                                const char *filters,
+                                bool is_locked)
+{
+    ThreadFilterFlush(vout, is_locked);
+
+    vlc_array_t array_static;
+    vlc_array_t array_interactive;
+
+    vlc_array_init(&array_static);
+    vlc_array_init(&array_interactive);
+    char *current = filters ? strdup(filters) : NULL;
+    while (current) {
+        config_chain_t *cfg;
+        char *name;
+        char *next = config_ChainCreate(&name, &cfg, current);
+
+        if (name && *name) {
+            vout_filter_t *e = xmalloc(sizeof(*e));
+            e->name = name;
+            e->cfg  = cfg;
+            if (!strcmp(e->name, "deinterlace") ||
+                !strcmp(e->name, "postproc")) {
+                vlc_array_append(&array_static, e);
+            } else {
+                vlc_array_append(&array_interactive, e);
+            }
+        } else {
+            if (cfg)
+                config_ChainDestroy(cfg);
+            free(name);
+        }
+        free(current);
+        current = next;
+    }
+
+    if (!is_locked)
+        vlc_mutex_lock(&vout->p->filter.lock);
+
+    es_format_t fmt_target;
+    es_format_InitFromVideo(&fmt_target, source ? source : &vout->p->filter.format);
+
+    es_format_t fmt_current = fmt_target;
+
+    for (int a = 0; a < 2; a++) {
+        vlc_array_t    *array = a == 0 ? &array_static :
+                                         &array_interactive;
+        filter_chain_t *chain = a == 0 ? vout->p->filter.chain_static :
+                                         vout->p->filter.chain_interactive;
+
+        filter_chain_Reset(chain, &fmt_current, &fmt_current);
+        for (int i = 0; i < vlc_array_count(array); i++) {
+            vout_filter_t *e = vlc_array_item_at_index(array, i);
+            msg_Dbg(vout, "Adding '%s' as %s", e->name, a == 0 ? "static" : "interactive");
+            if (!filter_chain_AppendFilter(chain, e->name, e->cfg, NULL, NULL)) {
+                msg_Err(vout, "Failed to add filter '%s'", e->name);
+                config_ChainDestroy(e->cfg);
+            }
+            free(e->name);
+            free(e);
+        }
+        fmt_current = *filter_chain_GetFmtOut(chain);
+        vlc_array_clear(array);
+    }
+    VideoFormatCopyCropAr(&fmt_target.video, &fmt_current.video);
+    if (!es_format_IsSimilar(&fmt_current, &fmt_target)) {
+        msg_Dbg(vout, "Adding a filter to compensate for format changes");
+        if (!filter_chain_AppendFilter(vout->p->filter.chain_interactive, NULL, NULL,
+                                       &fmt_current, &fmt_target)) {
+            msg_Err(vout, "Failed to compensate for the format changes, removing all filters");
+            filter_chain_Reset(vout->p->filter.chain_static,      &fmt_target, &fmt_target);
+            filter_chain_Reset(vout->p->filter.chain_interactive, &fmt_target, &fmt_target);
+        }
+    }
+
+    if (vout->p->filter.configuration != filters) {
+        free(vout->p->filter.configuration);
+        vout->p->filter.configuration = filters ? strdup(filters) : NULL;
+    }
+    if (source) {
+        video_format_Clean(&vout->p->filter.format);
+        video_format_Copy(&vout->p->filter.format, source);
+    }
+
+    if (!is_locked)
+        vlc_mutex_unlock(&vout->p->filter.lock);
+}
+
 
 /* */
 static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool is_late_dropped)
@@ -705,6 +841,9 @@ static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool is_
                     msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
                 }
             }
+            if (decoded &&
+                !VideoFormatIsCropArEqual(&decoded->format, &vout->p->filter.format))
+                ThreadChangeFilters(vout, &decoded->format, vout->p->filter.configuration, true);
         }
         if (!decoded)
             break;
@@ -723,7 +862,7 @@ static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool is_
 
     vlc_mutex_unlock(&vout->p->filter.lock);
 
-    vout_statistic_Update(&vout->p->statistic, 0, lost_count);
+    vout_statistic_AddLost(&vout->p->statistic, lost_count);
     if (!picture)
         return VLC_EGENERIC;
 
@@ -737,6 +876,7 @@ static int ThreadDisplayPreparePicture(vout_thread_t *vout, bool reuse, bool is_
 
 static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
 {
+    vout_thread_sys_t *sys = vout->p;
     vout_display_t *vd = vout->p->display.vd;
 
     picture_t *torender = picture_Hold(vout->p->displayed.current);
@@ -754,18 +894,74 @@ static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
         msg_Warn(vout, "Unsupported timestamp modifications done by chain_interactive");
 
     /*
-     * Check for subpictures to display
+     * Get the subpicture to be displayed
      */
     const bool do_snapshot = vout_snapshot_IsRequested(&vout->p->snapshot);
-    mtime_t spu_render_time;
+    mtime_t render_subtitle_date;
     if (vout->p->pause.is_on)
-        spu_render_time = vout->p->pause.date;
+        render_subtitle_date = vout->p->pause.date;
     else
-        spu_render_time = filtered->date > 1 ? filtered->date : mdate();
+        render_subtitle_date = filtered->date > 1 ? filtered->date : mdate();
+    mtime_t render_osd_date = mdate(); /* FIXME wrong */
 
-    subpicture_t *subpic = spu_SortSubpictures(vout->p->spu,
-                                               spu_render_time,
-                                               do_snapshot);
+    /*
+     * Get the subpicture to be displayed
+     */
+    const bool do_dr_spu = !do_snapshot &&
+                           vd->info.subpicture_chromas &&
+                           *vd->info.subpicture_chromas != 0;
+    const bool do_early_spu = !do_dr_spu &&
+                              (vd->info.is_slow ||
+                               sys->display.use_dr ||
+                               do_snapshot ||
+                               !vout_IsDisplayFiltered(vd) ||
+                               vd->fmt.i_width * vd->fmt.i_height <= vd->source.i_width * vd->source.i_height);
+
+    const vlc_fourcc_t *subpicture_chromas;
+    video_format_t fmt_spu;
+    if (do_dr_spu) {
+        vout_display_place_t place;
+        vout_display_PlacePicture(&place, &vd->source, vd->cfg, false);
+
+        fmt_spu = vd->source;
+        if (fmt_spu.i_width * fmt_spu.i_height < place.width * place.height) {
+            fmt_spu.i_sar_num = vd->cfg->display.sar.num;
+            fmt_spu.i_sar_den = vd->cfg->display.sar.den;
+            fmt_spu.i_width          =
+            fmt_spu.i_visible_width  = place.width;
+            fmt_spu.i_height         =
+            fmt_spu.i_visible_height = place.height;
+        }
+        subpicture_chromas = vd->info.subpicture_chromas;
+    } else {
+        if (do_early_spu) {
+            fmt_spu = vd->source;
+        } else {
+            fmt_spu = vd->fmt;
+            fmt_spu.i_sar_num = vd->cfg->display.sar.num;
+            fmt_spu.i_sar_den = vd->cfg->display.sar.den;
+        }
+        subpicture_chromas = NULL;
+
+        if (vout->p->spu_blend &&
+            vout->p->spu_blend->fmt_out.video.i_chroma != fmt_spu.i_chroma) {
+            filter_DeleteBlend(vout->p->spu_blend);
+            vout->p->spu_blend = NULL;
+            vout->p->spu_blend_chroma = 0;
+        }
+        if (!vout->p->spu_blend && vout->p->spu_blend_chroma != fmt_spu.i_chroma) {
+            vout->p->spu_blend_chroma = fmt_spu.i_chroma;
+            vout->p->spu_blend = filter_NewBlend(VLC_OBJECT(vout), &fmt_spu);
+            if (!vout->p->spu_blend)
+                msg_Err(vout, "Failed to create blending filter, OSD/Subtitles will not work");
+        }
+    }
+
+    subpicture_t *subpic = spu_Render(vout->p->spu,
+                                      subpicture_chromas, &fmt_spu,
+                                      &vd->source,
+                                      render_subtitle_date, render_osd_date,
+                                      do_snapshot);
     /*
      * Perform rendering
      *
@@ -773,41 +969,41 @@ static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
      * - be sure to end up with a direct buffer.
      * - blend subtitles, and in a fast access buffer
      */
-    picture_t *direct = NULL;
-    if (filtered &&
-        (vout->p->decoder_pool != vout->p->display_pool || subpic)) {
-        picture_t *render;
-        if (vout->p->is_decoder_pool_slow)
-            render = picture_NewFromFormat(&vd->source);
-        else if (vout->p->decoder_pool != vout->p->display_pool)
-            render = picture_pool_Get(vout->p->display_pool);
-        else
-            render = picture_pool_Get(vout->p->private_pool);
-
-        if (render) {
-            picture_Copy(render, filtered);
-
-            spu_RenderSubpictures(vout->p->spu,
-                                  render, &vd->source,
-                                  subpic, &vd->source, spu_render_time);
+    bool is_direct = vout->p->decoder_pool == vout->p->display_pool;
+    picture_t *todisplay = filtered;
+    if (do_early_spu && subpic) {
+        todisplay = picture_pool_Get(vout->p->private_pool);
+        if (todisplay) {
+            VideoFormatCopyCropAr(&todisplay->format, &filtered->format);
+            picture_Copy(todisplay, filtered);
+            if (vout->p->spu_blend)
+                picture_BlendSubpicture(todisplay, vout->p->spu_blend, subpic);
         }
-        if (vout->p->is_decoder_pool_slow) {
-            direct = picture_pool_Get(vout->p->display_pool);
-            if (direct)
-                picture_Copy(direct, render);
-            picture_Release(render);
+        picture_Release(filtered);
+        subpicture_Delete(subpic);
+        subpic = NULL;
 
-        } else {
-            direct = render;
+        if (!todisplay)
+            return VLC_EGENERIC;
+    }
+
+    picture_t *direct;
+    if (!is_direct && todisplay) {
+        direct = picture_pool_Get(vout->p->display_pool);
+        if (direct) {
+            VideoFormatCopyCropAr(&direct->format, &todisplay->format);
+            picture_Copy(direct, todisplay);
         }
-        picture_Release(filtered);
-        filtered = NULL;
+        picture_Release(todisplay);
     } else {
-        direct = filtered;
+        direct = todisplay;
     }
 
-    if (!direct)
+    if (!direct) {
+        if (subpic)
+            subpicture_Delete(subpic);
         return VLC_EGENERIC;
+    }
 
     /*
      * Take a snapshot if requested
@@ -815,8 +1011,23 @@ static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
     if (do_snapshot)
         vout_snapshot_Set(&vout->p->snapshot, &vd->source, direct);
 
-    /* Render the direct buffer returned by vout_RenderPicture */
-    vout_RenderWrapper(vout, direct);
+    /* Render the direct buffer */
+    assert(vout_IsDisplayFiltered(vd) == !sys->display.use_dr);
+    vout_UpdateDisplaySourceProperties(vd, &direct->format);
+    if (sys->display.use_dr) {
+        vout_display_Prepare(vd, direct, subpic);
+    } else {
+        sys->display.filtered = vout_FilterDisplay(vd, direct);
+        if (sys->display.filtered) {
+            if (!do_dr_spu && !do_early_spu && vout->p->spu_blend && subpic)
+                picture_BlendSubpicture(sys->display.filtered, vout->p->spu_blend, subpic);
+            vout_display_Prepare(vd, sys->display.filtered, do_dr_spu ? subpic : NULL);
+        }
+        if (!do_dr_spu && subpic)
+            subpicture_Delete(subpic);
+        if (!sys->display.filtered)
+            return VLC_EGENERIC;
+    }
 
     vout_chrono_Stop(&vout->p->render);
 #if 0
@@ -839,10 +1050,13 @@ static int ThreadDisplayRenderPicture(vout_thread_t *vout, bool is_forced)
 
     /* Display the direct buffer returned by vout_RenderPicture */
     vout->p->displayed.date = mdate();
+    vout_display_Display(vd,
+                         sys->display.filtered ? sys->display.filtered
+                                                : direct,
+                         subpic);
+    sys->display.filtered = NULL;
 
-    vout_DisplayWrapper(vout, direct);
-
-    vout_statistic_Update(&vout->p->statistic, 1, 0);
+    vout_statistic_AddDisplayed(&vout->p->statistic, 1);
 
     return VLC_SUCCESS;
 }
@@ -942,7 +1156,7 @@ static void ThreadManage(vout_thread_t *vout,
 static void ThreadDisplaySubpicture(vout_thread_t *vout,
                                     subpicture_t *subpicture)
 {
-    spu_DisplaySubpicture(vout->p->spu, subpicture);
+    spu_PutSubpicture(vout->p->spu, subpicture);
 }
 
 static void ThreadFlushSubpicture(vout_thread_t *vout, int channel)
@@ -960,105 +1174,16 @@ static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
                  string);
 }
 
-static void ThreadFilterFlush(vout_thread_t *vout)
+static void ThreadChangeSubSources(vout_thread_t *vout, const char *filters)
 {
-    if (vout->p->displayed.current)
-        picture_Release( vout->p->displayed.current );
-    vout->p->displayed.current = NULL;
-
-    if (vout->p->displayed.next)
-        picture_Release( vout->p->displayed.next );
-    vout->p->displayed.next = NULL;
-
-    vlc_mutex_lock(&vout->p->filter.lock);
-    filter_chain_VideoFlush(vout->p->filter.chain_static);
-    filter_chain_VideoFlush(vout->p->filter.chain_interactive);
-    vlc_mutex_unlock(&vout->p->filter.lock);
-}
-
-typedef struct {
-    char           *name;
-    config_chain_t *cfg;
-} vout_filter_t;
-
-static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
-{
-    ThreadFilterFlush(vout);
-
-    vlc_array_t array_static;
-    vlc_array_t array_interactive;
-
-    vlc_array_init(&array_static);
-    vlc_array_init(&array_interactive);
-    char *current = filters ? strdup(filters) : NULL;
-    while (current) {
-        config_chain_t *cfg;
-        char *name;
-        char *next = config_ChainCreate(&name, &cfg, current);
-
-        if (name && *name) {
-            vout_filter_t *e = xmalloc(sizeof(*e));
-            e->name = name;
-            e->cfg  = cfg;
-            if (!strcmp(e->name, "deinterlace") ||
-                !strcmp(e->name, "postproc")) {
-                vlc_array_append(&array_static, e);
-            } else {
-                vlc_array_append(&array_interactive, e);
-            }
-        } else {
-            if (cfg)
-                config_ChainDestroy(cfg);
-            free(name);
-        }
-        free(current);
-        current = next;
-    }
-
-    es_format_t fmt_target;
-    es_format_InitFromVideo(&fmt_target, &vout->p->original);
-
-    es_format_t fmt_current = fmt_target;
-
-    vlc_mutex_lock(&vout->p->filter.lock);
-
-    for (int a = 0; a < 2; a++) {
-        vlc_array_t    *array = a == 0 ? &array_static :
-                                         &array_interactive;
-        filter_chain_t *chain = a == 0 ? vout->p->filter.chain_static :
-                                         vout->p->filter.chain_interactive;
-
-        filter_chain_Reset(chain, &fmt_current, &fmt_current);
-        for (int i = 0; i < vlc_array_count(array); i++) {
-            vout_filter_t *e = vlc_array_item_at_index(array, i);
-            msg_Dbg(vout, "Adding '%s' as %s", e->name, a == 0 ? "static" : "interactive");
-            if (!filter_chain_AppendFilter(chain, e->name, e->cfg, NULL, NULL)) {
-                msg_Err(vout, "Failed to add filter '%s'", e->name);
-                config_ChainDestroy(e->cfg);
-            }
-            free(e->name);
-            free(e);
-        }
-        fmt_current = *filter_chain_GetFmtOut(chain);
-        vlc_array_clear(array);
-    }
-    if (!es_format_IsSimilar(&fmt_current, &fmt_target)) {
-        msg_Dbg(vout, "Adding a filter to compensate for format changes");
-        if (!filter_chain_AppendFilter(vout->p->filter.chain_interactive, NULL, NULL,
-                                       &fmt_current, &fmt_target)) {
-            msg_Err(vout, "Failed to compensate for the format changes, removing all filters");
-            filter_chain_Reset(vout->p->filter.chain_static,      &fmt_target, &fmt_target);
-            filter_chain_Reset(vout->p->filter.chain_interactive, &fmt_target, &fmt_target);
-        }
-    }
-
-    vlc_mutex_unlock(&vout->p->filter.lock);
+    spu_ChangeSources(vout->p->spu, filters);
 }
 
 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
 {
     spu_ChangeFilters(vout->p->spu, filters);
 }
+
 static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
 {
     spu_ChangeMargin(vout->p->spu, margin);
@@ -1080,7 +1205,7 @@ static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
             vout->p->displayed.decoded->date += duration;
         spu_OffsetSubtitleDate(vout->p->spu, duration);
 
-        ThreadFilterFlush(vout);
+        ThreadFilterFlush(vout, false);
     } else {
         vout->p->step.timestamp = VLC_TS_INVALID;
         vout->p->step.last      = VLC_TS_INVALID;
@@ -1094,7 +1219,7 @@ static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
     vout->p->step.timestamp = VLC_TS_INVALID;
     vout->p->step.last      = VLC_TS_INVALID;
 
-    ThreadFilterFlush(vout); /* FIXME too much */
+    ThreadFilterFlush(vout, false); /* FIXME too much */
 
     picture_t *last = vout->p->displayed.decoded;
     if (last) {
@@ -1210,6 +1335,8 @@ static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
     vout->p->display_pool = NULL;
     vout->p->private_pool = NULL;
 
+    vout->p->filter.configuration = NULL;
+    video_format_Copy(&vout->p->filter.format, &vout->p->original);
     vout->p->filter.chain_static =
         filter_chain_New( vout, "video filter2", true,
                           VoutVideoFilterStaticAllocationSetup, NULL, vout);
@@ -1245,12 +1372,18 @@ static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
     vout->p->step.last               = VLC_TS_INVALID;
     vout->p->step.timestamp          = VLC_TS_INVALID;
 
+    vout->p->spu_blend_chroma        = 0;
+    vout->p->spu_blend               = NULL;
+
     video_format_Print(VLC_OBJECT(vout), "original format", &vout->p->original);
     return VLC_SUCCESS;
 }
 
 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
 {
+    if (vout->p->spu_blend)
+        filter_DeleteBlend(vout->p->spu_blend);
+
     /* Destroy translation tables */
     if (vout->p->display.vd) {
         if (vout->p->decoder_pool) {
@@ -1263,6 +1396,8 @@ static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
     /* Destroy the video filters2 */
     filter_chain_Delete(vout->p->filter.chain_interactive);
     filter_chain_Delete(vout->p->filter.chain_static);
+    video_format_Clean(&vout->p->filter.format);
+    free(vout->p->filter.configuration);
 
     if (vout->p->decoder_fifo)
         picture_fifo_Delete(vout->p->decoder_fifo);
@@ -1301,6 +1436,8 @@ static int ThreadReinit(vout_thread_t *vout,
         ThreadClean(vout);
         return VLC_EGENERIC;
     }
+    /* We ignore crop/ar changes at this point, they are dynamically supported */
+    VideoFormatCopyCropAr(&vout->p->original, &original);
     if (video_format_IsSimilar(&original, &vout->p->original)) {
         if (cfg->dpb_size <= vout->p->dpb_size)
             return VLC_SUCCESS;
@@ -1384,7 +1521,10 @@ static void *Thread(void *object)
                 ThreadDisplayOsdTitle(vout, cmd.u.string);
                 break;
             case VOUT_CONTROL_CHANGE_FILTERS:
-                ThreadChangeFilters(vout, cmd.u.string);
+                ThreadChangeFilters(vout, NULL, cmd.u.string, false);
+                break;
+            case VOUT_CONTROL_CHANGE_SUB_SOURCES:
+                ThreadChangeSubSources(vout, cmd.u.string);
                 break;
             case VOUT_CONTROL_CHANGE_SUB_FILTERS:
                 ThreadChangeSubFilters(vout, cmd.u.string);