]> 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 7c1c8bd8bfbcbee241d88abca5149486c83d5afd..3bdf1bf384f1fbc5312d72236c1e00b82d01353e 100644 (file)
@@ -125,7 +125,8 @@ 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 */
     vlc_object_attach(vout, object);
@@ -155,6 +156,7 @@ static vout_thread_t *VoutCreate(vlc_object_t *object,
     /* */
     if (vlc_clone(&vout->p->thread, Thread, vout,
                   VLC_THREAD_PRIORITY_OUTPUT)) {
+        spu_Destroy(vout->p->p_spu);
         vlc_object_release(vout);
         return NULL;
     }
@@ -214,26 +216,22 @@ vout_thread_t *(vout_Request)(vlc_object_t *object,
     return VoutCreate(object, cfg);
 }
 
-/*****************************************************************************
- * vout_Close: Close a vout created by VoutCreate.
- *****************************************************************************
- * You HAVE to call it on vout created by VoutCreate before vlc_object_release.
- * You should NEVER call it on vout not obtained through VoutCreate
- * (like with vout_Request or vlc_object_find.)
- * You can use vout_CloseAndRelease() as a convenience method.
- *****************************************************************************/
 void vout_Close(vout_thread_t *vout)
 {
     assert(vout);
 
     if (vout->p->input)
         spu_Attach(vout->p->p_spu, vout->p->input, false);
-    vlc_object_detach(vout->p->p_spu);
 
     vout_snapshot_End(&vout->p->snapshot);
 
     vout_control_PushVoid(&vout->p->control, VOUT_CONTROL_CLEAN);
     vlc_join(vout->p->thread, NULL);
+
+    vlc_mutex_lock(&vout->p->spu_lock);
+    spu_Destroy(vout->p->p_spu);
+    vout->p->p_spu = NULL;
+    vlc_mutex_unlock(&vout->p->spu_lock);
 }
 
 /* */
@@ -246,12 +244,10 @@ static void VoutDestructor(vlc_object_t *object)
 
     free(vout->p->splitter_name);
 
-    /* */
-    spu_Destroy(vout->p->p_spu);
-
     /* 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);
 
     /* */
@@ -357,7 +353,14 @@ void vout_PutSubpicture( vout_thread_t *vout, subpicture_t *subpic )
 }
 int vout_RegisterSubpictureChannel( vout_thread_t *vout )
 {
-    return spu_RegisterChannel(vout->p->p_spu);
+    int channel = SPU_DEFAULT_CHANNEL;
+
+    vlc_mutex_lock(&vout->p->spu_lock);
+    if (vout->p->p_spu)
+        channel = spu_RegisterChannel(vout->p->p_spu);
+    vlc_mutex_unlock(&vout->p->spu_lock);
+
+    return channel;
 }
 void vout_FlushSubpictureChannel( vout_thread_t *vout, int channel )
 {
@@ -432,6 +435,11 @@ void vout_ControlChangeSubFilters(vout_thread_t *vout, const char *filters)
     vout_control_PushString(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_FILTERS,
                             filters);
 }
+void vout_ControlChangeSubMargin(vout_thread_t *vout, int margin)
+{
+    vout_control_PushInteger(&vout->p->control, VOUT_CONTROL_CHANGE_SUB_MARGIN,
+                             margin);
+}
 
 /* */
 static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, const char *title)
@@ -459,9 +467,9 @@ static void VoutGetDisplayCfg(vout_thread_t *vout, vout_display_cfg_t *cfg, cons
     else if (align_mask & 0x2)
         cfg->align.horizontal = VOUT_DISPLAY_ALIGN_RIGHT;
     if (align_mask & 0x4)
-        cfg->align.horizontal = VOUT_DISPLAY_ALIGN_TOP;
+        cfg->align.vertical = VOUT_DISPLAY_ALIGN_TOP;
     else if (align_mask & 0x8)
-        cfg->align.horizontal = VOUT_DISPLAY_ALIGN_BOTTOM;
+        cfg->align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
 }
 
 vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
@@ -494,7 +502,7 @@ vout_window_t * vout_NewDisplayWindow(vout_thread_t *vout, vout_display_t *vd,
         vout->p->window.object    = NULL;
     }
 
-    vout_window_t *window = vout_window_New(VLC_OBJECT(vout), NULL,
+    vout_window_t *window = vout_window_New(VLC_OBJECT(vout), "$window",
                                             &cfg_override);
     if (!window)
         return NULL;
@@ -522,119 +530,161 @@ 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;
 }
 
 /* */
-static int ThreadDisplayPicture(vout_thread_t *vout,
-                                bool now, mtime_t *deadline)
+static picture_t *ThreadDisplayGetDecodedPicture(vout_thread_t *vout,
+                                                 int *lost_count, bool *is_forced,
+                                                 bool now, mtime_t *deadline)
 {
     vout_display_t *vd = vout->p->display.vd;
-    int displayed_count = 0;
-    int lost_count = 0;
-
-    for (;;) {
-        const mtime_t date = mdate();
-        const bool is_paused = vout->p->pause.is_on;
-        bool redisplay = is_paused && !now && vout->p->displayed.decoded;
-        bool is_forced;
 
-        /* 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
-         * if and only if:
-         * - vout module emits events from theselves.
-         * - *and* SPU is modified to emit an event or a deadline when needed.
-         *
-         * So it will be done latter.
-         */
-        if (!redisplay) {
-            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);
-                picture_Release(peek);
-            } else {
-                redisplay = true;
-            }
+    const mtime_t date = mdate();
+    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
+     * if and only if:
+     * - vout module emits events from theselves.
+     * - *and* SPU is modified to emit an event or a deadline when needed.
+     *
+     * So it will be done latter.
+     */
+    if (!redisplay) {
+        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) -
+                        filter_delay;
+            picture_Release(peek);
+        } else {
+            redisplay = true;
         }
-        if (redisplay) {
-             /* FIXME a better way for this delay is needed */
-            const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
-            if (date_update > date || !vout->p->displayed.decoded) {
-                *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
-                break;
-            }
-            /* */
-            is_forced = true;
-            *deadline = date - vout_chrono_GetHigh(&vout->p->render);
+    }
+    if (redisplay) {
+         /* FIXME a better way for this delay is needed */
+        const mtime_t date_update = vout->p->displayed.date + VOUT_REDISPLAY_DELAY;
+        if (date_update > date || !vout->p->displayed.decoded) {
+            *deadline = vout->p->displayed.decoded ? date_update : VLC_TS_INVALID;
+            return NULL;
         }
-        if (*deadline > VOUT_MWAIT_TOLERANCE)
-            *deadline -= VOUT_MWAIT_TOLERANCE;
+        /* */
+        *is_forced = true;
+        *deadline = date - vout_chrono_GetHigh(&vout->p->render) - filter_delay;
+    }
+    if (*deadline > VOUT_MWAIT_TOLERANCE)
+        *deadline -= VOUT_MWAIT_TOLERANCE;
 
-        /* If we are too early and can wait, do it */
-        if (date < *deadline && !now)
-            break;
+    /* If we are too early and can wait, do it */
+    if (date < *deadline && !now)
+        return NULL;
 
-        picture_t *decoded;
-        if (redisplay) {
-            decoded = vout->p->displayed.decoded;
-            vout->p->displayed.decoded = NULL;
-        } else {
-            decoded = picture_fifo_Pop(vout->p->decoder_fifo);
-            assert(decoded);
-            if (!is_forced && !vout->p->is_late_dropped) {
-                const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
-                const mtime_t late = predicted - decoded->date;
-                if (late > 0) {
-                    msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
-                    if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
-                        msg_Warn(vout, "rejected picture because of render time");
-                        /* TODO */
-                        picture_Release(decoded);
-                        lost_count++;
-                        break;
-                    }
+    picture_t *decoded;
+    if (redisplay) {
+        decoded = vout->p->displayed.decoded;
+        vout->p->displayed.decoded = NULL;
+    } else {
+        decoded = picture_fifo_Pop(vout->p->decoder_fifo);
+        assert(decoded);
+        if (!*is_forced && !vout->p->is_late_dropped) {
+            const mtime_t predicted = date + vout_chrono_GetLow(&vout->p->render);
+            const mtime_t late = predicted - decoded->date;
+            if (late > 0) {
+                msg_Dbg(vout, "picture might be displayed late (missing %d ms)", (int)(late/1000));
+                if (late > VOUT_DISPLAY_LATE_THRESHOLD) {
+                    msg_Warn(vout, "rejected picture because of render time");
+                    /* TODO */
+                    picture_Release(decoded);
+                    (*lost_count)++;
+                    return NULL;
                 }
             }
-
-            vout->p->displayed.is_interlaced = !decoded->b_progressive;
-            vout->p->displayed.qtype         = decoded->i_qtype;
         }
-        vout->p->displayed.timestamp = decoded->date;
 
-        /* */
-        if (vout->p->displayed.decoded)
-            picture_Release(vout->p->displayed.decoded);
-        picture_Hold(decoded);
-        vout->p->displayed.decoded = decoded;
+        vout->p->displayed.is_interlaced = !decoded->b_progressive;
+        vout->p->displayed.qtype         = decoded->i_qtype;
+    }
+    vout->p->displayed.timestamp = decoded->date;
+
+    /* */
+    if (vout->p->displayed.decoded)
+        picture_Release(vout->p->displayed.decoded);
+    picture_Hold(decoded);
+    vout->p->displayed.decoded = decoded;
+
+    return decoded;
+}
+
+static int ThreadDisplayPicture(vout_thread_t *vout,
+                                bool now, mtime_t *deadline)
+{
+    vout_display_t *vd = vout->p->display.vd;
+    int displayed_count = 0;
+    int lost_count = 0;
+
+    for (;;) {
+        bool is_forced;
+        picture_t *decoded = ThreadDisplayGetDecodedPicture(vout,
+                                                            &lost_count, &is_forced,
+                                                            now, deadline);
+        if (!decoded)
+            break;
 
         /* */
         vout_chrono_Start(&vout->p->render);
 
         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;
         }
 
         /*
@@ -713,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();
@@ -775,26 +825,94 @@ 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)
 {
     spu_ChangeFilters(vout->p->p_spu, filters);
 }
+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)
 {
@@ -812,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;
@@ -836,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);
 }
 
@@ -958,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,
+                          VoutVideoFilterStaticAllocationSetup, NULL, vout);
+    vout->p->filter.chain_interactive =
         filter_chain_New( vout, "video filter2", false,
-                          VoutVideoFilterAllocationSetup, NULL, vout);
+                          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) {
@@ -995,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) {
@@ -1007,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);
@@ -1132,6 +1262,9 @@ static void *Thread(void *object)
             case VOUT_CONTROL_CHANGE_SUB_FILTERS:
                 ThreadChangeSubFilters(vout, cmd.u.string);
                 break;
+            case VOUT_CONTROL_CHANGE_SUB_MARGIN:
+                ThreadChangeSubMargin(vout, cmd.u.integer);
+                break;
             case VOUT_CONTROL_PAUSE:
                 ThreadChangePause(vout, cmd.u.pause.is_on, cmd.u.pause.date);
                 break;