17 #define FIRST_AC3_AID 128
18 #define FIRST_DTS_AID 136
19 #define FIRST_MPG_AID 0
20 #define FIRST_PCM_AID 160
26 #include "stream_dvd.h"
27 #include "stream_dvd_common.h"
28 #include "libmpdemux/demuxer.h"
30 static char* dvd_device_current
;
33 #define LIBDVDREAD_VERSION(maj,min,micro) ((maj)*10000 + (min)*100 + (micro))
35 * Try to autodetect the libdvd-0.9.0 library
36 * (0.9.0 removed the <dvdread/dvd_udf.h> header, and moved the two defines
37 * DVD_VIDEO_LB_LEN and MAX_UDF_FILE_NAME_LEN from it to
38 * <dvdread/dvd_reader.h>)
40 #ifndef DVDREAD_VERSION
41 #if defined(DVD_VIDEO_LB_LEN) && defined(MAX_UDF_FILE_NAME_LEN)
42 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,9,0)
44 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,8,0)
49 static struct stream_priv_s
{
52 } stream_priv_dflts
= {
57 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
59 static const m_option_t stream_opts_fields
[] = {
60 { "hostname", ST_OFF(title
), CONF_TYPE_INT
, M_OPT_RANGE
, 1, 99, NULL
},
61 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0 ,0, NULL
},
62 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
64 static const struct m_struct_st stream_opts
= {
66 sizeof(struct stream_priv_s
),
71 int dvd_parse_chapter_range(const m_option_t
*conf
, const char *range
) {
75 return M_OPT_MISSING_PARAM
;
79 if(*range
&& isdigit(*range
)) {
80 dvd_chapter
= strtol(range
, &s
, 10);
82 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
89 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
96 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
99 dvd_last_chapter
= strtol(s
, &t
, 10);
101 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
102 return M_OPT_INVALID
;
107 int dvd_chapter_from_cell(dvd_priv_t
* dvd
,int title
,int cell
)
113 if(title
< 0 || cell
< 0){
116 /* for most DVD's chapter == cell */
117 /* but there are more complecated cases... */
118 if(chapter
>= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
) {
119 chapter
= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
-1;
121 title
= dvd
->tt_srpt
->title
[title
].vts_ttn
-1;
122 ptt
= dvd
->vts_file
->vts_ptt_srpt
->title
[title
].ptt
;
123 while(chapter
>= 0) {
124 pgc_id
= ptt
[chapter
].pgcn
;
125 pgn
= ptt
[chapter
].pgn
;
126 cur_pgc
= dvd
->vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
127 if(cell
>= cur_pgc
->program_map
[pgn
-1]-1) {
132 /* didn't find a chapter ??? */
136 int dvd_lang_from_aid(stream_t
*stream
, int id
) {
139 if (!stream
) return 0;
142 for(i
=0;i
<d
->nr_of_channels
;i
++) {
143 if(d
->audio_streams
[i
].id
==id
)
144 return d
->audio_streams
[i
].language
;
149 int dvd_aid_from_lang(stream_t
*stream
, unsigned char* lang
) {
150 dvd_priv_t
*d
=stream
->priv
;
153 while(strlen(lang
)>=2) {
154 code
=lang
[1]|(lang
[0]<<8);
155 for(i
=0;i
<d
->nr_of_channels
;i
++) {
156 if(d
->audio_streams
[i
].language
==code
) {
157 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Selected DVD audio channel: %d language: %c%c\n",
158 d
->audio_streams
[i
].id
, lang
[0],lang
[1]);
159 return d
->audio_streams
[i
].id
;
161 //printf("%X != %X (%c%c)\n",code,d->audio_streams[i].language,lang[0],lang[1]);
163 lang
+=2; while (lang
[0]==',' || lang
[0]==' ') ++lang
;
165 mp_tmsg(MSGT_OPEN
,MSGL_WARN
,"No matching DVD audio language found!\n");
170 int dvd_number_of_subs(stream_t
*stream
) {
174 if (!stream
) return -1;
177 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
178 if (d
->subtitles
[i
].id
> maxid
) maxid
= d
->subtitles
[i
].id
;
182 int dvd_lang_from_sid(stream_t
*stream
, int id
) {
185 if (!stream
) return 0;
188 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
189 if (d
->subtitles
[i
].id
== id
&& d
->subtitles
[i
].language
) return d
->subtitles
[i
].language
;
193 int dvd_sid_from_lang(stream_t
*stream
, unsigned char* lang
) {
194 dvd_priv_t
*d
=stream
->priv
;
196 while(lang
&& strlen(lang
)>=2) {
197 code
=lang
[1]|(lang
[0]<<8);
198 for(i
=0;i
<d
->nr_of_subtitles
;i
++) {
199 if(d
->subtitles
[i
].language
==code
) {
200 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Selected DVD subtitle channel: %d language: %c%c\n", i
, lang
[0],lang
[1]);
201 return d
->subtitles
[i
].id
;
205 while (lang
[0]==',' || lang
[0]==' ') ++lang
;
207 mp_tmsg(MSGT_OPEN
,MSGL_WARN
,"No matching DVD subtitle language found!\n");
211 static int dvd_next_cell(dvd_priv_t
*d
) {
212 int next_cell
=d
->cur_cell
;
214 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next1=0x%X \n",next_cell
);
215 if( d
->cur_pgc
->cell_playback
[ next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
216 while(next_cell
<d
->last_cell
) {
217 if( d
->cur_pgc
->cell_playback
[next_cell
].block_mode
== BLOCK_MODE_LAST_CELL
)
222 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next2=0x%X \n",next_cell
);
225 if(next_cell
>=d
->last_cell
)
227 if(d
->cur_pgc
->cell_playback
[next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
228 next_cell
+=dvd_angle
;
229 if(next_cell
>=d
->last_cell
)
232 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next3=0x%X \n",next_cell
);
236 static int dvd_read_sector(dvd_priv_t
*d
,unsigned char* data
) {
239 if(d
->packs_left
==0) {
241 * If we're not at the end of this cell, we can determine the next
242 * VOBU to display using the VOBU_SRI information section of the
243 * DSI. Using this value correctly follows the current angle,
244 * avoiding the doubled scenes in The Matrix, and makes our life
247 * Otherwise, we set our next address past the end of this cell to
248 * force the code above to go to the next cell in the program.
250 if(d
->dsi_pack
.vobu_sri
.next_vobu
!= SRI_END_OF_CELL
) {
251 d
->cur_pack
= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+ ( d
->dsi_pack
.vobu_sri
.next_vobu
& 0x7fffffff );
252 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Navi new pos=0x%X \n",d
->cur_pack
);
254 // end of cell! find next cell!
255 mp_msg(MSGT_DVD
,MSGL_V
, "--- END OF CELL !!! ---\n");
256 d
->cur_pack
=d
->cell_last_pack
+1;
261 if(d
->cur_pack
>d
->cell_last_pack
) {
263 int next
=dvd_next_cell(d
);
266 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
267 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
268 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
269 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
270 mp_msg(MSGT_DVD
,MSGL_V
, "DVD next cell: %d pack: 0x%X-0x%X \n",d
->cur_cell
,d
->cur_pack
,d
->cell_last_pack
);
275 len
= DVDReadBlocks(d
->title
, d
->cur_pack
, 1, data
);
276 // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss
277 if(len
<= 0) return -1; //error
279 if(data
[38]==0 && data
[39]==0 && data
[40]==1 && data
[41]==0xBF &&
280 data
[1024]==0 && data
[1025]==0 && data
[1026]==1 && data
[1027]==0xBF) {
281 // found a Navi packet!!!
282 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
283 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]));
285 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]), sizeof(dsi_t
));
287 if(d
->cur_pack
!= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
) {
288 mp_msg(MSGT_DVD
,MSGL_V
, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
289 d
->cur_pack
,d
->dsi_pack
.dsi_gi
.nv_pck_lbn
);
292 d
->packs_left
= d
->dsi_pack
.dsi_gi
.vobu_ea
;
293 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Found NAVI packet! lba=0x%X len=%d \n",d
->cur_pack
,d
->packs_left
);
294 //navPrint_DSI(&d->dsi_pack);
295 mp_msg(MSGT_DVD
,MSGL_DBG3
,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d
->cur_cell
,
296 d
->dsi_pack
.dsi_gi
.vobu_c_idn
,d
->dsi_pack
.dsi_gi
.vobu_vob_idn
,
297 d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
,
298 d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
);
302 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
303 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
304 // it generates incorrect code for unaligned access to a packed
305 // structure member, resulting in an mplayer crash with a SIGBUS
308 // See also gcc problem report PR c/7847:
309 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
310 for(i
=0;i
<9;i
++) { // check if all values zero:
311 __typeof__(d
->dsi_pack
.sml_agli
.data
[i
].address
) tmp_addr
;
312 memcpy(&tmp_addr
,&d
->dsi_pack
.sml_agli
.data
[i
].address
,sizeof(tmp_addr
));
313 if((skip
=tmp_addr
)!=0) break;
316 for(i
=0;i
<9;i
++) // check if all values zero:
317 if((skip
=d
->dsi_pack
.sml_agli
.data
[i
].address
)!=0) break;
319 if(skip
&& skip
!=0x7fffffff) {
320 // sml_agli table has valid data (at least one non-zero):
321 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
322 d
->dsi_pack
.sml_agli
.data
[dvd_angle
].address
;
325 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d
->cur_pack
);
327 // check if we're in the right cell, jump otherwise:
328 if( (d
->dsi_pack
.dsi_gi
.vobu_c_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
) &&
329 (d
->dsi_pack
.dsi_gi
.vobu_vob_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
) ){
331 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced by cell/vob IDN search! \n");
333 // wrong angle, skip this vobu:
334 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
335 d
->dsi_pack
.dsi_gi
.vobu_ea
;
336 d
->angle_seek
=2; // DEBUG
346 if(d
->packs_left
>=0) --d
->packs_left
;
349 if(d
->angle_seek
==2) mp_msg(MSGT_DVD
,MSGL_V
, "!!! warning! reading packet while angle_seek !!!\n");
350 goto read_next
; // searching for Navi packet
353 return d
->cur_pack
-1;
356 static void dvd_seek(dvd_priv_t
*d
,int pos
) {
360 // check if we stay in current cell (speedup things, and avoid angle skip)
361 if(d
->cur_pack
>d
->cell_last_pack
||
362 d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
364 // ok, cell change, find the right cell!
366 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
367 d
->cur_cell
+=dvd_angle
;
371 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
372 if(d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
373 d
->cur_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
376 if(d
->cur_pack
<=d
->cell_last_pack
) break; // ok, we find it! :)
377 next
=dvd_next_cell(d
);
379 //d->cur_pack=d->cell_last_pack+1;
380 break; // we're after the last cell
386 mp_msg(MSGT_DVD
,MSGL_V
, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
387 d
->cur_pack
,d
->cur_cell
,d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
,d
->cell_last_pack
);
389 // if we're in interleaved multi-angle cell, find the right angle chain!
390 // (read Navi block, and use the seamless angle jump table)
394 static void dvd_close(dvd_priv_t
*d
) {
395 ifoClose(d
->vts_file
);
396 ifoClose(d
->vmg_file
);
397 DVDCloseFile(d
->title
);
400 dvd_last_chapter
= 0;
401 dvd_set_speed(dvd_device_current
, -1); /* -1 => restore default */
404 static int fill_buffer(stream_t
*s
, char *but
, int len
)
406 if(s
->type
== STREAMTYPE_DVD
) {
407 off_t pos
=dvd_read_sector(s
->priv
,s
->buffer
);
409 len
=2048; // full sector
411 } else len
=-1; // error
416 static int seek(stream_t
*s
, off_t newpos
) {
417 s
->pos
=newpos
; // real seek
418 dvd_seek(s
->priv
,s
->pos
/2048);
422 static void stream_dvd_close(stream_t
*s
) {
426 static int mp_get_titleset_length(ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
)
428 int vts_ttn
; ///< title number within video title set
429 int pgc_no
; ///< program chain number
430 int msec
; ///< time length in milliseconds
433 if(!vts_file
|| !tt_srpt
)
436 if(vts_file
->vtsi_mat
&& vts_file
->vts_pgcit
)
438 vts_ttn
= tt_srpt
->title
[title_no
].vts_ttn
- 1;
439 pgc_no
= vts_file
->vts_ptt_srpt
->title
[vts_ttn
].ptt
[0].pgcn
- 1;
440 msec
= mp_dvdtimetomsec(&vts_file
->vts_pgcit
->pgci_srp
[pgc_no
].pgc
->playback_time
);
446 static int mp_describe_titleset(dvd_reader_t
*dvd
, tt_srpt_t
*tt_srpt
, int vts_no
)
448 ifo_handle_t
*vts_file
;
449 int title_no
, msec
=0;
451 vts_file
= ifoOpen(dvd
, vts_no
);
455 if(!vts_file
->vtsi_mat
|| !vts_file
->vts_pgcit
)
461 for(title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
463 if (tt_srpt
->title
[title_no
].title_set_nr
!= vts_no
)
465 msec
= mp_get_titleset_length(vts_file
, tt_srpt
, title_no
);
466 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no
+ 1, msec
/ 1000, msec
% 1000);
472 static int seek_to_chapter(stream_t
*stream
, ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
, int chapter
)
479 if(!vts_file
|| !tt_srpt
)
482 if(title_no
< 0 || title_no
>= tt_srpt
->nr_of_srpts
)
485 // map global title to vts title
486 title_no
= tt_srpt
->title
[title_no
].vts_ttn
- 1;
488 if(title_no
< 0 || title_no
>= vts_file
->vts_ptt_srpt
->nr_of_srpts
)
491 if(chapter
< 0 || chapter
> vts_file
->vts_ptt_srpt
->title
[title_no
].nr_of_ptts
-1) //no such chapter
494 ptt
= vts_file
->vts_ptt_srpt
->title
[title_no
].ptt
[chapter
];
495 pgc
= vts_file
->vts_pgcit
->pgci_srp
[ptt
.pgcn
-1].pgc
;
497 cell
= pgc
->program_map
[ptt
.pgn
- 1] - 1;
498 pos
= (off_t
) pgc
->cell_playback
[cell
].first_sector
* 2048;
499 mp_msg(MSGT_OPEN
,MSGL_V
,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64
"\n",
500 chapter
, pgc
->cell_playback
[cell
].first_sector
, pos
);
501 stream_seek(stream
, pos
);
506 static void list_chapters(pgc_t
*pgc
)
508 unsigned int i
, cell
;
509 unsigned int t
=0, t2
=0;
511 if(pgc
->nr_of_programs
< 2)
514 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "CHAPTERS: ");
515 for(i
=0; i
<pgc
->nr_of_programs
; i
++)
517 cell
= pgc
->program_map
[i
]; //here the cell is 1-based
519 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "%02d:%02d:%02d,", t2
/3600, (t2
/60)%60, t2
%60);
520 while(i
+1<pgc
->nr_of_programs
&& cell
< pgc
->program_map
[i
+1]) {
521 if(!(pgc
->cell_playback
[cell
-1].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
522 pgc
->cell_playback
[cell
-1].block_mode
!= BLOCK_MODE_FIRST_CELL
)
524 t
+= mp_dvdtimetomsec(&pgc
->cell_playback
[cell
-1].playback_time
);
528 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "\n");
531 static double dvd_get_current_time(stream_t
*stream
, int cell
)
534 dvd_priv_t
*d
= stream
->priv
;
537 if(!cell
) cell
=d
->cur_cell
;
538 for(i
=0; i
<d
->cur_cell
; i
++) {
539 if(d
->cur_pgc
->cell_playback
[i
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
540 d
->cur_pgc
->cell_playback
[i
].block_mode
!= BLOCK_MODE_FIRST_CELL
543 tm
+= d
->cell_times_table
[i
];
545 tm
+= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
547 return (double)tm
/1000.0;
550 static int dvd_seek_to_time(stream_t
*stream
, ifo_handle_t
*vts_file
, double sec
)
552 unsigned int i
, j
, k
, timeunit
, ac_time
, tmap_sector
=0, cell_sector
=0, vobu_sector
=0;
556 dvd_priv_t
*d
= stream
->priv
;
557 vts_tmapt_t
*vts_tmapt
= vts_file
->vts_tmapt
;
559 if(!vts_file
->vts_tmapt
|| sec
< 0)
562 duration
= (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1) / 1000.0f
;
567 timeunit
= vts_tmapt
->tmap
[i
].tmu
;
568 for(j
= 0; j
< vts_tmapt
->tmap
[i
].nr_of_entries
; j
++) {
569 ac_time
= timeunit
* (j
+ 1);
572 tmap_sector
= vts_tmapt
->tmap
[i
].map_ent
[j
] & 0x7fffffff;
574 //search enclosing cell
575 for(i
=0; i
<d
->cur_pgc
->nr_of_cells
; i
++) {
576 if(tmap_sector
>= d
->cur_pgc
->cell_playback
[i
].first_sector
&& tmap_sector
<= d
->cur_pgc
->cell_playback
[i
].last_sector
) {
577 cell_sector
= d
->cur_pgc
->cell_playback
[i
].first_sector
;
582 pos
= ((off_t
)cell_sector
)<<11;
583 stream_seek(stream
, pos
);
585 stream_skip(stream
, 2048);
586 t
= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
588 tm
= dvd_get_current_time(stream
, 0);
590 pos
= ((off_t
)tmap_sector
)<<11;
591 stream_seek(stream
, pos
);
592 //now get current time in terms of the cell+cell time offset
593 memset(&d
->dsi_pack
.dsi_gi
.c_eltm
, 0, sizeof(dvd_time_t
));
595 if(!stream_skip(stream
, 2048))
597 tm
= dvd_get_current_time(stream
, 0);
599 tmap_sector
= stream
->pos
>> 11;
601 //search closest VOBU sector
602 k
=(vts_file
->vts_vobu_admap
->last_byte
+ 1 - VOBU_ADMAP_SIZE
)/4; //entries in the vobu admap
604 if(vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
] > tmap_sector
)
607 vobu_sector
= vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
-1];
608 pos
= ((off_t
)vobu_sector
) << 11;
609 stream_seek(stream
, pos
);
614 static int control(stream_t
*stream
,int cmd
,void* arg
)
616 dvd_priv_t
*d
= stream
->priv
;
619 case STREAM_CTRL_GET_TIME_LENGTH
:
621 *((double *)arg
) = (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1)/1000.0;
624 case STREAM_CTRL_GET_NUM_CHAPTERS
:
626 if(! d
->cur_pgc
->nr_of_programs
) return STREAM_UNSUPPORTED
;
627 *((unsigned int *)arg
) = d
->cur_pgc
->nr_of_programs
;
630 case STREAM_CTRL_SEEK_TO_CHAPTER
:
633 r
= seek_to_chapter(stream
, d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1, *((unsigned int *)arg
));
634 if(! r
) return STREAM_UNSUPPORTED
;
638 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
640 *((unsigned int *)arg
) = dvd_chapter_from_cell(d
, d
->cur_title
-1, d
->cur_cell
);
643 case STREAM_CTRL_GET_CURRENT_TIME
:
646 tm
= dvd_get_current_time(stream
, 0);
648 *((double *)arg
) = tm
;
653 case STREAM_CTRL_SEEK_TO_TIME
:
655 if(dvd_seek_to_time(stream
, d
->vts_file
, *((double*)arg
)))
659 case STREAM_CTRL_GET_ASPECT_RATIO
:
661 *((double *)arg
) = !d
->vts_file
->vtsi_mat
->vts_video_attr
.display_aspect_ratio
? 4.0/3.0 : 16.0/9.0;
664 case STREAM_CTRL_GET_NUM_ANGLES
:
666 *((int *)arg
) = d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
;
669 case STREAM_CTRL_GET_ANGLE
:
671 *((int *)arg
) = dvd_angle
+1;
674 case STREAM_CTRL_SET_ANGLE
:
676 int ang
= *((int *)arg
);
677 if(ang
>d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
|| ang
<=0)
684 return STREAM_UNSUPPORTED
;
688 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
689 struct stream_priv_s
* p
= (struct stream_priv_s
*)opts
;
692 mp_msg(MSGT_OPEN
,MSGL_V
,"URL: %s\n", stream
->url
);
693 dvd_title
= p
->title
;
700 ifo_handle_t
*vmg_file
;
702 ifo_handle_t
*vts_file
;
708 dvd_device_current
= p
->device
;
710 dvd_device_current
= dvd_device
;
712 dvd_device_current
= DEFAULT_DVD_DEVICE
;
713 dvd_set_speed(dvd_device_current
, dvd_speed
);
714 #if defined(__APPLE__) || defined(__DARWIN__)
715 /* Dynamic DVD drive selection on Darwin */
716 if(!strcmp(dvd_device_current
, "/dev/rdiskN")) {
718 size_t len
= strlen(dvd_device_current
)+1;
719 char *temp_device
= malloc(len
);
721 for (i
= 1; i
< 10; i
++) {
722 snprintf(temp_device
, len
, "/dev/rdisk%d", i
);
723 dvd
= DVDOpen(temp_device
);
725 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",temp_device
, strerror(errno
));
727 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
728 dvd_file_t
*dvdfile
= DVDOpenFile(dvd
,dvd_title
,DVD_READ_INFO_FILE
);
730 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",temp_device
, strerror(errno
));
734 DVDCloseFile(dvdfile
);
742 m_struct_free(&stream_opts
,opts
);
743 return STREAM_UNSUPPORTED
;
746 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
748 dvd
= DVDOpen(dvd_device_current
);
750 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",dvd_device_current
, strerror(errno
));
751 m_struct_free(&stream_opts
,opts
);
752 return STREAM_UNSUPPORTED
;
756 mp_msg(MSGT_OPEN
,MSGL_V
,"Reading disc structure, please wait...\n");
759 * Load the video manager to find out the information about the titles on
762 vmg_file
= ifoOpen(dvd
, 0);
764 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Can't open VMG info!\n");
766 m_struct_free(&stream_opts
,opts
);
767 return STREAM_UNSUPPORTED
;
769 tt_srpt
= vmg_file
->tt_srpt
;
770 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
))
772 int title_no
; ///< title number
773 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLES=%d\n", tt_srpt
->nr_of_srpts
);
774 for (title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
776 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_ptts
);
777 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_angles
);
780 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_V
))
783 unsigned char discid
[16]; ///< disk ID, a 128 bit MD5 sum
784 int vts_no
; ///< video title set number
785 for (vts_no
= 1; vts_no
<= vmg_file
->vts_atrt
->nr_of_vtss
; vts_no
++)
786 mp_describe_titleset(dvd
, tt_srpt
, vts_no
);
787 if (DVDDiscID(dvd
, discid
) >= 0)
790 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_DISC_ID=");
791 for (i
= 0; i
< 16; i
++)
792 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "%02X", discid
[i
]);
793 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "\n");
795 if (DVDUDFVolumeInfo(dvd
, volid
, sizeof(volid
), NULL
, 0) >= 0 || DVDISOVolumeInfo(dvd
, volid
, sizeof(volid
), NULL
, 0) >= 0)
796 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_VOLUME_ID=%s\n", volid
);
799 * Make sure our title number is valid.
801 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
, "There are %d titles on this DVD.\n", tt_srpt
->nr_of_srpts
);
802 if(dvd_title
< 1 || dvd_title
> tt_srpt
->nr_of_srpts
) {
803 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Invalid DVD title number: %d\n", dvd_title
);
804 ifoClose( vmg_file
);
806 m_struct_free(&stream_opts
,opts
);
807 return STREAM_UNSUPPORTED
;
809 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title
);
810 --dvd_title
; // remap 1.. -> 0..
812 * Make sure the angle number is valid for this title.
814 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
, "There are %d angles in this DVD title.\n", tt_srpt
->title
[dvd_title
].nr_of_angles
);
815 if(dvd_angle
<1 || dvd_angle
>tt_srpt
->title
[dvd_title
].nr_of_angles
) {
816 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Invalid DVD angle number: %d\n", dvd_angle
);
819 --dvd_angle
; // remap 1.. -> 0..
821 ttn
= tt_srpt
->title
[dvd_title
].vts_ttn
- 1;
823 * Load the VTS information for the title set our title is in.
825 vts_file
= ifoOpen( dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
);
827 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Cannot open the IFO file for DVD title %d.\n", tt_srpt
->title
[dvd_title
].title_set_nr
);
831 * We've got enough info, time to open the title set data.
833 title
= DVDOpenFile(dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
, DVD_READ_TITLE_VOBS
);
835 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Cannot open title VOBS (VTS_%02d_1.VOB).\n", tt_srpt
->title
[dvd_title
].title_set_nr
);
836 ifoClose( vts_file
);
840 mp_msg(MSGT_OPEN
,MSGL_V
, "DVD successfully opened.\n");
842 d
=malloc(sizeof(dvd_priv_t
)); memset(d
,0,sizeof(dvd_priv_t
));
845 d
->vmg_file
=vmg_file
;
847 d
->vts_file
=vts_file
;
848 d
->cur_title
= dvd_title
+1;
850 pgc
= vts_file
->vts_pgcit
? vts_file
->vts_pgcit
->pgci_srp
[ttn
].pgc
: NULL
;
852 * Check number of audio channels and types
856 if(vts_file
->vts_pgcit
) {
859 if(pgc
->audio_control
[i
] & 0x8000) {
860 audio_attr_t
* audio
= &vts_file
->vtsi_mat
->vts_audio_attr
[i
];
862 char tmp
[] = "unknown";
863 stream_language_t
*audio_stream
= &d
->audio_streams
[d
->nr_of_channels
];
865 if(audio
->lang_type
== 1) {
866 language
=audio
->lang_code
;
868 tmp
[1]=language
&0xff;
872 audio_stream
->language
=language
;
873 audio_stream
->id
=pgc
->audio_control
[i
] >> 8 & 7;
874 switch(audio
->audio_format
) {
876 audio_stream
->id
+=FIRST_AC3_AID
;
879 audio_stream
->id
+=FIRST_DTS_AID
;
881 case 2: // mpeg layer 1/2/3
883 audio_stream
->id
+=FIRST_MPG_AID
;
886 audio_stream
->id
+=FIRST_PCM_AID
;
890 audio_stream
->type
=audio
->audio_format
;
891 // Pontscho: to my mind, tha channels:
894 audio_stream
->channels
=audio
->channels
;
895 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"audio stream: %d format: %s (%s) language: %s aid: %d.\n",
897 dvd_audio_stream_types
[ audio
->audio_format
],
898 dvd_audio_stream_channels
[ audio
->channels
],
902 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", audio_stream
->id
);
903 if(language
&& tmp
[0])
904 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", audio_stream
->id
, tmp
);
909 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"number of audio channels on disk: %d.\n",d
->nr_of_channels
);
913 * Check number of subtitles and language
918 d
->nr_of_subtitles
=0;
920 if(pgc
->subp_control
[i
] & 0x80000000) {
921 subp_attr_t
* subtitle
= &vts_file
->vtsi_mat
->vts_subp_attr
[i
];
922 video_attr_t
*video
= &vts_file
->vtsi_mat
->vts_video_attr
;
924 char tmp
[] = "unknown";
925 stream_language_t
*sub_stream
= &d
->subtitles
[d
->nr_of_subtitles
];
927 if(subtitle
->type
== 1) {
928 language
=subtitle
->lang_code
;
930 tmp
[1]=language
&0xff;
934 sub_stream
->language
=language
;
935 sub_stream
->id
=d
->nr_of_subtitles
;
936 if(video
->display_aspect_ratio
== 0) /* 4:3 */
937 sub_stream
->id
= pgc
->subp_control
[i
] >> 24 & 31;
938 else if(video
->display_aspect_ratio
== 3) /* 16:9 */
939 sub_stream
->id
= pgc
->subp_control
[i
] >> 8 & 31;
941 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"subtitle ( sid ): %d language: %s\n", sub_stream
->id
, tmp
);
942 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sub_stream
->id
);
943 if(language
&& tmp
[0])
944 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sub_stream
->id
, tmp
);
945 d
->nr_of_subtitles
++;
947 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"number of subtitles on disk: %d\n",d
->nr_of_subtitles
);
951 * Determine which program chain we want to watch. This is based on the
954 pgc_id
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgcn
; // local
955 pgn
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgn
; // local
956 d
->cur_pgc_idx
= pgc_id
-1;
957 d
->cur_pgc
= vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
958 d
->cur_cell
= d
->cur_pgc
->program_map
[pgn
-1] - 1; // start playback here
959 d
->packs_left
=-1; // for Navi stuff
961 d
->last_cell
=d
->cur_pgc
->nr_of_cells
;
963 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
964 d
->cur_cell
+=dvd_angle
;
965 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
966 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
967 mp_msg(MSGT_DVD
,MSGL_V
, "DVD start cell: %d pack: 0x%X-0x%X \n",d
->cur_cell
,d
->cur_pack
,d
->cell_last_pack
);
969 //assign cell_times_table
970 d
->cell_times_table
= malloc(sizeof(unsigned int) * d
->cur_pgc
->nr_of_cells
);
971 if(d
->cell_times_table
== NULL
)
972 return STREAM_UNSUPPORTED
;
973 for(k
=0; k
<d
->cur_pgc
->nr_of_cells
; k
++)
974 d
->cell_times_table
[k
] = mp_dvdtimetomsec(&d
->cur_pgc
->cell_playback
[k
].playback_time
);
975 list_chapters(d
->cur_pgc
);
977 // ... (unimplemented)
979 stream
->type
= STREAMTYPE_DVD
;
980 stream
->sector_size
= 2048;
981 stream
->flags
= STREAM_READ
| MP_STREAM_SEEK
;
982 stream
->fill_buffer
= fill_buffer
;
984 stream
->control
= control
;
985 stream
->close
= stream_dvd_close
;
986 stream
->start_pos
= (off_t
)d
->cur_pack
*2048;
987 stream
->end_pos
= (off_t
)(d
->cur_pgc
->cell_playback
[d
->last_cell
-1].last_sector
)*2048;
988 *file_format
= DEMUXER_TYPE_MPEG_PS
;
989 mp_msg(MSGT_DVD
,MSGL_V
,"DVD start=%d end=%d \n",d
->cur_pack
,d
->cur_pgc
->cell_playback
[d
->last_cell
-1].last_sector
);
990 stream
->priv
= (void*)d
;
996 m_struct_free(&stream_opts
, opts
);
997 return STREAM_UNSUPPORTED
;
999 mp_tmsg(MSGT_DVD
,MSGL_ERR
,"MPlayer was compiled without DVD support, exiting.\n");
1000 m_struct_free(&stream_opts
,opts
);
1001 return STREAM_UNSUPPORTED
;
1004 static int ifo_stream_open (stream_t
*stream
, int mode
, void *opts
, int *file_format
)
1007 struct stream_priv_s
*spriv
;
1008 int len
= strlen(stream
->url
);
1010 if (len
< 4 || strcasecmp (stream
->url
+ len
- 4, ".ifo"))
1011 return STREAM_UNSUPPORTED
;
1013 mp_msg(MSGT_DVD
, MSGL_INFO
, ".IFO detected. Redirecting to dvd://\n");
1015 filename
= strdup(basename(stream
->url
));
1017 spriv
=calloc(1, sizeof(struct stream_priv_s
));
1018 spriv
->device
= strdup(dirname(stream
->url
));
1019 if(!strncasecmp(filename
,"vts_",4))
1021 if(sscanf(filename
+3, "_%02d_", &spriv
->title
)!=1)
1028 stream
->url
=strdup("dvd://");
1030 return open_s(stream
, mode
, spriv
, file_format
);
1033 const stream_info_t stream_info_dvd
= {
1041 1 // Urls are an option string
1044 const stream_info_t stream_info_ifo
= {
1048 "Mostly used to play DVDs on disk through OSD Menu",
1050 { "file", "", NULL
},