Coverage Report

Created: 2025-12-31 07:55

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
104
vlc_module_begin ()
50
52
    set_shortname( "H264")
51
52
    set_subcategory( SUBCAT_INPUT_DEMUX )
52
52
    set_description( N_("H264 video demuxer" ) )
53
52
    set_capability( "demux", 8 )
54
52
    set_section( N_("H264 video demuxer" ), NULL )
55
52
    add_float( "h264-fps", 0.0, FPS_TEXT, FPS_LONGTEXT )
56
52
    set_callbacks( OpenH264, Close )
57
52
    add_shortcut( "h264" )
58
52
    add_file_extension("h264")
59
60
52
    add_submodule()
61
52
        set_shortname( "HEVC")
62
52
        set_subcategory( SUBCAT_INPUT_DEMUX )
63
52
        set_description( N_("HEVC/H.265 video demuxer" ) )
64
52
        set_capability( "demux", 8 )
65
52
        set_section( N_("HEVC/H.265 video demuxer" ), NULL )
66
52
        add_float( "hevc-fps", 0.0, FPS_TEXT, FPS_LONGTEXT )
67
52
        set_callbacks( OpenHEVC, Close )
68
52
        add_shortcut( "hevc", "h265" )
69
70
52
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
129k
#define H26X_PACKET_SIZE 2048
91
18.3k
#define H26X_PEEK_CHUNK  (H26X_PACKET_SIZE * 4)
92
7.07M
#define H26X_MIN_PEEK    (4 + 7 + 10)
93
6.31k
#define H26X_MAX_PEEK    (H26X_PEEK_CHUNK * 8) /* max data to check */
94
10.9k
#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.72k
{
114
1.72k
    hevc_probe_ctx_t *p_ctx = (hevc_probe_ctx_t *) p_priv;
115
116
1.72k
    if( i_peek < 2 )
117
0
        return -1;
118
119
1.72k
    if( p_peek[0] & 0x80 )
120
15
        return -1;
121
122
1.70k
    const uint8_t i_type = hevc_getNALType( p_peek );
123
1.70k
    const uint8_t i_layer = hevc_getNALLayer( p_peek );
124
125
1.70k
   if ( i_type == HEVC_NAL_VPS ) /* VPS */
126
399
   {
127
399
       if( i_layer != 0 || i_peek < 6 ||
128
362
           p_peek[4] != 0xFF || p_peek[5] != 0xFF ) /* Check reserved bits */
129
195
           return -1;
130
204
       p_ctx->b_vps = true;
131
204
       return 0;
132
399
   }
133
1.31k
   else if( i_type == HEVC_NAL_SPS )  /* SPS */
134
416
   {
135
416
       if( i_layer != 0 )
136
72
           return -1;
137
344
       p_ctx->b_sps = true;
138
344
       return 0;
139
416
   }
140
894
   else if( i_type == HEVC_NAL_PPS )  /* PPS */
141
491
   {
142
491
       if( i_layer != 0 )
143
89
           return -1;
144
402
       p_ctx->b_pps = true;
145
402
       return 0;
146
491
   }
147
403
   else if( i_type >= HEVC_NAL_BLA_W_LP && i_type <= HEVC_NAL_CRA ) /* Key Frame */
148
185
   {
149
185
        if( p_ctx->b_vps && p_ctx->b_sps && p_ctx->b_pps && i_layer == 0 )
150
7
            return 1;
151
185
   }
152
218
   else if( i_type == HEVC_NAL_AUD ) /* AU */
153
86
   {
154
86
        if( i_peek < H26X_MIN_PEEK ||
155
85
            p_peek[4] != 0 || p_peek[5] != 0 ) /* Must prefix another NAL */
156
27
            return -1;
157
86
   }
158
132
   else if( i_type == HEVC_NAL_PREF_SEI ) /* Prefix SEI */
159
47
   {
160
47
       if( p_peek[2] == 0xFF ) /* empty SEI */
161
2
           return -1;
162
47
   }
163
85
   else
164
85
   {
165
85
       return -1; /* See 7.4.2.4.4 for sequence order */
166
85
   }
167
168
282
    return 0; /* Probe more */
169
1.70k
}
170
171
static int ProbeH264( const uint8_t *p_peek, size_t i_peek, void *p_priv )
172
3.70k
{
173
3.70k
    h264_probe_ctx_t *p_ctx = (h264_probe_ctx_t *) p_priv;
174
175
3.70k
    if( i_peek < 1 )
176
0
        return -1;
177
3.70k
    const uint8_t i_nal_type = p_peek[0] & 0x1F;
178
3.70k
    const uint8_t i_ref_idc = p_peek[0] & 0x60;
179
180
3.70k
    if( (p_peek[0] & 0x80) ) /* reserved 0 */
181
87
        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
3.61k
    if( i_nal_type == H264_NAL_SPS )
187
1.70k
    {
188
1.70k
        if( i_ref_idc == 0 || i_peek < 3 ||
189
1.66k
           (p_peek[2] & 0x03) /* reserved 0 bits */ )
190
320
            return -1;
191
1.38k
        p_ctx->b_sps = true;
192
1.38k
    }
193
1.91k
    else if( i_nal_type == H264_NAL_PPS )
194
878
    {
195
878
        if( i_ref_idc == 0 )
196
5
            return -1;
197
873
        p_ctx->b_pps = true;
198
873
    }
199
1.03k
    else if( i_nal_type == H264_NAL_SLICE_IDR )
200
73
    {
201
73
        if( i_ref_idc == 0 || ! p_ctx->b_pps || ! p_ctx->b_sps )
202
29
            return -1;
203
44
        else
204
44
            return 1;
205
73
    }
206
961
    else if( i_nal_type == H264_NAL_AU_DELIMITER )
207
53
    {
208
53
        if( i_ref_idc )
209
1
            return -1;
210
53
    }
211
908
    else if ( i_nal_type == H264_NAL_SEI )
212
555
    {
213
555
        if( i_ref_idc )
214
6
            return -1;
215
555
    }
216
     /* 7.4.1.1 */
217
353
    else if ( i_nal_type == H264_NAL_SPS_EXT ||
218
339
              i_nal_type == H264_NAL_SUBSET_SPS )
219
62
    {
220
62
        if( i_ref_idc == 0 || !p_ctx->b_sps )
221
4
            return -1;
222
62
    }
223
291
    else if( i_nal_type == H264_NAL_PREFIX )
224
17
    {
225
17
        if( i_ref_idc == 0 || !p_ctx->b_pps || !p_ctx->b_sps )
226
6
            return -1;
227
17
    }
228
274
    else return -1; /* see 7.4.1.2.3 for sequence */
229
230
2.92k
    return 0;
231
3.61k
}
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
0
{
239
0
    while( *pp_psz )
240
0
    {
241
0
        if( pf_check( p_demux, *pp_psz ) )
242
0
            return true;
243
0
        pp_psz++;
244
0
    }
245
0
    return false;
246
0
}
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
17.2k
{
255
17.2k
    demux_sys_t *p_sys;
256
17.2k
    const uint8_t *p_peek;
257
17.2k
    es_format_t fmt;
258
17.2k
    uint8_t annexb_startcode[] = {0,0,0,1};
259
17.2k
    int i_ret = 0;
260
261
    /* Restrict by type first */
262
17.2k
    if( !p_demux->obj.force &&
263
0
        !check_Property( p_demux, pp_psz_exts, demux_IsPathExtension ) &&
264
0
        !check_Property( p_demux, pp_psz_mimes, demux_IsContentType ) )
265
0
    {
266
0
        return VLC_EGENERIC;
267
0
    }
268
269
    /* First check for first AnnexB header */
270
17.2k
    if( vlc_stream_Peek( p_demux->s, &p_peek, H26X_MIN_PEEK ) == H26X_MIN_PEEK &&
271
14.3k
       !memcmp( p_peek, annexb_startcode, 4 ) )
272
2.20k
    {
273
2.20k
        ssize_t i_peek = H26X_MIN_PEEK;
274
2.20k
        size_t i_peek_target = H26X_MIN_PEEK;
275
2.20k
        ssize_t i_probe_offset = 4;
276
2.20k
        const uint8_t *p_probe = p_peek;
277
2.20k
        bool b_synced = true;
278
2.20k
        unsigned i_bitflow = 0;
279
280
10.9k
        for( unsigned i=0; i<H26X_NAL_COUNT; i++ )
281
10.0k
        {
282
3.51M
            while( !b_synced )
283
3.51M
            {
284
3.51M
                if( i_probe_offset + H26X_MIN_PEEK >= i_peek &&
285
6.31k
                    i_peek_target + H26X_PEEK_CHUNK <= H26X_MAX_PEEK )
286
5.71k
                {
287
5.71k
                    i_peek_target += H26X_PEEK_CHUNK;
288
5.71k
                    i_peek = vlc_stream_Peek( p_demux->s, &p_peek, i_peek_target );
289
5.71k
                }
290
291
3.51M
                if( i_probe_offset + H26X_MIN_PEEK >= i_peek )
292
4.60k
                    break;
293
294
3.50M
                p_probe = &p_peek[i_probe_offset];
295
3.50M
                i_bitflow = (i_bitflow << 1) | (!p_probe[0]);
296
                /* Check for annexB */
297
3.50M
                if( p_probe[0] == 0x01 && ((i_bitflow & 0x06) == 0x06) )
298
3.22k
                    b_synced = true;
299
300
3.50M
                i_probe_offset++;
301
3.50M
            }
302
303
10.0k
            if( b_synced )
304
5.42k
            {
305
5.42k
                p_probe = &p_peek[i_probe_offset];
306
5.42k
                i_ret = pf_probe( p_probe, (size_t)(i_peek - i_probe_offset), p_ctx );
307
5.42k
            }
308
309
10.0k
            if( i_ret != 0 )
310
1.26k
                break;
311
312
8.75k
            i_probe_offset += 4;
313
8.75k
            b_synced = false;
314
8.75k
        }
315
2.20k
    }
316
317
17.2k
    if( i_ret < 1 )
318
17.2k
    {
319
17.2k
        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
17.2k
        msg_Err( p_demux, "this doesn't look like a %s ES stream, "
326
17.2k
                 "continuing anyway", psz_module );
327
17.2k
    }
328
329
17.2k
    p_demux->pf_demux  = Demux;
330
17.2k
    p_demux->pf_control= Control;
331
17.2k
    p_demux->p_sys     = p_sys = malloc( sizeof( demux_sys_t ) );
332
17.2k
    p_sys->p_es        = NULL;
333
17.2k
    p_sys->frame_rate_num = 0;
334
17.2k
    p_sys->frame_rate_den = 0;
335
336
17.2k
    float f_fps = 0;
337
17.2k
    char *psz_fpsvar;
338
17.2k
    if( asprintf( &psz_fpsvar, "%s-fps", psz_module ) != -1 )
339
17.2k
    {
340
17.2k
        f_fps = var_CreateGetFloat( p_demux, psz_fpsvar );
341
17.2k
        free( psz_fpsvar );
342
17.2k
    }
343
344
17.2k
    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
17.2k
    else
352
17.2k
        date_Init( &p_sys->feed_dts, 25000, 1000 );
353
17.2k
    date_Set( &p_sys->feed_dts, VLC_TICK_0 );
354
17.2k
    p_sys->output_dts = p_sys->feed_dts;
355
356
    /* Load the mpegvideo packetizer */
357
17.2k
    es_format_Init( &fmt, VIDEO_ES, i_codec );
358
17.2k
    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
17.2k
    p_sys->p_packetizer = demux_PacketizerNew( VLC_OBJECT(p_demux), &fmt, psz_module );
364
17.2k
    if( !p_sys->p_packetizer )
365
0
    {
366
0
        free( p_sys );
367
0
        return VLC_EGENERIC;
368
0
    }
369
370
17.2k
    return VLC_SUCCESS;
371
17.2k
}
372
373
/*****************************************************************************
374
 * Open: initializes demux structures
375
 *****************************************************************************/
376
static int OpenH264( vlc_object_t * p_this )
377
9.22k
{
378
9.22k
    h264_probe_ctx_t ctx = { 0, 0 };
379
9.22k
    const char *rgi_psz_ext[] = { ".h264", ".264", ".bin", ".bit", ".raw", NULL };
380
9.22k
    const char *rgi_psz_mime[] = { "video/H264", "video/h264", "video/avc", NULL };
381
382
9.22k
    return GenericOpen( (demux_t*)p_this, "h264", VLC_CODEC_H264, ProbeH264,
383
9.22k
                        &ctx, rgi_psz_ext, rgi_psz_mime );
384
9.22k
}
385
386
static int OpenHEVC( vlc_object_t * p_this )
387
8.02k
{
388
8.02k
    hevc_probe_ctx_t ctx = { 0, 0, 0 };
389
8.02k
    const char *rgi_psz_ext[] = { ".h265", ".265", ".hevc", ".bin", ".bit", ".raw", NULL };
390
8.02k
    const char *rgi_psz_mime[] = { "video/h265", "video/hevc", "video/HEVC", NULL };
391
392
8.02k
    return GenericOpen( (demux_t*)p_this, "hevc", VLC_CODEC_HEVC, ProbeHEVC,
393
8.02k
                        &ctx, rgi_psz_ext, rgi_psz_mime );
394
8.02k
}
395
396
/*****************************************************************************
397
 * Close: frees unused data
398
 *****************************************************************************/
399
static void Close( vlc_object_t * p_this )
400
17.2k
{
401
17.2k
    demux_t     *p_demux = (demux_t*)p_this;
402
17.2k
    demux_sys_t *p_sys = p_demux->p_sys;
403
404
17.2k
    demux_PacketizerDestroy( p_sys->p_packetizer );
405
17.2k
    free( p_sys );
406
17.2k
}
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
110k
{
415
110k
    demux_sys_t *p_sys = p_demux->p_sys;
416
110k
    block_t *p_block_in, *p_block_out;
417
110k
    bool b_eof = false;
418
419
110k
    p_block_in = vlc_stream_Block( p_demux->s, H26X_PACKET_SIZE );
420
110k
    if( p_block_in == NULL )
421
17.2k
    {
422
17.2k
        b_eof = true;
423
17.2k
    }
424
93.5k
    else
425
93.5k
    {
426
93.5k
        p_block_in->i_dts = date_Get( &p_sys->feed_dts );
427
93.5k
    }
428
429
1.09M
    while( (p_block_out = p_sys->p_packetizer->pf_packetize( p_sys->p_packetizer,
430
1.09M
                                                             p_block_in ? &p_block_in : NULL )) )
431
980k
    {
432
1.96M
        while( p_block_out )
433
980k
        {
434
980k
            block_t *p_next = p_block_out->p_next;
435
980k
            p_block_out->p_next = NULL;
436
437
980k
            if( p_sys->p_packetizer->fmt_out.video.i_frame_rate_base &&
438
971k
                p_sys->p_packetizer->fmt_out.video.i_frame_rate_base != p_sys->frame_rate_den &&
439
3.71k
                p_sys->p_packetizer->fmt_out.video.i_frame_rate &&
440
3.71k
                p_sys->p_packetizer->fmt_out.video.i_frame_rate_base != p_sys->frame_rate_num )
441
3.71k
            {
442
3.71k
                p_sys->frame_rate_num = p_sys->p_packetizer->fmt_out.video.i_frame_rate;
443
3.71k
                p_sys->frame_rate_den = p_sys->p_packetizer->fmt_out.video.i_frame_rate_base;
444
3.71k
                date_Change( &p_sys->feed_dts, 2 * p_sys->frame_rate_num, p_sys->frame_rate_den );
445
3.71k
                date_Change( &p_sys->output_dts, 2 * p_sys->frame_rate_num, p_sys->frame_rate_den );
446
3.71k
                msg_Dbg( p_demux, "using %.2f fps", (double) p_sys->frame_rate_num / p_sys->frame_rate_den );
447
3.71k
            }
448
449
            /* we only want to use the pts-dts offset and length from packetizer */
450
980k
            vlc_tick_t dtsdiff = p_block_out->i_pts > p_block_out->i_dts
451
980k
                               ? p_block_out->i_pts - p_block_out->i_dts
452
980k
                               : 0;
453
            /* Always start frame N=1 so we get PCR on N=0 */
454
980k
            date_t dtsdate = p_sys->output_dts;
455
980k
            vlc_tick_t dts = date_Increment( &dtsdate, 2 );
456
457
980k
            p_block_out->i_dts = dts;
458
980k
            if( p_block_out->i_pts != VLC_TICK_INVALID )
459
114k
                p_block_out->i_pts = dts + dtsdiff;
460
461
980k
            if( p_block_in )
462
975k
            {
463
975k
                p_block_in->i_dts = date_Get( &p_sys->feed_dts );
464
975k
                p_block_in->i_pts = VLC_TICK_INVALID;
465
975k
            }
466
467
980k
            if( p_sys->p_es == NULL )
468
3.79k
            {
469
3.79k
                p_sys->p_packetizer->fmt_out.b_packetized = true;
470
3.79k
                p_sys->p_packetizer->fmt_out.i_id = 0;
471
3.79k
                p_sys->p_es = es_out_Add( p_demux->out, &p_sys->p_packetizer->fmt_out );
472
3.79k
                if( !p_sys->p_es )
473
0
                {
474
0
                    block_ChainRelease( p_block_out );
475
0
                    return VLC_DEMUXER_EOF;
476
0
                }
477
3.79k
            }
478
479
            /* h264 packetizer does merge multiple NAL into AU, but slice flag persists */
480
980k
            bool frame = p_block_out->i_flags & BLOCK_FLAG_TYPE_MASK;
481
980k
            const vlc_tick_t i_frame_length = p_block_out->i_length;
482
483
            /* first output */
484
980k
            if( date_Get( &p_sys->output_dts ) == VLC_TICK_0 )
485
6.66k
                es_out_SetPCR( p_demux->out, date_Get( &p_sys->output_dts ) );
486
487
980k
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
488
489
980k
            vlc_tick_t pcr = b_eof ? dts : date_Get( &p_sys->output_dts );
490
491
980k
            if( frame )
492
969k
            {
493
969k
                unsigned i_nb_fields;
494
969k
                if( i_frame_length > 0 )
495
858k
                {
496
858k
                    i_nb_fields = round( (double)i_frame_length * 2 * p_sys->frame_rate_num /
497
858k
                                  ( p_sys->frame_rate_den * CLOCK_FREQ ) );
498
858k
                }
499
111k
                else i_nb_fields = 2;
500
969k
                if( i_nb_fields <= 6 ) /* in the legit range */
501
969k
                {
502
969k
                    date_Increment( &p_sys->output_dts, i_nb_fields );
503
969k
                }
504
969k
            }
505
506
980k
            es_out_SetPCR( p_demux->out, pcr );
507
508
980k
            p_block_out = p_next;
509
980k
        }
510
980k
    }
511
110k
    return (b_eof) ? VLC_DEMUXER_EOF : VLC_DEMUXER_SUCCESS;
512
110k
}
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
}