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_chapter_from_cell(dvd_priv_t
* dvd
,int title
,int cell
)
92 if(title
< 0 || cell
< 0){
95 /* for most DVD's chapter == cell */
96 /* but there are more complecated cases... */
97 if(chapter
>= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
) {
98 chapter
= dvd
->vmg_file
->tt_srpt
->title
[title
].nr_of_ptts
-1;
100 title
= dvd
->tt_srpt
->title
[title
].vts_ttn
-1;
101 ptt
= dvd
->vts_file
->vts_ptt_srpt
->title
[title
].ptt
;
102 while(chapter
>= 0) {
103 pgc_id
= ptt
[chapter
].pgcn
;
104 pgn
= ptt
[chapter
].pgn
;
105 cur_pgc
= dvd
->vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
106 if(cell
>= cur_pgc
->program_map
[pgn
-1]-1) {
111 /* didn't find a chapter ??? */
115 int dvd_lang_from_aid(stream_t
*stream
, int id
) {
118 if (!stream
) return 0;
121 for(i
=0;i
<d
->nr_of_channels
;i
++) {
122 if(d
->audio_streams
[i
].id
==id
)
123 return d
->audio_streams
[i
].language
;
128 int dvd_aid_from_lang(stream_t
*stream
, unsigned char* lang
) {
129 dvd_priv_t
*d
=stream
->priv
;
132 while(strlen(lang
)>=2) {
133 code
=lang
[1]|(lang
[0]<<8);
134 for(i
=0;i
<d
->nr_of_channels
;i
++) {
135 if(d
->audio_streams
[i
].language
==code
) {
136 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Selected DVD audio channel: %d language: %c%c\n",
137 d
->audio_streams
[i
].id
, lang
[0],lang
[1]);
138 return d
->audio_streams
[i
].id
;
140 //printf("%X != %X (%c%c)\n",code,d->audio_streams[i].language,lang[0],lang[1]);
142 lang
+=2; while (lang
[0]==',' || lang
[0]==' ') ++lang
;
144 mp_tmsg(MSGT_OPEN
,MSGL_WARN
,"No matching DVD audio language found!\n");
149 int dvd_number_of_subs(stream_t
*stream
) {
153 if (!stream
) return -1;
156 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
157 if (d
->subtitles
[i
].id
> maxid
) maxid
= d
->subtitles
[i
].id
;
161 int dvd_lang_from_sid(stream_t
*stream
, int id
) {
164 if (!stream
) return 0;
167 for (i
= 0; i
< d
->nr_of_subtitles
; i
++)
168 if (d
->subtitles
[i
].id
== id
&& d
->subtitles
[i
].language
) return d
->subtitles
[i
].language
;
172 int dvd_sid_from_lang(stream_t
*stream
, unsigned char* lang
) {
173 dvd_priv_t
*d
=stream
->priv
;
175 while(lang
&& strlen(lang
)>=2) {
176 code
=lang
[1]|(lang
[0]<<8);
177 for(i
=0;i
<d
->nr_of_subtitles
;i
++) {
178 if(d
->subtitles
[i
].language
==code
) {
179 mp_tmsg(MSGT_OPEN
,MSGL_INFO
,"Selected DVD subtitle channel: %d language: %c%c\n", i
, lang
[0],lang
[1]);
180 return d
->subtitles
[i
].id
;
184 while (lang
[0]==',' || lang
[0]==' ') ++lang
;
186 mp_tmsg(MSGT_OPEN
,MSGL_WARN
,"No matching DVD subtitle language found!\n");
190 static int dvd_next_cell(dvd_priv_t
*d
) {
191 int next_cell
=d
->cur_cell
;
193 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next1=0x%X \n",next_cell
);
194 if( d
->cur_pgc
->cell_playback
[ next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
195 while(next_cell
<d
->last_cell
) {
196 if( d
->cur_pgc
->cell_playback
[next_cell
].block_mode
== BLOCK_MODE_LAST_CELL
)
201 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next2=0x%X \n",next_cell
);
204 if(next_cell
>=d
->last_cell
)
206 if(d
->cur_pgc
->cell_playback
[next_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
) {
207 next_cell
+=dvd_angle
;
208 if(next_cell
>=d
->last_cell
)
211 mp_msg(MSGT_DVD
,MSGL_DBG2
, "dvd_next_cell: next3=0x%X \n",next_cell
);
215 static int dvd_read_sector(dvd_priv_t
*d
, unsigned char *data
)
219 if(d
->packs_left
==0) {
221 * If we're not at the end of this cell, we can determine the next
222 * VOBU to display using the VOBU_SRI information section of the
223 * DSI. Using this value correctly follows the current angle,
224 * avoiding the doubled scenes in The Matrix, and makes our life
227 * Otherwise, we set our next address past the end of this cell to
228 * force the code above to go to the next cell in the program.
230 if(d
->dsi_pack
.vobu_sri
.next_vobu
!= SRI_END_OF_CELL
) {
231 d
->cur_pack
= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+ ( d
->dsi_pack
.vobu_sri
.next_vobu
& 0x7fffffff );
232 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Navi new pos=0x%X \n",d
->cur_pack
);
234 // end of cell! find next cell!
235 mp_msg(MSGT_DVD
,MSGL_V
, "--- END OF CELL !!! ---\n");
236 d
->cur_pack
=d
->cell_last_pack
+1;
241 if(d
->cur_pack
>d
->cell_last_pack
) {
243 int next
=dvd_next_cell(d
);
246 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
247 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
248 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
249 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
250 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
);
255 len
= DVDReadBlocks(d
->title
, d
->cur_pack
, 1, data
);
256 // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss
257 if(len
<= 0) return -1; //error
259 if(data
[38]==0 && data
[39]==0 && data
[40]==1 && data
[41]==0xBF &&
260 data
[1024]==0 && data
[1025]==0 && data
[1026]==1 && data
[1027]==0xBF) {
261 // found a Navi packet!!!
262 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
263 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]));
265 navRead_DSI(&d
->dsi_pack
, &(data
[ DSI_START_BYTE
]), sizeof(dsi_t
));
267 if(d
->cur_pack
!= d
->dsi_pack
.dsi_gi
.nv_pck_lbn
) {
268 mp_msg(MSGT_DVD
,MSGL_V
, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
269 d
->cur_pack
,d
->dsi_pack
.dsi_gi
.nv_pck_lbn
);
272 d
->packs_left
= d
->dsi_pack
.dsi_gi
.vobu_ea
;
273 mp_msg(MSGT_DVD
,MSGL_DBG2
, "Found NAVI packet! lba=0x%X len=%d \n",d
->cur_pack
,d
->packs_left
);
274 //navPrint_DSI(&d->dsi_pack);
275 mp_msg(MSGT_DVD
,MSGL_DBG3
,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d
->cur_cell
,
276 d
->dsi_pack
.dsi_gi
.vobu_c_idn
,d
->dsi_pack
.dsi_gi
.vobu_vob_idn
,
277 d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
,
278 d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
);
282 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
283 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
284 // it generates incorrect code for unaligned access to a packed
285 // structure member, resulting in an mplayer crash with a SIGBUS
288 // See also gcc problem report PR c/7847:
289 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
290 for(i
=0;i
<9;i
++) { // check if all values zero:
291 __typeof__(d
->dsi_pack
.sml_agli
.data
[i
].address
) tmp_addr
;
292 memcpy(&tmp_addr
,&d
->dsi_pack
.sml_agli
.data
[i
].address
,sizeof(tmp_addr
));
293 if((skip
=tmp_addr
)!=0) break;
296 for(i
=0;i
<9;i
++) // check if all values zero:
297 if((skip
=d
->dsi_pack
.sml_agli
.data
[i
].address
)!=0) break;
299 if(skip
&& skip
!=0x7fffffff) {
300 // sml_agli table has valid data (at least one non-zero):
301 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
302 d
->dsi_pack
.sml_agli
.data
[dvd_angle
].address
;
305 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d
->cur_pack
);
307 // check if we're in the right cell, jump otherwise:
308 if( (d
->dsi_pack
.dsi_gi
.vobu_c_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].cell_nr
) &&
309 (d
->dsi_pack
.dsi_gi
.vobu_vob_idn
==d
->cur_pgc
->cell_position
[d
->cur_cell
].vob_id_nr
) ){
311 mp_msg(MSGT_DVD
,MSGL_V
, "Angle-seek synced by cell/vob IDN search! \n");
313 // wrong angle, skip this vobu:
314 d
->cur_pack
=d
->dsi_pack
.dsi_gi
.nv_pck_lbn
+
315 d
->dsi_pack
.dsi_gi
.vobu_ea
;
316 d
->angle_seek
=2; // DEBUG
326 if(d
->packs_left
>=0) --d
->packs_left
;
329 if(d
->angle_seek
==2) mp_msg(MSGT_DVD
,MSGL_V
, "!!! warning! reading packet while angle_seek !!!\n");
330 goto read_next
; // searching for Navi packet
333 return d
->cur_pack
-1;
336 static void dvd_seek(dvd_priv_t
*d
, int pos
)
341 // check if we stay in current cell (speedup things, and avoid angle skip)
342 if(d
->cur_pack
>d
->cell_last_pack
||
343 d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
345 // ok, cell change, find the right cell!
347 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
348 d
->cur_cell
+=dvd_angle
;
352 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
353 if(d
->cur_pack
<d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
) {
354 d
->cur_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
357 if(d
->cur_pack
<=d
->cell_last_pack
) break; // ok, we find it! :)
358 next
=dvd_next_cell(d
);
360 //d->cur_pack=d->cell_last_pack+1;
361 break; // we're after the last cell
367 mp_msg(MSGT_DVD
,MSGL_V
, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
368 d
->cur_pack
,d
->cur_cell
,d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
,d
->cell_last_pack
);
370 // if we're in interleaved multi-angle cell, find the right angle chain!
371 // (read Navi block, and use the seamless angle jump table)
375 static void dvd_close(dvd_priv_t
*d
)
377 ifoClose(d
->vts_file
);
378 ifoClose(d
->vmg_file
);
379 DVDCloseFile(d
->title
);
381 dvd_set_speed(dvd_device_current
, -1); /* -1 => restore default */
384 static int fill_buffer(stream_t
*s
, char *but
, int len
)
386 if(s
->type
== STREAMTYPE_DVD
) {
387 off_t pos
=dvd_read_sector(s
->priv
,s
->buffer
);
389 len
=2048; // full sector
391 } else len
=-1; // error
396 static int seek(stream_t
*s
, off_t newpos
) {
397 s
->pos
=newpos
; // real seek
398 dvd_seek(s
->priv
,s
->pos
/2048);
402 static void stream_dvd_close(stream_t
*s
) {
406 static int mp_get_titleset_length(ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
)
408 int vts_ttn
; ///< title number within video title set
409 int pgc_no
; ///< program chain number
410 int msec
; ///< time length in milliseconds
413 if(!vts_file
|| !tt_srpt
)
416 if(vts_file
->vtsi_mat
&& vts_file
->vts_pgcit
)
418 vts_ttn
= tt_srpt
->title
[title_no
].vts_ttn
- 1;
419 pgc_no
= vts_file
->vts_ptt_srpt
->title
[vts_ttn
].ptt
[0].pgcn
- 1;
420 msec
= mp_dvdtimetomsec(&vts_file
->vts_pgcit
->pgci_srp
[pgc_no
].pgc
->playback_time
);
426 static int mp_describe_titleset(dvd_reader_t
*dvd
, tt_srpt_t
*tt_srpt
, int vts_no
)
428 ifo_handle_t
*vts_file
;
429 int title_no
, msec
=0;
431 vts_file
= ifoOpen(dvd
, vts_no
);
435 if(!vts_file
->vtsi_mat
|| !vts_file
->vts_pgcit
)
441 for(title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
443 if (tt_srpt
->title
[title_no
].title_set_nr
!= vts_no
)
445 msec
= mp_get_titleset_length(vts_file
, tt_srpt
, title_no
);
446 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no
+ 1, msec
/ 1000, msec
% 1000);
452 static int seek_to_chapter(stream_t
*stream
, ifo_handle_t
*vts_file
, tt_srpt_t
*tt_srpt
, int title_no
, int chapter
)
459 if(!vts_file
|| !tt_srpt
)
462 if(title_no
< 0 || title_no
>= tt_srpt
->nr_of_srpts
)
465 // map global title to vts title
466 title_no
= tt_srpt
->title
[title_no
].vts_ttn
- 1;
468 if(title_no
< 0 || title_no
>= vts_file
->vts_ptt_srpt
->nr_of_srpts
)
471 if(chapter
< 0 || chapter
> vts_file
->vts_ptt_srpt
->title
[title_no
].nr_of_ptts
-1) //no such chapter
474 ptt
= vts_file
->vts_ptt_srpt
->title
[title_no
].ptt
[chapter
];
475 pgc
= vts_file
->vts_pgcit
->pgci_srp
[ptt
.pgcn
-1].pgc
;
477 cell
= pgc
->program_map
[ptt
.pgn
- 1] - 1;
478 pos
= (off_t
) pgc
->cell_playback
[cell
].first_sector
* 2048;
479 mp_msg(MSGT_OPEN
,MSGL_V
,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64
"\n",
480 chapter
, pgc
->cell_playback
[cell
].first_sector
, pos
);
481 stream_seek(stream
, pos
);
486 static void list_chapters(pgc_t
*pgc
)
488 unsigned int i
, cell
;
489 unsigned int t
=0, t2
=0;
491 if(pgc
->nr_of_programs
< 2)
494 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "CHAPTERS: ");
495 for(i
=0; i
<pgc
->nr_of_programs
; i
++)
497 cell
= pgc
->program_map
[i
]; //here the cell is 1-based
499 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "%02d:%02d:%02d,", t2
/3600, (t2
/60)%60, t2
%60);
500 while(i
+1<pgc
->nr_of_programs
&& cell
< pgc
->program_map
[i
+1]) {
501 if(!(pgc
->cell_playback
[cell
-1].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
502 pgc
->cell_playback
[cell
-1].block_mode
!= BLOCK_MODE_FIRST_CELL
)
504 t
+= mp_dvdtimetomsec(&pgc
->cell_playback
[cell
-1].playback_time
);
508 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "\n");
511 static double dvd_get_current_time(stream_t
*stream
, int cell
)
514 dvd_priv_t
*d
= stream
->priv
;
517 if(!cell
) cell
=d
->cur_cell
;
518 for(i
=0; i
<d
->cur_cell
; i
++) {
519 if(d
->cur_pgc
->cell_playback
[i
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
&&
520 d
->cur_pgc
->cell_playback
[i
].block_mode
!= BLOCK_MODE_FIRST_CELL
523 tm
+= d
->cell_times_table
[i
];
525 tm
+= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
527 return (double)tm
/1000.0;
530 static int dvd_seek_to_time(stream_t
*stream
, ifo_handle_t
*vts_file
, double sec
)
532 unsigned int i
, j
, k
, timeunit
, ac_time
, tmap_sector
=0, cell_sector
=0, vobu_sector
=0;
536 dvd_priv_t
*d
= stream
->priv
;
537 vts_tmapt_t
*vts_tmapt
= vts_file
->vts_tmapt
;
539 if(!vts_file
->vts_tmapt
|| sec
< 0)
542 duration
= (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1) / 1000.0f
;
547 timeunit
= vts_tmapt
->tmap
[i
].tmu
;
548 for(j
= 0; j
< vts_tmapt
->tmap
[i
].nr_of_entries
; j
++) {
549 ac_time
= timeunit
* (j
+ 1);
552 tmap_sector
= vts_tmapt
->tmap
[i
].map_ent
[j
] & 0x7fffffff;
554 //search enclosing cell
555 for(i
=0; i
<d
->cur_pgc
->nr_of_cells
; i
++) {
556 if(tmap_sector
>= d
->cur_pgc
->cell_playback
[i
].first_sector
&& tmap_sector
<= d
->cur_pgc
->cell_playback
[i
].last_sector
) {
557 cell_sector
= d
->cur_pgc
->cell_playback
[i
].first_sector
;
562 pos
= ((off_t
)cell_sector
)<<11;
563 stream_seek(stream
, pos
);
565 stream_skip(stream
, 2048);
566 t
= mp_dvdtimetomsec(&d
->dsi_pack
.dsi_gi
.c_eltm
);
568 tm
= dvd_get_current_time(stream
, 0);
570 pos
= ((off_t
)tmap_sector
)<<11;
571 stream_seek(stream
, pos
);
572 //now get current time in terms of the cell+cell time offset
573 memset(&d
->dsi_pack
.dsi_gi
.c_eltm
, 0, sizeof(dvd_time_t
));
575 if(!stream_skip(stream
, 2048))
577 tm
= dvd_get_current_time(stream
, 0);
579 tmap_sector
= stream
->pos
>> 11;
581 //search closest VOBU sector
582 k
=(vts_file
->vts_vobu_admap
->last_byte
+ 1 - VOBU_ADMAP_SIZE
)/4; //entries in the vobu admap
584 if(vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
] > tmap_sector
)
587 vobu_sector
= vts_file
->vts_vobu_admap
->vobu_start_sectors
[i
-1];
588 pos
= ((off_t
)vobu_sector
) << 11;
589 stream_seek(stream
, pos
);
594 static int control(stream_t
*stream
,int cmd
,void* arg
)
596 dvd_priv_t
*d
= stream
->priv
;
599 case STREAM_CTRL_GET_TIME_LENGTH
:
601 *((double *)arg
) = (double) mp_get_titleset_length(d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1)/1000.0;
604 case STREAM_CTRL_GET_NUM_CHAPTERS
:
606 if(! d
->cur_pgc
->nr_of_programs
) return STREAM_UNSUPPORTED
;
607 *((unsigned int *)arg
) = d
->cur_pgc
->nr_of_programs
;
610 case STREAM_CTRL_SEEK_TO_CHAPTER
:
613 r
= seek_to_chapter(stream
, d
->vts_file
, d
->tt_srpt
, d
->cur_title
-1, *((unsigned int *)arg
));
614 if(! r
) return STREAM_UNSUPPORTED
;
618 case STREAM_CTRL_GET_CURRENT_CHAPTER
:
620 *((unsigned int *)arg
) = dvd_chapter_from_cell(d
, d
->cur_title
-1, d
->cur_cell
);
623 case STREAM_CTRL_GET_CURRENT_TIME
:
626 tm
= dvd_get_current_time(stream
, 0);
628 *((double *)arg
) = tm
;
633 case STREAM_CTRL_SEEK_TO_TIME
:
635 if(dvd_seek_to_time(stream
, d
->vts_file
, *((double*)arg
)))
639 case STREAM_CTRL_GET_ASPECT_RATIO
:
641 *((double *)arg
) = !d
->vts_file
->vtsi_mat
->vts_video_attr
.display_aspect_ratio
? 4.0/3.0 : 16.0/9.0;
644 case STREAM_CTRL_GET_NUM_ANGLES
:
646 *((int *)arg
) = d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
;
649 case STREAM_CTRL_GET_ANGLE
:
651 *((int *)arg
) = dvd_angle
+1;
654 case STREAM_CTRL_SET_ANGLE
:
656 int ang
= *((int *)arg
);
657 if(ang
>d
->vmg_file
->tt_srpt
->title
[dvd_title
].nr_of_angles
|| ang
<=0)
664 return STREAM_UNSUPPORTED
;
668 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
669 struct stream_priv_s
* p
= (struct stream_priv_s
*)opts
;
672 mp_msg(MSGT_OPEN
,MSGL_V
,"URL: %s\n", stream
->url
);
673 dvd_title
= p
->title
;
680 ifo_handle_t
*vmg_file
;
682 ifo_handle_t
*vts_file
;
688 dvd_device_current
= p
->device
;
690 dvd_device_current
= dvd_device
;
692 dvd_device_current
= DEFAULT_DVD_DEVICE
;
693 dvd_set_speed(dvd_device_current
, dvd_speed
);
694 #if defined(__APPLE__) || defined(__DARWIN__)
695 /* Dynamic DVD drive selection on Darwin */
696 if(!strcmp(dvd_device_current
, "/dev/rdiskN")) {
698 size_t len
= strlen(dvd_device_current
)+1;
699 char *temp_device
= malloc(len
);
701 for (i
= 1; i
< 10; i
++) {
702 snprintf(temp_device
, len
, "/dev/rdisk%d", i
);
703 dvd
= DVDOpen(temp_device
);
705 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",temp_device
, strerror(errno
));
707 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
708 dvd_file_t
*dvdfile
= DVDOpenFile(dvd
,dvd_title
,DVD_READ_INFO_FILE
);
710 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",temp_device
, strerror(errno
));
714 DVDCloseFile(dvdfile
);
722 m_struct_free(&stream_opts
,opts
);
723 return STREAM_UNSUPPORTED
;
726 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
728 dvd
= DVDOpen(dvd_device_current
);
730 mp_tmsg(MSGT_OPEN
,MSGL_ERR
,"Couldn't open DVD device: %s (%s)\n",dvd_device_current
, strerror(errno
));
731 m_struct_free(&stream_opts
,opts
);
732 return STREAM_UNSUPPORTED
;
736 mp_msg(MSGT_OPEN
,MSGL_V
,"Reading disc structure, please wait...\n");
739 * Load the video manager to find out the information about the titles on
742 vmg_file
= ifoOpen(dvd
, 0);
744 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Can't open VMG info!\n");
746 m_struct_free(&stream_opts
,opts
);
747 return STREAM_UNSUPPORTED
;
749 tt_srpt
= vmg_file
->tt_srpt
;
750 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
))
752 int title_no
; ///< title number
753 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLES=%d\n", tt_srpt
->nr_of_srpts
);
754 for (title_no
= 0; title_no
< tt_srpt
->nr_of_srpts
; title_no
++)
756 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_ptts
);
757 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no
+ 1, tt_srpt
->title
[title_no
].nr_of_angles
);
760 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_V
))
763 unsigned char discid
[16]; ///< disk ID, a 128 bit MD5 sum
764 int vts_no
; ///< video title set number
765 for (vts_no
= 1; vts_no
<= vmg_file
->vts_atrt
->nr_of_vtss
; vts_no
++)
766 mp_describe_titleset(dvd
, tt_srpt
, vts_no
);
767 if (DVDDiscID(dvd
, discid
) >= 0)
770 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_DISC_ID=");
771 for (i
= 0; i
< 16; i
++)
772 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "%02X", discid
[i
]);
773 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "\n");
775 if (DVDUDFVolumeInfo(dvd
, volid
, sizeof(volid
), NULL
, 0) >= 0 || DVDISOVolumeInfo(dvd
, volid
, sizeof(volid
), NULL
, 0) >= 0)
776 mp_msg(MSGT_IDENTIFY
, MSGL_V
, "ID_DVD_VOLUME_ID=%s\n", volid
);
779 * Make sure our title number is valid.
781 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
, "There are %d titles on this DVD.\n", tt_srpt
->nr_of_srpts
);
782 if(dvd_title
< 1 || dvd_title
> tt_srpt
->nr_of_srpts
) {
783 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Invalid DVD title number: %d\n", dvd_title
);
784 ifoClose( vmg_file
);
786 m_struct_free(&stream_opts
,opts
);
787 return STREAM_UNSUPPORTED
;
789 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title
);
790 --dvd_title
; // remap 1.. -> 0..
792 * Make sure the angle number is valid for this title.
794 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
, "There are %d angles in this DVD title.\n", tt_srpt
->title
[dvd_title
].nr_of_angles
);
795 if(dvd_angle
<1 || dvd_angle
>tt_srpt
->title
[dvd_title
].nr_of_angles
) {
796 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Invalid DVD angle number: %d\n", dvd_angle
);
799 --dvd_angle
; // remap 1.. -> 0..
801 ttn
= tt_srpt
->title
[dvd_title
].vts_ttn
- 1;
803 * Load the VTS information for the title set our title is in.
805 vts_file
= ifoOpen( dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
);
807 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Cannot open the IFO file for DVD title %d.\n", tt_srpt
->title
[dvd_title
].title_set_nr
);
811 * We've got enough info, time to open the title set data.
813 title
= DVDOpenFile(dvd
, tt_srpt
->title
[dvd_title
].title_set_nr
, DVD_READ_TITLE_VOBS
);
815 mp_tmsg(MSGT_OPEN
,MSGL_ERR
, "Cannot open title VOBS (VTS_%02d_1.VOB).\n", tt_srpt
->title
[dvd_title
].title_set_nr
);
816 ifoClose( vts_file
);
820 mp_msg(MSGT_OPEN
,MSGL_V
, "DVD successfully opened.\n");
822 d
=malloc(sizeof(dvd_priv_t
)); memset(d
,0,sizeof(dvd_priv_t
));
825 d
->vmg_file
=vmg_file
;
827 d
->vts_file
=vts_file
;
828 d
->cur_title
= dvd_title
+1;
830 pgc
= vts_file
->vts_pgcit
? vts_file
->vts_pgcit
->pgci_srp
[ttn
].pgc
: NULL
;
832 * Check number of audio channels and types
836 if(vts_file
->vts_pgcit
) {
839 if(pgc
->audio_control
[i
] & 0x8000) {
840 audio_attr_t
* audio
= &vts_file
->vtsi_mat
->vts_audio_attr
[i
];
842 char tmp
[] = "unknown";
843 stream_language_t
*audio_stream
= &d
->audio_streams
[d
->nr_of_channels
];
845 if(audio
->lang_type
== 1) {
846 language
=audio
->lang_code
;
848 tmp
[1]=language
&0xff;
852 audio_stream
->language
=language
;
853 audio_stream
->id
=pgc
->audio_control
[i
] >> 8 & 7;
854 switch(audio
->audio_format
) {
856 audio_stream
->id
+=FIRST_AC3_AID
;
859 audio_stream
->id
+=FIRST_DTS_AID
;
861 case 2: // mpeg layer 1/2/3
863 audio_stream
->id
+=FIRST_MPG_AID
;
866 audio_stream
->id
+=FIRST_PCM_AID
;
870 audio_stream
->type
=audio
->audio_format
;
871 // Pontscho: to my mind, tha channels:
874 audio_stream
->channels
=audio
->channels
;
875 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"audio stream: %d format: %s (%s) language: %s aid: %d.\n",
877 dvd_audio_stream_types
[ audio
->audio_format
],
878 dvd_audio_stream_channels
[ audio
->channels
],
882 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AUDIO_ID=%d\n", audio_stream
->id
);
883 if(language
&& tmp
[0])
884 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_AID_%d_LANG=%s\n", audio_stream
->id
, tmp
);
889 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"number of audio channels on disk: %d.\n",d
->nr_of_channels
);
893 * Check number of subtitles and language
898 d
->nr_of_subtitles
=0;
900 if(pgc
->subp_control
[i
] & 0x80000000) {
901 subp_attr_t
* subtitle
= &vts_file
->vtsi_mat
->vts_subp_attr
[i
];
902 video_attr_t
*video
= &vts_file
->vtsi_mat
->vts_video_attr
;
904 char tmp
[] = "unknown";
905 stream_language_t
*sub_stream
= &d
->subtitles
[d
->nr_of_subtitles
];
907 if(subtitle
->type
== 1) {
908 language
=subtitle
->lang_code
;
910 tmp
[1]=language
&0xff;
914 sub_stream
->language
=language
;
915 sub_stream
->id
=d
->nr_of_subtitles
;
916 if(video
->display_aspect_ratio
== 0) /* 4:3 */
917 sub_stream
->id
= pgc
->subp_control
[i
] >> 24 & 31;
918 else if(video
->display_aspect_ratio
== 3) /* 16:9 */
919 sub_stream
->id
= pgc
->subp_control
[i
] >> 8 & 31;
921 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"subtitle ( sid ): %d language: %s\n", sub_stream
->id
, tmp
);
922 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SUBTITLE_ID=%d\n", sub_stream
->id
);
923 if(language
&& tmp
[0])
924 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_SID_%d_LANG=%s\n", sub_stream
->id
, tmp
);
925 d
->nr_of_subtitles
++;
927 mp_tmsg(MSGT_OPEN
,MSGL_STATUS
,"number of subtitles on disk: %d\n",d
->nr_of_subtitles
);
931 * Determine which program chain we want to watch. This is based on the
934 pgc_id
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgcn
; // local
935 pgn
= vts_file
->vts_ptt_srpt
->title
[ttn
].ptt
[0].pgn
; // local
936 d
->cur_pgc_idx
= pgc_id
-1;
937 d
->cur_pgc
= vts_file
->vts_pgcit
->pgci_srp
[pgc_id
-1].pgc
;
938 d
->cur_cell
= d
->cur_pgc
->program_map
[pgn
-1] - 1; // start playback here
939 d
->packs_left
=-1; // for Navi stuff
941 d
->last_cell
=d
->cur_pgc
->nr_of_cells
;
943 if(d
->cur_pgc
->cell_playback
[d
->cur_cell
].block_type
== BLOCK_TYPE_ANGLE_BLOCK
)
944 d
->cur_cell
+=dvd_angle
;
945 d
->cur_pack
= d
->cur_pgc
->cell_playback
[ d
->cur_cell
].first_sector
;
946 d
->cell_last_pack
=d
->cur_pgc
->cell_playback
[ d
->cur_cell
].last_sector
;
947 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
);
949 //assign cell_times_table
950 d
->cell_times_table
= malloc(sizeof(unsigned int) * d
->cur_pgc
->nr_of_cells
);
951 if(d
->cell_times_table
== NULL
)
952 return STREAM_UNSUPPORTED
;
953 for(k
=0; k
<d
->cur_pgc
->nr_of_cells
; k
++)
954 d
->cell_times_table
[k
] = mp_dvdtimetomsec(&d
->cur_pgc
->cell_playback
[k
].playback_time
);
955 list_chapters(d
->cur_pgc
);
957 // ... (unimplemented)
959 stream
->type
= STREAMTYPE_DVD
;
960 stream
->sector_size
= 2048;
961 stream
->flags
= STREAM_READ
| MP_STREAM_SEEK
;
962 stream
->fill_buffer
= fill_buffer
;
964 stream
->control
= control
;
965 stream
->close
= stream_dvd_close
;
966 stream
->start_pos
= (off_t
)d
->cur_pack
*2048;
967 stream
->end_pos
= (off_t
)(d
->cur_pgc
->cell_playback
[d
->last_cell
-1].last_sector
)*2048;
968 *file_format
= DEMUXER_TYPE_MPEG_PS
;
969 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
);
970 stream
->priv
= (void*)d
;
976 m_struct_free(&stream_opts
, opts
);
977 return STREAM_UNSUPPORTED
;
979 mp_tmsg(MSGT_DVD
,MSGL_ERR
,"MPlayer was compiled without DVD support, exiting.\n");
980 m_struct_free(&stream_opts
,opts
);
981 return STREAM_UNSUPPORTED
;
984 static int ifo_stream_open (stream_t
*stream
, int mode
, void *opts
, int *file_format
)
987 struct stream_priv_s
*spriv
;
988 int len
= strlen(stream
->url
);
990 if (len
< 4 || strcasecmp (stream
->url
+ len
- 4, ".ifo"))
991 return STREAM_UNSUPPORTED
;
993 mp_msg(MSGT_DVD
, MSGL_INFO
, ".IFO detected. Redirecting to dvd://\n");
995 filename
= strdup(basename(stream
->url
));
997 spriv
=calloc(1, sizeof(struct stream_priv_s
));
998 spriv
->device
= strdup(dirname(stream
->url
));
999 if(!strncasecmp(filename
,"vts_",4))
1001 if(sscanf(filename
+3, "_%02d_", &spriv
->title
)!=1)
1008 stream
->url
=strdup("dvd://");
1010 return open_s(stream
, mode
, spriv
, file_format
);
1013 const stream_info_t stream_info_dvd
= {
1021 1 // Urls are an option string
1024 const stream_info_t stream_info_ifo
= {
1028 "Mostly used to play DVDs on disk through OSD Menu",
1030 { "file", "", NULL
},