-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSampleVideoEncoder.cpp
More file actions
445 lines (374 loc) · 12.9 KB
/
Copy pathSampleVideoEncoder.cpp
File metadata and controls
445 lines (374 loc) · 12.9 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
// SPDX-License-Identifier: BSD-3-Clause-Clear
#include "QC/sample/SampleVideoEncoder.hpp"
namespace QC
{
namespace sample
{
void SampleVideoEncoder::OnDoneCb( const QCNodeEventInfo_t &eventInfo )
{
auto &fd = eventInfo.frameDesc;
auto &bufIn = fd.GetBuffer( QC_NODE_VIDEO_ENCODER_INPUT_BUFF_ID );
VideoFrameDescriptor *pBufIn = dynamic_cast<VideoFrameDescriptor *>( &bufIn );
if ( nullptr != pBufIn )
{
InFrameCallback( *pBufIn, eventInfo );
}
auto &bufOut = fd.GetBuffer( QC_NODE_VIDEO_ENCODER_OUTPUT_BUFF_ID );
VideoFrameDescriptor *pBufOut = dynamic_cast<VideoFrameDescriptor *>( &bufOut );
if ( nullptr != pBufOut )
{
std::unique_lock<std::mutex> l( m_lock );
m_frameOutQueue.push( *pBufOut );
m_condVar.notify_one();
// unlock @m_lock
}
}
void SampleVideoEncoder::InFrameCallback( VideoFrameDescriptor &inFrame,
const QCNodeEventInfo_t &eventInfo )
{
uint64_t dmaHandle = inFrame.dmaHandle;
QC_DEBUG( "Received input video frame dmaHandle 0x%" PRIx64 " from node %d, status %d",
dmaHandle, eventInfo.node, eventInfo.status, eventInfo.state );
TRACE_EVENT( SYSTRACE_EVENT_VENC_INPUT_DONE );
std::unique_lock<std::mutex> l( m_lock );
m_frameReleaseQueue.push( dmaHandle );
m_condVar.notify_one();
// unlock @m_lock
}
void SampleVideoEncoder::OutFrameCallback( VideoFrameDescriptor &outFrame )
{
uint64_t frameId = outFrame.appMarkData;
QC_DEBUG( "Received output video frame Id %" PRIu64, frameId );
DataFrames_t frames;
DataFrame_t frame;
SharedBuffer_t *pSharedBuffer = new SharedBuffer_t;
pSharedBuffer->SetBuffer( outFrame );
pSharedBuffer->pubHandle = 0;
std::shared_ptr<SharedBuffer_t> buffer( pSharedBuffer, [&]( SharedBuffer_t *pSharedBuffer ) {
VideoFrameDescriptor buffDesc;
buffDesc = pSharedBuffer->GetBuffer();
NodeFrameDescriptor frameDesc( QC_NODE_VIDEO_ENCODER_OUTPUT_BUFF_ID + 1 );
frameDesc.SetBuffer( QC_NODE_VIDEO_ENCODER_OUTPUT_BUFF_ID, buffDesc );
QC_DEBUG( "enc-out-buf back, handle:0x%x", buffDesc.dmaHandle );
m_encoder.ProcessFrameDescriptor( frameDesc );
delete pSharedBuffer;
} );
if ( false == m_frameInfoQueue.empty() )
{
FrameInfo info = m_frameInfoQueue.front();
m_frameInfoQueue.pop();
frame.frameId = info.frameId;
frame.buffer = buffer;
frame.timestamp = info.timestamp;
frames.Add( frame );
PROFILER_END();
TRACE_END( frame.frameId );
QC_DEBUG( "enc-outFrameCallback, frameId %" PRIu64 " tsNs:%" PRIu64
" type %d size %" PRIu32,
info.frameId, info.timestamp, outFrame.frameType, outFrame.GetDataSize() );
m_pub.Publish( frames );
}
else
{
/* for the first input-buffer, will produce two output buffer */
frame.frameId = outFrame.appMarkData;
frame.buffer = buffer;
frame.timestamp = outFrame.timestampNs;
frames.frames.push_back( frame );
TRACE_EVENT( SYSTRACE_EVENT_VENC_OUTPUT_WITH_2ND_FRAME );
QC_DEBUG( "enc-outFrameCallback, frame info queue is empty, frameId:%" PRIu64
" tsNs:%" PRIu64,
frame.frameId, frame.timestamp );
m_pub.Publish( frames );
}
}
QCStatus_e SampleVideoEncoder::Init( std::string name, SampleConfig_t &samplecfg )
{
QCStatus_e ret = SampleIF::Init( name );
if ( QC_STATUS_OK == ret )
{
ret = ParseConfig( samplecfg );
}
if ( QC_STATUS_OK == ret )
{
using std::placeholders::_1;
m_nodeCfg.config = m_dataTree.Dump();
m_nodeCfg.callback = std::bind( &SampleVideoEncoder::OnDoneCb, this, _1 );
}
ImageProps_t imgProp;
imgProp.format = m_outFormat;
imgProp.width = m_width;
imgProp.height = m_height;
imgProp.batchSize = 1;
imgProp.numPlanes = 1;
imgProp.planeBufSize[0] = m_bufSize;
imgProp.allocatorType = QC_MEMORY_ALLOCATOR_DMA_VPU;
ret = m_frameBufferPools.Init( m_name, m_nodeId, LOGGER_LEVEL_INFO, m_numOutputBufferReq,
imgProp );
if ( QC_STATUS_OK == ret )
{
ret = m_frameBufferPools.GetBuffers( m_nodeCfg.buffers );
}
if ( QC_STATUS_OK == ret )
{
ret = m_encoder.Initialize( m_nodeCfg );
}
if ( QC_STATUS_OK == ret )
{
ret = m_sub.Init( name, m_inputTopicName );
}
if ( QC_STATUS_OK == ret )
{
ret = m_pub.Init( name, m_outputTopicName );
}
return ret;
}
QCStatus_e SampleVideoEncoder::Start()
{
QCStatus_e ret = QC_STATUS_OK;
TRACE_BEGIN( SYSTRACE_TASK_START );
ret = (QCStatus_e) m_encoder.Start();
TRACE_END( SYSTRACE_TASK_START );
if ( QC_STATUS_OK == ret )
{
m_stop = false;
m_thread = std::thread( &SampleVideoEncoder::ThreadMain, this );
m_threadProc = std::thread( &SampleVideoEncoder::ThreadProcMain, this );
}
return ret;
}
void SampleVideoEncoder::ThreadMain()
{
QCStatus_e ret;
NodeFrameDescriptor frameDesc( QC_NODE_VIDEO_ENCODER_INPUT_BUFF_ID + 1 ); // for input only
while ( false == m_stop )
{
DataFrames_t frames;
ret = m_sub.Receive( frames );
if ( QC_STATUS_OK == ret )
{
for ( DataFrame_t &frame : frames.frames )
{
QC_DEBUG( "Received frameId %" PRIu64 ", type %d, size %lu, timestamp %" PRIu64,
frame.frameId, frame.GetBufferType(), frame.GetDataSize(),
frame.timestamp );
VideoFrameDescriptor frameBuffer;
frameBuffer = frame.GetBuffer();
frameBuffer.timestampNs = frame.timestamp;
frameBuffer.appMarkData = frame.frameId;
ret = frameDesc.SetBuffer( QC_NODE_VIDEO_ENCODER_INPUT_BUFF_ID, frameBuffer );
if ( QC_STATUS_OK != ret )
{
break;
}
{
std::unique_lock<std::mutex> l( m_lock );
m_camFrameMap[frameBuffer.dmaHandle] = frame;
// unlock @m_lock
}
PROFILER_BEGIN();
TRACE_BEGIN( frame.frameId );
ret = m_encoder.ProcessFrameDescriptor( frameDesc );
if ( QC_STATUS_OK == ret )
{
QC_DEBUG( "Processed input frameId %" PRIu64 " successfully", frame.frameId );
FrameInfo info = { frame.frameId, frame.timestamp };
std::unique_lock<std::mutex> l( m_lock );
m_frameInfoQueue.push( info );
// unlock @m_lock
}
else
{
QC_ERROR( "failed to process input frameId %" PRIu64, frame.frameId );
std::unique_lock<std::mutex> l( m_lock );
m_camFrameMap.erase( frameBuffer.dmaHandle );
// unlock @m_lock
}
}
}
}
}
void SampleVideoEncoder::ThreadProcMain()
{
while ( false == m_stop )
{
std::unique_lock<std::mutex> l( m_lock );
(void) m_condVar.wait_for( l, std::chrono::milliseconds( 10 ) );
while ( false == m_frameReleaseQueue.empty() )
{
uint64_t dmaHandle;
dmaHandle = m_frameReleaseQueue.front();
m_frameReleaseQueue.pop();
auto it = m_camFrameMap.find( dmaHandle );
if ( it != m_camFrameMap.end() )
{ // release the input camera frame
QC_DEBUG( "release dmaHandle 0x%" PRIx64, dmaHandle );
m_camFrameMap.erase( dmaHandle );
}
else
{
QC_ERROR( "ThreadProcMain with invalid dmaHandle 0x%" PRIx64, dmaHandle );
}
}
while ( false == m_frameOutQueue.empty() )
{
VideoFrameDescriptor_t outFrame = m_frameOutQueue.front();
m_frameOutQueue.pop();
OutFrameCallback( outFrame );
}
}
}
QCStatus_e SampleVideoEncoder::Stop()
{
QCStatus_e ret = QC_STATUS_OK;
m_stop = true;
if ( m_thread.joinable() )
{
m_thread.join();
}
if ( m_threadProc.joinable() )
{
m_threadProc.join();
}
m_pub.Clear();
TRACE_BEGIN( SYSTRACE_TASK_STOP );
ret = (QCStatus_e) m_encoder.Stop();
TRACE_END( SYSTRACE_TASK_STOP );
m_camFrameMap.clear();
return ret;
}
QCStatus_e SampleVideoEncoder::Deinit()
{
QCStatus_e ret = QC_STATUS_OK;
TRACE_BEGIN( SYSTRACE_TASK_DEINIT );
ret = m_encoder.DeInitialize();
TRACE_END( SYSTRACE_TASK_DEINIT );
return ret;
}
QCStatus_e SampleVideoEncoder::ParseConfig( SampleConfig_t &config )
{
QCStatus_e ret = QC_STATUS_OK;
m_config.Set<std::string>( "name", m_name );
m_width = Get( config, "width", 0 );
if ( 0 == m_width )
{
QC_ERROR( "invalid width = %u", m_width );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
m_config.Set<uint32_t>( "width", m_width );
}
m_height = Get( config, "height", 0 );
if ( 0 == m_height )
{
QC_ERROR( "invalid height = %u", m_height );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
m_config.Set<uint32_t>( "height", m_height );
}
m_poolSize = Get( config, "pool_size", 4 );
m_numInputBufferReq = Get( config, "numInputBufferReq", m_poolSize );
if ( 0 == m_numInputBufferReq )
{
QC_ERROR( "invalid numInputBufferReq = %u", m_numInputBufferReq );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
m_config.Set<uint32_t>( "numInputBufferReq", m_numInputBufferReq );
}
m_numOutputBufferReq = Get( config, "numOutputBufferReq", m_numInputBufferReq );
if ( 0 == m_numOutputBufferReq )
{
QC_ERROR( "invalid numOutputBufferReq = %u", m_numOutputBufferReq );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
m_config.Set<uint32_t>( "numOutputBufferReq", m_numOutputBufferReq );
}
m_bitRate = Get( config, "bitrate", 8000000 );
if ( 0 == m_bitRate )
{
QC_ERROR( "invalid bitrate = %u", m_bitRate );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
m_config.Set<uint32_t>( "bitrate", m_bitRate );
}
m_frameRate = Get( config, "fps", 30 );
if ( 0 == m_frameRate )
{
QC_ERROR( "invalid fps = %u", m_frameRate );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
m_config.Set<uint32_t>( "frameRate", m_frameRate );
}
m_bSyncFrameSeqHdr = Get( config, "sync_frame", false );
m_config.Set<bool>( "sync_frame", m_bSyncFrameSeqHdr );
m_inputTopicName = Get( config, "input_topic", "" );
if ( "" == m_inputTopicName )
{
QC_ERROR( "no input topic" );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
}
m_bufSize = Get( config, "buffer_size", ( 2 * m_width * m_height ) );
if ( 0 == m_bufSize )
{
QC_ERROR( "invalid buffer_size = %u", m_bufSize );
ret = QC_STATUS_BAD_ARGUMENTS;
}
m_outputTopicName = Get( config, "output_topic", "" );
if ( "" == m_outputTopicName )
{
QC_ERROR( "no output topic" );
ret = QC_STATUS_BAD_ARGUMENTS;
}
else
{
}
m_inFormat = Get( config, "format", QC_IMAGE_FORMAT_NV12 );
if ( QC_IMAGE_FORMAT_MAX == m_inFormat ||
( m_inFormat != QC_IMAGE_FORMAT_NV12 && m_inFormat != QC_IMAGE_FORMAT_NV12_UBWC ) )
{
QC_ERROR( "invalid format for input stream" );
ret = QC_STATUS_BAD_ARGUMENTS;
}
m_outFormat = Get( config, "output_format", QC_IMAGE_FORMAT_COMPRESSED_H265 );
if ( QC_IMAGE_FORMAT_MAX == m_outFormat || ( m_outFormat != QC_IMAGE_FORMAT_COMPRESSED_H264 &&
m_outFormat != QC_IMAGE_FORMAT_COMPRESSED_H265 ) )
{
QC_ERROR( "invalid format for output stream" );
ret = QC_STATUS_BAD_ARGUMENTS;
}
m_config.Set<std::string>( "inputImageFormat", Get( config, "format", "nv12" ) );
m_config.Set<std::string>( "outputImageFormat", Get( config, "output_format", "h265" ) );
m_config.Set<uint32_t>( "gop", Get( config, "gop", 20 ) );
m_config.Set<bool>( "bInputDynamicMode", true );
m_config.Set<bool>( "bOutputDynamicMode", false );
std::string dftProfile = "HEVC_MAIN";
if ( QC_IMAGE_FORMAT_COMPRESSED_H264 == m_outFormat )
{
dftProfile = "H264_MAIN";
}
m_config.Set<std::string>( "profile", Get( config, "profile", dftProfile ) );
m_dataTree.Set( "static", m_config );
return ret;
}
const uint32_t SampleVideoEncoder::GetVersion() const
{
return QCNODE_VIDEOENCODER_VERSION;
}
REGISTER_SAMPLE( VideoEncoder, SampleVideoEncoder );
} // namespace sample
} // namespace QC