-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobs-vce-amf.cpp
More file actions
371 lines (329 loc) · 11.4 KB
/
Copy pathobs-vce-amf.cpp
File metadata and controls
371 lines (329 loc) · 11.4 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/******************************************************************************
Copyright (C) 2015 by Sean Nelson <audiohacked@gmail.com>
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-vce.h"
#define ENC_WIDTH obs_encoder_get_width(obs_vce->encoder)
#define ENC_HEIGHT obs_encoder_get_height(obs_vce->encoder)
class VCEDeviceD3D11;
const char *obs_vce_amf_getname(void)
{
return "AMD Video Codec Engine: AMF";
}
void *obs_vce_amf_create(obs_data_t *settings, obs_encoder_t *encoder)
{
UNUSED_PARAMETER(settings);
struct obs_amd *obs_vce = reinterpret_cast<struct obs_amd*>(bzalloc(sizeof(struct obs_amd)));
obs_vce->encoder = encoder;
AMF_RESULT amfReturn = AMF_OK;
struct obs_vce_amf *ova = reinterpret_cast<struct obs_vce_amf*>(bzalloc(sizeof(struct obs_vce_amf)));
obs_vce->vid_out_info = video_output_get_info(obs_get_video());
//struct obs_video_info *ovi;
//bool got_video = obs_get_video_info(ovi);
//if (!got_video)
// warn("No active video");
/* Create and Initialize context
* Allocate via AMFCreateContext()
* Set Device via
*/
amfReturn = AMFCreateContext(&obs_vce->context);
if (amfReturn != AMF_OK) {
warn("Could not create AMF Context!");
}
if (gs_get_device_type() == GS_DEVICE_DIRECT3D_11) {
debug("DX11 device and AMF");
obs_vce->context->InitDX11(NULL);
}
else if (gs_get_device_type() == GS_DEVICE_OPENGL) {
debug("OpenGL device and AMF");
}
else {
debug("Unknown device and AMF");
obs_vce_amf_d3d11_init(obs_vce, ova, 0);
obs_vce->context->InitDX11(obs_vce->dx11_device);
}
/* Create Component via AMFCreateComponent
*/
AMFCreateComponent(obs_vce->context, AMFVideoEncoderVCE_AVC, &obs_vce->vce_encoder);
if (amfReturn != AMF_OK)
warn("Could not create AMF VCE Component");
/* Initialize Encoder component
* Set all optional properties on component:
* AMFPropertyStorage::SetProperty()
* Init component:
* AMFComponent::Init()
*/
// Static properties - can be set befor Init()
// amf_int64(AMF_VIDEO_ENCODER_USAGE_ENUM); default = N/A; Encoder usage type. fully configures parameter set.
amfReturn = obs_vce->vce_encoder->SetProperty(AMF_VIDEO_ENCODER_USAGE, amf_int64(AMF_VIDEO_ENCODER_USAGE_LOW_LATENCY));
if (amfReturn != AMF_OK)
warn("Unable to set encoder usage type");
// amf_int64(AMF_VIDEO_ENCODER_PROFILE_ENUM) ; default = AMF_VIDEO_ENCODER_PROFILE_MAIN; H264 profile
amfReturn = obs_vce->vce_encoder->SetProperty(AMF_VIDEO_ENCODER_PROFILE, amf_int64(AMF_VIDEO_ENCODER_PROFILE_MAIN));
if (amfReturn != AMF_OK)
warn("Unable to set encoder profile");
// amf_int64; default = 42; H264 profile level
amfReturn = obs_vce->vce_encoder->SetProperty(AMF_VIDEO_ENCODER_PROFILE_LEVEL, amf_int64(41));
if (amfReturn != AMF_OK)
warn("Unable to set encoder profile level");
// amf_int64(AMF_VIDEO_ENCODER_QUALITY_PRESET_ENUM); default = depends on USAGE; Quality Preset
amfReturn = obs_vce->vce_encoder->SetProperty(AMF_VIDEO_ENCODER_QUALITY_PRESET, amf_int64(AMF_VIDEO_ENCODER_QUALITY_PRESET_SPEED));
if (amfReturn != AMF_OK)
warn("Unable to set encoder quality preset");
// AMFSize; default = 0,0; Frame size
// obs_vce->vce_encoder->SetProperty(amf::AMF_VIDEO_ENCODER_FRAMESIZE, );
// AMFRate; default = depends on usage; Frame Rate
// obs_vce->vce_encoder->SetProperty(amf::AMF_VIDEO_ENCODER_FRAMERATE, );
// AMFInterface* - > AMFBuffer*; SPS/PPS buffer - read-only
// obs_vce->vce_encoder->SetProperty(amf::AMF_VIDEO_ENCODER_EXTRADATA, );
if (obs_vce->vid_out_info->format == VIDEO_FORMAT_NV12) {
obs_vce->vce_format = amf::AMF_SURFACE_NV12;
}
amfReturn = obs_vce->vce_encoder->Init(obs_vce->vce_format, amf_int32(ENC_WIDTH), amf_int32(ENC_HEIGHT));
if (amfReturn != AMF_OK)
warn("Unable to Init Encoder");
obs_vce->performance_token =
os_request_high_performance("vce_amf encoding");
return obs_vce;
}
bool obs_vce_amf_encode(void *data, struct encoder_frame *frame,
struct encoder_packet *packet, bool *received_packet)
{
struct obs_amd *obs_vce = reinterpret_cast<struct obs_amd*>(data);
amf::AMFDataPtr outData;
amf::AMFBufferPtr pBuffer;
AMF_RESULT amfReturn = AMF_OK;
debug("AMF Encode");
if (!frame || !packet || !received_packet) {
warn("Data missing");
return false;
}
/* Create input data object
* Allocate input surface with allocated internally surface
* AMFContext::AllocSurface()
* or Allocate input surface with attached
* AMFContext::CreateSurfaceFrom<>
* Copy input data object using native data-access functionality
* AMFSurface::GetPlane(), AMFPlane::GetNative()
*/
debug("Allocating Surface");
if (gs_get_device_type() == GS_DEVICE_DIRECT3D_11) {
debug("Using DX11 Device for AMF Surface");
//obs_vce->context->CreateSurfaceFromDX11Native();
}
else if (gs_get_device_type() == GS_DEVICE_OPENGL) {
debug("Using OpenGL Device for AMF Surface");
//obs_vce->context->CreateSurfaceFromOpenGLNative();
}
else {
debug("AllocSurface");
amfReturn = obs_vce->context->AllocSurface(amf::AMF_MEMORY_HOST,
obs_vce->vce_format, ENC_WIDTH, ENC_HEIGHT,
&obs_vce->vce_input);
obs_amf_result(obs_vce, amfReturn);
}
if (frame)
init_pic_data(obs_vce, frame);
obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_END_OF_SEQUENCE, false);
obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_END_OF_STREAM, false);
// obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_FORCE_PICTURE_TYPE, AMF_VIDEO_ENCODER_PICTURE_TYPE_IDR);
obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_INSERT_AUD, false);
obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_INSERT_SPS, false);
obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_INSERT_PPS, false);
obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_PICTURE_STRUCTURE, AMF_VIDEO_ENCODER_PICTURE_STRUCTURE_FRAME);
// obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_MARK_CURRENT_WITH_LTR_INDEX, -1);
// obs_vce->vce_input->SetProperty(AMF_VIDEO_ENCODER_FORCE_LTR_REFERENCE_BITFIELD, 0);
/* Submit data object to encoder
* Set additional parameters on the data object
* (e.g. some application ID if needed) using
* AMFPropertyStorage::SetProperty()
* Submits data to component by
* AMFComponent::SubmitInput()
*/
debug("SubmitInput to VCE");
amfReturn = obs_vce->vce_encoder->SubmitInput(obs_vce->vce_input);
obs_amf_result(obs_vce, amfReturn);
while (amfReturn == AMF_INPUT_FULL) {
os_sleep_ms(1);
amfReturn = obs_vce->vce_encoder->SubmitInput(obs_vce->vce_input);
}
/* Queries for results (likely in a separate thread) by
* AMFComponent::QueryOutput
*/
debug("QueryOutput from VCE");
amfReturn = obs_vce->vce_encoder->QueryOutput(&outData);
obs_amf_result(obs_vce, amfReturn);
while (amfReturn == AMF_REPEAT) {
amfReturn = obs_vce->vce_encoder->QueryOutput(&outData);
}
if (amfReturn == AMF_OK) {
pBuffer = amf::AMFBufferPtr(outData);
pBuffer->Convert(amf::AMF_MEMORY_HOST);
parse_packet(obs_vce, packet, pBuffer);
debug("setting received_packet");
*received_packet = false;
return true;
}
return false;
}
void obs_vce_amf_destroy(void *data)
{
struct obs_amd *obs_vce = reinterpret_cast<struct obs_amd*>(data);
amf::AMFDataPtr amfdata;
AMF_RESULT amfReturn = AMF_OK;
if (obs_vce) {
/* At the end of the file execute 'drain' to force the component to
* return all accumulated frames: AMFComponent::Drain()
*/
amfReturn = obs_vce->vce_encoder->Drain();
obs_amf_result(obs_vce, amfReturn);
/* Checks for EOF error returning from QueryResult() to detect end of drain
*/
amfReturn = obs_vce->vce_encoder->QueryOutput(reinterpret_cast<amf::AMFData**>(&obs_vce->vce_output));
obs_amf_result(obs_vce, amfReturn);
obs_vce->vce_input->Release();
//obs_vce->vce_output->Release();
/* Terminate component and context
* Terminate component and release all internal resources by AMFComponent::Terminate()
* Terminate context by AMFContext::Terminate()
*/
obs_vce->vce_encoder->Terminate();
obs_vce->context->Terminate();
/* Clean up OBS data
*/
os_end_high_performance(obs_vce->performance_token);
bfree(obs_vce->sei);
bfree(obs_vce->extra_data);
obs_vce->sei = NULL;
obs_vce->extra_data = NULL;
da_free(obs_vce->packet_data);
bfree(obs_vce);
}
}
void obs_amf_result(struct obs_amd *obs_vce, AMF_RESULT amf_res)
{
char* string = NULL;
switch (amf_res) {
case AMF_OK:
string = "Ok!";
break;
case AMF_FAIL:
string = "Failed!";
break;
case AMF_UNEXPECTED:
string = "Unexpected!";
break;
case AMF_ACCESS_DENIED:
string = "Access Denied!";
break;
case AMF_INVALID_ARG:
string = "Invalid Arg";
break;
case AMF_OUT_OF_RANGE:
string = "Out Of Memory";
break;
case AMF_OUT_OF_MEMORY:
string = "Out Of Memory";
break;
case AMF_INVALID_POINTER:
string = "Invalid Pointer";
break;
case AMF_NO_INTERFACE:
string = "No Interface";
break;
case AMF_NOT_IMPLEMENTED:
string = "Not Implemented";
break;
case AMF_NOT_SUPPORTED:
string = "Not Supported";
break;
case AMF_NOT_FOUND:
string = "Not Found";
break;
case AMF_ALREADY_INITIALIZED:
string = "Already Initialized";
break;
case AMF_NOT_INITIALIZED:
string = "Not Initialized";
break;
case AMF_INVALID_FORMAT:// invalid data format
string = "Invalid Format";
break;
case AMF_WRONG_STATE:
string = "Wrong State";
break;
case AMF_FILE_NOT_OPEN: // cannot open file
string = "File Not Open";
break;
// device common codes
case AMF_NO_DEVICE:
string = "No Device";
break;
// device directx
case AMF_DIRECTX_FAILED:
string = "DirectX Failed";
break;
// device opencl
case AMF_OPENCL_FAILED:
string = "OpenCL Failed";
break;
// device opengl
case AMF_GLX_FAILED: //failed to use GLX
string = "GLX Failed";
break;
// device alsa
case AMF_ALSA_FAILED: //failed to use ALSA
string = "Alsa Failed";
break;
case AMF_EOF:
string = "End of File";
break;
case AMF_REPEAT:
string = "Repeat";
break;
case AMF_NEED_MORE_INPUT: //returned by AMFComponent::QueryOutput if more frames to be submited
string = "Need More Input";
break;
case AMF_INPUT_FULL: //returned by AMFComponent::SubmitInput if input queue is full
string = "Input Full";
break;
case AMF_RESOLUTION_CHANGED: //resolution changed client needs to Drain / Terminate / Init
string = "Resolution Changed";
break;
//error codes
case AMF_INVALID_DATA_TYPE:
string = "Invalid Data Type";
break;
case AMF_INVALID_RESOLUTION:
string = "Invalid Resolution";
break;
case AMF_CODEC_NOT_SUPPORTED:
string = "Codec Not Supported";
break;
case AMF_SURFACE_FORMAT_NOT_SUPPORTED:
string = "Surface Format Not Supported";
break;
//component video encoder
case AMF_ENCODER_NOT_PRESENT:
string = "Encoder Not Present";
break;
default:
string = "Unknown";
break;
}
if (amf_res == AMF_OK) {
debug("AMF_RESULT: %s", string);
}
else {
warn("AMF_RESULT: %s", string);
}
}