]> git.sesse.net Git - vlc/blobdiff - src/video_output/video_output.c
Separated vout filter chain into static/interactive filter chains.
[vlc] / src / video_output / video_output.c
index d043b7f723fb16a2bc900b63983a1fa2dc52f430..3bdf1bf384f1fbc5312d72236c1e00b82d01353e 100644 (file)
@@ -125,7 +125,7 @@ static vout_thread_t *VoutCreate(vlc_object_t *object,
 
     /* Initialize locks */
     vlc_mutex_init(&vout->p->picture_lock);
-    vlc_mutex_init(&vout->p->vfilter_lock);
+    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 */
@@ -247,7 +247,7 @@ static void VoutDestructor(vlc_object_t *object)
     /* Destroy the locks */
     vlc_mutex_destroy(&vout->p->spu_lock);
     vlc_mutex_destroy(&vout->p->picture_lock);
-    vlc_mutex_destroy(&vout->p->vfilter_lock);
+    vlc_mutex_destroy(&vout->p->filter.lock);
     vout_control_Clean(&vout->p->control);
 
     /* */
@@ -530,19 +530,37 @@ void vout_DeleteDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
 }
 
 /* */
-static picture_t *VoutVideoFilterNewPicture(filter_t *filter)
+
+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);
 }
+static picture_t *VoutVideoFilterStaticNewPicture(filter_t *filter)
+{
+    vout_thread_t *vout = (vout_thread_t*)filter->p_owner;
+
+    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)
 {
     VLC_UNUSED(filter);
     picture_Release(picture);
 }
-static int VoutVideoFilterAllocationSetup(filter_t *filter, void *data)
+static int VoutVideoFilterStaticAllocationSetup(filter_t *filter, void *data)
+{
+    filter->pf_video_buffer_new = VoutVideoFilterStaticNewPicture;
+    filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
+    filter->p_owner             = data; /* vout */
+    return VLC_SUCCESS;
+}
+static int VoutVideoFilterInteractiveAllocationSetup(filter_t *filter, void *data)
 {
-    filter->pf_video_buffer_new = VoutVideoFilterNewPicture;
+    filter->pf_video_buffer_new = VoutVideoFilterInteractiveNewPicture;
     filter->pf_video_buffer_del = VoutVideoFilterDelPicture;
     filter->p_owner             = data; /* vout */
     return VLC_SUCCESS;
@@ -559,6 +577,10 @@ static picture_t *ThreadDisplayGetDecodedPicture(vout_thread_t *vout,
     const bool is_paused = vout->p->pause.is_on;
     bool redisplay = is_paused && !now && vout->p->displayed.decoded;
 
+    mtime_t filter_delay = 0;
+    for (int i = 0; i < VOUT_FILTER_DELAYS; i++)
+        filter_delay = __MAX(filter_delay, vout->p->filter.delay[i]);
+
     /* FIXME/XXX we must redisplay the last decoded picture (because
      * of potential vout updated, or filters update or SPU update)
      * For now a high update period is needed but it coulmd be removed
@@ -572,7 +594,9 @@ static picture_t *ThreadDisplayGetDecodedPicture(vout_thread_t *vout,
         picture_t *peek = picture_fifo_Peek(vout->p->decoder_fifo);
         if (peek) {
             *is_forced = peek->b_force || is_paused || now;
-            *deadline = (*is_forced ? date : peek->date) - vout_chrono_GetHigh(&vout->p->render);
+            *deadline = (*is_forced ? date : peek->date) -
+                        vout_chrono_GetHigh(&vout->p->render) -
+                        filter_delay;
             picture_Release(peek);
         } else {
             redisplay = true;
@@ -587,7 +611,7 @@ static picture_t *ThreadDisplayGetDecodedPicture(vout_thread_t *vout,
         }
         /* */
         *is_forced = true;
-        *deadline = date - vout_chrono_GetHigh(&vout->p->render);
+        *deadline = date - vout_chrono_GetHigh(&vout->p->render) - filter_delay;
     }
     if (*deadline > VOUT_MWAIT_TOLERANCE)
         *deadline -= VOUT_MWAIT_TOLERANCE;
@@ -652,12 +676,15 @@ static int ThreadDisplayPicture(vout_thread_t *vout,
 
         picture_t *filtered = NULL;
         if (decoded) {
-            vlc_mutex_lock(&vout->p->vfilter_lock);
-            filtered = filter_chain_VideoFilter(vout->p->vfilter_chain, decoded);
-            //assert(filtered == decoded); // TODO implement
-            vlc_mutex_unlock(&vout->p->vfilter_lock);
+            vlc_mutex_lock(&vout->p->filter.lock);
+            filtered = filter_chain_VideoFilter(vout->p->filter.chain_static, decoded);
+            if (filtered)
+                filtered = filter_chain_VideoFilter(vout->p->filter.chain_interactive, filtered);
+            vlc_mutex_unlock(&vout->p->filter.lock);
             if (!filtered)
                 continue;
+            vout->p->filter.delay[vout->p->filter.delay_index] = decoded->date - filtered->date;
+            vout->p->filter.delay_index = (vout->p->filter.delay_index + 1) % VOUT_FILTER_DELAYS;
         }
 
         /*
@@ -736,7 +763,7 @@ static int ThreadDisplayPicture(vout_thread_t *vout,
 
         /* Wait the real date (for rendering jitter) */
         if (!is_forced)
-            mwait(decoded->date);
+            mwait(direct->date);
 
         /* Display the direct buffer returned by vout_RenderPicture */
         vout->p->displayed.date = mdate();
@@ -798,20 +825,76 @@ static void ThreadDisplayOsdTitle(vout_thread_t *vout, const char *string)
                  string);
 }
 
+typedef struct {
+    char           *name;
+    config_chain_t *cfg;
+} vout_filter_t;
+
 static void ThreadChangeFilters(vout_thread_t *vout, const char *filters)
 {
+    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;
     es_format_Init(&fmt, VIDEO_ES, vout->p->original.i_chroma);
     fmt.video = vout->p->original;
 
-    vlc_mutex_lock(&vout->p->vfilter_lock);
+    vlc_mutex_lock(&vout->p->filter.lock);
+
+    filter_chain_Reset(vout->p->filter.chain_static,      &fmt, &fmt);
+    filter_chain_Reset(vout->p->filter.chain_interactive, &fmt, &fmt);
 
-    filter_chain_Reset(vout->p->vfilter_chain, &fmt, &fmt);
-    if (filter_chain_AppendFromString(vout->p->vfilter_chain,
-                                      filters) < 0)
-        msg_Err(vout, "Video filter chain creation failed");
+    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;
 
-    vlc_mutex_unlock(&vout->p->vfilter_lock);
+        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);
+        }
+        vlc_array_clear(array);
+    }
+
+    vlc_mutex_unlock(&vout->p->filter.lock);
+
+    vout->p->filter.delay_index = 0;
+    for (int i = 0; i < VOUT_FILTER_DELAYS; i++)
+        vout->p->filter.delay[i] = 0;
 }
 
 static void ThreadChangeSubFilters(vout_thread_t *vout, const char *filters)
@@ -823,6 +906,14 @@ static void ThreadChangeSubMargin(vout_thread_t *vout, int margin)
     spu_ChangeMargin(vout->p->p_spu, margin);
 }
 
+static void ThreadFilterFlush(vout_thread_t *vout)
+{
+    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);
+}
+
 static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
 {
     assert(!vout->p->pause.is_on || !is_paused);
@@ -839,6 +930,8 @@ static void ThreadChangePause(vout_thread_t *vout, bool is_paused, mtime_t date)
             vout->p->displayed.decoded->date += duration;
 
         spu_OffsetSubtitleDate(vout->p->p_spu, duration);
+
+        ThreadFilterFlush(vout);
     } else {
         vout->p->step.timestamp = VLC_TS_INVALID;
         vout->p->step.last      = VLC_TS_INVALID;
@@ -863,6 +956,8 @@ static void ThreadFlush(vout_thread_t *vout, bool below, mtime_t date)
             vout->p->displayed.timestamp = VLC_TS_INVALID;
         }
     }
+    ThreadFilterFlush(vout);
+
     picture_fifo_Flush(vout->p->decoder_fifo, date, below);
 }
 
@@ -985,9 +1080,16 @@ 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->vfilter_chain =
+    vout->p->filter.chain_static =
         filter_chain_New( vout, "video filter2", false,
-                          VoutVideoFilterAllocationSetup, NULL, vout);
+                          VoutVideoFilterStaticAllocationSetup, NULL, vout);
+    vout->p->filter.chain_interactive =
+        filter_chain_New( vout, "video filter2", false,
+                          VoutVideoFilterInteractiveAllocationSetup, NULL, vout);
+
+    vout->p->filter.delay_index = 0;
+    for (int i = 0; i < VOUT_FILTER_DELAYS; i++)
+        vout->p->filter.delay[i] = 0;
 
     vout_display_state_t state_default;
     if (!state) {
@@ -1022,9 +1124,6 @@ static int ThreadStart(vout_thread_t *vout, const vout_display_state_t *state)
 
 static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
 {
-    /* Destroy the video filters2 */
-    filter_chain_Delete(vout->p->vfilter_chain);
-
     /* Destroy translation tables */
     if (vout->p->display.vd) {
         if (vout->p->decoder_pool) {
@@ -1034,6 +1133,10 @@ static void ThreadStop(vout_thread_t *vout, vout_display_state_t *state)
         vout_CloseWrapper(vout, state);
     }
 
+    /* Destroy the video filters2 */
+    filter_chain_Delete(vout->p->filter.chain_interactive);
+    filter_chain_Delete(vout->p->filter.chain_static);
+
     if (vout->p->decoder_fifo)
         picture_fifo_Delete(vout->p->decoder_fifo);
     assert(!vout->p->decoder_pool);