dvdnav.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /*****************************************************************************
  2. * dvdnav.c: DVD module using the dvdnav library.
  3. *****************************************************************************
  4. * Copyright (C) 2004-2009 VLC authors and VideoLAN
  5. * $Id$
  6. *
  7. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22. *****************************************************************************/
  23. /*****************************************************************************
  24. * NOTA BENE: this module requires the linking against a library which is
  25. * known to require licensing under the GNU General Public License version 2
  26. * (or later). Therefore, the result of compiling this module will normally
  27. * be subject to the terms of that later license.
  28. *****************************************************************************/
  29. /*****************************************************************************
  30. * Preamble
  31. *****************************************************************************/
  32. #ifdef HAVE_CONFIG_H
  33. # include "config.h"
  34. #endif
  35. #include <assert.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40. #include <errno.h>
  41. #include <vlc_common.h>
  42. #include <vlc_plugin.h>
  43. #include <vlc_input.h>
  44. #include <vlc_access.h>
  45. #include <vlc_demux.h>
  46. #include <vlc_charset.h>
  47. #include <vlc_fs.h>
  48. #include <vlc_url.h>
  49. #include <vlc_vout.h>
  50. #include <vlc_dialog.h>
  51. #include <vlc_iso_lang.h>
  52. /* FIXME we should find a better way than including that */
  53. #include "../../src/text/iso-639_def.h"
  54. #include <dvdnav/dvdnav.h>
  55. #include "../demux/ps.h"
  56. /*****************************************************************************
  57. * Module descriptor
  58. *****************************************************************************/
  59. #define ANGLE_TEXT N_("DVD angle")
  60. #define ANGLE_LONGTEXT N_( \
  61. "Default DVD angle." )
  62. #define MENU_TEXT N_("Start directly in menu")
  63. #define MENU_LONGTEXT N_( \
  64. "Start the DVD directly in the main menu. This "\
  65. "will try to skip all the useless warning introductions." )
  66. #define LANGUAGE_DEFAULT ("en")
  67. static int Open ( vlc_object_t * );
  68. static void Close( vlc_object_t * );
  69. vlc_module_begin ()
  70. set_shortname( N_("DVD with menus") )
  71. set_description( N_("DVDnav Input") )
  72. set_category( CAT_INPUT )
  73. set_subcategory( SUBCAT_INPUT_ACCESS )
  74. add_integer( "dvdnav-angle", 1, ANGLE_TEXT,
  75. ANGLE_LONGTEXT, false )
  76. add_bool( "dvdnav-menu", true,
  77. MENU_TEXT, MENU_LONGTEXT, false )
  78. set_capability( "access_demux", 5 )
  79. add_shortcut( "dvd", "dvdnav", "file" )
  80. set_callbacks( Open, Close )
  81. vlc_module_end ()
  82. /* Shall we use libdvdnav's read ahead cache? */
  83. #ifdef __OS2__
  84. #define DVD_READ_CACHE 0
  85. #else
  86. #define DVD_READ_CACHE 1
  87. #endif
  88. /*****************************************************************************
  89. * Local prototypes
  90. *****************************************************************************/
  91. struct demux_sys_t
  92. {
  93. dvdnav_t *dvdnav;
  94. /* */
  95. bool b_reset_pcr;
  96. struct
  97. {
  98. bool b_created;
  99. bool b_enabled;
  100. vlc_mutex_t lock;
  101. vlc_timer_t timer;
  102. } still;
  103. /* track */
  104. ps_track_t tk[PS_TK_COUNT];
  105. int i_mux_rate;
  106. /* for spu variables */
  107. input_thread_t *p_input;
  108. /* event */
  109. vout_thread_t *p_vout;
  110. /* palette for menus */
  111. uint32_t clut[16];
  112. uint8_t palette[4][4];
  113. bool b_spu_change;
  114. /* Aspect ration */
  115. struct {
  116. unsigned i_num;
  117. unsigned i_den;
  118. } sar;
  119. /* */
  120. int i_title;
  121. input_title_t **title;
  122. /* lenght of program group chain */
  123. mtime_t i_pgc_length;
  124. int i_vobu_index;
  125. int i_vobu_flush;
  126. };
  127. static int Control( demux_t *, int, va_list );
  128. static int Demux( demux_t * );
  129. static int DemuxBlock( demux_t *, const uint8_t *, int );
  130. static void DemuxForceStill( demux_t * );
  131. static void DemuxTitles( demux_t * );
  132. static void ESSubtitleUpdate( demux_t * );
  133. static void ButtonUpdate( demux_t *, bool );
  134. static void ESNew( demux_t *, int );
  135. static int ProbeDVD( const char * );
  136. static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
  137. static int ControlInternal( demux_t *, int, ... );
  138. static void StillTimer( void * );
  139. static int EventMouse( vlc_object_t *, char const *,
  140. vlc_value_t, vlc_value_t, void * );
  141. static int EventIntf( vlc_object_t *, char const *,
  142. vlc_value_t, vlc_value_t, void * );
  143. /*****************************************************************************
  144. * DemuxOpen:
  145. *****************************************************************************/
  146. static int Open( vlc_object_t *p_this )
  147. {
  148. demux_t *p_demux = (demux_t*)p_this;
  149. demux_sys_t *p_sys;
  150. dvdnav_t *p_dvdnav;
  151. int i_angle;
  152. char *psz_file;
  153. char *psz_code;
  154. bool forced = false;
  155. if( p_demux->psz_access != NULL
  156. && !strncmp(p_demux->psz_access, "dvd", 3) )
  157. forced = true;
  158. if( !p_demux->psz_file || !*p_demux->psz_file )
  159. {
  160. /* Only when selected */
  161. if( !forced )
  162. return VLC_EGENERIC;
  163. psz_file = var_InheritString( p_this, "dvd" );
  164. }
  165. else
  166. psz_file = strdup( p_demux->psz_file );
  167. #if defined( _WIN32 ) || defined( __OS2__ )
  168. if( psz_file != NULL )
  169. {
  170. /* Remove trailing backslash, otherwise dvdnav_open will fail */
  171. size_t flen = strlen( psz_file );
  172. if( flen > 0 && psz_file[flen - 1] == '\\' )
  173. psz_file[flen - 1] = '\0';
  174. }
  175. else
  176. psz_file = strdup("");
  177. #endif
  178. if( unlikely(psz_file == NULL) )
  179. return VLC_EGENERIC;
  180. /* Try some simple probing to avoid going through dvdnav_open too often */
  181. if( !forced && ProbeDVD( psz_file ) != VLC_SUCCESS )
  182. {
  183. free( psz_file );
  184. return VLC_EGENERIC;
  185. }
  186. /* Open dvdnav */
  187. const char *psz_path = ToLocale( psz_file );
  188. if( dvdnav_open( &p_dvdnav, psz_path ) != DVDNAV_STATUS_OK )
  189. p_dvdnav = NULL;
  190. LocaleFree( psz_path );
  191. if( p_dvdnav == NULL )
  192. {
  193. msg_Warn( p_demux, "cannot open DVD (%s)", psz_file);
  194. free( psz_file );
  195. return VLC_EGENERIC;
  196. }
  197. free( psz_file );
  198. /* Fill p_demux field */
  199. DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
  200. p_sys->dvdnav = p_dvdnav;
  201. p_sys->b_reset_pcr = false;
  202. ps_track_init( p_sys->tk );
  203. p_sys->sar.i_num = 0;
  204. p_sys->sar.i_den = 0;
  205. p_sys->i_mux_rate = 0;
  206. p_sys->i_pgc_length = 0;
  207. p_sys->b_spu_change = false;
  208. p_sys->i_vobu_index = 0;
  209. p_sys->i_vobu_flush = 0;
  210. if( 1 )
  211. {
  212. // Hack for libdvdnav CVS.
  213. // Without it dvdnav_get_number_of_titles() fails.
  214. // Remove when fixed in libdvdnav CVS.
  215. uint8_t buffer[DVD_VIDEO_LB_LEN];
  216. int i_event, i_len;
  217. if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
  218. == DVDNAV_STATUS_ERR )
  219. {
  220. msg_Warn( p_demux, "dvdnav_get_next_block failed" );
  221. }
  222. dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
  223. }
  224. /* Configure dvdnav */
  225. if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
  226. DVDNAV_STATUS_OK )
  227. {
  228. msg_Warn( p_demux, "cannot set read-a-head flag" );
  229. }
  230. if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
  231. DVDNAV_STATUS_OK )
  232. {
  233. msg_Warn( p_demux, "cannot set PGC positioning flag" );
  234. }
  235. /* Set menu language */
  236. psz_code = DemuxGetLanguageCode( p_demux, "menu-language" );
  237. if( dvdnav_menu_language_select( p_sys->dvdnav, psz_code ) !=
  238. DVDNAV_STATUS_OK )
  239. {
  240. msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
  241. psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
  242. /* We try to fall back to 'en' */
  243. if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
  244. dvdnav_menu_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
  245. }
  246. free( psz_code );
  247. /* Set audio language */
  248. psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
  249. if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
  250. DVDNAV_STATUS_OK )
  251. {
  252. msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
  253. psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
  254. /* We try to fall back to 'en' */
  255. if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
  256. dvdnav_audio_language_select( p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
  257. }
  258. free( psz_code );
  259. /* Set spu language */
  260. psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
  261. if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
  262. DVDNAV_STATUS_OK )
  263. {
  264. msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
  265. psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
  266. /* We try to fall back to 'en' */
  267. if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
  268. dvdnav_spu_language_select(p_sys->dvdnav, (char*)LANGUAGE_DEFAULT );
  269. }
  270. free( psz_code );
  271. DemuxTitles( p_demux );
  272. if( var_CreateGetBool( p_demux, "dvdnav-menu" ) )
  273. {
  274. msg_Dbg( p_demux, "trying to go to dvd menu" );
  275. if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
  276. {
  277. msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
  278. dialog_Fatal( p_demux, _("Playback failure"), "%s",
  279. _("VLC cannot set the DVD's title. It possibly "
  280. "cannot decrypt the entire disc.") );
  281. dvdnav_close( p_sys->dvdnav );
  282. free( p_sys );
  283. return VLC_EGENERIC;
  284. }
  285. if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
  286. DVDNAV_STATUS_OK )
  287. {
  288. /* Try going to menu root */
  289. if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
  290. DVDNAV_STATUS_OK )
  291. msg_Warn( p_demux, "cannot go to dvd menu" );
  292. }
  293. }
  294. i_angle = var_CreateGetInteger( p_demux, "dvdnav-angle" );
  295. if( i_angle <= 0 ) i_angle = 1;
  296. /* FIXME hack hack hack hack FIXME */
  297. /* Get p_input and create variable */
  298. p_sys->p_input = demux_GetParentInput( p_demux );
  299. var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
  300. var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
  301. var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
  302. var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
  303. var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
  304. var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
  305. var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
  306. /* catch vout creation event */
  307. var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
  308. p_sys->still.b_enabled = false;
  309. vlc_mutex_init( &p_sys->still.lock );
  310. if( !vlc_timer_create( &p_sys->still.timer, StillTimer, p_sys ) )
  311. p_sys->still.b_created = true;
  312. return VLC_SUCCESS;
  313. }
  314. /*****************************************************************************
  315. * Close:
  316. *****************************************************************************/
  317. static void Close( vlc_object_t *p_this )
  318. {
  319. demux_t *p_demux = (demux_t*)p_this;
  320. demux_sys_t *p_sys = p_demux->p_sys;
  321. /* Stop vout event handler */
  322. var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
  323. if( p_sys->p_vout != NULL )
  324. { /* Should not happen, but better be safe than sorry. */
  325. msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
  326. var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
  327. var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
  328. }
  329. /* Stop still image handler */
  330. if( p_sys->still.b_created )
  331. vlc_timer_destroy( p_sys->still.timer );
  332. vlc_mutex_destroy( &p_sys->still.lock );
  333. var_Destroy( p_sys->p_input, "highlight" );
  334. var_Destroy( p_sys->p_input, "x-start" );
  335. var_Destroy( p_sys->p_input, "x-end" );
  336. var_Destroy( p_sys->p_input, "y-start" );
  337. var_Destroy( p_sys->p_input, "y-end" );
  338. var_Destroy( p_sys->p_input, "color" );
  339. var_Destroy( p_sys->p_input, "menu-palette" );
  340. vlc_object_release( p_sys->p_input );
  341. for( int i = 0; i < PS_TK_COUNT; i++ )
  342. {
  343. ps_track_t *tk = &p_sys->tk[i];
  344. if( tk->b_seen )
  345. {
  346. es_format_Clean( &tk->fmt );
  347. if( tk->es ) es_out_Del( p_demux->out, tk->es );
  348. }
  349. }
  350. /* Free the array of titles */
  351. for( int i = 0; i < p_sys->i_title; i++ )
  352. vlc_input_title_Delete( p_sys->title[i] );
  353. TAB_CLEAN( p_sys->i_title, p_sys->title );
  354. dvdnav_close( p_sys->dvdnav );
  355. free( p_sys );
  356. }
  357. /*****************************************************************************
  358. * Control:
  359. *****************************************************************************/
  360. static int Control( demux_t *p_demux, int i_query, va_list args )
  361. {
  362. demux_sys_t *p_sys = p_demux->p_sys;
  363. input_title_t ***ppp_title;
  364. int i;
  365. switch( i_query )
  366. {
  367. case DEMUX_SET_POSITION:
  368. case DEMUX_GET_POSITION:
  369. case DEMUX_GET_TIME:
  370. case DEMUX_GET_LENGTH:
  371. {
  372. uint32_t pos, len;
  373. if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
  374. DVDNAV_STATUS_OK || len == 0 )
  375. {
  376. return VLC_EGENERIC;
  377. }
  378. switch( i_query )
  379. {
  380. case DEMUX_GET_POSITION:
  381. *va_arg( args, double* ) = (double)pos / (double)len;
  382. return VLC_SUCCESS;
  383. case DEMUX_SET_POSITION:
  384. pos = va_arg( args, double ) * len;
  385. if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
  386. DVDNAV_STATUS_OK )
  387. {
  388. return VLC_SUCCESS;
  389. }
  390. break;
  391. case DEMUX_GET_TIME:
  392. if( p_sys->i_pgc_length > 0 )
  393. {
  394. *va_arg( args, int64_t * ) = p_sys->i_pgc_length*pos/len;
  395. return VLC_SUCCESS;
  396. }
  397. break;
  398. case DEMUX_GET_LENGTH:
  399. if( p_sys->i_pgc_length > 0 )
  400. {
  401. *va_arg( args, int64_t * ) = (int64_t)p_sys->i_pgc_length;
  402. return VLC_SUCCESS;
  403. }
  404. break;
  405. }
  406. return VLC_EGENERIC;
  407. }
  408. /* Special for access_demux */
  409. case DEMUX_CAN_PAUSE:
  410. case DEMUX_CAN_SEEK:
  411. case DEMUX_CAN_CONTROL_PACE:
  412. /* TODO */
  413. *va_arg( args, bool * ) = true;
  414. return VLC_SUCCESS;
  415. case DEMUX_SET_PAUSE_STATE:
  416. return VLC_SUCCESS;
  417. case DEMUX_GET_TITLE_INFO:
  418. ppp_title = va_arg( args, input_title_t*** );
  419. *va_arg( args, int* ) = p_sys->i_title;
  420. *va_arg( args, int* ) = 0; /* Title offset */
  421. *va_arg( args, int* ) = 1; /* Chapter offset */
  422. /* Duplicate title infos */
  423. *ppp_title = malloc( p_sys->i_title * sizeof( input_title_t * ) );
  424. for( i = 0; i < p_sys->i_title; i++ )
  425. {
  426. (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
  427. }
  428. return VLC_SUCCESS;
  429. case DEMUX_SET_TITLE:
  430. i = (int)va_arg( args, int );
  431. if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
  432. != DVDNAV_STATUS_OK ) ||
  433. ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
  434. != DVDNAV_STATUS_OK ) )
  435. {
  436. msg_Warn( p_demux, "cannot set title/chapter" );
  437. return VLC_EGENERIC;
  438. }
  439. p_demux->info.i_update |=
  440. INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
  441. p_demux->info.i_title = i;
  442. p_demux->info.i_seekpoint = 0;
  443. return VLC_SUCCESS;
  444. case DEMUX_SET_SEEKPOINT:
  445. i = va_arg( args, int );
  446. if( p_demux->info.i_title == 0 )
  447. {
  448. static const int argtab[] = {
  449. DVD_MENU_Escape,
  450. DVD_MENU_Root,
  451. DVD_MENU_Title,
  452. DVD_MENU_Part,
  453. DVD_MENU_Subpicture,
  454. DVD_MENU_Audio,
  455. DVD_MENU_Angle
  456. };
  457. enum { numargs = sizeof(argtab)/sizeof(int) };
  458. if( (unsigned)i >= numargs || DVDNAV_STATUS_OK !=
  459. dvdnav_menu_call(p_sys->dvdnav,argtab[i]) )
  460. return VLC_EGENERIC;
  461. }
  462. else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
  463. i + 1 ) != DVDNAV_STATUS_OK )
  464. {
  465. msg_Warn( p_demux, "cannot set title/chapter" );
  466. return VLC_EGENERIC;
  467. }
  468. p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
  469. p_demux->info.i_seekpoint = i;
  470. return VLC_SUCCESS;
  471. case DEMUX_GET_PTS_DELAY:
  472. *va_arg( args, int64_t * ) =
  473. INT64_C(1000) * var_InheritInteger( p_demux, "disc-caching" );
  474. return VLC_SUCCESS;
  475. case DEMUX_GET_META:
  476. {
  477. const char *title_name = NULL;
  478. dvdnav_get_title_string(p_sys->dvdnav, &title_name);
  479. if( (NULL != title_name) && ('\0' != title_name[0]) )
  480. {
  481. vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
  482. vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
  483. return VLC_SUCCESS;
  484. }
  485. return VLC_EGENERIC;
  486. }
  487. case DEMUX_NAV_ACTIVATE:
  488. {
  489. pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
  490. ButtonUpdate( p_demux, true );
  491. dvdnav_button_activate( p_sys->dvdnav, pci );
  492. break;
  493. }
  494. case DEMUX_NAV_UP:
  495. {
  496. pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
  497. dvdnav_upper_button_select( p_sys->dvdnav, pci );
  498. break;
  499. }
  500. case DEMUX_NAV_DOWN:
  501. {
  502. pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
  503. dvdnav_lower_button_select( p_sys->dvdnav, pci );
  504. break;
  505. }
  506. case DEMUX_NAV_LEFT:
  507. {
  508. pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
  509. dvdnav_left_button_select( p_sys->dvdnav, pci );
  510. break;
  511. }
  512. case DEMUX_NAV_RIGHT:
  513. {
  514. pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
  515. dvdnav_right_button_select( p_sys->dvdnav, pci );
  516. break;
  517. }
  518. /* TODO implement others */
  519. default:
  520. return VLC_EGENERIC;
  521. }
  522. return VLC_SUCCESS;
  523. }
  524. static int ControlInternal( demux_t *p_demux, int i_query, ... )
  525. {
  526. va_list args;
  527. int i_result;
  528. va_start( args, i_query );
  529. i_result = Control( p_demux, i_query, args );
  530. va_end( args );
  531. return i_result;
  532. }
  533. /*****************************************************************************
  534. * Demux:
  535. *****************************************************************************/
  536. static int Demux( demux_t *p_demux )
  537. {
  538. demux_sys_t *p_sys = p_demux->p_sys;
  539. uint8_t buffer[DVD_VIDEO_LB_LEN];
  540. uint8_t *packet = buffer;
  541. int i_event;
  542. int i_len;
  543. #if DVD_READ_CACHE
  544. if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
  545. == DVDNAV_STATUS_ERR )
  546. #else
  547. if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
  548. == DVDNAV_STATUS_ERR )
  549. #endif
  550. {
  551. msg_Warn( p_demux, "cannot get next block (%s)",
  552. dvdnav_err_to_string( p_sys->dvdnav ) );
  553. if( p_demux->info.i_title == 0 )
  554. {
  555. msg_Dbg( p_demux, "jumping to first title" );
  556. return ControlInternal( p_demux, DEMUX_SET_TITLE, 1 ) == VLC_SUCCESS ? 1 : -1;
  557. }
  558. return -1;
  559. }
  560. switch( i_event )
  561. {
  562. case DVDNAV_BLOCK_OK: /* mpeg block */
  563. vlc_mutex_lock( &p_sys->still.lock );
  564. vlc_timer_schedule( p_sys->still.timer, false, 0, 0 );
  565. p_sys->still.b_enabled = false;
  566. vlc_mutex_unlock( &p_sys->still.lock );
  567. if( p_sys->b_reset_pcr )
  568. {
  569. es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
  570. p_sys->b_reset_pcr = false;
  571. }
  572. DemuxBlock( p_demux, packet, i_len );
  573. if( p_sys->i_vobu_index > 0 )
  574. {
  575. if( p_sys->i_vobu_flush == p_sys->i_vobu_index )
  576. DemuxForceStill( p_demux );
  577. p_sys->i_vobu_index++;
  578. }
  579. break;
  580. case DVDNAV_NOP: /* Nothing */
  581. msg_Dbg( p_demux, "DVDNAV_NOP" );
  582. break;
  583. case DVDNAV_STILL_FRAME:
  584. {
  585. dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
  586. bool b_still_init = false;
  587. vlc_mutex_lock( &p_sys->still.lock );
  588. if( !p_sys->still.b_enabled )
  589. {
  590. msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
  591. msg_Dbg( p_demux, " - length=0x%x", event->length );
  592. p_sys->still.b_enabled = true;
  593. if( event->length != 0xff && p_sys->still.b_created )
  594. {
  595. mtime_t delay = event->length * CLOCK_FREQ;
  596. vlc_timer_schedule( p_sys->still.timer, false, delay, 0 );
  597. }
  598. b_still_init = true;
  599. }
  600. vlc_mutex_unlock( &p_sys->still.lock );
  601. if( b_still_init )
  602. {
  603. DemuxForceStill( p_demux );
  604. p_sys->b_reset_pcr = true;
  605. }
  606. msleep( 40000 );
  607. break;
  608. }
  609. case DVDNAV_SPU_CLUT_CHANGE:
  610. {
  611. int i;
  612. msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
  613. /* Update color lookup table (16 *uint32_t in packet) */
  614. memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
  615. /* HACK to get the SPU tracks registered in the right order */
  616. for( i = 0; i < 0x1f; i++ )
  617. {
  618. if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
  619. ESNew( p_demux, 0xbd20 + i );
  620. }
  621. /* END HACK */
  622. break;
  623. }
  624. case DVDNAV_SPU_STREAM_CHANGE:
  625. {
  626. dvdnav_spu_stream_change_event_t *event =
  627. (dvdnav_spu_stream_change_event_t*)packet;
  628. int i;
  629. msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
  630. msg_Dbg( p_demux, " - physical_wide=%d",
  631. event->physical_wide );
  632. msg_Dbg( p_demux, " - physical_letterbox=%d",
  633. event->physical_letterbox);
  634. msg_Dbg( p_demux, " - physical_pan_scan=%d",
  635. event->physical_pan_scan );
  636. ESSubtitleUpdate( p_demux );
  637. p_sys->b_spu_change = true;
  638. /* HACK to get the SPU tracks registered in the right order */
  639. for( i = 0; i < 0x1f; i++ )
  640. {
  641. if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
  642. ESNew( p_demux, 0xbd20 + i );
  643. }
  644. /* END HACK */
  645. break;
  646. }
  647. case DVDNAV_AUDIO_STREAM_CHANGE:
  648. {
  649. dvdnav_audio_stream_change_event_t *event =
  650. (dvdnav_audio_stream_change_event_t*)packet;
  651. msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
  652. msg_Dbg( p_demux, " - physical=%d", event->physical );
  653. /* TODO */
  654. break;
  655. }
  656. case DVDNAV_VTS_CHANGE:
  657. {
  658. int32_t i_title = 0;
  659. int32_t i_part = 0;
  660. int i;
  661. dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
  662. msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
  663. msg_Dbg( p_demux, " - vtsN=%d", event->new_vtsN );
  664. msg_Dbg( p_demux, " - domain=%d", event->new_domain );
  665. /* reset PCR */
  666. es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
  667. for( i = 0; i < PS_TK_COUNT; i++ )
  668. {
  669. ps_track_t *tk = &p_sys->tk[i];
  670. if( tk->b_seen )
  671. {
  672. es_format_Clean( &tk->fmt );
  673. if( tk->es ) es_out_Del( p_demux->out, tk->es );
  674. }
  675. tk->b_seen = false;
  676. }
  677. #if defined(HAVE_DVDNAV_GET_VIDEO_RESOLUTION)
  678. uint32_t i_width, i_height;
  679. if( dvdnav_get_video_resolution( p_sys->dvdnav,
  680. &i_width, &i_height ) )
  681. i_width = i_height = 0;
  682. switch( dvdnav_get_video_aspect( p_sys->dvdnav ) )
  683. {
  684. case 0:
  685. p_sys->sar.i_num = 4 * i_height;
  686. p_sys->sar.i_den = 3 * i_width;
  687. break;
  688. case 3:
  689. p_sys->sar.i_num = 16 * i_height;
  690. p_sys->sar.i_den = 9 * i_width;
  691. break;
  692. default:
  693. p_sys->sar.i_num = 0;
  694. p_sys->sar.i_den = 0;
  695. break;
  696. }
  697. #endif
  698. if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
  699. &i_part ) == DVDNAV_STATUS_OK )
  700. {
  701. if( i_title >= 0 && i_title < p_sys->i_title &&
  702. p_demux->info.i_title != i_title )
  703. {
  704. p_demux->info.i_update |= INPUT_UPDATE_TITLE;
  705. p_demux->info.i_title = i_title;
  706. }
  707. }
  708. break;
  709. }
  710. case DVDNAV_CELL_CHANGE:
  711. {
  712. int32_t i_title = 0;
  713. int32_t i_part = 0;
  714. dvdnav_cell_change_event_t *event =
  715. (dvdnav_cell_change_event_t*)packet;
  716. msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
  717. msg_Dbg( p_demux, " - cellN=%d", event->cellN );
  718. msg_Dbg( p_demux, " - pgN=%d", event->pgN );
  719. msg_Dbg( p_demux, " - cell_length=%"PRId64, event->cell_length );
  720. msg_Dbg( p_demux, " - pg_length=%"PRId64, event->pg_length );
  721. msg_Dbg( p_demux, " - pgc_length=%"PRId64, event->pgc_length );
  722. msg_Dbg( p_demux, " - cell_start=%"PRId64, event->cell_start );
  723. msg_Dbg( p_demux, " - pg_start=%"PRId64, event->pg_start );
  724. /* Store the lenght in time of the current PGC */
  725. p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
  726. p_sys->i_vobu_index = 0;
  727. p_sys->i_vobu_flush = 0;
  728. /* FIXME is it correct or there is better way to know chapter change */
  729. if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
  730. &i_part ) == DVDNAV_STATUS_OK )
  731. {
  732. if( i_title >= 0 && i_title < p_sys->i_title )
  733. {
  734. p_demux->info.i_update |= INPUT_UPDATE_TITLE;
  735. p_demux->info.i_title = i_title;
  736. if( i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
  737. p_demux->info.i_seekpoint != i_part - 1 )
  738. {
  739. p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
  740. p_demux->info.i_seekpoint = i_part - 1;
  741. }
  742. }
  743. }
  744. break;
  745. }
  746. case DVDNAV_NAV_PACKET:
  747. {
  748. p_sys->i_vobu_index = 1;
  749. p_sys->i_vobu_flush = 0;
  750. /* Look if we have need to force a flush (and when) */
  751. const pci_gi_t *p_pci_gi = &dvdnav_get_current_nav_pci( p_sys->dvdnav )->pci_gi;
  752. if( p_pci_gi->vobu_se_e_ptm != 0 && p_pci_gi->vobu_se_e_ptm < p_pci_gi->vobu_e_ptm )
  753. {
  754. const dsi_gi_t *p_dsi_gi = &dvdnav_get_current_nav_dsi( p_sys->dvdnav )->dsi_gi;
  755. if( p_dsi_gi->vobu_3rdref_ea != 0 )
  756. p_sys->i_vobu_flush = p_dsi_gi->vobu_3rdref_ea;
  757. else if( p_dsi_gi->vobu_2ndref_ea != 0 )
  758. p_sys->i_vobu_flush = p_dsi_gi->vobu_2ndref_ea;
  759. else if( p_dsi_gi->vobu_1stref_ea != 0 )
  760. p_sys->i_vobu_flush = p_dsi_gi->vobu_1stref_ea;
  761. }
  762. #ifdef DVDNAV_DEBUG
  763. msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
  764. #endif
  765. /* A lot of thing to do here :
  766. * - handle packet
  767. * - fetch pts (for time display)
  768. * - ...
  769. */
  770. DemuxBlock( p_demux, packet, i_len );
  771. if( p_sys->b_spu_change )
  772. {
  773. ButtonUpdate( p_demux, false );
  774. p_sys->b_spu_change = false;
  775. }
  776. break;
  777. }
  778. case DVDNAV_STOP: /* EOF */
  779. msg_Dbg( p_demux, "DVDNAV_STOP" );
  780. #if DVD_READ_CACHE
  781. dvdnav_free_cache_block( p_sys->dvdnav, packet );
  782. #endif
  783. return 0;
  784. case DVDNAV_HIGHLIGHT:
  785. {
  786. dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
  787. msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
  788. msg_Dbg( p_demux, " - display=%d", event->display );
  789. msg_Dbg( p_demux, " - buttonN=%d", event->buttonN );
  790. ButtonUpdate( p_demux, false );
  791. break;
  792. }
  793. case DVDNAV_HOP_CHANNEL:
  794. msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
  795. p_sys->i_vobu_index = 0;
  796. p_sys->i_vobu_flush = 0;
  797. es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
  798. break;
  799. case DVDNAV_WAIT:
  800. msg_Dbg( p_demux, "DVDNAV_WAIT" );
  801. bool b_empty;
  802. es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
  803. if( !b_empty )
  804. {
  805. msleep( 40*1000 );
  806. }
  807. else
  808. {
  809. dvdnav_wait_skip( p_sys->dvdnav );
  810. p_sys->b_reset_pcr = true;
  811. }
  812. break;
  813. default:
  814. msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
  815. break;
  816. }
  817. #if DVD_READ_CACHE
  818. dvdnav_free_cache_block( p_sys->dvdnav, packet );
  819. #endif
  820. return 1;
  821. }
  822. /* Get a 2 char code
  823. * FIXME: partiallyy duplicated from src/input/es_out.c
  824. */
  825. static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
  826. {
  827. const iso639_lang_t *pl;
  828. char *psz_lang;
  829. char *p;
  830. psz_lang = var_CreateGetString( p_demux, psz_var );
  831. if( !psz_lang )
  832. return strdup(LANGUAGE_DEFAULT);
  833. /* XXX: we will use only the first value
  834. * (and ignore other ones in case of a list) */
  835. if( ( p = strchr( psz_lang, ',' ) ) )
  836. *p = '\0';
  837. for( pl = p_languages; pl->psz_eng_name != NULL; pl++ )
  838. {
  839. if( *psz_lang == '\0' )
  840. continue;
  841. if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
  842. !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
  843. !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
  844. !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
  845. break;
  846. }
  847. free( psz_lang );
  848. if( pl->psz_eng_name != NULL )
  849. return strdup( pl->psz_iso639_1 );
  850. return strdup(LANGUAGE_DEFAULT);
  851. }
  852. static void DemuxTitles( demux_t *p_demux )
  853. {
  854. demux_sys_t *p_sys = p_demux->p_sys;
  855. input_title_t *t;
  856. seekpoint_t *s;
  857. int32_t i_titles;
  858. int i;
  859. /* Menu */
  860. t = vlc_input_title_New();
  861. t->b_menu = true;
  862. t->psz_name = strdup( "DVD Menu" );
  863. s = vlc_seekpoint_New();
  864. s->psz_name = strdup( "Resume" );
  865. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  866. s = vlc_seekpoint_New();
  867. s->psz_name = strdup( "Root" );
  868. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  869. s = vlc_seekpoint_New();
  870. s->psz_name = strdup( "Title" );
  871. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  872. s = vlc_seekpoint_New();
  873. s->psz_name = strdup( "Chapter" );
  874. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  875. s = vlc_seekpoint_New();
  876. s->psz_name = strdup( "Subtitle" );
  877. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  878. s = vlc_seekpoint_New();
  879. s->psz_name = strdup( "Audio" );
  880. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  881. s = vlc_seekpoint_New();
  882. s->psz_name = strdup( "Angle" );
  883. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  884. TAB_APPEND( p_sys->i_title, p_sys->title, t );
  885. /* Find out number of titles/chapters */
  886. dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
  887. for( i = 1; i <= i_titles; i++ )
  888. {
  889. int32_t i_chapters;
  890. uint64_t i_title_length;
  891. uint64_t *p_chapters_time;
  892. #if defined(HAVE_DVDNAV_DESCRIBE_TITLE_CHAPTERS)
  893. i_chapters = dvdnav_describe_title_chapters( p_sys->dvdnav, i,
  894. &p_chapters_time,
  895. &i_title_length );
  896. if( i_chapters < 1 )
  897. {
  898. i_title_length = 0;
  899. p_chapters_time = NULL;
  900. }
  901. #else
  902. if( dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters ) != DVDNAV_STATUS_OK )
  903. i_chapters = 0;
  904. i_title_length = 0;
  905. p_chapters_time = NULL;
  906. #endif
  907. t = vlc_input_title_New();
  908. t->i_length = i_title_length * 1000 / 90;
  909. for( int j = 0; j < __MAX( i_chapters, 1 ); j++ )
  910. {
  911. s = vlc_seekpoint_New();
  912. if( p_chapters_time )
  913. s->i_time_offset = p_chapters_time[j] * 1000 / 90;
  914. TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
  915. }
  916. free( p_chapters_time );
  917. TAB_APPEND( p_sys->i_title, p_sys->title, t );
  918. }
  919. }
  920. /*****************************************************************************
  921. * Update functions:
  922. *****************************************************************************/
  923. static void ButtonUpdate( demux_t *p_demux, bool b_mode )
  924. {
  925. demux_sys_t *p_sys = p_demux->p_sys;
  926. int32_t i_title, i_part;
  927. dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
  928. dvdnav_highlight_area_t hl;
  929. int32_t i_button;
  930. bool b_button_ok;
  931. if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
  932. != DVDNAV_STATUS_OK )
  933. {
  934. msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
  935. return;
  936. }
  937. b_button_ok = false;
  938. if( i_button > 0 && i_title == 0 )
  939. {
  940. pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
  941. b_button_ok = DVDNAV_STATUS_OK ==
  942. dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
  943. }
  944. if( b_button_ok )
  945. {
  946. for( unsigned i = 0; i < 4; i++ )
  947. {
  948. uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
  949. uint8_t i_alpha = ( (hl.palette>>(i*4))&0x0f ) * 0xff / 0xf;
  950. p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
  951. p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
  952. p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
  953. p_sys->palette[i][3] = i_alpha;
  954. }
  955. vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
  956. var_SetInteger( p_sys->p_input, "x-start", hl.sx );
  957. var_SetInteger( p_sys->p_input, "x-end", hl.ex );
  958. var_SetInteger( p_sys->p_input, "y-start", hl.sy );
  959. var_SetInteger( p_sys->p_input, "y-end", hl.ey );
  960. var_SetAddress( p_sys->p_input, "menu-palette", p_sys->palette );
  961. var_SetBool( p_sys->p_input, "highlight", true );
  962. msg_Dbg( p_demux, "buttonUpdate %d", i_button );
  963. }
  964. else
  965. {
  966. msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
  967. i_button, i_title );
  968. /* Show all */
  969. vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
  970. var_SetBool( p_sys->p_input, "highlight", false );
  971. }
  972. vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
  973. }
  974. static void ESSubtitleUpdate( demux_t *p_demux )
  975. {
  976. demux_sys_t *p_sys = p_demux->p_sys;
  977. int i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
  978. int32_t i_title, i_part;
  979. ButtonUpdate( p_demux, false );
  980. dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
  981. if( i_title > 0 ) return;
  982. if( i_spu >= 0 && i_spu <= 0x1f )
  983. {
  984. ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
  985. ESNew( p_demux, 0xbd20 + i_spu );
  986. /* be sure to unselect it (reset) */
  987. es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
  988. (bool)false );
  989. /* now select it */
  990. es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
  991. }
  992. else
  993. {
  994. for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
  995. {
  996. ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
  997. if( tk->b_seen )
  998. {
  999. es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
  1000. (bool)false );
  1001. }
  1002. }
  1003. }
  1004. }
  1005. /*****************************************************************************
  1006. * DemuxBlock: demux a given block
  1007. *****************************************************************************/
  1008. static int DemuxBlock( demux_t *p_demux, const uint8_t *p, int len )
  1009. {
  1010. demux_sys_t *p_sys = p_demux->p_sys;
  1011. while( len > 0 )
  1012. {
  1013. int i_size = ps_pkt_size( p, len );
  1014. if( i_size <= 0 || i_size > len )
  1015. {
  1016. break;
  1017. }
  1018. /* Create a block */
  1019. block_t *p_pkt = block_Alloc( i_size );
  1020. memcpy( p_pkt->p_buffer, p, i_size);
  1021. /* Parse it and send it */
  1022. switch( 0x100 | p[3] )
  1023. {
  1024. case 0x1b9:
  1025. case 0x1bb:
  1026. case 0x1bc:
  1027. #ifdef DVDNAV_DEBUG
  1028. if( p[3] == 0xbc )
  1029. {
  1030. msg_Warn( p_demux, "received a PSM packet" );
  1031. }
  1032. else if( p[3] == 0xbb )
  1033. {
  1034. msg_Warn( p_demux, "received a SYSTEM packet" );
  1035. }
  1036. #endif
  1037. block_Release( p_pkt );
  1038. break;
  1039. case 0x1ba:
  1040. {
  1041. int64_t i_scr;
  1042. int i_mux_rate;
  1043. if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
  1044. {
  1045. es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr + 1 );
  1046. if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
  1047. }
  1048. block_Release( p_pkt );
  1049. break;
  1050. }
  1051. default:
  1052. {
  1053. int i_id = ps_pkt_id( p_pkt );
  1054. if( i_id >= 0xc0 )
  1055. {
  1056. ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
  1057. if( !tk->b_seen )
  1058. {
  1059. ESNew( p_demux, i_id );
  1060. }
  1061. if( tk->b_seen && tk->es &&
  1062. !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
  1063. {
  1064. es_out_Send( p_demux->out, tk->es, p_pkt );
  1065. }
  1066. else
  1067. {
  1068. block_Release( p_pkt );
  1069. }
  1070. }
  1071. else
  1072. {
  1073. block_Release( p_pkt );
  1074. }
  1075. break;
  1076. }
  1077. }
  1078. p += i_size;
  1079. len -= i_size;
  1080. }
  1081. return VLC_SUCCESS;
  1082. }
  1083. /*****************************************************************************
  1084. * Force still images to be displayed by sending EOS and stopping buffering.
  1085. *****************************************************************************/
  1086. static void DemuxForceStill( demux_t *p_demux )
  1087. {
  1088. static const uint8_t buffer[] = {
  1089. 0x00, 0x00, 0x01, 0xe0, 0x00, 0x07,
  1090. 0x80, 0x00, 0x00,
  1091. 0x00, 0x00, 0x01, 0xB7,
  1092. };
  1093. DemuxBlock( p_demux, buffer, sizeof(buffer) );
  1094. bool b_empty;
  1095. es_out_Control( p_demux->out, ES_OUT_GET_EMPTY, &b_empty );
  1096. }
  1097. /*****************************************************************************
  1098. * ESNew: register a new elementary stream
  1099. *****************************************************************************/
  1100. static void ESNew( demux_t *p_demux, int i_id )
  1101. {
  1102. demux_sys_t *p_sys = p_demux->p_sys;
  1103. ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
  1104. bool b_select = false;
  1105. if( tk->b_seen ) return;
  1106. if( ps_track_fill( tk, 0, i_id ) )
  1107. {
  1108. msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
  1109. return;
  1110. }
  1111. /* Add a new ES */
  1112. if( tk->fmt.i_cat == VIDEO_ES )
  1113. {
  1114. tk->fmt.video.i_sar_num = p_sys->sar.i_num;
  1115. tk->fmt.video.i_sar_den = p_sys->sar.i_den;
  1116. b_select = true;
  1117. }
  1118. else if( tk->fmt.i_cat == AUDIO_ES )
  1119. {
  1120. int i_audio = -1;
  1121. /* find the audio number PLEASE find another way */
  1122. if( (i_id&0xbdf8) == 0xbd88 ) /* dts */
  1123. {
  1124. i_audio = i_id&0x07;
  1125. }
  1126. else if( (i_id&0xbdf0) == 0xbd80 ) /* a52 */
  1127. {
  1128. i_audio = i_id&0xf;
  1129. }
  1130. else if( (i_id&0xbdf0) == 0xbda0 ) /* lpcm */
  1131. {
  1132. i_audio = i_id&0x1f;
  1133. }
  1134. else if( ( i_id&0xe0 ) == 0xc0 ) /* mpga */
  1135. {
  1136. i_audio = i_id&0x1f;
  1137. }
  1138. if( i_audio >= 0 )
  1139. {
  1140. int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
  1141. if( i_lang != 0xffff )
  1142. {
  1143. tk->fmt.psz_language = malloc( 3 );
  1144. tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
  1145. tk->fmt.psz_language[1] = (i_lang )&0xff;
  1146. tk->fmt.psz_language[2] = 0;
  1147. }
  1148. if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
  1149. {
  1150. b_select = true;
  1151. }
  1152. }
  1153. }
  1154. else if( tk->fmt.i_cat == SPU_ES )
  1155. {
  1156. int32_t i_title, i_part;
  1157. int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
  1158. if( i_lang != 0xffff )
  1159. {
  1160. tk->fmt.psz_language = malloc( 3 );
  1161. tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
  1162. tk->fmt.psz_language[1] = (i_lang )&0xff;
  1163. tk->fmt.psz_language[2] = 0;
  1164. }
  1165. /* Palette */
  1166. tk->fmt.subs.spu.palette[0] = 0xBeef;
  1167. memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
  1168. 16 * sizeof( uint32_t ) );
  1169. /* We select only when we are not in the menu */
  1170. dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
  1171. if( i_title > 0 &&
  1172. dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
  1173. {
  1174. b_select = true;
  1175. }
  1176. }
  1177. tk->es = es_out_Add( p_demux->out, &tk->fmt );
  1178. if( b_select )
  1179. {
  1180. es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
  1181. }
  1182. tk->b_seen = true;
  1183. if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
  1184. }
  1185. /*****************************************************************************
  1186. * Still image end
  1187. *****************************************************************************/
  1188. static void StillTimer( void *p_data )
  1189. {
  1190. demux_sys_t *p_sys = p_data;
  1191. vlc_mutex_lock( &p_sys->still.lock );
  1192. if( likely(p_sys->still.b_enabled) )
  1193. {
  1194. p_sys->still.b_enabled = false;
  1195. dvdnav_still_skip( p_sys->dvdnav );
  1196. }
  1197. vlc_mutex_unlock( &p_sys->still.lock );
  1198. }
  1199. static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
  1200. vlc_value_t oldval, vlc_value_t val, void *p_data )
  1201. {
  1202. demux_t *p_demux = p_data;
  1203. demux_sys_t *p_sys = p_demux->p_sys;
  1204. /* FIXME? PCI usage thread safe? */
  1205. pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
  1206. int x = val.coords.x;
  1207. int y = val.coords.y;
  1208. if( psz_var[6] == 'm' ) /* mouse-moved */
  1209. dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
  1210. else
  1211. {
  1212. assert( psz_var[6] == 'c' ); /* mouse-clicked */
  1213. ButtonUpdate( p_demux, true );
  1214. dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
  1215. }
  1216. (void)p_vout;
  1217. (void)oldval;
  1218. return VLC_SUCCESS;
  1219. }
  1220. static int EventIntf( vlc_object_t *p_input, char const *psz_var,
  1221. vlc_value_t oldval, vlc_value_t val, void *p_data )
  1222. {
  1223. demux_t *p_demux = p_data;
  1224. demux_sys_t *p_sys = p_demux->p_sys;
  1225. if (val.i_int == INPUT_EVENT_VOUT)
  1226. {
  1227. if( p_sys->p_vout != NULL )
  1228. {
  1229. var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
  1230. var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
  1231. vlc_object_release( p_sys->p_vout );
  1232. }
  1233. p_sys->p_vout = input_GetVout( (input_thread_t *)p_input );
  1234. if( p_sys->p_vout != NULL )
  1235. {
  1236. var_AddCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
  1237. var_AddCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
  1238. }
  1239. }
  1240. (void) psz_var; (void) oldval;
  1241. return VLC_SUCCESS;
  1242. }
  1243. /*****************************************************************************
  1244. * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
  1245. *****************************************************************************/
  1246. static int ProbeDVD( const char *psz_name )
  1247. {
  1248. if( !*psz_name )
  1249. /* Triggers libdvdcss autodetection */
  1250. return VLC_SUCCESS;
  1251. int fd = vlc_open( psz_name, O_RDONLY | O_NONBLOCK );
  1252. if( fd == -1 )
  1253. #ifdef HAVE_FDOPENDIR
  1254. return VLC_EGENERIC;
  1255. #else
  1256. return (errno == ENOENT) ? VLC_EGENERIC : VLC_SUCCESS;
  1257. #endif
  1258. int ret = VLC_EGENERIC;
  1259. struct stat stat_info;
  1260. if( fstat( fd, &stat_info ) == -1 )
  1261. goto bailout;
  1262. if( !S_ISREG( stat_info.st_mode ) )
  1263. {
  1264. if( S_ISDIR( stat_info.st_mode ) || S_ISBLK( stat_info.st_mode ) )
  1265. ret = VLC_SUCCESS; /* Let dvdnav_open() do the probing */
  1266. goto bailout;
  1267. }
  1268. /* ISO 9660 volume descriptor */
  1269. char iso_dsc[6];
  1270. if( lseek( fd, 0x8000 + 1, SEEK_SET ) == -1
  1271. || read( fd, iso_dsc, sizeof (iso_dsc) ) < sizeof (iso_dsc)
  1272. || memcmp( iso_dsc, "CD001\x01", 6 ) )
  1273. goto bailout;
  1274. /* Try to find the anchor (2 bytes at LBA 256) */
  1275. uint16_t anchor;
  1276. if( lseek( fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) != -1
  1277. && read( fd, &anchor, 2 ) == 2
  1278. && GetWLE( &anchor ) == 2 )
  1279. ret = VLC_SUCCESS; /* Found a potential anchor */
  1280. bailout:
  1281. close( fd );
  1282. return ret;
  1283. }