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**)xrealloc( title.seekpoint,
66 title.i_seekpoint * sizeof( seekpoint_t* ) );
67 title.seekpoint[title.i_seekpoint-1] = sk;
73 for ( size_t i=0; i<sub_chapters.size() ; i++)
75 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
78 i_seekpoint_num = i_user_chapters;
80 return i_user_chapters;
83 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
84 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
86 size_t i_cookie_size )
89 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
90 while ( index != codecs.end() )
92 if ( match( **index ,p_cookie, i_cookie_size ) )
98 chapter_item_c *p_result = NULL;
99 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
100 while ( index2 != sub_chapters.end() )
102 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
103 if ( p_result != NULL )
111 void chapter_item_c::Append( const chapter_item_c & chapter )
113 // we are appending content for the same chapter UID
115 chapter_item_c *p_chapter;
117 for ( i=0; i<chapter.sub_chapters.size(); i++ )
119 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
120 if ( p_chapter != NULL )
122 p_chapter->Append( *chapter.sub_chapters[i] );
126 sub_chapters.push_back( chapter.sub_chapters[i] );
130 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
131 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
134 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
137 chapter_item_c *p_result = NULL;
139 if ( i_uid == i_find_uid )
142 for ( i=0; i<sub_chapters.size(); i++)
144 p_result = sub_chapters[i]->FindChapter( i_find_uid );
145 if ( p_result != NULL )
151 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
155 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
156 while ( index != codecs.end() )
158 result = (*index)->GetCodecName( f_for_title );
167 int16 chapter_item_c::GetTitleNumber( ) const
171 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
172 while ( index != codecs.end() )
174 result = (*index)->GetTitleNumber( );
183 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
185 int64_t i_user_time = i_prev_user_time;
187 // first the sub-chapters, and then ourself
188 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
189 while ( index != sub_chapters.end() )
191 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
197 // the ordered chapters always start at zero
198 if ( i_prev_user_time == -1 )
200 if ( i_user_time == -1 )
202 i_prev_user_time = 0;
205 i_user_start_time = i_prev_user_time;
206 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
208 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
212 i_user_end_time = i_user_time;
217 if ( sub_chapters.begin() != sub_chapters.end() )
218 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
219 i_user_start_time = i_start_time;
220 if ( i_end_time != -1 )
221 i_user_end_time = i_end_time;
222 else if ( i_user_time != -1 )
223 i_user_end_time = i_user_time;
225 i_user_end_time = i_user_start_time;
228 return i_user_end_time;
231 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
233 chapter_item_c *psz_result = NULL;
235 if ( p_current == this )
238 if ( i_user_timecode >= i_user_start_time &&
239 ( i_user_timecode < i_user_end_time ||
240 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
242 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
243 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
245 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
249 if ( psz_result == NULL )
256 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
261 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
262 while ( index != sub_chapters.end() )
264 if ( (*index)->ParentOf( item ) )
272 bool chapter_item_c::Enter( bool b_do_subs )
274 bool f_result = false;
275 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
276 while ( index != codecs.end() )
278 f_result |= (*index)->Enter();
285 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
286 while ( index_ != sub_chapters.end() )
288 f_result |= (*index_)->Enter( true );
295 bool chapter_item_c::Leave( bool b_do_subs )
297 bool f_result = false;
299 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
300 while ( index != codecs.end() )
302 f_result |= (*index)->Leave();
309 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
310 while ( index_ != sub_chapters.end() )
312 f_result |= (*index_)->Leave( true );
316 b_is_leaving = false;
320 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
322 chapter_item_c *p_common_parent = p_item;
324 // leave, up to a common parent
325 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
327 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
329 p_common_parent = p_common_parent->psz_parent;
332 // enter from the parent to <this>
333 if ( p_common_parent != NULL )
337 if ( p_common_parent == this )
338 return Enter( true );
340 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
342 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
344 p_common_parent = p_common_parent->sub_chapters[i];
345 if ( p_common_parent != this )
346 if ( p_common_parent->Enter( false ) )
356 return Enter( true );
363 /* Chapter Edition Class */
364 std::string chapter_edition_c::GetMainName() const
366 if ( sub_chapters.size() )
368 return sub_chapters[0]->GetCodecName( true );
373 void chapter_edition_c::RefreshChapters( )
375 chapter_item_c::RefreshChapters( b_ordered, -1 );
376 b_display_seekpoint = false;
379 mtime_t chapter_edition_c::Duration() const
381 mtime_t i_result = 0;
383 if ( sub_chapters.size() )
385 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
387 i_result = (*index)->i_user_end_time;
393 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
397 bool b_found_current = false;
398 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );