1 /*****************************************************************************
2 * bluray.c: Blu-ray disc support plugin
3 *****************************************************************************
4 * Copyright © 2010-2012 VideoLAN, VLC authors and libbluray AUTHORS
6 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
7 * Hugo Beauzée-Luyssen <hugo@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <limits.h> /* PATH_MAX */
31 #if defined (HAVE_MNTENT_H) && defined(HAVE_SYS_STAT_H)
33 # include <sys/stat.h>
37 # include <sys/stat.h>
38 # include <sys/param.h>
39 # include <sys/ucred.h>
40 # include <sys/mount.h>
43 #include <vlc_common.h>
44 #include <vlc_plugin.h>
45 #include <vlc_demux.h> /* demux_t */
46 #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 #include <vlc_iso_lang.h>
53 /* FIXME we should find a better way than including that */
54 #include "../../src/text/iso-639_def.h"
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>
63 /*****************************************************************************
65 *****************************************************************************/
67 #define BD_MENU_TEXT N_("Blu-ray menus")
68 #define BD_MENU_LONGTEXT N_("Use Blu-ray menus. If disabled, "\
69 "the movie will start directly")
70 #define BD_REGION_TEXT N_("Region code")
71 #define BD_REGION_LONGTEXT N_("Blu-Ray player region code. "\
72 "Some discs can be played only with a correct region code.")
74 static const char *const ppsz_region_code[] = {
76 static const char *const ppsz_region_code_text[] = {
77 "Region A", "Region B", "Region C" };
79 #define REGION_DEFAULT 1 /* Index to region list. Actual region code is (1<<REGION_DEFAULT) */
80 #define LANGUAGE_DEFAULT ("eng")
83 static int blurayOpen (vlc_object_t *);
84 static void blurayClose(vlc_object_t *);
87 set_shortname(N_("Blu-ray"))
88 set_description(N_("Blu-ray Disc support (libbluray)"))
90 set_category(CAT_INPUT)
91 set_subcategory(SUBCAT_INPUT_ACCESS)
92 set_capability("access_demux", 200)
93 add_bool("bluray-menu", false, BD_MENU_TEXT, BD_MENU_LONGTEXT, false)
94 add_string("bluray-region", ppsz_region_code[REGION_DEFAULT], BD_REGION_TEXT, BD_REGION_LONGTEXT, false)
95 change_string_list(ppsz_region_code, ppsz_region_code_text)
97 add_shortcut("bluray", "file")
99 set_callbacks(blurayOpen, blurayClose)
102 /* libbluray's overlay.h defines 2 types of overlay (bd_overlay_plane_e). */
103 #define MAX_OVERLAY 2
105 typedef enum OverlayStatus {
107 ToDisplay, //Used to mark the overlay to be displayed the first time.
109 Outdated //used to update the overlay after it has been sent to the vout
112 typedef struct bluray_overlay_t
114 atomic_flag released_once;
117 OverlayStatus status;
118 subpicture_region_t *p_regions;
126 unsigned int i_title;
127 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;
132 /* Meta information */
133 const META_DL *p_meta;
136 bluray_overlay_t *p_overlays[MAX_OVERLAY];
137 int current_overlay; // -1 if no current overlay;
140 bool b_popup_available;
141 mtime_t i_still_end_time;
144 input_thread_t *p_input;
145 vout_thread_t *p_vout;
150 int i_audio_stream; /* Selected audio stream. -1 if default */
151 int i_spu_stream; /* Selected subtitle stream. -1 if default */
155 /* Used to store bluray disc path */
159 struct subpicture_updater_sys_t
161 bluray_overlay_t *p_overlay;
165 * FIXME: partiallyy duplicated from src/input/es_out.c
167 static const char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
169 const iso639_lang_t *pl;
173 psz_lang = var_CreateGetString( p_demux, psz_var );
175 return LANGUAGE_DEFAULT;
177 /* XXX: we will use only the first value
178 * (and ignore other ones in case of a list) */
179 if( ( p = strchr( psz_lang, ',' ) ) )
182 for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
184 if( *psz_lang == '\0' )
186 if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
187 !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
188 !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
189 !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
195 if( pl->psz_eng_name != NULL )
196 return pl->psz_iso639_2T;
198 return LANGUAGE_DEFAULT;
201 /*****************************************************************************
203 *****************************************************************************/
204 static es_out_t *esOutNew(demux_t *p_demux);
206 static int blurayControl(demux_t *, int, va_list);
207 static int blurayDemux(demux_t *);
209 static void blurayInitTitles(demux_t *p_demux, int menu_titles);
210 static int bluraySetTitle(demux_t *p_demux, int i_title);
212 static void blurayOverlayProc(void *ptr, const BD_OVERLAY * const overlay);
213 static void blurayArgbOverlayProc(void *ptr, const BD_ARGB_OVERLAY * const overlay);
215 static int onMouseEvent(vlc_object_t *p_vout, const char *psz_var,
216 vlc_value_t old, vlc_value_t val, void *p_data);
218 static void blurayResetParser(demux_t *p_demux);
220 #define FROM_TICKS(a) (a*CLOCK_FREQ / INT64_C(90000))
221 #define TO_TICKS(a) (a*INT64_C(90000)/CLOCK_FREQ)
222 #define CUR_LENGTH p_sys->pp_title[p_demux->info.i_title]->i_length
225 static void FindMountPoint(char **file)
227 char *device = *file;
228 #if defined (HAVE_MNTENT_H) && defined (HAVE_SYS_STAT_H)
230 if (!stat (device, &st) && S_ISBLK (st.st_mode)) {
231 FILE *mtab = setmntent ("/proc/self/mounts", "r");
232 struct mntent *m, mbuf;
234 /* bd path may be a symlink (e.g. /dev/dvd -> /dev/sr0), so make
235 * sure we look up the real device */
236 char *bd_device = realpath(device, NULL);
238 bd_device = strdup(device);
240 while ((m = getmntent_r (mtab, &mbuf, buf, sizeof(buf))) != NULL) {
241 if (!strcmp (m->mnt_fsname, bd_device)) {
243 *file = strdup(m->mnt_dir);
250 #elif defined(__APPLE__)
252 if (!stat (device, &st) && S_ISBLK (st.st_mode)) {
253 int fs_count = getfsstat (NULL, 0, MNT_NOWAIT);
255 struct statfs mbuf[128];
256 getfsstat (mbuf, fs_count * sizeof(mbuf[0]), MNT_NOWAIT);
257 for (int i = 0; i < fs_count; ++i)
258 if (!strcmp (mbuf[i].f_mntfromname, device)) {
260 *file = strdup(mbuf[i].f_mntonname);
266 # warning Disc device to mount point not implemented
270 /*****************************************************************************
271 * blurayOpen: module init function
272 *****************************************************************************/
273 static int blurayOpen(vlc_object_t *object)
275 demux_t *p_demux = (demux_t*)object;
278 const char *error_msg = NULL;
279 #define BLURAY_ERROR(s) do { error_msg = s; goto error; } while(0)
281 if (strcmp(p_demux->psz_access, "bluray")) {
282 // TODO BDMV support, once we figure out what to do in libbluray
287 p_demux->p_sys = p_sys = calloc(1, sizeof(*p_sys));
288 if (unlikely(!p_sys))
291 p_sys->current_overlay = -1;
292 p_sys->i_audio_stream = -1;
293 p_sys->i_spu_stream = -1;
294 p_sys->i_video_stream = -1;
295 p_sys->i_still_end_time = 0;
297 /* init demux info fields */
298 p_demux->info.i_update = 0;
299 p_demux->info.i_title = 0;
300 p_demux->info.i_seekpoint = 0;
302 TAB_INIT(p_sys->i_title, p_sys->pp_title);
304 /* store current bd path */
305 if (p_demux->psz_file)
306 p_sys->psz_bd_path = strdup(p_demux->psz_file);
308 /* If we're passed a block device, try to convert it to the mount point. */
309 FindMountPoint(&p_sys->psz_bd_path);
311 p_sys->bluray = bd_open(p_sys->psz_bd_path, NULL);
312 if (!p_sys->bluray) {
313 free(p_sys->psz_bd_path);
318 /* Warning the user about AACS/BD+ */
319 const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
321 /* Is it a bluray? */
322 if (!disc_info->bluray_detected)
323 BLURAY_ERROR(_("Path doesn't appear to be a Blu-ray"));
325 msg_Info(p_demux, "First play: %i, Top menu: %i\n"
326 "HDMV Titles: %i, BD-J Titles: %i, Other: %i",
327 disc_info->first_play_supported, disc_info->top_menu_supported,
328 disc_info->num_hdmv_titles, disc_info->num_bdj_titles,
329 disc_info->num_unsupported_titles);
332 if (disc_info->aacs_detected) {
333 msg_Dbg(p_demux, "Disc is using AACS");
334 if (!disc_info->libaacs_detected)
335 BLURAY_ERROR(_("This Blu-ray Disc needs a library for AACS decoding"
336 ", and your system does not have it."));
337 if (!disc_info->aacs_handled) {
338 if (disc_info->aacs_error_code) {
339 switch (disc_info->aacs_error_code) {
340 case BD_AACS_CORRUPTED_DISC:
341 BLURAY_ERROR(_("Blu-ray Disc is corrupted."));
342 case BD_AACS_NO_CONFIG:
343 BLURAY_ERROR(_("Missing AACS configuration file!"));
345 BLURAY_ERROR(_("No valid processing key found in AACS config file."));
346 case BD_AACS_NO_CERT:
347 BLURAY_ERROR(_("No valid host certificate found in AACS config file."));
348 case BD_AACS_CERT_REVOKED:
349 BLURAY_ERROR(_("AACS Host certificate revoked."));
350 case BD_AACS_MMC_FAILED:
351 BLURAY_ERROR(_("AACS MMC failed."));
358 if (disc_info->bdplus_detected) {
359 msg_Dbg(p_demux, "Disc is using BD+");
360 if (!disc_info->libbdplus_detected)
361 BLURAY_ERROR(_("This Blu-ray Disc needs a library for BD+ decoding"
362 ", and your system does not have it."));
363 if (!disc_info->bdplus_handled)
364 BLURAY_ERROR(_("Your system BD+ decoding library does not work. "
365 "Missing configuration?"));
368 /* set player region code */
369 char *psz_region = var_InheritString(p_demux, "bluray-region");
370 unsigned int region = psz_region ? (psz_region[0] - 'A') : REGION_DEFAULT;
372 bd_set_player_setting(p_sys->bluray, BLURAY_PLAYER_SETTING_REGION_CODE, 1<<region);
374 /* set preferred languages */
375 const char *psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
376 bd_set_player_setting_str(p_sys->bluray, BLURAY_PLAYER_SETTING_AUDIO_LANG, psz_code);
377 psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
378 bd_set_player_setting_str(p_sys->bluray, BLURAY_PLAYER_SETTING_PG_LANG, psz_code);
379 psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
380 bd_set_player_setting_str(p_sys->bluray, BLURAY_PLAYER_SETTING_MENU_LANG, psz_code);
382 /* Get titles and chapters */
383 p_sys->p_meta = bd_get_meta(p_sys->bluray);
385 msg_Warn(p_demux, "Failed to get meta info.");
387 p_sys->b_menu = var_InheritBool(p_demux, "bluray-menu");
389 blurayInitTitles(p_demux, disc_info->num_hdmv_titles + disc_info->num_bdj_titles + 1/*Top Menu*/ + 1/*First Play*/);
392 * Initialize the event queue, so we can receive events in blurayDemux(Menu).
394 bd_get_event(p_sys->bluray, NULL);
396 /* Registering overlay event handler */
397 bd_register_overlay_proc(p_sys->bluray, p_demux, blurayOverlayProc);
398 p_sys->p_input = demux_GetParentInput(p_demux);
399 if (unlikely(!p_sys->p_input)) {
400 msg_Err(p_demux, "Could not get parent input");
406 /* Register ARGB overlay handler for BD-J */
407 if (disc_info->num_bdj_titles)
408 bd_register_argb_overlay_proc(p_sys->bluray, p_demux, blurayArgbOverlayProc, NULL);
410 /* libbluray will start playback from "First-Title" title */
411 if (bd_play(p_sys->bluray) == 0)
412 BLURAY_ERROR(_("Failed to start bluray playback. Please try without menu support."));
415 /* set start title number */
416 if (bluraySetTitle(p_demux, p_sys->i_longest_title) != VLC_SUCCESS) {
417 msg_Err(p_demux, "Could not set the title %d", p_sys->i_longest_title);
422 vlc_array_init(&p_sys->es);
423 p_sys->p_out = esOutNew(p_demux);
424 if (unlikely(p_sys->p_out == NULL))
427 blurayResetParser(p_demux);
428 if (!p_sys->p_parser) {
429 msg_Err(p_demux, "Failed to create TS demuxer");
433 p_demux->pf_control = blurayControl;
434 p_demux->pf_demux = blurayDemux;
440 dialog_Fatal(p_demux, _("Blu-ray error"), "%s", error_msg);
447 /*****************************************************************************
448 * blurayClose: module destroy function
449 *****************************************************************************/
450 static void blurayClose(vlc_object_t *object)
452 demux_t *p_demux = (demux_t*)object;
453 demux_sys_t *p_sys = p_demux->p_sys;
456 * Close libbluray first.
457 * This will close all the overlays before we release p_vout
458 * bd_close(NULL) can crash
460 assert(p_sys->bluray);
461 bd_close(p_sys->bluray);
463 if (p_sys->p_vout != NULL) {
464 var_DelCallback(p_sys->p_vout, "mouse-moved", onMouseEvent, p_demux);
465 var_DelCallback(p_sys->p_vout, "mouse-clicked", onMouseEvent, p_demux);
466 vlc_object_release(p_sys->p_vout);
468 if (p_sys->p_input != NULL)
469 vlc_object_release(p_sys->p_input);
471 stream_Delete(p_sys->p_parser);
472 if (p_sys->p_out != NULL)
473 es_out_Delete(p_sys->p_out);
474 assert(vlc_array_count(&p_sys->es) == 0);
475 vlc_array_clear(&p_sys->es);
478 for (unsigned int i = 0; i < p_sys->i_title; i++)
479 vlc_input_title_Delete(p_sys->pp_title[i]);
480 TAB_CLEAN(p_sys->i_title, p_sys->pp_title);
482 free(p_sys->psz_bd_path);
486 /*****************************************************************************
487 * Elementary streams handling
488 *****************************************************************************/
490 struct es_out_sys_t {
494 typedef struct fmt_es_pair {
499 static int findEsPairIndex(demux_sys_t *p_sys, int i_id)
501 for (int i = 0; i < vlc_array_count(&p_sys->es); ++i)
502 if (((fmt_es_pair_t*)vlc_array_item_at_index(&p_sys->es, i))->i_id == i_id)
508 static int findEsPairIndexByEs(demux_sys_t *p_sys, es_out_id_t *p_es)
510 for (int i = 0; i < vlc_array_count(&p_sys->es); ++i)
511 if (((fmt_es_pair_t*)vlc_array_item_at_index(&p_sys->es, i))->p_es == p_es)
517 static void setStreamLang(es_format_t *p_fmt,
518 const BLURAY_STREAM_INFO *p_streams, int i_stream_count)
520 for (int i = 0; i < i_stream_count; i++) {
521 if (p_fmt->i_id == p_streams[i].pid) {
522 free(p_fmt->psz_language);
523 p_fmt->psz_language = strndup((const char *)p_streams[i].lang, 3);
529 static es_out_id_t *esOutAdd(es_out_t *p_out, const es_format_t *p_fmt)
531 demux_sys_t *p_sys = p_out->p_sys->p_demux->p_sys;
532 BLURAY_TITLE_INFO *title_info = bd_get_playlist_info(p_sys->bluray, p_sys->i_playlist, 0);
533 BLURAY_CLIP_INFO *clip_info = NULL;
536 if (title_info && p_sys->i_current_clip < title_info->clip_count) {
537 clip_info = &title_info->clips[p_sys->i_current_clip];
540 es_format_Copy(&fmt, p_fmt);
543 if (p_sys->i_video_stream != -1 && p_sys->i_video_stream != p_fmt->i_id)
544 fmt.i_priority = ES_PRIORITY_NOT_SELECTABLE;
547 if (p_sys->i_audio_stream != -1 && p_sys->i_audio_stream != p_fmt->i_id)
548 fmt.i_priority = ES_PRIORITY_NOT_SELECTABLE;
550 setStreamLang(&fmt, clip_info->audio_streams, clip_info->audio_stream_count);
553 if (p_sys->i_spu_stream != -1 && p_sys->i_spu_stream != p_fmt->i_id)
554 fmt.i_priority = ES_PRIORITY_NOT_SELECTABLE;
556 setStreamLang(&fmt, clip_info->pg_streams, clip_info->pg_stream_count);
561 bd_free_title_info(title_info);
563 es_out_id_t *p_es = es_out_Add(p_out->p_sys->p_demux->out, &fmt);
564 if (p_fmt->i_id >= 0) {
565 /* Ensure we are not overriding anything */
566 int idx = findEsPairIndex(p_sys, p_fmt->i_id);
568 fmt_es_pair_t *p_pair = malloc(sizeof(*p_pair));
569 if (likely(p_pair != NULL)) {
570 p_pair->i_id = p_fmt->i_id;
572 msg_Info(p_out->p_sys->p_demux, "Adding ES %d", p_fmt->i_id);
573 vlc_array_append(&p_sys->es, p_pair);
577 es_format_Clean(&fmt);
581 static int esOutSend(es_out_t *p_out, es_out_id_t *p_es, block_t *p_block)
583 return es_out_Send(p_out->p_sys->p_demux->out, p_es, p_block);
586 static void esOutDel(es_out_t *p_out, es_out_id_t *p_es)
588 int idx = findEsPairIndexByEs(p_out->p_sys->p_demux->p_sys, p_es);
590 free(vlc_array_item_at_index(&p_out->p_sys->p_demux->p_sys->es, idx));
591 vlc_array_remove(&p_out->p_sys->p_demux->p_sys->es, idx);
593 es_out_Del(p_out->p_sys->p_demux->out, p_es);
596 static int esOutControl(es_out_t *p_out, int i_query, va_list args)
598 return es_out_vaControl(p_out->p_sys->p_demux->out, i_query, args);
601 static void esOutDestroy(es_out_t *p_out)
603 for (int i = 0; i < vlc_array_count(&p_out->p_sys->p_demux->p_sys->es); ++i)
604 free(vlc_array_item_at_index(&p_out->p_sys->p_demux->p_sys->es, i));
605 vlc_array_clear(&p_out->p_sys->p_demux->p_sys->es);
610 static es_out_t *esOutNew(demux_t *p_demux)
612 assert(vlc_array_count(&p_demux->p_sys->es) == 0);
613 es_out_t *p_out = malloc(sizeof(*p_out));
614 if (unlikely(p_out == NULL))
617 p_out->pf_add = esOutAdd;
618 p_out->pf_control = esOutControl;
619 p_out->pf_del = esOutDel;
620 p_out->pf_destroy = esOutDestroy;
621 p_out->pf_send = esOutSend;
623 p_out->p_sys = malloc(sizeof(*p_out->p_sys));
624 if (unlikely(p_out->p_sys == NULL)) {
628 p_out->p_sys->p_demux = p_demux;
632 /*****************************************************************************
633 * subpicture_updater_t functions:
634 *****************************************************************************/
635 static int subpictureUpdaterValidate(subpicture_t *p_subpic,
636 bool b_fmt_src, const video_format_t *p_fmt_src,
637 bool b_fmt_dst, const video_format_t *p_fmt_dst,
640 VLC_UNUSED(b_fmt_src);
641 VLC_UNUSED(b_fmt_dst);
642 VLC_UNUSED(p_fmt_src);
643 VLC_UNUSED(p_fmt_dst);
646 subpicture_updater_sys_t *p_upd_sys = p_subpic->updater.p_sys;
647 bluray_overlay_t *p_overlay = p_upd_sys->p_overlay;
649 vlc_mutex_lock(&p_overlay->lock);
650 int res = p_overlay->status == Outdated;
651 vlc_mutex_unlock(&p_overlay->lock);
655 /* This should probably be moved to subpictures.c afterward */
656 static subpicture_region_t* subpicture_region_Clone(subpicture_region_t *p_region_src)
660 subpicture_region_t *p_region_dst = subpicture_region_New(&p_region_src->fmt);
661 if (unlikely(!p_region_dst))
664 p_region_dst->i_x = p_region_src->i_x;
665 p_region_dst->i_y = p_region_src->i_y;
666 p_region_dst->i_align = p_region_src->i_align;
667 p_region_dst->i_alpha = p_region_src->i_alpha;
669 p_region_dst->psz_text = p_region_src->psz_text ? strdup(p_region_src->psz_text) : NULL;
670 p_region_dst->psz_html = p_region_src->psz_html ? strdup(p_region_src->psz_html) : NULL;
671 if (p_region_src->p_style != NULL) {
672 p_region_dst->p_style = malloc(sizeof(*p_region_dst->p_style));
673 p_region_dst->p_style = text_style_Copy(p_region_dst->p_style,
674 p_region_src->p_style);
677 //Palette is already copied by subpicture_region_New, we just have to duplicate p_pixels
678 for (int i = 0; i < p_region_src->p_picture->i_planes; i++)
679 memcpy(p_region_dst->p_picture->p[i].p_pixels,
680 p_region_src->p_picture->p[i].p_pixels,
681 p_region_src->p_picture->p[i].i_lines * p_region_src->p_picture->p[i].i_pitch);
685 static void subpictureUpdaterUpdate(subpicture_t *p_subpic,
686 const video_format_t *p_fmt_src,
687 const video_format_t *p_fmt_dst,
690 VLC_UNUSED(p_fmt_src);
691 VLC_UNUSED(p_fmt_dst);
693 subpicture_updater_sys_t *p_upd_sys = p_subpic->updater.p_sys;
694 bluray_overlay_t *p_overlay = p_upd_sys->p_overlay;
697 * When this function is called, all p_subpic regions are gone.
698 * We need to duplicate our regions (stored internaly) to this subpic.
700 vlc_mutex_lock(&p_overlay->lock);
702 subpicture_region_t *p_src = p_overlay->p_regions;
704 vlc_mutex_unlock(&p_overlay->lock);
708 subpicture_region_t **p_dst = &p_subpic->p_region;
709 while (p_src != NULL) {
710 *p_dst = subpicture_region_Clone(p_src);
713 p_dst = &(*p_dst)->p_next;
714 p_src = p_src->p_next;
717 (*p_dst)->p_next = NULL;
718 p_overlay->status = Displayed;
719 vlc_mutex_unlock(&p_overlay->lock);
722 static void blurayCleanOverlayStruct(bluray_overlay_t *);
724 static void subpictureUpdaterDestroy(subpicture_t *p_subpic)
726 blurayCleanOverlayStruct(p_subpic->updater.p_sys->p_overlay);
727 free(p_subpic->updater.p_sys);
730 /*****************************************************************************
732 *****************************************************************************/
733 static int onMouseEvent(vlc_object_t *p_vout, const char *psz_var, vlc_value_t old,
734 vlc_value_t val, void *p_data)
736 demux_t *p_demux = (demux_t*)p_data;
737 demux_sys_t *p_sys = p_demux->p_sys;
738 mtime_t now = mdate();
742 if (psz_var[6] == 'm') //Mouse moved
743 bd_mouse_select(p_sys->bluray, now, val.coords.x, val.coords.y);
744 else if (psz_var[6] == 'c') {
745 bd_mouse_select(p_sys->bluray, now, val.coords.x, val.coords.y);
746 bd_user_input(p_sys->bluray, now, BD_VK_MOUSE_ACTIVATE);
753 static int sendKeyEvent(demux_sys_t *p_sys, unsigned int key)
755 mtime_t now = mdate();
756 if (bd_user_input(p_sys->bluray, now, key) < 0)
762 /*****************************************************************************
763 * libbluray overlay handling:
764 *****************************************************************************/
765 static void blurayCleanOverlayStruct(bluray_overlay_t *p_overlay)
767 if (!atomic_flag_test_and_set(&p_overlay->released_once))
770 * This will be called when destroying the picture.
771 * Don't delete it again from here!
773 vlc_mutex_destroy(&p_overlay->lock);
774 subpicture_region_ChainDelete(p_overlay->p_regions);
778 static void blurayCloseOverlay(demux_t *p_demux, int plane)
780 demux_sys_t *p_sys = p_demux->p_sys;
781 bluray_overlay_t *ov = p_sys->p_overlays[plane];
785 vout_FlushSubpictureChannel(p_sys->p_vout, ov->p_pic->i_channel);
786 blurayCleanOverlayStruct(ov);
787 if (p_sys->current_overlay == plane)
788 p_sys->current_overlay = -1;
790 p_sys->p_overlays[plane] = NULL;
793 for (int i = 0; i < MAX_OVERLAY; i++)
794 if (p_sys->p_overlays[i])
797 /* All overlays have been closed */
798 if (p_sys->p_vout != NULL) {
799 var_DelCallback(p_sys->p_vout, "mouse-moved", onMouseEvent, p_demux);
800 var_DelCallback(p_sys->p_vout, "mouse-clicked", onMouseEvent, p_demux);
801 vlc_object_release(p_sys->p_vout);
802 p_sys->p_vout = NULL;
807 * Mark the overlay as "ToDisplay" status.
808 * This will not send the overlay to the vout instantly, as the vout
809 * may not be acquired (not acquirable) yet.
810 * If is has already been acquired, the overlay has already been sent to it,
811 * therefore, we only flag the overlay as "Outdated"
813 static void blurayActivateOverlay(demux_t *p_demux, int plane)
815 demux_sys_t *p_sys = p_demux->p_sys;
816 bluray_overlay_t *ov = p_sys->p_overlays[plane];
819 * If the overlay is already displayed, mark the picture as outdated.
820 * We must NOT use vout_PutSubpicture if a picture is already displayed.
822 vlc_mutex_lock(&ov->lock);
823 if (ov->status >= Displayed && p_sys->p_vout) {
824 ov->status = Outdated;
825 vlc_mutex_unlock(&ov->lock);
830 * Mark the overlay as available, but don't display it right now.
831 * the blurayDemuxMenu will send it to vout, as it may be unavailable when
832 * the overlay is computed
834 p_sys->current_overlay = plane;
835 ov->status = ToDisplay;
836 vlc_mutex_unlock(&ov->lock);
839 static void blurayInitOverlay(demux_t *p_demux, int plane, int width, int height)
841 demux_sys_t *p_sys = p_demux->p_sys;
843 assert(p_sys->p_overlays[plane] == NULL);
845 p_sys->p_overlays[plane] = calloc(1, sizeof(**p_sys->p_overlays));
846 if (unlikely(!p_sys->p_overlays[plane]))
849 bluray_overlay_t *ov = p_sys->p_overlays[plane];
851 subpicture_updater_sys_t *p_upd_sys = malloc(sizeof(*p_upd_sys));
852 if (unlikely(!p_upd_sys)) {
854 p_sys->p_overlays[plane] = NULL;
857 /* two references: vout + demux */
858 ov->released_once = ATOMIC_FLAG_INIT;
860 p_upd_sys->p_overlay = ov;
861 subpicture_updater_t updater = {
862 .pf_validate = subpictureUpdaterValidate,
863 .pf_update = subpictureUpdaterUpdate,
864 .pf_destroy = subpictureUpdaterDestroy,
867 vlc_mutex_init(&ov->lock);
868 ov->p_pic = subpicture_New(&updater);
869 ov->p_pic->i_original_picture_width = width;
870 ov->p_pic->i_original_picture_height = height;
871 ov->p_pic->b_ephemer = true;
872 ov->p_pic->b_absolute = true;
876 * Destroy every regions in the subpicture.
877 * This is done in two steps:
878 * - Wiping our private regions list
879 * - Flagging the overlay as outdated, so the changes are replicated from
880 * the subpicture_updater_t::pf_update
881 * This doesn't destroy the subpicture, as the overlay may be used again by libbluray.
883 static void blurayClearOverlay(demux_t *p_demux, int plane)
885 demux_sys_t *p_sys = p_demux->p_sys;
886 bluray_overlay_t *ov = p_sys->p_overlays[plane];
888 vlc_mutex_lock(&ov->lock);
890 subpicture_region_ChainDelete(ov->p_regions);
891 ov->p_regions = NULL;
892 ov->status = Outdated;
894 vlc_mutex_unlock(&ov->lock);
898 * This will draw to the overlay by adding a region to our region list
899 * This will have to be copied to the subpicture used to render the overlay.
901 static void blurayDrawOverlay(demux_t *p_demux, const BD_OVERLAY* const ov)
903 demux_sys_t *p_sys = p_demux->p_sys;
906 * Compute a subpicture_region_t.
907 * It will be copied and sent to the vout later.
912 vlc_mutex_lock(&p_sys->p_overlays[ov->plane]->lock);
914 /* Find a region to update */
915 subpicture_region_t *p_reg = p_sys->p_overlays[ov->plane]->p_regions;
916 subpicture_region_t *p_last = NULL;
917 while (p_reg != NULL) {
919 if (p_reg->i_x == ov->x && p_reg->i_y == ov->y &&
920 p_reg->fmt.i_width == ov->w && p_reg->fmt.i_height == ov->h)
922 p_reg = p_reg->p_next;
925 /* If there is no region to update, create a new one. */
928 video_format_Init(&fmt, 0);
929 video_format_Setup(&fmt, VLC_CODEC_YUVP, ov->w, ov->h, ov->w, ov->h, 1, 1);
931 p_reg = subpicture_region_New(&fmt);
934 /* Append it to our list. */
936 p_last->p_next = p_reg;
937 else /* If we don't have a last region, then our list empty */
938 p_sys->p_overlays[ov->plane]->p_regions = p_reg;
941 /* Now we can update the region, regardless it's an update or an insert */
942 const BD_PG_RLE_ELEM *img = ov->img;
943 for (int y = 0; y < ov->h; y++)
944 for (int x = 0; x < ov->w;) {
945 plane_t *p = &p_reg->p_picture->p[0];
946 memset(&p->p_pixels[y * p->i_pitch + x], img->color, img->len);
952 p_reg->fmt.p_palette->i_entries = 256;
953 for (int i = 0; i < 256; ++i) {
954 p_reg->fmt.p_palette->palette[i][0] = ov->palette[i].Y;
955 p_reg->fmt.p_palette->palette[i][1] = ov->palette[i].Cb;
956 p_reg->fmt.p_palette->palette[i][2] = ov->palette[i].Cr;
957 p_reg->fmt.p_palette->palette[i][3] = ov->palette[i].T;
961 vlc_mutex_unlock(&p_sys->p_overlays[ov->plane]->lock);
963 * /!\ The region is now stored in our internal list, but not in the subpicture /!\
967 static void blurayOverlayProc(void *ptr, const BD_OVERLAY *const overlay)
969 demux_t *p_demux = (demux_t*)ptr;
970 demux_sys_t *p_sys = p_demux->p_sys;
973 msg_Info(p_demux, "Closing overlays.");
974 p_sys->current_overlay = -1;
976 for (int i = 0; i < MAX_OVERLAY; i++)
977 blurayCloseOverlay(p_demux, i);
981 switch (overlay->cmd) {
982 case BD_OVERLAY_INIT:
983 msg_Info(p_demux, "Initializing overlay");
984 blurayInitOverlay(p_demux, overlay->plane, overlay->w, overlay->h);
986 case BD_OVERLAY_CLOSE:
987 blurayClearOverlay(p_demux, overlay->plane);
988 blurayCloseOverlay(p_demux, overlay->plane);
990 case BD_OVERLAY_CLEAR:
991 blurayClearOverlay(p_demux, overlay->plane);
993 case BD_OVERLAY_FLUSH:
994 blurayActivateOverlay(p_demux, overlay->plane);
996 case BD_OVERLAY_DRAW:
997 blurayDrawOverlay(p_demux, overlay);
1000 msg_Warn(p_demux, "Unknown BD overlay command: %u", overlay->cmd);
1006 * ARGB overlay (BD-J)
1008 static void blurayInitArgbOverlay(demux_t *p_demux, int plane, int width, int height)
1010 demux_sys_t *p_sys = p_demux->p_sys;
1012 blurayInitOverlay(p_demux, plane, width, height);
1014 if (!p_sys->p_overlays[plane]->p_regions) {
1016 video_format_Init(&fmt, 0);
1017 video_format_Setup(&fmt, VLC_CODEC_RGBA, width, height, width, height, 1, 1);
1019 p_sys->p_overlays[plane]->p_regions = subpicture_region_New(&fmt);
1023 static void blurayDrawArgbOverlay(demux_t *p_demux, const BD_ARGB_OVERLAY* const ov)
1025 demux_sys_t *p_sys = p_demux->p_sys;
1027 vlc_mutex_lock(&p_sys->p_overlays[ov->plane]->lock);
1029 /* Find a region to update */
1030 subpicture_region_t *p_reg = p_sys->p_overlays[ov->plane]->p_regions;
1032 vlc_mutex_unlock(&p_sys->p_overlays[ov->plane]->lock);
1036 /* Now we can update the region */
1037 const uint32_t *src0 = ov->argb;
1038 uint8_t *dst0 = p_reg->p_picture->p[0].p_pixels +
1039 p_reg->p_picture->p[0].i_pitch * ov->y +
1042 for (int y = 0; y < ov->h; y++) {
1043 // XXX: add support for this format ? Should be possible with OPENGL/VDPAU/...
1044 // - or add libbluray option to select the format ?
1045 for (int x = 0; x < ov->w; x++) {
1046 dst0[x*4 ] = src0[x]>>16; /* R */
1047 dst0[x*4+1] = src0[x]>>8; /* G */
1048 dst0[x*4+2] = src0[x]; /* B */
1049 dst0[x*4+3] = src0[x]>>24; /* A */
1053 dst0 += p_reg->p_picture->p[0].i_pitch;
1056 vlc_mutex_unlock(&p_sys->p_overlays[ov->plane]->lock);
1058 * /!\ The region is now stored in our internal list, but not in the subpicture /!\
1062 static void blurayArgbOverlayProc(void *ptr, const BD_ARGB_OVERLAY *const overlay)
1064 demux_t *p_demux = (demux_t*)ptr;
1066 switch (overlay->cmd) {
1067 case BD_ARGB_OVERLAY_INIT:
1068 blurayInitArgbOverlay(p_demux, overlay->plane, overlay->w, overlay->h);
1070 case BD_ARGB_OVERLAY_CLOSE:
1071 blurayClearOverlay(p_demux, overlay->plane);
1072 blurayCloseOverlay(p_demux, overlay->plane);
1074 case BD_ARGB_OVERLAY_FLUSH:
1075 blurayActivateOverlay(p_demux, overlay->plane);
1077 case BD_ARGB_OVERLAY_DRAW:
1078 blurayDrawArgbOverlay(p_demux, overlay);
1081 msg_Warn(p_demux, "Unknown BD ARGB overlay command: %u", overlay->cmd);
1086 static void bluraySendOverlayToVout(demux_t *p_demux)
1088 demux_sys_t *p_sys = p_demux->p_sys;
1090 assert(p_sys->current_overlay >= 0 &&
1091 p_sys->p_overlays[p_sys->current_overlay] != NULL &&
1092 p_sys->p_overlays[p_sys->current_overlay]->p_pic != NULL);
1094 p_sys->p_overlays[p_sys->current_overlay]->p_pic->i_start =
1095 p_sys->p_overlays[p_sys->current_overlay]->p_pic->i_stop = mdate();
1096 p_sys->p_overlays[p_sys->current_overlay]->p_pic->i_channel =
1097 vout_RegisterSubpictureChannel(p_sys->p_vout);
1099 * After this point, the picture should not be accessed from the demux thread,
1100 * as it is held by the vout thread.
1101 * This must be done only once per subpicture, ie. only once between each
1102 * blurayInitOverlay & blurayCloseOverlay call.
1104 vout_PutSubpicture(p_sys->p_vout, p_sys->p_overlays[p_sys->current_overlay]->p_pic);
1106 * Mark the picture as Outdated, as it contains no region for now.
1107 * This will make the subpicture_updater_t call pf_update
1109 p_sys->p_overlays[p_sys->current_overlay]->status = Outdated;
1112 static void blurayUpdateTitleInfo(demux_t *p_demux, input_title_t *t, int i_title_idx, int i_playlist)
1114 demux_sys_t *p_sys = p_demux->p_sys;
1115 BLURAY_TITLE_INFO *title_info = NULL;
1117 if (i_playlist >= 0)
1118 title_info = bd_get_playlist_info(p_sys->bluray, i_playlist, 0);
1119 else if (i_title_idx >= 0)
1120 title_info = bd_get_title_info(p_sys->bluray, i_title_idx, 0);
1125 t->i_length = FROM_TICKS(title_info->duration);
1127 if (!t->i_seekpoint) {
1128 for (unsigned int j = 0; j < title_info->chapter_count; j++) {
1129 seekpoint_t *s = vlc_seekpoint_New();
1133 s->i_time_offset = title_info->chapters[j].offset;
1135 TAB_APPEND(t->i_seekpoint, t->seekpoint, s);
1139 bd_free_title_info(title_info);
1142 static void blurayInitTitles(demux_t *p_demux, int menu_titles)
1144 demux_sys_t *p_sys = p_demux->p_sys;
1145 #if BLURAY_VERSION < BLURAY_VERSION_CODE(0,5,0)
1146 int64_t duration = 0;
1149 /* get and set the titles */
1150 unsigned i_title = menu_titles;
1152 if (!p_sys->b_menu) {
1153 i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
1154 #if BLURAY_VERSION >= BLURAY_VERSION_CODE(0,5,0)
1155 p_sys->i_longest_title = bd_get_main_title(p_sys->bluray);
1159 for (unsigned int i = 0; i < i_title; i++) {
1160 input_title_t *t = vlc_input_title_New();
1164 if (!p_sys->b_menu) {
1165 blurayUpdateTitleInfo(p_demux, t, i, -1);
1167 #if BLURAY_VERSION < BLURAY_VERSION_CODE(0,5,0)
1168 if (t->i_length > duration) {
1169 duration = t->i_length;
1170 p_sys->i_longest_title = i;
1173 } else if (i == 0) {
1174 t->psz_name = strdup(_("Top Menu"));
1175 } else if (i == i_title - 1) {
1176 t->psz_name = strdup(_("First Play"));
1179 TAB_APPEND(p_sys->i_title, p_sys->pp_title, t);
1183 static void blurayResetParser(demux_t *p_demux)
1186 * This is a hack and will have to be removed.
1187 * The parser should be flushed, and not destroy/created each time
1188 * we are changing title.
1190 demux_sys_t *p_sys = p_demux->p_sys;
1191 if (p_sys->p_parser)
1192 stream_Delete(p_sys->p_parser);
1194 p_sys->p_parser = stream_DemuxNew(p_demux, "ts", p_sys->p_out);
1196 if (!p_sys->p_parser)
1197 msg_Err(p_demux, "Failed to create TS demuxer");
1200 /*****************************************************************************
1201 * bluraySetTitle: select new BD title
1202 *****************************************************************************/
1203 static int bluraySetTitle(demux_t *p_demux, int i_title)
1205 demux_sys_t *p_sys = p_demux->p_sys;
1207 if (p_sys->b_menu) {
1209 msg_Dbg(p_demux, "Playing TopMenu Title");
1210 } else if (i_title >= (int)p_sys->i_title - 1) {
1211 msg_Dbg(p_demux, "Playing FirstPlay Title");
1212 i_title = BLURAY_TITLE_FIRST_PLAY;
1214 msg_Dbg(p_demux, "Playing Title %i", i_title);
1217 if (bd_play_title(p_demux->p_sys->bluray, i_title) == 0) {
1218 msg_Err(p_demux, "cannot play bd title '%d'", i_title);
1219 return VLC_EGENERIC;
1225 /* Looking for the main title, ie the longest duration */
1227 i_title = p_sys->i_longest_title;
1228 else if ((unsigned)i_title > p_sys->i_title)
1229 return VLC_EGENERIC;
1231 msg_Dbg(p_demux, "Selecting Title %i", i_title);
1233 if (bd_select_title(p_demux->p_sys->bluray, i_title) == 0) {
1234 msg_Err(p_demux, "cannot select bd title '%d'", i_title);
1235 return VLC_EGENERIC;
1238 blurayResetParser(p_demux);
1243 /*****************************************************************************
1244 * blurayControl: handle the controls
1245 *****************************************************************************/
1246 static int blurayControl(demux_t *p_demux, int query, va_list args)
1248 demux_sys_t *p_sys = p_demux->p_sys;
1253 case DEMUX_CAN_SEEK:
1254 case DEMUX_CAN_PAUSE:
1255 case DEMUX_CAN_CONTROL_PACE:
1256 pb_bool = (bool*)va_arg(args, bool *);
1260 case DEMUX_GET_PTS_DELAY:
1261 pi_64 = (int64_t*)va_arg(args, int64_t *);
1262 *pi_64 = INT64_C(1000) * var_InheritInteger(p_demux, "disc-caching");
1265 case DEMUX_SET_PAUSE_STATE:
1269 case DEMUX_SET_TITLE:
1271 int i_title = (int)va_arg(args, int);
1272 if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
1273 return VLC_EGENERIC;
1276 case DEMUX_SET_SEEKPOINT:
1278 int i_chapter = (int)va_arg(args, int);
1279 bd_seek_chapter(p_sys->bluray, i_chapter);
1280 p_demux->info.i_update = INPUT_UPDATE_SEEKPOINT;
1284 case DEMUX_GET_TITLE_INFO:
1286 input_title_t ***ppp_title = (input_title_t***)va_arg(args, input_title_t***);
1287 int *pi_int = (int*)va_arg(args, int*);
1288 int *pi_title_offset = (int*)va_arg(args, int*);
1289 int *pi_chapter_offset = (int*)va_arg(args, int*);
1292 *pi_title_offset = 0;
1293 *pi_chapter_offset = 0;
1295 /* Duplicate local title infos */
1296 *pi_int = p_sys->i_title;
1297 *ppp_title = malloc(p_sys->i_title * sizeof(input_title_t *));
1298 for (unsigned int i = 0; i < p_sys->i_title; i++)
1299 (*ppp_title)[i] = vlc_input_title_Duplicate(p_sys->pp_title[i]);
1304 case DEMUX_GET_LENGTH:
1306 int64_t *pi_length = (int64_t*)va_arg(args, int64_t *);
1307 *pi_length = p_demux->info.i_title < (int)p_sys->i_title ? CUR_LENGTH : 0;
1310 case DEMUX_SET_TIME:
1312 int64_t i_time = (int64_t)va_arg(args, int64_t);
1313 bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
1316 case DEMUX_GET_TIME:
1318 int64_t *pi_time = (int64_t*)va_arg(args, int64_t *);
1319 *pi_time = (int64_t)FROM_TICKS(bd_tell_time(p_sys->bluray));
1323 case DEMUX_GET_POSITION:
1325 double *pf_position = (double*)va_arg(args, double *);
1326 *pf_position = p_demux->info.i_title < (int)p_sys->i_title && CUR_LENGTH > 0 ?
1327 (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH : 0.0;
1330 case DEMUX_SET_POSITION:
1332 double f_position = (double)va_arg(args, double);
1333 bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
1337 case DEMUX_GET_META:
1339 vlc_meta_t *p_meta = (vlc_meta_t *) va_arg (args, vlc_meta_t*);
1340 const META_DL *meta = p_sys->p_meta;
1342 return VLC_EGENERIC;
1344 if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
1346 if (!EMPTY_STR(meta->language_code)) vlc_meta_AddExtra(p_meta, "Language", meta->language_code);
1347 if (!EMPTY_STR(meta->filename)) vlc_meta_AddExtra(p_meta, "Filename", meta->filename);
1348 if (!EMPTY_STR(meta->di_alternative)) vlc_meta_AddExtra(p_meta, "Alternative", meta->di_alternative);
1350 // if (meta->di_set_number > 0) vlc_meta_SetTrackNum(p_meta, meta->di_set_number);
1351 // if (meta->di_num_sets > 0) vlc_meta_AddExtra(p_meta, "Discs numbers in Set", meta->di_num_sets);
1353 if (meta->thumb_count > 0 && meta->thumbnails) {
1354 char *psz_thumbpath;
1355 if (asprintf(&psz_thumbpath, "%s" DIR_SEP "BDMV" DIR_SEP "META" DIR_SEP "DL" DIR_SEP "%s",
1356 p_sys->psz_bd_path, meta->thumbnails[0].path) > 0) {
1357 char *psz_thumburl = vlc_path2uri(psz_thumbpath, "file");
1358 if (unlikely(psz_thumburl == NULL)) {
1359 free(psz_thumbpath);
1363 vlc_meta_SetArtURL(p_meta, psz_thumburl);
1366 free(psz_thumbpath);
1372 case DEMUX_NAV_ACTIVATE:
1373 if (p_sys->b_popup_available && !p_sys->b_menu_open) {
1374 return sendKeyEvent(p_sys, BD_VK_POPUP);
1376 return sendKeyEvent(p_sys, BD_VK_ENTER);
1378 return sendKeyEvent(p_sys, BD_VK_UP);
1379 case DEMUX_NAV_DOWN:
1380 return sendKeyEvent(p_sys, BD_VK_DOWN);
1381 case DEMUX_NAV_LEFT:
1382 return sendKeyEvent(p_sys, BD_VK_LEFT);
1383 case DEMUX_NAV_RIGHT:
1384 return sendKeyEvent(p_sys, BD_VK_RIGHT);
1386 case DEMUX_CAN_RECORD:
1388 case DEMUX_SET_GROUP:
1389 case DEMUX_HAS_UNSUPPORTED_META:
1390 case DEMUX_GET_ATTACHMENTS:
1391 return VLC_EGENERIC;
1393 msg_Warn(p_demux, "unimplemented query (%d) in control", query);
1394 return VLC_EGENERIC;
1399 static void blurayResetStillImage( demux_t *p_demux )
1401 demux_sys_t *p_sys = p_demux->p_sys;
1403 if (p_sys->i_still_end_time) {
1404 p_sys->i_still_end_time = 0;
1406 blurayResetParser(p_demux);
1407 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1411 static void blurayStillImage( demux_t *p_demux, unsigned i_timeout )
1413 demux_sys_t *p_sys = p_demux->p_sys;
1415 /* time period elapsed ? */
1416 if (p_sys->i_still_end_time > 0 && p_sys->i_still_end_time <= mdate()) {
1417 msg_Dbg(p_demux, "Still image end");
1418 bd_read_skip_still(p_sys->bluray);
1420 blurayResetStillImage(p_demux);
1424 /* show last frame as still image */
1425 if (!p_sys->i_still_end_time) {
1427 msg_Dbg(p_demux, "Still image (%d seconds)", i_timeout);
1428 p_sys->i_still_end_time = mdate() + i_timeout * CLOCK_FREQ;
1430 msg_Dbg(p_demux, "Still image (infinite)");
1431 p_sys->i_still_end_time = -1;
1435 /* avoid busy loops (read returns no data) */
1439 static void blurayStreamSelect(demux_t *p_demux, uint32_t i_type, uint32_t i_id)
1441 demux_sys_t *p_sys = p_demux->p_sys;
1444 if (p_sys->i_playlist < 0)
1447 BLURAY_TITLE_INFO *title_info = bd_get_playlist_info(p_sys->bluray, p_sys->i_playlist, 0);
1448 if (title_info == NULL)
1451 if (p_sys->i_current_clip < title_info->clip_count) {
1452 BLURAY_CLIP_INFO *clip_info = &title_info->clips[p_sys->i_current_clip];
1454 /* The param we get is the real stream id, not an index, ie. it starts from 1 */
1456 if (i_type == BD_EVENT_AUDIO_STREAM) {
1457 if (i_id < clip_info->audio_stream_count) {
1458 i_pid = clip_info->audio_streams[i_id].pid;
1459 p_sys->i_audio_stream = i_pid;
1461 } else if (i_type == BD_EVENT_PG_TEXTST_STREAM) {
1462 if (i_id < clip_info->pg_stream_count) {
1463 i_pid = clip_info->pg_streams[i_id].pid;
1464 p_sys->i_spu_stream = i_pid;
1468 bd_free_title_info(title_info);
1471 int i_idx = findEsPairIndex(p_sys, i_pid);
1473 es_out_id_t *p_es = vlc_array_item_at_index(&p_sys->es, i_idx);
1474 es_out_Control(p_demux->out, ES_OUT_SET_ES, p_es);
1479 static void blurayUpdatePlaylist(demux_t *p_demux, unsigned i_playlist)
1481 blurayResetParser(p_demux);
1483 p_demux->p_sys->i_playlist = i_playlist;
1484 p_demux->p_sys->i_current_clip = 0;
1486 /* read title info and init some values */
1487 if (!p_demux->p_sys->b_menu)
1488 p_demux->info.i_title = bd_get_current_title(p_demux->p_sys->bluray);
1489 p_demux->info.i_seekpoint = 0;
1490 p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
1492 blurayUpdateTitleInfo(p_demux, p_demux->p_sys->pp_title[p_demux->info.i_title], -1, i_playlist);
1494 blurayResetStillImage(p_demux);
1497 static void blurayUpdateCurrentClip(demux_t *p_demux, uint32_t clip)
1501 demux_sys_t *p_sys = p_demux->p_sys;
1503 p_sys->i_current_clip = clip;
1504 BLURAY_TITLE_INFO *info = bd_get_playlist_info(p_sys->bluray, p_sys->i_playlist, 0);
1507 /* Let's assume a single video track for now.
1508 * This may brake later, but it's enough for now.
1510 assert(info->clips[p_sys->i_current_clip].video_stream_count >= 1);
1511 p_sys->i_video_stream = info->clips[p_sys->i_current_clip].video_streams[0].pid;
1512 bd_free_title_info(info);
1514 blurayResetStillImage(p_demux);
1517 static void blurayHandleEvent(demux_t *p_demux, const BD_EVENT *e)
1519 demux_sys_t *p_sys = p_demux->p_sys;
1522 case BD_EVENT_TITLE:
1523 if (e->param == BLURAY_TITLE_FIRST_PLAY)
1524 p_demux->info.i_title = p_sys->i_title - 1;
1526 p_demux->info.i_title = e->param;
1527 /* this is feature title, we don't know yet which playlist it will play (if any) */
1528 p_sys->i_playlist = -1;
1529 /* reset title infos here ? */
1530 p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT; /* might be BD-J title with no video */
1532 case BD_EVENT_PLAYLIST:
1533 /* Start of playlist playback (?????.mpls) */
1534 blurayUpdatePlaylist(p_demux, e->param);
1536 case BD_EVENT_PLAYITEM:
1537 blurayUpdateCurrentClip(p_demux, e->param);
1539 case BD_EVENT_CHAPTER:
1540 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1541 p_demux->info.i_seekpoint = e->param;
1543 case BD_EVENT_ANGLE:
1546 p_sys->b_menu_open = e->param;
1548 case BD_EVENT_POPUP:
1549 p_sys->b_popup_available = e->param;
1550 /* TODO: show / hide pop-up menu button in gui ? */
1554 * stream selection events
1556 case BD_EVENT_AUDIO_STREAM:
1557 case BD_EVENT_PG_TEXTST_STREAM:
1558 blurayStreamSelect(p_demux, e->event, e->param);
1560 case BD_EVENT_IG_STREAM:
1564 * playback control events
1566 case BD_EVENT_STILL_TIME:
1567 blurayStillImage(p_demux, e->param);
1569 case BD_EVENT_DISCONTINUITY:
1570 /* reset demuxer (partially decoded PES packets must be dropped) */
1571 blurayResetParser(p_demux);
1574 /* nothing to do (ex. BD-J is preparing menus, waiting user input or running animation) */
1575 /* avoid busy loop (bd_read() returns no data) */
1580 msg_Warn(p_demux, "event: %d param: %d", e->event, e->param);
1585 #define BD_TS_PACKET_SIZE (192)
1586 #define NB_TS_PACKETS (200)
1588 static int blurayDemux(demux_t *p_demux)
1590 demux_sys_t *p_sys = p_demux->p_sys;
1593 block_t *p_block = block_Alloc(NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
1599 if (p_sys->b_menu == false) {
1600 while (bd_get_event(p_demux->p_sys->bluray, &e))
1601 blurayHandleEvent(p_demux, &e);
1603 nread = bd_read(p_sys->bluray, p_block->p_buffer,
1604 NB_TS_PACKETS * BD_TS_PACKET_SIZE);
1606 nread = bd_read_ext(p_sys->bluray, p_block->p_buffer,
1607 NB_TS_PACKETS * BD_TS_PACKET_SIZE, &e);
1608 while (e.event != BD_EVENT_NONE) {
1609 blurayHandleEvent(p_demux, &e);
1610 bd_get_event(p_sys->bluray, &e);
1614 if (p_sys->current_overlay != -1) {
1615 bluray_overlay_t *ov = p_sys->p_overlays[p_sys->current_overlay];
1616 vlc_mutex_lock(&ov->lock);
1617 bool display = ov->status == ToDisplay;
1618 vlc_mutex_unlock(&ov->lock);
1620 if (p_sys->p_vout == NULL)
1621 p_sys->p_vout = input_GetVout(p_sys->p_input);
1622 if (p_sys->p_vout != NULL) {
1623 var_AddCallback(p_sys->p_vout, "mouse-moved", onMouseEvent, p_demux);
1624 var_AddCallback(p_sys->p_vout, "mouse-clicked", onMouseEvent, p_demux);
1625 bluraySendOverlayToVout(p_demux);
1631 block_Release(p_block);
1637 p_block->i_buffer = nread;
1639 stream_DemuxSend(p_sys->p_parser, p_block);