1 /*****************************************************************************
2 * bluray.c: Blu-ray disc support plugin
3 *****************************************************************************
4 * Copyright © 2010-2011 VideoLAN, VLC authors and libbluray AUTHORS
6 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
28 #include <limits.h> /* PATH_MAX */
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_demux.h> /* demux_t */
33 #include <vlc_input.h> /* Seekpoints, chapters */
34 #include <vlc_dialog.h> /* BD+/AACS warnings */
36 #include <libbluray/bluray.h>
37 #include <libbluray/meta_data.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 #define CACHING_TEXT N_("Caching value in ms")
43 #define CACHING_LONGTEXT N_( "Caching value for BDs. This "\
44 "value should be set in milliseconds." )
47 static int blurayOpen ( vlc_object_t * );
48 static void blurayClose( vlc_object_t * );
51 set_shortname( N_("BluRay") )
52 set_description( N_("Blu-Ray Disc support (libbluray)") )
54 set_category( CAT_INPUT )
55 set_subcategory( SUBCAT_INPUT_ACCESS )
56 set_capability( "access_demux", 200)
58 add_integer( "bluray-caching", 1000,
59 CACHING_TEXT, CACHING_LONGTEXT, true )
61 add_shortcut( "bluray", "file" )
63 set_callbacks( blurayOpen, blurayClose )
73 unsigned int i_longest_title;
74 input_title_t **pp_title;
83 /*****************************************************************************
85 *****************************************************************************/
86 static int blurayControl(demux_t *, int, va_list);
87 static int blurayDemux (demux_t *);
89 static int blurayInitTitles(demux_t *p_demux );
90 static int bluraySetTitle(demux_t *p_demux, int i_title);
92 #define FROM_TICKS(a) (a*CLOCK_FREQ / INT64_C(90000))
93 #define TO_TICKS(a) (a*INT64_C(90000)/CLOCK_FREQ)
94 #define CUR_LENGTH p_sys->pp_title[p_demux->info.i_title]->i_length
96 /*****************************************************************************
97 * blurayOpen: module init function
98 *****************************************************************************/
99 static int blurayOpen( vlc_object_t *object )
101 demux_t *p_demux = (demux_t*)object;
106 char bd_path[PATH_MAX];
108 if (strcmp(p_demux->psz_access, "bluray")) {
109 // TODO BDMV support, once we figure out what to do in libbluray
114 p_demux->p_sys = p_sys = malloc(sizeof(*p_sys));
115 if (unlikely(!p_sys)) {
118 p_sys->p_parser = NULL;
120 /* init demux info fields */
121 p_demux->info.i_update = 0;
122 p_demux->info.i_title = 0;
123 p_demux->info.i_seekpoint = 0;
125 TAB_INIT( p_sys->i_title, p_sys->pp_title );
127 /* store current bd_path */
128 strncpy(bd_path, p_demux->psz_file, sizeof(bd_path));
129 bd_path[PATH_MAX - 1] = '\0';
131 p_sys->bluray = bd_open(bd_path, NULL);
132 if (!p_sys->bluray) {
137 /* Warning the user about AACS/BD+ */
138 const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
139 msg_Info(p_demux, "First play: %i, Top menu: %i\n"
140 "HDMV Titles: %i, BD-J Titles: %i, Other: %i",
141 disc_info->first_play_supported, disc_info->top_menu_supported,
142 disc_info->num_hdmv_titles, disc_info->num_bdj_titles,
143 disc_info->num_unsupported_titles);
146 if (disc_info->aacs_detected) {
147 if (!disc_info->libaacs_detected) {
148 dialog_Fatal (p_demux, _("Blu-Ray error"),
149 _("This Blu-Ray Disc needs a library for AACS decoding, "
150 "and your system does not have it."));
154 if (!disc_info->aacs_handled) {
155 dialog_Fatal (p_demux, _("Blu-Ray error"),
156 _("Your system AACS decoding library does not work. "
164 if (disc_info->bdplus_detected) {
165 if (!disc_info->libbdplus_detected) {
166 dialog_Fatal (p_demux, _("Blu-Ray error"),
167 _("This Blu-Ray Disc needs a library for BD+ decoding, "
168 "and your system does not have it."));
172 if (!disc_info->bdplus_handled) {
173 dialog_Fatal (p_demux, _("Blu-Ray error"),
174 _("Your system BD+ decoding library does not work. "
175 "Missing configuration?"));
181 /* Get titles and chapters */
182 if (blurayInitTitles(p_demux) != VLC_SUCCESS) {
187 /* get title request */
188 if ((pos_title = strrchr(bd_path, ':'))) {
189 /* found character ':' for title information */
190 *(pos_title++) = '\0';
191 i_title = atoi(pos_title);
194 /* set start title number */
195 if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS) {
196 msg_Err( p_demux, "Could not set the title %d", i_title );
201 p_sys->i_bd_delay = var_InheritInteger(p_demux, "bluray-caching");
202 p_sys->p_parser = stream_DemuxNew(p_demux, "ts", p_demux->out);
203 if (!p_sys->p_parser) {
204 msg_Err(p_demux, "Failed to create TS demuxer");
209 p_demux->pf_control = blurayControl;
210 p_demux->pf_demux = blurayDemux;
216 /*****************************************************************************
217 * blurayClose: module destroy function
218 *****************************************************************************/
219 static void blurayClose( vlc_object_t *object )
221 demux_t *p_demux = (demux_t*)object;
222 demux_sys_t *p_sys = p_demux->p_sys;
225 stream_Delete(p_sys->p_parser);
228 for (unsigned int i = 0; i < p_sys->i_title; i++)
229 vlc_input_title_Delete(p_sys->pp_title[i]);
230 TAB_CLEAN( p_sys->i_title, p_sys->pp_title );
232 /* bd_close( NULL ) can crash */
233 assert(p_sys->bluray);
234 bd_close(p_sys->bluray);
239 static int blurayInitTitles(demux_t *p_demux )
241 demux_sys_t *p_sys = p_demux->p_sys;
243 /* get and set the titles */
244 unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
245 int64_t duration = 0;
247 for (unsigned int i = 0; i < i_title; i++) {
248 input_title_t *t = vlc_input_title_New();
252 BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
255 t->i_length = FROM_TICKS(title_info->duration);
257 if (t->i_length > duration) {
258 duration = t->i_length;
259 p_sys->i_longest_title = i;
262 for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
263 seekpoint_t *s = vlc_seekpoint_New();
266 s->i_time_offset = title_info->chapters[j].offset;
268 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
270 TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
271 bd_free_title_info(title_info);
277 /*****************************************************************************
278 * bluraySetTitle: select new BD title
279 *****************************************************************************/
280 static int bluraySetTitle(demux_t *p_demux, int i_title)
282 demux_sys_t *p_sys = p_demux->p_sys;
284 /* Looking for the main title, ie the longest duration */
286 i_title = p_sys->i_longest_title;
287 else if ((unsigned)i_title > p_sys->i_title)
290 msg_Dbg( p_demux, "Selecting Title %i", i_title);
292 /* Select Blu-Ray title */
293 if (bd_select_title(p_demux->p_sys->bluray, i_title) == 0 ) {
294 msg_Err(p_demux, "cannot select bd title '%d'", p_demux->info.i_title);
298 /* read title info and init some values */
299 p_demux->info.i_title = i_title;
300 p_demux->info.i_seekpoint = 0;
301 p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
307 /*****************************************************************************
308 * blurayControl: handle the controls
309 *****************************************************************************/
310 static int blurayControl(demux_t *p_demux, int query, va_list args)
312 demux_sys_t *p_sys = p_demux->p_sys;
318 case DEMUX_CAN_PAUSE:
319 case DEMUX_CAN_CONTROL_PACE:
320 pb_bool = (bool*)va_arg( args, bool * );
324 case DEMUX_GET_PTS_DELAY:
325 pi_64 = (int64_t*)va_arg( args, int64_t * );
326 *pi_64 = p_sys->i_bd_delay;
329 case DEMUX_SET_PAUSE_STATE:
333 case DEMUX_SET_TITLE:
335 int i_title = (int)va_arg( args, int );
336 if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
340 case DEMUX_SET_SEEKPOINT:
342 int i_chapter = (int)va_arg( args, int );
343 bd_seek_chapter( p_sys->bluray, i_chapter );
344 p_demux->info.i_update = INPUT_UPDATE_SEEKPOINT;
348 case DEMUX_GET_TITLE_INFO:
350 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
351 int *pi_int = (int*)va_arg( args, int* );
352 int *pi_title_offset = (int*)va_arg( args, int* );
353 int *pi_chapter_offset = (int*)va_arg( args, int* );
356 *pi_title_offset = 0;
357 *pi_chapter_offset = 0;
359 /* Duplicate local title infos */
360 *pi_int = p_sys->i_title;
361 *ppp_title = calloc( p_sys->i_title, sizeof(input_title_t **) );
362 for( unsigned int i = 0; i < p_sys->i_title; i++ )
363 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->pp_title[i]);
368 case DEMUX_GET_LENGTH:
370 int64_t *pi_length = (int64_t*)va_arg(args, int64_t *);
371 *pi_length = CUR_LENGTH;
376 int64_t i_time = (int64_t)va_arg(args, int64_t);
377 bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
382 int64_t *pi_time = (int64_t*)va_arg(args, int64_t *);
383 *pi_time = (int64_t)FROM_TICKS(bd_tell_time(p_sys->bluray));
387 case DEMUX_GET_POSITION:
389 double *pf_position = (double*)va_arg( args, double * );
390 *pf_position = (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH;
393 case DEMUX_SET_POSITION:
395 double f_position = (double)va_arg(args, double);
396 bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
402 struct meta_dl *meta = bd_get_meta(p_sys->bluray);
403 vlc_meta_t *p_meta = (vlc_meta_t *) va_arg (args, vlc_meta_t*);
405 if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
407 if (!EMPTY_STR(meta->language_code)) vlc_meta_AddExtra(p_meta, "Language", meta->language_code);
408 if (!EMPTY_STR(meta->filename)) vlc_meta_AddExtra(p_meta, "Filename", meta->filename);
409 if (!EMPTY_STR(meta->di_alternative)) vlc_meta_AddExtra(p_meta, "Alternative", meta->di_alternative);
411 // if (meta->di_set_number > 0) vlc_meta_SetTrackNum(p_meta, meta->di_set_number);
412 // if (meta->di_num_sets > 0) vlc_meta_AddExtra(p_meta, "Discs numbers in Set", meta->di_num_sets);
414 if (meta->thumb_count > 0 && meta->thumbnails) {
415 vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
421 case DEMUX_CAN_RECORD:
423 case DEMUX_SET_GROUP:
424 case DEMUX_HAS_UNSUPPORTED_META:
425 case DEMUX_GET_ATTACHMENTS:
428 msg_Warn( p_demux, "unimplemented query (%d) in control", query );
435 #define BD_TS_PACKET_SIZE (192)
436 #define NB_TS_PACKETS (200)
438 static int blurayDemux(demux_t *p_demux)
440 demux_sys_t *p_sys = p_demux->p_sys;
442 block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
447 int nread = bd_read(p_sys->bluray, p_block->p_buffer,
448 NB_TS_PACKETS * BD_TS_PACKET_SIZE);
450 block_Release(p_block);
454 p_block->i_buffer = nread;
456 stream_DemuxSend( p_sys->p_parser, p_block );