1 /*
2 * AVC helper functions for muxers
3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
26
28 {
29 const uint8_t *
a = p + 4 - ((intptr_t)p & 3);
30
31 for (end -= 3; p < a && p <
end; p++) {
32 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
33 return p;
34 }
35
36 for (end -= 3; p <
end; p += 4) {
37 uint32_t x = *(const uint32_t*)p;
38 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
39 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
40 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
41 if (p[1] == 0) {
42 if (p[0] == 0 && p[2] == 1)
43 return p;
44 if (p[2] == 0 && p[3] == 1)
45 return p+1;
46 }
47 if (p[3] == 0) {
48 if (p[2] == 0 && p[4] == 1)
49 return p+2;
50 if (p[4] == 0 && p[5] == 1)
51 return p+3;
52 }
53 }
54 }
55
56 for (end += 3; p <
end; p++) {
57 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
58 return p;
59 }
60
61 return end + 3;
62 }
63
66 if(p<out && out<end && !out[-1]) out--;
68 }
69
71 {
74 const uint8_t *nal_start, *nal_end;
75
76 size = 0;
78 for (;;) {
79 while (nal_start < end && !*(nal_start++));
80 if (nal_start == end)
81 break;
82
85 avio_write(pb, nal_start, nal_end - nal_start);
86 size += 4 + nal_end - nal_start;
87 nal_start = nal_end;
88 }
90 }
91
93 {
96 if(ret < 0)
98
100
103 return 0;
104 }
105
107 {
108 if (len > 6) {
109 /* check for h264 start code */
110 if (
AV_RB32(data) == 0x00000001 ||
113 uint32_t sps_size=0, pps_size=0;
115
117 if (ret < 0)
121
122 /* look for sps and pps */
123 while (end - buf > 4) {
127 buf += 4;
128 nal_type = buf[0] & 0x1f;
129
130 if (nal_type == 7) { /* SPS */
133 } else if (nal_type == 8) { /* PPS */
136 }
137
139 }
140
141 if (!sps || !pps || sps_size < 4 || sps_size > UINT16_MAX || pps_size > UINT16_MAX)
143
145 avio_w8(pb, sps[1]);
/* profile */
146 avio_w8(pb, sps[2]);
/* profile compat */
147 avio_w8(pb, sps[3]);
/* level */
148 avio_w8(pb, 0xff);
/* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
149 avio_w8(pb, 0xe1);
/* 3 bits reserved (111) + 5 bits number of sps (00001) */
150
153 avio_w8(pb, 1);
/* number of pps */
157 } else {
159 }
160 }
161 return 0;
162 }
163
165 {
166 uint16_t sps_size, pps_size;
168 int out_size;
169
170 *buf = NULL;
171 if (*size >= 4 && (
AV_RB32(in) == 0x00000001 ||
AV_RB24(in) == 0x000001))
172 return 0;
173 if (*size < 11 || in[0] != 1)
175
177 if (11 + sps_size > *size)
179 pps_size =
AV_RB16(&in[9 + sps_size]);
180 if (11 + sps_size + pps_size > *size)
182 out_size = 8 + sps_size + pps_size;
184 if (!out)
187 memcpy(out + 4, &in[8], sps_size);
188 AV_WB32(&out[4 + sps_size], 0x00000001);
189 memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
191 *size = out_size;
192 return 0;
193 }