]> git.sesse.net Git - vlc/blob - modules/access/bluray.c
Bluray: changing the module type from access to access_demux
[vlc] / modules / access / bluray.c
1 /*****************************************************************************
2  * bluray.c: Blu-ray disc support plugin
3  *****************************************************************************
4  * Copyright © 2010-2011 VideoLAN, VLC authors and libbluray AUTHORS
5  *
6  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
27 #include <assert.h>
28 #include <limits.h>                         /* PATH_MAX */
29
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 */
35
36 #include <libbluray/bluray.h>
37 #include <libbluray/meta_data.h>
38
39 /*****************************************************************************
40  * Module descriptor
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." )
45
46 /* Callbacks */
47 static int  blurayOpen ( vlc_object_t * );
48 static void blurayClose( vlc_object_t * );
49
50 vlc_module_begin ()
51     set_shortname( N_("BluRay") )
52     set_description( N_("Blu-Ray Disc support (libbluray)") )
53
54     set_category( CAT_INPUT )
55     set_subcategory( SUBCAT_INPUT_ACCESS )
56     set_capability( "access_demux", 200)
57
58     add_integer( "bluray-caching", 1000,
59         CACHING_TEXT, CACHING_LONGTEXT, true )
60
61     add_shortcut( "bluray", "file" )
62
63     set_callbacks( blurayOpen, blurayClose )
64 vlc_module_end ()
65
66
67 struct demux_sys_t
68 {
69     BLURAY         *bluray;
70
71     /* Titles */
72     unsigned int    i_title;
73     unsigned int    i_longest_title;
74     input_title_t **pp_title;
75
76     /* caching */
77     int             i_bd_delay;
78
79     /* TS stream */
80     stream_t       *p_parser;
81 };
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static int     blurayControl(demux_t *, int, va_list);
87 static int     blurayDemux  (demux_t *);
88
89 static int     blurayInitTitles(demux_t *p_demux );
90 static int     bluraySetTitle(demux_t *p_demux, int i_title);
91
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
95
96 /*****************************************************************************
97  * blurayOpen: module init function
98  *****************************************************************************/
99 static int blurayOpen( vlc_object_t *object )
100 {
101     demux_t *p_demux = (demux_t*)object;
102     demux_sys_t *p_sys;
103
104     char *pos_title;
105     int i_title = -1;
106     char bd_path[PATH_MAX];
107
108     if (strcmp(p_demux->psz_access, "bluray")) {
109         // TODO BDMV support, once we figure out what to do in libbluray
110         return VLC_EGENERIC;
111     }
112
113     /* */
114     p_demux->p_sys = p_sys = malloc(sizeof(*p_sys));
115     if (unlikely(!p_sys)) {
116         return VLC_ENOMEM;
117     }
118     p_sys->p_parser = NULL;
119
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;
124
125     TAB_INIT( p_sys->i_title, p_sys->pp_title );
126
127     /* store current bd_path */
128     strncpy(bd_path, p_demux->psz_file, sizeof(bd_path));
129     bd_path[PATH_MAX - 1] = '\0';
130
131     p_sys->bluray = bd_open(bd_path, NULL);
132     if (!p_sys->bluray) {
133         free(p_sys);
134         return VLC_EGENERIC;
135     }
136
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);
144
145     /* AACS */
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."));
151             blurayClose(object);
152             return VLC_EGENERIC;
153         }
154         if (!disc_info->aacs_handled) {
155             dialog_Fatal (p_demux, _("Blu-Ray error"),
156                     _("Your system AACS decoding library does not work. "
157                       "Missing keys?"));
158             blurayClose(object);
159             return VLC_EGENERIC;
160         }
161     }
162
163     /* BD+ */
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."));
169             blurayClose(object);
170             return VLC_EGENERIC;
171         }
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?"));
176             blurayClose(object);
177             return VLC_EGENERIC;
178         }
179     }
180
181     /* Get titles and chapters */
182     if (blurayInitTitles(p_demux) != VLC_SUCCESS) {
183         blurayClose(object);
184         return VLC_EGENERIC;
185     }
186
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);
192     }
193
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 );
197         blurayClose(object);
198         return VLC_EGENERIC;
199     }
200
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");
205         blurayClose(object);
206         return VLC_EGENERIC;
207     }
208
209     p_demux->pf_control = blurayControl;
210     p_demux->pf_demux   = blurayDemux;
211
212     return VLC_SUCCESS;
213 }
214
215
216 /*****************************************************************************
217  * blurayClose: module destroy function
218  *****************************************************************************/
219 static void blurayClose( vlc_object_t *object )
220 {
221     demux_t *p_demux = (demux_t*)object;
222     demux_sys_t *p_sys = p_demux->p_sys;
223
224     if (p_sys->p_parser)
225         stream_Delete(p_sys->p_parser);
226
227     /* Titles */
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 );
231
232     /* bd_close( NULL ) can crash */
233     assert(p_sys->bluray);
234     bd_close(p_sys->bluray);
235     free(p_sys);
236 }
237
238
239 static int blurayInitTitles(demux_t *p_demux )
240 {
241     demux_sys_t *p_sys = p_demux->p_sys;
242
243     /* get and set the titles */
244     unsigned i_title = bd_get_titles(p_sys->bluray, TITLES_RELEVANT, 60);
245     int64_t duration = 0;
246
247     for (unsigned int i = 0; i < i_title; i++) {
248         input_title_t *t = vlc_input_title_New();
249         if (!t)
250             break;
251
252         BLURAY_TITLE_INFO *title_info = bd_get_title_info(p_sys->bluray, i, 0);
253         if (!title_info)
254             break;
255         t->i_length = FROM_TICKS(title_info->duration);
256
257         if (t->i_length > duration) {
258             duration = t->i_length;
259             p_sys->i_longest_title = i;
260         }
261
262         for ( unsigned int j = 0; j < title_info->chapter_count; j++) {
263             seekpoint_t *s = vlc_seekpoint_New();
264             if (!s)
265                 break;
266             s->i_time_offset = title_info->chapters[j].offset;
267
268             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
269         }
270         TAB_APPEND( p_sys->i_title, p_sys->pp_title, t );
271         bd_free_title_info(title_info);
272     }
273     return VLC_SUCCESS;
274 }
275
276
277 /*****************************************************************************
278  * bluraySetTitle: select new BD title
279  *****************************************************************************/
280 static int bluraySetTitle(demux_t *p_demux, int i_title)
281 {
282     demux_sys_t *p_sys = p_demux->p_sys;
283
284     /* Looking for the main title, ie the longest duration */
285     if (i_title < 0)
286         i_title = p_sys->i_longest_title;
287     else if ((unsigned)i_title > p_sys->i_title)
288         return VLC_EGENERIC;
289
290     msg_Dbg( p_demux, "Selecting Title %i", i_title);
291
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);
295         return VLC_EGENERIC;
296     }
297
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;
302
303     return VLC_SUCCESS;
304 }
305
306
307 /*****************************************************************************
308  * blurayControl: handle the controls
309  *****************************************************************************/
310 static int blurayControl(demux_t *p_demux, int query, va_list args)
311 {
312     demux_sys_t *p_sys = p_demux->p_sys;
313     bool     *pb_bool;
314     int64_t  *pi_64;
315
316     switch (query) {
317         case DEMUX_CAN_SEEK:
318         case DEMUX_CAN_PAUSE:
319         case DEMUX_CAN_CONTROL_PACE:
320              pb_bool = (bool*)va_arg( args, bool * );
321              *pb_bool = true;
322              break;
323
324         case DEMUX_GET_PTS_DELAY:
325             pi_64 = (int64_t*)va_arg( args, int64_t * );
326             *pi_64 = p_sys->i_bd_delay;
327             break;
328
329         case DEMUX_SET_PAUSE_STATE:
330             /* Nothing to do */
331             break;
332
333         case DEMUX_SET_TITLE:
334         {
335             int i_title = (int)va_arg( args, int );
336             if (bluraySetTitle(p_demux, i_title) != VLC_SUCCESS)
337                 return VLC_EGENERIC;
338             break;
339         }
340         case DEMUX_SET_SEEKPOINT:
341         {
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;
345             break;
346         }
347
348         case DEMUX_GET_TITLE_INFO:
349         {
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* );
354
355             /* */
356             *pi_title_offset   = 0;
357             *pi_chapter_offset = 0;
358
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]);
364
365             return VLC_SUCCESS;
366         }
367
368         case DEMUX_GET_LENGTH:
369         {
370             int64_t *pi_length = (int64_t*)va_arg(args, int64_t *);
371             *pi_length = CUR_LENGTH;
372             return VLC_SUCCESS;
373         }
374         case DEMUX_SET_TIME:
375         {
376             int64_t i_time = (int64_t)va_arg(args, int64_t);
377             bd_seek_time(p_sys->bluray, TO_TICKS(i_time));
378             return VLC_SUCCESS;
379         }
380         case DEMUX_GET_TIME:
381         {
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));
384             return VLC_SUCCESS;
385         }
386
387         case DEMUX_GET_POSITION:
388         {
389             double *pf_position = (double*)va_arg( args, double * );
390             *pf_position = (double)FROM_TICKS(bd_tell_time(p_sys->bluray))/CUR_LENGTH;
391             return VLC_SUCCESS;
392         }
393         case DEMUX_SET_POSITION:
394         {
395             double f_position = (double)va_arg(args, double);
396             bd_seek_time(p_sys->bluray, TO_TICKS(f_position*CUR_LENGTH));
397             return VLC_SUCCESS;
398         }
399
400         case DEMUX_GET_META:
401         {
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*);
404
405             if (!EMPTY_STR(meta->di_name)) vlc_meta_SetTitle(p_meta, meta->di_name);
406
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);
410
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);
413
414             if (meta->thumb_count > 0 && meta->thumbnails) {
415                 vlc_meta_SetArtURL(p_meta, meta->thumbnails[0].path);
416             }
417
418             return VLC_SUCCESS;
419         }
420
421         case DEMUX_CAN_RECORD:
422         case DEMUX_GET_FPS:
423         case DEMUX_SET_GROUP:
424         case DEMUX_HAS_UNSUPPORTED_META:
425         case DEMUX_GET_ATTACHMENTS:
426             return VLC_EGENERIC;
427         default:
428             msg_Warn( p_demux, "unimplemented query (%d) in control", query );
429             return VLC_EGENERIC;
430     }
431     return VLC_SUCCESS;
432 }
433
434
435 #define BD_TS_PACKET_SIZE (192)
436 #define NB_TS_PACKETS (200)
437
438 static int blurayDemux(demux_t *p_demux)
439 {
440     demux_sys_t *p_sys = p_demux->p_sys;
441
442     block_t *p_block = block_New(p_demux, NB_TS_PACKETS * (int64_t)BD_TS_PACKET_SIZE);
443     if (!p_block) {
444         return -1;
445     }
446
447     int nread = bd_read(p_sys->bluray, p_block->p_buffer,
448                         NB_TS_PACKETS * BD_TS_PACKET_SIZE);
449     if (nread < 0) {
450         block_Release(p_block);
451         return nread;
452     }
453
454     p_block->i_buffer = nread;
455
456     stream_DemuxSend( p_sys->p_parser, p_block );
457
458     return 1;
459 }