/*****************************************************************************
* mkv.cpp : matroska demuxer
*****************************************************************************
- * Copyright (C) 2003-2005, 2008, 2010 the VideoLAN team
+ * Copyright (C) 2003-2005, 2008, 2010 VLC authors and VideoLAN
* $Id$
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
* Steve Lhomme <steve.lhomme@free.fr>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "mkv.hpp"
#include "stream_io_callback.hpp"
+extern "C" {
+#include "../../modules/codec/dts_header.h"
+}
+
#include <vlc_fs.h>
#include <vlc_url.h>
add_shortcut( "mka", "mkv" )
vlc_module_end ()
-class demux_sys_t;
+struct demux_sys_t;
static int Demux ( demux_t * );
static int Control( demux_t *, int, va_list );
if (p_src_dir != NULL)
{
- char *psz_file;
+ const char *psz_file;
while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
{
if (strlen(psz_file) > 4)
{
s_filename = s_path + DIR_SEP_CHAR + psz_file;
-#ifdef WIN32
+#if defined(_WIN32) || defined(__OS2__)
if (!strcasecmp(s_filename.c_str(), p_demux->psz_file))
#else
if (!s_filename.compare(p_demux->psz_file))
#endif
{
- free (psz_file);
continue; // don't reuse the original opened file
}
if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
!s_filename.compare(s_filename.length() - 3, 3, "mka"))
{
- // test wether this file belongs to our family
+ // test whether this file belongs to our family
const uint8_t *p_peek;
bool file_ok = false;
- std::string s_url = make_URI( s_filename.c_str(), "file" );
+#warning Memory leak!
+ std::string s_url = vlc_path2uri( s_filename.c_str(), "file" );
stream_t *p_file_stream = stream_UrlNew(
p_demux,
s_url.c_str() );
}
}
}
- free (psz_file);
}
closedir( p_src_dir );
}
goto error;
}
+ p_sys->FreeUnused();
+
p_sys->InitUi();
return VLC_SUCCESS;
return VLC_EGENERIC;
*pi_int = p_sys->stored_attachments.size();
- *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
+ *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t*) *
p_sys->stored_attachments.size() );
if( !(*ppp_attach) )
return VLC_ENOMEM;
return VLC_SUCCESS;
case DEMUX_SET_POSITION:
- f = (double)va_arg( args, double );
- Seek( p_demux, -1, f, NULL );
- return VLC_SUCCESS;
+ if( p_sys->f_duration > 0.0 )
+ {
+ f = (double)va_arg( args, double );
+ Seek( p_demux, -1, f, NULL );
+ return VLC_SUCCESS;
+ }
+ return VLC_EGENERIC;
case DEMUX_GET_TIME:
pi64 = (int64_t*)va_arg( args, int64_t * );
int *pi_int = (int*)va_arg( args, int* );
*pi_int = p_sys->titles.size();
- *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
+ *ppp_title = (input_title_t**)malloc( sizeof( input_title_t* ) * p_sys->titles.size() );
for( size_t i = 0; i < p_sys->titles.size(); i++ )
- {
(*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
- }
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_SET_TITLE:
- /* TODO handle editions as titles */
+ /* handle editions as titles */
i_idx = (int)va_arg( args, int );
- if( i_idx < p_sys->used_segments.size() )
+ if(i_idx < p_sys->titles.size() && p_sys->titles[i_idx]->i_seekpoint)
{
- p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
+ p_sys->p_current_segment->i_current_edition = i_idx;
+ p_sys->i_current_title = i_idx;
+ p_sys->p_current_segment->p_current_chapter = p_sys->p_current_segment->editions[p_sys->p_current_segment->i_current_edition]->getChapterbyTimecode(0);
+
+ Seek( p_demux, (int64_t)p_sys->titles[i_idx]->seekpoint[0]->i_time_offset, -1, NULL);
+ p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_TITLE;
+ p_demux->info.i_seekpoint = 0;
+ p_demux->info.i_title = i_idx;
+ p_sys->f_duration = (float) p_sys->titles[i_idx]->i_length / 1000.f;
return VLC_SUCCESS;
}
return VLC_EGENERIC;
if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
{
Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
- p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
+ p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
p_demux->info.i_seekpoint = i_skp;
return VLC_SUCCESS;
}
case DEMUX_SET_TIME:
i64 = (int64_t) va_arg( args, int64_t );
- msg_Dbg(p_demux,"SET_TIME to %"PRId64, i64 );
+ msg_Dbg(p_demux,"SET_TIME to %" PRId64, i64 );
Seek( p_demux, i64, -1, NULL );
return VLC_SUCCESS;
default:
int i_index;
- msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
+ msg_Dbg( p_demux, "seek request to %" PRId64 " (%f%%)", i_date, f_percent );
if( i_date < 0 && f_percent < 0 )
{
msg_Warn( p_demux, "cannot seek nowhere!" );
msg_Warn( p_demux, "cannot seek without duration!");
return;
}
+ if( !p_segment )
+ {
+ msg_Warn( p_demux, "cannot seek without valid segment position");
+ return;
+ }
/* seek without index or without date */
if( f_percent >= 0 && (var_InheritBool( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
{
int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
- msg_Dbg( p_demux, "lengthy way of seeking for pos:%"PRId64, i_pos );
+ msg_Dbg( p_demux, "lengthy way of seeking for pos:%" PRId64, i_pos );
for( i_index = 0; i_index < p_segment->i_index; i_index++ )
{
if( p_segment->p_indexes[i_index].i_position >= i_pos &&
if( p_segment->p_indexes[i_index].i_position < i_pos )
{
- msg_Dbg( p_demux, "no cues, seek request to global pos: %"PRId64, i_pos );
+ msg_Dbg( p_demux, "no cues, seek request to global pos: %" PRId64, i_pos );
i_global_position = i_pos;
}
}
p_vsegment->Seek( *p_demux, i_date, i_time_offset, p_chapter, i_global_position );
}
-/* Utility function for BlockDecode */
-static block_t *MemToBlock( uint8_t *p_mem, size_t i_mem, size_t offset)
-{
- if( unlikely( i_mem > SIZE_MAX - offset ) )
- return NULL;
-
- block_t *p_block = block_New( p_demux, i_mem + offset );
- if( likely(p_block != NULL) )
- {
- memcpy( p_block->p_buffer + offset, p_mem, i_mem );
- }
- return p_block;
-}
-
/* Needed by matroska_segment::Seek() and Seek */
void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
msg_Err( p_demux, "unknown track number" );
return;
}
- if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
- {
- return; /* discard audio packets that shouldn't be rendered */
- }
+
+ i_pts -= tk->i_codec_delay;
if ( tk->fmt.i_cat != NAV_ES )
{
if( !b )
{
tk->b_inited = false;
+ if( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES )
+ tk->i_last_dts = i_pts;
return;
}
}
else
block_size = block->GetSize();
- for( unsigned int i = 0;
- ( block != NULL && i < block->NumberFrames()) || ( simpleblock != NULL && i < simpleblock->NumberFrames() );
- i++ )
+ const unsigned int i_number_frames = block != NULL ? block->NumberFrames() :
+ ( simpleblock != NULL ? simpleblock->NumberFrames() : 0 );
+ for( unsigned int i_frame = 0; i_frame < i_number_frames; i_frame++ )
{
block_t *p_block;
DataBuffer *data;
if( simpleblock != NULL )
{
- data = &simpleblock->GetBuffer(i);
+ data = &simpleblock->GetBuffer(i_frame);
// condition when the DTS is correct (keyframe or B frame == NOT P frame)
f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
}
else
{
- data = &block->GetBuffer(i);
+ data = &block->GetBuffer(i_frame);
// condition when the DTS is correct (keyframe or B frame == NOT P frame)
}
frame_size += data->Size();
- if( !data->Buffer() || data->Size() > SIZE_MAX || frame_size > block_size )
+ if( !data->Buffer() || data->Size() > frame_size || frame_size > block_size )
{
msg_Warn( p_demux, "Cannot read frame (too long or no frame)" );
break;
}
- if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
+ if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
+ tk->p_compression_data != NULL &&
+ tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
p_block = MemToBlock( data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
+ else if( unlikely( tk->fmt.i_codec == VLC_CODEC_WAVPACK ) )
+ p_block = packetize_wavpack(tk, data->Buffer(), data->Size());
else
p_block = MemToBlock( data->Buffer(), data->Size(), 0 );
}
#if defined(HAVE_ZLIB_H)
- if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
+ if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
+ tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
{
p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
if( p_block == NULL )
}
else
#endif
- if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
+ if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
+ tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
{
memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
}
-
- if ( tk->fmt.i_cat == NAV_ES )
+ switch( tk->fmt.i_codec )
{
- // TODO handle the start/stop times of this packet
- p_sys->p_ev->SetPci( (const pci_t *)&p_block->p_buffer[1]);
- block_Release( p_block );
- return;
+ case VLC_CODEC_COOK:
+ case VLC_CODEC_ATRAC3:
+ {
+ handle_real_audio(p_demux, tk, p_block, i_pts);
+ block_Release(p_block);
+ i_pts = ( tk->i_default_duration )?
+ i_pts + ( mtime_t )( tk->i_default_duration / 1000 ):
+ VLC_TS_INVALID;
+ continue;
+ }
+
+ case VLC_CODEC_DTS:
+ /* Check if packetization is correct and without padding.
+ * example: Test_mkv_div3_DTS_1920x1080_1785Kbps_23,97fps.mkv */
+ if( p_block->i_buffer > 6 )
+ {
+ unsigned int a, b, c, d;
+ bool e;
+ int i_frame_size = GetSyncInfo( p_block->p_buffer, &e, &a, &b, &c, &d );
+ if( i_frame_size > 0 )
+ p_block->i_buffer = __MIN(p_block->i_buffer, (size_t)i_frame_size);
+ }
+ break;
+
+ case VLC_CODEC_OPUS:
+ mtime_t i_length = i_duration * tk-> f_timecodescale *
+ (double) p_segment->i_timescale / 1000.0;
+ if ( i_length < 0 ) i_length = 0;
+ p_block->i_nb_samples = i_length * tk->fmt.audio.i_rate
+ / CLOCK_FREQ;
+ break;
}
- // correct timestamping when B frames are used
+
+
if( tk->fmt.i_cat != VIDEO_ES )
{
+ if ( tk->fmt.i_cat == NAV_ES )
+ {
+ // TODO handle the start/stop times of this packet
+ p_sys->p_ev->SetPci( (const pci_t *)&p_block->p_buffer[1]);
+ block_Release( p_block );
+ return;
+ }
+ else if( tk->fmt.i_cat == AUDIO_ES )
+ {
+ if( tk->i_chans_to_reorder )
+ aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
+ tk->fmt.audio.i_channels,
+ tk->pi_chan_table, tk->fmt.i_codec );
+
+ }
p_block->i_dts = p_block->i_pts = i_pts;
}
else
{
+ // correct timestamping when B frames are used
if( tk->b_dts_only )
{
p_block->i_pts = VLC_TS_INVALID;
if ( f_mandatory )
p_block->i_dts = p_block->i_pts;
else
- p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
+ p_block->i_dts = min( i_pts, tk->i_last_dts + ( mtime_t )( tk->i_default_duration / 1000 ) );
}
}
- tk->i_last_dts = p_block->i_dts;
+ if( p_block->i_dts > VLC_TS_INVALID &&
+ ( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES ) )
+ {
+ tk->i_last_dts = p_block->i_dts;
+ }
#if 0
-msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
+msg_Dbg( p_demux, "block (track=%d) i_dts: %"PRId64" / i_pts: %"PRId64, tk->i_number, p_block->i_dts, p_block->i_pts);
#endif
- if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
+ if( !tk->b_no_duration )
{
- p_block->i_length = i_duration * 1000;
+ p_block->i_length = i_duration * tk-> f_timecodescale *
+ (double) p_segment->i_timescale / ( 1000.0 * i_number_frames );
}
/* FIXME remove when VLC_TS_INVALID work is done */
- if( i == 0 || p_block->i_dts > VLC_TS_INVALID )
+ if( i_frame == 0 || p_block->i_dts > VLC_TS_INVALID )
p_block->i_dts += VLC_TS_0;
- if( !tk->b_dts_only && ( i == 0 || p_block->i_pts > VLC_TS_INVALID ) )
+ if( !tk->b_dts_only && ( i_frame == 0 || p_block->i_pts > VLC_TS_INVALID ) )
p_block->i_pts += VLC_TS_0;
es_out_Send( p_demux->out, tk->p_es, p_block );
/* use time stamp only for first block */
- i_pts = VLC_TS_INVALID;
+ i_pts = ( tk->i_default_duration )?
+ i_pts + ( mtime_t )( tk->i_default_duration / 1000 ):
+ VLC_TS_INVALID;
}
}
virtual_segment_c *p_vsegment = p_sys->p_current_segment;
matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
- if ( p_segment == NULL ) return 0;
- int i_block_count = 0;
- int i_return = 0;
-
- for( ;; )
+ if ( p_segment == NULL )
{
- if ( p_sys->demuxer.b_die )
- break;
+ vlc_mutex_unlock( &p_sys->lock_demuxer );
+ return 0;
+ }
+ int i_return = 0;
+ do
+ {
if( p_sys->i_pts >= p_sys->i_start_pts )
if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
{
else
{
msg_Warn( p_demux, "cannot get block EOF?" );
- es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
break;
}
}
if( simpleblock != NULL )
- p_sys->i_pts = p_sys->i_chapter_time + ( simpleblock->GlobalTimecode() / (mtime_t) 1000 );
+ p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)simpleblock->GlobalTimecode() / INT64_C(1000) );
else
- p_sys->i_pts = p_sys->i_chapter_time + ( block->GlobalTimecode() / (mtime_t) 1000 );
+ p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)block->GlobalTimecode() / INT64_C(1000) );
- /* The blocks are in coding order so we can safely consider that only references are in chronological order */
- if( simpleblock == NULL || b_key_picture )
- es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pts );
+ mtime_t i_pcr = VLC_TS_INVALID;
+ for( size_t i = 0; i < p_segment->tracks.size(); i++)
+ if( p_segment->tracks[i]->i_last_dts > VLC_TS_INVALID &&
+ ( p_segment->tracks[i]->i_last_dts < i_pcr || i_pcr == VLC_TS_INVALID ))
+ i_pcr = p_segment->tracks[i]->i_last_dts;
+
+ if( i_pcr > p_sys->i_pcr + 300000 )
+ {
+ es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
+ p_sys->i_pcr = i_pcr;
+ }
if( p_sys->i_pts >= p_sys->i_start_pts )
{
BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, b_key_picture || b_discardable_picture );
delete block;
- i_block_count++;
- // TODO optimize when there is need to leave or when seeking has been called
- if( i_block_count > 5 )
- {
- i_return = 1;
- break;
- }
+ vlc_mutex_unlock( &p_sys->lock_demuxer );
+ return 1;
}
+ while (0);
vlc_mutex_unlock( &p_sys->lock_demuxer );