Coverage Report

Created: 2026-08-31 08:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/mpeg4video.c
Line
Count
Source
1
/*****************************************************************************
2
 * mpeg4video.c: mpeg 4 video packetizer
3
 *****************************************************************************
4
 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5
 *
6
 * Authors: Gildas Bazin <gbazin@videolan.org>
7
 *          Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Eric Petit <titer@videolan.org>
9
 *
10
 * This program is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU Lesser General Public License as published by
12
 * the Free Software Foundation; either version 2.1 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with this program; if not, write to the Free Software Foundation,
22
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24
25
/*****************************************************************************
26
 * Preamble
27
 *****************************************************************************/
28
29
#ifdef HAVE_CONFIG_H
30
# include "config.h"
31
#endif
32
33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35
#include <vlc_sout.h>
36
#include <vlc_codec.h>
37
#include <vlc_block.h>
38
39
#include <vlc_bits.h>
40
#include <vlc_block_helper.h>
41
#include "packetizer_helper.h"
42
#include "startcode_helper.h"
43
#include "h26x_nal_common.h"
44
45
/*****************************************************************************
46
 * Module descriptor
47
 *****************************************************************************/
48
static int  Open ( vlc_object_t * );
49
static void Close( vlc_object_t * );
50
51
170
vlc_module_begin ()
52
85
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
53
85
    set_description( N_("MPEG4 video packetizer") )
54
85
    set_capability( "video packetizer", 50 )
55
170
    set_callbacks( Open, Close )
56
85
vlc_module_end ()
57
58
/****************************************************************************
59
 * Local prototypes
60
 ****************************************************************************/
61
typedef struct
62
{
63
    /*
64
     * Input properties
65
     */
66
    packetizer_t packetizer;
67
68
    /*
69
     * Common properties
70
     */
71
    vlc_tick_t i_interpolated_pts;
72
    vlc_tick_t i_interpolated_dts;
73
    vlc_tick_t i_last_ref_pts;
74
    uint64_t i_last_time_ref;
75
    uint64_t i_time_ref;
76
    int64_t i_last_time;
77
    uint32_t i_last_timeincr;
78
79
    unsigned int i_flags;
80
81
    uint16_t    i_fps_num;
82
83
    bool  b_frame;
84
85
    /* Current frame being built */
86
    block_t    *p_frame;
87
    block_t    **pp_last;
88
} decoder_sys_t;
89
90
static block_t *Packetize( decoder_t *, block_t ** );
91
static void PacketizeFlush( decoder_t * );
92
93
static void PacketizeReset( void *p_private, bool b_broken );
94
static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
95
static int PacketizeValidate( void *p_private, block_t * );
96
97
static block_t *ParseMPEGBlock( decoder_t *, block_t * );
98
static int ParseVOL( decoder_t *, es_format_t *, uint8_t *, int );
99
static int ParseVO( decoder_t *, block_t * );
100
static int ParseVOP( decoder_t *, block_t * );
101
102
#define VIDEO_OBJECT_MASK                       0x01f
103
#define VIDEO_OBJECT_LAYER_MASK                 0x00f
104
105
#define VIDEO_OBJECT_START_CODE                 0x100
106
1.08M
#define VIDEO_OBJECT_LAYER_START_CODE           0x120
107
430k
#define RESERVED_START_CODE                     0x130
108
778k
#define VISUAL_OBJECT_SEQUENCE_START_CODE       0x1b0
109
778k
#define VISUAL_OBJECT_SEQUENCE_END_CODE         0x1b1
110
388k
#define USER_DATA_START_CODE                    0x1b2
111
#define GROUP_OF_VOP_START_CODE                 0x1b3
112
#define VIDEO_SESSION_ERROR_CODE                0x1b4
113
248k
#define VISUAL_OBJECT_START_CODE                0x1b5
114
477k
#define VOP_START_CODE                          0x1b6
115
#define FACE_OBJECT_START_CODE                  0x1ba
116
#define FACE_OBJECT_PLANE_START_CODE            0x1bb
117
#define MESH_OBJECT_START_CODE                  0x1bc
118
#define MESH_OBJECT_PLANE_START_CODE            0x1bd
119
#define STILL_TEXTURE_OBJECT_START_CODE         0x1be
120
#define TEXTURE_SPATIAL_LAYER_START_CODE        0x1bf
121
#define TEXTURE_SNR_LAYER_START_CODE            0x1c0
122
123
static const uint8_t p_mp4v_startcode[3] = { 0x00, 0x00, 0x01 };
124
125
/*****************************************************************************
126
 * Open: probe the packetizer and return score
127
 *****************************************************************************/
128
static int Open( vlc_object_t *p_this )
129
82.5k
{
130
82.5k
    decoder_t     *p_dec = (decoder_t*)p_this;
131
82.5k
    decoder_sys_t *p_sys;
132
133
82.5k
    if( p_dec->fmt_in->i_codec != VLC_CODEC_MP4V )
134
75.3k
        return VLC_EGENERIC;
135
136
    /* Allocate the memory needed to store the decoder's structure */
137
7.19k
    if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
138
0
        return VLC_ENOMEM;
139
7.19k
    memset( p_sys, 0, sizeof(decoder_sys_t) );
140
141
    /* Misc init */
142
7.19k
    packetizer_Init( &p_sys->packetizer,
143
7.19k
                     p_mp4v_startcode, sizeof(p_mp4v_startcode), startcode_FindAnnexB,
144
7.19k
                     NULL, 0, 4,
145
7.19k
                     PacketizeReset, PacketizeParse, PacketizeValidate, NULL,
146
7.19k
                     p_dec );
147
148
7.19k
    p_sys->p_frame = NULL;
149
7.19k
    p_sys->pp_last = &p_sys->p_frame;
150
151
    /* Setup properties */
152
7.19k
    es_format_Copy( &p_dec->fmt_out, p_dec->fmt_in );
153
7.19k
    p_dec->fmt_out.i_codec = VLC_CODEC_MP4V;
154
155
7.19k
    if( p_dec->fmt_out.i_extra )
156
1.82k
    {
157
        /* We have a vol */
158
1.82k
        msg_Dbg( p_dec, "opening with vol size: %zu", p_dec->fmt_out.i_extra );
159
1.82k
        ParseVOL( p_dec, &p_dec->fmt_out,
160
1.82k
                  p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
161
1.82k
    }
162
163
    /* Set callback */
164
7.19k
    p_dec->pf_packetize = Packetize;
165
7.19k
    p_dec->pf_flush = PacketizeFlush;
166
7.19k
    p_dec->pf_get_cc = NULL;
167
168
7.19k
    return VLC_SUCCESS;
169
7.19k
}
170
171
/*****************************************************************************
172
 * Close: clean up the packetizer
173
 *****************************************************************************/
174
static void Close( vlc_object_t *p_this )
175
7.19k
{
176
7.19k
    decoder_t *p_dec = (decoder_t*)p_this;
177
7.19k
    decoder_sys_t *p_sys = p_dec->p_sys;
178
179
7.19k
    packetizer_Clean( &p_sys->packetizer );
180
7.19k
    if( p_sys->p_frame )
181
1.71k
        block_ChainRelease( p_sys->p_frame );
182
7.19k
    free( p_sys );
183
7.19k
}
184
185
/****************************************************************************
186
 * Packetize: the whole thing
187
 ****************************************************************************/
188
static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
189
96.1k
{
190
96.1k
    decoder_sys_t *p_sys = p_dec->p_sys;
191
192
96.1k
    return packetizer_Packetize( &p_sys->packetizer, pp_block );
193
96.1k
}
194
195
static void PacketizeFlush( decoder_t *p_dec )
196
0
{
197
0
    decoder_sys_t *p_sys = p_dec->p_sys;
198
199
0
    packetizer_Flush( &p_sys->packetizer );
200
0
}
201
202
/*****************************************************************************
203
 * Helpers:
204
 *****************************************************************************/
205
static void PacketizeReset( void *p_private, bool b_flush )
206
31.4k
{
207
31.4k
    decoder_t *p_dec = p_private;
208
31.4k
    decoder_sys_t *p_sys = p_dec->p_sys;
209
210
31.4k
    if( b_flush )
211
0
    {
212
0
        if( p_sys->p_frame )
213
0
            block_ChainRelease( p_sys->p_frame );
214
0
        p_sys->p_frame = NULL;
215
0
        p_sys->pp_last = &p_sys->p_frame;
216
0
    }
217
218
31.4k
    p_sys->i_interpolated_pts =
219
31.4k
    p_sys->i_interpolated_dts =
220
31.4k
    p_sys->i_last_ref_pts = VLC_TICK_INVALID;
221
222
31.4k
    p_sys->i_last_time_ref =
223
31.4k
    p_sys->i_time_ref =
224
31.4k
    p_sys->i_last_time =
225
31.4k
    p_sys->i_last_timeincr = 0;
226
31.4k
}
227
228
static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
229
389k
{
230
389k
    decoder_t *p_dec = p_private;
231
389k
    const vlc_tick_t i_dts = p_block->i_dts;
232
389k
    const vlc_tick_t i_pts = p_block->i_pts;
233
234
389k
    block_t *p_au = ParseMPEGBlock( p_dec, p_block );
235
236
389k
    *pb_ts_used = p_au &&  p_au->i_dts == i_dts && p_au->i_pts == i_pts;
237
238
389k
    return p_au;
239
389k
}
240
241
242
static int PacketizeValidate( void *p_private, block_t *p_au )
243
4.63k
{
244
4.63k
    decoder_t *p_dec = p_private;
245
4.63k
    decoder_sys_t *p_sys = p_dec->p_sys;
246
247
    /* We've just started the stream, wait for the first PTS.
248
     * We discard here so we can still get the sequence header. */
249
4.63k
    if( p_sys->i_interpolated_pts == VLC_TICK_INVALID &&
250
40
        p_sys->i_interpolated_dts == VLC_TICK_INVALID )
251
0
    {
252
0
        msg_Dbg( p_dec, "need a starting pts/dts" );
253
0
        return VLC_EGENERIC;
254
0
    }
255
256
    /* When starting the stream we can have the first frame with
257
     * a null DTS (i_interpolated_pts is initialized to 0) */
258
4.63k
    if( p_au->i_dts == VLC_TICK_INVALID )
259
130
        p_au->i_dts = p_au->i_pts;
260
4.63k
    return VLC_SUCCESS;
261
4.63k
}
262
263
/*****************************************************************************
264
 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
265
 *****************************************************************************/
266
static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
267
389k
{
268
389k
    decoder_sys_t *p_sys = p_dec->p_sys;
269
389k
    block_t *p_pic = NULL;
270
271
389k
    if( p_frag->i_buffer < 4 )
272
0
        return p_frag;
273
274
389k
    const uint32_t i_startcode = GetDWBE( p_frag->p_buffer );
275
389k
    if( i_startcode == VISUAL_OBJECT_SEQUENCE_START_CODE ||
276
388k
        i_startcode == VISUAL_OBJECT_SEQUENCE_END_CODE ||
277
388k
        i_startcode == USER_DATA_START_CODE )
278
5.02k
    {   /* VOS and USERDATA */
279
#if 0
280
        /* Remove VOS start/end code from the original stream */
281
        block_Release( p_frag );
282
#else
283
        /* Append the block for now since ts/ps muxers rely on VOL
284
         * being present in the stream */
285
5.02k
        block_ChainLastAppend( &p_sys->pp_last, p_frag );
286
5.02k
#endif
287
5.02k
        return NULL;
288
5.02k
    }
289
384k
    else if( i_startcode >= VIDEO_OBJECT_LAYER_START_CODE &&
290
280k
             i_startcode < RESERVED_START_CODE )
291
109k
    {
292
        /* Copy the complete VOL */
293
109k
        if( (size_t)p_dec->fmt_out.i_extra != p_frag->i_buffer ||
294
29.8k
           ( p_frag->i_buffer && memcmp(p_dec->fmt_out.p_extra,
295
29.8k
                                        p_frag->p_buffer, p_frag->i_buffer) ) )
296
84.7k
        {
297
84.7k
            void *p_realloc = realloc( p_dec->fmt_out.p_extra, p_frag->i_buffer );
298
84.7k
            if( p_realloc )
299
84.7k
            {
300
84.7k
                p_dec->fmt_out.p_extra = p_realloc;
301
84.7k
                p_dec->fmt_out.i_extra = p_frag->i_buffer;
302
84.7k
                memcpy( p_realloc, p_frag->p_buffer, p_frag->i_buffer );
303
84.7k
            }
304
84.7k
        }
305
109k
        ParseVOL( p_dec, &p_dec->fmt_out,
306
109k
                  p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
307
308
#if 0
309
        /* Remove from the original stream */
310
        block_Release( p_frag );
311
#else
312
        /* Append the block for now since ts/ps muxers rely on VOL
313
         * being present in the stream */
314
109k
        block_ChainLastAppend( &p_sys->pp_last, p_frag );
315
109k
#endif
316
109k
        return NULL;
317
109k
    }
318
275k
    else
319
275k
    {
320
275k
        if( !p_dec->fmt_out.i_extra )
321
26.9k
        {
322
26.9k
            msg_Warn( p_dec, "waiting for VOL" );
323
26.9k
            block_Release( p_frag );
324
26.9k
            return NULL;
325
26.9k
        }
326
327
        /* Append the block */
328
248k
        block_ChainLastAppend( &p_sys->pp_last, p_frag );
329
248k
    }
330
331
248k
    if( i_startcode == VISUAL_OBJECT_START_CODE )
332
9.56k
    {
333
9.56k
        ParseVO( p_dec, p_frag );
334
9.56k
    }
335
238k
    else
336
238k
    if( i_startcode == VOP_START_CODE &&
337
7.53k
        ParseVOP( p_dec, p_frag ) == VLC_SUCCESS )
338
4.63k
    {
339
        /* We are dealing with a VOP */
340
4.63k
        p_pic = block_ChainGather( p_sys->p_frame );
341
4.63k
        p_pic->i_flags = p_sys->i_flags;
342
4.63k
        p_pic->i_pts = p_sys->i_interpolated_pts;
343
4.63k
        p_pic->i_dts = p_sys->i_interpolated_dts;
344
345
#if 0
346
    msg_Err( p_dec, "output dts/pts (%"PRId64",%"PRId64")", p_pic->i_dts, p_pic->i_pts );
347
#endif
348
        /* Reset context */
349
4.63k
        p_sys->p_frame = NULL;
350
4.63k
        p_sys->pp_last = &p_sys->p_frame;
351
4.63k
    }
352
353
248k
    return p_pic;
354
389k
}
355
356
/* ParseVOL: */
357
static int ParseVOL( decoder_t *p_dec, es_format_t *fmt,
358
                     uint8_t *p_vol, int i_vol )
359
110k
{
360
110k
    decoder_sys_t *p_sys = p_dec->p_sys;
361
110k
    int i_vo_ver_id, i_shape;
362
110k
    h26x_aspect_ratio_t ar;
363
110k
    bs_t s;
364
365
110k
    for( ;; )
366
158k
    {
367
158k
        if( i_vol <= 5 )
368
2.34k
            return VLC_EGENERIC;
369
370
156k
        uint32_t i_startcode = GetDWBE( p_vol );
371
156k
        if( i_startcode >= VIDEO_OBJECT_LAYER_START_CODE &&
372
150k
            i_startcode < RESERVED_START_CODE )
373
108k
            break;
374
375
47.6k
        p_vol++; i_vol--;
376
47.6k
    }
377
378
108k
    bs_init( &s, &p_vol[4], i_vol - 4 );
379
380
108k
    bs_skip( &s, 1 );   /* random access */
381
108k
    bs_skip( &s, 8 );   /* vo_type */
382
108k
    if( bs_read1( &s ) )
383
76.9k
    {
384
76.9k
        i_vo_ver_id = bs_read( &s, 4 );
385
76.9k
        bs_skip( &s, 3 );
386
76.9k
    }
387
31.6k
    else
388
31.6k
    {
389
31.6k
        i_vo_ver_id = 1;
390
31.6k
    }
391
108k
    ar.aspect_ratio_idc = bs_read( &s, 4 ) & 0xf;
392
108k
    if( ar.aspect_ratio_idc == 0xf )
393
13.9k
    {
394
13.9k
        ar.aspect_ratio_idc = 0xff; // was only coded on 4 bits in MPEG4
395
13.9k
        ar.sar_width = bs_read( &s, 8 );
396
13.9k
        ar.sar_height = bs_read( &s, 8 );
397
13.9k
    }
398
108k
    if(!p_dec->fmt_in->video.i_sar_num ||
399
5.84k
       !p_dec->fmt_in->video.i_sar_den)
400
103k
    {
401
103k
        h26x_get_aspect_ratio(&ar,
402
103k
                              &fmt->video.i_sar_num,
403
103k
                              &fmt->video.i_sar_den);
404
103k
    }
405
406
108k
    if( bs_read1( &s ) )
407
71.3k
    {
408
        /* vol control parameter */
409
71.3k
        bs_skip( &s, 2 ); /* chroma_format */
410
71.3k
        bs_skip( &s, 1 ); /* low_delay */
411
412
71.3k
        if( bs_read1( &s ) )
413
27.1k
        {
414
27.1k
            bs_skip( &s, 16 );
415
27.1k
            bs_skip( &s, 16 );
416
27.1k
            bs_skip( &s, 16 );
417
27.1k
            bs_skip( &s, 3 );
418
27.1k
            bs_skip( &s, 11 );
419
27.1k
            bs_skip( &s, 1 );
420
27.1k
            bs_skip( &s, 16 );
421
27.1k
        }
422
71.3k
    }
423
    /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
424
108k
    i_shape = bs_read( &s, 2 );
425
108k
    if( i_shape == 3 && i_vo_ver_id != 1 )
426
19.0k
    {
427
19.0k
        bs_skip( &s, 4 );
428
19.0k
    }
429
430
108k
    if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
431
432
39.6k
    p_sys->i_fps_num = bs_read( &s, 16 ); /* Time increment resolution*/
433
39.6k
    if( !p_sys->i_fps_num ) p_sys->i_fps_num = 1;
434
435
39.6k
    if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
436
437
31.1k
    if( bs_read1( &s ) )
438
29.3k
    {
439
29.3k
        unsigned i_time_increment_bits = vlc_log2( p_sys->i_fps_num - 1 ) + 1;
440
441
29.3k
        bs_skip( &s, i_time_increment_bits ); /* i_fps_den */
442
29.3k
    }
443
31.1k
    if( i_shape == 0 )
444
9.73k
    {
445
9.73k
        bs_skip( &s, 1 );
446
9.73k
        fmt->video.i_width = bs_read( &s, 13 );
447
9.73k
        bs_skip( &s, 1 );
448
9.73k
        fmt->video.i_height= bs_read( &s, 13 );
449
9.73k
        bs_skip( &s, 1 );
450
9.73k
    }
451
452
31.1k
    return VLC_SUCCESS;
453
39.6k
}
454
455
static int ParseVO( decoder_t *p_dec, block_t *p_vo )
456
9.56k
{
457
9.56k
    bs_t s;
458
9.56k
    bs_init( &s, &p_vo->p_buffer[4], p_vo->i_buffer - 4 );
459
9.56k
    if( bs_read1( &s ) )
460
2.19k
        bs_skip( &s, 7 );
461
462
9.56k
    const uint8_t visual_object_type = bs_read( &s, 4 );
463
9.56k
    if( visual_object_type == 1 /* video ID */ ||
464
9.50k
        visual_object_type == 2 /* still texture ID */ )
465
2.61k
    {
466
2.61k
        h26x_colour_description_t colour =
467
2.61k
        {
468
2.61k
            .colour_primaries = 1,
469
2.61k
            .transfer_characteristics = 1,
470
2.61k
            .matrix_coeffs = 1,
471
2.61k
            .full_range_flag = 0,
472
2.61k
        };
473
2.61k
        if( bs_read1( &s ) ) /* video_signal_type */
474
1.72k
        {
475
1.72k
            bs_skip( &s, 3 );
476
1.72k
            colour.full_range_flag = bs_read( &s, 1 );
477
1.72k
            if( bs_read( &s, 1 ) ) /* colour description */
478
1.20k
            {
479
1.20k
                colour.colour_primaries = bs_read( &s, 8 );
480
1.20k
                colour.transfer_characteristics = bs_read( &s, 8 );
481
1.20k
                colour.matrix_coeffs = bs_read( &s, 8 );
482
1.20k
            }
483
1.72k
        }
484
485
2.61k
        if( p_dec->fmt_in->video.primaries == COLOR_PRIMARIES_UNDEF )
486
2.61k
        {
487
2.61k
            h26x_get_colorimetry( &colour,
488
2.61k
                                  &p_dec->fmt_out.video.primaries,
489
2.61k
                                  &p_dec->fmt_out.video.transfer,
490
2.61k
                                  &p_dec->fmt_out.video.space,
491
2.61k
                                  &p_dec->fmt_out.video.color_range );
492
2.61k
        }
493
2.61k
    }
494
495
9.56k
    return VLC_SUCCESS;
496
9.56k
}
497
498
static int ParseVOP( decoder_t *p_dec, block_t *p_vop )
499
7.53k
{
500
7.53k
    decoder_sys_t *p_sys = p_dec->p_sys;
501
7.53k
    uint64_t i_time_ref;
502
7.53k
    uint32_t i_time_increment;
503
7.53k
    uint64_t i_modulo_time_base = 0;
504
7.53k
    bs_t s;
505
506
7.53k
    bs_init( &s, &p_vop->p_buffer[4], p_vop->i_buffer - 4 );
507
508
7.53k
    switch( bs_read( &s, 2 ) )
509
7.53k
    {
510
2.95k
    case 0:
511
2.95k
        p_sys->i_flags = BLOCK_FLAG_TYPE_I;
512
2.95k
        break;
513
1.11k
    case 1:
514
1.11k
        p_sys->i_flags = BLOCK_FLAG_TYPE_P;
515
1.11k
        break;
516
1.69k
    case 2:
517
1.69k
        p_sys->i_flags = BLOCK_FLAG_TYPE_B;
518
1.69k
        p_sys->b_frame = true;
519
1.69k
        break;
520
1.76k
    case 3: /* Sprite : ISO 14496-2 ยง 6.3.5 */
521
1.76k
        p_sys->i_flags = BLOCK_FLAG_TYPE_PB;
522
1.76k
        break;
523
7.53k
    }
524
525
308k
    while( bs_read( &s, 1 ) ) i_modulo_time_base++;
526
7.53k
    if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
527
528
    /* Interpolate PTS/DTS */
529
4.63k
    if( !(p_sys->i_flags & BLOCK_FLAG_TYPE_B) )
530
3.30k
    {
531
3.30k
        p_sys->i_last_time_ref = p_sys->i_time_ref;
532
3.30k
        p_sys->i_time_ref +=
533
3.30k
            (i_modulo_time_base * p_sys->i_fps_num);
534
3.30k
        i_time_ref = p_sys->i_time_ref;
535
3.30k
    }
536
1.33k
    else
537
1.33k
    {
538
1.33k
        i_time_ref = p_sys->i_last_time_ref +
539
1.33k
            (i_modulo_time_base * p_sys->i_fps_num);
540
1.33k
    }
541
542
4.63k
    if( p_sys->i_fps_num )
543
4.62k
    {
544
        /* VOP time increment */
545
4.62k
        unsigned i_time_increment_bits = vlc_log2(p_sys->i_fps_num - 1) + 1;
546
4.62k
        i_time_increment = bs_read( &s, i_time_increment_bits );
547
548
4.62k
        int64_t i_time_diff = (i_time_ref + i_time_increment) - (p_sys->i_last_time + p_sys->i_last_timeincr);
549
4.62k
        if( i_modulo_time_base == 0 && i_time_diff < 0 && -i_time_diff > p_sys->i_fps_num )
550
436
        {
551
436
            msg_Warn(p_dec, "missing modulo_time_base update");
552
436
            i_modulo_time_base += -i_time_diff / p_sys->i_fps_num;
553
436
            p_sys->i_time_ref += (i_modulo_time_base * p_sys->i_fps_num);
554
436
            p_sys->i_time_ref += p_sys->i_last_timeincr % p_sys->i_fps_num;
555
436
            i_time_ref = p_sys->i_time_ref;
556
436
        }
557
4.62k
    }
558
559
4.63k
    if( p_sys->i_fps_num < 5 && /* Work-around buggy streams */
560
28
        p_dec->fmt_in->video.i_frame_rate > 0 &&
561
4
        p_dec->fmt_in->video.i_frame_rate_base > 0 )
562
4
    {
563
4
        p_sys->i_interpolated_pts += vlc_tick_from_samples(
564
4
        p_dec->fmt_in->video.i_frame_rate_base,
565
4
        p_dec->fmt_in->video.i_frame_rate);
566
4
    }
567
4.62k
    else
568
4.62k
    {
569
4.62k
        int64_t i_time_diff = (i_time_ref + i_time_increment) - (p_sys->i_last_time + p_sys->i_last_timeincr);
570
4.62k
        p_sys->i_interpolated_pts += vlc_tick_from_samples( i_time_diff, p_sys->i_fps_num );
571
4.62k
    }
572
573
#if 0
574
    msg_Err( p_dec, "interp dts/pts (%"PRId64",%"PRId64"), dts/pts (%"PRId64",%"PRId64") %"PRId64" mod %d inc %"PRId64,
575
             p_sys->i_interpolated_dts, p_sys->i_interpolated_pts,
576
             p_vop->i_dts, p_vop->i_pts, p_sys->i_time_ref, i_modulo_time_base, i_time_increment );
577
#endif
578
579
4.63k
    p_sys->i_last_time = i_time_ref;
580
4.63k
    if( p_sys->i_fps_num )
581
4.62k
        p_sys->i_last_timeincr = i_time_increment;
582
583
    /* Correct interpolated dts when we receive a new pts/dts */
584
4.63k
    if( p_vop->i_pts != VLC_TICK_INVALID )
585
221
        p_sys->i_interpolated_pts = p_vop->i_pts;
586
4.63k
    if( p_vop->i_dts != VLC_TICK_INVALID )
587
2.31k
        p_sys->i_interpolated_dts = p_vop->i_dts;
588
589
4.63k
    if( (p_sys->i_flags & BLOCK_FLAG_TYPE_B) || !p_sys->b_frame )
590
2.77k
    {
591
        /* Trivial case (DTS == PTS) */
592
593
2.77k
        p_sys->i_interpolated_dts = p_sys->i_interpolated_pts;
594
595
2.77k
        if( p_vop->i_pts != VLC_TICK_INVALID )
596
220
            p_sys->i_interpolated_dts = p_vop->i_pts;
597
2.77k
        if( p_vop->i_dts != VLC_TICK_INVALID )
598
914
            p_sys->i_interpolated_dts = p_vop->i_dts;
599
600
2.77k
        p_sys->i_interpolated_pts = p_sys->i_interpolated_dts;
601
2.77k
    }
602
1.85k
    else
603
1.85k
    {
604
1.85k
        if( p_sys->i_last_ref_pts != VLC_TICK_INVALID )
605
1.67k
            p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
606
607
1.85k
        p_sys->i_last_ref_pts = p_sys->i_interpolated_pts;
608
1.85k
    }
609
610
4.63k
    return VLC_SUCCESS;
611
7.53k
}