codecs.conf: add matchware screen codec fourcc MWSC
[mplayer/glamo.git] / stream / stream_dvd.c
blob0a93c187678aae74528622a4bcee095e156775c2
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_chapter_from_cell(dvd_priv_t* dvd,int title,int cell)
88 pgc_t * cur_pgc;
89 ptt_info_t* ptt;
90 int chapter = cell;
91 int pgc_id,pgn;
92 if(title < 0 || cell < 0){
93 return 0;
95 /* for most DVD's chapter == cell */
96 /* but there are more complecated cases... */
97 if(chapter >= dvd->vmg_file->tt_srpt->title[title].nr_of_ptts) {
98 chapter = dvd->vmg_file->tt_srpt->title[title].nr_of_ptts-1;
100 title = dvd->tt_srpt->title[title].vts_ttn-1;
101 ptt = dvd->vts_file->vts_ptt_srpt->title[title].ptt;
102 while(chapter >= 0) {
103 pgc_id = ptt[chapter].pgcn;
104 pgn = ptt[chapter].pgn;
105 cur_pgc = dvd->vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
106 if(cell >= cur_pgc->program_map[pgn-1]-1) {
107 return chapter;
109 --chapter;
111 /* didn't find a chapter ??? */
112 return chapter;
115 int dvd_lang_from_aid(stream_t *stream, int id) {
116 dvd_priv_t *d;
117 int i;
118 if (!stream) return 0;
119 d = stream->priv;
120 if (!d) return 0;
121 for(i=0;i<d->nr_of_channels;i++) {
122 if(d->audio_streams[i].id==id)
123 return d->audio_streams[i].language;
125 return 0;
128 int dvd_aid_from_lang(stream_t *stream, const unsigned char* lang) {
129 dvd_priv_t *d=stream->priv;
130 int code,i;
131 if(lang) {
132 while(strlen(lang)>=2) {
133 code=lang[1]|(lang[0]<<8);
134 for(i=0;i<d->nr_of_channels;i++) {
135 if(d->audio_streams[i].language==code) {
136 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Selected DVD audio channel: %d language: %c%c\n",
137 d->audio_streams[i].id, lang[0],lang[1]);
138 return d->audio_streams[i].id;
140 //printf("%X != %X (%c%c)\n",code,d->audio_streams[i].language,lang[0],lang[1]);
142 lang+=2; while (lang[0]==',' || lang[0]==' ') ++lang;
144 mp_tmsg(MSGT_OPEN,MSGL_WARN,"No matching DVD audio language found!\n");
146 return -1;
149 int dvd_number_of_subs(stream_t *stream) {
150 int i;
151 int maxid = -1;
152 dvd_priv_t *d;
153 if (!stream) return -1;
154 d = stream->priv;
155 if (!d) return -1;
156 for (i = 0; i < d->nr_of_subtitles; i++)
157 if (d->subtitles[i].id > maxid) maxid = d->subtitles[i].id;
158 return maxid + 1;
161 int dvd_lang_from_sid(stream_t *stream, int id) {
162 int i;
163 dvd_priv_t *d;
164 if (!stream) return 0;
165 d = stream->priv;
166 if (!d) return 0;
167 for (i = 0; i < d->nr_of_subtitles; i++)
168 if (d->subtitles[i].id == id && d->subtitles[i].language) return d->subtitles[i].language;
169 return 0;
172 int dvd_sid_from_lang(stream_t *stream, const unsigned char* lang) {
173 dvd_priv_t *d=stream->priv;
174 int code,i;
175 while(lang && strlen(lang)>=2) {
176 code=lang[1]|(lang[0]<<8);
177 for(i=0;i<d->nr_of_subtitles;i++) {
178 if(d->subtitles[i].language==code) {
179 mp_tmsg(MSGT_OPEN,MSGL_INFO,"Selected DVD subtitle channel: %d language: %c%c\n", i, lang[0],lang[1]);
180 return d->subtitles[i].id;
183 lang+=2;
184 while (lang[0]==',' || lang[0]==' ') ++lang;
186 mp_tmsg(MSGT_OPEN,MSGL_WARN,"No matching DVD subtitle language found!\n");
187 return -1;
190 static int dvd_next_cell(dvd_priv_t *d) {
191 int next_cell=d->cur_cell;
193 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next1=0x%X \n",next_cell);
194 if( d->cur_pgc->cell_playback[ next_cell ].block_type == BLOCK_TYPE_ANGLE_BLOCK ) {
195 while(next_cell<d->last_cell) {
196 if( d->cur_pgc->cell_playback[next_cell].block_mode == BLOCK_MODE_LAST_CELL )
197 break;
198 ++next_cell;
201 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next2=0x%X \n",next_cell);
203 ++next_cell;
204 if(next_cell>=d->last_cell)
205 return -1; // EOF
206 if(d->cur_pgc->cell_playback[next_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK ) {
207 next_cell+=dvd_angle;
208 if(next_cell>=d->last_cell)
209 return -1; // EOF
211 mp_msg(MSGT_DVD,MSGL_DBG2, "dvd_next_cell: next3=0x%X \n",next_cell);
212 return next_cell;
215 static int dvd_read_sector(dvd_priv_t *d, unsigned char *data)
217 int len;
219 if(d->packs_left==0) {
221 * If we're not at the end of this cell, we can determine the next
222 * VOBU to display using the VOBU_SRI information section of the
223 * DSI. Using this value correctly follows the current angle,
224 * avoiding the doubled scenes in The Matrix, and makes our life
225 * really happy.
227 * Otherwise, we set our next address past the end of this cell to
228 * force the code above to go to the next cell in the program.
230 if(d->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
231 d->cur_pack= d->dsi_pack.dsi_gi.nv_pck_lbn + ( d->dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
232 mp_msg(MSGT_DVD,MSGL_DBG2, "Navi new pos=0x%X \n",d->cur_pack);
233 } else {
234 // end of cell! find next cell!
235 mp_msg(MSGT_DVD,MSGL_V, "--- END OF CELL !!! ---\n");
236 d->cur_pack=d->cell_last_pack+1;
240 read_next:
241 if(d->cur_pack>d->cell_last_pack) {
242 // end of cell!
243 int next=dvd_next_cell(d);
244 if(next>=0) {
245 d->cur_cell=next;
246 // if( d->cur_pgc->cell_playback[d->cur_cell].block_type
247 // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle;
248 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
249 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
250 mp_msg(MSGT_DVD,MSGL_V, "DVD next cell: %d pack: 0x%X-0x%X \n",d->cur_cell,d->cur_pack,d->cell_last_pack);
251 } else
252 return -1; // EOF
255 len = DVDReadBlocks(d->title, d->cur_pack, 1, data);
256 // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss
257 if(len <= 0) return -1; //error
259 if(data[38]==0 && data[39]==0 && data[40]==1 && data[41]==0xBF &&
260 data[1024]==0 && data[1025]==0 && data[1026]==1 && data[1027]==0xBF) {
261 // found a Navi packet!!!
262 #if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0)
263 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]));
264 #else
265 navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t));
266 #endif
267 if(d->cur_pack != d->dsi_pack.dsi_gi.nv_pck_lbn ) {
268 mp_msg(MSGT_DVD,MSGL_V, "Invalid NAVI packet! lba=0x%X navi=0x%X \n",
269 d->cur_pack,d->dsi_pack.dsi_gi.nv_pck_lbn);
270 } else {
271 // process!
272 d->packs_left = d->dsi_pack.dsi_gi.vobu_ea;
273 mp_msg(MSGT_DVD,MSGL_DBG2, "Found NAVI packet! lba=0x%X len=%d \n",d->cur_pack,d->packs_left);
274 //navPrint_DSI(&d->dsi_pack);
275 mp_msg(MSGT_DVD,MSGL_DBG3,"\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d->cur_cell,
276 d->dsi_pack.dsi_gi.vobu_c_idn,d->dsi_pack.dsi_gi.vobu_vob_idn,
277 d->cur_pgc->cell_position[d->cur_cell].cell_nr,
278 d->cur_pgc->cell_position[d->cur_cell].vob_id_nr);
280 if(d->angle_seek) {
281 int i,skip=0;
282 #if defined(__GNUC__) && ( defined(__sparc__) || defined(hpux) )
283 // workaround for a bug in the sparc/hpux version of gcc 2.95.X ... 3.2,
284 // it generates incorrect code for unaligned access to a packed
285 // structure member, resulting in an mplayer crash with a SIGBUS
286 // signal.
288 // See also gcc problem report PR c/7847:
289 // http://gcc.gnu.org/cgi-bin/gnatsweb.pl?database=gcc&cmd=view+audit-trail&pr=7847
290 for(i=0;i<9;i++) { // check if all values zero:
291 __typeof__(d->dsi_pack.sml_agli.data[i].address) tmp_addr;
292 memcpy(&tmp_addr,&d->dsi_pack.sml_agli.data[i].address,sizeof(tmp_addr));
293 if((skip=tmp_addr)!=0) break;
295 #else
296 for(i=0;i<9;i++) // check if all values zero:
297 if((skip=d->dsi_pack.sml_agli.data[i].address)!=0) break;
298 #endif
299 if(skip && skip!=0x7fffffff) {
300 // sml_agli table has valid data (at least one non-zero):
301 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
302 d->dsi_pack.sml_agli.data[dvd_angle].address;
303 d->angle_seek=0;
304 d->cur_pack--;
305 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d->cur_pack);
306 } else {
307 // check if we're in the right cell, jump otherwise:
308 if( (d->dsi_pack.dsi_gi.vobu_c_idn==d->cur_pgc->cell_position[d->cur_cell].cell_nr) &&
309 (d->dsi_pack.dsi_gi.vobu_vob_idn==d->cur_pgc->cell_position[d->cur_cell].vob_id_nr) ){
310 d->angle_seek=0;
311 mp_msg(MSGT_DVD,MSGL_V, "Angle-seek synced by cell/vob IDN search! \n");
312 } else {
313 // wrong angle, skip this vobu:
314 d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+
315 d->dsi_pack.dsi_gi.vobu_ea;
316 d->angle_seek=2; // DEBUG
321 ++d->cur_pack;
322 goto read_next;
325 ++d->cur_pack;
326 if(d->packs_left>=0) --d->packs_left;
328 if(d->angle_seek) {
329 if(d->angle_seek==2) mp_msg(MSGT_DVD,MSGL_V, "!!! warning! reading packet while angle_seek !!!\n");
330 goto read_next; // searching for Navi packet
333 return d->cur_pack-1;
336 static void dvd_seek(dvd_priv_t *d, int pos)
338 d->packs_left=-1;
339 d->cur_pack=pos;
341 // check if we stay in current cell (speedup things, and avoid angle skip)
342 if(d->cur_pack>d->cell_last_pack ||
343 d->cur_pack<d->cur_pgc->cell_playback[ d->cur_cell ].first_sector) {
345 // ok, cell change, find the right cell!
346 cell_playback_t *cell;
347 for(d->cur_cell=0; d->cur_cell < d->cur_pgc->nr_of_cells; d->cur_cell++) {
348 cell = &(d->cur_pgc->cell_playback[d->cur_cell]);
349 if(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL)
350 continue;
351 d->cell_last_pack=cell->last_sector;
352 if(d->cur_pack<cell->first_sector) {
353 d->cur_pack=cell->first_sector;
354 break;
356 if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :)
360 mp_msg(MSGT_DVD,MSGL_V, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n",
361 d->cur_pack,d->cur_cell,d->cur_pgc->cell_playback[ d->cur_cell ].first_sector,d->cell_last_pack);
363 // if we're in interleaved multi-angle cell, find the right angle chain!
364 // (read Navi block, and use the seamless angle jump table)
365 d->angle_seek=1;
368 static void dvd_close(dvd_priv_t *d)
370 ifoClose(d->vts_file);
371 ifoClose(d->vmg_file);
372 DVDCloseFile(d->title);
373 DVDClose(d->dvd);
374 dvd_set_speed(dvd_device_current, -1); /* -1 => restore default */
377 static int fill_buffer(stream_t *s, char *but, int len)
379 if(s->type == STREAMTYPE_DVD) {
380 off_t pos=dvd_read_sector(s->priv,s->buffer);
381 if(pos>=0) {
382 len=2048; // full sector
383 s->pos=2048*pos-len;
384 } else len=-1; // error
386 return len;
389 static int seek(stream_t *s, off_t newpos) {
390 s->pos=newpos; // real seek
391 dvd_seek(s->priv,s->pos/2048);
392 return 1;
395 static void stream_dvd_close(stream_t *s) {
396 dvd_close(s->priv);
399 static int mp_get_titleset_length(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no)
401 int vts_ttn; ///< title number within video title set
402 int pgc_no; ///< program chain number
403 int msec; ///< time length in milliseconds
405 msec=0;
406 if(!vts_file || !tt_srpt)
407 return 0;
409 if(vts_file->vtsi_mat && vts_file->vts_pgcit)
411 vts_ttn = tt_srpt->title[title_no].vts_ttn - 1;
412 pgc_no = vts_file->vts_ptt_srpt->title[vts_ttn].ptt[0].pgcn - 1;
413 msec = mp_dvdtimetomsec(&vts_file->vts_pgcit->pgci_srp[pgc_no].pgc->playback_time);
415 return msec;
419 static int mp_describe_titleset(dvd_reader_t *dvd, tt_srpt_t *tt_srpt, int vts_no)
421 ifo_handle_t *vts_file;
422 int title_no, msec=0;
424 vts_file = ifoOpen(dvd, vts_no);
425 if(!vts_file)
426 return 0;
428 if(!vts_file->vtsi_mat || !vts_file->vts_pgcit)
430 ifoClose(vts_file);
431 return 0;
434 for(title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
436 if (tt_srpt->title[title_no].title_set_nr != vts_no)
437 continue;
438 msec = mp_get_titleset_length(vts_file, tt_srpt, title_no);
439 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title_no + 1, msec / 1000, msec % 1000);
441 ifoClose(vts_file);
442 return 1;
445 static int get_num_chapter(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no)
447 if(!vts_file || !tt_srpt)
448 return 0;
450 if(title_no < 0 || title_no >= tt_srpt->nr_of_srpts)
451 return 0;
453 // map global title to vts title
454 title_no = tt_srpt->title[title_no].vts_ttn - 1;
456 if(title_no < 0 || title_no >= vts_file->vts_ptt_srpt->nr_of_srpts)
457 return 0;
459 return vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts;
462 static int seek_to_chapter(stream_t *stream, ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no, int chapter)
464 dvd_priv_t *d = stream->priv;
465 ptt_info_t ptt;
466 pgc_t *pgc;
467 off_t pos;
469 if(!vts_file || !tt_srpt)
470 return 0;
472 if(title_no < 0 || title_no >= tt_srpt->nr_of_srpts)
473 return 0;
475 // map global title to vts title
476 title_no = tt_srpt->title[title_no].vts_ttn - 1;
478 if(title_no < 0 || title_no >= vts_file->vts_ptt_srpt->nr_of_srpts)
479 return 0;
481 if(chapter < 0 || chapter > vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts-1) //no such chapter
482 return 0;
484 ptt = vts_file->vts_ptt_srpt->title[title_no].ptt[chapter];
485 pgc = vts_file->vts_pgcit->pgci_srp[ptt.pgcn-1].pgc;
487 d->cur_cell = pgc->program_map[ptt.pgn - 1] - 1;
488 if(pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK)
489 d->cur_cell += dvd_angle;
490 d->cur_pack = pgc->cell_playback[d->cur_cell].first_sector;
491 d->cell_last_pack = pgc->cell_playback[d->cur_cell].last_sector;
493 d->packs_left = -1;
494 d->angle_seek = 0;
496 pos = (off_t) d->cur_pack * 2048;
497 mp_msg(MSGT_OPEN,MSGL_V,"\r\nSTREAM_DVD, seeked to chapter: %d, cell: %u, pos: %"PRIu64"\n",
498 chapter, d->cur_pack, pos);
500 return chapter;
503 static void list_chapters(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no)
505 unsigned int i, cell, last_cell;
506 unsigned int t=0, t2=0;
507 ptt_info_t *ptt;
508 pgc_t *pgc;
510 title_no = tt_srpt->title[title_no].vts_ttn - 1;
511 if(vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts < 2)
512 return;
513 ptt = vts_file->vts_ptt_srpt->title[title_no].ptt;
515 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "CHAPTERS: ");
516 for(i=0; i<vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts; i++)
518 pgc = vts_file->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc;
519 cell = pgc->program_map[ptt[i].pgn-1]; //here the cell is 1-based
520 if(ptt[i].pgn<pgc->nr_of_programs)
521 last_cell = pgc->program_map[ptt[i].pgn];
522 else
523 last_cell = 0;
524 t2 = t/1000;
525 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "%02d:%02d:%02d,", t2/3600, (t2/60)%60, t2%60);
526 do {
527 if(!(pgc->cell_playback[cell-1].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
528 pgc->cell_playback[cell-1].block_mode != BLOCK_MODE_FIRST_CELL)
530 t += mp_dvdtimetomsec(&pgc->cell_playback[cell-1].playback_time);
531 cell++;
532 } while(cell < last_cell);
534 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "\n");
537 static double dvd_get_current_time(stream_t *stream, int cell)
539 int i, tm;
540 dvd_priv_t *d = stream->priv;
542 tm=0;
543 if(!cell) cell=d->cur_cell;
544 for(i=0; i<d->cur_cell; i++) {
545 if(d->cur_pgc->cell_playback[i].block_type == BLOCK_TYPE_ANGLE_BLOCK &&
546 d->cur_pgc->cell_playback[i].block_mode != BLOCK_MODE_FIRST_CELL
548 continue;
549 tm += d->cell_times_table[i];
551 tm += mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
553 return (double)tm/1000.0;
556 static int dvd_seek_to_time(stream_t *stream, ifo_handle_t *vts_file, double sec)
558 unsigned int i, j, k, timeunit, ac_time, tmap_sector=0, cell_sector=0, vobu_sector=0;
559 int t=0;
560 double tm, duration;
561 off_t pos = -1;
562 dvd_priv_t *d = stream->priv;
563 vts_tmapt_t *vts_tmapt = vts_file->vts_tmapt;
565 if(!vts_file->vts_tmapt || sec < 0)
566 return 0;
568 duration = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1) / 1000.0f;
569 if(sec > duration)
570 return 0;
572 i=d->cur_pgc_idx;
573 timeunit = vts_tmapt->tmap[i].tmu;
574 for(j = 0; j < vts_tmapt->tmap[i].nr_of_entries; j++) {
575 ac_time = timeunit * (j + 1);
576 if(ac_time >= sec)
577 break;
578 tmap_sector = vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff;
580 //search enclosing cell
581 for(i=0; i<d->cur_pgc->nr_of_cells; i++) {
582 if(tmap_sector >= d->cur_pgc->cell_playback[i].first_sector && tmap_sector <= d->cur_pgc->cell_playback[i].last_sector) {
583 cell_sector = d->cur_pgc->cell_playback[i].first_sector;
584 break;
588 pos = ((off_t)cell_sector)<<11;
589 stream_seek(stream, pos);
590 do {
591 stream_skip(stream, 2048);
592 t = mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm);
593 } while(!t);
594 tm = dvd_get_current_time(stream, 0);
596 pos = ((off_t)tmap_sector)<<11;
597 stream_seek(stream, pos);
598 //now get current time in terms of the cell+cell time offset
599 memset(&d->dsi_pack.dsi_gi.c_eltm, 0, sizeof(dvd_time_t));
600 while(tm <= sec) {
601 if(!stream_skip(stream, 2048))
602 break;
603 tm = dvd_get_current_time(stream, 0);
605 tmap_sector = stream->pos >> 11;
607 //search closest VOBU sector
608 k=(vts_file->vts_vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE)/4; //entries in the vobu admap
609 for(i=1; i<k; i++) {
610 if(vts_file->vts_vobu_admap->vobu_start_sectors[i] > tmap_sector)
611 break;
613 vobu_sector = vts_file->vts_vobu_admap->vobu_start_sectors[i-1];
614 pos = ((off_t)vobu_sector) << 11;
615 stream_seek(stream, pos);
617 return 1;
620 static int control(stream_t *stream,int cmd,void* arg)
622 dvd_priv_t *d = stream->priv;
623 switch(cmd)
625 case STREAM_CTRL_GET_TIME_LENGTH:
627 *((double *)arg) = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title-1)/1000.0;
628 return 1;
630 case STREAM_CTRL_GET_NUM_CHAPTERS:
632 int r;
633 r = get_num_chapter(d->vts_file, d->tt_srpt, d->cur_title-1);
634 if(! r) return STREAM_UNSUPPORTED;
635 *((unsigned int *)arg) = r;
636 return 1;
638 case STREAM_CTRL_SEEK_TO_CHAPTER:
640 int r;
641 r = seek_to_chapter(stream, d->vts_file, d->tt_srpt, d->cur_title-1, *((unsigned int *)arg));
642 if(! r) return STREAM_UNSUPPORTED;
644 return 1;
646 case STREAM_CTRL_GET_CURRENT_CHAPTER:
648 *((unsigned int *)arg) = dvd_chapter_from_cell(d, d->cur_title-1, d->cur_cell);
649 return 1;
651 case STREAM_CTRL_GET_CURRENT_TIME:
653 double tm;
654 tm = dvd_get_current_time(stream, 0);
655 if(tm != -1) {
656 *((double *)arg) = tm;
657 return 1;
659 break;
661 case STREAM_CTRL_SEEK_TO_TIME:
663 if(dvd_seek_to_time(stream, d->vts_file, *((double*)arg)))
664 return 1;
665 break;
667 case STREAM_CTRL_GET_ASPECT_RATIO:
669 *((double *)arg) = !d->vts_file->vtsi_mat->vts_video_attr.display_aspect_ratio ? 4.0/3.0 : 16.0/9.0;
670 return 1;
672 case STREAM_CTRL_GET_NUM_ANGLES:
674 *((int *)arg) = d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles;
675 return 1;
677 case STREAM_CTRL_GET_ANGLE:
679 *((int *)arg) = dvd_angle+1;
680 return 1;
682 case STREAM_CTRL_SET_ANGLE:
684 int ang = *((int *)arg);
685 if(ang>d->vmg_file->tt_srpt->title[dvd_title].nr_of_angles || ang<=0)
686 break;
687 dvd_angle = ang - 1;
688 d->angle_seek = 1;
689 return 1;
692 return STREAM_UNSUPPORTED;
696 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
697 struct stream_priv_s* p = (struct stream_priv_s*)opts;
698 int k;
700 mp_msg(MSGT_OPEN,MSGL_V,"URL: %s\n", stream->url);
701 dvd_title = p->title;
702 if(1){
703 //int ret,ret2;
704 dvd_priv_t *d;
705 int ttn,pgc_id,pgn;
706 dvd_reader_t *dvd;
707 dvd_file_t *title;
708 ifo_handle_t *vmg_file;
709 tt_srpt_t *tt_srpt;
710 ifo_handle_t *vts_file;
711 pgc_t *pgc;
713 * Open the disc.
715 if(p->device)
716 dvd_device_current = p->device;
717 else if(dvd_device)
718 dvd_device_current = dvd_device;
719 else
720 dvd_device_current = DEFAULT_DVD_DEVICE;
721 dvd_set_speed(dvd_device_current, dvd_speed);
722 #if defined(__APPLE__) || defined(__DARWIN__)
723 /* Dynamic DVD drive selection on Darwin */
724 if(!strcmp(dvd_device_current, "/dev/rdiskN")) {
725 int i;
726 size_t len = strlen(dvd_device_current)+1;
727 char *temp_device = malloc(len);
729 for (i = 1; i < 10; i++) {
730 snprintf(temp_device, len, "/dev/rdisk%d", i);
731 dvd = DVDOpen(temp_device);
732 if(!dvd) {
733 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno));
734 } else {
735 #if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4)
736 dvd_file_t *dvdfile = DVDOpenFile(dvd,dvd_title,DVD_READ_INFO_FILE);
737 if(!dvdfile) {
738 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno));
739 DVDClose(dvd);
740 continue;
742 DVDCloseFile(dvdfile);
743 #endif
744 break;
747 free(temp_device);
749 if(!dvd) {
750 m_struct_free(&stream_opts,opts);
751 return STREAM_UNSUPPORTED;
753 } else
754 #endif /* defined(__APPLE__) || defined(__DARWIN__) */
756 dvd = DVDOpen(dvd_device_current);
757 if(!dvd) {
758 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",dvd_device_current, strerror(errno));
759 m_struct_free(&stream_opts,opts);
760 return STREAM_UNSUPPORTED;
764 mp_msg(MSGT_OPEN,MSGL_V,"Reading disc structure, please wait...\n");
767 * Load the video manager to find out the information about the titles on
768 * this disc.
770 vmg_file = ifoOpen(dvd, 0);
771 if(!vmg_file) {
772 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Can't open VMG info!\n");
773 DVDClose( dvd );
774 m_struct_free(&stream_opts,opts);
775 return STREAM_UNSUPPORTED;
777 tt_srpt = vmg_file->tt_srpt;
778 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
780 int title_no; ///< title number
781 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLES=%d\n", tt_srpt->nr_of_srpts);
782 for (title_no = 0; title_no < tt_srpt->nr_of_srpts; title_no++)
784 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_CHAPTERS=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_ptts);
785 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_TITLE_%d_ANGLES=%d\n", title_no + 1, tt_srpt->title[title_no].nr_of_angles);
788 if (mp_msg_test(MSGT_IDENTIFY, MSGL_V))
790 char volid[32];
791 unsigned char discid [16]; ///< disk ID, a 128 bit MD5 sum
792 int vts_no; ///< video title set number
793 for (vts_no = 1; vts_no <= vmg_file->vts_atrt->nr_of_vtss; vts_no++)
794 mp_describe_titleset(dvd, tt_srpt, vts_no);
795 if (DVDDiscID(dvd, discid) >= 0)
797 int i;
798 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_DISC_ID=");
799 for (i = 0; i < 16; i ++)
800 mp_msg(MSGT_IDENTIFY, MSGL_V, "%02X", discid[i]);
801 mp_msg(MSGT_IDENTIFY, MSGL_V, "\n");
803 if (DVDUDFVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0 || DVDISOVolumeInfo(dvd, volid, sizeof(volid), NULL, 0) >= 0)
804 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_VOLUME_ID=%s\n", volid);
807 * Make sure our title number is valid.
809 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "There are %d titles on this DVD.\n", tt_srpt->nr_of_srpts );
810 if(dvd_title < 1 || dvd_title > tt_srpt->nr_of_srpts) {
811 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Invalid DVD title number: %d\n", dvd_title);
812 ifoClose( vmg_file );
813 DVDClose( dvd );
814 m_struct_free(&stream_opts,opts);
815 return STREAM_UNSUPPORTED;
817 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_DVD_CURRENT_TITLE=%d\n", dvd_title);
818 --dvd_title; // remap 1.. -> 0..
820 * Make sure the angle number is valid for this title.
822 mp_tmsg(MSGT_OPEN,MSGL_STATUS, "There are %d angles in this DVD title.\n", tt_srpt->title[dvd_title].nr_of_angles);
823 if(dvd_angle<1 || dvd_angle>tt_srpt->title[dvd_title].nr_of_angles) {
824 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Invalid DVD angle number: %d\n", dvd_angle);
825 goto fail;
827 --dvd_angle; // remap 1.. -> 0..
829 ttn = tt_srpt->title[dvd_title].vts_ttn - 1;
831 * Load the VTS information for the title set our title is in.
833 vts_file = ifoOpen( dvd, tt_srpt->title[dvd_title].title_set_nr );
834 if(!vts_file) {
835 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Cannot open the IFO file for DVD title %d.\n", tt_srpt->title[dvd_title].title_set_nr );
836 goto fail;
839 * We've got enough info, time to open the title set data.
841 title = DVDOpenFile(dvd, tt_srpt->title[dvd_title].title_set_nr, DVD_READ_TITLE_VOBS);
842 if(!title) {
843 mp_tmsg(MSGT_OPEN,MSGL_ERR, "Cannot open title VOBS (VTS_%02d_1.VOB).\n", tt_srpt->title[dvd_title].title_set_nr);
844 ifoClose( vts_file );
845 goto fail;
848 mp_msg(MSGT_OPEN,MSGL_V, "DVD successfully opened.\n");
849 // store data
850 d=malloc(sizeof(dvd_priv_t)); memset(d,0,sizeof(dvd_priv_t));
851 d->dvd=dvd;
852 d->title=title;
853 d->vmg_file=vmg_file;
854 d->tt_srpt=tt_srpt;
855 d->vts_file=vts_file;
856 d->cur_title = dvd_title+1;
858 pgc = vts_file->vts_pgcit ? vts_file->vts_pgcit->pgci_srp[ttn].pgc : NULL;
860 * Check number of audio channels and types
863 d->nr_of_channels=0;
864 if(vts_file->vts_pgcit) {
865 int i;
866 for(i=0;i<8;i++)
867 if(pgc->audio_control[i] & 0x8000) {
868 audio_attr_t * audio = &vts_file->vtsi_mat->vts_audio_attr[i];
869 int language = 0;
870 char tmp[] = "unknown";
871 stream_language_t *audio_stream = &d->audio_streams[d->nr_of_channels];
873 if(audio->lang_type == 1) {
874 language=audio->lang_code;
875 tmp[0]=language>>8;
876 tmp[1]=language&0xff;
877 tmp[2]=0;
880 audio_stream->language=language;
881 audio_stream->id=pgc->audio_control[i] >> 8 & 7;
882 switch(audio->audio_format) {
883 case 0: // ac3
884 audio_stream->id+=FIRST_AC3_AID;
885 break;
886 case 6: // dts
887 audio_stream->id+=FIRST_DTS_AID;
888 break;
889 case 2: // mpeg layer 1/2/3
890 case 3: // mpeg2 ext
891 audio_stream->id+=FIRST_MPG_AID;
892 break;
893 case 4: // lpcm
894 audio_stream->id+=FIRST_PCM_AID;
895 break;
898 audio_stream->type=audio->audio_format;
899 // Pontscho: to my mind, tha channels:
900 // 1 - stereo
901 // 5 - 5.1
902 audio_stream->channels=audio->channels;
903 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"audio stream: %d format: %s (%s) language: %s aid: %d.\n",
904 d->nr_of_channels,
905 dvd_audio_stream_types[ audio->audio_format ],
906 dvd_audio_stream_channels[ audio->channels ],
907 tmp,
908 audio_stream->id
910 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_ID=%d\n", audio_stream->id);
911 if(language && tmp[0])
912 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", audio_stream->id, tmp);
914 d->nr_of_channels++;
917 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"number of audio channels on disk: %d.\n",d->nr_of_channels );
921 * Check number of subtitles and language
924 int i;
926 d->nr_of_subtitles=0;
927 for(i=0;i<32;i++)
928 if(pgc->subp_control[i] & 0x80000000) {
929 subp_attr_t * subtitle = &vts_file->vtsi_mat->vts_subp_attr[i];
930 video_attr_t *video = &vts_file->vtsi_mat->vts_video_attr;
931 int language = 0;
932 char tmp[] = "unknown";
933 stream_language_t *sub_stream = &d->subtitles[d->nr_of_subtitles];
935 if(subtitle->type == 1) {
936 language=subtitle->lang_code;
937 tmp[0]=language>>8;
938 tmp[1]=language&0xff;
939 tmp[2]=0;
942 sub_stream->language=language;
943 sub_stream->id=d->nr_of_subtitles;
944 if(video->display_aspect_ratio == 0) /* 4:3 */
945 sub_stream->id = pgc->subp_control[i] >> 24 & 31;
946 else if(video->display_aspect_ratio == 3) /* 16:9 */
947 sub_stream->id = pgc->subp_control[i] >> 8 & 31;
949 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"subtitle ( sid ): %d language: %s\n", sub_stream->id, tmp);
950 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SUBTITLE_ID=%d\n", sub_stream->id);
951 if(language && tmp[0])
952 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", sub_stream->id, tmp);
953 d->nr_of_subtitles++;
955 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"number of subtitles on disk: %d\n",d->nr_of_subtitles);
959 * Determine which program chain we want to watch. This is based on the
960 * chapter number.
962 pgc_id = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgcn; // local
963 pgn = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgn; // local
964 d->cur_pgc_idx = pgc_id-1;
965 d->cur_pgc = vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc;
966 d->cur_cell = d->cur_pgc->program_map[pgn-1] - 1; // start playback here
967 d->packs_left=-1; // for Navi stuff
968 d->angle_seek=0;
969 d->last_cell=d->cur_pgc->nr_of_cells;
971 if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK )
972 d->cur_cell+=dvd_angle;
973 d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector;
974 d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector;
975 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);
977 //assign cell_times_table
978 d->cell_times_table = malloc(sizeof(unsigned int) * d->cur_pgc->nr_of_cells);
979 if(d->cell_times_table == NULL)
980 return STREAM_UNSUPPORTED;
981 for(k=0; k<d->cur_pgc->nr_of_cells; k++)
982 d->cell_times_table[k] = mp_dvdtimetomsec(&d->cur_pgc->cell_playback[k].playback_time);
983 list_chapters(vts_file,tt_srpt,dvd_title);
985 // ... (unimplemented)
986 // return NULL;
987 stream->type = STREAMTYPE_DVD;
988 stream->sector_size = 2048;
989 stream->flags = STREAM_READ | MP_STREAM_SEEK;
990 stream->fill_buffer = fill_buffer;
991 stream->seek = seek;
992 stream->control = control;
993 stream->close = stream_dvd_close;
994 stream->start_pos = (off_t)d->cur_pack*2048;
995 stream->end_pos = (off_t)(d->cur_pgc->cell_playback[d->last_cell-1].last_sector)*2048;
996 *file_format = DEMUXER_TYPE_MPEG_PS;
997 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);
998 stream->priv = (void*)d;
999 return STREAM_OK;
1001 fail:
1002 ifoClose(vmg_file);
1003 DVDClose(dvd);
1004 m_struct_free(&stream_opts, opts);
1005 return STREAM_UNSUPPORTED;
1007 mp_tmsg(MSGT_DVD,MSGL_ERR,"MPlayer was compiled without DVD support, exiting.\n");
1008 m_struct_free(&stream_opts,opts);
1009 return STREAM_UNSUPPORTED;
1012 static int ifo_stream_open (stream_t *stream, int mode, void *opts, int *file_format)
1014 char* filename;
1015 struct stream_priv_s *spriv;
1016 int len = strlen(stream->url);
1018 if (len < 4 || strcasecmp (stream->url + len - 4, ".ifo"))
1019 return STREAM_UNSUPPORTED;
1021 mp_msg(MSGT_DVD, MSGL_INFO, ".IFO detected. Redirecting to dvd://\n");
1023 filename = strdup(basename(stream->url));
1025 spriv=calloc(1, sizeof(struct stream_priv_s));
1026 spriv->device = strdup(dirname(stream->url));
1027 if(!strncasecmp(filename,"vts_",4))
1029 if(sscanf(filename+3, "_%02d_", &spriv->title)!=1)
1030 spriv->title=1;
1031 }else
1032 spriv->title=1;
1034 free(filename);
1035 free(stream->url);
1036 stream->url=strdup("dvd://");
1038 return open_s(stream, mode, spriv, file_format);
1041 const stream_info_t stream_info_dvd = {
1042 "DVD stream",
1043 "null",
1046 open_s,
1047 { "dvd", NULL },
1048 &stream_opts,
1049 1 // Urls are an option string
1052 const stream_info_t stream_info_ifo = {
1053 "DVD IFO input",
1054 "ifo",
1055 "Benjamin Zores",
1056 "Mostly used to play DVDs on disk through OSD Menu",
1057 ifo_stream_open,
1058 { "file", "", NULL },
1059 NULL,