mphq now runs Subversion 1.5.
[mplayer/glamo.git] / stream / stream_dvd.c
blobae07239ad52972af65542e7ab21806ca19e3f010
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 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 // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss
368 if(len <= 0) return -1; //error
370 if(data[38]==0 && data[39]==0 && data[40]==1 && data[41]==0xBF &&
371 data[1024]==0 && data[1025]==0 && data[1026]==1 && data[1027]==0xBF) {
372 // found a Navi packet!!!
373 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
374 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]));
375 #else
376 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t));
377 #endif
378 if(d->cur_pack != d->dsi_pack.dsi_gi.nv_pck_lbn ) {
379 mp_msg(MSGT_DVD,MSGL_V, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
380 d->cur_pack,d->dsi_pack.dsi_gi.nv_pck_lbn);
381 } else {
382 // process!
383 d->packs_left = d->dsi_pack.dsi_gi.vobu_ea;
384 mp_msg(MSGT_DVD,MSGL_DBG2, "Found NAVI packet! lba=0x%X len=%d \n",d->cur_pack,d->packs_left);
385 //navPrint_DSI(&d->dsi_pack);
386 mp_msg(MSGT_DVD,MSGL_DBG3,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d->cur_cell,
387 d->dsi_pack.dsi_gi.vobu_c_idn,d->dsi_pack.dsi_gi.vobu_vob_idn,
388 d->cur_pgc->cell_position[d->cur_cell].cell_nr,
389 d->cur_pgc->cell_position[d->cur_cell].vob_id_nr);
391 if(d->angle_seek) {
392 int i,skip=0;
393 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
394 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
395 // it generates incorrect code for unaligned access to a packed
396 // structure member, resulting in an mplayer crash with a SIGBUS
397 // signal.
399 // See also gcc problem report PR c/7847:
400 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
401 for(i=0;i<9;i++) { // check if all values zero:
402 __typeof__(d->dsi_pack.sml_agli.data[i].address) tmp_addr;
403 memcpy(&tmp_addr,&d->dsi_pack.sml_agli.data[i].address,sizeof(tmp_addr));
404 if((skip=tmp_addr)!=0) break;
406 #else
407 for(i=0;i<9;i++) // check if all values zero:
408 if((skip=d->dsi_pack.sml_agli.data[i].address)!=0) break;
409 #endif
410 if(skip && skip!=0x7fffffff) {
411 // sml_agli table has valid data (at least one non-zero):
412 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
413 d->dsi_pack.sml_agli.data[dvd_angle].address;
414 d->angle_seek=0;
415 d->cur_pack--;
416 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d->cur_pack);
417 } else {
418 // check if we're in the right cell, jump otherwise:
419 if( (d->dsi_pack.dsi_gi.vobu_c_idn==d->cur_pgc->cell_position[d->cur_cell].cell_nr) &&
420 (d->dsi_pack.dsi_gi.vobu_vob_idn==d->cur_pgc->cell_position[d->cur_cell].vob_id_nr) ){
421 d->angle_seek=0;
422 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced by cell/vob IDN search! \n");
423 } else {
424 // wrong angle, skip this vobu:
425 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
426 d->dsi_pack.dsi_gi.vobu_ea;
427 d->angle_seek=2; // DEBUG
432 ++d->cur_pack;
433 goto read_next;
436 ++d->cur_pack;
437 if(d->packs_left>=0) --d->packs_left;
439 if(d->angle_seek) {
440 if(d->angle_seek==2) mp_msg(MSGT_DVD,MSGL_V, "!!! warning! reading packet while angle_seek !!!\n");
441 goto read_next; // searching for Navi packet
444 return d->cur_pack-1;
447 void dvd_seek(dvd_priv_t *d,int pos) {
448 d->packs_left=-1;
449 d->cur_pack=pos;
451 // check if we stay in current cell (speedup things, and avoid angle skip)
452 if(d->cur_pack>d->cell_last_pack ||
453 d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector) {
455 // ok, cell change, find the right cell!
456 d->cur_cell=0;
457 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
458 d->cur_cell+=dvd_angle;
460 while(1) {
461 int next;
462 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
463 if(d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector) {
464 d->cur_pack=d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
465 break;
467 if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :)
468 next=dvd_next_cell(d);
469 if(next<0) {
470 //d->cur_pack=d->cell_last_pack+1;
471 break; // we're after the last cell
473 d->cur_cell=next;
477 mp_msg(MSGT_DVD,MSGL_V, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
478 d->cur_pack,d->cur_cell,d->cur_pgc->cell_playback[ d->cur_cell ].first_sector,d->cell_last_pack);
480 // if we're in interleaved multi-angle cell, find the right angle chain!
481 // (read Navi block, and use the seamless angle jump table)
482 d->angle_seek=1;
485 void dvd_close(dvd_priv_t *d) {
486 ifoClose(d->vts_file);
487 ifoClose(d->vmg_file);
488 DVDCloseFile(d->title);
489 DVDClose(d->dvd);
490 dvd_chapter = 1;
491 dvd_last_chapter = 0;
492 dvd_set_speed(dvd_device_current, -1); /* -1 => restore default */
495 static int fill_buffer(stream_t *s, char *but, int len)
497 if(s->type == STREAMTYPE_DVD) {
498 off_t pos=dvd_read_sector(s->priv,s->buffer);
499 if(pos>=0) {
500 len=2048; // full sector
501 s->pos=2048*pos-len;
502 } else len=-1; // error
504 return len;
507 static int seek(stream_t *s, off_t newpos) {
508 s->pos=newpos; // real seek
509 dvd_seek(s->priv,s->pos/2048);
510 return 1;
513 static void stream_dvd_close(stream_t *s) {
514 dvd_close(s->priv);
517 static int mp_get_titleset_length(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no)
519 int vts_ttn; ///< title number within video title set
520 int pgc_no; ///< program chain number
521 int msec; ///< time length in milliseconds
523 msec=0;
524 if(!vts_file || !tt_srpt)
525 return 0;
527 if(vts_file->vtsi_mat && vts_file->vts_pgcit)
529 vts_ttn = tt_srpt->title[title_no].vts_ttn - 1;
530 pgc_no = vts_file->vts_ptt_srpt->title[vts_ttn].ptt[0].pgcn - 1;
531 msec = mp_dvdtimetomsec(&vts_file->vts_pgcit->pgci_srp[pgc_no].pgc->playback_time);
533 return msec;
537 static int mp_describe_titleset(dvd_reader_t *dvd, tt_srpt_t *tt_srpt, int vts_no)
539 ifo_handle_t *vts_file;
540 int title_no, msec=0;
542 vts_file = ifoOpen(dvd, vts_no);
543 if(!vts_file)
544 return 0;
546 if(!vts_file->vtsi_mat || !vts_file->vts_pgcit)
548 ifoClose(vts_file);
549 return 0;
552 for(title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
554 if (tt_srpt->title[title_no].title_set_nr != vts_no)
555 continue;
556 msec = mp_get_titleset_length(vts_file, tt_srpt, title_no);
557 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no + 1, msec / 1000, msec % 1000);
559 ifoClose(vts_file);
560 return 1;
563 static int seek_to_chapter(stream_t *stream, ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no, int chapter)
565 int cell;
566 ptt_info_t ptt;
567 pgc_t *pgc;
568 off_t pos;
570 if(!vts_file || !tt_srpt)
571 return 0;
573 if(title_no < 0 || title_no >= tt_srpt->nr_of_srpts)
574 return 0;
576 // map global title to vts title
577 title_no = tt_srpt->title[title_no].vts_ttn - 1;
579 if(title_no < 0 || title_no >= vts_file->vts_ptt_srpt->nr_of_srpts)
580 return 0;
582 if(chapter < 0 || chapter > vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts-1) //no such chapter
583 return 0;
585 ptt = vts_file->vts_ptt_srpt->title[title_no].ptt[chapter];
586 pgc = vts_file->vts_pgcit->pgci_srp[ptt.pgcn-1].pgc;
588 cell = pgc->program_map[ptt.pgn - 1] - 1;
589 pos = (off_t) pgc->cell_playback[cell].first_sector * 2048;
590 mp_msg(MSGT_OPEN,MSGL_V,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64"\n",
591 chapter, pgc->cell_playback[cell].first_sector, pos);
592 stream_seek(stream, pos);
594 return chapter;
597 static void list_chapters(pgc_t *pgc)
599 unsigned int i, cell;
600 unsigned int t=0, t2=0;
602 if(pgc->nr_of_programs < 2)
603 return;
605 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "CHAPTERS: ");
606 for(i=0; i<pgc->nr_of_programs; i++)
608 cell = pgc->program_map[i]; //here the cell is 1-based
609 t2 = t/1000;
610 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "%02d:%02d:%02d,", t2/3600, (t2/60)%60, t2%60);
611 while(i+1<pgc->nr_of_programs && cell < pgc->program_map[i+1]) {
612 if(!(pgc->cell_playback[cell-1].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
613 pgc->cell_playback[cell-1].block_mode != BLOCK_MODE_FIRST_CELL)
615 t += mp_dvdtimetomsec(&pgc->cell_playback[cell-1].playback_time);
616 cell++;
619 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "\n");
622 static double dvd_get_current_time(stream_t *stream, int cell)
624 int i, tm;
625 dvd_priv_t *d = stream->priv;
627 tm=0;
628 if(!cell) cell=d->cur_cell;
629 for(i=0; i<d->cur_cell; i++) {
630 if(d->cur_pgc->cell_playback[i].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
631 d->cur_pgc->cell_playback[i].block_mode != BLOCK_MODE_FIRST_CELL
633 continue;
634 tm += d->cell_times_table[i];
636 tm += mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
638 return (double)tm/1000.0;
641 static int dvd_seek_to_time(stream_t *stream, ifo_handle_t *vts_file, double sec)
643 unsigned int i, j, k, timeunit, ac_time, tmap_sector=0, cell_sector=0, vobu_sector=0;
644 int t=0;
645 double tm, duration;
646 off_t pos = -1;
647 dvd_priv_t *d = stream->priv;
648 vts_tmapt_t *vts_tmapt = vts_file->vts_tmapt;
650 if(!vts_file->vts_tmapt || sec < 0)
651 return 0;
653 duration = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1) / 1000.0f;
654 if(sec > duration)
655 return 0;
657 i=d->cur_pgc_idx;
658 timeunit = vts_tmapt->tmap[i].tmu;
659 for(j = 0; j < vts_tmapt->tmap[i].nr_of_entries; j++) {
660 ac_time = timeunit * (j + 1);
661 if(ac_time >= sec)
662 break;
663 tmap_sector = vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff;
665 //search enclosing cell
666 for(i=0; i<d->cur_pgc->nr_of_cells; i++) {
667 if(tmap_sector >= d->cur_pgc->cell_playback[i].first_sector && tmap_sector <= d->cur_pgc->cell_playback[i].last_sector) {
668 cell_sector = d->cur_pgc->cell_playback[i].first_sector;
669 break;
673 pos = ((off_t)cell_sector)<<11;
674 stream_seek(stream, pos);
675 do {
676 stream_skip(stream, 2048);
677 t = mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
678 } while(!t);
679 tm = dvd_get_current_time(stream, 0);
681 pos = ((off_t)tmap_sector)<<11;
682 stream_seek(stream, pos);
683 //now get current time in terms of the cell+cell time offset
684 memset(&d->dsi_pack.dsi_gi.c_eltm, 0, sizeof(dvd_time_t));
685 while(tm <= sec) {
686 if(!stream_skip(stream, 2048))
687 break;
688 tm = dvd_get_current_time(stream, 0);
690 tmap_sector = stream->pos >> 11;
692 //search closest VOBU sector
693 k=(vts_file->vts_vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE)/4; //entries in the vobu admap
694 for(i=1; i<k; i++) {
695 if(vts_file->vts_vobu_admap->vobu_start_sectors[i] > tmap_sector)
696 break;
698 vobu_sector = vts_file->vts_vobu_admap->vobu_start_sectors[i-1];
699 pos = ((off_t)vobu_sector) << 11;
700 stream_seek(stream, pos);
702 return 1;
705 static int control(stream_t *stream,int cmd,void* arg)
707 dvd_priv_t *d = stream->priv;
708 switch(cmd)
710 case STREAM_CTRL_GET_TIME_LENGTH:
712 *((double *)arg) = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1)/1000.0;
713 return 1;
715 case STREAM_CTRL_GET_NUM_CHAPTERS:
717 if(! d->cur_pgc->nr_of_programs) return STREAM_UNSUPPORTED;
718 *((unsigned int *)arg) = d->cur_pgc->nr_of_programs;
719 return 1;
721 case STREAM_CTRL_SEEK_TO_CHAPTER:
723 int r;
724 r = seek_to_chapter(stream, d->vts_file, d->tt_srpt, d->cur_title-1, *((unsigned int *)arg));
725 if(! r) return STREAM_UNSUPPORTED;
727 return 1;
729 case STREAM_CTRL_GET_CURRENT_CHAPTER:
731 *((unsigned int *)arg) = dvd_chapter_from_cell(d, d->cur_title-1, d->cur_cell);
732 return 1;
734 case STREAM_CTRL_GET_CURRENT_TIME:
736 double tm;
737 tm = dvd_get_current_time(stream, 0);
738 if(tm != -1) {
739 *((double *)arg) = tm;
740 return 1;
742 break;
744 case STREAM_CTRL_SEEK_TO_TIME:
746 if(dvd_seek_to_time(stream, d->vts_file, *((double*)arg)))
747 return 1;
748 break;
750 case STREAM_CTRL_GET_ASPECT_RATIO:
752 *((double *)arg) = !d->vts_file->vtsi_mat->vts_video_attr.display_aspect_ratio ? 4.0/3.0 : 16.0/9.0;
753 return 1;
755 case STREAM_CTRL_GET_NUM_ANGLES:
757 *((int *)arg) = d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles;
758 return 1;
760 case STREAM_CTRL_GET_ANGLE:
762 *((int *)arg) = dvd_angle+1;
763 return 1;
765 case STREAM_CTRL_SET_ANGLE:
767 int ang = *((int *)arg);
768 if(ang>d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles || ang<=0)
769 break;
770 dvd_angle = ang - 1;
771 d->angle_seek = 1;
772 return 1;
775 return STREAM_UNSUPPORTED;
779 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
780 struct stream_priv_s* p = (struct stream_priv_s*)opts;
781 int k;
783 mp_msg(MSGT_OPEN,MSGL_V,"URL: %s\n", stream->url);
784 dvd_title = p->title;
785 if(1){
786 //int ret,ret2;
787 dvd_priv_t *d;
788 int ttn,pgc_id,pgn;
789 dvd_reader_t *dvd;
790 dvd_file_t *title;
791 ifo_handle_t *vmg_file;
792 tt_srpt_t *tt_srpt;
793 ifo_handle_t *vts_file;
794 pgc_t *pgc;
796 * Open the disc.
798 if(p->device)
799 dvd_device_current = p->device;
800 else if(dvd_device)
801 dvd_device_current = dvd_device;
802 else
803 dvd_device_current = DEFAULT_DVD_DEVICE;
804 dvd_set_speed(dvd_device_current, dvd_speed);
805 #if defined(__APPLE__) || defined(__DARWIN__)
806 /* Dynamic DVD drive selection on Darwin */
807 if(!strcmp(dvd_device_current, "/dev/rdiskN")) {
808 int i;
809 size_t len = strlen(dvd_device_current)+1;
810 char *temp_device = malloc(len);
812 for (i = 1; i < 10; i++) {
813 snprintf(temp_device, len, "/dev/rdisk%d", i);
814 dvd = DVDOpen(temp_device);
815 if(!dvd) {
816 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CantOpenDVD,temp_device, strerror(errno));
817 } else {
818 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
819 dvd_file_t *dvdfile = DVDOpenFile(dvd,dvd_title,DVD_READ_INFO_FILE);
820 if(!dvdfile) {
821 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CantOpenDVD,temp_device, strerror(errno));
822 DVDClose(dvd);
823 continue;
825 DVDCloseFile(dvdfile);
826 #endif
827 break;
830 free(temp_device);
832 if(!dvd) {
833 m_struct_free(&stream_opts,opts);
834 return STREAM_UNSUPPORTED;
836 } else
837 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
839 dvd = DVDOpen(dvd_device_current);
840 if(!dvd) {
841 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CantOpenDVD,dvd_device_current, strerror(errno));
842 m_struct_free(&stream_opts,opts);
843 return STREAM_UNSUPPORTED;
847 mp_msg(MSGT_OPEN,MSGL_V,"Reading disc structure, please wait...\n");
850 * Load the video manager to find out the information about the titles on
851 * this disc.
853 vmg_file = ifoOpen(dvd, 0);
854 if(!vmg_file) {
855 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDnoVMG);
856 DVDClose( dvd );
857 m_struct_free(&stream_opts,opts);
858 return STREAM_UNSUPPORTED;
860 tt_srpt = vmg_file->tt_srpt;
861 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
863 int title_no; ///< title number
864 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLES=%d\n", tt_srpt->nr_of_srpts);
865 for (title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
867 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_ptts);
868 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_angles);
871 if (mp_msg_test(MSGT_IDENTIFY, MSGL_V))
873 char volid[32];
874 unsigned char discid [16]; ///< disk ID, a 128 bit MD5 sum
875 int vts_no; ///< video title set number
876 for (vts_no = 1; vts_no <= vmg_file->vts_atrt->nr_of_vtss; vts_no++)
877 mp_describe_titleset(dvd, tt_srpt, vts_no);
878 if (DVDDiscID(dvd, discid) >= 0)
880 int i;
881 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_DISC_ID=");
882 for (i = 0; i < 16; i ++)
883 mp_msg(MSGT_IDENTIFY, MSGL_V, "%02X", discid[i]);
884 mp_msg(MSGT_IDENTIFY, MSGL_V, "\n");
886 if (DVDUDFVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0 || DVDISOVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0)
887 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_VOLUME_ID=%s\n", volid);
890 * Make sure our title number is valid.
892 mp_msg(MSGT_OPEN,MSGL_STATUS, MSGTR_DVDnumTitles, tt_srpt->nr_of_srpts );
893 if(dvd_title < 1 || dvd_title > tt_srpt->nr_of_srpts) {
894 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDinvalidTitle, dvd_title);
895 ifoClose( vmg_file );
896 DVDClose( dvd );
897 m_struct_free(&stream_opts,opts);
898 return STREAM_UNSUPPORTED;
900 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title);
901 --dvd_title; // remap 1.. -> 0..
903 * Make sure the angle number is valid for this title.
905 mp_msg(MSGT_OPEN,MSGL_STATUS, MSGTR_DVDnumAngles, tt_srpt->title[dvd_title].nr_of_angles);
906 if(dvd_angle<1 || dvd_angle>tt_srpt->title[dvd_title].nr_of_angles) {
907 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDinvalidAngle, dvd_angle);
908 goto fail;
910 --dvd_angle; // remap 1.. -> 0..
912 ttn = tt_srpt->title[dvd_title].vts_ttn - 1;
914 * Load the VTS information for the title set our title is in.
916 vts_file = ifoOpen( dvd, tt_srpt->title[dvd_title].title_set_nr );
917 if(!vts_file) {
918 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDnoIFO, tt_srpt->title[dvd_title].title_set_nr );
919 goto fail;
922 * We've got enough info, time to open the title set data.
924 title = DVDOpenFile(dvd, tt_srpt->title[dvd_title].title_set_nr, DVD_READ_TITLE_VOBS);
925 if(!title) {
926 mp_msg(MSGT_OPEN,MSGL_ERR, MSGTR_DVDnoVOBs, tt_srpt->title[dvd_title].title_set_nr);
927 ifoClose( vts_file );
928 goto fail;
931 mp_msg(MSGT_OPEN,MSGL_V, "DVD successfully opened.\n");
932 // store data
933 d=malloc(sizeof(dvd_priv_t)); memset(d,0,sizeof(dvd_priv_t));
934 d->dvd=dvd;
935 d->title=title;
936 d->vmg_file=vmg_file;
937 d->tt_srpt=tt_srpt;
938 d->vts_file=vts_file;
939 d->cur_title = dvd_title+1;
941 pgc = vts_file->vts_pgcit ? vts_file->vts_pgcit->pgci_srp[ttn].pgc : NULL;
943 * Check number of audio channels and types
946 d->nr_of_channels=0;
947 if(vts_file->vts_pgcit) {
948 int i;
949 for(i=0;i<8;i++)
950 if(pgc->audio_control[i] & 0x8000) {
951 audio_attr_t * audio = &vts_file->vtsi_mat->vts_audio_attr[i];
952 int language = 0;
953 char tmp[] = "unknown";
954 stream_language_t *audio_stream = &d->audio_streams[d->nr_of_channels];
956 if(audio->lang_type == 1) {
957 language=audio->lang_code;
958 tmp[0]=language>>8;
959 tmp[1]=language&0xff;
960 tmp[2]=0;
963 audio_stream->language=language;
964 audio_stream->id=pgc->audio_control[i] >> 8 & 7;
965 switch(audio->audio_format) {
966 case 0: // ac3
967 audio_stream->id+=FIRST_AC3_AID;
968 break;
969 case 6: // dts
970 audio_stream->id+=FIRST_DTS_AID;
971 break;
972 case 2: // mpeg layer 1/2/3
973 case 3: // mpeg2 ext
974 audio_stream->id+=FIRST_MPG_AID;
975 break;
976 case 4: // lpcm
977 audio_stream->id+=FIRST_PCM_AID;
978 break;
981 audio_stream->type=audio->audio_format;
982 // Pontscho: to my mind, tha channels:
983 // 1 - stereo
984 // 5 - 5.1
985 audio_stream->channels=audio->channels;
986 mp_msg(MSGT_OPEN,MSGL_STATUS,MSGTR_DVDaudioStreamInfo,
987 d->nr_of_channels,
988 dvd_audio_stream_types[ audio->audio_format ],
989 dvd_audio_stream_channels[ audio->channels ],
990 tmp,
991 audio_stream->id
993 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", audio_stream->id);
994 if(language && tmp[0])
995 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", audio_stream->id, tmp);
997 d->nr_of_channels++;
1000 mp_msg(MSGT_OPEN,MSGL_STATUS,MSGTR_DVDnumAudioChannels,d->nr_of_channels );
1004 * Check number of subtitles and language
1007 int i;
1009 d->nr_of_subtitles=0;
1010 for(i=0;i<32;i++)
1011 if(pgc->subp_control[i] & 0x80000000) {
1012 subp_attr_t * subtitle = &vts_file->vtsi_mat->vts_subp_attr[i];
1013 video_attr_t *video = &vts_file->vtsi_mat->vts_video_attr;
1014 int language = 0;
1015 char tmp[] = "unknown";
1016 stream_language_t *sub_stream = &d->subtitles[d->nr_of_subtitles];
1018 if(subtitle->type == 1) {
1019 language=subtitle->lang_code;
1020 tmp[0]=language>>8;
1021 tmp[1]=language&0xff;
1022 tmp[2]=0;
1025 sub_stream->language=language;
1026 sub_stream->id=d->nr_of_subtitles;
1027 if(video->display_aspect_ratio == 0) /* 4:3 */
1028 sub_stream->id = pgc->subp_control[i] >> 24 & 31;
1029 else if(video->display_aspect_ratio == 3) /* 16:9 */
1030 sub_stream->id = pgc->subp_control[i] >> 8 & 31;
1032 mp_msg(MSGT_OPEN,MSGL_STATUS,MSGTR_DVDsubtitleLanguage, sub_stream->id, tmp);
1033 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sub_stream->id);
1034 if(language && tmp[0])
1035 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sub_stream->id, tmp);
1036 d->nr_of_subtitles++;
1038 mp_msg(MSGT_OPEN,MSGL_STATUS,MSGTR_DVDnumSubtitles,d->nr_of_subtitles);
1042 * Determine which program chain we want to watch. This is based on the
1043 * chapter number.
1045 pgc_id = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgcn; // local
1046 pgn = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgn; // local
1047 d->cur_pgc_idx = pgc_id-1;
1048 d->cur_pgc = vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
1049 d->cur_cell = d->cur_pgc->program_map[pgn-1] - 1; // start playback here
1050 d->packs_left=-1; // for Navi stuff
1051 d->angle_seek=0;
1052 d->last_cell=d->cur_pgc->nr_of_cells;
1054 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
1055 d->cur_cell+=dvd_angle;
1056 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
1057 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
1058 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);
1060 //assign cell_times_table
1061 d->cell_times_table = malloc(sizeof(unsigned int) * d->cur_pgc->nr_of_cells);
1062 if(d->cell_times_table == NULL)
1063 return STREAM_UNSUPPORTED;
1064 for(k=0; k<d->cur_pgc->nr_of_cells; k++)
1065 d->cell_times_table[k] = mp_dvdtimetomsec(&d->cur_pgc->cell_playback[k].playback_time);
1066 list_chapters(d->cur_pgc);
1068 // ... (unimplemented)
1069 // return NULL;
1070 stream->type = STREAMTYPE_DVD;
1071 stream->sector_size = 2048;
1072 stream->flags = STREAM_READ | STREAM_SEEK;
1073 stream->fill_buffer = fill_buffer;
1074 stream->seek = seek;
1075 stream->control = control;
1076 stream->close = stream_dvd_close;
1077 stream->start_pos = (off_t)d->cur_pack*2048;
1078 stream->end_pos = (off_t)(d->cur_pgc->cell_playback[d->last_cell-1].last_sector)*2048;
1079 *file_format = DEMUXER_TYPE_MPEG_PS;
1080 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);
1081 stream->priv = (void*)d;
1082 return STREAM_OK;
1084 fail:
1085 ifoClose(vmg_file);
1086 DVDClose(dvd);
1087 m_struct_free(&stream_opts, opts);
1088 return STREAM_UNSUPPORTED;
1090 mp_msg(MSGT_DVD,MSGL_ERR,MSGTR_NoDVDSupport);
1091 m_struct_free(&stream_opts,opts);
1092 return STREAM_UNSUPPORTED;
1095 static int ifo_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1097 char *ext;
1098 char* filename;
1099 struct stream_priv_s *spriv;
1101 ext = strrchr (stream->url, '.');
1102 if (!ext || strcasecmp (ext + 1, "ifo"))
1103 return STREAM_UNSUPPORTED;
1105 mp_msg(MSGT_DVD, MSGL_INFO, ".IFO detected. Redirecting to dvd://\n");
1107 filename = strdup(basename(stream->url));
1109 spriv=calloc(1, sizeof(struct stream_priv_s));
1110 spriv->device = strdup(dirname(stream->url));
1111 if(!strncasecmp(filename,"vts_",4))
1113 if(sscanf(filename+3, "_%02d_", &spriv->title)!=1)
1114 spriv->title=1;
1115 }else
1116 spriv->title=1;
1118 free(filename);
1119 free(stream->url);
1120 stream->url=strdup("dvd://");
1122 return open_s(stream, mode, spriv, file_format);
1125 const stream_info_t stream_info_dvd = {
1126 "DVD stream",
1127 "null",
1130 open_s,
1131 { "dvd", NULL },
1132 &stream_opts,
1133 1 // Urls are an option string
1136 const stream_info_t stream_info_ifo = {
1137 "DVD IFO input",
1138 "ifo",
1139 "Benjamin Zores",
1140 "Mostly used to play DVDs on disk through OSD Menu",
1141 ifo_stream_open,
1142 { "file", "", NULL },
1143 NULL,