Document lscale=2
[mplayer/glamo.git] / stream / stream_dvdnav.c
blob5d52b72d489a7ccfb9d55308929ce1ae2fa3a771
1 #include "config.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include "mp_msg.h"
8 #include "osdep/timer.h"
9 #include "input/input.h"
10 #include "stream.h"
11 #include "libmpdemux/demuxer.h"
12 #include "stream_dvdnav.h"
13 #include "libvo/video_out.h"
14 #include "libavutil/common.h"
15 #include "spudec.h"
16 #include "m_option.h"
17 #include "m_struct.h"
18 #include "help_mp.h"
20 extern char *dvd_device;
21 extern int dvd_chapter;
22 extern int dvd_last_chapter;
23 extern int dvd_angle;
24 extern char *audio_lang, *dvdsub_lang;
26 static struct stream_priv_s {
27 int track;
28 char* device;
29 } stream_priv_dflts = {
31 NULL
34 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
35 /// URL definition
36 static m_option_t stream_opts_fields[] = {
37 {"filename", ST_OFF(device), CONF_TYPE_STRING, 0, 0, 0, NULL },
38 {"hostname", ST_OFF(track), CONF_TYPE_INT, 0, 0, 0, NULL},
39 { NULL, NULL, 0, 0, 0, 0, NULL }
41 static struct m_struct_st stream_opts = {
42 "dvd",
43 sizeof(struct stream_priv_s),
44 &stream_priv_dflts,
45 stream_opts_fields
48 int dvd_nav_still=0; /* are we on a still picture? */
50 static int seek(stream_t *s, off_t newpos);
52 static dvdnav_priv_t * new_dvdnav_stream(char * filename) {
53 char * title_str;
54 dvdnav_priv_t *priv;
56 if (!filename)
57 return NULL;
59 if (!(priv=calloc(1,sizeof(dvdnav_priv_t))))
60 return NULL;
62 if (!(priv->filename=strdup(filename))) {
63 free(priv);
64 return NULL;
67 if(dvdnav_open(&(priv->dvdnav),priv->filename)!=DVDNAV_STATUS_OK)
69 free(priv->filename);
70 free(priv);
71 return NULL;
74 if (!priv->dvdnav) {
75 free(priv);
76 return NULL;
79 if(1) //from vlc: if not used dvdnav from cvs will fail
81 int len, event;
82 char buf[2048];
84 dvdnav_get_next_block(priv->dvdnav,buf,&event,&len);
85 dvdnav_sector_search(priv->dvdnav, 0, SEEK_SET);
88 /* turn off dvdnav caching */
89 dvdnav_set_readahead_flag(priv->dvdnav, 0);
90 if(dvdnav_set_PGC_positioning_flag(priv->dvdnav, 1) != DVDNAV_STATUS_OK)
91 mp_msg(MSGT_OPEN,MSGL_ERR,"stream_dvdnav, failed to set PGC positioning\n");
92 #if 1
93 /* report the title?! */
94 if (dvdnav_get_title_string(priv->dvdnav,&title_str)==DVDNAV_STATUS_OK) {
95 mp_msg(MSGT_IDENTIFY, MSGL_INFO,"Title: '%s'\n",title_str);
97 #endif
99 //dvdnav_event_clear(priv);
101 return priv;
104 static void dvdnav_get_highlight (dvdnav_priv_t *priv, int display_mode) {
105 pci_t *pnavpci = NULL;
106 dvdnav_highlight_event_t *hlev = &(priv->hlev);
107 int btnum;
109 if (!priv || !priv->dvdnav)
110 return;
112 pnavpci = dvdnav_get_current_nav_pci (priv->dvdnav);
113 if (!pnavpci)
114 return;
116 dvdnav_get_current_highlight (priv->dvdnav, &(hlev->buttonN));
117 hlev->display = display_mode; /* show */
119 if (hlev->buttonN > 0 && pnavpci->hli.hl_gi.btn_ns > 0 && hlev->display) {
120 for (btnum = 0; btnum < pnavpci->hli.hl_gi.btn_ns; btnum++) {
121 btni_t *btni = &(pnavpci->hli.btnit[btnum]);
123 if (hlev->buttonN == btnum + 1) {
124 hlev->sx = FFMIN (btni->x_start, btni->x_end);
125 hlev->ex = FFMAX (btni->x_start, btni->x_end);
126 hlev->sy = FFMIN (btni->y_start, btni->y_end);
127 hlev->ey = FFMAX (btni->y_start, btni->y_end);
129 hlev->palette = (btni->btn_coln == 0) ? 0 :
130 pnavpci->hli.btn_colit.btn_coli[btni->btn_coln - 1][0];
131 break;
134 } else { /* hide button or no button */
135 hlev->sx = hlev->ex = 0;
136 hlev->sy = hlev->ey = 0;
137 hlev->palette = hlev->buttonN = 0;
141 static int dvdnav_stream_read(dvdnav_priv_t * priv, unsigned char *buf, int *len) {
142 int event = DVDNAV_NOP;
144 if (!len) return -1;
145 *len=-1;
146 if (!priv) return -1;
147 if (!buf) return -1;
149 if (dvd_nav_still) {
150 mp_msg(MSGT_OPEN,MSGL_V, "%s: got a stream_read while I should be asleep!\n",__FUNCTION__);
151 *len=0;
152 return -1;
155 if (dvdnav_get_next_block(priv->dvdnav,buf,&event,len)!=DVDNAV_STATUS_OK) {
156 mp_msg(MSGT_OPEN,MSGL_V, "Error getting next block from DVD %d (%s)\n",event, dvdnav_err_to_string(priv->dvdnav) );
157 *len=-1;
159 else if (event!=DVDNAV_BLOCK_OK) {
160 // need to handle certain events internally (like skipping stills)
161 switch (event) {
162 case DVDNAV_NAV_PACKET:
163 return event;
164 case DVDNAV_STILL_FRAME: {
165 dvdnav_still_event_t *still_event = (dvdnav_still_event_t*)(buf);
166 //if (priv->started) dvd_nav_still=1;
167 //else
168 dvdnav_still_skip(priv->dvdnav); // don't let dvdnav stall on this image
170 break;
172 case DVDNAV_HIGHLIGHT: {
173 dvdnav_get_highlight (priv, 1);
174 break;
176 case DVDNAV_CELL_CHANGE: {
177 dvdnav_cell_change_event_t *ev = (dvdnav_cell_change_event_t*)buf;
178 if(ev->pgc_length)
179 priv->duration = ev->pgc_length/90;
180 break;
182 case DVDNAV_SPU_CLUT_CHANGE: {
183 memcpy(priv->spu_clut, buf, 16*sizeof(unsigned int));
184 priv->spu_set = 1;
185 break;
187 case DVDNAV_WAIT:
188 dvdnav_wait_skip(priv->dvdnav);
189 break;
192 *len=0;
194 return event;
197 static void update_title_len(stream_t *stream) {
198 dvdnav_priv_t *priv = stream->priv;
199 dvdnav_status_t status;
200 uint32_t pos = 0, len = 0;
202 status = dvdnav_get_position(priv->dvdnav, &pos, &len);
203 if(status == DVDNAV_STATUS_OK && len) {
204 stream->end_pos = (off_t) len * 2048;
205 stream->seek = seek;
206 } else {
207 stream->seek = NULL;
208 stream->end_pos = 0;
213 static int seek(stream_t *s, off_t newpos) {
214 uint32_t pos = 0, len = 0, sector = 0;
215 dvdnav_priv_t *priv = s->priv;
217 if(s->end_pos && newpos > s->end_pos)
218 newpos = s->end_pos;
219 sector = newpos / 2048ULL;
220 if(dvdnav_sector_search(priv->dvdnav, (uint64_t) sector, SEEK_SET) != DVDNAV_STATUS_OK)
221 goto fail;
223 s->pos = newpos;
225 return 1;
227 fail:
228 mp_msg(MSGT_STREAM,MSGL_INFO,"dvdnav_stream, seeking to %"PRIu64" failed: %s\n", newpos, dvdnav_err_to_string(priv->dvdnav));
230 return 1;
233 static void stream_dvdnav_close(stream_t *s) {
234 dvdnav_priv_t *priv = s->priv;
235 dvdnav_close(priv->dvdnav);
236 priv->dvdnav = NULL;
237 free(priv);
241 static int fill_buffer(stream_t *s, char *but, int len)
243 int event;
245 dvdnav_priv_t* priv=s->priv;
246 len=0;
247 if(!s->end_pos)
248 update_title_len(s);
249 while(!len) /* grab all event until DVDNAV_BLOCK_OK (len=2048), DVDNAV_STOP or DVDNAV_STILL_FRAME */
251 event=dvdnav_stream_read(priv, s->buffer, &len);
252 if(event==-1 || len==-1)
254 mp_msg(MSGT_CPLAYER,MSGL_ERR, "DVDNAV stream read error!\n");
255 return 0;
257 switch (event) {
258 case DVDNAV_STOP:
259 case DVDNAV_BLOCK_OK:
260 case DVDNAV_NAV_PACKET:
261 return len;
262 case DVDNAV_VTS_CHANGE: {
263 int tit = 0, part = 0;
264 s->end_pos = 0;
265 update_title_len(s);
266 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) == DVDNAV_STATUS_OK) {
267 mp_msg(MSGT_CPLAYER,MSGL_V, "\r\nDVDNAV, NEW TITLE %d\r\n", tit);
268 dvdnav_get_highlight (priv, 0);
269 if(priv->title > 0 && tit != priv->title)
270 return 0;
272 break;
274 case DVDNAV_CELL_CHANGE: {
275 if(priv->title > 0 && dvd_last_chapter > 0) {
276 int tit=0, part=0;
277 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) == DVDNAV_STATUS_OK && part > dvd_last_chapter)
278 return 0;
281 break;
284 mp_msg(MSGT_STREAM,MSGL_DBG2,"DVDNAV fill_buffer len: %d\n",len);
285 return len;
288 static int control(stream_t *stream, int cmd, void* arg) {
289 dvdnav_priv_t* priv=stream->priv;
290 int tit, part;
292 switch(cmd)
294 case STREAM_CTRL_SEEK_TO_CHAPTER:
296 int chap = *((unsigned int *)arg)+1;
298 if(chap < 1 || dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
299 break;
300 if(dvdnav_part_play(priv->dvdnav, tit, chap) != DVDNAV_STATUS_OK)
301 break;
302 return 1;
304 case STREAM_CTRL_GET_NUM_CHAPTERS:
306 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
307 break;
308 if(dvdnav_get_number_of_parts(priv->dvdnav, tit, &part) != DVDNAV_STATUS_OK)
309 break;
310 if(!part)
311 break;
312 *((unsigned int *)arg) = part;
313 return 1;
315 case STREAM_CTRL_GET_CURRENT_CHAPTER:
317 if(dvdnav_current_title_info(priv->dvdnav, &tit, &part) != DVDNAV_STATUS_OK)
318 break;
319 *((unsigned int *)arg) = part - 1;
320 return 1;
322 case STREAM_CTRL_GET_TIME_LENGTH:
324 if(priv->duration)
326 *((double *)arg) = (double)priv->duration / 1000.0;
327 return 1;
329 break;
333 return STREAM_UNSUPORTED;
336 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
337 struct stream_priv_s* p = (struct stream_priv_s*)opts;
338 char *filename;
339 int event,len,tmplen=0;
340 uint32_t pos, l2;
341 dvdnav_priv_t *priv;
342 dvdnav_status_t status;
344 if(p->device) filename = p->device;
345 else if(dvd_device) filename= dvd_device;
346 else filename = DEFAULT_DVD_DEVICE;
347 if(!(priv=new_dvdnav_stream(filename))) {
348 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_CantOpenDVD,filename);
349 return STREAM_UNSUPORTED;
352 if(p->track > 0) {
353 if(dvd_chapter > 0 && dvd_last_chapter > 0 && dvd_chapter > dvd_last_chapter) {
354 mp_msg(MSGT_OPEN,MSGL_FATAL,"dvdnav_stream, invalid chapter range: %d > %d\n", dvd_chapter, dvd_last_chapter);
355 return STREAM_UNSUPORTED;
357 priv->title = p->track;
358 if(dvdnav_title_play(priv->dvdnav, p->track) != DVDNAV_STATUS_OK) {
359 mp_msg(MSGT_OPEN,MSGL_FATAL,"dvdnav_stream, couldn't select title %d, error '%s'\n", p->track, dvdnav_err_to_string(priv->dvdnav));
360 return STREAM_UNSUPORTED;
362 if(dvd_chapter > 0)
363 dvdnav_part_play(priv->dvdnav, p->track, dvd_chapter);
364 } else if(p->track == -1)
365 dvdnav_menu_call(priv->dvdnav, DVD_MENU_Root);
366 else {
367 mp_msg(MSGT_OPEN,MSGL_INFO,"dvdnav_stream, you didn't specify a track number (as in dvdnav://1), playing whole disc\n");
368 dvdnav_menu_call(priv->dvdnav, DVD_MENU_Title);
370 if(dvd_angle > 1)
371 dvdnav_angle_change(priv->dvdnav, dvd_angle);
373 stream->sector_size = 2048;
374 stream->flags = STREAM_READ | STREAM_SEEK;
375 stream->fill_buffer = fill_buffer;
376 stream->seek = seek;
377 stream->control = control;
378 stream->close = stream_dvdnav_close;
379 stream->type = STREAMTYPE_DVDNAV;
380 stream->priv=(void*)priv;
381 *file_format = DEMUXER_TYPE_MPEG_PS;
383 update_title_len(stream);
384 if(!stream->pos)
385 mp_msg(MSGT_OPEN,MSGL_ERR, "INIT ERROR: %d, couldn't get init pos %s\r\n", status, dvdnav_err_to_string(priv->dvdnav));
387 mp_msg(MSGT_OPEN,MSGL_INFO, "Remember to disable MPlayer's cache when playing dvdnav:// streams (adding -nocache to your command line)\r\n");
389 return STREAM_OK;
393 int mp_dvdnav_handle_input(stream_t *stream, int cmd, int *button) {
394 dvdnav_priv_t * priv=(dvdnav_priv_t*)stream->priv;
395 dvdnav_t *nav = priv->dvdnav;
396 dvdnav_status_t status=DVDNAV_STATUS_ERR;
397 pci_t *pci = dvdnav_get_current_nav_pci(nav);
398 int reset = 0;
400 if(cmd != MP_CMD_DVDNAV_SELECT && !pci)
401 return 0;
403 switch(cmd) {
404 case MP_CMD_DVDNAV_UP:
405 status = dvdnav_upper_button_select(nav, pci);
406 break;
407 case MP_CMD_DVDNAV_DOWN:
408 status = dvdnav_lower_button_select(nav, pci);
409 break;
410 case MP_CMD_DVDNAV_LEFT:
411 status = dvdnav_left_button_select(nav, pci);
412 break;
413 case MP_CMD_DVDNAV_RIGHT:
414 status = dvdnav_right_button_select(nav, pci);
415 break;
416 case MP_CMD_DVDNAV_MENU:
417 status = dvdnav_menu_call(nav,DVD_MENU_Root);
418 reset = 1;
419 break;
420 case MP_CMD_DVDNAV_PREVMENU: {
421 int title=0, part=0;
423 dvdnav_current_title_info(nav, &title, &part);
424 if(title) {
425 if(dvdnav_menu_call(nav, DVD_MENU_Part) == DVDNAV_STATUS_OK
426 || dvdnav_menu_call(nav, DVD_MENU_Title) == DVDNAV_STATUS_OK) {
427 reset = 1;
428 break;
431 if(dvdnav_menu_call(nav, DVD_MENU_Root) == DVDNAV_STATUS_OK)
432 reset = 1;
434 break;
435 case MP_CMD_DVDNAV_SELECT:
436 status = dvdnav_button_activate(nav, pci);
437 if(status == DVDNAV_STATUS_OK) reset = 1;
438 break;
439 case MP_CMD_DVDNAV_MOUSECLICK:
441 this is a workaround: in theory the simple dvdnav_lower_button_select()+dvdnav_button_activate()
442 should be enough (and generally it is), but there are cases when the calls to dvdnav_lower_button_select()
443 and friends fail! Hence we have to call dvdnav_mouse_activate(priv->mousex, priv->mousey) with
444 the coodinates saved by mp_dvdnav_update_mouse_pos().
445 This last call always works well
447 status = dvdnav_mouse_activate(nav, pci, priv->mousex, priv->mousey);
448 break;
449 default:
450 mp_msg(MSGT_CPLAYER, MSGL_V, "Unknown DVDNAV cmd %d\n", cmd);
451 break;
454 if(status == DVDNAV_STATUS_OK)
455 dvdnav_get_current_highlight(nav, button);
457 return reset;
460 void mp_dvdnav_update_mouse_pos(stream_t *stream, int32_t x, int32_t y, int* button) {
461 dvdnav_priv_t * priv=(dvdnav_priv_t*)stream->priv;
462 dvdnav_t *nav = priv->dvdnav;
463 dvdnav_status_t status;
464 pci_t *pci = dvdnav_get_current_nav_pci(nav);
466 if(!pci) return;
468 status = dvdnav_mouse_select(nav, pci, x, y);
469 if(status == DVDNAV_STATUS_OK) dvdnav_get_current_highlight(nav, button);
470 else *button = -1;
471 priv->mousex = x;
472 priv->mousey = y;
476 * \brief dvdnav_aid_from_lang() returns the audio id corresponding to the language code 'lang'
477 * \param stream: - stream pointer
478 * \param lang: 2-characters language code[s], eventually separated by spaces of commas
479 * \return -1 on error, current subtitle id if successful
481 int dvdnav_aid_from_lang(stream_t *stream, unsigned char *language) {
482 dvdnav_priv_t * priv=(dvdnav_priv_t*)stream->priv;
483 int k;
484 uint8_t format, lg;
485 uint16_t lang, lcode;;
487 #ifdef DVDNAV_FORMAT_AC3
488 //this macro is defined only in libdvdnav-cvs
489 while(language && strlen(language)>=2) {
490 lcode = (language[0] << 8) | (language[1]);
491 for(k=0; k<32; k++) {
492 lg = dvdnav_get_audio_logical_stream(priv->dvdnav, k);
493 if(lg == 0xff) continue;
494 lang = dvdnav_audio_stream_to_lang(priv->dvdnav, lg);
495 if(lang != 0xFFFF && lang == lcode) {
496 format = dvdnav_audio_stream_format(priv->dvdnav, lg);
497 switch(format) {
498 case DVDNAV_FORMAT_AC3:
499 return k+128;
500 case DVDNAV_FORMAT_DTS:
501 return k+136;
502 case DVDNAV_FORMAT_LPCM:
503 return k+160;
504 case DVDNAV_FORMAT_MPEGAUDIO:
505 return k;
506 default:
507 return -1;
511 language += 2;
512 while(language[0]==',' || language[0]==' ') ++language;
514 #endif
515 return -1;
519 * \brief dvdnav_lang_from_aid() assigns to buf the language corresponding to audio id 'aid'
520 * \param stream: - stream pointer
521 * \param sid: physical subtitle id
522 * \param buf: buffer to contain the 2-chars language string
523 * \return 0 on error, 1 if successful
525 int dvdnav_lang_from_aid(stream_t *stream, int aid, unsigned char *buf) {
526 uint8_t lg;
527 uint16_t lang;
528 dvdnav_priv_t * priv=(dvdnav_priv_t*)stream->priv;
530 if(aid < 0)
531 return 0;
532 lg = dvdnav_get_audio_logical_stream(priv->dvdnav, aid & 0x7);
533 if(lg == 0xff) return 0;
534 lang = dvdnav_audio_stream_to_lang(priv->dvdnav, lg);
535 if(lang == 0xffff) return 0;
536 buf[0] = lang >> 8;
537 buf[1] = lang & 0xFF;
538 buf[2] = 0;
539 return 1;
544 * \brief dvdnav_sid_from_lang() returns the subtitle id corresponding to the language code 'lang'
545 * \param stream: - stream pointer
546 * \param lang: 2-characters language code[s], eventually separated by spaces of commas
547 * \return -1 on error, current subtitle id if successful
549 int dvdnav_sid_from_lang(stream_t *stream, unsigned char *language) {
550 dvdnav_priv_t * priv=(dvdnav_priv_t*)stream->priv;
551 uint8_t format, lg, k;
552 uint16_t lang, lcode;
554 while(language && strlen(language)>=2) {
555 lcode = (language[0] << 8) | (language[1]);
556 for(k=0; k<32; k++) {
557 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, k);
558 if(lg == 0xff) continue;
559 lang = dvdnav_spu_stream_to_lang(priv->dvdnav, lg);
560 if(lang != 0xFFFF && lang == lcode) {
561 return k;
564 language += 2;
565 while(language[0]==',' || language[0]==' ') ++language;
567 return -1;
571 * \brief dvdnav_lang_from_sid() assigns to buf the language corresponding to subtitle id 'sid'
572 * \param stream: - stream pointer
573 * \param sid: physical subtitle id
574 * \param buf: buffer to contain the 2-chars language string
575 * \return 0 on error, 1 if successful
577 int dvdnav_lang_from_sid(stream_t *stream, int sid, unsigned char *buf) {
578 uint8_t lg, k;
579 uint16_t lang;
580 dvdnav_priv_t *priv=(dvdnav_priv_t*)stream->priv;
581 if(sid < 0) return 0;
582 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, sid);
583 lang = dvdnav_spu_stream_to_lang(priv->dvdnav, lg);
584 if(lang == 0xffff) return 0;
585 buf[0] = lang >> 8;
586 buf[1] = lang & 0xFF;
587 buf[2] = 0;
588 return 1;
592 * \brief dvdnav_number_of_subs() returns the count of available subtitles
593 * \param stream: - stream pointer
594 * \return 0 on error, something meaningful otherwise
596 int dvdnav_number_of_subs(stream_t *stream) {
597 dvdnav_priv_t * priv=(dvdnav_priv_t*)stream->priv;
598 uint8_t lg, k, n=0;
600 for(k=0; k<32; k++) {
601 lg = dvdnav_get_spu_logical_stream(priv->dvdnav, k);
602 if(lg == 0xff) continue;
603 n++;
605 return n;
608 unsigned int *mp_dvdnav_get_spu_clut(stream_t *stream) {
609 dvdnav_priv_t *priv=(dvdnav_priv_t*)stream->priv;
610 if(!priv->spu_set) return NULL;
611 return priv->spu_clut;
614 void mp_dvdnav_get_highlight (stream_t *stream, nav_highlight_t *hl) {
615 dvdnav_priv_t *priv = (dvdnav_priv_t *) stream->priv;
616 dvdnav_highlight_event_t hlev = priv->hlev;
618 hl->sx = hlev.sx;
619 hl->sy = hlev.sy;
620 hl->ex = hlev.ex;
621 hl->ey = hlev.ey;
624 stream_info_t stream_info_dvdnav = {
625 "DVDNAV stream",
626 "null",
629 open_s,
630 { "dvdnav", NULL },
631 &stream_opts,
632 1 // Urls are an option string