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"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
46 set_shortname( "Matroska" )
47 set_description( N_("Matroska stream demuxer" ) )
48 set_capability( "demux", 50 )
49 set_callbacks( Open, Close )
50 set_category( CAT_INPUT )
51 set_subcategory( SUBCAT_INPUT_DEMUX )
53 add_bool( "mkv-use-ordered-chapters", true,
54 N_("Respect ordered chapters"),
55 N_("Play chapters in the order specified in the segment."), false );
57 add_bool( "mkv-use-chapter-codec", true,
59 N_("Use chapter codecs found in the segment."), true );
61 add_bool( "mkv-preload-local-dir", true,
62 N_("Preload MKV files in the same directory"),
63 N_("Preload matroska files in the same directory to find linked segments (not good for broken files)."), false );
65 add_bool( "mkv-seek-percent", false,
66 N_("Seek based on percent not time"),
67 N_("Seek based on percent not time."), true );
69 add_bool( "mkv-use-dummy", false,
71 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
73 add_shortcut( "mka", "mkv" )
78 static int Demux ( demux_t * );
79 static int Control( demux_t *, int, va_list );
80 static void Seek ( demux_t *, mtime_t i_date, double f_percent, virtual_chapter_c *p_chapter );
82 /*****************************************************************************
83 * Open: initializes matroska demux structures
84 *****************************************************************************/
85 static int Open( vlc_object_t * p_this )
87 demux_t *p_demux = (demux_t*)p_this;
89 matroska_stream_c *p_stream;
90 matroska_segment_c *p_segment;
91 const uint8_t *p_peek;
92 std::string s_path, s_filename;
93 vlc_stream_io_callback *p_io_callback;
94 EbmlStream *p_io_stream;
95 bool b_need_preload = false;
97 /* peek the begining */
98 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
100 /* is a valid file */
101 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
102 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
104 /* Set the demux function */
105 p_demux->pf_demux = Demux;
106 p_demux->pf_control = Control;
107 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
109 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
110 p_io_stream = new EbmlStream( *p_io_callback );
112 if( p_io_stream == NULL )
114 msg_Err( p_demux, "failed to create EbmlStream" );
115 delete p_io_callback;
120 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
121 if( p_stream == NULL )
123 msg_Err( p_demux, "cannot find KaxSegment or missing mandatory KaxInfo" );
126 p_sys->streams.push_back( p_stream );
128 p_stream->p_io_callback = p_io_callback;
129 p_stream->p_estream = p_io_stream;
131 for (size_t i=0; i<p_stream->segments.size(); i++)
133 p_stream->segments[i]->Preload();
134 b_need_preload |= p_stream->segments[i]->b_ref_external_segments;
137 p_segment = p_stream->segments[0];
138 if( p_segment->cluster == NULL )
140 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
144 if (b_need_preload && var_InheritBool( p_demux, "mkv-preload-local-dir" ))
146 msg_Dbg( p_demux, "Preloading local dir" );
147 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
148 if ( p_demux->psz_file && !strcmp( p_demux->psz_access, "file" ) )
150 // assume it's a regular file
151 // get the directory path
152 s_path = p_demux->psz_file;
153 if (s_path.at(s_path.length() - 1) == DIR_SEP_CHAR)
155 s_path = s_path.substr(0,s_path.length()-1);
159 if (s_path.find_last_of(DIR_SEP_CHAR) > 0)
161 s_path = s_path.substr(0,s_path.find_last_of(DIR_SEP_CHAR));
165 DIR *p_src_dir = vlc_opendir(s_path.c_str());
167 if (p_src_dir != NULL)
170 while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
172 if (strlen(psz_file) > 4)
174 s_filename = s_path + DIR_SEP_CHAR + psz_file;
176 #if defined(WIN32) || defined(__OS2__)
177 if (!strcasecmp(s_filename.c_str(), p_demux->psz_file))
179 if (!s_filename.compare(p_demux->psz_file))
183 continue; // don't reuse the original opened file
186 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
187 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
189 // test whether this file belongs to our family
190 const uint8_t *p_peek;
191 bool file_ok = false;
192 #warning Memory leak!
193 std::string s_url = vlc_path2uri( s_filename.c_str(), "file" );
194 stream_t *p_file_stream = stream_UrlNew(
197 /* peek the begining */
199 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
200 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
201 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
205 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
206 EbmlStream *p_estream = new EbmlStream(*p_file_io);
208 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
210 if ( p_stream == NULL )
212 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
218 p_stream->p_io_callback = p_file_io;
219 p_stream->p_estream = p_estream;
220 p_sys->streams.push_back( p_stream );
225 if( p_file_stream ) {
226 stream_Delete( p_file_stream );
228 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
234 closedir( p_src_dir );
238 p_sys->PreloadFamily( *p_segment );
240 else if (b_need_preload)
241 msg_Warn( p_demux, "This file references other files, you may want to enable the preload of local directory");
243 if ( !p_sys->PreloadLinked() ||
244 !p_sys->PreparePlayback( NULL ) )
246 msg_Err( p_demux, "cannot use the segment" );
261 /*****************************************************************************
262 * Close: frees unused data
263 *****************************************************************************/
264 static void Close( vlc_object_t *p_this )
266 demux_t *p_demux = (demux_t*)p_this;
267 demux_sys_t *p_sys = p_demux->p_sys;
268 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
271 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
273 p_segment->UnSelect();
279 /*****************************************************************************
281 *****************************************************************************/
282 static int Control( demux_t *p_demux, int i_query, va_list args )
284 demux_sys_t *p_sys = p_demux->p_sys;
291 input_attachment_t ***ppp_attach;
296 case DEMUX_GET_ATTACHMENTS:
297 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
298 pi_int = (int*)va_arg( args, int * );
300 if( p_sys->stored_attachments.size() <= 0 )
303 *pi_int = p_sys->stored_attachments.size();
304 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
305 p_sys->stored_attachments.size() );
308 for( size_t i = 0; i < p_sys->stored_attachments.size(); i++ )
310 attachment_c *a = p_sys->stored_attachments[i];
311 (*ppp_attach)[i] = vlc_input_attachment_New( a->fileName(), a->mimeType(), NULL,
312 a->p_data, a->size() );
317 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
318 vlc_meta_Merge( p_meta, p_sys->meta );
321 case DEMUX_GET_LENGTH:
322 pi64 = (int64_t*)va_arg( args, int64_t * );
323 if( p_sys->f_duration > 0.0 )
325 *pi64 = (int64_t)(p_sys->f_duration * 1000);
330 case DEMUX_GET_POSITION:
331 pf = (double*)va_arg( args, double * );
332 if ( p_sys->f_duration > 0.0 )
333 *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);
336 case DEMUX_SET_POSITION:
337 if( p_sys->f_duration > 0.0 )
339 f = (double)va_arg( args, double );
340 Seek( p_demux, -1, f, NULL );
346 pi64 = (int64_t*)va_arg( args, int64_t * );
347 *pi64 = p_sys->i_pts;
350 case DEMUX_GET_TITLE_INFO:
351 if( p_sys->titles.size() > 1 || ( p_sys->titles.size() == 1 && p_sys->titles[0]->i_seekpoint > 0 ) )
353 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
354 int *pi_int = (int*)va_arg( args, int* );
356 *pi_int = p_sys->titles.size();
357 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
359 for( size_t i = 0; i < p_sys->titles.size(); i++ )
360 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
365 case DEMUX_SET_TITLE:
366 /* handle editions as titles */
367 i_idx = (int)va_arg( args, int );
368 if(i_idx < p_sys->titles.size() && p_sys->titles[i_idx]->i_seekpoint)
370 p_sys->p_current_segment->i_current_edition = i_idx;
371 p_sys->i_current_title = i_idx;
372 p_sys->p_current_segment->p_current_chapter = p_sys->p_current_segment->editions[p_sys->p_current_segment->i_current_edition]->getChapterbyTimecode(0);
374 Seek( p_demux, (int64_t)p_sys->titles[i_idx]->seekpoint[0]->i_time_offset, -1, NULL);
375 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
376 p_demux->info.i_seekpoint = 0;
377 p_sys->f_duration = (float) p_sys->titles[i_idx]->i_length / 1000.f;
382 case DEMUX_SET_SEEKPOINT:
383 i_skp = (int)va_arg( args, int );
385 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
386 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
388 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
389 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
390 p_demux->info.i_seekpoint = i_skp;
396 pf = (double *)va_arg( args, double * );
398 if( p_sys->p_current_segment && p_sys->p_current_segment->CurrentSegment() )
400 const matroska_segment_c *p_segment = p_sys->p_current_segment->CurrentSegment();
401 for( size_t i = 0; i < p_segment->tracks.size(); i++ )
403 mkv_track_t *tk = p_segment->tracks[i];
404 if( tk->fmt.i_cat == VIDEO_ES && tk->fmt.video.i_frame_rate_base > 0 )
406 *pf = (double)tk->fmt.video.i_frame_rate / tk->fmt.video.i_frame_rate_base;
414 i64 = (int64_t) va_arg( args, int64_t );
415 msg_Dbg(p_demux,"SET_TIME to %"PRId64, i64 );
416 Seek( p_demux, i64, -1, NULL );
424 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, virtual_chapter_c *p_chapter )
426 demux_sys_t *p_sys = p_demux->p_sys;
427 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
428 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
429 mtime_t i_time_offset = 0;
430 int64_t i_global_position = -1;
434 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
435 if( i_date < 0 && f_percent < 0 )
437 msg_Warn( p_demux, "cannot seek nowhere!" );
440 if( f_percent > 1.0 )
442 msg_Warn( p_demux, "cannot seek so far!" );
445 if( p_sys->f_duration < 0 )
447 msg_Warn( p_demux, "cannot seek without duration!");
451 /* seek without index or without date */
452 if( f_percent >= 0 && (var_InheritBool( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
454 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
455 if( !p_segment->b_cues )
457 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
459 msg_Dbg( p_demux, "lengthy way of seeking for pos:%"PRId64, i_pos );
460 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
462 if( p_segment->p_indexes[i_index].i_position >= i_pos &&
463 p_segment->p_indexes[i_index].i_time > 0 )
466 if( i_index == p_segment->i_index )
469 if( p_segment->p_indexes[i_index].i_position < i_pos )
471 msg_Dbg( p_demux, "no cues, seek request to global pos: %"PRId64, i_pos );
472 i_global_position = i_pos;
476 p_vsegment->Seek( *p_demux, i_date, i_time_offset, p_chapter, i_global_position );
479 /* Needed by matroska_segment::Seek() and Seek */
480 void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
481 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
483 demux_sys_t *p_sys = p_demux->p_sys;
484 matroska_segment_c *p_segment = p_sys->p_current_segment->CurrentSegment();
486 if( !p_segment ) return;
489 if( p_segment->BlockFindTrackIndex( &i_track, block, simpleblock ) )
491 msg_Err( p_demux, "invalid track number" );
495 mkv_track_t *tk = p_segment->tracks[i_track];
497 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
499 msg_Err( p_demux, "unknown track number" );
502 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
504 return; /* discard audio packets that shouldn't be rendered */
507 if ( tk->fmt.i_cat != NAV_ES )
510 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
514 tk->b_inited = false;
515 if( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES )
516 tk->i_last_dts = i_pts;
522 /* First send init data */
523 if( !tk->b_inited && tk->i_data_init > 0 )
527 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
528 p_init = MemToBlock( tk->p_data_init, tk->i_data_init, 0 );
529 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
534 size_t frame_size = 0;
535 size_t block_size = 0;
537 if( simpleblock != NULL )
538 block_size = simpleblock->GetSize();
540 block_size = block->GetSize();
542 for( unsigned int i = 0;
543 ( block != NULL && i < block->NumberFrames()) || ( simpleblock != NULL && i < simpleblock->NumberFrames() );
548 if( simpleblock != NULL )
550 data = &simpleblock->GetBuffer(i);
551 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
552 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
556 data = &block->GetBuffer(i);
557 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
559 frame_size += data->Size();
560 if( !data->Buffer() || data->Size() > SIZE_MAX || frame_size > block_size )
562 msg_Warn( p_demux, "Cannot read frame (too long or no frame)" );
566 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
567 tk->p_compression_data != NULL &&
568 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
569 p_block = MemToBlock( data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
571 p_block = MemToBlock( data->Buffer(), data->Size(), 0 );
573 if( p_block == NULL )
578 #if defined(HAVE_ZLIB_H)
579 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
580 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
582 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
583 if( p_block == NULL )
588 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
589 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
591 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
594 if( tk->fmt.i_codec == VLC_CODEC_COOK ||
595 tk->fmt.i_codec == VLC_CODEC_ATRAC3 )
597 handle_real_audio(p_demux, tk, p_block, i_pts);
598 block_Release(p_block);
599 i_pts = ( tk->i_default_duration )?
600 i_pts + ( mtime_t )( tk->i_default_duration / 1000 ):
605 if ( tk->fmt.i_cat == NAV_ES )
607 // TODO handle the start/stop times of this packet
608 p_sys->p_ev->SetPci( (const pci_t *)&p_block->p_buffer[1]);
609 block_Release( p_block );
612 // correct timestamping when B frames are used
613 if( tk->fmt.i_cat != VIDEO_ES )
615 p_block->i_dts = p_block->i_pts = i_pts;
621 p_block->i_pts = VLC_TS_INVALID;
622 p_block->i_dts = i_pts;
624 else if( tk->b_pts_only )
626 p_block->i_pts = i_pts;
627 p_block->i_dts = i_pts;
631 p_block->i_pts = i_pts;
633 p_block->i_dts = p_block->i_pts;
635 p_block->i_dts = min( i_pts, tk->i_last_dts + ( mtime_t )( tk->i_default_duration / 1000 ) );
638 if( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES )
639 tk->i_last_dts = p_block->i_dts;
642 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
644 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
646 p_block->i_length = i_duration * tk-> f_timecodescale * (double) p_segment->i_timescale / 1000.0;
649 /* FIXME remove when VLC_TS_INVALID work is done */
650 if( i == 0 || p_block->i_dts > VLC_TS_INVALID )
651 p_block->i_dts += VLC_TS_0;
652 if( !tk->b_dts_only && ( i == 0 || p_block->i_pts > VLC_TS_INVALID ) )
653 p_block->i_pts += VLC_TS_0;
655 es_out_Send( p_demux->out, tk->p_es, p_block );
657 /* use time stamp only for first block */
658 i_pts = ( tk->i_default_duration )?
659 i_pts + ( mtime_t )( tk->i_default_duration / 1000 ):
664 /*****************************************************************************
665 * Demux: reads and demuxes data packets
666 *****************************************************************************
667 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
668 *****************************************************************************/
669 static int Demux( demux_t *p_demux)
671 demux_sys_t *p_sys = p_demux->p_sys;
673 vlc_mutex_lock( &p_sys->lock_demuxer );
675 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
676 matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
677 if ( p_segment == NULL ) return 0;
678 int i_block_count = 0;
683 if( p_sys->i_pts >= p_sys->i_start_pts )
684 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
690 if ( p_vsegment->CurrentEdition() &&
691 p_vsegment->CurrentEdition()->b_ordered &&
692 p_vsegment->CurrentChapter() == NULL )
693 /* nothing left to read in this ordered edition */
697 KaxSimpleBlock *simpleblock;
698 int64_t i_block_duration = 0;
700 bool b_discardable_picture;
701 if( p_segment->BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
703 if ( p_vsegment->CurrentEdition() && p_vsegment->CurrentEdition()->b_ordered )
705 const virtual_chapter_c *p_chap = p_vsegment->CurrentChapter();
706 // check if there are more chapters to read
707 if ( p_chap != NULL )
709 /* TODO handle successive chapters with the same user_start_time/user_end_time
711 p_sys->i_pts = p_chap->i_virtual_stop_time;
712 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
721 msg_Warn( p_demux, "cannot get block EOF?" );
726 if( simpleblock != NULL )
727 p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)simpleblock->GlobalTimecode() / INT64_C(1000) );
729 p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)block->GlobalTimecode() / INT64_C(1000) );
731 mtime_t i_pcr = VLC_TS_INVALID;
732 for( size_t i = 0; i < p_segment->tracks.size(); i++)
733 if( p_segment->tracks[i]->i_last_dts > VLC_TS_INVALID &&
734 ( p_segment->tracks[i]->i_last_dts < i_pcr || i_pcr == VLC_TS_INVALID ))
735 i_pcr = p_segment->tracks[i]->i_last_dts;
737 if( i_pcr > p_sys->i_pcr + 300000 )
739 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
740 p_sys->i_pcr = i_pcr;
743 if( p_sys->i_pts >= p_sys->i_start_pts )
745 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
753 if ( p_vsegment->CurrentEdition() &&
754 p_vsegment->CurrentEdition()->b_ordered &&
755 p_vsegment->CurrentChapter() == NULL )
757 /* nothing left to read in this ordered edition */
762 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, b_key_picture || b_discardable_picture );
767 // TODO optimize when there is need to leave or when seeking has been called
768 if( i_block_count > 5 )
775 vlc_mutex_unlock( &p_sys->lock_demuxer );