-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv_mpp.cpp
443 lines (368 loc) · 12.8 KB
/
cv_mpp.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
/******
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include <mpp.h>
#include <rk_mpi.h>
#include <mpp_enc_cfg.h>
#include <mpp_env.h>
#include <mpp_common.h>
#include <mpp_frame.h>
typedef int BOOL;
#define TRUE 1
#define FALSE 0
#define TARGET_W 1920
#define TARGET_H 1080
#define FPS 25
MppBufferGroup mppBufGrp = nullptr;
size_t convertToMppFrame(const cv::Mat &cvFrame, MppFrame &mppFrame, BOOL isLastFrame = FALSE) {
// Convert BGR to RGB
cv::Mat rgbFrame;
cv::cvtColor(cvFrame, rgbFrame, cv::COLOR_BGR2RGB);
// Create an MppFrame object
mppFrame = nullptr;
mpp_frame_init(&mppFrame);
// Get frame dimensions
int width = rgbFrame.cols;
int height = rgbFrame.rows;
MppFrameFormat fmt = MPP_FMT_RGB888;
// Set frame dimensions and format
mpp_frame_set_width(mppFrame, width);
mpp_frame_set_height(mppFrame, height);
mpp_frame_set_hor_stride(mppFrame, TARGET_W);
mpp_frame_set_ver_stride(mppFrame, TARGET_H);
mpp_frame_set_fmt(mppFrame, fmt); // Assuming RGB format
mpp_frame_set_eos(mppFrame, isLastFrame);
// Allocate an MppBuffer to hold the frame data
MppBuffer mppBuffer;
size_t frame_size = MPP_ALIGN(TARGET_W, 64) * MPP_ALIGN(TARGET_H, 64);
size_t header_size = 0;
if (MPP_FRAME_FMT_IS_FBC(fmt)) {
if ((fmt & MPP_FRAME_FBC_MASK) == MPP_FRAME_FBC_AFBC_V1)
header_size = MPP_ALIGN(MPP_ALIGN(TARGET_W, 16) * MPP_ALIGN(TARGET_H, 16) / 16, SZ_4K);
else
header_size = MPP_ALIGN(TARGET_W, 16) * MPP_ALIGN(TARGET_H, 16) / 16;
} else {
header_size = 0;
}
MPP_RET ret = mpp_buffer_get(mppBufGrp, &mppBuffer, frame_size + header_size);
if (ret != MPP_OK)
{
printf("Failed to mpp_buffer_get\n");
printf("Error: %d\n", ret);
return 4;
}
// Copy data from rgbFrame to mppBuffer
memcpy(mpp_buffer_get_ptr(mppBuffer), rgbFrame.data, bufferSize);
// Attach the MppBuffer to the MppFrame
mpp_frame_set_buffer(mppFrame, mppBuffer);
return bufferSize;
}
int main(int argc, char *argv[]) {
printf("Start Application ...\n");
MPP_RET mpp_ret = MPP_OK;
char output_filename[] = "output.mp4";
// Remove the output file if it already exists
if (access(output_filename, F_OK) == 0) {
remove(output_filename);
}
// Open the output file
FILE *fp = fopen(output_filename, "ab+");
if (fp == NULL) {
printf("Failed to open output file: %s\n", output_filename);
return 3;
}
printf("Creating MPP API Context ...\n");
// Initialize Rockchip MPP
MppCtx ctx;
MppApi *mpi;
mpp_ret = mpp_create(&ctx, &mpi);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_create\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
// Initialize MPP for encoding
printf("Initialize MPP Encoder ...\n");
mpp_ret = mpp_init(ctx, MPP_CTX_ENC, MPP_VIDEO_CodingAVC);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_init\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
printf("Initialize MppBufferGroup ...\n");
mpp_ret = mpp_buffer_group_get_internal(&mppBufGrp, MPP_BUFFER_TYPE_DMA_HEAP);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_buffer_group_get_internal\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_buffer_group_limit_config(mppBufGrp, 0, 20);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_buffer_group_limit_config\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
printf("Set MPP Encoder Configuration ...\n");
// Set encoding parameters (assuming an API similar to FFmpeg)
// This is hypothetical and may not match the exact MPP API
// Initialize the encoder configuration structure
MppEncCfg mppEncCfg;
mpp_ret = mpp_enc_cfg_init(&mppEncCfg);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_init\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "prep:hor_stride", TARGET_W);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [prep:hor_stride]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "prep:ver_stride", TARGET_H);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [prep:ver_stride]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "prep:format", MPP_FMT_RGB888);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [prep:format]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:rc_mode", MPP_ENC_RC_MODE_CBR);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:rc_mode]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:quality", MPP_ENC_RC_QUALITY_BEST);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:quality]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
/* drop frame or not when bitrate overflow */
mpp_ret = mpp_enc_cfg_set_u32(mppEncCfg, "rc:drop_mode", MPP_ENC_RC_DROP_FRM_DISABLED);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_u32 [rc:drop_mode]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_u32(mppEncCfg, "rc:drop_thd", 20); /* 20% of max bps */
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_u32 [rc:drop_thd]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_u32(mppEncCfg, "rc:drop_gap", 1); /* Do not continuous drop frame */
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_u32 [rc:drop_gap]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:bps_target", 4 * 1024 * 1024);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:bps_target]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:bps_max", 4 * 1024 * 1024);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:bps_max]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:bps_min", 4 * 1024 * 1024);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:bps_min]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:fps_in_num", FPS);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:fps_in_num]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:fps_in_denorm", 1);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:fps_in_denorm]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:fps_out_num", FPS);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:fps_out_num]\n");
printf("Error: %d\n", mpp_ret);
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "rc:fps_out_denorm", 1);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [rc:fps_out_denorm]\n");
printf("Error: %d\n", mpp_ret);
}
mpp_ret = mpp_enc_cfg_set_s32(mppEncCfg, "codec:type", MPP_VIDEO_CodingAVC);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_set_s32 [codec:type]\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
RK_U32 constraint_set;
/*
* H.264 profile_idc parameter
* 66 - Baseline profile
* 77 - Main profile
* 100 - High profile
*/
mpp_enc_cfg_set_s32(mppEncCfg, "h264:profile", 100);
/*
* H.264 level_idc parameter
* 10 / 11 / 12 / 13 - qcif@15fps / [email protected] / cif@15fps / cif@30fps
* 20 / 21 / 22 - cif@30fps / half-D1@@25fps / [email protected]
* 30 / 31 / 32 - D1@25fps / 720p@30fps / 720p@60fps
* 40 / 41 / 42 - 1080p@30fps / 1080p@30fps / 1080p@60fps
* 50 / 51 / 52 - 4K@30fps
*/
mpp_enc_cfg_set_s32(mppEncCfg, "h264:level", 40);
mpp_enc_cfg_set_s32(mppEncCfg, "h264:cabac_en", 1);
mpp_enc_cfg_set_s32(mppEncCfg, "h264:cabac_idc", 0);
mpp_enc_cfg_set_s32(mppEncCfg, "h264:trans8x8", 1);
mpp_env_get_u32("constraint_set", &constraint_set, 0);
if (constraint_set & 0x3f0000)
mpp_enc_cfg_set_s32(mppEncCfg, "h264:constraint_set", constraint_set);
// Set the encoder configuration
mpp_ret = mpi->control(ctx, MPP_ENC_SET_CFG, mppEncCfg);
if (mpp_ret != MPP_OK)
{
printf("Failed to set MPP encoder configuration\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
int frame_idx = 0;
printf("Start Video loop ...\n");
// if press Ctrl+C, exit loop
while (true) {
frame_idx += 1;
// create an empty frame with size 1920x1080x3
cv::Mat frame = cv::Mat::zeros(TARGET_H, TARGET_W, CV_8UC3);
// write yellow text frame_idx in the center of frame
char text[256] = {0};
snprintf(text, sizeof(text), "frame_idx: %d", frame_idx);
cv::putText(frame, text, cv::Point(TARGET_W/2-100, TARGET_H/2), cv::FONT_HERSHEY_SIMPLEX, 2, cv::Scalar(0, 255, 255), 2);
// Assume MPP requires a specific buffer format
size_t in_bufsize = frame.total() * frame.elemSize() * 2;
printf("frame.total() = %ld, frame.elemSize() = %ld\n", frame.total(), frame.elemSize());
printf("converting cv_frame to mpp_frame ... %ld \n", in_bufsize);
MppFrame mpp_frame = nullptr;
size_t frame_size = convertToMppFrame(frame, mpp_frame, (BOOL)(frame_idx >= 100) );
printf("[Converting Done]\n");
// Encode the frame using MPP
printf("Encoding frame ... \n");
MppPacket packet = NULL;
printf("encode_put_frame ... \n");
mpp_ret = mpi->encode_put_frame(ctx, mpp_frame);
if (mpp_ret != MPP_OK)
{
printf("Failed to encode_put_frame\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
printf("Deinitialize MPP Frame ...\n");
mpp_ret = mpp_frame_deinit(&mpp_frame);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_frame_deinit\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
printf("encode_get_packet ... \n");
mpp_ret = mpi->encode_get_packet(ctx, &packet);
if (mpp_ret != MPP_OK)
{
printf("Failed to encode_get_packet\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
printf("[Encoding Done]\n");
if (packet) {
// write to file
//unsigned char* bufdata = (unsigned char*)mpp_packet_get_data(packet);
void *bufdata = mpp_packet_get_pos(packet); // Get the data pointer from the packet.
size_t buflen = mpp_packet_get_length(packet); // Get the data size from the packet.
// Write the packet to the output file
fwrite(bufdata, 1, buflen, fp);
printf("Wrote %zu bytes\n", buflen);
// Release the packet
mpp_ret = mpp_packet_deinit(&packet);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_packet_deinit\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
}
// frame_idx > 20, break
if (frame_idx > 100) {
break;
}
}
MPP_TEST_OUT:
printf("Video loop exited ...\n");
mpp_buffer_group_put(mppBufGrp);
// Finalize MPP Encoder
printf("Finalize MPP Encoder ...\n");
mpp_ret = mpp_enc_cfg_deinit(mppEncCfg);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_enc_cfg_deinit\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
// Finalize MPP and release resources
printf("Finalize MPP API Context ...\n");
mpp_ret = mpi->reset(ctx);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpi->reset\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
mpp_ret = mpp_destroy(ctx);
if (mpp_ret != MPP_OK)
{
printf("Failed to mpp_destroy\n");
printf("Error: %d\n", mpp_ret);
return 4;
}
// Close the output file
if (fp) fclose(fp);
printf("Application exited ...\n");
return 0;
}