16 #include <sys/cdrio.h>
20 #include <linux/cdrom.h>
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
30 #define FIRST_AC3_AID 128
31 #define FIRST_DTS_AID 136
32 #define FIRST_MPG_AID 0
33 #define FIRST_PCM_AID 160
39 #include "stream_dvd.h"
40 #include "stream_dvd_common.h"
41 #include "libmpdemux/demuxer.h"
42 #include "libavutil/intreadwrite.h"
44 extern char* dvd_device
;
45 static char* dvd_device_current
;
47 int dvd_speed
=0; /* 0 => don't touch speed */
49 static void dvd_set_speed(char *device
, unsigned speed
)
51 #if defined(__linux__) && defined(SG_IO) && defined(GPCMD_SET_STREAMING)
53 unsigned char buffer
[28];
54 unsigned char cmd
[12];
55 struct sg_io_hdr sghdr
;
58 memset(&st
, 0, sizeof(st
));
60 if (stat(device
, &st
) == -1) return;
62 if (!S_ISBLK(st
.st_mode
)) return; /* not a block device */
65 case 0: /* don't touch speed setting */
67 case -1: /* restore default value */
68 if (dvd_speed
== 0) return; /* we haven't touched the speed setting */
69 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_DVDrestoreSpeed
);
71 default: /* limit to <speed> KB/s */
72 // speed < 100 is multiple of DVD single speed (1350KB/s)
75 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_DVDlimitSpeed
, speed
);
79 memset(&sghdr
, 0, sizeof(sghdr
));
80 sghdr
.interface_id
= 'S';
82 sghdr
.dxfer_direction
= SG_DXFER_TO_DEV
;
83 sghdr
.dxfer_len
= sizeof(buffer
);
84 sghdr
.dxferp
= buffer
;
85 sghdr
.cmd_len
= sizeof(cmd
);
88 memset(cmd
, 0, sizeof(cmd
));
89 cmd
[0] = GPCMD_SET_STREAMING
;
90 cmd
[10] = sizeof(buffer
);
92 memset(buffer
, 0, sizeof(buffer
));
93 /* first sector 0, last sector 0xffffffff */
94 AV_WB32(buffer
+ 8, 0xffffffff);
96 buffer
[0] = 4; /* restore default */
98 /* <speed> kilobyte */
99 AV_WB32(buffer
+ 12, speed
);
100 AV_WB32(buffer
+ 20, speed
);
103 AV_WB16(buffer
+ 18, 1000);
104 AV_WB16(buffer
+ 26, 1000);
106 fd
= open(device
, O_RDWR
| O_NONBLOCK
);
108 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_DVDspeedCantOpen
);
112 if (ioctl(fd
, SG_IO
, &sghdr
) < 0)
113 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_DVDlimitFail
);
115 mp_msg(MSGT_OPEN
, MSGL_INFO
, MSGTR_DVDlimitOk
);
121 #define LIBDVDREAD_VERSION(maj,min,micro) ((maj)*10000 + (min)*100 + (micro))
123 * Try to autodetect the libdvd-0.9.0 library
124 * (0.9.0 removed the <dvdread/dvd_udf.h> header, and moved the two defines
125 * DVD_VIDEO_LB_LEN and MAX_UDF_FILE_NAME_LEN from it to
126 * <dvdread/dvd_reader.h>)
128 #ifndef DVDREAD_VERSION
129 #if defined(DVD_VIDEO_LB_LEN) && defined(MAX_UDF_FILE_NAME_LEN)
130 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,9,0)
132 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,8,0)
136 const char * const dvd_audio_stream_types
[8] = { "ac3","unknown","mpeg1","mpeg2ext","lpcm","unknown","dts" };
137 const char * const dvd_audio_stream_channels
[6] = { "mono", "stereo", "unknown", "unknown", "5.1/6.1", "5.1" };
140 static struct stream_priv_s
{
143 } stream_priv_dflts
= {
148 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
150 static const m_option_t stream_opts_fields
[] = {
151 { "hostname", ST_OFF(title
), CONF_TYPE_INT
, M_OPT_RANGE
, 1, 99, NULL
},
152 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0 ,0, NULL
},
153 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
155 static const struct m_struct_st stream_opts
= {
157 sizeof(struct stream_priv_s
),
162 int dvd_parse_chapter_range(const m_option_t
*conf
, const char *range
) {
166 return M_OPT_MISSING_PARAM
;
169 dvd_last_chapter
= 0;
170 if(*range
&& isdigit(*range
)) {
171 dvd_chapter
= strtol(range
, &s
, 10);
173 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_DVDinvalidChapterRange
, range
);
174 return M_OPT_INVALID
;
180 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_DVDinvalidChapterRange
, range
);
181 return M_OPT_INVALID
;
187 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_DVDinvalidChapterRange
, range
);
188 return M_OPT_INVALID
;
190 dvd_last_chapter
= strtol(s
, &t
, 10);
192 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_DVDinvalidChapterRange
, range
);
193 return M_OPT_INVALID
;
198 int dvd_chapter_from_cell(dvd_priv_t
* dvd
,int title
,int cell
)
204 if(title
< 0 || cell
< 0){
207 /* for most DVD's chapter == cell */
208 /* but there are more complecated cases... */
209 if(chapter
>= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
) {
210 chapter
= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
-1;
212 title
= dvd
->tt_srpt
->title
[title
].vts_ttn
-1;
213 ptt
= dvd
->vts_file
->vts_ptt_srpt
->title
[title
].ptt
;
214 while(chapter
>= 0) {
215 pgc_id
= ptt
[chapter
].pgcn
;
216 pgn
= ptt
[chapter
].pgn
;
217 cur_pgc
= dvd
->vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
218 if(cell
>= cur_pgc
->program_map
[pgn
-1]-1) {
223 /* didn't find a chapter ??? */
227 int dvd_lang_from_aid(stream_t
*stream
, int id
) {
230 if (!stream
) return 0;
233 for(i
=0;i
<d
->nr_of_channels
;i
++) {
234 if(d
->audio_streams
[i
].id
==id
)
235 return d
->audio_streams
[i
].language
;
240 int dvd_aid_from_lang(stream_t
*stream
, unsigned char* lang
) {
241 dvd_priv_t
*d
=stream
->priv
;
244 while(strlen(lang
)>=2) {
245 code
=lang
[1]|(lang
[0]<<8);
246 for(i
=0;i
<d
->nr_of_channels
;i
++) {
247 if(d
->audio_streams
[i
].language
==code
) {
248 mp_msg(MSGT_OPEN
,MSGL_INFO
,MSGTR_DVDaudioChannel
,
249 d
->audio_streams
[i
].id
, lang
[0],lang
[1]);
250 return d
->audio_streams
[i
].id
;
252 //printf("%X != %X (%c%c)\n",code,d->audio_streams[i].language,lang[0],lang[1]);
254 lang
+=2; while (lang
[0]==',' || lang
[0]==' ') ++lang
;
256 mp_msg(MSGT_OPEN
,MSGL_WARN
,MSGTR_DVDnoMatchingAudio
);
261 int dvd_number_of_subs(stream_t
*stream
) {
265 if (!stream
) return -1;
268 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
269 if (d
->subtitles
[i
].id
> maxid
) maxid
= d
->subtitles
[i
].id
;
273 int dvd_lang_from_sid(stream_t
*stream
, int id
) {
276 if (!stream
) return 0;
279 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
280 if (d
->subtitles
[i
].id
== id
&& d
->subtitles
[i
].language
) return d
->subtitles
[i
].language
;
284 int dvd_sid_from_lang(stream_t
*stream
, unsigned char* lang
) {
285 dvd_priv_t
*d
=stream
->priv
;
287 while(lang
&& strlen(lang
)>=2) {
288 code
=lang
[1]|(lang
[0]<<8);
289 for(i
=0;i
<d
->nr_of_subtitles
;i
++) {
290 if(d
->subtitles
[i
].language
==code
) {
291 mp_msg(MSGT_OPEN
,MSGL_INFO
,MSGTR_DVDsubtitleChannel
, i
, lang
[0],lang
[1]);
292 return d
->subtitles
[i
].id
;
296 while (lang
[0]==',' || lang
[0]==' ') ++lang
;
298 mp_msg(MSGT_OPEN
,MSGL_WARN
,MSGTR_DVDnoMatchingSubtitle
);
302 static int dvd_next_cell(dvd_priv_t
*d
) {
303 int next_cell
=d
->cur_cell
;
305 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next1=0x%X \n",next_cell
);
306 if( d
->cur_pgc
->cell_playback
[ next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
307 while(next_cell
<d
->last_cell
) {
308 if( d
->cur_pgc
->cell_playback
[next_cell
].block_mode
== BLOCK_MODE_LAST_CELL
)
313 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next2=0x%X \n",next_cell
);
316 if(next_cell
>=d
->last_cell
)
318 if(d
->cur_pgc
->cell_playback
[next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
319 next_cell
+=dvd_angle
;
320 if(next_cell
>=d
->last_cell
)
323 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next3=0x%X \n",next_cell
);
327 static int dvd_read_sector(dvd_priv_t
*d
,unsigned char* data
) {
330 if(d
->packs_left
==0) {
332 * If we're not at the end of this cell, we can determine the next
333 * VOBU to display using the VOBU_SRI information section of the
334 * DSI. Using this value correctly follows the current angle,
335 * avoiding the doubled scenes in The Matrix, and makes our life
338 * Otherwise, we set our next address past the end of this cell to
339 * force the code above to go to the next cell in the program.
341 if(d
->dsi_pack
.vobu_sri
.next_vobu
!= SRI_END_OF_CELL
) {
342 d
->cur_pack
= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+ ( d
->dsi_pack
.vobu_sri
.next_vobu
& 0x7fffffff );
343 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Navi new pos=0x%X \n",d
->cur_pack
);
345 // end of cell! find next cell!
346 mp_msg(MSGT_DVD
,MSGL_V
, "--- END OF CELL !!! ---\n");
347 d
->cur_pack
=d
->cell_last_pack
+1;
352 if(d
->cur_pack
>d
->cell_last_pack
) {
354 int next
=dvd_next_cell(d
);
357 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
358 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
359 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
360 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
361 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
);
366 len
= DVDReadBlocks(d
->title
, d
->cur_pack
, 1, data
);
367 if(!len
) return -1; //error
369 if(data
[38]==0 && data
[39]==0 && data
[40]==1 && data
[41]==0xBF &&
370 data
[1024]==0 && data
[1025]==0 && data
[1026]==1 && data
[1027]==0xBF) {
371 // found a Navi packet!!!
372 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
373 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]));
375 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]), sizeof(dsi_t
));
377 if(d
->cur_pack
!= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
) {
378 mp_msg(MSGT_DVD
,MSGL_V
, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
379 d
->cur_pack
,d
->dsi_pack
.dsi_gi
.nv_pck_lbn
);
382 d
->packs_left
= d
->dsi_pack
.dsi_gi
.vobu_ea
;
383 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Found NAVI packet! lba=0x%X len=%d \n",d
->cur_pack
,d
->packs_left
);
384 //navPrint_DSI(&d->dsi_pack);
385 mp_msg(MSGT_DVD
,MSGL_DBG3
,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d
->cur_cell
,
386 d
->dsi_pack
.dsi_gi
.vobu_c_idn
,d
->dsi_pack
.dsi_gi
.vobu_vob_idn
,
387 d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
,
388 d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
);
392 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
393 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
394 // it generates incorrect code for unaligned access to a packed
395 // structure member, resulting in an mplayer crash with a SIGBUS
398 // See also gcc problem report PR c/7847:
399 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
400 for(i
=0;i
<9;i
++) { // check if all values zero:
401 typeof(d
->dsi_pack
.sml_agli
.data
[i
].address
) tmp_addr
;
402 memcpy(&tmp_addr
,&d
->dsi_pack
.sml_agli
.data
[i
].address
,sizeof(tmp_addr
));
403 if((skip
=tmp_addr
)!=0) break;
406 for(i
=0;i
<9;i
++) // check if all values zero:
407 if((skip
=d
->dsi_pack
.sml_agli
.data
[i
].address
)!=0) break;
409 if(skip
&& skip
!=0x7fffffff) {
410 // sml_agli table has valid data (at least one non-zero):
411 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
412 d
->dsi_pack
.sml_agli
.data
[dvd_angle
].address
;
415 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d
->cur_pack
);
417 // check if we're in the right cell, jump otherwise:
418 if( (d
->dsi_pack
.dsi_gi
.vobu_c_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
) &&
419 (d
->dsi_pack
.dsi_gi
.vobu_vob_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
) ){
421 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced by cell/vob IDN search! \n");
423 // wrong angle, skip this vobu:
424 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
425 d
->dsi_pack
.dsi_gi
.vobu_ea
;
426 d
->angle_seek
=2; // DEBUG
436 if(d
->packs_left
>=0) --d
->packs_left
;
439 if(d
->angle_seek
==2) mp_msg(MSGT_DVD
,MSGL_V
, "!!! warning! reading packet while angle_seek !!!\n");
440 goto read_next
; // searching for Navi packet
443 return d
->cur_pack
-1;
446 static void dvd_seek(dvd_priv_t
*d
,int pos
) {
450 // check if we stay in current cell (speedup things, and avoid angle skip)
451 if(d
->cur_pack
>d
->cell_last_pack
||
452 d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
454 // ok, cell change, find the right cell!
456 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
457 d
->cur_cell
+=dvd_angle
;
461 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
462 if(d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
463 d
->cur_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
466 if(d
->cur_pack
<=d
->cell_last_pack
) break; // ok, we find it! :)
467 next
=dvd_next_cell(d
);
469 //d->cur_pack=d->cell_last_pack+1;
470 break; // we're after the last cell
476 mp_msg(MSGT_DVD
,MSGL_V
, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
477 d
->cur_pack
,d
->cur_cell
,d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
,d
->cell_last_pack
);
479 // if we're in interleaved multi-angle cell, find the right angle chain!
480 // (read Navi block, and use the seamless angle jump table)
484 static void dvd_close(dvd_priv_t
*d
) {
485 ifoClose(d
->vts_file
);
486 ifoClose(d
->vmg_file
);
487 DVDCloseFile(d
->title
);
490 dvd_last_chapter
= 0;
491 dvd_set_speed(dvd_device_current
, -1); /* -1 => restore default */
494 static int fill_buffer(stream_t
*s
, char *but
, int len
)
496 if(s
->type
== STREAMTYPE_DVD
) {
497 off_t pos
=dvd_read_sector(s
->priv
,s
->buffer
);
499 len
=2048; // full sector
501 } else len
=-1; // error
506 static int seek(stream_t
*s
, off_t newpos
) {
507 s
->pos
=newpos
; // real seek
508 dvd_seek(s
->priv
,s
->pos
/2048);
512 static void stream_dvd_close(stream_t
*s
) {
516 static int mp_get_titleset_length(ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
)
518 int vts_ttn
; ///< title number within video title set
519 int pgc_no
; ///< program chain number
520 int msec
; ///< time length in milliseconds
523 if(!vts_file
|| !tt_srpt
)
526 if(vts_file
->vtsi_mat
&& vts_file
->vts_pgcit
)
528 vts_ttn
= tt_srpt
->title
[title_no
].vts_ttn
- 1;
529 pgc_no
= vts_file
->vts_ptt_srpt
->title
[vts_ttn
].ptt
[0].pgcn
- 1;
530 msec
= mp_dvdtimetomsec(&vts_file
->vts_pgcit
->pgci_srp
[pgc_no
].pgc
->playback_time
);
536 static int mp_describe_titleset(dvd_reader_t
*dvd
, tt_srpt_t
*tt_srpt
, int vts_no
)
538 ifo_handle_t
*vts_file
;
539 int title_no
, msec
=0;
541 vts_file
= ifoOpen(dvd
, vts_no
);
545 if(!vts_file
->vtsi_mat
|| !vts_file
->vts_pgcit
)
551 for(title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
553 if (tt_srpt
->title
[title_no
].title_set_nr
!= vts_no
)
555 msec
= mp_get_titleset_length(vts_file
, tt_srpt
, title_no
);
556 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no
+ 1, msec
/ 1000, msec
% 1000);
562 static int seek_to_chapter(stream_t
*stream
, ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
, int chapter
)
569 if(!vts_file
|| !tt_srpt
)
572 if(chapter
< 0 || chapter
> vts_file
->vts_ptt_srpt
->title
[title_no
].nr_of_ptts
-1) //no such chapter
575 ptt
= vts_file
->vts_ptt_srpt
->title
[title_no
].ptt
[chapter
];
576 pgc
= vts_file
->vts_pgcit
->pgci_srp
[ptt
.pgcn
-1].pgc
;
578 cell
= pgc
->program_map
[ptt
.pgn
- 1] - 1;
579 pos
= (off_t
) pgc
->cell_playback
[cell
].first_sector
* 2048;
580 mp_msg(MSGT_OPEN
,MSGL_V
,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64
"\n",
581 chapter
, pgc
->cell_playback
[cell
].first_sector
, pos
);
582 stream_seek(stream
, pos
);
587 static void list_chapters(pgc_t
*pgc
)
589 unsigned int i
, cell
;
590 unsigned int t
=0, t2
=0;
592 if(pgc
->nr_of_programs
< 2)
595 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "CHAPTERS: ");
596 for(i
=0; i
<pgc
->nr_of_programs
; i
++)
598 cell
= pgc
->program_map
[i
]; //here the cell is 1-based
600 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "%02d:%02d:%02d,", t2
/3600, (t2
/60)%60, t2
%60);
601 while(i
+1<pgc
->nr_of_programs
&& cell
< pgc
->program_map
[i
+1]) {
602 if(!(pgc
->cell_playback
[cell
-1].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
603 pgc
->cell_playback
[cell
-1].block_mode
!= BLOCK_MODE_FIRST_CELL
)
605 t
+= mp_dvdtimetomsec(&pgc
->cell_playback
[cell
-1].playback_time
);
609 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "\n");
612 static double dvd_get_current_time(stream_t
*stream
, int cell
)
615 dvd_priv_t
*d
= stream
->priv
;
618 if(!cell
) cell
=d
->cur_cell
;
619 for(i
=0; i
<d
->cur_cell
; i
++) {
620 if(d
->cur_pgc
->cell_playback
[i
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
621 d
->cur_pgc
->cell_playback
[i
].block_mode
!= BLOCK_MODE_FIRST_CELL
624 tm
+= d
->cell_times_table
[i
];
626 tm
+= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
628 return (double)tm
/1000.0;
631 static int dvd_seek_to_time(stream_t
*stream
, ifo_handle_t
*vts_file
, double sec
)
633 unsigned int i
, j
, k
, timeunit
, ac_time
, tmap_sector
=0, cell_sector
=0, vobu_sector
=0;
637 dvd_priv_t
*d
= stream
->priv
;
638 vts_tmapt_t
*vts_tmapt
= vts_file
->vts_tmapt
;
640 if(!vts_file
->vts_tmapt
|| sec
< 0)
643 duration
= (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1) / 1000.0f
;
648 timeunit
= vts_tmapt
->tmap
[i
].tmu
;
649 for(j
= 0; j
< vts_tmapt
->tmap
[i
].nr_of_entries
; j
++) {
650 ac_time
= timeunit
* (j
+ 1);
653 tmap_sector
= vts_tmapt
->tmap
[i
].map_ent
[j
] & 0x7fffffff;
655 //search enclosing cell
656 for(i
=0; i
<d
->cur_pgc
->nr_of_cells
; i
++) {
657 if(tmap_sector
>= d
->cur_pgc
->cell_playback
[i
].first_sector
&& tmap_sector
<= d
->cur_pgc
->cell_playback
[i
].last_sector
) {
658 cell_sector
= d
->cur_pgc
->cell_playback
[i
].first_sector
;
663 pos
= ((off_t
)cell_sector
)<<11;
664 stream_seek(stream
, pos
);
666 stream_skip(stream
, 2048);
667 t
= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
669 tm
= dvd_get_current_time(stream
, 0);
671 pos
= ((off_t
)tmap_sector
)<<11;
672 stream_seek(stream
, pos
);
673 //now get current time in terms of the cell+cell time offset
674 memset(&d
->dsi_pack
.dsi_gi
.c_eltm
, 0, sizeof(dvd_time_t
));
676 if(!stream_skip(stream
, 2048))
678 tm
= dvd_get_current_time(stream
, 0);
680 tmap_sector
= stream
->pos
>> 11;
682 //search closest VOBU sector
683 k
=(vts_file
->vts_vobu_admap
->last_byte
+ 1 - VOBU_ADMAP_SIZE
)/4; //entries in the vobu admap
685 if(vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
] > tmap_sector
)
688 vobu_sector
= vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
-1];
689 pos
= ((off_t
)vobu_sector
) << 11;
690 stream_seek(stream
, pos
);
695 static int control(stream_t
*stream
,int cmd
,void* arg
)
697 dvd_priv_t
*d
= stream
->priv
;
700 case STREAM_CTRL_GET_TIME_LENGTH
:
702 *((double *)arg
) = (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1)/1000.0;
705 case STREAM_CTRL_GET_NUM_CHAPTERS
:
707 if(! d
->cur_pgc
->nr_of_programs
) return STREAM_UNSUPPORTED
;
708 *((unsigned int *)arg
) = d
->cur_pgc
->nr_of_programs
;
711 case STREAM_CTRL_SEEK_TO_CHAPTER
:
714 r
= seek_to_chapter(stream
, d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1, *((unsigned int *)arg
));
715 if(! r
) return STREAM_UNSUPPORTED
;
719 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
721 *((unsigned int *)arg
) = dvd_chapter_from_cell(d
, d
->cur_title
-1, d
->cur_cell
);
724 case STREAM_CTRL_GET_CURRENT_TIME
:
727 tm
= dvd_get_current_time(stream
, 0);
729 *((double *)arg
) = tm
;
734 case STREAM_CTRL_SEEK_TO_TIME
:
736 if(dvd_seek_to_time(stream
, d
->vts_file
, *((double*)arg
)))
740 case STREAM_CTRL_GET_ASPECT_RATIO
:
742 *((double *)arg
) = !d
->vts_file
->vtsi_mat
->vts_video_attr
.display_aspect_ratio
? 4.0/3.0 : 16.0/9.0;
745 case STREAM_CTRL_GET_NUM_ANGLES
:
747 *((int *)arg
) = d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
;
750 case STREAM_CTRL_GET_ANGLE
:
752 *((int *)arg
) = dvd_angle
+1;
755 case STREAM_CTRL_SET_ANGLE
:
757 int ang
= *((int *)arg
);
758 if(ang
>d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
|| ang
<=0)
765 return STREAM_UNSUPPORTED
;
769 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
770 struct stream_priv_s
* p
= (struct stream_priv_s
*)opts
;
773 mp_msg(MSGT_OPEN
,MSGL_V
,"URL: %s\n", stream
->url
);
774 dvd_title
= p
->title
;
781 ifo_handle_t
*vmg_file
;
783 ifo_handle_t
*vts_file
;
789 dvd_device_current
= p
->device
;
791 dvd_device_current
= dvd_device
;
793 dvd_device_current
= DEFAULT_DVD_DEVICE
;
794 dvd_set_speed(dvd_device_current
, dvd_speed
);
795 #if defined(__APPLE__) || defined(__DARWIN__)
796 /* Dynamic DVD drive selection on Darwin */
797 if(!strcmp(dvd_device_current
, "/dev/rdiskN")) {
799 size_t len
= strlen(dvd_device_current
)+1;
800 char *temp_device
= malloc(len
);
802 for (i
= 1; i
< 10; i
++) {
803 snprintf(temp_device
, len
, "/dev/rdisk%d", i
);
804 dvd
= DVDOpen(temp_device
);
806 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_CantOpenDVD
,temp_device
, strerror(errno
));
808 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
809 dvd_file_t
*dvdfile
= DVDOpenFile(dvd
,dvd_title
,DVD_READ_INFO_FILE
);
811 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_CantOpenDVD
,temp_device
, strerror(errno
));
815 DVDCloseFile(dvdfile
);
823 m_struct_free(&stream_opts
,opts
);
824 return STREAM_UNSUPPORTED
;
827 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
829 dvd
= DVDOpen(dvd_device_current
);
831 mp_msg(MSGT_OPEN
,MSGL_ERR
,MSGTR_CantOpenDVD
,dvd_device_current
, strerror(errno
));
832 m_struct_free(&stream_opts
,opts
);
833 return STREAM_UNSUPPORTED
;
837 mp_msg(MSGT_OPEN
,MSGL_V
,"Reading disc structure, please wait...\n");
840 * Load the video manager to find out the information about the titles on
843 vmg_file
= ifoOpen(dvd
, 0);
845 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_DVDnoVMG
);
847 m_struct_free(&stream_opts
,opts
);
848 return STREAM_UNSUPPORTED
;
850 tt_srpt
= vmg_file
->tt_srpt
;
851 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
))
853 int title_no
; ///< title number
854 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLES=%d\n", tt_srpt
->nr_of_srpts
);
855 for (title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
857 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_ptts
);
858 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_angles
);
861 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_V
))
864 unsigned char discid
[16]; ///< disk ID, a 128 bit MD5 sum
865 int vts_no
; ///< video title set number
866 for (vts_no
= 1; vts_no
<= vmg_file
->vts_atrt
->nr_of_vtss
; vts_no
++)
867 mp_describe_titleset(dvd
, tt_srpt
, vts_no
);
868 if (DVDDiscID(dvd
, discid
) >= 0)
871 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_DISC_ID=");
872 for (i
= 0; i
< 16; i
++)
873 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "%02X", discid
[i
]);
874 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "\n");
876 if (DVDUDFVolumeInfo(dvd
, volid
, sizeof(volid
), NULL
, 0) >= 0)
877 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_VOLUME_ID=%s\n", volid
);
880 * Make sure our title number is valid.
882 mp_msg(MSGT_OPEN
,MSGL_STATUS
, MSGTR_DVDnumTitles
, tt_srpt
->nr_of_srpts
);
883 if(dvd_title
< 1 || dvd_title
> tt_srpt
->nr_of_srpts
) {
884 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_DVDinvalidTitle
, dvd_title
);
885 ifoClose( vmg_file
);
887 m_struct_free(&stream_opts
,opts
);
888 return STREAM_UNSUPPORTED
;
890 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title
);
891 --dvd_title
; // remap 1.. -> 0..
893 * Make sure the angle number is valid for this title.
895 mp_msg(MSGT_OPEN
,MSGL_STATUS
, MSGTR_DVDnumAngles
, tt_srpt
->title
[dvd_title
].nr_of_angles
);
896 if(dvd_angle
<1 || dvd_angle
>tt_srpt
->title
[dvd_title
].nr_of_angles
) {
897 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_DVDinvalidAngle
, dvd_angle
);
900 --dvd_angle
; // remap 1.. -> 0..
902 ttn
= tt_srpt
->title
[dvd_title
].vts_ttn
- 1;
904 * Load the VTS information for the title set our title is in.
906 vts_file
= ifoOpen( dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
);
908 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_DVDnoIFO
, tt_srpt
->title
[dvd_title
].title_set_nr
);
912 * We've got enough info, time to open the title set data.
914 title
= DVDOpenFile(dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
, DVD_READ_TITLE_VOBS
);
916 mp_msg(MSGT_OPEN
,MSGL_ERR
, MSGTR_DVDnoVOBs
, tt_srpt
->title
[dvd_title
].title_set_nr
);
917 ifoClose( vts_file
);
921 mp_msg(MSGT_OPEN
,MSGL_V
, "DVD successfully opened.\n");
923 d
=malloc(sizeof(dvd_priv_t
)); memset(d
,0,sizeof(dvd_priv_t
));
926 d
->vmg_file
=vmg_file
;
928 d
->vts_file
=vts_file
;
929 d
->cur_title
= dvd_title
+1;
931 pgc
= vts_file
->vts_pgcit
? vts_file
->vts_pgcit
->pgci_srp
[ttn
].pgc
: NULL
;
933 * Check number of audio channels and types
937 if(vts_file
->vts_pgcit
) {
940 if(pgc
->audio_control
[i
] & 0x8000) {
941 audio_attr_t
* audio
= &vts_file
->vtsi_mat
->vts_audio_attr
[i
];
943 char tmp
[] = "unknown";
944 stream_language_t
*audio_stream
= &d
->audio_streams
[d
->nr_of_channels
];
946 if(audio
->lang_type
== 1) {
947 language
=audio
->lang_code
;
949 tmp
[1]=language
&0xff;
953 audio_stream
->language
=language
;
954 audio_stream
->id
=pgc
->audio_control
[i
] >> 8 & 7;
955 switch(audio
->audio_format
) {
957 audio_stream
->id
+=FIRST_AC3_AID
;
960 audio_stream
->id
+=FIRST_DTS_AID
;
962 case 2: // mpeg layer 1/2/3
964 audio_stream
->id
+=FIRST_MPG_AID
;
967 audio_stream
->id
+=FIRST_PCM_AID
;
971 audio_stream
->type
=audio
->audio_format
;
972 // Pontscho: to my mind, tha channels:
975 audio_stream
->channels
=audio
->channels
;
976 mp_msg(MSGT_OPEN
,MSGL_STATUS
,MSGTR_DVDaudioStreamInfo
,
978 dvd_audio_stream_types
[ audio
->audio_format
],
979 dvd_audio_stream_channels
[ audio
->channels
],
983 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", audio_stream
->id
);
984 if(language
&& tmp
[0])
985 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", audio_stream
->id
, tmp
);
990 mp_msg(MSGT_OPEN
,MSGL_STATUS
,MSGTR_DVDnumAudioChannels
,d
->nr_of_channels
);
994 * Check number of subtitles and language
999 d
->nr_of_subtitles
=0;
1001 if(pgc
->subp_control
[i
] & 0x80000000) {
1002 subp_attr_t
* subtitle
= &vts_file
->vtsi_mat
->vts_subp_attr
[i
];
1003 video_attr_t
*video
= &vts_file
->vtsi_mat
->vts_video_attr
;
1005 char tmp
[] = "unknown";
1006 stream_language_t
*sub_stream
= &d
->subtitles
[d
->nr_of_subtitles
];
1008 if(subtitle
->type
== 1) {
1009 language
=subtitle
->lang_code
;
1011 tmp
[1]=language
&0xff;
1015 sub_stream
->language
=language
;
1016 sub_stream
->id
=d
->nr_of_subtitles
;
1017 if(video
->display_aspect_ratio
== 0) /* 4:3 */
1018 sub_stream
->id
= pgc
->subp_control
[i
] >> 24 & 31;
1019 else if(video
->display_aspect_ratio
== 3) /* 16:9 */
1020 sub_stream
->id
= pgc
->subp_control
[i
] >> 8 & 31;
1022 mp_msg(MSGT_OPEN
,MSGL_STATUS
,MSGTR_DVDsubtitleLanguage
, sub_stream
->id
, tmp
);
1023 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sub_stream
->id
);
1024 if(language
&& tmp
[0])
1025 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sub_stream
->id
, tmp
);
1026 d
->nr_of_subtitles
++;
1028 mp_msg(MSGT_OPEN
,MSGL_STATUS
,MSGTR_DVDnumSubtitles
,d
->nr_of_subtitles
);
1032 * Determine which program chain we want to watch. This is based on the
1035 pgc_id
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgcn
; // local
1036 pgn
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgn
; // local
1037 d
->cur_pgc_idx
= pgc_id
-1;
1038 d
->cur_pgc
= vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
1039 d
->cur_cell
= d
->cur_pgc
->program_map
[pgn
-1] - 1; // start playback here
1040 d
->packs_left
=-1; // for Navi stuff
1042 d
->last_cell
=d
->cur_pgc
->nr_of_cells
;
1044 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
1045 d
->cur_cell
+=dvd_angle
;
1046 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
1047 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
1048 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
);
1050 //assign cell_times_table
1051 d
->cell_times_table
= malloc(sizeof(unsigned int) * d
->cur_pgc
->nr_of_cells
);
1052 if(d
->cell_times_table
== NULL
)
1053 return STREAM_UNSUPPORTED
;
1054 for(k
=0; k
<d
->cur_pgc
->nr_of_cells
; k
++)
1055 d
->cell_times_table
[k
] = mp_dvdtimetomsec(&d
->cur_pgc
->cell_playback
[k
].playback_time
);
1056 list_chapters(d
->cur_pgc
);
1058 // ... (unimplemented)
1060 stream
->type
= STREAMTYPE_DVD
;
1061 stream
->sector_size
= 2048;
1062 stream
->flags
= STREAM_READ
| STREAM_SEEK
;
1063 stream
->fill_buffer
= fill_buffer
;
1064 stream
->seek
= seek
;
1065 stream
->control
= control
;
1066 stream
->close
= stream_dvd_close
;
1067 stream
->start_pos
= (off_t
)d
->cur_pack
*2048;
1068 stream
->end_pos
= (off_t
)(d
->cur_pgc
->cell_playback
[d
->last_cell
-1].last_sector
)*2048;
1069 *file_format
= DEMUXER_TYPE_MPEG_PS
;
1070 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
);
1071 stream
->priv
= (void*)d
;
1077 m_struct_free(&stream_opts
, opts
);
1078 return STREAM_UNSUPPORTED
;
1080 mp_msg(MSGT_DVD
,MSGL_ERR
,MSGTR_NoDVDSupport
);
1081 m_struct_free(&stream_opts
,opts
);
1082 return STREAM_UNSUPPORTED
;
1085 static int ifo_stream_open (stream_t
*stream
, int mode
, void *opts
, int *file_format
)
1089 struct stream_priv_s
*spriv
;
1091 ext
= strrchr (stream
->url
, '.');
1092 if (!ext
|| strcasecmp (ext
+ 1, "ifo"))
1093 return STREAM_UNSUPPORTED
;
1095 mp_msg(MSGT_DVD
, MSGL_INFO
, ".IFO detected. Redirecting to dvd://\n");
1097 filename
= strdup(basename(stream
->url
));
1099 spriv
=calloc(1, sizeof(struct stream_priv_s
));
1100 spriv
->device
= strdup(dirname(stream
->url
));
1101 if(!strncasecmp(filename
,"vts_",4))
1103 if(sscanf(filename
+3, "_%02d_", &spriv
->title
)!=1)
1110 stream
->url
=strdup("dvd://");
1112 return open_s(stream
, mode
, spriv
, file_format
);
1115 const stream_info_t stream_info_dvd
= {
1123 1 // Urls are an option string
1126 const stream_info_t stream_info_ifo
= {
1130 "Mostly used to play DVDs on disk through OSD Menu",
1132 { "file", "", NULL
},