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 *****************************************************************************/
28 #include "matroska_segment.hpp"
31 #include "chapters.hpp"
32 #include "Ebml_parser.hpp"
34 #include "stream_io_callback.hpp"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
43 set_shortname( "Matroska" );
44 set_description( N_("Matroska stream demuxer" ) );
45 set_capability( "demux", 0 );
46 set_callbacks( Open, Close );
47 set_category( CAT_INPUT );
48 set_subcategory( SUBCAT_INPUT_DEMUX );
50 add_bool( "mkv-use-ordered-chapters", 1, NULL,
51 N_("Ordered chapters"),
52 N_("Play ordered chapters as specified in the segment."), true );
54 add_bool( "mkv-use-chapter-codec", 1, NULL,
56 N_("Use chapter codecs found in the segment."), true );
58 add_bool( "mkv-preload-local-dir", 1, NULL,
59 N_("Preload Directory"),
60 N_("Preload matroska files from the same family in the same directory (not good for broken files)."), true );
62 add_bool( "mkv-seek-percent", 0, NULL,
63 N_("Seek based on percent not time"),
64 N_("Seek based on percent not time."), true );
66 add_bool( "mkv-use-dummy", 0, NULL,
68 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
70 add_shortcut( "mka" );
71 add_shortcut( "mkv" );
76 static int Demux ( demux_t * );
77 static int Control( demux_t *, int, va_list );
78 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
80 /*****************************************************************************
81 * Open: initializes matroska demux structures
82 *****************************************************************************/
83 static int Open( vlc_object_t * p_this )
85 demux_t *p_demux = (demux_t*)p_this;
87 matroska_stream_c *p_stream;
88 matroska_segment_c *p_segment;
89 const uint8_t *p_peek;
90 std::string s_path, s_filename;
91 vlc_stream_io_callback *p_io_callback;
92 EbmlStream *p_io_stream;
94 /* peek the begining */
95 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
98 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
99 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
101 /* Set the demux function */
102 p_demux->pf_demux = Demux;
103 p_demux->pf_control = Control;
104 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
106 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
107 p_io_stream = new EbmlStream( *p_io_callback );
109 if( p_io_stream == NULL )
111 msg_Err( p_demux, "failed to create EbmlStream" );
112 delete p_io_callback;
117 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
118 if( p_stream == NULL )
120 msg_Err( p_demux, "cannot find KaxSegment" );
123 p_sys->streams.push_back( p_stream );
125 p_stream->p_in = p_io_callback;
126 p_stream->p_es = p_io_stream;
128 for (size_t i=0; i<p_stream->segments.size(); i++)
130 p_stream->segments[i]->Preload();
133 p_segment = p_stream->segments[0];
134 if( p_segment->cluster == NULL )
136 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
140 if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
142 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
143 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
145 // assume it's a regular file
146 // get the directory path
147 s_path = p_demux->psz_path;
148 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
150 s_path = s_path.substr(0,s_path.length()-1);
154 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
156 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
160 DIR *p_src_dir = utf8_opendir(s_path.c_str());
162 if (p_src_dir != NULL)
165 while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
167 if (strlen(psz_file) > 4)
169 s_filename = s_path + DIRECTORY_SEPARATOR + psz_file;
172 if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
174 if (!s_filename.compare(p_demux->psz_path))
178 continue; // don't reuse the original opened file
181 #if defined(__GNUC__) && (__GNUC__ < 3)
182 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
183 !s_filename.compare("mka", s_filename.length() - 3, 3))
185 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
186 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
189 // test wether this file belongs to our family
190 const uint8_t *p_peek;
191 bool file_ok = false;
192 stream_t *p_file_stream = stream_UrlNew(
195 /* peek the begining */
197 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
198 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
199 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
203 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
204 EbmlStream *p_estream = new EbmlStream(*p_file_io);
206 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
208 if ( p_stream == NULL )
210 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
216 p_stream->p_in = p_file_io;
217 p_stream->p_es = p_estream;
218 p_sys->streams.push_back( p_stream );
223 if( p_file_stream ) {
224 stream_Delete( p_file_stream );
226 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
232 closedir( p_src_dir );
236 p_sys->PreloadFamily( *p_segment );
239 p_sys->PreloadLinked( p_segment );
241 if ( !p_sys->PreparePlayback( NULL ) )
243 msg_Err( p_demux, "cannot use the segment" );
247 p_sys->StartUiThread();
256 /*****************************************************************************
257 * Close: frees unused data
258 *****************************************************************************/
259 static void Close( vlc_object_t *p_this )
261 demux_t *p_demux = (demux_t*)p_this;
262 demux_sys_t *p_sys = p_demux->p_sys;
267 /*****************************************************************************
269 *****************************************************************************/
270 static int Control( demux_t *p_demux, int i_query, va_list args )
272 demux_sys_t *p_sys = p_demux->p_sys;
279 input_attachment_t ***ppp_attach;
285 case DEMUX_GET_ATTACHMENTS:
286 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
287 pi_int = (int*)va_arg( args, int * );
289 if( p_sys->stored_attachments.size() <= 0 )
292 *pi_int = p_sys->stored_attachments.size();
293 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
294 p_sys->stored_attachments.size() );
297 for( i = 0; i < p_sys->stored_attachments.size(); i++ )
299 attachment_c *a = p_sys->stored_attachments[i];
300 (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
301 a->p_data, a->i_size );
306 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
307 vlc_meta_Merge( p_meta, p_sys->meta );
310 case DEMUX_GET_LENGTH:
311 pi64 = (int64_t*)va_arg( args, int64_t * );
312 if( p_sys->f_duration > 0.0 )
314 *pi64 = (int64_t)(p_sys->f_duration * 1000);
319 case DEMUX_GET_POSITION:
320 pf = (double*)va_arg( args, double * );
321 if ( p_sys->f_duration > 0.0 )
322 *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);
325 case DEMUX_SET_POSITION:
326 f = (double)va_arg( args, double );
327 Seek( p_demux, -1, f, NULL );
331 pi64 = (int64_t*)va_arg( args, int64_t * );
332 *pi64 = p_sys->i_pts;
335 case DEMUX_GET_TITLE_INFO:
336 if( p_sys->titles.size() > 1 || ( p_sys->titles.size() == 1 && p_sys->titles[0]->i_seekpoint > 0 ) )
338 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
339 int *pi_int = (int*)va_arg( args, int* );
341 *pi_int = p_sys->titles.size();
342 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
344 for( size_t i = 0; i < p_sys->titles.size(); i++ )
346 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
352 case DEMUX_SET_TITLE:
353 /* TODO handle editions as titles */
354 i_idx = (int)va_arg( args, int );
355 if( i_idx < p_sys->used_segments.size() )
357 p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
362 case DEMUX_SET_SEEKPOINT:
363 i_skp = (int)va_arg( args, int );
365 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
366 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
368 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
369 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
370 p_demux->info.i_seekpoint = i_skp;
376 pf = (double *)va_arg( args, double * );
378 if( p_sys->p_current_segment && p_sys->p_current_segment->Segment() )
380 const matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
381 for( size_t i = 0; i < p_segment->tracks.size(); i++ )
383 mkv_track_t *tk = p_segment->tracks[i];
384 if( tk->fmt.i_cat == VIDEO_ES && tk->fmt.video.i_frame_rate_base > 0 )
386 *pf = (double)tk->fmt.video.i_frame_rate / tk->fmt.video.i_frame_rate_base;
400 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
402 demux_sys_t *p_sys = p_demux->p_sys;
403 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
404 matroska_segment_c *p_segment = p_vsegment->Segment();
405 mtime_t i_time_offset = 0;
406 int64_t i_global_position = -1;
410 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
411 if( i_date < 0 && f_percent < 0 )
413 msg_Warn( p_demux, "cannot seek nowhere !" );
416 if( f_percent > 1.0 )
418 msg_Warn( p_demux, "cannot seek so far !" );
422 /* seek without index or without date */
423 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
425 if( p_sys->f_duration >= 0 && p_segment->b_cues )
427 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
431 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
433 msg_Dbg( p_demux, "inaccurate way of seeking for pos:%"PRId64, i_pos );
434 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
436 if( p_segment->b_cues && p_segment->p_indexes[i_index].i_position < i_pos )
438 if( !p_segment->b_cues && p_segment->p_indexes[i_index].i_position >= i_pos && p_segment->p_indexes[i_index].i_time > 0 )
441 if( i_index == p_segment->i_index )
446 i_date = p_segment->p_indexes[i_index].i_time;
448 if( !p_segment->b_cues && ( p_segment->p_indexes[i_index].i_position < i_pos || p_segment->p_indexes[i_index].i_position - i_pos > 2000000 ))
450 msg_Dbg( p_demux, "no cues, seek request to global pos: %"PRId64, i_pos );
451 i_global_position = i_pos;
456 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter, i_global_position );
459 /* Utility function for BlockDecode */
460 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
463 if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
464 memcpy( p_block->p_buffer + offset, p_mem, i_mem );
465 //p_block->i_rate = p_input->stream.control.i_rate;
469 /* Needed by matroska_segment::Seek() */
470 static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
471 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
473 demux_sys_t *p_sys = p_demux->p_sys;
474 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
480 if( p_segment->BlockFindTrackIndex( &i_track, block, simpleblock ) )
482 msg_Err( p_demux, "invalid track number" );
486 mkv_track_t *tk = p_segment->tracks[i_track];
488 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
490 msg_Err( p_demux, "unknown track number" );
493 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
495 return; /* discard audio packets that shouldn't be rendered */
498 if ( tk->fmt.i_cat != NAV_ES )
500 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
504 tk->b_inited = false;
510 /* First send init data */
511 if( !tk->b_inited && tk->i_data_init > 0 )
515 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
516 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
517 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
523 (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
528 if( simpleblock != NULL )
530 data = &simpleblock->GetBuffer(i);
531 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
532 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
536 data = &block->GetBuffer(i);
539 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
540 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
542 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
544 if( p_block == NULL )
549 #if defined(HAVE_ZLIB_H)
550 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
552 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
556 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
558 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
561 if ( tk->fmt.i_cat == NAV_ES )
563 // TODO handle the start/stop times of this packet
564 if ( p_sys->b_ui_hooked )
566 vlc_mutex_lock( &p_sys->p_ev->lock );
567 memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
568 p_sys->SwapButtons();
569 p_sys->b_pci_packet_set = true;
570 vlc_mutex_unlock( &p_sys->p_ev->lock );
571 block_Release( p_block );
575 // correct timestamping when B frames are used
576 if( tk->fmt.i_cat != VIDEO_ES )
578 p_block->i_dts = p_block->i_pts = i_pts;
582 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
584 // in VFW we have no idea about B frames
586 p_block->i_dts = i_pts;
590 p_block->i_pts = i_pts;
592 p_block->i_dts = p_block->i_pts;
594 p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
595 p_sys->i_pts = p_block->i_dts;
598 tk->i_last_dts = p_block->i_dts;
601 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
603 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
605 p_block->i_length = i_duration * 1000;
608 es_out_Send( p_demux->out, tk->p_es, p_block );
610 /* use time stamp only for first block */
616 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
619 KaxSimpleBlock *simpleblock;
620 int i_track_skipping;
621 int64_t i_block_duration;
622 int64_t i_block_ref1;
623 int64_t i_block_ref2;
625 int64_t i_seek_position = i_start_pos;
626 int64_t i_seek_time = i_start_time;
628 if( i_global_position >= 0 )
630 /* Special case for seeking in files with no cues */
631 EbmlElement *el = NULL;
632 es.I_O().setFilePointer( i_start_pos, seek_beginning );
634 ep = new EbmlParser( &es, segment, &sys.demuxer );
637 while( ( el = ep->Get() ) != NULL )
639 if( MKV_IS_ID( el, KaxCluster ) )
641 cluster = (KaxCluster *)el;
642 i_cluster_pos = cluster->GetElementPosition();
644 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
646 IndexAppendCluster( cluster );
648 if( es.I_O().getFilePointer() >= i_global_position )
651 msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
652 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
657 msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
665 for( ; i_idx < i_index; i_idx++ )
667 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
678 i_seek_position = p_indexes[i_idx].i_position;
679 i_seek_time = p_indexes[i_idx].i_time;
682 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
683 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
685 es.I_O().setFilePointer( i_seek_position, seek_beginning );
688 ep = new EbmlParser( &es, segment, &sys.demuxer );
691 sys.i_start_pts = i_date;
693 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
695 /* now parse until key frame */
696 i_track_skipping = 0;
697 for( i_track = 0; i_track < tracks.size(); i_track++ )
699 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
701 tracks[i_track]->b_search_keyframe = true;
705 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
707 while( i_track_skipping > 0 )
709 if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
711 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
717 for( i_track = 0; i_track < tracks.size(); i_track++ )
719 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
720 (block && tracks[i_track]->i_number == block->TrackNum()) )
727 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
729 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
731 if( i_track < tracks.size() )
733 if( sys.i_pts >= sys.i_start_pts )
735 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
736 i_track_skipping = 0;
738 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
740 if( i_block_ref1 == 0 && tracks[i_track]->b_search_keyframe )
742 tracks[i_track]->b_search_keyframe = false;
745 if( !tracks[i_track]->b_search_keyframe )
747 //es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, sys.i_pts );
748 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
756 /* FIXME current ES_OUT_SET_NEXT_DISPLAY_TIME does not work that well if
757 * the delay is too high. */
758 if( sys.i_pts + 500*1000 < sys.i_start_pts )
760 sys.i_start_pts = sys.i_pts;
762 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, sys.i_start_pts );
767 /*****************************************************************************
768 * Demux: reads and demuxes data packets
769 *****************************************************************************
770 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
771 *****************************************************************************/
772 static int Demux( demux_t *p_demux)
774 demux_sys_t *p_sys = p_demux->p_sys;
776 vlc_mutex_lock( &p_sys->lock_demuxer );
778 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
779 matroska_segment_c *p_segment = p_vsegment->Segment();
780 if ( p_segment == NULL ) return 0;
781 int i_block_count = 0;
786 if ( p_sys->demuxer.b_die )
789 if( p_sys->i_pts >= p_sys->i_start_pts )
790 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
796 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
798 /* nothing left to read in this ordered edition */
799 if ( !p_vsegment->SelectNext() )
801 p_segment->UnSelect( );
803 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
805 /* switch to the next segment */
806 p_segment = p_vsegment->Segment();
807 if ( !p_segment->Select( 0 ) )
809 msg_Err( p_demux, "Failed to select new segment" );
816 KaxSimpleBlock *simpleblock;
817 int64_t i_block_duration = 0;
818 int64_t i_block_ref1;
819 int64_t i_block_ref2;
821 if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
823 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
825 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
826 // check if there are more chapters to read
827 if ( p_chap != NULL )
829 /* TODO handle successive chapters with the same user_start_time/user_end_time
830 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
831 p_vsegment->SelectNext();
833 p_sys->i_pts = p_chap->i_user_end_time;
834 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
843 msg_Warn( p_demux, "cannot get block EOF?" );
844 p_segment->UnSelect( );
846 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
848 /* switch to the next segment */
849 if ( !p_vsegment->SelectNext() )
850 // no more segments in this stream
852 p_segment = p_vsegment->Segment();
853 if ( !p_segment->Select( 0 ) )
855 msg_Err( p_demux, "Failed to select new segment" );
863 if( simpleblock != NULL )
864 p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
866 p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
868 if( p_sys->i_pts >= p_sys->i_start_pts )
870 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
872 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
880 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
882 /* nothing left to read in this ordered edition */
883 if ( !p_vsegment->SelectNext() )
888 p_segment->UnSelect( );
890 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
892 /* switch to the next segment */
893 p_segment = p_vsegment->Segment();
894 if ( !p_segment->Select( 0 ) )
896 msg_Err( p_demux, "Failed to select new segment" );
904 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
909 // TODO optimize when there is need to leave or when seeking has been called
910 if( i_block_count > 5 )
917 vlc_mutex_unlock( &p_sys->lock_demuxer );