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