demux: take chapter/attachment name strings without 0-termination
[mplayer/glamo.git] / stream / stream_dvd.c
blobb5894c6c13faa176faca231122d602be440257aa
3 #include <ctype.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <string.h>
10 #include "config.h"
11 #include "mp_msg.h"
12 #include "help_mp.h"
14 #include <libgen.h>
15 #include <errno.h>
17 #define FIRST_AC3_AID 128
18 #define FIRST_DTS_AID 136
19 #define FIRST_MPG_AID 0
20 #define FIRST_PCM_AID 160
22 #include "stream.h"
23 #include "m_option.h"
24 #include "m_struct.h"
26 #include "stream_dvd.h"
27 #include "stream_dvd_common.h"
28 #include "libmpdemux/demuxer.h"
30 static char* dvd_device_current;
31 int dvd_angle=1;
33 #define LIBDVDREAD_VERSION(maj,min,micro) ((maj)*10000 + (min)*100 + (micro))
35 * Try to autodetect the libdvd-0.9.0 library
36 * (0.9.0 removed the <dvdread/dvd_udf.h> header, and moved the two defines
37 * DVD_VIDEO_LB_LEN and MAX_UDF_FILE_NAME_LEN from it to
38 * <dvdread/dvd_reader.h>)
40 #ifndef DVDREAD_VERSION
41 #if defined(DVD_VIDEO_LB_LEN) && defined(MAX_UDF_FILE_NAME_LEN)
42 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,9,0)
43 #else
44 #define DVDREAD_VERSION LIBDVDREAD_VERSION(0,8,0)
45 #endif
46 #endif
49 static struct stream_priv_s {
50 int title;
51 char* device;
52 } stream_priv_dflts = {
54 NULL
57 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
58 /// URL definition
59 static const m_option_t stream_opts_fields[] = {
60 { "hostname", ST_OFF(title), CONF_TYPE_INT, M_OPT_RANGE, 1, 99, NULL},
61 { "filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0 ,0, NULL},
62 { NULL, NULL, 0, 0, 0, 0, NULL }
64 static const struct m_struct_st stream_opts = {
65 "dvd",
66 sizeof(struct stream_priv_s),
67 &stream_priv_dflts,
68 stream_opts_fields
71 int dvd_parse_chapter_range(const m_option_t *conf, const char *range) {
72 const char *s;
73 char *t;
74 if (!range)
75 return M_OPT_MISSING_PARAM;
76 s = range;
77 dvd_chapter = 1;
78 dvd_last_chapter = 0;
79 if(*range && isdigit(*range)) {
80 dvd_chapter = strtol(range, &s, 10);
81 if(range == s) {
82 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
83 return M_OPT_INVALID;
86 if(*s == 0)
87 return 0;
88 else if(*s != '-') {
89 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
90 return M_OPT_INVALID;
92 ++s;
93 if(*s == 0)
94 return 0;
95 if(! isdigit(*s)) {
96 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
97 return M_OPT_INVALID;
99 dvd_last_chapter = strtol(s, &t, 10);
100 if (s == t || *t) {
101 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Invalid chapter range specification %s\n", range);
102 return M_OPT_INVALID;
104 return 0;
107 int dvd_chapter_from_cell(dvd_priv_t* dvd,int title,int cell)
109 pgc_t * cur_pgc;
110 ptt_info_t* ptt;
111 int chapter = cell;
112 int pgc_id,pgn;
113 if(title < 0 || cell < 0){
114 return 0;
116 /* for most DVD's chapter == cell */
117 /* but there are more complecated cases... */
118 if(chapter >= dvd->vmg_file->tt_srpt->title[title].nr_of_ptts) {
119 chapter = dvd->vmg_file->tt_srpt->title[title].nr_of_ptts-1;
121 title = dvd->tt_srpt->title[title].vts_ttn-1;
122 ptt = dvd->vts_file->vts_ptt_srpt->title[title].ptt;
123 while(chapter >= 0) {
124 pgc_id = ptt[chapter].pgcn;
125 pgn = ptt[chapter].pgn;
126 cur_pgc = dvd->vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
127 if(cell >= cur_pgc->program_map[pgn-1]-1) {
128 return chapter;
130 --chapter;
132 /* didn't find a chapter ??? */
133 return chapter;
136 int dvd_lang_from_aid(stream_t *stream, int id) {
137 dvd_priv_t *d;
138 int i;
139 if (!stream) return 0;
140 d = stream->priv;
141 if (!d) return 0;
142 for(i=0;i<d->nr_of_channels;i++) {
143 if(d->audio_streams[i].id==id)
144 return d->audio_streams[i].language;
146 return 0;
149 int dvd_aid_from_lang(stream_t *stream, unsigned char* lang) {
150 dvd_priv_t *d=stream->priv;
151 int code,i;
152 if(lang) {
153 while(strlen(lang)>=2) {
154 code=lang[1]|(lang[0]<<8);
155 for(i=0;i<d->nr_of_channels;i++) {
156 if(d->audio_streams[i].language==code) {
157 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Selected DVD audio channel: %d language: %c%c\n",
158 d->audio_streams[i].id, lang[0],lang[1]);
159 return d->audio_streams[i].id;
161 //printf("%X != %X (%c%c)\n",code,d->audio_streams[i].language,lang[0],lang[1]);
163 lang+=2; while (lang[0]==',' || lang[0]==' ') ++lang;
165 mp_tmsg(MSGT_OPEN,MSGL_WARN,"No matching DVD audio language found!\n");
167 return -1;
170 int dvd_number_of_subs(stream_t *stream) {
171 int i;
172 int maxid = -1;
173 dvd_priv_t *d;
174 if (!stream) return -1;
175 d = stream->priv;
176 if (!d) return -1;
177 for (i = 0; i < d->nr_of_subtitles; i++)
178 if (d->subtitles[i].id > maxid) maxid = d->subtitles[i].id;
179 return maxid + 1;
182 int dvd_lang_from_sid(stream_t *stream, int id) {
183 int i;
184 dvd_priv_t *d;
185 if (!stream) return 0;
186 d = stream->priv;
187 if (!d) return 0;
188 for (i = 0; i < d->nr_of_subtitles; i++)
189 if (d->subtitles[i].id == id && d->subtitles[i].language) return d->subtitles[i].language;
190 return 0;
193 int dvd_sid_from_lang(stream_t *stream, unsigned char* lang) {
194 dvd_priv_t *d=stream->priv;
195 int code,i;
196 while(lang && strlen(lang)>=2) {
197 code=lang[1]|(lang[0]<<8);
198 for(i=0;i<d->nr_of_subtitles;i++) {
199 if(d->subtitles[i].language==code) {
200 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Selected DVD subtitle channel: %d language: %c%c\n", i, lang[0],lang[1]);
201 return d->subtitles[i].id;
204 lang+=2;
205 while (lang[0]==',' || lang[0]==' ') ++lang;
207 mp_tmsg(MSGT_OPEN,MSGL_WARN,"No matching DVD subtitle language found!\n");
208 return -1;
211 static int dvd_next_cell(dvd_priv_t *d) {
212 int next_cell=d->cur_cell;
214 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next1=0x%X \n",next_cell);
215 if( d->cur_pgc->cell_playback[ next_cell ].block_type == BLOCK_TYPE_ANGLE_BLOCK ) {
216 while(next_cell<d->last_cell) {
217 if( d->cur_pgc->cell_playback[next_cell].block_mode == BLOCK_MODE_LAST_CELL )
218 break;
219 ++next_cell;
222 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next2=0x%X \n",next_cell);
224 ++next_cell;
225 if(next_cell>=d->last_cell)
226 return -1; // EOF
227 if(d->cur_pgc->cell_playback[next_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK ) {
228 next_cell+=dvd_angle;
229 if(next_cell>=d->last_cell)
230 return -1; // EOF
232 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next3=0x%X \n",next_cell);
233 return next_cell;
236 static int dvd_read_sector(dvd_priv_t *d,unsigned char* data) {
237 int len;
239 if(d->packs_left==0) {
241 * If we're not at the end of this cell, we can determine the next
242 * VOBU to display using the VOBU_SRI information section of the
243 * DSI. Using this value correctly follows the current angle,
244 * avoiding the doubled scenes in The Matrix, and makes our life
245 * really happy.
247 * Otherwise, we set our next address past the end of this cell to
248 * force the code above to go to the next cell in the program.
250 if(d->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
251 d->cur_pack= d->dsi_pack.dsi_gi.nv_pck_lbn + ( d->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
252 mp_msg(MSGT_DVD,MSGL_DBG2, "Navi new pos=0x%X \n",d->cur_pack);
253 } else {
254 // end of cell! find next cell!
255 mp_msg(MSGT_DVD,MSGL_V, "--- END OF CELL !!! ---\n");
256 d->cur_pack=d->cell_last_pack+1;
260 read_next:
261 if(d->cur_pack>d->cell_last_pack) {
262 // end of cell!
263 int next=dvd_next_cell(d);
264 if(next>=0) {
265 d->cur_cell=next;
266 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
267 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
268 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
269 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
270 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);
271 } else
272 return -1; // EOF
275 len = DVDReadBlocks(d->title, d->cur_pack, 1, data);
276 // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss
277 if(len <= 0) return -1; //error
279 if(data[38]==0 && data[39]==0 && data[40]==1 && data[41]==0xBF &&
280 data[1024]==0 && data[1025]==0 && data[1026]==1 && data[1027]==0xBF) {
281 // found a Navi packet!!!
282 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
283 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]));
284 #else
285 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t));
286 #endif
287 if(d->cur_pack != d->dsi_pack.dsi_gi.nv_pck_lbn ) {
288 mp_msg(MSGT_DVD,MSGL_V, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
289 d->cur_pack,d->dsi_pack.dsi_gi.nv_pck_lbn);
290 } else {
291 // process!
292 d->packs_left = d->dsi_pack.dsi_gi.vobu_ea;
293 mp_msg(MSGT_DVD,MSGL_DBG2, "Found NAVI packet! lba=0x%X len=%d \n",d->cur_pack,d->packs_left);
294 //navPrint_DSI(&d->dsi_pack);
295 mp_msg(MSGT_DVD,MSGL_DBG3,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d->cur_cell,
296 d->dsi_pack.dsi_gi.vobu_c_idn,d->dsi_pack.dsi_gi.vobu_vob_idn,
297 d->cur_pgc->cell_position[d->cur_cell].cell_nr,
298 d->cur_pgc->cell_position[d->cur_cell].vob_id_nr);
300 if(d->angle_seek) {
301 int i,skip=0;
302 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
303 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
304 // it generates incorrect code for unaligned access to a packed
305 // structure member, resulting in an mplayer crash with a SIGBUS
306 // signal.
308 // See also gcc problem report PR c/7847:
309 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
310 for(i=0;i<9;i++) { // check if all values zero:
311 __typeof__(d->dsi_pack.sml_agli.data[i].address) tmp_addr;
312 memcpy(&tmp_addr,&d->dsi_pack.sml_agli.data[i].address,sizeof(tmp_addr));
313 if((skip=tmp_addr)!=0) break;
315 #else
316 for(i=0;i<9;i++) // check if all values zero:
317 if((skip=d->dsi_pack.sml_agli.data[i].address)!=0) break;
318 #endif
319 if(skip && skip!=0x7fffffff) {
320 // sml_agli table has valid data (at least one non-zero):
321 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
322 d->dsi_pack.sml_agli.data[dvd_angle].address;
323 d->angle_seek=0;
324 d->cur_pack--;
325 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d->cur_pack);
326 } else {
327 // check if we're in the right cell, jump otherwise:
328 if( (d->dsi_pack.dsi_gi.vobu_c_idn==d->cur_pgc->cell_position[d->cur_cell].cell_nr) &&
329 (d->dsi_pack.dsi_gi.vobu_vob_idn==d->cur_pgc->cell_position[d->cur_cell].vob_id_nr) ){
330 d->angle_seek=0;
331 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced by cell/vob IDN search! \n");
332 } else {
333 // wrong angle, skip this vobu:
334 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
335 d->dsi_pack.dsi_gi.vobu_ea;
336 d->angle_seek=2; // DEBUG
341 ++d->cur_pack;
342 goto read_next;
345 ++d->cur_pack;
346 if(d->packs_left>=0) --d->packs_left;
348 if(d->angle_seek) {
349 if(d->angle_seek==2) mp_msg(MSGT_DVD,MSGL_V, "!!! warning! reading packet while angle_seek !!!\n");
350 goto read_next; // searching for Navi packet
353 return d->cur_pack-1;
356 static void dvd_seek(dvd_priv_t *d,int pos) {
357 d->packs_left=-1;
358 d->cur_pack=pos;
360 // check if we stay in current cell (speedup things, and avoid angle skip)
361 if(d->cur_pack>d->cell_last_pack ||
362 d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector) {
364 // ok, cell change, find the right cell!
365 d->cur_cell=0;
366 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
367 d->cur_cell+=dvd_angle;
369 while(1) {
370 int next;
371 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
372 if(d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector) {
373 d->cur_pack=d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
374 break;
376 if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :)
377 next=dvd_next_cell(d);
378 if(next<0) {
379 //d->cur_pack=d->cell_last_pack+1;
380 break; // we're after the last cell
382 d->cur_cell=next;
386 mp_msg(MSGT_DVD,MSGL_V, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
387 d->cur_pack,d->cur_cell,d->cur_pgc->cell_playback[ d->cur_cell ].first_sector,d->cell_last_pack);
389 // if we're in interleaved multi-angle cell, find the right angle chain!
390 // (read Navi block, and use the seamless angle jump table)
391 d->angle_seek=1;
394 static void dvd_close(dvd_priv_t *d) {
395 ifoClose(d->vts_file);
396 ifoClose(d->vmg_file);
397 DVDCloseFile(d->title);
398 DVDClose(d->dvd);
399 dvd_chapter = 1;
400 dvd_last_chapter = 0;
401 dvd_set_speed(dvd_device_current, -1); /* -1 => restore default */
404 static int fill_buffer(stream_t *s, char *but, int len)
406 if(s->type == STREAMTYPE_DVD) {
407 off_t pos=dvd_read_sector(s->priv,s->buffer);
408 if(pos>=0) {
409 len=2048; // full sector
410 s->pos=2048*pos-len;
411 } else len=-1; // error
413 return len;
416 static int seek(stream_t *s, off_t newpos) {
417 s->pos=newpos; // real seek
418 dvd_seek(s->priv,s->pos/2048);
419 return 1;
422 static void stream_dvd_close(stream_t *s) {
423 dvd_close(s->priv);
426 static int mp_get_titleset_length(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no)
428 int vts_ttn; ///< title number within video title set
429 int pgc_no; ///< program chain number
430 int msec; ///< time length in milliseconds
432 msec=0;
433 if(!vts_file || !tt_srpt)
434 return 0;
436 if(vts_file->vtsi_mat && vts_file->vts_pgcit)
438 vts_ttn = tt_srpt->title[title_no].vts_ttn - 1;
439 pgc_no = vts_file->vts_ptt_srpt->title[vts_ttn].ptt[0].pgcn - 1;
440 msec = mp_dvdtimetomsec(&vts_file->vts_pgcit->pgci_srp[pgc_no].pgc->playback_time);
442 return msec;
446 static int mp_describe_titleset(dvd_reader_t *dvd, tt_srpt_t *tt_srpt, int vts_no)
448 ifo_handle_t *vts_file;
449 int title_no, msec=0;
451 vts_file = ifoOpen(dvd, vts_no);
452 if(!vts_file)
453 return 0;
455 if(!vts_file->vtsi_mat || !vts_file->vts_pgcit)
457 ifoClose(vts_file);
458 return 0;
461 for(title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
463 if (tt_srpt->title[title_no].title_set_nr != vts_no)
464 continue;
465 msec = mp_get_titleset_length(vts_file, tt_srpt, title_no);
466 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no + 1, msec / 1000, msec % 1000);
468 ifoClose(vts_file);
469 return 1;
472 static int seek_to_chapter(stream_t *stream, ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no, int chapter)
474 int cell;
475 ptt_info_t ptt;
476 pgc_t *pgc;
477 off_t pos;
479 if(!vts_file || !tt_srpt)
480 return 0;
482 if(title_no < 0 || title_no >= tt_srpt->nr_of_srpts)
483 return 0;
485 // map global title to vts title
486 title_no = tt_srpt->title[title_no].vts_ttn - 1;
488 if(title_no < 0 || title_no >= vts_file->vts_ptt_srpt->nr_of_srpts)
489 return 0;
491 if(chapter < 0 || chapter > vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts-1) //no such chapter
492 return 0;
494 ptt = vts_file->vts_ptt_srpt->title[title_no].ptt[chapter];
495 pgc = vts_file->vts_pgcit->pgci_srp[ptt.pgcn-1].pgc;
497 cell = pgc->program_map[ptt.pgn - 1] - 1;
498 pos = (off_t) pgc->cell_playback[cell].first_sector * 2048;
499 mp_msg(MSGT_OPEN,MSGL_V,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64"\n",
500 chapter, pgc->cell_playback[cell].first_sector, pos);
501 stream_seek(stream, pos);
503 return chapter;
506 static void list_chapters(pgc_t *pgc)
508 unsigned int i, cell;
509 unsigned int t=0, t2=0;
511 if(pgc->nr_of_programs < 2)
512 return;
514 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "CHAPTERS: ");
515 for(i=0; i<pgc->nr_of_programs; i++)
517 cell = pgc->program_map[i]; //here the cell is 1-based
518 t2 = t/1000;
519 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "%02d:%02d:%02d,", t2/3600, (t2/60)%60, t2%60);
520 while(i+1<pgc->nr_of_programs && cell < pgc->program_map[i+1]) {
521 if(!(pgc->cell_playback[cell-1].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
522 pgc->cell_playback[cell-1].block_mode != BLOCK_MODE_FIRST_CELL)
524 t += mp_dvdtimetomsec(&pgc->cell_playback[cell-1].playback_time);
525 cell++;
528 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "\n");
531 static double dvd_get_current_time(stream_t *stream, int cell)
533 int i, tm;
534 dvd_priv_t *d = stream->priv;
536 tm=0;
537 if(!cell) cell=d->cur_cell;
538 for(i=0; i<d->cur_cell; i++) {
539 if(d->cur_pgc->cell_playback[i].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
540 d->cur_pgc->cell_playback[i].block_mode != BLOCK_MODE_FIRST_CELL
542 continue;
543 tm += d->cell_times_table[i];
545 tm += mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
547 return (double)tm/1000.0;
550 static int dvd_seek_to_time(stream_t *stream, ifo_handle_t *vts_file, double sec)
552 unsigned int i, j, k, timeunit, ac_time, tmap_sector=0, cell_sector=0, vobu_sector=0;
553 int t=0;
554 double tm, duration;
555 off_t pos = -1;
556 dvd_priv_t *d = stream->priv;
557 vts_tmapt_t *vts_tmapt = vts_file->vts_tmapt;
559 if(!vts_file->vts_tmapt || sec < 0)
560 return 0;
562 duration = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1) / 1000.0f;
563 if(sec > duration)
564 return 0;
566 i=d->cur_pgc_idx;
567 timeunit = vts_tmapt->tmap[i].tmu;
568 for(j = 0; j < vts_tmapt->tmap[i].nr_of_entries; j++) {
569 ac_time = timeunit * (j + 1);
570 if(ac_time >= sec)
571 break;
572 tmap_sector = vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff;
574 //search enclosing cell
575 for(i=0; i<d->cur_pgc->nr_of_cells; i++) {
576 if(tmap_sector >= d->cur_pgc->cell_playback[i].first_sector && tmap_sector <= d->cur_pgc->cell_playback[i].last_sector) {
577 cell_sector = d->cur_pgc->cell_playback[i].first_sector;
578 break;
582 pos = ((off_t)cell_sector)<<11;
583 stream_seek(stream, pos);
584 do {
585 stream_skip(stream, 2048);
586 t = mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
587 } while(!t);
588 tm = dvd_get_current_time(stream, 0);
590 pos = ((off_t)tmap_sector)<<11;
591 stream_seek(stream, pos);
592 //now get current time in terms of the cell+cell time offset
593 memset(&d->dsi_pack.dsi_gi.c_eltm, 0, sizeof(dvd_time_t));
594 while(tm <= sec) {
595 if(!stream_skip(stream, 2048))
596 break;
597 tm = dvd_get_current_time(stream, 0);
599 tmap_sector = stream->pos >> 11;
601 //search closest VOBU sector
602 k=(vts_file->vts_vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE)/4; //entries in the vobu admap
603 for(i=1; i<k; i++) {
604 if(vts_file->vts_vobu_admap->vobu_start_sectors[i] > tmap_sector)
605 break;
607 vobu_sector = vts_file->vts_vobu_admap->vobu_start_sectors[i-1];
608 pos = ((off_t)vobu_sector) << 11;
609 stream_seek(stream, pos);
611 return 1;
614 static int control(stream_t *stream,int cmd,void* arg)
616 dvd_priv_t *d = stream->priv;
617 switch(cmd)
619 case STREAM_CTRL_GET_TIME_LENGTH:
621 *((double *)arg) = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1)/1000.0;
622 return 1;
624 case STREAM_CTRL_GET_NUM_CHAPTERS:
626 if(! d->cur_pgc->nr_of_programs) return STREAM_UNSUPPORTED;
627 *((unsigned int *)arg) = d->cur_pgc->nr_of_programs;
628 return 1;
630 case STREAM_CTRL_SEEK_TO_CHAPTER:
632 int r;
633 r = seek_to_chapter(stream, d->vts_file, d->tt_srpt, d->cur_title-1, *((unsigned int *)arg));
634 if(! r) return STREAM_UNSUPPORTED;
636 return 1;
638 case STREAM_CTRL_GET_CURRENT_CHAPTER:
640 *((unsigned int *)arg) = dvd_chapter_from_cell(d, d->cur_title-1, d->cur_cell);
641 return 1;
643 case STREAM_CTRL_GET_CURRENT_TIME:
645 double tm;
646 tm = dvd_get_current_time(stream, 0);
647 if(tm != -1) {
648 *((double *)arg) = tm;
649 return 1;
651 break;
653 case STREAM_CTRL_SEEK_TO_TIME:
655 if(dvd_seek_to_time(stream, d->vts_file, *((double*)arg)))
656 return 1;
657 break;
659 case STREAM_CTRL_GET_ASPECT_RATIO:
661 *((double *)arg) = !d->vts_file->vtsi_mat->vts_video_attr.display_aspect_ratio ? 4.0/3.0 : 16.0/9.0;
662 return 1;
664 case STREAM_CTRL_GET_NUM_ANGLES:
666 *((int *)arg) = d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles;
667 return 1;
669 case STREAM_CTRL_GET_ANGLE:
671 *((int *)arg) = dvd_angle+1;
672 return 1;
674 case STREAM_CTRL_SET_ANGLE:
676 int ang = *((int *)arg);
677 if(ang>d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles || ang<=0)
678 break;
679 dvd_angle = ang - 1;
680 d->angle_seek = 1;
681 return 1;
684 return STREAM_UNSUPPORTED;
688 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
689 struct stream_priv_s* p = (struct stream_priv_s*)opts;
690 int k;
692 mp_msg(MSGT_OPEN,MSGL_V,"URL: %s\n", stream->url);
693 dvd_title = p->title;
694 if(1){
695 //int ret,ret2;
696 dvd_priv_t *d;
697 int ttn,pgc_id,pgn;
698 dvd_reader_t *dvd;
699 dvd_file_t *title;
700 ifo_handle_t *vmg_file;
701 tt_srpt_t *tt_srpt;
702 ifo_handle_t *vts_file;
703 pgc_t *pgc;
705 * Open the disc.
707 if(p->device)
708 dvd_device_current = p->device;
709 else if(dvd_device)
710 dvd_device_current = dvd_device;
711 else
712 dvd_device_current = DEFAULT_DVD_DEVICE;
713 dvd_set_speed(dvd_device_current, dvd_speed);
714 #if defined(__APPLE__) || defined(__DARWIN__)
715 /* Dynamic DVD drive selection on Darwin */
716 if(!strcmp(dvd_device_current, "/dev/rdiskN")) {
717 int i;
718 size_t len = strlen(dvd_device_current)+1;
719 char *temp_device = malloc(len);
721 for (i = 1; i < 10; i++) {
722 snprintf(temp_device, len, "/dev/rdisk%d", i);
723 dvd = DVDOpen(temp_device);
724 if(!dvd) {
725 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno));
726 } else {
727 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
728 dvd_file_t *dvdfile = DVDOpenFile(dvd,dvd_title,DVD_READ_INFO_FILE);
729 if(!dvdfile) {
730 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno));
731 DVDClose(dvd);
732 continue;
734 DVDCloseFile(dvdfile);
735 #endif
736 break;
739 free(temp_device);
741 if(!dvd) {
742 m_struct_free(&stream_opts,opts);
743 return STREAM_UNSUPPORTED;
745 } else
746 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
748 dvd = DVDOpen(dvd_device_current);
749 if(!dvd) {
750 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",dvd_device_current, strerror(errno));
751 m_struct_free(&stream_opts,opts);
752 return STREAM_UNSUPPORTED;
756 mp_msg(MSGT_OPEN,MSGL_V,"Reading disc structure, please wait...\n");
759 * Load the video manager to find out the information about the titles on
760 * this disc.
762 vmg_file = ifoOpen(dvd, 0);
763 if(!vmg_file) {
764 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Can't open VMG info!\n");
765 DVDClose( dvd );
766 m_struct_free(&stream_opts,opts);
767 return STREAM_UNSUPPORTED;
769 tt_srpt = vmg_file->tt_srpt;
770 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
772 int title_no; ///< title number
773 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLES=%d\n", tt_srpt->nr_of_srpts);
774 for (title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
776 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_ptts);
777 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_angles);
780 if (mp_msg_test(MSGT_IDENTIFY, MSGL_V))
782 char volid[32];
783 unsigned char discid [16]; ///< disk ID, a 128 bit MD5 sum
784 int vts_no; ///< video title set number
785 for (vts_no = 1; vts_no <= vmg_file->vts_atrt->nr_of_vtss; vts_no++)
786 mp_describe_titleset(dvd, tt_srpt, vts_no);
787 if (DVDDiscID(dvd, discid) >= 0)
789 int i;
790 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_DISC_ID=");
791 for (i = 0; i < 16; i ++)
792 mp_msg(MSGT_IDENTIFY, MSGL_V, "%02X", discid[i]);
793 mp_msg(MSGT_IDENTIFY, MSGL_V, "\n");
795 if (DVDUDFVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0 || DVDISOVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0)
796 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_VOLUME_ID=%s\n", volid);
799 * Make sure our title number is valid.
801 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "There are %d titles on this DVD.\n", tt_srpt->nr_of_srpts );
802 if(dvd_title < 1 || dvd_title > tt_srpt->nr_of_srpts) {
803 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Invalid DVD title number: %d\n", dvd_title);
804 ifoClose( vmg_file );
805 DVDClose( dvd );
806 m_struct_free(&stream_opts,opts);
807 return STREAM_UNSUPPORTED;
809 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title);
810 --dvd_title; // remap 1.. -> 0..
812 * Make sure the angle number is valid for this title.
814 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "There are %d angles in this DVD title.\n", tt_srpt->title[dvd_title].nr_of_angles);
815 if(dvd_angle<1 || dvd_angle>tt_srpt->title[dvd_title].nr_of_angles) {
816 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Invalid DVD angle number: %d\n", dvd_angle);
817 goto fail;
819 --dvd_angle; // remap 1.. -> 0..
821 ttn = tt_srpt->title[dvd_title].vts_ttn - 1;
823 * Load the VTS information for the title set our title is in.
825 vts_file = ifoOpen( dvd, tt_srpt->title[dvd_title].title_set_nr );
826 if(!vts_file) {
827 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Cannot open the IFO file for DVD title %d.\n", tt_srpt->title[dvd_title].title_set_nr );
828 goto fail;
831 * We've got enough info, time to open the title set data.
833 title = DVDOpenFile(dvd, tt_srpt->title[dvd_title].title_set_nr, DVD_READ_TITLE_VOBS);
834 if(!title) {
835 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Cannot open title VOBS (VTS_%02d_1.VOB).\n", tt_srpt->title[dvd_title].title_set_nr);
836 ifoClose( vts_file );
837 goto fail;
840 mp_msg(MSGT_OPEN,MSGL_V, "DVD successfully opened.\n");
841 // store data
842 d=malloc(sizeof(dvd_priv_t)); memset(d,0,sizeof(dvd_priv_t));
843 d->dvd=dvd;
844 d->title=title;
845 d->vmg_file=vmg_file;
846 d->tt_srpt=tt_srpt;
847 d->vts_file=vts_file;
848 d->cur_title = dvd_title+1;
850 pgc = vts_file->vts_pgcit ? vts_file->vts_pgcit->pgci_srp[ttn].pgc : NULL;
852 * Check number of audio channels and types
855 d->nr_of_channels=0;
856 if(vts_file->vts_pgcit) {
857 int i;
858 for(i=0;i<8;i++)
859 if(pgc->audio_control[i] & 0x8000) {
860 audio_attr_t * audio = &vts_file->vtsi_mat->vts_audio_attr[i];
861 int language = 0;
862 char tmp[] = "unknown";
863 stream_language_t *audio_stream = &d->audio_streams[d->nr_of_channels];
865 if(audio->lang_type == 1) {
866 language=audio->lang_code;
867 tmp[0]=language>>8;
868 tmp[1]=language&0xff;
869 tmp[2]=0;
872 audio_stream->language=language;
873 audio_stream->id=pgc->audio_control[i] >> 8 & 7;
874 switch(audio->audio_format) {
875 case 0: // ac3
876 audio_stream->id+=FIRST_AC3_AID;
877 break;
878 case 6: // dts
879 audio_stream->id+=FIRST_DTS_AID;
880 break;
881 case 2: // mpeg layer 1/2/3
882 case 3: // mpeg2 ext
883 audio_stream->id+=FIRST_MPG_AID;
884 break;
885 case 4: // lpcm
886 audio_stream->id+=FIRST_PCM_AID;
887 break;
890 audio_stream->type=audio->audio_format;
891 // Pontscho: to my mind, tha channels:
892 // 1 - stereo
893 // 5 - 5.1
894 audio_stream->channels=audio->channels;
895 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"audio stream: %d format: %s (%s) language: %s aid: %d.\n",
896 d->nr_of_channels,
897 dvd_audio_stream_types[ audio->audio_format ],
898 dvd_audio_stream_channels[ audio->channels ],
899 tmp,
900 audio_stream->id
902 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", audio_stream->id);
903 if(language && tmp[0])
904 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", audio_stream->id, tmp);
906 d->nr_of_channels++;
909 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"number of audio channels on disk: %d.\n",d->nr_of_channels );
913 * Check number of subtitles and language
916 int i;
918 d->nr_of_subtitles=0;
919 for(i=0;i<32;i++)
920 if(pgc->subp_control[i] & 0x80000000) {
921 subp_attr_t * subtitle = &vts_file->vtsi_mat->vts_subp_attr[i];
922 video_attr_t *video = &vts_file->vtsi_mat->vts_video_attr;
923 int language = 0;
924 char tmp[] = "unknown";
925 stream_language_t *sub_stream = &d->subtitles[d->nr_of_subtitles];
927 if(subtitle->type == 1) {
928 language=subtitle->lang_code;
929 tmp[0]=language>>8;
930 tmp[1]=language&0xff;
931 tmp[2]=0;
934 sub_stream->language=language;
935 sub_stream->id=d->nr_of_subtitles;
936 if(video->display_aspect_ratio == 0) /* 4:3 */
937 sub_stream->id = pgc->subp_control[i] >> 24 & 31;
938 else if(video->display_aspect_ratio == 3) /* 16:9 */
939 sub_stream->id = pgc->subp_control[i] >> 8 & 31;
941 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"subtitle ( sid ): %d language: %s\n", sub_stream->id, tmp);
942 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sub_stream->id);
943 if(language && tmp[0])
944 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sub_stream->id, tmp);
945 d->nr_of_subtitles++;
947 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"number of subtitles on disk: %d\n",d->nr_of_subtitles);
951 * Determine which program chain we want to watch. This is based on the
952 * chapter number.
954 pgc_id = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgcn; // local
955 pgn = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgn; // local
956 d->cur_pgc_idx = pgc_id-1;
957 d->cur_pgc = vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
958 d->cur_cell = d->cur_pgc->program_map[pgn-1] - 1; // start playback here
959 d->packs_left=-1; // for Navi stuff
960 d->angle_seek=0;
961 d->last_cell=d->cur_pgc->nr_of_cells;
963 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
964 d->cur_cell+=dvd_angle;
965 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
966 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
967 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);
969 //assign cell_times_table
970 d->cell_times_table = malloc(sizeof(unsigned int) * d->cur_pgc->nr_of_cells);
971 if(d->cell_times_table == NULL)
972 return STREAM_UNSUPPORTED;
973 for(k=0; k<d->cur_pgc->nr_of_cells; k++)
974 d->cell_times_table[k] = mp_dvdtimetomsec(&d->cur_pgc->cell_playback[k].playback_time);
975 list_chapters(d->cur_pgc);
977 // ... (unimplemented)
978 // return NULL;
979 stream->type = STREAMTYPE_DVD;
980 stream->sector_size = 2048;
981 stream->flags = STREAM_READ | MP_STREAM_SEEK;
982 stream->fill_buffer = fill_buffer;
983 stream->seek = seek;
984 stream->control = control;
985 stream->close = stream_dvd_close;
986 stream->start_pos = (off_t)d->cur_pack*2048;
987 stream->end_pos = (off_t)(d->cur_pgc->cell_playback[d->last_cell-1].last_sector)*2048;
988 *file_format = DEMUXER_TYPE_MPEG_PS;
989 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);
990 stream->priv = (void*)d;
991 return STREAM_OK;
993 fail:
994 ifoClose(vmg_file);
995 DVDClose(dvd);
996 m_struct_free(&stream_opts, opts);
997 return STREAM_UNSUPPORTED;
999 mp_tmsg(MSGT_DVD,MSGL_ERR,"MPlayer was compiled without DVD support, exiting.\n");
1000 m_struct_free(&stream_opts,opts);
1001 return STREAM_UNSUPPORTED;
1004 static int ifo_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1006 char* filename;
1007 struct stream_priv_s *spriv;
1008 int len = strlen(stream->url);
1010 if (len < 4 || strcasecmp (stream->url + len - 4, ".ifo"))
1011 return STREAM_UNSUPPORTED;
1013 mp_msg(MSGT_DVD, MSGL_INFO, ".IFO detected. Redirecting to dvd://\n");
1015 filename = strdup(basename(stream->url));
1017 spriv=calloc(1, sizeof(struct stream_priv_s));
1018 spriv->device = strdup(dirname(stream->url));
1019 if(!strncasecmp(filename,"vts_",4))
1021 if(sscanf(filename+3, "_%02d_", &spriv->title)!=1)
1022 spriv->title=1;
1023 }else
1024 spriv->title=1;
1026 free(filename);
1027 free(stream->url);
1028 stream->url=strdup("dvd://");
1030 return open_s(stream, mode, spriv, file_format);
1033 const stream_info_t stream_info_dvd = {
1034 "DVD stream",
1035 "null",
1038 open_s,
1039 { "dvd", NULL },
1040 &stream_opts,
1041 1 // Urls are an option string
1044 const stream_info_t stream_info_ifo = {
1045 "DVD IFO input",
1046 "ifo",
1047 "Benjamin Zores",
1048 "Mostly used to play DVDs on disk through OSD Menu",
1049 ifo_stream_open,
1050 { "file", "", NULL },
1051 NULL,