mp4.c 61.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * mp4.c : MP4 file input module for vlc
 *****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
4
 * Copyright (C) 2001-2004 VideoLAN
5
 * $Id$
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8 9 10 11
 * 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.
12
 *
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * 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>
#include <vlc/input.h>
30
#include <vlc_playlist.h>
31
#include "iso_lang.h"
32
#include "vlc_meta.h"
33

34 35
#include "libmp4.h"
#include "mp4.h"
36
#include "drms.h"
37 38 39 40

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
41 42
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
43

44
vlc_module_begin();
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
45
    set_description( _("MP4 stream demuxer") );
46
    set_capability( "demux", 242 );
47
    set_callbacks( Open, Close );
48 49
vlc_module_end();

50 51 52 53 54 55 56 57 58 59 60
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int    Demux   ( input_thread_t * );
static int    DemuxRef( input_thread_t *p_input )
{
    return 0;
}
static int   Seek     ( input_thread_t *, mtime_t );
static int   Control  ( input_thread_t *, int, va_list );

61
/*****************************************************************************
62
 * Declaration of local function
63
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
64 65
static void MP4_TrackCreate ( input_thread_t *, mp4_track_t *, MP4_Box_t  *);
static void MP4_TrackDestroy( input_thread_t *, mp4_track_t * );
66

Laurent Aimar's avatar
Laurent Aimar committed
67 68
static int  MP4_TrackSelect ( input_thread_t *, mp4_track_t *, mtime_t );
static void MP4_TrackUnselect(input_thread_t *, mp4_track_t * );
69

Laurent Aimar's avatar
Laurent Aimar committed
70
static int  MP4_TrackSeek   ( input_thread_t *, mp4_track_t *, mtime_t );
71

Laurent Aimar's avatar
Laurent Aimar committed
72 73 74
static uint64_t MP4_TrackGetPos    ( mp4_track_t * );
static int      MP4_TrackSampleSize( mp4_track_t * );
static int      MP4_TrackNextSample( input_thread_t *, mp4_track_t * );
75
static void     MP4_TrackSetELST( input_thread_t *, mp4_track_t *, int64_t );
Laurent Aimar's avatar
Laurent Aimar committed
76 77

/* Return time in s of a track */
78
static inline int64_t MP4_TrackGetPTS( input_thread_t *p_input, mp4_track_t *p_track )
Laurent Aimar's avatar
Laurent Aimar committed
79 80 81
{
    unsigned int i_sample;
    unsigned int i_index;
82
    int64_t i_dts;
Laurent Aimar's avatar
Laurent Aimar committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    i_sample = p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first;
    i_dts = p_track->chunk[p_track->i_chunk].i_first_dts;
    i_index = 0;
    while( i_sample > 0 )
    {
        if( i_sample > p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] )
        {
            i_dts += p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] *
                        p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
            i_sample -= p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index];
            i_index++;
        }
        else
        {
            i_dts += i_sample *
                        p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
            i_sample = 0;
            break;
        }
    }

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    /* now handle elst */
    if( p_track->p_elst )
    {
        demux_sys_t         *p_sys = p_input->p_demux_data;
        MP4_Box_data_elst_t *elst = p_track->p_elst->data.p_elst;

        /* convert to offset */
        if( ( elst->i_media_rate_integer[p_track->i_elst] > 0 ||
              elst->i_media_rate_fraction[p_track->i_elst] > 0 ) &&
            elst->i_media_time[p_track->i_elst] > 0 )
        {
            i_dts -= elst->i_media_time[p_track->i_elst];
        }

        /* add i_elst_time */
        i_dts += p_track->i_elst_time * p_track->i_timescale / p_sys->i_timescale;

        if( i_dts < 0 ) i_dts = 0;
    }

Laurent Aimar's avatar
Laurent Aimar committed
125 126 127 128 129 130
    return (int64_t)1000000 * i_dts / p_track->i_timescale;
}
static inline int64_t MP4_GetMoviePTS(demux_sys_t *p_sys )
{
    return (int64_t)1000000 * p_sys->i_time / p_sys->i_timescale;
}
131

Laurent Aimar's avatar
Laurent Aimar committed
132
#define FREE( p ) if( p ) { free( p ); (p) = NULL;}
133

134
/*****************************************************************************
135
 * Open: check file and initializes MP4 structures
136
 *****************************************************************************/
137
static int Open( vlc_object_t * p_this )
138
{
139
    input_thread_t  *p_input = (input_thread_t *)p_this;
140
    demux_sys_t     *p_sys;
141

142
    uint8_t         *p_peek;
143

144
    MP4_Box_t       *p_ftyp;
145
    MP4_Box_t       *p_rmra;
146 147
    MP4_Box_t       *p_mvhd;
    MP4_Box_t       *p_trak;
148

149
    unsigned int    i;
150
    vlc_bool_t      b_seekable;
151

152
    /* a little test to see if it could be a mp4 */
153
    if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
154 155
    {
        msg_Warn( p_input, "MP4 plugin discarded (cannot peek)" );
156
        return VLC_EGENERIC;
157
    }
158
    switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
159
    {
160 161 162 163 164 165 166 167 168
        case FOURCC_ftyp:
        case FOURCC_moov:
        case FOURCC_foov:
        case FOURCC_moof:
        case FOURCC_mdat:
        case FOURCC_udta:
        case FOURCC_free:
        case FOURCC_skip:
        case FOURCC_wide:
169
        case VLC_FOURCC( 'p', 'n', 'o', 't' ):
170 171 172
            break;
         default:
            msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
173
            return VLC_EGENERIC;
174
    }
175

176
    /* I need to seek */
177 178
    stream_Control( p_input->s, STREAM_CAN_SEEK, &b_seekable );
    if( !b_seekable )
179
    {
180 181
        msg_Warn( p_input, "MP4 plugin discarded (unseekable)" );
        return VLC_EGENERIC;
182
    }
183

184 185 186 187 188
    /*Set exported functions */
    p_input->pf_demux = Demux;
    p_input->pf_demux_control = Control;

    /* create our structure that will contains all data */
189 190
    p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
    memset( p_sys, 0, sizeof( demux_sys_t ) );
191 192

    /* Now load all boxes ( except raw data ) */
193
    if( ( p_sys->p_root = MP4_BoxGetRoot( p_input ) ) == NULL )
194 195
    {
        msg_Warn( p_input, "MP4 plugin discarded (not a valid file)" );
Laurent Aimar's avatar
Laurent Aimar committed
196
        goto error;
197 198
    }

199
    MP4_BoxDumpStructure( p_input, p_sys->p_root );
200

201
    if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
202 203 204 205
    {
        switch( p_ftyp->data.p_ftyp->i_major_brand )
        {
            case( FOURCC_isom ):
206
                msg_Dbg( p_input,
207 208
                         "ISO Media file (isom) version %d.",
                         p_ftyp->data.p_ftyp->i_minor_version );
209 210
                break;
            default:
211
                msg_Dbg( p_input,
212 213
                         "unrecognized major file specification (%4.4s).",
                          (char*)&p_ftyp->data.p_ftyp->i_major_brand );
214 215 216 217 218
                break;
        }
    }
    else
    {
219
        msg_Dbg( p_input, "file type box missing (assuming ISO Media file)" );
220 221 222
    }

    /* the file need to have one moov box */
223
    if( MP4_BoxCount( p_sys->p_root, "/moov" ) <= 0 )
224
    {
225
        MP4_Box_t *p_foov = MP4_BoxGet( p_sys->p_root, "/foov" );
226 227 228 229

        if( !p_foov )
        {
            msg_Err( p_input, "MP4 plugin discarded (no moov box)" );
230
            goto error;
231 232 233
        }
        /* we have a free box as a moov, rename it */
        p_foov->i_type = FOURCC_moov;
234 235
    }

236
    if( ( p_rmra = MP4_BoxGet( p_sys->p_root,  "/moov/rmra" ) ) )
237 238 239 240 241 242 243 244 245 246 247 248 249
    {
        playlist_t *p_playlist;
        int        i_count = MP4_BoxCount( p_rmra, "rmda" );
        int        i;

        msg_Dbg( p_input, "detected playlist mov file (%d ref)", i_count );

        p_playlist =
            (playlist_t *)vlc_object_find( p_input,
                                           VLC_OBJECT_PLAYLIST,
                                           FIND_ANYWHERE );
        if( p_playlist )
        {
250
            //p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
            for( i = 0; i < i_count; i++ )
            {
                MP4_Box_t *p_rdrf = MP4_BoxGet( p_rmra, "rmda[%d]/rdrf", i );
                char      *psz_ref;
                uint32_t  i_ref_type;

                if( !p_rdrf || !( psz_ref = p_rdrf->data.p_rdrf->psz_ref ) )
                {
                    continue;
                }
                i_ref_type = p_rdrf->data.p_rdrf->i_ref_type;

                msg_Dbg( p_input, "new ref=`%s' type=%4.4s",
                         psz_ref, (char*)&i_ref_type );

                if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
                {
                    if( strstr( psz_ref, "qt5gateQT" ) )
                    {
                        msg_Dbg( p_input, "ignoring pseudo ref =`%s'", psz_ref );
                        continue;
                    }
273 274
                    if( !strncmp( psz_ref, "http://", 7 ) ||
                        !strncmp( psz_ref, "rtsp://", 7 ) )
275 276
                    {
                        msg_Dbg( p_input, "adding ref = `%s'", psz_ref );
Clément Stenac's avatar
Clément Stenac committed
277
                        playlist_Add( p_playlist, psz_ref, psz_ref,
Gildas Bazin's avatar
 
Gildas Bazin committed
278
                                      PLAYLIST_APPEND, PLAYLIST_END );
279 280 281 282
                    }
                    else
                    {
                        /* msg dbg relative ? */
283 284
                        char *psz_absolute = alloca( strlen( p_input->psz_source ) + strlen( psz_ref ) + 1);
                        char *end = strrchr( p_input->psz_source, '/' );
285 286 287

                        if( end )
                        {
288
                            int i_len = end + 1 - p_input->psz_source;
289

290
                            strncpy( psz_absolute, p_input->psz_source, i_len);
291
                            psz_absolute[i_len] = '\0';
292 293 294 295 296 297 298
                        }
                        else
                        {
                            strcpy( psz_absolute, "" );
                        }
                        strcat( psz_absolute, psz_ref );
                        msg_Dbg( p_input, "adding ref = `%s'", psz_absolute );
Clément Stenac's avatar
Clément Stenac committed
299
                        playlist_Add( p_playlist, psz_absolute, psz_absolute,
Gildas Bazin's avatar
 
Gildas Bazin committed
300
                                      PLAYLIST_APPEND, PLAYLIST_END );
301 302 303 304
                    }
                }
                else
                {
Laurent Aimar's avatar
Laurent Aimar committed
305 306
                    msg_Err( p_input, "unknown ref type=%4.4s FIXME (send a bug report)",
                             (char*)&p_rdrf->data.p_rdrf->i_ref_type );
307 308 309 310 311 312 313 314 315 316
                }
            }
            vlc_object_release( p_playlist );
        }
        else
        {
            msg_Err( p_input, "can't find playlist" );
        }
    }

317
    if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
318
    {
319 320 321
        if( !p_rmra )
        {
            msg_Err( p_input, "cannot find /moov/mvhd" );
322
            goto error;
323 324 325 326
        }
        else
        {
            msg_Warn( p_input, "cannot find /moov/mvhd (pure ref file)" );
327
            p_input->pf_demux = DemuxRef;
328
            return VLC_SUCCESS;
329
        }
330 331 332
    }
    else
    {
333 334
        p_sys->i_timescale = p_mvhd->data.p_mvhd->i_timescale;
        p_sys->i_duration = p_mvhd->data.p_mvhd->i_duration;
335
    }
336

337
    if( !( p_sys->i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" ) ) )
338
    {
339
        msg_Err( p_input, "cannot find any /moov/trak" );
340
        goto error;
341
    }
342
    msg_Dbg( p_input, "find %d track%c",
343 344
                        p_sys->i_tracks,
                        p_sys->i_tracks ? 's':' ' );
345

346 347 348 349 350 351
    /*  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" );
352
        goto error;
353
    }
354
    if( p_sys->i_duration/p_sys->i_timescale > 0 )
355 356 357
    {
        p_input->stream.i_mux_rate =
            p_input->stream.p_selected_area->i_size / 50 /
358
            ( p_sys->i_duration / p_sys->i_timescale );
359 360 361 362 363 364 365 366
    }
    else
    {
        p_input->stream.i_mux_rate = 0;
    }
    vlc_mutex_unlock( &p_input->stream.stream_lock );


367
    /* allocate memory */
Laurent Aimar's avatar
Laurent Aimar committed
368 369
    p_sys->track = calloc( p_sys->i_tracks, sizeof( mp4_track_t ) );
    memset( p_sys->track, 0, p_sys->i_tracks * sizeof( mp4_track_t ) );
370 371

    /* now process each track and extract all usefull informations */
372
    for( i = 0; i < p_sys->i_tracks; i++ )
373
    {
374 375
        p_trak = MP4_BoxGet( p_sys->p_root, "/moov/trak[%d]", i );
        MP4_TrackCreate( p_input, &p_sys->track[i], p_trak );
376

377
        if( p_sys->track[i].b_ok )
378 379
        {
            char *psz_cat;
380
            switch( p_sys->track[i].fmt.i_cat )
381 382 383 384 385 386 387 388
            {
                case( VIDEO_ES ):
                    psz_cat = "video";
                    break;
                case( AUDIO_ES ):
                    psz_cat = "audio";
                    break;
                default:
389
                    psz_cat = "unknown";
390 391
                    break;
            }
392

393 394
            msg_Dbg( p_input, "adding track[Id 0x%x] %s (%s) language %s",
                            p_sys->track[i].i_track_ID,
395
                            psz_cat,
396 397
                            p_sys->track[i].b_enable ? "enable":"disable",
                            p_sys->track[i].fmt.psz_language ? p_sys->track[i].fmt.psz_language : "undef" );
398 399 400
        }
        else
        {
401
            msg_Dbg( p_input, "ignoring track[Id 0x%x]", p_sys->track[i].i_track_ID );
402 403 404
        }

    }
405 406 407
    return VLC_SUCCESS;

error:
408
    if( p_sys->p_root )
Laurent Aimar's avatar
Laurent Aimar committed
409
    {
410
        MP4_BoxFree( p_input, p_sys->p_root );
Laurent Aimar's avatar
Laurent Aimar committed
411
    }
412
    free( p_sys );
413
    return VLC_EGENERIC;
414 415 416
}

/*****************************************************************************
417
 * Demux: read packet and send them to decoders
418 419
 *****************************************************************************
 * TODO check for newly selected track (ie audio upt to now )
420
 *****************************************************************************/
421
static int Demux( input_thread_t *p_input )
422
{
423
    demux_sys_t *p_sys = p_input->p_demux_data;
424
    unsigned int i_track;
425

426 427 428 429 430

    unsigned int i_track_selected;
    vlc_bool_t   b_play_audio;

    /* check for newly selected/unselected track */
431
    for( i_track = 0, i_track_selected = 0; i_track <  p_sys->i_tracks; i_track++ )
432
    {
Laurent Aimar's avatar
Laurent Aimar committed
433 434 435
        mp4_track_t *tk = &p_sys->track[i_track];

        if( tk->b_selected && tk->i_sample >= tk->i_sample_count )
436
        {
Laurent Aimar's avatar
Laurent Aimar committed
437 438
            msg_Warn( p_input, "track[0x%x] will be disabled", tk->i_track_ID );
            MP4_TrackUnselect( p_input, tk);
439
        }
Laurent Aimar's avatar
Laurent Aimar committed
440
        else if( tk->b_ok )
441
        {
442
            vlc_bool_t b;
Laurent Aimar's avatar
Laurent Aimar committed
443
            es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
444

Laurent Aimar's avatar
Laurent Aimar committed
445
            if( tk->b_selected && !b )
446
            {
Laurent Aimar's avatar
Laurent Aimar committed
447
                MP4_TrackUnselect( p_input, tk );
448
            }
Laurent Aimar's avatar
Laurent Aimar committed
449
            else if( !tk->b_selected && b)
450
            {
Laurent Aimar's avatar
Laurent Aimar committed
451
                MP4_TrackSelect( p_input, tk, MP4_GetMoviePTS( p_sys ) );
452 453
            }

Laurent Aimar's avatar
Laurent Aimar committed
454
            if( tk->b_selected )
455 456 457 458 459 460 461 462 463
            {
                i_track_selected++;
            }
        }
    }

    if( i_track_selected <= 0 )
    {
        msg_Warn( p_input, "no track selected, exiting..." );
Laurent Aimar's avatar
Laurent Aimar committed
464
        return 0;
465 466
    }

467 468 469
    /* first wait for the good time to read a packet */
    input_ClockManageRef( p_input,
                          p_input->stream.p_selected_program,
470
                          p_sys->i_pcr );
471 472

    /* update pcr XXX in mpeg scale so in 90000 unit/s */
473
    p_sys->i_pcr = MP4_GetMoviePTS( p_sys ) * 9 / 100;
474 475

    /* we will read 100ms for each stream so ...*/
476
    p_sys->i_time += __MAX( p_sys->i_timescale / 10 , 1 );
477

Gildas Bazin's avatar
 
Gildas Bazin committed
478 479
    /* Check if we need to send the audio data to decoder */
    b_play_audio = !p_input->stream.control.b_mute;
480

481
    for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
482
    {
Laurent Aimar's avatar
Laurent Aimar committed
483 484 485
        mp4_track_t *tk = &p_sys->track[i_track];

        if( !tk->b_ok || !tk->b_selected )
486
        {
487
            continue;
488
        }
489

490
        while( MP4_TrackGetPTS( p_input, tk ) < MP4_GetMoviePTS( p_sys ) )
491
        {
492 493 494 495 496 497
#if 0
            msg_Dbg( p_input, "tk=%lld mv=%lld",
                     MP4_TrackGetPTS( p_input, tk ),
                     MP4_GetMoviePTS( p_sys ) );
#endif

Laurent Aimar's avatar
Laurent Aimar committed
498 499
            if( MP4_TrackSampleSize( tk ) > 0 &&
                ( b_play_audio || tk->fmt.i_cat != AUDIO_ES ) )
500
            {
501
                block_t *p_block;
502

503
                /* go,go go ! */
Laurent Aimar's avatar
Laurent Aimar committed
504
                if( stream_Seek( p_input->s, MP4_TrackGetPos( tk ) ) )
505
                {
Laurent Aimar's avatar
Laurent Aimar committed
506 507
                    msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
                    MP4_TrackUnselect( p_input, tk );
508 509 510
                    break;
                }

Laurent Aimar's avatar
Laurent Aimar committed
511
                /* now read pes */
512
                if( ( p_block = stream_Block( p_input->s,
Laurent Aimar's avatar
Laurent Aimar committed
513
                                              MP4_TrackSampleSize( tk ) ) ) == NULL )
514
                {
Laurent Aimar's avatar
Laurent Aimar committed
515 516
                    msg_Warn( p_input, "track[0x%x] will be disabled (eof?)", tk->i_track_ID );
                    MP4_TrackUnselect( p_input, tk );
517 518
                    break;
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
519

Laurent Aimar's avatar
Laurent Aimar committed
520
                if( tk->b_drms && tk->p_drms )
521
                {
Laurent Aimar's avatar
Laurent Aimar committed
522 523
                    drms_decrypt( tk->p_drms,
                                  (uint32_t*)p_block->p_buffer,
524 525
                                  p_block->i_buffer );
                }
Laurent Aimar's avatar
Laurent Aimar committed
526 527 528
                p_block->i_dts =
                    input_ClockGetTS( p_input,
                                      p_input->stream.p_selected_program,
529
                                      MP4_TrackGetPTS( p_input, tk ) * 9/100 );
530

Laurent Aimar's avatar
Laurent Aimar committed
531
                p_block->i_pts = tk->fmt.i_cat == VIDEO_ES ? 0 : p_block->i_dts;
532

Laurent Aimar's avatar
Laurent Aimar committed
533
                if( !tk->b_drms || ( tk->b_drms && tk->p_drms ) )
534
                {
Laurent Aimar's avatar
Laurent Aimar committed
535
                    es_out_Send( p_input->p_es_out, tk->p_es, p_block );
536
                }
Laurent Aimar's avatar
Laurent Aimar committed
537
            }
538

Laurent Aimar's avatar
Laurent Aimar committed
539 540 541 542
            /* Next sample */
            if( MP4_TrackNextSample( p_input, tk ) )
            {
                break;
543
            }
544 545 546
        }
    }

Laurent Aimar's avatar
Laurent Aimar committed
547
    return 1;
548
}
549
/*****************************************************************************
550
 * Seek: Got to i_date
551
 ******************************************************************************/
552
static int   Seek     ( input_thread_t *p_input, mtime_t i_date )
553
{
554
    demux_sys_t *p_sys = p_input->p_demux_data;
555
    unsigned int i_track;
Laurent Aimar's avatar
Laurent Aimar committed
556

557
    /* First update update global time */
558 559
    p_sys->i_time = i_date * p_sys->i_timescale / 1000000;
    p_sys->i_pcr  = i_date* 9 / 100;
560

561
    /* Now for each stream try to go to this time */
562
    for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
563
    {
Laurent Aimar's avatar
Laurent Aimar committed
564 565 566
        mp4_track_t *tk = &p_sys->track[i_track];

        if( tk->b_ok && tk->b_selected )
567
        {
Laurent Aimar's avatar
Laurent Aimar committed
568
            MP4_TrackSeek( p_input, tk, i_date );
569
        }
570 571 572
    }
    return( 1 );
}
573 574

/*****************************************************************************
575
 * Control:
576
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
577
static int Control( input_thread_t *p_input, int i_query, va_list args )
578 579 580
{
    demux_sys_t *p_sys = p_input->p_demux_data;

Gildas Bazin's avatar
 
Gildas Bazin committed
581
    double f, *pf;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    int64_t i64, *pi64;

    switch( i_query )
    {
        case DEMUX_GET_POSITION:
            pf = (double*)va_arg( args, double * );
            if( p_sys->i_duration > 0 )
            {
                *pf = (double)p_sys->i_time / (double)p_sys->i_duration;
            }
            else
            {
                *pf = 0.0;
            }
            return VLC_SUCCESS;

        case DEMUX_SET_POSITION:
            f = (double)va_arg( args, double );
Gildas Bazin's avatar
 
Gildas Bazin committed
600 601 602 603 604 605 606 607
            if( p_sys->i_timescale > 0 )
            {
                i64 = (int64_t)( f * (double)1000000 *
                                 (double)p_sys->i_duration /
                                 (double)p_sys->i_timescale );
                return Seek( p_input, i64 );
            }
            else return VLC_SUCCESS;
608 609 610

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
611 612 613 614 615 616 617
            if( p_sys->i_timescale > 0 )
            {
                *pi64 = (mtime_t)1000000 *
                        (mtime_t)p_sys->i_time /
                        (mtime_t)p_sys->i_timescale;
            }
            else *pi64 = 0;
618 619 620 621 622 623 624 625
            return VLC_SUCCESS;

        case DEMUX_SET_TIME:
            i64 = (int64_t)va_arg( args, int64_t );
            return Seek( p_input, i64 );

        case DEMUX_GET_LENGTH:
            pi64 = (int64_t*)va_arg( args, int64_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
626 627 628 629 630 631 632
            if( p_sys->i_timescale > 0 )
            {
                *pi64 = (mtime_t)1000000 *
                        (mtime_t)p_sys->i_duration /
                        (mtime_t)p_sys->i_timescale;
            }
            else *pi64 = 0;
633
            return VLC_SUCCESS;
Gildas Bazin's avatar
 
Gildas Bazin committed
634

635 636 637
        case DEMUX_GET_FPS:
            msg_Warn( p_input, "DEMUX_GET_FPS unimplemented !!" );
            return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
638

639
        case DEMUX_GET_META:
640 641 642 643 644 645 646 647 648 649
        {
            vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
            vlc_meta_t *meta;
            MP4_Box_t  *p_udta   = MP4_BoxGet( p_sys->p_root, "/moov/udta" );
            MP4_Box_t  *p_0xa9xxx;
            if( p_udta == NULL )
            {
                return VLC_EGENERIC;
            }
            *pp_meta = meta = vlc_meta_New();
650 651
            for( p_0xa9xxx = p_udta->p_first; p_0xa9xxx != NULL;
                 p_0xa9xxx = p_0xa9xxx->p_next )
652 653 654
            {
                switch( p_0xa9xxx->i_type )
                {
655 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
                case FOURCC_0xa9nam:
                    vlc_meta_Add( meta, VLC_META_TITLE,
                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
                    break;
                case FOURCC_0xa9aut:
                    vlc_meta_Add( meta, VLC_META_AUTHOR,
                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
                    break;
                case FOURCC_0xa9ART:
                    vlc_meta_Add( meta, VLC_META_ARTIST,
                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
                    break;
                case FOURCC_0xa9cpy:
                    vlc_meta_Add( meta, VLC_META_COPYRIGHT,
                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
                    break;
                case FOURCC_0xa9day:
                    vlc_meta_Add( meta, VLC_META_DATE,
                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
                    break;
                case FOURCC_0xa9des:
                    vlc_meta_Add( meta, VLC_META_DESCRIPTION,
                                  p_0xa9xxx->data.p_0xa9xxx->psz_text );
                    break;

                case FOURCC_0xa9swr:
                case FOURCC_0xa9inf:
                case FOURCC_0xa9dir:
                case FOURCC_0xa9cmt:
                case FOURCC_0xa9req:
                case FOURCC_0xa9fmt:
                case FOURCC_0xa9prd:
                case FOURCC_0xa9prf:
                case FOURCC_0xa9src:
                    /* TODO one day, but they aren't really meaningfull */
                    break;

                default:
                    break;
694 695 696 697
                }
            }
            return VLC_SUCCESS;
        }
698

699 700 701 702 703 704 705 706 707 708
        default:
            msg_Err( p_input, "control query unimplemented !!!" );
            return demux_vaControlDefault( p_input, i_query, args );
    }
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close ( vlc_object_t * p_this )
709 710
{
    unsigned int i_track;
711
    input_thread_t *  p_input = (input_thread_t *)p_this;
712
    demux_sys_t *p_sys = p_input->p_demux_data;
713

714
    msg_Dbg( p_input, "freeing all memory" );
Laurent Aimar's avatar
Laurent Aimar committed
715

716 717
    MP4_BoxFree( p_input, p_sys->p_root );
    for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
718
    {
719
        MP4_TrackDestroy( p_input, &p_sys->track[i_track] );
720
    }
721
    FREE( p_sys->track );
722

723
    free( p_sys );
724 725 726
}


727

728 729 730 731
/****************************************************************************
 * Local functions, specific to vlc
 ****************************************************************************/

732 733
/* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
static int TrackCreateChunksIndex( input_thread_t *p_input,
Laurent Aimar's avatar
Laurent Aimar committed
734
                                   mp4_track_t *p_demux_track )
735
{
736 737
    MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
    MP4_Box_t *p_stsc;
738

739 740 741 742 743 744
    unsigned int i_chunk;
    unsigned int i_index, i_last;

    if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
          !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
        ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
745
    {
746
        return( VLC_EGENERIC );
747
    }
748 749 750

    p_demux_track->i_chunk_count = p_co64->data.p_co64->i_entry_count;
    if( !p_demux_track->i_chunk_count )
751
    {
752 753
        msg_Warn( p_input, "no chunk defined" );
        return( VLC_EGENERIC );
754
    }
755
    p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
Laurent Aimar's avatar
Laurent Aimar committed
756
                                   sizeof( mp4_chunk_t ) );
757 758 759

    /* first we read chunk offset */
    for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
760
    {
761 762
        p_demux_track->chunk[i_chunk].i_offset =
                p_co64->data.p_co64->i_chunk_offset[i_chunk];
763 764
    }

765 766 767 768 769 770
    /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
        to be used for the sample XXX begin to 1
        We construct it begining at the end */
    i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
    i_index = p_stsc->data.p_stsc->i_entry_count;
    if( !i_index )
771
    {
772 773
        msg_Warn( p_input, "cannot read chunk table or table empty" );
        return( VLC_EGENERIC );
774
    }
775 776

    while( i_index )
777
    {
778 779 780 781 782 783 784 785 786 787
        i_index--;
        for( i_chunk = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
                i_chunk < i_last; i_chunk++ )
        {
            p_demux_track->chunk[i_chunk].i_sample_description_index =
                    p_stsc->data.p_stsc->i_sample_description_index[i_index];
            p_demux_track->chunk[i_chunk].i_sample_count =
                    p_stsc->data.p_stsc->i_samples_per_chunk[i_index];
        }
        i_last = p_stsc->data.p_stsc->i_first_chunk[i_index] - 1;
788 789
    }

790 791
    p_demux_track->chunk[0].i_sample_first = 0;
    for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
792
    {
793 794 795
        p_demux_track->chunk[i_chunk].i_sample_first =
            p_demux_track->chunk[i_chunk-1].i_sample_first +
                p_demux_track->chunk[i_chunk-1].i_sample_count;
796
    }
797

798 799 800
    msg_Dbg( p_input,
             "track[Id 0x%x] read %d chunk",
             p_demux_track->i_track_ID,
801
             p_demux_track->i_chunk_count );
802

803
    return( VLC_SUCCESS );
804
}
805
static int TrackCreateSamplesIndex( input_thread_t *p_input,
Laurent Aimar's avatar
Laurent Aimar committed
806
                                    mp4_track_t *p_demux_track )
807 808
{
    MP4_Box_t *p_stts; /* makes mapping between sample and decoding time,
809
                          ctts make same mapping but for composition time,
810
                          not yet used and probably not usefull */
811 812
    MP4_Box_t *p_stsz; /* gives sample size of each samples, there is also stz2
                          that uses a compressed form FIXME make them in libmp4
813 814 815
                          as a unique type */
    /* TODO use also stss and stsh table for seeking */
    /* FIXME use edit table */
816 817
    int64_t i_sample;
    int64_t i_chunk;
818

819 820
    int64_t i_index;
    int64_t i_index_sample_used;
821

822 823
    int64_t i_last_dts;

824 825
    p_stts = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
    p_stsz = MP4_BoxGet( p_demux_track->p_stbl, "stsz" ); /* FIXME and stz2 */
826 827 828 829

    if( ( !p_stts )||( !p_stsz ) )
    {
        msg_Warn( p_input, "cannot read sample table" );
830
        return( VLC_EGENERIC );
831
    }
832

833 834 835 836 837 838 839 840 841 842 843 844 845 846
    p_demux_track->i_sample_count = p_stsz->data.p_stsz->i_sample_count;


    /* for sample size, there are 2 case */
    if( p_stsz->data.p_stsz->i_sample_size )
    {
        /* 1: all sample have the same size, so no need to construct a table */
        p_demux_track->i_sample_size = p_stsz->data.p_stsz->i_sample_size;
        p_demux_track->p_sample_size = NULL;
    }
    else
    {
        /* 2: each sample can have a different size */
        p_demux_track->i_sample_size = 0;
847
        p_demux_track->p_sample_size =
848
            calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
849

850 851
        for( i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
        {
852
            p_demux_track->p_sample_size[i_sample] =
853 854 855 856 857 858 859
                    p_stsz->data.p_stsz->i_entry_size[i_sample];
        }
    }
    /* we have extract all information from stsz,
        now use stts */

    /* if we don't want to waste too much memory, we can't expand
860
       the box !, so each chunk will contain an "extract" of this table
861
       for fast research */
862

863 864
    i_last_dts = 0;
    i_index = 0; i_index_sample_used =0;
865

866 867 868 869
    /* create and init last data for each chunk */
    for(i_chunk = 0 ; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
    {

870
        int64_t i_entry, i_sample_count, i;
871 872
        /* save last dts */
        p_demux_track->chunk[i_chunk].i_first_dts = i_last_dts;
873
    /* count how many entries needed for this chunk
874 875 876
       for p_sample_delta_dts and p_sample_count_dts */

        i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
877 878

        i_entry = 0;
879 880 881 882 883
        while( i_sample_count > 0 )
        {
            i_sample_count -= p_stts->data.p_stts->i_sample_count[i_index+i_entry];
            if( i_entry == 0 )
            {
884
                i_sample_count += i_index_sample_used; /* don't count already used sample
885 886 887 888
                                                   int this entry */
            }
            i_entry++;
        }
889

890
        /* allocate them */
891
        p_demux_track->chunk[i_chunk].p_sample_count_dts =
892
            calloc( i_entry, sizeof( uint32_t ) );
893
        p_demux_track->chunk[i_chunk].p_sample_delta_dts =
894
            calloc( i_entry, sizeof( uint32_t ) );
895 896 897 898 899

        /* now copy */
        i_sample_count = p_demux_track->chunk[i_chunk].i_sample_count;
        for( i = 0; i < i_entry; i++ )
        {
900 901
            int64_t i_used;
            int64_t i_rest;
902

903 904 905 906 907
            i_rest = p_stts->data.p_stts->i_sample_count[i_index] - i_index_sample_used;

            i_used = __MIN( i_rest, i_sample_count );

            i_index_sample_used += i_used;
908
            i_sample_count -= i_used;
909 910 911 912 913

            p_demux_track->chunk[i_chunk].p_sample_count_dts[i] = i_used;

            p_demux_track->chunk[i_chunk].p_sample_delta_dts[i] =
                        p_stts->data.p_stts->i_sample_delta[i_index];
914 915

            i_last_dts += i_used *
916 917 918 919 920
                    p_demux_track->chunk[i_chunk].p_sample_delta_dts[i];

            if(<