]> git.sesse.net Git - vlc/blob - modules/access/dvdplay/access.c
99b4fd8ec5e8aa2021bbbeafe49d237387d654dd
[vlc] / modules / access / dvdplay / access.c
1 /*****************************************************************************
2  * access.c: access capabilities for dvdplay plugin.
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: access.c,v 1.16 2003/04/05 12:32:19 gbazin Exp $
6  *
7  * Author: Stéphane Borel <stef@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include "../../demux/mpeg/system.h"
34
35 #ifdef HAVE_UNISTD_H
36 #   include <unistd.h>
37 #endif
38
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <string.h>
42 #include <errno.h>
43
44 #ifdef STRNCASECMP_IN_STRINGS_H
45 #   include <strings.h>
46 #endif
47
48 #include "dvd.h"
49 #include "es.h"
50 #include "tools.h"
51 #include "intf.h"
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 /* called from outside */
57 static int    dvdplay_SetArea       ( input_thread_t *, input_area_t * );
58 static int    dvdplay_SetProgram    ( input_thread_t *, pgrm_descriptor_t * );
59 static int    dvdplay_Read          ( input_thread_t *, byte_t *, size_t );
60 static void   dvdplay_Seek          ( input_thread_t *, off_t );
61
62 static void   pf_vmg_callback       ( void*, dvdplay_event_t );
63
64 /* only from inside */
65 static int dvdNewArea( input_thread_t *, input_area_t * );
66 static int dvdNewPGC ( input_thread_t * );
67
68 /*****************************************************************************
69  * OpenDVD: open libdvdplay
70  *****************************************************************************/
71 int E_(OpenDVD) ( vlc_object_t *p_this )
72 {
73     input_thread_t *        p_input = (input_thread_t *)p_this;
74     char *                  psz_source;
75     dvd_data_t *            p_dvd;
76     input_area_t *          p_area;
77     unsigned int            i_title_nr;
78     unsigned int            i_title;
79     unsigned int            i_chapter;
80     unsigned int            i_angle;
81     unsigned int            i;
82
83     p_dvd = malloc( sizeof(dvd_data_t) );
84     if( p_dvd == NULL )
85     {
86         msg_Err( p_input, "out of memory" );
87         return -1;
88     }
89
90     p_input->p_access_data = (void *)p_dvd;
91
92     p_input->pf_read = dvdplay_Read;
93     p_input->pf_seek = dvdplay_Seek;
94     p_input->pf_set_area = dvdplay_SetArea;
95     p_input->pf_set_program = dvdplay_SetProgram;
96
97     /* command line */
98     if( ( psz_source = dvdplay_ParseCL( p_input,
99                         &i_title, &i_chapter, &i_angle ) ) == NULL )
100     {
101         free( p_dvd );
102         return -1;
103     }
104
105     /* Open libdvdplay */
106     p_dvd->vmg = dvdplay_open( psz_source, pf_vmg_callback, (void*)p_input );
107
108     if( p_dvd->vmg == NULL )
109     {
110         msg_Warn( p_input, "cannot open %s", psz_source );
111         free( psz_source );
112         free( p_dvd );
113         return -1;
114     }
115
116     /* free allocated strings */
117     free( psz_source );
118
119     p_dvd->p_intf = NULL;
120
121     p_dvd->i_still_time = 0;
122
123     /* set up input  */
124     p_input->i_mtu = 0;
125
126     /* Set stream and area data */
127     vlc_mutex_lock( &p_input->stream.stream_lock );
128
129     /* If we are here we can control the pace... */
130     p_input->stream.b_pace_control = 1;
131     /* seek is only allowed when we have size info */
132     p_input->stream.b_seekable = 0;
133
134     /* Initialize ES structures */
135     input_InitStream( p_input, sizeof( stream_ps_data_t ) );
136
137     /* disc input method */
138     p_input->stream.i_method = INPUT_METHOD_DVD;
139
140     i_title_nr = dvdplay_title_nr( p_dvd->vmg );
141 #define area p_input->stream.pp_areas
142
143     /* Area 0 for menu */
144     area[0]->i_plugin_data = 0;
145     input_DelArea( p_input, p_input->stream.pp_areas[0] );
146     input_AddArea( p_input, 0, 1 );
147
148     for( i = 1 ; i <= i_title_nr ; i++ )
149     {
150         input_AddArea( p_input, i, dvdplay_chapter_nr( p_dvd->vmg, i ) );
151         area[i]->i_plugin_data = 0;
152     }
153 #undef area
154     msg_Dbg( p_input, "number of titles: %d", i_title_nr );
155
156     i_title = i_title <= i_title_nr ? i_title : 0;
157
158     p_area = p_input->stream.pp_areas[i_title];
159     p_area->i_part = i_chapter;
160     p_input->stream.p_selected_area = NULL;
161
162     /* set title, chapter, audio and subpic */
163     if( dvdplay_SetArea( p_input, p_area ) )
164     {
165         vlc_mutex_unlock( &p_input->stream.stream_lock );
166         return -1;
167     }
168
169     if( i_angle <= p_input->stream.i_pgrm_number )
170     {
171         dvdplay_SetProgram( p_input,
172                             p_input->stream.pp_programs[i_angle - 1] );
173     }
174
175     vlc_mutex_unlock( &p_input->stream.stream_lock );
176
177     if( !p_input->psz_demux || !*p_input->psz_demux )
178     {
179         p_input->psz_demux = "dvdplay";
180     }
181
182     /* FIXME: we might lose variables here */
183     var_Create( p_input, "x-start", VLC_VAR_INTEGER );
184     var_Create( p_input, "y-start", VLC_VAR_INTEGER );
185     var_Create( p_input, "x-end", VLC_VAR_INTEGER );
186     var_Create( p_input, "y-end", VLC_VAR_INTEGER );
187
188     var_Create( p_input, "color", VLC_VAR_ADDRESS );
189     var_Create( p_input, "contrast", VLC_VAR_ADDRESS );
190
191     var_Create( p_input, "highlight", VLC_VAR_BOOL );
192     var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
193
194     return 0;
195 }
196
197 /*****************************************************************************
198  * CloseDVD: close libdvdplay
199  *****************************************************************************/
200 void E_(CloseDVD) ( vlc_object_t *p_this )
201 {
202     input_thread_t * p_input = (input_thread_t *)p_this;
203     dvd_data_t *     p_dvd = (dvd_data_t *)p_input->p_access_data;
204
205     var_Destroy( p_input, "highlight-mutex" );
206     var_Destroy( p_input, "highlight" );
207
208     var_Destroy( p_input, "x-start" );
209     var_Destroy( p_input, "x-end" );
210     var_Destroy( p_input, "y-start" );
211     var_Destroy( p_input, "y-end" );
212
213     var_Destroy( p_input, "color" );
214     var_Destroy( p_input, "contrast" );
215
216     /* close libdvdplay */
217     dvdplay_close( p_dvd->vmg );
218
219     free( p_dvd );
220     p_input->p_access_data = NULL;
221
222 }
223
224 /*****************************************************************************
225  * dvdplay_SetProgram: set dvd angle.
226  *****************************************************************************
227  * This is actually a hack to make angle change through vlc interface with
228  * no need for a specific button.
229  *****************************************************************************/
230 static int dvdplay_SetProgram( input_thread_t *     p_input,
231                                pgrm_descriptor_t *  p_program )
232 {
233     if( p_input->stream.p_selected_program != p_program )
234     {
235         dvd_data_t *    p_dvd;
236         int             i_angle;
237         vlc_value_t     val;
238
239         p_dvd = (dvd_data_t*)(p_input->p_access_data);
240         i_angle = p_program->i_number;
241
242         if( !dvdplay_angle( p_dvd->vmg, i_angle ) )
243         {
244             memcpy( p_program, p_input->stream.p_selected_program,
245                     sizeof(pgrm_descriptor_t) );
246             p_program->i_number = i_angle;
247             p_input->stream.p_selected_program = p_program;
248
249             msg_Dbg( p_input, "angle %d selected", i_angle );
250         }
251
252         /* Update the navigation variables without triggering a callback */
253         val.i_int = p_program->i_number;
254         var_Change( p_input, "program", VLC_VAR_SETVALUE, &val );
255     }
256
257     return 0;
258 }
259
260 /*****************************************************************************
261  * dvdplay_SetArea: initialize input data for title x, chapter y.
262  * It should be called for each user navigation request.
263  *****************************************************************************
264  * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
265  * Note that you have to take the lock before entering here.
266  *****************************************************************************/
267 static int dvdplay_SetArea( input_thread_t * p_input, input_area_t * p_area )
268 {
269     dvd_data_t *    p_dvd;
270     vlc_value_t     val;
271
272     p_dvd = (dvd_data_t*)p_input->p_access_data;
273
274     /*
275      * Title selection
276      */
277     if( p_area != p_input->stream.p_selected_area )
278     {
279         int i_chapter;
280
281         /* prevent intf to try to seek */
282         p_input->stream.b_seekable = 0;
283
284         /* Store selected chapter */
285         i_chapter = p_area->i_part;
286
287         dvdNewArea( p_input, p_area );
288
289         /* Reinit ES */
290         dvdNewPGC( p_input );
291
292         dvdplay_start( p_dvd->vmg, p_area->i_id );
293
294         p_area->i_part = i_chapter;
295
296     } /* i_title >= 0 */
297     else
298     {
299         p_area = p_input->stream.p_selected_area;
300     }
301
302     /*
303      * Chapter selection
304      */
305
306     if( (int)p_area->i_part != dvdplay_chapter_cur( p_dvd->vmg ) )
307     {
308         if( ( p_area->i_part > 0 ) &&
309             ( p_area->i_part <= p_area->i_part_nb ))
310         {
311             dvdplay_pg( p_dvd->vmg, p_area->i_part );
312         }
313         p_area->i_part = dvdplay_chapter_cur( p_dvd->vmg );
314     }
315
316     /* warn interface that something has changed */
317     p_area->i_tell =
318         LB2OFF( dvdplay_position( p_dvd->vmg ) ) - p_area->i_start;
319     p_input->stream.b_changed = 1;
320
321     /* Update the navigation variables without triggering a callback */
322     val.i_int = p_area->i_part;
323     var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val );
324
325     return 0;
326 }
327
328 /*****************************************************************************
329  * dvdplay_Read: reads data packets.
330  *****************************************************************************
331  * Returns -1 in case of error, the number of bytes read if everything went
332  * well.
333  *****************************************************************************/
334 static int dvdplay_Read( input_thread_t * p_input,
335                          byte_t * p_buffer, size_t i_count )
336 {
337     dvd_data_t *    p_dvd;
338     off_t           i_read;
339
340     p_dvd = (dvd_data_t *)p_input->p_access_data;
341
342     vlc_mutex_lock( &p_input->stream.stream_lock );
343     i_read = LB2OFF( dvdplay_read( p_dvd->vmg, p_buffer, OFF2LB( i_count ) ) );
344     vlc_mutex_unlock( &p_input->stream.stream_lock );
345
346     return i_read;
347 }
348
349 /*****************************************************************************
350  * dvdplay_Seek : Goes to a given position on the stream.
351  *****************************************************************************
352  * This one is used by the input and translate chronological position from
353  * input to logical position on the device.
354  * The lock should be taken before calling this function.
355  *****************************************************************************/
356 static void dvdplay_Seek( input_thread_t * p_input, off_t i_off )
357 {
358     dvd_data_t *     p_dvd;
359
360     p_dvd = (dvd_data_t *)p_input->p_access_data;
361
362     vlc_mutex_lock( &p_input->stream.stream_lock );
363
364     dvdplay_seek( p_dvd->vmg, OFF2LB( i_off ) );
365
366     p_input->stream.p_selected_area->i_tell  =
367         LB2OFF( dvdplay_position( p_dvd->vmg ) ) -
368         p_input->stream.p_selected_area->i_start;
369
370     vlc_mutex_unlock( &p_input->stream.stream_lock );
371
372     return;
373 }
374
375
376 /*****************************************************************************
377  * pf_vmg_callback: called by libdvdplay when some event happens
378  *****************************************************************************
379  * The stream lock has to be taken before entering here
380  *****************************************************************************/
381 static void pf_vmg_callback( void* p_args, dvdplay_event_t event )
382 {
383     input_thread_t *    p_input;
384     dvd_data_t *        p_dvd;
385     vlc_value_t         val;
386     unsigned int        i;
387
388     p_input = (input_thread_t*)p_args;
389     p_dvd   = (dvd_data_t*)p_input->p_access_data;
390
391     switch( event )
392     {
393     case NEW_DOMAIN:
394         break;
395     case NEW_VTS:
396         break;
397     case NEW_FILE:
398         break;
399     case NEW_PGC:
400         /* prevent intf to try to seek  by default */
401         p_input->stream.b_seekable = 0;
402
403         i = dvdplay_title_cur( p_dvd->vmg );
404         if( i != p_input->stream.p_selected_area->i_id )
405         {
406             /* the title number has changed: update area */
407             msg_Warn( p_input, "new title %d (%d)", i,
408                                p_input->stream.p_selected_area->i_id );
409             dvdNewArea( p_input,
410                         p_input->stream.pp_areas[i] );
411         }
412
413         /* new pgc in same title: reinit ES */
414         dvdNewPGC( p_input );
415
416         p_input->stream.b_changed = 1;
417
418         break;
419     case NEW_PG:
420         /* update current chapter */
421         p_input->stream.p_selected_area->i_part =
422             dvdplay_chapter_cur( p_dvd->vmg );
423
424         p_input->stream.p_selected_area->i_tell =
425             LB2OFF( dvdplay_position( p_dvd->vmg ) ) -
426             p_input->stream.p_selected_area->i_start;
427
428         /* warn interface that something has changed */
429         p_input->stream.b_changed = 1;
430
431         /* Update the navigation variables without triggering a callback */
432         val.i_int = p_input->stream.p_selected_area->i_part;
433         var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val );
434         break;
435     case NEW_CELL:
436         p_dvd->b_end_of_cell = 0;
437         break;
438     case END_OF_CELL:
439         p_dvd->b_end_of_cell = 1;
440         break;
441     case JUMP:
442         dvdplay_ES( p_input );
443         break;
444     case STILL_TIME:
445         /* we must pause only from demux
446          * when the data in cache has been decoded */
447         p_dvd->i_still_time = dvdplay_still_time( p_dvd->vmg );
448         msg_Dbg( p_input, "still time %d", p_dvd->i_still_time );
449         break;
450     case COMPLETE_VIDEO:
451         break;
452     case NEW_HIGHLIGHT:
453         if( var_Get( p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
454         {
455             vlc_mutex_t *p_mutex = val.p_address;
456             vlc_mutex_lock( p_mutex );
457
458             /* Retrieve the highlight from dvdplay */
459             dvdplay_highlight( p_dvd->vmg, &p_dvd->hli );
460
461             if( p_dvd->hli.i_x_start || p_dvd->hli.i_y_start ||
462                 p_dvd->hli.i_x_end || p_dvd->hli.i_y_end )
463             {
464                 /* Fill our internal variables with this data */
465                 val.i_int = p_dvd->hli.i_x_start;
466                 var_Set( p_input, "x-start", val );
467                 val.i_int = p_dvd->hli.i_y_start;
468                 var_Set( p_input, "y-start", val );
469                 val.i_int = p_dvd->hli.i_x_end;
470                 var_Set( p_input, "x-end", val );
471                 val.i_int = p_dvd->hli.i_y_end;
472                 var_Set( p_input, "y-end", val );
473
474                 val.p_address = (void *)p_dvd->hli.pi_color;
475                 var_Set( p_input, "color", val );
476                 val.p_address = (void *)p_dvd->hli.pi_contrast;
477                 var_Set( p_input, "contrast", val );
478
479                 /* Tell the SPU decoder that there's a new highlight */
480                 val.b_bool = VLC_TRUE;
481             }
482             else
483             {
484                 /* Turn off the highlight */
485                 val.b_bool = VLC_FALSE;
486             }
487             var_Set( p_input, "highlight", val );
488
489             vlc_mutex_unlock( p_mutex );
490         }
491         break;
492     default:
493         msg_Err( p_input, "unknown event from libdvdplay (%d)", event );
494     }
495
496     return;
497 }
498
499 static int dvdNewArea( input_thread_t * p_input, input_area_t * p_area )
500 {
501     dvd_data_t *    p_dvd;
502     int             i_angle_nb, i_angle;
503     vlc_value_t     val;
504     int             i;
505
506     p_dvd = (dvd_data_t*)p_input->p_access_data;
507
508     p_input->stream.p_selected_area = p_area;
509
510     /*
511      * One program for each angle
512      */
513     while( p_input->stream.i_pgrm_number )
514     {
515         input_DelProgram( p_input, p_input->stream.pp_programs[0] );
516     }
517
518     input_AddProgram( p_input, 1, sizeof( stream_ps_data_t ) );
519     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
520
521     dvdplay_angle_info( p_dvd->vmg, &i_angle_nb, &i_angle );
522     for( i = 1 ; i < i_angle_nb ; i++ )
523     {
524         input_AddProgram( p_input, i+1, 0 );
525     }
526
527     if( i_angle )
528         dvdplay_SetProgram( p_input,
529                             p_input->stream.pp_programs[i_angle-1] );
530     else
531         dvdplay_SetProgram( p_input,
532                             p_input->stream.pp_programs[0] );
533
534     /* No PSM to read in DVD mode, we already have all information */
535     p_input->stream.p_selected_program->b_is_ok = 1;
536
537     /* Update the navigation variables without triggering a callback */
538     val.i_int = p_area->i_id;
539     var_Change( p_input, "title", VLC_VAR_SETVALUE, &val );
540     var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL );
541     for( i = 1; (unsigned int)i <= p_area->i_part_nb; i++ )
542     {
543         val.i_int = i;
544         var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val );
545     }
546
547     /* Update the navigation variables without triggering a callback */
548     val.i_int = p_area->i_part;
549     var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val );
550
551     return 0;
552 }
553
554 static int dvdNewPGC( input_thread_t * p_input )
555 {
556     dvd_data_t *    p_dvd;
557 //    int             i_audio_nr  = -1;
558 //    int             i_audio     = -1;
559 //    int             i_subp_nr   = -1;
560 //    int             i_subp      = -1;
561 //    int             i_sec;
562
563     p_dvd = (dvd_data_t*)p_input->p_access_data;
564
565 //    dvdplay_audio_info( p_dvd->vmg, &i_audio_nr, &i_audio );
566 //    dvdplay_subp_info( p_dvd->vmg, &i_subp_nr, &i_subp );
567
568     dvdplay_ES( p_input );
569     p_input->stream.p_selected_area->i_start =
570         LB2OFF( dvdplay_title_first( p_dvd->vmg ) );
571     p_input->stream.p_selected_area->i_size  =
572         LB2OFF( dvdplay_title_end ( p_dvd->vmg ) ) -
573         p_input->stream.p_selected_area->i_start;
574     p_input->stream.p_selected_area->i_tell = 0;
575
576     if( p_input->stream.p_selected_area->i_size > 0 )
577     {
578         p_input->stream.b_seekable = 1;
579     }
580     else
581     {
582         p_input->stream.b_seekable = 0;
583     }
584
585 #if 0
586     i_sec = dvdplay_title_time( p_dvd->vmg );
587     msg_Dbg( p_input, "title time: %d:%02d:%02d (%d)",
588                      i_sec/3600, (i_sec%3600)/60, i_sec%60, i_sec );
589 #endif
590
591     return 0;
592 }