forked from jocover/jetson-ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvmpi_enc.cpp
459 lines (363 loc) · 12.2 KB
/
nvmpi_enc.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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "nvmpi.h"
#include "NvVideoEncoder.h"
#include "nvbuf_utils.h"
#include <vector>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <queue>
#define CHUNK_SIZE 2*1024*1024
#define MAX_BUFFERS 32
#define TEST_ERROR(condition, message, errorCode) \
if (condition) \
{ \
std::cout<< message; \
}
using namespace std;
struct nvmpictx{
NvVideoEncoder *enc;
int index;
std::queue<int> * packet_pools;
uint32_t width;
uint32_t height;
uint32_t profile;
bool enableLossless;
uint32_t bitrate;
uint32_t peak_bitrate;
uint32_t raw_pixfmt;
uint32_t encoder_pixfmt;
enum v4l2_mpeg_video_bitrate_mode ratecontrol;
enum v4l2_mpeg_video_h264_level level;
enum v4l2_enc_hw_preset_type hw_preset_type;
uint32_t iframe_interval;
uint32_t idr_interval;
uint32_t fps_n;
uint32_t fps_d;
bool enable_extended_colorformat;
uint32_t qmax;
uint32_t qmin;
uint32_t num_b_frames;
uint32_t num_reference_frames;
bool insert_sps_pps_at_idr;
uint32_t packets_buf_size;
uint32_t packets_num;
unsigned char * packets[MAX_BUFFERS];
uint32_t packets_size[MAX_BUFFERS];
bool packets_keyflag[MAX_BUFFERS];
uint64_t timestamp[MAX_BUFFERS];
int buf_index;
};
static bool encoder_capture_plane_dq_callback(struct v4l2_buffer *v4l2_buf, NvBuffer * buffer, NvBuffer * shared_buffer, void *arg){
nvmpictx *ctx = (nvmpictx *) arg;
NvVideoEncoder *enc = ctx->enc;
//uint32_t frame_num = ctx->enc->capture_plane.getTotalDequeuedBuffers() - 1;
if (v4l2_buf == NULL)
{
cout << "Error while dequeing buffer from output plane" << endl;
return false;
}
if (buffer->planes[0].bytesused == 0)
{
cout << "Got 0 size buffer in capture \n";
return false;
}
if(ctx->packets_buf_size < buffer->planes[0].bytesused){
ctx->packets_buf_size=buffer->planes[0].bytesused;
for(int index=0;index< ctx->packets_num;index++){
delete[] ctx->packets[index];
ctx->packets[index]=new unsigned char[ctx->packets_buf_size];
}
}
ctx->packets_size[ctx->buf_index]=buffer->planes[0].bytesused;
memcpy(ctx->packets[ctx->buf_index],buffer->planes[0].data,buffer->planes[0].bytesused);
ctx->timestamp[ctx->buf_index] = (v4l2_buf->timestamp.tv_usec % 1000000) + (v4l2_buf->timestamp.tv_sec * 1000000UL);
ctx->packet_pools->push(ctx->buf_index);
v4l2_ctrl_videoenc_outputbuf_metadata enc_metadata;
ctx->enc->getMetadata(v4l2_buf->index, enc_metadata);
if(enc_metadata.KeyFrame){
ctx->packets_keyflag[ctx->buf_index]=true;
}else{
ctx->packets_keyflag[ctx->buf_index]=false;
}
ctx->buf_index=(ctx->buf_index+1)%ctx->packets_num;
if (ctx->enc->capture_plane.qBuffer(*v4l2_buf, NULL) < 0)
{
ERROR_MSG("Error while Qing buffer at capture plane");
return false;
}
return true;
}
nvmpictx* nvmpi_create_encoder(nvCodingType codingType,nvEncParam * param){
int ret;
log_level = LOG_LEVEL_INFO;
nvmpictx *ctx=new nvmpictx;
ctx->index=0;
ctx->width=param->width;
ctx->height=param->height;
ctx->enableLossless=false;
ctx->bitrate=param->bitrate;
ctx->ratecontrol = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
ctx->idr_interval = param->idr_interval;
ctx->fps_n = param->fps_n;
ctx->fps_d = param->fps_d;
ctx->iframe_interval = param->iframe_interval;
ctx->packet_pools=new std::queue<int>;
ctx->buf_index=0;
ctx->enable_extended_colorformat=false;
ctx->packets_num=param->capture_num;
ctx->qmax=param->qmax;
ctx->qmin=param->qmin;
ctx->num_b_frames=param->max_b_frames;
ctx->num_reference_frames=param->refs;
ctx->insert_sps_pps_at_idr=(param->insert_spspps_idr==1)?true:false;
switch(param->profile){
case 77://FF_PROFILE_H264_MAIN
ctx->profile=V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
break;
case 66://FF_PROFILE_H264_BASELINE
ctx->profile=V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
break;
case 100://FF_PROFILE_H264_HIGH
ctx->profile=V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
break;
default:
ctx->profile=V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
break;
}
switch(param->level){
case 10:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
break;
case 11:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
break;
case 12:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_1_2;
break;
case 13:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_1_3;
break;
case 20:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_2_0;
break;
case 21:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
break;
case 22:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
break;
case 30:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_3_0;
break;
case 31:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
break;
case 32:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
break;
case 40:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
break;
case 41:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
break;
case 42:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
break;
case 50:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
break;
case 51:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
break;
default:
ctx->level=V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
break;
}
switch(param->hw_preset_type){
case 1:
ctx->hw_preset_type = V4L2_ENC_HW_PRESET_ULTRAFAST;
break;
case 2:
ctx->hw_preset_type = V4L2_ENC_HW_PRESET_FAST;
break;
case 3:
ctx->hw_preset_type = V4L2_ENC_HW_PRESET_MEDIUM;
break;
case 4:
ctx->hw_preset_type = V4L2_ENC_HW_PRESET_SLOW;
break;
default:
ctx->hw_preset_type = V4L2_ENC_HW_PRESET_MEDIUM;
break;
}
if(param->enableLossless)
ctx->enableLossless=true;
if(param->mode_vbr)
ctx->ratecontrol=V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
ctx->packets_buf_size=CHUNK_SIZE;
for(int index=0;index<MAX_BUFFERS;index++)
ctx->packets[index]=new unsigned char[ctx->packets_buf_size];
if(codingType==NV_VIDEO_CodingH264){
ctx->encoder_pixfmt=V4L2_PIX_FMT_H264;
}else if(codingType==NV_VIDEO_CodingHEVC){
ctx->encoder_pixfmt=V4L2_PIX_FMT_H265;
}
ctx->enc=NvVideoEncoder::createVideoEncoder("enc0");
TEST_ERROR(!ctx->enc, "Could not create encoder",ret);
ret = ctx->enc->setCapturePlaneFormat(ctx->encoder_pixfmt, ctx->width,ctx->height, CHUNK_SIZE);
TEST_ERROR(ret < 0, "Could not set output plane format", ret);
switch (ctx->profile)
{
case V4L2_MPEG_VIDEO_H265_PROFILE_MAIN10:
ctx->raw_pixfmt = V4L2_PIX_FMT_P010M;
break;
case V4L2_MPEG_VIDEO_H265_PROFILE_MAIN:
default:
ctx->raw_pixfmt = V4L2_PIX_FMT_YUV420M;
}
if (ctx->enableLossless && codingType == NV_VIDEO_CodingH264)
{
ctx->profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_444_PREDICTIVE;
ret = ctx->enc->setOutputPlaneFormat(V4L2_PIX_FMT_YUV444M, ctx->width,ctx->height);
}
else{
ret = ctx->enc->setOutputPlaneFormat(ctx->raw_pixfmt, ctx->width,ctx->height);
}
TEST_ERROR(ret < 0, "Could not set output plane format", ret);
ret = ctx->enc->setBitrate(ctx->bitrate);
TEST_ERROR(ret < 0, "Could not set encoder bitrate", ret);
ret=ctx->enc->setHWPresetType(ctx->hw_preset_type);
TEST_ERROR(ret < 0, "Could not set encoder HW Preset Type", ret);
if(ctx->num_reference_frames){
ret = ctx->enc->setNumReferenceFrames(ctx->num_reference_frames);
TEST_ERROR(ret < 0, "Could not set num reference frames", ret);
}
if(ctx->num_b_frames != (uint32_t) -1 && codingType == NV_VIDEO_CodingH264 ){
ret = ctx->enc->setNumBFrames(ctx->num_b_frames);
TEST_ERROR(ret < 0, "Could not set number of B Frames", ret);
}
if (codingType == NV_VIDEO_CodingH264 || codingType == NV_VIDEO_CodingHEVC)
{
ret = ctx->enc->setProfile(ctx->profile);
TEST_ERROR(ret < 0, "Could not set encoder profile", ret);
}
if( codingType== NV_VIDEO_CodingH264){
ret = ctx->enc->setLevel(ctx->level);
TEST_ERROR(ret < 0, "Could not set encoder level", ret);
}
if (ctx->enableLossless){
ret = ctx->enc->setConstantQp(0);
TEST_ERROR(ret < 0, "Could not set encoder constant qp=0", ret);
}else{
ret = ctx->enc->setRateControlMode(ctx->ratecontrol);
TEST_ERROR(ret < 0, "Could not set encoder rate control mode", ret);
if (ctx->ratecontrol == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR){
uint32_t peak_bitrate;
if (ctx->peak_bitrate < ctx->bitrate)
peak_bitrate = 1.2f * ctx->bitrate;
else
peak_bitrate = ctx->peak_bitrate;
ret = ctx->enc->setPeakBitrate(peak_bitrate);
TEST_ERROR(ret < 0, "Could not set encoder peak bitrate", ret);
}
}
ret = ctx->enc->setIDRInterval(ctx->idr_interval);
TEST_ERROR(ret < 0, "Could not set encoder IDR interval", ret);
if(ctx->qmax>0 ||ctx->qmin >0){
ctx->enc->setQpRange(ctx->qmin, ctx->qmax, ctx->qmin,ctx->qmax, ctx->qmin, ctx->qmax);
}
ret = ctx->enc->setIFrameInterval(ctx->iframe_interval);
TEST_ERROR(ret < 0, "Could not set encoder I-Frame interval", ret);
if(ctx->insert_sps_pps_at_idr){
ret = ctx->enc->setInsertSpsPpsAtIdrEnabled(true);
TEST_ERROR(ret < 0, "Could not set insertSPSPPSAtIDR", ret);
}
ret = ctx->enc->setFrameRate(ctx->fps_n, ctx->fps_d);
TEST_ERROR(ret < 0, "Could not set framerate", ret);
ret = ctx->enc->output_plane.setupPlane(V4L2_MEMORY_USERPTR, ctx->packets_num, false, true);
TEST_ERROR(ret < 0, "Could not setup output plane", ret);
ret = ctx->enc->capture_plane.setupPlane(V4L2_MEMORY_MMAP, ctx->packets_num, true, false);
TEST_ERROR(ret < 0, "Could not setup capture plane", ret);
ret = ctx->enc->subscribeEvent(V4L2_EVENT_EOS,0,0);
TEST_ERROR(ret < 0, "Could not subscribe EOS event", ret);
ret = ctx->enc->output_plane.setStreamStatus(true);
TEST_ERROR(ret < 0, "Error in output plane streamon", ret);
ret = ctx->enc->capture_plane.setStreamStatus(true);
TEST_ERROR(ret < 0, "Error in capture plane streamon", ret);
ctx->enc->capture_plane.setDQThreadCallback(encoder_capture_plane_dq_callback);
ctx->enc->capture_plane.startDQThread(ctx);
// Enqueue all the empty capture plane buffers
for (uint32_t i = 0; i < ctx->enc->capture_plane.getNumBuffers(); i++){
struct v4l2_buffer v4l2_buf;
struct v4l2_plane planes[MAX_PLANES];
memset(&v4l2_buf, 0, sizeof(v4l2_buf));
memset(planes, 0, MAX_PLANES * sizeof(struct v4l2_plane));
v4l2_buf.index = i;
v4l2_buf.m.planes = planes;
ret = ctx->enc->capture_plane.qBuffer(v4l2_buf, NULL);
TEST_ERROR(ret < 0, "Error while queueing buffer at capture plane", ret);
}
return ctx;
}
int nvmpi_encoder_put_frame(nvmpictx* ctx,nvFrame* frame){
int ret;
struct v4l2_buffer v4l2_buf;
struct v4l2_plane planes[MAX_PLANES];
NvBuffer *nvBuffer;
memset(&v4l2_buf, 0, sizeof(v4l2_buf));
memset(planes, 0, sizeof(planes));
v4l2_buf.m.planes = planes;
if(ctx->enc->isInError())
return -1;
if(ctx->index < ctx->enc->output_plane.getNumBuffers()){
nvBuffer=ctx->enc->output_plane.getNthBuffer(ctx->index);
v4l2_buf.index = ctx->index ;
ctx->index++;
}else{
ret = ctx->enc->output_plane.dqBuffer(v4l2_buf, &nvBuffer, NULL, -1);
if (ret < 0) {
cout << "Error DQing buffer at output plane" << std::endl;
return false;
}
}
memcpy(nvBuffer->planes[0].data,frame->payload[0],frame->payload_size[0]);
memcpy(nvBuffer->planes[1].data,frame->payload[1],frame->payload_size[1]);
memcpy(nvBuffer->planes[2].data,frame->payload[2],frame->payload_size[2]);
nvBuffer->planes[0].bytesused=frame->payload_size[0];
nvBuffer->planes[1].bytesused=frame->payload_size[1];
nvBuffer->planes[2].bytesused=frame->payload_size[2];
v4l2_buf.flags |= V4L2_BUF_FLAG_TIMESTAMP_COPY;
v4l2_buf.timestamp.tv_usec = frame->timestamp % 1000000;
v4l2_buf.timestamp.tv_sec = frame->timestamp / 1000000;
ret = ctx->enc->output_plane.qBuffer(v4l2_buf, NULL);
TEST_ERROR(ret < 0, "Error while queueing buffer at output plane", ret);
return 0;
}
int nvmpi_encoder_get_packet(nvmpictx* ctx,nvPacket* packet){
int ret,packet_index;
if(ctx->packet_pools->empty())
return -1;
packet_index= ctx->packet_pools->front();
auto ts = ctx->timestamp[packet_index];
auto size = ctx->packets_size[packet_index];
if((ts > 0) && (size == 0)) // Old packet, but 0-0 skip!
{
return -1;
}
packet->payload=ctx->packets[packet_index];
packet->pts=ts;
packet->payload_size=size;
if(ctx->packets_keyflag[packet_index])
packet->flags|= 0x0001;//AV_PKT_FLAG_KEY 0x0001
ctx->packets_size[packet_index] = 0; // mark as readed
ctx->packet_pools->pop();
return 0;
}
int nvmpi_encoder_close(nvmpictx* ctx){
ctx->enc->capture_plane.stopDQThread();
ctx->enc->capture_plane.waitForDQThread(1000);
delete ctx->enc;
delete ctx->packet_pools;
delete ctx;
}