spudec: Fix calc_bbox to always return sensible values
[mplayer/glamo.git] / stream / stream_dvdnav.c
blob5932fd049c638a6b4a159eea0133b544671bde3e
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 "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26 #include "options.h"
27 #include "mp_msg.h"
28 #include "osdep/timer.h"
29 #include "input/input.h"
30 #include "stream.h"
31 #include "libmpdemux/demuxer.h"
32 #include <dvdnav/dvdnav.h>
33 #include "stream_dvdnav.h"
34 #include "libvo/video_out.h"
35 #include "libavutil/common.h"
36 #include "spudec.h"
37 #include "m_option.h"
38 #include "m_struct.h"
39 #include "stream_dvd_common.h"
41 /* state flags */
42 typedef enum {
43 NAV_FLAG_EOF = 1 << 0, /* end of stream has been reached */
44 NAV_FLAG_WAIT = 1 << 1, /* wait event */
45 NAV_FLAG_WAIT_SKIP = 1 << 2, /* wait skip disable */
46 NAV_FLAG_CELL_CHANGE = 1 << 3, /* cell change event */
47 NAV_FLAG_WAIT_READ_AUTO = 1 << 4, /* wait read auto mode */
48 NAV_FLAG_WAIT_READ = 1 << 5, /* suspend read from stream */
49 NAV_FLAG_VTS_DOMAIN = 1 << 6, /* vts domain */
50 NAV_FLAG_SPU_SET = 1 << 7, /* spu_clut is valid */
51 NAV_FLAG_STREAM_CHANGE = 1 << 8, /* title, chapter, audio or SPU */
52 NAV_FLAG_AUDIO_CHANGE = 1 << 9, /* audio stream change event */
53 NAV_FLAG_SPU_CHANGE = 1 << 10, /* spu stream change event */
54 } dvdnav_state_t;
56 typedef struct {
57 dvdnav_t * dvdnav; /* handle to libdvdnav stuff */
58 char * filename; /* path */
59 unsigned int duration; /* in milliseconds */
60 int mousex, mousey;
61 int title;
62 unsigned int spu_clut[16];
63 dvdnav_highlight_event_t hlev;
64 int still_length; /* still frame duration */
65 unsigned int state;
66 } dvdnav_priv_t;
68 static struct stream_priv_s {
69 int track;
70 char* device;
71 } stream_priv_dflts = {
73 NULL
76 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
77 /// URL definition
78 static const m_option_t stream_opts_fields[] = {
79 {"filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL },
80 {"hostname", ST_OFF(track), CONF_TYPE_INT, M_OPT_RANGE, 1, 99, NULL},
81 { NULL, NULL, 0, 0, 0, 0, NULL }
83 static const struct m_struct_st stream_opts = {
84 "dvd",
85 sizeof(struct stream_priv_s),
86 &stream_priv_dflts,
87 stream_opts_fields
90 static int seek(stream_t *s, off_t newpos);
91 static void show_audio_subs_languages(dvdnav_t *nav);
93 static dvdnav_priv_t * new_dvdnav_stream(char * filename) {
94 const char * title_str;
95 dvdnav_priv_t *priv;
97 if (!filename)
98 return NULL;
100 if (!(priv=calloc(1,sizeof(dvdnav_priv_t))))
101 return NULL;
103 if (!(priv->filename=strdup(filename))) {
104 free(priv);
105 return NULL;
108 dvd_set_speed(priv->filename, dvd_speed);
110 if(dvdnav_open(&(priv->dvdnav),priv->filename)!=DVDNAV_STATUS_OK)
112 free(priv->filename);
113 free(priv);
114 return NULL;
117 if (!priv->dvdnav) {
118 free(priv);
119 return NULL;
122 if(1) //from vlc: if not used dvdnav from cvs will fail
124 int len, event;
125 char buf[2048];
127 dvdnav_get_next_block(priv->dvdnav,buf,&event,&len);
128 dvdnav_sector_search(priv->dvdnav, 0, SEEK_SET);
131 /* turn off dvdnav caching */
132 dvdnav_set_readahead_flag(priv->dvdnav, 0);
133 if(dvdnav_set_PGC_positioning_flag(priv->dvdnav, 1) != DVDNAV_STATUS_OK)
134 mp_msg(MSGT_OPEN,MSGL_ERR,"stream_dvdnav, failed to set PGC positioning\n");
135 /* report the title?! */
136 if (dvdnav_get_title_string(priv->dvdnav,&title_str)==DVDNAV_STATUS_OK) {
137 mp_msg(MSGT_IDENTIFY, MSGL_INFO,"Title: '%s'\n",title_str);
140 //dvdnav_event_clear(priv);
142 return priv;
145 static void dvdnav_get_highlight (dvdnav_priv_t *priv, int display_mode) {
146 pci_t *pnavpci = NULL;
147 dvdnav_highlight_event_t *hlev = &(priv->hlev);
148 int btnum;
150 if (!priv || !priv->dvdnav)
151 return;
153 pnavpci = dvdnav_get_current_nav_pci (priv->dvdnav);
154 if (!pnavpci)
155 return;
157 dvdnav_get_current_highlight (priv->dvdnav, &(hlev->buttonN));
158 hlev->display = display_mode; /* show */
160 if (hlev->buttonN > 0 && pnavpci->hli.hl_gi.btn_ns > 0 && hlev->display) {
161 for (btnum = 0; btnum < pnavpci->hli.hl_gi.btn_ns; btnum++) {
162 btni_t *btni = &(pnavpci->hli.btnit[btnum]);
164 if (hlev->buttonN == btnum + 1) {
165 hlev->sx = FFMIN (btni->x_start, btni->x_end);
166 hlev->ex = FFMAX (btni->x_start, btni->x_end);
167 hlev->sy = FFMIN (btni->y_start, btni->y_end);
168 hlev->ey = FFMAX (btni->y_start, btni->y_end);
170 hlev->palette = (btni->btn_coln == 0) ? 0 :
171 pnavpci->hli.btn_colit.btn_coli[btni->btn_coln - 1][0];
172 break;
175 } else { /* hide button or no button */
176 hlev->sx = hlev->ex = 0;
177 hlev->sy = hlev->ey = 0;
178 hlev->palette = hlev->buttonN = 0;
182 static inline int dvdnav_get_duration (int length) {
183 return (length == 255) ? 0 : length * 1000;
186 static int dvdnav_stream_read(dvdnav_priv_t * priv, unsigned char *buf, int *len) {
187 int event = DVDNAV_NOP;
189 *len=-1;
190 if (dvdnav_get_next_block(priv->dvdnav,buf,&event,len)!=DVDNAV_STATUS_OK) {
191 mp_msg(MSGT_OPEN,MSGL_V, "Error getting next block from DVD %d (%s)\n",event, dvdnav_err_to_string(priv->dvdnav) );
192 *len=-1;
194 else if (event!=DVDNAV_BLOCK_OK) {
195 // need to handle certain events internally (like skipping stills)
196 switch (event) {
197 case DVDNAV_NAV_PACKET:
198 return event;
199 case DVDNAV_STILL_FRAME: {
200 dvdnav_still_event_t *still_event = (dvdnav_still_event_t *) buf;
201 priv->still_length = still_event->length;
202 /* set still frame duration */
203 priv->duration = dvdnav_get_duration (priv->still_length);
204 if (priv->still_length <= 1) {
205 pci_t *pnavpci = dvdnav_get_current_nav_pci (priv->dvdnav);
206 priv->duration = mp_dvdtimetomsec (&pnavpci->pci_gi.e_eltm);
208 break;
210 case DVDNAV_HIGHLIGHT: {
211 dvdnav_get_highlight (priv, 1);
212 break;
214 case DVDNAV_CELL_CHANGE: {
215 dvdnav_cell_change_event_t *ev = (dvdnav_cell_change_event_t*)buf;
216 uint32_t nextstill;
218 priv->state &= ~NAV_FLAG_WAIT_SKIP;
219 priv->state |= NAV_FLAG_STREAM_CHANGE;
220 if(ev->pgc_length)
221 priv->duration = ev->pgc_length/90;
223 if (dvdnav_is_domain_vts(priv->dvdnav)) {
224 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "DVDNAV_TITLE_IS_MOVIE\n");
225 priv->state &= ~NAV_FLAG_VTS_DOMAIN;
226 } else {
227 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "DVDNAV_TITLE_IS_MENU\n");
228 priv->state |= NAV_FLAG_VTS_DOMAIN;
231 nextstill = dvdnav_get_next_still_flag (priv->dvdnav);
232 if (nextstill) {
233 priv->duration = dvdnav_get_duration (nextstill);
234 priv->still_length = nextstill;
235 if (priv->still_length <= 1) {
236 pci_t *pnavpci = dvdnav_get_current_nav_pci (priv->dvdnav);
237 priv->duration = mp_dvdtimetomsec (&pnavpci->pci_gi.e_eltm);
241 break;
243 case DVDNAV_SPU_CLUT_CHANGE: {
244 memcpy(priv->spu_clut, buf, 16*sizeof(unsigned int));
245 priv->state |= NAV_FLAG_SPU_SET;
246 break;
248 case DVDNAV_WAIT: {
249 if ((priv->state & NAV_FLAG_WAIT_SKIP) &&
250 !(priv->state & NAV_FLAG_WAIT))
251 dvdnav_wait_skip (priv->dvdnav);
252 else
253 priv->state |= NAV_FLAG_WAIT;
254 break;
256 case DVDNAV_VTS_CHANGE: {
257 priv->state &= ~NAV_FLAG_WAIT_SKIP;
258 priv->state |= NAV_FLAG_STREAM_CHANGE;
259 break;
261 case DVDNAV_SPU_STREAM_CHANGE: {
262 priv->state |= NAV_FLAG_STREAM_CHANGE;
263 break;
267 *len=0;
269 return event;
272 static void update_title_len(stream_t *stream) {
273 dvdnav_priv_t *priv = stream->priv;
274 dvdnav_status_t status;
275 uint32_t pos = 0, len = 0;
277 status = dvdnav_get_position(priv->dvdnav, &pos, &len);
278 if(status == DVDNAV_STATUS_OK && len) {
279 stream->end_pos = (off_t) len * 2048;
280 stream->seek = seek;
281 } else {
282 stream->seek = NULL;
283 stream->end_pos = 0;
288 static int seek(stream_t *s, off_t newpos) {
289 uint32_t sector = 0;
290 dvdnav_priv_t *priv = s->priv;
292 if(s->end_pos && newpos > s->end_pos)
293 newpos = s->end_pos;
294 sector = newpos / 2048ULL;
295 if(dvdnav_sector_search(priv->dvdnav, (uint64_t) sector, SEEK_SET) != DVDNAV_STATUS_OK)
296 goto fail;
298 s->pos = newpos;
300 return 1;
302 fail:
303 mp_msg(MSGT_STREAM,MSGL_INFO,"dvdnav_stream, seeking to %"PRIu64" failed: %s\n", newpos, dvdnav_err_to_string(priv->dvdnav));
305 return 1;
308 static void stream_dvdnav_close(stream_t *s) {
309 dvdnav_priv_t *priv = s->priv;
310 dvdnav_close(priv->dvdnav);
311 priv->dvdnav = NULL;
312 dvd_set_speed(priv->filename, -1);
313 free(priv);
317 static int fill_buffer(stream_t *s, char *but, int len)
319 int event;
321 dvdnav_priv_t* priv=s->priv;
322 if (priv->state & NAV_FLAG_WAIT_READ) /* read is suspended */
323 return -1;
324 len=0;
325 if(!s->end_pos)
326 update_title_len(s);
327 while(!len) /* grab all event until DVDNAV_BLOCK_OK (len=2048), DVDNAV_STOP or DVDNAV_STILL_FRAME */
329 event=dvdnav_stream_read(priv, s->buffer, &len);
330 if(event==-1 || len==-1)
332 mp_msg(MSGT_CPLAYER,MSGL_ERR, "DVDNAV stream read error!\n");
333 return 0;
335 if (event != DVDNAV_BLOCK_OK)
336 dvdnav_get_highlight (priv, 1);
337 switch (event) {
338 case DVDNAV_STOP: {
339 priv->state |= NAV_FLAG_EOF;
340 return len;
342 case DVDNAV_BLOCK_OK:
343 case DVDNAV_NAV_PACKET:
344 case DVDNAV_STILL_FRAME:
345 return len;
346 case DVDNAV_WAIT: {
347 if (priv->state & NAV_FLAG_WAIT)
348 return len;
349 break;
351 case DVDNAV_VTS_CHANGE: {
352 int tit = 0, part = 0;
353 dvdnav_vts_change_event_t *vts_event = (dvdnav_vts_change_event_t *)s->buffer;
354 mp_msg(MSGT_CPLAYER,MSGL_INFO, "DVDNAV, switched to title: %d\r\n", vts_event->new_vtsN);
355 priv->state |= NAV_FLAG_CELL_CHANGE;
356 priv->state |= NAV_FLAG_AUDIO_CHANGE;
357 priv->state |= NAV_FLAG_SPU_CHANGE;
358 priv->state &= ~NAV_FLAG_WAIT_SKIP;
359 priv->state &= ~NAV_FLAG_WAIT;
360 s->end_pos = 0;
361 update_title_len(s);
362 show_audio_subs_languages(priv->dvdnav);
363 if (priv->state & NAV_FLAG_WAIT_READ_AUTO)
364 priv->state |= NAV_FLAG_WAIT_READ;
365 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) == DVDNAV_STATUS_OK) {
366 mp_msg(MSGT_CPLAYER,MSGL_V, "\r\nDVDNAV, NEW TITLE %d\r\n", tit);
367 dvdnav_get_highlight (priv, 0);
368 if(priv->title > 0 && tit != priv->title) {
369 priv->state |= NAV_FLAG_EOF;
370 return 0;
373 break;
375 case DVDNAV_CELL_CHANGE: {
376 priv->state |= NAV_FLAG_CELL_CHANGE;
377 priv->state |= NAV_FLAG_AUDIO_CHANGE;
378 priv->state |= NAV_FLAG_SPU_CHANGE;
379 priv->state &= ~NAV_FLAG_WAIT_SKIP;
380 priv->state &= ~NAV_FLAG_WAIT;
381 if (priv->state & NAV_FLAG_WAIT_READ_AUTO)
382 priv->state |= NAV_FLAG_WAIT_READ;
383 if(priv->title > 0 && s->opts->chapterrange[1] > 0) {
384 int tit=0, part=0;
385 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) == DVDNAV_STATUS_OK && part > s->opts->chapterrange[1]) {
386 priv->state |= NAV_FLAG_EOF;
387 return 0;
390 dvdnav_get_highlight (priv, 1);
392 break;
393 case DVDNAV_AUDIO_STREAM_CHANGE:
394 priv->state |= NAV_FLAG_AUDIO_CHANGE;
395 break;
396 case DVDNAV_SPU_STREAM_CHANGE:
397 priv->state |= NAV_FLAG_SPU_CHANGE;
398 break;
401 mp_msg(MSGT_STREAM,MSGL_DBG2,"DVDNAV fill_buffer len: %d\n",len);
402 return len;
405 static int control(stream_t *stream, int cmd, void* arg) {
406 dvdnav_priv_t* priv=stream->priv;
407 int tit, part;
409 switch(cmd)
411 case STREAM_CTRL_SEEK_TO_CHAPTER:
413 int chap = *((unsigned int *)arg)+1;
415 if(chap < 1 || dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
416 break;
417 if(dvdnav_part_play(priv->dvdnav, tit, chap) != DVDNAV_STATUS_OK)
418 break;
419 return 1;
421 case STREAM_CTRL_GET_NUM_CHAPTERS:
423 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
424 break;
425 if(dvdnav_get_number_of_parts(priv->dvdnav, tit, &part) != DVDNAV_STATUS_OK)
426 break;
427 if(!part)
428 break;
429 *((unsigned int *)arg) = part;
430 return 1;
432 case STREAM_CTRL_GET_CURRENT_CHAPTER:
434 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
435 break;
436 *((unsigned int *)arg) = part - 1;
437 return 1;
439 case STREAM_CTRL_GET_TIME_LENGTH:
441 if(priv->duration || priv->still_length)
443 *((double *)arg) = (double)priv->duration / 1000.0;
444 return 1;
446 break;
448 case STREAM_CTRL_GET_ASPECT_RATIO:
450 uint8_t ar = dvdnav_get_video_aspect(priv->dvdnav);
451 *((double *)arg) = !ar ? 4.0/3.0 : 16.0/9.0;
452 return 1;
454 case STREAM_CTRL_GET_CURRENT_TIME:
456 double tm;
457 tm = dvdnav_get_current_time(priv->dvdnav)/90000.0f;
458 if(tm != -1)
460 *((double *)arg) = tm;
461 return 1;
463 break;
465 case STREAM_CTRL_SEEK_TO_TIME:
467 uint64_t tm = (uint64_t) (*((double*)arg) * 90000);
468 if(dvdnav_time_search(priv->dvdnav, tm) == DVDNAV_STATUS_OK)
469 return 1;
470 break;
472 case STREAM_CTRL_GET_NUM_ANGLES:
474 uint32_t curr, angles;
475 if(dvdnav_get_angle_info(priv->dvdnav, &curr, &angles) != DVDNAV_STATUS_OK)
476 break;
477 *((int *)arg) = angles;
478 return 1;
480 case STREAM_CTRL_GET_ANGLE:
482 uint32_t curr, angles;
483 if(dvdnav_get_angle_info(priv->dvdnav, &curr, &angles) != DVDNAV_STATUS_OK)
484 break;
485 *((int *)arg) = curr;
486 return 1;
488 case STREAM_CTRL_SET_ANGLE:
490 uint32_t curr, angles;
491 int new_angle = *((int *)arg);
492 if(dvdnav_get_angle_info(priv->dvdnav, &curr, &angles) != DVDNAV_STATUS_OK)
493 break;
494 if(new_angle>angles || new_angle<1)
495 break;
496 if(dvdnav_angle_change(priv->dvdnav, new_angle) != DVDNAV_STATUS_OK)
497 return 1;
501 return STREAM_UNSUPPORTED;
504 static void identify_chapters(dvdnav_t *nav, uint32_t title)
506 uint64_t *parts=NULL, duration=0;
507 uint32_t n, i, t;
508 n = dvdnav_describe_title_chapters(nav, title, &parts, &duration);
509 if(parts) {
510 t = duration / 90;
511 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title, t / 1000, t % 1000);
512 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "TITLE %u, CHAPTERS: ", title);
513 for(i=0; i<n; i++) {
514 t = parts[i] / 90000;
515 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "%02d:%02d:%02d,", t/3600, (t/60)%60, t%60);
517 free(parts);
518 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "\n");
522 static void identify(dvdnav_priv_t *priv, struct stream_priv_s *p)
524 uint32_t titles=0, i;
525 if(p->track <= 0) {
526 dvdnav_get_number_of_titles(priv->dvdnav, &titles);
527 for(i=0; i<titles; i++)
528 identify_chapters(priv->dvdnav, i);
530 else
531 identify_chapters(priv->dvdnav, p->track);
534 static void show_audio_subs_languages(dvdnav_t *nav)
536 uint8_t lg;
537 uint16_t i, lang, format, id, channels;
538 int base[7] = {128, 0, 0, 0, 160, 136, 0};
539 for(i=0; i<8; i++)
541 char tmp[] = "unknown";
542 lg = dvdnav_get_audio_logical_stream(nav, i);
543 if(lg == 0xff) continue;
544 channels = dvdnav_audio_stream_channels(nav, lg);
545 if(channels == 0xFFFF)
546 channels = 2; //unknown
547 else
548 channels--;
549 lang = dvdnav_audio_stream_to_lang(nav, lg);
550 if(lang != 0xFFFF)
552 tmp[0] = lang >> 8;
553 tmp[1] = lang & 0xFF;
554 tmp[2] = 0;
556 format = dvdnav_audio_stream_format(nav, lg);
557 if(format == 0xFFFF || format > 6)
558 format = 1; //unknown
559 id = i + base[format];
560 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"audio stream: %d format: %s (%s) language: %s aid: %d.\n", i,
561 dvd_audio_stream_types[format], dvd_audio_stream_channels[channels], tmp, id);
562 if (lang != 0xFFFF && lang && tmp[0])
563 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", id, tmp);
566 for(i=0; i<32; i++)
568 char tmp[] = "unknown";
569 lg = dvdnav_get_spu_logical_stream(nav, i);
570 if(lg == 0xff) continue;
571 lang = dvdnav_spu_stream_to_lang(nav, i);
572 if(lang != 0xFFFF)
574 tmp[0] = lang >> 8;
575 tmp[1] = lang & 0xFF;
576 tmp[2] = 0;
578 mp_msg(MSGT_OPEN,MSGL_STATUS,"subtitle ( sid ): %d language: %s\n", lg, tmp);
579 if (lang != 0xFFFF && lang && tmp[0])
580 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_SID_%d_LANG=%s\n", lg, tmp);
584 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
585 struct stream_priv_s* p = (struct stream_priv_s*)opts;
586 char *filename;
587 dvdnav_priv_t *priv;
589 if(p->device) filename = p->device;
590 else if(dvd_device) filename= dvd_device;
591 else filename = DEFAULT_DVD_DEVICE;
592 if(!(priv=new_dvdnav_stream(filename))) {
593 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",filename, strerror(errno));
594 return STREAM_UNSUPPORTED;
597 if(p->track > 0) {
598 priv->title = p->track;
599 if(dvdnav_title_play(priv->dvdnav, p->track) != DVDNAV_STATUS_OK) {
600 mp_msg(MSGT_OPEN,MSGL_FATAL,"dvdnav_stream, couldn't select title %d, error '%s'\n", p->track, dvdnav_err_to_string(priv->dvdnav));
601 return STREAM_UNSUPPORTED;
603 } else if (p->track == 0) {
604 if(dvdnav_menu_call(priv->dvdnav, DVD_MENU_Root) != DVDNAV_STATUS_OK)
605 dvdnav_menu_call(priv->dvdnav, DVD_MENU_Title);
607 if(mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
608 identify(priv, p);
609 if(p->track > 0)
610 show_audio_subs_languages(priv->dvdnav);
611 if(dvd_angle > 1)
612 dvdnav_angle_change(priv->dvdnav, dvd_angle);
614 stream->sector_size = 2048;
615 stream->flags = STREAM_READ | MP_STREAM_SEEK;
616 stream->fill_buffer = fill_buffer;
617 stream->seek = seek;
618 stream->control = control;
619 stream->close = stream_dvdnav_close;
620 stream->type = STREAMTYPE_DVDNAV;
621 stream->priv=(void*)priv;
622 *file_format = DEMUXER_TYPE_MPEG_PS;
624 update_title_len(stream);
625 if(!stream->pos && p->track > 0)
626 mp_msg(MSGT_OPEN,MSGL_ERR, "INIT ERROR: couldn't get init pos %s\r\n", dvdnav_err_to_string(priv->dvdnav));
628 mp_msg(MSGT_OPEN,MSGL_INFO, "Remember to disable MPlayer's cache when playing dvdnav:// streams (adding -nocache to your command line)\r\n");
630 return STREAM_OK;
634 void mp_dvdnav_handle_input(stream_t *stream, int cmd, int *button) {
635 dvdnav_priv_t * priv = stream->priv;
636 dvdnav_t *nav = priv->dvdnav;
637 dvdnav_status_t status=DVDNAV_STATUS_ERR;
638 pci_t *pci = dvdnav_get_current_nav_pci(nav);
640 if(cmd != MP_CMD_DVDNAV_SELECT && !pci)
641 return;
643 switch(cmd) {
644 case MP_CMD_DVDNAV_UP:
645 status = dvdnav_upper_button_select(nav, pci);
646 break;
647 case MP_CMD_DVDNAV_DOWN:
648 status = dvdnav_lower_button_select(nav, pci);
649 break;
650 case MP_CMD_DVDNAV_LEFT:
651 status = dvdnav_left_button_select(nav, pci);
652 break;
653 case MP_CMD_DVDNAV_RIGHT:
654 status = dvdnav_right_button_select(nav, pci);
655 break;
656 case MP_CMD_DVDNAV_MENU:
657 status = dvdnav_menu_call(nav,DVD_MENU_Root);
658 break;
659 case MP_CMD_DVDNAV_PREVMENU: {
660 int title=0, part=0;
662 dvdnav_current_title_info(nav, &title, &part);
663 if(title) {
664 if((status=dvdnav_menu_call(nav, DVD_MENU_Part)) == DVDNAV_STATUS_OK)
665 break;
667 if((status=dvdnav_menu_call(nav, DVD_MENU_Title)) == DVDNAV_STATUS_OK)
668 break;
669 status=dvdnav_menu_call(nav, DVD_MENU_Root);
671 break;
672 case MP_CMD_DVDNAV_SELECT:
673 status = dvdnav_button_activate(nav, pci);
674 break;
675 case MP_CMD_DVDNAV_MOUSECLICK:
677 this is a workaround: in theory the simple dvdnav_lower_button_select()+dvdnav_button_activate()
678 should be enough (and generally it is), but there are cases when the calls to dvdnav_lower_button_select()
679 and friends fail! Hence we have to call dvdnav_mouse_activate(priv->mousex, priv->mousey) with
680 the coodinates saved by mp_dvdnav_update_mouse_pos().
681 This last call always works well
683 status = dvdnav_mouse_activate(nav, pci, priv->mousex, priv->mousey);
684 break;
685 default:
686 mp_msg(MSGT_CPLAYER, MSGL_V, "Unknown DVDNAV cmd %d\n", cmd);
687 break;
690 if(status == DVDNAV_STATUS_OK)
691 dvdnav_get_current_highlight(nav, button);
694 void mp_dvdnav_update_mouse_pos(stream_t *stream, int32_t x, int32_t y, int* button) {
695 dvdnav_priv_t * priv = stream->priv;
696 dvdnav_t *nav = priv->dvdnav;
697 dvdnav_status_t status;
698 pci_t *pci = dvdnav_get_current_nav_pci(nav);
700 if(!pci) return;
702 status = dvdnav_mouse_select(nav, pci, x, y);
703 if(status == DVDNAV_STATUS_OK) dvdnav_get_current_highlight(nav, button);
704 else *button = -1;
705 priv->mousex = x;
706 priv->mousey = y;
709 static int mp_dvdnav_get_aid_from_format (stream_t *stream, int index, uint8_t lg) {
710 dvdnav_priv_t * priv = stream->priv;
711 uint8_t format;
713 format = dvdnav_audio_stream_format(priv->dvdnav, lg);
714 switch(format) {
715 case DVDNAV_FORMAT_AC3:
716 return index + 128;
717 case DVDNAV_FORMAT_DTS:
718 return index + 136;
719 case DVDNAV_FORMAT_LPCM:
720 return index + 160;
721 case DVDNAV_FORMAT_MPEGAUDIO:
722 return index;
723 default:
724 return -1;
727 return -1;
731 * \brief mp_dvdnav_aid_from_lang() returns the audio id corresponding to the language code 'lang'
732 * \param stream: - stream pointer
733 * \param lang: 2-characters language code[s], eventually separated by spaces of commas
734 * \return -1 on error, current subtitle id if successful
736 int mp_dvdnav_aid_from_lang(stream_t *stream, unsigned char *language) {
737 dvdnav_priv_t * priv = stream->priv;
738 int k;
739 uint8_t lg;
740 uint16_t lang, lcode;
742 while(language && strlen(language)>=2) {
743 lcode = (language[0] << 8) | (language[1]);
744 for(k=0; k<32; k++) {
745 lg = dvdnav_get_audio_logical_stream(priv->dvdnav, k);
746 if(lg == 0xff) continue;
747 lang = dvdnav_audio_stream_to_lang(priv->dvdnav, lg);
748 if(lang != 0xFFFF && lang == lcode)
749 return mp_dvdnav_get_aid_from_format (stream, k, lg);
751 language += 2;
752 while(language[0]==',' || language[0]==' ') ++language;
754 return -1;
758 * \brief mp_dvdnav_lang_from_aid() assigns to buf the language corresponding to audio id 'aid'
759 * \param stream: - stream pointer
760 * \param sid: physical subtitle id
761 * \param buf: buffer to contain the 2-chars language string
762 * \return 0 on error, 1 if successful
764 int mp_dvdnav_lang_from_aid(stream_t *stream, int aid, unsigned char *buf) {
765 uint8_t lg;
766 uint16_t lang;
767 dvdnav_priv_t * priv = stream->priv;
769 if(aid < 0)
770 return 0;
771 lg = dvdnav_get_audio_logical_stream(priv->dvdnav, aid & 0x7);
772 if(lg == 0xff) return 0;
773 lang = dvdnav_audio_stream_to_lang(priv->dvdnav, lg);
774 if(lang == 0xffff) return 0;
775 buf[0] = lang >> 8;
776 buf[1] = lang & 0xFF;
777 buf[2] = 0;
778 return 1;
783 * \brief mp_dvdnav_sid_from_lang() returns the subtitle id corresponding to the language code 'lang'
784 * \param stream: - stream pointer
785 * \param lang: 2-characters language code[s], eventually separated by spaces of commas
786 * \return -1 on error, current subtitle id if successful
788 int mp_dvdnav_sid_from_lang(stream_t *stream, unsigned char *language) {
789 dvdnav_priv_t * priv = stream->priv;
790 uint8_t lg, k;
791 uint16_t lang, lcode;
793 while(language && strlen(language)>=2) {
794 lcode = (language[0] << 8) | (language[1]);
795 for(k=0; k<32; k++) {
796 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, k);
797 if(lg == 0xff) continue;
798 lang = dvdnav_spu_stream_to_lang(priv->dvdnav, k);
799 if(lang != 0xFFFF && lang == lcode) {
800 return lg;
803 language += 2;
804 while(language[0]==',' || language[0]==' ') ++language;
806 return -1;
810 * \brief mp_dvdnav_lang_from_sid() assigns to buf the language corresponding to subtitle id 'sid'
811 * \param stream: - stream pointer
812 * \param sid: physical subtitle id
813 * \param buf: buffer to contain the 2-chars language string
814 * \return 0 on error, 1 if successful
816 int mp_dvdnav_lang_from_sid(stream_t *stream, int sid, unsigned char *buf) {
817 uint8_t k;
818 uint16_t lang;
819 dvdnav_priv_t *priv = stream->priv;
820 if(sid < 0) return 0;
821 for (k=0; k<32; k++)
822 if (dvdnav_get_spu_logical_stream(priv->dvdnav, k) == sid)
823 break;
824 if (k == 32)
825 return 0;
826 lang = dvdnav_spu_stream_to_lang(priv->dvdnav, k);
827 if(lang == 0xffff) return 0;
828 buf[0] = lang >> 8;
829 buf[1] = lang & 0xFF;
830 buf[2] = 0;
831 return 1;
835 * \brief mp_dvdnav_number_of_subs() returns the count of available subtitles
836 * \param stream: - stream pointer
837 * \return 0 on error, something meaningful otherwise
839 int mp_dvdnav_number_of_subs(stream_t *stream) {
840 dvdnav_priv_t * priv = stream->priv;
841 uint8_t lg, k, n=0;
843 if (priv->state & NAV_FLAG_VTS_DOMAIN) return 0;
844 for(k=0; k<32; k++) {
845 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, k);
846 if(lg == 0xff) continue;
847 if(lg >= n) n = lg + 1;
849 return n;
853 * \brief mp_dvdnav_get_spu_clut() returns the spu clut
854 * \param stream: - stream pointer
855 * \return spu clut pointer
857 unsigned int *mp_dvdnav_get_spu_clut(stream_t *stream) {
858 dvdnav_priv_t *priv = stream->priv;
859 return (priv->state & NAV_FLAG_SPU_SET) ? priv->spu_clut : NULL;
863 * \brief mp_dvdnav_get_highlight() get dvdnav highlight struct
864 * \param stream: - stream pointer
865 * \param hl : - highlight struct pointer
867 void mp_dvdnav_get_highlight (stream_t *stream, nav_highlight_t *hl) {
868 dvdnav_priv_t *priv = stream->priv;
869 dvdnav_highlight_event_t hlev = priv->hlev;
871 hl->sx = hlev.sx;
872 hl->sy = hlev.sy;
873 hl->ex = hlev.ex;
874 hl->ey = hlev.ey;
875 hl->palette = hlev.palette;
878 void mp_dvdnav_switch_title (stream_t *stream, int title) {
879 dvdnav_priv_t *priv = stream->priv;
880 uint32_t titles;
882 dvdnav_get_number_of_titles (priv->dvdnav, &titles);
883 if (title > 0 && title <= titles)
884 dvdnav_title_play (priv->dvdnav, title);
888 * \brief Check if end of stream has been reached
889 * \param stream: - stream pointer
890 * \return 1 on really eof
892 int mp_dvdnav_is_eof (stream_t *stream) {
893 return ((dvdnav_priv_t *) stream->priv)->state & NAV_FLAG_EOF;
897 * \brief Skip still frame
898 * \param stream: - stream pointer
899 * \return 0 on success
901 int mp_dvdnav_skip_still (stream_t *stream) {
902 dvdnav_priv_t *priv = stream->priv;
903 if (priv->still_length == 0xff)
904 return 1;
905 dvdnav_still_skip(priv->dvdnav);
906 return 0;
910 * \brief Skip wait event
911 * \param stream: - stream pointer
912 * \return 0 on success
914 int mp_dvdnav_skip_wait (stream_t *stream) {
915 dvdnav_priv_t *priv = stream->priv;
916 if (!(priv->state & NAV_FLAG_WAIT))
917 return 1;
918 priv->state &= ~NAV_FLAG_WAIT;
919 dvdnav_wait_skip(priv->dvdnav);
920 return 0;
924 * \brief Set wait mode
925 * \param stream : - stream pointer
926 * \param mode : - if true, then suspend block read
927 * \param automode: - if true, then VTS or cell change set wait mode
929 void mp_dvdnav_read_wait (stream_t *stream, int mode, int automode) {
930 dvdnav_priv_t *priv = stream->priv;
931 if (mode == 0)
932 priv->state &= ~NAV_FLAG_WAIT_READ;
933 if (mode > 0)
934 priv->state |= NAV_FLAG_WAIT_READ;
935 if (automode == 0)
936 priv->state &= ~NAV_FLAG_WAIT_READ_AUTO;
937 if (automode > 0)
938 priv->state |= NAV_FLAG_WAIT_READ_AUTO;
942 * \brief Check if cell has changed
943 * \param stream: - stream pointer
944 * \param clear : - if true, then clear cell change flag
945 * \return 1 if cell has changed
947 int mp_dvdnav_cell_has_changed (stream_t *stream, int clear) {
948 dvdnav_priv_t *priv = stream->priv;
949 if (!(priv->state & NAV_FLAG_CELL_CHANGE))
950 return 0;
951 if (clear) {
952 priv->state &= ~NAV_FLAG_CELL_CHANGE;
953 priv->state |= NAV_FLAG_STREAM_CHANGE;
955 return 1;
959 * \brief Check if audio has changed
960 * \param stream: - stream pointer
961 * \param clear : - if true, then clear audio change flag
962 * \return 1 if audio has changed
964 int mp_dvdnav_audio_has_changed (stream_t *stream, int clear) {
965 dvdnav_priv_t *priv = stream->priv;
967 if (!(priv->state & NAV_FLAG_AUDIO_CHANGE))
968 return 0;
970 if (clear)
971 priv->state &= ~NAV_FLAG_AUDIO_CHANGE;
973 return 1;
977 * \brief Check if SPU has changed
978 * \param stream: - stream pointer
979 * \param clear : - if true, then clear spu change flag
980 * \return 1 if spu has changed
982 int mp_dvdnav_spu_has_changed (stream_t *stream, int clear) {
983 dvdnav_priv_t *priv = stream->priv;
985 if (!(priv->state & NAV_FLAG_SPU_CHANGE))
986 return 0;
988 if (clear)
989 priv->state &= ~NAV_FLAG_SPU_CHANGE;
991 return 1;
994 /* Notify if something has changed in stream
995 * Can be related to title, chapter, audio or SPU
997 int mp_dvdnav_stream_has_changed (stream_t *stream) {
998 dvdnav_priv_t *priv = stream->priv;
1000 if (!(priv->state & NAV_FLAG_STREAM_CHANGE))
1001 return 0;
1003 priv->state &= ~NAV_FLAG_STREAM_CHANGE;
1004 return 1;
1007 const stream_info_t stream_info_dvdnav = {
1008 "DVDNAV stream",
1009 "null",
1012 open_s,
1013 { "dvdnav", NULL },
1014 &stream_opts,
1015 1 // Urls are an option string