2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #define FIRST_AC3_AID 128
33 #define FIRST_DTS_AID 136
34 #define FIRST_MPG_AID 0
35 #define FIRST_PCM_AID 160
41 #include "stream_dvd.h"
42 #include "stream_dvd_common.h"
43 #include "libmpdemux/demuxer.h"
45 static char* dvd_device_current
;
48 #define LIBDVDREAD_VERSION(maj,min,micro) ((maj)*10000 + (min)*100 + (micro))
50 * Try to autodetect the libdvd-0.9.0 library
51 * (0.9.0 removed the <dvdread/dvd_udf.h> header, and moved the two defines
52 * DVD_VIDEO_LB_LEN and MAX_UDF_FILE_NAME_LEN from it to
53 * <dvdread/dvd_reader.h>)
55 #ifndef DVDREAD_VERSION
56 #if defined(DVD_VIDEO_LB_LEN) && defined(MAX_UDF_FILE_NAME_LEN)
57 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,9,0)
59 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,8,0)
64 static struct stream_priv_s
{
67 } stream_priv_dflts
= {
72 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
74 static const m_option_t stream_opts_fields
[] = {
75 { "hostname", ST_OFF(title
), CONF_TYPE_INT
, M_OPT_RANGE
, 1, 99, NULL
},
76 { "filename", ST_OFF(device
), CONF_TYPE_STRING
, 0, 0 ,0, NULL
},
77 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
79 static const struct m_struct_st stream_opts
= {
81 sizeof(struct stream_priv_s
),
86 int dvd_parse_chapter_range(const m_option_t
*conf
, const char *range
) {
90 return M_OPT_MISSING_PARAM
;
94 if(*range
&& isdigit(*range
)) {
95 dvd_chapter
= strtol(range
, &s
, 10);
97 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
104 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
105 return M_OPT_INVALID
;
111 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
112 return M_OPT_INVALID
;
114 dvd_last_chapter
= strtol(s
, &t
, 10);
116 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Invalid chapter range specification %s\n", range
);
117 return M_OPT_INVALID
;
122 int dvd_chapter_from_cell(dvd_priv_t
* dvd
,int title
,int cell
)
128 if(title
< 0 || cell
< 0){
131 /* for most DVD's chapter == cell */
132 /* but there are more complecated cases... */
133 if(chapter
>= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
) {
134 chapter
= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
-1;
136 title
= dvd
->tt_srpt
->title
[title
].vts_ttn
-1;
137 ptt
= dvd
->vts_file
->vts_ptt_srpt
->title
[title
].ptt
;
138 while(chapter
>= 0) {
139 pgc_id
= ptt
[chapter
].pgcn
;
140 pgn
= ptt
[chapter
].pgn
;
141 cur_pgc
= dvd
->vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
142 if(cell
>= cur_pgc
->program_map
[pgn
-1]-1) {
147 /* didn't find a chapter ??? */
151 int dvd_lang_from_aid(stream_t
*stream
, int id
) {
154 if (!stream
) return 0;
157 for(i
=0;i
<d
->nr_of_channels
;i
++) {
158 if(d
->audio_streams
[i
].id
==id
)
159 return d
->audio_streams
[i
].language
;
164 int dvd_aid_from_lang(stream_t
*stream
, unsigned char* lang
) {
165 dvd_priv_t
*d
=stream
->priv
;
168 while(strlen(lang
)>=2) {
169 code
=lang
[1]|(lang
[0]<<8);
170 for(i
=0;i
<d
->nr_of_channels
;i
++) {
171 if(d
->audio_streams
[i
].language
==code
) {
172 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Selected DVD audio channel: %d language: %c%c\n",
173 d
->audio_streams
[i
].id
, lang
[0],lang
[1]);
174 return d
->audio_streams
[i
].id
;
176 //printf("%X != %X (%c%c)\n",code,d->audio_streams[i].language,lang[0],lang[1]);
178 lang
+=2; while (lang
[0]==',' || lang
[0]==' ') ++lang
;
180 mp_tmsg(MSGT_OPEN
,MSGL_WARN
,"No matching DVD audio language found!\n");
185 int dvd_number_of_subs(stream_t
*stream
) {
189 if (!stream
) return -1;
192 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
193 if (d
->subtitles
[i
].id
> maxid
) maxid
= d
->subtitles
[i
].id
;
197 int dvd_lang_from_sid(stream_t
*stream
, int id
) {
200 if (!stream
) return 0;
203 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
204 if (d
->subtitles
[i
].id
== id
&& d
->subtitles
[i
].language
) return d
->subtitles
[i
].language
;
208 int dvd_sid_from_lang(stream_t
*stream
, unsigned char* lang
) {
209 dvd_priv_t
*d
=stream
->priv
;
211 while(lang
&& strlen(lang
)>=2) {
212 code
=lang
[1]|(lang
[0]<<8);
213 for(i
=0;i
<d
->nr_of_subtitles
;i
++) {
214 if(d
->subtitles
[i
].language
==code
) {
215 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Selected DVD subtitle channel: %d language: %c%c\n", i
, lang
[0],lang
[1]);
216 return d
->subtitles
[i
].id
;
220 while (lang
[0]==',' || lang
[0]==' ') ++lang
;
222 mp_tmsg(MSGT_OPEN
,MSGL_WARN
,"No matching DVD subtitle language found!\n");
226 static int dvd_next_cell(dvd_priv_t
*d
) {
227 int next_cell
=d
->cur_cell
;
229 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next1=0x%X \n",next_cell
);
230 if( d
->cur_pgc
->cell_playback
[ next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
231 while(next_cell
<d
->last_cell
) {
232 if( d
->cur_pgc
->cell_playback
[next_cell
].block_mode
== BLOCK_MODE_LAST_CELL
)
237 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next2=0x%X \n",next_cell
);
240 if(next_cell
>=d
->last_cell
)
242 if(d
->cur_pgc
->cell_playback
[next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
243 next_cell
+=dvd_angle
;
244 if(next_cell
>=d
->last_cell
)
247 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next3=0x%X \n",next_cell
);
251 static int dvd_read_sector(dvd_priv_t
*d
,unsigned char* data
) {
254 if(d
->packs_left
==0) {
256 * If we're not at the end of this cell, we can determine the next
257 * VOBU to display using the VOBU_SRI information section of the
258 * DSI. Using this value correctly follows the current angle,
259 * avoiding the doubled scenes in The Matrix, and makes our life
262 * Otherwise, we set our next address past the end of this cell to
263 * force the code above to go to the next cell in the program.
265 if(d
->dsi_pack
.vobu_sri
.next_vobu
!= SRI_END_OF_CELL
) {
266 d
->cur_pack
= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+ ( d
->dsi_pack
.vobu_sri
.next_vobu
& 0x7fffffff );
267 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Navi new pos=0x%X \n",d
->cur_pack
);
269 // end of cell! find next cell!
270 mp_msg(MSGT_DVD
,MSGL_V
, "--- END OF CELL !!! ---\n");
271 d
->cur_pack
=d
->cell_last_pack
+1;
276 if(d
->cur_pack
>d
->cell_last_pack
) {
278 int next
=dvd_next_cell(d
);
281 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
282 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
283 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
284 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
285 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
);
290 len
= DVDReadBlocks(d
->title
, d
->cur_pack
, 1, data
);
291 // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss
292 if(len
<= 0) return -1; //error
294 if(data
[38]==0 && data
[39]==0 && data
[40]==1 && data
[41]==0xBF &&
295 data
[1024]==0 && data
[1025]==0 && data
[1026]==1 && data
[1027]==0xBF) {
296 // found a Navi packet!!!
297 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
298 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]));
300 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]), sizeof(dsi_t
));
302 if(d
->cur_pack
!= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
) {
303 mp_msg(MSGT_DVD
,MSGL_V
, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
304 d
->cur_pack
,d
->dsi_pack
.dsi_gi
.nv_pck_lbn
);
307 d
->packs_left
= d
->dsi_pack
.dsi_gi
.vobu_ea
;
308 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Found NAVI packet! lba=0x%X len=%d \n",d
->cur_pack
,d
->packs_left
);
309 //navPrint_DSI(&d->dsi_pack);
310 mp_msg(MSGT_DVD
,MSGL_DBG3
,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d
->cur_cell
,
311 d
->dsi_pack
.dsi_gi
.vobu_c_idn
,d
->dsi_pack
.dsi_gi
.vobu_vob_idn
,
312 d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
,
313 d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
);
317 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
318 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
319 // it generates incorrect code for unaligned access to a packed
320 // structure member, resulting in an mplayer crash with a SIGBUS
323 // See also gcc problem report PR c/7847:
324 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
325 for(i
=0;i
<9;i
++) { // check if all values zero:
326 __typeof__(d
->dsi_pack
.sml_agli
.data
[i
].address
) tmp_addr
;
327 memcpy(&tmp_addr
,&d
->dsi_pack
.sml_agli
.data
[i
].address
,sizeof(tmp_addr
));
328 if((skip
=tmp_addr
)!=0) break;
331 for(i
=0;i
<9;i
++) // check if all values zero:
332 if((skip
=d
->dsi_pack
.sml_agli
.data
[i
].address
)!=0) break;
334 if(skip
&& skip
!=0x7fffffff) {
335 // sml_agli table has valid data (at least one non-zero):
336 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
337 d
->dsi_pack
.sml_agli
.data
[dvd_angle
].address
;
340 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d
->cur_pack
);
342 // check if we're in the right cell, jump otherwise:
343 if( (d
->dsi_pack
.dsi_gi
.vobu_c_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
) &&
344 (d
->dsi_pack
.dsi_gi
.vobu_vob_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
) ){
346 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced by cell/vob IDN search! \n");
348 // wrong angle, skip this vobu:
349 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
350 d
->dsi_pack
.dsi_gi
.vobu_ea
;
351 d
->angle_seek
=2; // DEBUG
361 if(d
->packs_left
>=0) --d
->packs_left
;
364 if(d
->angle_seek
==2) mp_msg(MSGT_DVD
,MSGL_V
, "!!! warning! reading packet while angle_seek !!!\n");
365 goto read_next
; // searching for Navi packet
368 return d
->cur_pack
-1;
371 static void dvd_seek(dvd_priv_t
*d
,int pos
) {
375 // check if we stay in current cell (speedup things, and avoid angle skip)
376 if(d
->cur_pack
>d
->cell_last_pack
||
377 d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
379 // ok, cell change, find the right cell!
381 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
382 d
->cur_cell
+=dvd_angle
;
386 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
387 if(d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
388 d
->cur_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
391 if(d
->cur_pack
<=d
->cell_last_pack
) break; // ok, we find it! :)
392 next
=dvd_next_cell(d
);
394 //d->cur_pack=d->cell_last_pack+1;
395 break; // we're after the last cell
401 mp_msg(MSGT_DVD
,MSGL_V
, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
402 d
->cur_pack
,d
->cur_cell
,d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
,d
->cell_last_pack
);
404 // if we're in interleaved multi-angle cell, find the right angle chain!
405 // (read Navi block, and use the seamless angle jump table)
409 static void dvd_close(dvd_priv_t
*d
) {
410 ifoClose(d
->vts_file
);
411 ifoClose(d
->vmg_file
);
412 DVDCloseFile(d
->title
);
415 dvd_last_chapter
= 0;
416 dvd_set_speed(dvd_device_current
, -1); /* -1 => restore default */
419 static int fill_buffer(stream_t
*s
, char *but
, int len
)
421 if(s
->type
== STREAMTYPE_DVD
) {
422 off_t pos
=dvd_read_sector(s
->priv
,s
->buffer
);
424 len
=2048; // full sector
426 } else len
=-1; // error
431 static int seek(stream_t
*s
, off_t newpos
) {
432 s
->pos
=newpos
; // real seek
433 dvd_seek(s
->priv
,s
->pos
/2048);
437 static void stream_dvd_close(stream_t
*s
) {
441 static int mp_get_titleset_length(ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
)
443 int vts_ttn
; ///< title number within video title set
444 int pgc_no
; ///< program chain number
445 int msec
; ///< time length in milliseconds
448 if(!vts_file
|| !tt_srpt
)
451 if(vts_file
->vtsi_mat
&& vts_file
->vts_pgcit
)
453 vts_ttn
= tt_srpt
->title
[title_no
].vts_ttn
- 1;
454 pgc_no
= vts_file
->vts_ptt_srpt
->title
[vts_ttn
].ptt
[0].pgcn
- 1;
455 msec
= mp_dvdtimetomsec(&vts_file
->vts_pgcit
->pgci_srp
[pgc_no
].pgc
->playback_time
);
461 static int mp_describe_titleset(dvd_reader_t
*dvd
, tt_srpt_t
*tt_srpt
, int vts_no
)
463 ifo_handle_t
*vts_file
;
464 int title_no
, msec
=0;
466 vts_file
= ifoOpen(dvd
, vts_no
);
470 if(!vts_file
->vtsi_mat
|| !vts_file
->vts_pgcit
)
476 for(title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
478 if (tt_srpt
->title
[title_no
].title_set_nr
!= vts_no
)
480 msec
= mp_get_titleset_length(vts_file
, tt_srpt
, title_no
);
481 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no
+ 1, msec
/ 1000, msec
% 1000);
487 static int seek_to_chapter(stream_t
*stream
, ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
, int chapter
)
494 if(!vts_file
|| !tt_srpt
)
497 if(title_no
< 0 || title_no
>= tt_srpt
->nr_of_srpts
)
500 // map global title to vts title
501 title_no
= tt_srpt
->title
[title_no
].vts_ttn
- 1;
503 if(title_no
< 0 || title_no
>= vts_file
->vts_ptt_srpt
->nr_of_srpts
)
506 if(chapter
< 0 || chapter
> vts_file
->vts_ptt_srpt
->title
[title_no
].nr_of_ptts
-1) //no such chapter
509 ptt
= vts_file
->vts_ptt_srpt
->title
[title_no
].ptt
[chapter
];
510 pgc
= vts_file
->vts_pgcit
->pgci_srp
[ptt
.pgcn
-1].pgc
;
512 cell
= pgc
->program_map
[ptt
.pgn
- 1] - 1;
513 pos
= (off_t
) pgc
->cell_playback
[cell
].first_sector
* 2048;
514 mp_msg(MSGT_OPEN
,MSGL_V
,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64
"\n",
515 chapter
, pgc
->cell_playback
[cell
].first_sector
, pos
);
516 stream_seek(stream
, pos
);
521 static void list_chapters(pgc_t
*pgc
)
523 unsigned int i
, cell
;
524 unsigned int t
=0, t2
=0;
526 if(pgc
->nr_of_programs
< 2)
529 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "CHAPTERS: ");
530 for(i
=0; i
<pgc
->nr_of_programs
; i
++)
532 cell
= pgc
->program_map
[i
]; //here the cell is 1-based
534 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "%02d:%02d:%02d,", t2
/3600, (t2
/60)%60, t2
%60);
535 while(i
+1<pgc
->nr_of_programs
&& cell
< pgc
->program_map
[i
+1]) {
536 if(!(pgc
->cell_playback
[cell
-1].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
537 pgc
->cell_playback
[cell
-1].block_mode
!= BLOCK_MODE_FIRST_CELL
)
539 t
+= mp_dvdtimetomsec(&pgc
->cell_playback
[cell
-1].playback_time
);
543 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "\n");
546 static double dvd_get_current_time(stream_t
*stream
, int cell
)
549 dvd_priv_t
*d
= stream
->priv
;
552 if(!cell
) cell
=d
->cur_cell
;
553 for(i
=0; i
<d
->cur_cell
; i
++) {
554 if(d
->cur_pgc
->cell_playback
[i
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
555 d
->cur_pgc
->cell_playback
[i
].block_mode
!= BLOCK_MODE_FIRST_CELL
558 tm
+= d
->cell_times_table
[i
];
560 tm
+= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
562 return (double)tm
/1000.0;
565 static int dvd_seek_to_time(stream_t
*stream
, ifo_handle_t
*vts_file
, double sec
)
567 unsigned int i
, j
, k
, timeunit
, ac_time
, tmap_sector
=0, cell_sector
=0, vobu_sector
=0;
571 dvd_priv_t
*d
= stream
->priv
;
572 vts_tmapt_t
*vts_tmapt
= vts_file
->vts_tmapt
;
574 if(!vts_file
->vts_tmapt
|| sec
< 0)
577 duration
= (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1) / 1000.0f
;
582 timeunit
= vts_tmapt
->tmap
[i
].tmu
;
583 for(j
= 0; j
< vts_tmapt
->tmap
[i
].nr_of_entries
; j
++) {
584 ac_time
= timeunit
* (j
+ 1);
587 tmap_sector
= vts_tmapt
->tmap
[i
].map_ent
[j
] & 0x7fffffff;
589 //search enclosing cell
590 for(i
=0; i
<d
->cur_pgc
->nr_of_cells
; i
++) {
591 if(tmap_sector
>= d
->cur_pgc
->cell_playback
[i
].first_sector
&& tmap_sector
<= d
->cur_pgc
->cell_playback
[i
].last_sector
) {
592 cell_sector
= d
->cur_pgc
->cell_playback
[i
].first_sector
;
597 pos
= ((off_t
)cell_sector
)<<11;
598 stream_seek(stream
, pos
);
600 stream_skip(stream
, 2048);
601 t
= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
603 tm
= dvd_get_current_time(stream
, 0);
605 pos
= ((off_t
)tmap_sector
)<<11;
606 stream_seek(stream
, pos
);
607 //now get current time in terms of the cell+cell time offset
608 memset(&d
->dsi_pack
.dsi_gi
.c_eltm
, 0, sizeof(dvd_time_t
));
610 if(!stream_skip(stream
, 2048))
612 tm
= dvd_get_current_time(stream
, 0);
614 tmap_sector
= stream
->pos
>> 11;
616 //search closest VOBU sector
617 k
=(vts_file
->vts_vobu_admap
->last_byte
+ 1 - VOBU_ADMAP_SIZE
)/4; //entries in the vobu admap
619 if(vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
] > tmap_sector
)
622 vobu_sector
= vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
-1];
623 pos
= ((off_t
)vobu_sector
) << 11;
624 stream_seek(stream
, pos
);
629 static int control(stream_t
*stream
,int cmd
,void* arg
)
631 dvd_priv_t
*d
= stream
->priv
;
634 case STREAM_CTRL_GET_TIME_LENGTH
:
636 *((double *)arg
) = (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1)/1000.0;
639 case STREAM_CTRL_GET_NUM_CHAPTERS
:
641 if(! d
->cur_pgc
->nr_of_programs
) return STREAM_UNSUPPORTED
;
642 *((unsigned int *)arg
) = d
->cur_pgc
->nr_of_programs
;
645 case STREAM_CTRL_SEEK_TO_CHAPTER
:
648 r
= seek_to_chapter(stream
, d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1, *((unsigned int *)arg
));
649 if(! r
) return STREAM_UNSUPPORTED
;
653 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
655 *((unsigned int *)arg
) = dvd_chapter_from_cell(d
, d
->cur_title
-1, d
->cur_cell
);
658 case STREAM_CTRL_GET_CURRENT_TIME
:
661 tm
= dvd_get_current_time(stream
, 0);
663 *((double *)arg
) = tm
;
668 case STREAM_CTRL_SEEK_TO_TIME
:
670 if(dvd_seek_to_time(stream
, d
->vts_file
, *((double*)arg
)))
674 case STREAM_CTRL_GET_ASPECT_RATIO
:
676 *((double *)arg
) = !d
->vts_file
->vtsi_mat
->vts_video_attr
.display_aspect_ratio
? 4.0/3.0 : 16.0/9.0;
679 case STREAM_CTRL_GET_NUM_ANGLES
:
681 *((int *)arg
) = d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
;
684 case STREAM_CTRL_GET_ANGLE
:
686 *((int *)arg
) = dvd_angle
+1;
689 case STREAM_CTRL_SET_ANGLE
:
691 int ang
= *((int *)arg
);
692 if(ang
>d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
|| ang
<=0)
699 return STREAM_UNSUPPORTED
;
703 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
704 struct stream_priv_s
* p
= (struct stream_priv_s
*)opts
;
707 mp_msg(MSGT_OPEN
,MSGL_V
,"URL: %s\n", stream
->url
);
708 dvd_title
= p
->title
;
715 ifo_handle_t
*vmg_file
;
717 ifo_handle_t
*vts_file
;
723 dvd_device_current
= p
->device
;
725 dvd_device_current
= dvd_device
;
727 dvd_device_current
= DEFAULT_DVD_DEVICE
;
728 dvd_set_speed(dvd_device_current
, dvd_speed
);
729 #if defined(__APPLE__) || defined(__DARWIN__)
730 /* Dynamic DVD drive selection on Darwin */
731 if(!strcmp(dvd_device_current
, "/dev/rdiskN")) {
733 size_t len
= strlen(dvd_device_current
)+1;
734 char *temp_device
= malloc(len
);
736 for (i
= 1; i
< 10; i
++) {
737 snprintf(temp_device
, len
, "/dev/rdisk%d", i
);
738 dvd
= DVDOpen(temp_device
);
740 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",temp_device
, strerror(errno
));
742 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
743 dvd_file_t
*dvdfile
= DVDOpenFile(dvd
,dvd_title
,DVD_READ_INFO_FILE
);
745 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",temp_device
, strerror(errno
));
749 DVDCloseFile(dvdfile
);
757 m_struct_free(&stream_opts
,opts
);
758 return STREAM_UNSUPPORTED
;
761 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
763 dvd
= DVDOpen(dvd_device_current
);
765 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",dvd_device_current
, strerror(errno
));
766 m_struct_free(&stream_opts
,opts
);
767 return STREAM_UNSUPPORTED
;
771 mp_msg(MSGT_OPEN
,MSGL_V
,"Reading disc structure, please wait...\n");
774 * Load the video manager to find out the information about the titles on
777 vmg_file
= ifoOpen(dvd
, 0);
779 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Can't open VMG info!\n");
781 m_struct_free(&stream_opts
,opts
);
782 return STREAM_UNSUPPORTED
;
784 tt_srpt
= vmg_file
->tt_srpt
;
785 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
))
787 int title_no
; ///< title number
788 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLES=%d\n", tt_srpt
->nr_of_srpts
);
789 for (title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
791 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_ptts
);
792 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_angles
);
795 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_V
))
798 unsigned char discid
[16]; ///< disk ID, a 128 bit MD5 sum
799 int vts_no
; ///< video title set number
800 for (vts_no
= 1; vts_no
<= vmg_file
->vts_atrt
->nr_of_vtss
; vts_no
++)
801 mp_describe_titleset(dvd
, tt_srpt
, vts_no
);
802 if (DVDDiscID(dvd
, discid
) >= 0)
805 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_DISC_ID=");
806 for (i
= 0; i
< 16; i
++)
807 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "%02X", discid
[i
]);
808 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "\n");
810 if (DVDUDFVolumeInfo(dvd
, volid
, sizeof(volid
), NULL
, 0) >= 0 || DVDISOVolumeInfo(dvd
, volid
, sizeof(volid
), NULL
, 0) >= 0)
811 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_VOLUME_ID=%s\n", volid
);
814 * Make sure our title number is valid.
816 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
, "There are %d titles on this DVD.\n", tt_srpt
->nr_of_srpts
);
817 if(dvd_title
< 1 || dvd_title
> tt_srpt
->nr_of_srpts
) {
818 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Invalid DVD title number: %d\n", dvd_title
);
819 ifoClose( vmg_file
);
821 m_struct_free(&stream_opts
,opts
);
822 return STREAM_UNSUPPORTED
;
824 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title
);
825 --dvd_title
; // remap 1.. -> 0..
827 * Make sure the angle number is valid for this title.
829 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
, "There are %d angles in this DVD title.\n", tt_srpt
->title
[dvd_title
].nr_of_angles
);
830 if(dvd_angle
<1 || dvd_angle
>tt_srpt
->title
[dvd_title
].nr_of_angles
) {
831 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Invalid DVD angle number: %d\n", dvd_angle
);
834 --dvd_angle
; // remap 1.. -> 0..
836 ttn
= tt_srpt
->title
[dvd_title
].vts_ttn
- 1;
838 * Load the VTS information for the title set our title is in.
840 vts_file
= ifoOpen( dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
);
842 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Cannot open the IFO file for DVD title %d.\n", tt_srpt
->title
[dvd_title
].title_set_nr
);
846 * We've got enough info, time to open the title set data.
848 title
= DVDOpenFile(dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
, DVD_READ_TITLE_VOBS
);
850 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Cannot open title VOBS (VTS_%02d_1.VOB).\n", tt_srpt
->title
[dvd_title
].title_set_nr
);
851 ifoClose( vts_file
);
855 mp_msg(MSGT_OPEN
,MSGL_V
, "DVD successfully opened.\n");
857 d
=malloc(sizeof(dvd_priv_t
)); memset(d
,0,sizeof(dvd_priv_t
));
860 d
->vmg_file
=vmg_file
;
862 d
->vts_file
=vts_file
;
863 d
->cur_title
= dvd_title
+1;
865 pgc
= vts_file
->vts_pgcit
? vts_file
->vts_pgcit
->pgci_srp
[ttn
].pgc
: NULL
;
867 * Check number of audio channels and types
871 if(vts_file
->vts_pgcit
) {
874 if(pgc
->audio_control
[i
] & 0x8000) {
875 audio_attr_t
* audio
= &vts_file
->vtsi_mat
->vts_audio_attr
[i
];
877 char tmp
[] = "unknown";
878 stream_language_t
*audio_stream
= &d
->audio_streams
[d
->nr_of_channels
];
880 if(audio
->lang_type
== 1) {
881 language
=audio
->lang_code
;
883 tmp
[1]=language
&0xff;
887 audio_stream
->language
=language
;
888 audio_stream
->id
=pgc
->audio_control
[i
] >> 8 & 7;
889 switch(audio
->audio_format
) {
891 audio_stream
->id
+=FIRST_AC3_AID
;
894 audio_stream
->id
+=FIRST_DTS_AID
;
896 case 2: // mpeg layer 1/2/3
898 audio_stream
->id
+=FIRST_MPG_AID
;
901 audio_stream
->id
+=FIRST_PCM_AID
;
905 audio_stream
->type
=audio
->audio_format
;
906 // Pontscho: to my mind, tha channels:
909 audio_stream
->channels
=audio
->channels
;
910 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"audio stream: %d format: %s (%s) language: %s aid: %d.\n",
912 dvd_audio_stream_types
[ audio
->audio_format
],
913 dvd_audio_stream_channels
[ audio
->channels
],
917 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", audio_stream
->id
);
918 if(language
&& tmp
[0])
919 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", audio_stream
->id
, tmp
);
924 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"number of audio channels on disk: %d.\n",d
->nr_of_channels
);
928 * Check number of subtitles and language
933 d
->nr_of_subtitles
=0;
935 if(pgc
->subp_control
[i
] & 0x80000000) {
936 subp_attr_t
* subtitle
= &vts_file
->vtsi_mat
->vts_subp_attr
[i
];
937 video_attr_t
*video
= &vts_file
->vtsi_mat
->vts_video_attr
;
939 char tmp
[] = "unknown";
940 stream_language_t
*sub_stream
= &d
->subtitles
[d
->nr_of_subtitles
];
942 if(subtitle
->type
== 1) {
943 language
=subtitle
->lang_code
;
945 tmp
[1]=language
&0xff;
949 sub_stream
->language
=language
;
950 sub_stream
->id
=d
->nr_of_subtitles
;
951 if(video
->display_aspect_ratio
== 0) /* 4:3 */
952 sub_stream
->id
= pgc
->subp_control
[i
] >> 24 & 31;
953 else if(video
->display_aspect_ratio
== 3) /* 16:9 */
954 sub_stream
->id
= pgc
->subp_control
[i
] >> 8 & 31;
956 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"subtitle ( sid ): %d language: %s\n", sub_stream
->id
, tmp
);
957 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sub_stream
->id
);
958 if(language
&& tmp
[0])
959 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sub_stream
->id
, tmp
);
960 d
->nr_of_subtitles
++;
962 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"number of subtitles on disk: %d\n",d
->nr_of_subtitles
);
966 * Determine which program chain we want to watch. This is based on the
969 pgc_id
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgcn
; // local
970 pgn
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgn
; // local
971 d
->cur_pgc_idx
= pgc_id
-1;
972 d
->cur_pgc
= vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
973 d
->cur_cell
= d
->cur_pgc
->program_map
[pgn
-1] - 1; // start playback here
974 d
->packs_left
=-1; // for Navi stuff
976 d
->last_cell
=d
->cur_pgc
->nr_of_cells
;
978 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
979 d
->cur_cell
+=dvd_angle
;
980 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
981 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
982 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
);
984 //assign cell_times_table
985 d
->cell_times_table
= malloc(sizeof(unsigned int) * d
->cur_pgc
->nr_of_cells
);
986 if(d
->cell_times_table
== NULL
)
987 return STREAM_UNSUPPORTED
;
988 for(k
=0; k
<d
->cur_pgc
->nr_of_cells
; k
++)
989 d
->cell_times_table
[k
] = mp_dvdtimetomsec(&d
->cur_pgc
->cell_playback
[k
].playback_time
);
990 list_chapters(d
->cur_pgc
);
992 // ... (unimplemented)
994 stream
->type
= STREAMTYPE_DVD
;
995 stream
->sector_size
= 2048;
996 stream
->flags
= STREAM_READ
| MP_STREAM_SEEK
;
997 stream
->fill_buffer
= fill_buffer
;
999 stream
->control
= control
;
1000 stream
->close
= stream_dvd_close
;
1001 stream
->start_pos
= (off_t
)d
->cur_pack
*2048;
1002 stream
->end_pos
= (off_t
)(d
->cur_pgc
->cell_playback
[d
->last_cell
-1].last_sector
)*2048;
1003 *file_format
= DEMUXER_TYPE_MPEG_PS
;
1004 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
);
1005 stream
->priv
= (void*)d
;
1011 m_struct_free(&stream_opts
, opts
);
1012 return STREAM_UNSUPPORTED
;
1014 mp_tmsg(MSGT_DVD
,MSGL_ERR
,"MPlayer was compiled without DVD support, exiting.\n");
1015 m_struct_free(&stream_opts
,opts
);
1016 return STREAM_UNSUPPORTED
;
1019 static int ifo_stream_open (stream_t
*stream
, int mode
, void *opts
, int *file_format
)
1022 struct stream_priv_s
*spriv
;
1023 int len
= strlen(stream
->url
);
1025 if (len
< 4 || strcasecmp (stream
->url
+ len
- 4, ".ifo"))
1026 return STREAM_UNSUPPORTED
;
1028 mp_msg(MSGT_DVD
, MSGL_INFO
, ".IFO detected. Redirecting to dvd://\n");
1030 filename
= strdup(basename(stream
->url
));
1032 spriv
=calloc(1, sizeof(struct stream_priv_s
));
1033 spriv
->device
= strdup(dirname(stream
->url
));
1034 if(!strncasecmp(filename
,"vts_",4))
1036 if(sscanf(filename
+3, "_%02d_", &spriv
->title
)!=1)
1043 stream
->url
=strdup("dvd://");
1045 return open_s(stream
, mode
, spriv
, file_format
);
1048 const stream_info_t stream_info_dvd
= {
1056 1 // Urls are an option string
1059 const stream_info_t stream_info_ifo
= {
1063 "Mostly used to play DVDs on disk through OSD Menu",
1065 { "file", "", NULL
},