forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs-avc.c
223 lines (176 loc) · 5.55 KB
/
obs-avc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/******************************************************************************
Copyright (C) 2014 by Hugh Bailey <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "obs.h"
#include "obs-avc.h"
#include "util/array-serializer.h"
bool obs_avc_keyframe(const uint8_t *data, size_t size)
{
const uint8_t *nal_start, *nal_end;
const uint8_t *end = data + size;
int type;
nal_start = obs_avc_find_startcode(data, end);
while (true) {
while (nal_start < end && !*(nal_start++));
if (nal_start == end)
break;
type = nal_start[0] & 0x1F;
if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE)
return (type == OBS_NAL_SLICE_IDR);
nal_end = obs_avc_find_startcode(nal_start, end);
nal_start = nal_end;
}
return false;
}
/* NOTE: I noticed that FFmpeg does some unusual special handling of certain
* scenarios that I was unaware of, so instead of just searching for {0, 0, 1}
* we'll just use the code from FFmpeg - http://www.ffmpeg.org/ */
static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p,
const uint8_t *end)
{
const uint8_t *a = p + 4 - ((intptr_t)p & 3);
for (end -= 3; p < a && p < end; p++) {
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
return p;
}
for (end -= 3; p < end; p += 4) {
uint32_t x = *(const uint32_t*)p;
if ((x - 0x01010101) & (~x) & 0x80808080) {
if (p[1] == 0) {
if (p[0] == 0 && p[2] == 1)
return p;
if (p[2] == 0 && p[3] == 1)
return p+1;
}
if (p[3] == 0) {
if (p[2] == 0 && p[4] == 1)
return p+2;
if (p[4] == 0 && p[5] == 1)
return p+3;
}
}
}
for (end += 3; p < end; p++) {
if (p[0] == 0 && p[1] == 0 && p[2] == 1)
return p;
}
return end + 3;
}
const uint8_t *obs_avc_find_startcode(const uint8_t *p, const uint8_t *end)
{
const uint8_t *out= ff_avc_find_startcode_internal(p, end);
if (p < out && out < end && !out[-1]) out--;
return out;
}
static inline int get_drop_priority(int priority)
{
switch (priority) {
case OBS_NAL_PRIORITY_DISPOSABLE: return OBS_NAL_PRIORITY_DISPOSABLE;
case OBS_NAL_PRIORITY_LOW: return OBS_NAL_PRIORITY_LOW;
}
return OBS_NAL_PRIORITY_HIGHEST;
}
static void serialize_avc_data(struct serializer *s, const uint8_t *data,
size_t size, bool *is_keyframe, int *priority)
{
const uint8_t *nal_start, *nal_end;
const uint8_t *end = data+size;
int type;
nal_start = obs_avc_find_startcode(data, end);
while (true) {
while (nal_start < end && !*(nal_start++));
if (nal_start == end)
break;
type = nal_start[0] & 0x1F;
if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE) {
if (is_keyframe)
*is_keyframe = (type == OBS_NAL_SLICE_IDR);
if (priority)
*priority = nal_start[0] >> 5;
}
nal_end = obs_avc_find_startcode(nal_start, end);
s_wb32(s, (uint32_t)(nal_end - nal_start));
s_write(s, nal_start, nal_end - nal_start);
nal_start = nal_end;
}
}
void obs_parse_avc_packet(struct encoder_packet *avc_packet,
const struct encoder_packet *src)
{
struct array_output_data output;
struct serializer s;
array_output_serializer_init(&s, &output);
*avc_packet = *src;
serialize_avc_data(&s, src->data, src->size, &avc_packet->keyframe,
&avc_packet->priority);
avc_packet->data = output.bytes.array;
avc_packet->size = output.bytes.num;
avc_packet->drop_priority = get_drop_priority(avc_packet->priority);
}
static inline bool has_start_code(const uint8_t *data)
{
if (data[0] != 0 || data[1] != 0)
return false;
return data[2] == 1 || (data[2] == 0 && data[3] == 1);
}
static void get_sps_pps(const uint8_t *data, size_t size,
const uint8_t **sps, size_t *sps_size,
const uint8_t **pps, size_t *pps_size)
{
const uint8_t *nal_start, *nal_end;
const uint8_t *end = data+size;
int type;
nal_start = obs_avc_find_startcode(data, end);
while (true) {
while (nal_start < end && !*(nal_start++));
if (nal_start == end)
break;
nal_end = obs_avc_find_startcode(nal_start, end);
type = nal_start[0] & 0x1F;
if (type == OBS_NAL_SPS) {
*sps = nal_start;
*sps_size = nal_end - nal_start;
} else if (type == OBS_NAL_PPS) {
*pps = nal_start;
*pps_size = nal_end - nal_start;
}
nal_start = nal_end;
}
}
size_t obs_parse_avc_header(uint8_t **header, const uint8_t *data, size_t size)
{
struct array_output_data output;
struct serializer s;
const uint8_t *sps = NULL, *pps = NULL;
size_t sps_size = 0, pps_size = 0;
array_output_serializer_init(&s, &output);
if (size <= 6) return 0;
if (!has_start_code(data)) {
*header = bmemdup(data, size);
return size;
}
get_sps_pps(data, size, &sps, &sps_size, &pps, &pps_size);
if (!sps || !pps || sps_size < 4)
return 0;
s_w8(&s, 0x01);
s_write(&s, sps+1, 3);
s_w8(&s, 0xff);
s_w8(&s, 0xe1);
s_wb16(&s, (uint16_t)sps_size);
s_write(&s, sps, sps_size);
s_w8(&s, 0x01);
s_wb16(&s, (uint16_t)pps_size);
s_write(&s, pps, pps_size);
*header = output.bytes.array;
return output.bytes.num;
}