summaryrefslogtreecommitdiffstats
path: root/player/discnav.c
blob: 2c24fc9a427d0b919698514c9165b246be42d535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * This file is part of mpv.
 *
 * mpv 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
 * (at your option) any later version.
 *
 * mpv 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <limits.h>
#include <pthread.h>
#include <assert.h>

#include "core.h"
#include "command.h"

#include "common/msg.h"
#include "common/common.h"
#include "input/input.h"

#include "demux/demux.h"
#include "stream/discnav.h"

#include "sub/dec_sub.h"
#include "sub/osd.h"

#include "video/mp_image.h"
#include "video/decode/dec_video.h"

struct mp_nav_state {
    struct mp_log *log;

    int nav_still_frame;
    bool nav_eof;
    bool nav_menu;
    bool nav_draining;

    // Accessed by OSD (possibly separate thread)
    // Protected by the given lock
    pthread_mutex_t osd_lock;
    int hi_visible;
    int highlight[4]; // x0 y0 x1 y1
    int vidsize[2];
    int subsize[2];
    struct sub_bitmap *hi_elem;
    struct sub_bitmap *overlays[2];
    struct sub_bitmap outputs[3];
};

static inline bool is_valid_size(int size[2])
{
    return size[0] >= 1 && size[1] >= 1;
}

static void update_resolution(struct MPContext *mpctx)
{
    struct mp_nav_state *nav = mpctx->nav_state;
    int size[2] = {0};
    if (mpctx->d_sub[0])
        sub_control(mpctx->d_sub[0], SD_CTRL_GET_RESOLUTION, size);
    if (!is_valid_size(size)) {
        struct mp_image_params vid = {0};
        if (mpctx->d_video)
            vid = mpctx->d_video->decoder_output;
        size[0] = vid.w;
        size[1] = vid.h;
    }
    pthread_mutex_lock(&nav->osd_lock);
    nav->vidsize[0] = size[0];
    nav->vidsize[1] = size[1];
    pthread_mutex_unlock(&nav->osd_lock);
}

// Send update events and such.
static void update_state(struct MPContext *mpctx)
{
    mp_notify_property(mpctx, "disc-menu-active");
}

// Return 1 if in menu, 0 if in video, or <0 if no navigation possible.
int mp_nav_in_menu(struct MPContext *mpctx)
{
    return mpctx->nav_state ? mpctx->nav_state->nav_menu : -1;
}

// If a demuxer is accessing the stream, we have to use demux_stream_control()
// to avoid synchronization issues; otherwise access it directly.
static int run_stream_control(struct MPContext *mpctx, int cmd, void *arg)
{
    if (mpctx->demuxer) {
        return demux_stream_control(mpctx->demuxer, cmd, arg);
    } else if (mpctx->stream) {
        return stream_control(mpctx->stream, cmd, arg);
    }
    return STREAM_ERROR;
}

// Allocate state and enable navigation features. Must happen before
// initializing cache, because the cache would read data. Since stream_dvdnav is
// in a mode which skips all transitions on reading data (before enabling
// navigation), this would skip some menu screens.
void mp_nav_init(struct MPContext *mpctx)
{
    assert(!mpctx->nav_state);

    // dvdnav is interactive
    if (mpctx->encode_lavc_ctx)
        return;

    struct mp_nav_cmd inp = {MP_NAV_CMD_ENABLE};
    if (run_stream_control(mpctx, STREAM_CTRL_NAV_CMD, &inp) < 1)
        return;

    mpctx->nav_state = talloc_zero(NULL, struct mp_nav_state);
    mpctx->nav_state->log = mp_log_new(mpctx->nav_state, mpctx->log, "discnav");
    pthread_mutex_init(&mpctx->nav_state->osd_lock, NULL);

    MP_VERBOSE(mpctx->nav_state, "enabling\n");

    mp_input_enable_section(mpctx->input, "discnav", 0);
    mp_input_set_section_mouse_area(mpctx->input, "discnav-menu",
                                    INT_MIN, INT_MIN, INT_MAX, INT_MAX);

    update_state(mpctx);
}

void mp_nav_reset(struct MPContext *mpctx)
{
    struct mp_nav_state *nav = mpctx->nav_state;
    if (!nav)
        return;
    struct mp_nav_cmd inp = {MP_NAV_CMD_RESUME};
    run_stream_control(mpctx, STREAM_CTRL_NAV_CMD, &inp);
    osd_set_nav_highlight(mpctx->osd, NULL);
    nav->hi_visible = 0;
    nav->nav_menu = false;
    nav->nav_draining = false;
    nav->nav_still_frame = 0;
    mp_input_disable_section(mpctx->input, "discnav-menu");
    run_stream_control(mpctx, STREAM_CTRL_RESUME_CACHE, NULL);
    update_state(mpctx);
}

void mp_nav_destroy(struct MPContext *mpctx)
{
    osd_set_nav_highlight(mpctx->osd, NULL);
    if (!mpctx->nav_state)
        return;
    mp_input_disable_section(mpctx->input, "discnav");
    mp_input_disable_section(mpctx->input, "discnav-menu");
    pthread_mutex_destroy(&mpctx->nav_state->osd_lock);
    talloc_free(mpctx->nav_state);
    mpctx->nav_state = NULL;
    update_state(mpctx);
}

void mp_nav_user_input(struct MPContext *mpctx, char *command)
{
    struct mp_nav_state *nav = mpctx->nav_state;
    if (!nav)
        return;
    if (strcmp(command, "mouse_move") == 0) {
        struct mp_image_params vid = {0};
        if (mpctx->d_video)
            vid = mpctx->d_video->decoder_output;
        struct mp_nav_cmd inp = {MP_NAV_CMD_MOUSE_POS};
        int x, y;
        mp_input_get_mouse_pos(mpctx->input, &x, &y);
        osd_coords_to_video(mpctx->osd, vid.w, vid.h, &x, &y);
        inp.u.mouse_pos.x = x;
        inp.u.mouse_pos.y = y;
        run_stream_control(mpctx, STREAM_CTRL_NAV_CMD, &inp);
    } else {
        struct mp_nav_cmd inp = {MP_NAV_CMD_MENU};
        inp.u.menu.action = command;
        run_stream_control(mpctx, STREAM_CTRL_NAV_CMD, &inp);
    }
}

void mp_handle_nav(struct MPContext *mpctx)
{
    struct mp_nav_state *nav = mpctx->nav_state;
    if (!nav)
        return;
    while (1) {
        struct mp_nav_event *ev = NULL;
        run_stream_control(mpctx, STREAM_CTRL_GET_NAV_EVENT, &ev);
        if (!ev)
            break;
        switch (ev->event) {
        case MP_NAV_EVENT_DRAIN: {
            nav->nav_draining = true;
            MP_VERBOSE(nav, "drain requested\n");
            break;
        }
        case MP_NAV_EVENT_RESET_ALL: {
            mpctx->stop_play = PT_RELOAD_DEMUXER;
            MP_VERBOSE(nav, "reload\n");
            // return immediately.
            // other events should be handled after reloaded.
            talloc_free(ev);
            return;
        }
        case MP_NAV_EVENT_RESET: {
            nav->nav_still_frame = 0;
            break;
        }
        case MP_NAV_EVENT_EOF:
            nav->nav_eof = true;
            break;
        case MP_NAV_EVENT_STILL_FRAME: {
            int len = ev->u.still_frame.seconds;
            MP_VERBOSE(nav, "wait for %d seconds\n", len);
            if (len > 0 && nav->nav_still_frame == 0)
                nav->nav_still_frame = len;
            break;
        }
        case MP_NAV_EVENT_MENU_MODE:
            nav->nav_menu = ev->u.menu_mode.enable;
            if (nav->nav_menu) {
                mp_input_enable_section(mpctx->input, "discnav-menu",
                                        MP_INPUT_ON_TOP);
            } else {
                mp_input_disable_section(mpctx->input, "discnav-menu");
            }
            update_state(mpctx);
            break;
        case MP_NAV_EVENT_HIGHLIGHT: {
            pthread_mutex_lock(&nav->osd_lock);
            MP_VERBOSE(nav, "highlight: %d %d %d - %d %d\n",
                       ev->u.highlight.display,
                       ev->u.highlight.sx, ev->u.highlight.sy,
                       ev->u.highlight.ex, ev->u.highlight.ey);
            nav->highlight[0] = ev->u.highlight.sx;
            nav->highlight[1] = ev->u.highlight.sy;
            nav->highlight[2] = ev->u.highlight.ex;
            nav->highlight[3] = ev->u.highlight.ey;
            nav->hi_visible = ev->u.highlight.display;
            pthread_mutex_unlock(&nav->osd_lock);
            update_resolution(mpctx);
            osd_set_nav_highlight(mpctx->osd, mpctx);
            break;
        }
        case MP_NAV_EVENT_OVERLAY: {
            pthread_mutex_lock(&nav->osd_lock);
            for (int i = 0; i < 2; i++) {
                if (nav->overlays[i])
                    talloc_free(nav->overlays[i]);
                nav->overlays[i] = talloc_steal(nav, ev->u.overlay.images[i]);
            }
            pthread_mutex_unlock(&nav->osd_lock);
            update_resolution(mpctx);
            osd_set_nav_highlight(mpctx->osd, mpctx);
            break;
        }
        default: ; // ignore
        }
        talloc_free(ev);
    }
    update_resolution(mpctx);
    if (mpctx->stop_play == AT_END_OF_FILE) {
        if (nav->nav_still_frame > 0) {
            // gross hack
            mpctx->time_frame += nav->nav_still_frame;
            mpctx->playing_last_frame = true;
            nav->nav_still_frame = -2;
        } else if (nav->nav_still_frame == -2) {
            struct mp_nav_cmd inp = {MP_NAV_CMD_SKIP_STILL};
            run_stream_control(mpctx, STREAM_CTRL_NAV_CMD, &inp);
        }
    }
    if (nav->nav_draining && mpctx->stop_play == AT_END_OF_FILE) {
        MP_VERBOSE(nav, "execute drain\n");
        struct mp_nav_cmd inp = {MP_NAV_CMD_DRAIN_OK};
        run_stream_control(mpctx, STREAM_CTRL_NAV_CMD, &inp);
        nav->nav_draining = false;
        run_stream_control(mpctx, STREAM_CTRL_RESUME_CACHE, NULL);
    }
    // E.g. keep displaying still frames
    if (mpctx->stop_play == AT_END_OF_FILE && !nav->nav_eof)
        mpctx->stop_play = KEEP_PLAYING;
}

// Render "fake" highlights, because using actual dvd sub highlight elements
// is too hard, and would require changes to libavcodec's dvdsub decoder.
// Note: a proper solution would introduce something like
//       SD_CTRL_APPLY_DVDNAV, which would crop the vobsub frame,
//       and apply the current CLUT.
void mp_nav_get_highlight(void *priv, struct mp_osd_res res,
                          struct sub_bitmaps *out_imgs)
{
    struct MPContext *mpctx = priv;
    struct mp_nav_state *nav = mpctx->nav_state;

    pthread_mutex_lock(&nav->osd_lock);

    struct sub_bitmap *sub = nav->hi_elem;
    if (!sub)
        sub = talloc_zero(nav, struct sub_bitmap);

    nav->hi_elem = sub;
    if (!is_valid_size(nav->vidsize)) {
        pthread_mutex_unlock(&nav->osd_lock);
        return;
    }
    int sizes[2] = {nav->vidsize[0], nav->vidsize[1]};
    if (sizes[0] != nav->subsize[0] || sizes[1] != nav->subsize[1]) {
        talloc_free(sub->bitmap);
        sub->bitmap = talloc_array(sub, uint32_t, sizes[0] * sizes[1]);
        memset(sub->bitmap, 0x80, talloc_get_size(sub->bitmap));
        nav->subsize[0] = sizes[0];
        nav->subsize[1] = sizes[1];
    }

    out_imgs->num_parts = 0;

    if (nav->hi_visible) {
        sub->x = nav->highlight[0];
        sub->y = nav->highlight[1];
        sub->w = MPCLAMP(nav->highlight[2] - sub->x, 0, sizes[0]);
        sub->h = MPCLAMP(nav->highlight[3] - sub->y, 0, sizes[1]);
        sub->stride = sub->w * 4;
        if (sub->w > 0 && sub->h > 0)
            nav->outputs[out_imgs->num_parts++] = *sub;
    }

    if (nav->overlays[0])
        nav->outputs[out_imgs->num_parts++] = *nav->overlays[0];
    if (nav->overlays[1])
        nav->outputs[out_imgs->num_parts++] = *nav->overlays[1];

    if (out_imgs->num_parts) {
        out_imgs->parts = nav->outputs;
        out_imgs->format = SUBBITMAP_RGBA;
        osd_rescale_bitmaps(out_imgs, sizes[0], sizes[1], res, -1);
    }

    pthread_mutex_unlock(&nav->osd_lock);
}