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
30
32 {
33 const uint8_t *
a = p + 4 - ((intptr_t)p & 3);
34
35 for (end -= 3; p <
a && p < end; p++) {
36 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
37 return p;
38 }
39
40 for (end -= 3; p < end; p += 4) {
41 uint32_t x = *(const uint32_t*)p;
42 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
43 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
44 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
45 if (p[1] == 0) {
46 if (p[0] == 0 && p[2] == 1)
47 return p;
48 if (p[2] == 0 && p[3] == 1)
49 return p+1;
50 }
51 if (p[3] == 0) {
52 if (p[2] == 0 && p[4] == 1)
53 return p+2;
54 if (p[4] == 0 && p[5] == 1)
55 return p+3;
56 }
57 }
58 }
59
60 for (end += 3; p < end; p++) {
61 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
62 return p;
63 }
64
65 return end + 3;
66 }
67
72 }
73
75 const uint8_t *buf_in,
int size)
76 {
77 const uint8_t *p = buf_in;
78 const uint8_t *end = p +
size;
79 const uint8_t *nal_start, *nal_end;
80
83 for (;;) {
84 const size_t nalu_limit = SIZE_MAX /
sizeof(*
list->nalus);
85 while (nal_start < end && !*(nal_start++));
86 if (nal_start == end)
87 break;
88
90 if (pb) {
92 avio_write(pb, nal_start, nal_end - nal_start);
93 }
else if (
list->nb_nalus >= nalu_limit) {
95 } else {
97 (
list->nb_nalus + 1) *
sizeof(*
list->nalus));
101 tmp[
list->nb_nalus++] = (
NALU){ .offset = nal_start - p,
102 .size = nal_end - nal_start };
103 }
104 size += 4 + nal_end - nal_start;
105 nal_start = nal_end;
106 }
108 }
109
111 {
113 }
114
116 {
119 }
120
122 const uint8_t *buf)
123 {
124 for (
unsigned i = 0;
i <
list->nb_nalus;
i++) {
127 }
128 }
129
131 {
136
138
140 return 0;
141 }
142
144 const uint8_t *end,
145 int nal_length_size)
146 {
147 unsigned int res = 0;
148
149 if (end - start < nal_length_size)
151 while (nal_length_size--)
152 res = (res << 8) | *start++;
153
154 if (res > end - start)
156
157 return start + res;
158 }
159
161 uint32_t *dst_len, int header_len)
162 {
165
169
170 /* NAL unit header */
172 while (
i < header_len &&
i < src_len)
174
175 while (
i + 2 < src_len)
179 i++;
// remove emulation_prevention_three_byte
180 } else
182
185
187
190 }