Merge svn changes up to r27514
[mplayer.git] / stream / stream_dvd.c
blob2c4d1b859a41605fcf8fd22de5e9f3d04f3fb102
3 #include <ctype.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <string.h>
11 #include "config.h"
12 #include "mp_msg.h"
13 #include "help_mp.h"
15 #ifdef __FreeBSD__
16 #include <sys/cdrio.h>
17 #endif
19 #ifdef __linux__
20 #include <linux/cdrom.h>
21 #include <scsi/sg.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
25 #endif
27 #include <libgen.h>
28 #include <errno.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
35 #include "stream.h"
36 #include "m_option.h"
37 #include "m_struct.h"
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;
46 int dvd_angle=1;
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)
52 int fd;
53 unsigned char buffer[28];
54 unsigned char cmd[12];
55 struct sg_io_hdr sghdr;
56 struct stat st;
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 */
64 switch (speed) {
65 case 0: /* don't touch speed setting */
66 return;
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);
70 break;
71 default: /* limit to <speed> KB/s */
72 // speed < 100 is multiple of DVD single speed (1350KB/s)
73 if (speed < 100)
74 speed *= 1350;
75 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_DVDlimitSpeed, speed);
76 break;
79 memset(&sghdr, 0, sizeof(sghdr));
80 sghdr.interface_id = 'S';
81 sghdr.timeout = 5000;
82 sghdr.dxfer_direction = SG_DXFER_TO_DEV;
83 sghdr.dxfer_len = sizeof(buffer);
84 sghdr.dxferp = buffer;
85 sghdr.cmd_len = sizeof(cmd);
86 sghdr.cmdp = 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);
95 if (speed == -1)
96 buffer[0] = 4; /* restore default */
97 else {
98 /* <speed> kilobyte */
99 AV_WB32(buffer + 12, speed);
100 AV_WB32(buffer + 20, speed);
102 /* 1 second */
103 AV_WB16(buffer + 18, 1000);
104 AV_WB16(buffer + 26, 1000);
106 fd = open(device, O_RDWR | O_NONBLOCK);
107 if (fd == -1) {
108 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_DVDspeedCantOpen);
109 return;
112 if (ioctl(fd, SG_IO, &sghdr) < 0)
113 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_DVDlimitFail);
114 else
115 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_DVDlimitOk);
117 close(fd);
118 #endif
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)
131 #else
132 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,8,0)
133 #endif
134 #endif
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 {
141 int title;
142 char* device;
143 } stream_priv_dflts = {
145 NULL
148 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
149 /// URL definition
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 = {
156 "dvd",
157 sizeof(struct stream_priv_s),
158 &stream_priv_dflts,
159 stream_opts_fields
162 int dvd_parse_chapter_range(const m_option_t *conf, const char *range) {
163 const char *s;
164 char *t;
165 if (!range)
166 return M_OPT_MISSING_PARAM;
167 s = range;
168 dvd_chapter = 1;
169 dvd_last_chapter = 0;
170 if(*range && isdigit(*range)) {
171 dvd_chapter = strtol(range, &s, 10);
172 if(range == s) {
173 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_DVDinvalidChapterRange, range);
174 return M_OPT_INVALID;
177 if(*s == 0)
178 return 0;
179 else if(*s != '-') {
180 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_DVDinvalidChapterRange, range);
181 return M_OPT_INVALID;
183 ++s;
184 if(*s == 0)
185 return 0;
186 if(! isdigit(*s)) {
187 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_DVDinvalidChapterRange, range);
188 return M_OPT_INVALID;
190 dvd_last_chapter = strtol(s, &t, 10);
191 if (s == t || *t) {
192 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_DVDinvalidChapterRange, range);
193 return M_OPT_INVALID;
195 return 0;
198 int dvd_chapter_from_cell(dvd_priv_t* dvd,int title,int cell)
200 pgc_t * cur_pgc;
201 ptt_info_t* ptt;
202 int chapter = cell;
203 int pgc_id,pgn;
204 if(title < 0 || cell < 0){
205 return 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) {
219 return chapter;
221 --chapter;
223 /* didn't find a chapter ??? */
224 return chapter;
227 int dvd_lang_from_aid(stream_t *stream, int id) {
228 dvd_priv_t *d;
229 int i;
230 if (!stream) return 0;
231 d = stream->priv;
232 if (!d) 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;
237 return 0;
240 int dvd_aid_from_lang(stream_t *stream, unsigned char* lang) {
241 dvd_priv_t *d=stream->priv;
242 int code,i;
243 if(lang) {
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);
258 return -1;
261 int dvd_number_of_subs(stream_t *stream) {
262 int i;
263 int maxid = -1;
264 dvd_priv_t *d;
265 if (!stream) return -1;
266 d = stream->priv;
267 if (!d) return -1;
268 for (i = 0; i < d->nr_of_subtitles; i++)
269 if (d->subtitles[i].id > maxid) maxid = d->subtitles[i].id;
270 return maxid + 1;
273 int dvd_lang_from_sid(stream_t *stream, int id) {
274 int i;
275 dvd_priv_t *d;
276 if (!stream) return 0;
277 d = stream->priv;
278 if (!d) 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;
281 return 0;
284 int dvd_sid_from_lang(stream_t *stream, unsigned char* lang) {
285 dvd_priv_t *d=stream->priv;
286 int code,i;
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;
295 lang+=2;
296 while (lang[0]==',' || lang[0]==' ') ++lang;
298 mp_msg(MSGT_OPEN,MSGL_WARN,MSGTR_DVDnoMatchingSubtitle);
299 return -1;
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 )
309 break;
310 ++next_cell;
313 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next2=0x%X \n",next_cell);
315 ++next_cell;
316 if(next_cell>=d->last_cell)
317 return -1; // EOF
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)
321 return -1; // EOF
323 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next3=0x%X \n",next_cell);
324 return next_cell;
327 static int dvd_read_sector(dvd_priv_t *d,unsigned char* data) {
328 int len;
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
336 * really happy.
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);
344 } else {
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;
351 read_next:
352 if(d->cur_pack>d->cell_last_pack) {
353 // end of cell!
354 int next=dvd_next_cell(d);
355 if(next>=0) {
356 d->cur_cell=next;
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);
362 } else
363 return -1; // EOF
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 ]));
374 #else
375 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t));
376 #endif
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);
380 } else {
381 // process!
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);
390 if(d->angle_seek) {
391 int i,skip=0;
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
396 // signal.
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;
405 #else
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;
408 #endif
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;
413 d->angle_seek=0;
414 d->cur_pack--;
415 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d->cur_pack);
416 } else {
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) ){
420 d->angle_seek=0;
421 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced by cell/vob IDN search! \n");
422 } else {
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
431 ++d->cur_pack;
432 goto read_next;
435 ++d->cur_pack;
436 if(d->packs_left>=0) --d->packs_left;
438 if(d->angle_seek) {
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) {
447 d->packs_left=-1;
448 d->cur_pack=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!
455 d->cur_cell=0;
456 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
457 d->cur_cell+=dvd_angle;
459 while(1) {
460 int next;
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;
464 break;
466 if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :)
467 next=dvd_next_cell(d);
468 if(next<0) {
469 //d->cur_pack=d->cell_last_pack+1;
470 break; // we're after the last cell
472 d->cur_cell=next;
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)
481 d->angle_seek=1;
484 static void dvd_close(dvd_priv_t *d) {
485 ifoClose(d->vts_file);
486 ifoClose(d->vmg_file);
487 DVDCloseFile(d->title);
488 DVDClose(d->dvd);
489 dvd_chapter = 1;
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);
498 if(pos>=0) {
499 len=2048; // full sector
500 s->pos=2048*pos-len;
501 } else len=-1; // error
503 return len;
506 static int seek(stream_t *s, off_t newpos) {
507 s->pos=newpos; // real seek
508 dvd_seek(s->priv,s->pos/2048);
509 return 1;
512 static void stream_dvd_close(stream_t *s) {
513 dvd_close(s->priv);
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
522 msec=0;
523 if(!vts_file || !tt_srpt)
524 return 0;
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);
532 return msec;
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);
542 if(!vts_file)
543 return 0;
545 if(!vts_file->vtsi_mat || !vts_file->vts_pgcit)
547 ifoClose(vts_file);
548 return 0;
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)
554 continue;
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);
558 ifoClose(vts_file);
559 return 1;
562 static int seek_to_chapter(stream_t *stream, ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no, int chapter)
564 int cell;
565 ptt_info_t ptt;
566 pgc_t *pgc;
567 off_t pos;
569 if(!vts_file || !tt_srpt)
570 return 0;
572 if(chapter < 0 || chapter > vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts-1) //no such chapter
573 return 0;
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);
584 return chapter;
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)
593 return;
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
599 t2 = t/1000;
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);
606 cell++;
609 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "\n");
612 static double dvd_get_current_time(stream_t *stream, int cell)
614 int i, tm;
615 dvd_priv_t *d = stream->priv;
617 tm=0;
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
623 continue;
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;
634 int t=0;
635 double tm, duration;
636 off_t pos = -1;
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)
641 return 0;
643 duration = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1) / 1000.0f;
644 if(sec > duration)
645 return 0;
647 i=d->cur_pgc_idx;
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);
651 if(ac_time >= sec)
652 break;
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;
659 break;
663 pos = ((off_t)cell_sector)<<11;
664 stream_seek(stream, pos);
665 do {
666 stream_skip(stream, 2048);
667 t = mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
668 } while(!t);
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));
675 while(tm <= sec) {
676 if(!stream_skip(stream, 2048))
677 break;
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
684 for(i=1; i<k; i++) {
685 if(vts_file->vts_vobu_admap->vobu_start_sectors[i] > tmap_sector)
686 break;
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);
692 return 1;
695 static int control(stream_t *stream,int cmd,void* arg)
697 dvd_priv_t *d = stream->priv;
698 switch(cmd)
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;
703 return 1;
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;
709 return 1;
711 case STREAM_CTRL_SEEK_TO_CHAPTER:
713 int r;
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;
717 return 1;
719 case STREAM_CTRL_GET_CURRENT_CHAPTER:
721 *((unsigned int *)arg) = dvd_chapter_from_cell(d, d->cur_title-1, d->cur_cell);
722 return 1;
724 case STREAM_CTRL_GET_CURRENT_TIME:
726 double tm;
727 tm = dvd_get_current_time(stream, 0);
728 if(tm != -1) {
729 *((double *)arg) = tm;
730 return 1;
732 break;
734 case STREAM_CTRL_SEEK_TO_TIME:
736 if(dvd_seek_to_time(stream, d->vts_file, *((double*)arg)))
737 return 1;
738 break;
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;
743 return 1;
745 case STREAM_CTRL_GET_NUM_ANGLES:
747 *((int *)arg) = d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles;
748 return 1;
750 case STREAM_CTRL_GET_ANGLE:
752 *((int *)arg) = dvd_angle+1;
753 return 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)
759 break;
760 dvd_angle = ang - 1;
761 d->angle_seek = 1;
762 return 1;
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;
771 int k;
773 mp_msg(MSGT_OPEN,MSGL_V,"URL: %s\n", stream->url);
774 dvd_title = p->title;
775 if(1){
776 //int ret,ret2;
777 dvd_priv_t *d;
778 int ttn,pgc_id,pgn;
779 dvd_reader_t *dvd;
780 dvd_file_t *title;
781 ifo_handle_t *vmg_file;
782 tt_srpt_t *tt_srpt;
783 ifo_handle_t *vts_file;
784 pgc_t *pgc;
786 * Open the disc.
788 if(p->device)
789 dvd_device_current = p->device;
790 else if(dvd_device)
791 dvd_device_current = dvd_device;
792 else
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")) {
798 int i;
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);
805 if(!dvd) {
806 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CantOpenDVD,temp_device, strerror(errno));
807 } else {
808 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
809 dvd_file_t *dvdfile = DVDOpenFile(dvd,dvd_title,DVD_READ_INFO_FILE);
810 if(!dvdfile) {
811 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CantOpenDVD,temp_device, strerror(errno));
812 DVDClose(dvd);
813 continue;
815 DVDCloseFile(dvdfile);
816 #endif
817 break;
820 free(temp_device);
822 if(!dvd) {
823 m_struct_free(&stream_opts,opts);
824 return STREAM_UNSUPPORTED;
826 } else
827 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
829 dvd = DVDOpen(dvd_device_current);
830 if(!dvd) {
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
841 * this disc.
843 vmg_file = ifoOpen(dvd, 0);
844 if(!vmg_file) {
845 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDnoVMG);
846 DVDClose( dvd );
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))
863 char volid[32];
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)
870 int i;
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 );
886 DVDClose( dvd );
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);
898 goto fail;
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 );
907 if(!vts_file) {
908 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDnoIFO, tt_srpt->title[dvd_title].title_set_nr );
909 goto fail;
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);
915 if(!title) {
916 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDnoVOBs, tt_srpt->title[dvd_title].title_set_nr);
917 ifoClose( vts_file );
918 goto fail;
921 mp_msg(MSGT_OPEN,MSGL_V, "DVD successfully opened.\n");
922 // store data
923 d=malloc(sizeof(dvd_priv_t)); memset(d,0,sizeof(dvd_priv_t));
924 d->dvd=dvd;
925 d->title=title;
926 d->vmg_file=vmg_file;
927 d->tt_srpt=tt_srpt;
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
936 d->nr_of_channels=0;
937 if(vts_file->vts_pgcit) {
938 int i;
939 for(i=0;i<8;i++)
940 if(pgc->audio_control[i] & 0x8000) {
941 audio_attr_t * audio = &vts_file->vtsi_mat->vts_audio_attr[i];
942 int language = 0;
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;
948 tmp[0]=language>>8;
949 tmp[1]=language&0xff;
950 tmp[2]=0;
953 audio_stream->language=language;
954 audio_stream->id=pgc->audio_control[i] >> 8 & 7;
955 switch(audio->audio_format) {
956 case 0: // ac3
957 audio_stream->id+=FIRST_AC3_AID;
958 break;
959 case 6: // dts
960 audio_stream->id+=FIRST_DTS_AID;
961 break;
962 case 2: // mpeg layer 1/2/3
963 case 3: // mpeg2 ext
964 audio_stream->id+=FIRST_MPG_AID;
965 break;
966 case 4: // lpcm
967 audio_stream->id+=FIRST_PCM_AID;
968 break;
971 audio_stream->type=audio->audio_format;
972 // Pontscho: to my mind, tha channels:
973 // 1 - stereo
974 // 5 - 5.1
975 audio_stream->channels=audio->channels;
976 mp_msg(MSGT_OPEN,MSGL_STATUS,MSGTR_DVDaudioStreamInfo,
977 d->nr_of_channels,
978 dvd_audio_stream_types[ audio->audio_format ],
979 dvd_audio_stream_channels[ audio->channels ],
980 tmp,
981 audio_stream->id
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);
987 d->nr_of_channels++;
990 mp_msg(MSGT_OPEN,MSGL_STATUS,MSGTR_DVDnumAudioChannels,d->nr_of_channels );
994 * Check number of subtitles and language
997 int i;
999 d->nr_of_subtitles=0;
1000 for(i=0;i<32;i++)
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;
1004 int language = 0;
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;
1010 tmp[0]=language>>8;
1011 tmp[1]=language&0xff;
1012 tmp[2]=0;
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
1033 * chapter number.
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
1041 d->angle_seek=0;
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)
1059 // return NULL;
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;
1072 return STREAM_OK;
1074 fail:
1075 ifoClose(vmg_file);
1076 DVDClose(dvd);
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)
1087 char *ext;
1088 char* filename;
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)
1104 spriv->title=1;
1105 }else
1106 spriv->title=1;
1108 free(filename);
1109 free(stream->url);
1110 stream->url=strdup("dvd://");
1112 return open_s(stream, mode, spriv, file_format);
1115 const stream_info_t stream_info_dvd = {
1116 "DVD stream",
1117 "null",
1120 open_s,
1121 { "dvd", NULL },
1122 &stream_opts,
1123 1 // Urls are an option string
1126 const stream_info_t stream_info_ifo = {
1127 "DVD IFO input",
1128 "ifo",
1129 "Benjamin Zores",
1130 "Mostly used to play DVDs on disk through OSD Menu",
1131 ifo_stream_open,
1132 { "file", "", NULL },
1133 NULL,