2 /*****************************************************************************
3 * EbmlParser for the matroska demuxer
4 *****************************************************************************
5 * Copyright (C) 2003-2004 VLC authors and VideoLAN
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * Steve Lhomme <steve.lhomme@free.fr>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #include "Ebml_parser.hpp"
27 #include "stream_io_callback.hpp"
29 /*****************************************************************************
31 *****************************************************************************/
32 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux ) :
40 mi_remain_size[0] = el_start->GetSize();
41 memset( m_el, 0, 6 * sizeof( *m_el ) );
43 mb_dummy = var_InheritBool( p_demux, "mkv-use-dummy" );
46 EbmlParser::~EbmlParser( void )
55 for( int i = 1; i <= mi_level; i++ )
65 EbmlElement* EbmlParser::UnGet( uint64 i_block_pos, uint64 i_cluster_pos )
67 if ( mi_user_level > mi_level )
69 while ( mi_user_level != mi_level )
71 delete m_el[mi_user_level];
72 m_el[mi_user_level] = NULL;
77 /* Avoid data skip in BlockGet */
78 delete m_el[mi_level];
79 m_el[mi_level] = NULL;
83 if ( m_el[1] && m_el[1]->GetElementPosition() == i_cluster_pos )
85 m_es->I_O().setFilePointer( i_block_pos, seek_beginning );
90 // seek to the previous Cluster
91 m_es->I_O().setFilePointer( i_cluster_pos, seek_beginning );
96 delete m_el[mi_level];
97 m_el[mi_level] = NULL;
103 void EbmlParser::Up( void )
105 if( mi_user_level == mi_level )
107 msg_Warn( p_demux, "MKV/Ebml Parser: Up cannot escape itself" );
113 void EbmlParser::Down( void )
119 void EbmlParser::Keep( void )
124 int EbmlParser::GetLevel( void ) const
126 return mi_user_level;
129 void EbmlParser::Reset( demux_t *p_demux )
131 while ( mi_level > 0)
133 delete m_el[mi_level];
134 m_el[mi_level] = NULL;
137 this->p_demux = p_demux;
138 mi_user_level = mi_level = 1;
139 // a little faster and cleaner
140 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
141 mb_dummy = var_InheritBool( p_demux, "mkv-use-dummy" );
144 EbmlElement *EbmlParser::Get( int n_call )
147 EbmlElement *p_prev = NULL;
149 if( mi_user_level != mi_level )
155 EbmlElement *ret = m_got;
161 p_prev = m_el[mi_level];
164 m_el[mi_level]->SkipData( *m_es, EBML_CONTEXT(m_el[mi_level]) );
167 vlc_stream_io_callback & io_stream = (vlc_stream_io_callback &) m_es->I_O();
168 uint64 i_size = io_stream.toRead();
169 m_el[mi_level] = m_es->FindNextElement( EBML_CONTEXT(m_el[mi_level - 1]),
170 i_ulev, i_size, true, 1 );
171 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
178 if( MKV_IS_ID( p_prev, KaxBlockVirtual ) )
179 static_cast<KaxBlockVirtualWorkaround*>(p_prev)->Fix();
192 delete m_el[mi_level - 1];
193 m_got = m_el[mi_level -1] = m_el[mi_level];
194 m_el[mi_level] = NULL;
201 else if( m_el[mi_level] == NULL )
203 msg_Warn( p_demux,"MKV/Ebml Parser: m_el[mi_level] == NULL\n" );
205 else if( m_el[mi_level]->IsDummy() && !mb_dummy )
207 bool b_bad_position = false;
208 /* We got a dummy element but don't want those...
209 * perform a sanity check */
212 msg_Err(p_demux, "Got invalid lvl 0 element... Aborting");
216 if( p_prev && p_prev->IsFiniteSize() &&
217 p_prev->GetEndPosition() != m_el[mi_level]->GetElementPosition())
219 msg_Err( p_demux, "Dummy Element at unexpected position... corrupted file?" );
220 b_bad_position = true;
223 if( n_call < 10 && !b_bad_position && m_el[mi_level]->IsFiniteSize() &&
224 ( !m_el[mi_level-1]->IsFiniteSize() ||
225 m_el[mi_level]->GetEndPosition() <= m_el[mi_level-1]->GetEndPosition() ) )
227 /* The element fits inside its upper element */
228 msg_Warn( p_demux, "Dummy element found... skipping it" );
229 return Get( ++n_call );
233 /* Too large, misplaced or 10 successive dummy elements */
234 msg_Err( p_demux, "Dummy element too large or misplaced... skipping to next upper element" );
235 delete m_el[mi_level];
236 m_el[mi_level] = NULL;
237 m_el[mi_level - 1]->SkipData( *m_es, EBML_CONTEXT(m_el[mi_level - 1]) );
246 if( MKV_IS_ID( p_prev, KaxBlockVirtual ) )
247 static_cast<KaxBlockVirtualWorkaround*>(p_prev)->Fix();
252 return m_el[mi_level];
255 bool EbmlParser::IsTopPresent( EbmlElement *el ) const
257 for( int i = 0; i < mi_level; i++ )
259 if( m_el[i] && m_el[i] == el )