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