Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / stream_dvd.c
blobd1a91f88c14082db801f741af7142fc2d1881c41
1 /*
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.
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <string.h>
26 #include "config.h"
27 #include "mp_msg.h"
29 #include <libgen.h>
30 #include <errno.h>
32 #define FIRST_AC3_AID 128
33 #define FIRST_DTS_AID 136
34 #define FIRST_MPG_AID 0
35 #define FIRST_PCM_AID 160
37 #include "stream.h"
38 #include "m_option.h"
39 #include "m_struct.h"
41 #include "stream_dvd.h"
42 #include "stream_dvd_common.h"
43 #include "libmpdemux/demuxer.h"
45 static char* dvd_device_current;
46 int dvd_angle=1;
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)
58 #else
59 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,8,0)
60 #endif
61 #endif
64 static struct stream_priv_s {
65 int title;
66 char* device;
67 } stream_priv_dflts = {
69 NULL
72 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
73 /// URL definition
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 = {
80 "dvd",
81 sizeof(struct stream_priv_s),
82 &stream_priv_dflts,
83 stream_opts_fields
86 int dvd_parse_chapter_range(const m_option_t *conf, const char *range) {
87 const char *s;
88 char *t;
89 if (!range)
90 return M_OPT_MISSING_PARAM;
91 s = range;
92 dvd_chapter = 1;
93 dvd_last_chapter = 0;
94 if(*range && isdigit(*range)) {
95 dvd_chapter = strtol(range, &s, 10);
96 if(range == s) {
97 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
98 return M_OPT_INVALID;
101 if(*s == 0)
102 return 0;
103 else if(*s != '-') {
104 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
105 return M_OPT_INVALID;
107 ++s;
108 if(*s == 0)
109 return 0;
110 if(! isdigit(*s)) {
111 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
112 return M_OPT_INVALID;
114 dvd_last_chapter = strtol(s, &t, 10);
115 if (s == t || *t) {
116 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
117 return M_OPT_INVALID;
119 return 0;
122 int dvd_chapter_from_cell(dvd_priv_t* dvd,int title,int cell)
124 pgc_t * cur_pgc;
125 ptt_info_t* ptt;
126 int chapter = cell;
127 int pgc_id,pgn;
128 if(title < 0 || cell < 0){
129 return 0;
131 /* for most DVD's chapter == cell */
132 /* but there are more complecated cases... */
133 if(chapter >= dvd->vmg_file->tt_srpt->title[title].nr_of_ptts) {
134 chapter = dvd->vmg_file->tt_srpt->title[title].nr_of_ptts-1;
136 title = dvd->tt_srpt->title[title].vts_ttn-1;
137 ptt = dvd->vts_file->vts_ptt_srpt->title[title].ptt;
138 while(chapter >= 0) {
139 pgc_id = ptt[chapter].pgcn;
140 pgn = ptt[chapter].pgn;
141 cur_pgc = dvd->vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
142 if(cell >= cur_pgc->program_map[pgn-1]-1) {
143 return chapter;
145 --chapter;
147 /* didn't find a chapter ??? */
148 return chapter;
151 int dvd_lang_from_aid(stream_t *stream, int id) {
152 dvd_priv_t *d;
153 int i;
154 if (!stream) return 0;
155 d = stream->priv;
156 if (!d) return 0;
157 for(i=0;i<d->nr_of_channels;i++) {
158 if(d->audio_streams[i].id==id)
159 return d->audio_streams[i].language;
161 return 0;
164 int dvd_aid_from_lang(stream_t *stream, unsigned char* lang) {
165 dvd_priv_t *d=stream->priv;
166 int code,i;
167 if(lang) {
168 while(strlen(lang)>=2) {
169 code=lang[1]|(lang[0]<<8);
170 for(i=0;i<d->nr_of_channels;i++) {
171 if(d->audio_streams[i].language==code) {
172 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Selected DVD audio channel: %d language: %c%c\n",
173 d->audio_streams[i].id, lang[0],lang[1]);
174 return d->audio_streams[i].id;
176 //printf("%X != %X (%c%c)\n",code,d->audio_streams[i].language,lang[0],lang[1]);
178 lang+=2; while (lang[0]==',' || lang[0]==' ') ++lang;
180 mp_tmsg(MSGT_OPEN,MSGL_WARN,"No matching DVD audio language found!\n");
182 return -1;
185 int dvd_number_of_subs(stream_t *stream) {
186 int i;
187 int maxid = -1;
188 dvd_priv_t *d;
189 if (!stream) return -1;
190 d = stream->priv;
191 if (!d) return -1;
192 for (i = 0; i < d->nr_of_subtitles; i++)
193 if (d->subtitles[i].id > maxid) maxid = d->subtitles[i].id;
194 return maxid + 1;
197 int dvd_lang_from_sid(stream_t *stream, int id) {
198 int i;
199 dvd_priv_t *d;
200 if (!stream) return 0;
201 d = stream->priv;
202 if (!d) return 0;
203 for (i = 0; i < d->nr_of_subtitles; i++)
204 if (d->subtitles[i].id == id && d->subtitles[i].language) return d->subtitles[i].language;
205 return 0;
208 int dvd_sid_from_lang(stream_t *stream, unsigned char* lang) {
209 dvd_priv_t *d=stream->priv;
210 int code,i;
211 while(lang && strlen(lang)>=2) {
212 code=lang[1]|(lang[0]<<8);
213 for(i=0;i<d->nr_of_subtitles;i++) {
214 if(d->subtitles[i].language==code) {
215 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Selected DVD subtitle channel: %d language: %c%c\n", i, lang[0],lang[1]);
216 return d->subtitles[i].id;
219 lang+=2;
220 while (lang[0]==',' || lang[0]==' ') ++lang;
222 mp_tmsg(MSGT_OPEN,MSGL_WARN,"No matching DVD subtitle language found!\n");
223 return -1;
226 static int dvd_next_cell(dvd_priv_t *d) {
227 int next_cell=d->cur_cell;
229 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next1=0x%X \n",next_cell);
230 if( d->cur_pgc->cell_playback[ next_cell ].block_type == BLOCK_TYPE_ANGLE_BLOCK ) {
231 while(next_cell<d->last_cell) {
232 if( d->cur_pgc->cell_playback[next_cell].block_mode == BLOCK_MODE_LAST_CELL )
233 break;
234 ++next_cell;
237 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next2=0x%X \n",next_cell);
239 ++next_cell;
240 if(next_cell>=d->last_cell)
241 return -1; // EOF
242 if(d->cur_pgc->cell_playback[next_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK ) {
243 next_cell+=dvd_angle;
244 if(next_cell>=d->last_cell)
245 return -1; // EOF
247 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next3=0x%X \n",next_cell);
248 return next_cell;
251 static int dvd_read_sector(dvd_priv_t *d,unsigned char* data) {
252 int len;
254 if(d->packs_left==0) {
256 * If we're not at the end of this cell, we can determine the next
257 * VOBU to display using the VOBU_SRI information section of the
258 * DSI. Using this value correctly follows the current angle,
259 * avoiding the doubled scenes in The Matrix, and makes our life
260 * really happy.
262 * Otherwise, we set our next address past the end of this cell to
263 * force the code above to go to the next cell in the program.
265 if(d->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
266 d->cur_pack= d->dsi_pack.dsi_gi.nv_pck_lbn + ( d->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
267 mp_msg(MSGT_DVD,MSGL_DBG2, "Navi new pos=0x%X \n",d->cur_pack);
268 } else {
269 // end of cell! find next cell!
270 mp_msg(MSGT_DVD,MSGL_V, "--- END OF CELL !!! ---\n");
271 d->cur_pack=d->cell_last_pack+1;
275 read_next:
276 if(d->cur_pack>d->cell_last_pack) {
277 // end of cell!
278 int next=dvd_next_cell(d);
279 if(next>=0) {
280 d->cur_cell=next;
281 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
282 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
283 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
284 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
285 mp_msg(MSGT_DVD,MSGL_V, "DVD next cell: %d pack: 0x%X-0x%X \n",d->cur_cell,d->cur_pack,d->cell_last_pack);
286 } else
287 return -1; // EOF
290 len = DVDReadBlocks(d->title, d->cur_pack, 1, data);
291 // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss
292 if(len <= 0) return -1; //error
294 if(data[38]==0 && data[39]==0 && data[40]==1 && data[41]==0xBF &&
295 data[1024]==0 && data[1025]==0 && data[1026]==1 && data[1027]==0xBF) {
296 // found a Navi packet!!!
297 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
298 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]));
299 #else
300 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t));
301 #endif
302 if(d->cur_pack != d->dsi_pack.dsi_gi.nv_pck_lbn ) {
303 mp_msg(MSGT_DVD,MSGL_V, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
304 d->cur_pack,d->dsi_pack.dsi_gi.nv_pck_lbn);
305 } else {
306 // process!
307 d->packs_left = d->dsi_pack.dsi_gi.vobu_ea;
308 mp_msg(MSGT_DVD,MSGL_DBG2, "Found NAVI packet! lba=0x%X len=%d \n",d->cur_pack,d->packs_left);
309 //navPrint_DSI(&d->dsi_pack);
310 mp_msg(MSGT_DVD,MSGL_DBG3,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d->cur_cell,
311 d->dsi_pack.dsi_gi.vobu_c_idn,d->dsi_pack.dsi_gi.vobu_vob_idn,
312 d->cur_pgc->cell_position[d->cur_cell].cell_nr,
313 d->cur_pgc->cell_position[d->cur_cell].vob_id_nr);
315 if(d->angle_seek) {
316 int i,skip=0;
317 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
318 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
319 // it generates incorrect code for unaligned access to a packed
320 // structure member, resulting in an mplayer crash with a SIGBUS
321 // signal.
323 // See also gcc problem report PR c/7847:
324 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
325 for(i=0;i<9;i++) { // check if all values zero:
326 __typeof__(d->dsi_pack.sml_agli.data[i].address) tmp_addr;
327 memcpy(&tmp_addr,&d->dsi_pack.sml_agli.data[i].address,sizeof(tmp_addr));
328 if((skip=tmp_addr)!=0) break;
330 #else
331 for(i=0;i<9;i++) // check if all values zero:
332 if((skip=d->dsi_pack.sml_agli.data[i].address)!=0) break;
333 #endif
334 if(skip && skip!=0x7fffffff) {
335 // sml_agli table has valid data (at least one non-zero):
336 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
337 d->dsi_pack.sml_agli.data[dvd_angle].address;
338 d->angle_seek=0;
339 d->cur_pack--;
340 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d->cur_pack);
341 } else {
342 // check if we're in the right cell, jump otherwise:
343 if( (d->dsi_pack.dsi_gi.vobu_c_idn==d->cur_pgc->cell_position[d->cur_cell].cell_nr) &&
344 (d->dsi_pack.dsi_gi.vobu_vob_idn==d->cur_pgc->cell_position[d->cur_cell].vob_id_nr) ){
345 d->angle_seek=0;
346 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced by cell/vob IDN search! \n");
347 } else {
348 // wrong angle, skip this vobu:
349 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
350 d->dsi_pack.dsi_gi.vobu_ea;
351 d->angle_seek=2; // DEBUG
356 ++d->cur_pack;
357 goto read_next;
360 ++d->cur_pack;
361 if(d->packs_left>=0) --d->packs_left;
363 if(d->angle_seek) {
364 if(d->angle_seek==2) mp_msg(MSGT_DVD,MSGL_V, "!!! warning! reading packet while angle_seek !!!\n");
365 goto read_next; // searching for Navi packet
368 return d->cur_pack-1;
371 static void dvd_seek(dvd_priv_t *d,int pos) {
372 d->packs_left=-1;
373 d->cur_pack=pos;
375 // check if we stay in current cell (speedup things, and avoid angle skip)
376 if(d->cur_pack>d->cell_last_pack ||
377 d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector) {
379 // ok, cell change, find the right cell!
380 d->cur_cell=0;
381 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
382 d->cur_cell+=dvd_angle;
384 while(1) {
385 int next;
386 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
387 if(d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector) {
388 d->cur_pack=d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
389 break;
391 if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :)
392 next=dvd_next_cell(d);
393 if(next<0) {
394 //d->cur_pack=d->cell_last_pack+1;
395 break; // we're after the last cell
397 d->cur_cell=next;
401 mp_msg(MSGT_DVD,MSGL_V, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
402 d->cur_pack,d->cur_cell,d->cur_pgc->cell_playback[ d->cur_cell ].first_sector,d->cell_last_pack);
404 // if we're in interleaved multi-angle cell, find the right angle chain!
405 // (read Navi block, and use the seamless angle jump table)
406 d->angle_seek=1;
409 static void dvd_close(dvd_priv_t *d) {
410 ifoClose(d->vts_file);
411 ifoClose(d->vmg_file);
412 DVDCloseFile(d->title);
413 DVDClose(d->dvd);
414 dvd_chapter = 1;
415 dvd_last_chapter = 0;
416 dvd_set_speed(dvd_device_current, -1); /* -1 => restore default */
419 static int fill_buffer(stream_t *s, char *but, int len)
421 if(s->type == STREAMTYPE_DVD) {
422 off_t pos=dvd_read_sector(s->priv,s->buffer);
423 if(pos>=0) {
424 len=2048; // full sector
425 s->pos=2048*pos-len;
426 } else len=-1; // error
428 return len;
431 static int seek(stream_t *s, off_t newpos) {
432 s->pos=newpos; // real seek
433 dvd_seek(s->priv,s->pos/2048);
434 return 1;
437 static void stream_dvd_close(stream_t *s) {
438 dvd_close(s->priv);
441 static int mp_get_titleset_length(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no)
443 int vts_ttn; ///< title number within video title set
444 int pgc_no; ///< program chain number
445 int msec; ///< time length in milliseconds
447 msec=0;
448 if(!vts_file || !tt_srpt)
449 return 0;
451 if(vts_file->vtsi_mat && vts_file->vts_pgcit)
453 vts_ttn = tt_srpt->title[title_no].vts_ttn - 1;
454 pgc_no = vts_file->vts_ptt_srpt->title[vts_ttn].ptt[0].pgcn - 1;
455 msec = mp_dvdtimetomsec(&vts_file->vts_pgcit->pgci_srp[pgc_no].pgc->playback_time);
457 return msec;
461 static int mp_describe_titleset(dvd_reader_t *dvd, tt_srpt_t *tt_srpt, int vts_no)
463 ifo_handle_t *vts_file;
464 int title_no, msec=0;
466 vts_file = ifoOpen(dvd, vts_no);
467 if(!vts_file)
468 return 0;
470 if(!vts_file->vtsi_mat || !vts_file->vts_pgcit)
472 ifoClose(vts_file);
473 return 0;
476 for(title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
478 if (tt_srpt->title[title_no].title_set_nr != vts_no)
479 continue;
480 msec = mp_get_titleset_length(vts_file, tt_srpt, title_no);
481 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no + 1, msec / 1000, msec % 1000);
483 ifoClose(vts_file);
484 return 1;
487 static int seek_to_chapter(stream_t *stream, ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no, int chapter)
489 int cell;
490 ptt_info_t ptt;
491 pgc_t *pgc;
492 off_t pos;
494 if(!vts_file || !tt_srpt)
495 return 0;
497 if(title_no < 0 || title_no >= tt_srpt->nr_of_srpts)
498 return 0;
500 // map global title to vts title
501 title_no = tt_srpt->title[title_no].vts_ttn - 1;
503 if(title_no < 0 || title_no >= vts_file->vts_ptt_srpt->nr_of_srpts)
504 return 0;
506 if(chapter < 0 || chapter > vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts-1) //no such chapter
507 return 0;
509 ptt = vts_file->vts_ptt_srpt->title[title_no].ptt[chapter];
510 pgc = vts_file->vts_pgcit->pgci_srp[ptt.pgcn-1].pgc;
512 cell = pgc->program_map[ptt.pgn - 1] - 1;
513 pos = (off_t) pgc->cell_playback[cell].first_sector * 2048;
514 mp_msg(MSGT_OPEN,MSGL_V,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64"\n",
515 chapter, pgc->cell_playback[cell].first_sector, pos);
516 stream_seek(stream, pos);
518 return chapter;
521 static void list_chapters(pgc_t *pgc)
523 unsigned int i, cell;
524 unsigned int t=0, t2=0;
526 if(pgc->nr_of_programs < 2)
527 return;
529 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "CHAPTERS: ");
530 for(i=0; i<pgc->nr_of_programs; i++)
532 cell = pgc->program_map[i]; //here the cell is 1-based
533 t2 = t/1000;
534 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "%02d:%02d:%02d,", t2/3600, (t2/60)%60, t2%60);
535 while(i+1<pgc->nr_of_programs && cell < pgc->program_map[i+1]) {
536 if(!(pgc->cell_playback[cell-1].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
537 pgc->cell_playback[cell-1].block_mode != BLOCK_MODE_FIRST_CELL)
539 t += mp_dvdtimetomsec(&pgc->cell_playback[cell-1].playback_time);
540 cell++;
543 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "\n");
546 static double dvd_get_current_time(stream_t *stream, int cell)
548 int i, tm;
549 dvd_priv_t *d = stream->priv;
551 tm=0;
552 if(!cell) cell=d->cur_cell;
553 for(i=0; i<d->cur_cell; i++) {
554 if(d->cur_pgc->cell_playback[i].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
555 d->cur_pgc->cell_playback[i].block_mode != BLOCK_MODE_FIRST_CELL
557 continue;
558 tm += d->cell_times_table[i];
560 tm += mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
562 return (double)tm/1000.0;
565 static int dvd_seek_to_time(stream_t *stream, ifo_handle_t *vts_file, double sec)
567 unsigned int i, j, k, timeunit, ac_time, tmap_sector=0, cell_sector=0, vobu_sector=0;
568 int t=0;
569 double tm, duration;
570 off_t pos = -1;
571 dvd_priv_t *d = stream->priv;
572 vts_tmapt_t *vts_tmapt = vts_file->vts_tmapt;
574 if(!vts_file->vts_tmapt || sec < 0)
575 return 0;
577 duration = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1) / 1000.0f;
578 if(sec > duration)
579 return 0;
581 i=d->cur_pgc_idx;
582 timeunit = vts_tmapt->tmap[i].tmu;
583 for(j = 0; j < vts_tmapt->tmap[i].nr_of_entries; j++) {
584 ac_time = timeunit * (j + 1);
585 if(ac_time >= sec)
586 break;
587 tmap_sector = vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff;
589 //search enclosing cell
590 for(i=0; i<d->cur_pgc->nr_of_cells; i++) {
591 if(tmap_sector >= d->cur_pgc->cell_playback[i].first_sector && tmap_sector <= d->cur_pgc->cell_playback[i].last_sector) {
592 cell_sector = d->cur_pgc->cell_playback[i].first_sector;
593 break;
597 pos = ((off_t)cell_sector)<<11;
598 stream_seek(stream, pos);
599 do {
600 stream_skip(stream, 2048);
601 t = mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
602 } while(!t);
603 tm = dvd_get_current_time(stream, 0);
605 pos = ((off_t)tmap_sector)<<11;
606 stream_seek(stream, pos);
607 //now get current time in terms of the cell+cell time offset
608 memset(&d->dsi_pack.dsi_gi.c_eltm, 0, sizeof(dvd_time_t));
609 while(tm <= sec) {
610 if(!stream_skip(stream, 2048))
611 break;
612 tm = dvd_get_current_time(stream, 0);
614 tmap_sector = stream->pos >> 11;
616 //search closest VOBU sector
617 k=(vts_file->vts_vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE)/4; //entries in the vobu admap
618 for(i=1; i<k; i++) {
619 if(vts_file->vts_vobu_admap->vobu_start_sectors[i] > tmap_sector)
620 break;
622 vobu_sector = vts_file->vts_vobu_admap->vobu_start_sectors[i-1];
623 pos = ((off_t)vobu_sector) << 11;
624 stream_seek(stream, pos);
626 return 1;
629 static int control(stream_t *stream,int cmd,void* arg)
631 dvd_priv_t *d = stream->priv;
632 switch(cmd)
634 case STREAM_CTRL_GET_TIME_LENGTH:
636 *((double *)arg) = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1)/1000.0;
637 return 1;
639 case STREAM_CTRL_GET_NUM_CHAPTERS:
641 if(! d->cur_pgc->nr_of_programs) return STREAM_UNSUPPORTED;
642 *((unsigned int *)arg) = d->cur_pgc->nr_of_programs;
643 return 1;
645 case STREAM_CTRL_SEEK_TO_CHAPTER:
647 int r;
648 r = seek_to_chapter(stream, d->vts_file, d->tt_srpt, d->cur_title-1, *((unsigned int *)arg));
649 if(! r) return STREAM_UNSUPPORTED;
651 return 1;
653 case STREAM_CTRL_GET_CURRENT_CHAPTER:
655 *((unsigned int *)arg) = dvd_chapter_from_cell(d, d->cur_title-1, d->cur_cell);
656 return 1;
658 case STREAM_CTRL_GET_CURRENT_TIME:
660 double tm;
661 tm = dvd_get_current_time(stream, 0);
662 if(tm != -1) {
663 *((double *)arg) = tm;
664 return 1;
666 break;
668 case STREAM_CTRL_SEEK_TO_TIME:
670 if(dvd_seek_to_time(stream, d->vts_file, *((double*)arg)))
671 return 1;
672 break;
674 case STREAM_CTRL_GET_ASPECT_RATIO:
676 *((double *)arg) = !d->vts_file->vtsi_mat->vts_video_attr.display_aspect_ratio ? 4.0/3.0 : 16.0/9.0;
677 return 1;
679 case STREAM_CTRL_GET_NUM_ANGLES:
681 *((int *)arg) = d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles;
682 return 1;
684 case STREAM_CTRL_GET_ANGLE:
686 *((int *)arg) = dvd_angle+1;
687 return 1;
689 case STREAM_CTRL_SET_ANGLE:
691 int ang = *((int *)arg);
692 if(ang>d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles || ang<=0)
693 break;
694 dvd_angle = ang - 1;
695 d->angle_seek = 1;
696 return 1;
699 return STREAM_UNSUPPORTED;
703 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
704 struct stream_priv_s* p = (struct stream_priv_s*)opts;
705 int k;
707 mp_msg(MSGT_OPEN,MSGL_V,"URL: %s\n", stream->url);
708 dvd_title = p->title;
709 if(1){
710 //int ret,ret2;
711 dvd_priv_t *d;
712 int ttn,pgc_id,pgn;
713 dvd_reader_t *dvd;
714 dvd_file_t *title;
715 ifo_handle_t *vmg_file;
716 tt_srpt_t *tt_srpt;
717 ifo_handle_t *vts_file;
718 pgc_t *pgc;
720 * Open the disc.
722 if(p->device)
723 dvd_device_current = p->device;
724 else if(dvd_device)
725 dvd_device_current = dvd_device;
726 else
727 dvd_device_current = DEFAULT_DVD_DEVICE;
728 dvd_set_speed(dvd_device_current, dvd_speed);
729 #if defined(__APPLE__) || defined(__DARWIN__)
730 /* Dynamic DVD drive selection on Darwin */
731 if(!strcmp(dvd_device_current, "/dev/rdiskN")) {
732 int i;
733 size_t len = strlen(dvd_device_current)+1;
734 char *temp_device = malloc(len);
736 for (i = 1; i < 10; i++) {
737 snprintf(temp_device, len, "/dev/rdisk%d", i);
738 dvd = DVDOpen(temp_device);
739 if(!dvd) {
740 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno));
741 } else {
742 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
743 dvd_file_t *dvdfile = DVDOpenFile(dvd,dvd_title,DVD_READ_INFO_FILE);
744 if(!dvdfile) {
745 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno));
746 DVDClose(dvd);
747 continue;
749 DVDCloseFile(dvdfile);
750 #endif
751 break;
754 free(temp_device);
756 if(!dvd) {
757 m_struct_free(&stream_opts,opts);
758 return STREAM_UNSUPPORTED;
760 } else
761 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
763 dvd = DVDOpen(dvd_device_current);
764 if(!dvd) {
765 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",dvd_device_current, strerror(errno));
766 m_struct_free(&stream_opts,opts);
767 return STREAM_UNSUPPORTED;
771 mp_msg(MSGT_OPEN,MSGL_V,"Reading disc structure, please wait...\n");
774 * Load the video manager to find out the information about the titles on
775 * this disc.
777 vmg_file = ifoOpen(dvd, 0);
778 if(!vmg_file) {
779 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Can't open VMG info!\n");
780 DVDClose( dvd );
781 m_struct_free(&stream_opts,opts);
782 return STREAM_UNSUPPORTED;
784 tt_srpt = vmg_file->tt_srpt;
785 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
787 int title_no; ///< title number
788 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLES=%d\n", tt_srpt->nr_of_srpts);
789 for (title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
791 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_ptts);
792 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_angles);
795 if (mp_msg_test(MSGT_IDENTIFY, MSGL_V))
797 char volid[32];
798 unsigned char discid [16]; ///< disk ID, a 128 bit MD5 sum
799 int vts_no; ///< video title set number
800 for (vts_no = 1; vts_no <= vmg_file->vts_atrt->nr_of_vtss; vts_no++)
801 mp_describe_titleset(dvd, tt_srpt, vts_no);
802 if (DVDDiscID(dvd, discid) >= 0)
804 int i;
805 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_DISC_ID=");
806 for (i = 0; i < 16; i ++)
807 mp_msg(MSGT_IDENTIFY, MSGL_V, "%02X", discid[i]);
808 mp_msg(MSGT_IDENTIFY, MSGL_V, "\n");
810 if (DVDUDFVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0 || DVDISOVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0)
811 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_VOLUME_ID=%s\n", volid);
814 * Make sure our title number is valid.
816 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "There are %d titles on this DVD.\n", tt_srpt->nr_of_srpts );
817 if(dvd_title < 1 || dvd_title > tt_srpt->nr_of_srpts) {
818 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Invalid DVD title number: %d\n", dvd_title);
819 ifoClose( vmg_file );
820 DVDClose( dvd );
821 m_struct_free(&stream_opts,opts);
822 return STREAM_UNSUPPORTED;
824 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title);
825 --dvd_title; // remap 1.. -> 0..
827 * Make sure the angle number is valid for this title.
829 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "There are %d angles in this DVD title.\n", tt_srpt->title[dvd_title].nr_of_angles);
830 if(dvd_angle<1 || dvd_angle>tt_srpt->title[dvd_title].nr_of_angles) {
831 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Invalid DVD angle number: %d\n", dvd_angle);
832 goto fail;
834 --dvd_angle; // remap 1.. -> 0..
836 ttn = tt_srpt->title[dvd_title].vts_ttn - 1;
838 * Load the VTS information for the title set our title is in.
840 vts_file = ifoOpen( dvd, tt_srpt->title[dvd_title].title_set_nr );
841 if(!vts_file) {
842 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Cannot open the IFO file for DVD title %d.\n", tt_srpt->title[dvd_title].title_set_nr );
843 goto fail;
846 * We've got enough info, time to open the title set data.
848 title = DVDOpenFile(dvd, tt_srpt->title[dvd_title].title_set_nr, DVD_READ_TITLE_VOBS);
849 if(!title) {
850 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Cannot open title VOBS (VTS_%02d_1.VOB).\n", tt_srpt->title[dvd_title].title_set_nr);
851 ifoClose( vts_file );
852 goto fail;
855 mp_msg(MSGT_OPEN,MSGL_V, "DVD successfully opened.\n");
856 // store data
857 d=malloc(sizeof(dvd_priv_t)); memset(d,0,sizeof(dvd_priv_t));
858 d->dvd=dvd;
859 d->title=title;
860 d->vmg_file=vmg_file;
861 d->tt_srpt=tt_srpt;
862 d->vts_file=vts_file;
863 d->cur_title = dvd_title+1;
865 pgc = vts_file->vts_pgcit ? vts_file->vts_pgcit->pgci_srp[ttn].pgc : NULL;
867 * Check number of audio channels and types
870 d->nr_of_channels=0;
871 if(vts_file->vts_pgcit) {
872 int i;
873 for(i=0;i<8;i++)
874 if(pgc->audio_control[i] & 0x8000) {
875 audio_attr_t * audio = &vts_file->vtsi_mat->vts_audio_attr[i];
876 int language = 0;
877 char tmp[] = "unknown";
878 stream_language_t *audio_stream = &d->audio_streams[d->nr_of_channels];
880 if(audio->lang_type == 1) {
881 language=audio->lang_code;
882 tmp[0]=language>>8;
883 tmp[1]=language&0xff;
884 tmp[2]=0;
887 audio_stream->language=language;
888 audio_stream->id=pgc->audio_control[i] >> 8 & 7;
889 switch(audio->audio_format) {
890 case 0: // ac3
891 audio_stream->id+=FIRST_AC3_AID;
892 break;
893 case 6: // dts
894 audio_stream->id+=FIRST_DTS_AID;
895 break;
896 case 2: // mpeg layer 1/2/3
897 case 3: // mpeg2 ext
898 audio_stream->id+=FIRST_MPG_AID;
899 break;
900 case 4: // lpcm
901 audio_stream->id+=FIRST_PCM_AID;
902 break;
905 audio_stream->type=audio->audio_format;
906 // Pontscho: to my mind, tha channels:
907 // 1 - stereo
908 // 5 - 5.1
909 audio_stream->channels=audio->channels;
910 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"audio stream: %d format: %s (%s) language: %s aid: %d.\n",
911 d->nr_of_channels,
912 dvd_audio_stream_types[ audio->audio_format ],
913 dvd_audio_stream_channels[ audio->channels ],
914 tmp,
915 audio_stream->id
917 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", audio_stream->id);
918 if(language && tmp[0])
919 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", audio_stream->id, tmp);
921 d->nr_of_channels++;
924 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"number of audio channels on disk: %d.\n",d->nr_of_channels );
928 * Check number of subtitles and language
931 int i;
933 d->nr_of_subtitles=0;
934 for(i=0;i<32;i++)
935 if(pgc->subp_control[i] & 0x80000000) {
936 subp_attr_t * subtitle = &vts_file->vtsi_mat->vts_subp_attr[i];
937 video_attr_t *video = &vts_file->vtsi_mat->vts_video_attr;
938 int language = 0;
939 char tmp[] = "unknown";
940 stream_language_t *sub_stream = &d->subtitles[d->nr_of_subtitles];
942 if(subtitle->type == 1) {
943 language=subtitle->lang_code;
944 tmp[0]=language>>8;
945 tmp[1]=language&0xff;
946 tmp[2]=0;
949 sub_stream->language=language;
950 sub_stream->id=d->nr_of_subtitles;
951 if(video->display_aspect_ratio == 0) /* 4:3 */
952 sub_stream->id = pgc->subp_control[i] >> 24 & 31;
953 else if(video->display_aspect_ratio == 3) /* 16:9 */
954 sub_stream->id = pgc->subp_control[i] >> 8 & 31;
956 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"subtitle ( sid ): %d language: %s\n", sub_stream->id, tmp);
957 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sub_stream->id);
958 if(language && tmp[0])
959 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sub_stream->id, tmp);
960 d->nr_of_subtitles++;
962 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"number of subtitles on disk: %d\n",d->nr_of_subtitles);
966 * Determine which program chain we want to watch. This is based on the
967 * chapter number.
969 pgc_id = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgcn; // local
970 pgn = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgn; // local
971 d->cur_pgc_idx = pgc_id-1;
972 d->cur_pgc = vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
973 d->cur_cell = d->cur_pgc->program_map[pgn-1] - 1; // start playback here
974 d->packs_left=-1; // for Navi stuff
975 d->angle_seek=0;
976 d->last_cell=d->cur_pgc->nr_of_cells;
978 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
979 d->cur_cell+=dvd_angle;
980 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
981 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
982 mp_msg(MSGT_DVD,MSGL_V, "DVD start cell: %d pack: 0x%X-0x%X \n",d->cur_cell,d->cur_pack,d->cell_last_pack);
984 //assign cell_times_table
985 d->cell_times_table = malloc(sizeof(unsigned int) * d->cur_pgc->nr_of_cells);
986 if(d->cell_times_table == NULL)
987 return STREAM_UNSUPPORTED;
988 for(k=0; k<d->cur_pgc->nr_of_cells; k++)
989 d->cell_times_table[k] = mp_dvdtimetomsec(&d->cur_pgc->cell_playback[k].playback_time);
990 list_chapters(d->cur_pgc);
992 // ... (unimplemented)
993 // return NULL;
994 stream->type = STREAMTYPE_DVD;
995 stream->sector_size = 2048;
996 stream->flags = STREAM_READ | MP_STREAM_SEEK;
997 stream->fill_buffer = fill_buffer;
998 stream->seek = seek;
999 stream->control = control;
1000 stream->close = stream_dvd_close;
1001 stream->start_pos = (off_t)d->cur_pack*2048;
1002 stream->end_pos = (off_t)(d->cur_pgc->cell_playback[d->last_cell-1].last_sector)*2048;
1003 *file_format = DEMUXER_TYPE_MPEG_PS;
1004 mp_msg(MSGT_DVD,MSGL_V,"DVD start=%d end=%d \n",d->cur_pack,d->cur_pgc->cell_playback[d->last_cell-1].last_sector);
1005 stream->priv = (void*)d;
1006 return STREAM_OK;
1008 fail:
1009 ifoClose(vmg_file);
1010 DVDClose(dvd);
1011 m_struct_free(&stream_opts, opts);
1012 return STREAM_UNSUPPORTED;
1014 mp_tmsg(MSGT_DVD,MSGL_ERR,"MPlayer was compiled without DVD support, exiting.\n");
1015 m_struct_free(&stream_opts,opts);
1016 return STREAM_UNSUPPORTED;
1019 static int ifo_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1021 char* filename;
1022 struct stream_priv_s *spriv;
1023 int len = strlen(stream->url);
1025 if (len < 4 || strcasecmp (stream->url + len - 4, ".ifo"))
1026 return STREAM_UNSUPPORTED;
1028 mp_msg(MSGT_DVD, MSGL_INFO, ".IFO detected. Redirecting to dvd://\n");
1030 filename = strdup(basename(stream->url));
1032 spriv=calloc(1, sizeof(struct stream_priv_s));
1033 spriv->device = strdup(dirname(stream->url));
1034 if(!strncasecmp(filename,"vts_",4))
1036 if(sscanf(filename+3, "_%02d_", &spriv->title)!=1)
1037 spriv->title=1;
1038 }else
1039 spriv->title=1;
1041 free(filename);
1042 free(stream->url);
1043 stream->url=strdup("dvd://");
1045 return open_s(stream, mode, spriv, file_format);
1048 const stream_info_t stream_info_dvd = {
1049 "DVD stream",
1050 "null",
1053 open_s,
1054 { "dvd", NULL },
1055 &stream_opts,
1056 1 // Urls are an option string
1059 const stream_info_t stream_info_ifo = {
1060 "DVD IFO input",
1061 "ifo",
1062 "Benjamin Zores",
1063 "Mostly used to play DVDs on disk through OSD Menu",
1064 ifo_stream_open,
1065 { "file", "", NULL },
1066 NULL,