]> git.sesse.net Git - vlc/blob - modules/demux/mkv/virtual_segment.cpp
Handle MKV segment change properly
[vlc] / modules / demux / mkv / virtual_segment.cpp
1 /*****************************************************************************
2  * virtual_segment.cpp : virtual segment implementation in the MKV demuxer
3  *****************************************************************************
4  * Copyright © 2003-2011 VideoLAN and VLC authors
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *          Denis Charmet <typx@dinauz.org>
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 #include <vector>
26
27 #include "demux.hpp"
28
29 /* FIXME move this */
30 matroska_segment_c * getSegmentbyUID( KaxSegmentUID * p_uid, std::vector<matroska_segment_c*> *segments )
31 {
32     for( size_t i = 0; i < (*segments).size(); i++ )
33     {
34         if( *p_uid == *((*segments)[i]->p_segment_uid) )
35             return (*segments)[i];
36     }
37     return NULL;
38 }
39
40 virtual_chapter_c * virtual_chapter_c::CreateVirtualChapter( chapter_item_c * p_chap,
41                                                              matroska_segment_c * p_main_segment,
42                                                              std::vector<matroska_segment_c*> * segments,
43                                                              int64_t * usertime_offset, bool b_ordered)
44 {
45     matroska_segment_c * p_segment = p_main_segment;
46
47     if( !p_chap )
48     {
49         /* Dummy chapter use the whole segment */
50         return new virtual_chapter_c( p_segment, NULL, 0, p_segment->i_duration*1000 );
51     }
52
53     int64_t start = ( b_ordered )? *usertime_offset : p_chap->i_start_time;
54     int64_t stop = ( b_ordered )? ( *usertime_offset + p_chap->i_end_time - p_chap->i_start_time ) : p_chap->i_end_time;
55
56     if( p_chap->p_segment_uid && 
57        ( !( p_segment = getSegmentbyUID( (KaxSegmentUID*) p_chap->p_segment_uid,segments ) ) || !b_ordered ) )
58     {
59         msg_Warn( &p_main_segment->sys.demuxer,
60                   "Couldn't find segment 0x%x or not ordered... - ignoring chapter %s",
61                   *( (uint32_t *) p_chap->p_segment_uid->GetBuffer() ),p_chap->psz_name.c_str() );
62         return NULL;
63     }
64
65     /* Preload segment */
66     if ( !p_segment->b_preloaded )
67         p_segment->Preload();
68
69     virtual_chapter_c * p_vchap = new virtual_chapter_c( p_segment, p_chap, start, stop );
70
71     if( !p_vchap )
72         return NULL;
73
74     int64_t tmp = *usertime_offset;
75
76     for( size_t i = 0; i < p_chap->sub_chapters.size(); i++ )
77     {
78         virtual_chapter_c * p_vsubchap = CreateVirtualChapter( p_chap->sub_chapters[i], p_segment, segments, &tmp, b_ordered );
79
80         if( p_vsubchap )
81             p_vchap->sub_chapters.push_back( p_vsubchap );
82     }
83
84     if( tmp == *usertime_offset )
85         *usertime_offset += p_chap->i_end_time - p_chap->i_start_time;
86     else
87         *usertime_offset = tmp;
88
89     msg_Dbg( &p_main_segment->sys.demuxer,
90              "Virtual chapter %s from %"PRId64" to %"PRId64" - " ,
91              p_chap->psz_name.c_str(), p_vchap->i_virtual_start_time, p_vchap->i_virtual_stop_time );
92
93     return p_vchap;
94 }
95
96 virtual_chapter_c::~virtual_chapter_c()
97 {
98     for( size_t i = 0 ; i < sub_chapters.size(); i++ )
99         delete sub_chapters[i];
100 }
101
102
103 virtual_edition_c::virtual_edition_c( chapter_edition_c * p_edit, std::vector<matroska_segment_c*> *opened_segments)
104 {
105     matroska_segment_c *p_main_segment = (*opened_segments)[0];
106     p_edition = p_edit;
107
108     int64_t usertime_offset = 0;
109
110     /* ordered chapters */
111     if( p_edition && p_edition->b_ordered )
112     {
113         b_ordered = true;
114         for( size_t i = 0; i < p_edition->sub_chapters.size(); i++ )
115         {
116             virtual_chapter_c * p_vchap = virtual_chapter_c::CreateVirtualChapter( p_edition->sub_chapters[i],
117                                                                                    p_main_segment, opened_segments,
118                                                                                    &usertime_offset, b_ordered );
119             if( p_vchap )
120                 chapters.push_back( p_vchap );
121         }
122         i_duration = chapters[ chapters.size() - 1 ]->i_virtual_stop_time;
123     }
124     else /* Not ordered or no edition at all */
125     {
126         b_ordered = false;
127         matroska_segment_c * p_cur = p_main_segment;
128         virtual_chapter_c * p_vchap = NULL;
129         int64_t tmp = 0;
130
131         /* check for prev linked segments */
132         /* FIXME to avoid infinite recursion we limit to 5 prev sould be better as parameter */
133         for( int limit = 0; limit < 5 && p_cur->p_prev_segment_uid ; limit++ )
134         {
135             matroska_segment_c * p_prev = NULL;
136             if( ( p_prev = getSegmentbyUID( p_cur->p_prev_segment_uid, opened_segments ) ) )
137             {
138                 tmp = 0;
139                 msg_Dbg( &p_main_segment->sys.demuxer, "Prev segment 0x%x found\n",
140                          *(int32_t*)p_cur->p_prev_segment_uid->GetBuffer() );
141
142                 /* Create virtual_chapter from the first edition if any */
143                 chapter_item_c * p_chap = ( p_prev->stored_editions.size() > 0 )? ((chapter_item_c *)p_prev->stored_editions[0]) : NULL;
144
145                 p_vchap = virtual_chapter_c::CreateVirtualChapter( p_chap, p_prev, opened_segments, &tmp, b_ordered );
146
147                 if( p_vchap )
148                     chapters.insert( chapters.begin(), p_vchap );
149
150                 p_cur = p_prev;
151             }
152             else /* segment not found */
153                 break;
154         }
155
156         tmp = 0;
157
158         /* Append the main segment */
159         p_vchap = virtual_chapter_c::CreateVirtualChapter( (chapter_item_c*) p_edit, p_main_segment,
160                                                            opened_segments, &tmp, b_ordered );
161         if( p_vchap )
162             chapters.push_back( p_vchap );
163
164         /* Append next linked segments */
165         for( int limit = 0; limit < 5 && p_cur->p_next_segment_uid; limit++ )
166         {
167             matroska_segment_c * p_next = NULL;
168             if( ( p_next = getSegmentbyUID( p_cur->p_next_segment_uid, opened_segments ) ) )
169             {
170                 tmp = 0;
171                 msg_Dbg( &p_main_segment->sys.demuxer, "Next segment 0x%x found\n",
172                          *(int32_t*) p_cur->p_next_segment_uid->GetBuffer() );
173
174                 /* Create virtual_chapter from the first edition if any */
175                 chapter_item_c * p_chap = ( p_next->stored_editions.size() > 0 )?( (chapter_item_c *)p_next->stored_editions[0] ) : NULL;
176
177                  p_vchap = virtual_chapter_c::CreateVirtualChapter( p_chap, p_next, opened_segments, &tmp, b_ordered );
178
179                 if( p_vchap )
180                     chapters.push_back( p_vchap );
181
182
183                 p_cur = p_next;
184             }
185             else /* segment not found */
186                 break;
187         }
188
189         /* Retime chapters */
190         retimeChapters();
191     }
192
193 #if MKV_DEBUG
194     msg_Dbg( &p_main_segment->sys.demuxer, "-- RECAP-BEGIN --" );
195     print();
196     msg_Dbg( &p_main_segment->sys.demuxer, "-- RECAP-END --" );
197 #endif
198 }
199
200 virtual_edition_c::~virtual_edition_c()
201 {
202     for( size_t i = 0; i < chapters.size(); i++ )
203         delete chapters[i];
204 }
205
206 void virtual_edition_c::retimeSubChapters( virtual_chapter_c * p_vchap )
207 {
208     int64_t stop_time = p_vchap->i_virtual_stop_time;
209     for( size_t i = p_vchap->sub_chapters.size(); i-- > 0; )
210     {
211         virtual_chapter_c * p_vsubchap = p_vchap->sub_chapters[i];
212         p_vsubchap->i_virtual_start_time += p_vchap->i_virtual_start_time;
213
214         /*FIXME we artificially extend stop time if they were there before...*/
215         /* Just for comfort*/
216         p_vsubchap->i_virtual_stop_time = stop_time;
217         stop_time = p_vsubchap->i_virtual_start_time;
218
219         retimeSubChapters( p_vsubchap );
220     }
221 }
222
223 void virtual_edition_c::retimeChapters()
224 {
225     /* This function is just meant to be used on unordered chapters */
226     if( b_ordered )
227         return;
228
229     i_duration = 0;
230
231     /* Sort by start time */
232     if( chapters.size() > 1 )
233         std::sort( chapters.begin(), chapters.end(), virtual_chapter_c::CompareTimecode );
234
235     /* On non ordered editions we have one top chapter == one segment */
236     for( size_t i = 0; i < chapters.size(); i++ )
237     {
238         virtual_chapter_c * p_vchap = chapters[i];
239
240         p_vchap->i_virtual_start_time = i_duration;
241         i_duration += p_vchap->p_segment->i_duration * 1000;
242         p_vchap->i_virtual_stop_time = i_duration;
243
244         retimeSubChapters( p_vchap );
245     }
246 }
247
248 virtual_segment_c::virtual_segment_c( std::vector<matroska_segment_c*> * p_opened_segments )
249 {
250     /* Main segment */
251     matroska_segment_c *p_segment = (*p_opened_segments)[0];
252     i_current_edition = 0;
253     i_sys_title = 0;
254     p_current_chapter = NULL;
255
256     for( size_t i = 0; i < p_segment->stored_editions.size(); i++ )
257     {
258         /* Get the default edition, if non use the first one */
259         if( p_segment->stored_editions[i]->b_default )
260             i_current_edition = i;
261
262         /* Create a virtual edition from opened */
263         virtual_edition_c * p_vedition = new virtual_edition_c( p_segment->stored_editions[i], p_opened_segments );
264
265         /*FIXME if p_vedition failed...*/
266
267         editions.push_back( p_vedition );
268     }
269     /*if we don't have edition create a dummy one*/
270     if( !p_segment->stored_editions.size() )
271     {
272         virtual_edition_c * p_vedition = new virtual_edition_c( NULL, p_opened_segments );
273         editions.push_back( p_vedition );
274     }
275
276     /* Set current chapter */
277     p_current_chapter = editions[i_current_edition]->getChapterbyTimecode(0);
278
279 }
280
281 virtual_segment_c::~virtual_segment_c()
282 {
283     for( size_t i = 0; i < editions.size(); i++ )
284         delete editions[i];
285 }
286
287 virtual_chapter_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
288                                     bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
289                                     const void *p_cookie,
290                                     size_t i_cookie_size )
291 {
292     virtual_edition_c * p_ved = CurrentEdition();
293     if( p_ved )
294         return p_ved->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
295
296     return NULL;
297 }
298
299
300 virtual_chapter_c * virtual_edition_c::BrowseCodecPrivate( unsigned int codec_id,
301                                     bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
302                                     const void *p_cookie,
303                                     size_t i_cookie_size )
304 {
305     if( !p_edition )
306         return NULL;
307
308     for( size_t i = 0; i < chapters.size(); i++ )
309     {
310         virtual_chapter_c * p_result = chapters[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
311         if( p_result )
312             return p_result;
313     }
314     return NULL;
315 }
316
317
318
319 virtual_chapter_c * virtual_chapter_c::BrowseCodecPrivate( unsigned int codec_id,
320                                     bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
321                                     const void *p_cookie,
322                                     size_t i_cookie_size )
323 {
324     if( !p_chapter )
325         return NULL;
326
327     if( p_chapter->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size ) )
328         return this;
329
330     for( size_t i = 0; i < sub_chapters.size(); i++ )
331     {
332         virtual_chapter_c * p_result = sub_chapters[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
333         if( p_result )
334             return p_result;
335     }
336     return NULL;
337 }
338
339 virtual_chapter_c* virtual_chapter_c::getSubChapterbyTimecode( int64_t time )
340 {
341     for( size_t i = 0; i < sub_chapters.size(); i++ )
342     {
343         if( time >= sub_chapters[i]->i_virtual_start_time && time < sub_chapters[i]->i_virtual_stop_time )
344             return sub_chapters[i]->getSubChapterbyTimecode( time );
345     }
346
347     return this;
348 }
349
350 virtual_chapter_c* virtual_edition_c::getChapterbyTimecode( int64_t time )
351 {
352     for( size_t i = 0; i < chapters.size(); i++ )
353     {
354         if( time >= chapters[i]->i_virtual_start_time &&
355             ( chapters[i]->i_virtual_stop_time < 0 || time < chapters[i]->i_virtual_stop_time ) )
356             /*with the current implementation only the last chapter can have a negative virtual_stop_time*/
357             return chapters[i]->getSubChapterbyTimecode( time );
358     }
359
360     return NULL;
361 }
362
363 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
364 {
365     demux_sys_t & sys = *demux.p_sys;
366     virtual_chapter_c *p_cur_chapter;
367     virtual_edition_c * p_cur_edition = editions[ i_current_edition ];
368
369     bool b_has_seeked = false;
370
371     p_cur_chapter = p_cur_edition->getChapterbyTimecode( sys.i_pts );
372
373     /* we have moved to a new chapter */
374     if ( p_cur_chapter != NULL && p_current_chapter != p_cur_chapter )
375         {
376             msg_Dbg( &demux, "NEW CHAPTER %"PRId64, sys.i_pts );
377             if ( p_cur_edition->b_ordered )
378             {
379                 /* FIXME EnterAndLeave has probably been broken for a long time */
380                 // Leave/Enter up to the link point
381                 b_has_seeked = p_cur_chapter->EnterAndLeave( p_current_chapter );
382                 if ( !b_has_seeked )
383                 {
384                     // only physically seek if necessary
385                     if ( p_current_chapter == NULL ||
386                         ( p_current_chapter->p_chapter->i_end_time != p_cur_chapter->p_chapter->i_start_time ) ||
387                         ( p_current_chapter && p_current_chapter->p_segment != p_cur_chapter->p_segment ) )
388                     {
389                         Seek( demux, p_cur_chapter->i_virtual_start_time, 0, p_cur_chapter, -1 );
390                         return true;
391                     }
392                 }
393             }
394
395             p_current_chapter = p_cur_chapter;
396             if ( p_cur_chapter->i_seekpoint_num > 0 )
397             {
398                 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
399                 demux.info.i_title = sys.i_current_title = i_sys_title;
400                 demux.info.i_seekpoint = p_cur_chapter->i_seekpoint_num - 1;
401             }
402
403             return b_has_seeked;
404         }
405         else if ( p_cur_chapter == NULL )
406         {
407             /* out of the scope of the data described by chapters, leave the edition */
408             if ( p_cur_edition->b_ordered && p_current_chapter != NULL )
409             {
410                 /* TODO */
411                 if ( !p_cur_edition->p_edition->EnterAndLeave( p_current_chapter->p_chapter, false ) )
412                     p_current_chapter = NULL;
413                 else
414                     return true;
415             }
416         }
417     return false;
418 }
419
420 bool virtual_chapter_c::EnterAndLeave( virtual_chapter_c *p_item, bool b_enter )
421 {
422     if( !p_chapter )
423         return false;
424
425     return p_chapter->EnterAndLeave( p_item->p_chapter, b_enter );
426 }
427
428 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, 
429                               virtual_chapter_c *p_chapter, int64_t i_global_position )
430 {
431     demux_sys_t *p_sys = demuxer.p_sys;
432
433
434     /* find the actual time for an ordered edition */
435     if ( p_chapter == NULL )
436         /* 1st, we need to know in which chapter we are */
437         p_chapter = editions[ i_current_edition ]->getChapterbyTimecode( i_date );
438
439     if ( p_chapter != NULL )
440     {
441         p_sys->i_chapter_time =
442             i_time_offset = p_chapter->i_virtual_start_time - ( ( p_chapter->p_chapter )? p_chapter->p_chapter->i_start_time : 0 );
443         if ( p_chapter->p_chapter && p_chapter->i_seekpoint_num > 0 )
444         {
445             demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
446             demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
447             demuxer.info.i_seekpoint = p_chapter->i_seekpoint_num - 1;
448         }
449
450         if( p_current_chapter->p_segment != p_chapter->p_segment )
451         {
452             p_chapter->p_segment->Select( i_date );
453             p_current_chapter->p_segment->UnSelect();
454         }
455         p_current_chapter = p_chapter;
456
457         p_chapter->p_segment->Seek( i_date, i_time_offset, i_global_position );
458     }
459 }
460
461 virtual_chapter_c * virtual_chapter_c::FindChapter( int64_t i_find_uid )
462 {
463     if( p_chapter && ( p_chapter->i_uid == i_find_uid ) )
464         return this;
465
466     for( size_t i = 0; i < sub_chapters.size(); i++ )
467     {
468         virtual_chapter_c * p_res = sub_chapters[i]->FindChapter( i_find_uid );
469         if( p_res )
470             return p_res;
471     }
472
473     return NULL;
474 }
475
476 virtual_chapter_c * virtual_segment_c::FindChapter( int64_t i_find_uid )
477 {
478     virtual_edition_c * p_edition = editions[i_current_edition];
479
480     for( size_t i = 0; p_edition->chapters.size(); i++ )
481     {
482         virtual_chapter_c * p_chapter = p_edition->chapters[i]->FindChapter( i_find_uid );
483         if( p_chapter )
484             return p_chapter;
485     }
486     return NULL;
487 }
488
489 int virtual_chapter_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
490 {
491     if ( p_chapter && ( !p_chapter->b_display_seekpoint || p_chapter->psz_name == "" ) )
492     {
493         p_chapter->psz_name = p_chapter->GetCodecName();
494         if ( p_chapter->psz_name != "" )
495             p_chapter->b_display_seekpoint = true;
496     }
497
498     if ( ( p_chapter && p_chapter->b_display_seekpoint &&
499          ( ( sub_chapters.size() > 0 && i_virtual_start_time != sub_chapters[0]->i_virtual_start_time) ||
500            sub_chapters.size() == 0 ) ) || !p_chapter )
501     {
502         seekpoint_t *sk = vlc_seekpoint_New();
503
504         sk->i_time_offset = i_virtual_start_time;
505         if( p_chapter )
506             sk->psz_name = strdup( p_chapter->psz_name.c_str() );
507         else
508             sk->psz_name = strdup("dummy chapter");
509
510         /* A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value. */
511         title.i_seekpoint++;
512         title.seekpoint = (seekpoint_t**)xrealloc( title.seekpoint,
513                                  title.i_seekpoint * sizeof( seekpoint_t* ) );
514         title.seekpoint[title.i_seekpoint-1] = sk;
515
516         if ( (p_chapter && p_chapter->b_user_display ) ||  !p_chapter )
517             i_user_chapters++;
518     }
519     i_seekpoint_num = i_user_chapters;
520
521     for( size_t i = 0; i < sub_chapters.size(); i++ )
522         sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level + 1 );
523
524     return i_user_chapters;
525 }
526
527
528 int virtual_edition_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
529 {
530
531     /* HACK for now don't expose edition as a seekpoint if its start time is the same than it's first chapter */
532     if( chapters.size() > 0 &&
533         chapters[0]->i_virtual_start_time && p_edition )
534     {
535         seekpoint_t *sk = vlc_seekpoint_New();
536
537         sk->i_time_offset = 0;
538         sk->psz_name = strdup( p_edition->psz_name.c_str() );
539
540         title.i_seekpoint++;
541         title.seekpoint = (seekpoint_t**)xrealloc( title.seekpoint,
542                              title.i_seekpoint * sizeof( seekpoint_t* ) );
543         title.seekpoint[title.i_seekpoint - 1] = sk;
544         i_level++;
545
546         i_user_chapters++;
547         i_seekpoint_num = i_user_chapters;
548     }
549
550 //    if( chapters.size() > 1 )
551         for( size_t i = 0; i < chapters.size(); i++ )
552             chapters[i]->PublishChapters( title, i_user_chapters, i_level );
553
554     return i_user_chapters;
555 }
556
557 std::string virtual_edition_c::GetMainName()
558 {
559     if( p_edition )
560         return p_edition->GetMainName();
561
562     return std::string("");
563 }
564
565 bool virtual_chapter_c::Enter( bool b_do_subs )
566 {
567     if( p_chapter )
568         return p_chapter->Enter( b_do_subs );
569     return false;
570 }
571
572 bool virtual_chapter_c::Leave( bool b_do_subs )
573 {
574     if( p_chapter )
575         return p_chapter->Leave( b_do_subs );
576     return false;
577 }
578
579 #if MKV_DEBUG
580 void virtual_chapter_c::print() 
581 {
582     msg_Dbg( &p_segment->sys.demuxer, "*** chapter %"PRId64" - %"PRId64" (%u)",
583              i_virtual_start_time, i_virtual_stop_time, sub_chapters.size() );
584     for( size_t i = 0; i < sub_chapters.size(); i++ )
585         sub_chapters[i]->print();
586 }
587 #endif