Coverage Report

Created: 2026-12-01 08:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/packetizer/startcode_helper.h
Line
Count
Source
1
/*****************************************************************************
2
 * startcode_helper.h: Startcodes helpers
3
 *****************************************************************************
4
 * Copyright (C) 2016 VideoLAN Authors
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
#ifndef VLC_STARTCODE_HELPER_H_
21
#define VLC_STARTCODE_HELPER_H_
22
23
#include <vlc_cpu.h>
24
25
#ifdef CAN_COMPILE_SSE2
26
#  if defined __has_attribute
27
#    if __has_attribute(__vector_size__)
28
#      define HAS_ATTRIBUTE_VECTORSIZE
29
#    endif
30
#  endif
31
32
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
33
    typedef unsigned char v16qu __attribute__((__vector_size__(16)));
34
#  endif
35
#endif
36
37
/* Looks up efficiently for an AnnexB startcode 0x00 0x00 0x01
38
 * by using a 4 times faster trick than single byte lookup. */
39
40
2.04G
#define TRY_MATCH(p,a) {\
41
2.04G
     if (p[a+1] == 0) {\
42
1.35G
            if (p[a+0] == 0 && p[a+2] == 1)\
43
1.35G
                return a+p;\
44
1.35G
            if (p[a+2] == 0 && p[a+3] == 1)\
45
1.35G
                return a+p+1;\
46
1.35G
        }\
47
2.04G
        if (p[a+3] == 0) {\
48
1.30G
            if (p[a+2] == 0 && p[a+4] == 1)\
49
1.30G
                return a+p+2;\
50
1.30G
            if (p[a+4] == 0 && p[a+5] == 1)\
51
1.30G
                return a+p+3;\
52
1.30G
        }\
53
2.03G
    }
54
55
#ifdef CAN_COMPILE_SSE2
56
57
__attribute__ ((__target__ ("sse2")))
58
static inline const uint8_t * startcode_FindAnnexB_SSE2( const uint8_t *p, const uint8_t *end )
59
81.9M
{
60
    /* First align to 16 */
61
    /* Skipping this step and doing unaligned loads isn't faster */
62
81.9M
    const uint8_t *alignedend = p + 16 - ((intptr_t)p & 15);
63
617M
    for (end -= 3; p < alignedend && p <= end; p++) {
64
566M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
65
30.9M
            return p;
66
566M
    }
67
68
51.0M
    if( p == end )
69
127k
        return NULL;
70
71
50.9M
    alignedend = end - ((intptr_t) end & 15);
72
50.9M
    if( alignedend > p )
73
33.7M
    {
74
33.7M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
75
33.7M
        const v16qu zeros = { 0 };
76
33.7M
#  endif
77
78
2.14G
        for( ; p < alignedend; p += 16)
79
2.12G
        {
80
2.12G
            uint32_t match;
81
2.12G
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
82
2.12G
            asm volatile(
83
2.12G
                "movdqa   0(%[v]),   %%xmm0\n"
84
2.12G
                "pcmpeqb %[czero],   %%xmm0\n"
85
2.12G
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
86
2.12G
                : [match]"=r"(match)
87
2.12G
                : [v]"r"(p), [czero]"x"(zeros)
88
2.12G
                : "xmm0"
89
2.12G
            );
90
#  else
91
            asm volatile(
92
                "movdqa   0(%[v]),   %%xmm0\n"
93
                "pxor      %%xmm1,   %%xmm1\n"
94
                "pcmpeqb   %%xmm1,   %%xmm0\n"
95
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
96
                : [match]"=r"(match)
97
                : [v]"r"(p)
98
                : "xmm0", "xmm1"
99
            );
100
#  endif
101
2.12G
            if( match & 0x000F )
102
2.12G
                TRY_MATCH(p, 0);
103
2.12G
            if( match & 0x00F0 )
104
2.11G
                TRY_MATCH(p, 4);
105
2.11G
            if( match & 0x0F00 )
106
2.11G
                TRY_MATCH(p, 8);
107
2.11G
            if( match & 0xF000 )
108
2.11G
                TRY_MATCH(p, 12);
109
2.11G
        }
110
33.7M
    }
111
112
364M
    for (; p <= end; p++) {
113
321M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
114
260k
            return p;
115
321M
    }
116
117
42.8M
    return NULL;
118
43.0M
}
h264.c:startcode_FindAnnexB_SSE2
Line
Count
Source
59
7.33M
{
60
    /* First align to 16 */
61
    /* Skipping this step and doing unaligned loads isn't faster */
62
7.33M
    const uint8_t *alignedend = p + 16 - ((intptr_t)p & 15);
63
69.8M
    for (end -= 3; p < alignedend && p <= end; p++) {
64
64.7M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
65
2.20M
            return p;
66
64.7M
    }
67
68
5.13M
    if( p == end )
69
77.6k
        return NULL;
70
71
5.05M
    alignedend = end - ((intptr_t) end & 15);
72
5.05M
    if( alignedend > p )
73
4.73M
    {
74
4.73M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
75
4.73M
        const v16qu zeros = { 0 };
76
4.73M
#  endif
77
78
342M
        for( ; p < alignedend; p += 16)
79
338M
        {
80
338M
            uint32_t match;
81
338M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
82
338M
            asm volatile(
83
338M
                "movdqa   0(%[v]),   %%xmm0\n"
84
338M
                "pcmpeqb %[czero],   %%xmm0\n"
85
338M
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
86
338M
                : [match]"=r"(match)
87
338M
                : [v]"r"(p), [czero]"x"(zeros)
88
338M
                : "xmm0"
89
338M
            );
90
#  else
91
            asm volatile(
92
                "movdqa   0(%[v]),   %%xmm0\n"
93
                "pxor      %%xmm1,   %%xmm1\n"
94
                "pcmpeqb   %%xmm1,   %%xmm0\n"
95
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
96
                : [match]"=r"(match)
97
                : [v]"r"(p)
98
                : "xmm0", "xmm1"
99
            );
100
#  endif
101
338M
            if( match & 0x000F )
102
338M
                TRY_MATCH(p, 0);
103
338M
            if( match & 0x00F0 )
104
337M
                TRY_MATCH(p, 4);
105
337M
            if( match & 0x0F00 )
106
337M
                TRY_MATCH(p, 8);
107
337M
            if( match & 0xF000 )
108
337M
                TRY_MATCH(p, 12);
109
337M
        }
110
4.73M
    }
111
112
47.5M
    for (; p <= end; p++) {
113
43.8M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
114
27.4k
            return p;
115
43.8M
    }
116
117
3.64M
    return NULL;
118
3.67M
}
Unexecuted instantiation: hxxx_sei.c:startcode_FindAnnexB_SSE2
Unexecuted instantiation: h264_nal.c:startcode_FindAnnexB_SSE2
Unexecuted instantiation: h264_slice.c:startcode_FindAnnexB_SSE2
hevc.c:startcode_FindAnnexB_SSE2
Line
Count
Source
59
26.8M
{
60
    /* First align to 16 */
61
    /* Skipping this step and doing unaligned loads isn't faster */
62
26.8M
    const uint8_t *alignedend = p + 16 - ((intptr_t)p & 15);
63
132M
    for (end -= 3; p < alignedend && p <= end; p++) {
64
121M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
65
16.3M
            return p;
66
121M
    }
67
68
10.4M
    if( p == end )
69
5.60k
        return NULL;
70
71
10.4M
    alignedend = end - ((intptr_t) end & 15);
72
10.4M
    if( alignedend > p )
73
7.14M
    {
74
7.14M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
75
7.14M
        const v16qu zeros = { 0 };
76
7.14M
#  endif
77
78
507M
        for( ; p < alignedend; p += 16)
79
503M
        {
80
503M
            uint32_t match;
81
503M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
82
503M
            asm volatile(
83
503M
                "movdqa   0(%[v]),   %%xmm0\n"
84
503M
                "pcmpeqb %[czero],   %%xmm0\n"
85
503M
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
86
503M
                : [match]"=r"(match)
87
503M
                : [v]"r"(p), [czero]"x"(zeros)
88
503M
                : "xmm0"
89
503M
            );
90
#  else
91
            asm volatile(
92
                "movdqa   0(%[v]),   %%xmm0\n"
93
                "pxor      %%xmm1,   %%xmm1\n"
94
                "pcmpeqb   %%xmm1,   %%xmm0\n"
95
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
96
                : [match]"=r"(match)
97
                : [v]"r"(p)
98
                : "xmm0", "xmm1"
99
            );
100
#  endif
101
503M
            if( match & 0x000F )
102
502M
                TRY_MATCH(p, 0);
103
502M
            if( match & 0x00F0 )
104
501M
                TRY_MATCH(p, 4);
105
501M
            if( match & 0x0F00 )
106
501M
                TRY_MATCH(p, 8);
107
501M
            if( match & 0xF000 )
108
500M
                TRY_MATCH(p, 12);
109
500M
        }
110
7.14M
    }
111
112
65.4M
    for (; p <= end; p++) {
113
58.0M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
114
37.4k
            return p;
115
58.0M
    }
116
117
7.46M
    return NULL;
118
7.50M
}
Unexecuted instantiation: hevc_nal.c:startcode_FindAnnexB_SSE2
mpeg4video.c:startcode_FindAnnexB_SSE2
Line
Count
Source
59
4.11M
{
60
    /* First align to 16 */
61
    /* Skipping this step and doing unaligned loads isn't faster */
62
4.11M
    const uint8_t *alignedend = p + 16 - ((intptr_t)p & 15);
63
33.9M
    for (end -= 3; p < alignedend && p <= end; p++) {
64
30.5M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
65
745k
            return p;
66
30.5M
    }
67
68
3.36M
    if( p == end )
69
1.64k
        return NULL;
70
71
3.36M
    alignedend = end - ((intptr_t) end & 15);
72
3.36M
    if( alignedend > p )
73
971k
    {
74
971k
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
75
971k
        const v16qu zeros = { 0 };
76
971k
#  endif
77
78
6.14M
        for( ; p < alignedend; p += 16)
79
5.70M
        {
80
5.70M
            uint32_t match;
81
5.70M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
82
5.70M
            asm volatile(
83
5.70M
                "movdqa   0(%[v]),   %%xmm0\n"
84
5.70M
                "pcmpeqb %[czero],   %%xmm0\n"
85
5.70M
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
86
5.70M
                : [match]"=r"(match)
87
5.70M
                : [v]"r"(p), [czero]"x"(zeros)
88
5.70M
                : "xmm0"
89
5.70M
            );
90
#  else
91
            asm volatile(
92
                "movdqa   0(%[v]),   %%xmm0\n"
93
                "pxor      %%xmm1,   %%xmm1\n"
94
                "pcmpeqb   %%xmm1,   %%xmm0\n"
95
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
96
                : [match]"=r"(match)
97
                : [v]"r"(p)
98
                : "xmm0", "xmm1"
99
            );
100
#  endif
101
5.70M
            if( match & 0x000F )
102
5.55M
                TRY_MATCH(p, 0);
103
5.55M
            if( match & 0x00F0 )
104
5.40M
                TRY_MATCH(p, 4);
105
5.40M
            if( match & 0x0F00 )
106
5.27M
                TRY_MATCH(p, 8);
107
5.27M
            if( match & 0xF000 )
108
5.17M
                TRY_MATCH(p, 12);
109
5.17M
        }
110
971k
    }
111
112
18.4M
    for (; p <= end; p++) {
113
15.5M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
114
9.42k
            return p;
115
15.5M
    }
116
117
2.82M
    return NULL;
118
2.83M
}
mpegvideo.c:startcode_FindAnnexB_SSE2
Line
Count
Source
59
37.5M
{
60
    /* First align to 16 */
61
    /* Skipping this step and doing unaligned loads isn't faster */
62
37.5M
    const uint8_t *alignedend = p + 16 - ((intptr_t)p & 15);
63
344M
    for (end -= 3; p < alignedend && p <= end; p++) {
64
314M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
65
7.95M
            return p;
66
314M
    }
67
68
29.5M
    if( p == end )
69
21.8k
        return NULL;
70
71
29.5M
    alignedend = end - ((intptr_t) end & 15);
72
29.5M
    if( alignedend > p )
73
18.8M
    {
74
18.8M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
75
18.8M
        const v16qu zeros = { 0 };
76
18.8M
#  endif
77
78
993M
        for( ; p < alignedend; p += 16)
79
976M
        {
80
976M
            uint32_t match;
81
976M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
82
976M
            asm volatile(
83
976M
                "movdqa   0(%[v]),   %%xmm0\n"
84
976M
                "pcmpeqb %[czero],   %%xmm0\n"
85
976M
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
86
976M
                : [match]"=r"(match)
87
976M
                : [v]"r"(p), [czero]"x"(zeros)
88
976M
                : "xmm0"
89
976M
            );
90
#  else
91
            asm volatile(
92
                "movdqa   0(%[v]),   %%xmm0\n"
93
                "pxor      %%xmm1,   %%xmm1\n"
94
                "pcmpeqb   %%xmm1,   %%xmm0\n"
95
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
96
                : [match]"=r"(match)
97
                : [v]"r"(p)
98
                : "xmm0", "xmm1"
99
            );
100
#  endif
101
976M
            if( match & 0x000F )
102
975M
                TRY_MATCH(p, 0);
103
975M
            if( match & 0x00F0 )
104
975M
                TRY_MATCH(p, 4);
105
975M
            if( match & 0x0F00 )
106
974M
                TRY_MATCH(p, 8);
107
974M
            if( match & 0xF000 )
108
974M
                TRY_MATCH(p, 12);
109
974M
        }
110
18.8M
    }
111
112
213M
    for (; p <= end; p++) {
113
186M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
114
118k
            return p;
115
186M
    }
116
117
27.2M
    return NULL;
118
27.3M
}
vc1.c:startcode_FindAnnexB_SSE2
Line
Count
Source
59
6.14M
{
60
    /* First align to 16 */
61
    /* Skipping this step and doing unaligned loads isn't faster */
62
6.14M
    const uint8_t *alignedend = p + 16 - ((intptr_t)p & 15);
63
37.3M
    for (end -= 3; p < alignedend && p <= end; p++) {
64
34.8M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
65
3.62M
            return p;
66
34.8M
    }
67
68
2.52M
    if( p == end )
69
20.8k
        return NULL;
70
71
2.49M
    alignedend = end - ((intptr_t) end & 15);
72
2.49M
    if( alignedend > p )
73
2.11M
    {
74
2.11M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
75
2.11M
        const v16qu zeros = { 0 };
76
2.11M
#  endif
77
78
300M
        for( ; p < alignedend; p += 16)
79
298M
        {
80
298M
            uint32_t match;
81
298M
#  ifdef HAS_ATTRIBUTE_VECTORSIZE
82
298M
            asm volatile(
83
298M
                "movdqa   0(%[v]),   %%xmm0\n"
84
298M
                "pcmpeqb %[czero],   %%xmm0\n"
85
298M
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
86
298M
                : [match]"=r"(match)
87
298M
                : [v]"r"(p), [czero]"x"(zeros)
88
298M
                : "xmm0"
89
298M
            );
90
#  else
91
            asm volatile(
92
                "movdqa   0(%[v]),   %%xmm0\n"
93
                "pxor      %%xmm1,   %%xmm1\n"
94
                "pcmpeqb   %%xmm1,   %%xmm0\n"
95
                "pmovmskb  %%xmm0,   %[match]\n" /* mask will be in reversed match order */
96
                : [match]"=r"(match)
97
                : [v]"r"(p)
98
                : "xmm0", "xmm1"
99
            );
100
#  endif
101
298M
            if( match & 0x000F )
102
298M
                TRY_MATCH(p, 0);
103
298M
            if( match & 0x00F0 )
104
298M
                TRY_MATCH(p, 4);
105
298M
            if( match & 0x0F00 )
106
298M
                TRY_MATCH(p, 8);
107
298M
            if( match & 0xF000 )
108
297M
                TRY_MATCH(p, 12);
109
297M
        }
110
2.11M
    }
111
112
19.4M
    for (; p <= end; p++) {
113
17.7M
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
114
67.3k
            return p;
115
17.7M
    }
116
117
1.66M
    return NULL;
118
1.73M
}
119
120
#endif
121
122
/* That code is adapted from libav's ff_avc_find_startcode_internal
123
 * and i believe the trick originated from
124
 * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
125
 */
126
static inline const uint8_t * startcode_FindAnnexB_Bits( const uint8_t *p, const uint8_t *end )
127
0
{
128
0
    const uint8_t *a = p + 4 - ((intptr_t)p & 3);
129
130
0
    for (end -= 3; p < a && p <= end; p++) {
131
0
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
132
0
            return p;
133
0
    }
134
135
0
    for (end -= 3; p < end; p += 4) {
136
0
        uint32_t x;
137
0
        memcpy(&x, p, sizeof(x));
138
0
        if ((x - 0x01010101) & (~x) & 0x80808080)
139
0
        {
140
            /* matching DW isn't faster */
141
0
            TRY_MATCH(p, 0);
142
0
        }
143
0
    }
144
145
0
    for (end += 3; p <= end; p++) {
146
0
        if (p[0] == 0 && p[1] == 0 && p[2] == 1)
147
0
            return p;
148
0
    }
149
150
0
    return NULL;
151
0
}
Unexecuted instantiation: h264.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: hxxx_sei.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: h264_nal.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: h264_slice.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: hevc.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: hevc_nal.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: mpeg4video.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: mpegvideo.c:startcode_FindAnnexB_Bits
Unexecuted instantiation: vc1.c:startcode_FindAnnexB_Bits
152
#undef TRY_MATCH
153
154
#ifdef CAN_COMPILE_SSE2
155
static inline const uint8_t * startcode_FindAnnexB( const uint8_t *p, const uint8_t *end )
156
81.9M
{
157
81.9M
    if (vlc_CPU_SSE2())
158
81.9M
        return startcode_FindAnnexB_SSE2(p, end);
159
0
    else
160
0
        return startcode_FindAnnexB_Bits(p, end);
161
81.9M
}
h264.c:startcode_FindAnnexB
Line
Count
Source
156
7.33M
{
157
7.33M
    if (vlc_CPU_SSE2())
158
7.33M
        return startcode_FindAnnexB_SSE2(p, end);
159
0
    else
160
0
        return startcode_FindAnnexB_Bits(p, end);
161
7.33M
}
Unexecuted instantiation: hxxx_sei.c:startcode_FindAnnexB
Unexecuted instantiation: h264_nal.c:startcode_FindAnnexB
Unexecuted instantiation: h264_slice.c:startcode_FindAnnexB
hevc.c:startcode_FindAnnexB
Line
Count
Source
156
26.8M
{
157
26.8M
    if (vlc_CPU_SSE2())
158
26.8M
        return startcode_FindAnnexB_SSE2(p, end);
159
0
    else
160
0
        return startcode_FindAnnexB_Bits(p, end);
161
26.8M
}
Unexecuted instantiation: hevc_nal.c:startcode_FindAnnexB
mpeg4video.c:startcode_FindAnnexB
Line
Count
Source
156
4.11M
{
157
4.11M
    if (vlc_CPU_SSE2())
158
4.11M
        return startcode_FindAnnexB_SSE2(p, end);
159
0
    else
160
0
        return startcode_FindAnnexB_Bits(p, end);
161
4.11M
}
mpegvideo.c:startcode_FindAnnexB
Line
Count
Source
156
37.5M
{
157
37.5M
    if (vlc_CPU_SSE2())
158
37.5M
        return startcode_FindAnnexB_SSE2(p, end);
159
0
    else
160
0
        return startcode_FindAnnexB_Bits(p, end);
161
37.5M
}
vc1.c:startcode_FindAnnexB
Line
Count
Source
156
6.14M
{
157
6.14M
    if (vlc_CPU_SSE2())
158
6.14M
        return startcode_FindAnnexB_SSE2(p, end);
159
0
    else
160
0
        return startcode_FindAnnexB_Bits(p, end);
161
6.14M
}
162
#else
163
    #define startcode_FindAnnexB startcode_FindAnnexB_Bits
164
#endif
165
166
#endif