Coverage Report

Created: 2026-12-01 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/mkv/matroska_segment_seeker.cpp
Line
Count
Source
1
/*****************************************************************************
2
 * matroska_segment.hpp : matroska demuxer
3
 *****************************************************************************
4
 * Copyright (C) 2016 VLC authors and VideoLAN
5
 *
6
 * Authors: Filip Roséen <filip@videolabs.io>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
#include "matroska_segment_seeker.hpp"
24
#include "matroska_segment.hpp"
25
#include "demux.hpp"
26
#include "Ebml_parser.hpp"
27
#include "Ebml_dispatcher.hpp"
28
#include "util.hpp"
29
#include "stream_io_callback.hpp"
30
31
#include <sstream>
32
#include <limits>
33
34
namespace {
35
    template<class It, class T>
36
    It greatest_lower_bound( It beg, It end, T const& value )
37
36.0k
    {
38
36.0k
        It it = std::upper_bound( beg, end, value );
39
36.0k
        if( it != beg ) --it;
40
36.0k
        return it;
41
36.0k
    }
matroska_segment_seeker.cpp:std::__1::__wrap_iter<mkv::SegmentSeeker::Seekpoint const*> (anonymous namespace)::greatest_lower_bound<std::__1::__wrap_iter<mkv::SegmentSeeker::Seekpoint const*>, mkv::SegmentSeeker::Seekpoint>(std::__1::__wrap_iter<mkv::SegmentSeeker::Seekpoint const*>, std::__1::__wrap_iter<mkv::SegmentSeeker::Seekpoint const*>, mkv::SegmentSeeker::Seekpoint const&)
Line
Count
Source
37
17.5k
    {
38
17.5k
        It it = std::upper_bound( beg, end, value );
39
17.5k
        if( it != beg ) --it;
40
17.5k
        return it;
41
17.5k
    }
matroska_segment_seeker.cpp:std::__1::__wrap_iter<mkv::SegmentSeeker::Range const*> (anonymous namespace)::greatest_lower_bound<std::__1::__wrap_iter<mkv::SegmentSeeker::Range const*>, mkv::SegmentSeeker::Range>(std::__1::__wrap_iter<mkv::SegmentSeeker::Range const*>, std::__1::__wrap_iter<mkv::SegmentSeeker::Range const*>, mkv::SegmentSeeker::Range const&)
Line
Count
Source
37
6.48k
    {
38
6.48k
        It it = std::upper_bound( beg, end, value );
39
6.48k
        if( it != beg ) --it;
40
6.48k
        return it;
41
6.48k
    }
matroska_segment_seeker.cpp:std::__1::__wrap_iter<unsigned long*> (anonymous namespace)::greatest_lower_bound<std::__1::__wrap_iter<unsigned long*>, unsigned long>(std::__1::__wrap_iter<unsigned long*>, std::__1::__wrap_iter<unsigned long*>, unsigned long const&)
Line
Count
Source
37
11.9k
    {
38
11.9k
        It it = std::upper_bound( beg, end, value );
39
11.9k
        if( it != beg ) --it;
40
11.9k
        return it;
41
11.9k
    }
42
43
    // std::prev and std::next exists in C++11, in order to avoid ambiguity due
44
    // to ADL and iterators being defined within namespace std, these two
45
    // function-names have been postfixed with an underscore.
46
47
29.9k
    template<class It> It prev_( It it ) { return --it; }
48
124k
    template<class It> It next_( It it ) { return ++it; }
matroska_segment_seeker.cpp:std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::__value_type<long, mkv::SegmentSeeker::Cluster>, std::__1::__tree_node<std::__1::__value_type<long, mkv::SegmentSeeker::Cluster>, void*>*, long> > (anonymous namespace)::next_<std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::__value_type<long, mkv::SegmentSeeker::Cluster>, std::__1::__tree_node<std::__1::__value_type<long, mkv::SegmentSeeker::Cluster>, void*>*, long> > >(std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::__value_type<long, mkv::SegmentSeeker::Cluster>, std::__1::__tree_node<std::__1::__value_type<long, mkv::SegmentSeeker::Cluster>, void*>*, long> >)
Line
Count
Source
48
117k
    template<class It> It next_( It it ) { return ++it; }
matroska_segment_seeker.cpp:std::__1::__wrap_iter<mkv::SegmentSeeker::Seekpoint const*> (anonymous namespace)::next_<std::__1::__wrap_iter<mkv::SegmentSeeker::Seekpoint const*> >(std::__1::__wrap_iter<mkv::SegmentSeeker::Seekpoint const*>)
Line
Count
Source
48
6.92k
    template<class It> It next_( It it ) { return ++it; }
49
}
50
51
namespace mkv {
52
53
SegmentSeeker::cluster_positions_t::iterator
54
SegmentSeeker::add_cluster_position( fptr_t fpos )
55
123k
{
56
123k
    cluster_positions_t::iterator insertion_point = std::upper_bound(
57
123k
      _cluster_positions.begin(),
58
123k
      _cluster_positions.end(),
59
123k
      fpos
60
123k
    );
61
62
123k
    return _cluster_positions.insert( insertion_point, fpos );
63
123k
}
64
65
SegmentSeeker::cluster_map_t::iterator
66
SegmentSeeker::add_cluster( KaxCluster * const p_cluster )
67
88.9k
{
68
88.9k
    Cluster cinfo = {
69
88.9k
        /* fpos     */ p_cluster->GetElementPosition(),
70
88.9k
        /* pts      */ vlc_tick_t( VLC_TICK_FROM_NS( p_cluster->GlobalTimestamp() ) ),
71
88.9k
        /* duration */ vlc_tick_t( -1 ),
72
88.9k
        /* size     */ p_cluster->IsFiniteSize()
73
88.9k
            ? p_cluster->GetEndPosition() - p_cluster->GetElementPosition()
74
88.9k
            : UINT64_MAX
75
88.9k
    };
76
77
88.9k
    add_cluster_position( cinfo.fpos );
78
79
88.9k
    cluster_map_t::iterator it = _clusters.lower_bound( cinfo.pts );
80
81
88.9k
    if( it != _clusters.end() && it->second.pts == cinfo.pts )
82
78.5k
    {
83
        // cluster already known
84
78.5k
    }
85
10.3k
    else
86
10.3k
    {
87
10.3k
        it = _clusters.insert( cluster_map_t::value_type( cinfo.pts, cinfo ) ).first;
88
10.3k
    }
89
90
    // ------------------------------------------------------------------
91
    // IF we have two adjecent clusters, update duration where applicable
92
    // ------------------------------------------------------------------
93
94
88.9k
    struct Duration {
95
88.9k
        static void fix( Cluster& prev, Cluster& next )
96
88.9k
        {
97
58.1k
            if( ( prev.fpos + prev.size) == next.fpos )
98
2.04k
                prev.duration = next.pts - prev.pts;
99
58.1k
        }
100
88.9k
    };
101
102
88.9k
    if( it != _clusters.begin() )
103
29.9k
    {
104
29.9k
        Duration::fix( prev_( it )->second, it->second );
105
29.9k
    }
106
107
88.9k
    if( it != _clusters.end() && next_( it ) != _clusters.end() )
108
28.1k
    {
109
28.1k
        Duration::fix( it->second, next_( it )->second );
110
28.1k
    }
111
112
88.9k
    return it;
113
88.9k
}
114
115
void
116
SegmentSeeker::add_seekpoint( track_id_t track_id, Seekpoint sp )
117
77.6k
{
118
77.6k
    seekpoints_t&  seekpoints = _tracks_seekpoints[ track_id ];
119
77.6k
    seekpoints_t::iterator it = std::lower_bound( seekpoints.begin(), seekpoints.end(), sp );
120
121
77.6k
    if( it != seekpoints.end() && it->fpos == sp.fpos )
122
6.01k
    {
123
6.01k
        if (sp.trust_level <= it->trust_level)
124
5.98k
            return;
125
126
35
        *it = sp;
127
35
    }
128
71.6k
    else if( it != seekpoints.end() && it->pts == sp.pts )
129
36.1k
    {
130
36.1k
        if (sp.trust_level <= it->trust_level)
131
35.6k
            return;
132
133
574
        *it = sp;
134
574
    }
135
35.4k
    else
136
35.4k
    {
137
35.4k
        seekpoints.insert( it, sp );
138
35.4k
    }
139
77.6k
}
140
141
SegmentSeeker::tracks_seekpoint_t
142
SegmentSeeker::find_greatest_seekpoints_in_range( fptr_t start_fpos, vlc_tick_t end_pts, track_ids_t const& filter_tracks )
143
6.78k
{
144
6.78k
    tracks_seekpoint_t tpoints;
145
146
17.6k
    for( tracks_seekpoints_t::const_iterator it = _tracks_seekpoints.begin(); it != _tracks_seekpoints.end(); ++it )
147
10.8k
    {
148
10.8k
        if ( std::find( filter_tracks.begin(), filter_tracks.end(), it->first ) == filter_tracks.end() )
149
570
            continue;
150
151
10.3k
        Seekpoint sp = get_first_seekpoint_around( end_pts, it->second );
152
153
10.3k
        if( sp.fpos < start_fpos )
154
376
            continue;
155
156
9.93k
        if( sp.pts > end_pts )
157
14
            continue;
158
159
9.92k
        tpoints.insert( tracks_seekpoint_t::value_type( it->first, sp ) );
160
9.92k
    }
161
162
6.78k
    if (tpoints.empty())
163
284
    {
164
        // try a further pts
165
716
        for( tracks_seekpoints_t::const_iterator it = _tracks_seekpoints.begin(); it != _tracks_seekpoints.end(); ++it )
166
432
        {
167
432
            if ( std::find( filter_tracks.begin(), filter_tracks.end(), it->first ) == filter_tracks.end() )
168
71
                continue;
169
170
361
            Seekpoint sp = get_first_seekpoint_around( end_pts, it->second );
171
172
361
            if( sp.fpos < start_fpos )
173
353
                continue;
174
175
8
            tpoints.insert( tracks_seekpoint_t::value_type( it->first, sp ) );
176
8
        }
177
284
    }
178
179
6.78k
    return tpoints;
180
6.78k
}
181
182
SegmentSeeker::Seekpoint
183
SegmentSeeker::get_first_seekpoint_around( vlc_tick_t pts, seekpoints_t const& seekpoints,
184
                                           Seekpoint::TrustLevel trust_level )
185
10.6k
{
186
10.6k
    if( seekpoints.empty() )
187
24
    {
188
24
        return Seekpoint();
189
24
    }
190
191
10.6k
    typedef seekpoints_t::const_iterator iterator;
192
193
10.6k
    Seekpoint const needle ( std::numeric_limits<fptr_t>::max(), pts );
194
195
10.6k
    iterator const it_begin  = seekpoints.begin();
196
10.6k
    iterator const it_end    = seekpoints.end();
197
10.6k
    iterator const it_middle = greatest_lower_bound( it_begin, it_end, needle );
198
199
10.6k
    iterator it_before;
200
201
    // rewrind to _previous_ seekpoint with appropriate trust
202
13.6k
    for( it_before = it_middle; it_before != it_begin; --it_before )
203
8.15k
    {
204
8.15k
        if( it_before->trust_level >= trust_level )
205
5.17k
            return *it_before;
206
8.15k
    }
207
5.47k
    return *it_begin;
208
10.6k
}
209
210
SegmentSeeker::seekpoint_pair_t
211
SegmentSeeker::get_seekpoints_around( vlc_tick_t pts, seekpoints_t const& seekpoints )
212
6.97k
{
213
6.97k
    if( seekpoints.empty() )
214
32
    {
215
32
        return seekpoint_pair_t();
216
32
    }
217
218
6.94k
    typedef seekpoints_t::const_iterator iterator;
219
220
6.94k
    Seekpoint const needle ( std::numeric_limits<fptr_t>::max(), pts );
221
222
6.94k
    iterator const it_begin  = seekpoints.begin();
223
6.94k
    iterator const it_end    = seekpoints.end();
224
6.94k
    iterator const it_middle = greatest_lower_bound( it_begin, it_end, needle );
225
226
6.94k
    if ( it_middle != it_end && (*it_middle).pts > pts)
227
        // found nothing low enough, use the first one
228
17
        return seekpoint_pair_t( *it_begin, Seekpoint() );
229
230
6.92k
    iterator it_before = it_middle;
231
6.92k
    iterator it_after = it_middle == it_end ? it_middle : next_( it_middle ) ;
232
233
6.92k
    return seekpoint_pair_t( *it_before,
234
6.92k
      it_after == it_end ? Seekpoint() : *it_after
235
6.92k
    );
236
6.94k
}
237
238
SegmentSeeker::seekpoint_pair_t
239
SegmentSeeker::get_seekpoints_around( vlc_tick_t target_pts, track_ids_t const& priority_tracks )
240
7.03k
{
241
7.03k
    seekpoint_pair_t points;
242
243
7.03k
    if( _tracks_seekpoints.empty() )
244
221
        return points;
245
246
6.81k
    { // locate the max/min seekpoints for priority_tracks //
247
248
6.81k
        typedef track_ids_t::const_iterator track_iterator;
249
250
6.81k
        track_iterator const begin = priority_tracks.begin();
251
6.81k
        track_iterator const end   = priority_tracks.end();
252
253
13.7k
        for( track_iterator it = begin; it != end; ++it )
254
6.97k
        {
255
6.97k
            seekpoint_pair_t track_points = get_seekpoints_around( target_pts, _tracks_seekpoints[ *it ] );
256
257
6.97k
            if( it == begin ) {
258
6.78k
                points = track_points;
259
6.78k
                continue;
260
6.78k
            }
261
262
184
            if( track_points.first.trust_level > Seekpoint::DISABLED &&
263
155
                points.first.fpos > track_points.first.fpos )
264
7
                points.first = track_points.first;
265
266
184
            if( track_points.second.trust_level > Seekpoint::DISABLED &&
267
1
                points.second.fpos < track_points.second.fpos )
268
1
                points.second = track_points.second;
269
184
        }
270
6.81k
    }
271
272
6.81k
    { // check if we got a cluster which is closer to target_pts than the found cues //
273
274
6.81k
        cluster_map_t::iterator it = _clusters.lower_bound( target_pts );
275
276
6.81k
        if( it != _clusters.begin() && --it != _clusters.end() )
277
570
        {
278
570
            Cluster const& cluster = it->second;
279
280
570
            if( cluster.fpos > points.first.fpos )
281
133
            {
282
133
                points.first = Seekpoint( cluster.fpos, cluster.pts );
283
284
                // do we need to update the max point? //
285
286
133
                if( points.second.fpos < points.first.fpos )
287
5
                    points.second = Seekpoint( cluster.fpos + cluster.size, cluster.pts + cluster.duration );
288
133
            }
289
570
        }
290
6.81k
    }
291
292
6.81k
    return points;
293
7.03k
}
294
295
SegmentSeeker::tracks_seekpoint_t
296
SegmentSeeker::get_seekpoints( matroska_segment_c& ms, vlc_tick_t target_pts,
297
                               track_ids_t const& priority_tracks, track_ids_t const& filter_tracks )
298
6.74k
{
299
6.74k
    struct contains_all_of_t {
300
6.74k
        bool operator()( tracks_seekpoint_t const& haystack, track_ids_t const& track_ids )
301
6.78k
        {
302
13.4k
            for( track_ids_t::const_iterator it = track_ids.begin(); it != track_ids.end(); ++it ) {
303
6.94k
                if( haystack.find( *it ) == haystack.end() )
304
295
                    return false;
305
6.94k
            }
306
307
6.49k
            return true;
308
6.78k
        }
309
6.74k
    };
310
311
6.74k
    for( vlc_tick_t needle_pts = target_pts; ; )
312
7.03k
    {
313
7.03k
        seekpoint_pair_t seekpoints = get_seekpoints_around( needle_pts, priority_tracks );
314
315
7.03k
        Seekpoint const& start = seekpoints.first;
316
7.03k
        Seekpoint const& end   = seekpoints.second;
317
318
7.03k
        if ( start.fpos == std::numeric_limits<fptr_t>::max() )
319
254
            return tracks_seekpoint_t();
320
321
6.78k
        if ( (end.fpos != std::numeric_limits<fptr_t>::max() || !ms.b_cues) &&
322
6.53k
             (needle_pts != start.pts || start.trust_level < Seekpoint::TRUSTED))
323
            // do not read the whole (infinite?) file to get seek indexes
324
            // do not generate an index if we already have the correct seekpoint
325
6.48k
            index_range( ms, Range( start.fpos, end.fpos ), needle_pts );
326
327
6.78k
        tracks_seekpoint_t tpoints = find_greatest_seekpoints_in_range( start.fpos, target_pts, filter_tracks );
328
329
6.78k
        if( contains_all_of_t() ( tpoints, priority_tracks ) )
330
6.49k
            return tpoints;
331
332
        // Avoid busyloop, don't iterate on the same seekpoint
333
295
        if( needle_pts == start.pts - 1 )
334
            // we found the same needle twice, stop looking
335
3
            return tpoints;
336
337
292
        needle_pts = start.pts - 1;
338
292
    }
339
340
0
    vlc_assert_unreachable();
341
0
}
342
343
void
344
SegmentSeeker::index_range( matroska_segment_c& ms, Range search_area, vlc_tick_t max_pts )
345
6.48k
{
346
6.48k
    ranges_t areas_to_search = get_search_areas( search_area.start, search_area.end );
347
348
13.0k
    for( ranges_t::const_iterator range_it = areas_to_search.begin(); range_it != areas_to_search.end(); ++range_it )
349
6.52k
        index_unsearched_range( ms, *range_it, max_pts );
350
6.48k
}
351
352
void
353
SegmentSeeker::index_unsearched_range( matroska_segment_c& ms, Range search_area, vlc_tick_t max_pts )
354
6.52k
{
355
6.52k
    mkv_jump_to( ms, search_area.start );
356
357
6.52k
    search_area.start = ms.es.I_O().getFilePointer();
358
359
6.52k
    fptr_t  block_pos = search_area.start;
360
6.52k
    vlc_tick_t block_pts;
361
362
19.9k
    while( block_pos < search_area.end )
363
19.9k
    {
364
19.9k
        KaxBlock * block;
365
19.9k
        KaxSimpleBlock * simpleblock;
366
19.9k
        KaxBlockAdditions *additions;
367
368
19.9k
        bool     b_key_picture;
369
19.9k
        bool     b_discardable_picture;
370
19.9k
        uint64_t  i_block_duration;
371
19.9k
        track_id_t track_id;
372
373
19.9k
        if( ms.BlockGet( block, simpleblock, additions,
374
19.9k
                         &b_key_picture, &b_discardable_picture, &i_block_duration ) )
375
1.08k
        {
376
1.08k
            delete additions;
377
1.08k
            break;
378
1.08k
        }
379
18.8k
        delete additions;
380
381
18.8k
        KaxInternalBlock& internal_block = simpleblock
382
18.8k
            ? static_cast<KaxInternalBlock&>( *simpleblock )
383
18.8k
            : static_cast<KaxInternalBlock&>( *block );
384
385
18.8k
        block_pos = internal_block.GetElementPosition();
386
18.8k
        block_pts = VLC_TICK_FROM_NS(internal_block.GlobalTimestamp());
387
18.8k
        track_id  = internal_block.TrackNum();
388
389
18.8k
        bool const b_valid_track = ms.FindTrackByBlock( block, simpleblock ) != NULL;
390
391
18.8k
        delete block;
392
393
18.8k
        if( b_valid_track )
394
18.8k
        {
395
18.8k
            if( b_key_picture )
396
12.2k
                add_seekpoint( track_id, Seekpoint( block_pos, block_pts ) );
397
398
18.8k
            if( max_pts < block_pts )
399
5.43k
                break;
400
18.8k
        }
401
18.8k
    }
402
403
6.52k
    search_area.end = ms.es.I_O().getFilePointer();
404
405
6.52k
    mark_range_as_searched( search_area );
406
6.52k
}
407
408
void
409
SegmentSeeker::mark_range_as_searched( Range data )
410
31.1k
{
411
    /* TODO: this is utterly ugly, we should do the insertion in-place */
412
413
31.1k
    _ranges_searched.insert( std::upper_bound( _ranges_searched.begin(), _ranges_searched.end(), data ), data );
414
415
31.1k
    {
416
31.1k
        ranges_t merged;
417
418
88.1k
        for( ranges_t::iterator it = _ranges_searched.begin(); it != _ranges_searched.end(); ++it )
419
57.0k
        {
420
57.0k
            if( merged.size() )
421
25.9k
            {
422
25.9k
                Range& last_entry = *merged.rbegin();
423
424
25.9k
                if( last_entry.end+1 >= it->start && last_entry.end < it->end )
425
9.48k
                {
426
9.48k
                    last_entry.end = it->end;
427
9.48k
                    continue;
428
9.48k
                }
429
430
16.4k
                if( it->start >= last_entry.start && it->end <= last_entry.end )
431
15.3k
                {
432
15.3k
                    last_entry.end = std::max( last_entry.end, it->end );
433
15.3k
                    continue;
434
15.3k
                }
435
16.4k
            }
436
437
32.1k
            merged.push_back( *it );
438
32.1k
        }
439
440
31.1k
        _ranges_searched = std::move(merged);
441
31.1k
    }
442
31.1k
}
443
444
445
SegmentSeeker::ranges_t
446
SegmentSeeker::get_search_areas( fptr_t start, fptr_t end ) const
447
6.48k
{
448
6.48k
    ranges_t areas_to_search;
449
6.48k
    Range needle ( start, end );
450
451
6.48k
    ranges_t::const_iterator it = greatest_lower_bound( _ranges_searched.begin(), _ranges_searched.end(), needle );
452
453
7.08k
    for( ; it != _ranges_searched.end() && needle.start < needle.end; ++it )
454
601
    {
455
601
        if( needle.start < it->start )
456
87
        {
457
87
            areas_to_search.push_back( Range( needle.start, it->start ) );
458
87
        }
459
460
601
        if( needle.start <= it->end )
461
431
            needle.start = it->end + 1;
462
601
    }
463
464
6.48k
    needle.start = std::max( needle.start, start );
465
6.48k
    if( it == _ranges_searched.end() && needle.start < needle.end )
466
6.43k
    {
467
6.43k
        areas_to_search.push_back( needle );
468
6.43k
    }
469
470
6.48k
    return areas_to_search;
471
6.48k
}
472
473
void
474
SegmentSeeker::mkv_jump_to( matroska_segment_c& ms, fptr_t fpos )
475
13.0k
{
476
13.0k
    fptr_t i_cluster_pos = -1;
477
478
13.0k
    if ( fpos != std::numeric_limits<SegmentSeeker::fptr_t>::max() )
479
13.0k
    {
480
13.0k
        if ( !_cluster_positions.empty() )
481
11.9k
        {
482
11.9k
            cluster_positions_t::iterator cluster_it = greatest_lower_bound(
483
11.9k
              _cluster_positions.begin(), _cluster_positions.end(), fpos
484
11.9k
            );
485
486
11.9k
            ms.es.I_O().setFilePointer( *cluster_it );
487
11.9k
            ms.ep.reconstruct( &ms.es, ms.segment, &ms.sys.demuxer );
488
489
11.9k
            for(;;)
490
14.2k
            {
491
14.2k
                ms.cluster = NULL;
492
14.2k
                EbmlElement *el = ms.ep.Get();
493
14.2k
                if( el == nullptr )
494
227
                {
495
227
                    msg_Err( &ms.sys.demuxer, "unable to read KaxCluster during seek, giving up" );
496
227
                    return;
497
227
                }
498
14.0k
                if (!MKV_IS_ID( el, KaxCluster ))
499
2.18k
                {
500
2.18k
                    ms.cluster = nullptr; // the previous cluster found is not valid anymore
501
2.18k
                    continue; // look for the next element
502
2.18k
                }
503
504
11.8k
                ms.cluster = static_cast<KaxCluster*>( el );
505
506
11.8k
                i_cluster_pos = ms.cluster->GetElementPosition();
507
508
11.8k
                add_cluster_position( i_cluster_pos );
509
510
11.8k
                mark_range_as_searched( Range( i_cluster_pos, ms.es.I_O().getFilePointer() ) );
511
512
11.8k
                if ( !ms.cluster->IsFiniteSize() || ms.cluster->GetEndPosition() >= fpos)
513
11.7k
                    break;
514
11.8k
            }
515
11.9k
        }
516
13.0k
    }
517
0
    else if (ms.cluster != NULL)
518
0
    {
519
        // make sure we start reading after the Cluster start
520
0
        ms.es.I_O().setFilePointer(ms.cluster->GetDataStart());
521
0
    }
522
523
13.0k
    assert(ms.ep.GetLevel() == 1);
524
12.7k
    ms.ep.Down();
525
526
    /* read until cluster/timecode to initialize cluster */
527
528
13.2k
    while( EbmlElement * el = ms.ep.Get() )
529
12.1k
    {
530
12.1k
        try {
531
12.1k
            if( MKV_CHECKED_PTR_DECL( p_tc, KaxClusterTimestamp, el ) )
532
11.6k
            {
533
11.6k
                p_tc->ReadData( ms.es.I_O(), SCOPE_ALL_DATA );
534
11.6k
                ms.cluster->InitTimestamp( static_cast<uint64_t>( *p_tc ), ms.i_timescale );
535
11.6k
                add_cluster(ms.cluster);
536
11.6k
                break;
537
11.6k
            }
538
477
            else if( MKV_CHECKED_PTR_DECL( crc, EbmlCrc32, el ) )
539
294
            {
540
294
                crc->ReadData( ms.es.I_O(), SCOPE_ALL_DATA ); /* avoid a skip that may fail */
541
294
            }
542
12.1k
        }
543
12.1k
        catch(...)
544
12.1k
        {
545
0
            msg_Err( &ms.sys.demuxer,"Error while reading %s",  EBML_NAME(el) );
546
0
        }
547
12.1k
    }
548
549
    /* TODO: add error handling; what if we never get a KaxCluster and/or KaxClusterTimestamp? */
550
551
12.7k
    mark_range_as_searched( Range( i_cluster_pos, ms.es.I_O().getFilePointer() ) );
552
553
    /* jump to desired position */
554
555
12.7k
    if ( fpos != std::numeric_limits<SegmentSeeker::fptr_t>::max() )
556
12.7k
        ms.es.I_O().setFilePointer( fpos );
557
12.7k
}
558
559
} // namespace