1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2005, 2008, 2010 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
28 #include "matroska_segment.hpp"
31 #include "chapters.hpp"
32 #include "Ebml_parser.hpp"
34 #include "stream_io_callback.hpp"
37 #include "../../modules/codec/dts_header.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_shortname( "Matroska" )
51 set_description( N_("Matroska stream demuxer" ) )
52 set_capability( "demux", 50 )
53 set_callbacks( Open, Close )
54 set_category( CAT_INPUT )
55 set_subcategory( SUBCAT_INPUT_DEMUX )
57 add_bool( "mkv-use-ordered-chapters", true,
58 N_("Respect ordered chapters"),
59 N_("Play chapters in the order specified in the segment."), false );
61 add_bool( "mkv-use-chapter-codec", true,
63 N_("Use chapter codecs found in the segment."), true );
65 add_bool( "mkv-preload-local-dir", true,
66 N_("Preload MKV files in the same directory"),
67 N_("Preload matroska files in the same directory to find linked segments (not good for broken files)."), false );
69 add_bool( "mkv-seek-percent", false,
70 N_("Seek based on percent not time"),
71 N_("Seek based on percent not time."), true );
73 add_bool( "mkv-use-dummy", false,
75 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
77 add_shortcut( "mka", "mkv" )
82 static int Demux ( demux_t * );
83 static int Control( demux_t *, int, va_list );
84 static void Seek ( demux_t *, mtime_t i_date, double f_percent, virtual_chapter_c *p_chapter );
86 /*****************************************************************************
87 * Open: initializes matroska demux structures
88 *****************************************************************************/
89 static int Open( vlc_object_t * p_this )
91 demux_t *p_demux = (demux_t*)p_this;
93 matroska_stream_c *p_stream;
94 matroska_segment_c *p_segment;
95 const uint8_t *p_peek;
96 std::string s_path, s_filename;
97 vlc_stream_io_callback *p_io_callback;
98 EbmlStream *p_io_stream;
99 bool b_need_preload = false;
101 /* peek the begining */
102 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
104 /* is a valid file */
105 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
106 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
108 /* Set the demux function */
109 p_demux->pf_demux = Demux;
110 p_demux->pf_control = Control;
111 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
113 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
114 p_io_stream = new EbmlStream( *p_io_callback );
116 if( p_io_stream == NULL )
118 msg_Err( p_demux, "failed to create EbmlStream" );
119 delete p_io_callback;
124 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
125 if( p_stream == NULL )
127 msg_Err( p_demux, "cannot find KaxSegment or missing mandatory KaxInfo" );
130 p_sys->streams.push_back( p_stream );
132 p_stream->p_io_callback = p_io_callback;
133 p_stream->p_estream = p_io_stream;
135 for (size_t i=0; i<p_stream->segments.size(); i++)
137 p_stream->segments[i]->Preload();
138 b_need_preload |= p_stream->segments[i]->b_ref_external_segments;
141 p_segment = p_stream->segments[0];
142 if( p_segment->cluster == NULL )
144 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
148 if (b_need_preload && var_InheritBool( p_demux, "mkv-preload-local-dir" ))
150 msg_Dbg( p_demux, "Preloading local dir" );
151 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
152 if ( p_demux->psz_file && !strcmp( p_demux->psz_access, "file" ) )
154 // assume it's a regular file
155 // get the directory path
156 s_path = p_demux->psz_file;
157 if (s_path.at(s_path.length() - 1) == DIR_SEP_CHAR)
159 s_path = s_path.substr(0,s_path.length()-1);
163 if (s_path.find_last_of(DIR_SEP_CHAR) > 0)
165 s_path = s_path.substr(0,s_path.find_last_of(DIR_SEP_CHAR));
169 DIR *p_src_dir = vlc_opendir(s_path.c_str());
171 if (p_src_dir != NULL)
173 const char *psz_file;
174 while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
176 if (strlen(psz_file) > 4)
178 s_filename = s_path + DIR_SEP_CHAR + psz_file;
180 #if defined(_WIN32) || defined(__OS2__)
181 if (!strcasecmp(s_filename.c_str(), p_demux->psz_file))
183 if (!s_filename.compare(p_demux->psz_file))
186 continue; // don't reuse the original opened file
189 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
190 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
192 // test whether this file belongs to our family
193 const uint8_t *p_peek;
194 bool file_ok = false;
195 #warning Memory leak!
196 std::string s_url = vlc_path2uri( s_filename.c_str(), "file" );
197 stream_t *p_file_stream = stream_UrlNew(
200 /* peek the begining */
202 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
203 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
204 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
208 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
209 EbmlStream *p_estream = new EbmlStream(*p_file_io);
211 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
213 if ( p_stream == NULL )
215 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
221 p_stream->p_io_callback = p_file_io;
222 p_stream->p_estream = p_estream;
223 p_sys->streams.push_back( p_stream );
228 if( p_file_stream ) {
229 stream_Delete( p_file_stream );
231 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
236 closedir( p_src_dir );
240 p_sys->PreloadFamily( *p_segment );
242 else if (b_need_preload)
243 msg_Warn( p_demux, "This file references other files, you may want to enable the preload of local directory");
245 if ( !p_sys->PreloadLinked() ||
246 !p_sys->PreparePlayback( NULL ) )
248 msg_Err( p_demux, "cannot use the segment" );
263 /*****************************************************************************
264 * Close: frees unused data
265 *****************************************************************************/
266 static void Close( vlc_object_t *p_this )
268 demux_t *p_demux = (demux_t*)p_this;
269 demux_sys_t *p_sys = p_demux->p_sys;
270 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
273 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
275 p_segment->UnSelect();
281 /*****************************************************************************
283 *****************************************************************************/
284 static int Control( demux_t *p_demux, int i_query, va_list args )
286 demux_sys_t *p_sys = p_demux->p_sys;
293 input_attachment_t ***ppp_attach;
298 case DEMUX_GET_ATTACHMENTS:
299 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
300 pi_int = (int*)va_arg( args, int * );
302 if( p_sys->stored_attachments.size() <= 0 )
305 *pi_int = p_sys->stored_attachments.size();
306 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t*) *
307 p_sys->stored_attachments.size() );
310 for( size_t i = 0; i < p_sys->stored_attachments.size(); i++ )
312 attachment_c *a = p_sys->stored_attachments[i];
313 (*ppp_attach)[i] = vlc_input_attachment_New( a->fileName(), a->mimeType(), NULL,
314 a->p_data, a->size() );
319 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
320 vlc_meta_Merge( p_meta, p_sys->meta );
323 case DEMUX_GET_LENGTH:
324 pi64 = (int64_t*)va_arg( args, int64_t * );
325 if( p_sys->f_duration > 0.0 )
327 *pi64 = (int64_t)(p_sys->f_duration * 1000);
332 case DEMUX_GET_POSITION:
333 pf = (double*)va_arg( args, double * );
334 if ( p_sys->f_duration > 0.0 )
335 *pf = (double)(p_sys->i_pts >= p_sys->i_start_pts ? p_sys->i_pts : p_sys->i_start_pts ) / (1000.0 * p_sys->f_duration);
338 case DEMUX_SET_POSITION:
339 if( p_sys->f_duration > 0.0 )
341 f = (double)va_arg( args, double );
342 Seek( p_demux, -1, f, NULL );
348 pi64 = (int64_t*)va_arg( args, int64_t * );
349 *pi64 = p_sys->i_pts;
352 case DEMUX_GET_TITLE_INFO:
353 if( p_sys->titles.size() > 1 || ( p_sys->titles.size() == 1 && p_sys->titles[0]->i_seekpoint > 0 ) )
355 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
356 int *pi_int = (int*)va_arg( args, int* );
358 *pi_int = p_sys->titles.size();
359 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t* ) * p_sys->titles.size() );
361 for( size_t i = 0; i < p_sys->titles.size(); i++ )
362 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
367 case DEMUX_SET_TITLE:
368 /* handle editions as titles */
369 i_idx = (int)va_arg( args, int );
370 if(i_idx < p_sys->titles.size() && p_sys->titles[i_idx]->i_seekpoint)
372 p_sys->p_current_segment->i_current_edition = i_idx;
373 p_sys->i_current_title = i_idx;
374 p_sys->p_current_segment->p_current_chapter = p_sys->p_current_segment->editions[p_sys->p_current_segment->i_current_edition]->getChapterbyTimecode(0);
376 Seek( p_demux, (int64_t)p_sys->titles[i_idx]->seekpoint[0]->i_time_offset, -1, NULL);
377 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_TITLE;
378 p_demux->info.i_seekpoint = 0;
379 p_demux->info.i_title = i_idx;
380 p_sys->f_duration = (float) p_sys->titles[i_idx]->i_length / 1000.f;
385 case DEMUX_SET_SEEKPOINT:
386 i_skp = (int)va_arg( args, int );
388 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
389 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
391 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
392 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
393 p_demux->info.i_seekpoint = i_skp;
399 pf = (double *)va_arg( args, double * );
401 if( p_sys->p_current_segment && p_sys->p_current_segment->CurrentSegment() )
403 const matroska_segment_c *p_segment = p_sys->p_current_segment->CurrentSegment();
404 for( size_t i = 0; i < p_segment->tracks.size(); i++ )
406 mkv_track_t *tk = p_segment->tracks[i];
407 if( tk->fmt.i_cat == VIDEO_ES && tk->fmt.video.i_frame_rate_base > 0 )
409 *pf = (double)tk->fmt.video.i_frame_rate / tk->fmt.video.i_frame_rate_base;
417 i64 = (int64_t) va_arg( args, int64_t );
418 msg_Dbg(p_demux,"SET_TIME to %" PRId64, i64 );
419 Seek( p_demux, i64, -1, NULL );
427 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, virtual_chapter_c *p_chapter )
429 demux_sys_t *p_sys = p_demux->p_sys;
430 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
431 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
432 mtime_t i_time_offset = 0;
433 int64_t i_global_position = -1;
437 msg_Dbg( p_demux, "seek request to %" PRId64 " (%f%%)", i_date, f_percent );
438 if( i_date < 0 && f_percent < 0 )
440 msg_Warn( p_demux, "cannot seek nowhere!" );
443 if( f_percent > 1.0 )
445 msg_Warn( p_demux, "cannot seek so far!" );
448 if( p_sys->f_duration < 0 )
450 msg_Warn( p_demux, "cannot seek without duration!");
455 msg_Warn( p_demux, "cannot seek without valid segment position");
459 /* seek without index or without date */
460 if( f_percent >= 0 && (var_InheritBool( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
462 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
463 if( !p_segment->b_cues )
465 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
467 msg_Dbg( p_demux, "lengthy way of seeking for pos:%" PRId64, i_pos );
468 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
470 if( p_segment->p_indexes[i_index].i_position >= i_pos &&
471 p_segment->p_indexes[i_index].i_time > 0 )
474 if( i_index == p_segment->i_index )
477 if( p_segment->p_indexes[i_index].i_position < i_pos )
479 msg_Dbg( p_demux, "no cues, seek request to global pos: %" PRId64, i_pos );
480 i_global_position = i_pos;
484 p_vsegment->Seek( *p_demux, i_date, i_time_offset, p_chapter, i_global_position );
487 /* Needed by matroska_segment::Seek() and Seek */
488 void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
489 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
491 demux_sys_t *p_sys = p_demux->p_sys;
492 matroska_segment_c *p_segment = p_sys->p_current_segment->CurrentSegment();
494 if( !p_segment ) return;
497 if( p_segment->BlockFindTrackIndex( &i_track, block, simpleblock ) )
499 msg_Err( p_demux, "invalid track number" );
503 mkv_track_t *tk = p_segment->tracks[i_track];
505 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
507 msg_Err( p_demux, "unknown track number" );
511 i_pts -= tk->i_codec_delay;
513 if ( tk->fmt.i_cat != NAV_ES )
516 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
520 tk->b_inited = false;
521 if( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES )
522 tk->i_last_dts = i_pts;
528 /* First send init data */
529 if( !tk->b_inited && tk->i_data_init > 0 )
533 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
534 p_init = MemToBlock( tk->p_data_init, tk->i_data_init, 0 );
535 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
540 size_t frame_size = 0;
541 size_t block_size = 0;
543 if( simpleblock != NULL )
544 block_size = simpleblock->GetSize();
546 block_size = block->GetSize();
548 const unsigned int i_number_frames = block != NULL ? block->NumberFrames() :
549 ( simpleblock != NULL ? simpleblock->NumberFrames() : 0 );
550 for( unsigned int i = 0; i < i_number_frames; i++ )
554 if( simpleblock != NULL )
556 data = &simpleblock->GetBuffer(i);
557 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
558 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
562 data = &block->GetBuffer(i);
563 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
565 frame_size += data->Size();
566 if( !data->Buffer() || data->Size() > frame_size || frame_size > block_size )
568 msg_Warn( p_demux, "Cannot read frame (too long or no frame)" );
572 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
573 tk->p_compression_data != NULL &&
574 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
575 p_block = MemToBlock( data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
576 else if( unlikely( tk->fmt.i_codec == VLC_CODEC_WAVPACK ) )
577 p_block = packetize_wavpack(tk, data->Buffer(), data->Size());
579 p_block = MemToBlock( data->Buffer(), data->Size(), 0 );
581 if( p_block == NULL )
586 #if defined(HAVE_ZLIB_H)
587 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
588 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
590 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
591 if( p_block == NULL )
596 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
597 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
599 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
601 switch( tk->fmt.i_codec )
604 case VLC_CODEC_ATRAC3:
606 handle_real_audio(p_demux, tk, p_block, i_pts);
607 block_Release(p_block);
608 i_pts = ( tk->i_default_duration )?
609 i_pts + ( mtime_t )( tk->i_default_duration / 1000 ):
615 /* Check if packetization is correct and without padding.
616 * example: Test_mkv_div3_DTS_1920x1080_1785Kbps_23,97fps.mkv */
617 if( p_block->i_buffer > 6 )
619 unsigned int a, b, c, d;
621 int i_frame_size = GetSyncInfo( p_block->p_buffer, &e, &a, &b, &c, &d );
622 if( i_frame_size > 0 )
623 p_block->i_buffer = __MIN(p_block->i_buffer, (size_t)i_frame_size);
628 mtime_t i_length = i_duration * tk-> f_timecodescale *
629 (double) p_segment->i_timescale / 1000.0;
630 if ( i_length < 0 ) i_length = 0;
631 p_block->i_nb_samples = i_length * tk->fmt.audio.i_rate
637 if( tk->fmt.i_cat != VIDEO_ES )
639 if ( tk->fmt.i_cat == NAV_ES )
641 // TODO handle the start/stop times of this packet
642 p_sys->p_ev->SetPci( (const pci_t *)&p_block->p_buffer[1]);
643 block_Release( p_block );
646 else if( tk->fmt.i_cat == AUDIO_ES )
648 if( tk->i_chans_to_reorder )
649 aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
650 tk->fmt.audio.i_channels,
651 tk->pi_chan_table, tk->fmt.i_codec );
654 p_block->i_dts = p_block->i_pts = i_pts;
658 // correct timestamping when B frames are used
661 p_block->i_pts = VLC_TS_INVALID;
662 p_block->i_dts = i_pts;
664 else if( tk->b_pts_only )
666 p_block->i_pts = i_pts;
667 p_block->i_dts = i_pts;
671 p_block->i_pts = i_pts;
673 p_block->i_dts = p_block->i_pts;
675 p_block->i_dts = min( i_pts, tk->i_last_dts + ( mtime_t )( tk->i_default_duration / 1000 ) );
678 if( p_block->i_dts > VLC_TS_INVALID &&
679 ( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES ) )
681 tk->i_last_dts = p_block->i_dts;
685 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
687 if( !tk->b_no_duration )
689 p_block->i_length = i_duration * tk-> f_timecodescale *
690 (double) p_segment->i_timescale / ( 1000.0 * i_number_frames );
693 /* FIXME remove when VLC_TS_INVALID work is done */
694 if( i == 0 || p_block->i_dts > VLC_TS_INVALID )
695 p_block->i_dts += VLC_TS_0;
696 if( !tk->b_dts_only && ( i == 0 || p_block->i_pts > VLC_TS_INVALID ) )
697 p_block->i_pts += VLC_TS_0;
699 es_out_Send( p_demux->out, tk->p_es, p_block );
701 /* use time stamp only for first block */
702 i_pts = ( tk->i_default_duration )?
703 i_pts + ( mtime_t )( tk->i_default_duration / 1000 ):
708 /*****************************************************************************
709 * Demux: reads and demuxes data packets
710 *****************************************************************************
711 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
712 *****************************************************************************/
713 static int Demux( demux_t *p_demux)
715 demux_sys_t *p_sys = p_demux->p_sys;
717 vlc_mutex_lock( &p_sys->lock_demuxer );
719 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
720 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
721 if ( p_segment == NULL )
723 vlc_mutex_unlock( &p_sys->lock_demuxer );
730 if( p_sys->i_pts >= p_sys->i_start_pts )
731 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
737 if ( p_vsegment->CurrentEdition() &&
738 p_vsegment->CurrentEdition()->b_ordered &&
739 p_vsegment->CurrentChapter() == NULL )
740 /* nothing left to read in this ordered edition */
744 KaxSimpleBlock *simpleblock;
745 int64_t i_block_duration = 0;
747 bool b_discardable_picture;
748 if( p_segment->BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
750 if ( p_vsegment->CurrentEdition() && p_vsegment->CurrentEdition()->b_ordered )
752 const virtual_chapter_c *p_chap = p_vsegment->CurrentChapter();
753 // check if there are more chapters to read
754 if ( p_chap != NULL )
756 /* TODO handle successive chapters with the same user_start_time/user_end_time
758 p_sys->i_pts = p_chap->i_virtual_stop_time;
759 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
768 msg_Warn( p_demux, "cannot get block EOF?" );
773 if( simpleblock != NULL )
774 p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)simpleblock->GlobalTimecode() / INT64_C(1000) );
776 p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)block->GlobalTimecode() / INT64_C(1000) );
778 mtime_t i_pcr = VLC_TS_INVALID;
779 for( size_t i = 0; i < p_segment->tracks.size(); i++)
780 if( p_segment->tracks[i]->i_last_dts > VLC_TS_INVALID &&
781 ( p_segment->tracks[i]->i_last_dts < i_pcr || i_pcr == VLC_TS_INVALID ))
782 i_pcr = p_segment->tracks[i]->i_last_dts;
784 if( i_pcr > p_sys->i_pcr + 300000 )
786 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
787 p_sys->i_pcr = i_pcr;
790 if( p_sys->i_pts >= p_sys->i_start_pts )
792 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
800 if ( p_vsegment->CurrentEdition() &&
801 p_vsegment->CurrentEdition()->b_ordered &&
802 p_vsegment->CurrentChapter() == NULL )
804 /* nothing left to read in this ordered edition */
809 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, b_key_picture || b_discardable_picture );
813 vlc_mutex_unlock( &p_sys->lock_demuxer );
818 vlc_mutex_unlock( &p_sys->lock_demuxer );