Coverage Report

Created: 2026-03-07 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/hxxx_common.c
Line
Count
Source
1
/*****************************************************************************
2
 * hxxx_common.c: AVC/HEVC packetizers shared code
3
 *****************************************************************************
4
 * Copyright (C) 2001-2015 VLC authors and VideoLAN
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19
 *****************************************************************************/
20
#ifdef HAVE_CONFIG_H
21
# include "config.h"
22
#endif
23
24
#include <vlc_common.h>
25
#include <vlc_block.h>
26
#include <vlc_codec.h>
27
28
#include "hxxx_common.h"
29
#include "../codec/cc.h"
30
31
/****************************************************************************
32
 * Closed captions handling
33
 ****************************************************************************/
34
struct cc_storage_t
35
{
36
    uint32_t i_flags;
37
    vlc_tick_t i_dts;
38
    vlc_tick_t i_pts;
39
    cc_data_t current;
40
    cc_data_t next;
41
};
42
43
cc_storage_t * cc_storage_new( void )
44
35.1k
{
45
35.1k
    cc_storage_t *p_ccs = malloc( sizeof(*p_ccs) );
46
35.1k
    if( likely(p_ccs) )
47
35.1k
    {
48
35.1k
        p_ccs->i_pts = VLC_TICK_INVALID;
49
35.1k
        p_ccs->i_dts = VLC_TICK_INVALID;
50
35.1k
        p_ccs->i_flags = 0;
51
35.1k
        cc_Init( &p_ccs->current );
52
35.1k
        cc_Init( &p_ccs->next );
53
35.1k
    }
54
35.1k
    return p_ccs;
55
35.1k
}
56
57
void cc_storage_delete( cc_storage_t *p_ccs )
58
35.1k
{
59
35.1k
    cc_Exit( &p_ccs->current );
60
35.1k
    cc_Exit( &p_ccs->next );
61
35.1k
    free( p_ccs );
62
35.1k
}
63
64
void cc_storage_reset( cc_storage_t *p_ccs )
65
140k
{
66
140k
    cc_Flush( &p_ccs->next );
67
140k
}
68
69
void cc_storage_append( cc_storage_t *p_ccs, bool b_top_field_first,
70
                        const uint8_t *p_buf, size_t i_buf )
71
41.8k
{
72
41.8k
    cc_Extract( &p_ccs->next, CC_PAYLOAD_GA94, b_top_field_first, p_buf, i_buf );
73
41.8k
}
74
75
void cc_storage_commit( cc_storage_t *p_ccs, block_t *p_pic )
76
2.71M
{
77
2.71M
    p_ccs->i_pts = p_pic->i_pts;
78
2.71M
    p_ccs->i_dts = p_pic->i_dts;
79
2.71M
    p_ccs->i_flags = p_pic->i_flags;
80
2.71M
    p_ccs->current = p_ccs->next;
81
2.71M
    cc_Flush( &p_ccs->next );
82
2.71M
}
83
84
block_t * cc_storage_get_current( cc_storage_t *p_ccs, decoder_cc_desc_t *p_desc )
85
1.34M
{
86
1.34M
    block_t *p_block;
87
88
1.34M
    if( !p_ccs->current.b_reorder && p_ccs->current.i_data <= 0 )
89
1.32M
        return NULL;
90
91
26.3k
    p_block = block_Alloc( p_ccs->current.i_data);
92
26.3k
    if( p_block )
93
26.3k
    {
94
26.3k
        memcpy( p_block->p_buffer, p_ccs->current.p_data, p_ccs->current.i_data );
95
26.3k
        p_block->i_dts =
96
26.3k
        p_block->i_pts = p_ccs->current.b_reorder ? p_ccs->i_pts : p_ccs->i_dts;
97
26.3k
        p_block->i_flags = p_ccs->i_flags & BLOCK_FLAG_TYPE_MASK;
98
99
26.3k
        p_desc->i_608_channels = p_ccs->current.i_608channels;
100
26.3k
        p_desc->i_708_channels = p_ccs->current.i_708channels;
101
26.3k
        p_desc->i_reorder_depth = p_ccs->current.b_reorder ? 4 : -1;
102
26.3k
    }
103
26.3k
    cc_Flush( &p_ccs->current );
104
105
26.3k
    return p_block;
106
1.34M
}
107
108
/****************************************************************************
109
 * PacketizeXXC1: Takes VCL blocks of data and creates annexe B type NAL stream
110
 * Will always use 4 byte 0 0 0 1 startcodes
111
 * Will prepend a SPS and PPS before each keyframe
112
 ****************************************************************************/
113
block_t *PacketizeXXC1( void *p_private, struct vlc_logger *logger,
114
                        uint8_t i_nal_length_size, block_t **pp_block,
115
                        pf_annexb_nal_parse pf_nal_parser,
116
                        pf_annexb_nal_drain pf_nal_drain )
117
83
{
118
83
    block_t       *p_block;
119
83
    block_t       *p_ret = NULL;
120
83
    uint8_t       *p;
121
122
83
    if( !pp_block )
123
44
        return pf_nal_drain ? pf_nal_drain( p_private ) : NULL;
124
125
39
    if( !*pp_block )
126
0
        return NULL;
127
39
    if( (*pp_block)->i_flags&(BLOCK_FLAG_CORRUPTED) )
128
0
    {
129
0
        block_Release( *pp_block );
130
0
        return NULL;
131
0
    }
132
133
39
    p_block = *pp_block;
134
39
    *pp_block = NULL;
135
136
39
    for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
137
39
    {
138
39
        bool b_dummy;
139
39
        int i_size = 0;
140
39
        int i;
141
142
39
        if( &p_block->p_buffer[p_block->i_buffer] - p < i_nal_length_size )
143
0
            break;
144
145
89
        for( i = 0; i < i_nal_length_size; i++ )
146
50
        {
147
50
            i_size = (i_size << 8) | (*p++);
148
50
        }
149
150
39
        if( i_size <= 0 ||
151
38
            i_size > ( p_block->p_buffer + p_block->i_buffer - p ) )
152
35
        {
153
35
            vlc_error( logger, "Broken frame : size %d is too big", i_size );
154
35
            break;
155
35
        }
156
157
        /* Convert AVC to AnnexB */
158
4
        block_t *p_nal;
159
        /* If data exactly match remaining bytes (1 NAL only or trailing one) */
160
4
        if( i_size == p_block->p_buffer + p_block->i_buffer - p )
161
4
        {
162
4
            p_block->i_buffer = i_size;
163
4
            p_block->p_buffer = p;
164
4
            p_nal = block_Realloc( p_block, 4, i_size );
165
4
            if( p_nal )
166
4
                p_block = NULL;
167
4
        }
168
0
        else
169
0
        {
170
0
            p_nal = block_Alloc( 4 + i_size );
171
0
            if( p_nal )
172
0
            {
173
0
                p_nal->i_dts = p_block->i_dts;
174
0
                p_nal->i_pts = p_block->i_pts;
175
                /* Copy nalu */
176
0
                memcpy( &p_nal->p_buffer[4], p, i_size );
177
0
            }
178
0
            p += i_size;
179
0
        }
180
181
4
        if( !p_nal )
182
0
            break;
183
184
        /* Add start code */
185
4
        p_nal->p_buffer[0] = 0x00;
186
4
        p_nal->p_buffer[1] = 0x00;
187
4
        p_nal->p_buffer[2] = 0x00;
188
4
        p_nal->p_buffer[3] = 0x01;
189
190
        /* Parse the NAL */
191
4
        block_t *p_pic;
192
4
        if( ( p_pic = pf_nal_parser( p_private, &b_dummy, p_nal ) ) )
193
0
        {
194
0
            block_ChainAppend( &p_ret, p_pic );
195
0
        }
196
197
4
        if( !p_block )
198
4
            break;
199
4
    }
200
201
39
    if( p_block )
202
35
        block_Release( p_block );
203
204
39
    return p_ret;
205
39
}