Coverage Report

Created: 2026-10-17 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/mp4/mp4.c
Line
Count
Source
1
/*****************************************************************************
2
 * mp4.c : MP4 file input module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2001-2004, 2010 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
#ifdef HAVE_CONFIG_H
23
# include "config.h"
24
#endif
25
26
/*****************************************************************************
27
 * Preamble
28
 *****************************************************************************/
29
#include "mp4.h"
30
31
#include <vlc_arrays.h>
32
#include <vlc_demux.h>
33
#include <vlc_charset.h>                           /* EnsureUTF8 */
34
#include <vlc_input.h>
35
#include <vlc_aout.h>
36
#include <vlc_plugin.h>
37
#include <vlc_dialog.h>
38
#include <vlc_url.h>
39
#include <vlc_replay_gain.h>
40
#include <assert.h>
41
#include <limits.h>
42
#include <stdckdint.h>
43
#include <math.h>
44
#include "meta.h"
45
#include "attachments.h"
46
#include "heif.h"
47
#include "../../codec/cc.h"
48
#include "../av1_unpack.h"
49
50
#include <stdlib.h>
51
52
/*****************************************************************************
53
 * Module descriptor
54
 *****************************************************************************/
55
static int  Open ( vlc_object_t * );
56
static void Close( vlc_object_t * );
57
58
#define CFG_PREFIX "mp4-"
59
60
#define MP4_M4A_TEXT     N_("M4A audio only")
61
#define MP4_M4A_LONGTEXT N_("Ignore non audio tracks from iTunes audio files")
62
63
#define MP4_ELST_TEXT       N_("Handle edit list")
64
65
#define HEIF_DURATION_TEXT N_("Duration in seconds")
66
#define HEIF_DURATION_LONGTEXT N_( \
67
    "Duration in seconds before simulating an end of file. " \
68
    "A negative value means an unlimited play time.")
69
70
174
vlc_module_begin ()
71
87
    set_subcategory( SUBCAT_INPUT_DEMUX )
72
87
    set_description( N_("MP4 stream demuxer") )
73
87
    set_shortname( N_("MP4") )
74
87
    set_capability( "demux", 240 )
75
174
    set_callbacks( Open, Close )
76
87
    add_file_extension("m4a")
77
87
    add_file_extension("m4v")
78
87
    add_file_extension("moov")
79
87
    add_file_extension("mov")
80
87
    add_file_extension("mp4")
81
82
87
    set_section("Hacks", NULL)
83
87
    add_bool( CFG_PREFIX"m4a-audioonly", false, MP4_M4A_TEXT, MP4_M4A_LONGTEXT )
84
87
    add_bool( CFG_PREFIX"editlist", true, MP4_ELST_TEXT, MP4_ELST_TEXT )
85
86
87
    add_submodule()
87
87
        set_subcategory( SUBCAT_INPUT_DEMUX )
88
87
        set_description( N_("HEIF demuxer") )
89
87
        set_shortname( "heif" )
90
87
        add_shortcut( "heif" )
91
87
        set_capability( "demux", 239 )
92
174
        set_callbacks( OpenHEIF, CloseHEIF )
93
87
        set_section( N_("HEIF demuxer"), NULL )
94
87
        add_float( "heif-image-duration", HEIF_DEFAULT_DURATION,
95
87
                   HEIF_DURATION_TEXT, HEIF_DURATION_LONGTEXT )
96
87
            change_safe()
97
87
vlc_module_end ()
98
99
/*****************************************************************************
100
 * Local prototypes
101
 *****************************************************************************/
102
static int   Demux   ( demux_t * );
103
0
static int   DemuxRef( demux_t *p_demux ){ (void)p_demux; return 0;}
104
static int   DemuxFrag( demux_t * );
105
static int   Control ( demux_t *, int, va_list );
106
107
typedef struct
108
{
109
    MP4_Box_t    *p_root;      /* container for the whole file */
110
111
    vlc_tick_t   i_pcr;
112
113
    uint64_t     i_moov_duration;
114
    uint64_t     i_duration;           /* Declared fragmented duration (movie time scale) */
115
    uint64_t     i_cumulated_duration; /* Same as above, but not from probing, (movie time scale) */
116
    uint32_t     i_timescale;          /* movie time scale */
117
    vlc_tick_t   i_nztime;             /* time position of the presentation (CLOCK_FREQ timescale) */
118
    vlc_tick_t   i_max_pts_offset;
119
    unsigned int i_tracks;       /* number of tracks */
120
    mp4_track_t  *track;         /* array of track */
121
    float        f_fps;          /* number of frame per seconds */
122
123
    bool         b_quicktime;
124
    bool         b_fragmented;   /* fMP4 */
125
    bool         b_seekable;
126
    bool         b_fastseekable;
127
    bool         b_error;        /* unrecoverable */
128
129
    bool            b_index_probed;     /* mFra sync points index */
130
    bool            b_fragments_probed; /* moof segments index created */
131
132
    MP4_Box_t *p_moov;
133
134
    struct
135
    {
136
        uint32_t        i_current_box_type;
137
        MP4_Box_t      *p_fragment_atom;
138
        uint64_t        i_post_mdat_offset;
139
        uint32_t        i_lastseqnumber;
140
    } context;
141
142
    /* */
143
    bool seekpoint_changed;
144
    int          i_seekpoint;
145
    input_title_t *p_title;
146
    vlc_meta_t    *p_meta;
147
    struct
148
    {
149
        uint32_t i_delay_samples;
150
        float f_replay_gain_norm;
151
        float f_replay_gain_peak;
152
        uint32_t i_target_bitrate;
153
    } qt;
154
155
    /* ASF in MP4 */
156
    asf_packet_sys_t asfpacketsys;
157
    vlc_tick_t i_preroll;       /* foobar */
158
    vlc_tick_t i_preroll_start;
159
160
    struct
161
    {
162
        enum es_format_category_e es_cat_filter;
163
    } hacks;
164
165
    mp4_fragments_index_t *p_fragsindex;
166
167
    ssize_t i_attachments;
168
    input_attachment_t **pp_attachments;
169
} demux_sys_t;
170
171
634
#define DEMUX_INCREMENT VLC_TICK_FROM_MS(250) /* How far the pcr will go, each round */
172
0
#define DEMUX_TRACK_MAX_PRELOAD VLC_TICK_FROM_SEC(15) /* maximum preloading, to deal with interleaving */
173
174
65
#define INVALID_PRELOAD  UINT_MAX
175
259
#define UNKNOWN_DELTA    UINT32_MAX
176
64
#define INVALID_PTS      VLC_TICK_MIN
177
178
445
#define VLC_DEMUXER_EOS (VLC_DEMUXER_EGENERIC - 1)
179
0
#define VLC_DEMUXER_FATAL (VLC_DEMUXER_EGENERIC - 2)
180
181
/*****************************************************************************
182
 * Declaration of local function
183
 *****************************************************************************/
184
static void MP4_TrackSetup( demux_t *, mp4_track_t *, const MP4_Box_t  *, bool, bool );
185
static void MP4_TrackInit( mp4_track_t *, const MP4_Box_t * );
186
static void MP4_TrackClean( es_out_t *, mp4_track_t * );
187
188
static void MP4_Block_Send( demux_t *, mp4_track_t *, block_t * );
189
190
static void MP4_TrackSelect  ( demux_t *, mp4_track_t *, bool );
191
static int  MP4_TrackSeek   ( demux_t *, mp4_track_t *, vlc_tick_t );
192
193
static uint64_t MP4_TrackGetPos    ( mp4_track_t * );
194
static uint32_t MP4_TrackGetReadSize( mp4_track_t *, uint32_t * );
195
static int      MP4_TrackNextSample( demux_t *, mp4_track_t *, uint32_t );
196
static void     MP4_TrackSetELST( demux_t *, mp4_track_t *, vlc_tick_t );
197
static void TrackUpdateSampleAndTimes( mp4_track_t *p_track );
198
199
static void     MP4_UpdateSeekpoint( demux_t *, vlc_tick_t );
200
201
static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id );
202
static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov,
203
                                           const MP4_Box_data_tfhd_t *p_tfhd_data,
204
                                           uint32_t *pi_default_size,
205
                                           uint32_t *pi_default_duration );
206
207
static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID );
208
209
static int  ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented );
210
static int  ProbeFragmentsChecked( demux_t *p_demux );
211
static int  ProbeIndex( demux_t *p_demux );
212
213
static int FragCreateTrunIndex( demux_t *, MP4_Box_t *, MP4_Box_t *, stime_t );
214
215
static int FragGetMoofBySidxIndex( demux_t *p_demux, vlc_tick_t i_target_time,
216
                                   uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime );
217
static int FragGetMoofByTfraIndex( demux_t *p_demux, const vlc_tick_t i_target_time, unsigned i_track_ID,
218
                                   uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime );
219
static void FragResetContext( demux_sys_t * );
220
221
/* ASF Handlers */
222
static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number );
223
static void MP4ASF_Send(asf_packet_sys_t *p_packetsys, uint8_t i_stream_number, block_t **pp_frame);
224
static void MP4ASF_ResetFrames( demux_sys_t *p_sys );
225
226
/* RTP Hint track */
227
static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec );
228
static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount );
229
230
static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta );
231
232
/* Helpers */
233
234
static int64_t MP4_rescale( int64_t i_value, uint32_t i_timescale, uint32_t i_newscale )
235
637
{
236
637
    if( i_timescale == i_newscale )
237
0
        return i_value;
238
239
637
    if( i_value <= INT64_MAX / i_newscale )
240
637
        return i_value * i_newscale / i_timescale;
241
242
    /* overflow */
243
0
    int64_t q = i_value / i_timescale;
244
0
    int64_t r = i_value % i_timescale;
245
0
    return q * i_newscale + r * i_newscale / i_timescale;
246
637
}
247
248
static vlc_tick_t MP4_rescale_mtime( int64_t i_value, uint32_t i_timescale )
249
636
{
250
636
    return MP4_rescale(i_value, i_timescale, CLOCK_FREQ);
251
636
}
252
253
static int64_t MP4_rescale_qtime( vlc_tick_t i_value, uint32_t i_timescale )
254
1
{
255
1
    return MP4_rescale(i_value, CLOCK_FREQ, i_timescale);
256
1
}
257
258
static uint32_t stream_ReadU32( stream_t *s, void *p_read, uint32_t i_toread )
259
0
{
260
0
    ssize_t i_return = 0;
261
0
    if ( i_toread > INT32_MAX )
262
0
    {
263
0
        i_return = vlc_stream_Read( s, p_read, (size_t) INT32_MAX );
264
0
        if ( i_return < INT32_MAX )
265
0
            return i_return;
266
0
        else
267
0
            i_toread -= INT32_MAX;
268
0
    }
269
0
    i_return += vlc_stream_Read( s, (uint8_t *)p_read + i_return, (size_t) i_toread );
270
0
    return i_return;
271
0
}
272
273
static int MP4_reftypeToFlag( vlc_fourcc_t i_reftype )
274
0
{
275
0
    switch( i_reftype )
276
0
    {
277
0
        case ATOM_chap:
278
0
            return USEAS_CHAPTERS;
279
0
        case VLC_FOURCC('t','m','c','d'):
280
0
            return USEAS_TIMECODE;
281
0
        default:
282
0
            return USEAS_NONE;
283
0
    }
284
0
}
285
286
static bool MP4_isMetadata( const mp4_track_t *tk )
287
638
{
288
638
    return tk->i_use_flags & (USEAS_CHAPTERS|USEAS_TIMECODE);
289
638
}
290
291
static MP4_Box_t * MP4_GetTrexByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
292
0
{
293
0
    if(!p_moov)
294
0
        return NULL;
295
0
    MP4_Box_t *p_trex = MP4_BoxGet( p_moov, "mvex/trex" );
296
0
    while( p_trex )
297
0
    {
298
0
        if ( p_trex->i_type == ATOM_trex &&
299
0
             BOXDATA(p_trex) && BOXDATA(p_trex)->i_track_ID == i_id )
300
0
                break;
301
0
        else
302
0
            p_trex = p_trex->p_next;
303
0
    }
304
0
    return p_trex;
305
0
}
306
307
/**
308
 * Return the track identified by tid
309
 */
310
static mp4_track_t *MP4_GetTrackByTrackID( demux_t *p_demux, const uint32_t tid )
311
0
{
312
0
    demux_sys_t *p_sys = p_demux->p_sys;
313
314
0
    mp4_track_t *ret = NULL;
315
0
    for( unsigned i = 0; i < p_sys->i_tracks; i++ )
316
0
    {
317
0
        ret = &p_sys->track[i];
318
0
        if( ret->i_track_ID == tid )
319
0
            return ret;
320
0
    }
321
0
    return NULL;
322
0
}
323
324
static MP4_Box_t * MP4_GetTrakByTrackID( MP4_Box_t *p_moov, const uint32_t i_id )
325
0
{
326
0
    MP4_Box_t *p_trak = MP4_BoxGet( p_moov, "trak" );
327
0
    MP4_Box_t *p_tkhd;
328
0
    while( p_trak )
329
0
    {
330
0
        if( p_trak->i_type == ATOM_trak &&
331
0
            (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) && BOXDATA(p_tkhd) &&
332
0
            BOXDATA(p_tkhd)->i_track_ID == i_id )
333
0
                break;
334
0
        else
335
0
            p_trak = p_trak->p_next;
336
0
    }
337
0
    return p_trak;
338
0
}
339
340
static MP4_Box_t * MP4_GetTrafByTrackID( MP4_Box_t *p_moof, const uint32_t i_id )
341
0
{
342
0
    MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
343
0
    MP4_Box_t *p_tfhd;
344
0
    while( p_traf )
345
0
    {
346
0
        if( p_traf->i_type == ATOM_traf &&
347
0
            (p_tfhd = MP4_BoxGet( p_traf, "tfhd" )) && BOXDATA(p_tfhd) &&
348
0
            BOXDATA(p_tfhd)->i_track_ID == i_id )
349
0
                break;
350
0
        else
351
0
            p_traf = p_traf->p_next;
352
0
    }
353
0
    return p_traf;
354
0
}
355
356
static const MP4_Box_t * MP4_GroupDescriptionByType( const MP4_Box_t *p_node,
357
                                                     uint32_t i_grouping_type )
358
0
{
359
0
    for( const MP4_Box_t *p_sgpd = MP4_BoxGet( p_node, "sgpd" );
360
0
                          p_sgpd; p_sgpd = p_sgpd->p_next )
361
0
    {
362
0
        const MP4_Box_data_sgpd_t *p_sgpd_data = BOXDATA(p_sgpd);
363
0
        if( p_sgpd->i_type != ATOM_sgpd || !p_sgpd_data ||
364
0
            p_sgpd_data->i_grouping_type != i_grouping_type )
365
0
            continue;
366
0
        return p_sgpd;
367
0
    }
368
0
    return NULL;
369
0
}
370
371
enum SampleGroupMatch
372
{
373
    SAMPLE_GROUP_MATCH_EXACT = 0,
374
    SAMPLE_GROUP_MATCH_LAST_VALID = 1,
375
};
376
377
static const MP4_Box_data_sbgp_entry_t *
378
    MP4_SampleToGroupInfo( const MP4_Box_t *p_node, uint32_t i_sample,
379
                           uint32_t i_grouping_type,
380
                           uint32_t i_grouping_type_parameter,
381
                           uint32_t *pi_group_sample,
382
                           enum SampleGroupMatch match,
383
                           const MP4_Box_data_sgpd_entry_t **pp_descentry )
384
1
{
385
1
    const MP4_Box_t *p_sbgp = MP4_BoxGet( p_node, "sbgp" );
386
1
    const MP4_Box_data_sbgp_t *p_sbgp_data;
387
1
    if( !p_sbgp )
388
1
        return NULL;
389
390
0
    do
391
0
    {
392
0
        p_sbgp_data = BOXDATA(p_sbgp);
393
0
        if( p_sbgp->i_type == ATOM_sbgp && p_sbgp_data &&
394
0
            p_sbgp_data->i_grouping_type == i_grouping_type &&
395
0
            (i_grouping_type_parameter == 0 ||
396
0
             p_sbgp_data->i_grouping_type_parameter == i_grouping_type_parameter) )
397
0
            break;
398
0
        p_sbgp = p_sbgp->p_next;
399
0
    } while(p_sbgp);
400
401
0
    if( !p_sbgp )
402
0
        return NULL;
403
404
0
    const MP4_Box_data_sbgp_entry_t *p_sampleentry = NULL;
405
0
    uint32_t i_entry_start_sample = 0;
406
0
    for ( uint32_t i=0; i<p_sbgp_data->i_entry_count; i++ )
407
0
    {
408
0
        const uint32_t next_entry_start_sample = i_entry_start_sample +
409
0
                       p_sbgp_data->p_entries[i].i_sample_count;
410
0
        if( i_sample >= next_entry_start_sample )
411
0
        {
412
0
            if( match == SAMPLE_GROUP_MATCH_LAST_VALID &&
413
0
                p_sbgp_data->p_entries[i].i_group_description_index != 0 )
414
0
            {
415
0
                p_sampleentry = &p_sbgp_data->p_entries[i];
416
0
                if( pi_group_sample )
417
0
                    *pi_group_sample = i_entry_start_sample;
418
0
            }
419
0
            i_entry_start_sample = next_entry_start_sample;
420
0
            continue;
421
0
        }
422
423
        /* Sample has meaning for group */
424
0
        if( p_sbgp_data->p_entries[i].i_group_description_index != 0 )
425
0
        {
426
0
            p_sampleentry = &p_sbgp_data->p_entries[i];
427
0
            if( pi_group_sample )
428
0
                *pi_group_sample = i_entry_start_sample;
429
0
        }
430
0
        break;
431
0
    }
432
433
    /* No meaning found for sample */
434
0
    if( p_sampleentry == NULL )
435
0
        return NULL;
436
437
    /* We have a sample entry in the group and maybe a description */
438
0
    if( pp_descentry == NULL )
439
0
        return p_sampleentry;
440
441
    /* Lookup designated group description */
442
0
    *pp_descentry = NULL;
443
0
    const MP4_Box_t *p_sgpd = MP4_GroupDescriptionByType( p_node, i_grouping_type );
444
0
    if( p_sgpd )
445
0
    {
446
0
        const MP4_Box_data_sgpd_t *p_descdata = BOXDATA(p_sgpd);
447
0
        if( p_sampleentry &&
448
0
            p_sampleentry->i_group_description_index <= p_descdata->i_entry_count )
449
0
        {
450
0
            *pp_descentry = &p_descdata->
451
0
                p_entries[p_sampleentry->i_group_description_index - 1];
452
0
        }
453
0
        else if( p_sampleentry == NULL &&
454
0
                 p_descdata->i_version >= 2 &&
455
0
                 p_descdata->i_default_sample_description_index &&
456
0
                 p_descdata->i_default_sample_description_index <= p_descdata->i_entry_count )
457
0
        {
458
0
            *pp_descentry = &p_descdata->
459
0
                p_entries[p_descdata->i_default_sample_description_index - 1];
460
0
        }
461
0
    }
462
463
    /* If caller requested a description but we couldn't find one, report no match */
464
0
    if( *pp_descentry == NULL )
465
0
        return NULL;
466
467
0
    return p_sampleentry;
468
0
}
469
470
static es_out_id_t * MP4_CreateES( es_out_t *out, const es_format_t *p_fmt )
471
1
{
472
1
    es_out_id_t *p_es = es_out_Add( out, p_fmt );
473
    /* Force SPU which isn't selected/defaulted */
474
1
    if( p_fmt->i_cat == SPU_ES && p_es && p_fmt->subs.b_forced )
475
0
        es_out_Control( out, ES_OUT_SET_ES_DEFAULT, p_es );
476
477
1
    return p_es;
478
1
}
479
480
static int MP4_ChunkAllocEntries( size_t entries, uint32_t *smallbuf,
481
                                  uint32_t **dst0, uint32_t **dst1 )
482
27
{
483
27
    uint32_t *buf;
484
485
27
    if( entries > MP4_CHUNK_SMALLBUF_ENTRIES )
486
0
    {
487
0
        buf = calloc( entries, sizeof( buf[0] ) * 2 );
488
0
        if( unlikely(buf == NULL) )
489
0
            return VLC_ENOMEM;
490
0
    }
491
27
    else buf = smallbuf;
492
493
27
    *dst0 = buf;
494
27
    *dst1 = &buf[entries];
495
496
27
    return VLC_SUCCESS;
497
27
}
498
499
static void MP4_ChunkDestroy( mp4_chunk_t *ck )
500
27
{
501
27
    if( ck->p_sample_count_dts != ck->small_dts_buf )
502
0
        free( ck->p_sample_count_dts );
503
27
    if( ck->p_sample_count_pts != ck->small_pts_buf )
504
27
        free( ck->p_sample_count_pts );
505
27
}
506
507
static stime_t MP4_MapTrackTimeIntoTimeline( const mp4_track_t *p_track,
508
                                             uint32_t i_movie_timescale,
509
                                             stime_t i_time )
510
572
{
511
572
    if( !p_track->p_elst || p_track->BOXDATA(p_elst)->i_entry_count == 0 )
512
572
        return i_time;
513
514
0
    const MP4_Box_data_elst_entry_t *edit =
515
0
            &p_track->BOXDATA(p_elst)->entries[p_track->i_elst];
516
517
    /* convert to offset in our current edit */
518
0
    if( edit->i_media_time > 0 && p_track->i_start_dts >= edit->i_media_time )
519
0
        i_time -= edit->i_media_time;
520
521
    /* convert media timeline to track timescale */
522
0
    stime_t i_media_elst_start = MP4_rescale( p_track->i_elst_time,
523
0
                                              i_movie_timescale,
524
0
                                              p_track->i_timescale );
525
    /* add to timeline start in current edit */
526
0
    i_time += i_media_elst_start;
527
528
0
    return i_time;
529
572
}
530
531
static stime_t MP4_MapMediaTimelineToTrackTime( const mp4_track_t *p_track,
532
                                                uint32_t i_movie_timescale,
533
                                                stime_t i_time )
534
0
{
535
0
    if( !p_track->p_elst || p_track->BOXDATA(p_elst)->i_entry_count == 0 )
536
0
        return i_time;
537
538
0
    const MP4_Box_data_elst_t *elst = p_track->BOXDATA(p_elst);
539
    /* convert media timeline to track timescale */
540
0
    stime_t i_media_elst_start = MP4_rescale( p_track->i_elst_time,
541
0
                                              i_movie_timescale,
542
0
                                              p_track->i_timescale );
543
    /* convert to timeline offset in current edit */
544
0
    i_time -= i_media_elst_start;
545
546
    /* add edit start in track samples time to get track sample time */
547
0
    if( elst->entries[p_track->i_elst].i_media_time > 0 )
548
0
        i_time += elst->entries[p_track->i_elst].i_media_time;
549
550
0
    return i_time;
551
0
}
552
553
static stime_t MP4_ChunkGetSampleDTS( const mp4_chunk_t *p_chunk,
554
                                      uint32_t i_sample )
555
129
{
556
129
    uint32_t i_index = 0;
557
129
    stime_t sdts = p_chunk->i_first_dts;
558
129
    while( i_sample > 0 && i_index < p_chunk->i_entries_dts )
559
128
    {
560
128
        if( i_sample > p_chunk->p_sample_count_dts[i_index] )
561
0
        {
562
0
            sdts += (stime_t)p_chunk->p_sample_count_dts[i_index] *
563
0
                p_chunk->p_sample_delta_dts[i_index];
564
0
            i_sample -= p_chunk->p_sample_count_dts[i_index++];
565
0
        }
566
128
        else
567
128
        {
568
128
            sdts += (stime_t)i_sample * p_chunk->p_sample_delta_dts[i_index];
569
128
            break;
570
128
        }
571
128
    }
572
129
    return sdts;
573
129
}
574
575
static bool MP4_ChunkGetSampleCTSDelta( const mp4_chunk_t *p_chunk,
576
                                        uint32_t i_sample, stime_t *pi_delta )
577
129
{
578
129
    if( p_chunk->p_sample_count_pts && p_chunk->p_sample_offset_pts )
579
0
    {
580
0
        for( uint32_t i_index = 0; i_index < p_chunk->i_entries_pts ; i_index++ )
581
0
        {
582
0
            if( i_sample < p_chunk->p_sample_count_pts[i_index] )
583
0
            {
584
0
                *pi_delta = p_chunk->p_sample_offset_pts[i_index];
585
0
                return true;
586
0
            }
587
0
            i_sample -= p_chunk->p_sample_count_pts[i_index];
588
0
        }
589
0
    }
590
129
    return false;
591
129
}
592
593
static vlc_tick_t MP4_TrackGetDTSPTS( demux_t *p_demux, const mp4_track_t *p_track,
594
                                      vlc_tick_t *pi_nzpts )
595
572
{
596
572
    demux_sys_t *p_sys = p_demux->p_sys;
597
598
572
    stime_t sdts = p_track->i_next_dts;
599
572
    uint32_t delta = p_track->i_next_delta;
600
601
    /* now handle elst */
602
572
    sdts = MP4_MapTrackTimeIntoTimeline( p_track, p_sys->i_timescale, sdts );
603
604
572
    vlc_tick_t i_dts = MP4_rescale_mtime( sdts, p_track->i_timescale);
605
606
572
    if( pi_nzpts )
607
129
    {
608
129
        if( delta != UNKNOWN_DELTA )
609
0
        {
610
0
            *pi_nzpts = i_dts + MP4_rescale_mtime( delta, p_track->i_timescale );
611
0
        }
612
129
        else if( p_track->b_ignore_implicit_pts )
613
0
        {
614
0
            *pi_nzpts = INVALID_PTS;
615
0
        }
616
129
        else *pi_nzpts = i_dts;
617
129
    }
618
619
572
    if( p_track->i_pts_offset )
620
0
    {
621
0
        if( pi_nzpts && *pi_nzpts != INVALID_PTS )
622
0
            *pi_nzpts += p_track->i_pts_offset;
623
0
        i_dts += p_track->i_pts_offset;
624
0
    }
625
626
572
    return i_dts;
627
572
}
628
629
static stime_t MP4_GetChunkSamplesDuration( const mp4_chunk_t *p_chunk,
630
                                            uint32_t i_start_sample,
631
                                            uint32_t i_nb_samples )
632
64
{
633
64
    stime_t i_duration = 0;
634
635
    /* Forward to right index, and set remaining count in that index */
636
64
    uint32_t i_index = 0;
637
64
    uint32_t i_remain = 0;
638
64
    for( uint32_t i = p_chunk->i_sample_first;
639
64
         i<i_start_sample && i_index < p_chunk->i_entries_dts; )
640
63
    {
641
63
        if( i_start_sample - i >= p_chunk->p_sample_count_dts[i_index] )
642
0
        {
643
0
            i += p_chunk->p_sample_count_dts[i_index];
644
0
            i_index++;
645
0
        }
646
63
        else
647
63
        {
648
63
            i_remain = i_start_sample - i;
649
63
            break;
650
63
        }
651
63
    }
652
653
    /* Compute total duration from all samples from index */
654
64
    while( i_nb_samples > 0 && i_index < p_chunk->i_entries_dts )
655
64
    {
656
64
        if( i_nb_samples >= p_chunk->p_sample_count_dts[i_index] - i_remain )
657
0
        {
658
0
            i_duration += (p_chunk->p_sample_count_dts[i_index] - i_remain) *
659
0
                          (int64_t) p_chunk->p_sample_delta_dts[i_index];
660
0
            i_nb_samples -= (p_chunk->p_sample_count_dts[i_index] - i_remain);
661
0
            i_index++;
662
0
            i_remain = 0;
663
0
        }
664
64
        else
665
64
        {
666
64
            i_duration += (stime_t)i_nb_samples * p_chunk->p_sample_delta_dts[i_index];
667
64
            break;
668
64
        }
669
64
    }
670
671
64
    return i_duration;
672
64
}
673
674
static inline vlc_tick_t MP4_GetSamplesDuration( const mp4_track_t *p_track,
675
                                                 uint32_t i_nb_samples )
676
64
{
677
64
    stime_t i_duration = MP4_GetChunkSamplesDuration( &p_track->chunk[p_track->i_chunk],
678
64
                                                      p_track->i_sample,
679
64
                                                      i_nb_samples );
680
64
    return MP4_rescale_mtime( i_duration, p_track->i_timescale );
681
64
}
682
683
static inline vlc_tick_t MP4_GetMoviePTS(demux_sys_t *p_sys )
684
191
{
685
191
    return p_sys->i_nztime;
686
191
}
687
688
static void LoadChapter( demux_t  *p_demux );
689
690
static int LoadInitFrag( demux_t *p_demux )
691
1
{
692
1
    demux_sys_t *p_sys = p_demux->p_sys;
693
694
    /* Load all boxes ( except raw data ) */
695
1
    MP4_Box_t *p_root = MP4_BoxGetRoot( p_demux->s );
696
1
    if( p_root == NULL || !MP4_BoxGet( p_root, "/moov" ) )
697
0
    {
698
0
        MP4_BoxFree( p_root );
699
0
        goto LoadInitFragError;
700
0
    }
701
702
1
    p_sys->p_root = p_root;
703
704
1
    return VLC_SUCCESS;
705
706
0
LoadInitFragError:
707
0
    msg_Warn( p_demux, "MP4 plugin discarded (not a valid initialization chunk)" );
708
0
    return VLC_EGENERIC;
709
1
}
710
711
static int CreateTracks( demux_t *p_demux, unsigned i_tracks )
712
1
{
713
1
    demux_sys_t *p_sys = p_demux->p_sys;
714
715
1
    if( SIZE_MAX / i_tracks < sizeof(mp4_track_t) )
716
0
        return VLC_EGENERIC;
717
718
1
    p_sys->track = vlc_alloc( i_tracks, sizeof(mp4_track_t)  );
719
1
    if( p_sys->track == NULL )
720
0
        return VLC_ENOMEM;
721
1
    p_sys->i_tracks = i_tracks;
722
723
2
    for( unsigned i=0; i<i_tracks; i++ )
724
1
    {
725
1
        MP4_TrackInit( &p_sys->track[i],
726
1
                       MP4_BoxGetVa( p_sys->p_root, "/moov/trak[%u]", i ) );
727
1
    }
728
729
1
    return VLC_SUCCESS;
730
1
}
731
732
static block_t * MP4_CDP_Convert( block_t *p_block )
733
0
{
734
    /* FinalCut ST334-2 CDP put inside CC violation */
735
0
    if (p_block->i_buffer < 8 + 7)
736
0
        goto out;
737
738
0
    if(memcmp(&p_block->p_buffer[4], "ccdp", 4))
739
0
        goto out;
740
741
0
    uint_fast32_t ccdp_size = GetDWBE(p_block->p_buffer);
742
0
    if (ccdp_size > p_block->i_buffer || ccdp_size < 8)
743
0
        goto out;
744
745
0
    p_block->p_buffer += 8;
746
0
    p_block->i_buffer = ccdp_size - 8;
747
    /* Let cc.h handle extraction */
748
0
    cc_data_t cc;
749
0
    cc_Init(&cc);
750
0
    cc_Extract(&cc, CC_PAYLOAD_CDP, true, p_block->p_buffer, p_block->i_buffer);
751
0
    memcpy(p_block->p_buffer, cc.p_data, cc.i_data);
752
0
    p_block->i_buffer = cc.i_data;
753
754
0
out:
755
0
    return p_block;
756
0
}
757
758
static block_t * MP4_EIA608_Convert( block_t * p_block )
759
0
{
760
    /* Rebuild codec data from encap */
761
0
    block_t *p_newblock = NULL;
762
763
0
    assert(p_block->i_buffer <= SSIZE_MAX);
764
765
0
    if (p_block->i_buffer < 8)
766
0
        goto out;
767
768
0
    if(!memcmp(&p_block->p_buffer[4], "ccdp", 4))
769
0
        return MP4_CDP_Convert(p_block);
770
771
0
    uint_fast32_t atomsize = GetDWBE(p_block->p_buffer);
772
0
    if (atomsize < 8 || atomsize > p_block->i_buffer ||
773
0
        memcmp(&p_block->p_buffer[4], "cdat", 4))
774
0
        goto out;
775
776
0
    const uint8_t *cdat = p_block->p_buffer + 8;
777
0
    uint_fast32_t cdat_size = (atomsize - 8) & ~1;
778
779
0
    p_block->p_buffer += atomsize;
780
0
    p_block->i_buffer -= atomsize;
781
782
    /* cdt2 is optional */
783
0
    uint_fast32_t cdt2_size = 0;
784
0
    const uint8_t *cdt2 = NULL;
785
786
0
    if (p_block->i_buffer >= 8) {
787
0
        atomsize = GetDWBE(p_block->p_buffer);
788
789
0
        if (atomsize > 8 && atomsize <= p_block->i_buffer &&
790
0
            !memcmp(&p_block->p_buffer[4], "cdt2", 4)) {
791
0
            cdt2 = p_block->p_buffer + 8;
792
0
            cdt2_size = (atomsize - 8) & ~1;
793
0
        }
794
0
    }
795
796
0
    p_newblock = block_Alloc((cdat_size + cdt2_size) / 2 * 3);
797
0
    if (unlikely(p_newblock == NULL))
798
0
        goto out;
799
800
0
    uint8_t *out = p_newblock->p_buffer;
801
802
0
    while (cdat_size >= 2) {
803
0
         *(out++) = CC_PKT_BYTE0(0); /* cc1 == field 0 */
804
0
         *(out++) = *(cdat++);
805
0
         *(out++) = *(cdat++);
806
0
         cdat_size -= 2;
807
0
    }
808
809
0
    while (cdt2_size >= 2) {
810
0
         *(out++) = CC_PKT_BYTE0(1); /* cc2 == field 1 */
811
0
         *(out++) = *(cdt2++);
812
0
         *(out++) = *(cdt2++);
813
0
         cdt2_size -= 2;
814
0
    }
815
816
0
    p_newblock->i_pts = p_block->i_dts;
817
0
    p_newblock->i_flags = BLOCK_FLAG_TYPE_P;
818
0
out:
819
0
    block_Release( p_block );
820
0
    return p_newblock;
821
0
}
822
823
static uint32_t MP4_TrackGetRunSeq( mp4_track_t *p_track )
824
129
{
825
129
    if( p_track->i_chunk_count > 0 )
826
129
        return p_track->chunk[p_track->i_chunk].i_virtual_run_number;
827
0
    return 0;
828
129
}
829
830
/* Analyzes chunks to find max interleave length
831
 * sets flat flag if no interleaving is in use */
832
static void MP4_GetInterleaving( demux_t *p_demux, vlc_tick_t *pi_max_contiguous, bool *pb_flat )
833
0
{
834
0
    demux_sys_t *p_sys = p_demux->p_sys;
835
0
    *pi_max_contiguous = 0;
836
0
    *pb_flat = true;
837
838
    /* Find first recorded chunk */
839
0
    mp4_track_t *tk = NULL;
840
0
    uint64_t i_duration = 0;
841
0
    for( unsigned i=0; i < p_sys->i_tracks; i++ )
842
0
    {
843
0
        mp4_track_t *cur = &p_sys->track[i];
844
0
        if( !cur->i_chunk_count )
845
0
            continue;
846
847
0
        if( tk == NULL || cur->chunk[0].i_offset < tk->chunk[0].i_offset )
848
0
            tk = cur;
849
0
    }
850
851
0
    for( ; tk != NULL; )
852
0
    {
853
0
        i_duration += tk->chunk[tk->i_chunk].i_duration;
854
0
        tk->i_chunk++;
855
856
        /* Find next chunk in data order */
857
0
        mp4_track_t *nexttk = NULL;
858
0
        for( unsigned i=0; i < p_sys->i_tracks; i++ )
859
0
        {
860
0
            mp4_track_t *cur = &p_sys->track[i];
861
0
            if( cur->i_chunk == cur->i_chunk_count )
862
0
                continue;
863
864
0
            if( nexttk == NULL ||
865
0
                cur->chunk[cur->i_chunk].i_offset < nexttk->chunk[nexttk->i_chunk].i_offset )
866
0
                nexttk = cur;
867
0
        }
868
869
        /* copy previous run */
870
0
        if( nexttk && nexttk->i_chunk > 0 )
871
0
            nexttk->chunk[nexttk->i_chunk].i_virtual_run_number =
872
0
                    nexttk->chunk[nexttk->i_chunk - 1].i_virtual_run_number;
873
874
0
        if( tk != nexttk )
875
0
        {
876
0
            vlc_tick_t i_dur = MP4_rescale_mtime( i_duration, tk->i_timescale );
877
0
            if( i_dur > *pi_max_contiguous )
878
0
                *pi_max_contiguous = i_dur;
879
0
            i_duration = 0;
880
881
0
            if( tk->i_chunk != tk->i_chunk_count )
882
0
                *pb_flat = false;
883
884
0
            if( nexttk && nexttk->i_chunk > 0 ) /* new run number */
885
0
                nexttk->chunk[nexttk->i_chunk].i_virtual_run_number++;
886
0
        }
887
888
0
        tk = nexttk;
889
0
    }
890
891
    /* reset */
892
0
    for( unsigned i=0; i < p_sys->i_tracks; i++ )
893
0
        p_sys->track[i].i_chunk = 0;
894
0
}
895
896
static block_t * MP4_Block_Convert( demux_t *p_demux, const mp4_track_t *p_track, block_t *p_block )
897
64
{
898
    /* might have some encap */
899
64
    if( p_track->fmt.i_cat == SPU_ES )
900
0
    {
901
0
        switch( p_track->fmt.i_codec )
902
0
        {
903
0
            case VLC_CODEC_WEBVTT:
904
0
            case VLC_CODEC_TTML:
905
0
            case VLC_CODEC_QTXT:
906
0
            case VLC_CODEC_TX3G:
907
0
            case VLC_CODEC_SPU:
908
0
            case VLC_CODEC_SUBT:
909
            /* accept as-is */
910
0
            break;
911
0
            case VLC_CODEC_CEA608:
912
0
            case VLC_CODEC_CEA708:
913
0
                p_block = MP4_EIA608_Convert( p_block );
914
0
            break;
915
0
        default:
916
0
            p_block->i_buffer = 0;
917
0
            break;
918
0
        }
919
0
    }
920
64
    else if( p_track->fmt.i_codec == VLC_CODEC_AV1 )
921
64
    {
922
64
        p_block = AV1_Unpack_Sample( p_block );
923
64
    }
924
0
    else if( p_track->fmt.i_original_fourcc == ATOM_rrtp )
925
0
    {
926
0
        p_block = MP4_RTPHint_Convert( p_demux, p_block, p_track->fmt.i_codec );
927
0
    }
928
0
    else if ( p_track->fmt.i_codec == VLC_CODEC_APV )
929
0
    {
930
        // the APU are preceeded by 4 bytes containing the size of the data
931
        // this is not used by decoder like libavcodec or openapv.
932
0
        if( p_block->i_buffer < 4 )
933
0
        {
934
0
            block_Release( p_block );
935
0
            return NULL;
936
0
        }
937
0
        p_block->p_buffer += 4;
938
0
        p_block->i_buffer -= 4;
939
0
    }
940
941
64
    return p_block;
942
64
}
943
944
static void MP4_Block_Send( demux_t *p_demux, mp4_track_t *p_track, block_t *p_block )
945
64
{
946
64
    demux_sys_t *p_sys = p_demux->p_sys;
947
948
64
    p_block = MP4_Block_Convert( p_demux, p_track, p_block );
949
64
    if( p_block == NULL )
950
0
        return;
951
952
64
    if ( p_track->i_chans_to_reorder )
953
0
    {
954
0
        aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
955
0
                             p_track->i_chans_to_reorder,
956
0
                             p_track->rgi_chans_reordering,
957
0
                             p_track->fmt.i_codec );
958
0
    }
959
960
64
    p_block->i_flags |= p_track->i_block_flags;
961
64
    if( p_track->i_next_block_flags )
962
0
    {
963
0
        p_block->i_flags |= p_track->i_next_block_flags;
964
0
        p_track->i_next_block_flags = 0;
965
0
    }
966
967
    /* ASF packets in mov */
968
64
    if( p_track->p_asf )
969
0
    {
970
0
        p_sys->asfpacketsys.s = vlc_stream_MemoryNew( p_demux, p_block->p_buffer, p_block->i_buffer, true );
971
0
        if ( p_sys->asfpacketsys.s )
972
0
        {
973
0
            p_track->i_dts_backup = p_block->i_dts;
974
0
            p_track->i_pts_backup = p_block->i_pts;
975
            /* And demux it as ASF packet */
976
0
            uint64_t startpos;
977
0
            do
978
0
            {
979
0
                startpos = vlc_stream_Tell(p_sys->asfpacketsys.s);
980
0
                const uint32_t i_packet_size =
981
0
                    p_track->BOXDATA(p_asf)->i_packet_size;
982
0
                DemuxASFPacket( &p_sys->asfpacketsys,
983
0
                                __MIN(p_block->i_buffer - startpos, i_packet_size),
984
0
                                i_packet_size,
985
0
                                0, p_block->i_buffer );
986
0
            } while( vlc_stream_Tell(p_sys->asfpacketsys.s) != p_block->i_buffer &&
987
0
                     vlc_stream_Tell(p_sys->asfpacketsys.s) != startpos );
988
0
            vlc_stream_Delete(p_sys->asfpacketsys.s);
989
0
        }
990
0
        block_Release(p_block);
991
0
    }
992
64
    else
993
64
        es_out_Send( p_demux->out, p_track->p_es, p_block );
994
64
}
995
996
int  OpenHEIF ( vlc_object_t * );
997
void CloseHEIF( vlc_object_t * );
998
999
/*****************************************************************************
1000
 * Open: check file and initializes MP4 structures
1001
 *****************************************************************************/
1002
static int Open( vlc_object_t * p_this )
1003
32
{
1004
32
    demux_t  *p_demux = (demux_t *)p_this;
1005
32
    demux_sys_t     *p_sys;
1006
1007
32
    const uint8_t   *p_peek;
1008
1009
32
    MP4_Box_t       *p_ftyp;
1010
32
    const MP4_Box_t *p_mvhd = NULL;
1011
32
    const MP4_Box_t *p_mvex = NULL;
1012
1013
32
    bool      b_enabled_es;
1014
1015
    /* A little test to see if it could be a mp4 */
1016
32
    if( vlc_stream_Peek( p_demux->s, &p_peek, 12 ) < 12 ) return VLC_EGENERIC;
1017
1018
32
    switch( VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
1019
32
    {
1020
1
        case ATOM_moov:
1021
1
        case ATOM_foov:
1022
1
        case ATOM_moof:
1023
1
        case ATOM_mdat:
1024
1
        case ATOM_udta:
1025
1
        case ATOM_free:
1026
1
        case ATOM_skip:
1027
1
        case ATOM_wide:
1028
1
        case ATOM_uuid:
1029
1
        case VLC_FOURCC( 'p', 'n', 'o', 't' ):
1030
1
            break;
1031
0
        case ATOM_ftyp:
1032
0
        {
1033
            /* Early handle some brands */
1034
0
            switch( VLC_FOURCC(p_peek[8], p_peek[9], p_peek[10], p_peek[11]) )
1035
0
            {
1036
                /* HEIF pictures goes to heif demux */
1037
0
                case BRAND_heic:
1038
0
                case BRAND_heix:
1039
0
                case BRAND_mif1:
1040
0
                case BRAND_jpeg:
1041
0
                case BRAND_avci:
1042
0
                case BRAND_avif:
1043
                /* We don't yet support f4v, but avformat does. */
1044
0
                case BRAND_f4v:
1045
0
                    return VLC_EGENERIC;
1046
0
                default:
1047
0
                    break;
1048
0
            }
1049
0
            break;
1050
0
        }
1051
31
         default:
1052
31
            return VLC_EGENERIC;
1053
32
    }
1054
1055
    /* create our structure that will contains all data */
1056
1
    p_sys = calloc( 1, sizeof( demux_sys_t ) );
1057
1
    if ( !p_sys )
1058
0
        return VLC_EGENERIC;
1059
1060
    /* I need to seek */
1061
1
    vlc_stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
1062
1
    if( p_sys->b_seekable )
1063
1
        vlc_stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
1064
1065
    /*Set exported functions */
1066
1
    p_demux->pf_demux = Demux;
1067
1
    p_demux->pf_control = Control;
1068
1069
1
    p_sys->context.i_lastseqnumber = UINT32_MAX;
1070
1
    p_sys->i_attachments = -1;
1071
1
    p_sys->qt.f_replay_gain_norm = NAN;
1072
1
    p_sys->qt.f_replay_gain_peak = NAN;
1073
1074
1
    p_demux->p_sys = p_sys;
1075
1076
1
    if( LoadInitFrag( p_demux ) != VLC_SUCCESS )
1077
0
        goto error;
1078
1079
1
    MP4_BoxDumpStructure( VLC_OBJECT(p_demux), p_sys->p_root );
1080
1081
1
    if( ( p_ftyp = MP4_BoxGet( p_sys->p_root, "/ftyp" ) ) )
1082
0
    {
1083
0
        switch( BOXDATA(p_ftyp)->i_major_brand )
1084
0
        {
1085
0
            case BRAND_isom:
1086
0
                msg_Dbg( p_demux,
1087
0
                         "ISO Media (isom) version %d.",
1088
0
                         BOXDATA(p_ftyp)->i_minor_version );
1089
0
                break;
1090
0
            case BRAND_3gp4:
1091
0
            case BRAND_3gp5:
1092
0
            case BRAND_3gp6:
1093
0
            case BRAND_3gp7:
1094
0
                msg_Dbg( p_demux, "3GPP Media Release: %4.4s",
1095
0
                         (char *)&BOXDATA(p_ftyp)->i_major_brand );
1096
0
                break;
1097
0
            case BRAND_qt__:
1098
0
                msg_Dbg( p_demux, "Apple QuickTime media" );
1099
0
                p_sys->b_quicktime = true;
1100
0
                break;
1101
0
            case BRAND_isml:
1102
0
                msg_Dbg( p_demux, "PIFF (= isml = fMP4) media" );
1103
0
                break;
1104
0
            case BRAND_dash:
1105
0
                msg_Dbg( p_demux, "DASH Stream" );
1106
0
                break;
1107
0
            case BRAND_M4A:
1108
0
                msg_Dbg( p_demux, "iTunes audio" );
1109
0
                if( var_InheritBool( p_demux, CFG_PREFIX"m4a-audioonly" ) )
1110
0
                    p_sys->hacks.es_cat_filter = AUDIO_ES;
1111
0
                break;
1112
0
            default:
1113
0
                msg_Dbg( p_demux,
1114
0
                         "unrecognized major media specification (%4.4s).",
1115
0
                          (char*)&BOXDATA(p_ftyp)->i_major_brand );
1116
0
                break;
1117
0
        }
1118
        /* also lookup in compatibility list */
1119
0
        for(uint32_t i=0; i<BOXDATA(p_ftyp)->i_compatible_brands_count; i++)
1120
0
        {
1121
0
            if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_dash)
1122
0
            {
1123
0
                msg_Dbg( p_demux, "DASH Stream" );
1124
0
            }
1125
0
            else if (BOXDATA(p_ftyp)->i_compatible_brands[i] == BRAND_smoo)
1126
0
            {
1127
0
                msg_Dbg( p_demux, "Handling VLC Smooth Stream" );
1128
0
            }
1129
0
        }
1130
0
    }
1131
1
    else
1132
1
    {
1133
1
        msg_Dbg( p_demux, "file type box missing (assuming ISO Media)" );
1134
1
    }
1135
1136
    /* the file need to have one moov box */
1137
1
    p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/moov" );
1138
1
    if( unlikely(!p_sys->p_moov) )
1139
0
    {
1140
0
        p_sys->p_moov = MP4_BoxGet( p_sys->p_root, "/foov" );
1141
0
        if( !p_sys->p_moov )
1142
0
        {
1143
0
            msg_Err( p_demux, "MP4 plugin discarded (no moov,foov,moof box)" );
1144
0
            goto error;
1145
0
        }
1146
        /* we have a free box as a moov, rename it */
1147
0
        p_sys->p_moov->i_type = ATOM_moov;
1148
0
    }
1149
1150
1
    p_mvhd = MP4_BoxGet( p_sys->p_moov, "mvhd" );
1151
1
    if( p_mvhd && BOXDATA(p_mvhd) && BOXDATA(p_mvhd)->i_timescale &&
1152
1
        BOXDATA(p_mvhd)->i_duration < INT64_MAX )
1153
1
    {
1154
1
        p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
1155
1
        p_sys->i_moov_duration =
1156
1
        p_sys->i_duration =
1157
1
        p_sys->i_cumulated_duration = BOXDATA(p_mvhd)->i_duration;
1158
1
    }
1159
0
    else
1160
0
    {
1161
0
        msg_Warn( p_demux, "No valid mvhd found" );
1162
0
        goto error;
1163
0
    }
1164
1165
1
    MP4_Box_t *p_rmra = MP4_BoxGet( p_sys->p_root, "/moov/rmra" );
1166
1
    if( p_rmra != NULL && p_demux->p_input_item != NULL )
1167
0
    {
1168
0
        int        i_count = MP4_BoxCount( p_rmra, "rmda" );
1169
0
        int        i;
1170
1171
0
        msg_Dbg( p_demux, "detected playlist mov file (%d ref)", i_count );
1172
1173
0
        input_item_t *p_current = p_demux->p_input_item;
1174
1175
0
        input_item_node_t *p_subitems = input_item_node_Create( p_current );
1176
1177
0
        for( i = 0; i < i_count; i++ )
1178
0
        {
1179
0
            MP4_Box_t *p_rdrf = MP4_BoxGetVa( p_rmra, "rmda[%d]/rdrf", i );
1180
0
            char      *psz_ref;
1181
0
            uint32_t  i_ref_type;
1182
1183
0
            if( !p_rdrf || !BOXDATA(p_rdrf) || !( psz_ref = strdup( BOXDATA(p_rdrf)->psz_ref ) ) )
1184
0
            {
1185
0
                continue;
1186
0
            }
1187
0
            i_ref_type = BOXDATA(p_rdrf)->i_ref_type;
1188
1189
0
            msg_Dbg( p_demux, "new ref=`%s' type=%4.4s",
1190
0
                     psz_ref, (char*)&i_ref_type );
1191
1192
0
            if( i_ref_type == VLC_FOURCC( 'u', 'r', 'l', ' ' ) )
1193
0
            {
1194
0
                if( strstr( psz_ref, "qt5gateQT" ) )
1195
0
                {
1196
0
                    msg_Dbg( p_demux, "ignoring pseudo ref =`%s'", psz_ref );
1197
0
                    free( psz_ref );
1198
0
                    continue;
1199
0
                }
1200
0
                if( !strncmp( psz_ref, "http://", 7 ) ||
1201
0
                    !strncmp( psz_ref, "rtsp://", 7 ) )
1202
0
                {
1203
0
                    ;
1204
0
                }
1205
0
                else
1206
0
                {
1207
0
                    char *psz_absolute = vlc_uri_resolve( p_demux->psz_url,
1208
0
                                                          psz_ref );
1209
0
                    free( psz_ref );
1210
0
                    if( psz_absolute == NULL )
1211
0
                    {
1212
0
                        input_item_node_Delete( p_subitems );
1213
0
                        return VLC_ENOMEM;
1214
0
                    }
1215
0
                    psz_ref = psz_absolute;
1216
0
                }
1217
0
                msg_Dbg( p_demux, "adding ref = `%s'", psz_ref );
1218
0
                input_item_t *p_item = input_item_New( psz_ref, NULL );
1219
0
                input_item_node_AppendItem( p_subitems, p_item );
1220
0
                input_item_Release( p_item );
1221
0
            }
1222
0
            else
1223
0
            {
1224
0
                msg_Err( p_demux, "unknown ref type=%4.4s FIXME (send a bug report)",
1225
0
                         (char*)&BOXDATA(p_rdrf)->i_ref_type );
1226
0
            }
1227
0
            free( psz_ref );
1228
0
        }
1229
1230
        /* FIXME: create a stream_filter sub-module for this */
1231
0
        if (es_out_Control(p_demux->out, ES_OUT_POST_SUBNODE, p_subitems))
1232
0
            input_item_node_Delete(p_subitems);
1233
0
    }
1234
1235
1
    if( !(p_mvhd = MP4_BoxGet( p_sys->p_root, "/moov/mvhd" ) ) )
1236
0
    {
1237
0
        if( !p_rmra )
1238
0
        {
1239
0
            msg_Err( p_demux, "cannot find /moov/mvhd" );
1240
0
            goto error;
1241
0
        }
1242
0
        else
1243
0
        {
1244
0
            msg_Warn( p_demux, "cannot find /moov/mvhd (pure ref file)" );
1245
0
            p_demux->pf_demux = DemuxRef;
1246
0
            return VLC_SUCCESS;
1247
0
        }
1248
0
    }
1249
1
    else
1250
1
    {
1251
1
        p_sys->i_timescale = BOXDATA(p_mvhd)->i_timescale;
1252
1
        if( p_sys->i_timescale == 0 || p_sys->i_timescale > 0x10000000 )
1253
0
        {
1254
0
            msg_Err( p_this, "bad timescale %" PRIu32, p_sys->i_timescale );
1255
0
            goto error;
1256
0
        }
1257
1
    }
1258
1259
1
    const unsigned i_tracks = MP4_BoxCount( p_sys->p_root, "/moov/trak" );
1260
1
    if( i_tracks < 1 )
1261
0
    {
1262
0
        msg_Err( p_demux, "cannot find any /moov/trak" );
1263
0
        goto error;
1264
0
    }
1265
1
    msg_Dbg( p_demux, "found %u track%c", i_tracks, i_tracks ? 's':' ' );
1266
1267
    /* reject old monolithically embedded mpeg */
1268
1
    if( i_tracks == 1 )
1269
1
    {
1270
1
        const MP4_Box_t *p_hdlr = MP4_BoxGet( p_sys->p_root, "/moov/trak/mdia/hdlr" );
1271
1
        if( p_hdlr && BOXDATA(p_hdlr) )
1272
1
        {
1273
1
            switch( BOXDATA(p_hdlr)->i_handler_type )
1274
1
            {
1275
0
                case HANDLER_m1v:
1276
0
                case HANDLER_m1s:
1277
0
                case HANDLER_mpeg:
1278
0
                    msg_Dbg( p_demux, "Raw MPEG/PS not supported, passing to ps demux" );
1279
0
                    goto error;
1280
1
                default:
1281
1
                    break;
1282
1
            }
1283
1
        }
1284
1
    }
1285
1286
1
    if( CreateTracks( p_demux, i_tracks ) != VLC_SUCCESS )
1287
0
        goto error;
1288
1289
    /* set referenced tracks and
1290
     * check that at least 1 stream is enabled */
1291
1
    b_enabled_es = false;
1292
2
    for( unsigned i = 0; i < p_sys->i_tracks; i++ )
1293
1
    {
1294
1
        MP4_Box_t *p_trak = MP4_BoxGetVa( p_sys->p_root, "/moov/trak[%u]", i );
1295
1296
        /* Enabled check as explained above */
1297
1
        MP4_Box_t *p_tkhd = MP4_BoxGet( p_trak, "tkhd" );
1298
1
        if( p_tkhd && BOXDATA(p_tkhd) && (BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED) )
1299
0
            b_enabled_es = true;
1300
1301
        /* Referenced tracks checks */
1302
1
        MP4_Box_t *p_tref = MP4_BoxGet( p_trak, "tref" );
1303
1
        if(!p_tref)
1304
1
            continue;
1305
1306
0
        for( MP4_Box_t *p_refbox = p_tref->p_first; p_refbox; p_refbox = p_refbox->p_next )
1307
0
        {
1308
0
            const MP4_Box_data_trak_reference_t *refdata = p_refbox->data.p_track_reference;
1309
0
            if(unlikely(!refdata))
1310
0
                continue;
1311
1312
            /* Assign reference types */
1313
0
            for( uint32_t j = 0; j < refdata->i_entry_count; j++ )
1314
0
            {
1315
0
                mp4_track_t *reftk = MP4_GetTrackByTrackID(p_demux, refdata->i_track_ID[j]);
1316
0
                if(reftk)
1317
0
                {
1318
0
                    msg_Dbg( p_demux, "track 0x%x refs track 0x%x for %4.4s", i,
1319
0
                             refdata->i_track_ID[j], (const char *) &p_refbox->i_type );
1320
0
                    int i_flag = MP4_reftypeToFlag( p_refbox->i_type );
1321
                    /* chap may reference a video track holding slide artwork; such a track is not a chapter source */
1322
0
                    if( i_flag == USEAS_CHAPTERS )
1323
0
                    {
1324
0
                        const MP4_Box_t *p_hdlr = MP4_BoxGet( reftk->p_track, "mdia/hdlr" );
1325
0
                        if( p_hdlr && BOXDATA(p_hdlr) &&
1326
0
                            BOXDATA(p_hdlr)->i_handler_type == ATOM_vide )
1327
0
                        {
1328
0
                            i_flag = USEAS_NONE;
1329
0
                        }
1330
0
                    }
1331
0
                    reftk->i_use_flags |= i_flag;
1332
0
                }
1333
0
            }
1334
0
        }
1335
0
    }
1336
1337
    /* Set and store metadata */
1338
1
    if( (p_sys->p_meta = vlc_meta_New()) )
1339
1
        MP4_LoadMeta( p_sys, p_sys->p_meta );
1340
1341
    /* now process each track and extract all useful information */
1342
2
    for( unsigned i = 0; i < p_sys->i_tracks; i++ )
1343
1
    {
1344
1
        const MP4_Box_t *p_trakbox = MP4_BoxGetVa( p_sys->p_root, "/moov/trak[%u]", i );
1345
1
        MP4_TrackSetup( p_demux, &p_sys->track[i], p_trakbox, true, !b_enabled_es );
1346
1
        mp4_track_t *p_track = &p_sys->track[i];
1347
1348
1
        if( p_track->b_ok && ! MP4_isMetadata(p_track) )
1349
1
        {
1350
1
            const char *psz_cat;
1351
1
            switch( p_track->fmt.i_cat )
1352
1
            {
1353
0
                case( VIDEO_ES ):
1354
0
                    psz_cat = "video";
1355
0
                    break;
1356
1
                case( AUDIO_ES ):
1357
1
                    psz_cat = "audio";
1358
1
                    break;
1359
0
                case( SPU_ES ):
1360
0
                    psz_cat = "subtitle";
1361
0
                    break;
1362
1363
0
                default:
1364
0
                    psz_cat = "unknown";
1365
0
                    break;
1366
1
            }
1367
1368
1
            msg_Dbg( p_demux, "adding track[Id 0x%x] %s (%s) language %s",
1369
1
                     p_track->i_track_ID, psz_cat,
1370
1
                     p_track->b_enable ? "enable":"disable",
1371
1
                     p_track->fmt.psz_language ?
1372
1
                     p_track->fmt.psz_language : "undef" );
1373
1
        }
1374
0
        else if( p_track->b_ok && (p_track->i_use_flags & USEAS_CHAPTERS) )
1375
0
        {
1376
0
            msg_Dbg( p_demux, "using track[Id 0x%x] for chapter language %s",
1377
0
                     p_track->i_track_ID,
1378
0
                     p_track->fmt.psz_language ?
1379
0
                     p_track->fmt.psz_language : "undef" );
1380
0
        }
1381
0
        else
1382
0
        {
1383
0
            msg_Dbg( p_demux, "ignoring track[Id 0x%x] %d refs %x",
1384
0
                     p_track->i_track_ID, p_track->b_ok, p_track->i_use_flags );
1385
0
        }
1386
1387
1
        if( p_track->b_ok && p_track->i_cts_shift ) /* PTS shift handling pass 1 */
1388
0
        {
1389
0
            p_track->i_pts_offset = MP4_rescale_mtime( p_track->i_cts_shift,
1390
0
                                                       p_track->i_timescale );
1391
0
            if( p_track->i_pts_offset > p_sys->i_max_pts_offset )
1392
0
                p_sys->i_max_pts_offset = p_track->i_pts_offset;
1393
0
        }
1394
1
    }
1395
1396
2
    for( unsigned i = 0; i < p_sys->i_tracks; i++ ) /* PTS shift handling pass 2 */
1397
1
    {
1398
1
        mp4_track_t *p_track = &p_sys->track[i];
1399
1
        if( !p_track->b_ok )
1400
0
            continue;
1401
1
        p_track->i_pts_offset = p_sys->i_max_pts_offset - p_track->i_pts_offset;
1402
1
    }
1403
1404
1
    p_mvex = MP4_BoxGet( p_sys->p_moov, "mvex" );
1405
1
    if( p_mvex != NULL )
1406
0
    {
1407
0
        const MP4_Box_t *p_mehd = MP4_BoxGet( p_mvex, "mehd");
1408
0
        if ( p_mehd && BOXDATA(p_mehd) )
1409
0
        {
1410
0
            if( BOXDATA(p_mehd)->i_fragment_duration > p_sys->i_duration )
1411
0
            {
1412
0
                p_sys->b_fragmented = true;
1413
0
                p_sys->i_duration = BOXDATA(p_mehd)->i_fragment_duration;
1414
0
            }
1415
0
        }
1416
1417
0
        const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx");
1418
0
        if( p_sidx )
1419
0
            p_sys->b_fragmented = true;
1420
1421
0
        if ( p_sys->b_seekable )
1422
0
        {
1423
0
            if( !p_sys->b_fragmented /* as unknown */ )
1424
0
            {
1425
                /* Probe remaining to check if there's really fragments
1426
                   or if that file is just ready to append fragments */
1427
0
                ProbeFragments( p_demux, (p_sys->i_duration == 0), &p_sys->b_fragmented );
1428
0
            }
1429
1430
0
            if( vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos ) != VLC_SUCCESS )
1431
0
                goto error;
1432
0
        }
1433
0
        else /* Handle as fragmented by default as we can't see moof */
1434
0
        {
1435
0
            p_sys->context.p_fragment_atom = p_sys->p_moov;
1436
0
            p_sys->context.i_current_box_type = ATOM_moov;
1437
0
            p_sys->b_fragmented = true;
1438
0
        }
1439
0
    }
1440
1441
1
    if( p_sys->b_fragmented )
1442
0
    {
1443
0
        p_demux->pf_demux = DemuxFrag;
1444
0
        msg_Dbg( p_demux, "Set Fragmented demux mode" );
1445
0
    }
1446
1447
1
    if( !p_sys->b_seekable && p_demux->pf_demux == Demux )
1448
0
    {
1449
0
        msg_Warn( p_demux, "MP4 plugin discarded (not seekable)" );
1450
0
        goto error;
1451
0
    }
1452
1453
1
    if( p_sys->i_tracks > 1 && !p_sys->b_fastseekable )
1454
0
    {
1455
0
        vlc_tick_t i_max_continuity;
1456
0
        bool b_flat;
1457
0
        MP4_GetInterleaving( p_demux, &i_max_continuity, &b_flat );
1458
0
        if( b_flat )
1459
0
            msg_Warn( p_demux, "that media doesn't look interleaved, will need to seek");
1460
0
        else if( i_max_continuity > DEMUX_TRACK_MAX_PRELOAD )
1461
0
            msg_Warn( p_demux, "that media doesn't look properly interleaved, will need to seek");
1462
0
    }
1463
1464
    /* */
1465
1
    LoadChapter( p_demux );
1466
1467
1
    p_sys->asfpacketsys.priv = p_demux;
1468
1
    p_sys->asfpacketsys.s = p_demux->s;
1469
1
    p_sys->asfpacketsys.logger = p_demux->obj.logger;
1470
1
    p_sys->asfpacketsys.pi_preroll = &p_sys->i_preroll;
1471
1
    p_sys->asfpacketsys.pi_preroll_start = &p_sys->i_preroll_start;
1472
1
    p_sys->asfpacketsys.b_deduplicate = true;
1473
1
    p_sys->asfpacketsys.b_can_hold_multiple_packets = true;
1474
1
    p_sys->asfpacketsys.pf_doskip = NULL;
1475
1
    p_sys->asfpacketsys.pf_send = MP4ASF_Send;
1476
1
    p_sys->asfpacketsys.pf_gettrackinfo = MP4ASF_GetTrackInfo;
1477
1
    p_sys->asfpacketsys.pf_updatetime = NULL;
1478
1
    p_sys->asfpacketsys.pf_setaspectratio = NULL;
1479
1480
1
    p_sys->hacks.es_cat_filter = UNKNOWN_ES;
1481
1482
1
    return VLC_SUCCESS;
1483
1484
0
error:
1485
0
    if( vlc_stream_Tell( p_demux->s ) > 0 )
1486
0
    {
1487
0
        if( vlc_stream_Seek( p_demux->s, 0 ) != VLC_SUCCESS )
1488
0
            msg_Warn( p_demux, "Can't reset stream position from probing" );
1489
0
    }
1490
1491
0
    Close( p_this );
1492
1493
0
    return VLC_EGENERIC;
1494
1
}
1495
1496
const unsigned int SAMPLEHEADERSIZE = 4;
1497
const unsigned int RTPPACKETSIZE = 12;
1498
const unsigned int CONSTRUCTORSIZE = 16;
1499
1500
/*******************************************************************************
1501
 * MP4_RTPHintToFrame: converts RTP Reception Hint Track sample to H.264 frame
1502
 *******************************************************************************/
1503
static block_t * MP4_RTPHintToFrame( demux_t *p_demux, block_t *p_block, uint32_t packetcount )
1504
0
{
1505
0
    uint8_t *p_slice = p_block->p_buffer + SAMPLEHEADERSIZE;
1506
0
    block_t *p_newblock = NULL;
1507
0
    size_t i_payload = 0;
1508
1509
0
    if( p_block->i_buffer < SAMPLEHEADERSIZE + RTPPACKETSIZE + CONSTRUCTORSIZE )
1510
0
    {
1511
0
        msg_Err( p_demux, "Sample not large enough for necessary structs");
1512
0
        block_Release( p_block );
1513
0
        return NULL;
1514
0
    }
1515
1516
0
    for( uint32_t i = 0; i < packetcount; ++i )
1517
0
    {
1518
0
        if( (size_t)(p_slice - p_block->p_buffer) + RTPPACKETSIZE + CONSTRUCTORSIZE > p_block->i_buffer )
1519
0
            goto error;
1520
1521
        /* skip RTP header in sample. Could be used to detect packet losses */
1522
0
        p_slice += RTPPACKETSIZE;
1523
1524
0
        mp4_rtpsampleconstructor_t sample_cons;
1525
1526
0
        sample_cons.type =                      p_slice[0];
1527
0
        sample_cons.trackrefindex =             p_slice[1];
1528
0
        sample_cons.length =          GetWBE(  &p_slice[2] );
1529
0
        sample_cons.samplenumber =    GetDWBE( &p_slice[4] );
1530
0
        sample_cons.sampleoffset =    GetDWBE( &p_slice[8] );
1531
0
        sample_cons.bytesperblock =   GetWBE(  &p_slice[12] );
1532
0
        sample_cons.samplesperblock = GetWBE(  &p_slice[14] );
1533
1534
        /* skip packet constructor */
1535
0
        p_slice += CONSTRUCTORSIZE;
1536
1537
0
        if( sample_cons.length == 0 )
1538
0
            continue;
1539
1540
        /* check that is RTPsampleconstructor, referencing itself and no weird audio stuff */
1541
0
        if( sample_cons.type != 2||sample_cons.trackrefindex != -1
1542
0
            ||sample_cons.samplesperblock != 1||sample_cons.bytesperblock != 1 )
1543
0
        {
1544
0
            msg_Err(p_demux, "Unhandled constructor in RTP Reception Hint Track. Type:%u", sample_cons.type);
1545
0
            goto error;
1546
0
        }
1547
1548
        /* slice doesn't fit in buffer */
1549
0
        size_t slice_size;
1550
0
        if( ckd_add( &slice_size, sample_cons.sampleoffset, sample_cons.length ) ||
1551
0
            slice_size > p_block->i_buffer )
1552
0
        {
1553
0
            msg_Err(p_demux, "Sample buffer is smaller than sample" );
1554
0
            goto error;
1555
0
        }
1556
1557
0
        size_t realloc_size;
1558
0
        if( ckd_add( &realloc_size, i_payload, sample_cons.length ) ||
1559
0
            ckd_add( &realloc_size, realloc_size, 4 ) )
1560
0
        {
1561
0
            goto error;
1562
0
        }
1563
1564
0
        block_t *p_realloc = ( p_newblock ) ?
1565
0
                             block_TryRealloc( p_newblock, 0, realloc_size ):
1566
0
                             block_Alloc( realloc_size );
1567
0
        if( !p_realloc )
1568
0
            goto error;
1569
1570
0
        p_newblock = p_realloc;
1571
0
        uint8_t *p_dst = &p_newblock->p_buffer[i_payload];
1572
1573
0
        const uint8_t* p_src = p_block->p_buffer + sample_cons.sampleoffset;
1574
0
        uint8_t i_type = (*p_src) & ((1<<5)-1);
1575
1576
0
        const uint8_t synccode[4] = { 0, 0, 0, 1 };
1577
0
        if( sample_cons.length < 4 || memcmp( p_src, synccode, 4 ) )
1578
0
        {
1579
0
            if( i_type == 7 || i_type == 8 )
1580
0
                *p_dst++=0;
1581
1582
0
            p_dst[0] = 0;
1583
0
            p_dst[1] = 0;
1584
0
            p_dst[2] = 1;
1585
0
            p_dst += 3;
1586
0
        }
1587
1588
0
        memcpy( p_dst, p_src, sample_cons.length );
1589
0
        p_dst += sample_cons.length;
1590
1591
0
        i_payload = p_dst - p_newblock->p_buffer;
1592
0
    }
1593
1594
0
    block_Release( p_block );
1595
0
    if( p_newblock )
1596
0
        p_newblock->i_buffer = i_payload;
1597
0
    return p_newblock;
1598
1599
0
error:
1600
0
    block_Release( p_block );
1601
0
    if( p_newblock )
1602
0
        block_Release( p_newblock );
1603
0
    return NULL;
1604
0
}
1605
1606
/* RTP Reception Hint Track */
1607
static block_t * MP4_RTPHint_Convert( demux_t *p_demux, block_t *p_block, vlc_fourcc_t i_codec )
1608
0
{
1609
0
    block_t *p_converted = NULL;
1610
0
    if( p_block->i_buffer < 2 )
1611
0
    {
1612
0
        block_Release( p_block );
1613
0
        return NULL;
1614
0
    }
1615
1616
    /* number of RTP packets contained in this sample */
1617
0
    const uint16_t i_packets = GetWBE( p_block->p_buffer );
1618
0
    if( i_packets <= 1 || i_codec != VLC_CODEC_H264 )
1619
0
    {
1620
0
        const size_t i_skip = SAMPLEHEADERSIZE + i_packets * ( RTPPACKETSIZE + CONSTRUCTORSIZE );
1621
0
        if( i_packets == 1 && i_skip < p_block->i_buffer )
1622
0
        {
1623
0
            p_block->p_buffer += i_skip;
1624
0
            p_block->i_buffer -= i_skip;
1625
0
            p_converted = p_block;
1626
0
        }
1627
0
        else
1628
0
        {
1629
0
            block_Release( p_block );
1630
0
        }
1631
0
    }
1632
0
    else
1633
0
    {
1634
0
        p_converted = MP4_RTPHintToFrame( p_demux, p_block, i_packets );
1635
0
    }
1636
1637
0
    return p_converted;
1638
0
}
1639
1640
static uint64_t OverflowCheck( demux_t *p_demux, mp4_track_t *tk,
1641
                               uint64_t i_readpos, uint64_t i_samplessize )
1642
65
{
1643
65
    demux_sys_t *p_sys = p_demux->p_sys;
1644
65
    if( !p_sys->b_seekable && p_sys->b_fragmented &&
1645
0
         p_sys->context.i_post_mdat_offset )
1646
0
    {
1647
        /* avoid breaking non seekable demux */
1648
0
        if( i_readpos + i_samplessize > p_sys->context.i_post_mdat_offset )
1649
0
        {
1650
0
            msg_Err(p_demux, "Broken file. track[0x%x] "
1651
0
                             "Sample @%" PRIu64 " overflowing "
1652
0
                             "parent mdat by %" PRIu64,
1653
0
                    tk->i_track_ID, i_readpos,
1654
0
                    i_readpos + i_samplessize - p_sys->context.i_post_mdat_offset );
1655
0
            i_samplessize = p_sys->context.i_post_mdat_offset - i_readpos;
1656
0
        }
1657
0
    }
1658
65
    return i_samplessize;
1659
65
}
1660
1661
/*****************************************************************************
1662
 * Demux: read packet and send them to decoders
1663
 *****************************************************************************
1664
 * TODO check for newly selected track (ie audio upt to now )
1665
 *****************************************************************************/
1666
static int DemuxTrack( demux_t *p_demux, mp4_track_t *tk, uint64_t i_readpos,
1667
                       vlc_tick_t i_max_preload )
1668
65
{
1669
65
    demux_sys_t *p_sys = p_demux->p_sys;
1670
65
    uint32_t i_nb_samples = 0;
1671
65
    uint32_t i_samplessize = 0;
1672
1673
65
    if( !tk->b_ok || tk->i_sample >= tk->i_sample_count )
1674
0
        return VLC_DEMUXER_EOS;
1675
1676
65
    if( tk->i_use_flags & USEAS_CHAPTERS )
1677
0
        return VLC_DEMUXER_SUCCESS;
1678
1679
65
    uint32_t i_run_seq = MP4_TrackGetRunSeq( tk );
1680
65
    vlc_tick_t i_current_nzpts;
1681
65
    vlc_tick_t i_current_nzdts = MP4_TrackGetDTSPTS( p_demux, tk, &i_current_nzpts );
1682
65
    const vlc_tick_t i_demux_max_nzdts =i_max_preload < INVALID_PRELOAD
1683
65
                                    ? i_current_nzdts + i_max_preload
1684
65
                                    : VLC_TICK_MAX;
1685
1686
129
    for( ; i_demux_max_nzdts >= i_current_nzdts; )
1687
65
    {
1688
65
        if( tk->i_sample >= tk->i_sample_count )
1689
0
            return VLC_DEMUXER_EOS;
1690
1691
#if 0
1692
        msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, tk->i_track_ID,
1693
                 MP4_TrackGetDTSPTS( p_demux, tk, NULL ),
1694
                 MP4_GetMoviePTS( p_demux->p_sys ), i_readpos );
1695
#endif
1696
1697
65
        i_samplessize = MP4_TrackGetReadSize( tk, &i_nb_samples );
1698
65
        if( i_samplessize > 0 )
1699
65
        {
1700
65
            block_t *p_block;
1701
1702
65
            if( vlc_stream_Tell( p_demux->s ) != i_readpos )
1703
2
            {
1704
2
                if( MP4_Seek( p_demux->s, i_readpos ) != VLC_SUCCESS )
1705
0
                {
1706
0
                    msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1707
0
                                       ": Failed to seek to %"PRIu64,
1708
0
                              tk->i_track_ID, i_readpos );
1709
0
                    MP4_TrackSelect( p_demux, tk, false );
1710
0
                    goto end;
1711
0
                }
1712
2
            }
1713
1714
65
            i_samplessize = OverflowCheck( p_demux, tk, i_readpos, i_samplessize );
1715
1716
            /* now read pes */
1717
65
            if( !(p_block = vlc_stream_Block( p_demux->s, i_samplessize )) )
1718
1
            {
1719
1
                msg_Warn( p_demux, "track[0x%x] will be disabled (eof?)"
1720
1
                                   ": Failed to read %d bytes sample at %"PRIu64,
1721
1
                          tk->i_track_ID, i_samplessize, i_readpos );
1722
1
                MP4_TrackSelect( p_demux, tk, false );
1723
1
                goto end;
1724
1
            }
1725
1726
            /* !important! Ensure clock is set before sending data */
1727
64
            if( p_sys->i_pcr == VLC_TICK_INVALID )
1728
1
            {
1729
1
                es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_current_nzdts );
1730
1
                p_sys->i_pcr = VLC_TICK_0 + i_current_nzdts;
1731
1
            }
1732
1733
            /* dts */
1734
64
            p_block->i_dts = VLC_TICK_0 + i_current_nzdts;
1735
            /* pts */
1736
64
            p_block->i_pts = i_current_nzpts != INVALID_PTS
1737
64
                           ? VLC_TICK_0 + i_current_nzpts
1738
64
                           : VLC_TICK_INVALID;
1739
1740
64
            p_block->i_length = MP4_GetSamplesDuration( tk, i_nb_samples );
1741
1742
64
            MP4_Block_Send( p_demux, tk, p_block );
1743
64
        }
1744
1745
        /* Next sample */
1746
64
        if ( i_nb_samples && /* sample size could be 0, need to go fwd. see return */
1747
64
             MP4_TrackNextSample( p_demux, tk, i_nb_samples ) )
1748
0
            goto end;
1749
1750
64
        uint32_t i_next_run_seq = MP4_TrackGetRunSeq( tk );
1751
64
        if( i_next_run_seq != i_run_seq )
1752
0
            break;
1753
1754
64
        i_current_nzdts = MP4_TrackGetDTSPTS( p_demux, tk, &i_current_nzpts );
1755
64
        i_readpos = MP4_TrackGetPos( tk );
1756
64
    }
1757
1758
64
    return VLC_DEMUXER_SUCCESS;
1759
1760
1
end:
1761
1
    return VLC_DEMUXER_EGENERIC;
1762
65
}
1763
1764
static int DemuxMoov( demux_t *p_demux )
1765
190
{
1766
190
    demux_sys_t *p_sys = p_demux->p_sys;
1767
190
    unsigned int i_track;
1768
1769
    /* check for newly selected/unselected track */
1770
380
    for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1771
190
    {
1772
190
        mp4_track_t *tk = &p_sys->track[i_track];
1773
190
        bool b = true;
1774
1775
190
        if( !tk->b_ok || MP4_isMetadata( tk ) )
1776
0
        {
1777
0
            continue;
1778
0
        }
1779
1780
190
        if( p_sys->b_seekable )
1781
190
            es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1782
1783
190
        if( tk->b_selected && !b )
1784
0
        {
1785
0
            MP4_TrackSelect( p_demux, tk, false );
1786
0
        }
1787
190
        else if( !tk->b_selected && b)
1788
1
        {
1789
1
            MP4_TrackSeek( p_demux, tk, MP4_GetMoviePTS( p_sys ) );
1790
1
        }
1791
190
    }
1792
1793
190
    const vlc_tick_t i_nztime = MP4_GetMoviePTS( p_sys );
1794
1795
    /* We demux/set pcr, even without selected tracks, (empty edits, ...) */
1796
190
    if( p_sys->i_pcr != VLC_TICK_INVALID /* not after a seek */ )
1797
189
    {
1798
189
        bool b_eof = true;
1799
378
        for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1800
189
        {
1801
189
            mp4_track_t *tk = &p_sys->track[i_track];
1802
189
            if( !tk->b_ok || MP4_isMetadata( tk ) || tk->i_sample >= tk->i_sample_count )
1803
0
                continue;
1804
            /* Test for EOF on each track (samples count, edit list) */
1805
189
            b_eof &= ( i_nztime > MP4_TrackGetDTSPTS( p_demux, tk, NULL ) );
1806
189
        }
1807
189
        if( b_eof )
1808
0
            return VLC_DEMUXER_EOS;
1809
189
    }
1810
1811
190
    const vlc_tick_t i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : INVALID_PRELOAD;
1812
190
    int i_status;
1813
    /* demux up to increment amount of data on every track, or just set pcr if empty data */
1814
190
    for( ;; )
1815
255
    {
1816
255
        mp4_track_t *tk = NULL;
1817
255
        i_status = VLC_DEMUXER_EOS;
1818
255
        vlc_tick_t i_minnzdts = VLC_TICK_MAX;
1819
1820
        /* First pass, find any track within our target increment, ordered by position */
1821
510
        for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1822
255
        {
1823
255
            mp4_track_t *tk_tmp = &p_sys->track[i_track];
1824
255
            if( !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) ||
1825
255
                tk_tmp->i_sample >= tk_tmp->i_sample_count ||
1826
255
                (!tk_tmp->b_selected && p_sys->b_seekable) )
1827
1
                continue;
1828
1829
            /* At least still have data to demux on this or next turns */
1830
254
            i_status = VLC_DEMUXER_SUCCESS;
1831
1832
254
            vlc_tick_t i_nzdts = MP4_TrackGetDTSPTS( p_demux, tk_tmp, NULL );
1833
254
            if ( i_nzdts <= i_nztime + DEMUX_INCREMENT )
1834
65
            {
1835
65
                if( tk == NULL || MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1836
65
                    tk = tk_tmp;
1837
65
            }
1838
254
            i_minnzdts = __MIN(i_minnzdts, i_nzdts);
1839
254
        }
1840
1841
255
        if( tk )
1842
65
        {
1843
            /* Second pass, refine and find any best candidate having a chunk pos closer than
1844
             * current candidate (avoids seeks when increment falls between the 2) from
1845
             * current position, but within extended interleave time */
1846
65
            for( i_track = 0; i_max_preload != 0 && i_track < p_sys->i_tracks; i_track++ )
1847
0
            {
1848
0
                mp4_track_t *tk_tmp = &p_sys->track[i_track];
1849
0
                if( tk_tmp == tk ||
1850
0
                    !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) ||
1851
0
                   (!tk_tmp->b_selected && p_sys->b_seekable) ||
1852
0
                    tk_tmp->i_sample >= tk_tmp->i_sample_count )
1853
0
                    continue;
1854
1855
0
                vlc_tick_t i_nzdts = MP4_TrackGetDTSPTS( p_demux, tk_tmp, NULL );
1856
0
                if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD )
1857
0
                {
1858
                    /* Found a better candidate to avoid seeking */
1859
0
                    if( MP4_TrackGetPos( tk_tmp ) < MP4_TrackGetPos( tk ) )
1860
0
                        tk = tk_tmp;
1861
                    /* Note: previous candidate will be repicked on next loop */
1862
0
                }
1863
0
            }
1864
1865
65
            uint64_t i_pos = MP4_TrackGetPos( tk );
1866
65
            int i_ret = DemuxTrack( p_demux, tk, i_pos, i_max_preload );
1867
1868
65
            if( i_ret == VLC_DEMUXER_SUCCESS )
1869
64
                i_status = VLC_DEMUXER_SUCCESS;
1870
65
        }
1871
1872
255
        if( i_minnzdts != VLC_TICK_MAX && VLC_TICK_0 + i_minnzdts > p_sys->i_pcr )
1873
64
        {
1874
64
            p_sys->i_pcr = VLC_TICK_0 + i_minnzdts;
1875
64
            es_out_SetPCR( p_demux->out, p_sys->i_pcr );
1876
64
        }
1877
1878
255
        if( i_status != VLC_DEMUXER_SUCCESS || !tk )
1879
190
            break;
1880
255
    }
1881
1882
190
    p_sys->i_nztime += DEMUX_INCREMENT;
1883
190
    if( p_sys->i_pcr != VLC_TICK_INVALID && VLC_TICK_0 + p_sys->i_nztime > p_sys->i_pcr )
1884
1
    {
1885
1
        p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime;
1886
1
        es_out_SetPCR( p_demux->out, p_sys->i_pcr );
1887
1
    }
1888
1889
    /* */
1890
190
    MP4_UpdateSeekpoint( p_demux, i_nztime + DEMUX_INCREMENT );
1891
1892
190
    return i_status;
1893
190
}
1894
1895
static int Demux( demux_t *p_demux )
1896
190
{
1897
190
    demux_sys_t *p_sys = p_demux->p_sys;
1898
1899
190
    assert( ! p_sys->b_fragmented );
1900
1901
190
    int i_status = DemuxMoov( p_demux );
1902
1903
190
    if( i_status == VLC_DEMUXER_EOS )
1904
1
        i_status = VLC_DEMUXER_EOF;
1905
1906
190
    return i_status;
1907
190
}
1908
1909
static void MP4_UpdateSeekpoint( demux_t *p_demux, vlc_tick_t i_time )
1910
190
{
1911
190
    demux_sys_t *p_sys = p_demux->p_sys;
1912
190
    int i;
1913
190
    if( !p_sys->p_title )
1914
190
        return;
1915
0
    for( i = 0; i < p_sys->p_title->i_seekpoint; i++ )
1916
0
    {
1917
0
        if( i_time < p_sys->p_title->seekpoint[i]->i_time_offset )
1918
0
            break;
1919
0
    }
1920
0
    i--;
1921
1922
0
    if( i != p_sys->i_seekpoint && i >= 0 )
1923
0
    {
1924
0
        p_sys->i_seekpoint = i;
1925
0
        p_sys->seekpoint_changed = true;
1926
0
    }
1927
0
}
1928
/*****************************************************************************
1929
 * Seek: Go to i_date
1930
******************************************************************************/
1931
static int Seek( demux_t *p_demux, vlc_tick_t i_date, bool b_accurate )
1932
0
{
1933
0
    demux_sys_t *p_sys = p_demux->p_sys;
1934
0
    unsigned int i_track;
1935
1936
    /* Now for each stream try to go to this time */
1937
0
    vlc_tick_t i_start = i_date;
1938
0
    for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1939
0
    {
1940
0
        mp4_track_t *tk = &p_sys->track[i_track];
1941
        /* FIXME: we should find the lowest time from tracks with indexes.
1942
           considering only video for now */
1943
0
        if( tk->fmt.i_cat != VIDEO_ES || MP4_isMetadata( tk ) || !tk->b_ok )
1944
0
            continue;
1945
0
        if( MP4_TrackSeek( p_demux, tk, i_date ) == VLC_SUCCESS )
1946
0
        {
1947
0
            vlc_tick_t i_seeked = MP4_TrackGetDTSPTS( p_demux, tk, NULL );
1948
0
            if( i_seeked < i_start )
1949
0
                i_start = i_seeked;
1950
0
        }
1951
0
    }
1952
1953
0
    msg_Dbg( p_demux, "seeking with %"PRId64 "ms %s to %"PRId64,
1954
0
             MS_FROM_VLC_TICK(i_date - i_start),
1955
0
            !b_accurate ? "alignment" : "preroll (use input-fast-seek to avoid)", i_date );
1956
1957
0
    for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
1958
0
    {
1959
0
        mp4_track_t *tk = &p_sys->track[i_track];
1960
0
        tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
1961
0
        if( tk->fmt.i_cat == VIDEO_ES || !tk->b_ok )
1962
0
            continue;
1963
0
        MP4_TrackSeek( p_demux, tk, i_start );
1964
0
    }
1965
1966
0
    MP4_UpdateSeekpoint( p_demux, i_date );
1967
0
    MP4ASF_ResetFrames( p_sys );
1968
    /* update global time */
1969
0
    p_sys->i_nztime = i_start;
1970
0
    p_sys->i_pcr  = VLC_TICK_INVALID;
1971
1972
0
    if( b_accurate )
1973
0
        es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1974
1975
0
    return VLC_SUCCESS;
1976
0
}
1977
1978
static int FragPrepareChunk( demux_t *p_demux, MP4_Box_t *p_moof,
1979
                             MP4_Box_t *p_sidx, stime_t i_moof_time, bool b_discontinuity )
1980
0
{
1981
0
    demux_sys_t *p_sys = p_demux->p_sys;
1982
1983
0
    if( b_discontinuity )
1984
0
    {
1985
0
        for( unsigned i=0; i<p_sys->i_tracks; i++ )
1986
0
            p_sys->track[i].context.b_resync_time_offset = true;
1987
0
    }
1988
1989
0
    if( FragCreateTrunIndex( p_demux, p_moof, p_sidx, i_moof_time ) == VLC_SUCCESS )
1990
0
    {
1991
0
        for( unsigned i=0; i<p_sys->i_tracks; i++ )
1992
0
        {
1993
0
            mp4_track_t *p_track = &p_sys->track[i];
1994
0
            if( p_track->context.runs.i_count )
1995
0
            {
1996
0
                const mp4_run_t *p_run = &p_track->context.runs.p_array[0];
1997
0
                p_track->context.i_trun_sample_pos = p_run->i_offset;
1998
0
                p_track->context.i_trun_sample = 0;
1999
0
                p_track->i_time = p_run->i_first_dts;
2000
0
            }
2001
0
        }
2002
0
        return VLC_SUCCESS;
2003
0
    }
2004
2005
0
    return VLC_EGENERIC;
2006
0
}
2007
2008
static vlc_tick_t FragGetDemuxTimeFromTracksTime( demux_sys_t *p_sys )
2009
0
{
2010
0
    vlc_tick_t i_time = VLC_TICK_MAX;
2011
0
    for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
2012
0
    {
2013
0
        if( p_sys->track[i].context.runs.i_count == 0 )
2014
0
            continue;
2015
0
        vlc_tick_t i_ttime = MP4_rescale_mtime( p_sys->track[i].i_time,
2016
0
                                             p_sys->track[i].i_timescale );
2017
0
        i_time = __MIN( i_time, i_ttime );
2018
0
    }
2019
0
    return i_time;
2020
0
}
2021
2022
static uint32_t FragGetMoofSequenceNumber( MP4_Box_t *p_moof )
2023
0
{
2024
0
    const MP4_Box_t *p_mfhd = MP4_BoxGet( p_moof, "mfhd" );
2025
0
    if( p_mfhd && BOXDATA(p_mfhd) )
2026
0
        return BOXDATA(p_mfhd)->i_sequence_number;
2027
0
    return 0;
2028
0
}
2029
2030
static int FragSeekLoadFragment( demux_t *p_demux, uint32_t i_moox )
2031
0
{
2032
0
    demux_sys_t *p_sys = p_demux->p_sys;
2033
0
    MP4_Box_t *p_moox;
2034
2035
0
    if( i_moox == ATOM_moov )
2036
0
    {
2037
0
        p_moox = p_sys->p_moov;
2038
0
    }
2039
0
    else
2040
0
    {
2041
0
        const uint8_t *p_peek;
2042
0
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 )
2043
0
            return VLC_EGENERIC;
2044
2045
0
        if( ATOM_moof != VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) )
2046
0
            return VLC_EGENERIC;
2047
2048
0
        MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s );
2049
0
        if(!p_vroot)
2050
0
            return VLC_EGENERIC;
2051
0
        p_moox = MP4_BoxExtract( &p_vroot->p_first, ATOM_moof );
2052
0
        MP4_BoxFree( p_vroot );
2053
2054
0
        if(!p_moox)
2055
0
            return VLC_EGENERIC;
2056
0
    }
2057
2058
0
    FragResetContext( p_sys );
2059
2060
    /* map context */
2061
0
    p_sys->context.p_fragment_atom = p_moox;
2062
0
    p_sys->context.i_current_box_type = i_moox;
2063
2064
0
    msg_Dbg( p_demux, "seeked to %4.4s at pos %" PRIu64, (char *) &i_moox, p_moox->i_pos );
2065
0
    return VLC_SUCCESS;
2066
0
}
2067
2068
static unsigned GetSeekTrackIndex( demux_sys_t *p_sys )
2069
0
{
2070
0
    unsigned cand = 0;
2071
0
    for( unsigned i=0; i<p_sys->i_tracks; i++ )
2072
0
    {
2073
0
        if( p_sys->track[i].fmt.i_cat == VIDEO_ES ||
2074
0
            p_sys->track[i].fmt.i_cat == AUDIO_ES )
2075
0
        {
2076
0
            if( cand != i && !p_sys->track[cand].b_selected )
2077
0
                cand = i;
2078
0
        }
2079
0
    }
2080
0
    return cand;
2081
0
}
2082
2083
static void FragTrunSeekToTime( mp4_track_t *p_track, stime_t i_target_time )
2084
0
{
2085
0
    if( !p_track->b_ok || p_track->context.runs.i_count < 1 )
2086
0
        return;
2087
2088
0
    unsigned i_run = 0;
2089
0
    unsigned i_sample = 0;
2090
0
    uint64_t i_pos = p_track->context.runs.p_array[0].i_offset;
2091
0
    stime_t  i_time = p_track->context.runs.p_array[0].i_first_dts;
2092
2093
0
    for( unsigned r = 0; r < p_track->context.runs.i_count; r++ )
2094
0
    {
2095
0
        const mp4_run_t *p_run = &p_track->context.runs.p_array[r];
2096
0
        const MP4_Box_data_trun_t *p_data =
2097
0
                    p_track->context.runs.p_array[r].p_trun->data.p_trun;
2098
0
        if( i_time > i_target_time )
2099
0
            break;
2100
2101
0
        i_run = r;
2102
0
        i_time = p_run->i_first_dts;
2103
0
        i_pos = p_run->i_offset;
2104
0
        i_sample = 0;
2105
2106
0
        uint32_t dur = p_track->context.i_default_sample_duration;
2107
0
        uint32_t len = p_track->context.i_default_sample_size;
2108
0
        for ( unsigned i=0; i<p_data->i_sample_count; i++ )
2109
0
        {
2110
0
            if( p_data->i_flags & MP4_TRUN_SAMPLE_DURATION )
2111
0
                dur = p_data->p_samples[i].i_duration;
2112
2113
            /* check condition */
2114
0
            if( i_time + dur > i_target_time )
2115
0
                break;
2116
2117
0
            if( p_data->i_flags & MP4_TRUN_SAMPLE_SIZE )
2118
0
                len = p_data->p_samples[i].i_size;
2119
2120
0
            i_time += dur;
2121
0
            i_pos += len;
2122
0
            i_sample++;
2123
0
        }
2124
0
    }
2125
0
    p_track->context.i_trun_sample = i_sample;
2126
0
    p_track->context.i_trun_sample_pos = i_pos;
2127
0
    p_track->context.runs.i_current = i_run;
2128
0
    p_track->i_time = i_time;
2129
0
}
2130
2131
0
#define INVALID_SEGMENT_TIME  VLC_TICK_MAX
2132
2133
static int FragSeekToTime( demux_t *p_demux, vlc_tick_t i_nztime, bool b_accurate )
2134
0
{
2135
0
    demux_sys_t *p_sys = p_demux->p_sys;
2136
0
    uint64_t i64 = UINT64_MAX;
2137
0
    uint32_t i_segment_type = ATOM_moof;
2138
0
    stime_t  i_segment_time = INVALID_SEGMENT_TIME;
2139
0
    vlc_tick_t i_sync_time = i_nztime;
2140
2141
0
    const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2142
0
    if ( !p_sys->i_timescale || !i_duration || !p_sys->b_seekable )
2143
0
         return VLC_EGENERIC;
2144
2145
0
    uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s );
2146
2147
0
    if ( !p_sys->b_fragments_probed && !p_sys->b_index_probed && p_sys->b_seekable )
2148
0
    {
2149
0
        ProbeIndex( p_demux );
2150
0
        p_sys->b_index_probed = true;
2151
0
    }
2152
2153
0
    const unsigned i_seek_track_index = GetSeekTrackIndex( p_sys );
2154
0
    const unsigned i_seek_track_ID = p_sys->track[i_seek_track_index].i_track_ID;
2155
2156
0
    if( MP4_rescale_qtime( i_nztime, p_sys->i_timescale )
2157
0
                     < GetMoovTrackDuration( p_sys, i_seek_track_ID ) )
2158
0
    {
2159
0
        i64 = p_sys->p_moov->i_pos;
2160
0
        i_segment_type = ATOM_moov;
2161
0
    }
2162
0
    else if( FragGetMoofBySidxIndex( p_demux, i_nztime, &i64, &i_sync_time ) == VLC_SUCCESS )
2163
0
    {
2164
        /* provides base offset */
2165
0
        i_segment_time = MP4_rescale_qtime( i_sync_time, p_sys->i_timescale );
2166
0
        msg_Dbg( p_demux, "seeking to sidx moof pos %" PRId64 " %" PRId64, i64, i_sync_time );
2167
0
    }
2168
0
    else
2169
0
    {
2170
0
        if( FragGetMoofByTfraIndex( p_demux, i_nztime, i_seek_track_ID, &i64, &i_sync_time ) == VLC_SUCCESS )
2171
0
        {
2172
            /* Does only provide segment position and a sync sample time */
2173
0
            msg_Dbg( p_demux, "seeking to sync point %" PRId64, i_sync_time );
2174
0
        }
2175
0
        else if( !p_sys->b_fragments_probed )
2176
0
        {
2177
0
            int i_ret = ProbeFragmentsChecked( p_demux );
2178
0
            if( i_ret != VLC_SUCCESS )
2179
0
                return i_ret;
2180
0
        }
2181
2182
0
        if( p_sys->b_fragments_probed && p_sys->p_fragsindex )
2183
0
        {
2184
0
            stime_t i_basetime = MP4_rescale_qtime( i_sync_time, p_sys->i_timescale );
2185
0
            if( !MP4_Fragments_Index_Lookup( p_sys->p_fragsindex, &i_basetime, &i64, i_seek_track_index ) )
2186
0
            {
2187
0
                p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
2188
0
                return VLC_EGENERIC;
2189
0
            }
2190
0
            msg_Dbg( p_demux, "seeking to fragment index pos %" PRId64 " %" PRId64, i64,
2191
0
                     MP4_rescale_mtime( i_basetime, p_sys->i_timescale ) );
2192
0
        }
2193
0
    }
2194
2195
0
    if( i64 == UINT64_MAX )
2196
0
    {
2197
0
        msg_Warn( p_demux, "seek by index failed" );
2198
0
        p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
2199
0
        return VLC_EGENERIC;
2200
0
    }
2201
2202
0
    msg_Dbg( p_demux, "final seek to fragment at %"PRId64, i64 );
2203
0
    if( vlc_stream_Seek( p_demux->s, i64 ) )
2204
0
    {
2205
0
        msg_Err( p_demux, "seek failed to %"PRId64, i64 );
2206
0
        p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
2207
0
        return VLC_EGENERIC;
2208
0
    }
2209
2210
    /* Context is killed on success */
2211
0
    if( FragSeekLoadFragment( p_demux, i_segment_type ) != VLC_SUCCESS )
2212
0
    {
2213
0
        p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
2214
0
        return VLC_EGENERIC;
2215
0
    }
2216
0
    if( i_segment_type == ATOM_moof )
2217
0
    {
2218
0
        MP4_Box_t *p_moox = p_sys->context.p_fragment_atom;
2219
0
        FragPrepareChunk( p_demux, p_moox, NULL, i_segment_time, true );
2220
0
        p_sys->context.i_lastseqnumber = FragGetMoofSequenceNumber( p_moox );
2221
2222
0
        p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys );
2223
0
    }
2224
0
    else
2225
0
    {
2226
0
        p_sys->i_nztime = i_sync_time;
2227
0
    }
2228
2229
0
    p_sys->i_pcr  = VLC_TICK_INVALID;
2230
2231
0
    for( unsigned i=0; i<p_sys->i_tracks; i++ )
2232
0
    {
2233
0
        if( i_segment_type == ATOM_moov )
2234
0
        {
2235
0
            MP4_TrackSeek( p_demux, &p_sys->track[i], i_sync_time );
2236
0
            p_sys->i_pcr  = VLC_TICK_INVALID;
2237
0
        }
2238
0
        else
2239
0
        {
2240
0
            stime_t i_tst = MP4_rescale_qtime( i_sync_time, p_sys->track[i].i_timescale );
2241
0
            FragTrunSeekToTime( &p_sys->track[i], i_tst );
2242
0
            p_sys->track[i].i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
2243
0
        }
2244
0
    }
2245
2246
0
    MP4ASF_ResetFrames( p_sys );
2247
    /* And set next display time in that trun/fragment */
2248
0
    if( b_accurate )
2249
0
        es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, VLC_TICK_0 + i_nztime );
2250
0
    return VLC_SUCCESS;
2251
0
}
2252
2253
static int FragSeekToPos( demux_t *p_demux, double f, bool b_accurate )
2254
0
{
2255
0
    demux_sys_t *p_sys = p_demux->p_sys;
2256
2257
0
    if ( !p_sys->b_seekable || !p_sys->i_timescale )
2258
0
        return VLC_EGENERIC;
2259
2260
0
    uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2261
0
    if( !i_duration && !p_sys->b_fragments_probed )
2262
0
    {
2263
0
        int i_ret = ProbeFragmentsChecked( p_demux );
2264
0
        if( i_ret != VLC_SUCCESS )
2265
0
            return i_ret;
2266
0
        i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2267
0
    }
2268
2269
0
    if( !i_duration )
2270
0
        return VLC_EGENERIC;
2271
2272
0
    return FragSeekToTime( p_demux, (vlc_tick_t)( f *
2273
0
                           MP4_rescale_mtime( i_duration, p_sys->i_timescale ) ), b_accurate );
2274
0
}
2275
2276
static void ItunMetaCallback( const struct qt_itunes_triplet_data *data, void *priv )
2277
0
{
2278
0
    demux_sys_t *p_sys = priv;
2279
0
    if( data->type == iTunSMPB )
2280
0
    {
2281
0
        p_sys->qt.i_delay_samples = data->SMPB.delay;
2282
0
    }
2283
0
    else if( data->type == iTunNORM )
2284
0
    {
2285
0
        p_sys->qt.f_replay_gain_norm = data->NORM.volume_adjust;
2286
0
        p_sys->qt.f_replay_gain_peak = data->NORM.peak;
2287
0
    }
2288
0
    else if( data->type == iTunEncodingParams )
2289
0
    {
2290
0
        p_sys->qt.i_target_bitrate = data->EncodingParams.target_bitrate;
2291
0
    }
2292
0
}
2293
2294
static int MP4_LoadMeta( demux_sys_t *p_sys, vlc_meta_t *p_meta )
2295
1
{
2296
1
    if( !p_meta )
2297
0
        return VLC_EGENERIC;
2298
2299
1
    const char *psz_metapath;
2300
1
    const MP4_Box_t *p_metaroot = MP4_GetMetaRoot( p_sys->p_root, &psz_metapath );
2301
2302
1
    MP4_GetCoverMetaURI( p_sys->p_root, p_metaroot, psz_metapath, p_meta );
2303
2304
1
    if( p_metaroot )
2305
0
        SetupMeta( p_meta, p_metaroot, ItunMetaCallback, p_sys );
2306
2307
1
    return VLC_SUCCESS;
2308
1
}
2309
2310
/*****************************************************************************
2311
 * Control:
2312
 *****************************************************************************/
2313
static int Control( demux_t *p_demux, int i_query, va_list args )
2314
0
{
2315
0
    demux_sys_t *p_sys = p_demux->p_sys;
2316
2317
0
    double f, *pf;
2318
0
    vlc_tick_t i64;
2319
0
    bool b;
2320
2321
0
    const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2322
2323
0
    switch( i_query )
2324
0
    {
2325
0
        case DEMUX_CAN_SEEK:
2326
0
            *va_arg( args, bool * ) = p_sys->b_seekable;
2327
0
            return VLC_SUCCESS;
2328
2329
0
        case DEMUX_GET_POSITION:
2330
0
            pf = va_arg( args, double * );
2331
0
            if( i_duration > 0 )
2332
0
            {
2333
0
                *pf = (double)p_sys->i_nztime /
2334
0
                      MP4_rescale_mtime( i_duration, p_sys->i_timescale );
2335
0
            }
2336
0
            else
2337
0
            {
2338
0
                *pf = 0.0;
2339
0
            }
2340
0
            return VLC_SUCCESS;
2341
2342
0
        case DEMUX_GET_SEEKPOINT:
2343
0
            *va_arg( args, int * ) = p_sys->i_seekpoint;
2344
0
            return VLC_SUCCESS;
2345
2346
0
        case DEMUX_SET_POSITION:
2347
0
            f = va_arg( args, double );
2348
0
            b = va_arg( args, int );
2349
0
            if ( p_demux->pf_demux == DemuxFrag )
2350
0
                return FragSeekToPos( p_demux, f, b );
2351
0
            else if( p_sys->i_timescale > 0 )
2352
0
            {
2353
0
                i64 = (vlc_tick_t)( f * MP4_rescale_mtime( p_sys->i_duration,
2354
0
                                                        p_sys->i_timescale ) );
2355
0
                return Seek( p_demux, i64, b );
2356
0
            }
2357
0
            else return VLC_EGENERIC;
2358
2359
0
        case DEMUX_GET_TIME:
2360
0
            *va_arg( args, vlc_tick_t * ) = (p_sys->i_max_pts_offset < p_sys->i_nztime)
2361
0
                                          ? p_sys->i_nztime - p_sys->i_max_pts_offset : 0;
2362
0
            return VLC_SUCCESS;
2363
2364
0
        case DEMUX_SET_TIME:
2365
0
            i64 = va_arg( args, vlc_tick_t );
2366
0
            b = va_arg( args, int );
2367
0
            if ( p_demux->pf_demux == DemuxFrag )
2368
0
                return FragSeekToTime( p_demux, i64, b );
2369
0
            else
2370
0
                return Seek( p_demux, i64, b );
2371
2372
0
        case DEMUX_GET_LENGTH:
2373
0
            if( p_sys->i_timescale > 0 )
2374
0
            {
2375
0
                *va_arg( args, vlc_tick_t * ) = MP4_rescale_mtime( i_duration,
2376
0
                                                           p_sys->i_timescale );
2377
0
            }
2378
0
            else *va_arg( args, vlc_tick_t * ) = 0;
2379
0
            return VLC_SUCCESS;
2380
2381
0
        case DEMUX_GET_FPS:
2382
0
            pf = va_arg( args, double * );
2383
0
            *pf = p_sys->f_fps;
2384
0
            return VLC_SUCCESS;
2385
2386
0
        case DEMUX_GET_ATTACHMENTS:
2387
0
        {
2388
0
            input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
2389
0
            int *pi_int = va_arg( args, int * );
2390
2391
0
            if( p_sys->i_attachments == -1 )
2392
0
                p_sys->i_attachments = MP4_GetAttachments( p_sys->p_root, &p_sys->pp_attachments );
2393
0
            if( !p_sys->i_attachments )
2394
0
                return VLC_EGENERIC;
2395
0
            *ppp_attach = calloc( p_sys->i_attachments, sizeof(**ppp_attach ) );
2396
0
            if( !*ppp_attach )
2397
0
                return VLC_ENOMEM;
2398
0
            for ( ssize_t i = 0; i < p_sys->i_attachments; ++i )
2399
0
            {
2400
0
                (*ppp_attach)[i] = vlc_input_attachment_Hold( p_sys->pp_attachments[i] );
2401
0
                msg_Dbg( p_demux, "adding attachment %s", (*ppp_attach)[i]->psz_name );
2402
0
            }
2403
0
            *pi_int = p_sys->i_attachments;
2404
2405
0
            return VLC_SUCCESS;
2406
0
        }
2407
2408
0
        case DEMUX_GET_META:
2409
0
        {
2410
0
            vlc_meta_t *p_meta = va_arg( args, vlc_meta_t *);
2411
2412
0
            if( !p_sys->p_meta )
2413
0
                return VLC_EGENERIC;
2414
2415
0
            vlc_meta_Merge( p_meta, p_sys->p_meta );
2416
2417
0
            return VLC_SUCCESS;
2418
0
        }
2419
2420
0
        case DEMUX_GET_TITLE_INFO:
2421
0
        {
2422
0
            input_title_t ***ppp_title = va_arg( args, input_title_t *** );
2423
0
            int *pi_int = va_arg( args, int* );
2424
0
            int *pi_title_offset = va_arg( args, int* );
2425
0
            int *pi_seekpoint_offset = va_arg( args, int* );
2426
2427
0
            if( !p_sys->p_title )
2428
0
                return VLC_EGENERIC;
2429
2430
0
            *pi_int = 1;
2431
0
            input_title_t **titles = malloc(sizeof(*titles));
2432
0
            if (titles == NULL)
2433
0
                return VLC_ENOMEM;
2434
2435
0
            titles[0] = vlc_input_title_Duplicate(p_sys->p_title);
2436
0
            if (titles[0] == NULL)
2437
0
            {
2438
0
                free(titles);
2439
0
                return VLC_ENOMEM;
2440
0
            }
2441
0
            *ppp_title = titles;
2442
0
            *pi_title_offset = 0;
2443
0
            *pi_seekpoint_offset = 0;
2444
0
            return VLC_SUCCESS;
2445
0
        }
2446
0
        case DEMUX_SET_TITLE:
2447
0
        {
2448
0
            const int i_title = va_arg( args, int );
2449
0
            if( !p_sys->p_title || i_title != 0 )
2450
0
                return VLC_EGENERIC;
2451
0
            return VLC_SUCCESS;
2452
0
        }
2453
0
        case DEMUX_SET_SEEKPOINT:
2454
0
        {
2455
0
            const int i_seekpoint = va_arg( args, int );
2456
0
            if( !p_sys->p_title )
2457
0
                return VLC_EGENERIC;
2458
0
            return Seek( p_demux, p_sys->p_title->seekpoint[i_seekpoint]->i_time_offset, true );
2459
0
        }
2460
0
        case DEMUX_TEST_AND_CLEAR_FLAGS:
2461
0
        {
2462
0
            unsigned *restrict flags = va_arg( args, unsigned * );
2463
2464
0
            if ((*flags & INPUT_UPDATE_SEEKPOINT) && p_sys->seekpoint_changed)
2465
0
            {
2466
0
                *flags = INPUT_UPDATE_SEEKPOINT;
2467
0
                p_sys->seekpoint_changed = false;
2468
0
            }
2469
0
            else
2470
0
                *flags = 0;
2471
0
            return VLC_SUCCESS;
2472
0
        }
2473
2474
0
        case DEMUX_GET_PTS_DELAY:
2475
0
        {
2476
0
            for( unsigned int i = 0; i < p_sys->i_tracks; i++ )
2477
0
            {
2478
0
                const MP4_Box_t *p_load;
2479
0
                if ( (p_load = MP4_BoxGet( p_sys->track[i].p_track, "load" )) &&
2480
0
                     BOXDATA(p_load)->i_duration > 0 )
2481
0
                {
2482
0
                    *va_arg(args, vlc_tick_t *) =
2483
0
                            MP4_rescale_mtime( BOXDATA(p_load)->i_duration,
2484
0
                                               p_sys->track[i].i_timescale );
2485
0
                    return VLC_SUCCESS;
2486
0
                }
2487
0
            }
2488
0
            return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2489
0
        }
2490
0
        case DEMUX_SET_NEXT_DEMUX_TIME:
2491
0
        case DEMUX_SET_GROUP_DEFAULT:
2492
0
        case DEMUX_SET_GROUP_ALL:
2493
0
        case DEMUX_SET_GROUP_LIST:
2494
0
        case DEMUX_HAS_UNSUPPORTED_META:
2495
0
        case DEMUX_CAN_RECORD:
2496
0
            return VLC_EGENERIC;
2497
2498
0
        case DEMUX_CAN_PAUSE:
2499
0
        case DEMUX_SET_PAUSE_STATE:
2500
0
        case DEMUX_CAN_CONTROL_PACE:
2501
0
            return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
2502
2503
0
        default:
2504
0
            return VLC_EGENERIC;
2505
0
    }
2506
0
}
2507
2508
/*****************************************************************************
2509
 * Close: frees unused data
2510
 *****************************************************************************/
2511
static void Close ( vlc_object_t * p_this )
2512
1
{
2513
1
    demux_t *  p_demux = (demux_t *)p_this;
2514
1
    demux_sys_t *p_sys = p_demux->p_sys;
2515
1
    unsigned int i_track;
2516
2517
1
    msg_Dbg( p_demux, "freeing all memory" );
2518
2519
1
    FragResetContext( p_sys );
2520
2521
1
    MP4_BoxFree( p_sys->p_root );
2522
2523
1
    if( p_sys->p_title )
2524
0
        vlc_input_title_Delete( p_sys->p_title );
2525
2526
1
    if( p_sys->p_meta )
2527
1
        vlc_meta_Delete( p_sys->p_meta );
2528
2529
1
    MP4_Fragments_Index_Delete( p_sys->p_fragsindex );
2530
2531
2
    for( i_track = 0; i_track < p_sys->i_tracks; i_track++ )
2532
1
        MP4_TrackClean( p_demux->out, &p_sys->track[i_track] );
2533
1
    free( p_sys->track );
2534
2535
1
    for ( ssize_t i = 0; i < p_sys->i_attachments; ++i )
2536
0
        vlc_input_attachment_Release( p_sys->pp_attachments[i] );
2537
1
    free( p_sys->pp_attachments );
2538
2539
1
    free( p_sys );
2540
1
}
2541
2542
2543
2544
/****************************************************************************
2545
 * Local functions, specific to vlc
2546
 ****************************************************************************/
2547
/* Chapters */
2548
static void LoadChapterGpac( demux_t  *p_demux, MP4_Box_t *p_chpl )
2549
0
{
2550
0
    demux_sys_t *p_sys = p_demux->p_sys;
2551
2552
0
    if( BOXDATA(p_chpl)->i_chapter == 0 )
2553
0
        return;
2554
2555
0
    p_sys->p_title = vlc_input_title_New();
2556
0
    if (p_sys->p_title == NULL)
2557
0
        return;
2558
2559
0
    for( int i = 0; i < BOXDATA(p_chpl)->i_chapter; i++ )
2560
0
    {
2561
0
        seekpoint_t *s = vlc_seekpoint_New();
2562
0
        if( s == NULL) continue;
2563
2564
0
        s->psz_name = strdup( BOXDATA(p_chpl)->chapter[i].psz_name );
2565
0
        if( s->psz_name == NULL)
2566
0
        {
2567
0
            vlc_seekpoint_Delete( s );
2568
0
            continue;
2569
0
        }
2570
2571
0
        EnsureUTF8( s->psz_name );
2572
0
        msftime_t offset = BOXDATA(p_chpl)->chapter[i].i_start;
2573
0
        s->i_time_offset = VLC_TICK_FROM_MSFTIME(offset);
2574
0
        TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2575
0
    }
2576
0
}
2577
static void LoadChapterGoPro( demux_t *p_demux, MP4_Box_t *p_hmmt )
2578
0
{
2579
0
    demux_sys_t *p_sys = p_demux->p_sys;
2580
2581
0
    p_sys->p_title = vlc_input_title_New();
2582
0
    if( p_sys->p_title )
2583
0
        for( unsigned i = 0; i < BOXDATA(p_hmmt)->i_chapter_count; i++ )
2584
0
        {
2585
0
            seekpoint_t *s = vlc_seekpoint_New();
2586
0
            if( s == NULL)
2587
0
                continue;
2588
2589
0
            if( asprintf( &s->psz_name, "HiLight tag #%u", i+1 ) == -1 )
2590
0
            {
2591
0
                s->psz_name = NULL;
2592
0
                vlc_seekpoint_Delete( s );
2593
0
                continue;
2594
0
            }
2595
2596
0
            EnsureUTF8( s->psz_name );
2597
            /* HiLights are stored in ms so we convert them to µs */
2598
0
            s->i_time_offset = VLC_TICK_FROM_MS( BOXDATA(p_hmmt)->pi_chapter_start[i] );
2599
0
            TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2600
0
        }
2601
0
}
2602
static void LoadChapterApple( demux_t  *p_demux, mp4_track_t *tk )
2603
0
{
2604
0
    demux_sys_t *p_sys = p_demux->p_sys;
2605
2606
0
    for( tk->i_sample = 0; tk->i_sample < tk->i_sample_count; tk->i_sample++ )
2607
0
    {
2608
0
        if( tk->i_sample >= tk->chunk[tk->i_chunk].i_sample_first +
2609
0
                            tk->chunk[tk->i_chunk].i_sample_count )
2610
0
            tk->i_chunk++;
2611
0
        TrackUpdateSampleAndTimes( tk );
2612
2613
0
        vlc_tick_t i_pts;
2614
0
        MP4_TrackGetDTSPTS( p_demux, tk, &i_pts );
2615
0
        uint32_t i_nb_samples = 0;
2616
0
        const uint32_t i_size = MP4_TrackGetReadSize( tk, &i_nb_samples );
2617
2618
0
        if( i_nb_samples > 1 )
2619
0
            break; /* should not happen */
2620
2621
0
        if( i_size > 0 && !vlc_stream_Seek( p_demux->s, MP4_TrackGetPos( tk ) ) )
2622
0
        {
2623
0
            char p_buffer[256];
2624
0
            const uint32_t i_read = stream_ReadU32( p_demux->s, p_buffer,
2625
0
                                                    __MIN( sizeof(p_buffer), i_size ) );
2626
0
            if( i_read > 2 )
2627
0
            {
2628
0
                const uint32_t i_string = __MIN( GetWBE(p_buffer), i_read-2 );
2629
0
                const char *psnz_string = &p_buffer[2];
2630
2631
0
                seekpoint_t *s = vlc_seekpoint_New();
2632
0
                if( s == NULL ) continue;
2633
2634
0
                if( i_string > 1 && !memcmp( psnz_string, "\xFF\xFE", 2 ) )
2635
0
                    s->psz_name = FromCharset( "UTF-16LE", psnz_string, i_string );
2636
0
                else
2637
0
                    s->psz_name = strndup( psnz_string, i_string );
2638
2639
0
                if( s->psz_name == NULL )
2640
0
                {
2641
0
                    vlc_seekpoint_Delete( s );
2642
0
                    continue;
2643
0
                }
2644
2645
0
                EnsureUTF8( s->psz_name );
2646
0
                s->i_time_offset = i_pts;
2647
2648
0
                if( !p_sys->p_title )
2649
0
                    p_sys->p_title = vlc_input_title_New();
2650
0
                TAB_APPEND( p_sys->p_title->i_seekpoint, p_sys->p_title->seekpoint, s );
2651
0
            }
2652
0
        }
2653
0
    }
2654
0
}
2655
static void LoadChapter( demux_t  *p_demux )
2656
1
{
2657
1
    demux_sys_t *p_sys = p_demux->p_sys;
2658
1
    MP4_Box_t *p_chpl;
2659
1
    MP4_Box_t *p_hmmt;
2660
2661
    /* Prefer Apple-style chap reference track: Nero chpl is limited to 255
2662
       chapters by its 1-byte count, so larger files carry the full list here. */
2663
1
    mp4_track_t *p_chap_track = NULL;
2664
2
    for( unsigned i = 0; i < p_sys->i_tracks; i++ )
2665
1
    {
2666
1
        mp4_track_t *tk = &p_sys->track[i];
2667
1
        if ( tk->b_ok && (tk->i_use_flags & USEAS_CHAPTERS) && tk->fmt.i_cat == SPU_ES )
2668
0
        {
2669
0
            p_chap_track = tk;
2670
0
            break;
2671
0
        }
2672
1
    }
2673
2674
1
    if( p_chap_track )
2675
0
    {
2676
0
        LoadChapterApple( p_demux, p_chap_track );
2677
0
    }
2678
1
    else if( ( p_chpl = MP4_BoxGet( p_sys->p_root, "/moov/udta/chpl" ) ) &&
2679
0
          BOXDATA(p_chpl) && BOXDATA(p_chpl)->i_chapter > 0 )
2680
0
    {
2681
0
        LoadChapterGpac( p_demux, p_chpl );
2682
0
    }
2683
1
    else if( ( p_hmmt = MP4_BoxGet( p_sys->p_root, "/moov/udta/HMMT" ) ) &&
2684
0
             BOXDATA(p_hmmt) && BOXDATA(p_hmmt)->pi_chapter_start && BOXDATA(p_hmmt)->i_chapter_count > 0 )
2685
0
    {
2686
0
        LoadChapterGoPro( p_demux, p_hmmt );
2687
0
    }
2688
2689
    /* Add duration if titles are enabled */
2690
1
    if( p_sys->p_title )
2691
0
    {
2692
0
        const uint64_t i_duration = __MAX(p_sys->i_duration, p_sys->i_cumulated_duration);
2693
0
        p_sys->p_title->i_length =
2694
0
                MP4_rescale_mtime( i_duration, p_sys->i_timescale );
2695
0
    }
2696
1
}
2697
2698
/* now create basic chunk data, the rest will be filled by MP4_CreateSamplesIndex */
2699
static int TrackCreateChunksIndex( demux_t *p_demux,
2700
                                   mp4_track_t *p_demux_track )
2701
1
{
2702
1
    MP4_Box_t *p_co64; /* give offset for each chunk, same for stco and co64 */
2703
1
    MP4_Box_t *p_stsc;
2704
2705
1
    unsigned int i_chunk;
2706
1
    unsigned int i_index, i_last;
2707
2708
1
    if( ( !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "stco" ) )&&
2709
0
          !(p_co64 = MP4_BoxGet( p_demux_track->p_stbl, "co64" ) ) )||
2710
1
        ( !(p_stsc = MP4_BoxGet( p_demux_track->p_stbl, "stsc" ) ) ))
2711
0
    {
2712
0
        return( VLC_EGENERIC );
2713
0
    }
2714
2715
1
    p_demux_track->i_chunk_count = BOXDATA(p_co64)->i_entry_count;
2716
1
    if( !p_demux_track->i_chunk_count )
2717
0
    {
2718
0
        msg_Warn( p_demux, "no chunk defined" );
2719
0
    }
2720
1
    p_demux_track->chunk = calloc( p_demux_track->i_chunk_count,
2721
1
                                   sizeof( mp4_chunk_t ) );
2722
1
    if( p_demux_track->chunk == NULL )
2723
0
    {
2724
0
        return VLC_ENOMEM;
2725
0
    }
2726
2727
    /* first we read chunk offset */
2728
28
    for( i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2729
27
    {
2730
27
        mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2731
2732
27
        ck->i_offset = BOXDATA(p_co64)->i_chunk_offset[i_chunk];
2733
2734
27
        ck->i_first_dts = 0;
2735
27
        ck->i_entries_dts = 0;
2736
27
        ck->p_sample_count_dts = NULL;
2737
27
        ck->p_sample_delta_dts = NULL;
2738
27
        ck->i_entries_pts = 0;
2739
27
        ck->p_sample_count_pts = NULL;
2740
27
        ck->p_sample_offset_pts = NULL;
2741
27
    }
2742
2743
    /* now we read index for SampleEntry( soun vide mp4a mp4v ...)
2744
        to be used for the sample XXX begin to 1
2745
        We construct it beginning at the end */
2746
1
    i_last = p_demux_track->i_chunk_count; /* last chunk proceded */
2747
1
    i_index = BOXDATA(p_stsc)->i_entry_count;
2748
2749
3
    while( i_index-- > 0 )
2750
2
    {
2751
2
        for( i_chunk = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2752
29
             i_chunk < i_last; i_chunk++ )
2753
27
        {
2754
27
            if( i_chunk >= p_demux_track->i_chunk_count )
2755
0
            {
2756
0
                msg_Warn( p_demux, "corrupted chunk table" );
2757
0
                return VLC_EGENERIC;
2758
0
            }
2759
2760
27
            p_demux_track->chunk[i_chunk].i_sample_description_index =
2761
27
                    BOXDATA(p_stsc)->i_sample_description_index[i_index];
2762
27
            p_demux_track->chunk[i_chunk].i_sample_count =
2763
27
                    BOXDATA(p_stsc)->i_samples_per_chunk[i_index];
2764
27
        }
2765
2
        i_last = BOXDATA(p_stsc)->i_first_chunk[i_index] - 1;
2766
2
    }
2767
2768
1
    p_demux_track->i_sample_count = 0;
2769
1
    bool b_broken = false;
2770
1
    if ( p_demux_track->i_chunk_count )
2771
1
    {
2772
1
        p_demux_track->chunk[0].i_sample_first = 0;
2773
1
        p_demux_track->i_sample_count += p_demux_track->chunk[0].i_sample_count;
2774
2775
1
        const mp4_chunk_t *prev = &p_demux_track->chunk[0];
2776
27
        for( i_chunk = 1; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2777
26
        {
2778
26
            mp4_chunk_t *cur = &p_demux_track->chunk[i_chunk];
2779
26
            if( unlikely(UINT32_MAX - cur->i_sample_count < p_demux_track->i_sample_count) )
2780
0
            {
2781
0
                b_broken = true;
2782
0
                break;
2783
0
            }
2784
26
            p_demux_track->i_sample_count += cur->i_sample_count;
2785
26
            cur->i_sample_first = prev->i_sample_first + prev->i_sample_count;
2786
26
            prev = cur;
2787
26
        }
2788
1
    }
2789
2790
1
    if( unlikely(b_broken) )
2791
0
    {
2792
0
        msg_Err( p_demux, "Overflow in chunks total samples count" );
2793
0
        return VLC_EGENERIC;
2794
0
    }
2795
2796
1
    msg_Dbg( p_demux, "track[Id 0x%x] read %d chunk",
2797
1
             p_demux_track->i_track_ID, p_demux_track->i_chunk_count );
2798
2799
1
    return VLC_SUCCESS;
2800
1
}
2801
2802
static int xTTS_CountEntries( demux_t *p_demux, uint32_t *pi_entry /* out */,
2803
                              const uint32_t i_index,
2804
                              uint32_t i_index_samples_left,
2805
                              uint32_t i_sample_count,
2806
                              const uint32_t *pi_index_sample_count,
2807
                              const uint32_t i_table_count )
2808
27
{
2809
27
    uint32_t i_array_offset;
2810
28
    while( i_sample_count > 0 )
2811
27
    {
2812
27
        if ( likely((UINT32_MAX - i_index) >= *pi_entry) )
2813
27
            i_array_offset = i_index + *pi_entry;
2814
0
        else
2815
0
            return VLC_EGENERIC;
2816
2817
27
        if ( i_array_offset >= i_table_count )
2818
0
        {
2819
0
            msg_Err( p_demux, "invalid index counting total samples %u %u", i_array_offset,  i_table_count );
2820
0
            return VLC_EINVAL;
2821
0
        }
2822
2823
27
        if ( i_index_samples_left )
2824
26
        {
2825
26
            if ( i_index_samples_left > i_sample_count )
2826
26
            {
2827
26
                i_index_samples_left -= i_sample_count;
2828
26
                i_sample_count = 0;
2829
26
                *pi_entry +=1; /* No samples left, go copy */
2830
26
                break;
2831
26
            }
2832
0
            else
2833
0
            {
2834
0
                i_sample_count -= i_index_samples_left;
2835
0
                i_index_samples_left = 0;
2836
0
                *pi_entry += 1;
2837
0
                continue;
2838
0
            }
2839
26
        }
2840
1
        else
2841
1
        {
2842
1
            i_sample_count -= __MIN( i_sample_count, pi_index_sample_count[i_array_offset] );
2843
1
            *pi_entry += 1;
2844
1
        }
2845
27
    }
2846
2847
27
    return VLC_SUCCESS;
2848
27
}
2849
2850
static int TrackCreateSamplesIndex( demux_t *p_demux,
2851
                                    mp4_track_t *p_demux_track )
2852
1
{
2853
1
    MP4_Box_t *p_box;
2854
1
    MP4_Box_data_stsz_t *stsz;
2855
    /* TODO use also stss and stsh table for seeking */
2856
    /* FIXME use edit table */
2857
2858
    /* Find stsz or stz2
2859
     *  Gives the sample size for each samples. */
2860
1
    p_box = MP4_BoxGet( p_demux_track->p_stbl, "stsz" );
2861
1
    if( !p_box )
2862
0
        p_box = MP4_BoxGet( p_demux_track->p_stbl, "stz2" );
2863
1
    if( !p_box )
2864
0
    {
2865
0
        msg_Warn( p_demux, "cannot find STSZ or STZ2 box" );
2866
0
        return VLC_EGENERIC;
2867
0
    }
2868
1
    stsz = p_box->data.p_stsz;
2869
2870
    /* Use stsz table to create a sample number -> sample size table */
2871
1
    if( p_demux_track->i_sample_count != stsz->i_sample_count )
2872
1
    {
2873
1
        msg_Warn( p_demux, "Incorrect total samples stsc %" PRIu32 " <> stsz %"PRIu32 ", "
2874
1
                           " expect truncated media playback",
2875
1
                           p_demux_track->i_sample_count, stsz->i_sample_count );
2876
1
        p_demux_track->i_sample_count = __MIN(p_demux_track->i_sample_count, stsz->i_sample_count);
2877
1
    }
2878
2879
1
    if( stsz->i_sample_size )
2880
1
    {
2881
        /* 1: all sample have the same size, so no need to construct a table */
2882
1
        p_demux_track->i_sample_size = stsz->i_sample_size;
2883
1
        p_demux_track->p_sample_size = NULL;
2884
1
    }
2885
0
    else
2886
0
    {
2887
        /* 2: each sample can have a different size */
2888
0
        p_demux_track->i_sample_size = 0;
2889
0
        p_demux_track->p_sample_size =
2890
0
            calloc( p_demux_track->i_sample_count, sizeof( uint32_t ) );
2891
0
        if( p_demux_track->p_sample_size == NULL )
2892
0
            return VLC_ENOMEM;
2893
2894
0
        for( uint32_t i_sample = 0; i_sample < p_demux_track->i_sample_count; i_sample++ )
2895
0
        {
2896
0
            p_demux_track->p_sample_size[i_sample] =
2897
0
                    stsz->i_entry_size[i_sample];
2898
0
        }
2899
0
    }
2900
2901
1
    if ( p_demux_track->i_chunk_count && p_demux_track->i_sample_size == 0 )
2902
0
    {
2903
0
        const mp4_chunk_t *lastchunk = &p_demux_track->chunk[p_demux_track->i_chunk_count - 1];
2904
0
        if( (uint64_t)lastchunk->i_sample_count + p_demux_track->i_chunk_count - 1 > stsz->i_sample_count )
2905
0
        {
2906
0
            msg_Err( p_demux, "invalid samples table: stsz table is too small" );
2907
0
            return VLC_EGENERIC;
2908
0
        }
2909
0
    }
2910
2911
    /* Use stts table to create a sample number -> dts table.
2912
     * XXX: if we don't want to waste too much memory, we can't expand
2913
     *  the box! so each chunk will contain an "extract" of this table
2914
     *  for fast research (problem with raw stream where a sample is sometime
2915
     *  just channels*bits_per_sample/8 */
2916
2917
     /* FIXME: refactor STTS & CTTS, STTS having now only few extra lines and
2918
      *        differing in 2/2 fields and 1 signedness */
2919
2920
1
    int64_t i_next_dts = 0;
2921
    /* Find stts
2922
     *  Gives mapping between sample and decoding time
2923
     */
2924
1
    p_box = MP4_BoxGet( p_demux_track->p_stbl, "stts" );
2925
1
    if( !p_box )
2926
0
    {
2927
0
        msg_Warn( p_demux, "cannot find STTS box" );
2928
0
        return VLC_EGENERIC;
2929
0
    }
2930
1
    else
2931
1
    {
2932
1
        MP4_Box_data_stts_t *stts = p_box->data.p_stts;
2933
2934
1
        msg_Warn( p_demux, "STTS table of %"PRIu32" entries", stts->i_entry_count );
2935
2936
        /* Create sample -> dts table per chunk */
2937
1
        uint32_t i_index = 0;
2938
1
        uint32_t i_current_index_samples_left = 0;
2939
2940
28
        for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
2941
27
        {
2942
27
            mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
2943
27
            uint32_t i_sample_count;
2944
2945
            /* save first dts */
2946
27
            ck->i_first_dts = i_next_dts;
2947
2948
            /* count how many entries are needed for this chunk
2949
             * for p_sample_delta_dts and p_sample_count_dts */
2950
27
            ck->i_entries_dts = 0;
2951
2952
27
            int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_dts, i_index,
2953
27
                                           i_current_index_samples_left,
2954
27
                                           ck->i_sample_count,
2955
27
                                           stts->pi_sample_count,
2956
27
                                           stts->i_entry_count );
2957
27
            if ( i_ret == VLC_EGENERIC )
2958
0
                return i_ret;
2959
2960
            /* allocate them */
2961
27
            i_ret = MP4_ChunkAllocEntries( ck->i_entries_dts,
2962
27
                                           ck->small_dts_buf,
2963
27
                                           &ck->p_sample_count_dts,
2964
27
                                           &ck->p_sample_delta_dts );
2965
27
            if( i_ret )
2966
0
            {
2967
0
                msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_dts );
2968
0
                ck->i_entries_dts = 0;
2969
0
                return i_ret;
2970
0
            }
2971
2972
            /* now copy */
2973
27
            i_sample_count = ck->i_sample_count;
2974
2975
28
            for( uint32_t i = 0; i < ck->i_entries_dts; i++ )
2976
27
            {
2977
27
                if ( i_current_index_samples_left )
2978
26
                {
2979
26
                    if ( i_current_index_samples_left > i_sample_count )
2980
26
                    {
2981
26
                        ck->p_sample_count_dts[i] = i_sample_count;
2982
26
                        ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2983
26
                        i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2984
26
                        if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
2985
26
                        i_current_index_samples_left -= i_sample_count;
2986
26
                        i_sample_count = 0;
2987
26
                        assert( i == ck->i_entries_dts - 1 );
2988
26
                        break;
2989
26
                    }
2990
0
                    else
2991
0
                    {
2992
0
                        ck->p_sample_count_dts[i] = i_current_index_samples_left;
2993
0
                        ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
2994
0
                        i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
2995
0
                        if ( i_current_index_samples_left ) ck->i_duration = i_next_dts - ck->i_first_dts;
2996
0
                        i_sample_count -= i_current_index_samples_left;
2997
0
                        i_current_index_samples_left = 0;
2998
0
                        i_index++;
2999
0
                    }
3000
26
                }
3001
1
                else
3002
1
                {
3003
1
                    if ( stts->pi_sample_count[i_index] > i_sample_count )
3004
1
                    {
3005
1
                        ck->p_sample_count_dts[i] = i_sample_count;
3006
1
                        ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
3007
1
                        i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
3008
1
                        if ( i_sample_count ) ck->i_duration = i_next_dts - ck->i_first_dts;
3009
1
                        i_current_index_samples_left = stts->pi_sample_count[i_index] - i_sample_count;
3010
1
                        i_sample_count = 0;
3011
1
                        assert( i == ck->i_entries_dts - 1 );
3012
                        // keep building from same index
3013
1
                    }
3014
0
                    else
3015
0
                    {
3016
0
                        ck->p_sample_count_dts[i] = stts->pi_sample_count[i_index];
3017
0
                        ck->p_sample_delta_dts[i] = stts->pi_sample_delta[i_index];
3018
0
                        i_next_dts += (int64_t)ck->p_sample_count_dts[i] * stts->pi_sample_delta[i_index];
3019
0
                        if ( stts->pi_sample_count[i_index] ) ck->i_duration = i_next_dts - ck->i_first_dts;
3020
0
                        i_sample_count -= stts->pi_sample_count[i_index];
3021
0
                        i_index++;
3022
0
                    }
3023
1
                }
3024
3025
27
            }
3026
27
        }
3027
1
    }
3028
3029
3030
    /* Find ctts
3031
     *  Gives the delta between decoding time (dts) and composition table (pts)
3032
     */
3033
1
    p_box = MP4_BoxGet( p_demux_track->p_stbl, "ctts" );
3034
1
    if( p_box && p_box->data.p_ctts )
3035
0
    {
3036
0
        MP4_Box_data_ctts_t *ctts = p_box->data.p_ctts;
3037
3038
0
        msg_Warn( p_demux, "CTTS table of %"PRIu32" entries", ctts->i_entry_count );
3039
3040
0
        int64_t i_cts_shift = 0;
3041
0
        const MP4_Box_t *p_cslg = MP4_BoxGet( p_demux_track->p_stbl, "cslg" );
3042
0
        if( p_cslg && BOXDATA(p_cslg) )
3043
0
        {
3044
0
            i_cts_shift = BOXDATA(p_cslg)->ct_to_dts_shift;
3045
0
        }
3046
0
        else if( ctts->i_entry_count ) /* Compute for Quicktime */
3047
0
        {
3048
0
            for( uint32_t i = 0; i < ctts->i_entry_count; i++ )
3049
0
            {
3050
0
                if( ctts->pi_sample_offset[i] < 0 && ctts->pi_sample_offset[i] < -i_cts_shift )
3051
0
                    i_cts_shift = -ctts->pi_sample_offset[i];
3052
0
            }
3053
0
        }
3054
0
        p_demux_track->i_cts_shift = i_cts_shift;
3055
3056
        /* Create pts-dts table per chunk */
3057
0
        uint32_t i_index = 0;
3058
0
        uint32_t i_current_index_samples_left = 0;
3059
3060
0
        for( uint32_t i_chunk = 0; i_chunk < p_demux_track->i_chunk_count; i_chunk++ )
3061
0
        {
3062
0
            mp4_chunk_t *ck = &p_demux_track->chunk[i_chunk];
3063
0
            uint32_t i_sample_count;
3064
3065
            /* count how many entries are needed for this chunk
3066
             * for p_sample_offset_pts and p_sample_count_pts */
3067
0
            ck->i_entries_pts = 0;
3068
0
            int i_ret = xTTS_CountEntries( p_demux, &ck->i_entries_pts, i_index,
3069
0
                                           i_current_index_samples_left,
3070
0
                                           ck->i_sample_count,
3071
0
                                           ctts->pi_sample_count,
3072
0
                                           ctts->i_entry_count );
3073
0
            if ( i_ret == VLC_EGENERIC )
3074
0
                return i_ret;
3075
3076
            /* allocate them */
3077
0
            i_ret = MP4_ChunkAllocEntries( ck->i_entries_pts,
3078
0
                                           ck->small_pts_buf,
3079
0
                                           &ck->p_sample_count_pts,
3080
0
                                           &ck->p_sample_offset_pts );
3081
0
            if( i_ret )
3082
0
            {
3083
0
                msg_Err( p_demux, "can't allocate memory for i_entry=%"PRIu32, ck->i_entries_pts );
3084
0
                ck->i_entries_pts = 0;
3085
0
                return i_ret;
3086
0
            }
3087
3088
            /* now copy */
3089
0
            i_sample_count = ck->i_sample_count;
3090
3091
0
            for( uint32_t i = 0; i < ck->i_entries_pts; i++ )
3092
0
            {
3093
0
                int64_t i_ctsdelta = ctts->pi_sample_offset[i_index] + i_cts_shift;
3094
0
                if( i_ctsdelta < 0 ) /* should not */
3095
0
                    i_ctsdelta = 0;
3096
0
                ck->p_sample_offset_pts[i] = i_ctsdelta;
3097
0
                if ( i_current_index_samples_left )
3098
0
                {
3099
0
                    if ( i_current_index_samples_left > i_sample_count )
3100
0
                    {
3101
0
                        ck->p_sample_count_pts[i] = i_sample_count;
3102
0
                        i_current_index_samples_left -= i_sample_count;
3103
0
                        i_sample_count = 0;
3104
0
                        assert( i == ck->i_entries_pts - 1 );
3105
0
                        break;
3106
0
                    }
3107
0
                    else
3108
0
                    {
3109
0
                        ck->p_sample_count_pts[i] = i_current_index_samples_left;
3110
0
                        i_sample_count -= i_current_index_samples_left;
3111
0
                        i_current_index_samples_left = 0;
3112
0
                        i_index++;
3113
0
                    }
3114
0
                }
3115
0
                else
3116
0
                {
3117
0
                    if ( ctts->pi_sample_count[i_index] > i_sample_count )
3118
0
                    {
3119
0
                        ck->p_sample_count_pts[i] = i_sample_count;
3120
0
                        i_current_index_samples_left = ctts->pi_sample_count[i_index] - i_sample_count;
3121
0
                        i_sample_count = 0;
3122
0
                        assert( i == ck->i_entries_pts - 1 );
3123
                        // keep building from same index
3124
0
                    }
3125
0
                    else
3126
0
                    {
3127
0
                        ck->p_sample_count_pts[i] = ctts->pi_sample_count[i_index];
3128
0
                        i_sample_count -= ctts->pi_sample_count[i_index];
3129
0
                        i_index++;
3130
0
                    }
3131
0
                }
3132
0
            }
3133
0
        }
3134
0
    }
3135
3136
1
    msg_Dbg( p_demux, "track[Id 0x%x] read %"PRIu32" samples length:%"PRId64"s",
3137
1
             p_demux_track->i_track_ID, p_demux_track->i_sample_count,
3138
1
             i_next_dts / p_demux_track->i_timescale );
3139
3140
1
    return VLC_SUCCESS;
3141
1
}
3142
3143
3144
/**
3145
 * It computes the sample rate for a video track using the given sample
3146
 * description index
3147
 */
3148
static void TrackGetESSampleRate( demux_t *p_demux,
3149
                                  unsigned *pi_num, unsigned *pi_den,
3150
                                  const mp4_track_t *p_track,
3151
                                  unsigned i_sd_index,
3152
                                  unsigned i_chunk )
3153
0
{
3154
0
    demux_sys_t *p_sys = p_demux->p_sys;
3155
0
    *pi_num = 0;
3156
0
    *pi_den = 0;
3157
3158
0
    MP4_Box_t *p_trak = MP4_GetTrakByTrackID( MP4_BoxGet( p_sys->p_root,
3159
0
                                                          "/moov" ),
3160
0
                                              p_track->i_track_ID );
3161
0
    MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
3162
0
    if ( p_mdhd && BOXDATA(p_mdhd) )
3163
0
    {
3164
0
        vlc_ureduce( pi_num, pi_den,
3165
0
                     (uint64_t) BOXDATA(p_mdhd)->i_timescale * p_track->i_sample_count,
3166
0
                     (uint64_t) BOXDATA(p_mdhd)->i_duration,
3167
0
                     UINT16_MAX );
3168
0
        return;
3169
0
    }
3170
3171
0
    if( p_track->i_chunk_count == 0 )
3172
0
        return;
3173
3174
    /* */
3175
0
    const mp4_chunk_t *p_chunk = &p_track->chunk[i_chunk];
3176
0
    while( p_chunk > &p_track->chunk[0] &&
3177
0
           p_chunk[-1].i_sample_description_index == i_sd_index )
3178
0
    {
3179
0
        p_chunk--;
3180
0
    }
3181
3182
0
    uint64_t i_sample = 0;
3183
0
    uint64_t i_total_duration = 0;
3184
0
    do
3185
0
    {
3186
0
        i_sample += p_chunk->i_sample_count;
3187
0
        i_total_duration += p_chunk->i_duration;
3188
0
        p_chunk++;
3189
0
    }
3190
0
    while( p_chunk < &p_track->chunk[p_track->i_chunk_count] &&
3191
0
           p_chunk->i_sample_description_index == i_sd_index );
3192
3193
0
    if( i_sample > 0 && i_total_duration )
3194
0
        vlc_ureduce( pi_num, pi_den,
3195
0
                     i_sample * p_track->i_timescale,
3196
0
                     i_total_duration,
3197
0
                     UINT16_MAX);
3198
0
}
3199
3200
static void TrackConfigInit( track_config_t *p_cfg )
3201
1
{
3202
1
    memset( p_cfg, 0, sizeof(*p_cfg) );
3203
1
}
3204
3205
static void TrackConfigApply( const track_config_t *p_cfg,
3206
                              mp4_track_t *p_track )
3207
1
{
3208
1
    if( p_cfg->i_timescale_override )
3209
0
        p_track->i_timescale = p_cfg->i_timescale_override;
3210
1
     if( p_cfg->i_sample_size_override )
3211
0
        p_track->i_sample_size = p_cfg->i_sample_size_override;
3212
1
     p_track->p_asf = p_cfg->p_asf;
3213
1
     memcpy( p_track->rgi_chans_reordering, p_cfg->rgi_chans_reordering,
3214
1
             AOUT_CHAN_MAX * sizeof(p_cfg->rgi_chans_reordering[0]) );
3215
1
     p_track->i_chans_to_reorder = p_cfg->i_chans_to_reorder;
3216
1
     p_track->i_block_flags = p_cfg->i_block_flags;
3217
1
     p_track->b_ignore_implicit_pts = p_cfg->b_ignore_implicit_pts;
3218
1
}
3219
3220
static int TrackFillConfig( demux_t *p_demux, const mp4_track_t *p_track,
3221
                            const MP4_Box_t *p_sample,unsigned i_chunk,
3222
                            es_format_t *p_fmt, track_config_t *p_cfg )
3223
1
{
3224
    /* */
3225
1
    switch( p_track->fmt.i_cat )
3226
1
    {
3227
0
    case VIDEO_ES:
3228
0
        if ( p_sample->i_handler != ATOM_vide ||
3229
0
             !SetupVideoES( p_demux, p_track, p_sample, p_fmt, p_cfg ) )
3230
0
            return VLC_EGENERIC;
3231
3232
        /* Set frame rate */
3233
0
        TrackGetESSampleRate( p_demux,
3234
0
                              &p_fmt->video.i_frame_rate,
3235
0
                              &p_fmt->video.i_frame_rate_base,
3236
0
                              p_track, p_sample->i_index, i_chunk );
3237
0
        break;
3238
3239
1
    case AUDIO_ES:
3240
1
        if ( p_sample->i_handler != ATOM_soun ||
3241
1
             !SetupAudioES( p_demux, p_track, p_sample, p_fmt, p_cfg ) )
3242
0
            return VLC_EGENERIC;
3243
1
        break;
3244
3245
1
    case SPU_ES:
3246
0
        if ( ( p_sample->i_handler != ATOM_text &&
3247
0
               p_sample->i_handler != ATOM_subt &&
3248
0
               p_sample->i_handler != ATOM_sbtl &&
3249
0
               p_sample->i_handler != ATOM_clcp ) ||
3250
0
             !SetupSpuES( p_demux, p_track, p_sample, p_fmt, p_cfg ) )
3251
0
           return VLC_EGENERIC;
3252
0
        break;
3253
3254
0
    default:
3255
0
        break;
3256
1
    }
3257
3258
1
    return VLC_SUCCESS;
3259
1
}
3260
3261
/*
3262
 * TrackCreateES:
3263
 * Create ES and PES to init decoder if needed, for a track starting at i_chunk
3264
 */
3265
static int TrackCreateES( demux_t *p_demux, mp4_track_t *p_track,
3266
                          unsigned int i_chunk, es_out_id_t **pp_es )
3267
1
{
3268
1
    demux_sys_t *p_sys = p_demux->p_sys;
3269
1
    unsigned int i_sample_description_index;
3270
3271
1
    if( p_sys->b_fragmented || p_track->i_chunk_count == 0 )
3272
0
        i_sample_description_index = 1; /* XXX */
3273
1
    else
3274
1
        i_sample_description_index =
3275
1
                p_track->chunk[i_chunk].i_sample_description_index;
3276
3277
1
    if( pp_es )
3278
1
        *pp_es = NULL;
3279
3280
1
    if( !i_sample_description_index )
3281
0
    {
3282
0
        msg_Warn( p_demux, "invalid SampleEntry index (track[Id 0x%x])",
3283
0
                  p_track->i_track_ID );
3284
0
        return VLC_EGENERIC;
3285
0
    }
3286
3287
1
    const MP4_Box_t *p_sample = MP4_BoxGetVa( p_track->p_stsd, "[%u]",
3288
1
                                            i_sample_description_index - 1 );
3289
1
    if( !p_sample ||
3290
1
        ( !p_sample->data.p_payload && p_track->fmt.i_cat != SPU_ES ) )
3291
0
    {
3292
0
        msg_Warn( p_demux, "cannot find SampleEntry (track[Id 0x%x])",
3293
0
                  p_track->i_track_ID );
3294
0
        return VLC_EGENERIC;
3295
0
    }
3296
3297
1
    track_config_t cfg;
3298
1
    TrackConfigInit( &cfg );
3299
1
    es_format_t *p_fmt = &p_track->fmt;
3300
3301
1
    p_track->fmt.i_id = p_track->i_track_ID;
3302
3303
1
    if( TrackFillConfig( p_demux, p_track, p_sample, i_chunk,
3304
1
                         p_fmt, &cfg ) != VLC_SUCCESS )
3305
0
    {
3306
0
        return VLC_EGENERIC;
3307
0
    }
3308
3309
1
    TrackConfigApply( &cfg, p_track );
3310
3311
1
    switch( p_fmt->i_cat )
3312
1
    {
3313
0
        case VIDEO_ES:
3314
0
            p_sys->f_fps = (float)p_fmt->video.i_frame_rate /
3315
0
                    (float)p_fmt->video.i_frame_rate_base;
3316
0
            break;
3317
1
        case AUDIO_ES:
3318
1
        {
3319
1
            int i_ret = VLC_EGENERIC;
3320
1
            if( p_sys->p_meta )
3321
1
            {
3322
1
                i_ret = vlc_replay_gain_CopyFromMeta( &p_fmt->audio_replay_gain, p_sys->p_meta );
3323
1
            }
3324
3325
            /* use replay gain if available; otherwise use sound check */
3326
1
            if( i_ret != VLC_SUCCESS )
3327
1
            {
3328
1
                audio_replay_gain_t *p_arg = &p_fmt->audio_replay_gain;
3329
1
                replay_gain_Reset( p_arg );
3330
3331
1
                if( isfinite(p_sys->qt.f_replay_gain_norm) )
3332
0
                {
3333
0
                    p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] = true;
3334
0
                    p_arg->pf_gain[AUDIO_REPLAY_GAIN_TRACK] = p_sys->qt.f_replay_gain_norm;
3335
0
                }
3336
3337
1
                if( isfinite(p_sys->qt.f_replay_gain_peak) )
3338
0
                {
3339
0
                    p_arg->pb_peak[AUDIO_REPLAY_GAIN_TRACK] = true;
3340
0
                    p_arg->pf_peak[AUDIO_REPLAY_GAIN_TRACK] = p_sys->qt.f_replay_gain_peak;
3341
0
                }
3342
3343
1
                if( p_arg->pb_gain[AUDIO_REPLAY_GAIN_TRACK] )
3344
0
                {
3345
                    /* sound check uses -16 LUFS as the reference level */
3346
0
                    p_arg->pb_reference_loudness = true;
3347
0
                    p_arg->pf_reference_loudness = -16.f;
3348
0
                }
3349
1
            }
3350
3351
1
            if( p_sys->qt.i_target_bitrate )
3352
0
            {
3353
0
                p_fmt->i_bitrate = p_sys->qt.i_target_bitrate;
3354
0
            }
3355
3356
1
            switch( p_fmt->i_codec )
3357
1
            {
3358
                /* When not specified, set max standard decoder delay */
3359
0
                case VLC_CODEC_MP4A:
3360
0
                case VLC_CODEC_OPUS:
3361
0
                case VLC_CODEC_MP3:
3362
0
                case VLC_CODEC_MP2:
3363
0
                case VLC_CODEC_MPGA:
3364
0
                {
3365
0
                    const unsigned i_rate = p_fmt->audio.i_rate ? p_fmt->audio.i_rate : p_track->i_timescale;
3366
0
                    const MP4_Box_t *p_elst = p_track->p_elst;
3367
0
                    if( p_elst && BOXDATA(p_elst)->i_entry_count &&
3368
0
                        BOXDATA(p_elst)->entries[0].i_media_time < 0 )
3369
0
                    {
3370
0
                        p_track->i_decoder_delay = MP4_rescale( BOXDATA(p_elst)->entries[0].i_segment_duration,
3371
0
                                                                p_sys->i_timescale,
3372
0
                                                                p_track->i_timescale );
3373
0
                    }
3374
0
                    else if( p_sys->qt.i_delay_samples > 0 )
3375
0
                    {
3376
0
                        p_track->i_decoder_delay =  MP4_rescale(
3377
0
                                    p_sys->qt.i_delay_samples, i_rate,
3378
0
                                    p_track->i_timescale );
3379
0
                    }
3380
                    /* AAC historical Apple decoder delay 2112 > 2048 */
3381
0
                    else if( p_fmt->i_codec == VLC_CODEC_MP4A )
3382
0
                    {
3383
0
                        p_track->i_decoder_delay =  MP4_rescale(
3384
0
                                    2112, i_rate,
3385
0
                                    p_track->i_timescale );
3386
0
                    }
3387
                    /* Opus has an expected 80ms discard on seek */
3388
0
                    else if( p_fmt->i_codec == VLC_CODEC_OPUS )
3389
0
                    {
3390
0
                        p_track->i_decoder_delay =  MP4_rescale(
3391
0
                                    80 * 48, 48000,
3392
0
                                    p_track->i_timescale );
3393
0
                    }
3394
                    /* MPEG have One frame delay */
3395
0
                    else if( p_fmt->i_codec == VLC_CODEC_MP3 )
3396
0
                    {
3397
                        /* https://lame.sourceforge.io/tech-FAQ.txt
3398
                         * https://www.compuphase.com/mp3/mp3loops.htm
3399
                           https://www.iis.fraunhofer.de/content/dam/iis/de/doc/ame/conference/AES-116-Convention_guideline-to-audio-codec-delay_AES116.pdf */
3400
0
                        p_track->i_decoder_delay =  MP4_rescale(
3401
0
                                    576, i_rate,
3402
0
                                    p_track->i_timescale );
3403
0
                    }
3404
0
                    else if( p_fmt->i_codec == VLC_CODEC_MPGA ||
3405
0
                             p_fmt->i_codec == VLC_CODEC_MP2 )
3406
0
                    {
3407
0
                        p_track->i_decoder_delay =  MP4_rescale(
3408
0
                                    240, i_rate,
3409
0
                                    p_track->i_timescale );
3410
0
                    }
3411
0
                }
3412
0
                break;
3413
1
                default:
3414
1
                    break;
3415
1
            }
3416
1
            break;
3417
1
        }
3418
1
        default:
3419
0
            break;
3420
1
    }
3421
3422
1
    p_track->p_sample = p_sample;
3423
3424
1
    if( pp_es )
3425
1
        *pp_es = MP4_CreateES( p_demux->out, p_fmt );
3426
3427
1
    return ( !pp_es || *pp_es ) ? VLC_SUCCESS : VLC_EGENERIC;
3428
1
}
3429
3430
/* *** Try to find nearest sync points *** */
3431
static int TrackGetNearestSeekPoint( demux_t *p_demux, mp4_track_t *p_track,
3432
                                     uint32_t i_sample, uint32_t *pi_sync_sample )
3433
1
{
3434
1
    int i_ret = VLC_EGENERIC;
3435
1
    *pi_sync_sample = i_sample;
3436
3437
1
    const MP4_Box_t *p_stss;
3438
    /* try sync points without recovery roll */
3439
1
    if( ( p_stss = MP4_BoxGet( p_track->p_stbl, "stss" ) ) )
3440
0
    {
3441
        /* XXX in libmp4 sample begin at 0 */
3442
0
        const uint32_t stts_sample = i_sample + 1;
3443
0
        const MP4_Box_data_stss_t *p_stss_data = BOXDATA(p_stss);
3444
0
        msg_Dbg( p_demux, "track[Id 0x%x] using Sync Sample Box (stss)",
3445
0
                 p_track->i_track_ID );
3446
0
        for( unsigned i_index = 0; i_index < p_stss_data->i_entry_count; i_index++ )
3447
0
        {
3448
0
            if(( i_index == p_stss_data->i_entry_count - 1 ||
3449
0
                stts_sample < p_stss_data->i_sample_number[i_index+1] ) &&
3450
0
                p_stss_data->i_sample_number[i_index] != 0 )
3451
0
            {
3452
0
                *pi_sync_sample = p_stss_data->i_sample_number[i_index] - 1;
3453
0
                msg_Dbg( p_demux, "stss gives %d --> %" PRIu32 " (sample number)",
3454
0
                         i_sample, *pi_sync_sample );
3455
0
                i_ret = VLC_SUCCESS;
3456
0
                break;
3457
0
            }
3458
0
        }
3459
0
    }
3460
1
    else // no stss, any sample is sync point
3461
1
    {
3462
1
        return VLC_EGENERIC;
3463
1
    }
3464
3465
0
    if( i_ret == VLC_SUCCESS && *pi_sync_sample == i_sample )
3466
0
        return VLC_SUCCESS; // nothing to do
3467
3468
    /* Empty but present stss, try or refine using RAP with recovery roll info */
3469
0
    uint32_t i_alternative_sync_sample = i_sample;
3470
0
    if( MP4_SampleToGroupInfo( p_track->p_stbl, i_sample, SAMPLEGROUP_rap,
3471
0
                               0, &i_alternative_sync_sample, true, NULL ) )
3472
0
    {
3473
0
        msg_Dbg( p_demux, "tk %u sbgp gives %d --> %" PRIu32 " (sample number)",
3474
0
                 p_track->i_track_ID, i_sample, i_alternative_sync_sample );
3475
0
        if( i_alternative_sync_sample > *pi_sync_sample || i_ret != VLC_SUCCESS )
3476
0
        {
3477
0
            *pi_sync_sample = i_alternative_sync_sample;
3478
0
            return VLC_SUCCESS;
3479
0
        }
3480
0
    }
3481
3482
0
    return i_ret;
3483
0
}
3484
3485
struct cmplowestchunkdts_ctx
3486
{
3487
    uint64_t dts;
3488
    const mp4_chunk_t **pp_lowest;
3489
};
3490
3491
static int cmplowestchunkdts( const void *key, const void *other )
3492
0
{
3493
0
    const struct cmplowestchunkdts_ctx *ctx = key;
3494
0
    const mp4_chunk_t *ck = other;
3495
0
    if(ctx->dts > ck->i_first_dts)
3496
0
    {
3497
0
        *ctx->pp_lowest = ck;
3498
0
        return 1;
3499
0
    }
3500
0
    else if(ctx->dts < ck->i_first_dts)
3501
0
    {
3502
0
        return -1;
3503
0
    }
3504
0
    else
3505
0
    {
3506
0
        *ctx->pp_lowest = ck;
3507
0
        return 0;
3508
0
    }
3509
0
}
3510
3511
static int STTSToSampleChunk(const mp4_track_t *p_track, uint64_t i_dts,
3512
                             uint32_t *pi_chunk, uint32_t *pi_sample)
3513
1
{
3514
1
    const mp4_chunk_t *ck = NULL;
3515
3516
1
    if( p_track->i_chunk_count > 1 &&  /* start heuristic */
3517
1
        (uint64_t) i_dts < p_track->chunk[1].i_first_dts )
3518
1
    {
3519
1
        ck = &p_track->chunk[0];
3520
1
    }
3521
0
    else
3522
0
    {
3523
0
        const mp4_chunk_t *lowestchunk = NULL;
3524
0
        struct cmplowestchunkdts_ctx bsearchkeyctx = { .dts = i_dts,
3525
0
                                                       .pp_lowest = &lowestchunk };
3526
0
        ck = bsearch( &bsearchkeyctx, p_track->chunk, p_track->i_chunk_count,
3527
0
                      sizeof(*p_track->chunk), cmplowestchunkdts );
3528
0
        if( ck == NULL )
3529
0
            ck = lowestchunk;
3530
0
        if( unlikely(ck == NULL) )
3531
            /* if we default to last chunk, it will be checked searching i_sample */
3532
0
            ck = &p_track->chunk[p_track->i_chunk_count - 1];
3533
0
    }
3534
3535
    /* *** find sample in the chunk *** */
3536
1
    uint32_t i_sample = ck->i_sample_first;
3537
1
    uint64_t i_entrydts = ck->i_first_dts;
3538
3539
1
    for( uint_fast32_t i = 0;
3540
1
         i < ck->i_entries_dts && i_sample < ck->i_sample_count;
3541
1
         i++ )
3542
1
    {
3543
1
        uint64_t i_entry_duration = ck->p_sample_count_dts[i] * (uint64_t)
3544
1
                                    ck->p_sample_delta_dts[i];
3545
1
        if( i_entrydts + i_entry_duration < i_dts )
3546
0
        {
3547
0
            i_entrydts += i_entry_duration;
3548
0
            i_sample += ck->p_sample_count_dts[i];
3549
0
        }
3550
1
        else
3551
1
        {
3552
1
            if( ck->p_sample_delta_dts[i] > 0 )
3553
1
                i_sample += ( i_dts - i_entrydts ) / ck->p_sample_delta_dts[i];
3554
1
            break;
3555
1
        }
3556
1
    }
3557
3558
1
    *pi_chunk = ck - p_track->chunk;
3559
1
    *pi_sample = i_sample;
3560
3561
1
    if( i_sample >= p_track->i_sample_count )
3562
0
        return VLC_EGENERIC;
3563
3564
1
    return VLC_SUCCESS;
3565
1
}
3566
3567
/* given a time it return sample/chunk
3568
 * it also update elst field of the track
3569
 */
3570
static int TrackTimeToSampleChunk( demux_t *p_demux, mp4_track_t *p_track,
3571
                                   vlc_tick_t start, uint32_t *pi_chunk,
3572
                                   uint32_t *pi_sample )
3573
1
{
3574
1
    demux_sys_t *p_sys = p_demux->p_sys;
3575
1
    stime_t      i_start;
3576
3577
    /* FIXME see if it's needed to check p_track->i_chunk_count */
3578
1
    if( p_track->i_chunk_count == 0 )
3579
0
        return( VLC_EGENERIC );
3580
3581
    /* handle elst (find the correct one) */
3582
1
    MP4_TrackSetELST( p_demux, p_track, start );
3583
1
    if( p_track->p_elst && p_track->BOXDATA(p_elst)->i_entry_count > 0 )
3584
0
    {
3585
        /* now calculate i_start for this elst */
3586
        /* offset */
3587
0
        if( start < MP4_rescale_mtime( p_track->i_elst_time, p_sys->i_timescale ) )
3588
0
        {
3589
0
            *pi_chunk = 0;
3590
0
            *pi_sample= 0;
3591
3592
0
            return VLC_SUCCESS;
3593
0
        }
3594
        /* to track time scale */
3595
0
        i_start = MP4_rescale_qtime( start, p_track->i_timescale );
3596
        /* map through elst offset */
3597
0
        i_start = MP4_MapMediaTimelineToTrackTime( p_track, p_sys->i_timescale, i_start );
3598
3599
0
        msg_Dbg( p_demux, "elst (%d) gives %"PRId64"ms (movie)-> %"PRId64
3600
0
                 "ms (track)", p_track->i_elst,
3601
0
                 MS_FROM_VLC_TICK(start),
3602
0
                 MP4_rescale( i_start, p_track->i_timescale, 1000 ) );
3603
0
    }
3604
1
    else
3605
1
    {
3606
        /* convert absolute time to in timescale unit */
3607
1
        i_start = MP4_rescale_qtime( start, p_track->i_timescale );
3608
1
    }
3609
3610
    /* *** find good chunk *** */
3611
1
    uint32_t i_sample, i_chunk;
3612
1
    if( STTSToSampleChunk( p_track, i_start, &i_chunk, &i_sample ) != VLC_SUCCESS )
3613
0
    {
3614
0
        msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
3615
0
                  "(seeking too far) chunk=%"PRIu32" sample=%"PRIu32,
3616
0
                  p_track->i_track_ID, i_chunk, i_sample );
3617
0
        return( VLC_EGENERIC );
3618
0
    }
3619
3620
3621
    /* *** Try to find nearest sync points *** */
3622
1
    uint32_t i_sync_sample = i_sample;
3623
1
    if( VLC_SUCCESS ==
3624
1
        TrackGetNearestSeekPoint( p_demux, p_track, i_sample, &i_sync_sample ) )
3625
0
    {
3626
0
        msg_Dbg(p_demux,"track[Id 0x%x] sync point found sample %"PRIu32"(-%"PRIu32")",
3627
0
                p_track->i_track_ID, i_sync_sample, i_sample - i_sync_sample);
3628
0
    }
3629
1
    else
3630
1
    {
3631
1
        uint32_t i_group_sample_entry;
3632
1
        const MP4_Box_data_sgpd_entry_t *p_entrydesc;
3633
1
        if( MP4_SampleToGroupInfo( p_track->p_stbl, i_sample,
3634
1
                                  SAMPLEGROUP_roll, 0, &i_group_sample_entry,
3635
1
                                  SAMPLE_GROUP_MATCH_EXACT, &p_entrydesc ) )
3636
0
        {
3637
0
            msg_Dbg(p_demux, "track[Id 0x%x] preroll offset: %"PRId16" samples",
3638
0
                    p_track->i_track_ID, p_entrydesc->roll.i_roll_distance );
3639
0
            if( p_entrydesc->roll.i_roll_distance < 0 )
3640
0
            {
3641
0
                if( i_sync_sample > (uint32_t)-p_entrydesc->roll.i_roll_distance )
3642
0
                    i_sync_sample += p_entrydesc->roll.i_roll_distance;
3643
0
                else
3644
0
                    i_sync_sample = 0;
3645
0
            }
3646
0
        }
3647
1
        else if( p_track->i_decoder_delay > 0 &&
3648
0
                 p_track->i_decoder_delay <= i_start )
3649
0
        {
3650
0
            uint32_t i_withdelay_sample, i_withdelay_chunk;
3651
0
            if( STTSToSampleChunk( p_track, i_start - p_track->i_decoder_delay,
3652
0
                                   &i_withdelay_chunk,
3653
0
                                   &i_withdelay_sample ) == VLC_SUCCESS )
3654
0
            {
3655
0
                msg_Warn( p_demux, "track[Id 0x%x] has %" PRId64 "µs decoder delay from %" PRId64,
3656
0
                          p_track->i_track_ID,
3657
0
                          MP4_rescale_mtime( p_track->i_decoder_delay, p_track->i_timescale),
3658
0
                          i_start );
3659
0
                *pi_chunk  = i_withdelay_chunk;
3660
0
                *pi_sample = i_withdelay_sample;
3661
0
                return VLC_SUCCESS;
3662
0
            }
3663
0
        }
3664
1
    }
3665
3666
    /* Go to sync point chunk */
3667
1
    if( i_sync_sample <= i_sample )
3668
1
    {
3669
1
        while( i_chunk > 0 &&
3670
0
               i_sync_sample < p_track->chunk[i_chunk].i_sample_first )
3671
0
            i_chunk--;
3672
1
    }
3673
0
    else
3674
0
    {
3675
0
        while( i_chunk < p_track->i_chunk_count - 1 &&
3676
0
               i_sync_sample >= p_track->chunk[i_chunk].i_sample_first +
3677
0
                                p_track->chunk[i_chunk].i_sample_count )
3678
0
            i_chunk++;
3679
0
    }
3680
3681
1
    *pi_chunk  = i_chunk;
3682
1
    *pi_sample = i_sync_sample;
3683
3684
1
    return VLC_SUCCESS;
3685
1
}
3686
3687
static bool FormatIsCompatible( const es_format_t *p_fmt1, const es_format_t *p_fmt2 )
3688
0
{
3689
0
    if( p_fmt1->i_original_fourcc != p_fmt2->i_original_fourcc )
3690
0
        return false;
3691
0
    if( (p_fmt1->i_extra > 0) ^ (p_fmt2->i_extra > 0) )
3692
0
        return false;
3693
3694
0
    if( p_fmt1->i_codec == p_fmt2->i_codec &&
3695
0
        p_fmt1->i_extra && p_fmt2->i_extra &&
3696
0
        p_fmt1->i_extra == p_fmt2->i_extra )
3697
0
    {
3698
0
       return !!memcmp( p_fmt1->p_extra, p_fmt2->p_extra, p_fmt1->i_extra );
3699
0
    }
3700
3701
0
    if( p_fmt1->i_cat == AUDIO_ES )
3702
0
    {
3703
        /* Reject audio streams with different or unknown rates */
3704
0
        if( p_fmt1->audio.i_rate != p_fmt2->audio.i_rate || !p_fmt1->audio.i_rate )
3705
0
            return false;
3706
0
    }
3707
3708
0
    return es_format_IsSimilar( p_fmt1, p_fmt2 );
3709
0
}
3710
3711
static int TrackUpdateFormat( demux_t *p_demux, mp4_track_t *p_track,
3712
                              uint32_t i_previous_chunk )
3713
65
{
3714
65
    uint32_t i_chunk = p_track->i_chunk;
3715
    /* now see if actual es is ok */
3716
65
    if( i_previous_chunk != i_chunk &&
3717
0
        p_track->chunk[i_previous_chunk].i_sample_description_index !=
3718
0
        p_track->chunk[i_chunk].i_sample_description_index )
3719
0
    {
3720
0
        msg_Warn( p_demux, "recreate ES for track[Id 0x%x]",
3721
0
                  p_track->i_track_ID );
3722
3723
0
        const MP4_Box_t *p_newsample = MP4_BoxGetVa( p_track->p_stsd, "[%d]",
3724
0
                                                   p_track->chunk[i_chunk].i_sample_description_index - 1 );
3725
0
        if( p_newsample == NULL )
3726
0
        {
3727
0
            msg_Err( p_demux, "Can't change track[Id 0x%x] format for sample desc %" PRIu32,
3728
0
                     p_track->i_track_ID, p_track->chunk[i_chunk].i_sample_description_index );
3729
0
            p_track->b_ok = false;
3730
0
            p_track->b_selected = false;
3731
0
            return VLC_EGENERIC;
3732
0
        }
3733
3734
0
        track_config_t cfg;
3735
0
        TrackConfigInit( &cfg );
3736
0
        es_format_t tmpfmt;
3737
0
        es_format_Init( &tmpfmt, p_track->fmt.i_cat, 0 );
3738
0
        tmpfmt.i_id = p_track->i_track_ID;
3739
0
        if( TrackFillConfig( p_demux, p_track, p_newsample, i_chunk, &tmpfmt, &cfg ) == VLC_SUCCESS &&
3740
0
            !FormatIsCompatible( &p_track->fmt, &tmpfmt ) )
3741
0
        {
3742
0
            bool b_reselect = false;
3743
3744
0
            es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
3745
0
                            p_track->p_es, &b_reselect );
3746
3747
0
            es_out_Del( p_demux->out, p_track->p_es );
3748
3749
0
            p_track->p_es = MP4_CreateES( p_demux->out, &tmpfmt );
3750
0
            if( !p_track->p_es )
3751
0
            {
3752
0
                msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
3753
0
                         p_track->i_track_ID );
3754
3755
0
                p_track->b_ok       = false;
3756
0
                p_track->b_selected = false;
3757
0
                es_format_Clean( &tmpfmt );
3758
0
                return VLC_EGENERIC;
3759
0
            }
3760
3761
            /* select again the new decoder */
3762
0
            if( b_reselect )
3763
0
                es_out_Control( p_demux->out, ES_OUT_SET_ES, p_track->p_es );
3764
3765
0
            TrackConfigApply( &cfg, p_track );
3766
0
            es_format_Clean( &p_track->fmt );
3767
0
            es_format_Init( &p_track->fmt, tmpfmt.i_cat, tmpfmt.i_codec );
3768
0
            es_format_Copy( &p_track->fmt, &tmpfmt );
3769
0
            p_track->p_sample = p_newsample;
3770
0
        }
3771
0
        es_format_Clean( &tmpfmt );
3772
0
    }
3773
3774
65
    return VLC_SUCCESS;
3775
65
}
3776
3777
static int TrackGotoChunkSample( demux_t *p_demux, mp4_track_t *p_track,
3778
                                 uint32_t i_chunk, uint32_t i_sample )
3779
1
{
3780
1
    if( TrackUpdateFormat( p_demux, p_track, p_track->i_chunk ) != VLC_SUCCESS )
3781
0
        return VLC_EGENERIC;
3782
3783
1
    p_track->i_chunk    = i_chunk;
3784
1
    p_track->i_sample   = i_sample;
3785
3786
1
    return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
3787
1
}
3788
3789
static void TrackUpdateStarttimes( mp4_track_t *p_track )
3790
1
{
3791
1
    assert(p_track->i_chunk < p_track->i_chunk_count);
3792
3793
1
    p_track->i_start_dts = p_track->i_next_dts;
3794
1
    p_track->i_start_delta = p_track->i_next_delta;
3795
3796
    /* Probe the 16 first B frames */
3797
1
    uint32_t i_chunk = p_track->i_chunk;
3798
1
    if( !p_track->chunk[i_chunk].i_entries_pts )
3799
1
        return;
3800
3801
0
    stime_t lowest = p_track->i_start_dts;
3802
0
    if( p_track->i_start_delta != UNKNOWN_DELTA )
3803
0
        lowest += p_track->i_start_delta;
3804
3805
0
    for( uint32_t i=1; i<16; i++ )
3806
0
    {
3807
0
        uint32_t i_nextsample = p_track->i_sample + i;
3808
0
        if( i_nextsample >= p_track->i_sample_count )
3809
0
            break;
3810
0
        const mp4_chunk_t *ck = NULL;
3811
0
        for( ; i_chunk < p_track->i_chunk_count; i_chunk++ )
3812
0
        {
3813
0
            ck = &p_track->chunk[i_chunk];
3814
0
            if( i_nextsample < ck->i_sample_first + ck->i_sample_count )
3815
0
                break;
3816
0
        }
3817
0
        if( !ck )
3818
0
            break;
3819
0
        assert(i_nextsample >= ck->i_sample_first);
3820
0
        stime_t pts;
3821
0
        stime_t dts = pts = MP4_ChunkGetSampleDTS( ck, i_nextsample - ck->i_sample_first );
3822
0
        stime_t delta = UNKNOWN_DELTA;
3823
0
        if( MP4_ChunkGetSampleCTSDelta( ck, i_nextsample - ck->i_sample_first, &delta ) )
3824
0
            pts += delta;
3825
0
        if( pts < lowest )
3826
0
        {
3827
0
            p_track->i_start_dts = dts;
3828
0
            p_track->i_start_delta = delta;
3829
0
        }
3830
0
    }
3831
0
}
3832
3833
static void TrackUpdateSampleAndTimes( mp4_track_t *p_track )
3834
129
{
3835
129
    const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
3836
129
    uint32_t i_chunk_sample = p_track->i_sample - p_chunk->i_sample_first;
3837
129
    if( i_chunk_sample > p_chunk->i_sample_count && p_chunk->i_sample_count )
3838
0
        i_chunk_sample = p_chunk->i_sample_count - 1;
3839
129
    p_track->i_next_dts = MP4_ChunkGetSampleDTS( p_chunk, i_chunk_sample );
3840
129
    stime_t i_next_delta;
3841
129
    if( !MP4_ChunkGetSampleCTSDelta( p_chunk, i_chunk_sample, &i_next_delta ) )
3842
129
        p_track->i_next_delta = UNKNOWN_DELTA;
3843
0
    else
3844
0
        p_track->i_next_delta = i_next_delta;
3845
129
}
3846
3847
/****************************************************************************
3848
 * MP4_TrackSetup:
3849
 ****************************************************************************
3850
 * Parse track information and create all needed data to run a track
3851
 * If it succeed b_ok is set to 1 else to 0
3852
 ****************************************************************************/
3853
static void MP4_TrackSetup( demux_t *p_demux, mp4_track_t *p_track,
3854
                             const MP4_Box_t *p_box_trak,
3855
                             bool b_create_es, bool b_force_enable )
3856
1
{
3857
1
    demux_sys_t *p_sys = p_demux->p_sys;
3858
3859
1
    p_track->p_track = p_box_trak;
3860
3861
1
    char language[4] = { '\0' };
3862
1
    char sdp_media_type[8] = { '\0' };
3863
3864
1
    const MP4_Box_t *p_tkhd = MP4_BoxGet( p_box_trak, "tkhd" );
3865
1
    if( !p_tkhd )
3866
0
    {
3867
0
        return;
3868
0
    }
3869
3870
    /* do we launch this track by default ? */
3871
1
    p_track->b_enable =
3872
1
        ( ( BOXDATA(p_tkhd)->i_flags&MP4_TRACK_ENABLED ) != 0 );
3873
3874
1
    p_track->i_width = BOXDATA(p_tkhd)->i_width / BLOCK16x16;
3875
1
    p_track->i_height = BOXDATA(p_tkhd)->i_height / BLOCK16x16;
3876
1
    p_track->f_rotation = BOXDATA(p_tkhd)->f_rotation;
3877
1
    p_track->i_flip = BOXDATA(p_tkhd)->i_flip;
3878
3879
    /* FIXME: unhandled box: tref */
3880
3881
1
    const MP4_Box_t *p_mdhd = MP4_BoxGet( p_box_trak, "mdia/mdhd" );
3882
1
    const MP4_Box_t *p_hdlr = MP4_BoxGet( p_box_trak, "mdia/hdlr" );
3883
3884
1
    if( ( !p_mdhd )||( !p_hdlr ) )
3885
0
    {
3886
0
        return;
3887
0
    }
3888
3889
1
    if( BOXDATA(p_mdhd)->i_timescale == 0 )
3890
0
    {
3891
0
        msg_Warn( p_demux, "Invalid track timescale " );
3892
0
        return;
3893
0
    }
3894
1
    p_track->i_timescale = BOXDATA(p_mdhd)->i_timescale;
3895
3896
1
    memcpy( &language, BOXDATA(p_mdhd)->rgs_language, 3 );
3897
1
    p_track->b_mac_encoding = BOXDATA(p_mdhd)->b_mac_encoding;
3898
3899
1
    switch( p_hdlr->data.p_hdlr->i_handler_type )
3900
1
    {
3901
1
        case( ATOM_soun ):
3902
1
            if( !MP4_BoxGet( p_box_trak, "mdia/minf/smhd" ) )
3903
0
            {
3904
0
                return;
3905
0
            }
3906
1
            es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3907
1
            break;
3908
3909
0
        case( ATOM_pict ): /* heif */
3910
0
            es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3911
0
            break;
3912
3913
0
        case( ATOM_vide ):
3914
0
            if( !MP4_BoxGet( p_box_trak, "mdia/minf/vmhd") )
3915
0
            {
3916
0
                return;
3917
0
            }
3918
0
            es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3919
0
            break;
3920
3921
0
        case( ATOM_hint ):
3922
            /* RTP Reception Hint tracks */
3923
0
            if( !MP4_BoxGet( p_box_trak, "mdia/minf/hmhd" ) ||
3924
0
                !MP4_BoxGet( p_box_trak, "mdia/minf/stbl/stsd/rrtp" ) )
3925
0
            {
3926
0
                break;
3927
0
            }
3928
0
            MP4_Box_t *p_sdp;
3929
3930
            /* parse the sdp message to find out whether the RTP stream contained audio or video */
3931
0
            if( !( p_sdp  = MP4_BoxGet( p_box_trak, "udta/hnti/sdp " ) ) ||
3932
0
                !BOXDATA(p_sdp)->psz_text )
3933
0
            {
3934
0
                msg_Warn( p_demux, "Didn't find sdp box to determine stream type" );
3935
0
                return;
3936
0
            }
3937
3938
0
            strncpy( sdp_media_type, BOXDATA(p_sdp)->psz_text, 7 );
3939
0
            if( !strcmp(sdp_media_type, "m=audio") )
3940
0
            {
3941
0
                msg_Dbg( p_demux, "Found audio Rtp: %s", sdp_media_type );
3942
0
                es_format_Change( &p_track->fmt, AUDIO_ES, 0 );
3943
0
            }
3944
0
            else if( !strcmp(sdp_media_type, "m=video") )
3945
0
            {
3946
0
                msg_Dbg( p_demux, "Found video Rtp: %s", sdp_media_type );
3947
0
                es_format_Change( &p_track->fmt, VIDEO_ES, 0 );
3948
0
            }
3949
0
            else
3950
0
            {
3951
0
                msg_Warn( p_demux, "Malformed track SDP message: %s", sdp_media_type );
3952
0
                return;
3953
0
            }
3954
0
            break;
3955
3956
0
        case( ATOM_tx3g ):
3957
0
        case( ATOM_text ):
3958
0
        case( ATOM_subp ):
3959
0
        case( ATOM_subt ): /* ttml */
3960
0
        case( ATOM_sbtl ):
3961
0
        case( ATOM_clcp ): /* closed captions */
3962
0
            es_format_Change( &p_track->fmt, SPU_ES, 0 );
3963
0
            break;
3964
3965
0
        default:
3966
0
            return;
3967
1
    }
3968
3969
1
    p_track->asfinfo.i_cat = p_track->fmt.i_cat;
3970
3971
1
    const MP4_Box_t *p_elst;
3972
1
    p_track->i_elst = 0;
3973
1
    p_track->i_elst_time = 0;
3974
1
    p_track->p_elst = NULL;
3975
1
    if( ( p_elst = MP4_BoxGet( p_box_trak, "edts/elst" ) ) )
3976
1
    {
3977
1
        MP4_Box_data_elst_t *elst = BOXDATA(p_elst);
3978
1
        unsigned int i;
3979
3980
1
        msg_Warn( p_demux, "elst box found" );
3981
1
        bool use_editlist = var_InheritBool( p_demux, CFG_PREFIX"editlist" );
3982
1
        for( i = 0; i < elst->i_entry_count; i++ )
3983
0
        {
3984
0
            const MP4_Box_data_elst_entry_t *edit = &elst->entries[i];
3985
0
            if ( edit->i_segment_duration > INT64_MAX / CLOCK_FREQ ||
3986
0
                 ( edit->i_media_time >= 0 && edit->i_media_time > INT64_MAX / CLOCK_FREQ ) )
3987
0
            {
3988
0
                use_editlist = false;
3989
0
                msg_Dbg( p_demux, "   - [%d] bogus duration=%" PRId64 " media time=%" PRId64 ")",
3990
0
                         i, edit->i_segment_duration, edit->i_media_time );
3991
0
            }
3992
0
            else
3993
0
            msg_Dbg( p_demux, "   - [%d] duration=%"PRId64"ms media time=%"PRId64
3994
0
                     "ms) rate=%d.%d", i,
3995
0
                     MP4_rescale( edit->i_segment_duration, p_sys->i_timescale, 1000 ),
3996
0
                     edit->i_media_time >= 0 ?
3997
0
                        MP4_rescale( edit->i_media_time, p_track->i_timescale, 1000 ) :
3998
0
                        INT64_C(-1),
3999
0
                     edit->i_media_rate_integer,
4000
0
                     edit->i_media_rate_fraction );
4001
0
        }
4002
4003
1
        if( use_editlist )
4004
1
            p_track->p_elst = p_elst;
4005
0
        else
4006
1
            msg_Dbg( p_demux, "ignore editlist" );
4007
1
    }
4008
4009
/*  TODO
4010
    add support for:
4011
    p_dinf = MP4_BoxGet( p_minf, "dinf" );
4012
*/
4013
1
    if( !( p_track->p_stbl = MP4_BoxGet( p_box_trak,"mdia/minf/stbl" ) ) ||
4014
1
        !( p_track->p_stsd = MP4_BoxGet( p_box_trak,"mdia/minf/stbl/stsd") ) )
4015
0
    {
4016
0
        return;
4017
0
    }
4018
4019
    /* Set language */
4020
1
    if( *language && strcmp( language, "```" ) && strcmp( language, "und" ) )
4021
1
    {
4022
1
        p_track->fmt.psz_language = strdup( language );
4023
1
    }
4024
4025
1
    const MP4_Box_t *p_udta = MP4_BoxGet( p_box_trak, "udta" );
4026
1
    if( p_udta )
4027
0
    {
4028
0
        const MP4_Box_t *p_box_iter;
4029
0
        for( p_box_iter = p_udta->p_first; p_box_iter != NULL;
4030
0
                 p_box_iter = p_box_iter->p_next )
4031
0
        {
4032
0
            switch( p_box_iter->i_type )
4033
0
            {
4034
0
                case ATOM_0xa9nam:
4035
0
                case ATOM_name:
4036
0
                    p_track->fmt.psz_description =
4037
0
                        strndup( p_box_iter->data.p_binary->p_blob,
4038
0
                                 p_box_iter->data.p_binary->i_blob );
4039
0
                default:
4040
0
                    break;
4041
0
            }
4042
0
        }
4043
0
    }
4044
4045
    /* Create chunk index table and sample index table */
4046
1
    if( TrackCreateChunksIndex( p_demux,p_track  ) ||
4047
1
        TrackCreateSamplesIndex( p_demux, p_track ) )
4048
0
    {
4049
0
        msg_Err( p_demux, "cannot create chunks index" );
4050
0
        return; /* cannot create chunks index */
4051
0
    }
4052
4053
1
    p_track->i_next_dts = 0;
4054
1
    p_track->i_next_delta = UNKNOWN_DELTA;
4055
1
    p_track->i_chunk  = 0;
4056
1
    p_track->i_sample = 0;
4057
4058
    /* Disable chapter only track */
4059
1
    if( (p_track->i_use_flags & USEAS_CHAPTERS) && !p_sys->b_quicktime )
4060
0
        p_track->b_enable = false;
4061
4062
1
    const MP4_Box_t *p_tsel;
4063
    /* now create es */
4064
1
    if( b_force_enable &&
4065
1
        ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES ) )
4066
1
    {
4067
1
        msg_Warn( p_demux, "Enabling track[Id 0x%x] (buggy file without enabled track)",
4068
1
                  p_track->i_track_ID );
4069
1
        p_track->b_enable = true;
4070
1
        p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN;
4071
1
    }
4072
0
    else if ( (p_tsel = MP4_BoxGet( p_box_trak, "udta/tsel" )) )
4073
0
    {
4074
0
        if ( BOXDATA(p_tsel) && BOXDATA(p_tsel)->i_switch_group )
4075
0
        {
4076
0
            p_track->i_switch_group = BOXDATA(p_tsel)->i_switch_group;
4077
0
            int i_priority = ES_PRIORITY_SELECTABLE_MIN;
4078
0
            for ( unsigned int i = 0; i < p_sys->i_tracks; i++ )
4079
0
            {
4080
0
                const mp4_track_t *p_other = &p_sys->track[i];
4081
0
                if( p_other && p_other != p_track &&
4082
0
                    p_other->fmt.i_cat == p_track->fmt.i_cat &&
4083
0
                    p_track->i_switch_group == p_other->i_switch_group )
4084
0
                        i_priority = __MAX( i_priority, p_other->fmt.i_priority + 1 );
4085
0
            }
4086
            /* VLC only support ES priority for AUDIO_ES and SPU_ES.
4087
               If there's another VIDEO_ES in the same group, we need to unselect it then */
4088
0
            if ( p_track->fmt.i_cat == VIDEO_ES && i_priority > ES_PRIORITY_SELECTABLE_MIN )
4089
0
                p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
4090
0
            else
4091
0
                p_track->fmt.i_priority = i_priority;
4092
0
        }
4093
0
    }
4094
    /* If there's no tsel, try to enable the track coming first in edit list */
4095
0
    else if ( p_track->p_elst && p_track->fmt.i_priority == ES_PRIORITY_SELECTABLE_MIN )
4096
0
    {
4097
0
#define MAX_SELECTABLE (INT_MAX - ES_PRIORITY_SELECTABLE_MIN)
4098
0
        for ( uint32_t i=0; i<p_track->BOXDATA(p_elst)->i_entry_count; i++ )
4099
0
        {
4100
0
            const MP4_Box_data_elst_entry_t *edit = &p_track->BOXDATA(p_elst)->entries[i];
4101
0
            if ( edit->i_media_time >= 0 && edit->i_segment_duration )
4102
0
            {
4103
                /* We do selection by inverting start time into priority.
4104
                   The track with earliest edit will have the highest prio */
4105
0
                const int i_time = __MIN( MAX_SELECTABLE, edit->i_media_time );
4106
0
                p_track->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + MAX_SELECTABLE - i_time;
4107
0
                break;
4108
0
            }
4109
0
        }
4110
0
    }
4111
4112
1
    if( p_sys->hacks.es_cat_filter != UNKNOWN_ES &&
4113
0
        p_sys->hacks.es_cat_filter != p_track->fmt.i_cat )
4114
0
    {
4115
0
        p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
4116
0
    }
4117
4118
1
    if( !p_track->b_enable || (p_track->i_use_flags & USEAS_CHAPTERS) )
4119
0
        p_track->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
4120
4121
1
    if( !p_sys->b_quicktime || (p_track->i_use_flags & USEAS_CHAPTERS) == 0 )
4122
1
        b_create_es &= !MP4_isMetadata( p_track );
4123
4124
1
    if( TrackCreateES( p_demux,
4125
1
                       p_track, p_track->i_chunk,
4126
1
                       !b_create_es ? NULL : &p_track->p_es ) )
4127
0
    {
4128
0
        msg_Err( p_demux, "cannot create es for track[Id 0x%x]",
4129
0
                 p_track->i_track_ID );
4130
0
        return;
4131
0
    }
4132
4133
1
    p_track->b_ok = true;
4134
1
}
4135
4136
/****************************************************************************
4137
 * MP4_TrackClean:
4138
 ****************************************************************************
4139
 * Cleans a track created by MP4_TrackCreate.
4140
 ****************************************************************************/
4141
static void MP4_TrackClean( es_out_t *out, mp4_track_t *p_track )
4142
1
{
4143
1
    es_format_Clean( &p_track->fmt );
4144
4145
1
    if( p_track->p_es )
4146
1
        es_out_Del( out, p_track->p_es );
4147
4148
1
    if( p_track->chunk )
4149
1
    {
4150
28
        for( unsigned int i_chunk = 0; i_chunk < p_track->i_chunk_count; i_chunk++ )
4151
27
            MP4_ChunkDestroy( &p_track->chunk[i_chunk] );
4152
1
    }
4153
1
    free( p_track->chunk );
4154
4155
1
    if( !p_track->i_sample_size )
4156
0
        free( p_track->p_sample_size );
4157
4158
1
    ASFPacketTrackReset( &p_track->asfinfo );
4159
4160
1
    free( p_track->context.runs.p_array );
4161
1
}
4162
4163
static void MP4_TrackInit( mp4_track_t *p_track, const MP4_Box_t *p_trackbox )
4164
1
{
4165
1
    memset( p_track, 0, sizeof(mp4_track_t) );
4166
1
    es_format_Init( &p_track->fmt, UNKNOWN_ES, 0 );
4167
1
    p_track->i_timescale = 1;
4168
1
    p_track->p_track = p_trackbox;
4169
1
    const MP4_Box_t *p_tkhd = MP4_BoxGet( p_trackbox, "tkhd" );
4170
1
    if(likely(p_tkhd) && BOXDATA(p_tkhd))
4171
1
        p_track->i_track_ID = BOXDATA(p_tkhd)->i_track_ID;
4172
1
    ASFPacketTrackInit( &p_track->asfinfo );
4173
1
}
4174
4175
static void MP4_TrackSelect( demux_t *p_demux, mp4_track_t *p_track, bool b_select )
4176
1
{
4177
1
    if( !p_track->b_ok || MP4_isMetadata(p_track) )
4178
0
        return;
4179
4180
1
    if( b_select == p_track->b_selected )
4181
0
        return;
4182
4183
1
    if( !b_select && p_track->p_es )
4184
1
    {
4185
1
        es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE,
4186
1
                        p_track->p_es, false );
4187
1
    }
4188
4189
1
    p_track->b_selected = b_select;
4190
1
}
4191
4192
static int MP4_TrackSeek( demux_t *p_demux, mp4_track_t *p_track,
4193
                          vlc_tick_t i_start )
4194
1
{
4195
1
    uint32_t i_chunk;
4196
1
    uint32_t i_sample;
4197
4198
1
    if( !p_track->b_ok || MP4_isMetadata( p_track ) )
4199
0
        return VLC_EGENERIC;
4200
4201
1
    p_track->b_selected = false;
4202
4203
1
    if( TrackTimeToSampleChunk( p_demux, p_track, i_start,
4204
1
                                &i_chunk, &i_sample ) )
4205
0
    {
4206
0
        msg_Warn( p_demux, "cannot select track[Id 0x%x]",
4207
0
                  p_track->i_track_ID );
4208
0
        return VLC_EGENERIC;
4209
0
    }
4210
4211
1
    p_track->b_selected = true;
4212
1
    if( !TrackGotoChunkSample( p_demux, p_track, i_chunk, i_sample ) )
4213
1
    {
4214
1
        p_track->b_selected = true;
4215
1
        TrackUpdateSampleAndTimes( p_track );
4216
1
        TrackUpdateStarttimes( p_track );
4217
1
    }
4218
4219
1
    return p_track->b_selected ? VLC_SUCCESS : VLC_EGENERIC;
4220
1
}
4221
4222
4223
/*
4224
 * 3 types: for audio
4225
 *
4226
 */
4227
//#define MP4_DEBUG_AUDIO_SAMPLES
4228
static inline uint32_t MP4_GetAudioFrameInfo( const mp4_track_t *p_track,
4229
                                              const MP4_Box_data_sample_soun_t *p_soun,
4230
                                              uint32_t *pi_size )
4231
65
{
4232
65
    uint32_t i_samples_per_frame = p_soun->i_sample_per_packet;
4233
65
    uint32_t i_bytes_per_frame = p_soun->i_bytes_per_frame;
4234
4235
65
    switch( p_track->fmt.i_codec )
4236
65
    {
4237
        /* Quicktime builtin support, _must_ ignore sample tables */
4238
0
        case VLC_CODEC_GSM:
4239
0
            i_samples_per_frame = 160;
4240
0
            i_bytes_per_frame = 33;
4241
0
            break;
4242
0
        case VLC_CODEC_ADPCM_IMA_QT:
4243
0
            i_samples_per_frame = 64;
4244
0
            i_bytes_per_frame = 34 * p_soun->i_channelcount;
4245
0
            break;
4246
0
        case VLC_CODEC_MACE3:
4247
0
            i_samples_per_frame = 6;
4248
0
            i_bytes_per_frame = 2 * p_soun->i_channelcount;
4249
0
            break;
4250
0
        case VLC_CODEC_MACE6:
4251
0
            i_samples_per_frame = 6;
4252
0
            i_bytes_per_frame = 1 * p_soun->i_channelcount;
4253
0
            break;
4254
0
        case VLC_FOURCC( 'u', 'l', 'a', 'w' ):
4255
0
            i_samples_per_frame = 1;
4256
0
            i_bytes_per_frame = p_soun->i_channelcount;
4257
0
            break;
4258
0
        case VLC_CODEC_ALAW:
4259
0
        case VLC_CODEC_U8:
4260
0
        case VLC_CODEC_S8:
4261
0
        case VLC_CODEC_S16L:
4262
0
        case VLC_CODEC_S16B:
4263
0
        case VLC_CODEC_S24L:
4264
0
        case VLC_CODEC_S24B:
4265
0
        case VLC_CODEC_S32L:
4266
0
        case VLC_CODEC_S32B:
4267
0
        case VLC_CODEC_F32L:
4268
0
        case VLC_CODEC_F32B:
4269
0
        case VLC_CODEC_F64L:
4270
0
        case VLC_CODEC_F64B:
4271
0
            i_samples_per_frame = 1;
4272
0
            i_bytes_per_frame = (aout_BitsPerSample( p_track->fmt.i_codec ) *
4273
0
                                 p_soun->i_channelcount) >> 3;
4274
0
            assert(i_bytes_per_frame);
4275
0
            break;
4276
0
        case VLC_FOURCC( 'N', 'O', 'N', 'E' ):
4277
0
        case ATOM_twos:
4278
0
        case ATOM_sowt:
4279
0
        case ATOM_raw:
4280
0
            if( p_soun->i_sample_per_packet && p_soun->i_bytes_per_frame )
4281
0
            {
4282
0
                i_samples_per_frame = p_soun->i_sample_per_packet;
4283
0
                i_bytes_per_frame = p_soun->i_bytes_per_frame;
4284
0
            }
4285
0
            else
4286
0
            {
4287
0
                i_samples_per_frame = 1;
4288
0
                i_bytes_per_frame = (p_soun->i_samplesize+7) >> 3;
4289
0
                i_bytes_per_frame *= p_soun->i_channelcount;
4290
0
            }
4291
0
            break;
4292
65
        default:
4293
65
            break;
4294
65
    }
4295
4296
    /* Group audio packets so we don't call demux for single sample unit */
4297
65
    if( p_track->fmt.i_original_fourcc == VLC_CODEC_DVD_LPCM &&
4298
0
        p_soun->i_constLPCMframesperaudiopacket &&
4299
0
        p_soun->i_constbytesperaudiopacket )
4300
0
    {
4301
0
        i_samples_per_frame = p_soun->i_constLPCMframesperaudiopacket;
4302
0
        i_bytes_per_frame = p_soun->i_constbytesperaudiopacket;
4303
0
    }
4304
4305
    /* handle v0 or v1 MPGA having bytesperssample and samplesize and
4306
     * compression being 0.
4307
     * ends up reading samplesperframe instead of the number of
4308
     * stored samples units */
4309
65
    if( p_soun->i_compressionid == 0 && p_soun->i_qt_version <= 1 &&
4310
0
        p_track->i_sample_size > 0 &&
4311
0
        (
4312
0
            p_track->fmt.i_codec == VLC_CODEC_MP3 ||
4313
0
            p_track->fmt.i_codec == VLC_CODEC_MP2 ||
4314
0
            p_track->fmt.i_codec == VLC_CODEC_MPGA
4315
0
        ) )
4316
0
    {
4317
0
        i_bytes_per_frame = 0;
4318
0
    }
4319
4320
#ifdef MP4_DEBUG_AUDIO_SAMPLES
4321
    printf("qtv%d comp%x ss %d chan %d ba %d spp %d bpf %d / %d %d\n",
4322
           p_soun->i_qt_version, p_soun->i_compressionid,
4323
           p_soun->i_samplesize, p_soun->i_channelcount,
4324
           p_track->fmt.audio.i_blockalign,
4325
           p_soun->i_sample_per_packet, p_soun->i_bytes_per_frame,
4326
           i_samples_per_frame, i_bytes_per_frame);
4327
#endif
4328
4329
65
    *pi_size = i_bytes_per_frame;
4330
65
    return i_samples_per_frame;
4331
65
}
4332
4333
static uint32_t MP4_TrackGetReadSize( mp4_track_t *p_track, uint32_t *pi_nb_samples )
4334
65
{
4335
65
    uint32_t i_size = 0;
4336
65
    *pi_nb_samples = 0;
4337
4338
65
    if ( p_track->i_sample == p_track->i_sample_count )
4339
0
        return 0;
4340
4341
65
    if ( p_track->fmt.i_cat != AUDIO_ES )
4342
0
    {
4343
0
        *pi_nb_samples = 1;
4344
4345
0
        if( p_track->i_sample_size == 0 ) /* all sizes are different */
4346
0
            return p_track->p_sample_size[p_track->i_sample];
4347
0
        else
4348
0
            return p_track->i_sample_size;
4349
0
    }
4350
65
    else
4351
65
    {
4352
65
        const MP4_Box_data_sample_soun_t *p_soun = p_track->p_sample->data.p_sample_soun;
4353
65
        const mp4_chunk_t *p_chunk = &p_track->chunk[p_track->i_chunk];
4354
4355
        /* v0:
4356
         * - isobmff: codec is not using v1 fields
4357
         * - qt: codec is usually uncompressed and 1 stored sample == 1 byte
4358
         * and you need to packetize/group samples. hardcoded codecs.
4359
         * if compressed, samplesize == 16 and codec is also hardcoded (ex: adpcm)
4360
         * v1:
4361
         * - samplesize is meaningless, always 16
4362
         * - compressed frames/packet size parameters applies, but samplesize can
4363
         * still be bytes. use packet size to deinterleave, but samples per frames
4364
         * are not the number of stored samples. The chunk can also be a packet in some
4365
         *  (qcelp) cases, in that case we need to return chunk (return 1 stored sample).
4366
         * If the codec has -2 compression, this is vbr, we can't deinterleave or group
4367
         * samples at all. (return 1 stored sample)
4368
         * Again, some hardcoded codecs can exist in qt and we can't trust parameters.
4369
         * v2:
4370
         * - lpcm extensions provides additional parameters to group samples, but
4371
         * 1 stored sample is usually too small in length/bytes, so we need to
4372
         * packetize group them. Again, 1 stored sample != 1 audio sample. */
4373
4374
65
        if( p_track->fmt.i_original_fourcc == VLC_FOURCC('r','r','t','p') )
4375
0
        {
4376
0
            *pi_nb_samples = 1;
4377
0
            return p_track->i_sample_size;
4378
0
        }
4379
4380
        /* all samples have a different size */
4381
65
        if( p_track->i_sample_size == 0 )
4382
0
        {
4383
0
            *pi_nb_samples = 1;
4384
0
            return p_track->p_sample_size[p_track->i_sample];
4385
0
        }
4386
4387
        /* If we are compressed but not v2 LPCM frames extensions */
4388
65
        if ( p_soun->i_compressionid == 0xFFFE && p_soun->i_qt_version < 2 )
4389
0
        {
4390
0
            *pi_nb_samples = 1; /* != number of audio samples */
4391
0
            if ( p_track->i_sample_size )
4392
0
                return p_track->i_sample_size;
4393
0
            else
4394
0
                return p_track->p_sample_size[p_track->i_sample];
4395
0
        }
4396
4397
        /* More regular V0 cases */
4398
65
        uint32_t i_max_v0_samples;
4399
65
        switch( p_track->fmt.i_codec )
4400
65
        {
4401
            /* Compressed samples in V0 */
4402
0
            case VLC_CODEC_AMR_NB:
4403
0
            case VLC_CODEC_AMR_WB:
4404
0
                i_max_v0_samples = 16;
4405
0
                break;
4406
65
            default:
4407
65
                if( aout_BitsPerSample(p_track->fmt.i_codec) )
4408
0
                {
4409
                    /* Read 25ms of samples (uncompressed) */
4410
0
                    i_max_v0_samples = p_track->fmt.audio.i_rate / 40 *
4411
0
                                       p_track->fmt.audio.i_channels;
4412
0
                    if( i_max_v0_samples < 1 )
4413
0
                        i_max_v0_samples = 1;
4414
0
                }
4415
65
                else
4416
65
                {
4417
                    /* compressed codecs */
4418
65
                    i_max_v0_samples = 1;
4419
65
                }
4420
65
                break;
4421
65
        }
4422
4423
65
        *pi_nb_samples = 0;
4424
4425
65
        if( p_track->i_sample_size > 0 )
4426
65
        {
4427
65
            uint32_t i_bytes_per_frame;
4428
65
            uint32_t i_samples_per_frame =
4429
65
                    MP4_GetAudioFrameInfo( p_track, p_soun, &i_bytes_per_frame );
4430
65
            uint32_t i_chunk_remaining_samples = p_chunk->i_sample_count -
4431
65
                    (p_track->i_sample - p_chunk->i_sample_first);
4432
4433
65
            if( i_max_v0_samples > i_chunk_remaining_samples )
4434
0
                i_max_v0_samples = i_chunk_remaining_samples;
4435
4436
65
            if( i_samples_per_frame && i_bytes_per_frame )
4437
0
            {
4438
                /* GSM, ADPCM,  */
4439
0
                if( i_samples_per_frame > 64 &&
4440
0
                    i_samples_per_frame > i_bytes_per_frame )
4441
0
                {
4442
0
                    *pi_nb_samples = i_samples_per_frame;
4443
0
                    i_size = i_bytes_per_frame;
4444
0
                }
4445
0
                else
4446
0
                {
4447
                    /* Regular cases */
4448
0
                    uint32_t i_frames = __MAX(i_max_v0_samples / i_samples_per_frame, 1);
4449
0
                    *pi_nb_samples = i_frames * i_samples_per_frame;
4450
0
                    i_size = i_frames * i_bytes_per_frame;
4451
0
                }
4452
0
            }
4453
65
            else
4454
65
            {
4455
65
                *pi_nb_samples = 1;
4456
65
                return p_track->i_sample_size;
4457
65
            }
4458
#ifdef MP4_DEBUG_AUDIO_SAMPLES
4459
            printf("demuxing qtv%d comp %x, ss%d, spf %d bpf %d samples %d maxsamples %d remain samples %d\n",
4460
                    p_soun->i_qt_version, p_soun->i_compressionid, p_track->i_sample_size,
4461
                    i_samples_per_frame, i_bytes_per_frame,
4462
                    *pi_nb_samples, i_max_v0_samples, i_chunk_remaining_samples);
4463
#endif
4464
65
        }
4465
0
        else /* Compressed */
4466
0
        {
4467
0
            for( uint32_t i=p_track->i_sample;
4468
0
                 i<p_chunk->i_sample_first+p_chunk->i_sample_count &&
4469
0
                 i<p_track->i_sample_count;
4470
0
                 i++ )
4471
0
            {
4472
0
                i_size += p_track->p_sample_size[i];
4473
0
                (*pi_nb_samples)++;
4474
4475
                /* Try to detect compression in ISO */
4476
0
                if(p_soun->i_compressionid != 0)
4477
0
                {
4478
                    /* Return only 1 sample */
4479
0
                    break;
4480
0
                }
4481
4482
0
                if ( *pi_nb_samples == i_max_v0_samples )
4483
0
                    break;
4484
0
            }
4485
0
        }
4486
65
    }
4487
4488
0
    return i_size;
4489
65
}
4490
4491
static uint64_t MP4_TrackGetPos( mp4_track_t *p_track )
4492
129
{
4493
129
    unsigned int i_sample;
4494
129
    uint64_t i_pos;
4495
4496
129
    i_pos = p_track->chunk[p_track->i_chunk].i_offset;
4497
4498
129
    if( p_track->i_sample_size )
4499
129
    {
4500
        /* chunk offset in samples */
4501
129
        uint32_t i_samples = p_track->i_sample -
4502
129
                             p_track->chunk[p_track->i_chunk].i_sample_first;
4503
4504
129
        if( p_track->fmt.i_cat == AUDIO_ES )
4505
129
        {
4506
129
            MP4_Box_data_sample_soun_t *p_soun =
4507
129
                p_track->p_sample->data.p_sample_soun;
4508
4509
129
            if( p_soun->i_compressionid != 0xFFFE )
4510
0
            {
4511
0
                uint32_t i_bytes_per_frame;
4512
0
                uint32_t i_samples_per_frame = MP4_GetAudioFrameInfo( p_track, p_soun, &i_bytes_per_frame );
4513
0
                if( i_bytes_per_frame && i_samples_per_frame )
4514
0
                {
4515
                    /* we read chunk by chunk unless a blockalign is requested */
4516
0
                    return i_pos + i_samples /
4517
0
                                   i_samples_per_frame * (uint64_t) i_bytes_per_frame;
4518
0
                }
4519
0
            }
4520
129
        }
4521
4522
129
        i_pos += i_samples * (uint64_t) p_track->i_sample_size;
4523
129
    }
4524
0
    else
4525
0
    {
4526
0
        for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
4527
0
             i_sample < p_track->i_sample; i_sample++ )
4528
0
        {
4529
0
            i_pos += p_track->p_sample_size[i_sample];
4530
0
        }
4531
0
    }
4532
4533
129
    return i_pos;
4534
129
}
4535
4536
4537
static int MP4_TrackSetNextELST( mp4_track_t *tk )
4538
0
{
4539
0
    if( !tk->p_elst ||
4540
0
         tk->i_elst == UINT32_MAX - 1 ||
4541
0
         tk->i_elst + 1 >= tk->BOXDATA(p_elst)->i_entry_count )
4542
0
        return VLC_EGENERIC;
4543
4544
0
    tk->i_elst_time += tk->BOXDATA(p_elst)->entries[tk->i_elst++].i_segment_duration;
4545
4546
0
    return VLC_SUCCESS;
4547
0
}
4548
4549
static int MP4_TrackUpdateELST( mp4_track_t *tk, uint32_t i_movie_timescale,
4550
                                stime_t i_track_time, uint32_t i_track_sample )
4551
64
{
4552
64
    if( !tk->p_elst || tk->BOXDATA(p_elst)->i_entry_count == 0 )
4553
64
        return VLC_SUCCESS;
4554
4555
0
    if( i_track_sample >= tk->i_sample_count )
4556
0
        return MP4_TrackSetNextELST( tk );
4557
4558
0
    vlc_tick_t i_time = MP4_rescale_mtime( i_track_time, tk->i_timescale );
4559
0
    for(;;)
4560
0
    {
4561
0
        const MP4_Box_data_elst_entry_t *edit = &tk->BOXDATA(p_elst)->entries[tk->i_elst];
4562
0
        vlc_tick_t i_end = MP4_rescale_mtime( edit->i_media_time, tk->i_timescale ) +
4563
0
                           MP4_rescale_mtime( edit->i_segment_duration, i_movie_timescale );
4564
0
        if ( i_time < i_end || MP4_TrackSetNextELST( tk ) )
4565
0
            break;
4566
0
    }
4567
4568
0
    return VLC_SUCCESS;
4569
0
}
4570
4571
static int MP4_TrackNextSample( demux_t *p_demux, mp4_track_t *p_track, uint32_t i_samples )
4572
64
{
4573
64
    demux_sys_t *p_sys = p_demux->p_sys;
4574
4575
64
    if ( UINT32_MAX - p_track->i_sample < i_samples )
4576
0
        i_samples = UINT32_MAX - p_track->i_sample; /* Ensure we won't overflow next add */
4577
4578
64
    p_track->i_sample += i_samples;
4579
64
    const uint32_t i_previous_chunk = p_track->i_chunk;
4580
4581
64
    if( p_track->i_sample < p_track->i_sample_count )
4582
64
    {
4583
        /* Have we changed chunk ? */
4584
64
        while( p_track->i_sample >=
4585
64
               p_track->chunk[p_track->i_chunk].i_sample_first +
4586
64
               p_track->chunk[p_track->i_chunk].i_sample_count &&
4587
0
               p_track->i_chunk < p_track->i_chunk_count - 1 )
4588
0
        {
4589
0
            p_track->i_chunk++;
4590
0
        }
4591
64
    }
4592
4593
64
    if( p_track->p_elst ) /* Update */
4594
64
    {
4595
64
        uint32_t i_prev_elst = p_track->i_elst;
4596
64
        TrackUpdateSampleAndTimes( p_track );
4597
64
        MP4_TrackUpdateELST( p_track, p_sys->i_timescale, p_track->i_next_dts, p_track->i_sample);
4598
64
        if( p_track->i_elst != i_prev_elst )
4599
0
        {
4600
0
            msg_Dbg( p_demux, "changed elst %u -> %u", i_prev_elst, p_track->i_elst);
4601
            /* *** find good chunk *** */
4602
0
            uint32_t i_sample, i_chunk;
4603
0
            stime_t i_start = p_track->BOXDATA(p_elst)->entries[p_track->i_elst].i_media_time;
4604
0
            if( STTSToSampleChunk( p_track, i_start, &i_chunk, &i_sample ) != VLC_SUCCESS )
4605
0
            {
4606
0
                msg_Warn( p_demux, "track[Id 0x%x] will be disabled "
4607
0
                          "(seeking too far) chunk=%"PRIu32" sample=%"PRIu32,
4608
0
                          p_track->i_track_ID, i_chunk, i_sample );
4609
0
                return VLC_EGENERIC;
4610
0
            }
4611
0
            p_track->i_chunk = i_chunk;
4612
0
            p_track->i_sample = i_sample;
4613
0
        }
4614
64
    }
4615
4616
64
    TrackUpdateSampleAndTimes( p_track );
4617
4618
64
    if( TrackUpdateFormat( p_demux, p_track, i_previous_chunk ) != VLC_SUCCESS )
4619
0
    {
4620
0
        msg_Warn( p_demux, "track[0x%x] will be disabled "
4621
0
                  "(cannot restart decoder)", p_track->i_track_ID );
4622
0
        MP4_TrackSelect( p_demux, p_track, false );
4623
0
        return VLC_EGENERIC;
4624
0
    }
4625
4626
64
    if( !p_track->b_selected || p_track->i_sample >= p_track->i_sample_count )
4627
0
        return VLC_EGENERIC;
4628
4629
64
    return VLC_SUCCESS;
4630
64
}
4631
4632
static void MP4_TrackSetELST( demux_t *p_demux, mp4_track_t *tk,
4633
                              vlc_tick_t i_time )
4634
1
{
4635
1
    demux_sys_t *p_sys = p_demux->p_sys;
4636
1
    uint32_t i_elst_last = tk->i_elst;
4637
4638
    /* handle elst (find the correct one) */
4639
1
    tk->i_elst      = 0;
4640
1
    tk->i_elst_time = 0;
4641
1
    if( tk->p_elst && tk->BOXDATA(p_elst)->i_entry_count > 0 )
4642
0
    {
4643
0
        MP4_Box_data_elst_t *elst = tk->BOXDATA(p_elst);
4644
0
        int64_t i_mvt= MP4_rescale_qtime( i_time, p_sys->i_timescale );
4645
4646
0
        for( tk->i_elst = 0; tk->i_elst < elst->i_entry_count; tk->i_elst++ )
4647
0
        {
4648
0
            uint64_t i_dur = elst->entries[tk->i_elst].i_segment_duration;
4649
4650
0
            if( elst->entries[tk->i_elst].i_media_time >=0 &&
4651
0
                tk->i_elst_time <= i_mvt &&
4652
0
                i_mvt < (int64_t)(tk->i_elst_time + i_dur) )
4653
0
            {
4654
0
                break;
4655
0
            }
4656
0
            tk->i_elst_time += i_dur;
4657
0
        }
4658
4659
0
        if( tk->i_elst >= elst->i_entry_count )
4660
0
        {
4661
            /* msg_Dbg( p_demux, "invalid number of entry in elst" ); */
4662
0
            tk->i_elst = elst->i_entry_count - 1;
4663
0
            tk->i_elst_time -= elst->entries[tk->i_elst].i_segment_duration;
4664
0
        }
4665
4666
0
        if( i_elst_last != tk->i_elst )
4667
0
        {
4668
0
            msg_Warn( p_demux, "elst old=%d new=%"PRIu32, i_elst_last, tk->i_elst );
4669
0
            if( i_elst_last < elst->i_entry_count &&
4670
0
                elst->entries[i_elst_last].i_media_time >= 0 )
4671
0
                tk->i_next_block_flags |= BLOCK_FLAG_DISCONTINUITY;
4672
0
        }
4673
0
    }
4674
1
}
4675
4676
/******************************************************************************
4677
 *     Here are the functions used for fragmented MP4
4678
 *****************************************************************************/
4679
#if 0
4680
/**
4681
 * Re-init decoder.
4682
 * \Note If we call that function too soon,
4683
 * before the track has been selected by MP4_TrackSelect
4684
 * (during the first execution of Demux), then the track gets disabled
4685
 */
4686
static int ReInitDecoder( demux_t *p_demux, const MP4_Box_t *p_root,
4687
                          mp4_track_t *p_track )
4688
{
4689
    MP4_Box_t *p_paramsbox = MP4_BoxGet( p_root, "/moov/trak[0]" );
4690
    if( !p_paramsbox )
4691
        return VLC_EGENERIC;
4692
4693
    MP4_TrackRestart( p_demux, p_track, p_paramsbox );
4694
4695
    /* Temporary hack until we support track selection */
4696
    p_track->b_selected = true;
4697
    p_track->b_enable = true;
4698
4699
    return VLC_SUCCESS;
4700
}
4701
#endif
4702
4703
static stime_t GetCumulatedDuration( demux_t *p_demux )
4704
0
{
4705
0
    demux_sys_t *p_sys = p_demux->p_sys;
4706
0
    stime_t i_max_duration = 0;
4707
4708
0
    for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
4709
0
    {
4710
0
        MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, p_sys->track[i].i_track_ID );
4711
0
        const MP4_Box_t *p_stsz;
4712
0
        const MP4_Box_t *p_tkhd;
4713
0
        if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
4714
0
             (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
4715
             /* duration might be wrong an be set to whole duration :/ */
4716
0
             BOXDATA(p_stsz)->i_sample_count > 0 )
4717
0
        {
4718
0
            i_max_duration = __MAX( (uint64_t)i_max_duration, BOXDATA(p_tkhd)->i_duration );
4719
0
        }
4720
0
    }
4721
0
    if( p_sys->p_fragsindex )
4722
0
    {
4723
0
        stime_t i_tracks_duration = MP4_Fragment_Index_GetTracksDuration( p_sys->p_fragsindex );
4724
0
        i_max_duration = __MAX( i_max_duration, i_tracks_duration );
4725
0
    }
4726
4727
0
    return i_max_duration;
4728
0
}
4729
4730
static int ProbeIndex( demux_t *p_demux )
4731
0
{
4732
0
    demux_sys_t *p_sys = p_demux->p_sys;
4733
0
    uint64_t i_stream_size;
4734
0
    uint8_t mfro[MP4_MFRO_BOXSIZE];
4735
0
    assert( p_sys->b_seekable );
4736
4737
0
    if ( MP4_BoxCount( p_sys->p_root, "/mfra" ) )
4738
0
        return VLC_EGENERIC;
4739
4740
0
    if ( vlc_stream_GetSize( p_demux->s, &i_stream_size ) != VLC_SUCCESS )
4741
0
        return VLC_EGENERIC;
4742
4743
0
    if ( i_stream_size < MP4_MFRO_BOXSIZE ||
4744
0
         vlc_stream_Seek( p_demux->s, i_stream_size - MP4_MFRO_BOXSIZE ) != VLC_SUCCESS )
4745
0
    {
4746
0
        msg_Dbg( p_demux, "Probing tail for mfro has failed" );
4747
0
        return VLC_EGENERIC;
4748
0
    }
4749
4750
0
    if ( vlc_stream_Read( p_demux->s, &mfro, MP4_MFRO_BOXSIZE ) == MP4_MFRO_BOXSIZE &&
4751
0
         VLC_FOURCC(mfro[4],mfro[5],mfro[6],mfro[7]) == ATOM_mfro &&
4752
0
         GetDWBE( &mfro ) == MP4_MFRO_BOXSIZE )
4753
0
    {
4754
0
        uint32_t i_offset = GetDWBE( &mfro[12] );
4755
0
        msg_Dbg( p_demux, "will read mfra index at %"PRIu64, i_stream_size - i_offset );
4756
0
        if ( i_stream_size > i_offset &&
4757
0
             vlc_stream_Seek( p_demux->s, i_stream_size - i_offset ) == VLC_SUCCESS )
4758
0
        {
4759
0
            msg_Dbg( p_demux, "reading mfra index at %"PRIu64, i_stream_size - i_offset );
4760
0
            const uint32_t stoplist[] = { ATOM_mfra, 0 };
4761
0
            MP4_ReadBoxContainerChildren( p_demux->s, p_sys->p_root, stoplist );
4762
0
        }
4763
0
        return VLC_SUCCESS;
4764
0
    }
4765
0
    return VLC_EGENERIC;
4766
0
}
4767
4768
static stime_t GetMoovTrackDuration( demux_sys_t *p_sys, unsigned i_track_ID )
4769
0
{
4770
0
    MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_sys->p_moov, i_track_ID );
4771
0
    const MP4_Box_t *p_stsz;
4772
0
    const MP4_Box_t *p_tkhd;
4773
0
    if ( (p_tkhd = MP4_BoxGet( p_trak, "tkhd" )) &&
4774
0
         (p_stsz = MP4_BoxGet( p_trak, "mdia/minf/stbl/stsz" )) &&
4775
         /* duration might be wrong an be set to whole duration :/ */
4776
0
         BOXDATA(p_stsz)->i_sample_count > 0 )
4777
0
    {
4778
0
        if( BOXDATA(p_tkhd)->i_duration <= p_sys->i_moov_duration )
4779
0
            return BOXDATA(p_tkhd)->i_duration; /* In movie / mvhd scale */
4780
0
        else
4781
0
            return p_sys->i_moov_duration;
4782
0
    }
4783
0
    return 0;
4784
0
}
4785
4786
static bool GetMoofTrackDuration( MP4_Box_t *p_moov, MP4_Box_t *p_moof,
4787
                                  unsigned i_track_ID, stime_t *p_duration )
4788
0
{
4789
0
    if ( !p_moof || !p_moov )
4790
0
        return false;
4791
4792
0
    MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
4793
0
    while ( p_traf )
4794
0
    {
4795
0
        if ( p_traf->i_type != ATOM_traf )
4796
0
        {
4797
0
           p_traf = p_traf->p_next;
4798
0
           continue;
4799
0
        }
4800
4801
0
        const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
4802
0
        const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
4803
0
        if ( !p_tfhd || !p_trun || i_track_ID != BOXDATA(p_tfhd)->i_track_ID )
4804
0
        {
4805
0
           p_traf = p_traf->p_next;
4806
0
           continue;
4807
0
        }
4808
4809
0
        uint32_t i_track_timescale = 0;
4810
0
        uint32_t i_track_defaultsampleduration = 0;
4811
4812
        /* set trex for defaults */
4813
0
        MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moov, BOXDATA(p_tfhd)->i_track_ID );
4814
0
        if ( p_trex )
4815
0
        {
4816
0
            i_track_defaultsampleduration = BOXDATA(p_trex)->i_default_sample_duration;
4817
0
        }
4818
4819
0
        MP4_Box_t *p_trak = MP4_GetTrakByTrackID( p_moov, BOXDATA(p_tfhd)->i_track_ID );
4820
0
        if ( p_trak )
4821
0
        {
4822
0
            MP4_Box_t *p_mdhd = MP4_BoxGet( p_trak, "mdia/mdhd" );
4823
0
            if ( p_mdhd )
4824
0
                i_track_timescale = BOXDATA(p_mdhd)->i_timescale;
4825
0
        }
4826
4827
0
        if ( !i_track_timescale )
4828
0
        {
4829
0
           p_traf = p_traf->p_next;
4830
0
           continue;
4831
0
        }
4832
4833
0
        uint64_t i_traf_duration = 0;
4834
0
        while ( p_trun && p_tfhd )
4835
0
        {
4836
0
            if ( p_trun->i_type != ATOM_trun )
4837
0
            {
4838
0
               p_trun = p_trun->p_next;
4839
0
               continue;
4840
0
            }
4841
0
            const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun;
4842
4843
            /* Sum total time */
4844
0
            if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION )
4845
0
            {
4846
0
                for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
4847
0
                    i_traf_duration += p_trundata->p_samples[i].i_duration;
4848
0
            }
4849
0
            else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
4850
0
            {
4851
0
                i_traf_duration += (uint64_t)p_trundata->i_sample_count *
4852
0
                        BOXDATA(p_tfhd)->i_default_sample_duration;
4853
0
            }
4854
0
            else
4855
0
            {
4856
0
                i_traf_duration += (uint64_t)p_trundata->i_sample_count *
4857
0
                        i_track_defaultsampleduration;
4858
0
            }
4859
4860
0
            p_trun = p_trun->p_next;
4861
0
        }
4862
4863
0
        *p_duration = i_traf_duration;
4864
0
        return true;
4865
0
    }
4866
4867
0
    return false;
4868
0
}
4869
4870
static int ProbeFragments( demux_t *p_demux, bool b_force, bool *pb_fragmented )
4871
0
{
4872
0
    demux_sys_t *p_sys = p_demux->p_sys;
4873
4874
0
    msg_Dbg( p_demux, "probing fragments from %"PRId64, vlc_stream_Tell( p_demux->s ) );
4875
4876
0
    assert( p_sys->p_root );
4877
4878
0
    MP4_Box_t *p_vroot = MP4_BoxNew(ATOM_root);
4879
0
    if( !p_vroot )
4880
0
        return VLC_EGENERIC;
4881
4882
0
    if( p_sys->b_seekable && (p_sys->b_fastseekable || b_force) )
4883
0
    {
4884
0
        MP4_ReadBoxContainerChildren( p_demux->s, p_vroot, NULL ); /* Get the rest of the file */
4885
0
        p_sys->b_fragments_probed = true;
4886
4887
0
        const unsigned i_moof = MP4_BoxCount( p_vroot, "/moof" );
4888
0
        if( i_moof )
4889
0
        {
4890
0
            *pb_fragmented = true;
4891
0
            p_sys->p_fragsindex = MP4_Fragments_Index_New( p_sys->i_tracks, i_moof );
4892
0
            if( !p_sys->p_fragsindex )
4893
0
            {
4894
0
                MP4_BoxFree( p_vroot );
4895
0
                return VLC_EGENERIC;
4896
0
            }
4897
4898
0
            stime_t *pi_track_times = calloc( p_sys->i_tracks, sizeof(*pi_track_times) );
4899
0
            if( !pi_track_times )
4900
0
            {
4901
0
                MP4_Fragments_Index_Delete( p_sys->p_fragsindex );
4902
0
                p_sys->p_fragsindex = NULL;
4903
0
                MP4_BoxFree( p_vroot );
4904
0
                return VLC_EGENERIC;
4905
0
            }
4906
4907
0
            unsigned index = 0;
4908
4909
0
            for( MP4_Box_t *p_moof = p_vroot->p_first; p_moof; p_moof = p_moof->p_next )
4910
0
            {
4911
0
                if( p_moof->i_type != ATOM_moof )
4912
0
                    continue;
4913
4914
0
                for( unsigned i=0; i<p_sys->i_tracks; i++ )
4915
0
                {
4916
0
                    MP4_Box_t *p_tfdt = NULL;
4917
0
                    MP4_Box_t *p_traf = MP4_GetTrafByTrackID( p_moof, p_sys->track[i].i_track_ID );
4918
0
                    if( p_traf )
4919
0
                        p_tfdt = MP4_BoxGet( p_traf, "tfdt" );
4920
4921
0
                    if( p_tfdt && BOXDATA(p_tfdt) && p_tfdt->data.p_tfdt->i_base_media_decode_time <= INT64_MAX)
4922
0
                    {
4923
0
                        pi_track_times[i] = p_tfdt->data.p_tfdt->i_base_media_decode_time;
4924
0
                    }
4925
0
                    else if( index == 0 ) /* Set first fragment time offset from moov */
4926
0
                    {
4927
0
                        stime_t i_duration = GetMoovTrackDuration( p_sys, p_sys->track[i].i_track_ID );
4928
0
                        pi_track_times[i] = MP4_rescale( i_duration, p_sys->i_timescale, p_sys->track[i].i_timescale );
4929
0
                    }
4930
4931
0
                    stime_t i_movietime = MP4_rescale( pi_track_times[i], p_sys->track[i].i_timescale, p_sys->i_timescale );
4932
0
                    p_sys->p_fragsindex->p_times[index * p_sys->i_tracks + i] = i_movietime;
4933
4934
0
                    stime_t i_duration = 0;
4935
0
                    if( GetMoofTrackDuration( p_sys->p_moov, p_moof, p_sys->track[i].i_track_ID, &i_duration ) )
4936
0
                        pi_track_times[i] += i_duration;
4937
0
                }
4938
4939
0
                p_sys->p_fragsindex->pi_pos[index++] = p_moof->i_pos;
4940
0
            }
4941
4942
0
            for( unsigned i=0; i<p_sys->i_tracks; i++ )
4943
0
            {
4944
0
                stime_t i_movietime = MP4_rescale( pi_track_times[i], p_sys->track[i].i_timescale, p_sys->i_timescale );
4945
0
                if( p_sys->p_fragsindex->i_last_time < i_movietime )
4946
0
                    p_sys->p_fragsindex->i_last_time = i_movietime;
4947
0
            }
4948
4949
0
            free( pi_track_times );
4950
0
#ifdef MP4_VERBOSE
4951
0
            MP4_Fragments_Index_Dump( VLC_OBJECT(p_demux), p_sys->p_fragsindex, p_sys->i_timescale );
4952
0
#endif
4953
0
        }
4954
0
    }
4955
0
    else
4956
0
    {
4957
        /* We stop at first moof, which validates our fragmentation condition
4958
         * and we'll find others while reading. */
4959
0
        const uint32_t excllist[] = { ATOM_moof, 0 };
4960
0
        MP4_ReadBoxContainerRestricted( p_demux->s, p_vroot, NULL, excllist );
4961
        /* Peek since we stopped before restriction */
4962
0
        const uint8_t *p_peek;
4963
0
        if ( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) == 8 )
4964
0
            *pb_fragmented = (VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) == ATOM_moof);
4965
0
        else
4966
0
            *pb_fragmented = false;
4967
0
    }
4968
4969
0
    MP4_BoxFree( p_vroot );
4970
4971
0
    MP4_Box_t *p_mehd = MP4_BoxGet( p_sys->p_moov, "mvex/mehd");
4972
0
    if ( !p_mehd )
4973
0
           p_sys->i_cumulated_duration = GetCumulatedDuration( p_demux );
4974
4975
0
    return VLC_SUCCESS;
4976
0
}
4977
4978
static int ProbeFragmentsChecked( demux_t *p_demux )
4979
0
{
4980
0
    demux_sys_t *p_sys = p_demux->p_sys;
4981
4982
0
    if( p_sys->b_fragments_probed )
4983
0
        return VLC_SUCCESS;
4984
4985
0
    if( !p_sys->b_fastseekable )
4986
0
    {
4987
0
        const char *psz_msg = _(
4988
0
            "Because this file index is broken or missing, "
4989
0
            "seeking will not work correctly.\n"
4990
0
            "VLC won't repair your file but can temporary fix this "
4991
0
            "problem by building an index in memory.\n"
4992
0
            "This step might take a long time on a large file.\n"
4993
0
            "What do you want to do?");
4994
0
        bool b_continue = vlc_dialog_wait_question( p_demux,
4995
0
                                               VLC_DIALOG_QUESTION_NORMAL,
4996
0
                                               _("Do not seek"),
4997
0
                                               _("Build index"),
4998
0
                                               NULL,
4999
0
                                               _("Broken or missing Index"),
5000
0
                                               "%s", psz_msg );
5001
0
        if( !b_continue )
5002
0
            return VLC_EGENERIC;
5003
0
    }
5004
5005
0
    const uint64_t i_backup_pos = vlc_stream_Tell( p_demux->s );
5006
0
    int i_ret = vlc_stream_Seek( p_demux->s, p_sys->p_moov->i_pos + p_sys->p_moov->i_size );
5007
0
    if( i_ret == VLC_SUCCESS )
5008
0
    {
5009
0
        bool foo;
5010
0
        i_ret = ProbeFragments( p_demux, true, &foo );
5011
0
        p_sys->b_fragments_probed = true;
5012
0
    }
5013
5014
0
    if( i_ret != VLC_SUCCESS )
5015
0
        p_sys->b_error = (vlc_stream_Seek( p_demux->s, i_backup_pos ) != VLC_SUCCESS);
5016
5017
0
    return i_ret;
5018
0
}
5019
5020
static void FragResetContext( demux_sys_t *p_sys )
5021
1
{
5022
1
    if( p_sys->context.p_fragment_atom )
5023
0
    {
5024
0
        if( p_sys->context.p_fragment_atom != p_sys->p_moov )
5025
0
            MP4_BoxFree( p_sys->context.p_fragment_atom );
5026
0
        p_sys->context.p_fragment_atom = NULL;
5027
0
    }
5028
1
    p_sys->context.i_current_box_type = 0;
5029
5030
2
    for ( uint32_t i=0; i<p_sys->i_tracks; i++ )
5031
1
    {
5032
1
        mp4_track_t *p_track = &p_sys->track[i];
5033
1
        p_track->context.i_default_sample_size = 0;
5034
1
        p_track->context.i_default_sample_duration = 0;
5035
1
    }
5036
1
}
5037
5038
static int FragDemuxTrack( demux_t *p_demux, mp4_track_t *p_track,
5039
                           vlc_tick_t i_max_preload )
5040
0
{
5041
0
    if( !p_track->b_ok ||
5042
0
         p_track->context.runs.i_current >= p_track->context.runs.i_count )
5043
0
        return VLC_DEMUXER_EOS;
5044
5045
0
    const MP4_Box_data_trun_t *p_trun =
5046
0
            p_track->context.runs.p_array[p_track->context.runs.i_current].p_trun->data.p_trun;
5047
5048
0
    uint32_t dur = p_track->context.i_default_sample_duration,
5049
0
             len = p_track->context.i_default_sample_size;
5050
5051
0
    if( vlc_stream_Tell(p_demux->s) != p_track->context.i_trun_sample_pos &&
5052
0
        MP4_Seek( p_demux->s, p_track->context.i_trun_sample_pos ) != VLC_SUCCESS )
5053
0
        return VLC_DEMUXER_EOF;
5054
5055
0
    const stime_t i_demux_max_dts = (i_max_preload < INVALID_PRELOAD) ?
5056
0
                p_track->i_time + MP4_rescale_qtime( i_max_preload, p_track->i_timescale ) :
5057
0
                VLC_TICK_MAX;
5058
5059
0
    for( uint32_t i = p_track->context.i_trun_sample; i < p_trun->i_sample_count; i++ )
5060
0
    {
5061
0
        const stime_t i_dts = p_track->i_time;
5062
0
        stime_t i_pts = i_dts;
5063
5064
0
        if( p_trun->i_flags & MP4_TRUN_SAMPLE_DURATION )
5065
0
            dur = p_trun->p_samples[i].i_duration;
5066
5067
0
        if( i_dts > i_demux_max_dts )
5068
0
            return VLC_DEMUXER_SUCCESS;
5069
5070
0
        p_track->i_time += dur;
5071
0
        p_track->context.i_trun_sample = i + 1;
5072
5073
0
        if( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET )
5074
0
        {
5075
0
            if ( p_trun->i_version == 1 )
5076
0
                i_pts += p_trun->p_samples[i].i_composition_time_offset.v1;
5077
0
            else if( p_trun->p_samples[i].i_composition_time_offset.v0 < 0xFF000000 )
5078
0
                i_pts += p_trun->p_samples[i].i_composition_time_offset.v0;
5079
0
            else /* version 0 with negative */
5080
0
                i_pts += p_trun->p_samples[i].i_composition_time_offset.v1;
5081
0
        }
5082
5083
0
        if( p_trun->i_flags & MP4_TRUN_SAMPLE_SIZE )
5084
0
            len = p_trun->p_samples[i].i_size;
5085
5086
0
        if( !dur )
5087
0
            msg_Warn(p_demux, "Zero duration sample in trun.");
5088
5089
0
        if( !len )
5090
0
            msg_Warn(p_demux, "Zero length sample in trun.");
5091
5092
0
        len = OverflowCheck( p_demux, p_track, vlc_stream_Tell(p_demux->s), len );
5093
5094
0
        block_t *p_block = vlc_stream_Block( p_demux->s, len );
5095
0
        uint32_t i_read = ( p_block ) ? p_block->i_buffer : 0;
5096
0
        p_track->context.i_trun_sample_pos += i_read;
5097
0
        if( i_read < len || p_block == NULL )
5098
0
        {
5099
0
            if( p_block )
5100
0
                block_Release( p_block );
5101
0
            return VLC_DEMUXER_FATAL;
5102
0
        }
5103
5104
#if 0
5105
        msg_Dbg( p_demux, "tk(%i)=%"PRId64" mv=%"PRId64" pos=%"PRIu64, p_track->i_track_ID,
5106
                 VLC_TICK_0 + MP4_rescale_mtime( i_dts, p_track->i_timescale ),
5107
                 VLC_TICK_0 + MP4_rescale_mtime( i_pts, p_track->i_timescale ),
5108
                 p_track->context.i_trun_sample_pos - i_read );
5109
#endif
5110
0
        if ( p_track->p_es )
5111
0
        {
5112
0
            p_block->i_dts = VLC_TICK_0 + MP4_rescale_mtime( i_dts, p_track->i_timescale );
5113
0
            if( p_track->fmt.i_cat == VIDEO_ES && !( p_trun->i_flags & MP4_TRUN_SAMPLE_TIME_OFFSET ) )
5114
0
                p_block->i_pts = VLC_TICK_INVALID;
5115
0
            else
5116
0
                p_block->i_pts = VLC_TICK_0 + MP4_rescale_mtime( i_pts, p_track->i_timescale );
5117
0
            p_block->i_length = MP4_rescale_mtime( dur, p_track->i_timescale );
5118
0
            MP4_Block_Send( p_demux, p_track, p_block );
5119
0
        }
5120
0
        else block_Release( p_block );
5121
0
    }
5122
5123
0
    if( p_track->context.i_trun_sample == p_trun->i_sample_count )
5124
0
    {
5125
0
        p_track->context.i_trun_sample = 0;
5126
0
        if( ++p_track->context.runs.i_current < p_track->context.runs.i_count )
5127
0
        {
5128
0
            p_track->i_time = p_track->context.runs.p_array[p_track->context.runs.i_current].i_first_dts;
5129
0
            p_track->context.i_trun_sample_pos = p_track->context.runs.p_array[p_track->context.runs.i_current].i_offset;
5130
0
        }
5131
0
    }
5132
5133
0
    return VLC_DEMUXER_SUCCESS;
5134
0
}
5135
5136
static int DemuxMoof( demux_t *p_demux )
5137
0
{
5138
0
    demux_sys_t *p_sys = p_demux->p_sys;
5139
0
    int i_status;
5140
5141
0
    const vlc_tick_t i_max_preload = ( p_sys->b_fastseekable ) ? 0 : ( p_sys->b_seekable ) ? DEMUX_TRACK_MAX_PRELOAD : INVALID_PRELOAD;
5142
5143
0
    const vlc_tick_t i_nztime = MP4_GetMoviePTS( p_sys );
5144
5145
    /* !important! Ensure clock is set before sending data */
5146
0
    if( p_sys->i_pcr == VLC_TICK_INVALID )
5147
0
        es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_nztime );
5148
5149
    /* Set per track read state */
5150
0
    for( unsigned i = 0; i < p_sys->i_tracks; i++ )
5151
0
        p_sys->track[i].context.i_temp = VLC_DEMUXER_SUCCESS;
5152
5153
    /* demux up to increment amount of data on every track, or just set pcr if empty data */
5154
0
    for( ;; )
5155
0
    {
5156
0
        mp4_track_t *tk = NULL;
5157
0
        i_status = VLC_DEMUXER_EOS;
5158
0
        vlc_tick_t i_minnzdts = VLC_TICK_MAX;
5159
5160
        /* First pass, find any track within our target increment, ordered by position */
5161
0
        for( unsigned i = 0; i < p_sys->i_tracks; i++ )
5162
0
        {
5163
0
            mp4_track_t *tk_tmp = &p_sys->track[i];
5164
5165
0
            if( !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) ||
5166
0
               (!tk_tmp->b_selected && p_sys->b_seekable) ||
5167
0
                tk_tmp->context.runs.i_current >= tk_tmp->context.runs.i_count ||
5168
0
                tk_tmp->context.i_temp != VLC_DEMUXER_SUCCESS )
5169
0
                continue;
5170
5171
            /* At least still have data to demux on this or next turns */
5172
0
            i_status = VLC_DEMUXER_SUCCESS;
5173
5174
0
            vlc_tick_t i_nzdts = MP4_rescale_mtime( tk_tmp->i_time, tk_tmp->i_timescale );
5175
0
            if( i_nzdts <= i_nztime + DEMUX_INCREMENT )
5176
0
            {
5177
0
                if( tk == NULL || tk_tmp->context.i_trun_sample_pos < tk->context.i_trun_sample_pos )
5178
0
                    tk = tk_tmp;
5179
0
            }
5180
0
            i_minnzdts = __MIN(i_minnzdts, i_nzdts);
5181
0
        }
5182
5183
0
        if( tk )
5184
0
        {
5185
            /* Second pass, refine and find any best candidate having a chunk pos closer than
5186
             * current candidate (avoids seeks when increment falls between the 2) from
5187
             * current position, but within extended interleave time */
5188
0
            for( unsigned i = 0; i_max_preload != 0 && i < p_sys->i_tracks; i++ )
5189
0
            {
5190
0
                mp4_track_t *tk_tmp = &p_sys->track[i];
5191
0
                if( tk_tmp == tk ||
5192
0
                    !tk_tmp->b_ok || MP4_isMetadata( tk_tmp ) ||
5193
0
                   (!tk_tmp->b_selected && p_sys->b_seekable) ||
5194
0
                    tk_tmp->context.runs.i_current >= tk_tmp->context.runs.i_count ||
5195
0
                    tk_tmp->context.i_temp != VLC_DEMUXER_SUCCESS )
5196
0
                    continue;
5197
5198
0
                vlc_tick_t i_nzdts = MP4_rescale_mtime( tk_tmp->i_time, tk_tmp->i_timescale );
5199
0
                if ( i_nzdts <= i_nztime + DEMUX_TRACK_MAX_PRELOAD )
5200
0
                {
5201
                    /* Found a better candidate to avoid seeking */
5202
0
                    if( tk_tmp->context.i_trun_sample_pos < tk->context.i_trun_sample_pos )
5203
0
                        tk = tk_tmp;
5204
                    /* Note: previous candidate will be repicked on next loop */
5205
0
                }
5206
0
            }
5207
5208
0
            int i_ret = FragDemuxTrack( p_demux, tk, i_max_preload );
5209
5210
0
            tk->context.i_temp = i_ret;
5211
0
            if( i_ret == VLC_DEMUXER_SUCCESS )
5212
0
                i_status = VLC_DEMUXER_SUCCESS;
5213
0
            else if( i_ret == VLC_DEMUXER_FATAL )
5214
0
                i_status = VLC_DEMUXER_EOF;
5215
0
        }
5216
5217
0
        if( i_minnzdts != VLC_TICK_MAX && VLC_TICK_0 + i_minnzdts > p_sys->i_pcr )
5218
0
        {
5219
0
            p_sys->i_pcr = VLC_TICK_0 + i_minnzdts;
5220
0
            es_out_SetPCR( p_demux->out, p_sys->i_pcr );
5221
0
        }
5222
5223
0
        if( i_status != VLC_DEMUXER_SUCCESS || !tk )
5224
0
            break;
5225
0
    }
5226
5227
0
    if( i_status != VLC_DEMUXER_EOS )
5228
0
    {
5229
0
        p_sys->i_nztime += DEMUX_INCREMENT;
5230
0
        if ( VLC_TICK_0 + p_sys->i_nztime > p_sys->i_pcr )
5231
0
            p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime;
5232
0
        es_out_SetPCR( p_demux->out, p_sys->i_pcr );
5233
0
    }
5234
0
    else
5235
0
    {
5236
0
        vlc_tick_t i_segment_end = VLC_TICK_MAX;
5237
0
        for( unsigned i = 0; i < p_sys->i_tracks; i++ )
5238
0
        {
5239
0
            mp4_track_t *tk = &p_sys->track[i];
5240
0
            if( tk->b_ok || MP4_isMetadata( tk ) ||
5241
0
               (!tk->b_selected && !p_sys->b_seekable) )
5242
0
                continue;
5243
0
            vlc_tick_t i_track_end = MP4_rescale_mtime( tk->i_time, tk->i_timescale );
5244
0
            if( i_track_end < i_segment_end  )
5245
0
                i_segment_end = i_track_end;
5246
0
        }
5247
0
        if( i_segment_end != VLC_TICK_MAX )
5248
0
        {
5249
0
            p_sys->i_nztime = i_segment_end;
5250
0
            p_sys->i_pcr = VLC_TICK_0 + p_sys->i_nztime;
5251
0
            es_out_SetPCR( p_demux->out, p_sys->i_pcr );
5252
0
        }
5253
0
    }
5254
5255
0
    return i_status;
5256
0
}
5257
5258
static int FragCreateTrunIndex( demux_t *p_demux, MP4_Box_t *p_moof,
5259
                                MP4_Box_t *p_chunksidx, stime_t i_moof_time )
5260
0
{
5261
0
    demux_sys_t *p_sys = p_demux->p_sys;
5262
5263
0
    uint64_t i_traf_base_data_offset = p_moof->i_pos;
5264
0
    uint32_t i_traf = 0;
5265
0
    uint64_t i_prev_traf_end = 0;
5266
5267
0
    for( unsigned i=0; i<p_sys->i_tracks; i++ )
5268
0
    {
5269
0
        mp4_track_t *p_track = &p_sys->track[i];
5270
0
        if( p_track->context.runs.p_array )
5271
0
            free( p_track->context.runs.p_array );
5272
0
        p_track->context.runs.p_array = NULL;
5273
0
        p_track->context.runs.i_count = 0;
5274
0
        p_track->context.runs.i_current = 0;
5275
0
    }
5276
5277
0
    for( MP4_Box_t *p_traf = MP4_BoxGet( p_moof, "traf" );
5278
0
                    p_traf ; p_traf = p_traf->p_next )
5279
0
    {
5280
0
        if ( p_traf->i_type != ATOM_traf )
5281
0
            continue;
5282
5283
0
        const MP4_Box_t *p_tfhd = MP4_BoxGet( p_traf, "tfhd" );
5284
0
        uint32_t i_trun_count = MP4_BoxCount( p_traf, "trun" );
5285
0
        if ( !p_tfhd || !i_trun_count )
5286
0
            continue;
5287
5288
0
        mp4_track_t *p_track = MP4_GetTrackByTrackID( p_demux, BOXDATA(p_tfhd)->i_track_ID );
5289
0
        if( !p_track )
5290
0
            continue;
5291
5292
0
        void *r_array = vlc_reallocarray( p_track->context.runs.p_array,
5293
0
            i_trun_count + p_track->context.runs.i_count, sizeof(mp4_run_t) );
5294
0
        if ( unlikely( r_array == NULL ))
5295
0
        {
5296
0
            free( p_track->context.runs.p_array );
5297
0
            p_track->context.runs.p_array = NULL;
5298
0
            p_track->context.runs.i_count = 0;
5299
0
            continue;
5300
0
        }
5301
0
        p_track->context.runs.p_array = r_array;
5302
0
        memset(&p_track->context.runs.p_array[p_track->context.runs.i_count], 0, i_trun_count * sizeof(mp4_run_t));
5303
0
        i_trun_count += p_track->context.runs.i_count;
5304
5305
        /* Get defaults for this/these RUN */
5306
0
        uint32_t i_track_defaultsamplesize = 0;
5307
0
        uint32_t i_track_defaultsampleduration = 0;
5308
0
        MP4_GetDefaultSizeAndDuration( p_sys->p_moov, BOXDATA(p_tfhd),
5309
0
                                       &i_track_defaultsamplesize,
5310
0
                                       &i_track_defaultsampleduration );
5311
0
        p_track->context.i_default_sample_size = i_track_defaultsamplesize;
5312
0
        p_track->context.i_default_sample_duration = i_track_defaultsampleduration;
5313
5314
0
        stime_t  i_traf_start_time = p_track->i_time;
5315
0
        bool     b_has_base_media_decode_time = false;
5316
5317
0
        if( p_track->context.b_resync_time_offset ) /* We NEED start time offset for each track */
5318
0
        {
5319
0
            p_track->context.b_resync_time_offset = false;
5320
5321
            /* Find start time */
5322
0
            const MP4_Box_t *p_tfdt = MP4_BoxGet( p_traf, "tfdt" );
5323
0
            if( p_tfdt )
5324
0
            {
5325
0
                i_traf_start_time = BOXDATA(p_tfdt)->i_base_media_decode_time;
5326
0
                b_has_base_media_decode_time = true;
5327
0
            }
5328
5329
            /* Try using Tfxd for base offset (Smooth) */
5330
0
            if( !b_has_base_media_decode_time && p_sys->i_tracks == 1 )
5331
0
            {
5332
0
                const MP4_Box_t *p_uuid = MP4_BoxGet( p_traf, "uuid" );
5333
0
                for( ; p_uuid; p_uuid = p_uuid->p_next )
5334
0
                {
5335
0
                    if( p_uuid->i_type == ATOM_uuid &&
5336
0
                       !CmpUUID( &p_uuid->i_uuid, &TfxdBoxUUID ) && p_uuid->data.p_tfxd )
5337
0
                    {
5338
0
                        i_traf_start_time = p_uuid->data.p_tfxd->i_fragment_abs_time;
5339
0
                        b_has_base_media_decode_time = true;
5340
0
                        break;
5341
0
                    }
5342
0
                }
5343
0
            }
5344
5345
            /* After seek we should have probed fragments */
5346
0
            if( !b_has_base_media_decode_time && p_sys->p_fragsindex )
5347
0
            {
5348
0
                unsigned i_track_index = (p_track - p_sys->track);
5349
0
                assert(&p_sys->track[i_track_index] == p_track);
5350
0
                i_traf_start_time = MP4_Fragment_Index_GetTrackStartTime( p_sys->p_fragsindex,
5351
0
                                                                          i_track_index, p_moof->i_pos );
5352
0
                i_traf_start_time = MP4_rescale( i_traf_start_time,
5353
0
                                                 p_sys->i_timescale, p_track->i_timescale );
5354
0
                b_has_base_media_decode_time = true;
5355
0
            }
5356
5357
0
            if( !b_has_base_media_decode_time && p_chunksidx )
5358
0
            {
5359
                /* Try using SIDX as base offset.
5360
                 * This can not work for global sidx but only when sent within each fragment (dash) */
5361
0
                const MP4_Box_data_sidx_t *p_data = p_chunksidx->data.p_sidx;
5362
0
                if( p_data && p_data->i_timescale && p_data->i_reference_count == 1 )
5363
0
                {
5364
0
                    i_traf_start_time = MP4_rescale( p_data->i_earliest_presentation_time,
5365
0
                                                     p_data->i_timescale, p_track->i_timescale );
5366
0
                    b_has_base_media_decode_time = true;
5367
0
                }
5368
0
            }
5369
5370
            /* First contiguous segment (moov->moof) and there's no tfdt not probed index (yet) */
5371
0
            if( !b_has_base_media_decode_time && FragGetMoofSequenceNumber( p_moof ) == 1 )
5372
0
            {
5373
0
                i_traf_start_time = MP4_rescale( GetMoovTrackDuration( p_sys, p_track->i_track_ID ),
5374
0
                                                 p_sys->i_timescale, p_track->i_timescale );
5375
0
                b_has_base_media_decode_time = true;
5376
0
            }
5377
5378
            /* Use global sidx moof time, in case moof does not carry tfdt */
5379
0
            if( !b_has_base_media_decode_time )
5380
0
            {
5381
0
                if( i_moof_time != INVALID_SEGMENT_TIME )
5382
0
                    i_traf_start_time = MP4_rescale( i_moof_time, p_sys->i_timescale, p_track->i_timescale );
5383
0
                else if ( p_sys->i_nztime != VLC_TICK_MAX ) /* That should not happen */
5384
0
                    i_traf_start_time = MP4_rescale_qtime( p_sys->i_nztime, p_track->i_timescale );
5385
0
                else /* That should not happen */
5386
0
                    i_traf_start_time = p_track->i_time;
5387
0
            }
5388
0
        }
5389
5390
        /* Parse TRUN data */
5391
5392
0
        if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET )
5393
0
        {
5394
0
            i_traf_base_data_offset = BOXDATA(p_tfhd)->i_base_data_offset;
5395
0
        }
5396
        /* ignored if MP4_TFHD_BASE_DATA_OFFSET */
5397
0
        else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF )
5398
0
        {
5399
0
            i_traf_base_data_offset = p_moof->i_pos /* + 8*/;
5400
0
        }
5401
0
        else
5402
0
        {
5403
0
            if ( i_traf == 0 )
5404
0
                i_traf_base_data_offset = p_moof->i_pos /*+ 8*/;
5405
0
            else
5406
0
                i_traf_base_data_offset = i_prev_traf_end;
5407
0
        }
5408
5409
0
        uint64_t i_trun_dts = i_traf_start_time;
5410
0
        uint64_t i_trun_data_offset = i_traf_base_data_offset;
5411
0
        uint32_t i_trun_size = 0;
5412
5413
0
        for( const MP4_Box_t *p_trun = MP4_BoxGet( p_traf, "trun" );
5414
0
                              p_trun && p_tfhd;  p_trun = p_trun->p_next )
5415
0
        {
5416
0
            if ( p_trun->i_type != ATOM_trun )
5417
0
               continue;
5418
5419
0
            const MP4_Box_data_trun_t *p_trundata = p_trun->data.p_trun;
5420
5421
            /* Get data offset */
5422
0
            if ( p_trundata->i_flags & MP4_TRUN_DATA_OFFSET )
5423
0
            {
5424
                /* Fix for broken Trun data offset relative to tfhd instead of moof, as seen in smooth */
5425
0
                if( (BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET) == 0 &&
5426
0
                    i_traf == 0 &&
5427
0
                    i_traf_base_data_offset + p_trundata->i_data_offset < p_moof->i_pos + p_moof->i_size + 8 )
5428
0
                {
5429
0
                    i_trun_data_offset += p_moof->i_size + 8;
5430
0
                }
5431
0
                else if( (BOXDATA(p_tfhd)->i_flags & MP4_TFHD_BASE_DATA_OFFSET) )
5432
0
                {
5433
0
                    i_trun_data_offset = BOXDATA(p_tfhd)->i_base_data_offset + p_trundata->i_data_offset;
5434
0
                }
5435
                /* ignored if MP4_TFHD_BASE_DATA_OFFSET */
5436
0
                else if ( BOXDATA(p_tfhd)->i_flags & MP4_TFHD_DEFAULT_BASE_IS_MOOF )
5437
0
                {
5438
0
                    i_trun_data_offset = p_moof->i_pos + p_trundata->i_data_offset;
5439
0
                }
5440
0
                else
5441
0
                {
5442
0
                    i_trun_data_offset += p_trundata->i_data_offset;
5443
0
                }
5444
0
            }
5445
0
            else
5446
0
            {
5447
0
                i_trun_data_offset += i_trun_size;
5448
0
            }
5449
5450
0
            i_trun_size = 0;
5451
0
#ifndef NDEBUG
5452
0
            msg_Dbg( p_demux,
5453
0
                     "tk %u run %" PRIu32 " dflt dur %"PRIu32" size %"PRIu32" firstdts %"PRId64" offset %"PRIu64,
5454
0
                     p_track->i_track_ID,
5455
0
                     p_track->context.runs.i_count,
5456
0
                     i_track_defaultsampleduration,
5457
0
                     i_track_defaultsamplesize,
5458
0
                     MP4_rescale_mtime( i_trun_dts, p_track->i_timescale ), i_trun_data_offset );
5459
0
#endif
5460
            //************
5461
0
            assert(p_track->context.runs.i_count < i_trun_count);
5462
0
            mp4_run_t *p_run = &p_track->context.runs.p_array[p_track->context.runs.i_count++];
5463
0
            p_run->i_first_dts = i_trun_dts;
5464
0
            p_run->i_offset = i_trun_data_offset;
5465
0
            p_run->p_trun = p_trun;
5466
5467
            //************
5468
            /* Sum total time */
5469
0
            if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_DURATION )
5470
0
            {
5471
0
                for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
5472
0
                    i_trun_dts += p_trundata->p_samples[i].i_duration;
5473
0
            }
5474
0
            else
5475
0
            {
5476
0
                i_trun_dts += (uint64_t)p_trundata->i_sample_count *
5477
0
                        i_track_defaultsampleduration;
5478
0
            }
5479
5480
            /* Get total traf size */
5481
0
            if ( p_trundata->i_flags & MP4_TRUN_SAMPLE_SIZE )
5482
0
            {
5483
0
                for( uint32_t i=0; i< p_trundata->i_sample_count; i++ )
5484
0
                    i_trun_size += p_trundata->p_samples[i].i_size;
5485
0
            }
5486
0
            else
5487
0
            {
5488
0
                i_trun_size += p_trundata->i_sample_count *
5489
0
                        i_track_defaultsamplesize;
5490
0
            }
5491
5492
0
            i_prev_traf_end = i_trun_data_offset + i_trun_size;
5493
0
        }
5494
5495
0
        i_traf++;
5496
0
    }
5497
5498
0
    return VLC_SUCCESS;
5499
0
}
5500
5501
static int FragGetMoofBySidxIndex( demux_t *p_demux, vlc_tick_t target_time,
5502
                                   uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime )
5503
0
{
5504
0
    demux_sys_t *p_sys = p_demux->p_sys;
5505
0
    const MP4_Box_t *p_sidx = MP4_BoxGet( p_sys->p_root, "sidx" );
5506
0
    for( ; p_sidx ; p_sidx = p_sidx->p_next )
5507
0
    {
5508
0
        if( p_sidx->i_type != ATOM_sidx )
5509
0
            continue;
5510
5511
0
        const MP4_Box_data_sidx_t *p_data = BOXDATA(p_sidx);
5512
0
        if( !p_data || !p_data->i_timescale )
5513
0
            break;
5514
5515
0
        stime_t i_target_time = MP4_rescale_qtime( target_time, p_data->i_timescale );
5516
5517
        /* sidx refers to offsets from end of sidx pos in the file + first offset */
5518
0
        uint64_t i_pos = p_data->i_first_offset + p_sidx->i_pos + p_sidx->i_size;
5519
0
        stime_t i_time = 0;
5520
0
        for( uint16_t i=0; i<p_data->i_reference_count; i++ )
5521
0
        {
5522
0
            if(p_data->p_items[i].b_reference_type != 0)
5523
0
                continue;
5524
0
            if( i_time + p_data->p_items[i].i_subsegment_duration > i_target_time )
5525
0
            {
5526
0
                *pi_sampletime = MP4_rescale_mtime( i_time, p_data->i_timescale );
5527
0
                *pi_moof_pos = i_pos;
5528
0
                return VLC_SUCCESS;
5529
0
            }
5530
0
            i_pos += p_data->p_items[i].i_referenced_size;
5531
0
            i_time += p_data->p_items[i].i_subsegment_duration;
5532
0
        }
5533
0
    }
5534
0
    return VLC_EGENERIC;
5535
0
}
5536
5537
static int FragGetMoofByTfraIndex( demux_t *p_demux, const vlc_tick_t i_target_time, unsigned i_track_ID,
5538
                                   uint64_t *pi_moof_pos, vlc_tick_t *pi_sampletime )
5539
0
{
5540
0
    demux_sys_t *p_sys = p_demux->p_sys;
5541
0
    MP4_Box_t *p_tfra = MP4_BoxGet( p_sys->p_root, "mfra/tfra" );
5542
0
    for( ; p_tfra; p_tfra = p_tfra->p_next )
5543
0
    {
5544
0
        if ( p_tfra->i_type == ATOM_tfra )
5545
0
        {
5546
0
            const MP4_Box_data_tfra_t *p_data = BOXDATA(p_tfra);
5547
0
            if( !p_data || p_data->i_track_ID != i_track_ID )
5548
0
                continue;
5549
5550
0
            uint64_t i_pos = 0;
5551
0
            mp4_track_t *p_track = MP4_GetTrackByTrackID( p_demux, p_data->i_track_ID );
5552
0
            if ( p_track )
5553
0
            {
5554
0
                stime_t i_track_target_time = MP4_rescale_qtime( i_target_time, p_track->i_timescale );
5555
0
                for ( uint32_t i = 0; i<p_data->i_number_of_entries; i += ( p_data->i_version == 1 ) ? 2 : 1 )
5556
0
                {
5557
0
                    stime_t i_time = p_data->p_time[i];
5558
0
                    uint64_t i_offset = p_data->p_moof_offset[i];
5559
5560
0
                    if ( i_time >= i_track_target_time )
5561
0
                    {
5562
0
                        if ( i_pos == 0 ) /* Not in this traf */
5563
0
                            break;
5564
5565
0
                        *pi_moof_pos = i_pos;
5566
0
                        *pi_sampletime = MP4_rescale_mtime( i_time, p_track->i_timescale );
5567
0
                        return VLC_SUCCESS;
5568
0
                    }
5569
0
                    else
5570
0
                        i_pos = i_offset;
5571
0
                }
5572
0
            }
5573
0
        }
5574
0
    }
5575
0
    return VLC_EGENERIC;
5576
0
}
5577
5578
static void MP4_GetDefaultSizeAndDuration( MP4_Box_t *p_moov,
5579
                                           const MP4_Box_data_tfhd_t *p_tfhd_data,
5580
                                           uint32_t *pi_default_size,
5581
                                           uint32_t *pi_default_duration )
5582
0
{
5583
0
    if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_DURATION )
5584
0
        *pi_default_duration = p_tfhd_data->i_default_sample_duration;
5585
5586
0
    if( p_tfhd_data->i_flags & MP4_TFHD_DFLT_SAMPLE_SIZE )
5587
0
        *pi_default_size = p_tfhd_data->i_default_sample_size;
5588
5589
0
    if( !*pi_default_duration || !*pi_default_size )
5590
0
    {
5591
0
        const MP4_Box_t *p_trex = MP4_GetTrexByTrackID( p_moov, p_tfhd_data->i_track_ID );
5592
0
        if ( p_trex )
5593
0
        {
5594
0
            if ( !*pi_default_duration )
5595
0
                *pi_default_duration = BOXDATA(p_trex)->i_default_sample_duration;
5596
0
            if ( !*pi_default_size )
5597
0
                *pi_default_size = BOXDATA(p_trex)->i_default_sample_size;
5598
0
        }
5599
0
    }
5600
0
}
5601
5602
static int DemuxFrag( demux_t *p_demux )
5603
0
{
5604
0
    demux_sys_t *p_sys = p_demux->p_sys;
5605
0
    unsigned i_track_selected = 0;
5606
0
    int i_status = VLC_DEMUXER_SUCCESS;
5607
5608
0
    if( unlikely(p_sys->b_error) )
5609
0
    {
5610
0
        msg_Warn( p_demux, "unrecoverable error" );
5611
0
        i_status = VLC_DEMUXER_EOF;
5612
0
        goto end;
5613
0
    }
5614
5615
    /* check for newly selected/unselected track */
5616
0
    for( unsigned i_track = 0; i_track < p_sys->i_tracks; i_track++ )
5617
0
    {
5618
0
        mp4_track_t *tk = &p_sys->track[i_track];
5619
0
        bool b = true;
5620
5621
0
        if( !tk->b_ok || MP4_isMetadata( tk ) )
5622
0
            continue;
5623
5624
0
        if( p_sys->b_seekable )
5625
0
            es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
5626
5627
0
        if(tk->b_selected != b)
5628
0
        {
5629
0
            msg_Dbg( p_demux, "track %u %s!", tk->i_track_ID, b ? "enabled" : "disabled" );
5630
0
            MP4_TrackSelect( p_demux, tk, b );
5631
0
        }
5632
5633
0
        if( tk->b_selected )
5634
0
            i_track_selected++;
5635
0
    }
5636
5637
0
    if( i_track_selected <= 0 )
5638
0
    {
5639
0
        msg_Warn( p_demux, "no track selected, exiting..." );
5640
0
        i_status = VLC_DEMUXER_EOF;
5641
0
        goto end;
5642
0
    }
5643
5644
0
    if ( p_sys->context.i_current_box_type != ATOM_mdat )
5645
0
    {
5646
        /* Otherwise mdat is skipped. FIXME: mdat reading ! */
5647
0
        const uint8_t *p_peek;
5648
0
        if( vlc_stream_Peek( p_demux->s, &p_peek, 8 ) != 8 )
5649
0
        {
5650
0
            i_status = VLC_DEMUXER_EOF;
5651
0
            goto end;
5652
0
        }
5653
5654
0
        p_sys->context.i_current_box_type = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
5655
0
        if( p_sys->context.i_current_box_type != ATOM_moof &&
5656
0
            p_sys->context.i_current_box_type != ATOM_moov )
5657
0
        {
5658
0
            uint64_t i_pos = vlc_stream_Tell( p_demux->s );
5659
0
            uint64_t i_size = GetDWBE( p_peek );
5660
0
            if ( i_size == 1 )
5661
0
            {
5662
0
                if( vlc_stream_Peek( p_demux->s, &p_peek, 16 ) != 16 )
5663
0
                {
5664
0
                    i_status = VLC_DEMUXER_EOF;
5665
0
                    goto end;
5666
0
                }
5667
0
                i_size = GetQWBE( p_peek + 8 );
5668
0
            }
5669
5670
0
            if( UINT64_MAX - i_pos < i_size )
5671
0
            {
5672
0
                i_status = VLC_DEMUXER_EOF;
5673
0
                goto end;
5674
0
            }
5675
5676
0
            if( p_sys->context.i_current_box_type == ATOM_mdat )
5677
0
            {
5678
                /* We'll now read mdat using context atom,
5679
                 * but we'll need post mdat offset, as we'll never seek backward */
5680
0
                p_sys->context.i_post_mdat_offset = i_pos + i_size;
5681
0
            }
5682
0
            else if( MP4_Seek( p_demux->s, i_pos + i_size ) != VLC_SUCCESS ) /* skip other atoms */
5683
0
            {
5684
0
                i_status = VLC_DEMUXER_EOF;
5685
0
                goto end;
5686
0
            }
5687
0
        }
5688
0
        else
5689
0
        {
5690
0
            MP4_Box_t *p_vroot = MP4_BoxGetNextChunk( p_demux->s );
5691
0
            if(!p_vroot)
5692
0
            {
5693
0
                i_status = VLC_DEMUXER_EOF;
5694
0
                goto end;
5695
0
            }
5696
5697
0
            MP4_Box_t *p_box = NULL;
5698
0
            for( p_box = p_vroot->p_first; p_box; p_box = p_box->p_next )
5699
0
            {
5700
0
                if( p_box->i_type == ATOM_moof ||
5701
0
                    p_box->i_type == ATOM_moov )
5702
0
                    break;
5703
0
            }
5704
5705
0
            if( p_box )
5706
0
            {
5707
0
                FragResetContext( p_sys );
5708
5709
0
                if( p_box->i_type == ATOM_moov )
5710
0
                {
5711
0
                    p_sys->context.p_fragment_atom = p_sys->p_moov;
5712
0
                }
5713
0
                else
5714
0
                {
5715
0
                    p_sys->context.p_fragment_atom = MP4_BoxExtract( &p_vroot->p_first, p_box->i_type );
5716
5717
                    /* Detect and Handle Passive Seek */
5718
0
                    const uint32_t i_sequence_number = FragGetMoofSequenceNumber( p_sys->context.p_fragment_atom );
5719
0
                    const bool b_discontinuity = ( i_sequence_number != p_sys->context.i_lastseqnumber + 1 );
5720
0
                    if( b_discontinuity )
5721
0
                        msg_Info( p_demux, "Fragment sequence discontinuity detected %"PRIu32" != %"PRIu32,
5722
0
                                            i_sequence_number, p_sys->context.i_lastseqnumber + 1 );
5723
0
                    p_sys->context.i_lastseqnumber = i_sequence_number;
5724
5725
                    /* Prepare chunk */
5726
0
                    if( FragPrepareChunk( p_demux, p_sys->context.p_fragment_atom,
5727
0
                                          MP4_BoxGet( p_vroot, "sidx"), INVALID_SEGMENT_TIME,
5728
0
                                          b_discontinuity ) != VLC_SUCCESS )
5729
0
                    {
5730
0
                        MP4_BoxFree( p_vroot );
5731
0
                        i_status = VLC_DEMUXER_EOF;
5732
0
                        goto end;
5733
0
                    }
5734
5735
0
                    if( b_discontinuity )
5736
0
                    {
5737
0
                        p_sys->i_nztime = FragGetDemuxTimeFromTracksTime( p_sys );
5738
0
                        p_sys->i_pcr = VLC_TICK_INVALID;
5739
0
                    }
5740
                    /* !Prepare chunk */
5741
0
                }
5742
5743
0
                p_sys->context.i_current_box_type = p_box->i_type;
5744
0
            }
5745
5746
0
            MP4_BoxFree( p_vroot );
5747
5748
0
            if( p_sys->context.p_fragment_atom == NULL )
5749
0
            {
5750
0
                msg_Info(p_demux, "no moof or moov in current chunk");
5751
0
                return VLC_DEMUXER_SUCCESS;
5752
0
            }
5753
0
        }
5754
0
    }
5755
5756
0
    if ( p_sys->context.i_current_box_type == ATOM_mdat )
5757
0
    {
5758
0
        assert(p_sys->context.p_fragment_atom);
5759
5760
0
        if ( p_sys->context.p_fragment_atom )
5761
0
        switch( p_sys->context.p_fragment_atom->i_type )
5762
0
        {
5763
0
            case ATOM_moov://[ftyp/moov, mdat]+ -> [moof, mdat]+
5764
0
                i_status = DemuxMoov( p_demux );
5765
0
            break;
5766
0
            case ATOM_moof:
5767
0
                i_status = DemuxMoof( p_demux );
5768
0
              break;
5769
0
        default:
5770
0
             msg_Err( p_demux, "fragment type %4.4s", (char*) &p_sys->context.p_fragment_atom->i_type );
5771
0
             break;
5772
0
        }
5773
5774
0
        if( i_status == VLC_DEMUXER_EOS )
5775
0
        {
5776
0
            i_status = VLC_DEMUXER_SUCCESS;
5777
            /* Skip if we didn't reach the end of mdat box */
5778
0
            uint64_t i_pos = vlc_stream_Tell( p_demux->s );
5779
0
            if( i_pos != p_sys->context.i_post_mdat_offset && i_status != VLC_DEMUXER_EOF )
5780
0
            {
5781
0
                if( i_pos > p_sys->context.i_post_mdat_offset )
5782
0
                    msg_Err( p_demux, " Overread mdat by %" PRIu64, i_pos - p_sys->context.i_post_mdat_offset );
5783
0
                else if( !p_sys->b_seekable )
5784
0
                    msg_Warn( p_demux, "mdat had still %"PRIu64" bytes unparsed as samples",
5785
0
                                        p_sys->context.i_post_mdat_offset - i_pos );
5786
0
                if( MP4_Seek( p_demux->s, p_sys->context.i_post_mdat_offset ) != VLC_SUCCESS )
5787
0
                    i_status = VLC_DEMUXER_EGENERIC;
5788
0
            }
5789
0
            p_sys->context.i_current_box_type = 0;
5790
5791
0
        }
5792
0
    }
5793
5794
0
end:
5795
0
    if( i_status == VLC_DEMUXER_EOF )
5796
0
    {
5797
0
        vlc_tick_t i_demux_end = VLC_TICK_MIN;
5798
0
        for( unsigned i = 0; i < p_sys->i_tracks; i++ )
5799
0
        {
5800
0
            const mp4_track_t *tk = &p_sys->track[i];
5801
0
            vlc_tick_t i_track_end = MP4_rescale_mtime( tk->i_time, tk->i_timescale );
5802
0
            if( i_track_end > i_demux_end  )
5803
0
                i_demux_end = i_track_end;
5804
0
        }
5805
0
        if( i_demux_end != VLC_TICK_MIN )
5806
0
            es_out_SetPCR( p_demux->out, VLC_TICK_0 + i_demux_end );
5807
0
    }
5808
5809
0
    return i_status;
5810
0
}
5811
5812
/* ASF Handlers */
5813
inline static mp4_track_t *MP4ASF_GetTrack( asf_packet_sys_t *p_packetsys,
5814
                                            uint8_t i_stream_number )
5815
0
{
5816
0
    demux_t *p_demux = p_packetsys->priv;
5817
0
    demux_sys_t *p_sys = p_demux->p_sys;
5818
0
    for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
5819
0
    {
5820
0
        if ( p_sys->track[i].p_asf &&
5821
0
             i_stream_number == p_sys->track[i].BOXDATA(p_asf)->i_stream_number )
5822
0
        {
5823
0
            return &p_sys->track[i];
5824
0
        }
5825
0
    }
5826
0
    return NULL;
5827
0
}
5828
5829
static asf_track_info_t * MP4ASF_GetTrackInfo( asf_packet_sys_t *p_packetsys,
5830
                                               uint8_t i_stream_number )
5831
0
{
5832
0
    mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number );
5833
0
    if ( p_track )
5834
0
        return &p_track->asfinfo;
5835
0
    else
5836
0
        return NULL;
5837
0
}
5838
5839
static void MP4ASF_Send( asf_packet_sys_t *p_packetsys, uint8_t i_stream_number,
5840
                         block_t **pp_frame )
5841
0
{
5842
0
    demux_t *p_demux = p_packetsys->priv;
5843
0
    mp4_track_t *p_track = MP4ASF_GetTrack( p_packetsys, i_stream_number );
5844
0
    if ( !p_track )
5845
0
    {
5846
0
        block_Release( *pp_frame );
5847
0
    }
5848
0
    else
5849
0
    {
5850
0
        block_t *p_gather = block_ChainGather( *pp_frame );
5851
0
        p_gather->i_dts = p_track->i_dts_backup;
5852
0
        p_gather->i_pts = p_track->i_pts_backup;
5853
0
        es_out_Send( p_demux->out, p_track->p_es, p_gather );
5854
0
    }
5855
5856
0
    *pp_frame = NULL;
5857
0
}
5858
5859
static void MP4ASF_ResetFrames( demux_sys_t *p_sys )
5860
0
{
5861
0
    for ( unsigned int i=0; i<p_sys->i_tracks; i++ )
5862
0
    {
5863
0
        mp4_track_t *p_track = &p_sys->track[i];
5864
0
        ASFPacketTrackReset( &p_track->asfinfo );
5865
0
    }
5866
0
}