mkv.cpp 78.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * mkv.cpp : matroska demuxer
 *****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
4
 * Copyright (C) 2003-2004 VideoLAN
5
 * $Id: mkv.cpp,v 1.59 2004/03/03 12:03:15 fenrir Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.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
 * (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.
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>

31 32 33 34
#ifdef HAVE_TIME_H
#   include <time.h>                                               /* time() */
#endif

35 36 37
#include <vlc/input.h>

#include <codecs.h>                        /* BITMAPINFOHEADER, WAVEFORMATEX */
38
#include "iso_lang.h"
39
#include "vlc_meta.h"
40 41 42 43 44

#include <iostream>
#include <cassert>
#include <typeinfo>

45 46 47
#ifdef HAVE_WCHAR_H
#   include <wchar.h>
#endif
48

49 50 51 52 53 54 55 56 57
/* libebml and matroska */
#include "ebml/EbmlHead.h"
#include "ebml/EbmlSubHead.h"
#include "ebml/EbmlStream.h"
#include "ebml/EbmlContexts.h"
#include "ebml/EbmlVersion.h"
#include "ebml/EbmlVoid.h"

#include "matroska/FileKax.h"
58 59 60
#ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
#include "matroska/KaxAttachments.h"
#else
61
#include "matroska/KaxAttachements.h"
62
#endif
63 64 65 66 67 68 69 70 71 72 73 74 75
#include "matroska/KaxBlock.h"
#include "matroska/KaxBlockData.h"
#include "matroska/KaxChapters.h"
#include "matroska/KaxCluster.h"
#include "matroska/KaxClusterData.h"
#include "matroska/KaxContexts.h"
#include "matroska/KaxCues.h"
#include "matroska/KaxCuesData.h"
#include "matroska/KaxInfo.h"
#include "matroska/KaxInfoData.h"
#include "matroska/KaxSeekHead.h"
#include "matroska/KaxSegment.h"
#include "matroska/KaxTag.h"
76 77
#include "matroska/KaxTags.h"
#include "matroska/KaxTagMulti.h"
78 79 80
#include "matroska/KaxTracks.h"
#include "matroska/KaxTrackAudio.h"
#include "matroska/KaxTrackVideo.h"
81
#include "matroska/KaxTrackEntryData.h"
82 83 84 85 86 87 88 89 90

#include "ebml/StdIOCallback.h"

using namespace LIBMATROSKA_NAMESPACE;
using namespace std;

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
91 92 93
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

94
vlc_module_begin();
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
95
    set_description( _("Matroska stream demuxer" ) );
96
    set_capability( "demux", 50 );
Laurent Aimar's avatar
Laurent Aimar committed
97
    set_callbacks( Open, Close );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
98 99 100 101 102
    
    add_bool( "mkv-seek-percent", 1, NULL,
            N_("Seek based on percent not time"),
            N_("Seek based on percent not time"), VLC_TRUE );

103 104 105 106
    add_shortcut( "mka" );
    add_shortcut( "mkv" );
vlc_module_end();

Laurent Aimar's avatar
Laurent Aimar committed
107 108 109
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
110 111 112
static int  Demux  ( input_thread_t * );
static int  Control( input_thread_t *, int, va_list );
static void Seek   ( input_thread_t *, mtime_t i_date, int i_percent );
Laurent Aimar's avatar
Laurent Aimar committed
113

114 115 116 117 118 119 120

/*****************************************************************************
 * Stream managment
 *****************************************************************************/
class vlc_stream_io_callback: public IOCallback
{
  private:
Laurent Aimar's avatar
Laurent Aimar committed
121
    stream_t       *s;
122
    vlc_bool_t     mb_eof;
123 124

  public:
Laurent Aimar's avatar
Laurent Aimar committed
125
    vlc_stream_io_callback( stream_t * );
126 127 128 129 130 131 132 133

    virtual uint32_t read            ( void *p_buffer, size_t i_size);
    virtual void     setFilePointer  ( int64_t i_offset, seek_mode mode = seek_beginning );
    virtual size_t   write           ( const void *p_buffer, size_t i_size);
    virtual uint64_t getFilePointer  ( void );
    virtual void     close           ( void );
};

134 135 136 137
/*****************************************************************************
 * Ebml Stream parser
 *****************************************************************************/
class EbmlParser
138
{
139 140 141
  public:
    EbmlParser( EbmlStream *es, EbmlElement *el_start );
    ~EbmlParser( void );
142

143 144 145 146
    void Up( void );
    void Down( void );
    EbmlElement *Get( void );
    void        Keep( void );
147

148
    int GetLevel( void );
149

150 151 152 153
  private:
    EbmlStream  *m_es;
    int         mi_level;
    EbmlElement *m_el[6];
154

155
    EbmlElement *m_got;
156

157 158 159
    int         mi_user_level;
    vlc_bool_t  mb_keep;
};
160 161


162 163 164 165 166 167 168
/*****************************************************************************
 * Some functions to manipulate memory
 *****************************************************************************/
#define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
static vlc_fourcc_t __GetFOURCC( uint8_t *p )
{
    return VLC_FOURCC( p[0], p[1], p[2], p[3] );
169 170
}

171 172 173 174
/*****************************************************************************
 * definitions of structures and functions used by this plugins
 *****************************************************************************/
typedef struct
175
{
176
    vlc_bool_t  b_default;
177
    vlc_bool_t  b_enabled;
178
    int         i_number;
179

180 181
    int         i_extra_data;
    uint8_t     *p_extra_data;
182

183
    char         *psz_codec;
184

185
    uint64_t     i_default_duration;
186
    float        f_timecodescale;
Laurent Aimar's avatar
Laurent Aimar committed
187

188
    /* video */
Laurent Aimar's avatar
Laurent Aimar committed
189
    es_format_t fmt;
190
    float       f_fps;
Laurent Aimar's avatar
Laurent Aimar committed
191
    es_out_id_t *p_es;
192

193 194 195 196
    vlc_bool_t      b_inited;
    /* data to be send first */
    int             i_data_init;
    uint8_t         *p_data_init;
197

198 199
    /* hack : it's for seek */
    vlc_bool_t      b_search_keyframe;
200 201 202 203 204 205 206

    /* informative */
    char         *psz_codec_name;
    char         *psz_codec_settings;
    char         *psz_codec_info_url;
    char         *psz_codec_download_url;

207
} mkv_track_t;
208

209
typedef struct
210
{
211 212
    int     i_track;
    int     i_block_number;
213

214 215
    int64_t i_position;
    int64_t i_time;
216

217 218
    vlc_bool_t b_key;
} mkv_index_t;
219

220
struct demux_sys_t
221
{
222 223 224
    vlc_stream_io_callback  *in;
    EbmlStream              *es;
    EbmlParser              *ep;
225

226 227
    /* time scale */
    uint64_t                i_timescale;
228

229 230
    /* duration of the segment */
    float                   f_duration;
231

232 233 234
    /* all tracks */
    int                     i_track;
    mkv_track_t             *track;
235

236 237 238 239
    /* from seekhead */
    int64_t                 i_cues_position;
    int64_t                 i_chapters_position;
    int64_t                 i_tags_position;
240

241 242 243
    /* current data */
    KaxSegment              *segment;
    KaxCluster              *cluster;
244

245
    mtime_t                 i_pts;
246

247 248 249 250
    vlc_bool_t              b_cues;
    int                     i_index;
    int                     i_index_max;
    mkv_index_t             *index;
251

252 253 254 255 256 257
    /* info */
    char                    *psz_muxing_application;
    char                    *psz_writing_application;
    char                    *psz_segment_filename;
    char                    *psz_title;
    char                    *psz_date_utc;
258 259

    vlc_meta_t              *meta;
260 261
};

262 263 264 265 266 267 268 269
#define MKVD_TIMECODESCALE 1000000

static void IndexAppendCluster  ( input_thread_t *p_input, KaxCluster *cluster );
static char *UTF8ToStr          ( const UTFstring &u );
static void LoadCues            ( input_thread_t *);
static void InformationsCreate  ( input_thread_t *p_input );

/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
270
 * Open: initializes matroska demux structures
271
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
272
static int Open( vlc_object_t * p_this )
273
{
274 275 276
    input_thread_t *p_input = (input_thread_t *)p_this;
    demux_sys_t    *p_sys;
    uint8_t        *p_peek;
277

278
    int             i_track;
279

280 281 282 283
    EbmlElement     *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;

    /* peek the begining */
    if( input_Peek( p_input, &p_peek, 4 ) < 4 )
284 285 286 287 288 289
    {
        msg_Warn( p_input, "cannot peek" );
        return VLC_EGENERIC;
    }

    /* is a valid file */
Laurent Aimar's avatar
Laurent Aimar committed
290 291
    if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
        p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
292
    {
293 294 295
        msg_Warn( p_input, "matroska module discarded "
                           "(invalid header 0x%.2x%.2x%.2x%.2x)",
                           p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
296 297 298
        return VLC_EGENERIC;
    }

299 300 301 302
    /* Set the demux function */
    p_input->pf_demux = Demux;
    p_input->pf_demux_control = Control;

Laurent Aimar's avatar
Laurent Aimar committed
303
    p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
304 305
    memset( p_sys, 0, sizeof( demux_sys_t ) );

306
    p_sys->in = new vlc_stream_io_callback( p_input->s );
307 308
    p_sys->es = new EbmlStream( *p_sys->in );
    p_sys->f_duration   = -1;
309
    p_sys->i_timescale     = MKVD_TIMECODESCALE;
310 311
    p_sys->i_track      = 0;
    p_sys->track        = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
312 313 314
    p_sys->i_pts   = 0;
    p_sys->i_cues_position = -1;
    p_sys->i_chapters_position = -1;
315
    p_sys->i_tags_position = -1;
316

317
    p_sys->b_cues       = VLC_FALSE;
318 319
    p_sys->i_index      = 0;
    p_sys->i_index_max  = 1024;
Laurent Aimar's avatar
Laurent Aimar committed
320 321
    p_sys->index        = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
                                                p_sys->i_index_max );
322

323 324 325 326 327
    p_sys->psz_muxing_application = NULL;
    p_sys->psz_writing_application = NULL;
    p_sys->psz_segment_filename = NULL;
    p_sys->psz_title = NULL;
    p_sys->psz_date_utc = NULL;;
328
    p_sys->meta = NULL;
329

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    if( p_sys->es == NULL )
    {
        msg_Err( p_input, "failed to create EbmlStream" );
        delete p_sys->in;
        free( p_sys );
        return VLC_EGENERIC;
    }
    /* Find the EbmlHead element */
    el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
    if( el == NULL )
    {
        msg_Err( p_input, "cannot find EbmlHead" );
        goto error;
    }
    msg_Dbg( p_input, "EbmlHead" );
    /* skip it */
    el->SkipData( *p_sys->es, el->Generic().Context );
    delete el;

    /* Find a segment */
    el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
    if( el == NULL )
    {
        msg_Err( p_input, "cannot find KaxSegment" );
        goto error;
    }
    msg_Dbg( p_input, "+ Segment" );
    p_sys->segment = (KaxSegment*)el;
    p_sys->cluster = NULL;

    p_sys->ep = new EbmlParser( p_sys->es, el );

    while( ( el1 = p_sys->ep->Get() ) != NULL )
    {
        if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "|   + Informations" );

            p_sys->ep->Down();
            while( ( el2 = p_sys->ep->Get() ) != NULL )
            {
                if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
                {
                    KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;

                    tcs.ReadData( p_sys->es->I_O() );
376
                    p_sys->i_timescale = uint64(tcs);
377

Gildas Bazin's avatar
 
Gildas Bazin committed
378 379
                    msg_Dbg( p_input, "|   |   + TimecodeScale="I64Fd,
                             p_sys->i_timescale );
380 381 382 383 384 385 386 387
                }
                else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
                {
                    KaxDuration &dur = *(KaxDuration*)el2;

                    dur.ReadData( p_sys->es->I_O() );
                    p_sys->f_duration = float(dur);

Laurent Aimar's avatar
Laurent Aimar committed
388 389
                    msg_Dbg( p_input, "|   |   + Duration=%f",
                             p_sys->f_duration );
390
                }
391
                else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
392
                {
393 394 395 396 397 398
                    KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;

                    mapp.ReadData( p_sys->es->I_O() );

                    p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );

Laurent Aimar's avatar
Laurent Aimar committed
399 400
                    msg_Dbg( p_input, "|   |   + Muxing Application=%s",
                             p_sys->psz_muxing_application );
401
                }
402 403 404 405 406 407 408 409
                else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
                {
                    KaxWritingApp &wapp = *(KaxWritingApp*)el2;

                    wapp.ReadData( p_sys->es->I_O() );

                    p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );

Laurent Aimar's avatar
Laurent Aimar committed
410 411
                    msg_Dbg( p_input, "|   |   + Wrinting Application=%s",
                             p_sys->psz_writing_application );
412 413 414 415 416 417 418 419 420
                }
                else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
                {
                    KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;

                    sfn.ReadData( p_sys->es->I_O() );

                    p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );

Laurent Aimar's avatar
Laurent Aimar committed
421 422
                    msg_Dbg( p_input, "|   |   + Segment Filename=%s",
                             p_sys->psz_segment_filename );
423 424 425 426 427 428 429 430 431 432 433
                }
                else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
                {
                    KaxTitle &title = *(KaxTitle*)el2;

                    title.ReadData( p_sys->es->I_O() );

                    p_sys->psz_title = UTF8ToStr( UTFstring( title ) );

                    msg_Dbg( p_input, "|   |   + Title=%s", p_sys->psz_title );
                }
434
#ifdef HAVE_GMTIME_R
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
                else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
                {
                    KaxDateUTC &date = *(KaxDateUTC*)el2;
                    time_t i_date;
                    struct tm tmres;
                    char   buffer[256];

                    date.ReadData( p_sys->es->I_O() );

                    i_date = date.GetEpochDate();
                    memset( buffer, 0, 256 );
                    if( gmtime_r( &i_date, &tmres ) &&
                        asctime_r( &tmres, buffer ) )
                    {
                        buffer[strlen( buffer)-1]= '\0';
                        p_sys->psz_date_utc = strdup( buffer );
                        msg_Dbg( p_input, "|   |   + Date=%s", p_sys->psz_date_utc );
                    }
                }
#endif
                else
                {
457
                    msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid(*el2).name() );
458 459
                }
            }
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
            p_sys->ep->Up();
        }
        else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "|   + Tracks" );

            p_sys->ep->Down();
            while( ( el2 = p_sys->ep->Get() ) != NULL )
            {
                if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   |   + Track Entry" );

                    p_sys->i_track++;
                    p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
#define tk  p_sys->track[p_sys->i_track - 1]
                    memset( &tk, 0, sizeof( mkv_track_t ) );
Laurent Aimar's avatar
Laurent Aimar committed
477 478 479

                    es_format_Init( &tk.fmt, UNKNOWN_ES, 0 );
                    tk.fmt.psz_language = strdup("English");
480
                    tk.fmt.psz_description = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
481

482 483
                    tk.b_default = VLC_TRUE;
                    tk.b_enabled = VLC_TRUE;
484 485 486 487 488
                    tk.i_number = p_sys->i_track - 1;
                    tk.i_extra_data = 0;
                    tk.p_extra_data = NULL;
                    tk.psz_codec = NULL;
                    tk.i_default_duration = 0;
489
                    tk.f_timecodescale = 1.0;
490 491 492 493 494

                    tk.b_inited = VLC_FALSE;
                    tk.i_data_init = 0;
                    tk.p_data_init = NULL;

495 496 497 498 499
                    tk.psz_codec_name = NULL;
                    tk.psz_codec_settings = NULL;
                    tk.psz_codec_info_url = NULL;
                    tk.psz_codec_download_url = NULL;

500 501 502 503 504 505 506 507 508 509
                    p_sys->ep->Down();

                    while( ( el3 = p_sys->ep->Get() ) != NULL )
                    {
                        if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
                        {
                            KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
                            tnum.ReadData( p_sys->es->I_O() );

                            tk.i_number = uint32( tnum );
Laurent Aimar's avatar
Laurent Aimar committed
510 511
                            msg_Dbg( p_input, "|   |   |   + Track Number=%u",
                                     uint32( tnum ) );
512 513 514 515 516 517
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
                        {
                            KaxTrackUID &tuid = *(KaxTrackUID*)el3;
                            tuid.ReadData( p_sys->es->I_O() );

Laurent Aimar's avatar
Laurent Aimar committed
518 519
                            msg_Dbg( p_input, "|   |   |   + Track UID=%u",
                                     uint32( tuid ) );
520 521 522 523 524 525 526 527 528 529
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
                        {
                            char *psz_type;
                            KaxTrackType &ttype = *(KaxTrackType*)el3;
                            ttype.ReadData( p_sys->es->I_O() );
                            switch( uint8(ttype) )
                            {
                                case track_audio:
                                    psz_type = "audio";
Laurent Aimar's avatar
Laurent Aimar committed
530
                                    tk.fmt.i_cat = AUDIO_ES;
531 532 533
                                    break;
                                case track_video:
                                    psz_type = "video";
Laurent Aimar's avatar
Laurent Aimar committed
534
                                    tk.fmt.i_cat = VIDEO_ES;
535 536 537
                                    break;
                                case track_subtitle:
                                    psz_type = "subtitle";
Laurent Aimar's avatar
Laurent Aimar committed
538
                                    tk.fmt.i_cat = SPU_ES;
539 540 541
                                    break;
                                default:
                                    psz_type = "unknown";
Laurent Aimar's avatar
Laurent Aimar committed
542
                                    tk.fmt.i_cat = UNKNOWN_ES;
543 544 545
                                    break;
                            }

546 547
                            msg_Dbg( p_input, "|   |   |   + Track Type=%s",
                                     psz_type );
548
                        }
549 550 551 552 553 554 555 556 557
//                         else  if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
//                         {
//                             KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
//                             fenb.ReadData( p_sys->es->I_O() );

//                             tk.b_enabled = uint32( fenb );
//                             msg_Dbg( p_input, "|   |   |   + Track Enabled=%u",
//                                      uint32( fenb )  );
//                         }
558 559 560 561
                        else  if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
                        {
                            KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
                            fdef.ReadData( p_sys->es->I_O() );
562

563
                            tk.b_default = uint32( fdef );
564 565
                            msg_Dbg( p_input, "|   |   |   + Track Default=%u",
                                     uint32( fdef )  );
566 567 568 569 570
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
                        {
                            KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
                            lac.ReadData( p_sys->es->I_O() );
571

572 573
                            msg_Dbg( p_input, "|   |   |   + Track Lacing=%d",
                                     uint32( lac ) );
574 575 576 577 578
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
                        {
                            KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
                            cmin.ReadData( p_sys->es->I_O() );
579

580 581
                            msg_Dbg( p_input, "|   |   |   + Track MinCache=%d",
                                     uint32( cmin ) );
582 583 584 585 586
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
                        {
                            KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
                            cmax.ReadData( p_sys->es->I_O() );
587

588 589
                            msg_Dbg( p_input, "|   |   |   + Track MaxCache=%d",
                                     uint32( cmax ) );
590 591 592 593 594 595 596
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
                        {
                            KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
                            defd.ReadData( p_sys->es->I_O() );

                            tk.i_default_duration = uint64(defd);
Gildas Bazin's avatar
 
Gildas Bazin committed
597
                            msg_Dbg( p_input, "|   |   |   + Track Default Duration="I64Fd, uint64(defd) );
598 599 600 601 602 603 604 605 606 607 608 609 610 611
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
                        {
                            KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
                            ttcs.ReadData( p_sys->es->I_O() );

                            tk.f_timecodescale = float( ttcs );
                            msg_Dbg( p_input, "|   |   |   + Track TimeCodeScale=%f", tk.f_timecodescale );
                        }
                        else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
                        {
                            KaxTrackName &tname = *(KaxTrackName*)el3;
                            tname.ReadData( p_sys->es->I_O() );

612
                            tk.fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
613
                            msg_Dbg( p_input, "|   |   |   + Track Name=%s",
614
                                     tk.fmt.psz_description );
615 616 617 618 619 620
                        }
                        else  if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
                        {
                            KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
                            lang.ReadData( p_sys->es->I_O() );

621
                            tk.fmt.psz_language = strdup( string( lang ).c_str() );
622
                            msg_Dbg( p_input,
623 624
                                     "|   |   |   + Track Language=`%s'",
                                     tk.fmt.psz_language );
625 626 627 628 629 630 631
                        }
                        else  if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
                        {
                            KaxCodecID &codecid = *(KaxCodecID*)el3;
                            codecid.ReadData( p_sys->es->I_O() );

                            tk.psz_codec = strdup( string( codecid ).c_str() );
632 633
                            msg_Dbg( p_input, "|   |   |   + Track CodecId=%s",
                                     string( codecid ).c_str() );
634 635 636 637
                        }
                        else  if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
                        {
                            KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
638
                            cpriv.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
639 640 641 642 643 644

                            tk.i_extra_data = cpriv.GetSize();
                            if( tk.i_extra_data > 0 )
                            {
                                tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
                                memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
645
                            }
Gildas Bazin's avatar
 
Gildas Bazin committed
646
                            msg_Dbg( p_input, "|   |   |   + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
647 648 649 650 651 652 653 654 655
                        }
                        else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
                        {
                            KaxCodecName &cname = *(KaxCodecName*)el3;
                            cname.ReadData( p_sys->es->I_O() );

                            tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
                            msg_Dbg( p_input, "|   |   |   + Track Codec Name=%s", tk.psz_codec_name );
                        }
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
//                         else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
//                         {
//                             KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
//                             cset.ReadData( p_sys->es->I_O() );

//                             tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
//                             msg_Dbg( p_input, "|   |   |   + Track Codec Settings=%s", tk.psz_codec_settings );
//                         }
//                         else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
//                         {
//                             KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
//                             ciurl.ReadData( p_sys->es->I_O() );

//                             tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
//                             msg_Dbg( p_input, "|   |   |   + Track Codec Info URL=%s", tk.psz_codec_info_url );
//                         }
//                         else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
//                         {
//                             KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
//                             cdurl.ReadData( p_sys->es->I_O() );

//                             tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
//                             msg_Dbg( p_input, "|   |   |   + Track Codec Info URL=%s", tk.psz_codec_download_url );
//                         }
//                         else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
//                         {
//                             KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
//                             cdall.ReadData( p_sys->es->I_O() );

//                             msg_Dbg( p_input, "|   |   |   + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
//                         }
//                         else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
//                         {
//                             KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
//                             tovr.ReadData( p_sys->es->I_O() );

//                             msg_Dbg( p_input, "|   |   |   + Track Overlay=%u <== UNUSED", uint32( tovr ) );
//                         }
694 695 696 697 698 699 700 701 702
                        else  if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
                        {
                            msg_Dbg( p_input, "|   |   |   + Track Video" );
                            tk.f_fps = 0.0;

                            p_sys->ep->Down();

                            while( ( el4 = p_sys->ep->Get() ) != NULL )
                            {
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
//                                 if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
//                                 {
//                                     KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
//                                     fint.ReadData( p_sys->es->I_O() );

//                                     msg_Dbg( p_input, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
//                                 }
//                                 else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
//                                 {
//                                     KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
//                                     stereo.ReadData( p_sys->es->I_O() );

//                                     msg_Dbg( p_input, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
//                                 }
//                                 else 
                              if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
719 720 721 722
                                {
                                    KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
                                    vwidth.ReadData( p_sys->es->I_O() );

Laurent Aimar's avatar
Laurent Aimar committed
723
                                    tk.fmt.video.i_width = uint16( vwidth );
724 725 726 727 728 729 730
                                    msg_Dbg( p_input, "|   |   |   |   + width=%d", uint16( vwidth ) );
                                }
                                else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
                                {
                                    KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
                                    vheight.ReadData( p_sys->es->I_O() );

Laurent Aimar's avatar
Laurent Aimar committed
731
                                    tk.fmt.video.i_height = uint16( vheight );
732 733 734 735 736 737 738
                                    msg_Dbg( p_input, "|   |   |   |   + height=%d", uint16( vheight ) );
                                }
                                else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
                                {
                                    KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
                                    vwidth.ReadData( p_sys->es->I_O() );

Gildas Bazin's avatar
 
Gildas Bazin committed
739
                                    tk.fmt.video.i_visible_width = uint16( vwidth );
740 741 742 743 744 745 746
                                    msg_Dbg( p_input, "|   |   |   |   + display width=%d", uint16( vwidth ) );
                                }
                                else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
                                {
                                    KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
                                    vheight.ReadData( p_sys->es->I_O() );

Gildas Bazin's avatar
 
Gildas Bazin committed
747
                                    tk.fmt.video.i_visible_height = uint16( vheight );
748 749 750 751 752 753 754 755 756 757
                                    msg_Dbg( p_input, "|   |   |   |   + display height=%d", uint16( vheight ) );
                                }
                                else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
                                {
                                    KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
                                    vfps.ReadData( p_sys->es->I_O() );

                                    tk.f_fps = float( vfps );
                                    msg_Dbg( p_input, "   |   |   |   + fps=%f", float( vfps ) );
                                }
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
//                                 else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
//                                 {
//                                      KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
//                                     vdmode.ReadData( p_sys->es->I_O() );

//                                     msg_Dbg( p_input, "|   |   |   |   + Track Video Display Unit=%s",
//                                              uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
//                                 }
//                                 else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
//                                 {
//                                     KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
//                                     ratio.ReadData( p_sys->es->I_O() );

//                                     msg_Dbg( p_input, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
//                                 }
//                                 else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
//                                 {
//                                     KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
//                                     gamma.ReadData( p_sys->es->I_O() );

//                                     msg_Dbg( p_input, "   |   |   |   + fps=%f", float( gamma ) );
//                                 }
780 781
                                else
                                {
782
                                    msg_Dbg( p_input, "|   |   |   |   + Unknown (%s)", typeid(*el4).name() );
783 784 785 786
                                }
                            }
                            p_sys->ep->Up();
                        }
787
                        else  if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
788
                        {
789
                            msg_Dbg( p_input, "|   |   |   + Track Audio" );
790

791
                            p_sys->ep->Down();
792

793
                            while( ( el4 = p_sys->ep->Get() ) != NULL )
794
                            {
795 796 797 798
                                if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
                                {
                                    KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
                                    afreq.ReadData( p_sys->es->I_O() );
799

Gildas Bazin's avatar
 
Gildas Bazin committed
800 801
                                    tk.fmt.audio.i_rate = (int)float( afreq );
                                    msg_Dbg( p_input, "|   |   |   |   + afreq=%d", tk.fmt.audio.i_rate );
802 803 804 805 806
                                }
                                else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
                                {
                                    KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
                                    achan.ReadData( p_sys->es->I_O() );
807

Laurent Aimar's avatar
Laurent Aimar committed
808
                                    tk.fmt.audio.i_channels = uint8( achan );
809 810 811 812 813 814
                                    msg_Dbg( p_input, "|   |   |   |   + achan=%u", uint8( achan ) );
                                }
                                else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
                                {
                                    KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
                                    abits.ReadData( p_sys->es->I_O() );
815

Laurent Aimar's avatar
Laurent Aimar committed
816
                                    tk.fmt.audio.i_bitspersample = uint8( abits );
817 818 819 820 821 822 823 824
                                    msg_Dbg( p_input, "|   |   |   |   + abits=%u", uint8( abits ) );
                                }
                                else
                                {
                                    msg_Dbg( p_input, "|   |   |   |   + Unknown (%s)", typeid(*el4).name() );
                                }
                            }
                            p_sys->ep->Up();
825 826 827
                        }
                        else
                        {
Laurent Aimar's avatar
Laurent Aimar committed
828 829
                            msg_Dbg( p_input, "|   |   |   + Unknown (%s)",
                                     typeid(*el3).name() );
830 831 832 833 834 835
                        }
                    }
                    p_sys->ep->Up();
                }
                else
                {
Laurent Aimar's avatar
Laurent Aimar committed
836 837
                    msg_Dbg( p_input, "|   |   + Unknown (%s)",
                             typeid(*el2).name() );
838 839 840 841 842 843 844 845
                }
#undef tk
            }
            p_sys->ep->Up();
        }
        else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "|   + Seek head" );
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
            p_sys->ep->Down();
            while( ( el = p_sys->ep->Get() ) != NULL )
            {
                if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
                {
                    EbmlId id = EbmlVoid::ClassInfos.GlobalId;
                    int64_t i_pos = -1;

                    //msg_Dbg( p_input, "|   |   + Seek" );
                    p_sys->ep->Down();
                    while( ( el = p_sys->ep->Get() ) != NULL )
                    {
                        if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
                        {
                            KaxSeekID &sid = *(KaxSeekID*)el;

862
                            sid.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
863 864 865 866 867 868 869

                            id = EbmlId( sid.GetBuffer(), sid.GetSize() );
                        }
                        else  if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
                        {
                            KaxSeekPosition &spos = *(KaxSeekPosition*)el;

870
                            spos.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
871 872 873 874 875

                            i_pos = uint64( spos );
                        }
                        else
                        {
Laurent Aimar's avatar
Laurent Aimar committed
876 877
                            msg_Dbg( p_input, "|   |   |   + Unknown (%s)",
                                     typeid(*el).name() );
878 879 880 881 882 883 884 885
                        }
                    }
                    p_sys->ep->Up();

                    if( i_pos >= 0 )
                    {
                        if( id == KaxCues::ClassInfos.GlobalId )
                        {
Laurent Aimar's avatar
Laurent Aimar committed
886 887
                            msg_Dbg( p_input, "|   |   |   = cues at "I64Fd,
                                     i_pos );
888 889 890 891
                            p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
                        }
                        else if( id == KaxChapters::ClassInfos.GlobalId )
                        {
Laurent Aimar's avatar
Laurent Aimar committed
892 893
                            msg_Dbg( p_input, "|   |   |   = chapters at "I64Fd,
                                     i_pos );
894 895
                            p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
                        }
896 897
                        else if( id == KaxTags::ClassInfos.GlobalId )
                        {
Laurent Aimar's avatar
Laurent Aimar committed
898 899
                            msg_Dbg( p_input, "|   |   |   = tags at "I64Fd,
                                     i_pos );
900 901 902
                            p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
                        }

903 904 905 906
                    }
                }
                else
                {
Laurent Aimar's avatar
Laurent Aimar committed
907 908
                    msg_Dbg( p_input, "|   |   + Unknown (%s)",
                             typeid(*el).name() );
909 910 911
                }
            }
            p_sys->ep->Up();
912 913 914 915 916 917 918 919
        }
        else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "|   + Cues" );
        }
        else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "|   + Cluster" );
920

921 922
            p_sys->cluster = (KaxCluster*)el1;

923
            p_sys->ep->Down();
924 925 926
            /* stop parsing the stream */
            break;
        }
927 928 929
#ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
        else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
#else
930
        else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
931
#endif
932
        {
933
            msg_Dbg( p_input, "|   + Attachments FIXME TODO (but probably never supported)" );
934 935 936 937 938 939 940 941 942 943 944
        }
        else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "|   + Chapters FIXME TODO" );
        }
        else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "|   + Tags FIXME TODO" );
        }
        else
        {
945
            msg_Dbg( p_input, "|   + Unknown (%s)", typeid(*el1).name() );
946 947 948 949 950 951 952 953 954
        }
    }

    if( p_sys->cluster == NULL )
    {
        msg_Err( p_input, "cannot find any cluster, damaged file ?" );
        goto error;
    }

955 956 957 958 959 960
    if( p_sys->i_chapters_position >= 0 )
    {
        msg_Warn( p_input, "chapters unsupported" );
    }

    /* *** Load the cue if found *** */
Laurent Aimar's avatar
Laurent Aimar committed
961
    if( p_sys->i_cues_position >= 0 )
962
    {
Laurent Aimar's avatar
Laurent Aimar committed
963 964
        vlc_bool_t b_seekable;

965
        stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
Laurent Aimar's avatar
Laurent Aimar committed
966 967 968 969
        if( b_seekable )
        {
            LoadCues( p_input );
        }
970 971
    }

972
    if( !p_sys->b_cues || p_sys->i_index <= 0 )
973
    {
Laurent Aimar's avatar
Laurent Aimar committed
974
        msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
975 976 977 978

        IndexAppendCluster( p_input, p_sys->cluster );

        p_sys->b_cues = VLC_FALSE;
979 980
    }

981 982 983 984 985 986 987 988
    /* Create one program */
    vlc_mutex_lock( &p_input->stream.stream_lock );
    if( input_InitStream( p_input, 0 ) == -1)
    {
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        msg_Err( p_input, "cannot init stream" );
        goto error;
    }
Laurent Aimar's avatar
Laurent Aimar committed
989 990 991
    p_input->stream.i_mux_rate = 0;
    vlc_mutex_unlock( &p_input->stream.stream_lock );

992 993 994
    if( p_sys->f_duration > 1001.0 )
    {
        mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
995
        p_input->stream.i_mux_rate = stream_Size( p_input->s )/50 / i_duration;
996
    }
997 998 999 1000 1001 1002

    /* add all es */
    msg_Dbg( p_input, "found %d es", p_sys->i_track );
    for( i_track = 0; i_track < p_sys->i_track; i_track++ )
    {
#define tk  p_sys->track[i_track]
Laurent Aimar's avatar
Laurent Aimar committed
1003
        if( tk.fmt.i_cat == UNKNOWN_ES )
1004 1005 1006 1007 1008
        {
            msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
            tk.p_es = NULL;
            continue;
        }
Laurent Aimar's avatar
Laurent Aimar committed
1009 1010

        if( tk.fmt.i_cat == SPU_ES )
1011 1012 1013 1014 1015 1016
        {
            vlc_value_t val;
            val.psz_string = "UTF-8";
#if defined(HAVE_ICONV)
            var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
            var_Set( p_input, "subsdec-encoding", val );
1017
#endif
1018
        }
1019 1020
        if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
        {
1021
            if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1022 1023
            {
                msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
Laurent Aimar's avatar
Laurent Aimar committed
1024
                tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1025 1026 1027 1028 1029
            }
            else
            {
                BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;

Laurent Aimar's avatar
Laurent Aimar committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
                tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
                tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
                tk.fmt.i_codec       = GetFOURCC( &p_bih->biCompression );

                tk.fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
                if( tk.fmt.i_extra > 0 )
                {
                    tk.fmt.p_extra = malloc( tk.fmt.i_extra );
                    memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
                }
1040 1041 1042 1043 1044
            }
        }
        else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
                 !strcmp( tk.psz_codec, "V_MPEG2" ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1045
            tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1046 1047 1048 1049 1050
        }
        else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
        {
            if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1051
                tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1052 1053 1054
            }
            else
            {
Laurent Aimar's avatar
Laurent Aimar committed
1055
                tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1056 1057 1058 1059
            }
        }
        else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
        {
1060
            if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1061 1062
            {
                msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
Laurent Aimar's avatar
Laurent Aimar committed
1063
                tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1064 1065 1066 1067 1068
            }
            else
            {
                WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;

Laurent Aimar's avatar
Laurent Aimar committed
1069
                wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
1070

Laurent Aimar's avatar
Laurent Aimar committed
1071
                tk.fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
Gildas Bazin's avatar
 
Gildas Bazin committed
1072
                tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
Gildas Bazin's avatar
 
Gildas Bazin committed
1073
                tk.fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
Laurent Aimar's avatar
Laurent Aimar committed
1074 1075 1076 1077 1078 1079 1080 1081 1082
                tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
                tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );

                tk.fmt.i_extra            = GetWLE( &p_wf->cbSize );
                if( tk.fmt.i_extra > 0 )
                {
                    tk.fmt.p_extra = malloc( tk.fmt.i_extra );
                    memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
                }
1083 1084 1085 1086 1087 1088
            }
        }
        else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
                 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
                 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1089
            tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1090 1091 1092
        }
        else if( !strcmp( tk.psz_codec, "A_AC3" ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1093
            tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1094 1095 1096
        }
        else if( !strcmp( tk.psz_codec, "A_DTS" ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1097
            tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1098
        }
1099 1100 1101 1102 1103 1104 1105
        else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
        {
            tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
            tk.fmt.i_extra = tk.i_extra_data;
            tk.fmt.p_extra = malloc( tk.i_extra_data );
            memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
        }
1106 1107
        else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1108
            tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1109 1110 1111 1112 1113 1114 1115
            tk.i_data_init = tk.i_extra_data;
            tk.p_data_init = tk.p_extra_data;
        }
        else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
                 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
        {
            int i_profile, i_srate;
1116
            static unsigned int i_sample_rates[] =
1117 1118 1119 1120 1121
            {
                    96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
                        16000, 12000, 11025, 8000,  7350,  0,     0,     0
            };

Laurent Aimar's avatar
Laurent Aimar committed
1122
            tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
            /* create data for faad (MP4DecSpecificDescrTag)*/

            if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
            {
                i_profile = 0;
            }
            else if( !strcmp( &tk.psz_codec[12], "LC" ) )
            {
                i_profile = 1;
            }
            else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
            {
                i_profile = 2;
            }
            else
            {
                i_profile = 3;
            }

            for( i_srate = 0; i_srate < 13; i_srate++ )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
1144
                if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
1145 1146 1147 1148 1149 1150
                {
                    break;
                }
            }
            msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );

Laurent Aimar's avatar
Laurent Aimar committed
1151 1152 1153 1154
            tk.fmt.i_extra = 2;
            tk.fmt.p_extra = malloc( tk.fmt.i_extra );
            ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
            ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
1155 1156 1157 1158 1159 1160 1161
        }
        else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
                 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
                 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
        {
            if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1162
                tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1163 1164 1165
            }
            else
            {
Laurent Aimar's avatar
Laurent Aimar committed
1166
                tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1167
            }
Laurent Aimar's avatar
Laurent Aimar committed
1168
            tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
1169 1170 1171
        }
        else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1172
            tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1173
        }
1174 1175 1176
        else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
                 !strcmp( tk.psz_codec, "S_SSA" ) ||
                 !strcmp( tk.psz_codec, "S_ASS" ))
1177
        {
Laurent Aimar's avatar
Laurent Aimar committed
1178 1179 1180 1181 1182 1183 1184
            tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
#if 0
            /* FIXME */
            tk.fmt.i_extra = sizeof( subtitle_data_t );
            tk.fmt.p_extra = malloc( tk.fmt.i_extra );
            ((es_sys_t*)tk->fmt.p_extra)->psz_header = strdup( (char *)tk.p_extra_data );
#endif
1185
        }
1186 1187
        else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1188
            tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1189
        }
1190 1191 1192
        else
        {
            msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
Laurent Aimar's avatar
Laurent Aimar committed
1193
            tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1194 1195
        }

Laurent Aimar's avatar
Laurent Aimar committed
1196
        tk.p_es = es_out_Add( p_input->p_es_out, &tk.fmt );
1197 1198 1199
#undef tk
    }

1200
    /* add informations */
1201
    InformationsCreate( p_input );
1202

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    return VLC_SUCCESS;

error:
    delete p_sys->es;
    delete p_sys->in;
    free( p_sys );
    return VLC_EGENERIC;
}

/*****************************************************************************
Laurent Aimar's avatar
Laurent Aimar committed
1213
 * Close: frees unused data
1214
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
1215
static void Close( vlc_object_t *p_this )
1216 1217 1218 1219 1220 1221 1222 1223 1224
{
    input_thread_t *p_input = (input_thread_t *)p_this;
    demux_sys_t    *p_sys   = p_input->p_demux_data;

    int             i_track;

    for( i_track = 0; i_track < p_sys->i_track; i_track++ )
    {
#define tk  p_sys->track[i_track]
1225 1226
        if( tk.fmt.psz_description )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1227
            free( tk.fmt.psz_description );
1228
        }
1229 1230 1231 1232
        if( tk.psz_codec )
        {
            free( tk.psz_codec );
        }
Laurent Aimar's avatar
Laurent Aimar committed
1233
        if( tk.fmt.psz_language )
1234
        {
Laurent Aimar's avatar
Laurent Aimar committed
1235
            free( tk.fmt.psz_language );
1236 1237 1238 1239 1240
        }
#undef tk
    }
    free( p_sys->track );

1241 1242 1243 1244 1245 1246 1247 1248 1249
    if( p_sys->psz_writing_application  )
    {
        free( p_sys->psz_writing_application );
    }
    if( p_sys->psz_muxing_application  )
    {
        free( p_sys->psz_muxing_application );
    }

1250 1251
    delete p_sys->segment;

1252 1253 1254 1255 1256 1257 1258
    delete p_sys->ep;
    delete p_sys->es;
    delete p_sys->in;

    free( p_sys );
}

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( input_thread_t *p_input, int i_query, va_list args )
{
    demux_sys_t *p_sys = p_input->p_demux_data;
    int64_t     *pi64;

    vlc_meta_t **pp_meta;

    switch( i_query )
    {
        case DEMUX_GET_META:
            pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
            *pp_meta = vlc_meta_Duplicate( p_sys->meta );
            return VLC_SUCCESS;

        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            if( p_sys->f_duration > 0.0 )
            {
                *pi64 = (int64_t)(p_sys->f_duration * 1000);
                return VLC_SUCCESS;
            }
            return VLC_EGENERIC;

        default:
            return demux_vaControlDefault( p_input, i_query, args );
    }
}

1290
static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1291 1292 1293
{
    demux_sys_t    *p_sys   = p_input->p_demux_data;

1294 1295 1296
    *pp_block = NULL;
    *pi_ref1  = -1;
    *pi_ref2  = -1;
1297 1298 1299 1300 1301 1302

    for( ;; )
    {
        EbmlElement *el;
        int         i_level;

1303 1304 1305 1306 1307
        if( p_input->b_die )
        {
            return VLC_EGENERIC;
        }

1308 1309 1310
        el = p_sys->ep->Get();
        i_level = p_sys->ep->GetLevel();

1311
        if( el == NULL && *pp_block != NULL )
1312
        {
1313 1314 1315 1316 1317 1318 1319 1320
            /* update the index */
#define idx p_sys->index[p_sys->i_index - 1]
            if( p_sys->i_index > 0 && idx.i_time == -1 )
            {
                idx.i_time        = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
                idx.b_key         = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
            }
#undef idx
1321
            return VLC_SUCCESS;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
        }

        if( el == NULL )
        {
            if( p_sys->ep->GetLevel() > 1 )
            {
                p_sys->ep->Up();
                continue;
            }
            msg_Warn( p_input, "EOF" );
1332
            return VLC_EGENERIC;
1333 1334 1335 1336 1337 1338 1339 1340
        }

        /* do parsing */
        if( i_level == 1 )
        {
            if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
            {
                p_sys->cluster = (KaxCluster*)el;
1341 1342 1343

                /* add it to the index */
                if( p_sys->i_index == 0 ||
1344
                    ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1345 1346 1347 1348
                {
                    IndexAppendCluster( p_input, p_sys->cluster );
                }

1349 1350 1351 1352 1353
                p_sys->ep->Down();
            }
            else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
            {
                msg_Warn( p_input, "find KaxCues FIXME" );
1354
                return VLC_EGENERIC;
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
            }
            else
            {
                msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
            }
        }
        else if( i_level == 2 )
        {
            if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
            {
                KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;

1367
                ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1368
                p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
            }
            else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
            {
                p_sys->ep->Down();
            }
        }
        else if( i_level == 3 )
        {
            if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
            {
1379
                *pp_block = (KaxBlock*)el;
1380

1381 1382
                (*pp_block)->ReadData( p_sys->es->I_O() );
                (*pp_block)->SetParent( *p_sys->cluster );
1383 1384 1385 1386 1387 1388 1389 1390

                p_sys->ep->Keep();
            }
            else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
            {
                KaxBlockDuration &dur = *(KaxBlockDuration*)el;

                dur.ReadData( p_sys->es->I_O() );
1391
                *pi_duration = uint64( dur );
1392 1393 1394 1395 1396 1397
            }
            else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
            {
                KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;

                ref.ReadData( p_sys->es->I_O() );
1398
                if( *pi_ref1 == -1 )
1399
                {
1400
                    *pi_ref1 = int64( ref );
1401 1402 1403
                }
                else
                {
1404
                    *pi_ref2 = int64( ref );
1405 1406 1407 1408 1409 1410
                }
            }
        }
        else
        {
            msg_Err( p_input, "invalid level = %d", i_level );
1411 1412 1413 1414 1415
            return VLC_EGENERIC;
        }
    }
}

Gildas Bazin's avatar
 
Gildas Bazin committed
1416
static block_t *MemToBlock( input_thread_t *p_input, uint8_t *p_mem, int i_mem)
1417
{
Gildas Bazin's avatar
 
Gildas Bazin committed
1418 1419 1420 1421 1422
    block_t *p_block;
    if( !(p_block = block_New( p_input, i_mem ) ) ) return NULL;
    memcpy( p_block->p_buffer, p_mem, i_mem );
    //p_block->i_rate = p_input->stream.control.i_rate;
    return p_block;
1423 1424
}

1425 1426 1427 1428 1429 1430
static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
{
    demux_sys_t    *p_sys   = p_input->p_demux_data;

    int             i_track;
    unsigned int    i;
Laurent Aimar's avatar
Laurent Aimar committed
1431
    vlc_bool_t      b;
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447

#define tk  p_sys->track[i_track]
    for( i_track = 0; i_track < p_sys->i_track; i_track++ )
    {
        if( tk.i_number == block->TrackNum() )
        {
            break;
        }
    }

    if( i_track >= p_sys->i_track )
    {
        msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
        return;
    }

1448
    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
Laurent Aimar's avatar
Laurent Aimar committed
1449
    if( !b )
1450
    {
1451
        tk.b_inited = VLC_FALSE;
1452 1453 1454
        return;
    }

Laurent Aimar's avatar
Laurent Aimar committed
1455
    if( tk.fmt.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1456 1457 1458 1459
    {
        return;
    }

1460
    /* First send init data */
1461 1462
    if( !tk.b_inited && tk.i_data_init > 0 )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1463
        block_t *p_init;
1464 1465 1466

        msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );

Laurent Aimar's avatar
Laurent Aimar committed
1467
        if( tk.fmt.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1468 1469 1470 1471
        {
            int i;
            int i_offset = 1;
            int i_size[3];
1472

1473 1474 1475 1476 1477
            /* XXX hack split the 3 headers */
            if( tk.p_data_init[0] != 0x02 )
            {
                msg_Err( p_input, "invalid vorbis header" );
            }
1478

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
            for( i = 0; i < 2; i++ )
            {
                i_size[i] = 0;
                while( i_offset < tk.i_data_init )
                {
                    i_size[i] += tk.p_data_init[i_offset];
                    if( tk.p_data_init[i_offset++] != 0xff )
                    {
                        break;
                    }
                }
            }
            i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
            i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
            i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1494

Gildas Bazin's avatar
 
Gildas Bazin committed
1495
            p_init = MemToBlock( p_input, &tk.p_data_init[i_offset], i_size[0] );
1496 1497
            if( p_init )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1498
                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1499
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
1500
            p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1501 1502
            if( p_init )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1503
                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1504
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
1505
            p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1506 1507
            if( p_init )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1508
                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1509 1510 1511 1512
            }
        }
        else
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1513
            p_init = MemToBlock( p_input, tk.p_data_init, tk.i_data_init );
1514 1515
            if( p_init )
            {
Laurent Aimar's avatar
Laurent Aimar committed
1516
                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1517 1518
            }
        }
1519 1520 1521
    }
    tk.b_inited = VLC_TRUE;

1522 1523 1524

    for( i = 0; i < block->NumberFrames(); i++ )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1525
        block_t *p_block;
1526 1527
        DataBuffer &data = block->GetBuffer(i);

Gildas Bazin's avatar
 
Gildas Bazin committed
1528 1529
        p_block = MemToBlock( p_input, data.Buffer(), data.Size() );
        if( p_block == NULL )
1530 1531 1532 1533
        {
            break;
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
1534 1535 1536 1537 1538 1539 1540
        if( tk.fmt.i_cat != VIDEO_ES )
            p_block->i_dts = p_block->i_pts = i_pts;
        else
        {
            p_block->i_dts = i_pts;
            p_block->i_pts = 0;
        }
1541

Laurent Aimar's avatar
Laurent Aimar committed
1542
        if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1543 1544 1545
        {
            if( i_duration > 0 )
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
1546
                p_block->i_dts += i_duration * 1000;
1547 1548 1549
            }
            else
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
1550
                p_block->i_dts = 0;
1551 1552
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
1553
        es_out_Send( p_input->p_es_out, tk.p_es, p_block );
1554 1555

        /* use time stamp only for first block */
1556
        i_pts = 0;
1557
    }
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574

#undef tk
}

static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
{
    demux_sys_t    *p_sys   = p_input->p_demux_data;

    KaxBlock    *block;
    int64_t     i_block_duration;
    int64_t     i_block_ref1;
    int64_t     i_block_ref2;

    int         i_index;
    int         i_track_skipping;
    int         i_track;

Gildas Bazin's avatar
 
Gildas Bazin committed
1575
    msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1576 1577 1578 1579 1580 1581 1582 1583
    if( i_date < 0 && i_percent < 0 )
    {
        return;
    }

    delete p_sys->ep;
    p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
    p_sys->cluster = NULL;
1584

1585
    /* seek without index or without date */
1586
    if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1587
    {
1588
        int64_t i_pos = i_percent * stream_Size( p_input->s ) / 100;
1589

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1590
        msg_Dbg( p_input, "inacurate way of seeking" );
1591
        for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1592
        {
1593 1594 1595 1596 1597 1598 1599 1600
            if( p_sys->index[i_index].i_position >= i_pos)
            {
                break;
            }
        }
        if( i_index == p_sys->i_index )
        {
            i_index--;
1601 1602
        }

1603 1604
        p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
                                   seek_beginning );
1605

1606 1607 1608
        if( p_sys->index[i_index].i_position < i_pos )
        {
            EbmlElement *el;
1609

1610
            msg_Warn( p_input, "searching for cluster, could take some time" );
1611

1612 1613 1614 1615 1616 1617
            /* search a cluster */
            while( ( el = p_sys->ep->Get() ) != NULL )
            {
                if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
                {
                    KaxCluster *cluster = (KaxCluster*)el;
1618

1619 1620 1621
                    /* add it to the index */
                    IndexAppendCluster( p_input, cluster );

1622
                    if( (int64_t)cluster->GetElementPosition() >= i_pos )
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
                    {
                        p_sys->cluster = cluster;
                        p_sys->ep->Down();
                        break;
                    }
                }
            }
        }
    }
    else
    {
        for( i_index = 0; i_index < p_sys->i_index; i_index++ )
        {
            if( p_sys->index[i_index].i_time >= i_date )
            {
                break;
            }
        }

        if( i_index > 0 )
        {
            i_index--;
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
1647
        msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
Laurent Aimar's avatar
Laurent Aimar committed
1648 1649
                 p_sys->index[i_index].i_time,
                 (int)( 100 * p_sys->index[i_index].i_position /
1650
                        stream_Size( p_input->s ) ) );
1651

Laurent Aimar's avatar
Laurent Aimar committed
1652 1653
        p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
                                   seek_beginning );
1654
    }
1655 1656 1657 1658 1659 1660

    /* now parse until key frame */
#define tk  p_sys->track[i_track]
    i_track_skipping = 0;
    for( i_track = 0; i_track < p_sys->i_track; i_track++ )
    {
Laurent Aimar's avatar
Laurent Aimar committed
1661
        if( tk.fmt.i_cat == VIDEO_ES )
1662 1663 1664
        {
            tk.b_search_keyframe = VLC_TRUE;
            i_track_skipping++;
1665 1666 1667
        }
    }

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
    while( i_track_skipping > 0 )
    {
        if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
        {
            msg_Warn( p_input, "cannot get block EOF?" );

            return;
        }

        p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;

        for( i_track = 0; i_track < p_sys->i_track; i_track++ )
        {
            if( tk.i_number == block->TrackNum() )
            {
                break;
            }
        }

        if( i_track < p_sys->i_track )
        {
Laurent Aimar's avatar
Laurent Aimar committed
1689
            if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1690 1691 1692 1693
            {
                tk.b_search_keyframe = VLC_FALSE;
                i_track_skipping--;
            }
Laurent Aimar's avatar
Laurent Aimar committed
1694
            if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
            {
                BlockDecode( p_input, block, 0, 0 );
            }
        }

        delete block;
    }
#undef tk
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( input_thread_t * p_input )
{
    demux_sys_t    *p_sys   = p_input->p_demux_data;
    mtime_t        i_start_pts;
1714 1715
    int            i_block_count = 0;

1716 1717 1718 1719 1720 1721 1722 1723
    KaxBlock *block;
    int64_t i_block_duration;
    int64_t i_block_ref1;
    int64_t i_block_ref2;

    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
    {
        mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1724 1725
        mtime_t i_date = -1;
        int i_percent  = -1;
1726

1727 1728 1729 1730 1731
        if( i_duration > 0 )
        {
            i_date = (mtime_t)1000000 *
                     (mtime_t)i_duration*
                     (mtime_t)p_sys->in->getFilePointer() /
1732
                     (mtime_t)stream_Size( p_input->s );
1733
        }
1734
        if( stream_Size( p_input->s ) > 0 )
1735 1736
        {
            i_percent = 100 * p_sys->in->getFilePointer() /
1737
                        stream_Size( p_input->s );
1738
        }
1739 1740 1741 1742

        Seek( p_input, i_date, i_percent);
    }

1743
    i_start_pts = -1;
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768

    for( ;; )
    {
        mtime_t i_pts;

        if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
        {
            msg_Warn( p_input, "cannot get block EOF?" );

            return 0;
        }

        p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;

        if( p_sys->i_pts > 0 )
        {
            input_ClockManageRef( p_input,
                                  p_input->stream.p_selected_program,
                                  p_sys->i_pts * 9 / 100 );
        }

        i_pts = input_ClockGetTS( p_input,
                                  p_input->stream.p_selected_program,
                                  p_sys->i_pts * 9 / 100 );

1769 1770


1771 1772 1773
        BlockDecode( p_input, block, i_pts, i_block_duration );

        delete block;
1774
        i_block_count++;
1775 1776 1777 1778 1779

        if( i_start_pts == -1 )
        {
            i_start_pts = p_sys->i_pts;
        }
1780
        else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1781 1782 1783 1784
        {
            return 1;
        }
    }
1785 1786 1787
}


1788 1789 1790 1791

/*****************************************************************************
 * Stream managment
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
1792
vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1793
{
Laurent Aimar's avatar
Laurent Aimar committed
1794
    s = s_;
1795 1796
    mb_eof = VLC_FALSE;
}
Laurent Aimar's avatar
Laurent Aimar committed
1797

1798 1799
uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
{
Laurent Aimar's avatar
Laurent Aimar committed
1800
    if( i_size <= 0 || mb_eof )
1801 1802 1803 1804
    {
        return 0;
    }

Laurent Aimar's avatar
Laurent Aimar committed
1805
    return stream_Read( s, p_buffer, i_size );
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
}
void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
{
    int64_t i_pos;

    switch( mode )
    {
        case seek_beginning:
            i_pos = i_offset;
            break;
        case seek_end:
Laurent Aimar's avatar
Laurent Aimar committed
1817
            i_pos = stream_Size( s ) - i_offset;
1818 1819
            break;
        default:
Laurent Aimar's avatar
Laurent Aimar committed
1820
            i_pos= stream_Tell( s ) + i_offset;
1821 1822 1823
            break;
    }

Laurent Aimar's avatar
Laurent Aimar committed
1824
    if( i_pos < 0 || i_pos >= stream_Size( s ) )
1825 1826 1827 1828 1829 1830
    {
        mb_eof = VLC_TRUE;
        return;
    }

    mb_eof = VLC_FALSE;
Laurent Aimar's avatar
Laurent Aimar committed
1831
    if( stream_Seek( s, i_pos ) )
1832 1833 1834
    {
        mb_eof = VLC_TRUE;
    }
Laurent Aimar's avatar
Laurent Aimar committed
1835
    return;
1836 1837 1838 1839 1840 1841 1842
}
size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
{
    return 0;
}
uint64_t vlc_stream_io_callback::getFilePointer( void )
{
Laurent Aimar's avatar
Laurent Aimar committed
1843
    return stream_Tell( s );
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
}
void vlc_stream_io_callback::close( void )
{
    return;
}


/*****************************************************************************
 * Ebml Stream parser
 *****************************************************************************/
EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
{
    int i;

    m_es = es;
    m_got = NULL;
    m_el[0] = el_start;

    for( i = 1; i < 6; i++ )
    {
        m_el[i] = NULL;
    }
    mi_level = 1;
    mi_user_level = 1;
    mb_keep = VLC_FALSE;
}

EbmlParser::~EbmlParser( void )
{
    int i;

    for( i = 1; i < mi_level; i++ )
    {
        if( !mb_keep )
        {
            delete m_el[i];
        }
        mb_keep = VLC_FALSE;
    }
}

void EbmlParser::Up( void )
{
    if( mi_user_level == mi_level )
    {
        fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
    }

    mi_user_level--;
}

void EbmlParser::Down( void )
{
    mi_user_level++;
    mi_level++;
}

void EbmlParser::Keep( void )
{
    mb_keep = VLC_TRUE;
}

int EbmlParser::GetLevel( void )
{
    return mi_user_level;
}

EbmlElement *EbmlParser::Get( void )
{
    int i_ulev = 0;

    if( mi_user_level != mi_level )
    {
        return NULL;
    }
    if( m_got )
    {
        EbmlElement *ret = m_got;
        m_got = NULL;

        return ret;
    }

    if( m_el[mi_level] )
    {
        m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
        if( !mb_keep )
        {
            delete m_el[mi_level];
        }
        mb_keep = VLC_FALSE;
    }

    m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
    if( i_ulev > 0 )
    {
        while( i_ulev > 0 )
        {
            if( mi_level == 1 )
            {
                mi_level = 0;
                return NULL;
            }

            delete m_el[mi_level - 1];
            m_got = m_el[mi_level -1] = m_el[mi_level];
            m_el[mi_level] = NULL;

            mi_level--;
            i_ulev--;
        }
        return NULL;
    }
    else if( m_el[mi_level] == NULL )
    {
        fprintf( stderr," m_el[mi_level] == NULL\n" );
    }

    return m_el[mi_level];
}


/*****************************************************************************
 * Tools
 *  * LoadCues : load the cues element and update index
 *
 *  * LoadTags : load ... the tags element
 *
 *  * InformationsCreate : create all informations, load tags if present
 *
 *****************************************************************************/
static void LoadCues( input_thread_t *p_input )
{
    demux_sys_t *p_sys = p_input->p_demux_data;
    int64_t     i_sav_position = p_sys->in->getFilePointer();
    EbmlParser  *ep;
    EbmlElement *el, *cues;

    msg_Dbg( p_input, "loading cues" );
    p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
    cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);

    if( cues == NULL )
    {
        msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
        return;
    }

    ep = new EbmlParser( p_sys->es, cues );
    while( ( el = ep->Get() ) != NULL )
    {
        if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
        {
#define idx p_sys->index[p_sys->i_index]

            idx.i_track       = -1;
            idx.i_block_number= -1;
            idx.i_position    = -1;
            idx.i_time        = -1;
            idx.b_key         = VLC_TRUE;

            ep->Down();
            while( ( el = ep->Get() ) != NULL )
            {
                if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
                {
                    KaxCueTime &ctime = *(KaxCueTime*)el;

                    ctime.ReadData( p_sys->es->I_O() );

                    idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
                }
                else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
                {
                    ep->Down();
                    while( ( el = ep->Get() ) != NULL )
                    {
                        if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
                        {
                            KaxCueTrack &ctrack = *(KaxCueTrack*)el;

                            ctrack.ReadData( p_sys->es->I_O() );
                            idx.i_track = uint16( ctrack );
                        }
                        else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
                        {
                            KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;

                            ccpos.ReadData( p_sys->es->I_O() );
                            idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
                        }
                        else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
                        {
                            KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;

                            cbnum.ReadData( p_sys->es->I_O() );
                            idx.i_block_number = uint32( cbnum );
                        }
                        else
                        {
2044
                            msg_Dbg( p_input, "         * Unknown (%s)", typeid(*el).name() );
2045 2046 2047 2048 2049 2050
                        }
                    }
                    ep->Up();
                }
                else
                {
2051
                    msg_Dbg( p_input, "     * Unknown (%s)", typeid(*el).name() );
2052 2053 2054 2055
                }
            }
            ep->Up();

2056
#if 0
Gildas Bazin's avatar
 
Gildas Bazin committed
2057 2058 2059
            msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
                     " track=%d bnum=%d", idx.i_time, idx.i_position,
                     idx.i_track, idx.i_block_number );
2060
#endif
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071

            p_sys->i_index++;
            if( p_sys->i_index >= p_sys->i_index_max )
            {
                p_sys->i_index_max += 1024;
                p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
            }
#undef idx
        }
        else
        {
2072
            msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
        }
    }
    delete ep;
    delete cues;

    p_sys->b_cues = VLC_TRUE;

    msg_Dbg( p_input, "loading cues done." );
    p_sys->in->setFilePointer( i_sav_position, seek_beginning );
}

static void LoadTags( input_thread_t *p_input )
{
    demux_sys_t *p_sys = p_input->p_demux_data;
    int64_t     i_sav_position = p_sys->in->getFilePointer();
    EbmlParser  *ep;
    EbmlElement *el, *tags;

    msg_Dbg( p_input, "loading tags" );
    p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
    tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);

    if( tags == NULL )
    {
        msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
        return;
    }

    msg_Dbg( p_input, "Tags" );
    ep = new EbmlParser( p_sys->es, tags );
    while( ( el = ep->Get() ) != NULL )
    {
        if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
        {
            msg_Dbg( p_input, "+ Tag" );
            ep->Down();
            while( ( el = ep->Get() ) != NULL )
            {
                if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Targets" );
                    ep->Down();
                    while( ( el = ep->Get() ) != NULL )
                    {
                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
                    }
                    ep->Up();
                }
                else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + General" );
                    ep->Down();
                    while( ( el = ep->Get() ) != NULL )
                    {
                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
                    }
                    ep->Up();
                }
                else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Genres" );
                    ep->Down();
                    while( ( el = ep->Get() ) != NULL )
                    {
                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
                    }
                    ep->Up();
                }
                else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Audio Specific" );
                    ep->Down();
                    while( ( el = ep->Get() ) != NULL )
                    {
                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
                    }
                    ep->Up();
                }
                else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Images Specific" );
                    ep->Down();
                    while( ( el = ep->Get() ) != NULL )
                    {
                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
                    }
                    ep->Up();
                }
                else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Multi Comment" );
                }
                else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Multi Commercial" );
                }
                else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Multi Date" );
                }
                else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Multi Entity" );
                }
                else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Multi Identifier" );
                }
                else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Multi Legal" );
                }
                else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
                {
                    msg_Dbg( p_input, "|   + Multi Title" );
                }
                else
                {
                    msg_Dbg( p_input, "|   + Unknown (%s)", typeid( *el ).name() );
                }
            }
            ep->Up();
        }
        else
        {
            msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
        }
    }
    delete ep;
    delete tags;

    msg_Dbg( p_input, "loading tags done." );
    p_sys->in->setFilePointer( i_sav_position, seek_beginning );
}

static void InformationsCreate( input_thread_t *p_input )
{
    demux_sys_t           *p_sys = p_input->p_demux_data;
    int                   i_track;

2213
    p_sys->meta = vlc_meta_New();
2214 2215 2216

    if( p_sys->psz_title )
    {
2217
        vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2218 2219 2220
    }
    if( p_sys->psz_date_utc )
    {
2221
        vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2222 2223 2224
    }
    if( p_sys->psz_segment_filename )
    {
2225
        vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2226 2227 2228
    }
    if( p_sys->psz_muxing_application )
    {
2229
        vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2230 2231 2232
    }
    if( p_sys->psz_writing_application )
    {
2233
        vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2234 2235 2236 2237
    }

    for( i_track = 0; i_track < p_sys->i_track; i_track++ )
    {
2238 2239
        mkv_track_t *tk = &p_sys->track[i_track];
        vlc_meta_t *mtk = vlc_meta_New();
2240

2241 2242 2243
        TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, mtk );

        if( tk->fmt.psz_description )
2244
        {
2245
            vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2246
        }
2247
        if( tk->psz_codec_name )
2248
        {
2249
            vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2250
        }
2251
        if( tk->psz_codec_settings )
2252
        {
2253
            vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2254
        }
2255
        if( tk->psz_codec_info_url )
2256
        {
2257
            vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2258
        }
2259
        if( tk->psz_codec_download_url )
2260
        {
2261
            vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2262
        }
2263
    }
Laurent Aimar's avatar
Laurent Aimar committed
2264 2265

    if( p_sys->i_tags_position >= 0 )
2266
    {
Laurent Aimar's avatar
Laurent Aimar committed
2267 2268
        vlc_bool_t b_seekable;

2269
        stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
Laurent Aimar's avatar
Laurent Aimar committed
2270 2271 2272 2273
        if( b_seekable )
        {
            LoadTags( p_input );
        }
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
    }
}


/*****************************************************************************
 * Divers
 *****************************************************************************/

static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
{
    demux_sys_t    *p_sys   = p_input->p_demux_data;

#define idx p_sys->index[p_sys->i_index]
    idx.i_track       = -1;
    idx.i_block_number= -1;
    idx.i_position    = cluster->GetElementPosition();
    idx.i_time        = -1;
    idx.b_key         = VLC_TRUE;

    p_sys->i_index++;
    if( p_sys->i_index >= p_sys->i_index_max )
    {
        p_sys->i_index_max += 1024;
        p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
    }
#undef idx
}

static char * UTF8ToStr( const UTFstring &u )
{
    int     i_src;
    const wchar_t *src;
    char *dst, *p;

    i_src = u.length();
    src   = u.c_str();

    p = dst = (char*)malloc( i_src + 1);
    while( i_src > 0 )
    {
        if( *src < 255 )
        {
            *p++ = (char)*src;
        }
        else
        {
            *p++ = '?';
        }
        src++;
        i_src--;
    }
    *p++= '\0';

    return dst;
}