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
|
void CodecFactor::_handleVideoTag(VideoTagValue &tag, uint32_t timestamp) {
uint32_t width = 0;
uint32_t height = 0;
uint32_t stride0 = 0;
uint32_t stride1 = 0;
uint8_t *picPtr = nullptr;
#ifdef USE_OPEN_H265
// nothing to do
#elif defined(USE_OPEN_H264)
unsigned char *pDst[3] = {0};
SBufferInfo sDstInfo = {0};
#endif
// ###################流的开始,需要指定SPS和PPS#####################
// 内容是AVCDecoderConfigurationRecord, 也就是AVC sequence header
if (tag.AVCPacketType == 0) {
shared_ptr<Buffer> unit = tag.data;
// 版本号
uint8_t configurationVersion = unit->read_uint8(0);
// SPS[1]
uint8_t AVCProfileIndication = unit->read_uint8(1);
// SPS[2]
uint8_t profileCompatibility = unit->read_uint8(2);
// SPS[3]
uint8_t AVCLevelIndication = unit->read_uint8(3);
// NALU的长度占几个字节, 这里的名字误导人, 其实应该叫lengthSize了, 因为加了1了
_codec->lengthSizeMinusOne = (unit->read_uint8(4) & 3) + 1;
// SPS的个数
int numOfSequenceParameterSets = unit->read_uint8(5) & 0x1f;
// 16bit, SPS的长度, 这里只有一个SPS, 所以只读一个
int sequenceParameterSetLength = unit->read_uint16_be(6);
// 读取SPS, 存到成员变量中, 其中_mask的值为 {0x00, 0x00, 0x00, 0x01}
_codec->sps = make_shared<Buffer>(unit->slice(8, 8 + (uint32_t) sequenceParameterSetLength));
_codec->sps = make_shared<Buffer>(*_mask + *_codec->sps);
// 解码器处理SPS
#ifdef USE_OPEN_H265
// nothing to do
#elif defined(USE_OPEN_H264)
_codec->storage->DecodeFrame2(_codec->sps->get_buf_ptr(), _codec->sps->get_length(), &pDst[0], &sDstInfo);
#else
h264bsdDecode(_codec->storage, _codec->sps->get_buf_ptr(), _codec->sps->get_length(), &picPtr, &width, &height);
#endif
// PPS的个数
int numOfPictureParameterSets = unit->read_uint8(8 + (uint32_t) sequenceParameterSetLength);
// 16bit, 这里只有一个PPS, 只读一个
int pictureParameterSetLength = unit->read_uint16_be(8 + (uint32_t) sequenceParameterSetLength + 1);
// 读取PPS, 存到成员变量
_codec->pps = make_shared<Buffer>(unit->slice(
8 + (uint32_t) sequenceParameterSetLength + 3,
8 + (uint32_t) sequenceParameterSetLength + 3 + pictureParameterSetLength
));
// 其中_mask的值为 {0x00, 0x00, 0x00, 0x01}
_codec->pps = make_shared<Buffer>(*_mask + *_codec->pps);
//解码器处理PPS
#ifdef USE_OPEN_H265
// nothing to do
#elif defined(USE_OPEN_H264)
_codec->storage->DecodeFrame2(_codec->pps->get_buf_ptr(), _codec->pps->get_length(), &pDst[0], &sDstInfo);
#else
h264bsdDecode(_codec->storage, _codec->pps->get_buf_ptr(), _codec->pps->get_length(), &picPtr, &width, &height);
#endif
// ###################流的中间,nalus#####################
} else if (tag.AVCPacketType == 1) {
uint32_t size = tag.data->get_length();
shared_ptr<Buffer> unit = tag.data;
shared_ptr<Buffer> nalus = make_shared<Buffer>();
while (size > 0) {
int naluLen = 0;
// 计算本次NALU长度
for (uint32_t i = 0; i < _codec->lengthSizeMinusOne; i++) {
naluLen |= unit->read_uint8(i) << ((_codec->lengthSizeMinusOne - 1 - i) * 8);
}
shared_ptr<Buffer> nalu = make_shared<Buffer>(unit->slice(
(uint32_t) _codec->lengthSizeMinusOne,
(uint32_t) _codec->lengthSizeMinusOne + naluLen
));
// 将所有NALUS包写到一个buffer里面,其中_mask的值为 {0x00, 0x00, 0x00, 0x01}
nalus = make_shared<Buffer>(*nalus + *_mask + *nalu);
unit = make_shared<Buffer>(unit->slice((uint32_t) _codec->lengthSizeMinusOne + naluLen));
size -= _codec->lengthSizeMinusOne + naluLen;
}
// 解码视频帧
#ifdef USE_OPEN_H265
if(true) {
#elif defined(USE_OPEN_H264)
uint32_t retCode = _codec->storage->DecodeFrame2(nalus->get_buf_ptr(), nalus->get_length(), &pDst[0], &sDstInfo);
if (retCode == 0 && sDstInfo.iBufferStatus == 1) {
width = (uint32_t) sDstInfo.UsrData.sSystemBuffer.iWidth;
height = (uint32_t) sDstInfo.UsrData.sSystemBuffer.iHeight;
stride0 = (uint32_t) sDstInfo.UsrData.sSystemBuffer.iStride[0];
stride1 = (uint32_t) sDstInfo.UsrData.sSystemBuffer.iStride[1];
#else
uint32_t retCode = h264bsdDecode(_codec->storage, nalus->get_buf_ptr(), nalus->get_length(), &picPtr, &width, &height);
if (retCode == H264BSD_PIC_RDY) {
stride0 = width;
stride1 = height;
#endif
uint32_t totalSize = (width * height) * 3 / 2;
#ifdef __EMSCRIPTEN__
// 上报本次解码的视频帧的数据大小, 让上层准备好buffer
EM_ASM({
var isWorker = typeof importScripts == "function";
var bridge = (isWorker ? self : window)[UTF8ToString($0)];
if(bridge && typeof bridge["onVideoDataSize"] == "function"){
bridge["onVideoDataSize"]({
"size": $1,
});
}
}, _codec->bridgeName.c_str(), totalSize);
// 避免每次分配, js里面一次性分配好buffer
if(_codec->videoBuffer != nullptr){
#ifdef USE_OPEN_H265
// nothing to do
#elif defined(USE_OPEN_H264)
uint32_t startIndex = 0;
uint8_t *ptr = pDst[0];
uint32_t iWidth = width;
uint32_t iHeight = height;
for (uint32_t i = 0; i < iHeight; i++) {
memcpy(_codec->videoBuffer + startIndex, ptr, iWidth);
ptr += stride0;
startIndex += iWidth;
}
ptr = pDst[1];
iWidth = width / 2;
iHeight = height / 2;
for (uint32_t i = 0; i < iHeight; i++) {
memcpy(_codec->videoBuffer + startIndex, ptr, iWidth);
ptr += stride1;
startIndex += iWidth;
}
ptr = pDst[2];
iWidth = width / 2;
iHeight = height / 2;
for (uint32_t i = 0; i < iHeight; i++) {
memcpy(_codec->videoBuffer + startIndex, ptr, iWidth);
ptr += stride1;
startIndex += iWidth;
}
stride0 = width;
stride1 = height;
#else
memcpy(_codec->videoBuffer, picPtr, totalSize);
#endif
// 解码完成, 上报视频帧, 时间戳为tag里面的时间戳
EM_ASM({
var isWorker = typeof importScripts == "function";
var bridge = (isWorker ? self : window)[UTF8ToString($0)];
if(bridge && typeof bridge["onVideoData"] == "function"){
bridge["onVideoData"]({
"timestamp": $1,
"width": $2,
"height": $3,
"stride0": $4,
"stride1": $5
});
}
}, _codec->bridgeName.c_str(), timestamp, width, height, stride0, stride1);
}
#endif
}
// ###################流的最后,表明视频流结束#####################
} else if (tag.AVCPacketType == 2) {
#ifdef __EMSCRIPTEN__
EM_ASM({
var isWorker = typeof importScripts == "function";
var bridge = (isWorker ? self : window)[UTF8ToString($0)];
if(bridge && typeof bridge["onComplete"] == "function"){
bridge["onComplete"]();
}
}, _codec->bridgeName.c_str());
#endif
}
}
|