1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "chapters.hpp"
27 #include "chapter_command.hpp"
29 chapter_item_c::~chapter_item_c()
31 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
32 while ( index != codecs.end() )
37 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
38 while ( index_ != sub_chapters.end() )
45 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
47 // add support for meta-elements from codec like DVD Titles
48 if ( !b_display_seekpoint || psz_name == "" )
50 psz_name = GetCodecName();
52 b_display_seekpoint = true;
55 if (b_display_seekpoint)
57 seekpoint_t *sk = vlc_seekpoint_New();
59 sk->i_level = i_level;
60 sk->i_time_offset = i_start_time;
61 sk->psz_name = strdup( psz_name.c_str() );
63 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
65 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
66 title.seekpoint[title.i_seekpoint-1] = sk;
72 for ( size_t i=0; i<sub_chapters.size() ; i++)
74 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
77 i_seekpoint_num = i_user_chapters;
79 return i_user_chapters;
82 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
83 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
85 size_t i_cookie_size )
88 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
89 while ( index != codecs.end() )
91 if ( match( **index ,p_cookie, i_cookie_size ) )
97 chapter_item_c *p_result = NULL;
98 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
99 while ( index2 != sub_chapters.end() )
101 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
102 if ( p_result != NULL )
110 void chapter_item_c::Append( const chapter_item_c & chapter )
112 // we are appending content for the same chapter UID
114 chapter_item_c *p_chapter;
116 for ( i=0; i<chapter.sub_chapters.size(); i++ )
118 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
119 if ( p_chapter != NULL )
121 p_chapter->Append( *chapter.sub_chapters[i] );
125 sub_chapters.push_back( chapter.sub_chapters[i] );
129 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
130 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
133 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
136 chapter_item_c *p_result = NULL;
138 if ( i_uid == i_find_uid )
141 for ( i=0; i<sub_chapters.size(); i++)
143 p_result = sub_chapters[i]->FindChapter( i_find_uid );
144 if ( p_result != NULL )
150 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
154 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
155 while ( index != codecs.end() )
157 result = (*index)->GetCodecName( f_for_title );
166 int16 chapter_item_c::GetTitleNumber( ) const
170 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
171 while ( index != codecs.end() )
173 result = (*index)->GetTitleNumber( );
182 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
184 int64_t i_user_time = i_prev_user_time;
186 // first the sub-chapters, and then ourself
187 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
188 while ( index != sub_chapters.end() )
190 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
196 // the ordered chapters always start at zero
197 if ( i_prev_user_time == -1 )
199 if ( i_user_time == -1 )
201 i_prev_user_time = 0;
204 i_user_start_time = i_prev_user_time;
205 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
207 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
211 i_user_end_time = i_user_time;
216 if ( sub_chapters.begin() != sub_chapters.end() )
217 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
218 i_user_start_time = i_start_time;
219 if ( i_end_time != -1 )
220 i_user_end_time = i_end_time;
221 else if ( i_user_time != -1 )
222 i_user_end_time = i_user_time;
224 i_user_end_time = i_user_start_time;
227 return i_user_end_time;
230 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
232 chapter_item_c *psz_result = NULL;
234 if ( p_current == this )
237 if ( i_user_timecode >= i_user_start_time &&
238 ( i_user_timecode < i_user_end_time ||
239 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
241 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
242 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
244 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
248 if ( psz_result == NULL )
255 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
260 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
261 while ( index != sub_chapters.end() )
263 if ( (*index)->ParentOf( item ) )
271 bool chapter_item_c::Enter( bool b_do_subs )
273 bool f_result = false;
274 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
275 while ( index != codecs.end() )
277 f_result |= (*index)->Enter();
284 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
285 while ( index_ != sub_chapters.end() )
287 f_result |= (*index_)->Enter( true );
294 bool chapter_item_c::Leave( bool b_do_subs )
296 bool f_result = false;
298 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
299 while ( index != codecs.end() )
301 f_result |= (*index)->Leave();
308 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
309 while ( index_ != sub_chapters.end() )
311 f_result |= (*index_)->Leave( true );
315 b_is_leaving = false;
319 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
321 chapter_item_c *p_common_parent = p_item;
323 // leave, up to a common parent
324 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
326 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
328 p_common_parent = p_common_parent->psz_parent;
331 // enter from the parent to <this>
332 if ( p_common_parent != NULL )
336 if ( p_common_parent == this )
337 return Enter( true );
339 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
341 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
343 p_common_parent = p_common_parent->sub_chapters[i];
344 if ( p_common_parent != this )
345 if ( p_common_parent->Enter( false ) )
355 return Enter( true );
362 /* Chapter Edition Class */
363 std::string chapter_edition_c::GetMainName() const
365 if ( sub_chapters.size() )
367 return sub_chapters[0]->GetCodecName( true );
372 void chapter_edition_c::RefreshChapters( )
374 chapter_item_c::RefreshChapters( b_ordered, -1 );
375 b_display_seekpoint = false;
378 mtime_t chapter_edition_c::Duration() const
380 mtime_t i_result = 0;
382 if ( sub_chapters.size() )
384 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
386 i_result = (*index)->i_user_end_time;
392 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
396 bool b_found_current = false;
397 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );