forked from facebookarchive/360-Capture-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenH264Encoder.cpp
208 lines (177 loc) · 6.45 KB
/
OpenH264Encoder.cpp
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
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "OpenH264Encoder.h"
#include "Graphics/TextureRender.h"
#include "Graphics/ScreenVertexShader.h"
using namespace FBCapture::Render;
namespace FBCapture {
namespace Video {
OpenH264Encoder::OpenH264Encoder(ID3D11Device* device) {
device_ = device;
encodingInitiated_ = false;
}
FBCAPTURE_STATUS OpenH264Encoder::initEncodingSession() {
int32_t ret = 0;
if (svcEncoder_) {
return FBCAPTURE_STATUS_OK;
}
ret = WelsCreateSVCEncoder(&svcEncoder_);
if (ret != cmResultSuccess) {
DEBUG_ERROR_VAR("Failed to create encoder. [Error Code] ", to_string(ret));
return FBCAPTURE_STATUS_ENCODER_CREATION_FAILED;
}
return FBCAPTURE_STATUS_OK;
}
FBCAPTURE_STATUS OpenH264Encoder::setEncodeConfigs(const wstring& fullSavePath,
uint32_t width,
uint32_t height,
int bitrate,
int fps) {
int32_t ret = 0;
uint32_t kiPicResSize = width * height * 3 >> 1;
pYUV420_ = new uint8_t[kiPicResSize];
// Set source picture configures
memset(&srcPic_, 0, sizeof(SSourcePicture));
srcPic_.iColorFormat = videoFormatI420;
srcPic_.uiTimeStamp = 0;
srcPic_.iPicWidth = width;
srcPic_.iPicHeight = height;
srcPic_.iStride[0] = srcPic_.iPicWidth;
srcPic_.iStride[1] = srcPic_.iStride[2] = srcPic_.iStride[0] >> 1;
srcPic_.pData[0] = pYUV420_;
srcPic_.pData[1] = srcPic_.pData[0] + (srcPic_.iPicWidth * srcPic_.iPicHeight);
srcPic_.pData[2] = srcPic_.pData[1] + (srcPic_.iPicWidth * srcPic_.iPicHeight >> 2);
// Set encoding configs
svcEncoder_->GetDefaultParams(&encodeConfig_);
encodeConfig_.iUsageType = SCREEN_CONTENT_REAL_TIME;
encodeConfig_.fMaxFrameRate = static_cast<float>(fps);
encodeConfig_.iTargetBitrate = bitrate;
encodeConfig_.iPicWidth = width;
encodeConfig_.iPicHeight = height;
encodeConfig_.iRCMode = RC_QUALITY_MODE;
encodeConfig_.iTemporalLayerNum = 1;
for (int layer = 0; layer < encodeConfig_.iSpatialLayerNum; layer++) {
encodeConfig_.sSpatialLayers[layer].iVideoWidth = width;
encodeConfig_.sSpatialLayers[layer].iVideoHeight = height;
encodeConfig_.sSpatialLayers[layer].fFrameRate = static_cast<float>(fps);
encodeConfig_.sSpatialLayers[layer].iSpatialBitrate = bitrate;
encodeConfig_.sSpatialLayers[layer].iMaxSpatialBitrate = UNSPECIFIED_BIT_RATE;
encodeConfig_.sSpatialLayers[layer].sSliceArgument.uiSliceMode = SM_SINGLE_SLICE;
}
ret = svcEncoder_->InitializeExt(&encodeConfig_);
if (ret != cmResultSuccess) {
DEBUG_ERROR_VAR("Failed on initilaizing encoder with given extension parameters. [Error Code] ",
to_string(ret));
return FBCAPTURE_STATUS_ENCODE_INIT_FAILED;
}
file_ = _wfopen(fullSavePath.c_str(), L"wb");
if (file_ == NULL) {
DEBUG_ERROR("Failed on creating file with _wfopen");
return FBCAPTURE_STATUS_OUTPUT_FILE_CREATION_FAILED;
}
return FBCAPTURE_STATUS_OK;
}
FBCAPTURE_STATUS OpenH264Encoder::encodeProcess(const void* texturePtr,
const wstring& fullSavePath,
int bitrate,
int fps,
bool needFlipping) {
FBCAPTURE_STATUS status = FBCAPTURE_STATUS_OK;
int32_t ret = 0;
status = createD3D11Resources((ID3D11Texture2D*)texturePtr, encodingTexure_);
if (status != FBCAPTURE_STATUS_OK) {
DEBUG_ERROR("Failed to create texture");
return status;
}
// Initialize Encoder
if (!encodingInitiated_) {
status = setEncodeConfigs(fullSavePath, globalTexDesc_.Width, globalTexDesc_.Height, bitrate, fps); // Set Encode Configs
if (status != FBCAPTURE_STATUS_OK) {
return status;
}
setTextureDirtyRegion();
encodingInitiated_ = true;
}
D3D11_MAPPED_SUBRESOURCE resource = {};
status = mapTexture(resource);
if (status != FBCAPTURE_STATUS_OK) {
DEBUG_ERROR("Failed on context mapping");
return status;
}
// TODO Write seperate shader doing chroma subsampling.
chromaSubSamplingFromYUV444toYUV420(reinterpret_cast<byte*>(resource.pData), reinterpret_cast<byte*>(pYUV420_), srcPic_.iPicWidth, srcPic_.iPicHeight);
// To encoder this frame
memset(&frameBSInfo_, 0, sizeof(SFrameBSInfo));
ret = svcEncoder_->EncodeFrame(&srcPic_, &frameBSInfo_);
if (ret == cmResultSuccess) {
int layer = 0;
int frameSize = 0;
while (layer < frameBSInfo_.iLayerNum) {
SLayerBSInfo* pLayerBsInfo = &frameBSInfo_.sLayerInfo[layer];
if (pLayerBsInfo != NULL) {
int layerSize = 0;
int nalIdx = pLayerBsInfo->iNalCount - 1;
do {
layerSize += pLayerBsInfo->pNalLengthInByte[nalIdx];
--nalIdx;
} while (nalIdx >= 0);
fwrite(pLayerBsInfo->pBsBuf, 1, layerSize, file_); // write pure bit stream into file
frameSize += layerSize;
}
++layer;
}
} else {
DEBUG_ERROR_VAR("Failed on encoding frame. [Error Code] ", to_string(ret));
return FBCAPTURE_STATUS_ENCODE_PICTURE_FAILED;
}
return status;
}
void OpenH264Encoder::chromaSubSamplingFromYUV444toYUV420(byte* yuv444, byte* yuv420, int width, int height) {
int frameSize = width * height;
int chromasize = frameSize / 4;
int yIndex = 0;
int uIndex = frameSize;
int vIndex = frameSize + chromasize;
int R = 0, G = 0, B = 0, Y = 0, U = 0, V = 0;
int yuv444Index = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
Y = yuv444[yuv444Index++];
U = yuv444[yuv444Index++];
V = yuv444[yuv444Index++];
yuv444Index++; // Skip Alpha channel
yuv420[yIndex++] = Y;
// TODO Consider interplorating multiple neighbor's UV vales to get better UV on YUV420 planar
if (j % 2 == 0 && i % 2 == 0) {
yuv420[uIndex++] = U;
yuv420[vIndex++] = V;
}
}
}
}
FBCAPTURE_STATUS OpenH264Encoder::flushInputTextures() {
encodingInitiated_ = false;
if (file_) {
fclose(file_);
file_ = nullptr;
}
if (pYUV420_) {
delete[] pYUV420_;
pYUV420_ = nullptr;
}
if (svcEncoder_) {
svcEncoder_->Uninitialize();
WelsDestroySVCEncoder(svcEncoder_);
svcEncoder_ = nullptr;
}
return FBCAPTURE_STATUS_OK;
}
}
}