]> git.sesse.net Git - vlc/blob - modules/demux/mkv/demux.cpp
6ebcaef65184cc4661ca6e0bffcefcf88b4ab639
[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 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;
464
465     // verify the EBML Header
466     p_l0 = p_estream->FindNextID(EBML_INFO(EbmlHead), 0xFFFFFFFFL);
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, we only support Matroska version 1 for now
474     p_l0->Read(*p_estream, EBML_CLASS_CONTEXT(EbmlHead), i_upper_lvl, p_l0, true);
475
476     EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
477     if (std::string(doc_type) != "matroska" && std::string(doc_type) != "webm" )
478     {
479         msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
480         return NULL;
481     }
482
483     EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
484     if (uint64(doc_read_version) > 2)
485     {
486         msg_Err( p_demux, "matroska file needs version %"PRId64" but only versions 1 & 2 supported", uint64(doc_read_version));
487         return NULL;
488     }
489
490     delete p_l0;
491
492
493     // find all segments in this file
494     p_l0 = p_estream->FindNextID(EBML_INFO(KaxSegment), 0xFFFFFFFFFLL);
495     if (p_l0 == NULL)
496     {
497         return NULL;
498     }
499
500     matroska_stream_c *p_stream1 = new matroska_stream_c();
501
502     while (p_l0 != 0)
503     {
504         if ( MKV_IS_ID( p_l0, KaxSegment) )
505         {
506             EbmlParser  *ep;
507             matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
508             b_keep_segment = b_initial;
509
510             ep = new EbmlParser(p_estream, p_l0, &demuxer );
511             p_segment1->ep = ep;
512             p_segment1->segment = (KaxSegment*)p_l0;
513
514             while ((p_l1 = ep->Get()))
515             {
516                 if (MKV_IS_ID(p_l1, KaxInfo))
517                 {
518                     // find the families of this segment
519                     KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
520
521                     p_info->Read(*p_estream, EBML_CLASS_CONTEXT(KaxInfo), i_upper_lvl, p_l2, true);
522                     for( size_t i = 0; i < p_info->ListSize(); i++ )
523                     {
524                         EbmlElement *l = (*p_info)[i];
525
526                         if( MKV_IS_ID( l, KaxSegmentUID ) )
527                         {
528                             KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
529                             b_keep_segment = (FindSegment( *p_uid ) == NULL);
530                             if ( !b_keep_segment )
531                                 break; // this segment is already known
532                             delete p_segment1->p_segment_uid;
533                             p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
534                         }
535                         else if( MKV_IS_ID( l, KaxPrevUID ) )
536                         {
537                             p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
538                         }
539                         else if( MKV_IS_ID( l, KaxNextUID ) )
540                         {
541                             p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
542                         }
543                         else if( MKV_IS_ID( l, KaxSegmentFamily ) )
544                         {
545                             KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
546                             p_segment1->families.push_back( p_fam );
547                         }
548                     }
549                     if( b_keep_segment || !p_segment1->p_segment_uid )
550                         opened_segments.push_back( p_segment1 );
551                     break;
552                 }
553             }
554             if ( b_keep_segment )
555             {
556                 b_keep_stream = true;
557                 p_stream1->segments.push_back( p_segment1 );
558             }
559             else
560             {
561                 p_segment1->segment = NULL;
562                 delete p_segment1;
563             }
564         }
565         if (p_l0->IsFiniteSize() )
566         {
567             p_l0->SkipData(*p_estream, KaxMatroska_Context);
568             p_l0 = p_estream->FindNextID(EBML_INFO(KaxSegment), 0xFFFFFFFFL);
569         }
570         else
571         {
572             p_l0 = NULL;
573         }
574     }
575
576     if ( !b_keep_stream )
577     {
578         delete p_stream1;
579         p_stream1 = NULL;
580     }
581
582     return p_stream1;
583 }
584
585 void demux_sys_t::InitUi()
586 {
587     msg_Dbg( &demuxer, "Starting the UI Hook" );
588
589     /* FIXME hack hack hack hack FIXME */
590     /* Get p_input and create variable */
591     p_input = demux_GetParentInput( &demuxer );
592     if( p_input )
593     {
594         var_Create( p_input, "x-start", VLC_VAR_INTEGER );
595         var_Create( p_input, "y-start", VLC_VAR_INTEGER );
596         var_Create( p_input, "x-end", VLC_VAR_INTEGER );
597         var_Create( p_input, "y-end", VLC_VAR_INTEGER );
598         var_Create( p_input, "color", VLC_VAR_ADDRESS );
599         var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
600         var_Create( p_input, "highlight", VLC_VAR_BOOL );
601     }
602
603     /* Now create our event thread catcher */
604     p_ev = new event_thread_t(&demuxer);
605 }
606
607 void demux_sys_t::CleanUi()
608 {
609     delete p_ev;
610     p_ev = NULL;
611
612     if( p_input )
613     {
614         var_Destroy( p_input, "highlight" );
615         var_Destroy( p_input, "x-start" );
616         var_Destroy( p_input, "x-end" );
617         var_Destroy( p_input, "y-start" );
618         var_Destroy( p_input, "y-end" );
619         var_Destroy( p_input, "color" );
620         var_Destroy( p_input, "menu-palette" );
621
622         vlc_object_release( p_input );
623     }
624
625     msg_Dbg( &demuxer, "Stopping the UI Hook" );
626 }
627
628 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
629 {
630     for (size_t i=0; i<opened_segments.size(); i++)
631     {
632         opened_segments[i]->PreloadFamily( of_segment );
633     }
634 }
635
636 // preload all the linked segments for all preloaded segments
637 void demux_sys_t::PreloadLinked()
638 {
639     size_t i, j;
640     virtual_segment_c *p_seg;
641
642     p_current_segment = VirtualFromSegments( &opened_segments );
643
644     used_segments.push_back( p_current_segment );
645
646     // publish all editions of all usable segment
647     for ( i=0; i< used_segments.size(); i++ )
648     {
649         p_seg = used_segments[i];
650         if ( p_seg->Editions() != NULL )
651         {
652             input_title_t *p_title = vlc_input_title_New();
653             p_seg->i_sys_title = i;
654             int i_chapters;
655
656             // TODO use a name for each edition, let the TITLE deal with a codec name
657             for ( j=0; j<p_seg->Editions()->size(); j++ )
658             {
659                 if ( p_title->psz_name == NULL )
660                 {
661                     const char* psz_tmp = (*p_seg->Editions())[j]->GetMainName().c_str();
662                     if( *psz_tmp != '\0' )
663                         p_title->psz_name = strdup( psz_tmp );
664                 }
665
666                 i_chapters = 0;
667                 ( *p_seg->Editions() )[j]->PublishChapters( *p_title, i_chapters, 0 );
668             }
669
670             // create a name if there is none
671             if ( p_title->psz_name == NULL )
672             {
673                 if( asprintf(&(p_title->psz_name), "%s %d", N_("Segment"), (int)i) == -1 )
674                     p_title->psz_name = NULL;
675             }
676
677             titles.push_back( p_title );
678         }
679     }
680
681     // TODO decide which segment should be first used (VMG for DVD)
682 }
683
684 virtual_segment_c *demux_sys_t::VirtualFromSegments( std::vector<matroska_segment_c*> *p_segments ) const
685 {
686     virtual_segment_c *p_result = new virtual_segment_c( p_segments );
687     return p_result;
688 }
689
690 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
691 {
692     if ( p_new_segment != NULL && p_new_segment != p_current_segment )
693     {
694         if ( p_current_segment != NULL && p_current_segment->CurrentSegment() != NULL )
695             p_current_segment->CurrentSegment()->UnSelect();
696
697         p_current_segment = p_new_segment;
698         i_current_title = p_new_segment->i_sys_title;
699     }
700     if( !p_current_segment->CurrentSegment() )
701         return false;
702     if( !p_current_segment->CurrentSegment()->b_cues )
703         msg_Warn( &p_current_segment->CurrentSegment()->sys.demuxer, "no cues/empty cues found->seek won't be precise" );
704
705     f_duration = p_current_segment->Duration();
706
707     /* add information */
708     p_current_segment->CurrentSegment()->InformationCreate( );
709     p_current_segment->CurrentSegment()->Select( 0 );
710
711     return true;
712 }
713
714 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, virtual_chapter_c * p_chapter )
715 {
716     // if the segment is not part of the current segment, select the new one
717     if ( &vsegment != p_current_segment )
718     {
719         PreparePlayback( &vsegment );
720     }
721
722     if ( p_chapter )
723     {
724         if ( !p_chapter->p_chapter || !p_chapter->p_chapter->Enter( true ) )
725         {
726             // jump to the location in the found segment
727             vsegment.Seek( demuxer, p_chapter->i_virtual_start_time, -1, p_chapter, -1 );
728         }
729     }
730
731 }
732
733 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
734 {
735     for (size_t i=0; i<opened_segments.size(); i++)
736     {
737         if ( *opened_segments[i]->p_segment_uid == uid )
738             return opened_segments[i];
739     }
740     return NULL;
741 }
742
743 virtual_chapter_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
744                                         bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
745                                         const void *p_cookie,
746                                         size_t i_cookie_size,
747                                         virtual_segment_c * &p_segment_found )
748 {
749     virtual_chapter_c *p_result = NULL;
750     for (size_t i=0; i<used_segments.size(); i++)
751     {
752         p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
753         if ( p_result != NULL )
754         {
755             p_segment_found = used_segments[i];
756             break;
757         }
758     }
759     return p_result;
760 }
761
762 virtual_chapter_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
763 {
764     virtual_chapter_c *p_result = NULL;
765     for (size_t i=0; i<used_segments.size(); i++)
766     {
767         p_result = used_segments[i]->FindChapter( i_find_uid );
768         if ( p_result != NULL )
769         {
770             p_segment_found = used_segments[i];
771             break;
772         }
773     }
774     return p_result;
775 }
776