Commit 960da7cc authored by Steve Lhomme's avatar Steve Lhomme

mkv.cpp: fix various memory management problems

parent a718e0c6
...@@ -356,8 +356,8 @@ class dvd_command_interpretor_c ...@@ -356,8 +356,8 @@ class dvd_command_interpretor_c
public: public:
dvd_command_interpretor_c() dvd_command_interpretor_c()
{ {
memset( p_GPRM, 0, sizeof(p_GPRM) * 2 ); memset( p_GPRM, 0, sizeof(p_GPRM) );
memset( p_SPRM, 0, sizeof(p_SPRM) * 2 ); memset( p_SPRM, 0, sizeof(p_SPRM) );
p_SPRM[ 1 ] = 15; p_SPRM[ 1 ] = 15;
p_SPRM[ 2 ] = 62; p_SPRM[ 2 ] = 62;
p_SPRM[ 3 ] = 1; p_SPRM[ 3 ] = 1;
...@@ -445,6 +445,22 @@ public: ...@@ -445,6 +445,22 @@ public:
,psz_parent(NULL) ,psz_parent(NULL)
{} {}
~chapter_item_c()
{
std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
while ( index != codecs.end() )
{
delete (*index);
index++;
}
std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
while ( index_ != sub_chapters.end() )
{
delete (*index_);
index_++;
}
}
int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time ); int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
void PublishChapters( input_title_t & title, int i_level ); void PublishChapters( input_title_t & title, int i_level );
chapter_item_c * FindTimecode( mtime_t i_timecode ); chapter_item_c * FindTimecode( mtime_t i_timecode );
...@@ -453,14 +469,14 @@ public: ...@@ -453,14 +469,14 @@ public:
int64_t i_start_time, i_end_time; int64_t i_start_time, i_end_time;
int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */ int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
std::vector<chapter_item_c> sub_chapters; std::vector<chapter_item_c*> sub_chapters;
int i_seekpoint_num; int i_seekpoint_num;
int64_t i_uid; int64_t i_uid;
bool b_display_seekpoint; bool b_display_seekpoint;
std::string psz_name; std::string psz_name;
chapter_item_c *psz_parent; chapter_item_c *psz_parent;
std::vector<chapter_codec_cmds_c> codecs; std::vector<chapter_codec_cmds_c*> codecs;
bool operator<( const chapter_item_c & item ) const bool operator<( const chapter_item_c & item ) const
{ {
...@@ -562,6 +578,13 @@ public: ...@@ -562,6 +578,13 @@ public:
free( index ); free( index );
delete ep; delete ep;
std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
while ( index != stored_editions.end() )
{
delete (*index);
index++;
}
} }
KaxSegment *segment; KaxSegment *segment;
...@@ -600,7 +623,7 @@ public: ...@@ -600,7 +623,7 @@ public:
char *psz_title; char *psz_title;
char *psz_date_utc; char *psz_date_utc;
std::vector<chapter_edition_c> stored_editions; std::vector<chapter_edition_c*> stored_editions;
int i_default_edition; int i_default_edition;
std::vector<chapter_translation_c> translations; std::vector<chapter_translation_c> translations;
...@@ -636,6 +659,7 @@ class virtual_segment_c ...@@ -636,6 +659,7 @@ class virtual_segment_c
public: public:
virtual_segment_c( matroska_segment_c *p_segment ) virtual_segment_c( matroska_segment_c *p_segment )
:i_current_segment(0) :i_current_segment(0)
,p_editions(NULL)
,i_current_edition(-1) ,i_current_edition(-1)
,psz_current_chapter(NULL) ,psz_current_chapter(NULL)
{ {
...@@ -655,14 +679,14 @@ public: ...@@ -655,14 +679,14 @@ public:
inline chapter_edition_c *Edition() inline chapter_edition_c *Edition()
{ {
if ( i_current_edition >= 0 && size_t(i_current_edition) < editions.size() ) if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
return &editions[i_current_edition]; return (*p_editions)[i_current_edition];
return NULL; return NULL;
} }
inline bool EditionIsOrdered() const inline bool EditionIsOrdered() const
{ {
return (editions.size() != 0 && i_current_edition >= 0 && editions[i_current_edition].b_ordered); return (p_editions->size() != 0 && i_current_edition >= 0 && (*p_editions)[i_current_edition]->b_ordered);
} }
matroska_segment_c * Segment() const matroska_segment_c * Segment() const
...@@ -695,7 +719,7 @@ protected: ...@@ -695,7 +719,7 @@ protected:
std::vector<KaxSegmentUID> linked_uids; std::vector<KaxSegmentUID> linked_uids;
size_t i_current_segment; size_t i_current_segment;
std::vector<chapter_edition_c> editions; std::vector<chapter_edition_c*> *p_editions;
int i_current_edition; int i_current_edition;
chapter_item_c *psz_current_chapter; chapter_item_c *psz_current_chapter;
...@@ -1709,14 +1733,14 @@ bool virtual_segment_c::Select( input_title_t & title ) ...@@ -1709,14 +1733,14 @@ bool virtual_segment_c::Select( input_title_t & title )
// copy editions from the first segment // copy editions from the first segment
p_segment = linked_segments[0]; p_segment = linked_segments[0];
editions = p_segment->stored_editions; p_editions = &p_segment->stored_editions;
for ( i=1 ; i<linked_segments.size(); i++ ) for ( i=1 ; i<linked_segments.size(); i++ )
{ {
p_segment = linked_segments[i]; p_segment = linked_segments[i];
// FIXME assume we have the same editions in all segments // FIXME assume we have the same editions in all segments
for (j=0; j<p_segment->stored_editions.size(); j++) for (j=0; j<p_segment->stored_editions.size(); j++)
editions[j].Append( p_segment->stored_editions[j] ); (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
} }
if ( Edition() != NULL ) if ( Edition() != NULL )
...@@ -1753,7 +1777,7 @@ void chapter_item_c::PublishChapters( input_title_t & title, int i_level ) ...@@ -1753,7 +1777,7 @@ void chapter_item_c::PublishChapters( input_title_t & title, int i_level )
for ( size_t i=0; i<sub_chapters.size() ; i++) for ( size_t i=0; i<sub_chapters.size() ; i++)
{ {
sub_chapters[i].PublishChapters( title, i_level+1 ); sub_chapters[i]->PublishChapters( title, i_level+1 );
} }
} }
...@@ -1763,10 +1787,10 @@ void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux ) ...@@ -1763,10 +1787,10 @@ void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
chapter_item_c *psz_curr_chapter; chapter_item_c *psz_curr_chapter;
/* update current chapter/seekpoint */ /* update current chapter/seekpoint */
if ( editions.size() ) if ( p_editions->size() )
{ {
/* 1st, we need to know in which chapter we are */ /* 1st, we need to know in which chapter we are */
psz_curr_chapter = editions[i_current_edition].FindTimecode( sys.i_pts ); psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts );
/* we have moved to a new chapter */ /* we have moved to a new chapter */
if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter) if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
...@@ -1780,7 +1804,7 @@ void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux ) ...@@ -1780,7 +1804,7 @@ void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1; demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
} }
if ( editions[i_current_edition].b_ordered ) if ( (*p_editions)[i_current_edition]->b_ordered )
{ {
psz_curr_chapter->Enter(); psz_curr_chapter->Enter();
...@@ -1801,10 +1825,10 @@ void chapter_item_c::Append( const chapter_item_c & chapter ) ...@@ -1801,10 +1825,10 @@ void chapter_item_c::Append( const chapter_item_c & chapter )
for ( i=0; i<chapter.sub_chapters.size(); i++ ) for ( i=0; i<chapter.sub_chapters.size(); i++ )
{ {
p_chapter = FindChapter( chapter.sub_chapters[i] ); p_chapter = FindChapter( *chapter.sub_chapters[i] );
if ( p_chapter != NULL ) if ( p_chapter != NULL )
{ {
p_chapter->Append( chapter.sub_chapters[i] ); p_chapter->Append( *chapter.sub_chapters[i] );
} }
else else
{ {
...@@ -1821,8 +1845,8 @@ chapter_item_c * chapter_item_c::FindChapter( const chapter_item_c & chapter ) ...@@ -1821,8 +1845,8 @@ chapter_item_c * chapter_item_c::FindChapter( const chapter_item_c & chapter )
size_t i; size_t i;
for ( i=0; i<sub_chapters.size(); i++) for ( i=0; i<sub_chapters.size(); i++)
{ {
if ( sub_chapters[i].i_uid == chapter.i_uid ) if ( sub_chapters[i]->i_uid == chapter.i_uid )
return &sub_chapters[i]; return sub_chapters[i];
} }
return NULL; return NULL;
} }
...@@ -3232,6 +3256,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap ...@@ -3232,6 +3256,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
} }
if ( p_ccodec != NULL ) if ( p_ccodec != NULL )
{
for( j = 0; j < cp->ListSize(); j++ ) for( j = 0; j < cp->ListSize(); j++ )
{ {
EbmlElement *k= (*cp)[j]; EbmlElement *k= (*cp)[j];
...@@ -3246,13 +3271,14 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap ...@@ -3246,13 +3271,14 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) ); p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
} }
} }
chapters.codecs.push_back( *p_ccodec ); chapters.codecs.push_back( p_ccodec );
}
} }
else if( MKV_IS_ID( l, KaxChapterAtom ) ) else if( MKV_IS_ID( l, KaxChapterAtom ) )
{ {
chapter_item_c new_sub_chapter; chapter_item_c *new_sub_chapter = new chapter_item_c();
ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), new_sub_chapter ); ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
new_sub_chapter.psz_parent = &chapters; new_sub_chapter->psz_parent = &chapters;
chapters.sub_chapters.push_back( new_sub_chapter ); chapters.sub_chapters.push_back( new_sub_chapter );
} }
} }
...@@ -3277,7 +3303,7 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters ) ...@@ -3277,7 +3303,7 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
if( MKV_IS_ID( l, KaxEditionEntry ) ) if( MKV_IS_ID( l, KaxEditionEntry ) )
{ {
chapter_edition_c edition; chapter_edition_c *p_edition = new chapter_edition_c();
EbmlMaster *E = static_cast<EbmlMaster *>(l ); EbmlMaster *E = static_cast<EbmlMaster *>(l );
size_t j; size_t j;
...@@ -3288,17 +3314,17 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters ) ...@@ -3288,17 +3314,17 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
if( MKV_IS_ID( l, KaxChapterAtom ) ) if( MKV_IS_ID( l, KaxChapterAtom ) )
{ {
chapter_item_c new_sub_chapter; chapter_item_c *new_sub_chapter = new chapter_item_c();
ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), new_sub_chapter ); ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
edition.sub_chapters.push_back( new_sub_chapter ); p_edition->sub_chapters.push_back( new_sub_chapter );
} }
else if( MKV_IS_ID( l, KaxEditionUID ) ) else if( MKV_IS_ID( l, KaxEditionUID ) )
{ {
edition.i_uid = uint64(*static_cast<KaxEditionUID *>( l )); p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
} }
else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) ) else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
{ {
edition.b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0; p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
} }
else if( MKV_IS_ID( l, KaxEditionFlagDefault ) ) else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
{ {
...@@ -3310,7 +3336,7 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters ) ...@@ -3310,7 +3336,7 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() ); msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
} }
} }
stored_editions.push_back( edition ); stored_editions.push_back( p_edition );
} }
else else
{ {
...@@ -3320,13 +3346,13 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters ) ...@@ -3320,13 +3346,13 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
for( i = 0; i < stored_editions.size(); i++ ) for( i = 0; i < stored_editions.size(); i++ )
{ {
stored_editions[i].RefreshChapters( ); stored_editions[i]->RefreshChapters( );
} }
if ( stored_editions[i_default_edition].b_ordered ) if ( stored_editions[i_default_edition]->b_ordered )
{ {
/* update the duration of the segment according to the sum of all sub chapters */ /* update the duration of the segment according to the sum of all sub chapters */
i_dur = stored_editions[i_default_edition].Duration() / I64C(1000); i_dur = stored_editions[i_default_edition]->Duration() / I64C(1000);
if (i_dur > 0) if (i_dur > 0)
i_duration = i_dur; i_duration = i_dur;
} }
...@@ -3494,10 +3520,10 @@ int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_tim ...@@ -3494,10 +3520,10 @@ int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_tim
int64_t i_user_time = i_prev_user_time; int64_t i_user_time = i_prev_user_time;
// first the sub-chapters, and then ourself // first the sub-chapters, and then ourself
std::vector<chapter_item_c>::iterator index = sub_chapters.begin(); std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
while ( index != sub_chapters.end() ) while ( index != sub_chapters.end() )
{ {
i_user_time = (*index).RefreshChapters( b_ordered, i_user_time ); i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
index++; index++;
} }
...@@ -3542,9 +3568,9 @@ mtime_t chapter_edition_c::Duration() const ...@@ -3542,9 +3568,9 @@ mtime_t chapter_edition_c::Duration() const
if ( sub_chapters.size() ) if ( sub_chapters.size() )
{ {
std::vector<chapter_item_c>::const_iterator index = sub_chapters.end(); std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
index--; index--;
i_result = (*index).i_user_end_time; i_result = (*index)->i_user_end_time;
} }
return i_result; return i_result;
...@@ -3558,10 +3584,10 @@ chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode ) ...@@ -3558,10 +3584,10 @@ chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode )
( i_user_timecode < i_user_end_time || ( i_user_timecode < i_user_end_time ||
( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time ))) ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
{ {
std::vector<chapter_item_c>::iterator index = sub_chapters.begin(); std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
while ( index != sub_chapters.end() && psz_result == NULL ) while ( index != sub_chapters.end() && psz_result == NULL )
{ {
psz_result = (*index).FindTimecode( i_user_timecode ); psz_result = (*index)->FindTimecode( i_user_timecode );
index++; index++;
} }
...@@ -3920,7 +3946,7 @@ void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_ ...@@ -3920,7 +3946,7 @@ void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_
if ( EditionIsOrdered() ) if ( EditionIsOrdered() )
{ {
/* 1st, we need to know in which chapter we are */ /* 1st, we need to know in which chapter we are */
psz_chapter = editions[i_current_edition].FindTimecode( i_date ); psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date );
} }
} }
...@@ -3995,10 +4021,10 @@ void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command ...@@ -3995,10 +4021,10 @@ void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command
bool chapter_item_c::Enter() bool chapter_item_c::Enter()
{ {
std::vector<chapter_codec_cmds_c>::iterator index = codecs.begin(); std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
while ( index != codecs.end() ) while ( index != codecs.end() )
{ {
(*index).Enter(); (*index)->Enter();
index++; index++;
} }
return true; return true;
...@@ -4006,10 +4032,10 @@ bool chapter_item_c::Enter() ...@@ -4006,10 +4032,10 @@ bool chapter_item_c::Enter()
bool chapter_item_c::Leave() bool chapter_item_c::Leave()
{ {
std::vector<chapter_codec_cmds_c>::iterator index = codecs.begin(); std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
while ( index != codecs.end() ) while ( index != codecs.end() )
{ {
(*index).Leave(); (*index)->Leave();
index++; index++;
} }
return true; return true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment