Add try/catch when reading elements with libmatroska.
[vlc/vlc-2.0.git] / modules / demux / mkv / demux.cpp
1
2 /*****************************************************************************
3  * mkv.cpp : matroska demuxer
4  *****************************************************************************
5  * Copyright (C) 2003-2004 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9  *          Steve Lhomme <steve.lhomme@free.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include "demux.hpp"
27 #include "stream_io_callback.hpp"
28 #include "Ebml_parser.hpp"
29
30 event_thread_t::event_thread_t(demux_t *p_demux) : p_demux(p_demux)
31 {
32     vlc_mutex_init( &lock );
33     vlc_cond_init( &wait );
34     is_running = false;
35 }
36 event_thread_t::~event_thread_t()
37 {
38     ResetPci();
39     vlc_cond_destroy( &wait );
40     vlc_mutex_destroy( &lock );
41 }
42
43 void event_thread_t::SetPci(const pci_t *data)
44 {
45     vlc_mutex_locker l(&lock);
46
47     pci_packet = *data;
48
49 #ifndef WORDS_BIGENDIAN
50     for( uint8_t button = 1; button <= pci_packet.hli.hl_gi.btn_ns; button++) {
51         btni_t *button_ptr = &(pci_packet.hli.btnit[button-1]);
52         binary *p_data = (binary*) button_ptr;
53
54         uint16 i_x_start = ((p_data[0] & 0x3F) << 4 ) + ( p_data[1] >> 4 );
55         uint16 i_x_end   = ((p_data[1] & 0x03) << 8 ) + p_data[2];
56         uint16 i_y_start = ((p_data[3] & 0x3F) << 4 ) + ( p_data[4] >> 4 );
57         uint16 i_y_end   = ((p_data[4] & 0x03) << 8 ) + p_data[5];
58         button_ptr->x_start = i_x_start;
59         button_ptr->x_end   = i_x_end;
60         button_ptr->y_start = i_y_start;
61         button_ptr->y_end   = i_y_end;
62
63     }
64     for ( uint8_t i = 0; i<3; i++ )
65         for ( uint8_t j = 0; j<2; j++ )
66             pci_packet.hli.btn_colit.btn_coli[i][j] = U32_AT( &pci_packet.hli.btn_colit.btn_coli[i][j] );
67 #endif
68     if( !is_running )
69     {
70         b_abort = false;
71         is_running = !vlc_clone( &thread, EventThread, this, VLC_THREAD_PRIORITY_LOW );
72     }
73 }
74 void event_thread_t::ResetPci()
75 {
76     if( !is_running )
77         return;
78
79     vlc_mutex_lock( &lock );
80     b_abort = true;
81     vlc_cond_signal( &wait );
82     vlc_mutex_unlock( &lock );
83
84     vlc_join( thread, NULL );
85     is_running = false;
86 }
87 int event_thread_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
88                                 vlc_value_t oldval, vlc_value_t newval, void *p_data )
89 {
90     VLC_UNUSED( oldval ); VLC_UNUSED( newval );
91     event_thread_t *p_ev = (event_thread_t *) p_data;
92     vlc_mutex_lock( &p_ev->lock );
93     if( psz_var[6] == 'c' )
94     {
95         p_ev->b_clicked = true;
96         msg_Dbg( p_this, "Event Mouse: clicked");
97     }
98     else if( psz_var[6] == 'm' )
99         p_ev->b_moved = true;
100     vlc_cond_signal( &p_ev->wait );
101     vlc_mutex_unlock( &p_ev->lock );
102
103     return VLC_SUCCESS;
104 }
105
106 int event_thread_t::EventKey( vlc_object_t *p_this, char const *,
107                               vlc_value_t, vlc_value_t newval, void *p_data )
108 {
109     event_thread_t *p_ev = (event_thread_t *) p_data;
110     vlc_mutex_lock( &p_ev->lock );
111     p_ev->i_key_action = newval.i_int;
112     vlc_cond_signal( &p_ev->wait );
113     vlc_mutex_unlock( &p_ev->lock );
114     msg_Dbg( p_this, "Event Key");
115
116     return VLC_SUCCESS;
117 }
118
119 int event_thread_t::EventInput( vlc_object_t *p_this, char const *,
120                                 vlc_value_t, vlc_value_t newval, void *p_data )
121 {
122     VLC_UNUSED( p_this );
123     event_thread_t *p_ev = (event_thread_t *) p_data;
124     vlc_mutex_lock( &p_ev->lock );
125     if( newval.i_int == INPUT_EVENT_VOUT )
126     {
127         p_ev->b_vout |= true;
128         vlc_cond_signal( &p_ev->wait );
129     }
130     vlc_mutex_unlock( &p_ev->lock );
131
132     return VLC_SUCCESS;
133 }
134
135 void event_thread_t::EventThread()
136 {
137     demux_sys_t    *p_sys = p_demux->p_sys;
138     vlc_object_t   *p_vout = NULL;
139     int canc = vlc_savecancel ();
140
141     b_moved      = false;
142     b_clicked    = false;
143     i_key_action = 0;
144     b_vout       = true;
145
146     /* catch all key event */
147     var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, this );
148     /* catch input event */
149     var_AddCallback( p_sys->p_input, "intf-event", EventInput, this );
150
151     /* main loop */
152     for( ;; )
153     {
154         vlc_mutex_lock( &lock );
155         while( !b_abort && !i_key_action && !b_moved && !b_clicked && !b_vout)
156             vlc_cond_wait( &wait, &lock );
157
158         if( b_abort )
159         {
160             vlc_mutex_unlock( &lock );
161             break;
162         }
163
164         /* KEY part */
165         if( i_key_action )
166         {
167             msg_Dbg( p_demux, "Handle Key Event");
168
169             pci_t *pci = &pci_packet;
170
171             uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
172
173             switch( i_key_action )
174             {
175             case ACTIONID_NAV_LEFT:
176                 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
177                 {
178                     btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
179                     if ( p_button_ptr->left > 0 && p_button_ptr->left <= pci->hli.hl_gi.btn_ns )
180                     {
181                         i_curr_button = p_button_ptr->left;
182                         p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
183                         btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
184                         if ( button_ptr.auto_action_mode )
185                         {
186                             vlc_mutex_unlock( &lock );
187                             vlc_mutex_lock( &p_sys->lock_demuxer );
188
189                             // process the button action
190                             p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
191
192                             vlc_mutex_unlock( &p_sys->lock_demuxer );
193                             vlc_mutex_lock( &lock );
194                         }
195                     }
196                 }
197                 break;
198             case ACTIONID_NAV_RIGHT:
199                 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
200                 {
201                     btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
202                     if ( p_button_ptr->right > 0 && p_button_ptr->right <= pci->hli.hl_gi.btn_ns )
203                     {
204                         i_curr_button = p_button_ptr->right;
205                         p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
206                         btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
207                         if ( button_ptr.auto_action_mode )
208                         {
209                             vlc_mutex_unlock( &lock );
210                             vlc_mutex_lock( &p_sys->lock_demuxer );
211
212                             // process the button action
213                             p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
214
215                             vlc_mutex_unlock( &p_sys->lock_demuxer );
216                             vlc_mutex_lock( &lock );
217                         }
218                     }
219                 }
220                 break;
221             case ACTIONID_NAV_UP:
222                 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
223                 {
224                     btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
225                     if ( p_button_ptr->up > 0 && p_button_ptr->up <= pci->hli.hl_gi.btn_ns )
226                     {
227                         i_curr_button = p_button_ptr->up;
228                         p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
229                         btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
230                         if ( button_ptr.auto_action_mode )
231                         {
232                             vlc_mutex_unlock( &lock );
233                             vlc_mutex_lock( &p_sys->lock_demuxer );
234
235                             // process the button action
236                             p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
237
238                             vlc_mutex_unlock( &p_sys->lock_demuxer );
239                             vlc_mutex_lock( &lock );
240                         }
241                     }
242                 }
243                 break;
244             case ACTIONID_NAV_DOWN:
245                 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
246                 {
247                     btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
248                     if ( p_button_ptr->down > 0 && p_button_ptr->down <= pci->hli.hl_gi.btn_ns )
249                     {
250                         i_curr_button = p_button_ptr->down;
251                         p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
252                         btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
253                         if ( button_ptr.auto_action_mode )
254                         {
255                             vlc_mutex_unlock( &lock );
256                             vlc_mutex_lock( &p_sys->lock_demuxer );
257
258                             // process the button action
259                             p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
260
261                             vlc_mutex_unlock( &p_sys->lock_demuxer );
262                             vlc_mutex_lock( &lock );
263                         }
264                     }
265                 }
266                 break;
267             case ACTIONID_NAV_ACTIVATE:
268                 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
269                 {
270                     btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
271
272                     vlc_mutex_unlock( &lock );
273                     vlc_mutex_lock( &p_sys->lock_demuxer );
274
275                     // process the button action
276                     p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
277
278                     vlc_mutex_unlock( &p_sys->lock_demuxer );
279                     vlc_mutex_lock( &lock );
280                 }
281                 break;
282             default:
283                 break;
284             }
285             i_key_action = 0;
286         }
287
288         /* MOUSE part */
289         if( p_vout && ( b_moved || b_clicked ) )
290         {
291             int x, y;
292
293             var_GetCoords( p_vout, "mouse-moved", &x, &y );
294             pci_t *pci = &pci_packet;
295
296             if( b_clicked )
297             {
298                 int32_t button;
299                 int32_t best,dist,d;
300                 int32_t mx,my,dx,dy;
301
302                 msg_Dbg( p_demux, "Handle Mouse Event: Mouse clicked x(%d)*y(%d)", x, y);
303
304                 // get current button
305                 best = 0;
306                 dist = 0x08000000; /* >> than  (720*720)+(567*567); */
307                 for(button = 1; button <= pci->hli.hl_gi.btn_ns; button++)
308                 {
309                     btni_t *button_ptr = &(pci->hli.btnit[button-1]);
310
311                     if(((unsigned)x >= button_ptr->x_start)
312                      && ((unsigned)x <= button_ptr->x_end)
313                      && ((unsigned)y >= button_ptr->y_start)
314                      && ((unsigned)y <= button_ptr->y_end))
315                     {
316                         mx = (button_ptr->x_start + button_ptr->x_end)/2;
317                         my = (button_ptr->y_start + button_ptr->y_end)/2;
318                         dx = mx - x;
319                         dy = my - y;
320                         d = (dx*dx) + (dy*dy);
321                         /* If the mouse is within the button and the mouse is closer
322                         * to the center of this button then it is the best choice. */
323                         if(d < dist) {
324                             dist = d;
325                             best = button;
326                         }
327                     }
328                 }
329
330                 if ( best != 0)
331                 {
332                     btni_t button_ptr = pci->hli.btnit[best-1];
333                     uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
334
335                     msg_Dbg( &p_sys->demuxer, "Clicked button %d", best );
336                     vlc_mutex_unlock( &lock );
337                     vlc_mutex_lock( &p_sys->lock_demuxer );
338
339                     // process the button action
340                     p_sys->dvd_interpretor.SetSPRM( 0x88, best );
341                     p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
342
343                     msg_Dbg( &p_sys->demuxer, "Processed button %d", best );
344
345                     // select new button
346                     if ( best != i_curr_button )
347                     {
348                         uint32_t i_palette;
349
350                         if(button_ptr.btn_coln != 0) {
351                             i_palette = pci->hli.btn_colit.btn_coli[button_ptr.btn_coln-1][1];
352                         } else {
353                             i_palette = 0;
354                         }
355
356                         for( int i = 0; i < 4; i++ )
357                         {
358                             uint32_t i_yuv = 0xFF;//p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
359                             uint8_t i_alpha = (i_palette>>(i*4))&0x0f;
360                             i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
361
362                             p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
363                             p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
364                             p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
365                             p_sys->palette[i][3] = i_alpha;
366                         }
367
368                         vlc_global_lock( VLC_HIGHLIGHT_MUTEX );
369                         var_SetInteger( p_sys->p_input, "x-start",
370                                         button_ptr.x_start );
371                         var_SetInteger( p_sys->p_input, "x-end",
372                                         button_ptr.x_end );
373                         var_SetInteger( p_sys->p_input, "y-start",
374                                         button_ptr.y_start );
375                         var_SetInteger( p_sys->p_input, "y-end",
376                                         button_ptr.y_end );
377                         var_SetAddress( p_sys->p_input, "menu-palette",
378                                         p_sys->palette );
379                         var_SetBool( p_sys->p_input, "highlight", true );
380                         vlc_global_unlock( VLC_HIGHLIGHT_MUTEX );
381                     }
382                     vlc_mutex_unlock( &p_sys->lock_demuxer );
383                     vlc_mutex_lock( &lock );
384                 }
385             }
386             else if( b_moved )
387             {
388 //                dvdnav_mouse_select( NULL, pci, x, y );
389             }
390
391             b_moved = false;
392             b_clicked = false;
393         }
394
395         b_vout = false;
396         vlc_mutex_unlock( &lock );
397
398         /* Always check vout */
399         if( p_vout && !vlc_object_alive (p_vout) )
400         {
401             var_DelCallback( p_vout, "mouse-moved", EventMouse, this );
402             var_DelCallback( p_vout, "mouse-clicked", EventMouse, this );
403             vlc_object_release( p_vout );
404             p_vout = NULL;
405         }
406
407         else if( p_vout == NULL )
408         {
409             p_vout = (vlc_object_t*) input_GetVout(p_sys->p_input);
410             if( p_vout)
411             {
412                 var_AddCallback( p_vout, "mouse-moved", EventMouse, this );
413                 var_AddCallback( p_vout, "mouse-clicked", EventMouse, this );
414             }
415         }
416     }
417
418     /* Release callback */
419     if( p_vout )
420     {
421         var_DelCallback( p_vout, "mouse-moved", EventMouse, this );
422         var_DelCallback( p_vout, "mouse-clicked", EventMouse, this );
423         vlc_object_release( p_vout );
424     }
425     var_DelCallback( p_sys->p_input, "intf-event", EventInput, this );
426     var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, this );
427
428     vlc_restorecancel (canc);
429 }
430
431 void *event_thread_t::EventThread(void *data)
432 {
433     static_cast<event_thread_t*>(data)->EventThread();
434     return NULL;
435 }
436
437
438 demux_sys_t::~demux_sys_t()
439 {
440     CleanUi();
441     size_t i;
442     for ( i=0; i<streams.size(); i++ )
443         delete streams[i];
444     for ( i=0; i<opened_segments.size(); i++ )
445         delete opened_segments[i];
446     for ( i=0; i<used_segments.size(); i++ )
447         delete used_segments[i];
448     for ( i=0; i<stored_attachments.size(); i++ )
449         delete stored_attachments[i];
450     if( meta ) vlc_meta_Delete( meta );
451
452     while( titles.size() )
453     { vlc_input_title_Delete( titles.back() ); titles.pop_back();}
454
455     vlc_mutex_destroy( &lock_demuxer );
456 }
457
458
459 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial )
460 {
461     int i_upper_lvl = 0;
462     EbmlElement *p_l0, *p_l1, *p_l2;
463     bool b_keep_stream = false, b_keep_segment = false;
464
465     /* verify the EBML Header... it shouldn't be bigger than 1kB */
466     p_l0 = p_estream->FindNextID(EBML_INFO(EbmlHead), 1024);
467     if (p_l0 == NULL)
468     {
469         msg_Err( p_demux, "No EBML header found" );
470         return NULL;
471     }
472
473     /* verify we can read this Segment */
474     try
475     {
476         p_l0->Read(*p_estream, EBML_CLASS_CONTEXT(EbmlHead), i_upper_lvl, p_l0, true);
477     }
478     catch(...)
479     {
480         msg_Err(p_demux, "EBML Header Read failed");
481         return NULL;
482     }
483
484     EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
485     if (std::string(doc_type) != "matroska" && std::string(doc_type) != "webm" )
486     {
487         msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
488         return NULL;
489     }
490
491     EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
492     if (uint64(doc_read_version) > 2)
493     {
494         msg_Err( p_demux, "matroska file needs version %"PRId64" but only versions 1 & 2 supported", uint64(doc_read_version));
495         return NULL;
496     }
497
498     delete p_l0;
499
500
501     // find all segments in this file
502     p_l0 = p_estream->FindNextID(EBML_INFO(KaxSegment), UINT64_MAX);
503     if (p_l0 == NULL)
504     {
505         return NULL;
506     }
507
508     matroska_stream_c *p_stream1 = new matroska_stream_c();
509
510     while (p_l0 != 0)
511     {
512         if ( MKV_IS_ID( p_l0, KaxSegment) )
513         {
514             EbmlParser  *ep;
515             matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
516
517             ep = new EbmlParser(p_estream, p_l0, &demuxer );
518             p_segment1->ep = ep;
519             p_segment1->segment = (KaxSegment*)p_l0;
520
521             while ((p_l1 = ep->Get()))
522             {
523                 if (MKV_IS_ID(p_l1, KaxInfo))
524                 {
525                     // find the families of this segment
526                     KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
527                     b_keep_segment = b_initial;
528                     try
529                     {
530                         p_info->Read(*p_estream, EBML_CLASS_CONTEXT(KaxInfo), i_upper_lvl, p_l2, true);
531                     }
532                     catch (...)
533                     {
534                         msg_Err( p_demux, "KaxInfo found but corrupted");
535                         break;
536                     }
537                     for( size_t i = 0; i < p_info->ListSize(); i++ )
538                     {
539                         EbmlElement *l = (*p_info)[i];
540
541                         if( MKV_IS_ID( l, KaxSegmentUID ) )
542                         {
543                             KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
544                             b_keep_segment = (FindSegment( *p_uid ) == NULL);
545                             delete p_segment1->p_segment_uid;
546                             p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
547                             if ( !b_keep_segment )
548                                 break; // this segment is already known
549                         }
550                         else if( MKV_IS_ID( l, KaxPrevUID ) )
551                         {
552                             p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
553                             p_segment1->b_ref_external_segments = true;
554                         }
555                         else if( MKV_IS_ID( l, KaxNextUID ) )
556                         {
557                             p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
558                             p_segment1->b_ref_external_segments = true;
559                         }
560                         else if( MKV_IS_ID( l, KaxSegmentFamily ) )
561                         {
562                             KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
563                             p_segment1->families.push_back( p_fam );
564                         }
565                     }
566                     if( b_keep_segment || !p_segment1->p_segment_uid )
567                         opened_segments.push_back( p_segment1 );
568                     break;
569                 }
570             }
571             if ( b_keep_segment || !p_segment1->p_segment_uid )
572             {
573                 b_keep_stream = true;
574                 p_stream1->segments.push_back( p_segment1 );
575             }
576             else
577             {
578                 p_segment1->segment = NULL;
579                 delete p_segment1;
580             }
581         }
582         if (p_l0->IsFiniteSize() )
583         {
584             p_l0->SkipData(*p_estream, KaxMatroska_Context);
585             p_l0 = p_estream->FindNextID(EBML_INFO(KaxSegment), UINT64_MAX);
586         }
587         else
588         {
589             p_l0 = NULL;
590         }
591     }
592
593     if ( !b_keep_stream )
594     {
595         delete p_stream1;
596         p_stream1 = NULL;
597     }
598
599     return p_stream1;
600 }
601
602 void demux_sys_t::InitUi()
603 {
604     msg_Dbg( &demuxer, "Starting the UI Hook" );
605
606     /* FIXME hack hack hack hack FIXME */
607     /* Get p_input and create variable */
608     p_input = demux_GetParentInput( &demuxer );
609     if( p_input )
610     {
611         var_Create( p_input, "x-start", VLC_VAR_INTEGER );
612         var_Create( p_input, "y-start", VLC_VAR_INTEGER );
613         var_Create( p_input, "x-end", VLC_VAR_INTEGER );
614         var_Create( p_input, "y-end", VLC_VAR_INTEGER );
615         var_Create( p_input, "color", VLC_VAR_ADDRESS );
616         var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
617         var_Create( p_input, "highlight", VLC_VAR_BOOL );
618     }
619
620     /* Now create our event thread catcher */
621     p_ev = new event_thread_t(&demuxer);
622 }
623
624 void demux_sys_t::CleanUi()
625 {
626     delete p_ev;
627     p_ev = NULL;
628
629     if( p_input )
630     {
631         var_Destroy( p_input, "highlight" );
632         var_Destroy( p_input, "x-start" );
633         var_Destroy( p_input, "x-end" );
634         var_Destroy( p_input, "y-start" );
635         var_Destroy( p_input, "y-end" );
636         var_Destroy( p_input, "color" );
637         var_Destroy( p_input, "menu-palette" );
638
639         vlc_object_release( p_input );
640     }
641
642     msg_Dbg( &demuxer, "Stopping the UI Hook" );
643 }
644
645 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
646 {
647     for (size_t i=0; i<opened_segments.size(); i++)
648     {
649         opened_segments[i]->PreloadFamily( of_segment );
650     }
651 }
652
653 // preload all the linked segments for all preloaded segments
654 bool demux_sys_t::PreloadLinked()
655 {
656     size_t i, j;
657     virtual_segment_c *p_seg;
658
659     p_current_segment = VirtualFromSegments( &opened_segments );
660     if ( !p_current_segment )
661         return false;
662
663     used_segments.push_back( p_current_segment );
664
665     // publish all editions of all usable segment
666     for ( i=0; i< used_segments.size(); i++ )
667     {
668         p_seg = used_segments[i];
669         if ( p_seg->Editions() != NULL )
670         {
671             input_title_t *p_title = vlc_input_title_New();
672             p_seg->i_sys_title = i;
673             int i_chapters;
674
675             // TODO use a name for each edition, let the TITLE deal with a codec name
676             for ( j=0; j<p_seg->Editions()->size(); j++ )
677             {
678                 if ( p_title->psz_name == NULL )
679                 {
680                     const char* psz_tmp = (*p_seg->Editions())[j]->GetMainName().c_str();
681                     if( *psz_tmp != '\0' )
682                         p_title->psz_name = strdup( psz_tmp );
683                 }
684
685                 i_chapters = 0;
686                 ( *p_seg->Editions() )[j]->PublishChapters( *p_title, i_chapters, 0 );
687
688                 // Input duration into i_length
689                 p_title->i_length = ( *p_seg->Editions() )[j]->i_duration;
690             }
691
692             // create a name if there is none
693             if ( p_title->psz_name == NULL )
694             {
695                 if( asprintf(&(p_title->psz_name), "%s %d", N_("Segment"), (int)i) == -1 )
696                     p_title->psz_name = NULL;
697             }
698
699             titles.push_back( p_title );
700         }
701     }
702
703     // TODO decide which segment should be first used (VMG for DVD)
704
705     return true;
706 }
707
708 void demux_sys_t::FreeUnused()
709 {
710     size_t i;
711     for( i = 0; i < streams.size(); i++ )
712     {
713         bool used = false;
714         struct matroska_stream_c *p_s = streams[i];
715         for( size_t j = 0; j < p_s->segments.size(); j++ )
716         {
717             if( p_s->segments[j]->b_preloaded )
718             {
719                 used = true;
720                 break;
721             }
722         }
723         if( !used )
724         {
725             streams[i] = NULL;
726             delete p_s;
727         }
728         
729     }
730     for( i = 0; i < opened_segments.size(); i++)
731     {
732         if( !opened_segments[i]->b_preloaded )
733         {
734             delete opened_segments[i];
735             opened_segments[i] = NULL;
736         }
737     }
738 }
739
740 virtual_segment_c *demux_sys_t::VirtualFromSegments( std::vector<matroska_segment_c*> *p_segments ) const
741 {
742     if ( p_segments->empty() )
743         return NULL;
744     virtual_segment_c *p_result = new virtual_segment_c( p_segments );
745     return p_result;
746 }
747
748 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
749 {
750     if ( p_new_segment != NULL && p_new_segment != p_current_segment )
751     {
752         if ( p_current_segment != NULL && p_current_segment->CurrentSegment() != NULL )
753             p_current_segment->CurrentSegment()->UnSelect();
754
755         p_current_segment = p_new_segment;
756         i_current_title = p_new_segment->i_sys_title;
757     }
758     if( !p_current_segment->CurrentSegment() )
759         return false;
760     if( !p_current_segment->CurrentSegment()->b_cues )
761         msg_Warn( &p_current_segment->CurrentSegment()->sys.demuxer, "no cues/empty cues found->seek won't be precise" );
762
763     f_duration = p_current_segment->Duration();
764
765     /* add information */
766     p_current_segment->CurrentSegment()->InformationCreate( );
767     p_current_segment->CurrentSegment()->Select( 0 );
768
769     return true;
770 }
771
772 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, virtual_chapter_c * p_chapter )
773 {
774     // if the segment is not part of the current segment, select the new one
775     if ( &vsegment != p_current_segment )
776     {
777         PreparePlayback( &vsegment );
778     }
779
780     if ( p_chapter )
781     {
782         if ( !p_chapter->p_chapter || !p_chapter->p_chapter->Enter( true ) )
783         {
784             // jump to the location in the found segment
785             vsegment.Seek( demuxer, p_chapter->i_virtual_start_time, -1, p_chapter, -1 );
786         }
787     }
788
789 }
790
791 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
792 {
793     for (size_t i=0; i<opened_segments.size(); i++)
794     {
795         if ( opened_segments[i]->p_segment_uid && *opened_segments[i]->p_segment_uid == uid )
796             return opened_segments[i];
797     }
798     return NULL;
799 }
800
801 virtual_chapter_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
802                                         bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
803                                         const void *p_cookie,
804                                         size_t i_cookie_size,
805                                         virtual_segment_c * &p_segment_found )
806 {
807     virtual_chapter_c *p_result = NULL;
808     for (size_t i=0; i<used_segments.size(); i++)
809     {
810         p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
811         if ( p_result != NULL )
812         {
813             p_segment_found = used_segments[i];
814             break;
815         }
816     }
817     return p_result;
818 }
819
820 virtual_chapter_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
821 {
822     virtual_chapter_c *p_result = NULL;
823     for (size_t i=0; i<used_segments.size(); i++)
824     {
825         p_result = used_segments[i]->FindChapter( i_find_uid );
826         if ( p_result != NULL )
827         {
828             p_segment_found = used_segments[i];
829             break;
830         }
831     }
832     return p_result;
833 }
834