Translation system changes part 2: replace macros by strings
[mplayer.git] / stream / stream_dvdnav.c
blobd8dd416f9d39baebb3873f3d6b11ae887387579d
1 #include "config.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <errno.h>
8 #include "mp_msg.h"
9 #include "osdep/timer.h"
10 #include "input/input.h"
11 #include "stream.h"
12 #include "libmpdemux/demuxer.h"
13 #include <dvdnav/dvdnav.h>
14 #include "stream_dvdnav.h"
15 #include "libvo/video_out.h"
16 #include "libavutil/common.h"
17 #include "spudec.h"
18 #include "m_option.h"
19 #include "m_struct.h"
20 #include "help_mp.h"
21 #include "stream_dvd_common.h"
23 /* state flags */
24 typedef enum {
25 NAV_FLAG_EOF = 1 << 0, /* end of stream has been reached */
26 NAV_FLAG_WAIT = 1 << 1, /* wait event */
27 NAV_FLAG_WAIT_SKIP = 1 << 2, /* wait skip disable */
28 NAV_FLAG_CELL_CHANGE = 1 << 3, /* cell change event */
29 NAV_FLAG_WAIT_READ_AUTO = 1 << 4, /* wait read auto mode */
30 NAV_FLAG_WAIT_READ = 1 << 5, /* suspend read from stream */
31 NAV_FLAG_VTS_DOMAIN = 1 << 6, /* vts domain */
32 NAV_FLAG_SPU_SET = 1 << 7, /* spu_clut is valid */
33 NAV_FLAG_STREAM_CHANGE = 1 << 8, /* title, chapter, audio or SPU */
34 NAV_FLAG_AUDIO_CHANGE = 1 << 9, /* audio stream change event */
35 NAV_FLAG_SPU_CHANGE = 1 << 10, /* spu stream change event */
36 } dvdnav_state_t;
38 typedef struct {
39 dvdnav_t * dvdnav; /* handle to libdvdnav stuff */
40 char * filename; /* path */
41 unsigned int duration; /* in milliseconds */
42 int mousex, mousey;
43 int title;
44 unsigned int spu_clut[16];
45 dvdnav_highlight_event_t hlev;
46 int still_length; /* still frame duration */
47 unsigned int state;
48 } dvdnav_priv_t;
50 extern char *dvd_device;
51 extern char *audio_lang, *dvdsub_lang;
52 extern char *dvd_audio_stream_channels[6], *dvd_audio_stream_types[8];
54 static struct stream_priv_s {
55 int track;
56 char* device;
57 } stream_priv_dflts = {
59 NULL
62 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
63 /// URL definition
64 static const m_option_t stream_opts_fields[] = {
65 {"filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL },
66 {"hostname", ST_OFF(track), CONF_TYPE_INT, M_OPT_RANGE, 1, 99, NULL},
67 { NULL, NULL, 0, 0, 0, 0, NULL }
69 static const struct m_struct_st stream_opts = {
70 "dvd",
71 sizeof(struct stream_priv_s),
72 &stream_priv_dflts,
73 stream_opts_fields
76 static int seek(stream_t *s, off_t newpos);
77 static void show_audio_subs_languages(dvdnav_t *nav);
79 static dvdnav_priv_t * new_dvdnav_stream(char * filename) {
80 const char * title_str;
81 dvdnav_priv_t *priv;
83 if (!filename)
84 return NULL;
86 if (!(priv=calloc(1,sizeof(dvdnav_priv_t))))
87 return NULL;
89 if (!(priv->filename=strdup(filename))) {
90 free(priv);
91 return NULL;
94 if(dvdnav_open(&(priv->dvdnav),priv->filename)!=DVDNAV_STATUS_OK)
96 free(priv->filename);
97 free(priv);
98 return NULL;
101 if (!priv->dvdnav) {
102 free(priv);
103 return NULL;
106 if(1) //from vlc: if not used dvdnav from cvs will fail
108 int len, event;
109 char buf[2048];
111 dvdnav_get_next_block(priv->dvdnav,buf,&event,&len);
112 dvdnav_sector_search(priv->dvdnav, 0, SEEK_SET);
115 /* turn off dvdnav caching */
116 dvdnav_set_readahead_flag(priv->dvdnav, 0);
117 if(dvdnav_set_PGC_positioning_flag(priv->dvdnav, 1) != DVDNAV_STATUS_OK)
118 mp_msg(MSGT_OPEN,MSGL_ERR,"stream_dvdnav, failed to set PGC positioning\n");
119 #if 1
120 /* report the title?! */
121 if (dvdnav_get_title_string(priv->dvdnav,&title_str)==DVDNAV_STATUS_OK) {
122 mp_msg(MSGT_IDENTIFY, MSGL_INFO,"Title: '%s'\n",title_str);
124 #endif
126 //dvdnav_event_clear(priv);
128 return priv;
131 static void dvdnav_get_highlight (dvdnav_priv_t *priv, int display_mode) {
132 pci_t *pnavpci = NULL;
133 dvdnav_highlight_event_t *hlev = &(priv->hlev);
134 int btnum;
136 if (!priv || !priv->dvdnav)
137 return;
139 pnavpci = dvdnav_get_current_nav_pci (priv->dvdnav);
140 if (!pnavpci)
141 return;
143 dvdnav_get_current_highlight (priv->dvdnav, &(hlev->buttonN));
144 hlev->display = display_mode; /* show */
146 if (hlev->buttonN > 0 && pnavpci->hli.hl_gi.btn_ns > 0 && hlev->display) {
147 for (btnum = 0; btnum < pnavpci->hli.hl_gi.btn_ns; btnum++) {
148 btni_t *btni = &(pnavpci->hli.btnit[btnum]);
150 if (hlev->buttonN == btnum + 1) {
151 hlev->sx = FFMIN (btni->x_start, btni->x_end);
152 hlev->ex = FFMAX (btni->x_start, btni->x_end);
153 hlev->sy = FFMIN (btni->y_start, btni->y_end);
154 hlev->ey = FFMAX (btni->y_start, btni->y_end);
156 hlev->palette = (btni->btn_coln == 0) ? 0 :
157 pnavpci->hli.btn_colit.btn_coli[btni->btn_coln - 1][0];
158 break;
161 } else { /* hide button or no button */
162 hlev->sx = hlev->ex = 0;
163 hlev->sy = hlev->ey = 0;
164 hlev->palette = hlev->buttonN = 0;
168 static inline int dvdnav_get_duration (int length) {
169 return (length == 255) ? 0 : length * 1000;
172 static int dvdnav_stream_read(dvdnav_priv_t * priv, unsigned char *buf, int *len) {
173 int event = DVDNAV_NOP;
175 *len=-1;
176 if (dvdnav_get_next_block(priv->dvdnav,buf,&event,len)!=DVDNAV_STATUS_OK) {
177 mp_msg(MSGT_OPEN,MSGL_V, "Error getting next block from DVD %d (%s)\n",event, dvdnav_err_to_string(priv->dvdnav) );
178 *len=-1;
180 else if (event!=DVDNAV_BLOCK_OK) {
181 // need to handle certain events internally (like skipping stills)
182 switch (event) {
183 case DVDNAV_NAV_PACKET:
184 return event;
185 case DVDNAV_STILL_FRAME: {
186 dvdnav_still_event_t *still_event = (dvdnav_still_event_t *) buf;
187 priv->still_length = still_event->length;
188 /* set still frame duration */
189 priv->duration = dvdnav_get_duration (priv->still_length);
190 if (priv->still_length <= 1) {
191 pci_t *pnavpci = dvdnav_get_current_nav_pci (priv->dvdnav);
192 priv->duration = mp_dvdtimetomsec (&pnavpci->pci_gi.e_eltm);
194 break;
196 case DVDNAV_HIGHLIGHT: {
197 dvdnav_get_highlight (priv, 1);
198 break;
200 case DVDNAV_CELL_CHANGE: {
201 dvdnav_cell_change_event_t *ev = (dvdnav_cell_change_event_t*)buf;
202 uint32_t nextstill;
204 priv->state &= ~NAV_FLAG_WAIT_SKIP;
205 priv->state |= NAV_FLAG_STREAM_CHANGE;
206 if(ev->pgc_length)
207 priv->duration = ev->pgc_length/90;
209 if (dvdnav_is_domain_vts(priv->dvdnav)) {
210 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "DVDNAV_TITLE_IS_MOVIE\n");
211 priv->state &= ~NAV_FLAG_VTS_DOMAIN;
212 } else {
213 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "DVDNAV_TITLE_IS_MENU\n");
214 priv->state |= NAV_FLAG_VTS_DOMAIN;
217 nextstill = dvdnav_get_next_still_flag (priv->dvdnav);
218 if (nextstill) {
219 priv->duration = dvdnav_get_duration (nextstill);
220 priv->still_length = nextstill;
221 if (priv->still_length <= 1) {
222 pci_t *pnavpci = dvdnav_get_current_nav_pci (priv->dvdnav);
223 priv->duration = mp_dvdtimetomsec (&pnavpci->pci_gi.e_eltm);
227 break;
229 case DVDNAV_SPU_CLUT_CHANGE: {
230 memcpy(priv->spu_clut, buf, 16*sizeof(unsigned int));
231 priv->state |= NAV_FLAG_SPU_SET;
232 break;
234 case DVDNAV_WAIT: {
235 if ((priv->state & NAV_FLAG_WAIT_SKIP) &&
236 !(priv->state & NAV_FLAG_WAIT))
237 dvdnav_wait_skip (priv->dvdnav);
238 else
239 priv->state |= NAV_FLAG_WAIT;
240 break;
242 case DVDNAV_VTS_CHANGE: {
243 priv->state &= ~NAV_FLAG_WAIT_SKIP;
244 priv->state |= NAV_FLAG_STREAM_CHANGE;
245 break;
247 case DVDNAV_SPU_STREAM_CHANGE: {
248 priv->state |= NAV_FLAG_STREAM_CHANGE;
249 break;
253 *len=0;
255 return event;
258 static void update_title_len(stream_t *stream) {
259 dvdnav_priv_t *priv = stream->priv;
260 dvdnav_status_t status;
261 uint32_t pos = 0, len = 0;
263 status = dvdnav_get_position(priv->dvdnav, &pos, &len);
264 if(status == DVDNAV_STATUS_OK && len) {
265 stream->end_pos = (off_t) len * 2048;
266 stream->seek = seek;
267 } else {
268 stream->seek = NULL;
269 stream->end_pos = 0;
274 static int seek(stream_t *s, off_t newpos) {
275 uint32_t sector = 0;
276 dvdnav_priv_t *priv = s->priv;
278 if(s->end_pos && newpos > s->end_pos)
279 newpos = s->end_pos;
280 sector = newpos / 2048ULL;
281 if(dvdnav_sector_search(priv->dvdnav, (uint64_t) sector, SEEK_SET) != DVDNAV_STATUS_OK)
282 goto fail;
284 s->pos = newpos;
286 return 1;
288 fail:
289 mp_msg(MSGT_STREAM,MSGL_INFO,"dvdnav_stream, seeking to %"PRIu64" failed: %s\n", newpos, dvdnav_err_to_string(priv->dvdnav));
291 return 1;
294 static void stream_dvdnav_close(stream_t *s) {
295 dvdnav_priv_t *priv = s->priv;
296 dvdnav_close(priv->dvdnav);
297 priv->dvdnav = NULL;
298 free(priv);
302 static int fill_buffer(stream_t *s, char *but, int len)
304 int event;
306 dvdnav_priv_t* priv=s->priv;
307 if (priv->state & NAV_FLAG_WAIT_READ) /* read is suspended */
308 return -1;
309 len=0;
310 if(!s->end_pos)
311 update_title_len(s);
312 while(!len) /* grab all event until DVDNAV_BLOCK_OK (len=2048), DVDNAV_STOP or DVDNAV_STILL_FRAME */
314 event=dvdnav_stream_read(priv, s->buffer, &len);
315 if(event==-1 || len==-1)
317 mp_msg(MSGT_CPLAYER,MSGL_ERR, "DVDNAV stream read error!\n");
318 return 0;
320 if (event != DVDNAV_BLOCK_OK)
321 dvdnav_get_highlight (priv, 1);
322 switch (event) {
323 case DVDNAV_STOP: {
324 priv->state |= NAV_FLAG_EOF;
325 return len;
327 case DVDNAV_BLOCK_OK:
328 case DVDNAV_NAV_PACKET:
329 case DVDNAV_STILL_FRAME:
330 return len;
331 case DVDNAV_WAIT: {
332 if (priv->state & NAV_FLAG_WAIT)
333 return len;
334 break;
336 case DVDNAV_VTS_CHANGE: {
337 int tit = 0, part = 0;
338 dvdnav_vts_change_event_t *vts_event = (dvdnav_vts_change_event_t *)s->buffer;
339 mp_msg(MSGT_CPLAYER,MSGL_INFO, "DVDNAV, switched to title: %d\r\n", vts_event->new_vtsN);
340 priv->state |= NAV_FLAG_CELL_CHANGE;
341 priv->state |= NAV_FLAG_AUDIO_CHANGE;
342 priv->state |= NAV_FLAG_SPU_CHANGE;
343 priv->state &= ~NAV_FLAG_WAIT_SKIP;
344 priv->state &= ~NAV_FLAG_WAIT;
345 s->end_pos = 0;
346 update_title_len(s);
347 show_audio_subs_languages(priv->dvdnav);
348 if (priv->state & NAV_FLAG_WAIT_READ_AUTO)
349 priv->state |= NAV_FLAG_WAIT_READ;
350 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) == DVDNAV_STATUS_OK) {
351 mp_msg(MSGT_CPLAYER,MSGL_V, "\r\nDVDNAV, NEW TITLE %d\r\n", tit);
352 dvdnav_get_highlight (priv, 0);
353 if(priv->title > 0 && tit != priv->title)
354 return 0;
356 break;
358 case DVDNAV_CELL_CHANGE: {
359 priv->state |= NAV_FLAG_CELL_CHANGE;
360 priv->state |= NAV_FLAG_AUDIO_CHANGE;
361 priv->state |= NAV_FLAG_SPU_CHANGE;
362 priv->state &= ~NAV_FLAG_WAIT_SKIP;
363 priv->state &= ~NAV_FLAG_WAIT;
364 if (priv->state & NAV_FLAG_WAIT_READ_AUTO)
365 priv->state |= NAV_FLAG_WAIT_READ;
366 if(priv->title > 0 && dvd_last_chapter > 0) {
367 int tit=0, part=0;
368 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) == DVDNAV_STATUS_OK && part > dvd_last_chapter)
369 return 0;
371 dvdnav_get_highlight (priv, 1);
373 break;
374 case DVDNAV_AUDIO_STREAM_CHANGE:
375 priv->state |= NAV_FLAG_AUDIO_CHANGE;
376 break;
377 case DVDNAV_SPU_STREAM_CHANGE:
378 priv->state |= NAV_FLAG_SPU_CHANGE;
379 break;
382 mp_msg(MSGT_STREAM,MSGL_DBG2,"DVDNAV fill_buffer len: %d\n",len);
383 return len;
386 static int control(stream_t *stream, int cmd, void* arg) {
387 dvdnav_priv_t* priv=stream->priv;
388 int tit, part;
390 switch(cmd)
392 case STREAM_CTRL_SEEK_TO_CHAPTER:
394 int chap = *((unsigned int *)arg)+1;
396 if(chap < 1 || dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
397 break;
398 if(dvdnav_part_play(priv->dvdnav, tit, chap) != DVDNAV_STATUS_OK)
399 break;
400 return 1;
402 case STREAM_CTRL_GET_NUM_CHAPTERS:
404 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
405 break;
406 if(dvdnav_get_number_of_parts(priv->dvdnav, tit, &part) != DVDNAV_STATUS_OK)
407 break;
408 if(!part)
409 break;
410 *((unsigned int *)arg) = part;
411 return 1;
413 case STREAM_CTRL_GET_CURRENT_CHAPTER:
415 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
416 break;
417 *((unsigned int *)arg) = part - 1;
418 return 1;
420 case STREAM_CTRL_GET_TIME_LENGTH:
422 if(priv->duration || priv->still_length)
424 *((double *)arg) = (double)priv->duration / 1000.0;
425 return 1;
427 break;
429 case STREAM_CTRL_GET_ASPECT_RATIO:
431 uint8_t ar = dvdnav_get_video_aspect(priv->dvdnav);
432 *((double *)arg) = !ar ? 4.0/3.0 : 16.0/9.0;
433 return 1;
435 case STREAM_CTRL_GET_CURRENT_TIME:
437 double tm;
438 tm = dvdnav_get_current_time(priv->dvdnav)/90000.0f;
439 if(tm != -1)
441 *((double *)arg) = tm;
442 return 1;
444 break;
446 case STREAM_CTRL_SEEK_TO_TIME:
448 uint64_t tm = (uint64_t) (*((double*)arg) * 90000);
449 if(dvdnav_time_search(priv->dvdnav, tm) == DVDNAV_STATUS_OK)
450 return 1;
451 break;
453 case STREAM_CTRL_GET_NUM_ANGLES:
455 uint32_t curr, angles;
456 if(dvdnav_get_angle_info(priv->dvdnav, &curr, &angles) != DVDNAV_STATUS_OK)
457 break;
458 *((int *)arg) = angles;
459 return 1;
461 case STREAM_CTRL_GET_ANGLE:
463 uint32_t curr, angles;
464 if(dvdnav_get_angle_info(priv->dvdnav, &curr, &angles) != DVDNAV_STATUS_OK)
465 break;
466 *((int *)arg) = curr;
467 return 1;
469 case STREAM_CTRL_SET_ANGLE:
471 uint32_t curr, angles;
472 int new_angle = *((int *)arg);
473 if(dvdnav_get_angle_info(priv->dvdnav, &curr, &angles) != DVDNAV_STATUS_OK)
474 break;
475 if(new_angle>angles || new_angle<1)
476 break;
477 if(dvdnav_angle_change(priv->dvdnav, new_angle) != DVDNAV_STATUS_OK)
478 return 1;
482 return STREAM_UNSUPPORTED;
485 static void identify_chapters(dvdnav_t *nav, uint32_t title)
487 uint64_t *parts=NULL, duration=0;
488 uint32_t n, i, t;
489 n = dvdnav_describe_title_chapters(nav, title, &parts, &duration);
490 if(parts) {
491 t = duration / 90;
492 mp_msg(MSGT_IDENTIFY, MSGL_V, "ID_DVD_TITLE_%d_LENGTH=%d.%03d\n", title, t / 1000, t % 1000);
493 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "TITLE %u, CHAPTERS: ", title);
494 for(i=0; i<n; i++) {
495 t = parts[i] / 90000;
496 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "%02d:%02d:%02d,", t/3600, (t/60)%60, t%60);
498 free(parts);
499 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "\n");
503 static void identify(dvdnav_priv_t *priv, struct stream_priv_s *p)
505 uint32_t titles=0, i;
506 if(p->track <= 0) {
507 dvdnav_get_number_of_titles(priv->dvdnav, &titles);
508 for(i=0; i<titles; i++)
509 identify_chapters(priv->dvdnav, i);
511 else
512 identify_chapters(priv->dvdnav, p->track);
515 static void show_audio_subs_languages(dvdnav_t *nav)
517 uint8_t lg;
518 uint16_t i, lang, format, id, channels;
519 int base[7] = {128, 0, 0, 0, 160, 136, 0};
520 char tmp[3];
521 for(i=0; i<8; i++)
523 lg = dvdnav_get_audio_logical_stream(nav, i);
524 if(lg == 0xff) continue;
525 channels = dvdnav_audio_stream_channels(nav, lg);
526 if(channels == 0xFFFF)
527 channels = 2; //unknown
528 else
529 channels--;
530 lang = dvdnav_audio_stream_to_lang(nav, lg);
531 if(lang == 0xFFFF)
532 tmp[0] = tmp[1] = '?';
533 else
535 tmp[0] = lang >> 8;
536 tmp[1] = lang & 0xFF;
538 tmp[2] = 0;
539 format = dvdnav_audio_stream_format(nav, lg);
540 if(format == 0xFFFF || format > 6)
541 format = 1; //unknown
542 id = i + base[format];
543 if (lang != 0xFFFF) {
544 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"audio stream: %d format: %s (%s) language: %s aid: %d.\n", i,
545 dvd_audio_stream_types[format], dvd_audio_stream_channels[channels], tmp, id);
546 if(lang && tmp[0])
547 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AID_%d_LANG=%s\n", id, tmp);
551 for(i=0; i<32; i++)
553 lg = dvdnav_get_spu_logical_stream(nav, i);
554 if(lg == 0xff) continue;
555 lang = dvdnav_spu_stream_to_lang(nav, lg);
556 if(lang == 0xFFFF)
557 tmp[0] = tmp[1] = '?';
558 else
560 tmp[0] = lang >> 8;
561 tmp[1] = lang & 0xFF;
563 tmp[2] = 0;
564 if (lang != 0xFFFF)
565 mp_tmsg(MSGT_OPEN,MSGL_STATUS,"subtitle ( sid ): %d language: %s\n", i+0x20, tmp);
569 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
570 struct stream_priv_s* p = (struct stream_priv_s*)opts;
571 char *filename;
572 dvdnav_priv_t *priv;
574 if(p->device) filename = p->device;
575 else if(dvd_device) filename= dvd_device;
576 else filename = DEFAULT_DVD_DEVICE;
577 if(!(priv=new_dvdnav_stream(filename))) {
578 mp_tmsg(MSGT_OPEN,MSGL_ERR,"Couldn't open DVD device: %s (%s)\n",filename, strerror(errno));
579 return STREAM_UNSUPPORTED;
582 if(p->track > 0) {
583 priv->title = p->track;
584 if(dvdnav_title_play(priv->dvdnav, p->track) != DVDNAV_STATUS_OK) {
585 mp_msg(MSGT_OPEN,MSGL_FATAL,"dvdnav_stream, couldn't select title %d, error '%s'\n", p->track, dvdnav_err_to_string(priv->dvdnav));
586 return STREAM_UNSUPPORTED;
588 } else if (p->track == 0) {
589 if(dvdnav_menu_call(priv->dvdnav, DVD_MENU_Root) != DVDNAV_STATUS_OK)
590 dvdnav_menu_call(priv->dvdnav, DVD_MENU_Title);
592 if(mp_msg_test(MSGT_IDENTIFY, MSGL_INFO))
593 identify(priv, p);
594 if(p->track > 0)
595 show_audio_subs_languages(priv->dvdnav);
596 if(dvd_angle > 1)
597 dvdnav_angle_change(priv->dvdnav, dvd_angle);
599 stream->sector_size = 2048;
600 stream->flags = STREAM_READ | STREAM_SEEK;
601 stream->fill_buffer = fill_buffer;
602 stream->seek = seek;
603 stream->control = control;
604 stream->close = stream_dvdnav_close;
605 stream->type = STREAMTYPE_DVDNAV;
606 stream->priv=(void*)priv;
607 *file_format = DEMUXER_TYPE_MPEG_PS;
609 update_title_len(stream);
610 if(!stream->pos && p->track > 0)
611 mp_msg(MSGT_OPEN,MSGL_ERR, "INIT ERROR: couldn't get init pos %s\r\n", dvdnav_err_to_string(priv->dvdnav));
613 mp_msg(MSGT_OPEN,MSGL_INFO, "Remember to disable MPlayer's cache when playing dvdnav:// streams (adding -nocache to your command line)\r\n");
615 return STREAM_OK;
619 void mp_dvdnav_handle_input(stream_t *stream, int cmd, int *button) {
620 dvdnav_priv_t * priv = stream->priv;
621 dvdnav_t *nav = priv->dvdnav;
622 dvdnav_status_t status=DVDNAV_STATUS_ERR;
623 pci_t *pci = dvdnav_get_current_nav_pci(nav);
625 if(cmd != MP_CMD_DVDNAV_SELECT && !pci)
626 return;
628 switch(cmd) {
629 case MP_CMD_DVDNAV_UP:
630 status = dvdnav_upper_button_select(nav, pci);
631 break;
632 case MP_CMD_DVDNAV_DOWN:
633 status = dvdnav_lower_button_select(nav, pci);
634 break;
635 case MP_CMD_DVDNAV_LEFT:
636 status = dvdnav_left_button_select(nav, pci);
637 break;
638 case MP_CMD_DVDNAV_RIGHT:
639 status = dvdnav_right_button_select(nav, pci);
640 break;
641 case MP_CMD_DVDNAV_MENU:
642 status = dvdnav_menu_call(nav,DVD_MENU_Root);
643 break;
644 case MP_CMD_DVDNAV_PREVMENU: {
645 int title=0, part=0;
647 dvdnav_current_title_info(nav, &title, &part);
648 if(title) {
649 if((status=dvdnav_menu_call(nav, DVD_MENU_Part)) == DVDNAV_STATUS_OK)
650 break;
652 if((status=dvdnav_menu_call(nav, DVD_MENU_Title)) == DVDNAV_STATUS_OK)
653 break;
654 status=dvdnav_menu_call(nav, DVD_MENU_Root);
656 break;
657 case MP_CMD_DVDNAV_SELECT:
658 status = dvdnav_button_activate(nav, pci);
659 break;
660 case MP_CMD_DVDNAV_MOUSECLICK:
662 this is a workaround: in theory the simple dvdnav_lower_button_select()+dvdnav_button_activate()
663 should be enough (and generally it is), but there are cases when the calls to dvdnav_lower_button_select()
664 and friends fail! Hence we have to call dvdnav_mouse_activate(priv->mousex, priv->mousey) with
665 the coodinates saved by mp_dvdnav_update_mouse_pos().
666 This last call always works well
668 status = dvdnav_mouse_activate(nav, pci, priv->mousex, priv->mousey);
669 break;
670 default:
671 mp_msg(MSGT_CPLAYER, MSGL_V, "Unknown DVDNAV cmd %d\n", cmd);
672 break;
675 if(status == DVDNAV_STATUS_OK)
676 dvdnav_get_current_highlight(nav, button);
679 void mp_dvdnav_update_mouse_pos(stream_t *stream, int32_t x, int32_t y, int* button) {
680 dvdnav_priv_t * priv = stream->priv;
681 dvdnav_t *nav = priv->dvdnav;
682 dvdnav_status_t status;
683 pci_t *pci = dvdnav_get_current_nav_pci(nav);
685 if(!pci) return;
687 status = dvdnav_mouse_select(nav, pci, x, y);
688 if(status == DVDNAV_STATUS_OK) dvdnav_get_current_highlight(nav, button);
689 else *button = -1;
690 priv->mousex = x;
691 priv->mousey = y;
694 static int mp_dvdnav_get_aid_from_format (stream_t *stream, int index, uint8_t lg) {
695 dvdnav_priv_t * priv = stream->priv;
696 uint8_t format;
698 format = dvdnav_audio_stream_format(priv->dvdnav, lg);
699 switch(format) {
700 case DVDNAV_FORMAT_AC3:
701 return (index + 128);
702 case DVDNAV_FORMAT_DTS:
703 return (index + 136);
704 case DVDNAV_FORMAT_LPCM:
705 return (index + 160);
706 case DVDNAV_FORMAT_MPEGAUDIO:
707 return index;
708 default:
709 return -1;
712 return -1;
716 * \brief mp_dvdnav_aid_from_audio_num() returns the audio id corresponding to the logical number
717 * \param stream: - stream pointer
718 * \param audio_num: - logical number
719 * \return -1 on error, current subtitle id if successful
721 int mp_dvdnav_aid_from_audio_num(stream_t *stream, int audio_num) {
722 dvdnav_priv_t * priv = stream->priv;
723 int k;
724 uint8_t lg;
726 for(k=0; k<32; k++) {
727 lg = dvdnav_get_audio_logical_stream(priv->dvdnav, k);
728 if (lg == 0xff) continue;
729 if (lg != audio_num) continue;
731 return mp_dvdnav_get_aid_from_format (stream, k, lg);
733 return -1;
737 * \brief mp_dvdnav_aid_from_lang() returns the audio id corresponding to the language code 'lang'
738 * \param stream: - stream pointer
739 * \param lang: 2-characters language code[s], eventually separated by spaces of commas
740 * \return -1 on error, current subtitle id if successful
742 int mp_dvdnav_aid_from_lang(stream_t *stream, unsigned char *language) {
743 dvdnav_priv_t * priv = stream->priv;
744 int k;
745 uint8_t lg;
746 uint16_t lang, lcode;
748 while(language && strlen(language)>=2) {
749 lcode = (language[0] << 8) | (language[1]);
750 for(k=0; k<32; k++) {
751 lg = dvdnav_get_audio_logical_stream(priv->dvdnav, k);
752 if(lg == 0xff) continue;
753 lang = dvdnav_audio_stream_to_lang(priv->dvdnav, lg);
754 if(lang != 0xFFFF && lang == lcode)
755 return mp_dvdnav_get_aid_from_format (stream, k, lg);
757 language += 2;
758 while(language[0]==',' || language[0]==' ') ++language;
760 return -1;
764 * \brief mp_dvdnav_lang_from_aid() assigns to buf the language corresponding to audio id 'aid'
765 * \param stream: - stream pointer
766 * \param sid: physical subtitle id
767 * \param buf: buffer to contain the 2-chars language string
768 * \return 0 on error, 1 if successful
770 int mp_dvdnav_lang_from_aid(stream_t *stream, int aid, unsigned char *buf) {
771 uint8_t lg;
772 uint16_t lang;
773 dvdnav_priv_t * priv = stream->priv;
775 if(aid < 0)
776 return 0;
777 lg = dvdnav_get_audio_logical_stream(priv->dvdnav, aid & 0x7);
778 if(lg == 0xff) return 0;
779 lang = dvdnav_audio_stream_to_lang(priv->dvdnav, lg);
780 if(lang == 0xffff) return 0;
781 buf[0] = lang >> 8;
782 buf[1] = lang & 0xFF;
783 buf[2] = 0;
784 return 1;
789 * \brief mp_dvdnav_sid_from_lang() returns the subtitle id corresponding to the language code 'lang'
790 * \param stream: - stream pointer
791 * \param lang: 2-characters language code[s], eventually separated by spaces of commas
792 * \return -1 on error, current subtitle id if successful
794 int mp_dvdnav_sid_from_lang(stream_t *stream, unsigned char *language) {
795 dvdnav_priv_t * priv = stream->priv;
796 uint8_t lg, k;
797 uint16_t lang, lcode;
799 while(language && strlen(language)>=2) {
800 lcode = (language[0] << 8) | (language[1]);
801 for(k=0; k<32; k++) {
802 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, k);
803 if(lg == 0xff) continue;
804 lang = dvdnav_spu_stream_to_lang(priv->dvdnav, lg);
805 if(lang != 0xFFFF && lang == lcode) {
806 return k;
809 language += 2;
810 while(language[0]==',' || language[0]==' ') ++language;
812 return -1;
816 * \brief mp_dvdnav_lang_from_sid() assigns to buf the language corresponding to subtitle id 'sid'
817 * \param stream: - stream pointer
818 * \param sid: physical subtitle id
819 * \param buf: buffer to contain the 2-chars language string
820 * \return 0 on error, 1 if successful
822 int mp_dvdnav_lang_from_sid(stream_t *stream, int sid, unsigned char *buf) {
823 uint8_t lg;
824 uint16_t lang;
825 dvdnav_priv_t *priv = stream->priv;
826 if(sid < 0) return 0;
827 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, sid);
828 lang = dvdnav_spu_stream_to_lang(priv->dvdnav, lg);
829 if(lang == 0xffff) return 0;
830 buf[0] = lang >> 8;
831 buf[1] = lang & 0xFF;
832 buf[2] = 0;
833 return 1;
837 * \brief mp_dvdnav_number_of_subs() returns the count of available subtitles
838 * \param stream: - stream pointer
839 * \return 0 on error, something meaningful otherwise
841 int mp_dvdnav_number_of_subs(stream_t *stream) {
842 dvdnav_priv_t * priv = stream->priv;
843 uint8_t lg, k, n=0;
845 if (priv->state & NAV_FLAG_VTS_DOMAIN) return 0;
846 for(k=0; k<32; k++) {
847 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, k);
848 if(lg == 0xff) continue;
849 n++;
851 return n;
855 * \brief mp_dvdnav_get_spu_clut() returns the spu clut
856 * \param stream: - stream pointer
857 * \return spu clut pointer
859 unsigned int *mp_dvdnav_get_spu_clut(stream_t *stream) {
860 dvdnav_priv_t *priv = stream->priv;
861 return (priv->state & NAV_FLAG_SPU_SET) ? priv->spu_clut : NULL;
865 * \brief mp_dvdnav_get_highlight() get dvdnav highlight struct
866 * \param stream: - stream pointer
867 * \param hl : - highlight struct pointer
869 void mp_dvdnav_get_highlight (stream_t *stream, nav_highlight_t *hl) {
870 dvdnav_priv_t *priv = stream->priv;
871 dvdnav_highlight_event_t hlev = priv->hlev;
873 hl->sx = hlev.sx;
874 hl->sy = hlev.sy;
875 hl->ex = hlev.ex;
876 hl->ey = hlev.ey;
877 hl->palette = hlev.palette;
880 void mp_dvdnav_switch_title (stream_t *stream, int title) {
881 dvdnav_priv_t *priv = stream->priv;
882 uint32_t titles;
884 dvdnav_get_number_of_titles (priv->dvdnav, &titles);
885 if (title > 0 && title <= titles)
886 dvdnav_title_play (priv->dvdnav, title);
890 * \brief Check if end of stream has been reached
891 * \param stream: - stream pointer
892 * \return 1 on really eof
894 int mp_dvdnav_is_eof (stream_t *stream) {
895 return ((dvdnav_priv_t *) stream->priv)->state & NAV_FLAG_EOF;
899 * \brief Skip still frame
900 * \param stream: - stream pointer
901 * \return 0 on success
903 int mp_dvdnav_skip_still (stream_t *stream) {
904 dvdnav_priv_t *priv = stream->priv;
905 if (priv->still_length == 0xff)
906 return 1;
907 dvdnav_still_skip(priv->dvdnav);
908 return 0;
912 * \brief Skip wait event
913 * \param stream: - stream pointer
914 * \return 0 on success
916 int mp_dvdnav_skip_wait (stream_t *stream) {
917 dvdnav_priv_t *priv = stream->priv;
918 if (!(priv->state & NAV_FLAG_WAIT))
919 return 1;
920 priv->state &= ~NAV_FLAG_WAIT;
921 dvdnav_wait_skip(priv->dvdnav);
922 return 0;
926 * \brief Set wait mode
927 * \param stream : - stream pointer
928 * \param mode : - if true, then suspend block read
929 * \param automode: - if true, then VTS or cell change set wait mode
931 void mp_dvdnav_read_wait (stream_t *stream, int mode, int automode) {
932 dvdnav_priv_t *priv = stream->priv;
933 if (mode == 0)
934 priv->state &= ~NAV_FLAG_WAIT_READ;
935 if (mode > 0)
936 priv->state |= NAV_FLAG_WAIT_READ;
937 if (automode == 0)
938 priv->state &= ~NAV_FLAG_WAIT_READ_AUTO;
939 if (automode > 0)
940 priv->state |= NAV_FLAG_WAIT_READ_AUTO;
944 * \brief Check if cell has changed
945 * \param stream: - stream pointer
946 * \param clear : - if true, then clear cell change flag
947 * \return 1 if cell has changed
949 int mp_dvdnav_cell_has_changed (stream_t *stream, int clear) {
950 dvdnav_priv_t *priv = stream->priv;
951 if (!(priv->state & NAV_FLAG_CELL_CHANGE))
952 return 0;
953 if (clear) {
954 priv->state &= ~NAV_FLAG_CELL_CHANGE;
955 priv->state |= NAV_FLAG_STREAM_CHANGE;
957 return 1;
961 * \brief Check if audio has changed
962 * \param stream: - stream pointer
963 * \param clear : - if true, then clear audio change flag
964 * \return 1 if audio has changed
966 int mp_dvdnav_audio_has_changed (stream_t *stream, int clear) {
967 dvdnav_priv_t *priv = stream->priv;
969 if (!(priv->state & NAV_FLAG_AUDIO_CHANGE))
970 return 0;
972 if (clear)
973 priv->state &= ~NAV_FLAG_AUDIO_CHANGE;
975 return 1;
979 * \brief Check if SPU has changed
980 * \param stream: - stream pointer
981 * \param clear : - if true, then clear spu change flag
982 * \return 1 if spu has changed
984 int mp_dvdnav_spu_has_changed (stream_t *stream, int clear) {
985 dvdnav_priv_t *priv = stream->priv;
987 if (!(priv->state & NAV_FLAG_SPU_CHANGE))
988 return 0;
990 if (clear)
991 priv->state &= ~NAV_FLAG_SPU_CHANGE;
993 return 1;
996 /* Notify if something has changed in stream
997 * Can be related to title, chapter, audio or SPU
999 int mp_dvdnav_stream_has_changed (stream_t *stream) {
1000 dvdnav_priv_t *priv = stream->priv;
1002 if (!(priv->state & NAV_FLAG_STREAM_CHANGE))
1003 return 0;
1005 priv->state &= ~NAV_FLAG_STREAM_CHANGE;
1006 return 1;
1009 const stream_info_t stream_info_dvdnav = {
1010 "DVDNAV stream",
1011 "null",
1014 open_s,
1015 { "dvdnav", NULL },
1016 &stream_opts,
1017 1 // Urls are an option string