Coverage Report

Created: 2026-09-28 07:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/demux/mpeg/h26x.c
Line
Count
Source
1
/*****************************************************************************
2
 * h26x.c : raw H264 and HEVC Video demuxers
3
 *****************************************************************************
4
 * Copyright (C) 2002-2004, 2014-2015 VLC authors and VideoLAN
5
 *
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *          Denis Charmet <typx@videolan.org>
8
 *
9
 * This program is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU Lesser General Public License as published by
11
 * the Free Software Foundation; either version 2.1 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program; if not, write to the Free Software Foundation,
21
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
 *****************************************************************************/
23
24
/*****************************************************************************
25
 * Preamble
26
 *****************************************************************************/
27
28
#ifdef HAVE_CONFIG_H
29
# include "config.h"
30
#endif
31
32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_demux.h>
35
#include <vlc_codec.h>
36
#include "../../packetizer/hevc_nal.h" /* definitions, inline helpers */
37
#include "../../packetizer/h264_nal.h" /* definitions, inline helpers */
38
39
/*****************************************************************************
40
 * Module descriptor
41
 *****************************************************************************/
42
static int  OpenH264 ( vlc_object_t * );
43
static int  OpenHEVC ( vlc_object_t * );
44
static void Close( vlc_object_t * );
45
46
#define FPS_TEXT N_("Frames per Second")
47
#define FPS_LONGTEXT N_("Desired frame rate for the stream.")
48
49
174
vlc_module_begin ()
50
87
    set_shortname( "H264")
51
87
    set_subcategory( SUBCAT_INPUT_DEMUX )
52
87
    set_description( N_("H264 video demuxer" ) )
53
87
    set_capability( "demux", 8 )
54
87
    set_section( N_("H264 video demuxer" ), NULL )
55
87
    add_float( "h264-fps", 0.0, FPS_TEXT, FPS_LONGTEXT )
56
87
    set_callbacks( OpenH264, Close )
57
87
    add_shortcut( "h264" )
58
87
    add_file_extension("h264")
59
60
87
    add_submodule()
61
87
        set_shortname( "HEVC")
62
87
        set_subcategory( SUBCAT_INPUT_DEMUX )
63
87
        set_description( N_("HEVC/H.265 video demuxer" ) )
64
87
        set_capability( "demux", 8 )
65
87
        set_section( N_("HEVC/H.265 video demuxer" ), NULL )
66
87
        add_float( "hevc-fps", 0.0, FPS_TEXT, FPS_LONGTEXT )
67
87
        set_callbacks( OpenHEVC, Close )
68
87
        add_shortcut( "hevc", "h265" )
69
70
87
vlc_module_end ()
71
72
/*****************************************************************************
73
 * Local prototypes
74
 *****************************************************************************/
75
typedef struct
76
{
77
    es_out_id_t *p_es;
78
79
    date_t      feed_dts;
80
    date_t      output_dts;
81
    unsigned    frame_rate_num;
82
    unsigned    frame_rate_den;
83
84
    decoder_t *p_packetizer;
85
} demux_sys_t;
86
87
static int Demux( demux_t * );
88
static int Control( demux_t *, int, va_list );
89
90
64.5k
#define H26X_PACKET_SIZE 2048
91
1.92k
#define H26X_PEEK_CHUNK  (H26X_PACKET_SIZE * 4)
92
2.01M
#define H26X_MIN_PEEK    (4 + 7 + 10)
93
677
#define H26X_MAX_PEEK    (H26X_PEEK_CHUNK * 8) /* max data to check */
94
1.61k
#define H26X_NAL_COUNT   8 /* max # or NAL to check */
95
96
/*****************************************************************************
97
 * Probing
98
 *****************************************************************************/
99
typedef struct
100
{
101
    bool b_sps;
102
    bool b_pps;
103
    bool b_vps;
104
} hevc_probe_ctx_t;
105
106
typedef struct
107
{
108
    bool b_sps;
109
    bool b_pps;
110
} h264_probe_ctx_t;
111
112
static int ProbeHEVC( const uint8_t *p_peek, size_t i_peek, void *p_priv )
113
1.25k
{
114
1.25k
    hevc_probe_ctx_t *p_ctx = (hevc_probe_ctx_t *) p_priv;
115
116
1.25k
    if( i_peek < 2 )
117
0
        return -1;
118
119
1.25k
    if( p_peek[0] & 0x80 )
120
3
        return -1;
121
122
1.24k
    const uint8_t i_type = hevc_getNALType( p_peek );
123
1.24k
    const uint8_t i_layer = hevc_getNALLayer( p_peek );
124
125
1.24k
   if ( i_type == HEVC_NAL_VPS ) /* VPS */
126
374
   {
127
374
       if( i_layer != 0 || i_peek < 6 ||
128
357
           p_peek[4] != 0xFF || p_peek[5] != 0xFF ) /* Check reserved bits */
129
74
           return -1;
130
300
       p_ctx->b_vps = true;
131
300
       return 0;
132
374
   }
133
875
   else if( i_type == HEVC_NAL_SPS )  /* SPS */
134
318
   {
135
318
       if( i_layer != 0 )
136
11
           return -1;
137
307
       p_ctx->b_sps = true;
138
307
       return 0;
139
318
   }
140
557
   else if( i_type == HEVC_NAL_PPS )  /* PPS */
141
284
   {
142
284
       if( i_layer != 0 )
143
11
           return -1;
144
273
       p_ctx->b_pps = true;
145
273
       return 0;
146
284
   }
147
273
   else if( i_type >= HEVC_NAL_BLA_W_LP && i_type <= HEVC_NAL_CRA ) /* Key Frame */
148
186
   {
149
186
        if( p_ctx->b_vps && p_ctx->b_sps && p_ctx->b_pps && i_layer == 0 )
150
143
            return 1;
151
186
   }
152
87
   else if( i_type == HEVC_NAL_AUD ) /* AU */
153
14
   {
154
14
        if( i_peek < H26X_MIN_PEEK ||
155
14
            p_peek[4] != 0 || p_peek[5] != 0 ) /* Must prefix another NAL */
156
4
            return -1;
157
14
   }
158
73
   else if( i_type == HEVC_NAL_PREF_SEI ) /* Prefix SEI */
159
21
   {
160
21
       if( p_peek[2] == 0xFF ) /* empty SEI */
161
0
           return -1;
162
21
   }
163
52
   else
164
52
   {
165
52
       return -1; /* See 7.4.2.4.4 for sequence order */
166
52
   }
167
168
74
    return 0; /* Probe more */
169
1.24k
}
170
171
static int ProbeH264( const uint8_t *p_peek, size_t i_peek, void *p_priv )
172
1
{
173
1
    h264_probe_ctx_t *p_ctx = (h264_probe_ctx_t *) p_priv;
174
175
1
    if( i_peek < 1 )
176
0
        return -1;
177
1
    const uint8_t i_nal_type = p_peek[0] & 0x1F;
178
1
    const uint8_t i_ref_idc = p_peek[0] & 0x60;
179
180
1
    if( (p_peek[0] & 0x80) ) /* reserved 0 */
181
0
        return -1;
182
183
    /* ( !i_ref_idc && (i_nal_type < 6 || i_nal_type == 7 || i_nal_type == 8) ) ||
184
       (  i_ref_idc && (i_nal_type == 6 || i_nal_type >= 9) ) */
185
186
1
    if( i_nal_type == H264_NAL_SPS )
187
0
    {
188
0
        if( i_ref_idc == 0 || i_peek < 3 ||
189
0
           (p_peek[2] & 0x03) /* reserved 0 bits */ )
190
0
            return -1;
191
0
        p_ctx->b_sps = true;
192
0
    }
193
1
    else if( i_nal_type == H264_NAL_PPS )
194
0
    {
195
0
        if( i_ref_idc == 0 )
196
0
            return -1;
197
0
        p_ctx->b_pps = true;
198
0
    }
199
1
    else if( i_nal_type == H264_NAL_SLICE_IDR )
200
0
    {
201
0
        if( i_ref_idc == 0 || ! p_ctx->b_pps || ! p_ctx->b_sps )
202
0
            return -1;
203
0
        else
204
0
            return 1;
205
0
    }
206
1
    else if( i_nal_type == H264_NAL_AU_DELIMITER )
207
0
    {
208
0
        if( i_ref_idc )
209
0
            return -1;
210
0
    }
211
1
    else if ( i_nal_type == H264_NAL_SEI )
212
0
    {
213
0
        if( i_ref_idc )
214
0
            return -1;
215
0
    }
216
     /* 7.4.1.1 */
217
1
    else if ( i_nal_type == H264_NAL_SPS_EXT ||
218
1
              i_nal_type == H264_NAL_SUBSET_SPS )
219
0
    {
220
0
        if( i_ref_idc == 0 || !p_ctx->b_sps )
221
0
            return -1;
222
0
    }
223
1
    else if( i_nal_type == H264_NAL_PREFIX )
224
0
    {
225
0
        if( i_ref_idc == 0 || !p_ctx->b_pps || !p_ctx->b_sps )
226
0
            return -1;
227
0
    }
228
1
    else return -1; /* see 7.4.1.2.3 for sequence */
229
230
0
    return 0;
231
1
}
232
233
/*****************************************************************************
234
 * Shared Open code
235
 *****************************************************************************/
236
static inline bool check_Property( demux_t *p_demux, const char **pp_psz,
237
                                   bool(*pf_check)(demux_t *, const char *) )
238
32
{
239
168
    while( *pp_psz )
240
136
    {
241
136
        if( pf_check( p_demux, *pp_psz ) )
242
0
            return true;
243
136
        pp_psz++;
244
136
    }
245
32
    return false;
246
32
}
247
248
static int GenericOpen( demux_t *p_demux, const char *psz_module,
249
                        vlc_fourcc_t i_codec,
250
                        int(*pf_probe)(const uint8_t *, size_t, void *),
251
                        void *p_ctx,
252
                        const char **pp_psz_exts,
253
                        const char **pp_psz_mimes )
254
1.31k
{
255
1.31k
    demux_sys_t *p_sys;
256
1.31k
    const uint8_t *p_peek;
257
1.31k
    es_format_t fmt;
258
1.31k
    uint8_t annexb_startcode[] = {0,0,0,1};
259
1.31k
    int i_ret = 0;
260
261
    /* Restrict by type first */
262
1.31k
    if( !p_demux->obj.force &&
263
16
        !check_Property( p_demux, pp_psz_exts, demux_IsPathExtension ) &&
264
16
        !check_Property( p_demux, pp_psz_mimes, demux_IsContentType ) )
265
16
    {
266
16
        return VLC_EGENERIC;
267
16
    }
268
269
    /* First check for first AnnexB header */
270
1.29k
    if( vlc_stream_Peek( p_demux->s, &p_peek, H26X_MIN_PEEK ) == H26X_MIN_PEEK &&
271
1.29k
       !memcmp( p_peek, annexb_startcode, 4 ) )
272
368
    {
273
368
        ssize_t i_peek = H26X_MIN_PEEK;
274
368
        size_t i_peek_target = H26X_MIN_PEEK;
275
368
        ssize_t i_probe_offset = 4;
276
368
        const uint8_t *p_probe = p_peek;
277
368
        bool b_synced = true;
278
368
        unsigned i_bitflow = 0;
279
280
1.61k
        for( unsigned i=0; i<H26X_NAL_COUNT; i++ )
281
1.55k
        {
282
1.00M
            while( !b_synced )
283
1.00M
            {
284
1.00M
                if( i_probe_offset + H26X_MIN_PEEK >= i_peek &&
285
677
                    i_peek_target + H26X_PEEK_CHUNK <= H26X_MAX_PEEK )
286
569
                {
287
569
                    i_peek_target += H26X_PEEK_CHUNK;
288
569
                    i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_peek_target );
289
569
                }
290
291
1.00M
                if( i_probe_offset + H26X_MIN_PEEK >= i_peek )
292
297
                    break;
293
294
1.00M
                p_probe = &p_peek[i_probe_offset];
295
1.00M
                i_bitflow = (i_bitflow << 1) | (!p_probe[0]);
296
                /* Check for annexB */
297
1.00M
                if( p_probe[0] == 0x01 && ((i_bitflow & 0x06) == 0x06) )
298
885
                    b_synced = true;
299
300
1.00M
                i_probe_offset++;
301
1.00M
            }
302
303
1.55k
            if( b_synced )
304
1.25k
            {
305
1.25k
                p_probe = &p_peek[i_probe_offset];
306
1.25k
                i_ret = pf_probe( p_probe, (size_t)(i_peek - i_probe_offset), p_ctx );
307
1.25k
            }
308
309
1.55k
            if( i_ret != 0 )
310
299
                break;
311
312
1.25k
            i_probe_offset += 4;
313
1.25k
            b_synced = false;
314
1.25k
        }
315
368
    }
316
317
1.29k
    if( i_ret < 1 )
318
1.15k
    {
319
1.15k
        if( !p_demux->obj.force )
320
0
        {
321
0
            msg_Warn( p_demux, "%s module discarded (no startcode)", psz_module );
322
0
            return VLC_EGENERIC;
323
0
        }
324
325
1.15k
        msg_Err( p_demux, "this doesn't look like a %s ES stream, "
326
1.15k
                 "continuing anyway", psz_module );
327
1.15k
    }
328
329
1.29k
    p_demux->pf_demux  = Demux;
330
1.29k
    p_demux->pf_control= Control;
331
1.29k
    p_demux->p_sys     = p_sys = malloc( sizeof( demux_sys_t ) );
332
1.29k
    p_sys->p_es        = NULL;
333
1.29k
    p_sys->frame_rate_num = 0;
334
1.29k
    p_sys->frame_rate_den = 0;
335
336
1.29k
    float f_fps = 0;
337
1.29k
    char *psz_fpsvar;
338
1.29k
    if( asprintf( &psz_fpsvar, "%s-fps", psz_module ) != -1 )
339
1.29k
    {
340
1.29k
        f_fps = var_CreateGetFloat( p_demux, psz_fpsvar );
341
1.29k
        free( psz_fpsvar );
342
1.29k
    }
343
344
1.29k
    if( f_fps )
345
0
    {
346
0
        if ( f_fps < 0.001f ) f_fps = 0.001f;
347
0
        p_sys->frame_rate_den = 1000;
348
0
        p_sys->frame_rate_num = 2000 * f_fps;
349
0
        date_Init( &p_sys->feed_dts, p_sys->frame_rate_num, p_sys->frame_rate_den );
350
0
    }
351
1.29k
    else
352
1.29k
        date_Init( &p_sys->feed_dts, 25000, 1000 );
353
1.29k
    date_Set( &p_sys->feed_dts, VLC_TICK_0 );
354
1.29k
    p_sys->output_dts = p_sys->feed_dts;
355
356
    /* Load the mpegvideo packetizer */
357
1.29k
    es_format_Init( &fmt, VIDEO_ES, i_codec );
358
1.29k
    if( f_fps )
359
0
    {
360
0
        fmt.video.i_frame_rate = p_sys->feed_dts.i_divider_num;
361
0
        fmt.video.i_frame_rate_base = p_sys->feed_dts.i_divider_den;
362
0
    }
363
1.29k
    p_sys->p_packetizer = demux_PacketizerNew( VLC_OBJECT(p_demux), &fmt, psz_module );
364
1.29k
    if( !p_sys->p_packetizer )
365
0
    {
366
0
        free( p_sys );
367
0
        return VLC_EGENERIC;
368
0
    }
369
370
1.29k
    return VLC_SUCCESS;
371
1.29k
}
372
373
/*****************************************************************************
374
 * Open: initializes demux structures
375
 *****************************************************************************/
376
static int OpenH264( vlc_object_t * p_this )
377
34
{
378
34
    h264_probe_ctx_t ctx = { 0, 0 };
379
34
    const char *rgi_psz_ext[] = { ".h264", ".264", ".bin", ".bit", ".raw", NULL };
380
34
    const char *rgi_psz_mime[] = { "video/H264", "video/h264", "video/avc", NULL };
381
382
34
    return GenericOpen( (demux_t*)p_this, "h264", VLC_CODEC_H264, ProbeH264,
383
34
                        &ctx, rgi_psz_ext, rgi_psz_mime );
384
34
}
385
386
static int OpenHEVC( vlc_object_t * p_this )
387
1.27k
{
388
1.27k
    hevc_probe_ctx_t ctx = { 0, 0, 0 };
389
1.27k
    const char *rgi_psz_ext[] = { ".h265", ".265", ".hevc", ".bin", ".bit", ".raw", NULL };
390
1.27k
    const char *rgi_psz_mime[] = { "video/h265", "video/hevc", "video/HEVC", NULL };
391
392
1.27k
    return GenericOpen( (demux_t*)p_this, "hevc", VLC_CODEC_HEVC, ProbeHEVC,
393
1.27k
                        &ctx, rgi_psz_ext, rgi_psz_mime );
394
1.27k
}
395
396
/*****************************************************************************
397
 * Close: frees unused data
398
 *****************************************************************************/
399
static void Close( vlc_object_t * p_this )
400
1.29k
{
401
1.29k
    demux_t     *p_demux = (demux_t*)p_this;
402
1.29k
    demux_sys_t *p_sys = p_demux->p_sys;
403
404
1.29k
    demux_PacketizerDestroy( p_sys->p_packetizer );
405
1.29k
    free( p_sys );
406
1.29k
}
407
408
/*****************************************************************************
409
 * Demux: reads and demuxes data packets
410
 *****************************************************************************
411
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
412
 *****************************************************************************/
413
static int Demux( demux_t *p_demux)
414
62.6k
{
415
62.6k
    demux_sys_t *p_sys = p_demux->p_sys;
416
62.6k
    block_t *p_block_in, *p_block_out;
417
62.6k
    bool b_eof = false;
418
419
62.6k
    p_block_in = vlc_stream_Block( p_demux->s, H26X_PACKET_SIZE );
420
62.6k
    if( p_block_in == NULL )
421
1.29k
    {
422
1.29k
        b_eof = true;
423
1.29k
    }
424
61.3k
    else
425
61.3k
    {
426
61.3k
        p_block_in->i_dts = date_Get( &p_sys->feed_dts );
427
61.3k
    }
428
429
1.85M
    while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer,
430
1.85M
                                                             p_block_in ? &p_block_in : NULL )) )
431
1.79M
    {
432
3.58M
        while( p_block_out )
433
1.79M
        {
434
1.79M
            block_t *p_next = p_block_out->p_next;
435
1.79M
            p_block_out->p_next = NULL;
436
437
1.79M
            if( p_sys->p_packetizer->fmt_out.video.i_frame_rate_base &&
438
1.76M
                p_sys->p_packetizer->fmt_out.video.i_frame_rate_base != p_sys->frame_rate_den &&
439
833
                p_sys->p_packetizer->fmt_out.video.i_frame_rate &&
440
833
                p_sys->p_packetizer->fmt_out.video.i_frame_rate_base != p_sys->frame_rate_num )
441
833
            {
442
833
                p_sys->frame_rate_num = p_sys->p_packetizer->fmt_out.video.i_frame_rate;
443
833
                p_sys->frame_rate_den = p_sys->p_packetizer->fmt_out.video.i_frame_rate_base;
444
833
                date_Change( &p_sys->feed_dts, 2 * p_sys->frame_rate_num, p_sys->frame_rate_den );
445
833
                date_Change( &p_sys->output_dts, 2 * p_sys->frame_rate_num, p_sys->frame_rate_den );
446
833
                msg_Dbg( p_demux, "using %.2f fps", (double) p_sys->frame_rate_num / p_sys->frame_rate_den );
447
833
            }
448
449
            /* we only want to use the pts-dts offset and length from packetizer */
450
1.79M
            vlc_tick_t dtsdiff = p_block_out->i_pts > p_block_out->i_dts
451
1.79M
                               ? p_block_out->i_pts - p_block_out->i_dts
452
1.79M
                               : 0;
453
            /* Always start frame N=1 so we get PCR on N=0 */
454
1.79M
            date_t dtsdate = p_sys->output_dts;
455
1.79M
            vlc_tick_t dts = date_Increment( &dtsdate, 2 );
456
457
1.79M
            p_block_out->i_dts = dts;
458
1.79M
            if( p_block_out->i_pts != VLC_TICK_INVALID )
459
236
                p_block_out->i_pts = dts + dtsdiff;
460
461
1.79M
            if( p_block_in )
462
1.75M
            {
463
1.75M
                p_block_in->i_dts = date_Get( &p_sys->feed_dts );
464
1.75M
                p_block_in->i_pts = VLC_TICK_INVALID;
465
1.75M
            }
466
467
1.79M
            if( p_sys->p_es == NULL )
468
890
            {
469
890
                p_sys->p_packetizer->fmt_out.b_packetized = true;
470
890
                p_sys->p_packetizer->fmt_out.i_id = 0;
471
890
                p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out );
472
890
                if( !p_sys->p_es )
473
0
                {
474
0
                    block_ChainRelease( p_block_out );
475
0
                    return VLC_DEMUXER_EOF;
476
0
                }
477
890
            }
478
479
            /* h264 packetizer does merge multiple NAL into AU, but slice flag persists */
480
1.79M
            bool frame = p_block_out->i_flags & BLOCK_FLAG_TYPE_MASK;
481
1.79M
            const vlc_tick_t i_frame_length = p_block_out->i_length;
482
483
            /* first output */
484
1.79M
            if( date_Get( &p_sys->output_dts ) == VLC_TICK_0 )
485
1.14k
                es_out_SetPCR( p_demux->out, date_Get( &p_sys->output_dts ) );
486
487
1.79M
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
488
489
1.79M
            vlc_tick_t pcr = b_eof ? dts : date_Get( &p_sys->output_dts );
490
491
1.79M
            if( frame )
492
1.75M
            {
493
1.75M
                unsigned i_nb_fields;
494
1.75M
                if( i_frame_length > 0 )
495
703k
                {
496
703k
                    i_nb_fields = round( (double)i_frame_length * 2 * p_sys->frame_rate_num /
497
703k
                                  ( p_sys->frame_rate_den * CLOCK_FREQ ) );
498
703k
                }
499
1.05M
                else i_nb_fields = 2;
500
1.75M
                if( i_nb_fields <= 6 ) /* in the legit range */
501
1.75M
                {
502
1.75M
                    date_Increment( &p_sys->output_dts, i_nb_fields );
503
1.75M
                }
504
1.75M
            }
505
506
1.79M
            es_out_SetPCR( p_demux->out, pcr );
507
508
1.79M
            p_block_out = p_next;
509
1.79M
        }
510
1.79M
    }
511
62.6k
    return (b_eof) ? VLC_DEMUXER_EOF : VLC_DEMUXER_SUCCESS;
512
62.6k
}
513
514
/*****************************************************************************
515
 * Control:
516
 *****************************************************************************/
517
static int Control( demux_t *p_demux, int i_query, va_list args )
518
0
{
519
    /* demux_sys_t *p_sys  = p_demux->p_sys; */
520
    /* FIXME calculate the bitrate */
521
0
    if( i_query == DEMUX_SET_TIME )
522
0
        return VLC_EGENERIC;
523
0
    else
524
0
        return demux_vaControlHelper( p_demux->s,
525
0
                                       0, -1,
526
0
                                       0, 1, i_query, args );
527
0
}