bluray.c 50.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * bluray.c: Blu-ray disc support plugin
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright © 2010-2012 VideoLAN, VLC authors and libbluray AUTHORS
5 6
 *
 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
7
 *          Hugo Beauzée-Luyssen <hugo@videolan.org>
8
 *
9 10 11
 * 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
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Lesser General Public License for more details.
18
 *
19 20 21
 * 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.
22 23 24 25 26 27 28
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
29
#include <limits.h>                         /* PATH_MAX */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
30

31
#if defined (HAVE_MNTENT_H) && defined(HAVE_SYS_STAT_H)
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
32 33
# include <mntent.h>
# include <sys/stat.h>
34
#endif
35

36
#ifdef __APPLE__
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
37 38 39 40
# include <sys/stat.h>
# include <sys/param.h>
# include <sys/ucred.h>
# include <sys/mount.h>
41 42
#endif

43 44
#include <vlc_common.h>
#include <vlc_plugin.h>
45 46
#include <vlc_demux.h>                      /* demux_t */
#include <vlc_input.h>                      /* Seekpoints, chapters */
47
#include <vlc_atomic.h>
48
#include <vlc_dialog.h>                     /* BD+/AACS warnings */
49
#include <vlc_vout.h>                       /* vout_PutSubpicture / subpicture_t */
50
#include <vlc_url.h>                        /* vlc_path2uri */
51 52 53 54 55
#include <vlc_iso_lang.h>

/* FIXME we should find a better way than including that */
#include "../../src/text/iso-639_def.h"

56 57

#include <libbluray/bluray.h>
58
#include <libbluray/bluray-version.h>
59
#include <libbluray/keys.h>
60
#include <libbluray/meta_data.h>
61
#include <libbluray/overlay.h>
62 63 64 65 66

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

Rafaël Carré's avatar
Rafaël Carré committed
67 68 69 70 71
#define BD_MENU_TEXT        N_("Blu-ray menus")
#define BD_MENU_LONGTEXT    N_("Use Blu-ray menus. If disabled, "\
                                "the movie will start directly")
#define BD_REGION_TEXT      N_("Region code")
#define BD_REGION_LONGTEXT  N_("Blu-Ray player region code. "\
72 73 74 75 76 77 78 79
                                "Some discs can be played only with a correct region code.")

static const char *const ppsz_region_code[] = {
    "A", "B", "C" };
static const char *const ppsz_region_code_text[] = {
    "Region A", "Region B", "Region C" };

#define REGION_DEFAULT   1   /* Index to region list. Actual region code is (1<<REGION_DEFAULT) */
80
#define LANGUAGE_DEFAULT ("eng")
81

82
/* Callbacks */
Rafaël Carré's avatar
Rafaël Carré committed
83 84
static int  blurayOpen (vlc_object_t *);
static void blurayClose(vlc_object_t *);
85 86

vlc_module_begin ()
Rafaël Carré's avatar
Rafaël Carré committed
87 88
    set_shortname(N_("Blu-ray"))
    set_description(N_("Blu-ray Disc support (libbluray)"))
89

Rafaël Carré's avatar
Rafaël Carré committed
90 91 92 93 94 95
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_ACCESS)
    set_capability("access_demux", 200)
    add_bool("bluray-menu", false, BD_MENU_TEXT, BD_MENU_LONGTEXT, false)
    add_string("bluray-region", ppsz_region_code[REGION_DEFAULT], BD_REGION_TEXT, BD_REGION_LONGTEXT, false)
        change_string_list(ppsz_region_code, ppsz_region_code_text)
96

Rafaël Carré's avatar
Rafaël Carré committed
97
    add_shortcut("bluray", "file")
98

Rafaël Carré's avatar
Rafaël Carré committed
99
    set_callbacks(blurayOpen, blurayClose)
100 101
vlc_module_end ()

102 103
/* libbluray's overlay.h defines 2 types of overlay (bd_overlay_plane_e). */
#define MAX_OVERLAY 2
104

105 106 107 108 109 110 111 112
typedef enum OverlayStatus {
    Closed = 0,
    ToDisplay,  //Used to mark the overlay to be displayed the first time.
    Displayed,
    Outdated    //used to update the overlay after it has been sent to the vout
} OverlayStatus;

typedef struct bluray_overlay_t
113
{
114
    atomic_flag         released_once;
115 116 117 118 119 120 121 122 123
    vlc_mutex_t         lock;
    subpicture_t        *p_pic;
    OverlayStatus       status;
    subpicture_region_t *p_regions;
} bluray_overlay_t;

struct  demux_sys_t
{
    BLURAY              *bluray;
124 125

    /* Titles */
126 127
    unsigned int        i_title;
    unsigned int        i_longest_title;
128
    int                 i_playlist;          /* -1 = no playlist playing */
129
    unsigned int        i_current_clip;
130
    input_title_t       **pp_title;
131

132
    /* Meta information */
133 134
    const META_DL       *p_meta;

135
    /* Menus */
136 137 138 139 140 141 142
    bluray_overlay_t    *p_overlays[MAX_OVERLAY];
    int                 current_overlay; // -1 if no current overlay;
    bool                b_menu;

    /* */
    input_thread_t      *p_input;
    vout_thread_t       *p_vout;
143

144
    /* TS stream */
145
    es_out_t            *p_out;
146
    vlc_array_t         es;
147
    int                 i_audio_stream; /* Selected audio stream. -1 if default */
148
    int                 i_video_stream;
149
    stream_t            *p_parser;
150 151 152

    /* Used to store bluray disc path */
    char                *psz_bd_path;
153 154 155 156 157
};

struct subpicture_updater_sys_t
{
    bluray_overlay_t    *p_overlay;
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
/* Get a 3 char code
 * FIXME: partiallyy duplicated from src/input/es_out.c
 */
static const char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
{
    const iso639_lang_t *pl;
    char *psz_lang;
    char *p;

    psz_lang = var_CreateGetString( p_demux, psz_var );
    if( !psz_lang )
        return LANGUAGE_DEFAULT;

    /* XXX: we will use only the first value
     * (and ignore other ones in case of a list) */
    if( ( p = strchr( psz_lang, ',' ) ) )
        *p = '\0';

    for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
    {
        if( *psz_lang == '\0' )
            continue;
        if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
            !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
            !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
            !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
            break;
    }

    free( psz_lang );

    if( pl->psz_eng_name != NULL )
        return pl->psz_iso639_2T;

    return LANGUAGE_DEFAULT;
}

197 198 199
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
200
static es_out_t *esOutNew(demux_t *p_demux);
201

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
202 203
static int   blurayControl(demux_t *, int, va_list);
static int   blurayDemux(demux_t *);
204

205
static void  blurayInitTitles(demux_t *p_demux, int menu_titles);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
206
static int   bluraySetTitle(demux_t *p_demux, int i_title);
207

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
208
static void  blurayOverlayProc(void *ptr, const BD_OVERLAY * const overlay);
209
static void  blurayArgbOverlayProc(void *ptr, const BD_ARGB_OVERLAY * const overlay);
210

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
211 212
static int   onMouseEvent(vlc_object_t *p_vout, const char *psz_var,
                          vlc_value_t old, vlc_value_t val, void *p_data);
213

214 215
static void  blurayResetParser(demux_t *p_demux);

216 217 218
#define FROM_TICKS(a) (a*CLOCK_FREQ / INT64_C(90000))
#define TO_TICKS(a)   (a*INT64_C(90000)/CLOCK_FREQ)
#define CUR_LENGTH    p_sys->pp_title[p_demux->info.i_title]->i_length
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
/* */
static void FindMountPoint(char **file)
{
    char *device = *file;
#if defined (HAVE_MNTENT_H) && defined (HAVE_SYS_STAT_H)
    struct stat st;
    if (!stat (device, &st) && S_ISBLK (st.st_mode)) {
        FILE *mtab = setmntent ("/proc/self/mounts", "r");
        struct mntent *m, mbuf;
        char buf [8192];
        /* bd path may be a symlink (e.g. /dev/dvd -> /dev/sr0), so make
         * sure we look up the real device */
        char *bd_device = realpath(device, NULL);
        if (!bd_device)
            bd_device = strdup(device);

        while ((m = getmntent_r (mtab, &mbuf, buf, sizeof(buf))) != NULL) {
            if (!strcmp (m->mnt_fsname, bd_device)) {
                free(device);
                *file = strdup(m->mnt_dir);
                break;
            }
        }
        free(bd_device);
        endmntent (mtab);
    }
#elif defined(__APPLE__)
    struct stat st;
    if (!stat (device, &st) && S_ISBLK (st.st_mode)) {
        int fs_count = getfsstat (NULL, 0, MNT_NOWAIT);
        if (fs_count > 0) {
            struct statfs mbuf[128];
            getfsstat (mbuf, fs_count * sizeof(mbuf[0]), MNT_NOWAIT);
            for (int i = 0; i < fs_count; ++i)
                if (!strcmp (mbuf[i].f_mntfromname, device)) {
                    free(device);
                    *file = strdup(mbuf[i].f_mntonname);
                    return;
                }
        }
    }
#else
# warning Disc device to mount point not implemented
#endif
}

266 267 268
/*****************************************************************************
 * blurayOpen: module init function
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
269
static int blurayOpen(vlc_object_t *object)
270
{
271 272
    demux_t *p_demux = (demux_t*)object;
    demux_sys_t *p_sys;
273

274
    const char *error_msg = NULL;
275
#define BLURAY_ERROR(s) do { error_msg = s; goto error; } while(0)
276

277
    if (strcmp(p_demux->psz_access, "bluray")) {
278
        // TODO BDMV support, once we figure out what to do in libbluray
279 280 281
        return VLC_EGENERIC;
    }

282
    /* */
283
    p_demux->p_sys = p_sys = calloc(1, sizeof(*p_sys));
Rafaël Carré's avatar
Rafaël Carré committed
284
    if (unlikely(!p_sys))
285
        return VLC_ENOMEM;
Rafaël Carré's avatar
Rafaël Carré committed
286

287
    p_sys->current_overlay = -1;
288
    p_sys->i_audio_stream = -1;
289
    p_sys->i_video_stream = -1;
290 291 292 293 294

    /* init demux info fields */
    p_demux->info.i_update    = 0;
    p_demux->info.i_title     = 0;
    p_demux->info.i_seekpoint = 0;
295

Rafaël Carré's avatar
Rafaël Carré committed
296
    TAB_INIT(p_sys->i_title, p_sys->pp_title);
297

298
    /* store current bd path */
Rafaël Carré's avatar
Rafaël Carré committed
299 300
    if (p_demux->psz_file)
        p_sys->psz_bd_path = strdup(p_demux->psz_file);
301

302
    /* If we're passed a block device, try to convert it to the mount point. */
303 304
    FindMountPoint(&p_sys->psz_bd_path);

305
    p_sys->bluray = bd_open(p_sys->psz_bd_path, NULL);
306
    if (!p_sys->bluray) {
307
        free(p_sys->psz_bd_path);
308 309 310 311
        free(p_sys);
        return VLC_EGENERIC;
    }

312 313
    /* Warning the user about AACS/BD+ */
    const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
314 315

    /* Is it a bluray? */
316 317
    if (!disc_info->bluray_detected)
        BLURAY_ERROR(_("Path doesn't appear to be a Blu-ray"));
318

319 320
    msg_Info(p_demux, "First play: %i, Top menu: %i\n"
                      "HDMV Titles: %i, BD-J Titles: %i, Other: %i",
321 322 323 324 325 326
             disc_info->first_play_supported, disc_info->top_menu_supported,
             disc_info->num_hdmv_titles, disc_info->num_bdj_titles,
             disc_info->num_unsupported_titles);

    /* AACS */
    if (disc_info->aacs_detected) {
327
        msg_Dbg(p_demux, "Disc is using AACS");
328 329 330
        if (!disc_info->libaacs_detected)
            BLURAY_ERROR(_("This Blu-ray Disc needs a library for AACS decoding"
                      ", and your system does not have it."));
331
        if (!disc_info->aacs_handled) {
332 333
            if (disc_info->aacs_error_code) {
                switch (disc_info->aacs_error_code) {
334 335 336 337 338 339 340 341 342 343 344 345
                case BD_AACS_CORRUPTED_DISC:
                    BLURAY_ERROR(_("Blu-ray Disc is corrupted."));
                case BD_AACS_NO_CONFIG:
                    BLURAY_ERROR(_("Missing AACS configuration file!"));
                case BD_AACS_NO_PK:
                    BLURAY_ERROR(_("No valid processing key found in AACS config file."));
                case BD_AACS_NO_CERT:
                    BLURAY_ERROR(_("No valid host certificate found in AACS config file."));
                case BD_AACS_CERT_REVOKED:
                    BLURAY_ERROR(_("AACS Host certificate revoked."));
                case BD_AACS_MMC_FAILED:
                    BLURAY_ERROR(_("AACS MMC failed."));
346 347
                }
            }
348 349 350 351 352
        }
    }

    /* BD+ */
    if (disc_info->bdplus_detected) {
353
        msg_Dbg(p_demux, "Disc is using BD+");
354 355 356 357 358 359
        if (!disc_info->libbdplus_detected)
            BLURAY_ERROR(_("This Blu-ray Disc needs a library for BD+ decoding"
                      ", and your system does not have it."));
        if (!disc_info->bdplus_handled)
            BLURAY_ERROR(_("Your system BD+ decoding library does not work. "
                      "Missing configuration?"));
360 361
    }

362 363 364 365 366 367
    /* set player region code */
    char *psz_region = var_InheritString(p_demux, "bluray-region");
    unsigned int region = psz_region ? (psz_region[0] - 'A') : REGION_DEFAULT;
    free(psz_region);
    bd_set_player_setting(p_sys->bluray, BLURAY_PLAYER_SETTING_REGION_CODE, 1<<region);

368 369 370 371 372 373 374 375
    /* set preferred languages */
    const char *psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
    bd_set_player_setting_str(p_sys->bluray, BLURAY_PLAYER_SETTING_AUDIO_LANG, psz_code);
    psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
    bd_set_player_setting_str(p_sys->bluray, BLURAY_PLAYER_SETTING_PG_LANG,    psz_code);
    psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
    bd_set_player_setting_str(p_sys->bluray, BLURAY_PLAYER_SETTING_MENU_LANG,  psz_code);

376
    /* Get titles and chapters */
377 378
    p_sys->p_meta = bd_get_meta(p_sys->bluray);
    if (!p_sys->p_meta)
Rafaël Carré's avatar
Rafaël Carré committed
379
        msg_Warn(p_demux, "Failed to get meta info.");
380

381 382 383
    p_sys->b_menu = var_InheritBool(p_demux, "bluray-menu");

    blurayInitTitles(p_demux, disc_info->num_hdmv_titles + disc_info->num_bdj_titles + 1/*Top Menu*/ + 1/*First Play*/);
384

385 386 387 388 389
    /*
     * Initialize the event queue, so we can receive events in blurayDemux(Menu).
     */
    bd_get_event(p_sys->bluray, NULL);

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
390
    if (p_sys->b_menu) {
391
        p_sys->p_input = demux_GetParentInput(p_demux);
Rafaël Carré's avatar
Rafaël Carré committed
392
        if (unlikely(!p_sys->p_input)) {
393
            msg_Err(p_demux, "Could not get parent input");
394
            goto error;
Rafaël Carré's avatar
Rafaël Carré committed
395
        }
396

397
        /* Register ARGB overlay handler for BD-J */
398
        if (disc_info->num_bdj_titles)
399 400
            bd_register_argb_overlay_proc(p_sys->bluray, p_demux, blurayArgbOverlayProc, NULL);

401
        /* libbluray will start playback from "First-Title" title */
402 403 404
        if (bd_play(p_sys->bluray) == 0)
            BLURAY_ERROR(_("Failed to start bluray playback. Please try without menu support."));

405 406
        /* Registering overlay event handler */
        bd_register_overlay_proc(p_sys->bluray, p_demux, blurayOverlayProc);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
407
    } else {
408
        /* set start title number */
409
        if (bluraySetTitle(p_demux, p_sys->i_longest_title) != VLC_SUCCESS) {
Rafaël Carré's avatar
Rafaël Carré committed
410
            msg_Err(p_demux, "Could not set the title %d", p_sys->i_longest_title);
411 412
            goto error;
        }
413 414
    }

415
    vlc_array_init(&p_sys->es);
Rafaël Carré's avatar
Rafaël Carré committed
416
    p_sys->p_out = esOutNew(p_demux);
Rafaël Carré's avatar
Rafaël Carré committed
417
    if (unlikely(p_sys->p_out == NULL))
418 419
        goto error;

Rafaël Carré's avatar
Rafaël Carré committed
420
    blurayResetParser(p_demux);
421 422
    if (!p_sys->p_parser) {
        msg_Err(p_demux, "Failed to create TS demuxer");
423
        goto error;
424 425 426 427
    }

    p_demux->pf_control = blurayControl;
    p_demux->pf_demux   = blurayDemux;
428 429

    return VLC_SUCCESS;
430 431 432

error:
    if (error_msg)
Christoph Miebach's avatar
Christoph Miebach committed
433
        dialog_Fatal(p_demux, _("Blu-ray error"), "%s", error_msg);
434 435
    blurayClose(object);
    return VLC_EGENERIC;
436
#undef BLURAY_ERROR
437 438 439 440 441 442
}


/*****************************************************************************
 * blurayClose: module destroy function
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
443
static void blurayClose(vlc_object_t *object)
444
{
445 446 447
    demux_t *p_demux = (demux_t*)object;
    demux_sys_t *p_sys = p_demux->p_sys;

448 449 450
    /*
     * Close libbluray first.
     * This will close all the overlays before we release p_vout
Rafaël Carré's avatar
Rafaël Carré committed
451
     * bd_close(NULL) can crash
452 453 454 455
     */
    assert(p_sys->bluray);
    bd_close(p_sys->bluray);

456
    if (p_sys->p_vout != NULL) {
Rafaël Carré's avatar
Rafaël Carré committed
457 458
        var_DelCallback(p_sys->p_vout, "mouse-moved", onMouseEvent, p_demux);
        var_DelCallback(p_sys->p_vout, "mouse-clicked", onMouseEvent, p_demux);
459
        vlc_object_release(p_sys->p_vout);
460
    }
461 462
    if (p_sys->p_input != NULL)
        vlc_object_release(p_sys->p_input);
463 464
    if (p_sys->p_parser)
        stream_Delete(p_sys->p_parser);
465 466
    if (p_sys->p_out != NULL)
        es_out_Delete(p_sys->p_out);
Rafaël Carré's avatar
Rafaël Carré committed
467 468
    assert(vlc_array_count(&p_sys->es) == 0);
    vlc_array_clear(&p_sys->es);
469

470 471 472
    /* Titles */
    for (unsigned int i = 0; i < p_sys->i_title; i++)
        vlc_input_title_Delete(p_sys->pp_title[i]);
Rafaël Carré's avatar
Rafaël Carré committed
473
    TAB_CLEAN(p_sys->i_title, p_sys->pp_title);
474

475
    free(p_sys->psz_bd_path);
476 477 478
    free(p_sys);
}

479 480 481 482 483 484 485 486
/*****************************************************************************
 * Elementary streams handling
 *****************************************************************************/

struct es_out_sys_t {
    demux_t *p_demux;
};

487 488 489 490 491
typedef struct  fmt_es_pair {
    int         i_id;
    es_out_id_t *p_es;
}               fmt_es_pair_t;

Rafaël Carré's avatar
Rafaël Carré committed
492
static int  findEsPairIndex(demux_sys_t *p_sys, int i_id)
493
{
Rafaël Carré's avatar
Rafaël Carré committed
494 495
    for (int i = 0; i < vlc_array_count(&p_sys->es); ++i)
        if (((fmt_es_pair_t*)vlc_array_item_at_index(&p_sys->es, i))->i_id == i_id)
496
            return i;
Rafaël Carré's avatar
Rafaël Carré committed
497

498 499 500
    return -1;
}

Rafaël Carré's avatar
Rafaël Carré committed
501
static int  findEsPairIndexByEs(demux_sys_t *p_sys, es_out_id_t *p_es)
502
{
Rafaël Carré's avatar
Rafaël Carré committed
503 504
    for (int i = 0; i < vlc_array_count(&p_sys->es); ++i)
        if (((fmt_es_pair_t*)vlc_array_item_at_index(&p_sys->es, i))->p_es == p_es)
505
            return i;
Rafaël Carré's avatar
Rafaël Carré committed
506

507 508 509
    return -1;
}

510 511 512 513 514 515 516 517 518 519 520 521
static void setStreamLang(es_format_t *p_fmt,
                          const BLURAY_STREAM_INFO *p_streams, int i_stream_count)
{
    for (int i = 0; i < i_stream_count; i++) {
        if (p_fmt->i_id == p_streams[i].pid) {
            free(p_fmt->psz_language);
            p_fmt->psz_language = strndup(p_streams[i].lang, 3);
            return;
        }
    }
}

Rafaël Carré's avatar
Rafaël Carré committed
522
static es_out_id_t *esOutAdd(es_out_t *p_out, const es_format_t *p_fmt)
523
{
524
    demux_sys_t *p_sys = p_out->p_sys->p_demux->p_sys;
525 526
    BLURAY_TITLE_INFO *title_info = bd_get_playlist_info(p_sys->bluray, p_sys->i_playlist, 0);
    BLURAY_CLIP_INFO *clip_info = NULL;
527 528
    es_format_t fmt;

529 530 531 532
    if (title_info && p_sys->i_current_clip < title_info->clip_count) {
        clip_info = &title_info->clips[p_sys->i_current_clip];
    }

533
    es_format_Copy(&fmt, p_fmt);
Rafaël Carré's avatar
Rafaël Carré committed
534
    switch (fmt.i_cat) {
535
    case VIDEO_ES:
Rafaël Carré's avatar
Rafaël Carré committed
536
        if (p_sys->i_video_stream != -1 && p_sys->i_video_stream != p_fmt->i_id)
537
            fmt.i_priority = ES_PRIORITY_NOT_SELECTABLE;
538 539
        break ;
    case AUDIO_ES:
Rafaël Carré's avatar
Rafaël Carré committed
540
        if (p_sys->i_audio_stream != -1 && p_sys->i_audio_stream != p_fmt->i_id)
541
            fmt.i_priority = ES_PRIORITY_NOT_SELECTABLE;
542 543
        if (clip_info)
            setStreamLang(&fmt, clip_info->audio_streams, clip_info->audio_stream_count);
544 545
        break ;
    case SPU_ES:
546 547
        if (clip_info)
            setStreamLang(&fmt, clip_info->pg_streams, clip_info->pg_stream_count);
548 549 550
        break ;
    }

551 552 553
    if (title_info)
        bd_free_title_info(title_info);

Rafaël Carré's avatar
Rafaël Carré committed
554 555
    es_out_id_t *p_es = es_out_Add(p_out->p_sys->p_demux->out, &fmt);
    if (p_fmt->i_id >= 0) {
556
        /* Ensure we are not overriding anything */
557
        int idx = findEsPairIndex(p_sys, p_fmt->i_id);
Rafaël Carré's avatar
Rafaël Carré committed
558 559 560
        if (idx == -1) {
            fmt_es_pair_t *p_pair = malloc(sizeof(*p_pair));
            if (likely(p_pair != NULL)) {
561 562
                p_pair->i_id = p_fmt->i_id;
                p_pair->p_es = p_es;
Rafaël Carré's avatar
Rafaël Carré committed
563
                msg_Info(p_out->p_sys->p_demux, "Adding ES %d", p_fmt->i_id);
564
                vlc_array_append(&p_sys->es, p_pair);
565 566 567
            }
        }
    }
568
    es_format_Clean(&fmt);
569
    return p_es;
570 571
}

Rafaël Carré's avatar
Rafaël Carré committed
572
static int esOutSend(es_out_t *p_out, es_out_id_t *p_es, block_t *p_block)
573
{
Rafaël Carré's avatar
Rafaël Carré committed
574
    return es_out_Send(p_out->p_sys->p_demux->out, p_es, p_block);
575 576
}

Rafaël Carré's avatar
Rafaël Carré committed
577
static void esOutDel(es_out_t *p_out, es_out_id_t *p_es)
578
{
Rafaël Carré's avatar
Rafaël Carré committed
579 580 581
    int idx = findEsPairIndexByEs(p_out->p_sys->p_demux->p_sys, p_es);
    if (idx >= 0) {
        free(vlc_array_item_at_index(&p_out->p_sys->p_demux->p_sys->es, idx));
582 583
        vlc_array_remove(&p_out->p_sys->p_demux->p_sys->es, idx);
    }
Rafaël Carré's avatar
Rafaël Carré committed
584
    es_out_Del(p_out->p_sys->p_demux->out, p_es);
585 586
}

Rafaël Carré's avatar
Rafaël Carré committed
587
static int esOutControl(es_out_t *p_out, int i_query, va_list args)
588
{
Rafaël Carré's avatar
Rafaël Carré committed
589
    return es_out_vaControl(p_out->p_sys->p_demux->out, i_query, args);
590 591
}

Rafaël Carré's avatar
Rafaël Carré committed
592
static void esOutDestroy(es_out_t *p_out)
593
{
Rafaël Carré's avatar
Rafaël Carré committed
594 595
    for (int i = 0; i < vlc_array_count(&p_out->p_sys->p_demux->p_sys->es); ++i)
        free(vlc_array_item_at_index(&p_out->p_sys->p_demux->p_sys->es, i));
596
    vlc_array_clear(&p_out->p_sys->p_demux->p_sys->es);
Rafaël Carré's avatar
Rafaël Carré committed
597 598
    free(p_out->p_sys);
    free(p_out);
599 600
}

Rafaël Carré's avatar
Rafaël Carré committed
601
static es_out_t *esOutNew(demux_t *p_demux)
602
{
Rafaël Carré's avatar
Rafaël Carré committed
603 604 605
    assert(vlc_array_count(&p_demux->p_sys->es) == 0);
    es_out_t    *p_out = malloc(sizeof(*p_out));
    if (unlikely(p_out == NULL))
606 607
        return NULL;

Rafaël Carré's avatar
Rafaël Carré committed
608 609 610 611 612
    p_out->pf_add       = esOutAdd;
    p_out->pf_control   = esOutControl;
    p_out->pf_del       = esOutDel;
    p_out->pf_destroy   = esOutDestroy;
    p_out->pf_send      = esOutSend;
613

Rafaël Carré's avatar
Rafaël Carré committed
614 615 616
    p_out->p_sys = malloc(sizeof(*p_out->p_sys));
    if (unlikely(p_out->p_sys == NULL)) {
        free(p_out);
617 618 619 620 621 622
        return NULL;
    }
    p_out->p_sys->p_demux = p_demux;
    return p_out;
}

623 624 625
/*****************************************************************************
 * subpicture_updater_t functions:
 *****************************************************************************/
Rafaël Carré's avatar
Rafaël Carré committed
626
static int subpictureUpdaterValidate(subpicture_t *p_subpic,
627 628
                                      bool b_fmt_src, const video_format_t *p_fmt_src,
                                      bool b_fmt_dst, const video_format_t *p_fmt_dst,
Rafaël Carré's avatar
Rafaël Carré committed
629
                                      mtime_t i_ts)
630
{
Rafaël Carré's avatar
Rafaël Carré committed
631 632 633 634 635
    VLC_UNUSED(b_fmt_src);
    VLC_UNUSED(b_fmt_dst);
    VLC_UNUSED(p_fmt_src);
    VLC_UNUSED(p_fmt_dst);
    VLC_UNUSED(i_ts);
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

    subpicture_updater_sys_t *p_upd_sys = p_subpic->updater.p_sys;
    bluray_overlay_t         *p_overlay = p_upd_sys->p_overlay;

    vlc_mutex_lock(&p_overlay->lock);
    int res = p_overlay->status == Outdated;
    vlc_mutex_unlock(&p_overlay->lock);
    return res;
}

/* This should probably be moved to subpictures.c afterward */
static subpicture_region_t* subpicture_region_Clone(subpicture_region_t *p_region_src)
{
    if (!p_region_src)
        return NULL;
    subpicture_region_t *p_region_dst = subpicture_region_New(&p_region_src->fmt);
    if (unlikely(!p_region_dst))
        return NULL;

    p_region_dst->i_x      = p_region_src->i_x;
    p_region_dst->i_y      = p_region_src->i_y;
    p_region_dst->i_align  = p_region_src->i_align;
    p_region_dst->i_alpha  = p_region_src->i_alpha;

    p_region_dst->psz_text = p_region_src->psz_text ? strdup(p_region_src->psz_text) : NULL;
    p_region_dst->psz_html = p_region_src->psz_html ? strdup(p_region_src->psz_html) : NULL;
    if (p_region_src->p_style != NULL) {
        p_region_dst->p_style = malloc(sizeof(*p_region_dst->p_style));
        p_region_dst->p_style = text_style_Copy(p_region_dst->p_style,
                                                p_region_src->p_style);
    }

    //Palette is already copied by subpicture_region_New, we just have to duplicate p_pixels
    for (int i = 0; i < p_region_src->p_picture->i_planes; i++)
        memcpy(p_region_dst->p_picture->p[i].p_pixels,
               p_region_src->p_picture->p[i].p_pixels,
               p_region_src->p_picture->p[i].i_lines * p_region_src->p_picture->p[i].i_pitch);
    return p_region_dst;
}

static void subpictureUpdaterUpdate(subpicture_t *p_subpic,
                                    const video_format_t *p_fmt_src,
                                    const video_format_t *p_fmt_dst,
                                    mtime_t i_ts)
{
    VLC_UNUSED(p_fmt_src);
    VLC_UNUSED(p_fmt_dst);
    VLC_UNUSED(i_ts);
    subpicture_updater_sys_t *p_upd_sys = p_subpic->updater.p_sys;
    bluray_overlay_t         *p_overlay = p_upd_sys->p_overlay;

    /*
     * When this function is called, all p_subpic regions are gone.
     * We need to duplicate our regions (stored internaly) to this subpic.
     */
    vlc_mutex_lock(&p_overlay->lock);

    subpicture_region_t *p_src = p_overlay->p_regions;
Rafaël Carré's avatar
Rafaël Carré committed
694
    if (!p_src) {
695
        vlc_mutex_unlock(&p_overlay->lock);
696
        return;
697
    }
698

Rafaël Carré's avatar
Rafaël Carré committed
699
    subpicture_region_t **p_dst = &p_subpic->p_region;
700 701 702
    while (p_src != NULL) {
        *p_dst = subpicture_region_Clone(p_src);
        if (*p_dst == NULL)
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
703
            break;
Rafaël Carré's avatar
Rafaël Carré committed
704
        p_dst = &(*p_dst)->p_next;
705 706 707 708 709 710 711 712
        p_src = p_src->p_next;
    }
    if (*p_dst != NULL)
        (*p_dst)->p_next = NULL;
    p_overlay->status = Displayed;
    vlc_mutex_unlock(&p_overlay->lock);
}

713 714
static void blurayCleanOverlayStruct(bluray_overlay_t *);

715 716
static void subpictureUpdaterDestroy(subpicture_t *p_subpic)
{
717
    blurayCleanOverlayStruct(p_subpic->updater.p_sys->p_overlay);
Rafaël Carré's avatar
Rafaël Carré committed
718
    free(p_subpic->updater.p_sys);
719 720
}

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
/*****************************************************************************
 * User input events:
 *****************************************************************************/
static int onMouseEvent(vlc_object_t *p_vout, const char *psz_var, vlc_value_t old,
                        vlc_value_t val, void *p_data)
{
    demux_t     *p_demux = (demux_t*)p_data;
    demux_sys_t *p_sys   = p_demux->p_sys;
    mtime_t     now      = mdate();
    VLC_UNUSED(old);
    VLC_UNUSED(p_vout);

    if (psz_var[6] == 'm')   //Mouse moved
        bd_mouse_select(p_sys->bluray, now, val.coords.x, val.coords.y);
    else if (psz_var[6] == 'c') {
        bd_mouse_select(p_sys->bluray, now, val.coords.x, val.coords.y);
        bd_user_input(p_sys->bluray, now, BD_VK_MOUSE_ACTIVATE);
    } else {
        assert(0);
    }
    return VLC_SUCCESS;
}

744 745 746
static int sendKeyEvent(demux_sys_t *p_sys, unsigned int key)
{
    mtime_t now = mdate();
Rafaël Carré's avatar
Rafaël Carré committed
747
    if (bd_user_input(p_sys->bluray, now, key) < 0)
748
        return VLC_EGENERIC;
Rafaël Carré's avatar
Rafaël Carré committed
749

750 751 752
    return VLC_SUCCESS;
}

753 754 755
/*****************************************************************************
 * libbluray overlay handling:
 *****************************************************************************/
756
static void blurayCleanOverlayStruct(bluray_overlay_t *p_overlay)
757
{
758 759
    if (!atomic_flag_test_and_set(&p_overlay->released_once))
        return;
760 761 762 763 764
    /*
     * This will be called when destroying the picture.
     * Don't delete it again from here!
     */
    vlc_mutex_destroy(&p_overlay->lock);
Rafaël Carré's avatar
Rafaël Carré committed
765
    subpicture_region_ChainDelete(p_overlay->p_regions);
766 767 768
    free(p_overlay);
}

769 770 771 772 773 774
static void blurayCloseOverlay(demux_t *p_demux, int plane)
{
    demux_sys_t *p_sys = p_demux->p_sys;
    bluray_overlay_t *ov = p_sys->p_overlays[plane];

    if (ov != NULL) {
775 776
        if (p_sys->p_vout)
            vout_FlushSubpictureChannel(p_sys->p_vout, ov->p_pic->i_channel);
777 778 779
        blurayCleanOverlayStruct(ov);
        if (p_sys->current_overlay == plane)
            p_sys->current_overlay = -1;
780

781 782
        p_sys->p_overlays[plane] = NULL;
    }
783

784
    for (int i = 0; i < MAX_OVERLAY; i++)
785 786
        if (p_sys->p_overlays[i])
            return;
787

788
    /* All overlays have been closed */
789 790 791 792
    var_DelCallback(p_sys->p_vout, "mouse-moved", onMouseEvent, p_demux);
    var_DelCallback(p_sys->p_vout, "mouse-clicked", onMouseEvent, p_demux);
    vlc_object_release(p_sys->p_vout);
    p_sys->p_vout = NULL;
793 794 795 796 797 798 799 800 801
}

/*
 * Mark the overlay as "ToDisplay" status.
 * This will not send the overlay to the vout instantly, as the vout
 * may not be acquired (not acquirable) yet.
 * If is has already been acquired, the overlay has already been sent to it,
 * therefore, we only flag the overlay as "Outdated"
 */
802
static void blurayActivateOverlay(demux_t *p_demux, int plane)
803 804
{
    demux_sys_t *p_sys = p_demux->p_sys;
805
    bluray_overlay_t *ov = p_sys->p_overlays[plane];
806 807 808 809 810

    /*
     * If the overlay is already displayed, mark the picture as outdated.
     * We must NOT use vout_PutSubpicture if a picture is already displayed.
     */
811 812 813 814
    vlc_mutex_lock(&ov->lock);
    if (ov->status >= Displayed && p_sys->p_vout) {
        ov->status = Outdated;
        vlc_mutex_unlock(&ov->lock);
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
815
        return;
816
    }
817

818 819 820 821 822
    /*
     * Mark the overlay as available, but don't display it right now.
     * the blurayDemuxMenu will send it to vout, as it may be unavailable when
     * the overlay is computed
     */
823
    p_sys->current_overlay = plane;
824 825
    ov->status = ToDisplay;
    vlc_mutex_unlock(&ov->lock);
826 827
}

828
static void blurayInitOverlay(demux_t *p_demux, int plane, int width, int height)
829 830 831
{
    demux_sys_t *p_sys = p_demux->p_sys;

832
    assert(p_sys->p_overlays[plane] == NULL);
833

834 835
    p_sys->p_overlays[plane] = calloc(1, sizeof(**p_sys->p_overlays));
    if (unlikely(!p_sys->p_overlays[plane]))
836 837
        return;

838 839
    bluray_overlay_t *ov = p_sys->p_overlays[plane];

840 841
    subpicture_updater_sys_t *p_upd_sys = malloc(sizeof(*p_upd_sys));
    if (unlikely(!p_upd_sys)) {
842
        free(ov);
843
        p_sys->p_overlays[plane] = NULL;
844 845
        return;
    }
846
    /* two references: vout + demux */
847
    ov->released_once = ATOMIC_FLAG_INIT;
848

849
    p_upd_sys->p_overlay = ov;
850 851 852 853 854 855
    subpicture_updater_t updater = {
        .pf_validate = subpictureUpdaterValidate,
        .pf_update   = subpictureUpdaterUpdate,
        .pf_destroy  = subpictureUpdaterDestroy,
        .p_sys       = p_upd_sys,
    };
856 857 858 859 860 861
    vlc_mutex_init(&ov->lock);
    ov->p_pic = subpicture_New(&updater);
    ov->p_pic->i_original_picture_width = width;
    ov->p_pic->i_original_picture_height = height;
    ov->p_pic->b_ephemer = true;
    ov->p_pic->b_absolute = true;
862 863 864 865 866 867 868 869 870 871
}

/**
 * Destroy every regions in the subpicture.
 * This is done in two steps:
 * - Wiping our private regions list
 * - Flagging the overlay as outdated, so the changes are replicated from
 *   the subpicture_updater_t::pf_update
 * This doesn't destroy the subpicture, as the overlay may be used again by libbluray.
 */
872
static void blurayClearOverlay(demux_t *p_demux, int plane)
873 874
{
    demux_sys_t *p_sys = p_demux->p_sys;
875 876 877
    bluray_overlay_t *ov = p_sys->p_overlays[plane];

    vlc_mutex_lock(&ov->lock);
878

879 880 881
    subpicture_region_ChainDelete(ov->p_regions);
    ov->p_regions = NULL;
    ov->status = Outdated;
882

883
    vlc_mutex_unlock(&ov->lock);
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
}

/*
 * This will draw to the overlay by adding a region to our region list
 * This will have to be copied to the subpicture used to render the overlay.
 */
static void blurayDrawOverlay(demux_t *p_demux, const BD_OVERLAY* const ov)
{
    demux_sys_t *p_sys = p_demux->p_sys;

    /*
     * Compute a subpicture_region_t.
     * It will be copied and sent to the vout later.
     */
    if (!ov->img)
        return;

    vlc_mutex_lock(&p_sys->p_overlays[ov->plane]->lock);

    /* Find a region to update */
    subpicture_region_t *p_reg = p_sys->p_overlays[ov->plane]->p_regions;
    subpicture_region_t *p_last = NULL;
    while (p_reg != NULL) {
        p_last = p_reg;
        if (p_reg->i_x == ov->x && p_reg->i_y == ov->y &&
                p_reg->fmt.i_width == ov->w && p_reg->fmt.i_height == ov->h)
            break;
        p_reg = p_reg->p_next;
    }

    /* If there is no region to update, create a new one. */
    if (!p_reg) {
        video_format_t fmt;
        video_format_Init(&fmt, 0);
        video_format_Setup(&fmt, VLC_CODEC_YUVP, ov->w, ov->h, 1, 1);

        p_reg = subpicture_region_New(&fmt);
        p_reg->i_x = ov->x;
        p_reg->i_y = ov->y;
        /* Append it to our list. */
        if (p_last != NULL)
            p_last->p_next = p_reg;
        else /* If we don't have a last region, then our list empty */
            p_sys->p_overlays[ov->plane]->p_regions = p_reg;
    }

    /* Now we can update the region, regardless it's an update or an insert */
    const BD_PG_RLE_ELEM *img = ov->img;
Rafaël Carré's avatar
Rafaël Carré committed
932
    for (int y = 0; y < ov->h; y++)
933
        for (int x = 0; x < ov->w;) {
934 935
            plane_t *p = &p_reg->p_picture->p[0];
            memset(&p->p_pixels[y * p->i_pitch + x], img->color, img->len);
936 937 938
            x += img->len;
            img++;
        }
Rafaël Carré's avatar
Rafaël Carré committed
939

940 941 942 943 944 945 946 947 948
    if (ov->palette) {
        p_reg->fmt.p_palette->i_entries = 256;
        for (int i = 0; i < 256; ++i) {
            p_reg->fmt.p_palette->palette[i][0] = ov->palette[i].Y;
            p_reg->fmt.p_palette->palette[i][1] = ov->palette[i].Cb;
            p_reg->fmt.p_palette->palette[i][2] = ov->palette[i].Cr;
            p_reg->fmt.p_palette->palette[i][3] = ov->palette[i].T;
        }
    }
Rafaël Carré's avatar
Rafaël Carré committed
949

950 951 952 953 954 955 956 957 958
    vlc_mutex_unlock(&p_sys->p_overlays[ov->plane]->lock);
    /*
     * /!\ The region is now stored in our internal list, but not in the subpicture /!\
     */
}

static void blurayOverlayProc(void *ptr, const BD_OVERLAY *const overlay)
{
    demux_t *p_demux = (demux_t*)ptr;
959
    demux_sys_t *p_sys = p_demux->p_sys;
960 961

    if (!overlay) {
962 963 964 965 966
        msg_Info(p_demux, "Closing overlays.");
        p_sys->current_overlay = -1;
        if (p_sys->p_vout)
            for (int i = 0; i < MAX_OVERLAY; i++)
                blurayCloseOverlay(p_demux, i);
967 968
        return;
    }
Rafaël Carré's avatar
Rafaël Carré committed
969

970
    switch (overlay->cmd) {
Rafaël Carré's avatar
Rafaël Carré committed
971 972 973 974
    case BD_OVERLAY_INIT:
        msg_Info(p_demux, "Initializing overlay");
        blurayInitOverlay(p_demux, overlay->plane, overlay->w, overlay->h);
        break;
975 976 977 978
    case BD_OVERLAY_CLOSE:
        blurayClearOverlay(p_demux, overlay->plane);
        blurayCloseOverlay(p_demux, overlay->plane);
        break;
Rafaël Carré's avatar
Rafaël Carré committed
979 980 981 982 983 984 985 986 987 988 989 990
    case BD_OVERLAY_CLEAR:
        blurayClearOverlay(p_demux, overlay->plane);
        break;
    case BD_OVERLAY_FLUSH:
        blurayActivateOverlay(p_demux, overlay->plane);
        break;
    case BD_OVERLAY_DRAW:
        blurayDrawOverlay(p_demux, overlay);
        break;
    default:
        msg_Warn(p_demux,