FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nal.c
Date: 2026-04-23 02:20:26
Exec Total Coverage
Lines: 85 99 85.9%
Functions: 8 9 88.9%
Branches: 75 90 83.3%

Line Branch Exec Source
1 /*
2 * NAL helper functions for muxers
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #include "libavutil/mem.h"
25 #include "libavutil/error.h"
26 #include "libavcodec/defs.h"
27 #include "avio.h"
28 #include "avio_internal.h"
29 #include "nal.h"
30
31 3416 static const uint8_t *nal_find_startcode_internal(const uint8_t *p, const uint8_t *end)
32 {
33 3416 const uint8_t *a = p + 4 - ((intptr_t)p & 3);
34
35
4/4
✓ Branch 0 taken 9505 times.
✓ Branch 1 taken 2393 times.
✓ Branch 2 taken 9504 times.
✓ Branch 3 taken 1 times.
11898 for (end -= 3; p < a && p < end; p++) {
36
6/6
✓ Branch 0 taken 2381 times.
✓ Branch 1 taken 7123 times.
✓ Branch 2 taken 1967 times.
✓ Branch 3 taken 414 times.
✓ Branch 4 taken 1022 times.
✓ Branch 5 taken 945 times.
9504 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
37 1022 return p;
38 }
39
40
2/2
✓ Branch 0 taken 809785 times.
✓ Branch 1 taken 982 times.
810767 for (end -= 3; p < end; p += 4) {
41 809785 uint32_t x = *(const uint32_t*)p;
42 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
43 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
44
2/2
✓ Branch 0 taken 12915 times.
✓ Branch 1 taken 796870 times.
809785 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
45
2/2
✓ Branch 0 taken 3800 times.
✓ Branch 1 taken 9115 times.
12915 if (p[1] == 0) {
46
4/4
✓ Branch 0 taken 500 times.
✓ Branch 1 taken 3300 times.
✓ Branch 2 taken 306 times.
✓ Branch 3 taken 194 times.
3800 if (p[0] == 0 && p[2] == 1)
47 306 return p;
48
4/4
✓ Branch 0 taken 794 times.
✓ Branch 1 taken 2700 times.
✓ Branch 2 taken 526 times.
✓ Branch 3 taken 268 times.
3494 if (p[2] == 0 && p[3] == 1)
49 526 return p+1;
50 }
51
2/2
✓ Branch 0 taken 3694 times.
✓ Branch 1 taken 8389 times.
12083 if (p[3] == 0) {
52
4/4
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 3235 times.
✓ Branch 2 taken 284 times.
✓ Branch 3 taken 175 times.
3694 if (p[2] == 0 && p[4] == 1)
53 284 return p+2;
54
4/4
✓ Branch 0 taken 509 times.
✓ Branch 1 taken 2901 times.
✓ Branch 2 taken 296 times.
✓ Branch 3 taken 213 times.
3410 if (p[4] == 0 && p[5] == 1)
55 296 return p+3;
56 }
57 }
58 }
59
60
2/2
✓ Branch 0 taken 1492 times.
✓ Branch 1 taken 982 times.
2474 for (end += 3; p < end; p++) {
61
3/6
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1436 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1492 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
62 return p;
63 }
64
65 982 return end + 3;
66 }
67
68 3416 const uint8_t *ff_nal_find_startcode(const uint8_t *p, const uint8_t *end){
69 3416 const uint8_t *out = nal_find_startcode_internal(p, end);
70
6/6
✓ Branch 0 taken 3336 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 2354 times.
✓ Branch 3 taken 982 times.
✓ Branch 4 taken 1123 times.
✓ Branch 5 taken 1231 times.
3416 if(p<out && out<end && !out[-1]) out--;
71 3416 return out;
72 }
73
74 982 static int nal_parse_units(AVIOContext *pb, NALUList *list,
75 const uint8_t *buf_in, int size)
76 {
77 982 const uint8_t *p = buf_in;
78 982 const uint8_t *end = p + size;
79 const uint8_t *nal_start, *nal_end;
80
81 982 size = 0;
82 982 nal_start = ff_nal_find_startcode(p, end);
83 2434 for (;;) {
84 3416 const size_t nalu_limit = SIZE_MAX / sizeof(*list->nalus);
85
4/4
✓ Branch 0 taken 8425 times.
✓ Branch 1 taken 982 times.
✓ Branch 2 taken 5991 times.
✓ Branch 3 taken 2434 times.
9407 while (nal_start < end && !*(nal_start++));
86
2/2
✓ Branch 0 taken 982 times.
✓ Branch 1 taken 2434 times.
3416 if (nal_start == end)
87 982 break;
88
89 2434 nal_end = ff_nal_find_startcode(nal_start, end);
90
2/2
✓ Branch 0 taken 2106 times.
✓ Branch 1 taken 328 times.
2434 if (pb) {
91 2106 ptrdiff_t nalu_size = nal_end - nal_start;
92
3/4
✓ Branch 0 taken 2131 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 2106 times.
2131 while (nalu_size > 0 && nal_start[nalu_size - 1] == 0)
93 25 --nalu_size;
94 2106 avio_wb32(pb, nalu_size);
95 2106 avio_write(pb, nal_start, nalu_size);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 328 times.
328 } else if (list->nb_nalus >= nalu_limit) {
97 return AVERROR(ERANGE);
98 } else {
99 328 NALU *tmp = av_fast_realloc(list->nalus, &list->nalus_array_size,
100 328 (list->nb_nalus + 1) * sizeof(*list->nalus));
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 328 times.
328 if (!tmp)
102 return AVERROR(ENOMEM);
103 328 list->nalus = tmp;
104 328 tmp[list->nb_nalus++] = (NALU){ .offset = nal_start - p,
105 328 .size = nal_end - nal_start };
106 }
107 2434 size += 4 + nal_end - nal_start;
108 2434 nal_start = nal_end;
109 }
110 982 return size;
111 }
112
113 893 int ff_nal_parse_units(AVIOContext *pb, const uint8_t *buf_in, int size)
114 {
115 893 return nal_parse_units(pb, NULL, buf_in, size);
116 }
117
118 89 int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size)
119 {
120 89 list->nb_nalus = 0;
121 89 return nal_parse_units(NULL, list, buf, size);
122 }
123
124 89 void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb,
125 const uint8_t *buf)
126 {
127
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 89 times.
417 for (unsigned i = 0; i < list->nb_nalus; i++) {
128 328 avio_wb32(pb, list->nalus[i].size);
129 328 avio_write(pb, buf + list->nalus[i].offset, list->nalus[i].size);
130 }
131 89 }
132
133 13 int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
134 {
135 AVIOContext *pb;
136 13 int ret = avio_open_dyn_buf(&pb);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if(ret < 0)
138 return ret;
139
140 13 ff_nal_parse_units(pb, buf_in, *size);
141
142 13 *size = avio_close_dyn_buf(pb, buf);
143 13 return 0;
144 }
145
146 const uint8_t *ff_nal_mp4_find_startcode(const uint8_t *start,
147 const uint8_t *end,
148 int nal_length_size)
149 {
150 unsigned int res = 0;
151
152 if (end - start < nal_length_size)
153 return NULL;
154 while (nal_length_size--)
155 res = (res << 8) | *start++;
156
157 if (res > end - start)
158 return NULL;
159
160 return start + res;
161 }
162
163 77 uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
164 uint32_t *dst_len, int header_len)
165 {
166 uint8_t *dst;
167 uint32_t i, len;
168
169 77 dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77 times.
77 if (!dst)
171 return NULL;
172
173 /* NAL unit header */
174 77 i = len = 0;
175
3/4
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
215 while (i < header_len && i < src_len)
176 138 dst[len++] = src[i++];
177
178
2/2
✓ Branch 0 taken 5949 times.
✓ Branch 1 taken 77 times.
6026 while (i + 2 < src_len)
179
6/6
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 5600 times.
✓ Branch 2 taken 196 times.
✓ Branch 3 taken 153 times.
✓ Branch 4 taken 150 times.
✓ Branch 5 taken 46 times.
5949 if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
180 150 dst[len++] = src[i++];
181 150 dst[len++] = src[i++];
182 150 i++; // remove emulation_prevention_three_byte
183 } else
184 5799 dst[len++] = src[i++];
185
186
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 77 times.
231 while (i < src_len)
187 154 dst[len++] = src[i++];
188
189 77 memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
190
191 77 *dst_len = len;
192 77 return dst;
193 }
194