FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nal.c
Date: 2026-09-12 04:54:02
Exec Total Coverage
Lines: 84 98 85.7%
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 3481 static const uint8_t *nal_find_startcode_internal(const uint8_t *p, const uint8_t *end)
32 {
33 3481 const uint8_t *a = p + 4 - ((intptr_t)p & 3);
34
35
4/4
✓ Branch 0 taken 9684 times.
✓ Branch 1 taken 2438 times.
✓ Branch 2 taken 9683 times.
✓ Branch 3 taken 1 times.
12122 for (end -= 3; p < a && p < end; p++) {
36
6/6
✓ Branch 0 taken 2427 times.
✓ Branch 1 taken 7256 times.
✓ Branch 2 taken 2007 times.
✓ Branch 3 taken 420 times.
✓ Branch 4 taken 1042 times.
✓ Branch 5 taken 965 times.
9683 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
37 1042 return p;
38 }
39
40
2/2
✓ Branch 0 taken 828917 times.
✓ Branch 1 taken 1002 times.
829919 for (end -= 3; p < end; p += 4) {
41 828917 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 13269 times.
✓ Branch 1 taken 815648 times.
828917 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
45
2/2
✓ Branch 0 taken 3893 times.
✓ Branch 1 taken 9376 times.
13269 if (p[1] == 0) {
46
4/4
✓ Branch 0 taken 509 times.
✓ Branch 1 taken 3384 times.
✓ Branch 2 taken 308 times.
✓ Branch 3 taken 201 times.
3893 if (p[0] == 0 && p[2] == 1)
47 308 return p;
48
4/4
✓ Branch 0 taken 811 times.
✓ Branch 1 taken 2774 times.
✓ Branch 2 taken 527 times.
✓ Branch 3 taken 284 times.
3585 if (p[2] == 0 && p[3] == 1)
49 527 return p+1;
50 }
51
2/2
✓ Branch 0 taken 3801 times.
✓ Branch 1 taken 8633 times.
12434 if (p[3] == 0) {
52
4/4
✓ Branch 0 taken 485 times.
✓ Branch 1 taken 3316 times.
✓ Branch 2 taken 293 times.
✓ Branch 3 taken 192 times.
3801 if (p[2] == 0 && p[4] == 1)
53 293 return p+2;
54
4/4
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 2980 times.
✓ Branch 2 taken 309 times.
✓ Branch 3 taken 219 times.
3508 if (p[4] == 0 && p[5] == 1)
55 309 return p+3;
56 }
57 }
58 }
59
60
2/2
✓ Branch 0 taken 1519 times.
✓ Branch 1 taken 1002 times.
2521 for (end += 3; p < end; p++) {
61
3/6
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1463 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1519 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
62 return p;
63 }
64
65 1002 return end + 3;
66 }
67
68 3481 const uint8_t *ff_nal_find_startcode(const uint8_t *p, const uint8_t *end){
69 3481 const uint8_t *out = nal_find_startcode_internal(p, end);
70
6/6
✓ Branch 0 taken 3401 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 2399 times.
✓ Branch 3 taken 1002 times.
✓ Branch 4 taken 1166 times.
✓ Branch 5 taken 1233 times.
3481 if(p<out && out<end && !out[-1]) out--;
71 3481 return out;
72 }
73
74 984 static int nal_parse_units(AVIOContext *pb, NALUList *list,
75 const uint8_t *buf_in, int size)
76 {
77 984 const uint8_t *p = buf_in;
78 984 const uint8_t *end = p + size;
79 const uint8_t *nal_start, *nal_end;
80
81 984 size = 0;
82 984 nal_start = ff_nal_find_startcode(p, end);
83 2439 for (;;) {
84 3423 const size_t nalu_limit = SIZE_MAX / sizeof(*list->nalus);
85
4/4
✓ Branch 0 taken 8445 times.
✓ Branch 1 taken 984 times.
✓ Branch 2 taken 6006 times.
✓ Branch 3 taken 2439 times.
9429 while (nal_start < end && !*(nal_start++));
86
2/2
✓ Branch 0 taken 984 times.
✓ Branch 1 taken 2439 times.
3423 if (nal_start == end)
87 984 break;
88
89 2439 nal_end = ff_nal_find_startcode(nal_start, end);
90 2439 size_t nalu_size = nal_end - nal_start;
91
3/4
✓ Branch 0 taken 2468 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 2439 times.
2468 while (nalu_size > 0 && nal_start[nalu_size - 1] == 0)
92 29 --nalu_size;
93
2/2
✓ Branch 0 taken 2111 times.
✓ Branch 1 taken 328 times.
2439 if (pb) {
94 2111 avio_wb32(pb, nalu_size);
95 2111 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 .size = nalu_size };
106 }
107 2439 size += 4 + nalu_size;
108 2439 nal_start = nal_end;
109 }
110 984 return size;
111 }
112
113 895 int ff_nal_parse_units(AVIOContext *pb, const uint8_t *buf_in, int size)
114 {
115 895 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 15 int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
134 {
135 AVIOContext *pb;
136 15 int ret = avio_open_dyn_buf(&pb);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if(ret < 0)
138 return ret;
139
140 15 ff_nal_parse_units(pb, buf_in, *size);
141
142 15 *size = avio_close_dyn_buf(pb, buf);
143 15 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 109 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 109 dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 109 times.
109 if (!dst)
171 return NULL;
172
173 /* NAL unit header */
174 109 i = len = 0;
175
3/4
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 196 times.
✗ Branch 3 not taken.
305 while (i < header_len && i < src_len)
176 196 dst[len++] = src[i++];
177
178
2/2
✓ Branch 0 taken 6611 times.
✓ Branch 1 taken 109 times.
6720 while (i + 2 < src_len)
179
6/6
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 6097 times.
✓ Branch 2 taken 288 times.
✓ Branch 3 taken 226 times.
✓ Branch 4 taken 220 times.
✓ Branch 5 taken 68 times.
6611 if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
180 220 dst[len++] = src[i++];
181 220 dst[len++] = src[i++];
182 220 i++; // remove emulation_prevention_three_byte
183 } else
184 6391 dst[len++] = src[i++];
185
186
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 109 times.
327 while (i < src_len)
187 218 dst[len++] = src[i++];
188
189 109 memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
190
191 109 *dst_len = len;
192 109 return dst;
193 }
194