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 it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * 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 *****************************************************************************/
44 static int blurayOpen ( vlc_object_t * );
45 static void blurayClose( vlc_object_t * );
48 set_shortname( N_("BluRay") )
49 set_description( N_("Blu-Ray Disc support (libbluray)") )
51 set_category( CAT_INPUT )
52 set_subcategory( SUBCAT_INPUT_ACCESS )
53 set_capability( "access_demux", 200)
55 add_shortcut( "bluray", "file" )
57 set_callbacks( blurayOpen, blurayClose )
67 unsigned int i_longest_title;
68 input_title_t **pp_title;
74 /*****************************************************************************
76 *****************************************************************************/
77 static int blurayControl(demux_t *, int, va_list);
78 static int blurayDemux (demux_t *);
80 static int blurayInitTitles(demux_t *p_demux );
81 static int bluraySetTitle(demux_t *p_demux, int i_title);
83 #define FROM_TICKS(a) (a*CLOCK_FREQ / INT64_C(90000))
84 #define TO_TICKS(a) (a*INT64_C(90000)/CLOCK_FREQ)
85 #define CUR_LENGTH p_sys->pp_title[p_demux->info.i_title]->i_length
87 /*****************************************************************************
88 * blurayOpen: module init function
89 *****************************************************************************/
90 static int blurayOpen( vlc_object_t *object )
92 demux_t *p_demux = (demux_t*)object;
97 char bd_path[PATH_MAX] = { '\0' };
98 const char *error_msg = NULL;
100 if (strcmp(p_demux->psz_access, "bluray")) {
101 // TODO BDMV support, once we figure out what to do in libbluray
106 p_demux->p_sys = p_sys = malloc(sizeof(*p_sys));
107 if (unlikely(!p_sys)) {
110 p_sys->p_parser = NULL;
112 /* init demux info fields */
113 p_demux->info.i_update = 0;
114 p_demux->info.i_title = 0;
115 p_demux->info.i_seekpoint = 0;
117 TAB_INIT( p_sys->i_title, p_sys->pp_title );
119 /* store current bd_path */
120 if (p_demux->psz_file) {
121 strncpy(bd_path, p_demux->psz_file, sizeof(bd_path));
122 bd_path[PATH_MAX - 1] = '\0';
125 p_sys->bluray = bd_open(bd_path, NULL);
126 if (!p_sys->bluray) {
131 /* Warning the user about AACS/BD+ */
132 const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(p_sys->bluray);
133 msg_Info(p_demux, "First play: %i, Top menu: %i\n"
134 "HDMV Titles: %i, BD-J Titles: %i, Other: %i",
135 disc_info->first_play_supported, disc_info->top_menu_supported,
136 disc_info->num_hdmv_titles, disc_info->num_bdj_titles,
137 disc_info->num_unsupported_titles);
140 if (disc_info->aacs_detected) {
141 if (!disc_info->libaacs_detected) {
142 error_msg = _("This Blu-Ray Disc needs a library for AACS decoding, "
143 "and your system does not have it.");
146 if (!disc_info->aacs_handled) {
147 error_msg = _("Your system AACS decoding library does not work. "
154 if (disc_info->bdplus_detected) {
155 if (!disc_info->libbdplus_detected) {
156 error_msg = _("This Blu-Ray Disc needs a library for BD+ decoding, "
157 "and your system does not have it.");
160 if (!disc_info->bdplus_handled) {
161 error_msg = _("Your system BD+ decoding library does not work. "
162 "Missing configuration?");
167 /* Get titles and chapters */
168 if (blurayInitTitles(p_demux) != VLC_SUCCESS) {
172 /* get title request */
173 if ((pos_title = strrchr(bd_path, ':'))) {
174 /* found character ':' for title information */
175 *(pos_title++) = '\0';
176 i_title = atoi(pos_title);
179 /* set start title number */
180 if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS) {
181 msg_Err( p_demux, "Could not set the title %d", i_title );
185 p_sys->p_parser = stream_DemuxNew(p_demux, "ts", p_demux->out);
186 if (!p_sys->p_parser) {
187 msg_Err(p_demux, "Failed to create TS demuxer");
191 p_demux->pf_control = blurayControl;
192 p_demux->pf_demux = blurayDemux;
198 dialog_Fatal(p_demux, _("Blu-Ray error"), "%s", error_msg);
204 /*****************************************************************************
205 * blurayClose: module destroy function
206 *****************************************************************************/
207 static void blurayClose( vlc_object_t *object )
209 demux_t *p_demux = (demux_t*)object;
210 demux_sys_t *p_sys = p_demux->p_sys;
213 stream_Delete(p_sys->p_parser);
216 for (unsigned int i = 0; i < p_sys->i_title; i++)
217 vlc_input_title_Delete(p_sys->pp_title[i]);
218 TAB_CLEAN( p_sys->i_title, p_sys->pp_title );
220 /* bd_close( NULL ) can crash */
221 assert(p_sys->bluray);
222 bd_close(p_sys->bluray);
227 static int blurayInitTitles(demux_t *p_demux )
229 demux_sys_t *p_sys = p_demux->p_sys;
231 /* get and set the titles */
232 unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
233 int64_t duration = 0;
235 for (unsigned int i = 0; i < i_title; i++) {
236 input_title_t *t = vlc_input_title_New();
240 BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
243 t->i_length = FROM_TICKS(title_info->duration);
245 if (t->i_length > duration) {
246 duration = t->i_length;
247 p_sys->i_longest_title = i;
250 for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
251 seekpoint_t *s = vlc_seekpoint_New();
254 s->i_time_offset = title_info->chapters[j].offset;
256 TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
258 TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
259 bd_free_title_info(title_info);
265 /*****************************************************************************
266 * bluraySetTitle: select new BD title
267 *****************************************************************************/
268 static int bluraySetTitle(demux_t *p_demux, int i_title)
270 demux_sys_t *p_sys = p_demux->p_sys;
272 /* Looking for the main title, ie the longest duration */
274 i_title = p_sys->i_longest_title;
275 else if ((unsigned)i_title > p_sys->i_title)
278 msg_Dbg( p_demux, "Selecting Title %i", i_title);
280 /* Select Blu-Ray title */
281 if (bd_select_title(p_demux->p_sys->bluray, i_title) == 0 ) {
282 msg_Err(p_demux, "cannot select bd title '%d'", p_demux->info.i_title);
286 /* read title info and init some values */
287 p_demux->info.i_title = i_title;
288 p_demux->info.i_seekpoint = 0;
289 p_demux->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
295 /*****************************************************************************
296 * blurayControl: handle the controls
297 *****************************************************************************/
298 static int blurayControl(demux_t *p_demux, int query, va_list args)
300 demux_sys_t *p_sys = p_demux->p_sys;
306 case DEMUX_CAN_PAUSE:
307 case DEMUX_CAN_CONTROL_PACE:
308 pb_bool = (bool*)va_arg( args, bool * );
312 case DEMUX_GET_PTS_DELAY:
313 pi_64 = (int64_t*)va_arg( args, int64_t * );
315 INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
318 case DEMUX_SET_PAUSE_STATE:
322 case DEMUX_SET_TITLE:
324 int i_title = (int)va_arg( args, int );
325 if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
329 case DEMUX_SET_SEEKPOINT:
331 int i_chapter = (int)va_arg( args, int );
332 bd_seek_chapter( p_sys->bluray, i_chapter );
333 p_demux->info.i_update = INPUT_UPDATE_SEEKPOINT;
337 case DEMUX_GET_TITLE_INFO:
339 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
340 int *pi_int = (int*)va_arg( args, int* );
341 int *pi_title_offset = (int*)va_arg( args, int* );
342 int *pi_chapter_offset = (int*)va_arg( args, int* );
345 *pi_title_offset = 0;
346 *pi_chapter_offset = 0;
348 /* Duplicate local title infos */
349 *pi_int = p_sys->i_title;
350 *ppp_title = calloc( p_sys->i_title, sizeof(input_title_t **) );
351 for( unsigned int i = 0; i < p_sys->i_title; i++ )
352 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->pp_title[i]);
357 case DEMUX_GET_LENGTH:
359 int64_t *pi_length = (int64_t*)va_arg(args, int64_t *);
360 *pi_length = CUR_LENGTH;
365 int64_t i_time = (int64_t)va_arg(args, int64_t);
366 bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
371 int64_t *pi_time = (int64_t*)va_arg(args, int64_t *);
372 *pi_time = (int64_t)FROM_TICKS(bd_tell_time(p_sys->bluray));
376 case DEMUX_GET_POSITION:
378 double *pf_position = (double*)va_arg( args, double * );
379 *pf_position = (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH;
382 case DEMUX_SET_POSITION:
384 double f_position = (double)va_arg(args, double);
385 bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
391 struct meta_dl *meta = bd_get_meta(p_sys->bluray);
395 vlc_meta_t *p_meta = (vlc_meta_t *) va_arg (args, vlc_meta_t*);
397 if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
399 if (!EMPTY_STR(meta->language_code)) vlc_meta_AddExtra(p_meta, "Language", meta->language_code);
400 if (!EMPTY_STR(meta->filename)) vlc_meta_AddExtra(p_meta, "Filename", meta->filename);
401 if (!EMPTY_STR(meta->di_alternative)) vlc_meta_AddExtra(p_meta, "Alternative", meta->di_alternative);
403 // if (meta->di_set_number > 0) vlc_meta_SetTrackNum(p_meta, meta->di_set_number);
404 // if (meta->di_num_sets > 0) vlc_meta_AddExtra(p_meta, "Discs numbers in Set", meta->di_num_sets);
406 if (meta->thumb_count > 0 && meta->thumbnails) {
407 vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
413 case DEMUX_CAN_RECORD:
415 case DEMUX_SET_GROUP:
416 case DEMUX_HAS_UNSUPPORTED_META:
417 case DEMUX_GET_ATTACHMENTS:
420 msg_Warn( p_demux, "unimplemented query (%d) in control", query );
427 #define BD_TS_PACKET_SIZE (192)
428 #define NB_TS_PACKETS (200)
430 static int blurayDemux(demux_t *p_demux)
432 demux_sys_t *p_sys = p_demux->p_sys;
434 block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
439 int nread = bd_read(p_sys->bluray, p_block->p_buffer,
440 NB_TS_PACKETS * BD_TS_PACKET_SIZE);
442 block_Release(p_block);
446 p_block->i_buffer = nread;
448 stream_DemuxSend( p_sys->p_parser, p_block );